Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / jscript / jscript.c
blob9a516aa5d713da6402e8aeb7714287319ca8deec
1 /*
2 * Copyright 2008 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
19 #include "jscript.h"
20 #include "engine.h"
21 #include "objsafe.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27 #ifdef _WIN64
29 #define CTXARG_T DWORDLONG
30 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
31 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_64Vtbl
33 #else
35 #define CTXARG_T DWORD
36 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
37 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_32Vtbl
39 #endif
41 typedef struct {
42 const IActiveScriptVtbl *lpIActiveScriptVtbl;
43 const IActiveScriptParseVtbl *lpIActiveScriptParseVtbl;
44 const IActiveScriptParseProcedure2Vtbl *lpIActiveScriptParseProcedure2Vtbl;
45 const IActiveScriptPropertyVtbl *lpIActiveScriptPropertyVtbl;
46 const IObjectSafetyVtbl *lpIObjectSafetyVtbl;
48 LONG ref;
50 DWORD safeopt;
51 script_ctx_t *ctx;
52 LONG thread_id;
53 LCID lcid;
54 DWORD version;
56 IActiveScriptSite *site;
58 parser_ctx_t *queue_head;
59 parser_ctx_t *queue_tail;
60 } JScript;
62 #define ACTSCRIPT(x) ((IActiveScript*) &(x)->lpIActiveScriptVtbl)
63 #define ASPARSE(x) (&(x)->lpIActiveScriptParseVtbl)
64 #define ASPARSEPROC(x) (&(x)->lpIActiveScriptParseProcedure2Vtbl)
65 #define ACTSCPPROP(x) (&(x)->lpIActiveScriptPropertyVtbl)
66 #define OBJSAFETY(x) (&(x)->lpIObjectSafetyVtbl)
68 void script_release(script_ctx_t *ctx)
70 if(--ctx->ref)
71 return;
73 jsheap_free(&ctx->tmp_heap);
74 heap_free(ctx);
77 static void change_state(JScript *This, SCRIPTSTATE state)
79 if(This->ctx->state == state)
80 return;
82 This->ctx->state = state;
83 IActiveScriptSite_OnStateChange(This->site, state);
86 static inline BOOL is_started(script_ctx_t *ctx)
88 return ctx->state == SCRIPTSTATE_STARTED
89 || ctx->state == SCRIPTSTATE_CONNECTED
90 || ctx->state == SCRIPTSTATE_DISCONNECTED;
93 static HRESULT exec_global_code(JScript *This, parser_ctx_t *parser_ctx)
95 exec_ctx_t *exec_ctx;
96 jsexcept_t jsexcept;
97 HRESULT hres;
99 hres = create_exec_ctx(This->ctx, NULL, This->ctx->global, NULL, &exec_ctx);
100 if(FAILED(hres))
101 return hres;
103 IActiveScriptSite_OnEnterScript(This->site);
105 memset(&jsexcept, 0, sizeof(jsexcept));
106 hres = exec_source(exec_ctx, parser_ctx, parser_ctx->source, EXECT_PROGRAM, &jsexcept, NULL);
107 VariantClear(&jsexcept.var);
108 exec_release(exec_ctx);
110 IActiveScriptSite_OnLeaveScript(This->site);
111 return hres;
114 static void clear_script_queue(JScript *This)
116 parser_ctx_t *iter, *iter2;
118 if(!This->queue_head)
119 return;
121 iter = This->queue_head;
122 while(iter) {
123 iter2 = iter->next;
124 iter->next = NULL;
125 parser_release(iter);
126 iter = iter2;
129 This->queue_head = This->queue_tail = NULL;
132 static void exec_queued_code(JScript *This)
134 parser_ctx_t *iter;
136 for(iter = This->queue_head; iter; iter = iter->next)
137 exec_global_code(This, iter);
139 clear_script_queue(This);
142 static HRESULT set_ctx_site(JScript *This)
144 HRESULT hres;
146 This->ctx->lcid = This->lcid;
148 hres = init_global(This->ctx);
149 if(FAILED(hres))
150 return hres;
152 IActiveScriptSite_AddRef(This->site);
153 This->ctx->site = This->site;
155 change_state(This, SCRIPTSTATE_INITIALIZED);
156 return S_OK;
159 typedef struct {
160 const IServiceProviderVtbl *lpIServiceProviderVtbl;
162 LONG ref;
164 IServiceProvider *sp;
165 } AXSite;
167 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpIServiceProviderVtbl)
169 #define SERVPROV_THIS(iface) DEFINE_THIS(AXSite, IServiceProvider, iface)
171 static HRESULT WINAPI AXSite_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
173 AXSite *This = SERVPROV_THIS(iface);
175 if(IsEqualGUID(&IID_IUnknown, riid)) {
176 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
177 *ppv = SERVPROV(This);
178 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
179 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
180 *ppv = SERVPROV(This);
181 }else {
182 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
183 *ppv = NULL;
184 return E_NOINTERFACE;
187 IUnknown_AddRef((IUnknown*)*ppv);
188 return S_OK;
191 static ULONG WINAPI AXSite_AddRef(IServiceProvider *iface)
193 AXSite *This = SERVPROV_THIS(iface);
194 LONG ref = InterlockedIncrement(&This->ref);
196 TRACE("(%p) ref=%d\n", This, ref);
198 return ref;
201 static ULONG WINAPI AXSite_Release(IServiceProvider *iface)
203 AXSite *This = SERVPROV_THIS(iface);
204 LONG ref = InterlockedDecrement(&This->ref);
206 TRACE("(%p) ref=%d\n", This, ref);
208 if(!ref)
209 heap_free(This);
211 return ref;
214 static HRESULT WINAPI AXSite_QueryService(IServiceProvider *iface,
215 REFGUID guidService, REFIID riid, void **ppv)
217 AXSite *This = SERVPROV_THIS(iface);
219 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
221 return IServiceProvider_QueryService(This->sp, guidService, riid, ppv);
224 #undef SERVPROV_THIS
226 static IServiceProviderVtbl AXSiteVtbl = {
227 AXSite_QueryInterface,
228 AXSite_AddRef,
229 AXSite_Release,
230 AXSite_QueryService
233 IUnknown *create_ax_site(script_ctx_t *ctx)
235 IServiceProvider *sp;
236 AXSite *ret;
237 HRESULT hres;
239 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
240 if(FAILED(hres)) {
241 ERR("Could not get IServiceProvider iface: %08x\n", hres);
242 return NULL;
245 ret = heap_alloc(sizeof(AXSite));
246 if(!ret) {
247 IServiceProvider_Release(sp);
248 return NULL;
251 ret->lpIServiceProviderVtbl = &AXSiteVtbl;
252 ret->ref = 1;
253 ret->sp = sp;
255 return (IUnknown*)SERVPROV(ret);
258 #define ACTSCRIPT_THIS(iface) DEFINE_THIS(JScript, IActiveScript, iface)
260 static HRESULT WINAPI JScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
262 JScript *This = ACTSCRIPT_THIS(iface);
264 *ppv = NULL;
266 if(IsEqualGUID(riid, &IID_IUnknown)) {
267 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
268 *ppv = ACTSCRIPT(This);
269 }else if(IsEqualGUID(riid, &IID_IActiveScript)) {
270 TRACE("(%p)->(IID_IActiveScript %p)\n", This, ppv);
271 *ppv = ACTSCRIPT(This);
272 }else if(IsEqualGUID(riid, &IID_IActiveScriptParse)) {
273 TRACE("(%p)->(IID_IActiveScriptParse %p)\n", This, ppv);
274 *ppv = ASPARSE(This);
275 }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure)) {
276 TRACE("(%p)->(IID_IActiveScriptParseProcedure %p)\n", This, ppv);
277 *ppv = ASPARSEPROC(This);
278 }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure2)) {
279 TRACE("(%p)->(IID_IActiveScriptParseProcedure2 %p)\n", This, ppv);
280 *ppv = ASPARSEPROC(This);
281 }else if(IsEqualGUID(riid, &IID_IActiveScriptProperty)) {
282 TRACE("(%p)->(IID_IActiveScriptProperty %p)\n", This, ppv);
283 *ppv = ACTSCPPROP(This);
284 }else if(IsEqualGUID(riid, &IID_IObjectSafety)) {
285 TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
286 *ppv = OBJSAFETY(This);
289 if(*ppv) {
290 IUnknown_AddRef((IUnknown*)*ppv);
291 return S_OK;
294 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
295 return E_NOINTERFACE;
298 static ULONG WINAPI JScript_AddRef(IActiveScript *iface)
300 JScript *This = ACTSCRIPT_THIS(iface);
301 LONG ref = InterlockedIncrement(&This->ref);
303 TRACE("(%p) ref=%d\n", This, ref);
305 return ref;
308 static ULONG WINAPI JScript_Release(IActiveScript *iface)
310 JScript *This = ACTSCRIPT_THIS(iface);
311 LONG ref = InterlockedDecrement(&This->ref);
313 TRACE("(%p) ref=%d\n", iface, ref);
315 if(!ref) {
316 if(This->ctx && This->ctx->state != SCRIPTSTATE_CLOSED)
317 IActiveScript_Close(ACTSCRIPT(This));
318 if(This->ctx)
319 script_release(This->ctx);
320 heap_free(This);
321 unlock_module();
324 return ref;
327 static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface,
328 IActiveScriptSite *pass)
330 JScript *This = ACTSCRIPT_THIS(iface);
331 LCID lcid;
332 HRESULT hres;
334 TRACE("(%p)->(%p)\n", This, pass);
336 if(!pass)
337 return E_POINTER;
339 if(This->site)
340 return E_UNEXPECTED;
342 if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0))
343 return E_UNEXPECTED;
345 This->site = pass;
346 IActiveScriptSite_AddRef(This->site);
348 hres = IActiveScriptSite_GetLCID(This->site, &lcid);
349 if(hres == S_OK)
350 This->lcid = lcid;
352 return This->ctx ? set_ctx_site(This) : S_OK;
355 static HRESULT WINAPI JScript_GetScriptSite(IActiveScript *iface, REFIID riid,
356 void **ppvObject)
358 JScript *This = ACTSCRIPT_THIS(iface);
359 FIXME("(%p)->()\n", This);
360 return E_NOTIMPL;
363 static HRESULT WINAPI JScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
365 JScript *This = ACTSCRIPT_THIS(iface);
367 TRACE("(%p)->(%d)\n", This, ss);
369 if(!This->ctx || GetCurrentThreadId() != This->thread_id)
370 return E_UNEXPECTED;
372 switch(ss) {
373 case SCRIPTSTATE_STARTED:
374 case SCRIPTSTATE_CONNECTED: /* FIXME */
375 if(This->ctx->state == SCRIPTSTATE_CLOSED)
376 return E_UNEXPECTED;
378 exec_queued_code(This);
379 break;
380 default:
381 FIXME("unimplemented state %d\n", ss);
382 return E_NOTIMPL;
385 change_state(This, ss);
386 return S_OK;
389 static HRESULT WINAPI JScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
391 JScript *This = ACTSCRIPT_THIS(iface);
393 TRACE("(%p)->(%p)\n", This, pssState);
395 if(!pssState)
396 return E_POINTER;
398 if(!This->thread_id) {
399 *pssState = SCRIPTSTATE_UNINITIALIZED;
400 return S_OK;
403 if(This->thread_id != GetCurrentThreadId())
404 return E_UNEXPECTED;
406 *pssState = This->ctx ? This->ctx->state : SCRIPTSTATE_UNINITIALIZED;
407 return S_OK;
410 static HRESULT WINAPI JScript_Close(IActiveScript *iface)
412 JScript *This = ACTSCRIPT_THIS(iface);
414 TRACE("(%p)->()\n", This);
416 if(This->thread_id != GetCurrentThreadId())
417 return E_UNEXPECTED;
419 if(This->ctx) {
420 if(This->ctx->state == SCRIPTSTATE_CONNECTED)
421 change_state(This, SCRIPTSTATE_DISCONNECTED);
423 clear_script_queue(This);
425 if(This->ctx->state == SCRIPTSTATE_DISCONNECTED)
426 change_state(This, SCRIPTSTATE_INITIALIZED);
428 if(This->ctx->host_global) {
429 IDispatch_Release(This->ctx->host_global);
430 This->ctx->host_global = NULL;
433 if(This->ctx->named_items) {
434 named_item_t *iter, *iter2;
436 iter = This->ctx->named_items;
437 while(iter) {
438 iter2 = iter->next;
440 if(iter->disp)
441 IDispatch_Release(iter->disp);
442 heap_free(iter->name);
443 heap_free(iter);
444 iter = iter2;
447 This->ctx->named_items = NULL;
450 if(This->ctx->secmgr) {
451 IInternetHostSecurityManager_Release(This->ctx->secmgr);
452 This->ctx->secmgr = NULL;
455 if(This->ctx->site) {
456 IActiveScriptSite_Release(This->ctx->site);
457 This->ctx->site = NULL;
460 if (This->site)
461 change_state(This, SCRIPTSTATE_CLOSED);
463 if(This->ctx->global) {
464 jsdisp_release(This->ctx->global);
465 This->ctx->global = NULL;
469 if(This->site) {
470 IActiveScriptSite_Release(This->site);
471 This->site = NULL;
474 return S_OK;
477 static HRESULT WINAPI JScript_AddNamedItem(IActiveScript *iface,
478 LPCOLESTR pstrName, DWORD dwFlags)
480 JScript *This = ACTSCRIPT_THIS(iface);
481 named_item_t *item;
482 IDispatch *disp = NULL;
483 HRESULT hres;
485 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
487 if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->ctx->state == SCRIPTSTATE_CLOSED)
488 return E_UNEXPECTED;
490 if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
491 IUnknown *unk;
493 hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
494 if(FAILED(hres)) {
495 WARN("GetItemInfo failed: %08x\n", hres);
496 return hres;
499 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
500 IUnknown_Release(unk);
501 if(FAILED(hres)) {
502 WARN("object does not implement IDispatch\n");
503 return hres;
506 if(This->ctx->host_global)
507 IDispatch_Release(This->ctx->host_global);
508 IDispatch_AddRef(disp);
509 This->ctx->host_global = disp;
512 item = heap_alloc(sizeof(*item));
513 if(!item) {
514 if(disp)
515 IDispatch_Release(disp);
516 return E_OUTOFMEMORY;
519 item->disp = disp;
520 item->flags = dwFlags;
521 item->name = heap_strdupW(pstrName);
522 if(!item->name) {
523 IDispatch_Release(disp);
524 heap_free(item);
525 return E_OUTOFMEMORY;
528 item->next = This->ctx->named_items;
529 This->ctx->named_items = item;
531 return S_OK;
534 static HRESULT WINAPI JScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
535 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
537 JScript *This = ACTSCRIPT_THIS(iface);
538 FIXME("(%p)->()\n", This);
539 return E_NOTIMPL;
542 static HRESULT WINAPI JScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
543 IDispatch **ppdisp)
545 JScript *This = ACTSCRIPT_THIS(iface);
547 TRACE("(%p)->(%p)\n", This, ppdisp);
549 if(!ppdisp)
550 return E_POINTER;
552 if(This->thread_id != GetCurrentThreadId() || !This->ctx->global) {
553 *ppdisp = NULL;
554 return E_UNEXPECTED;
557 *ppdisp = (IDispatch*)_IDispatchEx_(This->ctx->global);
558 IDispatch_AddRef(*ppdisp);
559 return S_OK;
562 static HRESULT WINAPI JScript_GetCurrentScriptThreadID(IActiveScript *iface,
563 SCRIPTTHREADID *pstridThread)
565 JScript *This = ACTSCRIPT_THIS(iface);
566 FIXME("(%p)->()\n", This);
567 return E_NOTIMPL;
570 static HRESULT WINAPI JScript_GetScriptThreadID(IActiveScript *iface,
571 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
573 JScript *This = ACTSCRIPT_THIS(iface);
574 FIXME("(%p)->()\n", This);
575 return E_NOTIMPL;
578 static HRESULT WINAPI JScript_GetScriptThreadState(IActiveScript *iface,
579 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
581 JScript *This = ACTSCRIPT_THIS(iface);
582 FIXME("(%p)->()\n", This);
583 return E_NOTIMPL;
586 static HRESULT WINAPI JScript_InterruptScriptThread(IActiveScript *iface,
587 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
589 JScript *This = ACTSCRIPT_THIS(iface);
590 FIXME("(%p)->()\n", This);
591 return E_NOTIMPL;
594 static HRESULT WINAPI JScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
596 JScript *This = ACTSCRIPT_THIS(iface);
597 FIXME("(%p)->()\n", This);
598 return E_NOTIMPL;
601 #undef ACTSCRIPT_THIS
603 static const IActiveScriptVtbl JScriptVtbl = {
604 JScript_QueryInterface,
605 JScript_AddRef,
606 JScript_Release,
607 JScript_SetScriptSite,
608 JScript_GetScriptSite,
609 JScript_SetScriptState,
610 JScript_GetScriptState,
611 JScript_Close,
612 JScript_AddNamedItem,
613 JScript_AddTypeLib,
614 JScript_GetScriptDispatch,
615 JScript_GetCurrentScriptThreadID,
616 JScript_GetScriptThreadID,
617 JScript_GetScriptThreadState,
618 JScript_InterruptScriptThread,
619 JScript_Clone
622 #define ASPARSE_THIS(iface) DEFINE_THIS(JScript, IActiveScriptParse, iface)
624 static HRESULT WINAPI JScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
626 JScript *This = ASPARSE_THIS(iface);
627 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
630 static ULONG WINAPI JScriptParse_AddRef(IActiveScriptParse *iface)
632 JScript *This = ASPARSE_THIS(iface);
633 return IActiveScript_AddRef(ACTSCRIPT(This));
636 static ULONG WINAPI JScriptParse_Release(IActiveScriptParse *iface)
638 JScript *This = ASPARSE_THIS(iface);
639 return IActiveScript_Release(ACTSCRIPT(This));
642 static HRESULT WINAPI JScriptParse_InitNew(IActiveScriptParse *iface)
644 JScript *This = ASPARSE_THIS(iface);
645 script_ctx_t *ctx;
647 TRACE("(%p)\n", This);
649 if(This->ctx)
650 return E_UNEXPECTED;
652 ctx = heap_alloc_zero(sizeof(script_ctx_t));
653 if(!ctx)
654 return E_OUTOFMEMORY;
656 ctx->ref = 1;
657 ctx->state = SCRIPTSTATE_UNINITIALIZED;
658 ctx->safeopt = This->safeopt;
659 ctx->version = This->version;
660 jsheap_init(&ctx->tmp_heap);
662 ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
663 if(ctx) {
664 script_release(ctx);
665 return E_UNEXPECTED;
668 return This->site ? set_ctx_site(This) : S_OK;
671 static HRESULT WINAPI JScriptParse_AddScriptlet(IActiveScriptParse *iface,
672 LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
673 LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
674 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
675 BSTR *pbstrName, EXCEPINFO *pexcepinfo)
677 JScript *This = ASPARSE_THIS(iface);
678 FIXME("(%p)->(%s %s %s %s %s %s %s %u %x %p %p)\n", This, debugstr_w(pstrDefaultName),
679 debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
680 debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
681 ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
682 return E_NOTIMPL;
685 static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface,
686 LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
687 LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
688 DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
690 JScript *This = ASPARSE_THIS(iface);
691 parser_ctx_t *parser_ctx;
692 HRESULT hres;
694 TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode),
695 debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
696 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
698 if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
699 return E_UNEXPECTED;
701 hres = script_parse(This->ctx, pstrCode, pstrDelimiter, &parser_ctx);
702 if(FAILED(hres))
703 return hres;
705 if(!is_started(This->ctx)) {
706 if(This->queue_tail)
707 This->queue_tail = This->queue_tail->next = parser_ctx;
708 else
709 This->queue_head = This->queue_tail = parser_ctx;
710 return S_OK;
713 hres = exec_global_code(This, parser_ctx);
714 parser_release(parser_ctx);
716 return hres;
719 #undef ASPARSE_THIS
721 static const IActiveScriptParseVtbl JScriptParseVtbl = {
722 JScriptParse_QueryInterface,
723 JScriptParse_AddRef,
724 JScriptParse_Release,
725 JScriptParse_InitNew,
726 JScriptParse_AddScriptlet,
727 JScriptParse_ParseScriptText
730 #define ASPARSEPROC_THIS(iface) DEFINE_THIS(JScript, IActiveScriptParseProcedure2, iface)
732 static HRESULT WINAPI JScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
734 JScript *This = ASPARSEPROC_THIS(iface);
735 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
738 static ULONG WINAPI JScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
740 JScript *This = ASPARSEPROC_THIS(iface);
741 return IActiveScript_AddRef(ACTSCRIPT(This));
744 static ULONG WINAPI JScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
746 JScript *This = ASPARSEPROC_THIS(iface);
747 return IActiveScript_Release(ACTSCRIPT(This));
750 static HRESULT WINAPI JScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
751 LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
752 LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
753 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
755 JScript *This = ASPARSEPROC_THIS(iface);
756 parser_ctx_t *parser_ctx;
757 DispatchEx *dispex;
758 HRESULT hres;
760 TRACE("(%p)->(%s %s %s %s %p %s %s %u %x %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrFormalParams),
761 debugstr_w(pstrProcedureName), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
762 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLineNumber, dwFlags, ppdisp);
764 if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
765 return E_UNEXPECTED;
767 hres = script_parse(This->ctx, pstrCode, pstrDelimiter, &parser_ctx);
768 if(FAILED(hres)) {
769 WARN("Parse failed %08x\n", hres);
770 return hres;
773 hres = create_source_function(parser_ctx, NULL, parser_ctx->source, NULL, NULL, 0, &dispex);
774 parser_release(parser_ctx);
775 if(FAILED(hres))
776 return hres;
778 *ppdisp = (IDispatch*)_IDispatchEx_(dispex);
779 return S_OK;
782 #undef ASPARSEPROC_THIS
784 static const IActiveScriptParseProcedure2Vtbl JScriptParseProcedureVtbl = {
785 JScriptParseProcedure_QueryInterface,
786 JScriptParseProcedure_AddRef,
787 JScriptParseProcedure_Release,
788 JScriptParseProcedure_ParseProcedureText,
791 #define ACTSCPPROP_THIS(iface) DEFINE_THIS(JScript, IActiveScriptProperty, iface)
793 static HRESULT WINAPI JScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
795 JScript *This = ACTSCPPROP_THIS(iface);
796 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
799 static ULONG WINAPI JScriptProperty_AddRef(IActiveScriptProperty *iface)
801 JScript *This = ACTSCPPROP_THIS(iface);
802 return IActiveScript_AddRef(ACTSCRIPT(This));
805 static ULONG WINAPI JScriptProperty_Release(IActiveScriptProperty *iface)
807 JScript *This = ACTSCPPROP_THIS(iface);
808 return IActiveScript_Release(ACTSCRIPT(This));
811 static HRESULT WINAPI JScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
812 VARIANT *pvarIndex, VARIANT *pvarValue)
814 JScript *This = ACTSCPPROP_THIS(iface);
815 FIXME("(%p)->(%x %p %p)\n", This, dwProperty, pvarIndex, pvarValue);
816 return E_NOTIMPL;
819 static HRESULT WINAPI JScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
820 VARIANT *pvarIndex, VARIANT *pvarValue)
822 JScript *This = ACTSCPPROP_THIS(iface);
824 TRACE("(%p)->(%x %s %s)\n", This, dwProperty, debugstr_variant(pvarIndex), debugstr_variant(pvarValue));
826 if(pvarIndex)
827 FIXME("unsupported pvarIndex\n");
829 switch(dwProperty) {
830 case SCRIPTPROP_INVOKEVERSIONING:
831 if(V_VT(pvarValue) != VT_I4 || V_I4(pvarValue) < 0 || V_I4(pvarValue) > 15) {
832 WARN("invalid value %s\n", debugstr_variant(pvarValue));
833 return E_INVALIDARG;
836 This->version = V_I4(pvarValue);
837 break;
838 default:
839 FIXME("Unimplemented property %x\n", dwProperty);
840 return E_NOTIMPL;
843 return S_OK;
846 #undef ACTSCPPROP_THIS
848 static const IActiveScriptPropertyVtbl JScriptPropertyVtbl = {
849 JScriptProperty_QueryInterface,
850 JScriptProperty_AddRef,
851 JScriptProperty_Release,
852 JScriptProperty_GetProperty,
853 JScriptProperty_SetProperty
856 #define OBJSAFETY_THIS(iface) DEFINE_THIS(JScript, IObjectSafety, iface)
858 static HRESULT WINAPI JScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
860 JScript *This = OBJSAFETY_THIS(iface);
861 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
864 static ULONG WINAPI JScriptSafety_AddRef(IObjectSafety *iface)
866 JScript *This = OBJSAFETY_THIS(iface);
867 return IActiveScript_AddRef(ACTSCRIPT(This));
870 static ULONG WINAPI JScriptSafety_Release(IObjectSafety *iface)
872 JScript *This = OBJSAFETY_THIS(iface);
873 return IActiveScript_Release(ACTSCRIPT(This));
876 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
878 static HRESULT WINAPI JScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
879 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
881 JScript *This = OBJSAFETY_THIS(iface);
883 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
885 if(!pdwSupportedOptions || !pdwEnabledOptions)
886 return E_POINTER;
888 *pdwSupportedOptions = SUPPORTED_OPTIONS;
889 *pdwEnabledOptions = This->safeopt;
891 return S_OK;
894 static HRESULT WINAPI JScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
895 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
897 JScript *This = OBJSAFETY_THIS(iface);
899 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
901 if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
902 return E_FAIL;
904 This->safeopt = dwEnabledOptions & dwEnabledOptions;
905 return S_OK;
908 #undef OBJSAFETY_THIS
910 static const IObjectSafetyVtbl JScriptSafetyVtbl = {
911 JScriptSafety_QueryInterface,
912 JScriptSafety_AddRef,
913 JScriptSafety_Release,
914 JScriptSafety_GetInterfaceSafetyOptions,
915 JScriptSafety_SetInterfaceSafetyOptions
918 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
919 REFIID riid, void **ppv)
921 JScript *ret;
922 HRESULT hres;
924 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
926 lock_module();
928 ret = heap_alloc_zero(sizeof(*ret));
929 if(!ret)
930 return E_OUTOFMEMORY;
932 ret->lpIActiveScriptVtbl = &JScriptVtbl;
933 ret->lpIActiveScriptParseVtbl = &JScriptParseVtbl;
934 ret->lpIActiveScriptParseProcedure2Vtbl = &JScriptParseProcedureVtbl;
935 ret->lpIActiveScriptPropertyVtbl = &JScriptPropertyVtbl;
936 ret->lpIObjectSafetyVtbl = &JScriptSafetyVtbl;
937 ret->ref = 1;
938 ret->safeopt = INTERFACE_USES_DISPEX;
940 hres = IActiveScript_QueryInterface(ACTSCRIPT(ret), riid, ppv);
941 IActiveScript_Release(ACTSCRIPT(ret));
942 return hres;