msxml3: Release IDispatchEx dynamic data properly.
[wine] / dlls / vbscript / vbscript.c
1 /*
2  * Copyright 2011 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
20 #include <assert.h>
21
22 #include "vbscript.h"
23 #include "objsafe.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28
29 #ifdef _WIN64
30
31 #define CTXARG_T DWORDLONG
32 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
33
34 #else
35
36 #define CTXARG_T DWORD
37 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
38
39 #endif
40
41 struct VBScript {
42     IActiveScript IActiveScript_iface;
43     IActiveScriptParse IActiveScriptParse_iface;
44     IObjectSafety IObjectSafety_iface;
45
46     LONG ref;
47
48     DWORD safeopt;
49     SCRIPTSTATE state;
50     IActiveScriptSite *site;
51     script_ctx_t *ctx;
52     LONG thread_id;
53     LCID lcid;
54 };
55
56 static void change_state(VBScript *This, SCRIPTSTATE state)
57 {
58     if(This->state == state)
59         return;
60
61     This->state = state;
62     if(This->site)
63         IActiveScriptSite_OnStateChange(This->site, state);
64 }
65
66 static inline BOOL is_started(VBScript *This)
67 {
68     return This->state == SCRIPTSTATE_STARTED
69         || This->state == SCRIPTSTATE_CONNECTED
70         || This->state == SCRIPTSTATE_DISCONNECTED;
71 }
72
73 static HRESULT exec_global_code(script_ctx_t *ctx, vbscode_t *code)
74 {
75     HRESULT hres;
76
77     code->global_executed = TRUE;
78
79     IActiveScriptSite_OnEnterScript(ctx->site);
80     hres = exec_script(ctx, &code->global_code, NULL, NULL, NULL);
81     IActiveScriptSite_OnLeaveScript(ctx->site);
82
83     return hres;
84 }
85
86 static void exec_queued_code(script_ctx_t *ctx)
87 {
88     vbscode_t *iter;
89
90     LIST_FOR_EACH_ENTRY(iter, &ctx->code_list, vbscode_t, entry) {
91         if(!iter->global_executed)
92             exec_global_code(ctx, iter);
93     }
94 }
95
96 static HRESULT set_ctx_site(VBScript *This)
97 {
98     HRESULT hres;
99
100     This->ctx->lcid = This->lcid;
101
102     hres = init_global(This->ctx);
103     if(FAILED(hres))
104         return hres;
105
106     IActiveScriptSite_AddRef(This->site);
107     This->ctx->site = This->site;
108
109     change_state(This, SCRIPTSTATE_INITIALIZED);
110     return S_OK;
111 }
112
113 static void release_script(script_ctx_t *ctx)
114 {
115     collect_objects(ctx);
116
117     release_dynamic_vars(ctx->global_vars);
118     ctx->global_vars = NULL;
119
120     while(!list_empty(&ctx->named_items)) {
121         named_item_t *iter = LIST_ENTRY(list_head(&ctx->named_items), named_item_t, entry);
122
123         list_remove(&iter->entry);
124         if(iter->disp)
125             IDispatch_Release(iter->disp);
126         heap_free(iter->name);
127         heap_free(iter);
128     }
129
130     if(ctx->host_global) {
131         IDispatch_Release(ctx->host_global);
132         ctx->host_global = NULL;
133     }
134
135     if(ctx->secmgr) {
136         IInternetHostSecurityManager_Release(ctx->secmgr);
137         ctx->secmgr = NULL;
138     }
139
140     if(ctx->site) {
141         IActiveScriptSite_Release(ctx->site);
142         ctx->site = NULL;
143     }
144
145     if(ctx->err_obj) {
146         IDispatchEx_Release(&ctx->err_obj->IDispatchEx_iface);
147         ctx->err_obj = NULL;
148     }
149
150     if(ctx->global_obj) {
151         IDispatchEx_Release(&ctx->global_obj->IDispatchEx_iface);
152         ctx->global_obj = NULL;
153     }
154
155     if(ctx->script_obj) {
156         IDispatchEx_Release(&ctx->script_obj->IDispatchEx_iface);
157         ctx->script_obj = NULL;
158     }
159
160     vbsheap_free(&ctx->heap);
161     vbsheap_init(&ctx->heap);
162 }
163
164 static void destroy_script(script_ctx_t *ctx)
165 {
166     while(!list_empty(&ctx->code_list))
167         release_vbscode(LIST_ENTRY(list_head(&ctx->code_list), vbscode_t, entry));
168
169     release_script(ctx);
170     heap_free(ctx);
171 }
172
173 static void decrease_state(VBScript *This, SCRIPTSTATE state)
174 {
175     switch(This->state) {
176     case SCRIPTSTATE_CONNECTED:
177         change_state(This, SCRIPTSTATE_DISCONNECTED);
178         if(state == SCRIPTSTATE_DISCONNECTED)
179             return;
180         /* FALLTHROUGH */
181     case SCRIPTSTATE_STARTED:
182     case SCRIPTSTATE_DISCONNECTED:
183         if(This->state == SCRIPTSTATE_DISCONNECTED)
184             change_state(This, SCRIPTSTATE_INITIALIZED);
185         if(state == SCRIPTSTATE_INITIALIZED)
186             break;
187         /* FALLTHROUGH */
188     case SCRIPTSTATE_INITIALIZED:
189     case SCRIPTSTATE_UNINITIALIZED:
190         change_state(This, state);
191
192         if(This->site) {
193             IActiveScriptSite_Release(This->site);
194             This->site = NULL;
195         }
196
197         if(This->ctx)
198             release_script(This->ctx);
199
200         This->thread_id = 0;
201         break;
202     case SCRIPTSTATE_CLOSED:
203         break;
204     default:
205         assert(0);
206     }
207 }
208
209 static inline VBScript *impl_from_IActiveScript(IActiveScript *iface)
210 {
211     return CONTAINING_RECORD(iface, VBScript, IActiveScript_iface);
212 }
213
214 static HRESULT WINAPI VBScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
215 {
216     VBScript *This = impl_from_IActiveScript(iface);
217
218     if(IsEqualGUID(riid, &IID_IUnknown)) {
219         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
220         *ppv = &This->IActiveScript_iface;
221     }else if(IsEqualGUID(riid, &IID_IActiveScript)) {
222         TRACE("(%p)->(IID_IActiveScript %p)\n", This, ppv);
223         *ppv = &This->IActiveScript_iface;
224     }else if(IsEqualGUID(riid, &IID_IActiveScriptParse)) {
225         TRACE("(%p)->(IID_IActiveScriptParse %p)\n", This, ppv);
226         *ppv = &This->IActiveScriptParse_iface;
227     }else if(IsEqualGUID(riid, &IID_IObjectSafety)) {
228         TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
229         *ppv = &This->IObjectSafety_iface;
230     }else {
231         FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
232         *ppv = NULL;
233         return E_NOINTERFACE;
234     }
235
236     IUnknown_AddRef((IUnknown*)*ppv);
237     return S_OK;
238 }
239
240 static ULONG WINAPI VBScript_AddRef(IActiveScript *iface)
241 {
242     VBScript *This = impl_from_IActiveScript(iface);
243     LONG ref = InterlockedIncrement(&This->ref);
244
245     TRACE("(%p) ref=%d\n", This, ref);
246
247     return ref;
248 }
249
250 static ULONG WINAPI VBScript_Release(IActiveScript *iface)
251 {
252     VBScript *This = impl_from_IActiveScript(iface);
253     LONG ref = InterlockedDecrement(&This->ref);
254
255     TRACE("(%p) ref=%d\n", iface, ref);
256
257     if(!ref) {
258         if(This->ctx) {
259             decrease_state(This, SCRIPTSTATE_CLOSED);
260             destroy_script(This->ctx);
261             This->ctx = NULL;
262         }
263         if(This->site)
264             IActiveScriptSite_Release(This->site);
265         heap_free(This);
266     }
267
268     return ref;
269 }
270
271 static HRESULT WINAPI VBScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass)
272 {
273     VBScript *This = impl_from_IActiveScript(iface);
274     LCID lcid;
275     HRESULT hres;
276
277     TRACE("(%p)->(%p)\n", This, pass);
278
279     if(!pass)
280         return E_POINTER;
281
282     if(This->site)
283         return E_UNEXPECTED;
284
285     if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0))
286         return E_UNEXPECTED;
287
288     This->site = pass;
289     IActiveScriptSite_AddRef(This->site);
290
291     hres = IActiveScriptSite_GetLCID(This->site, &lcid);
292     if(hres == S_OK)
293         This->lcid = lcid;
294
295     return This->ctx ? set_ctx_site(This) : S_OK;
296 }
297
298 static HRESULT WINAPI VBScript_GetScriptSite(IActiveScript *iface, REFIID riid,
299                                             void **ppvObject)
300 {
301     VBScript *This = impl_from_IActiveScript(iface);
302     FIXME("(%p)->()\n", This);
303     return E_NOTIMPL;
304 }
305
306 static HRESULT WINAPI VBScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
307 {
308     VBScript *This = impl_from_IActiveScript(iface);
309
310     TRACE("(%p)->(%d)\n", This, ss);
311
312     if(This->thread_id && GetCurrentThreadId() != This->thread_id)
313         return E_UNEXPECTED;
314
315     if(ss == SCRIPTSTATE_UNINITIALIZED) {
316         if(This->state == SCRIPTSTATE_CLOSED)
317             return E_UNEXPECTED;
318
319         decrease_state(This, SCRIPTSTATE_UNINITIALIZED);
320         return S_OK;
321     }
322
323     if(!This->ctx)
324         return E_UNEXPECTED;
325
326     switch(ss) {
327     case SCRIPTSTATE_STARTED:
328     case SCRIPTSTATE_CONNECTED: /* FIXME */
329         if(This->state == SCRIPTSTATE_CLOSED)
330             return E_UNEXPECTED;
331
332         exec_queued_code(This->ctx);
333         break;
334     case SCRIPTSTATE_INITIALIZED:
335         FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
336         return S_OK;
337     default:
338         FIXME("unimplemented state %d\n", ss);
339         return E_NOTIMPL;
340     }
341
342     change_state(This, ss);
343     return S_OK;
344 }
345
346 static HRESULT WINAPI VBScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
347 {
348     VBScript *This = impl_from_IActiveScript(iface);
349
350     TRACE("(%p)->(%p)\n", This, pssState);
351
352     if(!pssState)
353         return E_POINTER;
354
355     if(This->thread_id && This->thread_id != GetCurrentThreadId())
356         return E_UNEXPECTED;
357
358     *pssState = This->state;
359     return S_OK;
360 }
361
362 static HRESULT WINAPI VBScript_Close(IActiveScript *iface)
363 {
364     VBScript *This = impl_from_IActiveScript(iface);
365
366     TRACE("(%p)->()\n", This);
367
368     if(This->thread_id && This->thread_id != GetCurrentThreadId())
369         return E_UNEXPECTED;
370
371     decrease_state(This, SCRIPTSTATE_CLOSED);
372     return S_OK;
373 }
374
375 static HRESULT WINAPI VBScript_AddNamedItem(IActiveScript *iface, LPCOLESTR pstrName, DWORD dwFlags)
376 {
377     VBScript *This = impl_from_IActiveScript(iface);
378     named_item_t *item;
379     IDispatch *disp = NULL;
380     HRESULT hres;
381
382     TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
383
384     if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->state == SCRIPTSTATE_CLOSED)
385         return E_UNEXPECTED;
386
387     if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
388         IUnknown *unk;
389
390         hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
391         if(FAILED(hres)) {
392             WARN("GetItemInfo failed: %08x\n", hres);
393             return hres;
394         }
395
396         hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
397         IUnknown_Release(unk);
398         if(FAILED(hres)) {
399             WARN("object does not implement IDispatch\n");
400             return hres;
401         }
402
403         if(This->ctx->host_global)
404             IDispatch_Release(This->ctx->host_global);
405         IDispatch_AddRef(disp);
406         This->ctx->host_global = disp;
407     }
408
409     item = heap_alloc(sizeof(*item));
410     if(!item) {
411         if(disp)
412             IDispatch_Release(disp);
413         return E_OUTOFMEMORY;
414     }
415
416     item->disp = disp;
417     item->flags = dwFlags;
418     item->name = heap_strdupW(pstrName);
419     if(!item->name) {
420         if(disp)
421             IDispatch_Release(disp);
422         heap_free(item);
423         return E_OUTOFMEMORY;
424     }
425
426     list_add_tail(&This->ctx->named_items, &item->entry);
427     return S_OK;
428 }
429
430 static HRESULT WINAPI VBScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
431         DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
432 {
433     VBScript *This = impl_from_IActiveScript(iface);
434     FIXME("(%p)->()\n", This);
435     return E_NOTIMPL;
436 }
437
438 static HRESULT WINAPI VBScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName, IDispatch **ppdisp)
439 {
440     VBScript *This = impl_from_IActiveScript(iface);
441
442     TRACE("(%p)->(%p)\n", This, ppdisp);
443
444     if(!ppdisp)
445         return E_POINTER;
446
447     if(This->thread_id != GetCurrentThreadId() || !This->ctx || !This->ctx->script_obj) {
448         *ppdisp = NULL;
449         return E_UNEXPECTED;
450     }
451
452     *ppdisp = (IDispatch*)&This->ctx->script_obj->IDispatchEx_iface;
453     IDispatch_AddRef(*ppdisp);
454     return S_OK;
455 }
456
457 static HRESULT WINAPI VBScript_GetCurrentScriptThreadID(IActiveScript *iface,
458                                                        SCRIPTTHREADID *pstridThread)
459 {
460     VBScript *This = impl_from_IActiveScript(iface);
461     FIXME("(%p)->()\n", This);
462     return E_NOTIMPL;
463 }
464
465 static HRESULT WINAPI VBScript_GetScriptThreadID(IActiveScript *iface,
466                                                 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
467 {
468     VBScript *This = impl_from_IActiveScript(iface);
469     FIXME("(%p)->()\n", This);
470     return E_NOTIMPL;
471 }
472
473 static HRESULT WINAPI VBScript_GetScriptThreadState(IActiveScript *iface,
474         SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
475 {
476     VBScript *This = impl_from_IActiveScript(iface);
477     FIXME("(%p)->()\n", This);
478     return E_NOTIMPL;
479 }
480
481 static HRESULT WINAPI VBScript_InterruptScriptThread(IActiveScript *iface,
482         SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
483 {
484     VBScript *This = impl_from_IActiveScript(iface);
485     FIXME("(%p)->()\n", This);
486     return E_NOTIMPL;
487 }
488
489 static HRESULT WINAPI VBScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
490 {
491     VBScript *This = impl_from_IActiveScript(iface);
492     FIXME("(%p)->()\n", This);
493     return E_NOTIMPL;
494 }
495
496 static const IActiveScriptVtbl VBScriptVtbl = {
497     VBScript_QueryInterface,
498     VBScript_AddRef,
499     VBScript_Release,
500     VBScript_SetScriptSite,
501     VBScript_GetScriptSite,
502     VBScript_SetScriptState,
503     VBScript_GetScriptState,
504     VBScript_Close,
505     VBScript_AddNamedItem,
506     VBScript_AddTypeLib,
507     VBScript_GetScriptDispatch,
508     VBScript_GetCurrentScriptThreadID,
509     VBScript_GetScriptThreadID,
510     VBScript_GetScriptThreadState,
511     VBScript_InterruptScriptThread,
512     VBScript_Clone
513 };
514
515 static inline VBScript *impl_from_IActiveScriptParse(IActiveScriptParse *iface)
516 {
517     return CONTAINING_RECORD(iface, VBScript, IActiveScriptParse_iface);
518 }
519
520 static HRESULT WINAPI VBScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
521 {
522     VBScript *This = impl_from_IActiveScriptParse(iface);
523     return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
524 }
525
526 static ULONG WINAPI VBScriptParse_AddRef(IActiveScriptParse *iface)
527 {
528     VBScript *This = impl_from_IActiveScriptParse(iface);
529     return IActiveScript_AddRef(&This->IActiveScript_iface);
530 }
531
532 static ULONG WINAPI VBScriptParse_Release(IActiveScriptParse *iface)
533 {
534     VBScript *This = impl_from_IActiveScriptParse(iface);
535     return IActiveScript_Release(&This->IActiveScript_iface);
536 }
537
538 static HRESULT WINAPI VBScriptParse_InitNew(IActiveScriptParse *iface)
539 {
540     VBScript *This = impl_from_IActiveScriptParse(iface);
541     script_ctx_t *ctx, *old_ctx;
542
543     TRACE("(%p)\n", This);
544
545     if(This->ctx)
546         return E_UNEXPECTED;
547
548     ctx = heap_alloc_zero(sizeof(script_ctx_t));
549     if(!ctx)
550         return E_OUTOFMEMORY;
551
552     ctx->safeopt = This->safeopt;
553     vbsheap_init(&ctx->heap);
554     list_init(&ctx->objects);
555     list_init(&ctx->code_list);
556     list_init(&ctx->named_items);
557
558     old_ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
559     if(old_ctx) {
560         destroy_script(ctx);
561         return E_UNEXPECTED;
562     }
563
564     return This->site ? set_ctx_site(This) : S_OK;
565 }
566
567 static HRESULT WINAPI VBScriptParse_AddScriptlet(IActiveScriptParse *iface,
568         LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
569         LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
570         CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
571         BSTR *pbstrName, EXCEPINFO *pexcepinfo)
572 {
573     VBScript *This = impl_from_IActiveScriptParse(iface);
574     FIXME("(%p)->(%s %s %s %s %s %s %s %u %x %p %p)\n", This, debugstr_w(pstrDefaultName),
575           debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
576           debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
577           ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
578     return E_NOTIMPL;
579 }
580
581 static HRESULT WINAPI VBScriptParse_ParseScriptText(IActiveScriptParse *iface,
582         LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
583         LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
584         DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
585 {
586     VBScript *This = impl_from_IActiveScriptParse(iface);
587     vbscode_t *code;
588     HRESULT hres;
589
590     TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode),
591           debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
592           wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
593
594     if(This->thread_id != GetCurrentThreadId() || This->state == SCRIPTSTATE_CLOSED)
595         return E_UNEXPECTED;
596
597     hres = compile_script(This->ctx, pstrCode, &code);
598     if(FAILED(hres))
599         return hres;
600
601     return is_started(This) ? exec_global_code(This->ctx, code) : S_OK;
602 }
603
604 static const IActiveScriptParseVtbl VBScriptParseVtbl = {
605     VBScriptParse_QueryInterface,
606     VBScriptParse_AddRef,
607     VBScriptParse_Release,
608     VBScriptParse_InitNew,
609     VBScriptParse_AddScriptlet,
610     VBScriptParse_ParseScriptText
611 };
612
613 static inline VBScript *impl_from_IObjectSafety(IObjectSafety *iface)
614 {
615     return CONTAINING_RECORD(iface, VBScript, IObjectSafety_iface);
616 }
617
618 static HRESULT WINAPI VBScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
619 {
620     VBScript *This = impl_from_IObjectSafety(iface);
621     return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
622 }
623
624 static ULONG WINAPI VBScriptSafety_AddRef(IObjectSafety *iface)
625 {
626     VBScript *This = impl_from_IObjectSafety(iface);
627     return IActiveScript_AddRef(&This->IActiveScript_iface);
628 }
629
630 static ULONG WINAPI VBScriptSafety_Release(IObjectSafety *iface)
631 {
632     VBScript *This = impl_from_IObjectSafety(iface);
633     return IActiveScript_Release(&This->IActiveScript_iface);
634 }
635
636 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
637
638 static HRESULT WINAPI VBScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
639         DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
640 {
641     VBScript *This = impl_from_IObjectSafety(iface);
642
643     TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
644
645     if(!pdwSupportedOptions || !pdwEnabledOptions)
646         return E_POINTER;
647
648     *pdwSupportedOptions = SUPPORTED_OPTIONS;
649     *pdwEnabledOptions = This->safeopt;
650     return S_OK;
651 }
652
653 static HRESULT WINAPI VBScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
654         DWORD dwOptionSetMask, DWORD dwEnabledOptions)
655 {
656     VBScript *This = impl_from_IObjectSafety(iface);
657
658     TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
659
660     if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
661         return E_FAIL;
662
663     This->safeopt = (dwEnabledOptions & dwOptionSetMask) | (This->safeopt & ~dwOptionSetMask) | INTERFACE_USES_DISPEX;
664     return S_OK;
665 }
666
667 static const IObjectSafetyVtbl VBScriptSafetyVtbl = {
668     VBScriptSafety_QueryInterface,
669     VBScriptSafety_AddRef,
670     VBScriptSafety_Release,
671     VBScriptSafety_GetInterfaceSafetyOptions,
672     VBScriptSafety_SetInterfaceSafetyOptions
673 };
674
675 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter, REFIID riid, void **ppv)
676 {
677     VBScript *ret;
678     HRESULT hres;
679
680     TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
681
682     ret = heap_alloc_zero(sizeof(*ret));
683     if(!ret)
684         return E_OUTOFMEMORY;
685
686     ret->IActiveScript_iface.lpVtbl = &VBScriptVtbl;
687     ret->IActiveScriptParse_iface.lpVtbl = &VBScriptParseVtbl;
688     ret->IObjectSafety_iface.lpVtbl = &VBScriptSafetyVtbl;
689
690     ret->ref = 1;
691     ret->state = SCRIPTSTATE_UNINITIALIZED;
692     ret->safeopt = INTERFACE_USES_DISPEX;
693
694     hres = IActiveScript_QueryInterface(&ret->IActiveScript_iface, riid, ppv);
695     IActiveScript_Release(&ret->IActiveScript_iface);
696     return hres;
697 }
698
699 typedef struct {
700     IServiceProvider IServiceProvider_iface;
701
702     LONG ref;
703
704     IServiceProvider *sp;
705 } AXSite;
706
707 static inline AXSite *impl_from_IServiceProvider(IServiceProvider *iface)
708 {
709     return CONTAINING_RECORD(iface, AXSite, IServiceProvider_iface);
710 }
711
712 static HRESULT WINAPI AXSite_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
713 {
714     AXSite *This = impl_from_IServiceProvider(iface);
715
716     if(IsEqualGUID(&IID_IUnknown, riid)) {
717         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
718         *ppv = &This->IServiceProvider_iface;
719     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
720         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
721         *ppv = &This->IServiceProvider_iface;
722     }else {
723         TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
724         *ppv = NULL;
725         return E_NOINTERFACE;
726     }
727
728     IUnknown_AddRef((IUnknown*)*ppv);
729     return S_OK;
730 }
731
732 static ULONG WINAPI AXSite_AddRef(IServiceProvider *iface)
733 {
734     AXSite *This = impl_from_IServiceProvider(iface);
735     LONG ref = InterlockedIncrement(&This->ref);
736
737     TRACE("(%p) ref=%d\n", This, ref);
738
739     return ref;
740 }
741
742 static ULONG WINAPI AXSite_Release(IServiceProvider *iface)
743 {
744     AXSite *This = impl_from_IServiceProvider(iface);
745     LONG ref = InterlockedDecrement(&This->ref);
746
747     TRACE("(%p) ref=%d\n", This, ref);
748
749     if(!ref)
750         heap_free(This);
751
752     return ref;
753 }
754
755 static HRESULT WINAPI AXSite_QueryService(IServiceProvider *iface,
756         REFGUID guidService, REFIID riid, void **ppv)
757 {
758     AXSite *This = impl_from_IServiceProvider(iface);
759
760     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
761
762     return IServiceProvider_QueryService(This->sp, guidService, riid, ppv);
763 }
764
765 static IServiceProviderVtbl AXSiteVtbl = {
766     AXSite_QueryInterface,
767     AXSite_AddRef,
768     AXSite_Release,
769     AXSite_QueryService
770 };
771
772 IUnknown *create_ax_site(script_ctx_t *ctx)
773 {
774     IServiceProvider *sp;
775     AXSite *ret;
776     HRESULT hres;
777
778     hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
779     if(FAILED(hres)) {
780         ERR("Could not get IServiceProvider iface: %08x\n", hres);
781         return NULL;
782     }
783
784     ret = heap_alloc(sizeof(*ret));
785     if(!ret) {
786         IServiceProvider_Release(sp);
787         return NULL;
788     }
789
790     ret->IServiceProvider_iface.lpVtbl = &AXSiteVtbl;
791     ret->ref = 1;
792     ret->sp = sp;
793
794     return (IUnknown*)&ret->IServiceProvider_iface;
795 }