2 * Copyright 2008,2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
30 struct _except_frame_t
{
57 static const size_t stack_size
= 0x4000;
59 static HRESULT
stack_push(script_ctx_t
*ctx
, jsval_t v
)
61 if(ctx
->stack_top
== stack_size
)
62 return JS_E_STACK_OVERFLOW
;
64 ctx
->stack
[ctx
->stack_top
++] = v
;
68 static inline HRESULT
stack_push_string(script_ctx_t
*ctx
, const WCHAR
*str
)
76 return stack_push(ctx
, jsval_string(v
));
79 static inline jsval_t
stack_top(script_ctx_t
*ctx
)
81 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
82 return ctx
->stack
[ctx
->stack_top
-1];
85 static inline jsval_t
*stack_top_ref(script_ctx_t
*ctx
, unsigned n
)
87 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
);
88 return ctx
->stack
+ctx
->stack_top
-1-n
;
91 static inline jsval_t
stack_topn(script_ctx_t
*ctx
, unsigned n
)
93 return *stack_top_ref(ctx
, n
);
96 static inline jsval_t
*stack_args(script_ctx_t
*ctx
, unsigned n
)
100 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
-1);
101 return ctx
->stack
+ ctx
->stack_top
-n
;
104 static inline jsval_t
stack_pop(script_ctx_t
*ctx
)
106 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
107 return ctx
->stack
[--ctx
->stack_top
];
110 static void stack_popn(script_ctx_t
*ctx
, unsigned n
)
113 jsval_release(stack_pop(ctx
));
116 static HRESULT
stack_pop_number(script_ctx_t
*ctx
, double *r
)
122 hres
= to_number(ctx
, v
, r
);
127 static HRESULT
stack_pop_object(script_ctx_t
*ctx
, IDispatch
**r
)
133 if(is_object_instance(v
)) {
135 return JS_E_OBJECT_REQUIRED
;
140 hres
= to_object(ctx
, v
, r
);
145 static inline HRESULT
stack_pop_int(script_ctx_t
*ctx
, INT
*r
)
147 return to_int32(ctx
, stack_pop(ctx
), r
);
150 static inline HRESULT
stack_pop_uint(script_ctx_t
*ctx
, DWORD
*r
)
152 return to_uint32(ctx
, stack_pop(ctx
), r
);
155 static inline unsigned local_off(call_frame_t
*frame
, int ref
)
158 ? frame
->arguments_off
- ref
-1
159 : frame
->variables_off
+ ref
;
162 static inline BSTR
local_name(call_frame_t
*frame
, int ref
)
164 return ref
< 0 ? frame
->function
->params
[-ref
-1] : frame
->function
->variables
[ref
].name
;
167 /* Steals input reference even on failure. */
168 static HRESULT
stack_push_exprval(script_ctx_t
*ctx
, exprval_t
*val
)
176 hres
= stack_push(ctx
, jsval_disp(val
->u
.idref
.disp
));
178 hres
= stack_push(ctx
, jsval_number(val
->u
.idref
.id
));
180 IDispatch_Release(val
->u
.idref
.disp
);
182 case EXPRVAL_STACK_REF
:
183 hres
= stack_push(ctx
, jsval_number(val
->u
.off
));
185 hres
= stack_push(ctx
, jsval_undefined());
187 case EXPRVAL_INVALID
:
188 hres
= stack_push(ctx
, jsval_undefined());
190 hres
= stack_push(ctx
, jsval_number(val
->u
.hres
));
198 static BOOL
stack_topn_exprval(script_ctx_t
*ctx
, unsigned n
, exprval_t
*r
)
200 jsval_t v
= stack_topn(ctx
, n
+1);
202 switch(jsval_type(v
)) {
204 call_frame_t
*frame
= ctx
->call_ctx
;
205 unsigned off
= get_number(v
);
207 if(!frame
->base_scope
->frame
&& off
>= frame
->arguments_off
) {
212 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
214 assert(off
< frame
->variables_off
+ frame
->function
->var_cnt
);
215 name
= off
>= frame
->variables_off
216 ? frame
->function
->variables
[off
- frame
->variables_off
].name
217 : frame
->function
->params
[off
- frame
->arguments_off
];
218 hres
= jsdisp_get_id(ctx
->call_ctx
->base_scope
->jsobj
, name
, 0, &id
);
220 r
->type
= EXPRVAL_INVALID
;
225 *stack_top_ref(ctx
, n
+1) = jsval_obj(jsdisp_addref(frame
->base_scope
->jsobj
));
226 *stack_top_ref(ctx
, n
) = jsval_number(id
);
227 r
->type
= EXPRVAL_IDREF
;
228 r
->u
.idref
.disp
= frame
->base_scope
->obj
;
233 r
->type
= EXPRVAL_STACK_REF
;
238 r
->type
= EXPRVAL_IDREF
;
239 r
->u
.idref
.disp
= get_object(v
);
240 assert(is_number(stack_topn(ctx
, n
)));
241 r
->u
.idref
.id
= get_number(stack_topn(ctx
, n
));
244 r
->type
= EXPRVAL_INVALID
;
245 assert(is_number(stack_topn(ctx
, n
)));
246 r
->u
.hres
= get_number(stack_topn(ctx
, n
));
254 static inline BOOL
stack_pop_exprval(script_ctx_t
*ctx
, exprval_t
*r
)
256 BOOL ret
= stack_topn_exprval(ctx
, 0, r
);
261 static HRESULT
exprval_propput(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t v
)
264 case EXPRVAL_STACK_REF
: {
265 jsval_t
*r
= ctx
->stack
+ ref
->u
.off
;
267 return jsval_copy(v
, r
);
270 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
);
277 static HRESULT
exprval_propget(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
280 case EXPRVAL_STACK_REF
:
281 return jsval_copy(ctx
->stack
[ref
->u
.off
], r
);
283 return disp_propget(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, r
);
290 static HRESULT
exprval_call(script_ctx_t
*ctx
, exprval_t
*ref
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
293 case EXPRVAL_STACK_REF
: {
294 jsval_t v
= ctx
->stack
[ref
->u
.off
];
296 if(!is_object_instance(v
)) {
297 FIXME("invoke %s\n", debugstr_jsval(v
));
301 return disp_call_value(ctx
, get_object(v
), NULL
, flags
, argc
, argv
, r
);
304 return disp_call(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, flags
, argc
, argv
, r
);
311 /* ECMA-262 3rd Edition 8.7.1 */
312 /* Steals input reference. */
313 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
317 if(ref
->type
== EXPRVAL_JSVAL
) {
322 hres
= exprval_propget(ctx
, ref
, r
);
324 if(ref
->type
== EXPRVAL_IDREF
)
325 IDispatch_Release(ref
->u
.idref
.disp
);
329 static void exprval_release(exprval_t
*val
)
333 jsval_release(val
->u
.val
);
336 if(val
->u
.idref
.disp
)
337 IDispatch_Release(val
->u
.idref
.disp
);
339 case EXPRVAL_STACK_REF
:
340 case EXPRVAL_INVALID
:
345 static inline void exprval_set_exception(exprval_t
*val
, HRESULT hres
)
347 val
->type
= EXPRVAL_INVALID
;
351 static inline void exprval_set_disp_ref(exprval_t
*ref
, IDispatch
*obj
, DISPID id
)
353 ref
->type
= EXPRVAL_IDREF
;
354 IDispatch_AddRef(ref
->u
.idref
.disp
= obj
);
355 ref
->u
.idref
.id
= id
;
358 static inline jsval_t
steal_ret(call_frame_t
*frame
)
360 jsval_t r
= frame
->ret
;
361 frame
->ret
= jsval_undefined();
365 static inline void clear_acc(script_ctx_t
*ctx
)
367 jsval_release(ctx
->acc
);
368 ctx
->acc
= jsval_undefined();
371 static HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
373 scope_chain_t
*new_scope
;
375 new_scope
= heap_alloc(sizeof(scope_chain_t
));
377 return E_OUTOFMEMORY
;
381 IDispatch_AddRef(obj
);
382 new_scope
->jsobj
= jsobj
;
383 new_scope
->obj
= obj
;
384 new_scope
->frame
= NULL
;
385 new_scope
->next
= scope
? scope_addref(scope
) : NULL
;
391 static void scope_pop(scope_chain_t
**scope
)
400 void scope_release(scope_chain_t
*scope
)
406 scope_release(scope
->next
);
408 IDispatch_Release(scope
->obj
);
412 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
419 jsdisp
= iface_to_jsdisp(disp
);
421 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
422 jsdisp_release(jsdisp
);
429 bstr
= SysAllocString(name
);
431 return E_OUTOFMEMORY
;
435 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
436 if(SUCCEEDED(hres
)) {
437 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
438 IDispatchEx_Release(dispex
);
440 TRACE("using IDispatch\n");
441 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
444 if(name_bstr
!= bstr
)
449 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
451 IObjectIdentity
*identity
;
452 IUnknown
*unk1
, *unk2
;
460 if(!disp1
|| !disp2
) {
465 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
469 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
471 IUnknown_Release(unk1
);
478 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
479 if(SUCCEEDED(hres
)) {
480 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
481 IObjectIdentity_Release(identity
);
488 IUnknown_Release(unk1
);
489 IUnknown_Release(unk2
);
493 /* ECMA-262 3rd Edition 11.9.6 */
494 HRESULT
jsval_strict_equal(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
496 jsval_type_t type
= jsval_type(lval
);
500 if(type
!= jsval_type(rval
)) {
501 if(is_null_instance(lval
))
502 *ret
= is_null_instance(rval
);
514 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
516 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
519 *ret
= get_number(lval
) == get_number(rval
);
522 *ret
= !get_bool(lval
) == !get_bool(rval
);
525 WARN("VARIANT type, returning false\n");
534 * Transfers local variables from stack to variable object.
535 * It's slow, so we want to avoid it as much as possible.
537 static HRESULT
detach_variable_object(script_ctx_t
*ctx
, call_frame_t
*frame
, BOOL from_release
)
542 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
545 TRACE("detaching %p\n", frame
);
547 assert(frame
== frame
->base_scope
->frame
);
548 assert(frame
->variable_obj
== frame
->base_scope
->jsobj
);
550 if(!from_release
&& !frame
->arguments_obj
) {
551 hres
= setup_arguments_object(ctx
, frame
);
556 frame
->base_scope
->frame
= NULL
;
558 for(i
= 0; i
< frame
->function
->locals_cnt
; i
++) {
559 hres
= jsdisp_propput_name(frame
->variable_obj
, frame
->function
->locals
[i
].name
,
560 ctx
->stack
[local_off(frame
, frame
->function
->locals
[i
].ref
)]);
568 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
574 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
575 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
576 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
577 if(SUCCEEDED(hres
)) {
579 exprval_set_disp_ref(ret
, item
->disp
, id
);
588 IDispatch
*lookup_global_host(script_ctx_t
*ctx
)
590 IDispatch
*disp
= NULL
;
593 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
594 if(!(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) continue;
598 if(!disp
) disp
= to_disp(ctx
->global
);
603 static int __cdecl
local_ref_cmp(const void *key
, const void *ref
)
605 return wcscmp((const WCHAR
*)key
, ((const local_ref_t
*)ref
)->name
);
608 local_ref_t
*lookup_local(const function_code_t
*function
, const WCHAR
*identifier
)
610 return bsearch(identifier
, function
->locals
, function
->locals_cnt
, sizeof(*function
->locals
), local_ref_cmp
);
613 /* ECMA-262 3rd Edition 10.1.4 */
614 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
616 scope_chain_t
*scope
;
621 TRACE("%s\n", debugstr_w(identifier
));
624 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
626 function_code_t
*func
= scope
->frame
->function
;
627 local_ref_t
*ref
= lookup_local(func
, identifier
);
630 ret
->type
= EXPRVAL_STACK_REF
;
631 ret
->u
.off
= local_off(scope
->frame
, ref
->ref
);
632 TRACE("returning ref %d for %d\n", ret
->u
.off
, ref
->ref
);
636 if(!wcscmp(identifier
, L
"arguments")) {
637 hres
= detach_variable_object(ctx
, scope
->frame
, FALSE
);
643 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
645 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
646 if(SUCCEEDED(hres
)) {
647 exprval_set_disp_ref(ret
, scope
->obj
, id
);
652 item
= ctx
->call_ctx
->bytecode
->named_item
;
654 hres
= jsdisp_get_id(item
->script_obj
, identifier
, 0, &id
);
655 if(SUCCEEDED(hres
)) {
656 exprval_set_disp_ref(ret
, to_disp(item
->script_obj
), id
);
659 if(!(item
->flags
& SCRIPTITEM_CODEONLY
)) {
660 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
661 if(SUCCEEDED(hres
)) {
662 exprval_set_disp_ref(ret
, item
->disp
, id
);
669 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
670 if(SUCCEEDED(hres
)) {
671 exprval_set_disp_ref(ret
, to_disp(ctx
->global
), id
);
675 item
= lookup_named_item(ctx
, identifier
, SCRIPTITEM_ISVISIBLE
);
677 IDispatch_AddRef(item
->disp
);
678 ret
->type
= EXPRVAL_JSVAL
;
679 ret
->u
.val
= jsval_disp(item
->disp
);
683 if(lookup_global_members(ctx
, identifier
, ret
))
686 exprval_set_exception(ret
, JS_E_UNDEFINED_VARIABLE
);
690 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
692 call_frame_t
*frame
= ctx
->call_ctx
;
693 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
696 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
698 call_frame_t
*frame
= ctx
->call_ctx
;
699 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
702 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
704 call_frame_t
*frame
= ctx
->call_ctx
;
705 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
708 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
710 call_frame_t
*frame
= ctx
->call_ctx
;
711 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
714 static inline double get_op_double(script_ctx_t
*ctx
)
716 call_frame_t
*frame
= ctx
->call_ctx
;
717 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
720 static inline void jmp_next(script_ctx_t
*ctx
)
725 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
727 ctx
->call_ctx
->ip
= dst
;
730 /* ECMA-262 3rd Edition 12.6.4 */
731 static HRESULT
interp_forin(script_ctx_t
*ctx
)
733 const HRESULT arg
= get_op_uint(ctx
, 0);
734 IDispatch
*obj
= NULL
;
743 assert(is_number(stack_top(ctx
)));
744 id
= get_number(stack_top(ctx
));
746 if(!stack_topn_exprval(ctx
, 1, &prop_ref
)) {
747 FIXME("invalid ref: %08x\n", prop_ref
.u
.hres
);
751 if(is_object_instance(stack_topn(ctx
, 3)))
752 obj
= get_object(stack_topn(ctx
, 3));
755 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
756 if(SUCCEEDED(hres
)) {
757 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
759 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
760 IDispatchEx_Release(dispex
);
764 TRACE("No IDispatchEx\n");
771 str
= jsstr_alloc_len(name
, SysStringLen(name
));
774 return E_OUTOFMEMORY
;
777 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
779 hres
= exprval_propput(ctx
, &prop_ref
, jsval_string(str
));
792 /* ECMA-262 3rd Edition 12.10 */
793 static HRESULT
interp_push_scope(script_ctx_t
*ctx
)
802 hres
= to_object(ctx
, v
, &disp
);
807 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
808 IDispatch_Release(disp
);
812 /* ECMA-262 3rd Edition 12.10 */
813 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
817 scope_pop(&ctx
->call_ctx
->scope
);
821 /* ECMA-262 3rd Edition 12.13 */
822 static HRESULT
interp_case(script_ctx_t
*ctx
)
824 const unsigned arg
= get_op_uint(ctx
, 0);
832 hres
= jsval_strict_equal(stack_top(ctx
), v
, &b
);
846 static void set_error_value(script_ctx_t
*ctx
, jsval_t value
)
848 jsexcept_t
*ei
= ctx
->ei
;
852 ei
->error
= JS_E_EXCEPTION_THROWN
;
853 ei
->valid_value
= TRUE
;
856 if(is_object_instance(value
) && get_object(value
) && (obj
= to_jsdisp(get_object(value
)))) {
862 /* FIXME: We should check if object is an error instance */
864 hres
= jsdisp_propget_name(obj
, L
"number", &v
);
865 if(SUCCEEDED(hres
)) {
866 hres
= to_uint32(ctx
, v
, &number
);
868 ei
->error
= FAILED(number
) ? number
: E_FAIL
;
872 hres
= jsdisp_propget_name(obj
, L
"description", &v
);
873 if(SUCCEEDED(hres
)) {
874 hres
= to_string(ctx
, v
, &str
);
882 /* ECMA-262 3rd Edition 12.13 */
883 static HRESULT
interp_throw(script_ctx_t
*ctx
)
887 set_error_value(ctx
, stack_pop(ctx
));
888 return DISP_E_EXCEPTION
;
891 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
893 const HRESULT arg
= get_op_uint(ctx
, 0);
895 TRACE("%08x\n", arg
);
900 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
902 const HRESULT hres
= get_op_uint(ctx
, 0);
903 jsstr_t
*str
= get_op_str(ctx
, 1);
906 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
908 ptr
= jsstr_flatten(str
);
909 return ptr
? throw_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
912 /* ECMA-262 3rd Edition 12.14 */
913 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
915 const unsigned catch_off
= get_op_uint(ctx
, 0);
916 const unsigned finally_off
= get_op_uint(ctx
, 1);
917 call_frame_t
*frame
= ctx
->call_ctx
;
918 except_frame_t
*except
;
922 except
= heap_alloc(sizeof(*except
));
924 return E_OUTOFMEMORY
;
926 except
->stack_top
= ctx
->stack_top
;
927 except
->scope
= frame
->scope
;
928 except
->catch_off
= catch_off
;
929 except
->finally_off
= finally_off
;
930 except
->next
= frame
->except_frame
;
931 frame
->except_frame
= except
;
935 /* ECMA-262 3rd Edition 12.14 */
936 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
938 const unsigned ret_off
= get_op_uint(ctx
, 0);
939 call_frame_t
*frame
= ctx
->call_ctx
;
940 except_frame_t
*except
;
941 unsigned finally_off
;
943 TRACE("%u\n", ret_off
);
945 except
= frame
->except_frame
;
946 assert(except
!= NULL
);
948 finally_off
= except
->finally_off
;
949 frame
->except_frame
= except
->next
;
955 hres
= stack_push(ctx
, jsval_number(ret_off
));
958 hres
= stack_push(ctx
, jsval_bool(TRUE
));
961 frame
->ip
= finally_off
;
969 /* ECMA-262 3rd Edition 12.14 */
970 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
972 call_frame_t
*frame
= ctx
->call_ctx
;
981 TRACE("passing exception\n");
983 set_error_value(ctx
, stack_pop(ctx
));
984 return DISP_E_EXCEPTION
;
988 assert(is_number(v
));
989 frame
->ip
= get_number(v
);
993 static HRESULT
interp_enter_catch(script_ctx_t
*ctx
)
995 const BSTR ident
= get_op_bstr(ctx
, 0);
1000 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
1005 hres
= jsdisp_propput_name(scope_obj
, ident
, v
);
1008 hres
= scope_push(ctx
->call_ctx
->scope
, scope_obj
, to_disp(scope_obj
), &ctx
->call_ctx
->scope
);
1009 jsdisp_release(scope_obj
);
1013 /* ECMA-262 3rd Edition 13 */
1014 static HRESULT
interp_func(script_ctx_t
*ctx
)
1016 unsigned func_idx
= get_op_uint(ctx
, 0);
1017 call_frame_t
*frame
= ctx
->call_ctx
;
1021 TRACE("%d\n", func_idx
);
1023 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
1024 frame
->scope
, &dispex
);
1028 return stack_push(ctx
, jsval_obj(dispex
));
1031 /* ECMA-262 3rd Edition 11.2.1 */
1032 static HRESULT
interp_array(script_ctx_t
*ctx
)
1043 namev
= stack_pop(ctx
);
1045 hres
= stack_pop_object(ctx
, &obj
);
1047 jsval_release(namev
);
1051 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1052 jsval_release(namev
);
1054 IDispatch_Release(obj
);
1058 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
1059 jsstr_release(name_str
);
1060 if(SUCCEEDED(hres
)) {
1061 hres
= disp_propget(ctx
, obj
, id
, &v
);
1062 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1063 v
= jsval_undefined();
1066 IDispatch_Release(obj
);
1070 return stack_push(ctx
, v
);
1073 /* ECMA-262 3rd Edition 11.2.1 */
1074 static HRESULT
interp_member(script_ctx_t
*ctx
)
1076 const BSTR arg
= get_op_bstr(ctx
, 0);
1084 hres
= stack_pop_object(ctx
, &obj
);
1088 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
1089 if(SUCCEEDED(hres
)) {
1090 hres
= disp_propget(ctx
, obj
, id
, &v
);
1091 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1092 v
= jsval_undefined();
1095 IDispatch_Release(obj
);
1099 return stack_push(ctx
, v
);
1102 /* ECMA-262 3rd Edition 11.2.1 */
1103 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
1105 const unsigned arg
= get_op_uint(ctx
, 0);
1106 jsval_t objv
, namev
;
1116 namev
= stack_pop(ctx
);
1117 objv
= stack_pop(ctx
);
1119 hres
= to_object(ctx
, objv
, &obj
);
1120 jsval_release(objv
);
1121 if(SUCCEEDED(hres
)) {
1122 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1124 IDispatch_Release(obj
);
1126 jsval_release(namev
);
1130 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
1131 jsstr_release(name_str
);
1132 if(SUCCEEDED(hres
)) {
1133 ref
.type
= EXPRVAL_IDREF
;
1134 ref
.u
.idref
.disp
= obj
;
1135 ref
.u
.idref
.id
= id
;
1137 IDispatch_Release(obj
);
1138 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1139 exprval_set_exception(&ref
, JS_E_INVALID_PROPERTY
);
1142 ERR("failed %08x\n", hres
);
1147 return stack_push_exprval(ctx
, &ref
);
1150 /* ECMA-262 3rd Edition 11.2.1 */
1151 static HRESULT
interp_refval(script_ctx_t
*ctx
)
1159 if(!stack_topn_exprval(ctx
, 0, &ref
))
1160 return JS_E_ILLEGAL_ASSIGN
;
1162 hres
= exprval_propget(ctx
, &ref
, &v
);
1166 return stack_push(ctx
, v
);
1169 /* ECMA-262 3rd Edition 11.2.2 */
1170 static HRESULT
interp_new(script_ctx_t
*ctx
)
1172 const unsigned argc
= get_op_uint(ctx
, 0);
1175 TRACE("%d\n", argc
);
1177 constr
= stack_topn(ctx
, argc
);
1179 /* NOTE: Should use to_object here */
1182 return JS_E_OBJECT_EXPECTED
;
1183 else if(!is_object_instance(constr
))
1184 return JS_E_INVALID_ACTION
;
1185 else if(!get_object(constr
))
1186 return JS_E_INVALID_PROPERTY
;
1189 return disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1190 argc
, stack_args(ctx
, argc
), &ctx
->acc
);
1193 /* ECMA-262 3rd Edition 11.2.3 */
1194 static HRESULT
interp_call(script_ctx_t
*ctx
)
1196 const unsigned argn
= get_op_uint(ctx
, 0);
1197 const int do_ret
= get_op_int(ctx
, 1);
1200 TRACE("%d %d\n", argn
, do_ret
);
1202 obj
= stack_topn(ctx
, argn
);
1203 if(!is_object_instance(obj
))
1204 return JS_E_INVALID_PROPERTY
;
1207 return disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1208 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1211 /* ECMA-262 3rd Edition 11.2.3 */
1212 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
1214 const unsigned argn
= get_op_uint(ctx
, 0);
1215 const int do_ret
= get_op_int(ctx
, 1);
1218 TRACE("%d %d\n", argn
, do_ret
);
1220 if(!stack_topn_exprval(ctx
, argn
, &ref
))
1224 return exprval_call(ctx
, &ref
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1225 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1228 /* ECMA-262 3rd Edition 11.1.1 */
1229 static HRESULT
interp_this(script_ctx_t
*ctx
)
1231 IDispatch
*this_obj
= ctx
->call_ctx
->this_obj
;
1236 named_item_t
*item
= ctx
->call_ctx
->bytecode
->named_item
;
1239 this_obj
= (item
->flags
& SCRIPTITEM_CODEONLY
) ? to_disp(item
->script_obj
) : item
->disp
;
1241 this_obj
= lookup_global_host(ctx
);
1244 IDispatch_AddRef(this_obj
);
1245 return stack_push(ctx
, jsval_disp(this_obj
));
1248 static HRESULT
interp_identifier_ref(script_ctx_t
*ctx
, BSTR identifier
, unsigned flags
)
1253 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1257 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1258 jsdisp_t
*script_obj
= ctx
->global
;
1261 if(ctx
->call_ctx
->bytecode
->named_item
)
1262 script_obj
= ctx
->call_ctx
->bytecode
->named_item
->script_obj
;
1264 hres
= jsdisp_get_id(script_obj
, identifier
, fdexNameEnsure
, &id
);
1268 exprval_set_disp_ref(&exprval
, to_disp(script_obj
), id
);
1271 if(exprval
.type
== EXPRVAL_JSVAL
|| exprval
.type
== EXPRVAL_INVALID
) {
1272 WARN("invalid ref\n");
1273 exprval_release(&exprval
);
1274 exprval_set_exception(&exprval
, JS_E_OBJECT_EXPECTED
);
1277 return stack_push_exprval(ctx
, &exprval
);
1280 static HRESULT
identifier_value(script_ctx_t
*ctx
, BSTR identifier
)
1286 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1290 if(exprval
.type
== EXPRVAL_INVALID
)
1291 return throw_error(ctx
, exprval
.u
.hres
, identifier
);
1293 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1297 return stack_push(ctx
, v
);
1300 static HRESULT
interp_local_ref(script_ctx_t
*ctx
)
1302 const int arg
= get_op_int(ctx
, 0);
1303 const unsigned flags
= get_op_uint(ctx
, 1);
1304 call_frame_t
*frame
= ctx
->call_ctx
;
1309 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1310 return interp_identifier_ref(ctx
, local_name(frame
, arg
), flags
);
1312 ref
.type
= EXPRVAL_STACK_REF
;
1313 ref
.u
.off
= local_off(frame
, arg
);
1314 return stack_push_exprval(ctx
, &ref
);
1317 static HRESULT
interp_local(script_ctx_t
*ctx
)
1319 const int arg
= get_op_int(ctx
, 0);
1320 call_frame_t
*frame
= ctx
->call_ctx
;
1324 TRACE("%d: %s\n", arg
, debugstr_w(local_name(frame
, arg
)));
1326 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1327 return identifier_value(ctx
, local_name(frame
, arg
));
1329 hres
= jsval_copy(ctx
->stack
[local_off(frame
, arg
)], ©
);
1333 return stack_push(ctx
, copy
);
1336 /* ECMA-262 3rd Edition 10.1.4 */
1337 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1339 const BSTR arg
= get_op_bstr(ctx
, 0);
1341 TRACE("%s\n", debugstr_w(arg
));
1343 return identifier_value(ctx
, arg
);
1346 /* ECMA-262 3rd Edition 10.1.4 */
1347 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1349 const BSTR arg
= get_op_bstr(ctx
, 0);
1350 const unsigned flags
= get_op_uint(ctx
, 1);
1352 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1354 return interp_identifier_ref(ctx
, arg
, flags
);
1357 /* ECMA-262 3rd Edition 7.8.1 */
1358 static HRESULT
interp_null(script_ctx_t
*ctx
)
1362 return stack_push(ctx
, jsval_null());
1365 /* ECMA-262 3rd Edition 7.8.2 */
1366 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1368 const int arg
= get_op_int(ctx
, 0);
1370 TRACE("%s\n", arg
? "true" : "false");
1372 return stack_push(ctx
, jsval_bool(arg
));
1375 /* ECMA-262 3rd Edition 7.8.3 */
1376 static HRESULT
interp_int(script_ctx_t
*ctx
)
1378 const int arg
= get_op_int(ctx
, 0);
1382 return stack_push(ctx
, jsval_number(arg
));
1385 /* ECMA-262 3rd Edition 7.8.3 */
1386 static HRESULT
interp_double(script_ctx_t
*ctx
)
1388 const double arg
= get_op_double(ctx
);
1390 TRACE("%lf\n", arg
);
1392 return stack_push(ctx
, jsval_number(arg
));
1395 /* ECMA-262 3rd Edition 7.8.4 */
1396 static HRESULT
interp_str(script_ctx_t
*ctx
)
1398 jsstr_t
*str
= get_op_str(ctx
, 0);
1400 TRACE("%s\n", debugstr_jsstr(str
));
1402 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1405 /* ECMA-262 3rd Edition 7.8 */
1406 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1408 jsstr_t
*source
= get_op_str(ctx
, 0);
1409 const unsigned flags
= get_op_uint(ctx
, 1);
1413 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1415 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1419 return stack_push(ctx
, jsval_obj(regexp
));
1422 /* ECMA-262 3rd Edition 11.1.4 */
1423 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1425 const unsigned arg
= get_op_uint(ctx
, 0);
1431 hres
= create_array(ctx
, arg
, &array
);
1435 return stack_push(ctx
, jsval_obj(array
));
1438 static HRESULT
interp_carray_set(script_ctx_t
*ctx
)
1440 const unsigned index
= get_op_uint(ctx
, 0);
1441 jsval_t value
, array
;
1444 value
= stack_pop(ctx
);
1446 TRACE("[%u] = %s\n", index
, debugstr_jsval(value
));
1448 array
= stack_top(ctx
);
1449 assert(is_object_instance(array
));
1451 hres
= jsdisp_propput_idx(iface_to_jsdisp(get_object(array
)), index
, value
);
1452 jsval_release(value
);
1456 /* ECMA-262 3rd Edition 11.1.5 */
1457 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1464 hres
= create_object(ctx
, NULL
, &obj
);
1468 return stack_push(ctx
, jsval_obj(obj
));
1471 /* ECMA-262 3rd Edition 11.1.5 */
1472 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1474 jsstr_t
*name_arg
= get_op_str(ctx
, 0);
1475 unsigned type
= get_op_uint(ctx
, 1);
1481 TRACE("%s\n", debugstr_jsstr(name_arg
));
1483 val
= stack_pop(ctx
);
1485 /* FIXME: we should pass it as jsstr_t */
1486 name
= jsstr_flatten(name_arg
);
1488 assert(is_object_instance(stack_top(ctx
)));
1489 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1491 if(type
== PROPERTY_DEFINITION_VALUE
) {
1492 hres
= jsdisp_propput_name(obj
, name
, val
);
1494 property_desc_t desc
= {PROPF_ENUMERABLE
| PROPF_CONFIGURABLE
};
1497 assert(is_object_instance(val
));
1498 func
= iface_to_jsdisp(get_object(val
));
1500 desc
.mask
= desc
.flags
;
1501 if(type
== PROPERTY_DEFINITION_GETTER
) {
1502 desc
.explicit_getter
= TRUE
;
1505 desc
.explicit_setter
= TRUE
;
1509 hres
= jsdisp_define_property(obj
, name
, &desc
);
1510 jsdisp_release(func
);
1517 /* ECMA-262 3rd Edition 11.11 */
1518 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1520 const unsigned arg
= get_op_uint(ctx
, 0);
1526 hres
= to_boolean(stack_top(ctx
), &b
);
1539 /* ECMA-262 3rd Edition 11.11 */
1540 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1542 const unsigned arg
= get_op_uint(ctx
, 0);
1548 hres
= to_boolean(stack_top(ctx
), &b
);
1561 /* ECMA-262 3rd Edition 11.10 */
1562 static HRESULT
interp_or(script_ctx_t
*ctx
)
1569 hres
= stack_pop_int(ctx
, &r
);
1573 hres
= stack_pop_int(ctx
, &l
);
1577 return stack_push(ctx
, jsval_number(l
|r
));
1580 /* ECMA-262 3rd Edition 11.10 */
1581 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1588 hres
= stack_pop_int(ctx
, &r
);
1592 hres
= stack_pop_int(ctx
, &l
);
1596 return stack_push(ctx
, jsval_number(l
^r
));
1599 /* ECMA-262 3rd Edition 11.10 */
1600 static HRESULT
interp_and(script_ctx_t
*ctx
)
1607 hres
= stack_pop_int(ctx
, &r
);
1611 hres
= stack_pop_int(ctx
, &l
);
1615 return stack_push(ctx
, jsval_number(l
&r
));
1618 /* ECMA-262 3rd Edition 11.8.6 */
1619 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1621 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1627 if(!is_object_instance(v
) || !get_object(v
)) {
1629 return JS_E_FUNCTION_EXPECTED
;
1632 obj
= iface_to_jsdisp(get_object(v
));
1633 IDispatch_Release(get_object(v
));
1635 FIXME("non-jsdisp objects not supported\n");
1639 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1640 hres
= jsdisp_propget_name(obj
, L
"prototype", &prot
);
1642 hres
= JS_E_FUNCTION_EXPECTED
;
1644 jsdisp_release(obj
);
1650 if(is_object_instance(prot
)) {
1651 if(is_object_instance(v
))
1652 tmp
= iface_to_jsdisp(get_object(v
));
1653 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1654 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1660 jsdisp_release(tmp
);
1662 FIXME("prototype is not an object\n");
1666 jsval_release(prot
);
1671 return stack_push(ctx
, jsval_bool(ret
));
1674 /* ECMA-262 3rd Edition 11.8.7 */
1675 static HRESULT
interp_in(script_ctx_t
*ctx
)
1686 obj
= stack_pop(ctx
);
1687 if(!is_object_instance(obj
) || !get_object(obj
)) {
1689 return JS_E_OBJECT_EXPECTED
;
1693 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1696 IDispatch_Release(get_object(obj
));
1700 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1701 IDispatch_Release(get_object(obj
));
1702 jsstr_release(jsstr
);
1705 else if(hres
== DISP_E_UNKNOWNNAME
)
1710 return stack_push(ctx
, jsval_bool(ret
));
1713 /* ECMA-262 3rd Edition 11.6.1 */
1714 static HRESULT
interp_add(script_ctx_t
*ctx
)
1716 jsval_t l
, r
, lval
, rval
, ret
;
1719 rval
= stack_pop(ctx
);
1720 lval
= stack_pop(ctx
);
1722 TRACE("%s + %s\n", debugstr_jsval(lval
), debugstr_jsval(rval
));
1724 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1725 if(SUCCEEDED(hres
)) {
1726 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1730 jsval_release(lval
);
1731 jsval_release(rval
);
1735 if(is_string(l
) || is_string(r
)) {
1736 jsstr_t
*lstr
, *rstr
= NULL
;
1738 hres
= to_string(ctx
, l
, &lstr
);
1740 hres
= to_string(ctx
, r
, &rstr
);
1742 if(SUCCEEDED(hres
)) {
1745 ret_str
= jsstr_concat(lstr
, rstr
);
1747 ret
= jsval_string(ret_str
);
1749 hres
= E_OUTOFMEMORY
;
1752 jsstr_release(lstr
);
1754 jsstr_release(rstr
);
1758 hres
= to_number(ctx
, l
, &nl
);
1759 if(SUCCEEDED(hres
)) {
1760 hres
= to_number(ctx
, r
, &nr
);
1762 ret
= jsval_number(nl
+nr
);
1771 return stack_push(ctx
, ret
);
1774 /* ECMA-262 3rd Edition 11.6.2 */
1775 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1782 hres
= stack_pop_number(ctx
, &r
);
1786 hres
= stack_pop_number(ctx
, &l
);
1790 return stack_push(ctx
, jsval_number(l
-r
));
1793 /* ECMA-262 3rd Edition 11.5.1 */
1794 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1801 hres
= stack_pop_number(ctx
, &r
);
1805 hres
= stack_pop_number(ctx
, &l
);
1809 return stack_push(ctx
, jsval_number(l
*r
));
1812 /* ECMA-262 3rd Edition 11.5.2 */
1813 static HRESULT
interp_div(script_ctx_t
*ctx
)
1820 hres
= stack_pop_number(ctx
, &r
);
1824 hres
= stack_pop_number(ctx
, &l
);
1828 return stack_push(ctx
, jsval_number(l
/r
));
1831 /* ECMA-262 3rd Edition 11.5.3 */
1832 static HRESULT
interp_mod(script_ctx_t
*ctx
)
1839 hres
= stack_pop_number(ctx
, &r
);
1843 hres
= stack_pop_number(ctx
, &l
);
1847 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1850 /* ECMA-262 3rd Edition 11.4.2 */
1851 static HRESULT
interp_delete(script_ctx_t
*ctx
)
1853 jsval_t objv
, namev
;
1861 namev
= stack_pop(ctx
);
1862 objv
= stack_pop(ctx
);
1864 hres
= to_object(ctx
, objv
, &obj
);
1865 jsval_release(objv
);
1867 jsval_release(namev
);
1871 hres
= to_string(ctx
, namev
, &name
);
1872 jsval_release(namev
);
1874 IDispatch_Release(obj
);
1878 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
1879 IDispatch_Release(obj
);
1880 jsstr_release(name
);
1884 return stack_push(ctx
, jsval_bool(ret
));
1887 /* ECMA-262 3rd Edition 11.4.2 */
1888 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
1890 const BSTR arg
= get_op_bstr(ctx
, 0);
1895 TRACE("%s\n", debugstr_w(arg
));
1897 hres
= identifier_eval(ctx
, arg
, &exprval
);
1901 switch(exprval
.type
) {
1902 case EXPRVAL_STACK_REF
:
1906 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1907 IDispatch_Release(exprval
.u
.idref
.disp
);
1911 case EXPRVAL_INVALID
:
1915 FIXME("Unsupported exprval\n");
1916 exprval_release(&exprval
);
1921 return stack_push(ctx
, jsval_bool(ret
));
1924 /* ECMA-262 3rd Edition 11.4.2 */
1925 static HRESULT
interp_void(script_ctx_t
*ctx
)
1930 return stack_push(ctx
, jsval_undefined());
1933 /* ECMA-262 3rd Edition 11.4.3 */
1934 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1936 switch(jsval_type(v
)) {
1938 *ret
= L
"undefined";
1946 if(get_object(v
) && (dispex
= iface_to_jsdisp(get_object(v
)))) {
1947 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? L
"function" : L
"object";
1948 jsdisp_release(dispex
);
1964 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1971 /* ECMA-262 3rd Edition 11.4.3 */
1972 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
1981 if(!stack_pop_exprval(ctx
, &ref
))
1982 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1984 hres
= exprval_propget(ctx
, &ref
, &v
);
1985 exprval_release(&ref
);
1987 return stack_push_string(ctx
, L
"unknown");
1989 hres
= typeof_string(v
, &ret
);
1994 return stack_push_string(ctx
, ret
);
1997 /* ECMA-262 3rd Edition 11.4.3 */
1998 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
2000 const BSTR arg
= get_op_bstr(ctx
, 0);
2006 TRACE("%s\n", debugstr_w(arg
));
2008 hres
= identifier_eval(ctx
, arg
, &exprval
);
2012 if(exprval
.type
== EXPRVAL_INVALID
)
2013 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2015 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2019 hres
= typeof_string(v
, &ret
);
2024 return stack_push_string(ctx
, ret
);
2027 /* ECMA-262 3rd Edition 11.4.3 */
2028 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
2037 hres
= typeof_string(v
, &ret
);
2042 return stack_push_string(ctx
, ret
);
2045 /* ECMA-262 3rd Edition 11.4.7 */
2046 static HRESULT
interp_minus(script_ctx_t
*ctx
)
2053 hres
= stack_pop_number(ctx
, &n
);
2057 return stack_push(ctx
, jsval_number(-n
));
2060 /* ECMA-262 3rd Edition 11.4.6 */
2061 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
2070 hres
= to_number(ctx
, v
, &n
);
2075 return stack_push(ctx
, jsval_number(n
));
2078 /* ECMA-262 3rd Edition 11.3.1 */
2079 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
2081 const int arg
= get_op_int(ctx
, 0);
2088 if(!stack_pop_exprval(ctx
, &ref
))
2089 return JS_E_OBJECT_EXPECTED
;
2091 hres
= exprval_propget(ctx
, &ref
, &v
);
2092 if(SUCCEEDED(hres
)) {
2095 hres
= to_number(ctx
, v
, &n
);
2097 hres
= exprval_propput(ctx
, &ref
, jsval_number(n
+(double)arg
));
2101 exprval_release(&ref
);
2105 return stack_push(ctx
, v
);
2108 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2109 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
2111 const int arg
= get_op_int(ctx
, 0);
2119 if(!stack_pop_exprval(ctx
, &ref
))
2120 return JS_E_OBJECT_EXPECTED
;
2122 hres
= exprval_propget(ctx
, &ref
, &v
);
2123 if(SUCCEEDED(hres
)) {
2126 hres
= to_number(ctx
, v
, &n
);
2128 if(SUCCEEDED(hres
)) {
2129 ret
= n
+(double)arg
;
2130 hres
= exprval_propput(ctx
, &ref
, jsval_number(ret
));
2133 exprval_release(&ref
);
2137 return stack_push(ctx
, jsval_number(ret
));
2140 /* ECMA-262 3rd Edition 11.9.3 */
2141 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
2143 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
2144 return jsval_strict_equal(lval
, rval
, ret
);
2146 /* FIXME: NULL disps should be handled in more general way */
2147 if(is_object_instance(lval
) && !get_object(lval
))
2148 return equal_values(ctx
, jsval_null(), rval
, ret
);
2149 if(is_object_instance(rval
) && !get_object(rval
))
2150 return equal_values(ctx
, lval
, jsval_null(), ret
);
2152 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
2157 if(is_string(lval
) && is_number(rval
)) {
2161 hres
= to_number(ctx
, lval
, &n
);
2165 /* FIXME: optimize */
2166 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
2169 if(is_string(rval
) && is_number(lval
)) {
2173 hres
= to_number(ctx
, rval
, &n
);
2177 /* FIXME: optimize */
2178 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
2182 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
2185 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
2188 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
2192 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
2196 hres
= equal_values(ctx
, lval
, prim
, ret
);
2197 jsval_release(prim
);
2202 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
2206 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
2210 hres
= equal_values(ctx
, prim
, rval
, ret
);
2211 jsval_release(prim
);
2220 /* ECMA-262 3rd Edition 11.9.1 */
2221 static HRESULT
interp_eq(script_ctx_t
*ctx
)
2230 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2232 hres
= equal_values(ctx
, l
, r
, &b
);
2238 return stack_push(ctx
, jsval_bool(b
));
2241 /* ECMA-262 3rd Edition 11.9.2 */
2242 static HRESULT
interp_neq(script_ctx_t
*ctx
)
2251 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2253 hres
= equal_values(ctx
, l
, r
, &b
);
2259 return stack_push(ctx
, jsval_bool(!b
));
2262 /* ECMA-262 3rd Edition 11.9.4 */
2263 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
2272 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2274 hres
= jsval_strict_equal(r
, l
, &b
);
2280 return stack_push(ctx
, jsval_bool(b
));
2283 /* ECMA-262 3rd Edition 11.9.5 */
2284 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2295 hres
= jsval_strict_equal(r
, l
, &b
);
2301 return stack_push(ctx
, jsval_bool(!b
));
2304 /* ECMA-262 3rd Edition 11.8.5 */
2305 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2311 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2315 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2321 if(is_string(l
) && is_string(r
)) {
2322 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2323 jsstr_release(get_string(l
));
2324 jsstr_release(get_string(r
));
2328 hres
= to_number(ctx
, l
, &ln
);
2331 hres
= to_number(ctx
, r
, &rn
);
2336 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2340 /* ECMA-262 3rd Edition 11.8.1 */
2341 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2350 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2352 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2358 return stack_push(ctx
, jsval_bool(b
));
2361 /* ECMA-262 3rd Edition 11.8.1 */
2362 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2371 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2373 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2379 return stack_push(ctx
, jsval_bool(b
));
2382 /* ECMA-262 3rd Edition 11.8.2 */
2383 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2392 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2394 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2400 return stack_push(ctx
, jsval_bool(b
));
2403 /* ECMA-262 3rd Edition 11.8.4 */
2404 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2413 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2415 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2421 return stack_push(ctx
, jsval_bool(b
));
2424 /* ECMA-262 3rd Edition 11.4.8 */
2425 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2434 hres
= to_int32(ctx
, v
, &i
);
2439 return stack_push(ctx
, jsval_number(~i
));
2442 /* ECMA-262 3rd Edition 11.4.9 */
2443 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2452 hres
= to_boolean(v
, &b
);
2457 return stack_push(ctx
, jsval_bool(!b
));
2460 /* ECMA-262 3rd Edition 11.7.1 */
2461 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2467 hres
= stack_pop_uint(ctx
, &r
);
2471 hres
= stack_pop_int(ctx
, &l
);
2475 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2478 /* ECMA-262 3rd Edition 11.7.2 */
2479 static HRESULT
interp_rshift(script_ctx_t
*ctx
)
2485 hres
= stack_pop_uint(ctx
, &r
);
2489 hres
= stack_pop_int(ctx
, &l
);
2493 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2496 /* ECMA-262 3rd Edition 11.7.3 */
2497 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2502 hres
= stack_pop_uint(ctx
, &r
);
2506 hres
= stack_pop_uint(ctx
, &l
);
2510 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2513 /* ECMA-262 3rd Edition 11.13.1 */
2514 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2524 if(!stack_pop_exprval(ctx
, &ref
)) {
2526 return JS_E_ILLEGAL_ASSIGN
;
2529 hres
= exprval_propput(ctx
, &ref
, v
);
2530 exprval_release(&ref
);
2536 return stack_push(ctx
, v
);
2539 /* JScript extension */
2540 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2542 const unsigned argc
= get_op_uint(ctx
, 0);
2547 TRACE("%u\n", argc
);
2549 if(!stack_topn_exprval(ctx
, argc
+1, &ref
))
2550 return JS_E_ILLEGAL_ASSIGN
;
2552 hres
= exprval_call(ctx
, &ref
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2557 stack_popn(ctx
, argc
+2);
2558 return stack_push(ctx
, v
);
2561 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2565 return stack_push(ctx
, jsval_undefined());
2568 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2570 const unsigned arg
= get_op_uint(ctx
, 0);
2578 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2580 const unsigned arg
= get_op_uint(ctx
, 0);
2588 hres
= to_boolean(v
, &b
);
2600 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2602 const unsigned arg
= get_op_uint(ctx
, 0);
2606 stack_popn(ctx
, arg
);
2610 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2612 const unsigned clear_ret
= get_op_uint(ctx
, 0);
2613 call_frame_t
*frame
= ctx
->call_ctx
;
2618 jsval_release(steal_ret(frame
));
2620 if((frame
->flags
& EXEC_CONSTRUCTOR
) && !is_object_instance(frame
->ret
)) {
2621 jsval_release(frame
->ret
);
2622 IDispatch_AddRef(frame
->this_obj
);
2623 frame
->ret
= jsval_disp(frame
->this_obj
);
2630 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2632 call_frame_t
*frame
= ctx
->call_ctx
;
2636 jsval_release(frame
->ret
);
2637 frame
->ret
= stack_pop(ctx
);
2641 static HRESULT
interp_push_acc(script_ctx_t
*ctx
)
2647 hres
= stack_push(ctx
, ctx
->acc
);
2649 ctx
->acc
= jsval_undefined();
2653 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2655 static const op_func_t op_funcs
[] = {
2656 #define X(x,a,b,c) interp_##x,
2661 static const unsigned op_move
[] = {
2662 #define X(a,x,b,c) x,
2667 static void pop_call_frame(script_ctx_t
*ctx
)
2669 call_frame_t
*frame
= ctx
->call_ctx
;
2671 frame
->stack_base
-= frame
->pop_locals
+ frame
->pop_variables
;
2673 assert(frame
->scope
== frame
->base_scope
);
2675 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2676 if(frame
->scope
&& frame
->scope
->ref
> 1) {
2677 HRESULT hres
= detach_variable_object(ctx
, frame
, TRUE
);
2679 ERR("Failed to detach variable object: %08x\n", hres
);
2682 if(frame
->arguments_obj
)
2683 detach_arguments_object(frame
->arguments_obj
);
2685 scope_release(frame
->scope
);
2687 if(frame
->pop_variables
)
2688 stack_popn(ctx
, frame
->pop_variables
);
2689 stack_popn(ctx
, frame
->pop_locals
);
2691 ctx
->call_ctx
= frame
->prev_frame
;
2693 if(frame
->function_instance
)
2694 jsdisp_release(frame
->function_instance
);
2695 if(frame
->variable_obj
)
2696 jsdisp_release(frame
->variable_obj
);
2698 IDispatch_Release(frame
->this_obj
);
2699 jsval_release(frame
->ret
);
2700 release_bytecode(frame
->bytecode
);
2704 static void print_backtrace(script_ctx_t
*ctx
)
2706 unsigned depth
= 0, i
;
2707 call_frame_t
*frame
;
2709 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
2710 WARN("%u\t", depth
);
2714 WARN("%p->", frame
->this_obj
);
2715 WARN("%s(", frame
->function
->name
? debugstr_w(frame
->function
->name
) : "[unnamed]");
2716 if(frame
->base_scope
&& frame
->base_scope
->frame
) {
2717 for(i
=0; i
< frame
->argc
; i
++) {
2718 if(i
< frame
->function
->param_cnt
)
2719 WARN("%s%s=%s", i
? ", " : "", debugstr_w(frame
->function
->params
[i
]),
2720 debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2722 WARN("%s%s", i
? ", " : "", debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2725 WARN("[detached frame]");
2729 if(!(frame
->flags
& EXEC_RETURN_TO_INTERP
)) {
2730 WARN("%u\t[native code]\n", depth
);
2736 static HRESULT
unwind_exception(script_ctx_t
*ctx
, HRESULT exception_hres
)
2738 except_frame_t
*except_frame
;
2739 jsexcept_t
*ei
= ctx
->ei
;
2740 call_frame_t
*frame
;
2745 if(WARN_ON(jscript
)) {
2746 jsdisp_t
*error_obj
;
2749 WARN("Exception %08x %s", exception_hres
, debugstr_jsval(ei
->valid_value
? ei
->value
: jsval_undefined()));
2750 if(ei
->valid_value
&& jsval_type(ei
->value
) == JSV_OBJECT
) {
2751 error_obj
= to_jsdisp(get_object(ei
->value
));
2753 hres
= jsdisp_propget_name(error_obj
, L
"message", &msg
);
2754 if(SUCCEEDED(hres
)) {
2755 WARN(" (message %s)", debugstr_jsval(msg
));
2762 print_backtrace(ctx
);
2765 frame
= ctx
->call_ctx
;
2766 if(exception_hres
!= DISP_E_EXCEPTION
)
2767 throw_error(ctx
, exception_hres
, NULL
);
2768 set_error_location(ei
, frame
->bytecode
, frame
->bytecode
->instrs
[frame
->ip
].loc
, IDS_RUNTIME_ERROR
, NULL
);
2770 while(!frame
->except_frame
) {
2773 while(frame
->scope
!= frame
->base_scope
)
2774 scope_pop(&frame
->scope
);
2776 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
2778 flags
= frame
->flags
;
2779 pop_call_frame(ctx
);
2780 if(!(flags
& EXEC_RETURN_TO_INTERP
))
2781 return DISP_E_EXCEPTION
;
2782 frame
= ctx
->call_ctx
;
2785 except_frame
= frame
->except_frame
;
2786 catch_off
= except_frame
->catch_off
;
2788 assert(except_frame
->stack_top
<= ctx
->stack_top
);
2789 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
2791 while(except_frame
->scope
!= frame
->scope
)
2792 scope_pop(&frame
->scope
);
2794 frame
->ip
= catch_off
? catch_off
: except_frame
->finally_off
;
2795 assert(!catch_off
|| frame
->bytecode
->instrs
[frame
->ip
].op
== OP_enter_catch
);
2797 if(ei
->valid_value
) {
2798 except_val
= ctx
->ei
->value
;
2799 ei
->valid_value
= FALSE
;
2802 if(!(err
= create_builtin_error(ctx
)))
2803 return E_OUTOFMEMORY
;
2804 except_val
= jsval_obj(err
);
2807 /* keep current except_frame if we're entering catch block with finally block associated */
2808 if(catch_off
&& except_frame
->finally_off
) {
2809 except_frame
->catch_off
= 0;
2811 frame
->except_frame
= except_frame
->next
;
2812 heap_free(except_frame
);
2815 hres
= stack_push(ctx
, except_val
);
2820 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2824 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, jsval_t
*r
)
2826 call_frame_t
*frame
;
2828 HRESULT hres
= S_OK
;
2833 frame
= ctx
->call_ctx
;
2834 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
2835 hres
= op_funcs
[op
](ctx
);
2837 hres
= unwind_exception(ctx
, hres
);
2840 }else if(frame
->ip
== -1) {
2841 const DWORD return_to_interp
= frame
->flags
& EXEC_RETURN_TO_INTERP
;
2843 assert(ctx
->stack_top
== frame
->stack_base
);
2844 assert(frame
->scope
== frame
->base_scope
);
2846 if(return_to_interp
) {
2847 jsval_release(ctx
->acc
);
2848 ctx
->acc
= steal_ret(frame
);
2850 *r
= steal_ret(frame
);
2852 pop_call_frame(ctx
);
2853 if(!return_to_interp
)
2856 frame
->ip
+= op_move
[op
];
2863 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
2865 IBindEventHandler
*target
;
2871 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
2875 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2879 if(!is_object_instance(v
)) {
2880 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
2884 disp
= get_object(v
);
2885 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
2886 if(SUCCEEDED(hres
)) {
2887 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
2888 IBindEventHandler_Release(target
);
2890 WARN("BindEvent failed: %08x\n", hres
);
2892 FIXME("No IBindEventHandler, not yet supported binding\n");
2895 IDispatch_Release(disp
);
2899 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
)
2901 const unsigned orig_stack
= ctx
->stack_top
;
2902 scope_chain_t
*scope
;
2907 /* If arguments are already on the stack, we may use them. */
2908 if(argv
+ argc
== ctx
->stack
+ ctx
->stack_top
) {
2909 frame
->arguments_off
= argv
- ctx
->stack
;
2912 frame
->arguments_off
= ctx
->stack_top
;
2913 for(i
= 0; i
< argc
; i
++) {
2914 hres
= jsval_copy(argv
[i
], &v
);
2916 hres
= stack_push(ctx
, v
);
2924 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2925 for(; i
< frame
->function
->param_cnt
; i
++) {
2926 hres
= stack_push(ctx
, jsval_undefined());
2928 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2933 frame
->pop_locals
= ctx
->stack_top
- orig_stack
;
2935 frame
->variables_off
= ctx
->stack_top
;
2937 for(i
= 0; i
< frame
->function
->var_cnt
; i
++) {
2938 hres
= stack_push(ctx
, jsval_undefined());
2940 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2945 frame
->pop_variables
= i
;
2947 hres
= scope_push(scope_chain
, variable_object
, to_disp(variable_object
), &scope
);
2949 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2953 for(i
= 0; i
< frame
->function
->func_cnt
; i
++) {
2954 if(frame
->function
->funcs
[i
].name
&& !frame
->function
->funcs
[i
].event_target
) {
2958 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+i
, scope
, &func_obj
);
2960 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2961 scope_release(scope
);
2965 off
= local_off(frame
, frame
->function
->funcs
[i
].local_ref
);
2966 jsval_release(ctx
->stack
[off
]);
2967 ctx
->stack
[off
] = jsval_obj(func_obj
);
2971 scope
->frame
= frame
;
2972 frame
->base_scope
= frame
->scope
= scope
;
2976 HRESULT
exec_source(script_ctx_t
*ctx
, DWORD flags
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
2977 IDispatch
*this_obj
, jsdisp_t
*function_instance
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
2979 jsdisp_t
*variable_obj
;
2980 call_frame_t
*frame
;
2985 ctx
->stack
= heap_alloc(stack_size
* sizeof(*ctx
->stack
));
2987 return E_OUTOFMEMORY
;
2990 if(bytecode
->named_item
) {
2991 if(!bytecode
->named_item
->script_obj
) {
2992 hres
= create_named_item_script_obj(ctx
, bytecode
->named_item
);
2993 if(FAILED(hres
)) return hres
;
2997 if(!ctx
->ei
->enter_notified
) {
2998 ctx
->ei
->enter_notified
= TRUE
;
2999 IActiveScriptSite_OnEnterScript(ctx
->site
);
3002 for(i
= 0; i
< function
->func_cnt
; i
++) {
3005 if(!function
->funcs
[i
].event_target
)
3008 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+i
, scope
, &func_obj
);
3012 hres
= bind_event_target(ctx
, function
->funcs
+i
, func_obj
);
3013 jsdisp_release(func_obj
);
3018 if((flags
& EXEC_EVAL
) && ctx
->call_ctx
) {
3019 variable_obj
= jsdisp_addref(ctx
->call_ctx
->variable_obj
);
3020 }else if(!(flags
& (EXEC_GLOBAL
| EXEC_EVAL
))) {
3021 hres
= create_dispex(ctx
, NULL
, NULL
, &variable_obj
);
3022 if(FAILED(hres
)) return hres
;
3023 }else if(bytecode
->named_item
) {
3024 variable_obj
= jsdisp_addref(bytecode
->named_item
->script_obj
);
3026 variable_obj
= jsdisp_addref(ctx
->global
);
3029 if(flags
& (EXEC_GLOBAL
| EXEC_EVAL
)) {
3030 named_item_t
*item
= bytecode
->named_item
;
3033 for(i
=0; i
< function
->var_cnt
; i
++) {
3034 TRACE("[%d] %s %d\n", i
, debugstr_w(function
->variables
[i
].name
), function
->variables
[i
].func_id
);
3035 if(function
->variables
[i
].func_id
!= -1) {
3038 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+function
->variables
[i
].func_id
, scope
, &func_obj
);
3042 hres
= jsdisp_propput_name(variable_obj
, function
->variables
[i
].name
, jsval_obj(func_obj
));
3043 jsdisp_release(func_obj
);
3047 if(item
&& !(item
->flags
& SCRIPTITEM_CODEONLY
)
3048 && SUCCEEDED(disp_get_id(ctx
, item
->disp
, function
->variables
[i
].name
, function
->variables
[i
].name
, 0, &id
)))
3051 if(!item
&& (flags
& EXEC_GLOBAL
) && lookup_global_members(ctx
, function
->variables
[i
].name
, NULL
))
3054 hres
= jsdisp_get_id(variable_obj
, function
->variables
[i
].name
, fdexNameEnsure
, &id
);
3060 /* ECMA-262 3rd Edition 11.2.3.7 */
3064 jsthis
= iface_to_jsdisp(this_obj
);
3066 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
3068 jsdisp_release(jsthis
);
3072 if(ctx
->call_ctx
&& (flags
& EXEC_EVAL
)) {
3073 hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
3078 frame
= heap_alloc_zero(sizeof(*frame
));
3080 hres
= E_OUTOFMEMORY
;
3084 frame
->function
= function
;
3085 frame
->ret
= jsval_undefined();
3087 frame
->bytecode
= bytecode_addref(bytecode
);
3089 if(!(flags
& (EXEC_GLOBAL
|EXEC_EVAL
))) {
3090 hres
= setup_scope(ctx
, frame
, scope
, variable_obj
, argc
, argv
);
3092 release_bytecode(frame
->bytecode
);
3097 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
3100 frame
->ip
= function
->instr_off
;
3101 frame
->stack_base
= ctx
->stack_top
;
3103 frame
->this_obj
= this_obj
;
3104 IDispatch_AddRef(frame
->this_obj
);
3107 if(function_instance
)
3108 frame
->function_instance
= jsdisp_addref(function_instance
);
3110 frame
->flags
= flags
;
3111 frame
->variable_obj
= variable_obj
;
3113 frame
->prev_frame
= ctx
->call_ctx
;
3114 ctx
->call_ctx
= frame
;
3116 if(flags
& EXEC_RETURN_TO_INTERP
) {
3118 * We're called directly from interpreter, so we may just setup call frame and return.
3119 * Already running interpreter will take care of execution.
3122 *r
= jsval_undefined();
3126 return enter_bytecode(ctx
, r
);
3129 jsdisp_release(variable_obj
);