2 * Copyright 2008,2011 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
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
30 struct _except_frame_t
{
57 static const size_t stack_size
= 0x40000;
59 static HRESULT
stack_push(script_ctx_t
*ctx
, jsval_t v
)
61 if(ctx
->stack_top
== stack_size
)
62 return JS_E_STACK_OVERFLOW
;
64 ctx
->stack
[ctx
->stack_top
++] = v
;
68 static inline HRESULT
stack_push_string(script_ctx_t
*ctx
, const WCHAR
*str
)
76 return stack_push(ctx
, jsval_string(v
));
79 static inline jsval_t
stack_top(script_ctx_t
*ctx
)
81 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
82 return ctx
->stack
[ctx
->stack_top
-1];
85 static inline jsval_t
*stack_top_ref(script_ctx_t
*ctx
, unsigned n
)
87 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
);
88 return ctx
->stack
+ctx
->stack_top
-1-n
;
91 static inline jsval_t
stack_topn(script_ctx_t
*ctx
, unsigned n
)
93 return *stack_top_ref(ctx
, n
);
96 static inline jsval_t
*stack_args(script_ctx_t
*ctx
, unsigned n
)
100 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
-1);
101 return ctx
->stack
+ ctx
->stack_top
-n
;
104 static inline jsval_t
stack_pop(script_ctx_t
*ctx
)
106 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
107 return ctx
->stack
[--ctx
->stack_top
];
110 static void stack_popn(script_ctx_t
*ctx
, unsigned n
)
113 jsval_release(stack_pop(ctx
));
116 static HRESULT
stack_pop_number(script_ctx_t
*ctx
, double *r
)
122 hres
= to_number(ctx
, v
, r
);
127 static HRESULT
stack_pop_object(script_ctx_t
*ctx
, IDispatch
**r
)
133 if(is_object_instance(v
)) {
138 hres
= to_object(ctx
, v
, r
);
143 static inline HRESULT
stack_pop_int(script_ctx_t
*ctx
, INT
*r
)
145 return to_int32(ctx
, stack_pop(ctx
), r
);
148 static inline HRESULT
stack_pop_uint(script_ctx_t
*ctx
, UINT32
*r
)
150 return to_uint32(ctx
, stack_pop(ctx
), r
);
153 static inline unsigned local_off(call_frame_t
*frame
, int ref
)
156 ? frame
->arguments_off
- ref
-1
157 : frame
->variables_off
+ ref
;
160 static inline BSTR
local_name(call_frame_t
*frame
, int ref
)
162 return ref
< 0 ? frame
->function
->params
[-ref
-1] : frame
->function
->variables
[ref
].name
;
165 /* Steals input reference even on failure. */
166 static HRESULT
stack_push_exprval(script_ctx_t
*ctx
, exprval_t
*val
)
172 hres
= stack_push(ctx
, jsval_null());
174 hres
= stack_push(ctx
, val
->u
.val
);
177 hres
= stack_push(ctx
, jsval_disp(val
->u
.idref
.disp
));
179 hres
= stack_push(ctx
, jsval_number(val
->u
.idref
.id
));
181 IDispatch_Release(val
->u
.idref
.disp
);
183 case EXPRVAL_STACK_REF
:
184 hres
= stack_push(ctx
, jsval_number(val
->u
.off
));
186 hres
= stack_push(ctx
, jsval_undefined());
188 case EXPRVAL_INVALID
:
189 hres
= stack_push(ctx
, jsval_undefined());
191 hres
= stack_push(ctx
, jsval_number(val
->u
.hres
));
199 static BOOL
stack_topn_exprval(script_ctx_t
*ctx
, unsigned n
, exprval_t
*r
)
201 jsval_t v
= stack_topn(ctx
, n
+1);
203 switch(jsval_type(v
)) {
205 call_frame_t
*frame
= ctx
->call_ctx
;
206 scope_chain_t
*scope
;
207 unsigned off
= get_number(v
);
209 if(!frame
->base_scope
->frame
&& off
>= frame
->arguments_off
) {
212 HRESULT hres
= E_FAIL
;
214 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
216 assert(off
< frame
->variables_off
+ frame
->function
->var_cnt
);
217 if (off
>= frame
->variables_off
)
219 name
= frame
->function
->variables
[off
- frame
->variables_off
].name
;
220 scope
= frame
->scope
;
224 name
= frame
->function
->params
[off
- frame
->arguments_off
];
225 scope
= frame
->base_scope
;
230 if (scope
->jsobj
&& SUCCEEDED(hres
= jsdisp_get_id(scope
->jsobj
, name
, 0, &id
)))
232 if (scope
== frame
->base_scope
)
234 r
->type
= EXPRVAL_INVALID
;
241 *stack_top_ref(ctx
, n
+1) = jsval_obj(jsdisp_addref(scope
->jsobj
));
242 *stack_top_ref(ctx
, n
) = jsval_number(id
);
243 r
->type
= EXPRVAL_IDREF
;
244 r
->u
.idref
.disp
= scope
->obj
;
249 r
->type
= EXPRVAL_STACK_REF
;
254 r
->type
= EXPRVAL_IDREF
;
255 r
->u
.idref
.disp
= get_object(v
);
256 assert(is_number(stack_topn(ctx
, n
)));
257 r
->u
.idref
.id
= get_number(stack_topn(ctx
, n
));
260 r
->type
= EXPRVAL_INVALID
;
261 assert(is_number(stack_topn(ctx
, n
)));
262 r
->u
.hres
= get_number(stack_topn(ctx
, n
));
265 r
->type
= EXPRVAL_JSVAL
;
266 r
->u
.val
= stack_topn(ctx
, n
);
274 static inline BOOL
stack_pop_exprval(script_ctx_t
*ctx
, exprval_t
*r
)
276 BOOL ret
= stack_topn_exprval(ctx
, 0, r
);
281 static HRESULT
exprval_propput(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t v
)
284 case EXPRVAL_STACK_REF
: {
285 jsval_t
*r
= ctx
->stack
+ ref
->u
.off
;
287 return jsval_copy(v
, r
);
290 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
);
292 WARN("ignoring an attempt to set value reference\n");
300 static HRESULT
exprval_propget(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
303 case EXPRVAL_STACK_REF
:
304 return jsval_copy(ctx
->stack
[ref
->u
.off
], r
);
306 return disp_propget(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, r
);
308 return jsval_copy(ref
->u
.val
, r
);
315 static HRESULT
exprval_call(script_ctx_t
*ctx
, exprval_t
*ref
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
322 case EXPRVAL_STACK_REF
: {
323 v
= ctx
->stack
[ref
->u
.off
];
325 if(!is_object_instance(v
)) {
326 FIXME("invoke %s\n", debugstr_jsval(v
));
330 return disp_call_value(ctx
, get_object(v
), NULL
, flags
, argc
, argv
, r
);
333 /* ECMA-262 3rd Edition 11.2.3.7 / ECMA-262 5.1 Edition 11.2.3.6 *
334 * Don't treat scope object props as PropertyReferences. */
335 if((jsdisp
= to_jsdisp(ref
->u
.idref
.disp
)) && jsdisp
->builtin_info
->class == JSCLASS_NONE
) {
336 hres
= disp_propget(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, &v
);
339 if(!is_object_instance(v
)) {
340 FIXME("invoke %s\n", debugstr_jsval(v
));
343 hres
= disp_call_value(ctx
, get_object(v
), NULL
, flags
, argc
, argv
, r
);
348 return disp_call(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, flags
, argc
, argv
, r
);
349 case EXPRVAL_JSVAL
: {
352 hres
= to_object(ctx
, ref
->u
.val
, &obj
);
353 if(SUCCEEDED(hres
)) {
354 hres
= disp_call_value(ctx
, obj
, NULL
, flags
, argc
, argv
, r
);
355 IDispatch_Release(obj
);
365 /* ECMA-262 3rd Edition 8.7.1 */
366 /* Steals input reference. */
367 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
371 if(ref
->type
== EXPRVAL_JSVAL
) {
376 hres
= exprval_propget(ctx
, ref
, r
);
378 if(ref
->type
== EXPRVAL_IDREF
)
379 IDispatch_Release(ref
->u
.idref
.disp
);
383 static void exprval_release(exprval_t
*val
)
387 jsval_release(val
->u
.val
);
390 if(val
->u
.idref
.disp
)
391 IDispatch_Release(val
->u
.idref
.disp
);
393 case EXPRVAL_STACK_REF
:
394 case EXPRVAL_INVALID
:
399 static inline void exprval_set_exception(exprval_t
*val
, HRESULT hres
)
401 val
->type
= EXPRVAL_INVALID
;
405 static inline void exprval_set_disp_ref(exprval_t
*ref
, IDispatch
*obj
, DISPID id
)
407 ref
->type
= EXPRVAL_IDREF
;
408 IDispatch_AddRef(ref
->u
.idref
.disp
= obj
);
409 ref
->u
.idref
.id
= id
;
412 static inline jsval_t
steal_ret(call_frame_t
*frame
)
414 jsval_t r
= frame
->ret
;
415 frame
->ret
= jsval_undefined();
419 static inline void clear_acc(script_ctx_t
*ctx
)
421 jsval_release(ctx
->acc
);
422 ctx
->acc
= jsval_undefined();
425 static HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
427 scope_chain_t
*new_scope
;
429 new_scope
= heap_alloc(sizeof(scope_chain_t
));
431 return E_OUTOFMEMORY
;
436 IDispatch_AddRef(obj
);
437 new_scope
->jsobj
= jsobj
;
438 new_scope
->obj
= obj
;
439 new_scope
->frame
= NULL
;
440 new_scope
->next
= scope
? scope_addref(scope
) : NULL
;
441 new_scope
->scope_index
= 0;
447 static void scope_pop(scope_chain_t
**scope
)
456 void scope_release(scope_chain_t
*scope
)
462 scope_release(scope
->next
);
465 IDispatch_Release(scope
->obj
);
469 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
476 jsdisp
= iface_to_jsdisp(disp
);
478 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
479 jsdisp_release(jsdisp
);
486 bstr
= SysAllocString(name
);
488 return E_OUTOFMEMORY
;
492 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
493 if(SUCCEEDED(hres
)) {
494 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
495 IDispatchEx_Release(dispex
);
497 TRACE("using IDispatch\n");
498 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
501 if(name_bstr
!= bstr
)
506 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
508 IObjectIdentity
*identity
;
509 IUnknown
*unk1
, *unk2
;
517 if(!disp1
|| !disp2
) {
522 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
526 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
528 IUnknown_Release(unk1
);
535 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
536 if(SUCCEEDED(hres
)) {
537 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
538 IObjectIdentity_Release(identity
);
545 IUnknown_Release(unk1
);
546 IUnknown_Release(unk2
);
550 /* ECMA-262 3rd Edition 11.9.6 */
551 HRESULT
jsval_strict_equal(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
553 jsval_type_t type
= jsval_type(lval
);
557 if(type
!= jsval_type(rval
)) {
568 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
570 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
573 *ret
= get_number(lval
) == get_number(rval
);
576 *ret
= !get_bool(lval
) == !get_bool(rval
);
579 WARN("VARIANT type, returning false\n");
587 static HRESULT
detach_scope(script_ctx_t
*ctx
, call_frame_t
*frame
, scope_chain_t
*scope
)
589 function_code_t
*func
= frame
->function
;
590 unsigned int i
, index
;
596 assert(scope
->frame
== frame
);
603 if (FAILED(hres
= create_object(ctx
, NULL
, &scope
->jsobj
)))
605 scope
->obj
= to_disp(scope
->jsobj
);
608 if (scope
== frame
->base_scope
&& func
->name
&& func
->local_ref
== INVALID_LOCAL_REF
&&
609 ctx
->version
>= SCRIPTLANGUAGEVERSION_ES5
)
610 jsdisp_propput_name(scope
->jsobj
, func
->name
, jsval_obj(jsdisp_addref(frame
->function_instance
)));
612 index
= scope
->scope_index
;
613 for(i
= 0; i
< frame
->function
->local_scopes
[index
].locals_cnt
; i
++)
615 WCHAR
*name
= frame
->function
->local_scopes
[index
].locals
[i
].name
;
616 int ref
= frame
->function
->local_scopes
[index
].locals
[i
].ref
;
618 if (FAILED(hres
= jsdisp_propput_name(scope
->jsobj
, name
, ctx
->stack
[local_off(frame
, ref
)])))
620 if (frame
->function
->variables
[ref
].func_id
!= -1 && scope
!= frame
->base_scope
621 && FAILED(hres
= jsdisp_propput_name(frame
->variable_obj
, name
, ctx
->stack
[local_off(frame
, ref
)])))
627 static HRESULT
detach_scope_chain(script_ctx_t
*ctx
, call_frame_t
*frame
, scope_chain_t
*scope
)
631 if (scope
!= frame
->base_scope
&& FAILED(hres
= detach_scope_chain(ctx
, frame
, scope
->next
)))
633 return detach_scope(ctx
, frame
, scope
);
637 * Transfers local variables from stack to variable object.
638 * It's slow, so we want to avoid it as much as possible.
640 static HRESULT
detach_variable_object(script_ctx_t
*ctx
, call_frame_t
*frame
, BOOL from_release
)
644 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
647 TRACE("detaching %p\n", frame
);
649 assert(frame
== frame
->base_scope
->frame
);
650 assert(frame
->variable_obj
== frame
->base_scope
->jsobj
);
652 if(!from_release
&& !frame
->arguments_obj
) {
653 hres
= setup_arguments_object(ctx
, frame
);
658 TRACE("detaching scope chain %p, frame %p.\n", ctx
->call_ctx
->scope
, frame
);
659 return detach_scope_chain(ctx
, frame
, ctx
->call_ctx
->scope
);
662 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
668 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
669 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
670 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
671 if(SUCCEEDED(hres
)) {
673 exprval_set_disp_ref(ret
, item
->disp
, id
);
682 IDispatch
*lookup_global_host(script_ctx_t
*ctx
)
684 IDispatch
*disp
= NULL
;
687 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
688 if(!(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) continue;
692 if(!disp
) disp
= to_disp(ctx
->global
);
697 static int __cdecl
local_ref_cmp(const void *key
, const void *ref
)
699 return wcscmp((const WCHAR
*)key
, ((const local_ref_t
*)ref
)->name
);
702 local_ref_t
*lookup_local(const function_code_t
*function
, const WCHAR
*identifier
, unsigned int scope
)
704 return bsearch(identifier
, function
->local_scopes
[scope
].locals
, function
->local_scopes
[scope
].locals_cnt
,
705 sizeof(*function
->local_scopes
[scope
].locals
), local_ref_cmp
);
708 /* ECMA-262 3rd Edition 10.1.4 */
709 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
711 scope_chain_t
*scope
;
716 TRACE("%s\n", debugstr_w(identifier
));
719 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
721 function_code_t
*func
= scope
->frame
->function
;
722 local_ref_t
*ref
= lookup_local(func
, identifier
, scope
->scope_index
);
725 ret
->type
= EXPRVAL_STACK_REF
;
726 ret
->u
.off
= local_off(scope
->frame
, ref
->ref
);
727 TRACE("returning ref %d for %d\n", ret
->u
.off
, ref
->ref
);
731 if(!wcscmp(identifier
, L
"arguments")) {
732 hres
= detach_variable_object(ctx
, scope
->frame
, FALSE
);
737 /* ECMA-262 5.1 Edition 13 */
738 if(func
->name
&& ctx
->version
>= SCRIPTLANGUAGEVERSION_ES5
&&
739 func
->local_ref
== INVALID_LOCAL_REF
&& !wcscmp(identifier
, func
->name
)) {
740 TRACE("returning a function from scope chain\n");
741 ret
->type
= EXPRVAL_JSVAL
;
742 ret
->u
.val
= jsval_obj(jsdisp_addref(scope
->frame
->function_instance
));
747 if (!scope
->jsobj
&& !scope
->obj
)
751 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
753 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
754 if(SUCCEEDED(hres
)) {
755 exprval_set_disp_ref(ret
, scope
->obj
, id
);
760 item
= ctx
->call_ctx
->bytecode
->named_item
;
762 hres
= jsdisp_get_id(item
->script_obj
, identifier
, 0, &id
);
763 if(SUCCEEDED(hres
)) {
764 exprval_set_disp_ref(ret
, to_disp(item
->script_obj
), id
);
767 if(!(item
->flags
& SCRIPTITEM_CODEONLY
)) {
768 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
769 if(SUCCEEDED(hres
)) {
770 exprval_set_disp_ref(ret
, item
->disp
, id
);
777 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
778 if(SUCCEEDED(hres
)) {
779 exprval_set_disp_ref(ret
, to_disp(ctx
->global
), id
);
783 item
= lookup_named_item(ctx
, identifier
, SCRIPTITEM_ISVISIBLE
);
785 IDispatch_AddRef(item
->disp
);
786 ret
->type
= EXPRVAL_JSVAL
;
787 ret
->u
.val
= jsval_disp(item
->disp
);
791 if(lookup_global_members(ctx
, identifier
, ret
))
794 exprval_set_exception(ret
, JS_E_UNDEFINED_VARIABLE
);
798 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
800 call_frame_t
*frame
= ctx
->call_ctx
;
801 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
804 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
806 call_frame_t
*frame
= ctx
->call_ctx
;
807 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
810 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
812 call_frame_t
*frame
= ctx
->call_ctx
;
813 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
816 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
818 call_frame_t
*frame
= ctx
->call_ctx
;
819 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
822 static inline double get_op_double(script_ctx_t
*ctx
)
824 call_frame_t
*frame
= ctx
->call_ctx
;
825 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
828 static inline void jmp_next(script_ctx_t
*ctx
)
833 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
835 ctx
->call_ctx
->ip
= dst
;
838 /* ECMA-262 3rd Edition 12.6.4 */
839 static HRESULT
interp_forin(script_ctx_t
*ctx
)
841 const HRESULT arg
= get_op_uint(ctx
, 0);
842 IDispatch
*obj
= NULL
;
851 assert(is_number(stack_top(ctx
)));
852 id
= get_number(stack_top(ctx
));
854 if(!stack_topn_exprval(ctx
, 1, &prop_ref
)) {
855 FIXME("invalid ref: %08lx\n", prop_ref
.u
.hres
);
859 if(is_object_instance(stack_topn(ctx
, 3)))
860 obj
= get_object(stack_topn(ctx
, 3));
863 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
864 if(SUCCEEDED(hres
)) {
865 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
867 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
868 IDispatchEx_Release(dispex
);
872 TRACE("No IDispatchEx\n");
879 str
= jsstr_alloc_len(name
, SysStringLen(name
));
882 return E_OUTOFMEMORY
;
885 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
887 hres
= exprval_propput(ctx
, &prop_ref
, jsval_string(str
));
900 static HRESULT
scope_init_locals(script_ctx_t
*ctx
)
902 call_frame_t
*frame
= ctx
->call_ctx
;
903 unsigned int i
, off
, index
;
904 scope_chain_t
*scope
;
908 scope
= frame
->scope
;
909 index
= scope
->scope_index
;
910 detached_vars
= !(frame
->base_scope
&& frame
->base_scope
->frame
);
914 assert(frame
->base_scope
->frame
== frame
);
915 frame
->scope
->frame
= ctx
->call_ctx
;
917 else if (!scope
->jsobj
)
920 if (FAILED(hres
= create_object(ctx
, NULL
, &scope
->jsobj
)))
922 scope
->obj
= to_disp(scope
->jsobj
);
925 for(i
= 0; i
< frame
->function
->local_scopes
[index
].locals_cnt
; i
++)
927 WCHAR
*name
= frame
->function
->local_scopes
[index
].locals
[i
].name
;
928 int ref
= frame
->function
->local_scopes
[index
].locals
[i
].ref
;
932 if (frame
->function
->variables
[ref
].func_id
!= -1)
934 TRACE("function %s %d\n", debugstr_w(name
), i
);
936 if (FAILED(hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
937 + frame
->function
->variables
[ref
].func_id
, ctx
->call_ctx
->scope
, &func_obj
)))
939 val
= jsval_obj(func_obj
);
940 if (detached_vars
&& FAILED(hres
= jsdisp_propput_name(frame
->variable_obj
, name
, jsval_obj(func_obj
))))
945 val
= jsval_undefined();
950 if (FAILED(hres
= jsdisp_propput_name(scope
->jsobj
, name
, val
)))
955 off
= local_off(frame
, ref
);
956 jsval_release(ctx
->stack
[off
]);
957 ctx
->stack
[off
] = val
;
963 /* ECMA-262 3rd Edition 12.10 */
964 static HRESULT
interp_push_with_scope(script_ctx_t
*ctx
)
973 hres
= to_object(ctx
, v
, &disp
);
978 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
979 IDispatch_Release(disp
);
983 /* ECMA-262 10th Edition 13.3.1 */
984 static HRESULT
interp_push_block_scope(script_ctx_t
*ctx
)
986 unsigned int scope_index
= get_op_uint(ctx
, 0);
987 call_frame_t
*frame
= ctx
->call_ctx
;
990 TRACE("scope_index %u.\n", scope_index
);
992 hres
= scope_push(ctx
->call_ctx
->scope
, NULL
, NULL
, &frame
->scope
);
994 if (FAILED(hres
) || !scope_index
)
997 frame
->scope
->scope_index
= scope_index
;
999 return scope_init_locals(ctx
);
1002 /* ECMA-262 3rd Edition 12.10 */
1003 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
1007 if(ctx
->call_ctx
->scope
->ref
> 1) {
1008 HRESULT hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
1010 ERR("Failed to detach variable object: %08lx\n", hres
);
1013 scope_pop(&ctx
->call_ctx
->scope
);
1017 /* ECMA-262 3rd Edition 12.13 */
1018 static HRESULT
interp_case(script_ctx_t
*ctx
)
1020 const unsigned arg
= get_op_uint(ctx
, 0);
1028 hres
= jsval_strict_equal(stack_top(ctx
), v
, &b
);
1042 static void set_error_value(script_ctx_t
*ctx
, jsval_t value
)
1044 jsexcept_t
*ei
= ctx
->ei
;
1048 ei
->error
= JS_E_EXCEPTION_THROWN
;
1049 ei
->valid_value
= TRUE
;
1052 if(is_object_instance(value
) && (obj
= to_jsdisp(get_object(value
)))) {
1058 /* FIXME: We should check if object is an error instance */
1060 hres
= jsdisp_propget_name(obj
, L
"number", &v
);
1061 if(SUCCEEDED(hres
)) {
1062 hres
= to_uint32(ctx
, v
, &number
);
1064 ei
->error
= FAILED(number
) ? number
: E_FAIL
;
1068 hres
= jsdisp_propget_name(obj
, L
"description", &v
);
1069 if(SUCCEEDED(hres
)) {
1070 hres
= to_string(ctx
, v
, &str
);
1078 /* ECMA-262 3rd Edition 12.13 */
1079 static HRESULT
interp_throw(script_ctx_t
*ctx
)
1083 set_error_value(ctx
, stack_pop(ctx
));
1084 return DISP_E_EXCEPTION
;
1087 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
1089 const HRESULT arg
= get_op_uint(ctx
, 0);
1091 TRACE("%08lx\n", arg
);
1096 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
1098 const HRESULT hres
= get_op_uint(ctx
, 0);
1099 jsstr_t
*str
= get_op_str(ctx
, 1);
1102 TRACE("%08lx %s\n", hres
, debugstr_jsstr(str
));
1104 ptr
= jsstr_flatten(str
);
1105 return ptr
? throw_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
1108 /* ECMA-262 3rd Edition 12.14 */
1109 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
1111 const unsigned catch_off
= get_op_uint(ctx
, 0);
1112 const unsigned finally_off
= get_op_uint(ctx
, 1);
1113 call_frame_t
*frame
= ctx
->call_ctx
;
1114 except_frame_t
*except
;
1118 except
= heap_alloc(sizeof(*except
));
1120 return E_OUTOFMEMORY
;
1122 except
->stack_top
= ctx
->stack_top
;
1123 except
->scope
= frame
->scope
;
1124 except
->catch_off
= catch_off
;
1125 except
->finally_off
= finally_off
;
1126 except
->next
= frame
->except_frame
;
1127 frame
->except_frame
= except
;
1131 /* ECMA-262 3rd Edition 12.14 */
1132 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
1134 const unsigned ret_off
= get_op_uint(ctx
, 0);
1135 call_frame_t
*frame
= ctx
->call_ctx
;
1136 except_frame_t
*except
;
1137 unsigned finally_off
;
1139 TRACE("%u\n", ret_off
);
1141 except
= frame
->except_frame
;
1142 assert(except
!= NULL
);
1144 finally_off
= except
->finally_off
;
1145 frame
->except_frame
= except
->next
;
1151 hres
= stack_push(ctx
, jsval_number(ret_off
));
1154 hres
= stack_push(ctx
, jsval_bool(TRUE
));
1157 frame
->ip
= finally_off
;
1159 frame
->ip
= ret_off
;
1165 /* ECMA-262 3rd Edition 12.14 */
1166 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
1168 call_frame_t
*frame
= ctx
->call_ctx
;
1177 TRACE("passing exception\n");
1179 set_error_value(ctx
, stack_pop(ctx
));
1180 return DISP_E_EXCEPTION
;
1184 assert(is_number(v
));
1185 frame
->ip
= get_number(v
);
1189 static HRESULT
interp_enter_catch(script_ctx_t
*ctx
)
1191 const BSTR ident
= get_op_bstr(ctx
, 0);
1192 jsdisp_t
*scope_obj
;
1196 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
1201 hres
= jsdisp_propput_name(scope_obj
, ident
, v
);
1204 hres
= scope_push(ctx
->call_ctx
->scope
, scope_obj
, to_disp(scope_obj
), &ctx
->call_ctx
->scope
);
1205 jsdisp_release(scope_obj
);
1209 /* ECMA-262 3rd Edition 13 */
1210 static HRESULT
interp_func(script_ctx_t
*ctx
)
1212 unsigned func_idx
= get_op_uint(ctx
, 0);
1213 call_frame_t
*frame
= ctx
->call_ctx
;
1217 TRACE("%d\n", func_idx
);
1219 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
1220 frame
->scope
, &dispex
);
1224 return stack_push(ctx
, jsval_obj(dispex
));
1227 /* ECMA-262 3rd Edition 11.2.1 */
1228 static HRESULT
interp_array(script_ctx_t
*ctx
)
1239 namev
= stack_pop(ctx
);
1241 hres
= stack_pop_object(ctx
, &obj
);
1243 jsval_release(namev
);
1247 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1248 jsval_release(namev
);
1250 IDispatch_Release(obj
);
1254 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
1255 jsstr_release(name_str
);
1256 if(SUCCEEDED(hres
)) {
1257 hres
= disp_propget(ctx
, obj
, id
, &v
);
1258 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1259 v
= jsval_undefined();
1262 IDispatch_Release(obj
);
1266 return stack_push(ctx
, v
);
1269 /* ECMA-262 3rd Edition 11.2.1 */
1270 static HRESULT
interp_member(script_ctx_t
*ctx
)
1272 const BSTR arg
= get_op_bstr(ctx
, 0);
1280 hres
= stack_pop_object(ctx
, &obj
);
1284 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
1285 if(SUCCEEDED(hres
)) {
1286 hres
= disp_propget(ctx
, obj
, id
, &v
);
1287 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1288 v
= jsval_undefined();
1291 IDispatch_Release(obj
);
1295 return stack_push(ctx
, v
);
1298 /* ECMA-262 3rd Edition 11.2.1 */
1299 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
1301 const unsigned arg
= get_op_uint(ctx
, 0);
1302 jsval_t objv
, namev
;
1312 namev
= stack_pop(ctx
);
1313 objv
= stack_pop(ctx
);
1315 hres
= to_object(ctx
, objv
, &obj
);
1316 jsval_release(objv
);
1317 if(SUCCEEDED(hres
)) {
1318 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1320 IDispatch_Release(obj
);
1322 jsval_release(namev
);
1326 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
1327 jsstr_release(name_str
);
1328 if(SUCCEEDED(hres
)) {
1329 ref
.type
= EXPRVAL_IDREF
;
1330 ref
.u
.idref
.disp
= obj
;
1331 ref
.u
.idref
.id
= id
;
1333 IDispatch_Release(obj
);
1334 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1335 exprval_set_exception(&ref
, JS_E_INVALID_PROPERTY
);
1338 ERR("failed %08lx\n", hres
);
1343 return stack_push_exprval(ctx
, &ref
);
1346 /* ECMA-262 3rd Edition 11.2.1 */
1347 static HRESULT
interp_refval(script_ctx_t
*ctx
)
1355 if(!stack_topn_exprval(ctx
, 0, &ref
))
1356 return JS_E_ILLEGAL_ASSIGN
;
1358 hres
= exprval_propget(ctx
, &ref
, &v
);
1362 return stack_push(ctx
, v
);
1365 /* ECMA-262 3rd Edition 11.2.2 */
1366 static HRESULT
interp_new(script_ctx_t
*ctx
)
1368 const unsigned argc
= get_op_uint(ctx
, 0);
1371 TRACE("%d\n", argc
);
1373 constr
= stack_topn(ctx
, argc
);
1375 /* NOTE: Should use to_object here */
1378 return is_null_disp(constr
) ? JS_E_INVALID_PROPERTY
: JS_E_OBJECT_EXPECTED
;
1379 if(!is_object_instance(constr
))
1380 return JS_E_INVALID_ACTION
;
1383 return disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1384 argc
, stack_args(ctx
, argc
), &ctx
->acc
);
1387 /* ECMA-262 3rd Edition 11.2.3 */
1388 static HRESULT
interp_call(script_ctx_t
*ctx
)
1390 const unsigned argn
= get_op_uint(ctx
, 0);
1391 const int do_ret
= get_op_int(ctx
, 1);
1394 TRACE("%d %d\n", argn
, do_ret
);
1396 obj
= stack_topn(ctx
, argn
);
1397 if(!is_object_instance(obj
))
1398 return JS_E_INVALID_PROPERTY
;
1401 return disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1402 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1405 /* ECMA-262 3rd Edition 11.2.3 */
1406 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
1408 const unsigned argn
= get_op_uint(ctx
, 0);
1409 const int do_ret
= get_op_int(ctx
, 1);
1412 TRACE("%d %d\n", argn
, do_ret
);
1414 if(!stack_topn_exprval(ctx
, argn
, &ref
))
1418 return exprval_call(ctx
, &ref
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1419 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1422 /* ECMA-262 3rd Edition 11.1.1 */
1423 static HRESULT
interp_this(script_ctx_t
*ctx
)
1425 IDispatch
*this_obj
= ctx
->call_ctx
->this_obj
;
1430 named_item_t
*item
= ctx
->call_ctx
->bytecode
->named_item
;
1433 this_obj
= (item
->flags
& SCRIPTITEM_CODEONLY
) ? to_disp(item
->script_obj
) : item
->disp
;
1435 this_obj
= lookup_global_host(ctx
);
1438 IDispatch_AddRef(this_obj
);
1439 return stack_push(ctx
, jsval_disp(this_obj
));
1442 static HRESULT
interp_identifier_ref(script_ctx_t
*ctx
, BSTR identifier
, unsigned flags
)
1447 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1451 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1452 jsdisp_t
*script_obj
= ctx
->global
;
1455 if(ctx
->call_ctx
->bytecode
->named_item
)
1456 script_obj
= ctx
->call_ctx
->bytecode
->named_item
->script_obj
;
1458 hres
= jsdisp_get_id(script_obj
, identifier
, fdexNameEnsure
, &id
);
1462 exprval_set_disp_ref(&exprval
, to_disp(script_obj
), id
);
1465 if(exprval
.type
== EXPRVAL_INVALID
||
1466 (exprval
.type
== EXPRVAL_JSVAL
&& ctx
->version
< SCRIPTLANGUAGEVERSION_ES5
)) {
1467 WARN("invalid ref\n");
1468 exprval_release(&exprval
);
1469 exprval_set_exception(&exprval
, JS_E_OBJECT_EXPECTED
);
1472 return stack_push_exprval(ctx
, &exprval
);
1475 static HRESULT
identifier_value(script_ctx_t
*ctx
, BSTR identifier
)
1481 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1485 if(exprval
.type
== EXPRVAL_INVALID
)
1486 return throw_error(ctx
, exprval
.u
.hres
, identifier
);
1488 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1492 return stack_push(ctx
, v
);
1495 static HRESULT
interp_local_ref(script_ctx_t
*ctx
)
1497 const int arg
= get_op_int(ctx
, 0);
1498 const unsigned flags
= get_op_uint(ctx
, 1);
1499 call_frame_t
*frame
= ctx
->call_ctx
;
1502 TRACE("%s\n", debugstr_w(local_name(frame
, arg
)));
1504 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1505 return interp_identifier_ref(ctx
, local_name(frame
, arg
), flags
);
1507 ref
.type
= EXPRVAL_STACK_REF
;
1508 ref
.u
.off
= local_off(frame
, arg
);
1509 return stack_push_exprval(ctx
, &ref
);
1512 static HRESULT
interp_local(script_ctx_t
*ctx
)
1514 const int arg
= get_op_int(ctx
, 0);
1515 call_frame_t
*frame
= ctx
->call_ctx
;
1519 if(!frame
->base_scope
|| !frame
->base_scope
->frame
) {
1520 TRACE("%s\n", debugstr_w(local_name(frame
, arg
)));
1521 return identifier_value(ctx
, local_name(frame
, arg
));
1524 hres
= jsval_copy(ctx
->stack
[local_off(frame
, arg
)], ©
);
1528 TRACE("%s: %s\n", debugstr_w(local_name(frame
, arg
)), debugstr_jsval(copy
));
1529 return stack_push(ctx
, copy
);
1532 /* ECMA-262 3rd Edition 10.1.4 */
1533 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1535 const BSTR arg
= get_op_bstr(ctx
, 0);
1537 TRACE("%s\n", debugstr_w(arg
));
1539 return identifier_value(ctx
, arg
);
1542 /* ECMA-262 3rd Edition 10.1.4 */
1543 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1545 const BSTR arg
= get_op_bstr(ctx
, 0);
1546 const unsigned flags
= get_op_uint(ctx
, 1);
1548 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1550 return interp_identifier_ref(ctx
, arg
, flags
);
1553 /* ECMA-262 3rd Edition 7.8.1 */
1554 static HRESULT
interp_null(script_ctx_t
*ctx
)
1558 return stack_push(ctx
, jsval_null());
1561 /* ECMA-262 3rd Edition 7.8.2 */
1562 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1564 const int arg
= get_op_int(ctx
, 0);
1566 TRACE("%s\n", arg
? "true" : "false");
1568 return stack_push(ctx
, jsval_bool(arg
));
1571 /* ECMA-262 3rd Edition 7.8.3 */
1572 static HRESULT
interp_int(script_ctx_t
*ctx
)
1574 const int arg
= get_op_int(ctx
, 0);
1578 return stack_push(ctx
, jsval_number(arg
));
1581 /* ECMA-262 3rd Edition 7.8.3 */
1582 static HRESULT
interp_double(script_ctx_t
*ctx
)
1584 const double arg
= get_op_double(ctx
);
1586 TRACE("%lf\n", arg
);
1588 return stack_push(ctx
, jsval_number(arg
));
1591 /* ECMA-262 3rd Edition 7.8.4 */
1592 static HRESULT
interp_str(script_ctx_t
*ctx
)
1594 jsstr_t
*str
= get_op_str(ctx
, 0);
1596 TRACE("%s\n", debugstr_jsstr(str
));
1598 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1601 /* ECMA-262 3rd Edition 7.8 */
1602 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1604 jsstr_t
*source
= get_op_str(ctx
, 0);
1605 const unsigned flags
= get_op_uint(ctx
, 1);
1609 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1611 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1615 return stack_push(ctx
, jsval_obj(regexp
));
1618 /* ECMA-262 3rd Edition 11.1.4 */
1619 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1621 const unsigned arg
= get_op_uint(ctx
, 0);
1627 hres
= create_array(ctx
, arg
, &array
);
1631 return stack_push(ctx
, jsval_obj(array
));
1634 static HRESULT
interp_carray_set(script_ctx_t
*ctx
)
1636 const unsigned index
= get_op_uint(ctx
, 0);
1637 jsval_t value
, array
;
1640 value
= stack_pop(ctx
);
1642 TRACE("[%u] = %s\n", index
, debugstr_jsval(value
));
1644 array
= stack_top(ctx
);
1645 assert(is_object_instance(array
));
1647 hres
= jsdisp_propput_idx(iface_to_jsdisp(get_object(array
)), index
, value
);
1648 jsval_release(value
);
1652 /* ECMA-262 3rd Edition 11.1.5 */
1653 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1660 hres
= create_object(ctx
, NULL
, &obj
);
1664 return stack_push(ctx
, jsval_obj(obj
));
1667 /* ECMA-262 3rd Edition 11.1.5 */
1668 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1670 jsstr_t
*name_arg
= get_op_str(ctx
, 0);
1671 unsigned type
= get_op_uint(ctx
, 1);
1677 TRACE("%s\n", debugstr_jsstr(name_arg
));
1679 val
= stack_pop(ctx
);
1681 /* FIXME: we should pass it as jsstr_t */
1682 name
= jsstr_flatten(name_arg
);
1684 assert(is_object_instance(stack_top(ctx
)));
1685 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1687 if(type
== PROPERTY_DEFINITION_VALUE
) {
1688 hres
= jsdisp_propput_name(obj
, name
, val
);
1690 property_desc_t desc
= {PROPF_ENUMERABLE
| PROPF_CONFIGURABLE
};
1693 assert(is_object_instance(val
));
1694 func
= iface_to_jsdisp(get_object(val
));
1696 desc
.mask
= desc
.flags
;
1697 if(type
== PROPERTY_DEFINITION_GETTER
) {
1698 desc
.explicit_getter
= TRUE
;
1701 desc
.explicit_setter
= TRUE
;
1705 hres
= jsdisp_define_property(obj
, name
, &desc
);
1706 jsdisp_release(func
);
1713 /* ECMA-262 3rd Edition 11.11 */
1714 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1716 const unsigned arg
= get_op_uint(ctx
, 0);
1722 hres
= to_boolean(stack_top(ctx
), &b
);
1735 /* ECMA-262 3rd Edition 11.11 */
1736 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1738 const unsigned arg
= get_op_uint(ctx
, 0);
1744 hres
= to_boolean(stack_top(ctx
), &b
);
1757 /* ECMA-262 3rd Edition 11.10 */
1758 static HRESULT
interp_or(script_ctx_t
*ctx
)
1765 hres
= stack_pop_int(ctx
, &r
);
1769 hres
= stack_pop_int(ctx
, &l
);
1773 return stack_push(ctx
, jsval_number(l
|r
));
1776 /* ECMA-262 3rd Edition 11.10 */
1777 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1784 hres
= stack_pop_int(ctx
, &r
);
1788 hres
= stack_pop_int(ctx
, &l
);
1792 return stack_push(ctx
, jsval_number(l
^r
));
1795 /* ECMA-262 3rd Edition 11.10 */
1796 static HRESULT
interp_and(script_ctx_t
*ctx
)
1803 hres
= stack_pop_int(ctx
, &r
);
1807 hres
= stack_pop_int(ctx
, &l
);
1811 return stack_push(ctx
, jsval_number(l
&r
));
1814 /* ECMA-262 3rd Edition 11.8.6 */
1815 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1817 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1823 if(!is_object_instance(v
)) {
1825 return JS_E_FUNCTION_EXPECTED
;
1828 obj
= iface_to_jsdisp(get_object(v
));
1829 IDispatch_Release(get_object(v
));
1831 FIXME("non-jsdisp objects not supported\n");
1835 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1836 hres
= jsdisp_propget_name(obj
, L
"prototype", &prot
);
1838 hres
= JS_E_FUNCTION_EXPECTED
;
1840 jsdisp_release(obj
);
1847 hres
= JS_E_OBJECT_EXPECTED
;
1848 else if(is_object_instance(prot
)) {
1849 if(is_object_instance(v
))
1850 tmp
= iface_to_jsdisp(get_object(v
));
1851 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1852 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1858 jsdisp_release(tmp
);
1860 FIXME("prototype is not an object\n");
1864 jsval_release(prot
);
1869 return stack_push(ctx
, jsval_bool(ret
));
1872 /* ECMA-262 3rd Edition 11.8.7 */
1873 static HRESULT
interp_in(script_ctx_t
*ctx
)
1884 obj
= stack_pop(ctx
);
1885 if(!is_object_instance(obj
)) {
1887 return JS_E_OBJECT_EXPECTED
;
1891 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1894 IDispatch_Release(get_object(obj
));
1898 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1899 IDispatch_Release(get_object(obj
));
1900 jsstr_release(jsstr
);
1903 else if(hres
== DISP_E_UNKNOWNNAME
)
1908 return stack_push(ctx
, jsval_bool(ret
));
1911 /* ECMA-262 3rd Edition 11.6.1 */
1912 static HRESULT
interp_add(script_ctx_t
*ctx
)
1914 jsval_t l
, r
, lval
, rval
, ret
;
1917 rval
= stack_pop(ctx
);
1918 lval
= stack_pop(ctx
);
1920 TRACE("%s + %s\n", debugstr_jsval(lval
), debugstr_jsval(rval
));
1922 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1923 if(SUCCEEDED(hres
)) {
1924 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1928 jsval_release(lval
);
1929 jsval_release(rval
);
1933 if(is_string(l
) || is_string(r
)) {
1934 jsstr_t
*lstr
, *rstr
= NULL
;
1936 hres
= to_string(ctx
, l
, &lstr
);
1938 hres
= to_string(ctx
, r
, &rstr
);
1940 if(SUCCEEDED(hres
)) {
1943 ret_str
= jsstr_concat(lstr
, rstr
);
1945 ret
= jsval_string(ret_str
);
1947 hres
= E_OUTOFMEMORY
;
1950 jsstr_release(lstr
);
1952 jsstr_release(rstr
);
1956 hres
= to_number(ctx
, l
, &nl
);
1957 if(SUCCEEDED(hres
)) {
1958 hres
= to_number(ctx
, r
, &nr
);
1960 ret
= jsval_number(nl
+nr
);
1969 return stack_push(ctx
, ret
);
1972 /* ECMA-262 3rd Edition 11.6.2 */
1973 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1980 hres
= stack_pop_number(ctx
, &r
);
1984 hres
= stack_pop_number(ctx
, &l
);
1988 return stack_push(ctx
, jsval_number(l
-r
));
1991 /* ECMA-262 3rd Edition 11.5.1 */
1992 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1999 hres
= stack_pop_number(ctx
, &r
);
2003 hres
= stack_pop_number(ctx
, &l
);
2007 return stack_push(ctx
, jsval_number(l
*r
));
2010 /* ECMA-262 3rd Edition 11.5.2 */
2011 static HRESULT
interp_div(script_ctx_t
*ctx
)
2018 hres
= stack_pop_number(ctx
, &r
);
2022 hres
= stack_pop_number(ctx
, &l
);
2026 return stack_push(ctx
, jsval_number(l
/r
));
2029 /* ECMA-262 3rd Edition 11.5.3 */
2030 static HRESULT
interp_mod(script_ctx_t
*ctx
)
2037 hres
= stack_pop_number(ctx
, &r
);
2041 hres
= stack_pop_number(ctx
, &l
);
2045 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
2048 /* ECMA-262 3rd Edition 11.4.2 */
2049 static HRESULT
interp_delete(script_ctx_t
*ctx
)
2051 jsval_t objv
, namev
;
2059 namev
= stack_pop(ctx
);
2060 objv
= stack_pop(ctx
);
2062 hres
= to_object(ctx
, objv
, &obj
);
2063 jsval_release(objv
);
2065 jsval_release(namev
);
2069 hres
= to_string(ctx
, namev
, &name
);
2070 jsval_release(namev
);
2072 IDispatch_Release(obj
);
2076 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
2077 IDispatch_Release(obj
);
2078 jsstr_release(name
);
2082 return stack_push(ctx
, jsval_bool(ret
));
2085 /* ECMA-262 3rd Edition 11.4.2 */
2086 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
2088 const BSTR arg
= get_op_bstr(ctx
, 0);
2093 TRACE("%s\n", debugstr_w(arg
));
2095 hres
= identifier_eval(ctx
, arg
, &exprval
);
2099 switch(exprval
.type
) {
2100 case EXPRVAL_STACK_REF
:
2104 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
2105 IDispatch_Release(exprval
.u
.idref
.disp
);
2109 case EXPRVAL_INVALID
:
2113 FIXME("Unsupported exprval\n");
2114 exprval_release(&exprval
);
2119 return stack_push(ctx
, jsval_bool(ret
));
2122 /* ECMA-262 3rd Edition 11.4.2 */
2123 static HRESULT
interp_void(script_ctx_t
*ctx
)
2128 return stack_push(ctx
, jsval_undefined());
2131 /* ECMA-262 3rd Edition 11.4.3 */
2132 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
2134 switch(jsval_type(v
)) {
2136 *ret
= L
"undefined";
2144 if((dispex
= iface_to_jsdisp(get_object(v
)))) {
2145 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? L
"function" : L
"object";
2146 jsdisp_release(dispex
);
2162 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
2169 /* ECMA-262 3rd Edition 11.4.3 */
2170 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
2179 if(!stack_pop_exprval(ctx
, &ref
))
2180 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2182 hres
= exprval_propget(ctx
, &ref
, &v
);
2183 exprval_release(&ref
);
2185 return stack_push_string(ctx
, L
"unknown");
2187 hres
= typeof_string(v
, &ret
);
2192 return stack_push_string(ctx
, ret
);
2195 /* ECMA-262 3rd Edition 11.4.3 */
2196 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
2198 const BSTR arg
= get_op_bstr(ctx
, 0);
2204 TRACE("%s\n", debugstr_w(arg
));
2206 hres
= identifier_eval(ctx
, arg
, &exprval
);
2210 if(exprval
.type
== EXPRVAL_INVALID
)
2211 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2213 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2217 hres
= typeof_string(v
, &ret
);
2222 return stack_push_string(ctx
, ret
);
2225 /* ECMA-262 3rd Edition 11.4.3 */
2226 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
2235 hres
= typeof_string(v
, &ret
);
2240 return stack_push_string(ctx
, ret
);
2243 /* ECMA-262 3rd Edition 11.4.7 */
2244 static HRESULT
interp_minus(script_ctx_t
*ctx
)
2251 hres
= stack_pop_number(ctx
, &n
);
2255 return stack_push(ctx
, jsval_number(-n
));
2258 /* ECMA-262 3rd Edition 11.4.6 */
2259 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
2268 hres
= to_number(ctx
, v
, &n
);
2273 return stack_push(ctx
, jsval_number(n
));
2276 /* ECMA-262 3rd Edition 11.3.1 */
2277 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
2279 const int arg
= get_op_int(ctx
, 0);
2286 if(!stack_pop_exprval(ctx
, &ref
))
2287 return JS_E_OBJECT_EXPECTED
;
2289 hres
= exprval_propget(ctx
, &ref
, &v
);
2290 if(SUCCEEDED(hres
)) {
2293 hres
= to_number(ctx
, v
, &n
);
2295 hres
= exprval_propput(ctx
, &ref
, jsval_number(n
+(double)arg
));
2299 exprval_release(&ref
);
2303 return stack_push(ctx
, v
);
2306 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2307 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
2309 const int arg
= get_op_int(ctx
, 0);
2317 if(!stack_pop_exprval(ctx
, &ref
))
2318 return JS_E_OBJECT_EXPECTED
;
2320 hres
= exprval_propget(ctx
, &ref
, &v
);
2321 if(SUCCEEDED(hres
)) {
2324 hres
= to_number(ctx
, v
, &n
);
2326 if(SUCCEEDED(hres
)) {
2327 ret
= n
+(double)arg
;
2328 hres
= exprval_propput(ctx
, &ref
, jsval_number(ret
));
2331 exprval_release(&ref
);
2335 return stack_push(ctx
, jsval_number(ret
));
2338 /* ECMA-262 3rd Edition 11.9.3 */
2339 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
2341 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
2342 return jsval_strict_equal(lval
, rval
, ret
);
2344 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
2349 if(is_string(lval
) && is_number(rval
)) {
2353 hres
= to_number(ctx
, lval
, &n
);
2357 /* FIXME: optimize */
2358 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
2361 if(is_string(rval
) && is_number(lval
)) {
2365 hres
= to_number(ctx
, rval
, &n
);
2369 /* FIXME: optimize */
2370 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
2374 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
2377 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
2380 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
2384 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
2388 hres
= equal_values(ctx
, lval
, prim
, ret
);
2389 jsval_release(prim
);
2394 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
2398 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
2402 hres
= equal_values(ctx
, prim
, rval
, ret
);
2403 jsval_release(prim
);
2412 /* ECMA-262 3rd Edition 11.9.1 */
2413 static HRESULT
interp_eq(script_ctx_t
*ctx
)
2422 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2424 hres
= equal_values(ctx
, l
, r
, &b
);
2430 return stack_push(ctx
, jsval_bool(b
));
2433 /* ECMA-262 3rd Edition 11.9.2 */
2434 static HRESULT
interp_neq(script_ctx_t
*ctx
)
2443 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2445 hres
= equal_values(ctx
, l
, r
, &b
);
2451 return stack_push(ctx
, jsval_bool(!b
));
2454 /* ECMA-262 3rd Edition 11.9.4 */
2455 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
2464 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2466 hres
= jsval_strict_equal(r
, l
, &b
);
2472 return stack_push(ctx
, jsval_bool(b
));
2475 /* ECMA-262 3rd Edition 11.9.5 */
2476 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2487 hres
= jsval_strict_equal(r
, l
, &b
);
2493 return stack_push(ctx
, jsval_bool(!b
));
2496 /* ECMA-262 3rd Edition 11.8.5 */
2497 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2503 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2507 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2513 if(is_string(l
) && is_string(r
)) {
2514 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2515 jsstr_release(get_string(l
));
2516 jsstr_release(get_string(r
));
2520 hres
= to_number(ctx
, l
, &ln
);
2523 hres
= to_number(ctx
, r
, &rn
);
2528 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2532 /* ECMA-262 3rd Edition 11.8.1 */
2533 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2542 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2544 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2550 return stack_push(ctx
, jsval_bool(b
));
2553 /* ECMA-262 3rd Edition 11.8.1 */
2554 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2563 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2565 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2571 return stack_push(ctx
, jsval_bool(b
));
2574 /* ECMA-262 3rd Edition 11.8.2 */
2575 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2584 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2586 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2592 return stack_push(ctx
, jsval_bool(b
));
2595 /* ECMA-262 3rd Edition 11.8.4 */
2596 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2605 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2607 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2613 return stack_push(ctx
, jsval_bool(b
));
2616 /* ECMA-262 3rd Edition 11.4.8 */
2617 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2626 hres
= to_int32(ctx
, v
, &i
);
2631 return stack_push(ctx
, jsval_number(~i
));
2634 /* ECMA-262 3rd Edition 11.4.9 */
2635 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2644 hres
= to_boolean(v
, &b
);
2649 return stack_push(ctx
, jsval_bool(!b
));
2652 /* ECMA-262 3rd Edition 11.7.1 */
2653 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2659 hres
= stack_pop_uint(ctx
, &r
);
2663 hres
= stack_pop_int(ctx
, &l
);
2667 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2670 /* ECMA-262 3rd Edition 11.7.2 */
2671 static HRESULT
interp_rshift(script_ctx_t
*ctx
)
2677 hres
= stack_pop_uint(ctx
, &r
);
2681 hres
= stack_pop_int(ctx
, &l
);
2685 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2688 /* ECMA-262 3rd Edition 11.7.3 */
2689 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2694 hres
= stack_pop_uint(ctx
, &r
);
2698 hres
= stack_pop_uint(ctx
, &l
);
2702 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2705 /* ECMA-262 3rd Edition 9.8 */
2706 static HRESULT
interp_to_string(script_ctx_t
*ctx
)
2713 TRACE("%s\n", debugstr_jsval(v
));
2714 hres
= to_string(ctx
, v
, &str
);
2717 WARN("failed %08lx\n", hres
);
2721 return stack_push(ctx
, jsval_string(str
));
2724 /* ECMA-262 3rd Edition 11.13.1 */
2725 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2735 if(!stack_pop_exprval(ctx
, &ref
)) {
2737 return JS_E_ILLEGAL_ASSIGN
;
2740 hres
= exprval_propput(ctx
, &ref
, v
);
2741 exprval_release(&ref
);
2747 return stack_push(ctx
, v
);
2750 /* ECMA-262 3rd Edition 11.13.1 */
2751 static HRESULT
interp_set_member(script_ctx_t
*ctx
)
2753 jsval_t objv
, namev
, value
;
2758 value
= stack_pop(ctx
);
2759 namev
= stack_pop(ctx
);
2760 assert(is_string(namev
));
2761 objv
= stack_pop(ctx
);
2763 TRACE("%s.%s = %s\n", debugstr_jsval(objv
), debugstr_jsval(namev
), debugstr_jsval(value
));
2765 hres
= to_object(ctx
, objv
, &obj
);
2766 jsval_release(objv
);
2767 if(SUCCEEDED(hres
) && !(name
= jsstr_flatten(get_string(namev
)))) {
2768 IDispatch_Release(obj
);
2769 hres
= E_OUTOFMEMORY
;
2771 if(SUCCEEDED(hres
)) {
2772 hres
= disp_propput_name(ctx
, obj
, name
, value
);
2773 IDispatch_Release(obj
);
2774 jsstr_release(get_string(namev
));
2777 WARN("failed %08lx\n", hres
);
2778 jsval_release(value
);
2782 return stack_push(ctx
, value
);
2785 /* JScript extension */
2786 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2788 const unsigned argc
= get_op_uint(ctx
, 0);
2793 TRACE("%u\n", argc
);
2795 if(!stack_topn_exprval(ctx
, argc
+1, &ref
))
2796 return JS_E_ILLEGAL_ASSIGN
;
2798 hres
= exprval_call(ctx
, &ref
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2803 stack_popn(ctx
, argc
+2);
2804 return stack_push(ctx
, v
);
2807 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2811 return stack_push(ctx
, jsval_undefined());
2814 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2816 const unsigned arg
= get_op_uint(ctx
, 0);
2824 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2826 const unsigned arg
= get_op_uint(ctx
, 0);
2834 hres
= to_boolean(v
, &b
);
2846 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2848 const unsigned arg
= get_op_uint(ctx
, 0);
2852 stack_popn(ctx
, arg
);
2856 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2858 const unsigned clear_ret
= get_op_uint(ctx
, 0);
2859 call_frame_t
*frame
= ctx
->call_ctx
;
2864 jsval_release(steal_ret(frame
));
2866 if((frame
->flags
& EXEC_CONSTRUCTOR
) && !is_object_instance(frame
->ret
)) {
2867 jsval_release(frame
->ret
);
2868 IDispatch_AddRef(frame
->this_obj
);
2869 frame
->ret
= jsval_disp(frame
->this_obj
);
2876 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2878 call_frame_t
*frame
= ctx
->call_ctx
;
2882 jsval_release(frame
->ret
);
2883 frame
->ret
= stack_pop(ctx
);
2887 static HRESULT
interp_push_acc(script_ctx_t
*ctx
)
2893 hres
= stack_push(ctx
, ctx
->acc
);
2895 ctx
->acc
= jsval_undefined();
2899 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2901 static const op_func_t op_funcs
[] = {
2902 #define X(x,a,b,c) interp_##x,
2907 static const unsigned op_move
[] = {
2908 #define X(a,x,b,c) x,
2913 static void pop_call_frame(script_ctx_t
*ctx
)
2915 call_frame_t
*frame
= ctx
->call_ctx
;
2917 frame
->stack_base
-= frame
->pop_locals
+ frame
->pop_variables
;
2919 assert(frame
->scope
== frame
->base_scope
);
2921 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2922 if(frame
->scope
&& frame
->scope
->ref
> 1) {
2923 HRESULT hres
= detach_variable_object(ctx
, frame
, TRUE
);
2925 ERR("Failed to detach variable object: %08lx\n", hres
);
2928 if(frame
->arguments_obj
)
2929 detach_arguments_object(frame
->arguments_obj
);
2931 scope_release(frame
->scope
);
2933 if(frame
->pop_variables
)
2934 stack_popn(ctx
, frame
->pop_variables
);
2935 stack_popn(ctx
, frame
->pop_locals
);
2937 ctx
->call_ctx
= frame
->prev_frame
;
2939 if(frame
->function_instance
)
2940 jsdisp_release(frame
->function_instance
);
2941 if(frame
->variable_obj
)
2942 jsdisp_release(frame
->variable_obj
);
2944 IDispatch_Release(frame
->this_obj
);
2945 jsval_release(frame
->ret
);
2946 release_bytecode(frame
->bytecode
);
2950 static void print_backtrace(script_ctx_t
*ctx
)
2952 unsigned depth
= 0, i
, line
, char_pos
;
2953 call_frame_t
*frame
;
2955 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
2956 WARN("%u\t", depth
);
2960 WARN("%p->", frame
->this_obj
);
2961 WARN("%s(", frame
->function
->name
? debugstr_w(frame
->function
->name
) : "[unnamed]");
2962 if(frame
->base_scope
&& frame
->base_scope
->frame
) {
2963 for(i
=0; i
< frame
->argc
; i
++) {
2964 if(i
< frame
->function
->param_cnt
)
2965 WARN("%s%s=%s", i
? ", " : "", debugstr_w(frame
->function
->params
[i
]),
2966 debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2968 WARN("%s%s", i
? ", " : "", debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2971 WARN("[detached frame]");
2973 line
= get_location_line(frame
->bytecode
, frame
->bytecode
->instrs
[frame
->ip
].loc
, &char_pos
);
2974 WARN(") context %s line %u char %u\n", wine_dbgstr_longlong(frame
->bytecode
->source_context
), line
, char_pos
);
2976 if(!(frame
->flags
& EXEC_RETURN_TO_INTERP
)) {
2977 WARN("%u\t[native code]\n", depth
);
2983 static HRESULT
unwind_exception(script_ctx_t
*ctx
, HRESULT exception_hres
)
2985 except_frame_t
*except_frame
;
2986 jsexcept_t
*ei
= ctx
->ei
;
2987 call_frame_t
*frame
;
2992 if(WARN_ON(jscript
)) {
2993 jsdisp_t
*error_obj
;
2996 WARN("Exception %08lx %s", exception_hres
, debugstr_jsval(ei
->valid_value
? ei
->value
: jsval_undefined()));
2997 if(ei
->valid_value
&& jsval_type(ei
->value
) == JSV_OBJECT
) {
2998 error_obj
= to_jsdisp(get_object(ei
->value
));
3000 hres
= jsdisp_propget_name(error_obj
, L
"message", &msg
);
3001 if(SUCCEEDED(hres
)) {
3002 WARN(" (message %s)", debugstr_jsval(msg
));
3009 print_backtrace(ctx
);
3012 frame
= ctx
->call_ctx
;
3013 if(exception_hres
!= DISP_E_EXCEPTION
)
3014 throw_error(ctx
, exception_hres
, NULL
);
3015 set_error_location(ei
, frame
->bytecode
, frame
->bytecode
->instrs
[frame
->ip
].loc
, IDS_RUNTIME_ERROR
, NULL
);
3017 while(!frame
->except_frame
) {
3020 while(frame
->scope
!= frame
->base_scope
)
3021 scope_pop(&frame
->scope
);
3023 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
3025 flags
= frame
->flags
;
3026 pop_call_frame(ctx
);
3027 if(!(flags
& EXEC_RETURN_TO_INTERP
))
3028 return DISP_E_EXCEPTION
;
3029 frame
= ctx
->call_ctx
;
3032 except_frame
= frame
->except_frame
;
3033 catch_off
= except_frame
->catch_off
;
3035 assert(except_frame
->stack_top
<= ctx
->stack_top
);
3036 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
3038 while(except_frame
->scope
!= frame
->scope
)
3039 scope_pop(&frame
->scope
);
3041 frame
->ip
= catch_off
? catch_off
: except_frame
->finally_off
;
3042 assert(!catch_off
|| frame
->bytecode
->instrs
[frame
->ip
].op
== OP_enter_catch
);
3044 if(ei
->valid_value
) {
3045 except_val
= ctx
->ei
->value
;
3046 ei
->valid_value
= FALSE
;
3049 if(!(err
= create_builtin_error(ctx
)))
3050 return E_OUTOFMEMORY
;
3051 except_val
= jsval_obj(err
);
3054 /* keep current except_frame if we're entering catch block with finally block associated */
3055 if(catch_off
&& except_frame
->finally_off
) {
3056 except_frame
->catch_off
= 0;
3058 frame
->except_frame
= except_frame
->next
;
3059 heap_free(except_frame
);
3062 hres
= stack_push(ctx
, except_val
);
3067 hres
= stack_push(ctx
, jsval_bool(FALSE
));
3071 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, jsval_t
*r
)
3073 call_frame_t
*frame
;
3075 HRESULT hres
= S_OK
;
3080 frame
= ctx
->call_ctx
;
3081 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
3082 hres
= op_funcs
[op
](ctx
);
3084 hres
= unwind_exception(ctx
, hres
);
3087 }else if(frame
->ip
== -1) {
3088 const DWORD return_to_interp
= frame
->flags
& EXEC_RETURN_TO_INTERP
;
3090 assert(ctx
->stack_top
== frame
->stack_base
);
3091 assert(frame
->scope
== frame
->base_scope
);
3093 if(return_to_interp
) {
3094 jsval_release(ctx
->acc
);
3095 ctx
->acc
= steal_ret(frame
);
3097 *r
= steal_ret(frame
);
3099 pop_call_frame(ctx
);
3100 if(!return_to_interp
)
3103 frame
->ip
+= op_move
[op
];
3110 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
3112 IBindEventHandler
*target
;
3118 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
3122 hres
= exprval_to_value(ctx
, &exprval
, &v
);
3126 if(!is_object_instance(v
)) {
3127 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
3131 disp
= get_object(v
);
3132 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
3133 if(SUCCEEDED(hres
)) {
3134 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
3135 IBindEventHandler_Release(target
);
3137 WARN("BindEvent failed: %08lx\n", hres
);
3139 FIXME("No IBindEventHandler, not yet supported binding\n");
3142 IDispatch_Release(disp
);
3146 static HRESULT
setup_scope(script_ctx_t
*ctx
, call_frame_t
*frame
, scope_chain_t
*scope_chain
, jsdisp_t
*variable_object
, unsigned argc
, jsval_t
*argv
)
3148 const unsigned orig_stack
= ctx
->stack_top
;
3149 scope_chain_t
*scope
;
3154 /* If arguments are already on the stack, we may use them. */
3155 if(argv
+ argc
== ctx
->stack
+ ctx
->stack_top
) {
3156 frame
->arguments_off
= argv
- ctx
->stack
;
3159 frame
->arguments_off
= ctx
->stack_top
;
3160 for(i
= 0; i
< argc
; i
++) {
3161 hres
= jsval_copy(argv
[i
], &v
);
3163 hres
= stack_push(ctx
, v
);
3171 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
3172 for(; i
< frame
->function
->param_cnt
; i
++) {
3173 hres
= stack_push(ctx
, jsval_undefined());
3175 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3180 frame
->pop_locals
= ctx
->stack_top
- orig_stack
;
3182 frame
->variables_off
= ctx
->stack_top
;
3184 for(i
= 0; i
< frame
->function
->var_cnt
; i
++) {
3185 hres
= stack_push(ctx
, jsval_undefined());
3187 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3192 frame
->pop_variables
= i
;
3194 hres
= scope_push(scope_chain
, variable_object
, to_disp(variable_object
), &scope
);
3196 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3200 for(i
= 0; i
< frame
->function
->func_cnt
; i
++) {
3201 if(frame
->function
->funcs
[i
].local_ref
!= INVALID_LOCAL_REF
3202 && !frame
->function
->funcs
[i
].scope_index
)
3207 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+i
, scope
, &func_obj
);
3209 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3210 scope_release(scope
);
3214 off
= local_off(frame
, frame
->function
->funcs
[i
].local_ref
);
3215 jsval_release(ctx
->stack
[off
]);
3216 ctx
->stack
[off
] = jsval_obj(func_obj
);
3220 scope
->frame
= frame
;
3221 frame
->base_scope
= frame
->scope
= scope
;
3225 HRESULT
exec_source(script_ctx_t
*ctx
, DWORD flags
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
3226 IDispatch
*this_obj
, jsdisp_t
*function_instance
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
3228 jsdisp_t
*variable_obj
;
3229 call_frame_t
*frame
;
3234 ctx
->stack
= heap_alloc(stack_size
* sizeof(*ctx
->stack
));
3236 return E_OUTOFMEMORY
;
3239 if(bytecode
->named_item
) {
3240 if(!bytecode
->named_item
->script_obj
) {
3241 hres
= create_named_item_script_obj(ctx
, bytecode
->named_item
);
3242 if(FAILED(hres
)) return hres
;
3246 if(!ctx
->ei
->enter_notified
) {
3247 ctx
->ei
->enter_notified
= TRUE
;
3248 IActiveScriptSite_OnEnterScript(ctx
->site
);
3251 for(i
= 0; i
< function
->func_cnt
; i
++) {
3254 if(!function
->funcs
[i
].event_target
)
3257 if (function
->funcs
[i
].scope_index
)
3259 /* TODO: Add tests and handle in interp_push_scope(). */
3260 FIXME("Event target with scope index are not properly handled.\n");
3263 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+i
, scope
, &func_obj
);
3267 hres
= bind_event_target(ctx
, function
->funcs
+i
, func_obj
);
3268 jsdisp_release(func_obj
);
3273 if((flags
& EXEC_EVAL
) && ctx
->call_ctx
) {
3274 variable_obj
= jsdisp_addref(ctx
->call_ctx
->variable_obj
);
3275 }else if(!(flags
& (EXEC_GLOBAL
| EXEC_EVAL
))) {
3276 hres
= create_dispex(ctx
, NULL
, NULL
, &variable_obj
);
3277 if(FAILED(hres
)) return hres
;
3278 }else if(bytecode
->named_item
) {
3279 variable_obj
= jsdisp_addref(bytecode
->named_item
->script_obj
);
3281 variable_obj
= jsdisp_addref(ctx
->global
);
3284 if(flags
& (EXEC_GLOBAL
| EXEC_EVAL
)) {
3285 named_item_t
*item
= bytecode
->named_item
;
3288 for(i
=0; i
< function
->var_cnt
; i
++) {
3289 TRACE("[%d] %s %d\n", i
, debugstr_w(function
->variables
[i
].name
), function
->variables
[i
].func_id
);
3290 if(function
->variables
[i
].func_id
!= -1) {
3293 if (function
->funcs
[function
->variables
[i
].func_id
].scope_index
&& flags
& EXEC_EVAL
)
3295 /* TODO: Add tests and handle in interp_push_scope(). */
3296 FIXME("Functions with scope index inside eval() are not properly handled.\n");
3299 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+function
->variables
[i
].func_id
, scope
, &func_obj
);
3303 hres
= jsdisp_propput_name(variable_obj
, function
->variables
[i
].name
, jsval_obj(func_obj
));
3304 jsdisp_release(func_obj
);
3308 if(item
&& !(item
->flags
& SCRIPTITEM_CODEONLY
)
3309 && SUCCEEDED(disp_get_id(ctx
, item
->disp
, function
->variables
[i
].name
, function
->variables
[i
].name
, 0, &id
)))
3312 if(!item
&& (flags
& EXEC_GLOBAL
) && lookup_global_members(ctx
, function
->variables
[i
].name
, NULL
))
3315 hres
= jsdisp_get_id(variable_obj
, function
->variables
[i
].name
, fdexNameEnsure
, &id
);
3322 jsdisp_t
*jsthis
= to_jsdisp(this_obj
);
3324 if(jsthis
&& jsthis
->builtin_info
->class == JSCLASS_GLOBAL
)
3328 if(ctx
->call_ctx
&& (flags
& EXEC_EVAL
)) {
3329 hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
3334 frame
= heap_alloc_zero(sizeof(*frame
));
3336 hres
= E_OUTOFMEMORY
;
3340 frame
->function
= function
;
3341 frame
->ret
= jsval_undefined();
3343 frame
->bytecode
= bytecode_addref(bytecode
);
3345 if(!(flags
& (EXEC_GLOBAL
|EXEC_EVAL
))) {
3346 hres
= setup_scope(ctx
, frame
, scope
, variable_obj
, argc
, argv
);
3348 release_bytecode(frame
->bytecode
);
3353 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
3356 frame
->ip
= function
->instr_off
;
3357 frame
->stack_base
= ctx
->stack_top
;
3359 frame
->this_obj
= this_obj
;
3360 IDispatch_AddRef(frame
->this_obj
);
3363 if(function_instance
)
3364 frame
->function_instance
= jsdisp_addref(function_instance
);
3366 frame
->flags
= flags
;
3367 frame
->variable_obj
= variable_obj
;
3369 frame
->prev_frame
= ctx
->call_ctx
;
3370 ctx
->call_ctx
= frame
;
3372 if(flags
& EXEC_RETURN_TO_INTERP
) {
3374 * We're called directly from interpreter, so we may just setup call frame and return.
3375 * Already running interpreter will take care of execution.
3378 *r
= jsval_undefined();
3382 return enter_bytecode(ctx
, r
);
3385 jsdisp_release(variable_obj
);