windowscodecs: Silence fixme for IID_CMetaBitmapRenderTarget.
[wine.git] / dlls / jscript / jscript.c
blob6829f80f299d1a7bd87e421142c27fad2f58d090
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;
49 IVariantChangeType IVariantChangeType_iface;
51 LONG ref;
53 DWORD safeopt;
54 struct thread_data *thread_data;
55 script_ctx_t *ctx;
56 LCID lcid;
57 DWORD version;
58 BOOL html_mode;
59 BOOL is_encode;
60 BOOL is_initialized;
62 IActiveScriptSite *site;
64 struct list persistent_code;
65 struct list queued_code;
66 } JScript;
68 typedef struct {
69 IActiveScriptError IActiveScriptError_iface;
70 LONG ref;
71 jsexcept_t ei;
72 } JScriptError;
74 void script_release(script_ctx_t *ctx)
76 if(--ctx->ref)
77 return;
79 jsval_release(ctx->acc);
80 if(ctx->cc)
81 release_cc(ctx->cc);
82 heap_pool_free(&ctx->tmp_heap);
83 if(ctx->last_match)
84 jsstr_release(ctx->last_match);
85 assert(!ctx->stack_top);
86 free(ctx->stack);
88 ctx->jscaller->ctx = NULL;
89 IServiceProvider_Release(&ctx->jscaller->IServiceProvider_iface);
91 release_thread_data(ctx->thread_data);
92 free(ctx);
95 static void script_globals_release(script_ctx_t *ctx)
97 unsigned i;
98 for(i = 0; i < ARRAY_SIZE(ctx->global_objects); i++) {
99 if(ctx->global_objects[i]) {
100 jsdisp_release(ctx->global_objects[i]);
101 ctx->global_objects[i] = NULL;
106 static void change_state(JScript *This, SCRIPTSTATE state)
108 if(This->ctx->state == state)
109 return;
111 This->ctx->state = state;
112 if(This->site)
113 IActiveScriptSite_OnStateChange(This->site, state);
116 static inline BOOL is_started(script_ctx_t *ctx)
118 return ctx->state == SCRIPTSTATE_STARTED
119 || ctx->state == SCRIPTSTATE_CONNECTED
120 || ctx->state == SCRIPTSTATE_DISCONNECTED;
123 HRESULT create_named_item_script_obj(script_ctx_t *ctx, named_item_t *item)
125 static const builtin_info_t disp_info = {
126 JSCLASS_GLOBAL,
127 NULL,
128 0, NULL,
129 NULL,
130 NULL
133 return create_dispex(ctx, &disp_info, NULL, &item->script_obj);
136 static void release_named_item_script_obj(named_item_t *item)
138 if(!item->script_obj) return;
140 jsdisp_release(item->script_obj);
141 item->script_obj = NULL;
144 static HRESULT retrieve_named_item_disp(IActiveScriptSite *site, named_item_t *item)
146 IUnknown *unk;
147 HRESULT hr;
149 if(!site)
150 return E_UNEXPECTED;
152 hr = IActiveScriptSite_GetItemInfo(site, item->name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
153 if(FAILED(hr)) {
154 WARN("GetItemInfo failed: %08lx\n", hr);
155 return hr;
158 hr = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
159 IUnknown_Release(unk);
160 if(FAILED(hr)) {
161 WARN("object does not implement IDispatch\n");
162 return hr;
165 return S_OK;
168 named_item_t *lookup_named_item(script_ctx_t *ctx, const WCHAR *item_name, unsigned flags)
170 named_item_t *item;
171 HRESULT hr;
173 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
174 if((item->flags & flags) == flags && !wcscmp(item->name, item_name)) {
175 if(!item->script_obj && !(item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
176 hr = create_named_item_script_obj(ctx, item);
177 if(FAILED(hr)) return NULL;
180 if(!item->disp && (flags || !(item->flags & SCRIPTITEM_CODEONLY))) {
181 hr = retrieve_named_item_disp(ctx->site, item);
182 if(FAILED(hr)) continue;
185 return item;
189 return NULL;
192 void release_named_item(named_item_t *item)
194 if(--item->ref) return;
196 free(item->name);
197 free(item);
200 static inline JScriptError *impl_from_IActiveScriptError(IActiveScriptError *iface)
202 return CONTAINING_RECORD(iface, JScriptError, IActiveScriptError_iface);
205 static HRESULT WINAPI JScriptError_QueryInterface(IActiveScriptError *iface, REFIID riid, void **ppv)
207 JScriptError *This = impl_from_IActiveScriptError(iface);
209 if(IsEqualGUID(riid, &IID_IUnknown)) {
210 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
211 *ppv = &This->IActiveScriptError_iface;
212 }else if(IsEqualGUID(riid, &IID_IActiveScriptError)) {
213 TRACE("(%p)->(IID_IActiveScriptError %p)\n", This, ppv);
214 *ppv = &This->IActiveScriptError_iface;
215 }else {
216 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
217 *ppv = NULL;
218 return E_NOINTERFACE;
221 IUnknown_AddRef((IUnknown*)*ppv);
222 return S_OK;
225 static ULONG WINAPI JScriptError_AddRef(IActiveScriptError *iface)
227 JScriptError *This = impl_from_IActiveScriptError(iface);
228 LONG ref = InterlockedIncrement(&This->ref);
230 TRACE("(%p) ref=%ld\n", This, ref);
232 return ref;
235 static ULONG WINAPI JScriptError_Release(IActiveScriptError *iface)
237 JScriptError *This = impl_from_IActiveScriptError(iface);
238 LONG ref = InterlockedDecrement(&This->ref);
240 TRACE("(%p) ref=%ld\n", This, ref);
242 if(!ref) {
243 reset_ei(&This->ei);
244 free(This);
247 return ref;
250 static HRESULT WINAPI JScriptError_GetExceptionInfo(IActiveScriptError *iface, EXCEPINFO *excepinfo)
252 JScriptError *This = impl_from_IActiveScriptError(iface);
254 TRACE("(%p)->(%p)\n", This, excepinfo);
256 if(!excepinfo)
257 return E_POINTER;
259 memset(excepinfo, 0, sizeof(*excepinfo));
260 excepinfo->scode = This->ei.error;
261 if(This->ei.source)
262 jsstr_to_bstr(This->ei.source, &excepinfo->bstrSource);
263 if(This->ei.message)
264 jsstr_to_bstr(This->ei.message, &excepinfo->bstrDescription);
265 return S_OK;
268 static HRESULT WINAPI JScriptError_GetSourcePosition(IActiveScriptError *iface, DWORD *source_context, ULONG *line, LONG *character)
270 JScriptError *This = impl_from_IActiveScriptError(iface);
271 bytecode_t *code = This->ei.code;
272 unsigned line_pos, char_pos;
274 TRACE("(%p)->(%p %p %p)\n", This, source_context, line, character);
276 if(!This->ei.code) {
277 FIXME("unknown position\n");
278 return E_FAIL;
281 if(source_context)
282 *source_context = This->ei.code->source_context;
283 if(!line && !character)
284 return S_OK;
286 line_pos = get_location_line(code, This->ei.loc, &char_pos);
287 if(line)
288 *line = line_pos;
289 if(character)
290 *character = char_pos;
291 return S_OK;
294 static HRESULT WINAPI JScriptError_GetSourceLineText(IActiveScriptError *iface, BSTR *source)
296 JScriptError *This = impl_from_IActiveScriptError(iface);
298 TRACE("(%p)->(%p)\n", This, source);
300 if(!source)
301 return E_POINTER;
303 if(!This->ei.line) {
304 *source = NULL;
305 return E_FAIL;
308 return jsstr_to_bstr(This->ei.line, source);
311 static const IActiveScriptErrorVtbl JScriptErrorVtbl = {
312 JScriptError_QueryInterface,
313 JScriptError_AddRef,
314 JScriptError_Release,
315 JScriptError_GetExceptionInfo,
316 JScriptError_GetSourcePosition,
317 JScriptError_GetSourceLineText
320 void reset_ei(jsexcept_t *ei)
322 ei->error = S_OK;
323 if(ei->valid_value) {
324 jsval_release(ei->value);
325 ei->valid_value = FALSE;
327 if(ei->code) {
328 release_bytecode(ei->code);
329 ei->code = NULL;
330 ei->loc = 0;
332 if(ei->source) {
333 jsstr_release(ei->source);
334 ei->source = NULL;
336 if(ei->message) {
337 jsstr_release(ei->message);
338 ei->message = NULL;
340 if(ei->line) {
341 jsstr_release(ei->line);
342 ei->line = NULL;
346 void enter_script(script_ctx_t *ctx, jsexcept_t *ei)
348 memset(ei, 0, sizeof(*ei));
349 ei->prev = ctx->ei;
350 ctx->ei = ei;
351 TRACE("ctx %p ei %p prev %p\n", ctx, ei, ei->prev);
354 HRESULT leave_script(script_ctx_t *ctx, HRESULT result)
356 jsexcept_t *ei = ctx->ei;
357 BOOL enter_notified = ei->enter_notified;
358 JScriptError *error;
360 TRACE("ctx %p ei %p prev %p\n", ctx, ei, ei->prev);
362 ctx->ei = ei->prev;
363 if(result == DISP_E_EXCEPTION) {
364 result = ei->error;
365 }else {
366 reset_ei(ei);
367 ei->error = result;
369 if(FAILED(result)) {
370 WARN("%08lx\n", result);
371 if(ctx->site && (error = malloc(sizeof(*error)))) {
372 HRESULT hres;
374 error->IActiveScriptError_iface.lpVtbl = &JScriptErrorVtbl;
375 error->ref = 1;
376 error->ei = *ei;
377 memset(ei, 0, sizeof(*ei));
379 hres = IActiveScriptSite_OnScriptError(ctx->site, &error->IActiveScriptError_iface);
380 IActiveScriptError_Release(&error->IActiveScriptError_iface);
381 if(hres == S_OK)
382 result = SCRIPT_E_REPORTED;
385 if(enter_notified && ctx->site)
386 IActiveScriptSite_OnLeaveScript(ctx->site);
387 reset_ei(ei);
388 return result;
391 static void clear_script_queue(JScript *This)
393 while(!list_empty(&This->queued_code))
395 bytecode_t *iter = LIST_ENTRY(list_head(&This->queued_code), bytecode_t, entry);
396 list_remove(&iter->entry);
397 if (iter->is_persistent)
398 list_add_tail(&This->persistent_code, &iter->entry);
399 else
400 release_bytecode(iter);
404 static void clear_persistent_code_list(JScript *This)
406 while(!list_empty(&This->persistent_code))
408 bytecode_t *iter = LIST_ENTRY(list_head(&This->persistent_code), bytecode_t, entry);
409 list_remove(&iter->entry);
410 release_bytecode(iter);
414 static void release_persistent_script_objs(JScript *This)
416 bytecode_t *iter;
418 LIST_FOR_EACH_ENTRY(iter, &This->persistent_code, bytecode_t, entry)
419 if(iter->named_item)
420 release_named_item_script_obj(iter->named_item);
423 static void release_named_item_list(JScript *This)
425 while(!list_empty(&This->ctx->named_items)) {
426 named_item_t *iter = LIST_ENTRY(list_head(&This->ctx->named_items), named_item_t, entry);
427 list_remove(&iter->entry);
428 release_named_item(iter);
432 static HRESULT exec_global_code(script_ctx_t *ctx, bytecode_t *code, jsval_t *r)
434 IServiceProvider *prev_caller = ctx->jscaller->caller;
435 HRESULT hres;
437 ctx->jscaller->caller = SP_CALLER_UNINITIALIZED;
438 hres = exec_source(ctx, EXEC_GLOBAL, code, &code->global_code, NULL, NULL, NULL, 0, NULL, r);
439 ctx->jscaller->caller = prev_caller;
440 return hres;
443 static void exec_queued_code(JScript *This)
445 bytecode_t *iter;
446 jsexcept_t ei;
447 HRESULT hres = S_OK;
449 LIST_FOR_EACH_ENTRY(iter, &This->queued_code, bytecode_t, entry) {
450 enter_script(This->ctx, &ei);
451 hres = exec_global_code(This->ctx, iter, NULL);
452 leave_script(This->ctx, hres);
453 if(FAILED(hres))
454 break;
457 clear_script_queue(This);
460 static void decrease_state(JScript *This, SCRIPTSTATE state)
462 named_item_t *item, *item_next;
464 if(This->ctx) {
465 switch(This->ctx->state) {
466 case SCRIPTSTATE_CONNECTED:
467 change_state(This, SCRIPTSTATE_DISCONNECTED);
468 if(state == SCRIPTSTATE_DISCONNECTED)
469 return;
470 /* FALLTHROUGH */
471 case SCRIPTSTATE_STARTED:
472 case SCRIPTSTATE_DISCONNECTED:
473 clear_script_queue(This);
475 if(This->ctx->state == SCRIPTSTATE_DISCONNECTED)
476 change_state(This, SCRIPTSTATE_INITIALIZED);
477 if(state == SCRIPTSTATE_INITIALIZED)
478 return;
479 /* FALLTHROUGH */
480 case SCRIPTSTATE_INITIALIZED:
481 clear_script_queue(This);
482 release_persistent_script_objs(This);
484 LIST_FOR_EACH_ENTRY_SAFE(item, item_next, &This->ctx->named_items, named_item_t, entry)
486 if(item->disp)
488 IDispatch_Release(item->disp);
489 item->disp = NULL;
491 release_named_item_script_obj(item);
492 if(!(item->flags & SCRIPTITEM_ISPERSISTENT))
494 list_remove(&item->entry);
495 release_named_item(item);
499 if(This->ctx->secmgr) {
500 IInternetHostSecurityManager_Release(This->ctx->secmgr);
501 This->ctx->secmgr = NULL;
504 if(This->ctx->site) {
505 IActiveScriptSite_Release(This->ctx->site);
506 This->ctx->site = NULL;
509 script_globals_release(This->ctx);
510 gc_run(This->ctx);
512 /* FALLTHROUGH */
513 case SCRIPTSTATE_UNINITIALIZED:
514 change_state(This, state);
515 break;
516 default:
517 assert(0);
520 change_state(This, state);
521 }else if(state == SCRIPTSTATE_UNINITIALIZED) {
522 if(This->site)
523 IActiveScriptSite_OnStateChange(This->site, state);
524 }else {
525 FIXME("NULL ctx\n");
528 if((state == SCRIPTSTATE_UNINITIALIZED || state == SCRIPTSTATE_CLOSED) && This->thread_data) {
529 release_thread_data(This->thread_data);
530 This->thread_data = NULL;
533 if(This->site) {
534 IActiveScriptSite_Release(This->site);
535 This->site = NULL;
539 typedef struct {
540 IServiceProvider IServiceProvider_iface;
542 LONG ref;
544 IServiceProvider *sp;
545 } AXSite;
547 static inline AXSite *impl_from_IServiceProvider(IServiceProvider *iface)
549 return CONTAINING_RECORD(iface, AXSite, IServiceProvider_iface);
552 static HRESULT WINAPI AXSite_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
554 AXSite *This = impl_from_IServiceProvider(iface);
556 if(IsEqualGUID(&IID_IUnknown, riid)) {
557 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
558 *ppv = &This->IServiceProvider_iface;
559 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
560 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
561 *ppv = &This->IServiceProvider_iface;
562 }else {
563 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
564 *ppv = NULL;
565 return E_NOINTERFACE;
568 IUnknown_AddRef((IUnknown*)*ppv);
569 return S_OK;
572 static ULONG WINAPI AXSite_AddRef(IServiceProvider *iface)
574 AXSite *This = impl_from_IServiceProvider(iface);
575 LONG ref = InterlockedIncrement(&This->ref);
577 TRACE("(%p) ref=%ld\n", This, ref);
579 return ref;
582 static ULONG WINAPI AXSite_Release(IServiceProvider *iface)
584 AXSite *This = impl_from_IServiceProvider(iface);
585 LONG ref = InterlockedDecrement(&This->ref);
587 TRACE("(%p) ref=%ld\n", This, ref);
589 if(!ref)
591 if(This->sp)
592 IServiceProvider_Release(This->sp);
594 free(This);
597 return ref;
600 static HRESULT WINAPI AXSite_QueryService(IServiceProvider *iface,
601 REFGUID guidService, REFIID riid, void **ppv)
603 AXSite *This = impl_from_IServiceProvider(iface);
605 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
607 if(!This->sp)
608 return E_NOINTERFACE;
610 return IServiceProvider_QueryService(This->sp, guidService, riid, ppv);
613 static IServiceProviderVtbl AXSiteVtbl = {
614 AXSite_QueryInterface,
615 AXSite_AddRef,
616 AXSite_Release,
617 AXSite_QueryService
620 IUnknown *create_ax_site(script_ctx_t *ctx)
622 IServiceProvider *sp = NULL;
623 AXSite *ret;
624 HRESULT hres;
626 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
627 if(FAILED(hres)) {
628 TRACE("Could not get IServiceProvider iface: %08lx\n", hres);
631 ret = malloc(sizeof(AXSite));
632 if(!ret) {
633 IServiceProvider_Release(sp);
634 return NULL;
637 ret->IServiceProvider_iface.lpVtbl = &AXSiteVtbl;
638 ret->ref = 1;
639 ret->sp = sp;
641 return (IUnknown*)&ret->IServiceProvider_iface;
644 static inline JScript *impl_from_IActiveScript(IActiveScript *iface)
646 return CONTAINING_RECORD(iface, JScript, IActiveScript_iface);
649 static HRESULT WINAPI JScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
651 JScript *This = impl_from_IActiveScript(iface);
653 *ppv = NULL;
655 if(IsEqualGUID(riid, &IID_IUnknown)) {
656 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
657 *ppv = &This->IActiveScript_iface;
658 }else if(IsEqualGUID(riid, &IID_IActiveScript)) {
659 TRACE("(%p)->(IID_IActiveScript %p)\n", This, ppv);
660 *ppv = &This->IActiveScript_iface;
661 }else if(IsEqualGUID(riid, &IID_IActiveScriptParse)) {
662 TRACE("(%p)->(IID_IActiveScriptParse %p)\n", This, ppv);
663 *ppv = &This->IActiveScriptParse_iface;
664 }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure)) {
665 TRACE("(%p)->(IID_IActiveScriptParseProcedure %p)\n", This, ppv);
666 *ppv = &This->IActiveScriptParseProcedure2_iface;
667 }else if(IsEqualGUID(riid, &IID_IActiveScriptParseProcedure2)) {
668 TRACE("(%p)->(IID_IActiveScriptParseProcedure2 %p)\n", This, ppv);
669 *ppv = &This->IActiveScriptParseProcedure2_iface;
670 }else if(IsEqualGUID(riid, &IID_IActiveScriptProperty)) {
671 TRACE("(%p)->(IID_IActiveScriptProperty %p)\n", This, ppv);
672 *ppv = &This->IActiveScriptProperty_iface;
673 }else if(IsEqualGUID(riid, &IID_IObjectSafety)) {
674 TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
675 *ppv = &This->IObjectSafety_iface;
676 }else if(IsEqualGUID(riid, &IID_IVariantChangeType)) {
677 TRACE("(%p)->(IID_IVariantChangeType %p)\n", This, ppv);
678 *ppv = &This->IVariantChangeType_iface;
681 if(*ppv) {
682 IUnknown_AddRef((IUnknown*)*ppv);
683 return S_OK;
686 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
687 return E_NOINTERFACE;
690 static ULONG WINAPI JScript_AddRef(IActiveScript *iface)
692 JScript *This = impl_from_IActiveScript(iface);
693 LONG ref = InterlockedIncrement(&This->ref);
695 TRACE("(%p) ref=%ld\n", This, ref);
697 return ref;
700 static ULONG WINAPI JScript_Release(IActiveScript *iface)
702 JScript *This = impl_from_IActiveScript(iface);
703 LONG ref = InterlockedDecrement(&This->ref);
705 TRACE("(%p) ref=%ld\n", iface, ref);
707 if(!ref) {
708 if(This->ctx && This->ctx->state != SCRIPTSTATE_CLOSED)
709 IActiveScript_Close(&This->IActiveScript_iface);
710 if(This->ctx) {
711 This->ctx->active_script = NULL;
712 script_release(This->ctx);
714 if(This->thread_data)
715 release_thread_data(This->thread_data);
716 free(This);
717 unlock_module();
720 return ref;
723 static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface,
724 IActiveScriptSite *pass)
726 JScript *This = impl_from_IActiveScript(iface);
727 struct thread_data *thread_data;
728 named_item_t *item;
729 LCID lcid;
730 HRESULT hres;
732 TRACE("(%p)->(%p)\n", This, pass);
734 if(!pass)
735 return E_POINTER;
737 if(This->site)
738 return E_UNEXPECTED;
740 if(!(thread_data = get_thread_data()))
741 return E_OUTOFMEMORY;
743 if(InterlockedCompareExchangePointer((void**)&This->thread_data, thread_data, NULL)) {
744 release_thread_data(thread_data);
745 return E_UNEXPECTED;
748 if(!This->ctx) {
749 script_ctx_t *ctx = calloc(1, sizeof(script_ctx_t));
750 if(!ctx)
751 return E_OUTOFMEMORY;
753 ctx->ref = 1;
754 ctx->state = SCRIPTSTATE_UNINITIALIZED;
755 ctx->active_script = &This->IActiveScript_iface;
756 ctx->safeopt = This->safeopt;
757 ctx->version = This->version;
758 ctx->html_mode = This->html_mode;
759 ctx->acc = jsval_undefined();
760 list_init(&ctx->named_items);
761 heap_pool_init(&ctx->tmp_heap);
763 hres = create_jscaller(ctx);
764 if(FAILED(hres)) {
765 free(ctx);
766 return hres;
769 thread_data->ref++;
770 ctx->thread_data = thread_data;
771 ctx->last_match = jsstr_empty();
773 This->ctx = ctx;
776 /* Retrieve new dispatches for persistent named items */
777 LIST_FOR_EACH_ENTRY(item, &This->ctx->named_items, named_item_t, entry)
779 if(!item->disp)
781 hres = retrieve_named_item_disp(pass, item);
782 if(FAILED(hres)) return hres;
785 /* For some reason, CODEONLY flag is lost in re-initialized scripts */
786 item->flags &= ~SCRIPTITEM_CODEONLY;
789 This->site = pass;
790 IActiveScriptSite_AddRef(This->site);
792 hres = IActiveScriptSite_GetLCID(This->site, &lcid);
793 if(hres == S_OK)
794 This->lcid = lcid;
796 This->ctx->lcid = This->lcid;
798 hres = init_global(This->ctx);
799 if(FAILED(hres))
800 return hres;
802 IActiveScriptSite_AddRef(This->site);
803 This->ctx->site = This->site;
805 if(This->is_initialized)
806 change_state(This, SCRIPTSTATE_INITIALIZED);
807 return S_OK;
810 static HRESULT WINAPI JScript_GetScriptSite(IActiveScript *iface, REFIID riid,
811 void **ppvObject)
813 JScript *This = impl_from_IActiveScript(iface);
814 FIXME("(%p)->()\n", This);
815 return E_NOTIMPL;
818 static HRESULT WINAPI JScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
820 JScript *This = impl_from_IActiveScript(iface);
822 TRACE("(%p)->(%d)\n", This, ss);
824 if(This->thread_data && This->thread_data->thread_id != GetCurrentThreadId())
825 return E_UNEXPECTED;
827 if(ss == SCRIPTSTATE_UNINITIALIZED) {
828 if(This->ctx && This->ctx->state == SCRIPTSTATE_CLOSED)
829 return E_UNEXPECTED;
831 decrease_state(This, SCRIPTSTATE_UNINITIALIZED);
832 list_move_tail(&This->queued_code, &This->persistent_code);
833 return S_OK;
836 if(!This->is_initialized || !This->ctx)
837 return E_UNEXPECTED;
839 switch(ss) {
840 case SCRIPTSTATE_STARTED:
841 case SCRIPTSTATE_CONNECTED: /* FIXME */
842 if(This->ctx->state == SCRIPTSTATE_UNINITIALIZED || This->ctx->state == SCRIPTSTATE_CLOSED)
843 return E_UNEXPECTED;
845 exec_queued_code(This);
846 break;
847 case SCRIPTSTATE_INITIALIZED:
848 FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
849 return S_OK;
850 default:
851 FIXME("unimplemented state %d\n", ss);
852 return E_NOTIMPL;
855 change_state(This, ss);
856 return S_OK;
859 static HRESULT WINAPI JScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
861 JScript *This = impl_from_IActiveScript(iface);
863 TRACE("(%p)->(%p)\n", This, pssState);
865 if(!pssState)
866 return E_POINTER;
868 if(This->thread_data && This->thread_data->thread_id != GetCurrentThreadId())
869 return E_UNEXPECTED;
871 *pssState = This->ctx ? This->ctx->state : SCRIPTSTATE_UNINITIALIZED;
872 return S_OK;
875 static HRESULT WINAPI JScript_Close(IActiveScript *iface)
877 JScript *This = impl_from_IActiveScript(iface);
879 TRACE("(%p)->()\n", This);
881 if(This->thread_data && This->thread_data->thread_id != GetCurrentThreadId())
882 return E_UNEXPECTED;
884 decrease_state(This, SCRIPTSTATE_CLOSED);
885 clear_persistent_code_list(This);
886 release_named_item_list(This);
887 return S_OK;
890 static HRESULT WINAPI JScript_AddNamedItem(IActiveScript *iface,
891 LPCOLESTR pstrName, DWORD dwFlags)
893 JScript *This = impl_from_IActiveScript(iface);
894 named_item_t *item;
895 IDispatch *disp = NULL;
896 HRESULT hres;
898 TRACE("(%p)->(%s %lx)\n", This, debugstr_w(pstrName), dwFlags);
900 if(!This->thread_data || This->thread_data->thread_id != GetCurrentThreadId() || !This->ctx || This->ctx->state == SCRIPTSTATE_CLOSED)
901 return E_UNEXPECTED;
903 if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
904 IUnknown *unk;
906 hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
907 if(FAILED(hres)) {
908 WARN("GetItemInfo failed: %08lx\n", hres);
909 return hres;
912 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
913 IUnknown_Release(unk);
914 if(FAILED(hres)) {
915 WARN("object does not implement IDispatch\n");
916 return hres;
920 item = malloc(sizeof(*item));
921 if(!item) {
922 if(disp)
923 IDispatch_Release(disp);
924 return E_OUTOFMEMORY;
927 item->ref = 1;
928 item->disp = disp;
929 item->flags = dwFlags;
930 item->script_obj = NULL;
931 item->name = wcsdup(pstrName);
932 if(!item->name) {
933 if(disp)
934 IDispatch_Release(disp);
935 free(item);
936 return E_OUTOFMEMORY;
939 list_add_tail(&This->ctx->named_items, &item->entry);
940 return S_OK;
943 static HRESULT WINAPI JScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
944 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
946 JScript *This = impl_from_IActiveScript(iface);
947 FIXME("(%p)->()\n", This);
948 return E_NOTIMPL;
951 static HRESULT WINAPI JScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
952 IDispatch **ppdisp)
954 JScript *This = impl_from_IActiveScript(iface);
955 jsdisp_t *script_obj;
957 TRACE("(%p)->(%s %p)\n", This, debugstr_w(pstrItemName), ppdisp);
959 if(!ppdisp)
960 return E_POINTER;
962 if(!This->thread_data || This->thread_data->thread_id != GetCurrentThreadId() || !This->ctx->global) {
963 *ppdisp = NULL;
964 return E_UNEXPECTED;
967 script_obj = This->ctx->global;
968 if(pstrItemName) {
969 named_item_t *item = lookup_named_item(This->ctx, pstrItemName, 0);
970 if(!item) return E_INVALIDARG;
971 if(item->script_obj) script_obj = item->script_obj;
974 *ppdisp = to_disp(script_obj);
975 IDispatch_AddRef(*ppdisp);
976 return S_OK;
979 static HRESULT WINAPI JScript_GetCurrentScriptThreadID(IActiveScript *iface,
980 SCRIPTTHREADID *pstridThread)
982 JScript *This = impl_from_IActiveScript(iface);
983 FIXME("(%p)->()\n", This);
984 return E_NOTIMPL;
987 static HRESULT WINAPI JScript_GetScriptThreadID(IActiveScript *iface,
988 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
990 JScript *This = impl_from_IActiveScript(iface);
991 FIXME("(%p)->()\n", This);
992 return E_NOTIMPL;
995 static HRESULT WINAPI JScript_GetScriptThreadState(IActiveScript *iface,
996 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
998 JScript *This = impl_from_IActiveScript(iface);
999 FIXME("(%p)->()\n", This);
1000 return E_NOTIMPL;
1003 static HRESULT WINAPI JScript_InterruptScriptThread(IActiveScript *iface,
1004 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
1006 JScript *This = impl_from_IActiveScript(iface);
1007 FIXME("(%p)->()\n", This);
1008 return E_NOTIMPL;
1011 static HRESULT WINAPI JScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
1013 JScript *This = impl_from_IActiveScript(iface);
1014 FIXME("(%p)->()\n", This);
1015 return E_NOTIMPL;
1018 static const IActiveScriptVtbl JScriptVtbl = {
1019 JScript_QueryInterface,
1020 JScript_AddRef,
1021 JScript_Release,
1022 JScript_SetScriptSite,
1023 JScript_GetScriptSite,
1024 JScript_SetScriptState,
1025 JScript_GetScriptState,
1026 JScript_Close,
1027 JScript_AddNamedItem,
1028 JScript_AddTypeLib,
1029 JScript_GetScriptDispatch,
1030 JScript_GetCurrentScriptThreadID,
1031 JScript_GetScriptThreadID,
1032 JScript_GetScriptThreadState,
1033 JScript_InterruptScriptThread,
1034 JScript_Clone
1037 static inline JScript *impl_from_IActiveScriptParse(IActiveScriptParse *iface)
1039 return CONTAINING_RECORD(iface, JScript, IActiveScriptParse_iface);
1042 static HRESULT WINAPI JScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
1044 JScript *This = impl_from_IActiveScriptParse(iface);
1045 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
1048 static ULONG WINAPI JScriptParse_AddRef(IActiveScriptParse *iface)
1050 JScript *This = impl_from_IActiveScriptParse(iface);
1051 return IActiveScript_AddRef(&This->IActiveScript_iface);
1054 static ULONG WINAPI JScriptParse_Release(IActiveScriptParse *iface)
1056 JScript *This = impl_from_IActiveScriptParse(iface);
1057 return IActiveScript_Release(&This->IActiveScript_iface);
1060 static HRESULT WINAPI JScriptParse_InitNew(IActiveScriptParse *iface)
1062 JScript *This = impl_from_IActiveScriptParse(iface);
1064 TRACE("(%p)\n", This);
1066 if(This->is_initialized)
1067 return E_UNEXPECTED;
1068 This->is_initialized = TRUE;
1070 if(This->site)
1071 change_state(This, SCRIPTSTATE_INITIALIZED);
1072 return S_OK;
1075 static HRESULT WINAPI JScriptParse_AddScriptlet(IActiveScriptParse *iface,
1076 LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
1077 LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
1078 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
1079 BSTR *pbstrName, EXCEPINFO *pexcepinfo)
1081 JScript *This = impl_from_IActiveScriptParse(iface);
1082 FIXME("(%p)->(%s %s %s %s %s %s %s %lu %lx %p %p)\n", This, debugstr_w(pstrDefaultName),
1083 debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
1084 debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
1085 ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
1086 return E_NOTIMPL;
1089 static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface,
1090 LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
1091 LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
1092 DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
1094 JScript *This = impl_from_IActiveScriptParse(iface);
1095 named_item_t *item = NULL;
1096 bytecode_t *code;
1097 jsexcept_t ei;
1098 HRESULT hres;
1100 TRACE("(%p)->(%s %s %p %s %s %lu %lx %p %p)\n", This, debugstr_w(pstrCode),
1101 debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
1102 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
1104 if(!This->thread_data || This->thread_data->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
1105 return E_UNEXPECTED;
1107 if(pstrItemName) {
1108 item = lookup_named_item(This->ctx, pstrItemName, 0);
1109 if(!item) {
1110 WARN("Unknown context %s\n", debugstr_w(pstrItemName));
1111 return E_INVALIDARG;
1113 if(!item->script_obj) item = NULL;
1116 enter_script(This->ctx, &ei);
1117 hres = compile_script(This->ctx, pstrCode, dwSourceContextCookie, ulStartingLine, NULL, pstrDelimiter,
1118 (dwFlags & SCRIPTTEXT_ISEXPRESSION) != 0, This->is_encode, item, &code);
1119 if(FAILED(hres))
1120 return leave_script(This->ctx, hres);
1122 if(dwFlags & SCRIPTTEXT_ISEXPRESSION) {
1123 jsval_t r;
1125 hres = exec_global_code(This->ctx, code, &r);
1126 if(SUCCEEDED(hres)) {
1127 if(pvarResult)
1128 hres = jsval_to_variant(r, pvarResult);
1129 jsval_release(r);
1132 return leave_script(This->ctx, hres);
1135 code->is_persistent = (dwFlags & SCRIPTTEXT_ISPERSISTENT) != 0;
1138 * Although pvarResult is not really used without SCRIPTTEXT_ISEXPRESSION flag, if it's not NULL,
1139 * script is executed immediately, even if it's not in started state yet.
1141 if(!pvarResult && !is_started(This->ctx)) {
1142 list_add_tail(&This->queued_code, &code->entry);
1143 }else {
1144 hres = exec_global_code(This->ctx, code, NULL);
1145 if(code->is_persistent)
1146 list_add_tail(&This->persistent_code, &code->entry);
1147 else
1148 release_bytecode(code);
1151 if(FAILED(hres = leave_script(This->ctx, hres)))
1152 return hres;
1154 if(pvarResult)
1155 V_VT(pvarResult) = VT_EMPTY;
1156 return S_OK;
1159 static const IActiveScriptParseVtbl JScriptParseVtbl = {
1160 JScriptParse_QueryInterface,
1161 JScriptParse_AddRef,
1162 JScriptParse_Release,
1163 JScriptParse_InitNew,
1164 JScriptParse_AddScriptlet,
1165 JScriptParse_ParseScriptText
1168 static inline JScript *impl_from_IActiveScriptParseProcedure2(IActiveScriptParseProcedure2 *iface)
1170 return CONTAINING_RECORD(iface, JScript, IActiveScriptParseProcedure2_iface);
1173 static HRESULT WINAPI JScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
1175 JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
1176 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
1179 static ULONG WINAPI JScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
1181 JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
1182 return IActiveScript_AddRef(&This->IActiveScript_iface);
1185 static ULONG WINAPI JScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
1187 JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
1188 return IActiveScript_Release(&This->IActiveScript_iface);
1191 static HRESULT WINAPI JScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
1192 LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
1193 LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
1194 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
1196 JScript *This = impl_from_IActiveScriptParseProcedure2(iface);
1197 named_item_t *item = NULL;
1198 bytecode_t *code;
1199 jsdisp_t *dispex;
1200 jsexcept_t ei;
1201 HRESULT hres;
1203 TRACE("(%p)->(%s %s %s %s %p %s %s %lu %lx %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrFormalParams),
1204 debugstr_w(pstrProcedureName), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
1205 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLineNumber, dwFlags, ppdisp);
1207 if(!This->thread_data || This->thread_data->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED)
1208 return E_UNEXPECTED;
1210 if(pstrItemName) {
1211 item = lookup_named_item(This->ctx, pstrItemName, 0);
1212 if(!item) {
1213 WARN("Unknown context %s\n", debugstr_w(pstrItemName));
1214 return E_INVALIDARG;
1216 if(!item->script_obj) item = NULL;
1219 enter_script(This->ctx, &ei);
1220 hres = compile_script(This->ctx, pstrCode, dwSourceContextCookie, ulStartingLineNumber, pstrFormalParams,
1221 pstrDelimiter, FALSE, This->is_encode, item, &code);
1222 if(FAILED(hres))
1223 return leave_script(This->ctx, hres);
1225 hres = create_source_function(This->ctx, code, &code->global_code, NULL, &dispex);
1226 release_bytecode(code);
1228 hres = leave_script(This->ctx, hres);
1229 if(FAILED(hres))
1230 return hres;
1232 *ppdisp = to_disp(dispex);
1233 return S_OK;
1236 static const IActiveScriptParseProcedure2Vtbl JScriptParseProcedureVtbl = {
1237 JScriptParseProcedure_QueryInterface,
1238 JScriptParseProcedure_AddRef,
1239 JScriptParseProcedure_Release,
1240 JScriptParseProcedure_ParseProcedureText,
1243 static inline JScript *impl_from_IActiveScriptProperty(IActiveScriptProperty *iface)
1245 return CONTAINING_RECORD(iface, JScript, IActiveScriptProperty_iface);
1248 static HRESULT WINAPI JScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
1250 JScript *This = impl_from_IActiveScriptProperty(iface);
1251 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
1254 static ULONG WINAPI JScriptProperty_AddRef(IActiveScriptProperty *iface)
1256 JScript *This = impl_from_IActiveScriptProperty(iface);
1257 return IActiveScript_AddRef(&This->IActiveScript_iface);
1260 static ULONG WINAPI JScriptProperty_Release(IActiveScriptProperty *iface)
1262 JScript *This = impl_from_IActiveScriptProperty(iface);
1263 return IActiveScript_Release(&This->IActiveScript_iface);
1266 static HRESULT WINAPI JScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
1267 VARIANT *pvarIndex, VARIANT *pvarValue)
1269 JScript *This = impl_from_IActiveScriptProperty(iface);
1270 FIXME("(%p)->(%lx %p %p)\n", This, dwProperty, pvarIndex, pvarValue);
1271 return E_NOTIMPL;
1274 static HRESULT WINAPI JScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
1275 VARIANT *pvarIndex, VARIANT *pvarValue)
1277 JScript *This = impl_from_IActiveScriptProperty(iface);
1279 TRACE("(%p)->(%lx %s %s)\n", This, dwProperty, debugstr_variant(pvarIndex), debugstr_variant(pvarValue));
1281 if(pvarIndex)
1282 FIXME("unsupported pvarIndex\n");
1284 switch(dwProperty) {
1285 case SCRIPTPROP_INVOKEVERSIONING:
1286 if(V_VT(pvarValue) != VT_I4 || V_I4(pvarValue) < 0
1287 || (V_I4(pvarValue) > 15 && !(V_I4(pvarValue) & SCRIPTLANGUAGEVERSION_HTML))) {
1288 WARN("invalid value %s\n", debugstr_variant(pvarValue));
1289 return E_INVALIDARG;
1292 This->version = V_I4(pvarValue) & 0x1ff;
1293 This->html_mode = (V_I4(pvarValue) & SCRIPTLANGUAGEVERSION_HTML) != 0;
1294 break;
1295 default:
1296 FIXME("Unimplemented property %lx\n", dwProperty);
1297 return E_NOTIMPL;
1300 return S_OK;
1303 static const IActiveScriptPropertyVtbl JScriptPropertyVtbl = {
1304 JScriptProperty_QueryInterface,
1305 JScriptProperty_AddRef,
1306 JScriptProperty_Release,
1307 JScriptProperty_GetProperty,
1308 JScriptProperty_SetProperty
1311 static inline JScript *impl_from_IObjectSafety(IObjectSafety *iface)
1313 return CONTAINING_RECORD(iface, JScript, IObjectSafety_iface);
1316 static HRESULT WINAPI JScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1318 JScript *This = impl_from_IObjectSafety(iface);
1319 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
1322 static ULONG WINAPI JScriptSafety_AddRef(IObjectSafety *iface)
1324 JScript *This = impl_from_IObjectSafety(iface);
1325 return IActiveScript_AddRef(&This->IActiveScript_iface);
1328 static ULONG WINAPI JScriptSafety_Release(IObjectSafety *iface)
1330 JScript *This = impl_from_IObjectSafety(iface);
1331 return IActiveScript_Release(&This->IActiveScript_iface);
1334 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
1336 static HRESULT WINAPI JScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1337 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
1339 JScript *This = impl_from_IObjectSafety(iface);
1341 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
1343 if(!pdwSupportedOptions || !pdwEnabledOptions)
1344 return E_POINTER;
1346 *pdwSupportedOptions = SUPPORTED_OPTIONS;
1347 *pdwEnabledOptions = This->safeopt;
1349 return S_OK;
1352 static HRESULT WINAPI JScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1353 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
1355 JScript *This = impl_from_IObjectSafety(iface);
1357 TRACE("(%p)->(%s %lx %lx)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
1359 if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
1360 return E_FAIL;
1362 This->safeopt = (dwEnabledOptions & dwOptionSetMask) | (This->safeopt & ~dwOptionSetMask) | INTERFACE_USES_DISPEX;
1363 return S_OK;
1366 static const IObjectSafetyVtbl JScriptSafetyVtbl = {
1367 JScriptSafety_QueryInterface,
1368 JScriptSafety_AddRef,
1369 JScriptSafety_Release,
1370 JScriptSafety_GetInterfaceSafetyOptions,
1371 JScriptSafety_SetInterfaceSafetyOptions
1374 static inline JScript *impl_from_IVariantChangeType(IVariantChangeType *iface)
1376 return CONTAINING_RECORD(iface, JScript, IVariantChangeType_iface);
1379 static HRESULT WINAPI VariantChangeType_QueryInterface(IVariantChangeType *iface, REFIID riid, void **ppv)
1381 JScript *This = impl_from_IVariantChangeType(iface);
1382 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
1385 static ULONG WINAPI VariantChangeType_AddRef(IVariantChangeType *iface)
1387 JScript *This = impl_from_IVariantChangeType(iface);
1388 return IActiveScript_AddRef(&This->IActiveScript_iface);
1391 static ULONG WINAPI VariantChangeType_Release(IVariantChangeType *iface)
1393 JScript *This = impl_from_IVariantChangeType(iface);
1394 return IActiveScript_Release(&This->IActiveScript_iface);
1397 static HRESULT WINAPI VariantChangeType_ChangeType(IVariantChangeType *iface, VARIANT *dst, VARIANT *src, LCID lcid, VARTYPE vt)
1399 JScript *This = impl_from_IVariantChangeType(iface);
1400 jsexcept_t ei;
1401 VARIANT res;
1402 HRESULT hres;
1404 TRACE("(%p)->(%p %s %lx %s)\n", This, dst, debugstr_variant(src), lcid, debugstr_vt(vt));
1406 if(!This->ctx) {
1407 FIXME("Object uninitialized\n");
1408 return E_UNEXPECTED;
1411 enter_script(This->ctx, &ei);
1412 hres = variant_change_type(This->ctx, &res, src, vt);
1413 hres = leave_script(This->ctx, hres);
1414 if(FAILED(hres))
1415 return hres;
1417 hres = VariantClear(dst);
1418 if(FAILED(hres)) {
1419 VariantClear(&res);
1420 return hres;
1423 *dst = res;
1424 return S_OK;
1427 static const IVariantChangeTypeVtbl VariantChangeTypeVtbl = {
1428 VariantChangeType_QueryInterface,
1429 VariantChangeType_AddRef,
1430 VariantChangeType_Release,
1431 VariantChangeType_ChangeType
1434 HRESULT create_jscript_object(BOOL is_encode, REFIID riid, void **ppv)
1436 JScript *ret;
1437 HRESULT hres;
1439 ret = calloc(1, sizeof(*ret));
1440 if(!ret)
1441 return E_OUTOFMEMORY;
1443 lock_module();
1445 ret->IActiveScript_iface.lpVtbl = &JScriptVtbl;
1446 ret->IActiveScriptParse_iface.lpVtbl = &JScriptParseVtbl;
1447 ret->IActiveScriptParseProcedure2_iface.lpVtbl = &JScriptParseProcedureVtbl;
1448 ret->IActiveScriptProperty_iface.lpVtbl = &JScriptPropertyVtbl;
1449 ret->IObjectSafety_iface.lpVtbl = &JScriptSafetyVtbl;
1450 ret->IVariantChangeType_iface.lpVtbl = &VariantChangeTypeVtbl;
1451 ret->ref = 1;
1452 ret->safeopt = INTERFACE_USES_DISPEX;
1453 ret->is_encode = is_encode;
1454 list_init(&ret->persistent_code);
1455 list_init(&ret->queued_code);
1457 hres = IActiveScript_QueryInterface(&ret->IActiveScript_iface, riid, ppv);
1458 IActiveScript_Release(&ret->IActiveScript_iface);
1459 return hres;