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
20 #include "wine/port.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
32 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
33 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
34 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
35 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
36 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
37 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
38 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
40 struct _except_frame_t
{
67 static HRESULT
stack_push(script_ctx_t
*ctx
, jsval_t v
)
69 if(!ctx
->stack_size
) {
70 ctx
->stack
= heap_alloc(16*sizeof(*ctx
->stack
));
74 }else if(ctx
->stack_size
== ctx
->stack_top
) {
77 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*new_stack
));
83 ctx
->stack
= new_stack
;
87 ctx
->stack
[ctx
->stack_top
++] = v
;
91 static inline HRESULT
stack_push_string(script_ctx_t
*ctx
, const WCHAR
*str
)
99 return stack_push(ctx
, jsval_string(v
));
102 static inline jsval_t
stack_top(script_ctx_t
*ctx
)
104 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
105 return ctx
->stack
[ctx
->stack_top
-1];
108 static inline jsval_t
*stack_top_ref(script_ctx_t
*ctx
, unsigned n
)
110 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
);
111 return ctx
->stack
+ctx
->stack_top
-1-n
;
114 static inline jsval_t
stack_topn(script_ctx_t
*ctx
, unsigned n
)
116 return *stack_top_ref(ctx
, n
);
119 static inline jsval_t
*stack_args(script_ctx_t
*ctx
, unsigned n
)
123 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
-1);
124 return ctx
->stack
+ ctx
->stack_top
-n
;
127 static inline jsval_t
stack_pop(script_ctx_t
*ctx
)
129 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
130 return ctx
->stack
[--ctx
->stack_top
];
133 static void stack_popn(script_ctx_t
*ctx
, unsigned n
)
136 jsval_release(stack_pop(ctx
));
139 static HRESULT
stack_pop_number(script_ctx_t
*ctx
, double *r
)
145 hres
= to_number(ctx
, v
, r
);
150 static HRESULT
stack_pop_object(script_ctx_t
*ctx
, IDispatch
**r
)
156 if(is_object_instance(v
)) {
158 return throw_type_error(ctx
, JS_E_OBJECT_REQUIRED
, NULL
);
163 hres
= to_object(ctx
, v
, r
);
168 static inline HRESULT
stack_pop_int(script_ctx_t
*ctx
, INT
*r
)
170 return to_int32(ctx
, stack_pop(ctx
), r
);
173 static inline HRESULT
stack_pop_uint(script_ctx_t
*ctx
, DWORD
*r
)
175 return to_uint32(ctx
, stack_pop(ctx
), r
);
178 static inline unsigned local_off(call_frame_t
*frame
, int ref
)
181 ? frame
->arguments_off
- ref
-1
182 : frame
->variables_off
+ ref
;
185 static inline BSTR
local_name(call_frame_t
*frame
, int ref
)
187 return ref
< 0 ? frame
->function
->params
[-ref
-1] : frame
->function
->variables
[ref
].name
;
190 /* Steals input reference even on failure. */
191 static HRESULT
stack_push_exprval(script_ctx_t
*ctx
, exprval_t
*val
)
199 hres
= stack_push(ctx
, jsval_disp(val
->u
.idref
.disp
));
201 hres
= stack_push(ctx
, jsval_number(val
->u
.idref
.id
));
203 IDispatch_Release(val
->u
.idref
.disp
);
205 case EXPRVAL_STACK_REF
:
206 hres
= stack_push(ctx
, jsval_number(val
->u
.off
));
208 hres
= stack_push(ctx
, jsval_undefined());
210 case EXPRVAL_INVALID
:
211 hres
= stack_push(ctx
, jsval_undefined());
213 hres
= stack_push(ctx
, jsval_number(val
->u
.hres
));
221 static BOOL
stack_topn_exprval(script_ctx_t
*ctx
, unsigned n
, exprval_t
*r
)
223 jsval_t v
= stack_topn(ctx
, n
+1);
225 switch(jsval_type(v
)) {
227 call_frame_t
*frame
= ctx
->call_ctx
;
228 unsigned off
= get_number(v
);
230 if(!frame
->base_scope
->frame
&& off
>= frame
->arguments_off
) {
235 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
237 assert(off
< frame
->variables_off
+ frame
->function
->var_cnt
);
238 name
= off
>= frame
->variables_off
239 ? frame
->function
->variables
[off
- frame
->variables_off
].name
240 : frame
->function
->params
[off
- frame
->arguments_off
];
241 hres
= jsdisp_get_id(ctx
->call_ctx
->base_scope
->jsobj
, name
, 0, &id
);
243 r
->type
= EXPRVAL_INVALID
;
248 *stack_top_ref(ctx
, n
+1) = jsval_obj(jsdisp_addref(frame
->base_scope
->jsobj
));
249 *stack_top_ref(ctx
, n
) = jsval_number(id
);
250 r
->type
= EXPRVAL_IDREF
;
251 r
->u
.idref
.disp
= frame
->base_scope
->obj
;
256 r
->type
= EXPRVAL_STACK_REF
;
261 r
->type
= EXPRVAL_IDREF
;
262 r
->u
.idref
.disp
= get_object(v
);
263 assert(is_number(stack_topn(ctx
, n
)));
264 r
->u
.idref
.id
= get_number(stack_topn(ctx
, n
));
267 r
->type
= EXPRVAL_INVALID
;
268 assert(is_number(stack_topn(ctx
, n
)));
269 r
->u
.hres
= get_number(stack_topn(ctx
, n
));
277 static inline BOOL
stack_pop_exprval(script_ctx_t
*ctx
, exprval_t
*r
)
279 BOOL ret
= stack_topn_exprval(ctx
, 0, r
);
284 static HRESULT
exprval_propput(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t v
)
287 case EXPRVAL_STACK_REF
: {
288 jsval_t
*r
= ctx
->stack
+ ref
->u
.off
;
290 return jsval_copy(v
, r
);
293 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
);
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
);
313 static HRESULT
exprval_call(script_ctx_t
*ctx
, exprval_t
*ref
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
316 case EXPRVAL_STACK_REF
: {
317 jsval_t v
= ctx
->stack
[ref
->u
.off
];
319 if(!is_object_instance(v
)) {
320 FIXME("invoke %s\n", debugstr_jsval(v
));
324 return disp_call_value(ctx
, get_object(v
), NULL
, flags
, argc
, argv
, r
);
327 return disp_call(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, flags
, argc
, argv
, r
);
334 /* ECMA-262 3rd Edition 8.7.1 */
335 /* Steals input reference. */
336 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
340 if(ref
->type
== EXPRVAL_JSVAL
) {
345 hres
= exprval_propget(ctx
, ref
, r
);
347 if(ref
->type
== EXPRVAL_IDREF
)
348 IDispatch_Release(ref
->u
.idref
.disp
);
352 static void exprval_release(exprval_t
*val
)
356 jsval_release(val
->u
.val
);
359 if(val
->u
.idref
.disp
)
360 IDispatch_Release(val
->u
.idref
.disp
);
362 case EXPRVAL_STACK_REF
:
363 case EXPRVAL_INVALID
:
368 static inline void exprval_set_exception(exprval_t
*val
, HRESULT hres
)
370 val
->type
= EXPRVAL_INVALID
;
374 static inline void exprval_set_disp_ref(exprval_t
*ref
, IDispatch
*obj
, DISPID id
)
376 ref
->type
= EXPRVAL_IDREF
;
377 #ifdef __REACTOS__ /* FIXME: Inspect */
378 IDispatch_AddRef(obj
);
379 ref
->u
.idref
.disp
= obj
;
381 IDispatch_AddRef(ref
->u
.idref
.disp
= obj
);
383 ref
->u
.idref
.id
= id
;
386 static inline jsval_t
steal_ret(call_frame_t
*frame
)
388 jsval_t r
= frame
->ret
;
389 frame
->ret
= jsval_undefined();
393 static inline void clear_acc(script_ctx_t
*ctx
)
395 jsval_release(ctx
->acc
);
396 ctx
->acc
= jsval_undefined();
399 static HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
401 scope_chain_t
*new_scope
;
403 new_scope
= heap_alloc(sizeof(scope_chain_t
));
405 return E_OUTOFMEMORY
;
409 IDispatch_AddRef(obj
);
410 new_scope
->jsobj
= jsobj
;
411 new_scope
->obj
= obj
;
412 new_scope
->frame
= NULL
;
413 new_scope
->next
= scope
? scope_addref(scope
) : NULL
;
419 static void scope_pop(scope_chain_t
**scope
)
428 void clear_ei(script_ctx_t
*ctx
)
430 memset(&ctx
->ei
.ei
, 0, sizeof(ctx
->ei
.ei
));
431 jsval_release(ctx
->ei
.val
);
432 ctx
->ei
.val
= jsval_undefined();
435 void scope_release(scope_chain_t
*scope
)
441 scope_release(scope
->next
);
443 IDispatch_Release(scope
->obj
);
447 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
454 jsdisp
= iface_to_jsdisp(disp
);
456 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
457 jsdisp_release(jsdisp
);
464 bstr
= SysAllocString(name
);
466 return E_OUTOFMEMORY
;
470 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
471 if(SUCCEEDED(hres
)) {
472 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
473 IDispatchEx_Release(dispex
);
475 TRACE("using IDispatch\n");
476 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
479 if(name_bstr
!= bstr
)
484 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
486 IObjectIdentity
*identity
;
487 IUnknown
*unk1
, *unk2
;
495 if(!disp1
|| !disp2
) {
500 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
504 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
506 IUnknown_Release(unk1
);
513 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
514 if(SUCCEEDED(hres
)) {
515 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
516 IObjectIdentity_Release(identity
);
523 IUnknown_Release(unk1
);
524 IUnknown_Release(unk2
);
528 /* ECMA-262 3rd Edition 11.9.6 */
529 HRESULT
jsval_strict_equal(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
531 jsval_type_t type
= jsval_type(lval
);
535 if(type
!= jsval_type(rval
)) {
536 if(is_null_instance(lval
))
537 *ret
= is_null_instance(rval
);
549 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
551 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
554 *ret
= get_number(lval
) == get_number(rval
);
557 *ret
= !get_bool(lval
) == !get_bool(rval
);
560 FIXME("VARIANT not implemented\n");
568 * Transfers local variables from stack to variable object.
569 * It's slow, so we want to avoid it as much as possible.
571 static HRESULT
detach_variable_object(script_ctx_t
*ctx
, call_frame_t
*frame
, BOOL from_release
)
576 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
579 TRACE("detaching %p\n", frame
);
581 assert(frame
== frame
->base_scope
->frame
);
582 assert(frame
->variable_obj
== frame
->base_scope
->jsobj
);
584 if(!from_release
&& !frame
->arguments_obj
) {
585 hres
= setup_arguments_object(ctx
, frame
);
590 frame
->base_scope
->frame
= NULL
;
592 for(i
= 0; i
< frame
->function
->locals_cnt
; i
++) {
593 hres
= jsdisp_propput_name(frame
->variable_obj
, frame
->function
->locals
[i
].name
,
594 ctx
->stack
[local_off(frame
, frame
->function
->locals
[i
].ref
)]);
602 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
608 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
609 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
610 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
611 if(SUCCEEDED(hres
)) {
613 exprval_set_disp_ref(ret
, item
->disp
, id
);
622 static int local_ref_cmp(const void *key
, const void *ref
)
624 return strcmpW((const WCHAR
*)key
, ((const local_ref_t
*)ref
)->name
);
627 local_ref_t
*lookup_local(const function_code_t
*function
, const WCHAR
*identifier
)
629 return bsearch(identifier
, function
->locals
, function
->locals_cnt
, sizeof(*function
->locals
), local_ref_cmp
);
632 /* ECMA-262 3rd Edition 10.1.4 */
633 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
635 scope_chain_t
*scope
;
640 TRACE("%s\n", debugstr_w(identifier
));
643 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
645 function_code_t
*func
= scope
->frame
->function
;
646 local_ref_t
*ref
= lookup_local(func
, identifier
);
647 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
650 ret
->type
= EXPRVAL_STACK_REF
;
651 ret
->u
.off
= local_off(scope
->frame
, ref
->ref
);
652 TRACE("returning ref %d for %d\n", ret
->u
.off
, ref
->ref
);
656 if(!strcmpW(identifier
, argumentsW
)) {
657 hres
= detach_variable_object(ctx
, scope
->frame
, FALSE
);
663 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
665 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
666 if(SUCCEEDED(hres
)) {
667 exprval_set_disp_ref(ret
, scope
->obj
, id
);
673 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
674 if(SUCCEEDED(hres
)) {
675 exprval_set_disp_ref(ret
, to_disp(ctx
->global
), id
);
679 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
680 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
687 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
688 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
690 WARN("GetItemInfo failed: %08x\n", hres
);
694 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
695 IUnknown_Release(unk
);
697 WARN("object does not implement IDispatch\n");
702 IDispatch_AddRef(item
->disp
);
703 ret
->type
= EXPRVAL_JSVAL
;
704 ret
->u
.val
= jsval_disp(item
->disp
);
709 if(lookup_global_members(ctx
, identifier
, ret
))
712 exprval_set_exception(ret
, JS_E_UNDEFINED_VARIABLE
);
716 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
718 call_frame_t
*frame
= ctx
->call_ctx
;
719 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
722 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
724 call_frame_t
*frame
= ctx
->call_ctx
;
725 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
728 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
730 call_frame_t
*frame
= ctx
->call_ctx
;
731 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
734 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
736 call_frame_t
*frame
= ctx
->call_ctx
;
737 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
740 static inline double get_op_double(script_ctx_t
*ctx
)
742 call_frame_t
*frame
= ctx
->call_ctx
;
743 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
746 static inline void jmp_next(script_ctx_t
*ctx
)
751 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
753 ctx
->call_ctx
->ip
= dst
;
756 /* ECMA-262 3rd Edition 12.6.4 */
757 static HRESULT
interp_forin(script_ctx_t
*ctx
)
759 const HRESULT arg
= get_op_uint(ctx
, 0);
760 IDispatch
*obj
= NULL
;
769 assert(is_number(stack_top(ctx
)));
770 id
= get_number(stack_top(ctx
));
772 if(!stack_topn_exprval(ctx
, 1, &prop_ref
)) {
773 FIXME("invalid ref: %08x\n", prop_ref
.u
.hres
);
777 if(is_object_instance(stack_topn(ctx
, 3)))
778 obj
= get_object(stack_topn(ctx
, 3));
781 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
782 if(SUCCEEDED(hres
)) {
783 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
785 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
786 IDispatchEx_Release(dispex
);
790 TRACE("No IDispatchEx\n");
797 str
= jsstr_alloc_len(name
, SysStringLen(name
));
800 return E_OUTOFMEMORY
;
803 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
805 hres
= exprval_propput(ctx
, &prop_ref
, jsval_string(str
));
818 /* ECMA-262 3rd Edition 12.10 */
819 static HRESULT
interp_push_scope(script_ctx_t
*ctx
)
828 hres
= to_object(ctx
, v
, &disp
);
833 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
834 IDispatch_Release(disp
);
838 /* ECMA-262 3rd Edition 12.10 */
839 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
843 scope_pop(&ctx
->call_ctx
->scope
);
847 /* ECMA-262 3rd Edition 12.13 */
848 static HRESULT
interp_case(script_ctx_t
*ctx
)
850 const unsigned arg
= get_op_uint(ctx
, 0);
858 hres
= jsval_strict_equal(stack_top(ctx
), v
, &b
);
872 /* ECMA-262 3rd Edition 12.13 */
873 static HRESULT
interp_throw(script_ctx_t
*ctx
)
877 jsval_release(ctx
->ei
.val
);
878 ctx
->ei
.val
= stack_pop(ctx
);
879 return DISP_E_EXCEPTION
;
882 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
884 const HRESULT arg
= get_op_uint(ctx
, 0);
886 TRACE("%08x\n", arg
);
888 return throw_reference_error(ctx
, arg
, NULL
);
891 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
893 const HRESULT hres
= get_op_uint(ctx
, 0);
894 jsstr_t
*str
= get_op_str(ctx
, 1);
897 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
899 ptr
= jsstr_flatten(str
);
900 return ptr
? throw_type_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
903 /* ECMA-262 3rd Edition 12.14 */
904 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
906 const unsigned catch_off
= get_op_uint(ctx
, 0);
907 const unsigned finally_off
= get_op_uint(ctx
, 1);
908 call_frame_t
*frame
= ctx
->call_ctx
;
909 except_frame_t
*except
;
913 except
= heap_alloc(sizeof(*except
));
915 return E_OUTOFMEMORY
;
917 except
->stack_top
= ctx
->stack_top
;
918 except
->scope
= frame
->scope
;
919 except
->catch_off
= catch_off
;
920 except
->finally_off
= finally_off
;
921 except
->next
= frame
->except_frame
;
922 frame
->except_frame
= except
;
926 /* ECMA-262 3rd Edition 12.14 */
927 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
929 const unsigned ret_off
= get_op_uint(ctx
, 0);
930 call_frame_t
*frame
= ctx
->call_ctx
;
931 except_frame_t
*except
;
932 unsigned finally_off
;
934 TRACE("%u\n", ret_off
);
936 except
= frame
->except_frame
;
937 assert(except
!= NULL
);
939 finally_off
= except
->finally_off
;
940 frame
->except_frame
= except
->next
;
946 hres
= stack_push(ctx
, jsval_number(ret_off
));
949 hres
= stack_push(ctx
, jsval_bool(TRUE
));
952 frame
->ip
= finally_off
;
960 /* ECMA-262 3rd Edition 12.14 */
961 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
963 call_frame_t
*frame
= ctx
->call_ctx
;
972 TRACE("passing exception\n");
974 ctx
->ei
.val
= stack_pop(ctx
);
975 return DISP_E_EXCEPTION
;
979 assert(is_number(v
));
980 frame
->ip
= get_number(v
);
984 static HRESULT
interp_enter_catch(script_ctx_t
*ctx
)
986 const BSTR ident
= get_op_bstr(ctx
, 0);
991 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
996 hres
= jsdisp_propput_name(scope_obj
, ident
, v
);
999 hres
= scope_push(ctx
->call_ctx
->scope
, scope_obj
, to_disp(scope_obj
), &ctx
->call_ctx
->scope
);
1000 jsdisp_release(scope_obj
);
1004 /* ECMA-262 3rd Edition 13 */
1005 static HRESULT
interp_func(script_ctx_t
*ctx
)
1007 unsigned func_idx
= get_op_uint(ctx
, 0);
1008 call_frame_t
*frame
= ctx
->call_ctx
;
1012 TRACE("%d\n", func_idx
);
1014 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
1015 frame
->scope
, &dispex
);
1019 return stack_push(ctx
, jsval_obj(dispex
));
1022 /* ECMA-262 3rd Edition 11.2.1 */
1023 static HRESULT
interp_array(script_ctx_t
*ctx
)
1034 namev
= stack_pop(ctx
);
1036 hres
= stack_pop_object(ctx
, &obj
);
1038 jsval_release(namev
);
1042 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1043 jsval_release(namev
);
1045 IDispatch_Release(obj
);
1049 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
1050 jsstr_release(name_str
);
1051 if(SUCCEEDED(hres
)) {
1052 hres
= disp_propget(ctx
, obj
, id
, &v
);
1053 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1054 v
= jsval_undefined();
1057 IDispatch_Release(obj
);
1061 return stack_push(ctx
, v
);
1064 /* ECMA-262 3rd Edition 11.2.1 */
1065 static HRESULT
interp_member(script_ctx_t
*ctx
)
1067 const BSTR arg
= get_op_bstr(ctx
, 0);
1075 hres
= stack_pop_object(ctx
, &obj
);
1079 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
1080 if(SUCCEEDED(hres
)) {
1081 hres
= disp_propget(ctx
, obj
, id
, &v
);
1082 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1083 v
= jsval_undefined();
1086 IDispatch_Release(obj
);
1090 return stack_push(ctx
, v
);
1093 /* ECMA-262 3rd Edition 11.2.1 */
1094 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
1096 const unsigned arg
= get_op_uint(ctx
, 0);
1097 jsval_t objv
, namev
;
1107 namev
= stack_pop(ctx
);
1108 objv
= stack_pop(ctx
);
1110 hres
= to_object(ctx
, objv
, &obj
);
1111 jsval_release(objv
);
1112 if(SUCCEEDED(hres
)) {
1113 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1115 IDispatch_Release(obj
);
1117 jsval_release(namev
);
1121 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
1122 jsstr_release(name_str
);
1123 if(SUCCEEDED(hres
)) {
1124 ref
.type
= EXPRVAL_IDREF
;
1125 ref
.u
.idref
.disp
= obj
;
1126 ref
.u
.idref
.id
= id
;
1128 IDispatch_Release(obj
);
1129 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1130 exprval_set_exception(&ref
, JS_E_INVALID_PROPERTY
);
1133 ERR("failed %08x\n", hres
);
1138 return stack_push_exprval(ctx
, &ref
);
1141 /* ECMA-262 3rd Edition 11.2.1 */
1142 static HRESULT
interp_refval(script_ctx_t
*ctx
)
1150 if(!stack_topn_exprval(ctx
, 0, &ref
))
1151 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
1153 hres
= exprval_propget(ctx
, &ref
, &v
);
1157 return stack_push(ctx
, v
);
1160 /* ECMA-262 3rd Edition 11.2.2 */
1161 static HRESULT
interp_new(script_ctx_t
*ctx
)
1163 const unsigned argc
= get_op_uint(ctx
, 0);
1166 TRACE("%d\n", argc
);
1168 constr
= stack_topn(ctx
, argc
);
1170 /* NOTE: Should use to_object here */
1173 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1174 else if(!is_object_instance(constr
))
1175 return throw_type_error(ctx
, JS_E_INVALID_ACTION
, NULL
);
1176 else if(!get_object(constr
))
1177 return throw_type_error(ctx
, JS_E_INVALID_PROPERTY
, NULL
);
1180 return disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1181 argc
, stack_args(ctx
, argc
), &ctx
->acc
);
1184 /* ECMA-262 3rd Edition 11.2.3 */
1185 static HRESULT
interp_call(script_ctx_t
*ctx
)
1187 const unsigned argn
= get_op_uint(ctx
, 0);
1188 const int do_ret
= get_op_int(ctx
, 1);
1191 TRACE("%d %d\n", argn
, do_ret
);
1193 obj
= stack_topn(ctx
, argn
);
1194 if(!is_object_instance(obj
))
1195 return throw_type_error(ctx
, JS_E_INVALID_PROPERTY
, NULL
);
1198 return disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1199 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1202 /* ECMA-262 3rd Edition 11.2.3 */
1203 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
1205 const unsigned argn
= get_op_uint(ctx
, 0);
1206 const int do_ret
= get_op_int(ctx
, 1);
1209 TRACE("%d %d\n", argn
, do_ret
);
1211 if(!stack_topn_exprval(ctx
, argn
, &ref
))
1212 return throw_type_error(ctx
, ref
.u
.hres
, NULL
);
1215 return exprval_call(ctx
, &ref
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1216 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1219 /* ECMA-262 3rd Edition 11.1.1 */
1220 static HRESULT
interp_this(script_ctx_t
*ctx
)
1222 call_frame_t
*frame
= ctx
->call_ctx
;
1226 IDispatch_AddRef(frame
->this_obj
);
1227 return stack_push(ctx
, jsval_disp(frame
->this_obj
));
1230 static HRESULT
interp_identifier_ref(script_ctx_t
*ctx
, BSTR identifier
, unsigned flags
)
1235 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1239 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1242 hres
= jsdisp_get_id(ctx
->global
, identifier
, fdexNameEnsure
, &id
);
1246 exprval_set_disp_ref(&exprval
, to_disp(ctx
->global
), id
);
1249 if(exprval
.type
== EXPRVAL_JSVAL
|| exprval
.type
== EXPRVAL_INVALID
) {
1250 WARN("invalid ref\n");
1251 exprval_release(&exprval
);
1252 exprval_set_exception(&exprval
, JS_E_OBJECT_EXPECTED
);
1255 return stack_push_exprval(ctx
, &exprval
);
1258 static HRESULT
identifier_value(script_ctx_t
*ctx
, BSTR identifier
)
1264 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1268 if(exprval
.type
== EXPRVAL_INVALID
)
1269 return throw_type_error(ctx
, exprval
.u
.hres
, identifier
);
1271 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1275 return stack_push(ctx
, v
);
1278 static HRESULT
interp_local_ref(script_ctx_t
*ctx
)
1280 const int arg
= get_op_int(ctx
, 0);
1281 const unsigned flags
= get_op_uint(ctx
, 1);
1282 call_frame_t
*frame
= ctx
->call_ctx
;
1287 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1288 return interp_identifier_ref(ctx
, local_name(frame
, arg
), flags
);
1290 ref
.type
= EXPRVAL_STACK_REF
;
1291 ref
.u
.off
= local_off(frame
, arg
);
1292 return stack_push_exprval(ctx
, &ref
);
1295 static HRESULT
interp_local(script_ctx_t
*ctx
)
1297 const int arg
= get_op_int(ctx
, 0);
1298 call_frame_t
*frame
= ctx
->call_ctx
;
1302 TRACE("%d: %s\n", arg
, debugstr_w(local_name(frame
, arg
)));
1304 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1305 return identifier_value(ctx
, local_name(frame
, arg
));
1307 hres
= jsval_copy(ctx
->stack
[local_off(frame
, arg
)], ©
);
1311 return stack_push(ctx
, copy
);
1314 /* ECMA-262 3rd Edition 10.1.4 */
1315 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1317 const BSTR arg
= get_op_bstr(ctx
, 0);
1319 TRACE("%s\n", debugstr_w(arg
));
1321 return identifier_value(ctx
, arg
);
1324 /* ECMA-262 3rd Edition 10.1.4 */
1325 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1327 const BSTR arg
= get_op_bstr(ctx
, 0);
1328 const unsigned flags
= get_op_uint(ctx
, 1);
1330 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1332 return interp_identifier_ref(ctx
, arg
, flags
);
1335 /* ECMA-262 3rd Edition 7.8.1 */
1336 static HRESULT
interp_null(script_ctx_t
*ctx
)
1340 return stack_push(ctx
, jsval_null());
1343 /* ECMA-262 3rd Edition 7.8.2 */
1344 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1346 const int arg
= get_op_int(ctx
, 0);
1348 TRACE("%s\n", arg
? "true" : "false");
1350 return stack_push(ctx
, jsval_bool(arg
));
1353 /* ECMA-262 3rd Edition 7.8.3 */
1354 static HRESULT
interp_int(script_ctx_t
*ctx
)
1356 const int arg
= get_op_int(ctx
, 0);
1360 return stack_push(ctx
, jsval_number(arg
));
1363 /* ECMA-262 3rd Edition 7.8.3 */
1364 static HRESULT
interp_double(script_ctx_t
*ctx
)
1366 const double arg
= get_op_double(ctx
);
1368 TRACE("%lf\n", arg
);
1370 return stack_push(ctx
, jsval_number(arg
));
1373 /* ECMA-262 3rd Edition 7.8.4 */
1374 static HRESULT
interp_str(script_ctx_t
*ctx
)
1376 jsstr_t
*str
= get_op_str(ctx
, 0);
1378 TRACE("%s\n", debugstr_jsstr(str
));
1380 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1383 /* ECMA-262 3rd Edition 7.8 */
1384 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1386 jsstr_t
*source
= get_op_str(ctx
, 0);
1387 const unsigned flags
= get_op_uint(ctx
, 1);
1391 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1393 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1397 return stack_push(ctx
, jsval_obj(regexp
));
1400 /* ECMA-262 3rd Edition 11.1.4 */
1401 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1403 const unsigned arg
= get_op_uint(ctx
, 0);
1409 hres
= create_array(ctx
, arg
, &array
);
1413 return stack_push(ctx
, jsval_obj(array
));
1416 static HRESULT
interp_carray_set(script_ctx_t
*ctx
)
1418 const unsigned index
= get_op_uint(ctx
, 0);
1419 jsval_t value
, array
;
1422 value
= stack_pop(ctx
);
1424 TRACE("[%u] = %s\n", index
, debugstr_jsval(value
));
1426 array
= stack_top(ctx
);
1427 assert(is_object_instance(array
));
1429 hres
= jsdisp_propput_idx(iface_to_jsdisp(get_object(array
)), index
, value
);
1430 jsval_release(value
);
1434 /* ECMA-262 3rd Edition 11.1.5 */
1435 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1442 hres
= create_object(ctx
, NULL
, &obj
);
1446 return stack_push(ctx
, jsval_obj(obj
));
1449 /* ECMA-262 3rd Edition 11.1.5 */
1450 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1452 const BSTR name
= get_op_bstr(ctx
, 0);
1453 unsigned type
= get_op_uint(ctx
, 1);
1458 TRACE("%s\n", debugstr_w(name
));
1460 val
= stack_pop(ctx
);
1462 assert(is_object_instance(stack_top(ctx
)));
1463 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1465 if(type
== PROPERTY_DEFINITION_VALUE
) {
1466 hres
= jsdisp_propput_name(obj
, name
, val
);
1468 property_desc_t desc
= {PROPF_ENUMERABLE
| PROPF_CONFIGURABLE
};
1471 assert(is_object_instance(val
));
1472 func
= iface_to_jsdisp(get_object(val
));
1474 desc
.mask
= desc
.flags
;
1475 if(type
== PROPERTY_DEFINITION_GETTER
) {
1476 desc
.explicit_getter
= TRUE
;
1479 desc
.explicit_setter
= TRUE
;
1483 hres
= jsdisp_define_property(obj
, name
, &desc
);
1484 jsdisp_release(func
);
1491 /* ECMA-262 3rd Edition 11.11 */
1492 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1494 const unsigned arg
= get_op_uint(ctx
, 0);
1500 hres
= to_boolean(stack_top(ctx
), &b
);
1513 /* ECMA-262 3rd Edition 11.11 */
1514 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1516 const unsigned arg
= get_op_uint(ctx
, 0);
1522 hres
= to_boolean(stack_top(ctx
), &b
);
1535 /* ECMA-262 3rd Edition 11.10 */
1536 static HRESULT
interp_or(script_ctx_t
*ctx
)
1543 hres
= stack_pop_int(ctx
, &r
);
1547 hres
= stack_pop_int(ctx
, &l
);
1551 return stack_push(ctx
, jsval_number(l
|r
));
1554 /* ECMA-262 3rd Edition 11.10 */
1555 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1562 hres
= stack_pop_int(ctx
, &r
);
1566 hres
= stack_pop_int(ctx
, &l
);
1570 return stack_push(ctx
, jsval_number(l
^r
));
1573 /* ECMA-262 3rd Edition 11.10 */
1574 static HRESULT
interp_and(script_ctx_t
*ctx
)
1581 hres
= stack_pop_int(ctx
, &r
);
1585 hres
= stack_pop_int(ctx
, &l
);
1589 return stack_push(ctx
, jsval_number(l
&r
));
1592 /* ECMA-262 3rd Edition 11.8.6 */
1593 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1595 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1600 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1603 if(!is_object_instance(v
) || !get_object(v
)) {
1605 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1608 obj
= iface_to_jsdisp(get_object(v
));
1609 IDispatch_Release(get_object(v
));
1611 FIXME("non-jsdisp objects not supported\n");
1615 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1616 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1618 hres
= throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1620 jsdisp_release(obj
);
1626 if(is_object_instance(prot
)) {
1627 if(is_object_instance(v
))
1628 tmp
= iface_to_jsdisp(get_object(v
));
1629 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1630 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1636 jsdisp_release(tmp
);
1638 FIXME("prototype is not an object\n");
1642 jsval_release(prot
);
1647 return stack_push(ctx
, jsval_bool(ret
));
1650 /* ECMA-262 3rd Edition 11.8.7 */
1651 static HRESULT
interp_in(script_ctx_t
*ctx
)
1662 obj
= stack_pop(ctx
);
1663 if(!is_object_instance(obj
) || !get_object(obj
)) {
1665 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1669 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1672 IDispatch_Release(get_object(obj
));
1676 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1677 IDispatch_Release(get_object(obj
));
1678 jsstr_release(jsstr
);
1681 else if(hres
== DISP_E_UNKNOWNNAME
)
1686 return stack_push(ctx
, jsval_bool(ret
));
1689 /* ECMA-262 3rd Edition 11.6.1 */
1690 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1695 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1699 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1705 if(is_string(l
) || is_string(r
)) {
1706 jsstr_t
*lstr
, *rstr
= NULL
;
1708 hres
= to_string(ctx
, l
, &lstr
);
1710 hres
= to_string(ctx
, r
, &rstr
);
1712 if(SUCCEEDED(hres
)) {
1715 ret_str
= jsstr_concat(lstr
, rstr
);
1717 *ret
= jsval_string(ret_str
);
1719 hres
= E_OUTOFMEMORY
;
1722 jsstr_release(lstr
);
1724 jsstr_release(rstr
);
1728 hres
= to_number(ctx
, l
, &nl
);
1729 if(SUCCEEDED(hres
)) {
1730 hres
= to_number(ctx
, r
, &nr
);
1732 *ret
= jsval_number(nl
+nr
);
1741 /* ECMA-262 3rd Edition 11.6.1 */
1742 static HRESULT
interp_add(script_ctx_t
*ctx
)
1750 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1752 hres
= add_eval(ctx
, l
, r
, &ret
);
1758 return stack_push(ctx
, ret
);
1761 /* ECMA-262 3rd Edition 11.6.2 */
1762 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1769 hres
= stack_pop_number(ctx
, &r
);
1773 hres
= stack_pop_number(ctx
, &l
);
1777 return stack_push(ctx
, jsval_number(l
-r
));
1780 /* ECMA-262 3rd Edition 11.5.1 */
1781 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1788 hres
= stack_pop_number(ctx
, &r
);
1792 hres
= stack_pop_number(ctx
, &l
);
1796 return stack_push(ctx
, jsval_number(l
*r
));
1799 /* ECMA-262 3rd Edition 11.5.2 */
1800 static HRESULT
interp_div(script_ctx_t
*ctx
)
1807 hres
= stack_pop_number(ctx
, &r
);
1811 hres
= stack_pop_number(ctx
, &l
);
1815 return stack_push(ctx
, jsval_number(l
/r
));
1818 /* ECMA-262 3rd Edition 11.5.3 */
1819 static HRESULT
interp_mod(script_ctx_t
*ctx
)
1826 hres
= stack_pop_number(ctx
, &r
);
1830 hres
= stack_pop_number(ctx
, &l
);
1834 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1837 /* ECMA-262 3rd Edition 11.4.2 */
1838 static HRESULT
interp_delete(script_ctx_t
*ctx
)
1840 jsval_t objv
, namev
;
1848 namev
= stack_pop(ctx
);
1849 objv
= stack_pop(ctx
);
1851 hres
= to_object(ctx
, objv
, &obj
);
1852 jsval_release(objv
);
1854 jsval_release(namev
);
1858 hres
= to_string(ctx
, namev
, &name
);
1859 jsval_release(namev
);
1861 IDispatch_Release(obj
);
1865 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
1866 IDispatch_Release(obj
);
1867 jsstr_release(name
);
1871 return stack_push(ctx
, jsval_bool(ret
));
1874 /* ECMA-262 3rd Edition 11.4.2 */
1875 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
1877 const BSTR arg
= get_op_bstr(ctx
, 0);
1882 TRACE("%s\n", debugstr_w(arg
));
1884 hres
= identifier_eval(ctx
, arg
, &exprval
);
1888 switch(exprval
.type
) {
1889 case EXPRVAL_STACK_REF
:
1893 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1894 IDispatch_Release(exprval
.u
.idref
.disp
);
1898 case EXPRVAL_INVALID
:
1902 FIXME("Unsupported exprval\n");
1903 exprval_release(&exprval
);
1908 return stack_push(ctx
, jsval_bool(ret
));
1911 /* ECMA-262 3rd Edition 11.4.2 */
1912 static HRESULT
interp_void(script_ctx_t
*ctx
)
1917 return stack_push(ctx
, jsval_undefined());
1920 /* ECMA-262 3rd Edition 11.4.3 */
1921 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1923 switch(jsval_type(v
)) {
1933 if(get_object(v
) && (dispex
= iface_to_jsdisp(get_object(v
)))) {
1934 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1935 jsdisp_release(dispex
);
1951 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1958 /* ECMA-262 3rd Edition 11.4.3 */
1959 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
1968 if(!stack_pop_exprval(ctx
, &ref
))
1969 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1971 hres
= exprval_propget(ctx
, &ref
, &v
);
1972 exprval_release(&ref
);
1974 return stack_push_string(ctx
, unknownW
);
1976 hres
= typeof_string(v
, &ret
);
1981 return stack_push_string(ctx
, ret
);
1984 /* ECMA-262 3rd Edition 11.4.3 */
1985 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
1987 const BSTR arg
= get_op_bstr(ctx
, 0);
1993 TRACE("%s\n", debugstr_w(arg
));
1995 hres
= identifier_eval(ctx
, arg
, &exprval
);
1999 if(exprval
.type
== EXPRVAL_INVALID
)
2000 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2002 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2006 hres
= typeof_string(v
, &ret
);
2011 return stack_push_string(ctx
, ret
);
2014 /* ECMA-262 3rd Edition 11.4.3 */
2015 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
2024 hres
= typeof_string(v
, &ret
);
2029 return stack_push_string(ctx
, ret
);
2032 /* ECMA-262 3rd Edition 11.4.7 */
2033 static HRESULT
interp_minus(script_ctx_t
*ctx
)
2040 hres
= stack_pop_number(ctx
, &n
);
2044 return stack_push(ctx
, jsval_number(-n
));
2047 /* ECMA-262 3rd Edition 11.4.6 */
2048 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
2057 hres
= to_number(ctx
, v
, &n
);
2062 return stack_push(ctx
, jsval_number(n
));
2065 /* ECMA-262 3rd Edition 11.3.1 */
2066 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
2068 const int arg
= get_op_int(ctx
, 0);
2075 if(!stack_pop_exprval(ctx
, &ref
))
2076 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
2078 hres
= exprval_propget(ctx
, &ref
, &v
);
2079 if(SUCCEEDED(hres
)) {
2082 hres
= to_number(ctx
, v
, &n
);
2084 hres
= exprval_propput(ctx
, &ref
, jsval_number(n
+(double)arg
));
2088 exprval_release(&ref
);
2092 return stack_push(ctx
, v
);
2095 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2096 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
2098 const int arg
= get_op_int(ctx
, 0);
2106 if(!stack_pop_exprval(ctx
, &ref
))
2107 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
2109 hres
= exprval_propget(ctx
, &ref
, &v
);
2110 if(SUCCEEDED(hres
)) {
2113 hres
= to_number(ctx
, v
, &n
);
2115 if(SUCCEEDED(hres
)) {
2116 ret
= n
+(double)arg
;
2117 hres
= exprval_propput(ctx
, &ref
, jsval_number(ret
));
2120 exprval_release(&ref
);
2124 return stack_push(ctx
, jsval_number(ret
));
2127 /* ECMA-262 3rd Edition 11.9.3 */
2128 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
2130 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
2131 return jsval_strict_equal(lval
, rval
, ret
);
2133 /* FIXME: NULL disps should be handled in more general way */
2134 if(is_object_instance(lval
) && !get_object(lval
))
2135 return equal_values(ctx
, jsval_null(), rval
, ret
);
2136 if(is_object_instance(rval
) && !get_object(rval
))
2137 return equal_values(ctx
, lval
, jsval_null(), ret
);
2139 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
2144 if(is_string(lval
) && is_number(rval
)) {
2148 hres
= to_number(ctx
, lval
, &n
);
2152 /* FIXME: optimize */
2153 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
2156 if(is_string(rval
) && is_number(lval
)) {
2160 hres
= to_number(ctx
, rval
, &n
);
2164 /* FIXME: optimize */
2165 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
2169 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
2172 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
2175 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
2179 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
2183 hres
= equal_values(ctx
, lval
, prim
, ret
);
2184 jsval_release(prim
);
2189 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
2193 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
2197 hres
= equal_values(ctx
, prim
, rval
, ret
);
2198 jsval_release(prim
);
2207 /* ECMA-262 3rd Edition 11.9.1 */
2208 static HRESULT
interp_eq(script_ctx_t
*ctx
)
2217 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2219 hres
= equal_values(ctx
, l
, r
, &b
);
2225 return stack_push(ctx
, jsval_bool(b
));
2228 /* ECMA-262 3rd Edition 11.9.2 */
2229 static HRESULT
interp_neq(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.4 */
2250 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
2259 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2261 hres
= jsval_strict_equal(r
, l
, &b
);
2267 return stack_push(ctx
, jsval_bool(b
));
2270 /* ECMA-262 3rd Edition 11.9.5 */
2271 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2282 hres
= jsval_strict_equal(r
, l
, &b
);
2288 return stack_push(ctx
, jsval_bool(!b
));
2291 /* ECMA-262 3rd Edition 11.8.5 */
2292 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2298 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2302 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2308 if(is_string(l
) && is_string(r
)) {
2309 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2310 jsstr_release(get_string(l
));
2311 jsstr_release(get_string(r
));
2315 hres
= to_number(ctx
, l
, &ln
);
2318 hres
= to_number(ctx
, r
, &rn
);
2323 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2327 /* ECMA-262 3rd Edition 11.8.1 */
2328 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2337 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2339 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2345 return stack_push(ctx
, jsval_bool(b
));
2348 /* ECMA-262 3rd Edition 11.8.1 */
2349 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2358 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2360 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2366 return stack_push(ctx
, jsval_bool(b
));
2369 /* ECMA-262 3rd Edition 11.8.2 */
2370 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2379 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2381 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2387 return stack_push(ctx
, jsval_bool(b
));
2390 /* ECMA-262 3rd Edition 11.8.4 */
2391 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2400 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2402 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2408 return stack_push(ctx
, jsval_bool(b
));
2411 /* ECMA-262 3rd Edition 11.4.8 */
2412 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2421 hres
= to_int32(ctx
, v
, &i
);
2426 return stack_push(ctx
, jsval_number(~i
));
2429 /* ECMA-262 3rd Edition 11.4.9 */
2430 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2439 hres
= to_boolean(v
, &b
);
2444 return stack_push(ctx
, jsval_bool(!b
));
2447 /* ECMA-262 3rd Edition 11.7.1 */
2448 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2454 hres
= stack_pop_uint(ctx
, &r
);
2458 hres
= stack_pop_int(ctx
, &l
);
2462 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2465 /* ECMA-262 3rd Edition 11.7.2 */
2466 static HRESULT
interp_rshift(script_ctx_t
*ctx
)
2472 hres
= stack_pop_uint(ctx
, &r
);
2476 hres
= stack_pop_int(ctx
, &l
);
2480 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2483 /* ECMA-262 3rd Edition 11.7.3 */
2484 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2489 hres
= stack_pop_uint(ctx
, &r
);
2493 hres
= stack_pop_uint(ctx
, &l
);
2497 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2500 /* ECMA-262 3rd Edition 11.13.1 */
2501 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2511 if(!stack_pop_exprval(ctx
, &ref
)) {
2513 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2516 hres
= exprval_propput(ctx
, &ref
, v
);
2517 exprval_release(&ref
);
2523 return stack_push(ctx
, v
);
2526 /* JScript extension */
2527 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2529 const unsigned argc
= get_op_uint(ctx
, 0);
2534 TRACE("%u\n", argc
);
2536 if(!stack_topn_exprval(ctx
, argc
+1, &ref
))
2537 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2539 hres
= exprval_call(ctx
, &ref
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2544 stack_popn(ctx
, argc
+2);
2545 return stack_push(ctx
, v
);
2548 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2552 return stack_push(ctx
, jsval_undefined());
2555 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2557 const unsigned arg
= get_op_uint(ctx
, 0);
2565 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2567 const unsigned arg
= get_op_uint(ctx
, 0);
2575 hres
= to_boolean(v
, &b
);
2587 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2589 const unsigned arg
= get_op_uint(ctx
, 0);
2593 stack_popn(ctx
, arg
);
2597 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2599 const unsigned clear_ret
= get_op_uint(ctx
, 0);
2600 call_frame_t
*frame
= ctx
->call_ctx
;
2605 jsval_release(steal_ret(frame
));
2607 if((frame
->flags
& EXEC_CONSTRUCTOR
) && !is_object_instance(frame
->ret
)) {
2608 jsval_release(frame
->ret
);
2609 IDispatch_AddRef(frame
->this_obj
);
2610 frame
->ret
= jsval_disp(frame
->this_obj
);
2617 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2619 call_frame_t
*frame
= ctx
->call_ctx
;
2623 jsval_release(frame
->ret
);
2624 frame
->ret
= stack_pop(ctx
);
2628 static HRESULT
interp_push_acc(script_ctx_t
*ctx
)
2634 hres
= stack_push(ctx
, ctx
->acc
);
2636 ctx
->acc
= jsval_undefined();
2640 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2642 static const op_func_t op_funcs
[] = {
2643 #define X(x,a,b,c) interp_##x,
2648 static const unsigned op_move
[] = {
2649 #define X(a,x,b,c) x,
2654 static void pop_call_frame(script_ctx_t
*ctx
)
2656 call_frame_t
*frame
= ctx
->call_ctx
;
2658 frame
->stack_base
-= frame
->pop_locals
+ frame
->pop_variables
;
2660 assert(frame
->scope
== frame
->base_scope
);
2662 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2663 if(frame
->scope
&& frame
->scope
->ref
> 1) {
2664 HRESULT hres
= detach_variable_object(ctx
, frame
, TRUE
);
2666 ERR("Failed to detach variable object: %08x\n", hres
);
2669 if(frame
->arguments_obj
)
2670 detach_arguments_object(frame
->arguments_obj
);
2672 scope_release(frame
->scope
);
2674 if(frame
->pop_variables
)
2675 stack_popn(ctx
, frame
->pop_variables
);
2676 stack_popn(ctx
, frame
->pop_locals
);
2678 ctx
->call_ctx
= frame
->prev_frame
;
2680 if(frame
->function_instance
)
2681 jsdisp_release(frame
->function_instance
);
2682 if(frame
->variable_obj
)
2683 jsdisp_release(frame
->variable_obj
);
2685 IDispatch_Release(frame
->this_obj
);
2686 jsval_release(frame
->ret
);
2687 release_bytecode(frame
->bytecode
);
2691 static void print_backtrace(script_ctx_t
*ctx
)
2693 unsigned depth
= 0, i
;
2694 call_frame_t
*frame
;
2696 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
2697 WARN("%u\t", depth
);
2700 if(frame
->this_obj
&& frame
->this_obj
!= to_disp(ctx
->global
) && frame
->this_obj
!= ctx
->host_global
)
2701 WARN("%p->", frame
->this_obj
);
2702 WARN("%s(", frame
->function
->name
? debugstr_w(frame
->function
->name
) : "[unnamed]");
2703 if(frame
->base_scope
&& frame
->base_scope
->frame
) {
2704 for(i
=0; i
< frame
->argc
; i
++) {
2705 if(i
< frame
->function
->param_cnt
)
2706 WARN("%s%s=%s", i
? ", " : "", debugstr_w(frame
->function
->params
[i
]),
2707 debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2709 WARN("%s%s", i
? ", " : "", debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2712 WARN("[detached frame]");
2716 if(!(frame
->flags
& EXEC_RETURN_TO_INTERP
)) {
2717 WARN("%u\t[native code]\n", depth
);
2723 static HRESULT
unwind_exception(script_ctx_t
*ctx
, HRESULT exception_hres
)
2725 except_frame_t
*except_frame
;
2726 call_frame_t
*frame
;
2731 if(WARN_ON(jscript
)) {
2732 jsdisp_t
*error_obj
;
2735 static const WCHAR messageW
[] = {'m','e','s','s','a','g','e',0};
2737 WARN("Exception %08x %s", exception_hres
, debugstr_jsval(ctx
->ei
.val
));
2738 if(jsval_type(ctx
->ei
.val
) == JSV_OBJECT
) {
2739 error_obj
= to_jsdisp(get_object(ctx
->ei
.val
));
2741 hres
= jsdisp_propget_name(error_obj
, messageW
, &msg
);
2742 if(SUCCEEDED(hres
)) {
2743 WARN(" (message %s)", debugstr_jsval(msg
));
2750 print_backtrace(ctx
);
2753 for(frame
= ctx
->call_ctx
; !frame
->except_frame
; frame
= ctx
->call_ctx
) {
2756 while(frame
->scope
!= frame
->base_scope
)
2757 scope_pop(&frame
->scope
);
2759 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
2761 flags
= frame
->flags
;
2762 pop_call_frame(ctx
);
2763 if(!(flags
& EXEC_RETURN_TO_INTERP
))
2764 return exception_hres
;
2767 except_frame
= frame
->except_frame
;
2768 catch_off
= except_frame
->catch_off
;
2770 assert(except_frame
->stack_top
<= ctx
->stack_top
);
2771 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
2773 while(except_frame
->scope
!= frame
->scope
)
2774 scope_pop(&frame
->scope
);
2776 frame
->ip
= catch_off
? catch_off
: except_frame
->finally_off
;
2777 if(catch_off
) assert(frame
->bytecode
->instrs
[frame
->ip
].op
== OP_enter_catch
);
2779 except_val
= ctx
->ei
.val
;
2780 ctx
->ei
.val
= jsval_undefined();
2783 /* keep current except_frame if we're entering catch block with finally block associated */
2784 if(catch_off
&& except_frame
->finally_off
) {
2785 except_frame
->catch_off
= 0;
2787 frame
->except_frame
= except_frame
->next
;
2788 heap_free(except_frame
);
2791 hres
= stack_push(ctx
, except_val
);
2796 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2800 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, jsval_t
*r
)
2802 call_frame_t
*frame
;
2804 HRESULT hres
= S_OK
;
2809 frame
= ctx
->call_ctx
;
2810 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
2811 hres
= op_funcs
[op
](ctx
);
2813 hres
= unwind_exception(ctx
, hres
);
2816 }else if(frame
->ip
== -1) {
2817 const DWORD return_to_interp
= frame
->flags
& EXEC_RETURN_TO_INTERP
;
2819 assert(ctx
->stack_top
== frame
->stack_base
);
2820 assert(frame
->scope
== frame
->base_scope
);
2822 if(return_to_interp
) {
2823 jsval_release(ctx
->acc
);
2824 ctx
->acc
= steal_ret(frame
);
2826 *r
= steal_ret(frame
);
2828 pop_call_frame(ctx
);
2829 if(!return_to_interp
)
2832 frame
->ip
+= op_move
[op
];
2839 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
2841 IBindEventHandler
*target
;
2847 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
2851 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2855 if(!is_object_instance(v
)) {
2856 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
2860 disp
= get_object(v
);
2861 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
2862 if(SUCCEEDED(hres
)) {
2863 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
2864 IBindEventHandler_Release(target
);
2866 WARN("BindEvent failed: %08x\n", hres
);
2868 FIXME("No IBindEventHandler, not yet supported binding\n");
2871 IDispatch_Release(disp
);
2875 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
)
2877 const unsigned orig_stack
= ctx
->stack_top
;
2878 scope_chain_t
*scope
;
2883 /* If arguments are already on the stack, we may use them. */
2884 if(argv
+ argc
== ctx
->stack
+ ctx
->stack_top
) {
2885 frame
->arguments_off
= argv
- ctx
->stack
;
2888 frame
->arguments_off
= ctx
->stack_top
;
2889 for(i
= 0; i
< argc
; i
++) {
2890 hres
= jsval_copy(argv
[i
], &v
);
2892 hres
= stack_push(ctx
, v
);
2900 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2901 for(; i
< frame
->function
->param_cnt
; i
++) {
2902 hres
= stack_push(ctx
, jsval_undefined());
2904 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2909 frame
->pop_locals
= ctx
->stack_top
- orig_stack
;
2911 frame
->variables_off
= ctx
->stack_top
;
2913 for(i
= 0; i
< frame
->function
->var_cnt
; i
++) {
2914 hres
= stack_push(ctx
, jsval_undefined());
2916 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2921 frame
->pop_variables
= i
;
2923 hres
= scope_push(scope_chain
, variable_object
, to_disp(variable_object
), &scope
);
2925 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2929 for(i
= 0; i
< frame
->function
->func_cnt
; i
++) {
2930 if(frame
->function
->funcs
[i
].name
&& !frame
->function
->funcs
[i
].event_target
) {
2934 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+i
, scope
, &func_obj
);
2936 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2937 scope_release(scope
);
2941 off
= local_off(frame
, frame
->function
->funcs
[i
].local_ref
);
2942 jsval_release(ctx
->stack
[off
]);
2943 ctx
->stack
[off
] = jsval_obj(func_obj
);
2947 scope
->frame
= frame
;
2948 frame
->base_scope
= frame
->scope
= scope
;
2952 HRESULT
exec_source(script_ctx_t
*ctx
, DWORD flags
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
2953 IDispatch
*this_obj
, jsdisp_t
*function_instance
, jsdisp_t
*variable_obj
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
2955 call_frame_t
*frame
;
2959 for(i
= 0; i
< function
->func_cnt
; i
++) {
2962 if(!function
->funcs
[i
].event_target
)
2965 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+i
, scope
, &func_obj
);
2969 hres
= bind_event_target(ctx
, function
->funcs
+i
, func_obj
);
2970 jsdisp_release(func_obj
);
2975 if(flags
& (EXEC_GLOBAL
| EXEC_EVAL
)) {
2976 for(i
=0; i
< function
->var_cnt
; i
++) {
2977 TRACE("[%d] %s %d\n", i
, debugstr_w(function
->variables
[i
].name
), function
->variables
[i
].func_id
);
2978 if(function
->variables
[i
].func_id
!= -1) {
2981 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+function
->variables
[i
].func_id
, scope
, &func_obj
);
2985 hres
= jsdisp_propput_name(variable_obj
, function
->variables
[i
].name
, jsval_obj(func_obj
));
2986 jsdisp_release(func_obj
);
2987 }else if(!(flags
& EXEC_GLOBAL
) || !lookup_global_members(ctx
, function
->variables
[i
].name
, NULL
)) {
2990 hres
= jsdisp_get_id(variable_obj
, function
->variables
[i
].name
, fdexNameEnsure
, &id
);
2997 /* ECMA-262 3rd Edition 11.2.3.7 */
3001 jsthis
= iface_to_jsdisp(this_obj
);
3003 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
3005 jsdisp_release(jsthis
);
3009 if(ctx
->call_ctx
&& (flags
& EXEC_EVAL
)) {
3010 hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
3015 frame
= heap_alloc_zero(sizeof(*frame
));
3017 return E_OUTOFMEMORY
;
3019 frame
->function
= function
;
3020 frame
->ret
= jsval_undefined();
3022 frame
->bytecode
= bytecode_addref(bytecode
);
3024 if(!(flags
& (EXEC_GLOBAL
|EXEC_EVAL
))) {
3025 hres
= setup_scope(ctx
, frame
, scope
, variable_obj
, argc
, argv
);
3027 release_bytecode(frame
->bytecode
);
3032 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
3035 frame
->ip
= function
->instr_off
;
3036 frame
->stack_base
= ctx
->stack_top
;
3038 frame
->this_obj
= this_obj
;
3039 else if(ctx
->host_global
)
3040 frame
->this_obj
= ctx
->host_global
;
3042 frame
->this_obj
= to_disp(ctx
->global
);
3043 IDispatch_AddRef(frame
->this_obj
);
3045 if(function_instance
)
3046 frame
->function_instance
= jsdisp_addref(function_instance
);
3048 frame
->flags
= flags
;
3049 frame
->variable_obj
= jsdisp_addref(variable_obj
);
3051 frame
->prev_frame
= ctx
->call_ctx
;
3052 ctx
->call_ctx
= frame
;
3054 if(flags
& EXEC_RETURN_TO_INTERP
) {
3056 * We're called directly from interpreter, so we may just setup call frame and return.
3057 * Already running interpreter will take care of execution.
3060 *r
= jsval_undefined();
3064 return enter_bytecode(ctx
, r
);