mshtml: Added IHTMLObjectElement::name property implementation.
[wine] / dlls / jscript / jscript.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <assert.h>
20
21 #include "jscript.h"
22 #include "engine.h"
23 #include "objsafe.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28
29 #ifdef _WIN64
30
31 #define CTXARG_T DWORDLONG
32 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
33 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_64Vtbl
34
35 #else
36
37 #define CTXARG_T DWORD
38 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
39 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_32Vtbl
40
41 #endif
42
43 typedef struct {
44     IActiveScript                IActiveScript_iface;
45     IActiveScriptParse           IActiveScriptParse_iface;
46     IActiveScriptParseProcedure2 IActiveScriptParseProcedure2_iface;
47     IActiveScriptProperty        IActiveScriptProperty_iface;
48     IObjectSafety                IObjectSafety_iface;
49     IVariantChangeType           IVariantChangeType_iface;
50
51     LONG ref;
52
53     DWORD safeopt;
54     script_ctx_t *ctx;
55     LONG thread_id;
56     LCID lcid;
57     DWORD version;
58     BOOL is_encode;
59
60     IActiveScriptSite *site;
61
62     bytecode_t *queue_head;
63     bytecode_t *queue_tail;
64 } JScript;
65
66 void script_release(script_ctx_t *ctx)
67 {
68     if(--ctx->ref)
69         return;
70
71     clear_ei(ctx);
72     if(ctx->cc)
73         release_cc(ctx->cc);
74     jsheap_free(&ctx->tmp_heap);
75     if(ctx->last_match)
76         jsstr_release(ctx->last_match);
77
78     ctx->jscaller->ctx = NULL;
79     IServiceProvider_Release(&ctx->jscaller->IServiceProvider_iface);
80
81     heap_free(ctx);
82 }
83
84 static void change_state(JScript *This, SCRIPTSTATE state)
85 {
86     if(This->ctx->state == state)
87         return;
88
89     This->ctx->state = state;
90     if(This->site)
91         IActiveScriptSite_OnStateChange(This->site, state);
92 }
93
94 static inline BOOL is_started(script_ctx_t *ctx)
95 {
96     return ctx->state == SCRIPTSTATE_STARTED
97         || ctx->state == SCRIPTSTATE_CONNECTED
98         || ctx->state == SCRIPTSTATE_DISCONNECTED;
99 }
100
101 static HRESULT exec_global_code(JScript *This, bytecode_t *code)
102 {
103     exec_ctx_t *exec_ctx;
104     HRESULT hres;
105
106     hres = create_exec_ctx(This->ctx, NULL, This->ctx->global, NULL, TRUE, &exec_ctx);
107     if(FAILED(hres))
108         return hres;
109
110     IActiveScriptSite_OnEnterScript(This->site);
111
112     clear_ei(This->ctx);
113     hres = exec_source(exec_ctx, code, &code->global_code, FALSE, NULL);
114     exec_release(exec_ctx);
115
116     IActiveScriptSite_OnLeaveScript(This->site);
117     return hres;
118 }
119
120 static void clear_script_queue(JScript *This)
121 {
122     bytecode_t *iter, *iter2;
123
124     if(!This->queue_head)
125         return;
126
127     iter = This->queue_head;
128     while(iter) {
129         iter2 = iter->next;
130         iter->next = NULL;
131         release_bytecode(iter);
132         iter = iter2;
133     }
134
135     This->queue_head = This->queue_tail = NULL;
136 }
137
138 static void exec_queued_code(JScript *This)
139 {
140     bytecode_t *iter;
141
142     for(iter = This->queue_head; iter; iter = iter->next)
143         exec_global_code(This, iter);
144
145     clear_script_queue(This);
146 }
147
148 static HRESULT set_ctx_site(JScript *This)
149 {
150     HRESULT hres;
151
152     This->ctx->lcid = This->lcid;
153
154     hres = init_global(This->ctx);
155     if(FAILED(hres))
156         return hres;
157
158     IActiveScriptSite_AddRef(This->site);
159     This->ctx->site = This->site;
160
161     change_state(This, SCRIPTSTATE_INITIALIZED);
162     return S_OK;
163 }
164
165 static void decrease_state(JScript *This, SCRIPTSTATE state)
166 {
167     if(This->ctx) {
168         switch(This->ctx->state) {
169         case SCRIPTSTATE_CONNECTED:
170             change_state(This, SCRIPTSTATE_DISCONNECTED);
171             if(state == SCRIPTSTATE_DISCONNECTED)
172                 return;
173             /* FALLTHROUGH */
174         case SCRIPTSTATE_STARTED:
175         case SCRIPTSTATE_DISCONNECTED:
176             clear_script_queue(This);
177
178             if(This->ctx->state == SCRIPTSTATE_DISCONNECTED)
179                 change_state(This, SCRIPTSTATE_INITIALIZED);
180             if(state == SCRIPTSTATE_INITIALIZED)
181                 return;
182             /* FALLTHROUGH */
183         case SCRIPTSTATE_INITIALIZED:
184             if(This->ctx->host_global) {
185                 IDispatch_Release(This->ctx->host_global);
186                 This->ctx->host_global = NULL;
187             }
188
189             if(This->ctx->named_items) {
190                 named_item_t *iter, *iter2;
191
192                 iter = This->ctx->named_items;
193                 while(iter) {
194                     iter2 = iter->next;
195
196                     if(iter->disp)
197                         IDispatch_Release(iter->disp);
198                     heap_free(iter->name);
199                     heap_free(iter);
200                     iter = iter2;
201                 }
202
203                 This->ctx->named_items = NULL;
204             }
205
206             if(This->ctx->secmgr) {
207                 IInternetHostSecurityManager_Release(This->ctx->secmgr);
208                 This->ctx->secmgr = NULL;
209             }
210
211             if(This->ctx->site) {
212                 IActiveScriptSite_Release(This->ctx->site);
213                 This->ctx->site = NULL;
214             }
215
216             if(This->ctx->global) {
217                 jsdisp_release(This->ctx->global);
218                 This->ctx->global = NULL;
219             }
220             /* FALLTHROUGH */
221         case SCRIPTSTATE_UNINITIALIZED:
222             change_state(This, state);
223             break;
224         default:
225             assert(0);
226         }
227
228         change_state(This, state);
229     }else if(state == SCRIPTSTATE_UNINITIALIZED) {
230         if(This->site)
231             IActiveScriptSite_OnStateChange(This->site, state);
232     }else {
233         FIXME("NULL ctx\n");
234     }
235
236     if(state == SCRIPTSTATE_UNINITIALIZED)
237         This->thread_id = 0;
238
239     if(This->site) {
240         IActiveScriptSite_Release(This->site);
241         This->site = NULL;
242     }
243 }
244
245 typedef struct {
246     IServiceProvider IServiceProvider_iface;
247
248     LONG ref;
249
250     IServiceProvider *sp;
251 } AXSite;
252
253 static inline AXSite *impl_from_IServiceProvider(IServiceProvider *iface)
254 {
255     return CONTAINING_RECORD(iface, AXSite, IServiceProvider_iface);
256 }
257
258 static HRESULT WINAPI AXSite_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
259 {
260     AXSite *This = impl_from_IServiceProvider(iface);
261
262     if(IsEqualGUID(&IID_IUnknown, riid)) {
263         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
264         *ppv = &This->IServiceProvider_iface;
265     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
266         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
267         *ppv = &This->IServiceProvider_iface;
268     }else {
269         TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
270         *ppv = NULL;
271         return E_NOINTERFACE;
272     }
273
274     IUnknown_AddRef((IUnknown*)*ppv);
275     return S_OK;
276 }
277
278 static ULONG WINAPI AXSite_AddRef(IServiceProvider *iface)
279 {
280     AXSite *This = impl_from_IServiceProvider(iface);
281     LONG ref = InterlockedIncrement(&This->ref);
282
283     TRACE("(%p) ref=%d\n", This, ref);
284
285     return ref;
286 }
287
288 static ULONG WINAPI AXSite_Release(IServiceProvider *iface)
289 {
290     AXSite *This = impl_from_IServiceProvider(iface);
291     LONG ref = InterlockedDecrement(&This->ref);
292
293     TRACE("(%p) ref=%d\n", This, ref);
294
295     if(!ref)
296     {
297         if(This->sp)
298             IServiceProvider_Release(This->sp);
299
300         heap_free(This);
301     }
302
303     return ref;
304 }
305
306 static HRESULT WINAPI AXSite_QueryService(IServiceProvider *iface,
307         REFGUID guidService, REFIID riid, void **ppv)
308 {
309     AXSite *This = impl_from_IServiceProvider(iface);
310
311     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
312
313     if(!This->sp)
314         return E_NOINTERFACE;
315
316     return IServiceProvider_QueryService(This->sp, guidService, riid, ppv);
317 }
318
319 static IServiceProviderVtbl AXSiteVtbl = {
320     AXSite_QueryInterface,
321     AXSite_AddRef,
322     AXSite_Release,
323     AXSite_QueryService
324 };
325
326 IUnknown *create_ax_site(script_ctx_t *ctx)
327 {
328     IServiceProvider *sp = NULL;
329     AXSite *ret;
330     HRESULT hres;
331
332     hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
333     if(FAILED(hres)) {
334         TRACE("Could not get IServiceProvider iface: %08x\n", hres);
335     }
336
337     ret = heap_alloc(sizeof(AXSite));
338     if(!ret) {
339         IServiceProvider_Release(sp);
340         return NULL;
341     }
342
343     ret->IServiceProvider_iface.lpVtbl = &AXSiteVtbl;
344     ret->ref = 1;
345     ret->sp = sp;
346
347     return (IUnknown*)&ret->IServiceProvider_iface;
348 }
349
350 static inline JScript *impl_from_IActiveScript(IActiveScript *iface)
351 {
352     return CONTAINING_RECORD(iface, JScript, IActiveScript_iface);
353 }
354
355 static HRESULT WINAPI JScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
356 {
357     JScript *This = impl_from_IActiveScript(iface);
358
359     *ppv = NULL;
360
361     if(IsEqualGUID(riid, &IID_IUnknown)) {
362         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
363         *ppv = &This->IActiveScript_iface;
364     }else if(IsEqualGUID(riid, &IID_IActiveScript)) {
365         TRACE("(%p)->(IID_IActiveScript %p)\n", This, ppv);
366         *ppv = &This->IActiveScript_iface;
367     }else if(IsEqualGUID(riid, &IID_IActiveScriptParse)) {
368         TRACE("(%p)->(IID_IActiveScriptParse %p)\n", This, ppv);
369         *ppv = &This->IActiveScriptParse_iface;
370     }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure)) {
371         TRACE("(%p)->(IID_IActiveScriptParseProcedure %p)\n", This, ppv);
372         *ppv = &This->IActiveScriptParseProcedure2_iface;
373     }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure2)) {
374         TRACE("(%p)->(IID_IActiveScriptParseProcedure2 %p)\n", This, ppv);
375         *ppv = &This->IActiveScriptParseProcedure2_iface;
376     }else if(IsEqualGUID(riid, &IID_IActiveScriptProperty)) {
377         TRACE("(%p)->(IID_IActiveScriptProperty %p)\n", This, ppv);
378         *ppv = &This->IActiveScriptProperty_iface;
379     }else if(IsEqualGUID(riid, &IID_IObjectSafety)) {
380         TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
381         *ppv = &This->IObjectSafety_iface;
382     }else if(IsEqualGUID(riid, &IID_IVariantChangeType)) {
383         TRACE("(%p)->(IID_IVariantChangeType %p)\n", This, ppv);
384         *ppv = &This->IVariantChangeType_iface;
385     }
386
387     if(*ppv) {
388         IUnknown_AddRef((IUnknown*)*ppv);
389         return S_OK;
390     }
391
392     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
393     return E_NOINTERFACE;
394 }
395
396 static ULONG WINAPI JScript_AddRef(IActiveScript *iface)
397 {
398     JScript *This = impl_from_IActiveScript(iface);
399     LONG ref = InterlockedIncrement(&This->ref);
400
401     TRACE("(%p) ref=%d\n", This, ref);
402
403     return ref;
404 }
405
406 static ULONG WINAPI JScript_Release(IActiveScript *iface)
407 {
408     JScript *This = impl_from_IActiveScript(iface);
409     LONG ref = InterlockedDecrement(&This->ref);
410
411     TRACE("(%p) ref=%d\n", iface, ref);
412
413     if(!ref) {
414         if(This->ctx && This->ctx->state != SCRIPTSTATE_CLOSED)
415             IActiveScript_Close(&This->IActiveScript_iface);
416         if(This->ctx) {
417             This->ctx->active_script = NULL;
418             script_release(This->ctx);
419         }
420         heap_free(This);
421         unlock_module();
422     }
423
424     return ref;
425 }
426
427 static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface,
428                                             IActiveScriptSite *pass)
429 {
430     JScript *This = impl_from_IActiveScript(iface);
431     LCID lcid;
432     HRESULT hres;
433
434     TRACE("(%p)->(%p)\n", This, pass);
435
436     if(!pass)
437         return E_POINTER;
438
439     if(This->site)
440         return E_UNEXPECTED;
441
442     if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0))
443         return E_UNEXPECTED;
444
445     This->site = pass;
446     IActiveScriptSite_AddRef(This->site);
447
448     hres = IActiveScriptSite_GetLCID(This->site, &lcid);
449     if(hres == S_OK)
450         This->lcid = lcid;
451
452     return This->ctx ? set_ctx_site(This) : S_OK;
453 }
454
455 static HRESULT WINAPI JScript_GetScriptSite(IActiveScript *iface, REFIID riid,
456                                             void **ppvObject)
457 {
458     JScript *This = impl_from_IActiveScript(iface);
459     FIXME("(%p)->()\n", This);
460     return E_NOTIMPL;
461 }
462
463 static HRESULT WINAPI JScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
464 {
465     JScript *This = impl_from_IActiveScript(iface);
466
467     TRACE("(%p)->(%d)\n", This, ss);
468
469     if(This->thread_id && GetCurrentThreadId() != This->thread_id)
470         return E_UNEXPECTED;
471
472     if(ss == SCRIPTSTATE_UNINITIALIZED) {
473         if(This->ctx && This->ctx->state == SCRIPTSTATE_CLOSED)
474             return E_UNEXPECTED;
475
476         decrease_state(This, SCRIPTSTATE_UNINITIALIZED);
477         return S_OK;
478     }
479
480     if(!This->ctx)
481         return E_UNEXPECTED;
482
483     switch(ss) {
484     case SCRIPTSTATE_STARTED:
485     case SCRIPTSTATE_CONNECTED: /* FIXME */
486         if(This->ctx->state == SCRIPTSTATE_CLOSED)
487             return E_UNEXPECTED;
488
489         exec_queued_code(This);
490         break;
491     case SCRIPTSTATE_INITIALIZED:
492         FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
493         return S_OK;
494     default:
495         FIXME("unimplemented state %d\n", ss);
496         return E_NOTIMPL;
497     }
498
499     change_state(This, ss);
500     return S_OK;
501 }
502
503 static HRESULT WINAPI JScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
504 {
505     JScript *This = impl_from_IActiveScript(iface);
506
507     TRACE("(%p)->(%p)\n", This, pssState);
508
509     if(!pssState)
510         return E_POINTER;
511
512     if(This->thread_id && This->thread_id != GetCurrentThreadId())
513         return E_UNEXPECTED;
514
515     *pssState = This->ctx ? This->ctx->state : SCRIPTSTATE_UNINITIALIZED;
516     return S_OK;
517 }
518
519 static HRESULT WINAPI JScript_Close(IActiveScript *iface)
520 {
521     JScript *This = impl_from_IActiveScript(iface);
522
523     TRACE("(%p)->()\n", This);
524
525     if(This->thread_id && This->thread_id != GetCurrentThreadId())
526         return E_UNEXPECTED;
527
528     decrease_state(This, SCRIPTSTATE_CLOSED);
529     return S_OK;
530 }
531
532 static HRESULT WINAPI JScript_AddNamedItem(IActiveScript *iface,
533                                            LPCOLESTR pstrName, DWORD dwFlags)
534 {
535     JScript *This = impl_from_IActiveScript(iface);
536     named_item_t *item;
537     IDispatch *disp = NULL;
538     HRESULT hres;
539
540     TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
541
542     if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->ctx->state == SCRIPTSTATE_CLOSED)
543         return E_UNEXPECTED;
544
545     if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
546         IUnknown *unk;
547
548         hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
549         if(FAILED(hres)) {
550             WARN("GetItemInfo failed: %08x\n", hres);
551             return hres;
552         }
553
554         hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
555         IUnknown_Release(unk);
556         if(FAILED(hres)) {
557             WARN("object does not implement IDispatch\n");
558             return hres;
559         }
560
561         if(This->ctx->host_global)
562             IDispatch_Release(This->ctx->host_global);
563         IDispatch_AddRef(disp);
564         This->ctx->host_global = disp;
565     }
566
567     item = heap_alloc(sizeof(*item));
568     if(!item) {
569         if(disp)
570             IDispatch_Release(disp);
571         return E_OUTOFMEMORY;
572     }
573
574     item->disp = disp;
575     item->flags = dwFlags;
576     item->name = heap_strdupW(pstrName);
577     if(!item->name) {
578         if(disp)
579             IDispatch_Release(disp);
580         heap_free(item);
581         return E_OUTOFMEMORY;
582     }
583
584     item->next = This->ctx->named_items;
585     This->ctx->named_items = item;
586
587     return S_OK;
588 }
589
590 static HRESULT WINAPI JScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
591                                          DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
592 {
593     JScript *This = impl_from_IActiveScript(iface);
594     FIXME("(%p)->()\n", This);
595     return E_NOTIMPL;
596 }
597
598 static HRESULT WINAPI JScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
599                                                 IDispatch **ppdisp)
600 {
601     JScript *This = impl_from_IActiveScript(iface);
602
603     TRACE("(%p)->(%p)\n", This, ppdisp);
604
605     if(!ppdisp)
606         return E_POINTER;
607
608     if(This->thread_id != GetCurrentThreadId() || !This->ctx->global) {
609         *ppdisp = NULL;
610         return E_UNEXPECTED;
611     }
612
613     *ppdisp = to_disp(This->ctx->global);
614     IDispatch_AddRef(*ppdisp);
615     return S_OK;
616 }
617
618 static HRESULT WINAPI JScript_GetCurrentScriptThreadID(IActiveScript *iface,
619                                                        SCRIPTTHREADID *pstridThread)
620 {
621     JScript *This = impl_from_IActiveScript(iface);
622     FIXME("(%p)->()\n", This);
623     return E_NOTIMPL;
624 }
625
626 static HRESULT WINAPI JScript_GetScriptThreadID(IActiveScript *iface,
627                                                 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
628 {
629     JScript *This = impl_from_IActiveScript(iface);
630     FIXME("(%p)->()\n", This);
631     return E_NOTIMPL;
632 }
633
634 static HRESULT WINAPI JScript_GetScriptThreadState(IActiveScript *iface,
635         SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
636 {
637     JScript *This = impl_from_IActiveScript(iface);
638     FIXME("(%p)->()\n", This);
639     return E_NOTIMPL;
640 }
641
642 static HRESULT WINAPI JScript_InterruptScriptThread(IActiveScript *iface,
643         SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
644 {
645     JScript *This = impl_from_IActiveScript(iface);
646     FIXME("(%p)->()\n", This);
647     return E_NOTIMPL;
648 }
649
650 static HRESULT WINAPI JScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
651 {
652     JScript *This = impl_from_IActiveScript(iface);
653     FIXME("(%p)->()\n", This);
654     return E_NOTIMPL;
655 }
656
657 static const IActiveScriptVtbl JScriptVtbl = {
658     JScript_QueryInterface,
659     JScript_AddRef,
660     JScript_Release,
661     JScript_SetScriptSite,
662     JScript_GetScriptSite,
663     JScript_SetScriptState,
664     JScript_GetScriptState,
665     JScript_Close,
666     JScript_AddNamedItem,
667     JScript_AddTypeLib,
668     JScript_GetScriptDispatch,
669     JScript_GetCurrentScriptThreadID,
670     JScript_GetScriptThreadID,
671     JScript_GetScriptThreadState,
672     JScript_InterruptScriptThread,
673     JScript_Clone
674 };
675
676 static inline JScript *impl_from_IActiveScriptParse(IActiveScriptParse *iface)
677 {
678     return CONTAINING_RECORD(iface, JScript, IActiveScriptParse_iface);
679 }
680
681 static HRESULT WINAPI JScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
682 {
683     JScript *This = impl_from_IActiveScriptParse(iface);
684     return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
685 }
686
687 static ULONG WINAPI JScriptParse_AddRef(IActiveScriptParse *iface)
688 {
689     JScript *This = impl_from_IActiveScriptParse(iface);
690     return IActiveScript_AddRef(&This->IActiveScript_iface);
691 }
692
693 static ULONG WINAPI JScriptParse_Release(IActiveScriptParse *iface)
694 {
695     JScript *This = impl_from_IActiveScriptParse(iface);
696     return IActiveScript_Release(&This->IActiveScript_iface);
697 }
698
699 static HRESULT WINAPI JScriptParse_InitNew(IActiveScriptParse *iface)
700 {
701     JScript *This = impl_from_IActiveScriptParse(iface);
702     script_ctx_t *ctx;
703     HRESULT hres;
704
705     TRACE("(%p)\n", This);
706
707     if(This->ctx)
708         return E_UNEXPECTED;
709
710     ctx = heap_alloc_zero(sizeof(script_ctx_t));
711     if(!ctx)
712         return E_OUTOFMEMORY;
713
714     ctx->ref = 1;
715     ctx->state = SCRIPTSTATE_UNINITIALIZED;
716     ctx->active_script = &This->IActiveScript_iface;
717     ctx->safeopt = This->safeopt;
718     ctx->version = This->version;
719     ctx->ei.val = jsval_undefined();
720     jsheap_init(&ctx->tmp_heap);
721
722     hres = create_jscaller(ctx);
723     if(FAILED(hres)) {
724         heap_free(ctx);
725         return hres;
726     }
727
728     ctx->last_match = jsstr_empty();
729
730     ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
731     if(ctx) {
732         script_release(ctx);
733         return E_UNEXPECTED;
734     }
735
736     return This->site ? set_ctx_site(This) : S_OK;
737 }
738
739 static HRESULT WINAPI JScriptParse_AddScriptlet(IActiveScriptParse *iface,
740         LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
741         LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
742         CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
743         BSTR *pbstrName, EXCEPINFO *pexcepinfo)
744 {
745     JScript *This = impl_from_IActiveScriptParse(iface);
746     FIXME("(%p)->(%s %s %s %s %s %s %s %u %x %p %p)\n", This, debugstr_w(pstrDefaultName),
747           debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
748           debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
749           ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
750     return E_NOTIMPL;
751 }
752
753 static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface,
754         LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
755         LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
756         DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
757 {
758     JScript *This = impl_from_IActiveScriptParse(iface);
759     bytecode_t *code;
760     HRESULT hres;
761
762     TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode),
763           debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
764           wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
765
766     if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
767         return E_UNEXPECTED;
768
769     hres = compile_script(This->ctx, pstrCode, NULL, pstrDelimiter, FALSE, This->is_encode, &code);
770     if(FAILED(hres))
771         return hres;
772
773     if(!is_started(This->ctx)) {
774         if(This->queue_tail)
775             This->queue_tail = This->queue_tail->next = code;
776         else
777             This->queue_head = This->queue_tail = code;
778         return S_OK;
779     }
780
781     hres = exec_global_code(This, code);
782
783     release_bytecode(code);
784     return hres;
785 }
786
787 static const IActiveScriptParseVtbl JScriptParseVtbl = {
788     JScriptParse_QueryInterface,
789     JScriptParse_AddRef,
790     JScriptParse_Release,
791     JScriptParse_InitNew,
792     JScriptParse_AddScriptlet,
793     JScriptParse_ParseScriptText
794 };
795
796 static inline JScript *impl_from_IActiveScriptParseProcedure2(IActiveScriptParseProcedure2 *iface)
797 {
798     return CONTAINING_RECORD(iface, JScript, IActiveScriptParseProcedure2_iface);
799 }
800
801 static HRESULT WINAPI JScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
802 {
803     JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
804     return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
805 }
806
807 static ULONG WINAPI JScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
808 {
809     JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
810     return IActiveScript_AddRef(&This->IActiveScript_iface);
811 }
812
813 static ULONG WINAPI JScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
814 {
815     JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
816     return IActiveScript_Release(&This->IActiveScript_iface);
817 }
818
819 static HRESULT WINAPI JScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
820         LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
821         LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
822         CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
823 {
824     JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
825     bytecode_t *code;
826     jsdisp_t *dispex;
827     HRESULT hres;
828
829     TRACE("(%p)->(%s %s %s %s %p %s %s %u %x %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrFormalParams),
830           debugstr_w(pstrProcedureName), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
831           wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLineNumber, dwFlags, ppdisp);
832
833     if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
834         return E_UNEXPECTED;
835
836     hres = compile_script(This->ctx, pstrCode, pstrFormalParams, pstrDelimiter, FALSE, This->is_encode, &code);
837     if(FAILED(hres)) {
838         WARN("Parse failed %08x\n", hres);
839         return hres;
840     }
841
842     hres = create_source_function(This->ctx, code, &code->global_code, NULL,  &dispex);
843     release_bytecode(code);
844     if(FAILED(hres))
845         return hres;
846
847     *ppdisp = to_disp(dispex);
848     return S_OK;
849 }
850
851 static const IActiveScriptParseProcedure2Vtbl JScriptParseProcedureVtbl = {
852     JScriptParseProcedure_QueryInterface,
853     JScriptParseProcedure_AddRef,
854     JScriptParseProcedure_Release,
855     JScriptParseProcedure_ParseProcedureText,
856 };
857
858 static inline JScript *impl_from_IActiveScriptProperty(IActiveScriptProperty *iface)
859 {
860     return CONTAINING_RECORD(iface, JScript, IActiveScriptProperty_iface);
861 }
862
863 static HRESULT WINAPI JScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
864 {
865     JScript *This = impl_from_IActiveScriptProperty(iface);
866     return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
867 }
868
869 static ULONG WINAPI JScriptProperty_AddRef(IActiveScriptProperty *iface)
870 {
871     JScript *This = impl_from_IActiveScriptProperty(iface);
872     return IActiveScript_AddRef(&This->IActiveScript_iface);
873 }
874
875 static ULONG WINAPI JScriptProperty_Release(IActiveScriptProperty *iface)
876 {
877     JScript *This = impl_from_IActiveScriptProperty(iface);
878     return IActiveScript_Release(&This->IActiveScript_iface);
879 }
880
881 static HRESULT WINAPI JScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
882         VARIANT *pvarIndex, VARIANT *pvarValue)
883 {
884     JScript *This = impl_from_IActiveScriptProperty(iface);
885     FIXME("(%p)->(%x %p %p)\n", This, dwProperty, pvarIndex, pvarValue);
886     return E_NOTIMPL;
887 }
888
889 static HRESULT WINAPI JScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
890         VARIANT *pvarIndex, VARIANT *pvarValue)
891 {
892     JScript *This = impl_from_IActiveScriptProperty(iface);
893
894     TRACE("(%p)->(%x %s %s)\n", This, dwProperty, debugstr_variant(pvarIndex), debugstr_variant(pvarValue));
895
896     if(pvarIndex)
897         FIXME("unsupported pvarIndex\n");
898
899     switch(dwProperty) {
900     case SCRIPTPROP_INVOKEVERSIONING:
901         if(V_VT(pvarValue) != VT_I4 || V_I4(pvarValue) < 0 || V_I4(pvarValue) > 15) {
902             WARN("invalid value %s\n", debugstr_variant(pvarValue));
903             return E_INVALIDARG;
904         }
905
906         This->version = V_I4(pvarValue);
907         break;
908     default:
909         FIXME("Unimplemented property %x\n", dwProperty);
910         return E_NOTIMPL;
911     }
912
913     return S_OK;
914 }
915
916 static const IActiveScriptPropertyVtbl JScriptPropertyVtbl = {
917     JScriptProperty_QueryInterface,
918     JScriptProperty_AddRef,
919     JScriptProperty_Release,
920     JScriptProperty_GetProperty,
921     JScriptProperty_SetProperty
922 };
923
924 static inline JScript *impl_from_IObjectSafety(IObjectSafety *iface)
925 {
926     return CONTAINING_RECORD(iface, JScript, IObjectSafety_iface);
927 }
928
929 static HRESULT WINAPI JScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
930 {
931     JScript *This = impl_from_IObjectSafety(iface);
932     return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
933 }
934
935 static ULONG WINAPI JScriptSafety_AddRef(IObjectSafety *iface)
936 {
937     JScript *This = impl_from_IObjectSafety(iface);
938     return IActiveScript_AddRef(&This->IActiveScript_iface);
939 }
940
941 static ULONG WINAPI JScriptSafety_Release(IObjectSafety *iface)
942 {
943     JScript *This = impl_from_IObjectSafety(iface);
944     return IActiveScript_Release(&This->IActiveScript_iface);
945 }
946
947 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
948
949 static HRESULT WINAPI JScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
950         DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
951 {
952     JScript *This = impl_from_IObjectSafety(iface);
953
954     TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
955
956     if(!pdwSupportedOptions || !pdwEnabledOptions)
957         return E_POINTER;
958
959     *pdwSupportedOptions = SUPPORTED_OPTIONS;
960     *pdwEnabledOptions = This->safeopt;
961
962     return S_OK;
963 }
964
965 static HRESULT WINAPI JScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
966         DWORD dwOptionSetMask, DWORD dwEnabledOptions)
967 {
968     JScript *This = impl_from_IObjectSafety(iface);
969
970     TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
971
972     if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
973         return E_FAIL;
974
975     This->safeopt = (dwEnabledOptions & dwOptionSetMask) | (This->safeopt & ~dwOptionSetMask) | INTERFACE_USES_DISPEX;
976     return S_OK;
977 }
978
979 static const IObjectSafetyVtbl JScriptSafetyVtbl = {
980     JScriptSafety_QueryInterface,
981     JScriptSafety_AddRef,
982     JScriptSafety_Release,
983     JScriptSafety_GetInterfaceSafetyOptions,
984     JScriptSafety_SetInterfaceSafetyOptions
985 };
986
987 static inline JScript *impl_from_IVariantChangeType(IVariantChangeType *iface)
988 {
989     return CONTAINING_RECORD(iface, JScript, IVariantChangeType_iface);
990 }
991
992 static HRESULT WINAPI VariantChangeType_QueryInterface(IVariantChangeType *iface, REFIID riid, void **ppv)
993 {
994     JScript *This = impl_from_IVariantChangeType(iface);
995     return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
996 }
997
998 static ULONG WINAPI VariantChangeType_AddRef(IVariantChangeType *iface)
999 {
1000     JScript *This = impl_from_IVariantChangeType(iface);
1001     return IActiveScript_AddRef(&This->IActiveScript_iface);
1002 }
1003
1004 static ULONG WINAPI VariantChangeType_Release(IVariantChangeType *iface)
1005 {
1006     JScript *This = impl_from_IVariantChangeType(iface);
1007     return IActiveScript_Release(&This->IActiveScript_iface);
1008 }
1009
1010 static HRESULT WINAPI VariantChangeType_ChangeType(IVariantChangeType *iface, VARIANT *dst, VARIANT *src, LCID lcid, VARTYPE vt)
1011 {
1012     JScript *This = impl_from_IVariantChangeType(iface);
1013     VARIANT res;
1014     HRESULT hres;
1015
1016     TRACE("(%p)->(%p %p%s %x %d)\n", This, dst, src, debugstr_variant(src), lcid, vt);
1017
1018     if(!This->ctx) {
1019         FIXME("Object uninitialized\n");
1020         return E_UNEXPECTED;
1021     }
1022
1023     hres = variant_change_type(This->ctx, &res, src, vt);
1024     if(FAILED(hres))
1025         return hres;
1026
1027     hres = VariantClear(dst);
1028     if(FAILED(hres)) {
1029         VariantClear(&res);
1030         return hres;
1031     }
1032
1033     *dst = res;
1034     return S_OK;
1035 }
1036
1037 static const IVariantChangeTypeVtbl VariantChangeTypeVtbl = {
1038     VariantChangeType_QueryInterface,
1039     VariantChangeType_AddRef,
1040     VariantChangeType_Release,
1041     VariantChangeType_ChangeType
1042 };
1043
1044 HRESULT create_jscript_object(BOOL is_encode, REFIID riid, void **ppv)
1045 {
1046     JScript *ret;
1047     HRESULT hres;
1048
1049     ret = heap_alloc_zero(sizeof(*ret));
1050     if(!ret)
1051         return E_OUTOFMEMORY;
1052
1053     lock_module();
1054
1055     ret->IActiveScript_iface.lpVtbl = &JScriptVtbl;
1056     ret->IActiveScriptParse_iface.lpVtbl = &JScriptParseVtbl;
1057     ret->IActiveScriptParseProcedure2_iface.lpVtbl = &JScriptParseProcedureVtbl;
1058     ret->IActiveScriptProperty_iface.lpVtbl = &JScriptPropertyVtbl;
1059     ret->IObjectSafety_iface.lpVtbl = &JScriptSafetyVtbl;
1060     ret->IVariantChangeType_iface.lpVtbl = &VariantChangeTypeVtbl;
1061     ret->ref = 1;
1062     ret->safeopt = INTERFACE_USES_DISPEX;
1063     ret->is_encode = is_encode;
1064
1065     hres = IActiveScript_QueryInterface(&ret->IActiveScript_iface, riid, ppv);
1066     IActiveScript_Release(&ret->IActiveScript_iface);
1067     return hres;
1068 }