po: Tweak the wrapping of a few Portuguese (Brazil) translations.
[wine/multimedia.git] / dlls / jscript / jscript.c
blob68ace36781ea83acd10db7c946118030bfaaed20
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 <assert.h>
21 #include "jscript.h"
22 #include "engine.h"
23 #include "objsafe.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 #ifdef _WIN64
31 #define CTXARG_T DWORDLONG
32 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
33 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_64Vtbl
35 #else
37 #define CTXARG_T DWORD
38 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
39 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_32Vtbl
41 #endif
43 typedef struct {
44 IActiveScript IActiveScript_iface;
45 IActiveScriptParse IActiveScriptParse_iface;
46 IActiveScriptParseProcedure2 IActiveScriptParseProcedure2_iface;
47 IActiveScriptProperty IActiveScriptProperty_iface;
48 IObjectSafety IObjectSafety_iface;
50 LONG ref;
52 DWORD safeopt;
53 script_ctx_t *ctx;
54 LONG thread_id;
55 LCID lcid;
56 DWORD version;
58 IActiveScriptSite *site;
60 parser_ctx_t *queue_head;
61 parser_ctx_t *queue_tail;
62 } JScript;
64 void script_release(script_ctx_t *ctx)
66 if(--ctx->ref)
67 return;
69 if(ctx->cc)
70 release_cc(ctx->cc);
71 jsheap_free(&ctx->tmp_heap);
72 SysFreeString(ctx->last_match);
73 heap_free(ctx);
76 static void change_state(JScript *This, SCRIPTSTATE state)
78 if(This->ctx->state == state)
79 return;
81 This->ctx->state = state;
82 if(This->site)
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, TRUE, &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, FALSE, &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 static void decrease_state(JScript *This, SCRIPTSTATE state)
161 if(This->ctx) {
162 switch(This->ctx->state) {
163 case SCRIPTSTATE_CONNECTED:
164 change_state(This, SCRIPTSTATE_DISCONNECTED);
165 if(state == SCRIPTSTATE_DISCONNECTED)
166 return;
167 /* FALLTHROUGH */
168 case SCRIPTSTATE_STARTED:
169 case SCRIPTSTATE_DISCONNECTED:
170 clear_script_queue(This);
172 if(This->ctx->state == SCRIPTSTATE_DISCONNECTED)
173 change_state(This, SCRIPTSTATE_INITIALIZED);
174 if(state == SCRIPTSTATE_INITIALIZED)
175 return;
176 /* FALLTHROUGH */
177 case SCRIPTSTATE_INITIALIZED:
178 if(This->ctx->host_global) {
179 IDispatch_Release(This->ctx->host_global);
180 This->ctx->host_global = NULL;
183 if(This->ctx->named_items) {
184 named_item_t *iter, *iter2;
186 iter = This->ctx->named_items;
187 while(iter) {
188 iter2 = iter->next;
190 if(iter->disp)
191 IDispatch_Release(iter->disp);
192 heap_free(iter->name);
193 heap_free(iter);
194 iter = iter2;
197 This->ctx->named_items = NULL;
200 if(This->ctx->secmgr) {
201 IInternetHostSecurityManager_Release(This->ctx->secmgr);
202 This->ctx->secmgr = NULL;
205 if(This->ctx->site) {
206 IActiveScriptSite_Release(This->ctx->site);
207 This->ctx->site = NULL;
210 if(This->ctx->global) {
211 jsdisp_release(This->ctx->global);
212 This->ctx->global = NULL;
214 /* FALLTHROUGH */
215 case SCRIPTSTATE_UNINITIALIZED:
216 change_state(This, state);
217 break;
218 default:
219 assert(0);
222 change_state(This, state);
223 }else if(state == SCRIPTSTATE_UNINITIALIZED) {
224 if(This->site)
225 IActiveScriptSite_OnStateChange(This->site, state);
226 }else {
227 FIXME("NULL ctx\n");
230 if(state == SCRIPTSTATE_UNINITIALIZED)
231 This->thread_id = 0;
233 if(This->site) {
234 IActiveScriptSite_Release(This->site);
235 This->site = NULL;
239 typedef struct {
240 IServiceProvider IServiceProvider_iface;
242 LONG ref;
244 IServiceProvider *sp;
245 } AXSite;
247 static inline AXSite *impl_from_IServiceProvider(IServiceProvider *iface)
249 return CONTAINING_RECORD(iface, AXSite, IServiceProvider_iface);
252 static HRESULT WINAPI AXSite_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
254 AXSite *This = impl_from_IServiceProvider(iface);
256 if(IsEqualGUID(&IID_IUnknown, riid)) {
257 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
258 *ppv = &This->IServiceProvider_iface;
259 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
260 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
261 *ppv = &This->IServiceProvider_iface;
262 }else {
263 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
264 *ppv = NULL;
265 return E_NOINTERFACE;
268 IUnknown_AddRef((IUnknown*)*ppv);
269 return S_OK;
272 static ULONG WINAPI AXSite_AddRef(IServiceProvider *iface)
274 AXSite *This = impl_from_IServiceProvider(iface);
275 LONG ref = InterlockedIncrement(&This->ref);
277 TRACE("(%p) ref=%d\n", This, ref);
279 return ref;
282 static ULONG WINAPI AXSite_Release(IServiceProvider *iface)
284 AXSite *This = impl_from_IServiceProvider(iface);
285 LONG ref = InterlockedDecrement(&This->ref);
287 TRACE("(%p) ref=%d\n", This, ref);
289 if(!ref)
291 if(This->sp)
292 IServiceProvider_Release(This->sp);
294 heap_free(This);
297 return ref;
300 static HRESULT WINAPI AXSite_QueryService(IServiceProvider *iface,
301 REFGUID guidService, REFIID riid, void **ppv)
303 AXSite *This = impl_from_IServiceProvider(iface);
305 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
307 if(!This->sp)
308 return E_NOINTERFACE;
310 return IServiceProvider_QueryService(This->sp, guidService, riid, ppv);
313 static IServiceProviderVtbl AXSiteVtbl = {
314 AXSite_QueryInterface,
315 AXSite_AddRef,
316 AXSite_Release,
317 AXSite_QueryService
320 IUnknown *create_ax_site(script_ctx_t *ctx)
322 IServiceProvider *sp = NULL;
323 AXSite *ret;
324 HRESULT hres;
326 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
327 if(FAILED(hres)) {
328 TRACE("Could not get IServiceProvider iface: %08x\n", hres);
331 ret = heap_alloc(sizeof(AXSite));
332 if(!ret) {
333 IServiceProvider_Release(sp);
334 return NULL;
337 ret->IServiceProvider_iface.lpVtbl = &AXSiteVtbl;
338 ret->ref = 1;
339 ret->sp = sp;
341 return (IUnknown*)&ret->IServiceProvider_iface;
344 static inline JScript *impl_from_IActiveScript(IActiveScript *iface)
346 return CONTAINING_RECORD(iface, JScript, IActiveScript_iface);
349 static HRESULT WINAPI JScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
351 JScript *This = impl_from_IActiveScript(iface);
353 *ppv = NULL;
355 if(IsEqualGUID(riid, &IID_IUnknown)) {
356 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
357 *ppv = &This->IActiveScript_iface;
358 }else if(IsEqualGUID(riid, &IID_IActiveScript)) {
359 TRACE("(%p)->(IID_IActiveScript %p)\n", This, ppv);
360 *ppv = &This->IActiveScript_iface;
361 }else if(IsEqualGUID(riid, &IID_IActiveScriptParse)) {
362 TRACE("(%p)->(IID_IActiveScriptParse %p)\n", This, ppv);
363 *ppv = &This->IActiveScriptParse_iface;
364 }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure)) {
365 TRACE("(%p)->(IID_IActiveScriptParseProcedure %p)\n", This, ppv);
366 *ppv = &This->IActiveScriptParseProcedure2_iface;
367 }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure2)) {
368 TRACE("(%p)->(IID_IActiveScriptParseProcedure2 %p)\n", This, ppv);
369 *ppv = &This->IActiveScriptParseProcedure2_iface;
370 }else if(IsEqualGUID(riid, &IID_IActiveScriptProperty)) {
371 TRACE("(%p)->(IID_IActiveScriptProperty %p)\n", This, ppv);
372 *ppv = &This->IActiveScriptProperty_iface;
373 }else if(IsEqualGUID(riid, &IID_IObjectSafety)) {
374 TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
375 *ppv = &This->IObjectSafety_iface;
378 if(*ppv) {
379 IUnknown_AddRef((IUnknown*)*ppv);
380 return S_OK;
383 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
384 return E_NOINTERFACE;
387 static ULONG WINAPI JScript_AddRef(IActiveScript *iface)
389 JScript *This = impl_from_IActiveScript(iface);
390 LONG ref = InterlockedIncrement(&This->ref);
392 TRACE("(%p) ref=%d\n", This, ref);
394 return ref;
397 static ULONG WINAPI JScript_Release(IActiveScript *iface)
399 JScript *This = impl_from_IActiveScript(iface);
400 LONG ref = InterlockedDecrement(&This->ref);
402 TRACE("(%p) ref=%d\n", iface, ref);
404 if(!ref) {
405 if(This->ctx && This->ctx->state != SCRIPTSTATE_CLOSED)
406 IActiveScript_Close(&This->IActiveScript_iface);
407 if(This->ctx)
408 script_release(This->ctx);
409 heap_free(This);
410 unlock_module();
413 return ref;
416 static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface,
417 IActiveScriptSite *pass)
419 JScript *This = impl_from_IActiveScript(iface);
420 LCID lcid;
421 HRESULT hres;
423 TRACE("(%p)->(%p)\n", This, pass);
425 if(!pass)
426 return E_POINTER;
428 if(This->site)
429 return E_UNEXPECTED;
431 if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0))
432 return E_UNEXPECTED;
434 This->site = pass;
435 IActiveScriptSite_AddRef(This->site);
437 hres = IActiveScriptSite_GetLCID(This->site, &lcid);
438 if(hres == S_OK)
439 This->lcid = lcid;
441 return This->ctx ? set_ctx_site(This) : S_OK;
444 static HRESULT WINAPI JScript_GetScriptSite(IActiveScript *iface, REFIID riid,
445 void **ppvObject)
447 JScript *This = impl_from_IActiveScript(iface);
448 FIXME("(%p)->()\n", This);
449 return E_NOTIMPL;
452 static HRESULT WINAPI JScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
454 JScript *This = impl_from_IActiveScript(iface);
456 TRACE("(%p)->(%d)\n", This, ss);
458 if(This->thread_id && GetCurrentThreadId() != This->thread_id)
459 return E_UNEXPECTED;
461 if(ss == SCRIPTSTATE_UNINITIALIZED) {
462 if(This->ctx && This->ctx->state == SCRIPTSTATE_CLOSED)
463 return E_UNEXPECTED;
465 decrease_state(This, SCRIPTSTATE_UNINITIALIZED);
466 return S_OK;
469 if(!This->ctx)
470 return E_UNEXPECTED;
472 switch(ss) {
473 case SCRIPTSTATE_STARTED:
474 case SCRIPTSTATE_CONNECTED: /* FIXME */
475 if(This->ctx->state == SCRIPTSTATE_CLOSED)
476 return E_UNEXPECTED;
478 exec_queued_code(This);
479 break;
480 case SCRIPTSTATE_INITIALIZED:
481 FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
482 return S_OK;
483 default:
484 FIXME("unimplemented state %d\n", ss);
485 return E_NOTIMPL;
488 change_state(This, ss);
489 return S_OK;
492 static HRESULT WINAPI JScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
494 JScript *This = impl_from_IActiveScript(iface);
496 TRACE("(%p)->(%p)\n", This, pssState);
498 if(!pssState)
499 return E_POINTER;
501 if(This->thread_id && This->thread_id != GetCurrentThreadId())
502 return E_UNEXPECTED;
504 *pssState = This->ctx ? This->ctx->state : SCRIPTSTATE_UNINITIALIZED;
505 return S_OK;
508 static HRESULT WINAPI JScript_Close(IActiveScript *iface)
510 JScript *This = impl_from_IActiveScript(iface);
512 TRACE("(%p)->()\n", This);
514 if(This->thread_id && This->thread_id != GetCurrentThreadId())
515 return E_UNEXPECTED;
517 decrease_state(This, SCRIPTSTATE_CLOSED);
518 return S_OK;
521 static HRESULT WINAPI JScript_AddNamedItem(IActiveScript *iface,
522 LPCOLESTR pstrName, DWORD dwFlags)
524 JScript *This = impl_from_IActiveScript(iface);
525 named_item_t *item;
526 IDispatch *disp = NULL;
527 HRESULT hres;
529 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
531 if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->ctx->state == SCRIPTSTATE_CLOSED)
532 return E_UNEXPECTED;
534 if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
535 IUnknown *unk;
537 hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
538 if(FAILED(hres)) {
539 WARN("GetItemInfo failed: %08x\n", hres);
540 return hres;
543 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
544 IUnknown_Release(unk);
545 if(FAILED(hres)) {
546 WARN("object does not implement IDispatch\n");
547 return hres;
550 if(This->ctx->host_global)
551 IDispatch_Release(This->ctx->host_global);
552 IDispatch_AddRef(disp);
553 This->ctx->host_global = disp;
556 item = heap_alloc(sizeof(*item));
557 if(!item) {
558 if(disp)
559 IDispatch_Release(disp);
560 return E_OUTOFMEMORY;
563 item->disp = disp;
564 item->flags = dwFlags;
565 item->name = heap_strdupW(pstrName);
566 if(!item->name) {
567 if(disp)
568 IDispatch_Release(disp);
569 heap_free(item);
570 return E_OUTOFMEMORY;
573 item->next = This->ctx->named_items;
574 This->ctx->named_items = item;
576 return S_OK;
579 static HRESULT WINAPI JScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
580 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
582 JScript *This = impl_from_IActiveScript(iface);
583 FIXME("(%p)->()\n", This);
584 return E_NOTIMPL;
587 static HRESULT WINAPI JScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
588 IDispatch **ppdisp)
590 JScript *This = impl_from_IActiveScript(iface);
592 TRACE("(%p)->(%p)\n", This, ppdisp);
594 if(!ppdisp)
595 return E_POINTER;
597 if(This->thread_id != GetCurrentThreadId() || !This->ctx->global) {
598 *ppdisp = NULL;
599 return E_UNEXPECTED;
602 *ppdisp = to_disp(This->ctx->global);
603 IDispatch_AddRef(*ppdisp);
604 return S_OK;
607 static HRESULT WINAPI JScript_GetCurrentScriptThreadID(IActiveScript *iface,
608 SCRIPTTHREADID *pstridThread)
610 JScript *This = impl_from_IActiveScript(iface);
611 FIXME("(%p)->()\n", This);
612 return E_NOTIMPL;
615 static HRESULT WINAPI JScript_GetScriptThreadID(IActiveScript *iface,
616 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
618 JScript *This = impl_from_IActiveScript(iface);
619 FIXME("(%p)->()\n", This);
620 return E_NOTIMPL;
623 static HRESULT WINAPI JScript_GetScriptThreadState(IActiveScript *iface,
624 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
626 JScript *This = impl_from_IActiveScript(iface);
627 FIXME("(%p)->()\n", This);
628 return E_NOTIMPL;
631 static HRESULT WINAPI JScript_InterruptScriptThread(IActiveScript *iface,
632 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
634 JScript *This = impl_from_IActiveScript(iface);
635 FIXME("(%p)->()\n", This);
636 return E_NOTIMPL;
639 static HRESULT WINAPI JScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
641 JScript *This = impl_from_IActiveScript(iface);
642 FIXME("(%p)->()\n", This);
643 return E_NOTIMPL;
646 static const IActiveScriptVtbl JScriptVtbl = {
647 JScript_QueryInterface,
648 JScript_AddRef,
649 JScript_Release,
650 JScript_SetScriptSite,
651 JScript_GetScriptSite,
652 JScript_SetScriptState,
653 JScript_GetScriptState,
654 JScript_Close,
655 JScript_AddNamedItem,
656 JScript_AddTypeLib,
657 JScript_GetScriptDispatch,
658 JScript_GetCurrentScriptThreadID,
659 JScript_GetScriptThreadID,
660 JScript_GetScriptThreadState,
661 JScript_InterruptScriptThread,
662 JScript_Clone
665 static inline JScript *impl_from_IActiveScriptParse(IActiveScriptParse *iface)
667 return CONTAINING_RECORD(iface, JScript, IActiveScriptParse_iface);
670 static HRESULT WINAPI JScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
672 JScript *This = impl_from_IActiveScriptParse(iface);
673 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
676 static ULONG WINAPI JScriptParse_AddRef(IActiveScriptParse *iface)
678 JScript *This = impl_from_IActiveScriptParse(iface);
679 return IActiveScript_AddRef(&This->IActiveScript_iface);
682 static ULONG WINAPI JScriptParse_Release(IActiveScriptParse *iface)
684 JScript *This = impl_from_IActiveScriptParse(iface);
685 return IActiveScript_Release(&This->IActiveScript_iface);
688 static HRESULT WINAPI JScriptParse_InitNew(IActiveScriptParse *iface)
690 JScript *This = impl_from_IActiveScriptParse(iface);
691 script_ctx_t *ctx;
693 TRACE("(%p)\n", This);
695 if(This->ctx)
696 return E_UNEXPECTED;
698 ctx = heap_alloc_zero(sizeof(script_ctx_t));
699 if(!ctx)
700 return E_OUTOFMEMORY;
702 ctx->ref = 1;
703 ctx->state = SCRIPTSTATE_UNINITIALIZED;
704 ctx->safeopt = This->safeopt;
705 ctx->version = This->version;
706 jsheap_init(&ctx->tmp_heap);
708 ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
709 if(ctx) {
710 script_release(ctx);
711 return E_UNEXPECTED;
714 return This->site ? set_ctx_site(This) : S_OK;
717 static HRESULT WINAPI JScriptParse_AddScriptlet(IActiveScriptParse *iface,
718 LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
719 LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
720 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
721 BSTR *pbstrName, EXCEPINFO *pexcepinfo)
723 JScript *This = impl_from_IActiveScriptParse(iface);
724 FIXME("(%p)->(%s %s %s %s %s %s %s %u %x %p %p)\n", This, debugstr_w(pstrDefaultName),
725 debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
726 debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
727 ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
728 return E_NOTIMPL;
731 static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface,
732 LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
733 LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
734 DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
736 JScript *This = impl_from_IActiveScriptParse(iface);
737 parser_ctx_t *parser_ctx;
738 HRESULT hres;
740 TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode),
741 debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
742 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
744 if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
745 return E_UNEXPECTED;
747 hres = script_parse(This->ctx, pstrCode, pstrDelimiter, &parser_ctx);
748 if(FAILED(hres))
749 return hres;
751 if(!is_started(This->ctx)) {
752 if(This->queue_tail)
753 This->queue_tail = This->queue_tail->next = parser_ctx;
754 else
755 This->queue_head = This->queue_tail = parser_ctx;
756 return S_OK;
759 hres = exec_global_code(This, parser_ctx);
760 parser_release(parser_ctx);
762 return hres;
765 static const IActiveScriptParseVtbl JScriptParseVtbl = {
766 JScriptParse_QueryInterface,
767 JScriptParse_AddRef,
768 JScriptParse_Release,
769 JScriptParse_InitNew,
770 JScriptParse_AddScriptlet,
771 JScriptParse_ParseScriptText
774 static inline JScript *impl_from_IActiveScriptParseProcedure2(IActiveScriptParseProcedure2 *iface)
776 return CONTAINING_RECORD(iface, JScript, IActiveScriptParseProcedure2_iface);
779 static HRESULT WINAPI JScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
781 JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
782 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
785 static ULONG WINAPI JScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
787 JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
788 return IActiveScript_AddRef(&This->IActiveScript_iface);
791 static ULONG WINAPI JScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
793 JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
794 return IActiveScript_Release(&This->IActiveScript_iface);
797 static HRESULT WINAPI JScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
798 LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
799 LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
800 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
802 JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
803 parser_ctx_t *parser_ctx;
804 jsdisp_t *dispex;
805 HRESULT hres;
807 TRACE("(%p)->(%s %s %s %s %p %s %s %u %x %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrFormalParams),
808 debugstr_w(pstrProcedureName), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
809 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLineNumber, dwFlags, ppdisp);
811 if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
812 return E_UNEXPECTED;
814 hres = script_parse(This->ctx, pstrCode, pstrDelimiter, &parser_ctx);
815 if(FAILED(hres)) {
816 WARN("Parse failed %08x\n", hres);
817 return hres;
820 hres = create_source_function(parser_ctx, NULL, parser_ctx->source, NULL, NULL, 0, &dispex);
821 parser_release(parser_ctx);
822 if(FAILED(hres))
823 return hres;
825 *ppdisp = to_disp(dispex);
826 return S_OK;
829 static const IActiveScriptParseProcedure2Vtbl JScriptParseProcedureVtbl = {
830 JScriptParseProcedure_QueryInterface,
831 JScriptParseProcedure_AddRef,
832 JScriptParseProcedure_Release,
833 JScriptParseProcedure_ParseProcedureText,
836 static inline JScript *impl_from_IActiveScriptProperty(IActiveScriptProperty *iface)
838 return CONTAINING_RECORD(iface, JScript, IActiveScriptProperty_iface);
841 static HRESULT WINAPI JScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
843 JScript *This = impl_from_IActiveScriptProperty(iface);
844 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
847 static ULONG WINAPI JScriptProperty_AddRef(IActiveScriptProperty *iface)
849 JScript *This = impl_from_IActiveScriptProperty(iface);
850 return IActiveScript_AddRef(&This->IActiveScript_iface);
853 static ULONG WINAPI JScriptProperty_Release(IActiveScriptProperty *iface)
855 JScript *This = impl_from_IActiveScriptProperty(iface);
856 return IActiveScript_Release(&This->IActiveScript_iface);
859 static HRESULT WINAPI JScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
860 VARIANT *pvarIndex, VARIANT *pvarValue)
862 JScript *This = impl_from_IActiveScriptProperty(iface);
863 FIXME("(%p)->(%x %p %p)\n", This, dwProperty, pvarIndex, pvarValue);
864 return E_NOTIMPL;
867 static HRESULT WINAPI JScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
868 VARIANT *pvarIndex, VARIANT *pvarValue)
870 JScript *This = impl_from_IActiveScriptProperty(iface);
872 TRACE("(%p)->(%x %s %s)\n", This, dwProperty, debugstr_variant(pvarIndex), debugstr_variant(pvarValue));
874 if(pvarIndex)
875 FIXME("unsupported pvarIndex\n");
877 switch(dwProperty) {
878 case SCRIPTPROP_INVOKEVERSIONING:
879 if(V_VT(pvarValue) != VT_I4 || V_I4(pvarValue) < 0 || V_I4(pvarValue) > 15) {
880 WARN("invalid value %s\n", debugstr_variant(pvarValue));
881 return E_INVALIDARG;
884 This->version = V_I4(pvarValue);
885 break;
886 default:
887 FIXME("Unimplemented property %x\n", dwProperty);
888 return E_NOTIMPL;
891 return S_OK;
894 static const IActiveScriptPropertyVtbl JScriptPropertyVtbl = {
895 JScriptProperty_QueryInterface,
896 JScriptProperty_AddRef,
897 JScriptProperty_Release,
898 JScriptProperty_GetProperty,
899 JScriptProperty_SetProperty
902 static inline JScript *impl_from_IObjectSafety(IObjectSafety *iface)
904 return CONTAINING_RECORD(iface, JScript, IObjectSafety_iface);
907 static HRESULT WINAPI JScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
909 JScript *This = impl_from_IObjectSafety(iface);
910 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
913 static ULONG WINAPI JScriptSafety_AddRef(IObjectSafety *iface)
915 JScript *This = impl_from_IObjectSafety(iface);
916 return IActiveScript_AddRef(&This->IActiveScript_iface);
919 static ULONG WINAPI JScriptSafety_Release(IObjectSafety *iface)
921 JScript *This = impl_from_IObjectSafety(iface);
922 return IActiveScript_Release(&This->IActiveScript_iface);
925 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
927 static HRESULT WINAPI JScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
928 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
930 JScript *This = impl_from_IObjectSafety(iface);
932 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
934 if(!pdwSupportedOptions || !pdwEnabledOptions)
935 return E_POINTER;
937 *pdwSupportedOptions = SUPPORTED_OPTIONS;
938 *pdwEnabledOptions = This->safeopt;
940 return S_OK;
943 static HRESULT WINAPI JScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
944 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
946 JScript *This = impl_from_IObjectSafety(iface);
948 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
950 if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
951 return E_FAIL;
953 This->safeopt = (dwEnabledOptions & dwOptionSetMask) | (This->safeopt & ~dwOptionSetMask) | INTERFACE_USES_DISPEX;
954 return S_OK;
957 static const IObjectSafetyVtbl JScriptSafetyVtbl = {
958 JScriptSafety_QueryInterface,
959 JScriptSafety_AddRef,
960 JScriptSafety_Release,
961 JScriptSafety_GetInterfaceSafetyOptions,
962 JScriptSafety_SetInterfaceSafetyOptions
965 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
966 REFIID riid, void **ppv)
968 JScript *ret;
969 HRESULT hres;
971 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
973 if(pUnkOuter) {
974 *ppv = NULL;
975 return CLASS_E_NOAGGREGATION;
978 lock_module();
980 ret = heap_alloc_zero(sizeof(*ret));
981 if(!ret)
982 return E_OUTOFMEMORY;
984 ret->IActiveScript_iface.lpVtbl = &JScriptVtbl;
985 ret->IActiveScriptParse_iface.lpVtbl = &JScriptParseVtbl;
986 ret->IActiveScriptParseProcedure2_iface.lpVtbl = &JScriptParseProcedureVtbl;
987 ret->IActiveScriptProperty_iface.lpVtbl = &JScriptPropertyVtbl;
988 ret->IObjectSafety_iface.lpVtbl = &JScriptSafetyVtbl;
989 ret->ref = 1;
990 ret->safeopt = INTERFACE_USES_DISPEX;
992 hres = IActiveScript_QueryInterface(&ret->IActiveScript_iface, riid, ppv);
993 IActiveScript_Release(&ret->IActiveScript_iface);
994 return hres;