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
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
31 #define CTXARG_T DWORDLONG
32 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
33 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_64Vtbl
37 #define CTXARG_T DWORD
38 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
39 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_32Vtbl
44 IActiveScript IActiveScript_iface
;
45 IActiveScriptParse IActiveScriptParse_iface
;
46 IActiveScriptParseProcedure2 IActiveScriptParseProcedure2_iface
;
47 IActiveScriptProperty IActiveScriptProperty_iface
;
48 IObjectSafety IObjectSafety_iface
;
49 IVariantChangeType IVariantChangeType_iface
;
54 struct thread_data
*thread_data
;
62 IActiveScriptSite
*site
;
64 struct list persistent_code
;
65 struct list queued_code
;
69 IActiveScriptError IActiveScriptError_iface
;
74 void script_release(script_ctx_t
*ctx
)
79 jsval_release(ctx
->acc
);
82 heap_pool_free(&ctx
->tmp_heap
);
84 jsstr_release(ctx
->last_match
);
85 assert(!ctx
->stack_top
);
88 ctx
->jscaller
->ctx
= NULL
;
89 IServiceProvider_Release(&ctx
->jscaller
->IServiceProvider_iface
);
91 release_thread_data(ctx
->thread_data
);
95 static void script_globals_release(script_ctx_t
*ctx
)
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
)
111 This
->ctx
->state
= state
;
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
= {
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
)
152 hr
= IActiveScriptSite_GetItemInfo(site
, item
->name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
154 WARN("GetItemInfo failed: %08lx\n", hr
);
158 hr
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
159 IUnknown_Release(unk
);
161 WARN("object does not implement IDispatch\n");
168 named_item_t
*lookup_named_item(script_ctx_t
*ctx
, const WCHAR
*item_name
, unsigned flags
)
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;
192 void release_named_item(named_item_t
*item
)
194 if(--item
->ref
) return;
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
;
216 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
218 return E_NOINTERFACE
;
221 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
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
);
250 static HRESULT WINAPI
JScriptError_GetExceptionInfo(IActiveScriptError
*iface
, EXCEPINFO
*excepinfo
)
252 JScriptError
*This
= impl_from_IActiveScriptError(iface
);
254 TRACE("(%p)->(%p)\n", This
, excepinfo
);
259 memset(excepinfo
, 0, sizeof(*excepinfo
));
260 excepinfo
->scode
= This
->ei
.error
;
262 jsstr_to_bstr(This
->ei
.source
, &excepinfo
->bstrSource
);
264 jsstr_to_bstr(This
->ei
.message
, &excepinfo
->bstrDescription
);
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
);
277 FIXME("unknown position\n");
282 *source_context
= This
->ei
.code
->source_context
;
283 if(!line
&& !character
)
286 line_pos
= get_location_line(code
, This
->ei
.loc
, &char_pos
);
290 *character
= char_pos
;
294 static HRESULT WINAPI
JScriptError_GetSourceLineText(IActiveScriptError
*iface
, BSTR
*source
)
296 JScriptError
*This
= impl_from_IActiveScriptError(iface
);
298 TRACE("(%p)->(%p)\n", This
, source
);
308 return jsstr_to_bstr(This
->ei
.line
, source
);
311 static const IActiveScriptErrorVtbl JScriptErrorVtbl
= {
312 JScriptError_QueryInterface
,
314 JScriptError_Release
,
315 JScriptError_GetExceptionInfo
,
316 JScriptError_GetSourcePosition
,
317 JScriptError_GetSourceLineText
320 void reset_ei(jsexcept_t
*ei
)
323 if(ei
->valid_value
) {
324 jsval_release(ei
->value
);
325 ei
->valid_value
= FALSE
;
328 release_bytecode(ei
->code
);
333 jsstr_release(ei
->source
);
337 jsstr_release(ei
->message
);
341 jsstr_release(ei
->line
);
346 void enter_script(script_ctx_t
*ctx
, jsexcept_t
*ei
)
348 memset(ei
, 0, sizeof(*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
;
360 TRACE("ctx %p ei %p prev %p\n", ctx
, ei
, ei
->prev
);
363 if(result
== DISP_E_EXCEPTION
) {
370 WARN("%08lx\n", result
);
371 if(ctx
->site
&& (error
= malloc(sizeof(*error
)))) {
374 error
->IActiveScriptError_iface
.lpVtbl
= &JScriptErrorVtbl
;
377 memset(ei
, 0, sizeof(*ei
));
379 hres
= IActiveScriptSite_OnScriptError(ctx
->site
, &error
->IActiveScriptError_iface
);
380 IActiveScriptError_Release(&error
->IActiveScriptError_iface
);
382 result
= SCRIPT_E_REPORTED
;
385 if(enter_notified
&& ctx
->site
)
386 IActiveScriptSite_OnLeaveScript(ctx
->site
);
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
);
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
)
418 LIST_FOR_EACH_ENTRY(iter
, &This
->persistent_code
, bytecode_t
, entry
)
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
;
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
;
443 static void exec_queued_code(JScript
*This
)
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
);
457 clear_script_queue(This
);
460 static void decrease_state(JScript
*This
, SCRIPTSTATE state
)
462 named_item_t
*item
, *item_next
;
465 switch(This
->ctx
->state
) {
466 case SCRIPTSTATE_CONNECTED
:
467 change_state(This
, SCRIPTSTATE_DISCONNECTED
);
468 if(state
== SCRIPTSTATE_DISCONNECTED
)
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
)
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
)
488 IDispatch_Release(item
->disp
);
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
);
513 case SCRIPTSTATE_UNINITIALIZED
:
514 change_state(This
, state
);
520 change_state(This
, state
);
521 }else if(state
== SCRIPTSTATE_UNINITIALIZED
) {
523 IActiveScriptSite_OnStateChange(This
->site
, state
);
528 if((state
== SCRIPTSTATE_UNINITIALIZED
|| state
== SCRIPTSTATE_CLOSED
) && This
->thread_data
) {
529 release_thread_data(This
->thread_data
);
530 This
->thread_data
= NULL
;
534 IActiveScriptSite_Release(This
->site
);
540 IServiceProvider IServiceProvider_iface
;
544 IServiceProvider
*sp
;
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
;
563 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
565 return E_NOINTERFACE
;
568 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
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
);
592 IServiceProvider_Release(This
->sp
);
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
);
608 return E_NOINTERFACE
;
610 return IServiceProvider_QueryService(This
->sp
, guidService
, riid
, ppv
);
613 static IServiceProviderVtbl AXSiteVtbl
= {
614 AXSite_QueryInterface
,
620 IUnknown
*create_ax_site(script_ctx_t
*ctx
)
622 IServiceProvider
*sp
= NULL
;
626 hres
= IActiveScriptSite_QueryInterface(ctx
->site
, &IID_IServiceProvider
, (void**)&sp
);
628 TRACE("Could not get IServiceProvider iface: %08lx\n", hres
);
631 ret
= malloc(sizeof(AXSite
));
633 IServiceProvider_Release(sp
);
637 ret
->IServiceProvider_iface
.lpVtbl
= &AXSiteVtbl
;
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
);
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
;
682 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
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
);
708 if(This
->ctx
&& This
->ctx
->state
!= SCRIPTSTATE_CLOSED
)
709 IActiveScript_Close(&This
->IActiveScript_iface
);
711 This
->ctx
->active_script
= NULL
;
712 script_release(This
->ctx
);
714 if(This
->thread_data
)
715 release_thread_data(This
->thread_data
);
723 static HRESULT WINAPI
JScript_SetScriptSite(IActiveScript
*iface
,
724 IActiveScriptSite
*pass
)
726 JScript
*This
= impl_from_IActiveScript(iface
);
727 struct thread_data
*thread_data
;
732 TRACE("(%p)->(%p)\n", This
, pass
);
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
);
749 script_ctx_t
*ctx
= calloc(1, sizeof(script_ctx_t
));
751 return E_OUTOFMEMORY
;
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
);
770 ctx
->thread_data
= thread_data
;
771 ctx
->last_match
= jsstr_empty();
776 /* Retrieve new dispatches for persistent named items */
777 LIST_FOR_EACH_ENTRY(item
, &This
->ctx
->named_items
, named_item_t
, entry
)
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
;
790 IActiveScriptSite_AddRef(This
->site
);
792 hres
= IActiveScriptSite_GetLCID(This
->site
, &lcid
);
796 This
->ctx
->lcid
= This
->lcid
;
798 hres
= init_global(This
->ctx
);
802 IActiveScriptSite_AddRef(This
->site
);
803 This
->ctx
->site
= This
->site
;
805 if(This
->is_initialized
)
806 change_state(This
, SCRIPTSTATE_INITIALIZED
);
810 static HRESULT WINAPI
JScript_GetScriptSite(IActiveScript
*iface
, REFIID riid
,
813 JScript
*This
= impl_from_IActiveScript(iface
);
814 FIXME("(%p)->()\n", This
);
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())
827 if(ss
== SCRIPTSTATE_UNINITIALIZED
) {
828 if(This
->ctx
&& This
->ctx
->state
== SCRIPTSTATE_CLOSED
)
831 decrease_state(This
, SCRIPTSTATE_UNINITIALIZED
);
832 list_move_tail(&This
->queued_code
, &This
->persistent_code
);
836 if(!This
->is_initialized
|| !This
->ctx
)
840 case SCRIPTSTATE_STARTED
:
841 case SCRIPTSTATE_CONNECTED
: /* FIXME */
842 if(This
->ctx
->state
== SCRIPTSTATE_UNINITIALIZED
|| This
->ctx
->state
== SCRIPTSTATE_CLOSED
)
845 exec_queued_code(This
);
847 case SCRIPTSTATE_INITIALIZED
:
848 FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
851 FIXME("unimplemented state %d\n", ss
);
855 change_state(This
, ss
);
859 static HRESULT WINAPI
JScript_GetScriptState(IActiveScript
*iface
, SCRIPTSTATE
*pssState
)
861 JScript
*This
= impl_from_IActiveScript(iface
);
863 TRACE("(%p)->(%p)\n", This
, pssState
);
868 if(This
->thread_data
&& This
->thread_data
->thread_id
!= GetCurrentThreadId())
871 *pssState
= This
->ctx
? This
->ctx
->state
: SCRIPTSTATE_UNINITIALIZED
;
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())
884 decrease_state(This
, SCRIPTSTATE_CLOSED
);
885 clear_persistent_code_list(This
);
886 release_named_item_list(This
);
890 static HRESULT WINAPI
JScript_AddNamedItem(IActiveScript
*iface
,
891 LPCOLESTR pstrName
, DWORD dwFlags
)
893 JScript
*This
= impl_from_IActiveScript(iface
);
895 IDispatch
*disp
= NULL
;
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
)
903 if(dwFlags
& SCRIPTITEM_GLOBALMEMBERS
) {
906 hres
= IActiveScriptSite_GetItemInfo(This
->site
, pstrName
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
908 WARN("GetItemInfo failed: %08lx\n", hres
);
912 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&disp
);
913 IUnknown_Release(unk
);
915 WARN("object does not implement IDispatch\n");
920 item
= malloc(sizeof(*item
));
923 IDispatch_Release(disp
);
924 return E_OUTOFMEMORY
;
929 item
->flags
= dwFlags
;
930 item
->script_obj
= NULL
;
931 item
->name
= wcsdup(pstrName
);
934 IDispatch_Release(disp
);
936 return E_OUTOFMEMORY
;
939 list_add_tail(&This
->ctx
->named_items
, &item
->entry
);
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
);
951 static HRESULT WINAPI
JScript_GetScriptDispatch(IActiveScript
*iface
, LPCOLESTR pstrItemName
,
954 JScript
*This
= impl_from_IActiveScript(iface
);
955 jsdisp_t
*script_obj
;
957 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(pstrItemName
), ppdisp
);
962 if(!This
->thread_data
|| This
->thread_data
->thread_id
!= GetCurrentThreadId() || !This
->ctx
->global
) {
967 script_obj
= This
->ctx
->global
;
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
);
979 static HRESULT WINAPI
JScript_GetCurrentScriptThreadID(IActiveScript
*iface
,
980 SCRIPTTHREADID
*pstridThread
)
982 JScript
*This
= impl_from_IActiveScript(iface
);
983 FIXME("(%p)->()\n", This
);
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
);
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
);
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
);
1011 static HRESULT WINAPI
JScript_Clone(IActiveScript
*iface
, IActiveScript
**ppscript
)
1013 JScript
*This
= impl_from_IActiveScript(iface
);
1014 FIXME("(%p)->()\n", This
);
1018 static const IActiveScriptVtbl JScriptVtbl
= {
1019 JScript_QueryInterface
,
1022 JScript_SetScriptSite
,
1023 JScript_GetScriptSite
,
1024 JScript_SetScriptState
,
1025 JScript_GetScriptState
,
1027 JScript_AddNamedItem
,
1029 JScript_GetScriptDispatch
,
1030 JScript_GetCurrentScriptThreadID
,
1031 JScript_GetScriptThreadID
,
1032 JScript_GetScriptThreadState
,
1033 JScript_InterruptScriptThread
,
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
;
1071 change_state(This
, SCRIPTSTATE_INITIALIZED
);
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
);
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
;
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
;
1108 item
= lookup_named_item(This
->ctx
, pstrItemName
, 0);
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
);
1120 return leave_script(This
->ctx
, hres
);
1122 if(dwFlags
& SCRIPTTEXT_ISEXPRESSION
) {
1125 hres
= exec_global_code(This
->ctx
, code
, &r
);
1126 if(SUCCEEDED(hres
)) {
1128 hres
= jsval_to_variant(r
, pvarResult
);
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
);
1144 hres
= exec_global_code(This
->ctx
, code
, NULL
);
1145 if(code
->is_persistent
)
1146 list_add_tail(&This
->persistent_code
, &code
->entry
);
1148 release_bytecode(code
);
1151 if(FAILED(hres
= leave_script(This
->ctx
, hres
)))
1155 V_VT(pvarResult
) = VT_EMPTY
;
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
;
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
;
1211 item
= lookup_named_item(This
->ctx
, pstrItemName
, 0);
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
);
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
);
1232 *ppdisp
= to_disp(dispex
);
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
);
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
));
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;
1296 FIXME("Unimplemented property %lx\n", dwProperty
);
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
)
1346 *pdwSupportedOptions
= SUPPORTED_OPTIONS
;
1347 *pdwEnabledOptions
= This
->safeopt
;
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
)
1362 This
->safeopt
= (dwEnabledOptions
& dwOptionSetMask
) | (This
->safeopt
& ~dwOptionSetMask
) | INTERFACE_USES_DISPEX
;
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
);
1404 TRACE("(%p)->(%p %s %lx %s)\n", This
, dst
, debugstr_variant(src
), lcid
, debugstr_vt(vt
));
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
);
1417 hres
= VariantClear(dst
);
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
)
1439 ret
= calloc(1, sizeof(*ret
));
1441 return E_OUTOFMEMORY
;
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
;
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
);