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);
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 destroy_script(script_ctx_t *ctx)
115 while(!list_empty(&ctx->code_list))
116 release_vbscode(LIST_ENTRY(list_head(&ctx->code_list), vbscode_t, entry));
118 while(!list_empty(&ctx->named_items)) {
119 named_item_t *iter = LIST_ENTRY(list_head(&ctx->named_items), named_item_t, entry);
121 list_remove(&iter->entry);
123 IDispatch_Release(iter->disp);
124 heap_free(iter->name);
129 IDispatch_Release(ctx->host_global);
131 IActiveScriptSite_Release(ctx->site);
133 IDispatchEx_Release(&ctx->script_obj->IDispatchEx_iface);
137 static void decrease_state(VBScript *This, SCRIPTSTATE state)
139 switch(This->state) {
140 case SCRIPTSTATE_CONNECTED:
141 change_state(This, SCRIPTSTATE_DISCONNECTED);
142 if(state == SCRIPTSTATE_DISCONNECTED)
145 case SCRIPTSTATE_STARTED:
146 case SCRIPTSTATE_DISCONNECTED:
147 if(This->state == SCRIPTSTATE_DISCONNECTED)
148 change_state(This, SCRIPTSTATE_INITIALIZED);
149 if(state == SCRIPTSTATE_INITIALIZED)
152 case SCRIPTSTATE_INITIALIZED:
153 case SCRIPTSTATE_UNINITIALIZED:
154 change_state(This, state);
157 IActiveScriptSite_Release(This->site);
163 if(state == SCRIPTSTATE_CLOSED) {
164 destroy_script(This->ctx);
174 static inline VBScript *impl_from_IActiveScript(IActiveScript *iface)
176 return CONTAINING_RECORD(iface, VBScript, IActiveScript_iface);
179 static HRESULT WINAPI VBScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
181 VBScript *This = impl_from_IActiveScript(iface);
183 if(IsEqualGUID(riid, &IID_IUnknown)) {
184 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
185 *ppv = &This->IActiveScript_iface;
186 }else if(IsEqualGUID(riid, &IID_IActiveScript)) {
187 TRACE("(%p)->(IID_IActiveScript %p)\n", This, ppv);
188 *ppv = &This->IActiveScript_iface;
189 }else if(IsEqualGUID(riid, &IID_IActiveScriptParse)) {
190 TRACE("(%p)->(IID_IActiveScriptParse %p)\n", This, ppv);
191 *ppv = &This->IActiveScriptParse_iface;
192 }else if(IsEqualGUID(riid, &IID_IObjectSafety)) {
193 TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
194 *ppv = &This->IObjectSafety_iface;
196 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
198 return E_NOINTERFACE;
201 IUnknown_AddRef((IUnknown*)*ppv);
205 static ULONG WINAPI VBScript_AddRef(IActiveScript *iface)
207 VBScript *This = impl_from_IActiveScript(iface);
208 LONG ref = InterlockedIncrement(&This->ref);
210 TRACE("(%p) ref=%d\n", This, ref);
215 static ULONG WINAPI VBScript_Release(IActiveScript *iface)
217 VBScript *This = impl_from_IActiveScript(iface);
218 LONG ref = InterlockedDecrement(&This->ref);
220 TRACE("(%p) ref=%d\n", iface, ref);
224 IActiveScriptSite_Release(This->site);
231 static HRESULT WINAPI VBScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass)
233 VBScript *This = impl_from_IActiveScript(iface);
237 TRACE("(%p)->(%p)\n", This, pass);
245 if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0))
249 IActiveScriptSite_AddRef(This->site);
251 hres = IActiveScriptSite_GetLCID(This->site, &lcid);
255 return This->ctx ? set_ctx_site(This) : S_OK;
258 static HRESULT WINAPI VBScript_GetScriptSite(IActiveScript *iface, REFIID riid,
261 VBScript *This = impl_from_IActiveScript(iface);
262 FIXME("(%p)->()\n", This);
266 static HRESULT WINAPI VBScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
268 VBScript *This = impl_from_IActiveScript(iface);
270 TRACE("(%p)->(%d)\n", This, ss);
272 if(This->thread_id && GetCurrentThreadId() != This->thread_id)
275 if(ss == SCRIPTSTATE_UNINITIALIZED) {
276 if(This->state == SCRIPTSTATE_CLOSED)
279 decrease_state(This, SCRIPTSTATE_UNINITIALIZED);
287 case SCRIPTSTATE_STARTED:
288 case SCRIPTSTATE_CONNECTED: /* FIXME */
289 if(This->state == SCRIPTSTATE_CLOSED)
292 exec_queued_code(This->ctx);
294 case SCRIPTSTATE_INITIALIZED:
295 FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
298 FIXME("unimplemented state %d\n", ss);
302 change_state(This, ss);
306 static HRESULT WINAPI VBScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
308 VBScript *This = impl_from_IActiveScript(iface);
310 TRACE("(%p)->(%p)\n", This, pssState);
315 if(This->thread_id && This->thread_id != GetCurrentThreadId())
318 *pssState = This->state;
322 static HRESULT WINAPI VBScript_Close(IActiveScript *iface)
324 VBScript *This = impl_from_IActiveScript(iface);
326 TRACE("(%p)->()\n", This);
328 if(This->thread_id && This->thread_id != GetCurrentThreadId())
331 decrease_state(This, SCRIPTSTATE_CLOSED);
335 static HRESULT WINAPI VBScript_AddNamedItem(IActiveScript *iface, LPCOLESTR pstrName, DWORD dwFlags)
337 VBScript *This = impl_from_IActiveScript(iface);
339 IDispatch *disp = NULL;
342 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
344 if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->state == SCRIPTSTATE_CLOSED)
347 if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
350 hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
352 WARN("GetItemInfo failed: %08x\n", hres);
356 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
357 IUnknown_Release(unk);
359 WARN("object does not implement IDispatch\n");
363 if(This->ctx->host_global)
364 IDispatch_Release(This->ctx->host_global);
365 IDispatch_AddRef(disp);
366 This->ctx->host_global = disp;
369 item = heap_alloc(sizeof(*item));
372 IDispatch_Release(disp);
373 return E_OUTOFMEMORY;
377 item->flags = dwFlags;
378 item->name = heap_strdupW(pstrName);
381 IDispatch_Release(disp);
383 return E_OUTOFMEMORY;
386 list_add_tail(&This->ctx->named_items, &item->entry);
390 static HRESULT WINAPI VBScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
391 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
393 VBScript *This = impl_from_IActiveScript(iface);
394 FIXME("(%p)->()\n", This);
398 static HRESULT WINAPI VBScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName, IDispatch **ppdisp)
400 VBScript *This = impl_from_IActiveScript(iface);
402 TRACE("(%p)->(%p)\n", This, ppdisp);
407 if(This->thread_id != GetCurrentThreadId() || !This->ctx->script_obj) {
412 *ppdisp = (IDispatch*)&This->ctx->script_obj->IDispatchEx_iface;
413 IDispatch_AddRef(*ppdisp);
417 static HRESULT WINAPI VBScript_GetCurrentScriptThreadID(IActiveScript *iface,
418 SCRIPTTHREADID *pstridThread)
420 VBScript *This = impl_from_IActiveScript(iface);
421 FIXME("(%p)->()\n", This);
425 static HRESULT WINAPI VBScript_GetScriptThreadID(IActiveScript *iface,
426 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
428 VBScript *This = impl_from_IActiveScript(iface);
429 FIXME("(%p)->()\n", This);
433 static HRESULT WINAPI VBScript_GetScriptThreadState(IActiveScript *iface,
434 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
436 VBScript *This = impl_from_IActiveScript(iface);
437 FIXME("(%p)->()\n", This);
441 static HRESULT WINAPI VBScript_InterruptScriptThread(IActiveScript *iface,
442 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
444 VBScript *This = impl_from_IActiveScript(iface);
445 FIXME("(%p)->()\n", This);
449 static HRESULT WINAPI VBScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
451 VBScript *This = impl_from_IActiveScript(iface);
452 FIXME("(%p)->()\n", This);
456 static const IActiveScriptVtbl VBScriptVtbl = {
457 VBScript_QueryInterface,
460 VBScript_SetScriptSite,
461 VBScript_GetScriptSite,
462 VBScript_SetScriptState,
463 VBScript_GetScriptState,
465 VBScript_AddNamedItem,
467 VBScript_GetScriptDispatch,
468 VBScript_GetCurrentScriptThreadID,
469 VBScript_GetScriptThreadID,
470 VBScript_GetScriptThreadState,
471 VBScript_InterruptScriptThread,
475 static inline VBScript *impl_from_IActiveScriptParse(IActiveScriptParse *iface)
477 return CONTAINING_RECORD(iface, VBScript, IActiveScriptParse_iface);
480 static HRESULT WINAPI VBScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
482 VBScript *This = impl_from_IActiveScriptParse(iface);
483 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
486 static ULONG WINAPI VBScriptParse_AddRef(IActiveScriptParse *iface)
488 VBScript *This = impl_from_IActiveScriptParse(iface);
489 return IActiveScript_AddRef(&This->IActiveScript_iface);
492 static ULONG WINAPI VBScriptParse_Release(IActiveScriptParse *iface)
494 VBScript *This = impl_from_IActiveScriptParse(iface);
495 return IActiveScript_Release(&This->IActiveScript_iface);
498 static HRESULT WINAPI VBScriptParse_InitNew(IActiveScriptParse *iface)
500 VBScript *This = impl_from_IActiveScriptParse(iface);
501 script_ctx_t *ctx, *old_ctx;
503 TRACE("(%p)\n", This);
508 ctx = heap_alloc_zero(sizeof(script_ctx_t));
510 return E_OUTOFMEMORY;
512 list_init(&ctx->code_list);
513 list_init(&ctx->named_items);
515 old_ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
521 return This->site ? set_ctx_site(This) : S_OK;
524 static HRESULT WINAPI VBScriptParse_AddScriptlet(IActiveScriptParse *iface,
525 LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
526 LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
527 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
528 BSTR *pbstrName, EXCEPINFO *pexcepinfo)
530 VBScript *This = impl_from_IActiveScriptParse(iface);
531 FIXME("(%p)->(%s %s %s %s %s %s %s %u %x %p %p)\n", This, debugstr_w(pstrDefaultName),
532 debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
533 debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
534 ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
538 static HRESULT WINAPI VBScriptParse_ParseScriptText(IActiveScriptParse *iface,
539 LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
540 LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
541 DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
543 VBScript *This = impl_from_IActiveScriptParse(iface);
547 TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode),
548 debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
549 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
551 if(This->thread_id != GetCurrentThreadId() || This->state == SCRIPTSTATE_CLOSED)
554 hres = compile_script(This->ctx, pstrCode, &code);
558 return is_started(This) ? exec_global_code(This->ctx, code) : S_OK;
561 static const IActiveScriptParseVtbl VBScriptParseVtbl = {
562 VBScriptParse_QueryInterface,
563 VBScriptParse_AddRef,
564 VBScriptParse_Release,
565 VBScriptParse_InitNew,
566 VBScriptParse_AddScriptlet,
567 VBScriptParse_ParseScriptText
570 static inline VBScript *impl_from_IObjectSafety(IObjectSafety *iface)
572 return CONTAINING_RECORD(iface, VBScript, IObjectSafety_iface);
575 static HRESULT WINAPI VBScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
577 VBScript *This = impl_from_IObjectSafety(iface);
578 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
581 static ULONG WINAPI VBScriptSafety_AddRef(IObjectSafety *iface)
583 VBScript *This = impl_from_IObjectSafety(iface);
584 return IActiveScript_AddRef(&This->IActiveScript_iface);
587 static ULONG WINAPI VBScriptSafety_Release(IObjectSafety *iface)
589 VBScript *This = impl_from_IObjectSafety(iface);
590 return IActiveScript_Release(&This->IActiveScript_iface);
593 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
595 static HRESULT WINAPI VBScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
596 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
598 VBScript *This = impl_from_IObjectSafety(iface);
600 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
602 if(!pdwSupportedOptions || !pdwEnabledOptions)
605 *pdwSupportedOptions = SUPPORTED_OPTIONS;
606 *pdwEnabledOptions = This->safeopt;
610 static HRESULT WINAPI VBScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
611 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
613 VBScript *This = impl_from_IObjectSafety(iface);
615 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
617 if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
620 This->safeopt = (dwEnabledOptions & dwOptionSetMask) | (This->safeopt & ~dwOptionSetMask) | INTERFACE_USES_DISPEX;
624 static const IObjectSafetyVtbl VBScriptSafetyVtbl = {
625 VBScriptSafety_QueryInterface,
626 VBScriptSafety_AddRef,
627 VBScriptSafety_Release,
628 VBScriptSafety_GetInterfaceSafetyOptions,
629 VBScriptSafety_SetInterfaceSafetyOptions
632 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter, REFIID riid, void **ppv)
637 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
639 ret = heap_alloc_zero(sizeof(*ret));
641 return E_OUTOFMEMORY;
643 ret->IActiveScript_iface.lpVtbl = &VBScriptVtbl;
644 ret->IActiveScriptParse_iface.lpVtbl = &VBScriptParseVtbl;
645 ret->IObjectSafety_iface.lpVtbl = &VBScriptSafetyVtbl;
648 ret->state = SCRIPTSTATE_UNINITIALIZED;
649 ret->safeopt = INTERFACE_USES_DISPEX;
651 hres = IActiveScript_QueryInterface(&ret->IActiveScript_iface, riid, ppv);
652 IActiveScript_Release(&ret->IActiveScript_iface);