2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
31 #define CTXARG_T DWORDLONG
32 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
36 #define CTXARG_T DWORD
37 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
42 IActiveScript IActiveScript_iface;
43 IActiveScriptParse IActiveScriptParse_iface;
44 IObjectSafety IObjectSafety_iface;
50 IActiveScriptSite *site;
56 static void change_state(VBScript *This, SCRIPTSTATE state)
58 if(This->state == state)
63 IActiveScriptSite_OnStateChange(This->site, state);
66 static inline BOOL is_started(VBScript *This)
68 return This->state == SCRIPTSTATE_STARTED
69 || This->state == SCRIPTSTATE_CONNECTED
70 || This->state == SCRIPTSTATE_DISCONNECTED;
73 static HRESULT exec_global_code(script_ctx_t *ctx, vbscode_t *code)
77 code->global_executed = TRUE;
79 IActiveScriptSite_OnEnterScript(ctx->site);
80 hres = exec_script(ctx, &code->global_code, NULL, NULL, NULL);
81 IActiveScriptSite_OnLeaveScript(ctx->site);
86 static void exec_queued_code(script_ctx_t *ctx)
90 LIST_FOR_EACH_ENTRY(iter, &ctx->code_list, vbscode_t, entry) {
91 if(!iter->global_executed)
92 exec_global_code(ctx, iter);
96 static HRESULT set_ctx_site(VBScript *This)
100 This->ctx->lcid = This->lcid;
102 hres = init_global(This->ctx);
106 IActiveScriptSite_AddRef(This->site);
107 This->ctx->site = This->site;
109 change_state(This, SCRIPTSTATE_INITIALIZED);
113 static void release_script(script_ctx_t *ctx)
115 collect_objects(ctx);
117 release_dynamic_vars(ctx->global_vars);
118 ctx->global_vars = NULL;
120 while(!list_empty(&ctx->named_items)) {
121 named_item_t *iter = LIST_ENTRY(list_head(&ctx->named_items), named_item_t, entry);
123 list_remove(&iter->entry);
125 IDispatch_Release(iter->disp);
126 heap_free(iter->name);
130 if(ctx->host_global) {
131 IDispatch_Release(ctx->host_global);
132 ctx->host_global = NULL;
136 IInternetHostSecurityManager_Release(ctx->secmgr);
141 IActiveScriptSite_Release(ctx->site);
146 IDispatchEx_Release(&ctx->err_obj->IDispatchEx_iface);
150 if(ctx->global_obj) {
151 IDispatchEx_Release(&ctx->global_obj->IDispatchEx_iface);
152 ctx->global_obj = NULL;
155 if(ctx->script_obj) {
156 IDispatchEx_Release(&ctx->script_obj->IDispatchEx_iface);
157 ctx->script_obj = NULL;
160 vbsheap_free(&ctx->heap);
161 vbsheap_init(&ctx->heap);
164 static void destroy_script(script_ctx_t *ctx)
166 while(!list_empty(&ctx->code_list))
167 release_vbscode(LIST_ENTRY(list_head(&ctx->code_list), vbscode_t, entry));
173 static void decrease_state(VBScript *This, SCRIPTSTATE state)
175 switch(This->state) {
176 case SCRIPTSTATE_CONNECTED:
177 change_state(This, SCRIPTSTATE_DISCONNECTED);
178 if(state == SCRIPTSTATE_DISCONNECTED)
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)
188 case SCRIPTSTATE_INITIALIZED:
189 case SCRIPTSTATE_UNINITIALIZED:
190 change_state(This, state);
193 IActiveScriptSite_Release(This->site);
198 release_script(This->ctx);
202 case SCRIPTSTATE_CLOSED:
209 static inline VBScript *impl_from_IActiveScript(IActiveScript *iface)
211 return CONTAINING_RECORD(iface, VBScript, IActiveScript_iface);
214 static HRESULT WINAPI VBScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
216 VBScript *This = impl_from_IActiveScript(iface);
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;
231 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
233 return E_NOINTERFACE;
236 IUnknown_AddRef((IUnknown*)*ppv);
240 static ULONG WINAPI VBScript_AddRef(IActiveScript *iface)
242 VBScript *This = impl_from_IActiveScript(iface);
243 LONG ref = InterlockedIncrement(&This->ref);
245 TRACE("(%p) ref=%d\n", This, ref);
250 static ULONG WINAPI VBScript_Release(IActiveScript *iface)
252 VBScript *This = impl_from_IActiveScript(iface);
253 LONG ref = InterlockedDecrement(&This->ref);
255 TRACE("(%p) ref=%d\n", iface, ref);
259 decrease_state(This, SCRIPTSTATE_CLOSED);
260 destroy_script(This->ctx);
264 IActiveScriptSite_Release(This->site);
271 static HRESULT WINAPI VBScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass)
273 VBScript *This = impl_from_IActiveScript(iface);
277 TRACE("(%p)->(%p)\n", This, pass);
285 if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0))
289 IActiveScriptSite_AddRef(This->site);
291 hres = IActiveScriptSite_GetLCID(This->site, &lcid);
295 return This->ctx ? set_ctx_site(This) : S_OK;
298 static HRESULT WINAPI VBScript_GetScriptSite(IActiveScript *iface, REFIID riid,
301 VBScript *This = impl_from_IActiveScript(iface);
302 FIXME("(%p)->()\n", This);
306 static HRESULT WINAPI VBScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
308 VBScript *This = impl_from_IActiveScript(iface);
310 TRACE("(%p)->(%d)\n", This, ss);
312 if(This->thread_id && GetCurrentThreadId() != This->thread_id)
315 if(ss == SCRIPTSTATE_UNINITIALIZED) {
316 if(This->state == SCRIPTSTATE_CLOSED)
319 decrease_state(This, SCRIPTSTATE_UNINITIALIZED);
327 case SCRIPTSTATE_STARTED:
328 case SCRIPTSTATE_CONNECTED: /* FIXME */
329 if(This->state == SCRIPTSTATE_CLOSED)
332 exec_queued_code(This->ctx);
334 case SCRIPTSTATE_INITIALIZED:
335 FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
338 FIXME("unimplemented state %d\n", ss);
342 change_state(This, ss);
346 static HRESULT WINAPI VBScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
348 VBScript *This = impl_from_IActiveScript(iface);
350 TRACE("(%p)->(%p)\n", This, pssState);
355 if(This->thread_id && This->thread_id != GetCurrentThreadId())
358 *pssState = This->state;
362 static HRESULT WINAPI VBScript_Close(IActiveScript *iface)
364 VBScript *This = impl_from_IActiveScript(iface);
366 TRACE("(%p)->()\n", This);
368 if(This->thread_id && This->thread_id != GetCurrentThreadId())
371 decrease_state(This, SCRIPTSTATE_CLOSED);
375 static HRESULT WINAPI VBScript_AddNamedItem(IActiveScript *iface, LPCOLESTR pstrName, DWORD dwFlags)
377 VBScript *This = impl_from_IActiveScript(iface);
379 IDispatch *disp = NULL;
382 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
384 if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->state == SCRIPTSTATE_CLOSED)
387 if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
390 hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
392 WARN("GetItemInfo failed: %08x\n", hres);
396 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
397 IUnknown_Release(unk);
399 WARN("object does not implement IDispatch\n");
403 if(This->ctx->host_global)
404 IDispatch_Release(This->ctx->host_global);
405 IDispatch_AddRef(disp);
406 This->ctx->host_global = disp;
409 item = heap_alloc(sizeof(*item));
412 IDispatch_Release(disp);
413 return E_OUTOFMEMORY;
417 item->flags = dwFlags;
418 item->name = heap_strdupW(pstrName);
421 IDispatch_Release(disp);
423 return E_OUTOFMEMORY;
426 list_add_tail(&This->ctx->named_items, &item->entry);
430 static HRESULT WINAPI VBScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
431 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
433 VBScript *This = impl_from_IActiveScript(iface);
434 FIXME("(%p)->()\n", This);
438 static HRESULT WINAPI VBScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName, IDispatch **ppdisp)
440 VBScript *This = impl_from_IActiveScript(iface);
442 TRACE("(%p)->(%p)\n", This, ppdisp);
447 if(This->thread_id != GetCurrentThreadId() || !This->ctx || !This->ctx->script_obj) {
452 *ppdisp = (IDispatch*)&This->ctx->script_obj->IDispatchEx_iface;
453 IDispatch_AddRef(*ppdisp);
457 static HRESULT WINAPI VBScript_GetCurrentScriptThreadID(IActiveScript *iface,
458 SCRIPTTHREADID *pstridThread)
460 VBScript *This = impl_from_IActiveScript(iface);
461 FIXME("(%p)->()\n", This);
465 static HRESULT WINAPI VBScript_GetScriptThreadID(IActiveScript *iface,
466 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
468 VBScript *This = impl_from_IActiveScript(iface);
469 FIXME("(%p)->()\n", This);
473 static HRESULT WINAPI VBScript_GetScriptThreadState(IActiveScript *iface,
474 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
476 VBScript *This = impl_from_IActiveScript(iface);
477 FIXME("(%p)->()\n", This);
481 static HRESULT WINAPI VBScript_InterruptScriptThread(IActiveScript *iface,
482 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
484 VBScript *This = impl_from_IActiveScript(iface);
485 FIXME("(%p)->()\n", This);
489 static HRESULT WINAPI VBScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
491 VBScript *This = impl_from_IActiveScript(iface);
492 FIXME("(%p)->()\n", This);
496 static const IActiveScriptVtbl VBScriptVtbl = {
497 VBScript_QueryInterface,
500 VBScript_SetScriptSite,
501 VBScript_GetScriptSite,
502 VBScript_SetScriptState,
503 VBScript_GetScriptState,
505 VBScript_AddNamedItem,
507 VBScript_GetScriptDispatch,
508 VBScript_GetCurrentScriptThreadID,
509 VBScript_GetScriptThreadID,
510 VBScript_GetScriptThreadState,
511 VBScript_InterruptScriptThread,
515 static inline VBScript *impl_from_IActiveScriptParse(IActiveScriptParse *iface)
517 return CONTAINING_RECORD(iface, VBScript, IActiveScriptParse_iface);
520 static HRESULT WINAPI VBScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
522 VBScript *This = impl_from_IActiveScriptParse(iface);
523 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
526 static ULONG WINAPI VBScriptParse_AddRef(IActiveScriptParse *iface)
528 VBScript *This = impl_from_IActiveScriptParse(iface);
529 return IActiveScript_AddRef(&This->IActiveScript_iface);
532 static ULONG WINAPI VBScriptParse_Release(IActiveScriptParse *iface)
534 VBScript *This = impl_from_IActiveScriptParse(iface);
535 return IActiveScript_Release(&This->IActiveScript_iface);
538 static HRESULT WINAPI VBScriptParse_InitNew(IActiveScriptParse *iface)
540 VBScript *This = impl_from_IActiveScriptParse(iface);
541 script_ctx_t *ctx, *old_ctx;
543 TRACE("(%p)\n", This);
548 ctx = heap_alloc_zero(sizeof(script_ctx_t));
550 return E_OUTOFMEMORY;
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);
558 old_ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
564 return This->site ? set_ctx_site(This) : S_OK;
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)
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);
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)
586 VBScript *This = impl_from_IActiveScriptParse(iface);
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);
594 if(This->thread_id != GetCurrentThreadId() || This->state == SCRIPTSTATE_CLOSED)
597 hres = compile_script(This->ctx, pstrCode, &code);
601 return is_started(This) ? exec_global_code(This->ctx, code) : S_OK;
604 static const IActiveScriptParseVtbl VBScriptParseVtbl = {
605 VBScriptParse_QueryInterface,
606 VBScriptParse_AddRef,
607 VBScriptParse_Release,
608 VBScriptParse_InitNew,
609 VBScriptParse_AddScriptlet,
610 VBScriptParse_ParseScriptText
613 static inline VBScript *impl_from_IObjectSafety(IObjectSafety *iface)
615 return CONTAINING_RECORD(iface, VBScript, IObjectSafety_iface);
618 static HRESULT WINAPI VBScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
620 VBScript *This = impl_from_IObjectSafety(iface);
621 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
624 static ULONG WINAPI VBScriptSafety_AddRef(IObjectSafety *iface)
626 VBScript *This = impl_from_IObjectSafety(iface);
627 return IActiveScript_AddRef(&This->IActiveScript_iface);
630 static ULONG WINAPI VBScriptSafety_Release(IObjectSafety *iface)
632 VBScript *This = impl_from_IObjectSafety(iface);
633 return IActiveScript_Release(&This->IActiveScript_iface);
636 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
638 static HRESULT WINAPI VBScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
639 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
641 VBScript *This = impl_from_IObjectSafety(iface);
643 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
645 if(!pdwSupportedOptions || !pdwEnabledOptions)
648 *pdwSupportedOptions = SUPPORTED_OPTIONS;
649 *pdwEnabledOptions = This->safeopt;
653 static HRESULT WINAPI VBScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
654 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
656 VBScript *This = impl_from_IObjectSafety(iface);
658 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
660 if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
663 This->safeopt = (dwEnabledOptions & dwOptionSetMask) | (This->safeopt & ~dwOptionSetMask) | INTERFACE_USES_DISPEX;
667 static const IObjectSafetyVtbl VBScriptSafetyVtbl = {
668 VBScriptSafety_QueryInterface,
669 VBScriptSafety_AddRef,
670 VBScriptSafety_Release,
671 VBScriptSafety_GetInterfaceSafetyOptions,
672 VBScriptSafety_SetInterfaceSafetyOptions
675 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter, REFIID riid, void **ppv)
680 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
682 ret = heap_alloc_zero(sizeof(*ret));
684 return E_OUTOFMEMORY;
686 ret->IActiveScript_iface.lpVtbl = &VBScriptVtbl;
687 ret->IActiveScriptParse_iface.lpVtbl = &VBScriptParseVtbl;
688 ret->IObjectSafety_iface.lpVtbl = &VBScriptSafetyVtbl;
691 ret->state = SCRIPTSTATE_UNINITIALIZED;
692 ret->safeopt = INTERFACE_USES_DISPEX;
694 hres = IActiveScript_QueryInterface(&ret->IActiveScript_iface, riid, ppv);
695 IActiveScript_Release(&ret->IActiveScript_iface);
700 IServiceProvider IServiceProvider_iface;
704 IServiceProvider *sp;
707 static inline AXSite *impl_from_IServiceProvider(IServiceProvider *iface)
709 return CONTAINING_RECORD(iface, AXSite, IServiceProvider_iface);
712 static HRESULT WINAPI AXSite_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
714 AXSite *This = impl_from_IServiceProvider(iface);
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;
723 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
725 return E_NOINTERFACE;
728 IUnknown_AddRef((IUnknown*)*ppv);
732 static ULONG WINAPI AXSite_AddRef(IServiceProvider *iface)
734 AXSite *This = impl_from_IServiceProvider(iface);
735 LONG ref = InterlockedIncrement(&This->ref);
737 TRACE("(%p) ref=%d\n", This, ref);
742 static ULONG WINAPI AXSite_Release(IServiceProvider *iface)
744 AXSite *This = impl_from_IServiceProvider(iface);
745 LONG ref = InterlockedDecrement(&This->ref);
747 TRACE("(%p) ref=%d\n", This, ref);
755 static HRESULT WINAPI AXSite_QueryService(IServiceProvider *iface,
756 REFGUID guidService, REFIID riid, void **ppv)
758 AXSite *This = impl_from_IServiceProvider(iface);
760 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
762 return IServiceProvider_QueryService(This->sp, guidService, riid, ppv);
765 static IServiceProviderVtbl AXSiteVtbl = {
766 AXSite_QueryInterface,
772 IUnknown *create_ax_site(script_ctx_t *ctx)
774 IServiceProvider *sp;
778 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
780 ERR("Could not get IServiceProvider iface: %08x\n", hres);
784 ret = heap_alloc(sizeof(*ret));
786 IServiceProvider_Release(sp);
790 ret->IServiceProvider_iface.lpVtbl = &AXSiteVtbl;
794 return (IUnknown*)&ret->IServiceProvider_iface;