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 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
31 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
32 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
33 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
34 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
35 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
36 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
38 struct _except_frame_t
{
65 static HRESULT
stack_push(script_ctx_t
*ctx
, jsval_t v
)
67 if(!ctx
->stack_size
) {
68 ctx
->stack
= heap_alloc(16*sizeof(*ctx
->stack
));
72 }else if(ctx
->stack_size
== ctx
->stack_top
) {
75 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*new_stack
));
81 ctx
->stack
= new_stack
;
85 ctx
->stack
[ctx
->stack_top
++] = v
;
89 static inline HRESULT
stack_push_string(script_ctx_t
*ctx
, const WCHAR
*str
)
97 return stack_push(ctx
, jsval_string(v
));
100 static inline jsval_t
stack_top(script_ctx_t
*ctx
)
102 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
103 return ctx
->stack
[ctx
->stack_top
-1];
106 static inline jsval_t
*stack_top_ref(script_ctx_t
*ctx
, unsigned n
)
108 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
);
109 return ctx
->stack
+ctx
->stack_top
-1-n
;
112 static inline jsval_t
stack_topn(script_ctx_t
*ctx
, unsigned n
)
114 return *stack_top_ref(ctx
, n
);
117 static inline jsval_t
*stack_args(script_ctx_t
*ctx
, unsigned n
)
121 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
-1);
122 return ctx
->stack
+ ctx
->stack_top
-n
;
125 static inline jsval_t
stack_pop(script_ctx_t
*ctx
)
127 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
128 return ctx
->stack
[--ctx
->stack_top
];
131 static void stack_popn(script_ctx_t
*ctx
, unsigned n
)
134 jsval_release(stack_pop(ctx
));
137 static HRESULT
stack_pop_number(script_ctx_t
*ctx
, double *r
)
143 hres
= to_number(ctx
, v
, r
);
148 static HRESULT
stack_pop_object(script_ctx_t
*ctx
, IDispatch
**r
)
154 if(is_object_instance(v
)) {
156 return JS_E_OBJECT_REQUIRED
;
161 hres
= to_object(ctx
, v
, r
);
166 static inline HRESULT
stack_pop_int(script_ctx_t
*ctx
, INT
*r
)
168 return to_int32(ctx
, stack_pop(ctx
), r
);
171 static inline HRESULT
stack_pop_uint(script_ctx_t
*ctx
, DWORD
*r
)
173 return to_uint32(ctx
, stack_pop(ctx
), r
);
176 static inline unsigned local_off(call_frame_t
*frame
, int ref
)
179 ? frame
->arguments_off
- ref
-1
180 : frame
->variables_off
+ ref
;
183 static inline BSTR
local_name(call_frame_t
*frame
, int ref
)
185 return ref
< 0 ? frame
->function
->params
[-ref
-1] : frame
->function
->variables
[ref
].name
;
188 /* Steals input reference even on failure. */
189 static HRESULT
stack_push_exprval(script_ctx_t
*ctx
, exprval_t
*val
)
197 hres
= stack_push(ctx
, jsval_disp(val
->u
.idref
.disp
));
199 hres
= stack_push(ctx
, jsval_number(val
->u
.idref
.id
));
201 IDispatch_Release(val
->u
.idref
.disp
);
203 case EXPRVAL_STACK_REF
:
204 hres
= stack_push(ctx
, jsval_number(val
->u
.off
));
206 hres
= stack_push(ctx
, jsval_undefined());
208 case EXPRVAL_INVALID
:
209 hres
= stack_push(ctx
, jsval_undefined());
211 hres
= stack_push(ctx
, jsval_number(val
->u
.hres
));
219 static BOOL
stack_topn_exprval(script_ctx_t
*ctx
, unsigned n
, exprval_t
*r
)
221 jsval_t v
= stack_topn(ctx
, n
+1);
223 switch(jsval_type(v
)) {
225 call_frame_t
*frame
= ctx
->call_ctx
;
226 unsigned off
= get_number(v
);
228 if(!frame
->base_scope
->frame
&& off
>= frame
->arguments_off
) {
233 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
235 assert(off
< frame
->variables_off
+ frame
->function
->var_cnt
);
236 name
= off
>= frame
->variables_off
237 ? frame
->function
->variables
[off
- frame
->variables_off
].name
238 : frame
->function
->params
[off
- frame
->arguments_off
];
239 hres
= jsdisp_get_id(ctx
->call_ctx
->base_scope
->jsobj
, name
, 0, &id
);
241 r
->type
= EXPRVAL_INVALID
;
246 *stack_top_ref(ctx
, n
+1) = jsval_obj(jsdisp_addref(frame
->base_scope
->jsobj
));
247 *stack_top_ref(ctx
, n
) = jsval_number(id
);
248 r
->type
= EXPRVAL_IDREF
;
249 r
->u
.idref
.disp
= frame
->base_scope
->obj
;
254 r
->type
= EXPRVAL_STACK_REF
;
259 r
->type
= EXPRVAL_IDREF
;
260 r
->u
.idref
.disp
= get_object(v
);
261 assert(is_number(stack_topn(ctx
, n
)));
262 r
->u
.idref
.id
= get_number(stack_topn(ctx
, n
));
265 r
->type
= EXPRVAL_INVALID
;
266 assert(is_number(stack_topn(ctx
, n
)));
267 r
->u
.hres
= get_number(stack_topn(ctx
, n
));
275 static inline BOOL
stack_pop_exprval(script_ctx_t
*ctx
, exprval_t
*r
)
277 BOOL ret
= stack_topn_exprval(ctx
, 0, r
);
282 static HRESULT
exprval_propput(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t v
)
285 case EXPRVAL_STACK_REF
: {
286 jsval_t
*r
= ctx
->stack
+ ref
->u
.off
;
288 return jsval_copy(v
, r
);
291 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
);
298 static HRESULT
exprval_propget(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
301 case EXPRVAL_STACK_REF
:
302 return jsval_copy(ctx
->stack
[ref
->u
.off
], r
);
304 return disp_propget(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, r
);
311 static HRESULT
exprval_call(script_ctx_t
*ctx
, exprval_t
*ref
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
314 case EXPRVAL_STACK_REF
: {
315 jsval_t v
= ctx
->stack
[ref
->u
.off
];
317 if(!is_object_instance(v
)) {
318 FIXME("invoke %s\n", debugstr_jsval(v
));
322 return disp_call_value(ctx
, get_object(v
), NULL
, flags
, argc
, argv
, r
);
325 return disp_call(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, flags
, argc
, argv
, r
);
332 /* ECMA-262 3rd Edition 8.7.1 */
333 /* Steals input reference. */
334 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
338 if(ref
->type
== EXPRVAL_JSVAL
) {
343 hres
= exprval_propget(ctx
, ref
, r
);
345 if(ref
->type
== EXPRVAL_IDREF
)
346 IDispatch_Release(ref
->u
.idref
.disp
);
350 static void exprval_release(exprval_t
*val
)
354 jsval_release(val
->u
.val
);
357 if(val
->u
.idref
.disp
)
358 IDispatch_Release(val
->u
.idref
.disp
);
360 case EXPRVAL_STACK_REF
:
361 case EXPRVAL_INVALID
:
366 static inline void exprval_set_exception(exprval_t
*val
, HRESULT hres
)
368 val
->type
= EXPRVAL_INVALID
;
372 static inline void exprval_set_disp_ref(exprval_t
*ref
, IDispatch
*obj
, DISPID id
)
374 ref
->type
= EXPRVAL_IDREF
;
375 IDispatch_AddRef(ref
->u
.idref
.disp
= obj
);
376 ref
->u
.idref
.id
= id
;
379 static inline jsval_t
steal_ret(call_frame_t
*frame
)
381 jsval_t r
= frame
->ret
;
382 frame
->ret
= jsval_undefined();
386 static inline void clear_acc(script_ctx_t
*ctx
)
388 jsval_release(ctx
->acc
);
389 ctx
->acc
= jsval_undefined();
392 static HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
394 scope_chain_t
*new_scope
;
396 new_scope
= heap_alloc(sizeof(scope_chain_t
));
398 return E_OUTOFMEMORY
;
402 IDispatch_AddRef(obj
);
403 new_scope
->jsobj
= jsobj
;
404 new_scope
->obj
= obj
;
405 new_scope
->frame
= NULL
;
406 new_scope
->next
= scope
? scope_addref(scope
) : NULL
;
412 static void scope_pop(scope_chain_t
**scope
)
421 void scope_release(scope_chain_t
*scope
)
427 scope_release(scope
->next
);
429 IDispatch_Release(scope
->obj
);
433 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
440 jsdisp
= iface_to_jsdisp(disp
);
442 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
443 jsdisp_release(jsdisp
);
450 bstr
= SysAllocString(name
);
452 return E_OUTOFMEMORY
;
456 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
457 if(SUCCEEDED(hres
)) {
458 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
459 IDispatchEx_Release(dispex
);
461 TRACE("using IDispatch\n");
462 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
465 if(name_bstr
!= bstr
)
470 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
472 IObjectIdentity
*identity
;
473 IUnknown
*unk1
, *unk2
;
481 if(!disp1
|| !disp2
) {
486 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
490 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
492 IUnknown_Release(unk1
);
499 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
500 if(SUCCEEDED(hres
)) {
501 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
502 IObjectIdentity_Release(identity
);
509 IUnknown_Release(unk1
);
510 IUnknown_Release(unk2
);
514 /* ECMA-262 3rd Edition 11.9.6 */
515 HRESULT
jsval_strict_equal(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
517 jsval_type_t type
= jsval_type(lval
);
521 if(type
!= jsval_type(rval
)) {
522 if(is_null_instance(lval
))
523 *ret
= is_null_instance(rval
);
535 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
537 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
540 *ret
= get_number(lval
) == get_number(rval
);
543 *ret
= !get_bool(lval
) == !get_bool(rval
);
546 FIXME("VARIANT not implemented\n");
554 * Transfers local variables from stack to variable object.
555 * It's slow, so we want to avoid it as much as possible.
557 static HRESULT
detach_variable_object(script_ctx_t
*ctx
, call_frame_t
*frame
, BOOL from_release
)
562 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
565 TRACE("detaching %p\n", frame
);
567 assert(frame
== frame
->base_scope
->frame
);
568 assert(frame
->variable_obj
== frame
->base_scope
->jsobj
);
570 if(!from_release
&& !frame
->arguments_obj
) {
571 hres
= setup_arguments_object(ctx
, frame
);
576 frame
->base_scope
->frame
= NULL
;
578 for(i
= 0; i
< frame
->function
->locals_cnt
; i
++) {
579 hres
= jsdisp_propput_name(frame
->variable_obj
, frame
->function
->locals
[i
].name
,
580 ctx
->stack
[local_off(frame
, frame
->function
->locals
[i
].ref
)]);
588 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
594 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
595 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
596 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
597 if(SUCCEEDED(hres
)) {
599 exprval_set_disp_ref(ret
, item
->disp
, id
);
608 IDispatch
*lookup_global_host(script_ctx_t
*ctx
)
610 IDispatch
*disp
= NULL
;
613 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
614 if(!(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) continue;
618 if(!disp
) disp
= to_disp(ctx
->global
);
623 static int __cdecl
local_ref_cmp(const void *key
, const void *ref
)
625 return wcscmp((const WCHAR
*)key
, ((const local_ref_t
*)ref
)->name
);
628 local_ref_t
*lookup_local(const function_code_t
*function
, const WCHAR
*identifier
)
630 return bsearch(identifier
, function
->locals
, function
->locals_cnt
, sizeof(*function
->locals
), local_ref_cmp
);
633 /* ECMA-262 3rd Edition 10.1.4 */
634 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
636 scope_chain_t
*scope
;
641 TRACE("%s\n", debugstr_w(identifier
));
644 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
646 function_code_t
*func
= scope
->frame
->function
;
647 local_ref_t
*ref
= lookup_local(func
, identifier
);
648 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
651 ret
->type
= EXPRVAL_STACK_REF
;
652 ret
->u
.off
= local_off(scope
->frame
, ref
->ref
);
653 TRACE("returning ref %d for %d\n", ret
->u
.off
, ref
->ref
);
657 if(!wcscmp(identifier
, argumentsW
)) {
658 hres
= detach_variable_object(ctx
, scope
->frame
, FALSE
);
664 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
666 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
667 if(SUCCEEDED(hres
)) {
668 exprval_set_disp_ref(ret
, scope
->obj
, id
);
674 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
675 if(SUCCEEDED(hres
)) {
676 exprval_set_disp_ref(ret
, to_disp(ctx
->global
), id
);
680 item
= lookup_named_item(ctx
, identifier
, SCRIPTITEM_ISVISIBLE
);
682 IDispatch_AddRef(item
->disp
);
683 ret
->type
= EXPRVAL_JSVAL
;
684 ret
->u
.val
= jsval_disp(item
->disp
);
688 if(lookup_global_members(ctx
, identifier
, ret
))
691 exprval_set_exception(ret
, JS_E_UNDEFINED_VARIABLE
);
695 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
697 call_frame_t
*frame
= ctx
->call_ctx
;
698 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
701 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
703 call_frame_t
*frame
= ctx
->call_ctx
;
704 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
707 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
709 call_frame_t
*frame
= ctx
->call_ctx
;
710 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
713 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
715 call_frame_t
*frame
= ctx
->call_ctx
;
716 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
719 static inline double get_op_double(script_ctx_t
*ctx
)
721 call_frame_t
*frame
= ctx
->call_ctx
;
722 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
725 static inline void jmp_next(script_ctx_t
*ctx
)
730 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
732 ctx
->call_ctx
->ip
= dst
;
735 /* ECMA-262 3rd Edition 12.6.4 */
736 static HRESULT
interp_forin(script_ctx_t
*ctx
)
738 const HRESULT arg
= get_op_uint(ctx
, 0);
739 IDispatch
*obj
= NULL
;
748 assert(is_number(stack_top(ctx
)));
749 id
= get_number(stack_top(ctx
));
751 if(!stack_topn_exprval(ctx
, 1, &prop_ref
)) {
752 FIXME("invalid ref: %08x\n", prop_ref
.u
.hres
);
756 if(is_object_instance(stack_topn(ctx
, 3)))
757 obj
= get_object(stack_topn(ctx
, 3));
760 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
761 if(SUCCEEDED(hres
)) {
762 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
764 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
765 IDispatchEx_Release(dispex
);
769 TRACE("No IDispatchEx\n");
776 str
= jsstr_alloc_len(name
, SysStringLen(name
));
779 return E_OUTOFMEMORY
;
782 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
784 hres
= exprval_propput(ctx
, &prop_ref
, jsval_string(str
));
797 /* ECMA-262 3rd Edition 12.10 */
798 static HRESULT
interp_push_scope(script_ctx_t
*ctx
)
807 hres
= to_object(ctx
, v
, &disp
);
812 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
813 IDispatch_Release(disp
);
817 /* ECMA-262 3rd Edition 12.10 */
818 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
822 scope_pop(&ctx
->call_ctx
->scope
);
826 /* ECMA-262 3rd Edition 12.13 */
827 static HRESULT
interp_case(script_ctx_t
*ctx
)
829 const unsigned arg
= get_op_uint(ctx
, 0);
837 hres
= jsval_strict_equal(stack_top(ctx
), v
, &b
);
851 static void set_error_value(script_ctx_t
*ctx
, jsval_t value
)
853 jsexcept_t
*ei
= ctx
->ei
;
857 ei
->error
= JS_E_EXCEPTION_THROWN
;
858 ei
->valid_value
= TRUE
;
861 if(is_object_instance(value
) && get_object(value
) && (obj
= to_jsdisp(get_object(value
)))) {
867 /* FIXME: We should check if object is an error instance */
869 hres
= jsdisp_propget_name(obj
, L
"number", &v
);
870 if(SUCCEEDED(hres
)) {
871 hres
= to_uint32(ctx
, v
, &number
);
873 ei
->error
= FAILED(number
) ? number
: E_FAIL
;
877 hres
= jsdisp_propget_name(obj
, L
"description", &v
);
878 if(SUCCEEDED(hres
)) {
879 hres
= to_string(ctx
, v
, &str
);
887 /* ECMA-262 3rd Edition 12.13 */
888 static HRESULT
interp_throw(script_ctx_t
*ctx
)
892 set_error_value(ctx
, stack_pop(ctx
));
893 return DISP_E_EXCEPTION
;
896 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
898 const HRESULT arg
= get_op_uint(ctx
, 0);
900 TRACE("%08x\n", arg
);
905 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
907 const HRESULT hres
= get_op_uint(ctx
, 0);
908 jsstr_t
*str
= get_op_str(ctx
, 1);
911 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
913 ptr
= jsstr_flatten(str
);
914 return ptr
? throw_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
917 /* ECMA-262 3rd Edition 12.14 */
918 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
920 const unsigned catch_off
= get_op_uint(ctx
, 0);
921 const unsigned finally_off
= get_op_uint(ctx
, 1);
922 call_frame_t
*frame
= ctx
->call_ctx
;
923 except_frame_t
*except
;
927 except
= heap_alloc(sizeof(*except
));
929 return E_OUTOFMEMORY
;
931 except
->stack_top
= ctx
->stack_top
;
932 except
->scope
= frame
->scope
;
933 except
->catch_off
= catch_off
;
934 except
->finally_off
= finally_off
;
935 except
->next
= frame
->except_frame
;
936 frame
->except_frame
= except
;
940 /* ECMA-262 3rd Edition 12.14 */
941 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
943 const unsigned ret_off
= get_op_uint(ctx
, 0);
944 call_frame_t
*frame
= ctx
->call_ctx
;
945 except_frame_t
*except
;
946 unsigned finally_off
;
948 TRACE("%u\n", ret_off
);
950 except
= frame
->except_frame
;
951 assert(except
!= NULL
);
953 finally_off
= except
->finally_off
;
954 frame
->except_frame
= except
->next
;
960 hres
= stack_push(ctx
, jsval_number(ret_off
));
963 hres
= stack_push(ctx
, jsval_bool(TRUE
));
966 frame
->ip
= finally_off
;
974 /* ECMA-262 3rd Edition 12.14 */
975 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
977 call_frame_t
*frame
= ctx
->call_ctx
;
986 TRACE("passing exception\n");
988 set_error_value(ctx
, stack_pop(ctx
));
989 return DISP_E_EXCEPTION
;
993 assert(is_number(v
));
994 frame
->ip
= get_number(v
);
998 static HRESULT
interp_enter_catch(script_ctx_t
*ctx
)
1000 const BSTR ident
= get_op_bstr(ctx
, 0);
1001 jsdisp_t
*scope_obj
;
1005 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
1010 hres
= jsdisp_propput_name(scope_obj
, ident
, v
);
1013 hres
= scope_push(ctx
->call_ctx
->scope
, scope_obj
, to_disp(scope_obj
), &ctx
->call_ctx
->scope
);
1014 jsdisp_release(scope_obj
);
1018 /* ECMA-262 3rd Edition 13 */
1019 static HRESULT
interp_func(script_ctx_t
*ctx
)
1021 unsigned func_idx
= get_op_uint(ctx
, 0);
1022 call_frame_t
*frame
= ctx
->call_ctx
;
1026 TRACE("%d\n", func_idx
);
1028 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
1029 frame
->scope
, &dispex
);
1033 return stack_push(ctx
, jsval_obj(dispex
));
1036 /* ECMA-262 3rd Edition 11.2.1 */
1037 static HRESULT
interp_array(script_ctx_t
*ctx
)
1048 namev
= stack_pop(ctx
);
1050 hres
= stack_pop_object(ctx
, &obj
);
1052 jsval_release(namev
);
1056 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1057 jsval_release(namev
);
1059 IDispatch_Release(obj
);
1063 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
1064 jsstr_release(name_str
);
1065 if(SUCCEEDED(hres
)) {
1066 hres
= disp_propget(ctx
, obj
, id
, &v
);
1067 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1068 v
= jsval_undefined();
1071 IDispatch_Release(obj
);
1075 return stack_push(ctx
, v
);
1078 /* ECMA-262 3rd Edition 11.2.1 */
1079 static HRESULT
interp_member(script_ctx_t
*ctx
)
1081 const BSTR arg
= get_op_bstr(ctx
, 0);
1089 hres
= stack_pop_object(ctx
, &obj
);
1093 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
1094 if(SUCCEEDED(hres
)) {
1095 hres
= disp_propget(ctx
, obj
, id
, &v
);
1096 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1097 v
= jsval_undefined();
1100 IDispatch_Release(obj
);
1104 return stack_push(ctx
, v
);
1107 /* ECMA-262 3rd Edition 11.2.1 */
1108 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
1110 const unsigned arg
= get_op_uint(ctx
, 0);
1111 jsval_t objv
, namev
;
1121 namev
= stack_pop(ctx
);
1122 objv
= stack_pop(ctx
);
1124 hres
= to_object(ctx
, objv
, &obj
);
1125 jsval_release(objv
);
1126 if(SUCCEEDED(hres
)) {
1127 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1129 IDispatch_Release(obj
);
1131 jsval_release(namev
);
1135 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
1136 jsstr_release(name_str
);
1137 if(SUCCEEDED(hres
)) {
1138 ref
.type
= EXPRVAL_IDREF
;
1139 ref
.u
.idref
.disp
= obj
;
1140 ref
.u
.idref
.id
= id
;
1142 IDispatch_Release(obj
);
1143 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1144 exprval_set_exception(&ref
, JS_E_INVALID_PROPERTY
);
1147 ERR("failed %08x\n", hres
);
1152 return stack_push_exprval(ctx
, &ref
);
1155 /* ECMA-262 3rd Edition 11.2.1 */
1156 static HRESULT
interp_refval(script_ctx_t
*ctx
)
1164 if(!stack_topn_exprval(ctx
, 0, &ref
))
1165 return JS_E_ILLEGAL_ASSIGN
;
1167 hres
= exprval_propget(ctx
, &ref
, &v
);
1171 return stack_push(ctx
, v
);
1174 /* ECMA-262 3rd Edition 11.2.2 */
1175 static HRESULT
interp_new(script_ctx_t
*ctx
)
1177 const unsigned argc
= get_op_uint(ctx
, 0);
1180 TRACE("%d\n", argc
);
1182 constr
= stack_topn(ctx
, argc
);
1184 /* NOTE: Should use to_object here */
1187 return JS_E_OBJECT_EXPECTED
;
1188 else if(!is_object_instance(constr
))
1189 return JS_E_INVALID_ACTION
;
1190 else if(!get_object(constr
))
1191 return JS_E_INVALID_PROPERTY
;
1194 return disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1195 argc
, stack_args(ctx
, argc
), &ctx
->acc
);
1198 /* ECMA-262 3rd Edition 11.2.3 */
1199 static HRESULT
interp_call(script_ctx_t
*ctx
)
1201 const unsigned argn
= get_op_uint(ctx
, 0);
1202 const int do_ret
= get_op_int(ctx
, 1);
1205 TRACE("%d %d\n", argn
, do_ret
);
1207 obj
= stack_topn(ctx
, argn
);
1208 if(!is_object_instance(obj
))
1209 return JS_E_INVALID_PROPERTY
;
1212 return disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1213 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1216 /* ECMA-262 3rd Edition 11.2.3 */
1217 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
1219 const unsigned argn
= get_op_uint(ctx
, 0);
1220 const int do_ret
= get_op_int(ctx
, 1);
1223 TRACE("%d %d\n", argn
, do_ret
);
1225 if(!stack_topn_exprval(ctx
, argn
, &ref
))
1229 return exprval_call(ctx
, &ref
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1230 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1233 /* ECMA-262 3rd Edition 11.1.1 */
1234 static HRESULT
interp_this(script_ctx_t
*ctx
)
1236 IDispatch
*this_obj
= ctx
->call_ctx
->this_obj
;
1241 this_obj
= lookup_global_host(ctx
);
1243 IDispatch_AddRef(this_obj
);
1244 return stack_push(ctx
, jsval_disp(this_obj
));
1247 static HRESULT
interp_identifier_ref(script_ctx_t
*ctx
, BSTR identifier
, unsigned flags
)
1252 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1256 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1259 hres
= jsdisp_get_id(ctx
->global
, identifier
, fdexNameEnsure
, &id
);
1263 exprval_set_disp_ref(&exprval
, to_disp(ctx
->global
), id
);
1266 if(exprval
.type
== EXPRVAL_JSVAL
|| exprval
.type
== EXPRVAL_INVALID
) {
1267 WARN("invalid ref\n");
1268 exprval_release(&exprval
);
1269 exprval_set_exception(&exprval
, JS_E_OBJECT_EXPECTED
);
1272 return stack_push_exprval(ctx
, &exprval
);
1275 static HRESULT
identifier_value(script_ctx_t
*ctx
, BSTR identifier
)
1281 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1285 if(exprval
.type
== EXPRVAL_INVALID
)
1286 return throw_error(ctx
, exprval
.u
.hres
, identifier
);
1288 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1292 return stack_push(ctx
, v
);
1295 static HRESULT
interp_local_ref(script_ctx_t
*ctx
)
1297 const int arg
= get_op_int(ctx
, 0);
1298 const unsigned flags
= get_op_uint(ctx
, 1);
1299 call_frame_t
*frame
= ctx
->call_ctx
;
1304 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1305 return interp_identifier_ref(ctx
, local_name(frame
, arg
), flags
);
1307 ref
.type
= EXPRVAL_STACK_REF
;
1308 ref
.u
.off
= local_off(frame
, arg
);
1309 return stack_push_exprval(ctx
, &ref
);
1312 static HRESULT
interp_local(script_ctx_t
*ctx
)
1314 const int arg
= get_op_int(ctx
, 0);
1315 call_frame_t
*frame
= ctx
->call_ctx
;
1319 TRACE("%d: %s\n", arg
, debugstr_w(local_name(frame
, arg
)));
1321 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1322 return identifier_value(ctx
, local_name(frame
, arg
));
1324 hres
= jsval_copy(ctx
->stack
[local_off(frame
, arg
)], ©
);
1328 return stack_push(ctx
, copy
);
1331 /* ECMA-262 3rd Edition 10.1.4 */
1332 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1334 const BSTR arg
= get_op_bstr(ctx
, 0);
1336 TRACE("%s\n", debugstr_w(arg
));
1338 return identifier_value(ctx
, arg
);
1341 /* ECMA-262 3rd Edition 10.1.4 */
1342 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1344 const BSTR arg
= get_op_bstr(ctx
, 0);
1345 const unsigned flags
= get_op_uint(ctx
, 1);
1347 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1349 return interp_identifier_ref(ctx
, arg
, flags
);
1352 /* ECMA-262 3rd Edition 7.8.1 */
1353 static HRESULT
interp_null(script_ctx_t
*ctx
)
1357 return stack_push(ctx
, jsval_null());
1360 /* ECMA-262 3rd Edition 7.8.2 */
1361 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1363 const int arg
= get_op_int(ctx
, 0);
1365 TRACE("%s\n", arg
? "true" : "false");
1367 return stack_push(ctx
, jsval_bool(arg
));
1370 /* ECMA-262 3rd Edition 7.8.3 */
1371 static HRESULT
interp_int(script_ctx_t
*ctx
)
1373 const int arg
= get_op_int(ctx
, 0);
1377 return stack_push(ctx
, jsval_number(arg
));
1380 /* ECMA-262 3rd Edition 7.8.3 */
1381 static HRESULT
interp_double(script_ctx_t
*ctx
)
1383 const double arg
= get_op_double(ctx
);
1385 TRACE("%lf\n", arg
);
1387 return stack_push(ctx
, jsval_number(arg
));
1390 /* ECMA-262 3rd Edition 7.8.4 */
1391 static HRESULT
interp_str(script_ctx_t
*ctx
)
1393 jsstr_t
*str
= get_op_str(ctx
, 0);
1395 TRACE("%s\n", debugstr_jsstr(str
));
1397 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1400 /* ECMA-262 3rd Edition 7.8 */
1401 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1403 jsstr_t
*source
= get_op_str(ctx
, 0);
1404 const unsigned flags
= get_op_uint(ctx
, 1);
1408 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1410 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1414 return stack_push(ctx
, jsval_obj(regexp
));
1417 /* ECMA-262 3rd Edition 11.1.4 */
1418 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1420 const unsigned arg
= get_op_uint(ctx
, 0);
1426 hres
= create_array(ctx
, arg
, &array
);
1430 return stack_push(ctx
, jsval_obj(array
));
1433 static HRESULT
interp_carray_set(script_ctx_t
*ctx
)
1435 const unsigned index
= get_op_uint(ctx
, 0);
1436 jsval_t value
, array
;
1439 value
= stack_pop(ctx
);
1441 TRACE("[%u] = %s\n", index
, debugstr_jsval(value
));
1443 array
= stack_top(ctx
);
1444 assert(is_object_instance(array
));
1446 hres
= jsdisp_propput_idx(iface_to_jsdisp(get_object(array
)), index
, value
);
1447 jsval_release(value
);
1451 /* ECMA-262 3rd Edition 11.1.5 */
1452 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1459 hres
= create_object(ctx
, NULL
, &obj
);
1463 return stack_push(ctx
, jsval_obj(obj
));
1466 /* ECMA-262 3rd Edition 11.1.5 */
1467 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1469 jsstr_t
*name_arg
= get_op_str(ctx
, 0);
1470 unsigned type
= get_op_uint(ctx
, 1);
1476 TRACE("%s\n", debugstr_jsstr(name_arg
));
1478 val
= stack_pop(ctx
);
1480 /* FIXME: we should pass it as jsstr_t */
1481 name
= jsstr_flatten(name_arg
);
1483 assert(is_object_instance(stack_top(ctx
)));
1484 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1486 if(type
== PROPERTY_DEFINITION_VALUE
) {
1487 hres
= jsdisp_propput_name(obj
, name
, val
);
1489 property_desc_t desc
= {PROPF_ENUMERABLE
| PROPF_CONFIGURABLE
};
1492 assert(is_object_instance(val
));
1493 func
= iface_to_jsdisp(get_object(val
));
1495 desc
.mask
= desc
.flags
;
1496 if(type
== PROPERTY_DEFINITION_GETTER
) {
1497 desc
.explicit_getter
= TRUE
;
1500 desc
.explicit_setter
= TRUE
;
1504 hres
= jsdisp_define_property(obj
, name
, &desc
);
1505 jsdisp_release(func
);
1512 /* ECMA-262 3rd Edition 11.11 */
1513 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1515 const unsigned arg
= get_op_uint(ctx
, 0);
1521 hres
= to_boolean(stack_top(ctx
), &b
);
1534 /* ECMA-262 3rd Edition 11.11 */
1535 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1537 const unsigned arg
= get_op_uint(ctx
, 0);
1543 hres
= to_boolean(stack_top(ctx
), &b
);
1556 /* ECMA-262 3rd Edition 11.10 */
1557 static HRESULT
interp_or(script_ctx_t
*ctx
)
1564 hres
= stack_pop_int(ctx
, &r
);
1568 hres
= stack_pop_int(ctx
, &l
);
1572 return stack_push(ctx
, jsval_number(l
|r
));
1575 /* ECMA-262 3rd Edition 11.10 */
1576 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1583 hres
= stack_pop_int(ctx
, &r
);
1587 hres
= stack_pop_int(ctx
, &l
);
1591 return stack_push(ctx
, jsval_number(l
^r
));
1594 /* ECMA-262 3rd Edition 11.10 */
1595 static HRESULT
interp_and(script_ctx_t
*ctx
)
1602 hres
= stack_pop_int(ctx
, &r
);
1606 hres
= stack_pop_int(ctx
, &l
);
1610 return stack_push(ctx
, jsval_number(l
&r
));
1613 /* ECMA-262 3rd Edition 11.8.6 */
1614 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1616 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1621 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1624 if(!is_object_instance(v
) || !get_object(v
)) {
1626 return JS_E_FUNCTION_EXPECTED
;
1629 obj
= iface_to_jsdisp(get_object(v
));
1630 IDispatch_Release(get_object(v
));
1632 FIXME("non-jsdisp objects not supported\n");
1636 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1637 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1639 hres
= JS_E_FUNCTION_EXPECTED
;
1641 jsdisp_release(obj
);
1647 if(is_object_instance(prot
)) {
1648 if(is_object_instance(v
))
1649 tmp
= iface_to_jsdisp(get_object(v
));
1650 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1651 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1657 jsdisp_release(tmp
);
1659 FIXME("prototype is not an object\n");
1663 jsval_release(prot
);
1668 return stack_push(ctx
, jsval_bool(ret
));
1671 /* ECMA-262 3rd Edition 11.8.7 */
1672 static HRESULT
interp_in(script_ctx_t
*ctx
)
1683 obj
= stack_pop(ctx
);
1684 if(!is_object_instance(obj
) || !get_object(obj
)) {
1686 return JS_E_OBJECT_EXPECTED
;
1690 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1693 IDispatch_Release(get_object(obj
));
1697 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1698 IDispatch_Release(get_object(obj
));
1699 jsstr_release(jsstr
);
1702 else if(hres
== DISP_E_UNKNOWNNAME
)
1707 return stack_push(ctx
, jsval_bool(ret
));
1710 /* ECMA-262 3rd Edition 11.6.1 */
1711 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1716 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1720 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1726 if(is_string(l
) || is_string(r
)) {
1727 jsstr_t
*lstr
, *rstr
= NULL
;
1729 hres
= to_string(ctx
, l
, &lstr
);
1731 hres
= to_string(ctx
, r
, &rstr
);
1733 if(SUCCEEDED(hres
)) {
1736 ret_str
= jsstr_concat(lstr
, rstr
);
1738 *ret
= jsval_string(ret_str
);
1740 hres
= E_OUTOFMEMORY
;
1743 jsstr_release(lstr
);
1745 jsstr_release(rstr
);
1749 hres
= to_number(ctx
, l
, &nl
);
1750 if(SUCCEEDED(hres
)) {
1751 hres
= to_number(ctx
, r
, &nr
);
1753 *ret
= jsval_number(nl
+nr
);
1762 /* ECMA-262 3rd Edition 11.6.1 */
1763 static HRESULT
interp_add(script_ctx_t
*ctx
)
1771 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1773 hres
= add_eval(ctx
, l
, r
, &ret
);
1779 return stack_push(ctx
, ret
);
1782 /* ECMA-262 3rd Edition 11.6.2 */
1783 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1790 hres
= stack_pop_number(ctx
, &r
);
1794 hres
= stack_pop_number(ctx
, &l
);
1798 return stack_push(ctx
, jsval_number(l
-r
));
1801 /* ECMA-262 3rd Edition 11.5.1 */
1802 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1809 hres
= stack_pop_number(ctx
, &r
);
1813 hres
= stack_pop_number(ctx
, &l
);
1817 return stack_push(ctx
, jsval_number(l
*r
));
1820 /* ECMA-262 3rd Edition 11.5.2 */
1821 static HRESULT
interp_div(script_ctx_t
*ctx
)
1828 hres
= stack_pop_number(ctx
, &r
);
1832 hres
= stack_pop_number(ctx
, &l
);
1836 return stack_push(ctx
, jsval_number(l
/r
));
1839 /* ECMA-262 3rd Edition 11.5.3 */
1840 static HRESULT
interp_mod(script_ctx_t
*ctx
)
1847 hres
= stack_pop_number(ctx
, &r
);
1851 hres
= stack_pop_number(ctx
, &l
);
1855 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1858 /* ECMA-262 3rd Edition 11.4.2 */
1859 static HRESULT
interp_delete(script_ctx_t
*ctx
)
1861 jsval_t objv
, namev
;
1869 namev
= stack_pop(ctx
);
1870 objv
= stack_pop(ctx
);
1872 hres
= to_object(ctx
, objv
, &obj
);
1873 jsval_release(objv
);
1875 jsval_release(namev
);
1879 hres
= to_string(ctx
, namev
, &name
);
1880 jsval_release(namev
);
1882 IDispatch_Release(obj
);
1886 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
1887 IDispatch_Release(obj
);
1888 jsstr_release(name
);
1892 return stack_push(ctx
, jsval_bool(ret
));
1895 /* ECMA-262 3rd Edition 11.4.2 */
1896 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
1898 const BSTR arg
= get_op_bstr(ctx
, 0);
1903 TRACE("%s\n", debugstr_w(arg
));
1905 hres
= identifier_eval(ctx
, arg
, &exprval
);
1909 switch(exprval
.type
) {
1910 case EXPRVAL_STACK_REF
:
1914 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1915 IDispatch_Release(exprval
.u
.idref
.disp
);
1919 case EXPRVAL_INVALID
:
1923 FIXME("Unsupported exprval\n");
1924 exprval_release(&exprval
);
1929 return stack_push(ctx
, jsval_bool(ret
));
1932 /* ECMA-262 3rd Edition 11.4.2 */
1933 static HRESULT
interp_void(script_ctx_t
*ctx
)
1938 return stack_push(ctx
, jsval_undefined());
1941 /* ECMA-262 3rd Edition 11.4.3 */
1942 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1944 switch(jsval_type(v
)) {
1954 if(get_object(v
) && (dispex
= iface_to_jsdisp(get_object(v
)))) {
1955 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1956 jsdisp_release(dispex
);
1972 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1979 /* ECMA-262 3rd Edition 11.4.3 */
1980 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
1989 if(!stack_pop_exprval(ctx
, &ref
))
1990 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1992 hres
= exprval_propget(ctx
, &ref
, &v
);
1993 exprval_release(&ref
);
1995 return stack_push_string(ctx
, unknownW
);
1997 hres
= typeof_string(v
, &ret
);
2002 return stack_push_string(ctx
, ret
);
2005 /* ECMA-262 3rd Edition 11.4.3 */
2006 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
2008 const BSTR arg
= get_op_bstr(ctx
, 0);
2014 TRACE("%s\n", debugstr_w(arg
));
2016 hres
= identifier_eval(ctx
, arg
, &exprval
);
2020 if(exprval
.type
== EXPRVAL_INVALID
)
2021 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2023 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2027 hres
= typeof_string(v
, &ret
);
2032 return stack_push_string(ctx
, ret
);
2035 /* ECMA-262 3rd Edition 11.4.3 */
2036 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
2045 hres
= typeof_string(v
, &ret
);
2050 return stack_push_string(ctx
, ret
);
2053 /* ECMA-262 3rd Edition 11.4.7 */
2054 static HRESULT
interp_minus(script_ctx_t
*ctx
)
2061 hres
= stack_pop_number(ctx
, &n
);
2065 return stack_push(ctx
, jsval_number(-n
));
2068 /* ECMA-262 3rd Edition 11.4.6 */
2069 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
2078 hres
= to_number(ctx
, v
, &n
);
2083 return stack_push(ctx
, jsval_number(n
));
2086 /* ECMA-262 3rd Edition 11.3.1 */
2087 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
2089 const int arg
= get_op_int(ctx
, 0);
2096 if(!stack_pop_exprval(ctx
, &ref
))
2097 return JS_E_OBJECT_EXPECTED
;
2099 hres
= exprval_propget(ctx
, &ref
, &v
);
2100 if(SUCCEEDED(hres
)) {
2103 hres
= to_number(ctx
, v
, &n
);
2105 hres
= exprval_propput(ctx
, &ref
, jsval_number(n
+(double)arg
));
2109 exprval_release(&ref
);
2113 return stack_push(ctx
, v
);
2116 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2117 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
2119 const int arg
= get_op_int(ctx
, 0);
2127 if(!stack_pop_exprval(ctx
, &ref
))
2128 return JS_E_OBJECT_EXPECTED
;
2130 hres
= exprval_propget(ctx
, &ref
, &v
);
2131 if(SUCCEEDED(hres
)) {
2134 hres
= to_number(ctx
, v
, &n
);
2136 if(SUCCEEDED(hres
)) {
2137 ret
= n
+(double)arg
;
2138 hres
= exprval_propput(ctx
, &ref
, jsval_number(ret
));
2141 exprval_release(&ref
);
2145 return stack_push(ctx
, jsval_number(ret
));
2148 /* ECMA-262 3rd Edition 11.9.3 */
2149 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
2151 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
2152 return jsval_strict_equal(lval
, rval
, ret
);
2154 /* FIXME: NULL disps should be handled in more general way */
2155 if(is_object_instance(lval
) && !get_object(lval
))
2156 return equal_values(ctx
, jsval_null(), rval
, ret
);
2157 if(is_object_instance(rval
) && !get_object(rval
))
2158 return equal_values(ctx
, lval
, jsval_null(), ret
);
2160 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
2165 if(is_string(lval
) && is_number(rval
)) {
2169 hres
= to_number(ctx
, lval
, &n
);
2173 /* FIXME: optimize */
2174 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
2177 if(is_string(rval
) && is_number(lval
)) {
2181 hres
= to_number(ctx
, rval
, &n
);
2185 /* FIXME: optimize */
2186 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
2190 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
2193 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
2196 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
2200 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
2204 hres
= equal_values(ctx
, lval
, prim
, ret
);
2205 jsval_release(prim
);
2210 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
2214 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
2218 hres
= equal_values(ctx
, prim
, rval
, ret
);
2219 jsval_release(prim
);
2228 /* ECMA-262 3rd Edition 11.9.1 */
2229 static HRESULT
interp_eq(script_ctx_t
*ctx
)
2238 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2240 hres
= equal_values(ctx
, l
, r
, &b
);
2246 return stack_push(ctx
, jsval_bool(b
));
2249 /* ECMA-262 3rd Edition 11.9.2 */
2250 static HRESULT
interp_neq(script_ctx_t
*ctx
)
2259 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2261 hres
= equal_values(ctx
, l
, r
, &b
);
2267 return stack_push(ctx
, jsval_bool(!b
));
2270 /* ECMA-262 3rd Edition 11.9.4 */
2271 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
2280 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2282 hres
= jsval_strict_equal(r
, l
, &b
);
2288 return stack_push(ctx
, jsval_bool(b
));
2291 /* ECMA-262 3rd Edition 11.9.5 */
2292 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2303 hres
= jsval_strict_equal(r
, l
, &b
);
2309 return stack_push(ctx
, jsval_bool(!b
));
2312 /* ECMA-262 3rd Edition 11.8.5 */
2313 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2319 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2323 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2329 if(is_string(l
) && is_string(r
)) {
2330 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2331 jsstr_release(get_string(l
));
2332 jsstr_release(get_string(r
));
2336 hres
= to_number(ctx
, l
, &ln
);
2339 hres
= to_number(ctx
, r
, &rn
);
2344 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2348 /* ECMA-262 3rd Edition 11.8.1 */
2349 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2358 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2360 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2366 return stack_push(ctx
, jsval_bool(b
));
2369 /* ECMA-262 3rd Edition 11.8.1 */
2370 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2379 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2381 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2387 return stack_push(ctx
, jsval_bool(b
));
2390 /* ECMA-262 3rd Edition 11.8.2 */
2391 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2400 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2402 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2408 return stack_push(ctx
, jsval_bool(b
));
2411 /* ECMA-262 3rd Edition 11.8.4 */
2412 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2421 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2423 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2429 return stack_push(ctx
, jsval_bool(b
));
2432 /* ECMA-262 3rd Edition 11.4.8 */
2433 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2442 hres
= to_int32(ctx
, v
, &i
);
2447 return stack_push(ctx
, jsval_number(~i
));
2450 /* ECMA-262 3rd Edition 11.4.9 */
2451 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2460 hres
= to_boolean(v
, &b
);
2465 return stack_push(ctx
, jsval_bool(!b
));
2468 /* ECMA-262 3rd Edition 11.7.1 */
2469 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2475 hres
= stack_pop_uint(ctx
, &r
);
2479 hres
= stack_pop_int(ctx
, &l
);
2483 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2486 /* ECMA-262 3rd Edition 11.7.2 */
2487 static HRESULT
interp_rshift(script_ctx_t
*ctx
)
2493 hres
= stack_pop_uint(ctx
, &r
);
2497 hres
= stack_pop_int(ctx
, &l
);
2501 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2504 /* ECMA-262 3rd Edition 11.7.3 */
2505 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2510 hres
= stack_pop_uint(ctx
, &r
);
2514 hres
= stack_pop_uint(ctx
, &l
);
2518 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2521 /* ECMA-262 3rd Edition 11.13.1 */
2522 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2532 if(!stack_pop_exprval(ctx
, &ref
)) {
2534 return JS_E_ILLEGAL_ASSIGN
;
2537 hres
= exprval_propput(ctx
, &ref
, v
);
2538 exprval_release(&ref
);
2544 return stack_push(ctx
, v
);
2547 /* JScript extension */
2548 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2550 const unsigned argc
= get_op_uint(ctx
, 0);
2555 TRACE("%u\n", argc
);
2557 if(!stack_topn_exprval(ctx
, argc
+1, &ref
))
2558 return JS_E_ILLEGAL_ASSIGN
;
2560 hres
= exprval_call(ctx
, &ref
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2565 stack_popn(ctx
, argc
+2);
2566 return stack_push(ctx
, v
);
2569 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2573 return stack_push(ctx
, jsval_undefined());
2576 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2578 const unsigned arg
= get_op_uint(ctx
, 0);
2586 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2588 const unsigned arg
= get_op_uint(ctx
, 0);
2596 hres
= to_boolean(v
, &b
);
2608 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2610 const unsigned arg
= get_op_uint(ctx
, 0);
2614 stack_popn(ctx
, arg
);
2618 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2620 const unsigned clear_ret
= get_op_uint(ctx
, 0);
2621 call_frame_t
*frame
= ctx
->call_ctx
;
2626 jsval_release(steal_ret(frame
));
2628 if((frame
->flags
& EXEC_CONSTRUCTOR
) && !is_object_instance(frame
->ret
)) {
2629 jsval_release(frame
->ret
);
2630 IDispatch_AddRef(frame
->this_obj
);
2631 frame
->ret
= jsval_disp(frame
->this_obj
);
2638 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2640 call_frame_t
*frame
= ctx
->call_ctx
;
2644 jsval_release(frame
->ret
);
2645 frame
->ret
= stack_pop(ctx
);
2649 static HRESULT
interp_push_acc(script_ctx_t
*ctx
)
2655 hres
= stack_push(ctx
, ctx
->acc
);
2657 ctx
->acc
= jsval_undefined();
2661 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2663 static const op_func_t op_funcs
[] = {
2664 #define X(x,a,b,c) interp_##x,
2669 static const unsigned op_move
[] = {
2670 #define X(a,x,b,c) x,
2675 static void pop_call_frame(script_ctx_t
*ctx
)
2677 call_frame_t
*frame
= ctx
->call_ctx
;
2679 frame
->stack_base
-= frame
->pop_locals
+ frame
->pop_variables
;
2681 assert(frame
->scope
== frame
->base_scope
);
2683 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2684 if(frame
->scope
&& frame
->scope
->ref
> 1) {
2685 HRESULT hres
= detach_variable_object(ctx
, frame
, TRUE
);
2687 ERR("Failed to detach variable object: %08x\n", hres
);
2690 if(frame
->arguments_obj
)
2691 detach_arguments_object(frame
->arguments_obj
);
2693 scope_release(frame
->scope
);
2695 if(frame
->pop_variables
)
2696 stack_popn(ctx
, frame
->pop_variables
);
2697 stack_popn(ctx
, frame
->pop_locals
);
2699 ctx
->call_ctx
= frame
->prev_frame
;
2701 if(frame
->function_instance
)
2702 jsdisp_release(frame
->function_instance
);
2703 if(frame
->variable_obj
)
2704 jsdisp_release(frame
->variable_obj
);
2706 IDispatch_Release(frame
->this_obj
);
2707 jsval_release(frame
->ret
);
2708 release_bytecode(frame
->bytecode
);
2712 static void print_backtrace(script_ctx_t
*ctx
)
2714 unsigned depth
= 0, i
;
2715 call_frame_t
*frame
;
2717 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
2718 WARN("%u\t", depth
);
2722 WARN("%p->", frame
->this_obj
);
2723 WARN("%s(", frame
->function
->name
? debugstr_w(frame
->function
->name
) : "[unnamed]");
2724 if(frame
->base_scope
&& frame
->base_scope
->frame
) {
2725 for(i
=0; i
< frame
->argc
; i
++) {
2726 if(i
< frame
->function
->param_cnt
)
2727 WARN("%s%s=%s", i
? ", " : "", debugstr_w(frame
->function
->params
[i
]),
2728 debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2730 WARN("%s%s", i
? ", " : "", debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2733 WARN("[detached frame]");
2737 if(!(frame
->flags
& EXEC_RETURN_TO_INTERP
)) {
2738 WARN("%u\t[native code]\n", depth
);
2744 static HRESULT
unwind_exception(script_ctx_t
*ctx
, HRESULT exception_hres
)
2746 except_frame_t
*except_frame
;
2747 jsexcept_t
*ei
= ctx
->ei
;
2748 call_frame_t
*frame
;
2753 if(WARN_ON(jscript
)) {
2754 jsdisp_t
*error_obj
;
2757 static const WCHAR messageW
[] = {'m','e','s','s','a','g','e',0};
2759 WARN("Exception %08x %s", exception_hres
, debugstr_jsval(ei
->valid_value
? ei
->value
: jsval_undefined()));
2760 if(ei
->valid_value
&& jsval_type(ei
->value
) == JSV_OBJECT
) {
2761 error_obj
= to_jsdisp(get_object(ei
->value
));
2763 hres
= jsdisp_propget_name(error_obj
, messageW
, &msg
);
2764 if(SUCCEEDED(hres
)) {
2765 WARN(" (message %s)", debugstr_jsval(msg
));
2772 print_backtrace(ctx
);
2775 frame
= ctx
->call_ctx
;
2776 if(exception_hres
!= DISP_E_EXCEPTION
)
2777 throw_error(ctx
, exception_hres
, NULL
);
2778 set_error_location(ei
, frame
->bytecode
, frame
->bytecode
->instrs
[frame
->ip
].loc
, IDS_RUNTIME_ERROR
, NULL
);
2780 while(!frame
->except_frame
) {
2783 while(frame
->scope
!= frame
->base_scope
)
2784 scope_pop(&frame
->scope
);
2786 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
2788 flags
= frame
->flags
;
2789 pop_call_frame(ctx
);
2790 if(!(flags
& EXEC_RETURN_TO_INTERP
))
2791 return DISP_E_EXCEPTION
;
2792 frame
= ctx
->call_ctx
;
2795 except_frame
= frame
->except_frame
;
2796 catch_off
= except_frame
->catch_off
;
2798 assert(except_frame
->stack_top
<= ctx
->stack_top
);
2799 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
2801 while(except_frame
->scope
!= frame
->scope
)
2802 scope_pop(&frame
->scope
);
2804 frame
->ip
= catch_off
? catch_off
: except_frame
->finally_off
;
2805 assert(!catch_off
|| frame
->bytecode
->instrs
[frame
->ip
].op
== OP_enter_catch
);
2807 if(ei
->valid_value
) {
2808 except_val
= ctx
->ei
->value
;
2809 ei
->valid_value
= FALSE
;
2812 if(!(err
= create_builtin_error(ctx
)))
2813 return E_OUTOFMEMORY
;
2814 except_val
= jsval_obj(err
);
2817 /* keep current except_frame if we're entering catch block with finally block associated */
2818 if(catch_off
&& except_frame
->finally_off
) {
2819 except_frame
->catch_off
= 0;
2821 frame
->except_frame
= except_frame
->next
;
2822 heap_free(except_frame
);
2825 hres
= stack_push(ctx
, except_val
);
2830 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2834 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, jsval_t
*r
)
2836 call_frame_t
*frame
;
2838 HRESULT hres
= S_OK
;
2843 frame
= ctx
->call_ctx
;
2844 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
2845 hres
= op_funcs
[op
](ctx
);
2847 hres
= unwind_exception(ctx
, hres
);
2850 }else if(frame
->ip
== -1) {
2851 const DWORD return_to_interp
= frame
->flags
& EXEC_RETURN_TO_INTERP
;
2853 assert(ctx
->stack_top
== frame
->stack_base
);
2854 assert(frame
->scope
== frame
->base_scope
);
2856 if(return_to_interp
) {
2857 jsval_release(ctx
->acc
);
2858 ctx
->acc
= steal_ret(frame
);
2860 *r
= steal_ret(frame
);
2862 pop_call_frame(ctx
);
2863 if(!return_to_interp
)
2866 frame
->ip
+= op_move
[op
];
2873 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
2875 IBindEventHandler
*target
;
2881 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
2885 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2889 if(!is_object_instance(v
)) {
2890 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
2894 disp
= get_object(v
);
2895 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
2896 if(SUCCEEDED(hres
)) {
2897 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
2898 IBindEventHandler_Release(target
);
2900 WARN("BindEvent failed: %08x\n", hres
);
2902 FIXME("No IBindEventHandler, not yet supported binding\n");
2905 IDispatch_Release(disp
);
2909 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
)
2911 const unsigned orig_stack
= ctx
->stack_top
;
2912 scope_chain_t
*scope
;
2917 /* If arguments are already on the stack, we may use them. */
2918 if(argv
+ argc
== ctx
->stack
+ ctx
->stack_top
) {
2919 frame
->arguments_off
= argv
- ctx
->stack
;
2922 frame
->arguments_off
= ctx
->stack_top
;
2923 for(i
= 0; i
< argc
; i
++) {
2924 hres
= jsval_copy(argv
[i
], &v
);
2926 hres
= stack_push(ctx
, v
);
2934 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2935 for(; i
< frame
->function
->param_cnt
; i
++) {
2936 hres
= stack_push(ctx
, jsval_undefined());
2938 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2943 frame
->pop_locals
= ctx
->stack_top
- orig_stack
;
2945 frame
->variables_off
= ctx
->stack_top
;
2947 for(i
= 0; i
< frame
->function
->var_cnt
; i
++) {
2948 hres
= stack_push(ctx
, jsval_undefined());
2950 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2955 frame
->pop_variables
= i
;
2957 hres
= scope_push(scope_chain
, variable_object
, to_disp(variable_object
), &scope
);
2959 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2963 for(i
= 0; i
< frame
->function
->func_cnt
; i
++) {
2964 if(frame
->function
->funcs
[i
].name
&& !frame
->function
->funcs
[i
].event_target
) {
2968 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+i
, scope
, &func_obj
);
2970 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2971 scope_release(scope
);
2975 off
= local_off(frame
, frame
->function
->funcs
[i
].local_ref
);
2976 jsval_release(ctx
->stack
[off
]);
2977 ctx
->stack
[off
] = jsval_obj(func_obj
);
2981 scope
->frame
= frame
;
2982 frame
->base_scope
= frame
->scope
= scope
;
2986 HRESULT
exec_source(script_ctx_t
*ctx
, DWORD flags
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
2987 IDispatch
*this_obj
, jsdisp_t
*function_instance
, jsdisp_t
*variable_obj
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
2989 call_frame_t
*frame
;
2993 if(!ctx
->ei
->enter_notified
) {
2994 ctx
->ei
->enter_notified
= TRUE
;
2995 IActiveScriptSite_OnEnterScript(ctx
->site
);
2998 for(i
= 0; i
< function
->func_cnt
; i
++) {
3001 if(!function
->funcs
[i
].event_target
)
3004 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+i
, scope
, &func_obj
);
3008 hres
= bind_event_target(ctx
, function
->funcs
+i
, func_obj
);
3009 jsdisp_release(func_obj
);
3014 if(flags
& (EXEC_GLOBAL
| EXEC_EVAL
)) {
3015 for(i
=0; i
< function
->var_cnt
; i
++) {
3016 TRACE("[%d] %s %d\n", i
, debugstr_w(function
->variables
[i
].name
), function
->variables
[i
].func_id
);
3017 if(function
->variables
[i
].func_id
!= -1) {
3020 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+function
->variables
[i
].func_id
, scope
, &func_obj
);
3024 hres
= jsdisp_propput_name(variable_obj
, function
->variables
[i
].name
, jsval_obj(func_obj
));
3025 jsdisp_release(func_obj
);
3026 }else if(!(flags
& EXEC_GLOBAL
) || !lookup_global_members(ctx
, function
->variables
[i
].name
, NULL
)) {
3029 hres
= jsdisp_get_id(variable_obj
, function
->variables
[i
].name
, fdexNameEnsure
, &id
);
3036 /* ECMA-262 3rd Edition 11.2.3.7 */
3040 jsthis
= iface_to_jsdisp(this_obj
);
3042 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
3044 jsdisp_release(jsthis
);
3048 if(ctx
->call_ctx
&& (flags
& EXEC_EVAL
)) {
3049 hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
3054 frame
= heap_alloc_zero(sizeof(*frame
));
3056 return E_OUTOFMEMORY
;
3058 frame
->function
= function
;
3059 frame
->ret
= jsval_undefined();
3061 frame
->bytecode
= bytecode_addref(bytecode
);
3063 if(!(flags
& (EXEC_GLOBAL
|EXEC_EVAL
))) {
3064 hres
= setup_scope(ctx
, frame
, scope
, variable_obj
, argc
, argv
);
3066 release_bytecode(frame
->bytecode
);
3071 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
3074 frame
->ip
= function
->instr_off
;
3075 frame
->stack_base
= ctx
->stack_top
;
3077 frame
->this_obj
= this_obj
;
3078 IDispatch_AddRef(frame
->this_obj
);
3081 if(function_instance
)
3082 frame
->function_instance
= jsdisp_addref(function_instance
);
3084 frame
->flags
= flags
;
3085 frame
->variable_obj
= jsdisp_addref(variable_obj
);
3087 frame
->prev_frame
= ctx
->call_ctx
;
3088 ctx
->call_ctx
= frame
;
3090 if(flags
& EXEC_RETURN_TO_INTERP
) {
3092 * We're called directly from interpreter, so we may just setup call frame and return.
3093 * Already running interpreter will take care of execution.
3096 *r
= jsval_undefined();
3100 return enter_bytecode(ctx
, r
);