wined3d: read_from_framebuffer_texture() isn't suitable for readback of onscreen...
[wine/testsucceed.git] / dlls / jscript / jscript.c
blobc7c77e3aa39639636b27ad387633c3d069dbf7d5
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 default:
382 FIXME("unimplemented state %d\n", ss);
383 return E_NOTIMPL;
386 change_state(This, ss);
387 return S_OK;
390 static HRESULT WINAPI JScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
392 JScript *This = ACTSCRIPT_THIS(iface);
394 TRACE("(%p)->(%p)\n", This, pssState);
396 if(!pssState)
397 return E_POINTER;
399 if(!This->thread_id) {
400 *pssState = SCRIPTSTATE_UNINITIALIZED;
401 return S_OK;
404 if(This->thread_id != GetCurrentThreadId())
405 return E_UNEXPECTED;
407 *pssState = This->ctx ? This->ctx->state : SCRIPTSTATE_UNINITIALIZED;
408 return S_OK;
411 static HRESULT WINAPI JScript_Close(IActiveScript *iface)
413 JScript *This = ACTSCRIPT_THIS(iface);
415 TRACE("(%p)->()\n", This);
417 if(This->thread_id != GetCurrentThreadId())
418 return E_UNEXPECTED;
420 if(This->ctx) {
421 if(This->ctx->state == SCRIPTSTATE_CONNECTED)
422 change_state(This, SCRIPTSTATE_DISCONNECTED);
424 clear_script_queue(This);
426 if(This->ctx->state == SCRIPTSTATE_DISCONNECTED)
427 change_state(This, SCRIPTSTATE_INITIALIZED);
429 if(This->ctx->host_global) {
430 IDispatch_Release(This->ctx->host_global);
431 This->ctx->host_global = NULL;
434 if(This->ctx->named_items) {
435 named_item_t *iter, *iter2;
437 iter = This->ctx->named_items;
438 while(iter) {
439 iter2 = iter->next;
441 if(iter->disp)
442 IDispatch_Release(iter->disp);
443 heap_free(iter->name);
444 heap_free(iter);
445 iter = iter2;
448 This->ctx->named_items = NULL;
451 if(This->ctx->secmgr) {
452 IInternetHostSecurityManager_Release(This->ctx->secmgr);
453 This->ctx->secmgr = NULL;
456 if(This->ctx->site) {
457 IActiveScriptSite_Release(This->ctx->site);
458 This->ctx->site = NULL;
461 if (This->site)
462 change_state(This, SCRIPTSTATE_CLOSED);
464 if(This->ctx->global) {
465 jsdisp_release(This->ctx->global);
466 This->ctx->global = NULL;
470 if(This->site) {
471 IActiveScriptSite_Release(This->site);
472 This->site = NULL;
475 return S_OK;
478 static HRESULT WINAPI JScript_AddNamedItem(IActiveScript *iface,
479 LPCOLESTR pstrName, DWORD dwFlags)
481 JScript *This = ACTSCRIPT_THIS(iface);
482 named_item_t *item;
483 IDispatch *disp = NULL;
484 HRESULT hres;
486 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
488 if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->ctx->state == SCRIPTSTATE_CLOSED)
489 return E_UNEXPECTED;
491 if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
492 IUnknown *unk;
494 hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
495 if(FAILED(hres)) {
496 WARN("GetItemInfo failed: %08x\n", hres);
497 return hres;
500 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
501 IUnknown_Release(unk);
502 if(FAILED(hres)) {
503 WARN("object does not implement IDispatch\n");
504 return hres;
507 if(This->ctx->host_global)
508 IDispatch_Release(This->ctx->host_global);
509 IDispatch_AddRef(disp);
510 This->ctx->host_global = disp;
513 item = heap_alloc(sizeof(*item));
514 if(!item) {
515 if(disp)
516 IDispatch_Release(disp);
517 return E_OUTOFMEMORY;
520 item->disp = disp;
521 item->flags = dwFlags;
522 item->name = heap_strdupW(pstrName);
523 if(!item->name) {
524 IDispatch_Release(disp);
525 heap_free(item);
526 return E_OUTOFMEMORY;
529 item->next = This->ctx->named_items;
530 This->ctx->named_items = item;
532 return S_OK;
535 static HRESULT WINAPI JScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
536 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
538 JScript *This = ACTSCRIPT_THIS(iface);
539 FIXME("(%p)->()\n", This);
540 return E_NOTIMPL;
543 static HRESULT WINAPI JScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
544 IDispatch **ppdisp)
546 JScript *This = ACTSCRIPT_THIS(iface);
548 TRACE("(%p)->(%p)\n", This, ppdisp);
550 if(!ppdisp)
551 return E_POINTER;
553 if(This->thread_id != GetCurrentThreadId() || !This->ctx->global) {
554 *ppdisp = NULL;
555 return E_UNEXPECTED;
558 *ppdisp = (IDispatch*)_IDispatchEx_(This->ctx->global);
559 IDispatch_AddRef(*ppdisp);
560 return S_OK;
563 static HRESULT WINAPI JScript_GetCurrentScriptThreadID(IActiveScript *iface,
564 SCRIPTTHREADID *pstridThread)
566 JScript *This = ACTSCRIPT_THIS(iface);
567 FIXME("(%p)->()\n", This);
568 return E_NOTIMPL;
571 static HRESULT WINAPI JScript_GetScriptThreadID(IActiveScript *iface,
572 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
574 JScript *This = ACTSCRIPT_THIS(iface);
575 FIXME("(%p)->()\n", This);
576 return E_NOTIMPL;
579 static HRESULT WINAPI JScript_GetScriptThreadState(IActiveScript *iface,
580 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
582 JScript *This = ACTSCRIPT_THIS(iface);
583 FIXME("(%p)->()\n", This);
584 return E_NOTIMPL;
587 static HRESULT WINAPI JScript_InterruptScriptThread(IActiveScript *iface,
588 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
590 JScript *This = ACTSCRIPT_THIS(iface);
591 FIXME("(%p)->()\n", This);
592 return E_NOTIMPL;
595 static HRESULT WINAPI JScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
597 JScript *This = ACTSCRIPT_THIS(iface);
598 FIXME("(%p)->()\n", This);
599 return E_NOTIMPL;
602 #undef ACTSCRIPT_THIS
604 static const IActiveScriptVtbl JScriptVtbl = {
605 JScript_QueryInterface,
606 JScript_AddRef,
607 JScript_Release,
608 JScript_SetScriptSite,
609 JScript_GetScriptSite,
610 JScript_SetScriptState,
611 JScript_GetScriptState,
612 JScript_Close,
613 JScript_AddNamedItem,
614 JScript_AddTypeLib,
615 JScript_GetScriptDispatch,
616 JScript_GetCurrentScriptThreadID,
617 JScript_GetScriptThreadID,
618 JScript_GetScriptThreadState,
619 JScript_InterruptScriptThread,
620 JScript_Clone
623 #define ASPARSE_THIS(iface) DEFINE_THIS(JScript, IActiveScriptParse, iface)
625 static HRESULT WINAPI JScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
627 JScript *This = ASPARSE_THIS(iface);
628 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
631 static ULONG WINAPI JScriptParse_AddRef(IActiveScriptParse *iface)
633 JScript *This = ASPARSE_THIS(iface);
634 return IActiveScript_AddRef(ACTSCRIPT(This));
637 static ULONG WINAPI JScriptParse_Release(IActiveScriptParse *iface)
639 JScript *This = ASPARSE_THIS(iface);
640 return IActiveScript_Release(ACTSCRIPT(This));
643 static HRESULT WINAPI JScriptParse_InitNew(IActiveScriptParse *iface)
645 JScript *This = ASPARSE_THIS(iface);
646 script_ctx_t *ctx;
648 TRACE("(%p)\n", This);
650 if(This->ctx)
651 return E_UNEXPECTED;
653 ctx = heap_alloc_zero(sizeof(script_ctx_t));
654 if(!ctx)
655 return E_OUTOFMEMORY;
657 ctx->ref = 1;
658 ctx->state = SCRIPTSTATE_UNINITIALIZED;
659 ctx->safeopt = This->safeopt;
660 ctx->version = This->version;
661 jsheap_init(&ctx->tmp_heap);
663 ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
664 if(ctx) {
665 script_release(ctx);
666 return E_UNEXPECTED;
669 return This->site ? set_ctx_site(This) : S_OK;
672 static HRESULT WINAPI JScriptParse_AddScriptlet(IActiveScriptParse *iface,
673 LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
674 LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
675 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
676 BSTR *pbstrName, EXCEPINFO *pexcepinfo)
678 JScript *This = ASPARSE_THIS(iface);
679 FIXME("(%p)->(%s %s %s %s %s %s %s %u %x %p %p)\n", This, debugstr_w(pstrDefaultName),
680 debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
681 debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
682 ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
683 return E_NOTIMPL;
686 static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface,
687 LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
688 LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
689 DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
691 JScript *This = ASPARSE_THIS(iface);
692 parser_ctx_t *parser_ctx;
693 HRESULT hres;
695 TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode),
696 debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
697 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
699 if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
700 return E_UNEXPECTED;
702 hres = script_parse(This->ctx, pstrCode, pstrDelimiter, &parser_ctx);
703 if(FAILED(hres))
704 return hres;
706 if(!is_started(This->ctx)) {
707 if(This->queue_tail)
708 This->queue_tail = This->queue_tail->next = parser_ctx;
709 else
710 This->queue_head = This->queue_tail = parser_ctx;
711 return S_OK;
714 hres = exec_global_code(This, parser_ctx);
715 parser_release(parser_ctx);
717 return hres;
720 #undef ASPARSE_THIS
722 static const IActiveScriptParseVtbl JScriptParseVtbl = {
723 JScriptParse_QueryInterface,
724 JScriptParse_AddRef,
725 JScriptParse_Release,
726 JScriptParse_InitNew,
727 JScriptParse_AddScriptlet,
728 JScriptParse_ParseScriptText
731 #define ASPARSEPROC_THIS(iface) DEFINE_THIS(JScript, IActiveScriptParseProcedure2, iface)
733 static HRESULT WINAPI JScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
735 JScript *This = ASPARSEPROC_THIS(iface);
736 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
739 static ULONG WINAPI JScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
741 JScript *This = ASPARSEPROC_THIS(iface);
742 return IActiveScript_AddRef(ACTSCRIPT(This));
745 static ULONG WINAPI JScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
747 JScript *This = ASPARSEPROC_THIS(iface);
748 return IActiveScript_Release(ACTSCRIPT(This));
751 static HRESULT WINAPI JScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
752 LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
753 LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
754 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
756 JScript *This = ASPARSEPROC_THIS(iface);
757 parser_ctx_t *parser_ctx;
758 DispatchEx *dispex;
759 HRESULT hres;
761 TRACE("(%p)->(%s %s %s %s %p %s %s %u %x %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrFormalParams),
762 debugstr_w(pstrProcedureName), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
763 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLineNumber, dwFlags, ppdisp);
765 if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
766 return E_UNEXPECTED;
768 hres = script_parse(This->ctx, pstrCode, pstrDelimiter, &parser_ctx);
769 if(FAILED(hres)) {
770 WARN("Parse failed %08x\n", hres);
771 return hres;
774 hres = create_source_function(parser_ctx, NULL, parser_ctx->source, NULL, NULL, 0, &dispex);
775 parser_release(parser_ctx);
776 if(FAILED(hres))
777 return hres;
779 *ppdisp = (IDispatch*)_IDispatchEx_(dispex);
780 return S_OK;
783 #undef ASPARSEPROC_THIS
785 static const IActiveScriptParseProcedure2Vtbl JScriptParseProcedureVtbl = {
786 JScriptParseProcedure_QueryInterface,
787 JScriptParseProcedure_AddRef,
788 JScriptParseProcedure_Release,
789 JScriptParseProcedure_ParseProcedureText,
792 #define ACTSCPPROP_THIS(iface) DEFINE_THIS(JScript, IActiveScriptProperty, iface)
794 static HRESULT WINAPI JScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
796 JScript *This = ACTSCPPROP_THIS(iface);
797 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
800 static ULONG WINAPI JScriptProperty_AddRef(IActiveScriptProperty *iface)
802 JScript *This = ACTSCPPROP_THIS(iface);
803 return IActiveScript_AddRef(ACTSCRIPT(This));
806 static ULONG WINAPI JScriptProperty_Release(IActiveScriptProperty *iface)
808 JScript *This = ACTSCPPROP_THIS(iface);
809 return IActiveScript_Release(ACTSCRIPT(This));
812 static HRESULT WINAPI JScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
813 VARIANT *pvarIndex, VARIANT *pvarValue)
815 JScript *This = ACTSCPPROP_THIS(iface);
816 FIXME("(%p)->(%x %p %p)\n", This, dwProperty, pvarIndex, pvarValue);
817 return E_NOTIMPL;
820 static HRESULT WINAPI JScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
821 VARIANT *pvarIndex, VARIANT *pvarValue)
823 JScript *This = ACTSCPPROP_THIS(iface);
825 TRACE("(%p)->(%x %s %s)\n", This, dwProperty, debugstr_variant(pvarIndex), debugstr_variant(pvarValue));
827 if(pvarIndex)
828 FIXME("unsupported pvarIndex\n");
830 switch(dwProperty) {
831 case SCRIPTPROP_INVOKEVERSIONING:
832 if(V_VT(pvarValue) != VT_I4 || V_I4(pvarValue) < 0 || V_I4(pvarValue) > 15) {
833 WARN("invalid value %s\n", debugstr_variant(pvarValue));
834 return E_INVALIDARG;
837 This->version = V_I4(pvarValue);
838 break;
839 default:
840 FIXME("Unimplemented property %x\n", dwProperty);
841 return E_NOTIMPL;
844 return S_OK;
847 #undef ACTSCPPROP_THIS
849 static const IActiveScriptPropertyVtbl JScriptPropertyVtbl = {
850 JScriptProperty_QueryInterface,
851 JScriptProperty_AddRef,
852 JScriptProperty_Release,
853 JScriptProperty_GetProperty,
854 JScriptProperty_SetProperty
857 #define OBJSAFETY_THIS(iface) DEFINE_THIS(JScript, IObjectSafety, iface)
859 static HRESULT WINAPI JScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
861 JScript *This = OBJSAFETY_THIS(iface);
862 return IActiveScript_QueryInterface(ACTSCRIPT(This), riid, ppv);
865 static ULONG WINAPI JScriptSafety_AddRef(IObjectSafety *iface)
867 JScript *This = OBJSAFETY_THIS(iface);
868 return IActiveScript_AddRef(ACTSCRIPT(This));
871 static ULONG WINAPI JScriptSafety_Release(IObjectSafety *iface)
873 JScript *This = OBJSAFETY_THIS(iface);
874 return IActiveScript_Release(ACTSCRIPT(This));
877 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
879 static HRESULT WINAPI JScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
880 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
882 JScript *This = OBJSAFETY_THIS(iface);
884 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
886 if(!pdwSupportedOptions || !pdwEnabledOptions)
887 return E_POINTER;
889 *pdwSupportedOptions = SUPPORTED_OPTIONS;
890 *pdwEnabledOptions = This->safeopt;
892 return S_OK;
895 static HRESULT WINAPI JScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
896 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
898 JScript *This = OBJSAFETY_THIS(iface);
900 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
902 if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
903 return E_FAIL;
905 This->safeopt = dwEnabledOptions & dwEnabledOptions;
906 return S_OK;
909 #undef OBJSAFETY_THIS
911 static const IObjectSafetyVtbl JScriptSafetyVtbl = {
912 JScriptSafety_QueryInterface,
913 JScriptSafety_AddRef,
914 JScriptSafety_Release,
915 JScriptSafety_GetInterfaceSafetyOptions,
916 JScriptSafety_SetInterfaceSafetyOptions
919 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
920 REFIID riid, void **ppv)
922 JScript *ret;
923 HRESULT hres;
925 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
927 lock_module();
929 ret = heap_alloc_zero(sizeof(*ret));
930 if(!ret)
931 return E_OUTOFMEMORY;
933 ret->lpIActiveScriptVtbl = &JScriptVtbl;
934 ret->lpIActiveScriptParseVtbl = &JScriptParseVtbl;
935 ret->lpIActiveScriptParseProcedure2Vtbl = &JScriptParseProcedureVtbl;
936 ret->lpIActiveScriptPropertyVtbl = &JScriptPropertyVtbl;
937 ret->lpIObjectSafetyVtbl = &JScriptSafetyVtbl;
938 ret->ref = 1;
939 ret->safeopt = INTERFACE_USES_DISPEX;
941 hres = IActiveScript_QueryInterface(ACTSCRIPT(ret), riid, ppv);
942 IActiveScript_Release(ACTSCRIPT(ret));
943 return hres;