wer: Add new stubbed wer.dll.
[wine/hramrach.git] / dlls / jscript / jscript.c
blobbcb81fa0c2f6e16cad032894f265ff1991fa5d62
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 SysFreeString(ctx->last_match);
75 heap_free(ctx);
78 static void change_state(JScript *This, SCRIPTSTATE state)
80 if(This->ctx->state == state)
81 return;
83 This->ctx->state = state;
84 IActiveScriptSite_OnStateChange(This->site, state);
87 static inline BOOL is_started(script_ctx_t *ctx)
89 return ctx->state == SCRIPTSTATE_STARTED
90 || ctx->state == SCRIPTSTATE_CONNECTED
91 || ctx->state == SCRIPTSTATE_DISCONNECTED;
94 static HRESULT exec_global_code(JScript *This, parser_ctx_t *parser_ctx)
96 exec_ctx_t *exec_ctx;
97 jsexcept_t jsexcept;
98 HRESULT hres;
100 hres = create_exec_ctx(This->ctx, NULL, This->ctx->global, NULL, &exec_ctx);
101 if(FAILED(hres))
102 return hres;
104 IActiveScriptSite_OnEnterScript(This->site);
106 memset(&jsexcept, 0, sizeof(jsexcept));
107 hres = exec_source(exec_ctx, parser_ctx, parser_ctx->source, EXECT_PROGRAM, &jsexcept, NULL);
108 VariantClear(&jsexcept.var);
109 exec_release(exec_ctx);
111 IActiveScriptSite_OnLeaveScript(This->site);
112 return hres;
115 static void clear_script_queue(JScript *This)
117 parser_ctx_t *iter, *iter2;
119 if(!This->queue_head)
120 return;
122 iter = This->queue_head;
123 while(iter) {
124 iter2 = iter->next;
125 iter->next = NULL;
126 parser_release(iter);
127 iter = iter2;
130 This->queue_head = This->queue_tail = NULL;
133 static void exec_queued_code(JScript *This)
135 parser_ctx_t *iter;
137 for(iter = This->queue_head; iter; iter = iter->next)
138 exec_global_code(This, iter);
140 clear_script_queue(This);
143 static HRESULT set_ctx_site(JScript *This)
145 HRESULT hres;
147 This->ctx->lcid = This->lcid;
149 hres = init_global(This->ctx);
150 if(FAILED(hres))
151 return hres;
153 IActiveScriptSite_AddRef(This->site);
154 This->ctx->site = This->site;
156 change_state(This, SCRIPTSTATE_INITIALIZED);
157 return S_OK;
160 typedef struct {
161 const IServiceProviderVtbl *lpIServiceProviderVtbl;
163 LONG ref;
165 IServiceProvider *sp;
166 } AXSite;
168 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpIServiceProviderVtbl)
170 #define SERVPROV_THIS(iface) DEFINE_THIS(AXSite, IServiceProvider, iface)
172 static HRESULT WINAPI AXSite_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
174 AXSite *This = SERVPROV_THIS(iface);
176 if(IsEqualGUID(&IID_IUnknown, riid)) {
177 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
178 *ppv = SERVPROV(This);
179 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
180 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
181 *ppv = SERVPROV(This);
182 }else {
183 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
184 *ppv = NULL;
185 return E_NOINTERFACE;
188 IUnknown_AddRef((IUnknown*)*ppv);
189 return S_OK;
192 static ULONG WINAPI AXSite_AddRef(IServiceProvider *iface)
194 AXSite *This = SERVPROV_THIS(iface);
195 LONG ref = InterlockedIncrement(&This->ref);
197 TRACE("(%p) ref=%d\n", This, ref);
199 return ref;
202 static ULONG WINAPI AXSite_Release(IServiceProvider *iface)
204 AXSite *This = SERVPROV_THIS(iface);
205 LONG ref = InterlockedDecrement(&This->ref);
207 TRACE("(%p) ref=%d\n", This, ref);
209 if(!ref)
210 heap_free(This);
212 return ref;
215 static HRESULT WINAPI AXSite_QueryService(IServiceProvider *iface,
216 REFGUID guidService, REFIID riid, void **ppv)
218 AXSite *This = SERVPROV_THIS(iface);
220 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
222 return IServiceProvider_QueryService(This->sp, guidService, riid, ppv);
225 #undef SERVPROV_THIS
227 static IServiceProviderVtbl AXSiteVtbl = {
228 AXSite_QueryInterface,
229 AXSite_AddRef,
230 AXSite_Release,
231 AXSite_QueryService
234 IUnknown *create_ax_site(script_ctx_t *ctx)
236 IServiceProvider *sp;
237 AXSite *ret;
238 HRESULT hres;
240 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
241 if(FAILED(hres)) {
242 ERR("Could not get IServiceProvider iface: %08x\n", hres);
243 return NULL;
246 ret = heap_alloc(sizeof(AXSite));
247 if(!ret) {
248 IServiceProvider_Release(sp);
249 return NULL;
252 ret->lpIServiceProviderVtbl = &AXSiteVtbl;
253 ret->ref = 1;
254 ret->sp = sp;
256 return (IUnknown*)SERVPROV(ret);
259 #define ACTSCRIPT_THIS(iface) DEFINE_THIS(JScript, IActiveScript, iface)
261 static HRESULT WINAPI JScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
263 JScript *This = ACTSCRIPT_THIS(iface);
265 *ppv = NULL;
267 if(IsEqualGUID(riid, &IID_IUnknown)) {
268 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
269 *ppv = ACTSCRIPT(This);
270 }else if(IsEqualGUID(riid, &IID_IActiveScript)) {
271 TRACE("(%p)->(IID_IActiveScript %p)\n", This, ppv);
272 *ppv = ACTSCRIPT(This);
273 }else if(IsEqualGUID(riid, &IID_IActiveScriptParse)) {
274 TRACE("(%p)->(IID_IActiveScriptParse %p)\n", This, ppv);
275 *ppv = ASPARSE(This);
276 }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure)) {
277 TRACE("(%p)->(IID_IActiveScriptParseProcedure %p)\n", This, ppv);
278 *ppv = ASPARSEPROC(This);
279 }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure2)) {
280 TRACE("(%p)->(IID_IActiveScriptParseProcedure2 %p)\n", This, ppv);
281 *ppv = ASPARSEPROC(This);
282 }else if(IsEqualGUID(riid, &IID_IActiveScriptProperty)) {
283 TRACE("(%p)->(IID_IActiveScriptProperty %p)\n", This, ppv);
284 *ppv = ACTSCPPROP(This);
285 }else if(IsEqualGUID(riid, &IID_IObjectSafety)) {
286 TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
287 *ppv = OBJSAFETY(This);
290 if(*ppv) {
291 IUnknown_AddRef((IUnknown*)*ppv);
292 return S_OK;
295 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
296 return E_NOINTERFACE;
299 static ULONG WINAPI JScript_AddRef(IActiveScript *iface)
301 JScript *This = ACTSCRIPT_THIS(iface);
302 LONG ref = InterlockedIncrement(&This->ref);
304 TRACE("(%p) ref=%d\n", This, ref);
306 return ref;
309 static ULONG WINAPI JScript_Release(IActiveScript *iface)
311 JScript *This = ACTSCRIPT_THIS(iface);
312 LONG ref = InterlockedDecrement(&This->ref);
314 TRACE("(%p) ref=%d\n", iface, ref);
316 if(!ref) {
317 if(This->ctx && This->ctx->state != SCRIPTSTATE_CLOSED)
318 IActiveScript_Close(ACTSCRIPT(This));
319 if(This->ctx)
320 script_release(This->ctx);
321 heap_free(This);
322 unlock_module();
325 return ref;
328 static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface,
329 IActiveScriptSite *pass)
331 JScript *This = ACTSCRIPT_THIS(iface);
332 LCID lcid;
333 HRESULT hres;
335 TRACE("(%p)->(%p)\n", This, pass);
337 if(!pass)
338 return E_POINTER;
340 if(This->site)
341 return E_UNEXPECTED;
343 if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0))
344 return E_UNEXPECTED;
346 This->site = pass;
347 IActiveScriptSite_AddRef(This->site);
349 hres = IActiveScriptSite_GetLCID(This->site, &lcid);
350 if(hres == S_OK)
351 This->lcid = lcid;
353 return This->ctx ? set_ctx_site(This) : S_OK;
356 static HRESULT WINAPI JScript_GetScriptSite(IActiveScript *iface, REFIID riid,
357 void **ppvObject)
359 JScript *This = ACTSCRIPT_THIS(iface);
360 FIXME("(%p)->()\n", This);
361 return E_NOTIMPL;
364 static HRESULT WINAPI JScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
366 JScript *This = ACTSCRIPT_THIS(iface);
368 TRACE("(%p)->(%d)\n", This, ss);
370 if(!This->ctx || GetCurrentThreadId() != This->thread_id)
371 return E_UNEXPECTED;
373 switch(ss) {
374 case SCRIPTSTATE_STARTED:
375 case SCRIPTSTATE_CONNECTED: /* FIXME */
376 if(This->ctx->state == SCRIPTSTATE_CLOSED)
377 return E_UNEXPECTED;
379 exec_queued_code(This);
380 break;
381 case SCRIPTSTATE_INITIALIZED:
382 FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
383 return S_OK;
384 default:
385 FIXME("unimplemented state %d\n", ss);
386 return E_NOTIMPL;
389 change_state(This, ss);
390 return S_OK;
393 static HRESULT WINAPI JScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
395 JScript *This = ACTSCRIPT_THIS(iface);
397 TRACE("(%p)->(%p)\n", This, pssState);
399 if(!pssState)
400 return E_POINTER;
402 if(!This->thread_id) {
403 *pssState = SCRIPTSTATE_UNINITIALIZED;
404 return S_OK;
407 if(This->thread_id != GetCurrentThreadId())
408 return E_UNEXPECTED;
410 *pssState = This->ctx ? This->ctx->state : SCRIPTSTATE_UNINITIALIZED;
411 return S_OK;
414 static HRESULT WINAPI JScript_Close(IActiveScript *iface)
416 JScript *This = ACTSCRIPT_THIS(iface);
418 TRACE("(%p)->()\n", This);
420 if(This->thread_id != GetCurrentThreadId())
421 return E_UNEXPECTED;
423 if(This->ctx) {
424 if(This->ctx->state == SCRIPTSTATE_CONNECTED)
425 change_state(This, SCRIPTSTATE_DISCONNECTED);
427 clear_script_queue(This);
429 if(This->ctx->state == SCRIPTSTATE_DISCONNECTED)
430 change_state(This, SCRIPTSTATE_INITIALIZED);
432 if(This->ctx->host_global) {
433 IDispatch_Release(This->ctx->host_global);
434 This->ctx->host_global = NULL;
437 if(This->ctx->named_items) {
438 named_item_t *iter, *iter2;
440 iter = This->ctx->named_items;
441 while(iter) {
442 iter2 = iter->next;
444 if(iter->disp)
445 IDispatch_Release(iter->disp);
446 heap_free(iter->name);
447 heap_free(iter);
448 iter = iter2;
451 This->ctx->named_items = NULL;
454 if(This->ctx->secmgr) {
455 IInternetHostSecurityManager_Release(This->ctx->secmgr);
456 This->ctx->secmgr = NULL;
459 if(This->ctx->site) {
460 IActiveScriptSite_Release(This->ctx->site);
461 This->ctx->site = NULL;
464 if (This->site)
465 change_state(This, SCRIPTSTATE_CLOSED);
467 if(This->ctx->global) {
468 jsdisp_release(This->ctx->global);
469 This->ctx->global = NULL;
473 if(This->site) {
474 IActiveScriptSite_Release(This->site);
475 This->site = NULL;
478 return S_OK;
481 static HRESULT WINAPI JScript_AddNamedItem(IActiveScript *iface,
482 LPCOLESTR pstrName, DWORD dwFlags)
484 JScript *This = ACTSCRIPT_THIS(iface);
485 named_item_t *item;
486 IDispatch *disp = NULL;
487 HRESULT hres;
489 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
491 if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->ctx->state == SCRIPTSTATE_CLOSED)
492 return E_UNEXPECTED;
494 if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
495 IUnknown *unk;
497 hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
498 if(FAILED(hres)) {
499 WARN("GetItemInfo failed: %08x\n", hres);
500 return hres;
503 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
504 IUnknown_Release(unk);
505 if(FAILED(hres)) {
506 WARN("object does not implement IDispatch\n");
507 return hres;
510 if(This->ctx->host_global)
511 IDispatch_Release(This->ctx->host_global);
512 IDispatch_AddRef(disp);
513 This->ctx->host_global = disp;
516 item = heap_alloc(sizeof(*item));
517 if(!item) {
518 if(disp)
519 IDispatch_Release(disp);
520 return E_OUTOFMEMORY;
523 item->disp = disp;
524 item->flags = dwFlags;
525 item->name = heap_strdupW(pstrName);
526 if(!item->name) {
527 IDispatch_Release(disp);
528 heap_free(item);
529 return E_OUTOFMEMORY;
532 item->next = This->ctx->named_items;
533 This->ctx->named_items = item;
535 return S_OK;
538 static HRESULT WINAPI JScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
539 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
541 JScript *This = ACTSCRIPT_THIS(iface);
542 FIXME("(%p)->()\n", This);
543 return E_NOTIMPL;
546 static HRESULT WINAPI JScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
547 IDispatch **ppdisp)
549 JScript *This = ACTSCRIPT_THIS(iface);
551 TRACE("(%p)->(%p)\n", This, ppdisp);
553 if(!ppdisp)
554 return E_POINTER;
556 if(This->thread_id != GetCurrentThreadId() || !This->ctx->global) {
557 *ppdisp = NULL;
558 return E_UNEXPECTED;
561 *ppdisp = (IDispatch*)_IDispatchEx_(This->ctx->global);
562 IDispatch_AddRef(*ppdisp);
563 return S_OK;
566 static HRESULT WINAPI JScript_GetCurrentScriptThreadID(IActiveScript *iface,
567 SCRIPTTHREADID *pstridThread)
569 JScript *This = ACTSCRIPT_THIS(iface);
570 FIXME("(%p)->()\n", This);
571 return E_NOTIMPL;
574 static HRESULT WINAPI JScript_GetScriptThreadID(IActiveScript *iface,
575 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
577 JScript *This = ACTSCRIPT_THIS(iface);
578 FIXME("(%p)->()\n", This);
579 return E_NOTIMPL;
582 static HRESULT WINAPI JScript_GetScriptThreadState(IActiveScript *iface,
583 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
585 JScript *This = ACTSCRIPT_THIS(iface);
586 FIXME("(%p)->()\n", This);
587 return E_NOTIMPL;
590 static HRESULT WINAPI JScript_InterruptScriptThread(IActiveScript *iface,
591 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
593 JScript *This = ACTSCRIPT_THIS(iface);
594 FIXME("(%p)->()\n", This);
595 return E_NOTIMPL;
598 static HRESULT WINAPI JScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
600 JScript *This = ACTSCRIPT_THIS(iface);
601 FIXME("(%p)->()\n", This);
602 return E_NOTIMPL;
605 #undef ACTSCRIPT_THIS
607 static const IActiveScriptVtbl JScriptVtbl = {
608 JScript_QueryInterface,
609 JScript_AddRef,
610 JScript_Release,
611 JScript_SetScriptSite,
612 JScript_GetScriptSite,
613 JScript_SetScriptState,
614 JScript_GetScriptState,
615 JScript_Close,
616 JScript_AddNamedItem,
617 JScript_AddTypeLib,
618 JScript_GetScriptDispatch,
619 JScript_GetCurrentScriptThreadID,
620 JScript_GetScriptThreadID,
621 JScript_GetScriptThreadState,
622 JScript_InterruptScriptThread,
623 JScript_Clone
626 #define ASPARSE_THIS(iface) DEFINE_THIS(JScript, IActiveScriptParse, iface)
628 static HRESULT WINAPI JScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
630 JScript *This = ASPARSE_THIS(iface);
631 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
634 static ULONG WINAPI JScriptParse_AddRef(IActiveScriptParse *iface)
636 JScript *This = ASPARSE_THIS(iface);
637 return IActiveScript_AddRef(ACTSCRIPT(This));
640 static ULONG WINAPI JScriptParse_Release(IActiveScriptParse *iface)
642 JScript *This = ASPARSE_THIS(iface);
643 return IActiveScript_Release(ACTSCRIPT(This));
646 static HRESULT WINAPI JScriptParse_InitNew(IActiveScriptParse *iface)
648 JScript *This = ASPARSE_THIS(iface);
649 script_ctx_t *ctx;
651 TRACE("(%p)\n", This);
653 if(This->ctx)
654 return E_UNEXPECTED;
656 ctx = heap_alloc_zero(sizeof(script_ctx_t));
657 if(!ctx)
658 return E_OUTOFMEMORY;
660 ctx->ref = 1;
661 ctx->state = SCRIPTSTATE_UNINITIALIZED;
662 ctx->safeopt = This->safeopt;
663 ctx->version = This->version;
664 jsheap_init(&ctx->tmp_heap);
666 ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
667 if(ctx) {
668 script_release(ctx);
669 return E_UNEXPECTED;
672 return This->site ? set_ctx_site(This) : S_OK;
675 static HRESULT WINAPI JScriptParse_AddScriptlet(IActiveScriptParse *iface,
676 LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
677 LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
678 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
679 BSTR *pbstrName, EXCEPINFO *pexcepinfo)
681 JScript *This = ASPARSE_THIS(iface);
682 FIXME("(%p)->(%s %s %s %s %s %s %s %u %x %p %p)\n", This, debugstr_w(pstrDefaultName),
683 debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
684 debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
685 ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
686 return E_NOTIMPL;
689 static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface,
690 LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
691 LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
692 DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
694 JScript *This = ASPARSE_THIS(iface);
695 parser_ctx_t *parser_ctx;
696 HRESULT hres;
698 TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode),
699 debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
700 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
702 if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
703 return E_UNEXPECTED;
705 hres = script_parse(This->ctx, pstrCode, pstrDelimiter, &parser_ctx);
706 if(FAILED(hres))
707 return hres;
709 if(!is_started(This->ctx)) {
710 if(This->queue_tail)
711 This->queue_tail = This->queue_tail->next = parser_ctx;
712 else
713 This->queue_head = This->queue_tail = parser_ctx;
714 return S_OK;
717 hres = exec_global_code(This, parser_ctx);
718 parser_release(parser_ctx);
720 return hres;
723 #undef ASPARSE_THIS
725 static const IActiveScriptParseVtbl JScriptParseVtbl = {
726 JScriptParse_QueryInterface,
727 JScriptParse_AddRef,
728 JScriptParse_Release,
729 JScriptParse_InitNew,
730 JScriptParse_AddScriptlet,
731 JScriptParse_ParseScriptText
734 #define ASPARSEPROC_THIS(iface) DEFINE_THIS(JScript, IActiveScriptParseProcedure2, iface)
736 static HRESULT WINAPI JScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
738 JScript *This = ASPARSEPROC_THIS(iface);
739 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
742 static ULONG WINAPI JScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
744 JScript *This = ASPARSEPROC_THIS(iface);
745 return IActiveScript_AddRef(ACTSCRIPT(This));
748 static ULONG WINAPI JScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
750 JScript *This = ASPARSEPROC_THIS(iface);
751 return IActiveScript_Release(ACTSCRIPT(This));
754 static HRESULT WINAPI JScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
755 LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
756 LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
757 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
759 JScript *This = ASPARSEPROC_THIS(iface);
760 parser_ctx_t *parser_ctx;
761 DispatchEx *dispex;
762 HRESULT hres;
764 TRACE("(%p)->(%s %s %s %s %p %s %s %u %x %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrFormalParams),
765 debugstr_w(pstrProcedureName), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
766 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLineNumber, dwFlags, ppdisp);
768 if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
769 return E_UNEXPECTED;
771 hres = script_parse(This->ctx, pstrCode, pstrDelimiter, &parser_ctx);
772 if(FAILED(hres)) {
773 WARN("Parse failed %08x\n", hres);
774 return hres;
777 hres = create_source_function(parser_ctx, NULL, parser_ctx->source, NULL, NULL, 0, &dispex);
778 parser_release(parser_ctx);
779 if(FAILED(hres))
780 return hres;
782 *ppdisp = (IDispatch*)_IDispatchEx_(dispex);
783 return S_OK;
786 #undef ASPARSEPROC_THIS
788 static const IActiveScriptParseProcedure2Vtbl JScriptParseProcedureVtbl = {
789 JScriptParseProcedure_QueryInterface,
790 JScriptParseProcedure_AddRef,
791 JScriptParseProcedure_Release,
792 JScriptParseProcedure_ParseProcedureText,
795 #define ACTSCPPROP_THIS(iface) DEFINE_THIS(JScript, IActiveScriptProperty, iface)
797 static HRESULT WINAPI JScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
799 JScript *This = ACTSCPPROP_THIS(iface);
800 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
803 static ULONG WINAPI JScriptProperty_AddRef(IActiveScriptProperty *iface)
805 JScript *This = ACTSCPPROP_THIS(iface);
806 return IActiveScript_AddRef(ACTSCRIPT(This));
809 static ULONG WINAPI JScriptProperty_Release(IActiveScriptProperty *iface)
811 JScript *This = ACTSCPPROP_THIS(iface);
812 return IActiveScript_Release(ACTSCRIPT(This));
815 static HRESULT WINAPI JScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
816 VARIANT *pvarIndex, VARIANT *pvarValue)
818 JScript *This = ACTSCPPROP_THIS(iface);
819 FIXME("(%p)->(%x %p %p)\n", This, dwProperty, pvarIndex, pvarValue);
820 return E_NOTIMPL;
823 static HRESULT WINAPI JScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
824 VARIANT *pvarIndex, VARIANT *pvarValue)
826 JScript *This = ACTSCPPROP_THIS(iface);
828 TRACE("(%p)->(%x %s %s)\n", This, dwProperty, debugstr_variant(pvarIndex), debugstr_variant(pvarValue));
830 if(pvarIndex)
831 FIXME("unsupported pvarIndex\n");
833 switch(dwProperty) {
834 case SCRIPTPROP_INVOKEVERSIONING:
835 if(V_VT(pvarValue) != VT_I4 || V_I4(pvarValue) < 0 || V_I4(pvarValue) > 15) {
836 WARN("invalid value %s\n", debugstr_variant(pvarValue));
837 return E_INVALIDARG;
840 This->version = V_I4(pvarValue);
841 break;
842 default:
843 FIXME("Unimplemented property %x\n", dwProperty);
844 return E_NOTIMPL;
847 return S_OK;
850 #undef ACTSCPPROP_THIS
852 static const IActiveScriptPropertyVtbl JScriptPropertyVtbl = {
853 JScriptProperty_QueryInterface,
854 JScriptProperty_AddRef,
855 JScriptProperty_Release,
856 JScriptProperty_GetProperty,
857 JScriptProperty_SetProperty
860 #define OBJSAFETY_THIS(iface) DEFINE_THIS(JScript, IObjectSafety, iface)
862 static HRESULT WINAPI JScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
864 JScript *This = OBJSAFETY_THIS(iface);
865 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
868 static ULONG WINAPI JScriptSafety_AddRef(IObjectSafety *iface)
870 JScript *This = OBJSAFETY_THIS(iface);
871 return IActiveScript_AddRef(ACTSCRIPT(This));
874 static ULONG WINAPI JScriptSafety_Release(IObjectSafety *iface)
876 JScript *This = OBJSAFETY_THIS(iface);
877 return IActiveScript_Release(ACTSCRIPT(This));
880 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
882 static HRESULT WINAPI JScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
883 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
885 JScript *This = OBJSAFETY_THIS(iface);
887 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
889 if(!pdwSupportedOptions || !pdwEnabledOptions)
890 return E_POINTER;
892 *pdwSupportedOptions = SUPPORTED_OPTIONS;
893 *pdwEnabledOptions = This->safeopt;
895 return S_OK;
898 static HRESULT WINAPI JScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
899 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
901 JScript *This = OBJSAFETY_THIS(iface);
903 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
905 if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
906 return E_FAIL;
908 This->safeopt = dwEnabledOptions & dwEnabledOptions;
909 return S_OK;
912 #undef OBJSAFETY_THIS
914 static const IObjectSafetyVtbl JScriptSafetyVtbl = {
915 JScriptSafety_QueryInterface,
916 JScriptSafety_AddRef,
917 JScriptSafety_Release,
918 JScriptSafety_GetInterfaceSafetyOptions,
919 JScriptSafety_SetInterfaceSafetyOptions
922 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
923 REFIID riid, void **ppv)
925 JScript *ret;
926 HRESULT hres;
928 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
930 lock_module();
932 ret = heap_alloc_zero(sizeof(*ret));
933 if(!ret)
934 return E_OUTOFMEMORY;
936 ret->lpIActiveScriptVtbl = &JScriptVtbl;
937 ret->lpIActiveScriptParseVtbl = &JScriptParseVtbl;
938 ret->lpIActiveScriptParseProcedure2Vtbl = &JScriptParseProcedureVtbl;
939 ret->lpIActiveScriptPropertyVtbl = &JScriptPropertyVtbl;
940 ret->lpIObjectSafetyVtbl = &JScriptSafetyVtbl;
941 ret->ref = 1;
942 ret->safeopt = INTERFACE_USES_DISPEX;
944 hres = IActiveScript_QueryInterface(ACTSCRIPT(ret), riid, ppv);
945 IActiveScript_Release(ACTSCRIPT(ret));
946 return hres;