2 * Copyright 2008,2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
30 struct _except_frame_t
{
57 static const size_t stack_size
= 0x40000;
59 static HRESULT
stack_push(script_ctx_t
*ctx
, jsval_t v
)
61 if(ctx
->stack_top
== stack_size
)
62 return JS_E_STACK_OVERFLOW
;
64 ctx
->stack
[ctx
->stack_top
++] = v
;
68 static inline HRESULT
stack_push_string(script_ctx_t
*ctx
, const WCHAR
*str
)
76 return stack_push(ctx
, jsval_string(v
));
79 static inline jsval_t
stack_top(script_ctx_t
*ctx
)
81 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
82 return ctx
->stack
[ctx
->stack_top
-1];
85 static inline jsval_t
*stack_top_ref(script_ctx_t
*ctx
, unsigned n
)
87 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
);
88 return ctx
->stack
+ctx
->stack_top
-1-n
;
91 static inline jsval_t
stack_topn(script_ctx_t
*ctx
, unsigned n
)
93 return *stack_top_ref(ctx
, n
);
96 static inline jsval_t
*stack_args(script_ctx_t
*ctx
, unsigned n
)
100 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
-1);
101 return ctx
->stack
+ ctx
->stack_top
-n
;
104 static inline jsval_t
stack_pop(script_ctx_t
*ctx
)
106 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
107 return ctx
->stack
[--ctx
->stack_top
];
110 static void stack_popn(script_ctx_t
*ctx
, unsigned n
)
113 jsval_release(stack_pop(ctx
));
116 static HRESULT
stack_pop_number(script_ctx_t
*ctx
, double *r
)
122 hres
= to_number(ctx
, v
, r
);
127 static HRESULT
stack_pop_object(script_ctx_t
*ctx
, IDispatch
**r
)
133 if(is_object_instance(v
)) {
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
)
174 hres
= stack_push(ctx
, jsval_null());
176 hres
= stack_push(ctx
, val
->u
.val
);
179 hres
= stack_push(ctx
, jsval_disp(val
->u
.idref
.disp
));
181 hres
= stack_push(ctx
, jsval_number(val
->u
.idref
.id
));
183 IDispatch_Release(val
->u
.idref
.disp
);
185 case EXPRVAL_STACK_REF
:
186 hres
= stack_push(ctx
, jsval_number(val
->u
.off
));
188 hres
= stack_push(ctx
, jsval_undefined());
190 case EXPRVAL_INVALID
:
191 hres
= stack_push(ctx
, jsval_undefined());
193 hres
= stack_push(ctx
, jsval_number(val
->u
.hres
));
201 static BOOL
stack_topn_exprval(script_ctx_t
*ctx
, unsigned n
, exprval_t
*r
)
203 jsval_t v
= stack_topn(ctx
, n
+1);
205 switch(jsval_type(v
)) {
207 call_frame_t
*frame
= ctx
->call_ctx
;
208 unsigned off
= get_number(v
);
210 if(!frame
->base_scope
->frame
&& off
>= frame
->arguments_off
) {
215 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
217 assert(off
< frame
->variables_off
+ frame
->function
->var_cnt
);
218 name
= off
>= frame
->variables_off
219 ? frame
->function
->variables
[off
- frame
->variables_off
].name
220 : frame
->function
->params
[off
- frame
->arguments_off
];
221 hres
= jsdisp_get_id(ctx
->call_ctx
->base_scope
->jsobj
, name
, 0, &id
);
223 r
->type
= EXPRVAL_INVALID
;
228 *stack_top_ref(ctx
, n
+1) = jsval_obj(jsdisp_addref(frame
->base_scope
->jsobj
));
229 *stack_top_ref(ctx
, n
) = jsval_number(id
);
230 r
->type
= EXPRVAL_IDREF
;
231 r
->u
.idref
.disp
= frame
->base_scope
->obj
;
236 r
->type
= EXPRVAL_STACK_REF
;
241 r
->type
= EXPRVAL_IDREF
;
242 r
->u
.idref
.disp
= get_object(v
);
243 assert(is_number(stack_topn(ctx
, n
)));
244 r
->u
.idref
.id
= get_number(stack_topn(ctx
, n
));
247 r
->type
= EXPRVAL_INVALID
;
248 assert(is_number(stack_topn(ctx
, n
)));
249 r
->u
.hres
= get_number(stack_topn(ctx
, n
));
252 r
->type
= EXPRVAL_JSVAL
;
253 r
->u
.val
= stack_topn(ctx
, n
);
261 static inline BOOL
stack_pop_exprval(script_ctx_t
*ctx
, exprval_t
*r
)
263 BOOL ret
= stack_topn_exprval(ctx
, 0, r
);
268 static HRESULT
exprval_propput(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t v
)
271 case EXPRVAL_STACK_REF
: {
272 jsval_t
*r
= ctx
->stack
+ ref
->u
.off
;
274 return jsval_copy(v
, r
);
277 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
);
279 WARN("ignoring an attempt to set value reference\n");
287 static HRESULT
exprval_propget(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
290 case EXPRVAL_STACK_REF
:
291 return jsval_copy(ctx
->stack
[ref
->u
.off
], r
);
293 return disp_propget(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, r
);
295 return jsval_copy(ref
->u
.val
, r
);
302 static HRESULT
exprval_call(script_ctx_t
*ctx
, exprval_t
*ref
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
305 case EXPRVAL_STACK_REF
: {
306 jsval_t v
= ctx
->stack
[ref
->u
.off
];
308 if(!is_object_instance(v
)) {
309 FIXME("invoke %s\n", debugstr_jsval(v
));
313 return disp_call_value(ctx
, get_object(v
), NULL
, flags
, argc
, argv
, r
);
316 return disp_call(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, flags
, argc
, argv
, r
);
317 case EXPRVAL_JSVAL
: {
321 hres
= to_object(ctx
, ref
->u
.val
, &obj
);
322 if(SUCCEEDED(hres
)) {
323 hres
= disp_call_value(ctx
, obj
, NULL
, flags
, argc
, argv
, r
);
324 IDispatch_Release(obj
);
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 IDispatch_AddRef(ref
->u
.idref
.disp
= obj
);
378 ref
->u
.idref
.id
= id
;
381 static inline jsval_t
steal_ret(call_frame_t
*frame
)
383 jsval_t r
= frame
->ret
;
384 frame
->ret
= jsval_undefined();
388 static inline void clear_acc(script_ctx_t
*ctx
)
390 jsval_release(ctx
->acc
);
391 ctx
->acc
= jsval_undefined();
394 static HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
396 scope_chain_t
*new_scope
;
398 new_scope
= heap_alloc(sizeof(scope_chain_t
));
400 return E_OUTOFMEMORY
;
404 IDispatch_AddRef(obj
);
405 new_scope
->jsobj
= jsobj
;
406 new_scope
->obj
= obj
;
407 new_scope
->frame
= NULL
;
408 new_scope
->next
= scope
? scope_addref(scope
) : NULL
;
414 static void scope_pop(scope_chain_t
**scope
)
423 void scope_release(scope_chain_t
*scope
)
429 scope_release(scope
->next
);
431 IDispatch_Release(scope
->obj
);
435 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
442 jsdisp
= iface_to_jsdisp(disp
);
444 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
445 jsdisp_release(jsdisp
);
452 bstr
= SysAllocString(name
);
454 return E_OUTOFMEMORY
;
458 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
459 if(SUCCEEDED(hres
)) {
460 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
461 IDispatchEx_Release(dispex
);
463 TRACE("using IDispatch\n");
464 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
467 if(name_bstr
!= bstr
)
472 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
474 IObjectIdentity
*identity
;
475 IUnknown
*unk1
, *unk2
;
483 if(!disp1
|| !disp2
) {
488 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
492 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
494 IUnknown_Release(unk1
);
501 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
502 if(SUCCEEDED(hres
)) {
503 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
504 IObjectIdentity_Release(identity
);
511 IUnknown_Release(unk1
);
512 IUnknown_Release(unk2
);
516 /* ECMA-262 3rd Edition 11.9.6 */
517 HRESULT
jsval_strict_equal(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
519 jsval_type_t type
= jsval_type(lval
);
523 if(type
!= jsval_type(rval
)) {
524 if(is_null_instance(lval
))
525 *ret
= is_null_instance(rval
);
537 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
539 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
542 *ret
= get_number(lval
) == get_number(rval
);
545 *ret
= !get_bool(lval
) == !get_bool(rval
);
548 WARN("VARIANT type, returning false\n");
557 * Transfers local variables from stack to variable object.
558 * It's slow, so we want to avoid it as much as possible.
560 static HRESULT
detach_variable_object(script_ctx_t
*ctx
, call_frame_t
*frame
, BOOL from_release
)
565 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
568 TRACE("detaching %p\n", frame
);
570 assert(frame
== frame
->base_scope
->frame
);
571 assert(frame
->variable_obj
== frame
->base_scope
->jsobj
);
573 if(!from_release
&& !frame
->arguments_obj
) {
574 hres
= setup_arguments_object(ctx
, frame
);
579 frame
->base_scope
->frame
= NULL
;
581 for(i
= 0; i
< frame
->function
->locals_cnt
; i
++) {
582 hres
= jsdisp_propput_name(frame
->variable_obj
, frame
->function
->locals
[i
].name
,
583 ctx
->stack
[local_off(frame
, frame
->function
->locals
[i
].ref
)]);
591 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
597 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
598 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
599 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
600 if(SUCCEEDED(hres
)) {
602 exprval_set_disp_ref(ret
, item
->disp
, id
);
611 IDispatch
*lookup_global_host(script_ctx_t
*ctx
)
613 IDispatch
*disp
= NULL
;
616 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
617 if(!(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) continue;
621 if(!disp
) disp
= to_disp(ctx
->global
);
626 static int __cdecl
local_ref_cmp(const void *key
, const void *ref
)
628 return wcscmp((const WCHAR
*)key
, ((const local_ref_t
*)ref
)->name
);
631 local_ref_t
*lookup_local(const function_code_t
*function
, const WCHAR
*identifier
)
633 return bsearch(identifier
, function
->locals
, function
->locals_cnt
, sizeof(*function
->locals
), local_ref_cmp
);
636 /* ECMA-262 3rd Edition 10.1.4 */
637 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
639 scope_chain_t
*scope
;
644 TRACE("%s\n", debugstr_w(identifier
));
647 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
649 function_code_t
*func
= scope
->frame
->function
;
650 local_ref_t
*ref
= lookup_local(func
, identifier
);
653 ret
->type
= EXPRVAL_STACK_REF
;
654 ret
->u
.off
= local_off(scope
->frame
, ref
->ref
);
655 TRACE("returning ref %d for %d\n", ret
->u
.off
, ref
->ref
);
659 if(!wcscmp(identifier
, L
"arguments")) {
660 hres
= detach_variable_object(ctx
, scope
->frame
, FALSE
);
666 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
668 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
669 if(SUCCEEDED(hres
)) {
670 exprval_set_disp_ref(ret
, scope
->obj
, id
);
675 item
= ctx
->call_ctx
->bytecode
->named_item
;
677 hres
= jsdisp_get_id(item
->script_obj
, identifier
, 0, &id
);
678 if(SUCCEEDED(hres
)) {
679 exprval_set_disp_ref(ret
, to_disp(item
->script_obj
), id
);
682 if(!(item
->flags
& SCRIPTITEM_CODEONLY
)) {
683 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
684 if(SUCCEEDED(hres
)) {
685 exprval_set_disp_ref(ret
, item
->disp
, id
);
692 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
693 if(SUCCEEDED(hres
)) {
694 exprval_set_disp_ref(ret
, to_disp(ctx
->global
), id
);
698 item
= lookup_named_item(ctx
, identifier
, SCRIPTITEM_ISVISIBLE
);
700 IDispatch_AddRef(item
->disp
);
701 ret
->type
= EXPRVAL_JSVAL
;
702 ret
->u
.val
= jsval_disp(item
->disp
);
706 if(lookup_global_members(ctx
, identifier
, ret
))
709 exprval_set_exception(ret
, JS_E_UNDEFINED_VARIABLE
);
713 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
715 call_frame_t
*frame
= ctx
->call_ctx
;
716 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
719 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
721 call_frame_t
*frame
= ctx
->call_ctx
;
722 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
725 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
727 call_frame_t
*frame
= ctx
->call_ctx
;
728 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
731 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
733 call_frame_t
*frame
= ctx
->call_ctx
;
734 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
737 static inline double get_op_double(script_ctx_t
*ctx
)
739 call_frame_t
*frame
= ctx
->call_ctx
;
740 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
743 static inline void jmp_next(script_ctx_t
*ctx
)
748 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
750 ctx
->call_ctx
->ip
= dst
;
753 /* ECMA-262 3rd Edition 12.6.4 */
754 static HRESULT
interp_forin(script_ctx_t
*ctx
)
756 const HRESULT arg
= get_op_uint(ctx
, 0);
757 IDispatch
*obj
= NULL
;
766 assert(is_number(stack_top(ctx
)));
767 id
= get_number(stack_top(ctx
));
769 if(!stack_topn_exprval(ctx
, 1, &prop_ref
)) {
770 FIXME("invalid ref: %08x\n", prop_ref
.u
.hres
);
774 if(is_object_instance(stack_topn(ctx
, 3)))
775 obj
= get_object(stack_topn(ctx
, 3));
778 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
779 if(SUCCEEDED(hres
)) {
780 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
782 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
783 IDispatchEx_Release(dispex
);
787 TRACE("No IDispatchEx\n");
794 str
= jsstr_alloc_len(name
, SysStringLen(name
));
797 return E_OUTOFMEMORY
;
800 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
802 hres
= exprval_propput(ctx
, &prop_ref
, jsval_string(str
));
815 /* ECMA-262 3rd Edition 12.10 */
816 static HRESULT
interp_push_scope(script_ctx_t
*ctx
)
825 hres
= to_object(ctx
, v
, &disp
);
830 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
831 IDispatch_Release(disp
);
835 /* ECMA-262 3rd Edition 12.10 */
836 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
840 scope_pop(&ctx
->call_ctx
->scope
);
844 /* ECMA-262 3rd Edition 12.13 */
845 static HRESULT
interp_case(script_ctx_t
*ctx
)
847 const unsigned arg
= get_op_uint(ctx
, 0);
855 hres
= jsval_strict_equal(stack_top(ctx
), v
, &b
);
869 static void set_error_value(script_ctx_t
*ctx
, jsval_t value
)
871 jsexcept_t
*ei
= ctx
->ei
;
875 ei
->error
= JS_E_EXCEPTION_THROWN
;
876 ei
->valid_value
= TRUE
;
879 if(is_object_instance(value
) && get_object(value
) && (obj
= to_jsdisp(get_object(value
)))) {
885 /* FIXME: We should check if object is an error instance */
887 hres
= jsdisp_propget_name(obj
, L
"number", &v
);
888 if(SUCCEEDED(hres
)) {
889 hres
= to_uint32(ctx
, v
, &number
);
891 ei
->error
= FAILED(number
) ? number
: E_FAIL
;
895 hres
= jsdisp_propget_name(obj
, L
"description", &v
);
896 if(SUCCEEDED(hres
)) {
897 hres
= to_string(ctx
, v
, &str
);
905 /* ECMA-262 3rd Edition 12.13 */
906 static HRESULT
interp_throw(script_ctx_t
*ctx
)
910 set_error_value(ctx
, stack_pop(ctx
));
911 return DISP_E_EXCEPTION
;
914 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
916 const HRESULT arg
= get_op_uint(ctx
, 0);
918 TRACE("%08x\n", arg
);
923 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
925 const HRESULT hres
= get_op_uint(ctx
, 0);
926 jsstr_t
*str
= get_op_str(ctx
, 1);
929 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
931 ptr
= jsstr_flatten(str
);
932 return ptr
? throw_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
935 /* ECMA-262 3rd Edition 12.14 */
936 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
938 const unsigned catch_off
= get_op_uint(ctx
, 0);
939 const unsigned finally_off
= get_op_uint(ctx
, 1);
940 call_frame_t
*frame
= ctx
->call_ctx
;
941 except_frame_t
*except
;
945 except
= heap_alloc(sizeof(*except
));
947 return E_OUTOFMEMORY
;
949 except
->stack_top
= ctx
->stack_top
;
950 except
->scope
= frame
->scope
;
951 except
->catch_off
= catch_off
;
952 except
->finally_off
= finally_off
;
953 except
->next
= frame
->except_frame
;
954 frame
->except_frame
= except
;
958 /* ECMA-262 3rd Edition 12.14 */
959 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
961 const unsigned ret_off
= get_op_uint(ctx
, 0);
962 call_frame_t
*frame
= ctx
->call_ctx
;
963 except_frame_t
*except
;
964 unsigned finally_off
;
966 TRACE("%u\n", ret_off
);
968 except
= frame
->except_frame
;
969 assert(except
!= NULL
);
971 finally_off
= except
->finally_off
;
972 frame
->except_frame
= except
->next
;
978 hres
= stack_push(ctx
, jsval_number(ret_off
));
981 hres
= stack_push(ctx
, jsval_bool(TRUE
));
984 frame
->ip
= finally_off
;
992 /* ECMA-262 3rd Edition 12.14 */
993 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
995 call_frame_t
*frame
= ctx
->call_ctx
;
1004 TRACE("passing exception\n");
1006 set_error_value(ctx
, stack_pop(ctx
));
1007 return DISP_E_EXCEPTION
;
1011 assert(is_number(v
));
1012 frame
->ip
= get_number(v
);
1016 static HRESULT
interp_enter_catch(script_ctx_t
*ctx
)
1018 const BSTR ident
= get_op_bstr(ctx
, 0);
1019 jsdisp_t
*scope_obj
;
1023 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
1028 hres
= jsdisp_propput_name(scope_obj
, ident
, v
);
1031 hres
= scope_push(ctx
->call_ctx
->scope
, scope_obj
, to_disp(scope_obj
), &ctx
->call_ctx
->scope
);
1032 jsdisp_release(scope_obj
);
1036 /* ECMA-262 3rd Edition 13 */
1037 static HRESULT
interp_func(script_ctx_t
*ctx
)
1039 unsigned func_idx
= get_op_uint(ctx
, 0);
1040 call_frame_t
*frame
= ctx
->call_ctx
;
1044 TRACE("%d\n", func_idx
);
1046 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
1047 frame
->scope
, &dispex
);
1051 return stack_push(ctx
, jsval_obj(dispex
));
1054 /* ECMA-262 3rd Edition 11.2.1 */
1055 static HRESULT
interp_array(script_ctx_t
*ctx
)
1066 namev
= stack_pop(ctx
);
1068 hres
= stack_pop_object(ctx
, &obj
);
1070 jsval_release(namev
);
1074 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1075 jsval_release(namev
);
1077 IDispatch_Release(obj
);
1081 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
1082 jsstr_release(name_str
);
1083 if(SUCCEEDED(hres
)) {
1084 hres
= disp_propget(ctx
, obj
, id
, &v
);
1085 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1086 v
= jsval_undefined();
1089 IDispatch_Release(obj
);
1093 return stack_push(ctx
, v
);
1096 /* ECMA-262 3rd Edition 11.2.1 */
1097 static HRESULT
interp_member(script_ctx_t
*ctx
)
1099 const BSTR arg
= get_op_bstr(ctx
, 0);
1107 hres
= stack_pop_object(ctx
, &obj
);
1111 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
1112 if(SUCCEEDED(hres
)) {
1113 hres
= disp_propget(ctx
, obj
, id
, &v
);
1114 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1115 v
= jsval_undefined();
1118 IDispatch_Release(obj
);
1122 return stack_push(ctx
, v
);
1125 /* ECMA-262 3rd Edition 11.2.1 */
1126 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
1128 const unsigned arg
= get_op_uint(ctx
, 0);
1129 jsval_t objv
, namev
;
1139 namev
= stack_pop(ctx
);
1140 objv
= stack_pop(ctx
);
1142 hres
= to_object(ctx
, objv
, &obj
);
1143 jsval_release(objv
);
1144 if(SUCCEEDED(hres
)) {
1145 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1147 IDispatch_Release(obj
);
1149 jsval_release(namev
);
1153 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
1154 jsstr_release(name_str
);
1155 if(SUCCEEDED(hres
)) {
1156 ref
.type
= EXPRVAL_IDREF
;
1157 ref
.u
.idref
.disp
= obj
;
1158 ref
.u
.idref
.id
= id
;
1160 IDispatch_Release(obj
);
1161 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1162 exprval_set_exception(&ref
, JS_E_INVALID_PROPERTY
);
1165 ERR("failed %08x\n", hres
);
1170 return stack_push_exprval(ctx
, &ref
);
1173 /* ECMA-262 3rd Edition 11.2.1 */
1174 static HRESULT
interp_refval(script_ctx_t
*ctx
)
1182 if(!stack_topn_exprval(ctx
, 0, &ref
))
1183 return JS_E_ILLEGAL_ASSIGN
;
1185 hres
= exprval_propget(ctx
, &ref
, &v
);
1189 return stack_push(ctx
, v
);
1192 /* ECMA-262 3rd Edition 11.2.2 */
1193 static HRESULT
interp_new(script_ctx_t
*ctx
)
1195 const unsigned argc
= get_op_uint(ctx
, 0);
1198 TRACE("%d\n", argc
);
1200 constr
= stack_topn(ctx
, argc
);
1202 /* NOTE: Should use to_object here */
1205 return JS_E_OBJECT_EXPECTED
;
1206 else if(!is_object_instance(constr
))
1207 return JS_E_INVALID_ACTION
;
1208 else if(!get_object(constr
))
1209 return JS_E_INVALID_PROPERTY
;
1212 return disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1213 argc
, stack_args(ctx
, argc
), &ctx
->acc
);
1216 /* ECMA-262 3rd Edition 11.2.3 */
1217 static HRESULT
interp_call(script_ctx_t
*ctx
)
1219 const unsigned argn
= get_op_uint(ctx
, 0);
1220 const int do_ret
= get_op_int(ctx
, 1);
1223 TRACE("%d %d\n", argn
, do_ret
);
1225 obj
= stack_topn(ctx
, argn
);
1226 if(!is_object_instance(obj
))
1227 return JS_E_INVALID_PROPERTY
;
1230 return disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1231 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1234 /* ECMA-262 3rd Edition 11.2.3 */
1235 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
1237 const unsigned argn
= get_op_uint(ctx
, 0);
1238 const int do_ret
= get_op_int(ctx
, 1);
1241 TRACE("%d %d\n", argn
, do_ret
);
1243 if(!stack_topn_exprval(ctx
, argn
, &ref
))
1247 return exprval_call(ctx
, &ref
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1248 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1251 /* ECMA-262 3rd Edition 11.1.1 */
1252 static HRESULT
interp_this(script_ctx_t
*ctx
)
1254 IDispatch
*this_obj
= ctx
->call_ctx
->this_obj
;
1259 named_item_t
*item
= ctx
->call_ctx
->bytecode
->named_item
;
1262 this_obj
= (item
->flags
& SCRIPTITEM_CODEONLY
) ? to_disp(item
->script_obj
) : item
->disp
;
1264 this_obj
= lookup_global_host(ctx
);
1267 IDispatch_AddRef(this_obj
);
1268 return stack_push(ctx
, jsval_disp(this_obj
));
1271 static HRESULT
interp_identifier_ref(script_ctx_t
*ctx
, BSTR identifier
, unsigned flags
)
1276 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1280 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1281 jsdisp_t
*script_obj
= ctx
->global
;
1284 if(ctx
->call_ctx
->bytecode
->named_item
)
1285 script_obj
= ctx
->call_ctx
->bytecode
->named_item
->script_obj
;
1287 hres
= jsdisp_get_id(script_obj
, identifier
, fdexNameEnsure
, &id
);
1291 exprval_set_disp_ref(&exprval
, to_disp(script_obj
), id
);
1294 if(exprval
.type
== EXPRVAL_JSVAL
|| exprval
.type
== EXPRVAL_INVALID
) {
1295 WARN("invalid ref\n");
1296 exprval_release(&exprval
);
1297 exprval_set_exception(&exprval
, JS_E_OBJECT_EXPECTED
);
1300 return stack_push_exprval(ctx
, &exprval
);
1303 static HRESULT
identifier_value(script_ctx_t
*ctx
, BSTR identifier
)
1309 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1313 if(exprval
.type
== EXPRVAL_INVALID
)
1314 return throw_error(ctx
, exprval
.u
.hres
, identifier
);
1316 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1320 return stack_push(ctx
, v
);
1323 static HRESULT
interp_local_ref(script_ctx_t
*ctx
)
1325 const int arg
= get_op_int(ctx
, 0);
1326 const unsigned flags
= get_op_uint(ctx
, 1);
1327 call_frame_t
*frame
= ctx
->call_ctx
;
1330 TRACE("%s\n", debugstr_w(local_name(frame
, arg
)));
1332 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1333 return interp_identifier_ref(ctx
, local_name(frame
, arg
), flags
);
1335 ref
.type
= EXPRVAL_STACK_REF
;
1336 ref
.u
.off
= local_off(frame
, arg
);
1337 return stack_push_exprval(ctx
, &ref
);
1340 static HRESULT
interp_local(script_ctx_t
*ctx
)
1342 const int arg
= get_op_int(ctx
, 0);
1343 call_frame_t
*frame
= ctx
->call_ctx
;
1347 if(!frame
->base_scope
|| !frame
->base_scope
->frame
) {
1348 TRACE("%s\n", debugstr_w(local_name(frame
, arg
)));
1349 return identifier_value(ctx
, local_name(frame
, arg
));
1352 hres
= jsval_copy(ctx
->stack
[local_off(frame
, arg
)], ©
);
1356 TRACE("%s: %s\n", debugstr_w(local_name(frame
, arg
)), debugstr_jsval(copy
));
1357 return stack_push(ctx
, copy
);
1360 /* ECMA-262 3rd Edition 10.1.4 */
1361 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1363 const BSTR arg
= get_op_bstr(ctx
, 0);
1365 TRACE("%s\n", debugstr_w(arg
));
1367 return identifier_value(ctx
, arg
);
1370 /* ECMA-262 3rd Edition 10.1.4 */
1371 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1373 const BSTR arg
= get_op_bstr(ctx
, 0);
1374 const unsigned flags
= get_op_uint(ctx
, 1);
1376 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1378 return interp_identifier_ref(ctx
, arg
, flags
);
1381 /* ECMA-262 3rd Edition 7.8.1 */
1382 static HRESULT
interp_null(script_ctx_t
*ctx
)
1386 return stack_push(ctx
, jsval_null());
1389 /* ECMA-262 3rd Edition 7.8.2 */
1390 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1392 const int arg
= get_op_int(ctx
, 0);
1394 TRACE("%s\n", arg
? "true" : "false");
1396 return stack_push(ctx
, jsval_bool(arg
));
1399 /* ECMA-262 3rd Edition 7.8.3 */
1400 static HRESULT
interp_int(script_ctx_t
*ctx
)
1402 const int arg
= get_op_int(ctx
, 0);
1406 return stack_push(ctx
, jsval_number(arg
));
1409 /* ECMA-262 3rd Edition 7.8.3 */
1410 static HRESULT
interp_double(script_ctx_t
*ctx
)
1412 const double arg
= get_op_double(ctx
);
1414 TRACE("%lf\n", arg
);
1416 return stack_push(ctx
, jsval_number(arg
));
1419 /* ECMA-262 3rd Edition 7.8.4 */
1420 static HRESULT
interp_str(script_ctx_t
*ctx
)
1422 jsstr_t
*str
= get_op_str(ctx
, 0);
1424 TRACE("%s\n", debugstr_jsstr(str
));
1426 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1429 /* ECMA-262 3rd Edition 7.8 */
1430 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1432 jsstr_t
*source
= get_op_str(ctx
, 0);
1433 const unsigned flags
= get_op_uint(ctx
, 1);
1437 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1439 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1443 return stack_push(ctx
, jsval_obj(regexp
));
1446 /* ECMA-262 3rd Edition 11.1.4 */
1447 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1449 const unsigned arg
= get_op_uint(ctx
, 0);
1455 hres
= create_array(ctx
, arg
, &array
);
1459 return stack_push(ctx
, jsval_obj(array
));
1462 static HRESULT
interp_carray_set(script_ctx_t
*ctx
)
1464 const unsigned index
= get_op_uint(ctx
, 0);
1465 jsval_t value
, array
;
1468 value
= stack_pop(ctx
);
1470 TRACE("[%u] = %s\n", index
, debugstr_jsval(value
));
1472 array
= stack_top(ctx
);
1473 assert(is_object_instance(array
));
1475 hres
= jsdisp_propput_idx(iface_to_jsdisp(get_object(array
)), index
, value
);
1476 jsval_release(value
);
1480 /* ECMA-262 3rd Edition 11.1.5 */
1481 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1488 hres
= create_object(ctx
, NULL
, &obj
);
1492 return stack_push(ctx
, jsval_obj(obj
));
1495 /* ECMA-262 3rd Edition 11.1.5 */
1496 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1498 jsstr_t
*name_arg
= get_op_str(ctx
, 0);
1499 unsigned type
= get_op_uint(ctx
, 1);
1505 TRACE("%s\n", debugstr_jsstr(name_arg
));
1507 val
= stack_pop(ctx
);
1509 /* FIXME: we should pass it as jsstr_t */
1510 name
= jsstr_flatten(name_arg
);
1512 assert(is_object_instance(stack_top(ctx
)));
1513 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1515 if(type
== PROPERTY_DEFINITION_VALUE
) {
1516 hres
= jsdisp_propput_name(obj
, name
, val
);
1518 property_desc_t desc
= {PROPF_ENUMERABLE
| PROPF_CONFIGURABLE
};
1521 assert(is_object_instance(val
));
1522 func
= iface_to_jsdisp(get_object(val
));
1524 desc
.mask
= desc
.flags
;
1525 if(type
== PROPERTY_DEFINITION_GETTER
) {
1526 desc
.explicit_getter
= TRUE
;
1529 desc
.explicit_setter
= TRUE
;
1533 hres
= jsdisp_define_property(obj
, name
, &desc
);
1534 jsdisp_release(func
);
1541 /* ECMA-262 3rd Edition 11.11 */
1542 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1544 const unsigned arg
= get_op_uint(ctx
, 0);
1550 hres
= to_boolean(stack_top(ctx
), &b
);
1563 /* ECMA-262 3rd Edition 11.11 */
1564 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1566 const unsigned arg
= get_op_uint(ctx
, 0);
1572 hres
= to_boolean(stack_top(ctx
), &b
);
1585 /* ECMA-262 3rd Edition 11.10 */
1586 static HRESULT
interp_or(script_ctx_t
*ctx
)
1593 hres
= stack_pop_int(ctx
, &r
);
1597 hres
= stack_pop_int(ctx
, &l
);
1601 return stack_push(ctx
, jsval_number(l
|r
));
1604 /* ECMA-262 3rd Edition 11.10 */
1605 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1612 hres
= stack_pop_int(ctx
, &r
);
1616 hres
= stack_pop_int(ctx
, &l
);
1620 return stack_push(ctx
, jsval_number(l
^r
));
1623 /* ECMA-262 3rd Edition 11.10 */
1624 static HRESULT
interp_and(script_ctx_t
*ctx
)
1631 hres
= stack_pop_int(ctx
, &r
);
1635 hres
= stack_pop_int(ctx
, &l
);
1639 return stack_push(ctx
, jsval_number(l
&r
));
1642 /* ECMA-262 3rd Edition 11.8.6 */
1643 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1645 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1651 if(!is_object_instance(v
) || !get_object(v
)) {
1653 return JS_E_FUNCTION_EXPECTED
;
1656 obj
= iface_to_jsdisp(get_object(v
));
1657 IDispatch_Release(get_object(v
));
1659 FIXME("non-jsdisp objects not supported\n");
1663 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1664 hres
= jsdisp_propget_name(obj
, L
"prototype", &prot
);
1666 hres
= JS_E_FUNCTION_EXPECTED
;
1668 jsdisp_release(obj
);
1674 if(is_object_instance(prot
)) {
1675 if(is_object_instance(v
))
1676 tmp
= iface_to_jsdisp(get_object(v
));
1677 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1678 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1684 jsdisp_release(tmp
);
1686 FIXME("prototype is not an object\n");
1690 jsval_release(prot
);
1695 return stack_push(ctx
, jsval_bool(ret
));
1698 /* ECMA-262 3rd Edition 11.8.7 */
1699 static HRESULT
interp_in(script_ctx_t
*ctx
)
1710 obj
= stack_pop(ctx
);
1711 if(!is_object_instance(obj
) || !get_object(obj
)) {
1713 return JS_E_OBJECT_EXPECTED
;
1717 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1720 IDispatch_Release(get_object(obj
));
1724 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1725 IDispatch_Release(get_object(obj
));
1726 jsstr_release(jsstr
);
1729 else if(hres
== DISP_E_UNKNOWNNAME
)
1734 return stack_push(ctx
, jsval_bool(ret
));
1737 /* ECMA-262 3rd Edition 11.6.1 */
1738 static HRESULT
interp_add(script_ctx_t
*ctx
)
1740 jsval_t l
, r
, lval
, rval
, ret
;
1743 rval
= stack_pop(ctx
);
1744 lval
= stack_pop(ctx
);
1746 TRACE("%s + %s\n", debugstr_jsval(lval
), debugstr_jsval(rval
));
1748 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1749 if(SUCCEEDED(hres
)) {
1750 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1754 jsval_release(lval
);
1755 jsval_release(rval
);
1759 if(is_string(l
) || is_string(r
)) {
1760 jsstr_t
*lstr
, *rstr
= NULL
;
1762 hres
= to_string(ctx
, l
, &lstr
);
1764 hres
= to_string(ctx
, r
, &rstr
);
1766 if(SUCCEEDED(hres
)) {
1769 ret_str
= jsstr_concat(lstr
, rstr
);
1771 ret
= jsval_string(ret_str
);
1773 hres
= E_OUTOFMEMORY
;
1776 jsstr_release(lstr
);
1778 jsstr_release(rstr
);
1782 hres
= to_number(ctx
, l
, &nl
);
1783 if(SUCCEEDED(hres
)) {
1784 hres
= to_number(ctx
, r
, &nr
);
1786 ret
= jsval_number(nl
+nr
);
1795 return stack_push(ctx
, ret
);
1798 /* ECMA-262 3rd Edition 11.6.2 */
1799 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1806 hres
= stack_pop_number(ctx
, &r
);
1810 hres
= stack_pop_number(ctx
, &l
);
1814 return stack_push(ctx
, jsval_number(l
-r
));
1817 /* ECMA-262 3rd Edition 11.5.1 */
1818 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1825 hres
= stack_pop_number(ctx
, &r
);
1829 hres
= stack_pop_number(ctx
, &l
);
1833 return stack_push(ctx
, jsval_number(l
*r
));
1836 /* ECMA-262 3rd Edition 11.5.2 */
1837 static HRESULT
interp_div(script_ctx_t
*ctx
)
1844 hres
= stack_pop_number(ctx
, &r
);
1848 hres
= stack_pop_number(ctx
, &l
);
1852 return stack_push(ctx
, jsval_number(l
/r
));
1855 /* ECMA-262 3rd Edition 11.5.3 */
1856 static HRESULT
interp_mod(script_ctx_t
*ctx
)
1863 hres
= stack_pop_number(ctx
, &r
);
1867 hres
= stack_pop_number(ctx
, &l
);
1871 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1874 /* ECMA-262 3rd Edition 11.4.2 */
1875 static HRESULT
interp_delete(script_ctx_t
*ctx
)
1877 jsval_t objv
, namev
;
1885 namev
= stack_pop(ctx
);
1886 objv
= stack_pop(ctx
);
1888 hres
= to_object(ctx
, objv
, &obj
);
1889 jsval_release(objv
);
1891 jsval_release(namev
);
1895 hres
= to_string(ctx
, namev
, &name
);
1896 jsval_release(namev
);
1898 IDispatch_Release(obj
);
1902 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
1903 IDispatch_Release(obj
);
1904 jsstr_release(name
);
1908 return stack_push(ctx
, jsval_bool(ret
));
1911 /* ECMA-262 3rd Edition 11.4.2 */
1912 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
1914 const BSTR arg
= get_op_bstr(ctx
, 0);
1919 TRACE("%s\n", debugstr_w(arg
));
1921 hres
= identifier_eval(ctx
, arg
, &exprval
);
1925 switch(exprval
.type
) {
1926 case EXPRVAL_STACK_REF
:
1930 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1931 IDispatch_Release(exprval
.u
.idref
.disp
);
1935 case EXPRVAL_INVALID
:
1939 FIXME("Unsupported exprval\n");
1940 exprval_release(&exprval
);
1945 return stack_push(ctx
, jsval_bool(ret
));
1948 /* ECMA-262 3rd Edition 11.4.2 */
1949 static HRESULT
interp_void(script_ctx_t
*ctx
)
1954 return stack_push(ctx
, jsval_undefined());
1957 /* ECMA-262 3rd Edition 11.4.3 */
1958 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1960 switch(jsval_type(v
)) {
1962 *ret
= L
"undefined";
1970 if(get_object(v
) && (dispex
= iface_to_jsdisp(get_object(v
)))) {
1971 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? L
"function" : L
"object";
1972 jsdisp_release(dispex
);
1988 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1995 /* ECMA-262 3rd Edition 11.4.3 */
1996 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
2005 if(!stack_pop_exprval(ctx
, &ref
))
2006 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2008 hres
= exprval_propget(ctx
, &ref
, &v
);
2009 exprval_release(&ref
);
2011 return stack_push_string(ctx
, L
"unknown");
2013 hres
= typeof_string(v
, &ret
);
2018 return stack_push_string(ctx
, ret
);
2021 /* ECMA-262 3rd Edition 11.4.3 */
2022 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
2024 const BSTR arg
= get_op_bstr(ctx
, 0);
2030 TRACE("%s\n", debugstr_w(arg
));
2032 hres
= identifier_eval(ctx
, arg
, &exprval
);
2036 if(exprval
.type
== EXPRVAL_INVALID
)
2037 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2039 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2043 hres
= typeof_string(v
, &ret
);
2048 return stack_push_string(ctx
, ret
);
2051 /* ECMA-262 3rd Edition 11.4.3 */
2052 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
2061 hres
= typeof_string(v
, &ret
);
2066 return stack_push_string(ctx
, ret
);
2069 /* ECMA-262 3rd Edition 11.4.7 */
2070 static HRESULT
interp_minus(script_ctx_t
*ctx
)
2077 hres
= stack_pop_number(ctx
, &n
);
2081 return stack_push(ctx
, jsval_number(-n
));
2084 /* ECMA-262 3rd Edition 11.4.6 */
2085 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
2094 hres
= to_number(ctx
, v
, &n
);
2099 return stack_push(ctx
, jsval_number(n
));
2102 /* ECMA-262 3rd Edition 11.3.1 */
2103 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
2105 const int arg
= get_op_int(ctx
, 0);
2112 if(!stack_pop_exprval(ctx
, &ref
))
2113 return JS_E_OBJECT_EXPECTED
;
2115 hres
= exprval_propget(ctx
, &ref
, &v
);
2116 if(SUCCEEDED(hres
)) {
2119 hres
= to_number(ctx
, v
, &n
);
2121 hres
= exprval_propput(ctx
, &ref
, jsval_number(n
+(double)arg
));
2125 exprval_release(&ref
);
2129 return stack_push(ctx
, v
);
2132 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2133 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
2135 const int arg
= get_op_int(ctx
, 0);
2143 if(!stack_pop_exprval(ctx
, &ref
))
2144 return JS_E_OBJECT_EXPECTED
;
2146 hres
= exprval_propget(ctx
, &ref
, &v
);
2147 if(SUCCEEDED(hres
)) {
2150 hres
= to_number(ctx
, v
, &n
);
2152 if(SUCCEEDED(hres
)) {
2153 ret
= n
+(double)arg
;
2154 hres
= exprval_propput(ctx
, &ref
, jsval_number(ret
));
2157 exprval_release(&ref
);
2161 return stack_push(ctx
, jsval_number(ret
));
2164 /* ECMA-262 3rd Edition 11.9.3 */
2165 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
2167 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
2168 return jsval_strict_equal(lval
, rval
, ret
);
2170 /* FIXME: NULL disps should be handled in more general way */
2171 if(is_object_instance(lval
) && !get_object(lval
))
2172 return equal_values(ctx
, jsval_null(), rval
, ret
);
2173 if(is_object_instance(rval
) && !get_object(rval
))
2174 return equal_values(ctx
, lval
, jsval_null(), ret
);
2176 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
2181 if(is_string(lval
) && is_number(rval
)) {
2185 hres
= to_number(ctx
, lval
, &n
);
2189 /* FIXME: optimize */
2190 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
2193 if(is_string(rval
) && is_number(lval
)) {
2197 hres
= to_number(ctx
, rval
, &n
);
2201 /* FIXME: optimize */
2202 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
2206 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
2209 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
2212 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
2216 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
2220 hres
= equal_values(ctx
, lval
, prim
, ret
);
2221 jsval_release(prim
);
2226 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
2230 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
2234 hres
= equal_values(ctx
, prim
, rval
, ret
);
2235 jsval_release(prim
);
2244 /* ECMA-262 3rd Edition 11.9.1 */
2245 static HRESULT
interp_eq(script_ctx_t
*ctx
)
2254 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2256 hres
= equal_values(ctx
, l
, r
, &b
);
2262 return stack_push(ctx
, jsval_bool(b
));
2265 /* ECMA-262 3rd Edition 11.9.2 */
2266 static HRESULT
interp_neq(script_ctx_t
*ctx
)
2275 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2277 hres
= equal_values(ctx
, l
, r
, &b
);
2283 return stack_push(ctx
, jsval_bool(!b
));
2286 /* ECMA-262 3rd Edition 11.9.4 */
2287 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
2296 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2298 hres
= jsval_strict_equal(r
, l
, &b
);
2304 return stack_push(ctx
, jsval_bool(b
));
2307 /* ECMA-262 3rd Edition 11.9.5 */
2308 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2319 hres
= jsval_strict_equal(r
, l
, &b
);
2325 return stack_push(ctx
, jsval_bool(!b
));
2328 /* ECMA-262 3rd Edition 11.8.5 */
2329 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2335 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2339 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2345 if(is_string(l
) && is_string(r
)) {
2346 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2347 jsstr_release(get_string(l
));
2348 jsstr_release(get_string(r
));
2352 hres
= to_number(ctx
, l
, &ln
);
2355 hres
= to_number(ctx
, r
, &rn
);
2360 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2364 /* ECMA-262 3rd Edition 11.8.1 */
2365 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2374 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2376 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2382 return stack_push(ctx
, jsval_bool(b
));
2385 /* ECMA-262 3rd Edition 11.8.1 */
2386 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2395 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2397 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2403 return stack_push(ctx
, jsval_bool(b
));
2406 /* ECMA-262 3rd Edition 11.8.2 */
2407 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2416 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2418 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2424 return stack_push(ctx
, jsval_bool(b
));
2427 /* ECMA-262 3rd Edition 11.8.4 */
2428 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2437 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2439 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2445 return stack_push(ctx
, jsval_bool(b
));
2448 /* ECMA-262 3rd Edition 11.4.8 */
2449 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2458 hres
= to_int32(ctx
, v
, &i
);
2463 return stack_push(ctx
, jsval_number(~i
));
2466 /* ECMA-262 3rd Edition 11.4.9 */
2467 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2476 hres
= to_boolean(v
, &b
);
2481 return stack_push(ctx
, jsval_bool(!b
));
2484 /* ECMA-262 3rd Edition 11.7.1 */
2485 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2491 hres
= stack_pop_uint(ctx
, &r
);
2495 hres
= stack_pop_int(ctx
, &l
);
2499 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2502 /* ECMA-262 3rd Edition 11.7.2 */
2503 static HRESULT
interp_rshift(script_ctx_t
*ctx
)
2509 hres
= stack_pop_uint(ctx
, &r
);
2513 hres
= stack_pop_int(ctx
, &l
);
2517 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2520 /* ECMA-262 3rd Edition 11.7.3 */
2521 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2526 hres
= stack_pop_uint(ctx
, &r
);
2530 hres
= stack_pop_uint(ctx
, &l
);
2534 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2537 /* ECMA-262 3rd Edition 9.8 */
2538 static HRESULT
interp_to_string(script_ctx_t
*ctx
)
2545 TRACE("%s\n", debugstr_jsval(v
));
2546 hres
= to_string(ctx
, v
, &str
);
2549 WARN("failed %08x\n", hres
);
2553 return stack_push(ctx
, jsval_string(str
));
2556 /* ECMA-262 3rd Edition 11.13.1 */
2557 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2567 if(!stack_pop_exprval(ctx
, &ref
)) {
2569 return JS_E_ILLEGAL_ASSIGN
;
2572 hres
= exprval_propput(ctx
, &ref
, v
);
2573 exprval_release(&ref
);
2579 return stack_push(ctx
, v
);
2582 /* ECMA-262 3rd Edition 11.13.1 */
2583 static HRESULT
interp_set_member(script_ctx_t
*ctx
)
2585 jsval_t objv
, namev
, value
;
2590 value
= stack_pop(ctx
);
2591 namev
= stack_pop(ctx
);
2592 assert(is_string(namev
));
2593 objv
= stack_pop(ctx
);
2595 TRACE("%s.%s = %s\n", debugstr_jsval(objv
), debugstr_jsval(namev
), debugstr_jsval(value
));
2597 hres
= to_object(ctx
, objv
, &obj
);
2598 jsval_release(objv
);
2599 if(SUCCEEDED(hres
) && !(name
= jsstr_flatten(get_string(namev
)))) {
2600 IDispatch_Release(obj
);
2601 hres
= E_OUTOFMEMORY
;
2603 if(SUCCEEDED(hres
)) {
2604 hres
= disp_propput_name(ctx
, obj
, name
, value
);
2605 IDispatch_Release(obj
);
2606 jsstr_release(get_string(namev
));
2609 WARN("failed %08x\n", hres
);
2610 jsval_release(value
);
2614 return stack_push(ctx
, value
);
2617 /* JScript extension */
2618 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2620 const unsigned argc
= get_op_uint(ctx
, 0);
2625 TRACE("%u\n", argc
);
2627 if(!stack_topn_exprval(ctx
, argc
+1, &ref
))
2628 return JS_E_ILLEGAL_ASSIGN
;
2630 hres
= exprval_call(ctx
, &ref
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2635 stack_popn(ctx
, argc
+2);
2636 return stack_push(ctx
, v
);
2639 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2643 return stack_push(ctx
, jsval_undefined());
2646 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2648 const unsigned arg
= get_op_uint(ctx
, 0);
2656 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2658 const unsigned arg
= get_op_uint(ctx
, 0);
2666 hres
= to_boolean(v
, &b
);
2678 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2680 const unsigned arg
= get_op_uint(ctx
, 0);
2684 stack_popn(ctx
, arg
);
2688 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2690 const unsigned clear_ret
= get_op_uint(ctx
, 0);
2691 call_frame_t
*frame
= ctx
->call_ctx
;
2696 jsval_release(steal_ret(frame
));
2698 if((frame
->flags
& EXEC_CONSTRUCTOR
) && !is_object_instance(frame
->ret
)) {
2699 jsval_release(frame
->ret
);
2700 IDispatch_AddRef(frame
->this_obj
);
2701 frame
->ret
= jsval_disp(frame
->this_obj
);
2708 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2710 call_frame_t
*frame
= ctx
->call_ctx
;
2714 jsval_release(frame
->ret
);
2715 frame
->ret
= stack_pop(ctx
);
2719 static HRESULT
interp_push_acc(script_ctx_t
*ctx
)
2725 hres
= stack_push(ctx
, ctx
->acc
);
2727 ctx
->acc
= jsval_undefined();
2731 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2733 static const op_func_t op_funcs
[] = {
2734 #define X(x,a,b,c) interp_##x,
2739 static const unsigned op_move
[] = {
2740 #define X(a,x,b,c) x,
2745 static void pop_call_frame(script_ctx_t
*ctx
)
2747 call_frame_t
*frame
= ctx
->call_ctx
;
2749 frame
->stack_base
-= frame
->pop_locals
+ frame
->pop_variables
;
2751 assert(frame
->scope
== frame
->base_scope
);
2753 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2754 if(frame
->scope
&& frame
->scope
->ref
> 1) {
2755 HRESULT hres
= detach_variable_object(ctx
, frame
, TRUE
);
2757 ERR("Failed to detach variable object: %08x\n", hres
);
2760 if(frame
->arguments_obj
)
2761 detach_arguments_object(frame
->arguments_obj
);
2763 scope_release(frame
->scope
);
2765 if(frame
->pop_variables
)
2766 stack_popn(ctx
, frame
->pop_variables
);
2767 stack_popn(ctx
, frame
->pop_locals
);
2769 ctx
->call_ctx
= frame
->prev_frame
;
2771 if(frame
->function_instance
)
2772 jsdisp_release(frame
->function_instance
);
2773 if(frame
->variable_obj
)
2774 jsdisp_release(frame
->variable_obj
);
2776 IDispatch_Release(frame
->this_obj
);
2777 jsval_release(frame
->ret
);
2778 release_bytecode(frame
->bytecode
);
2782 static void print_backtrace(script_ctx_t
*ctx
)
2784 unsigned depth
= 0, i
, line
, char_pos
;
2785 call_frame_t
*frame
;
2787 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
2788 WARN("%u\t", depth
);
2792 WARN("%p->", frame
->this_obj
);
2793 WARN("%s(", frame
->function
->name
? debugstr_w(frame
->function
->name
) : "[unnamed]");
2794 if(frame
->base_scope
&& frame
->base_scope
->frame
) {
2795 for(i
=0; i
< frame
->argc
; i
++) {
2796 if(i
< frame
->function
->param_cnt
)
2797 WARN("%s%s=%s", i
? ", " : "", debugstr_w(frame
->function
->params
[i
]),
2798 debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2800 WARN("%s%s", i
? ", " : "", debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2803 WARN("[detached frame]");
2805 line
= get_location_line(frame
->bytecode
, frame
->bytecode
->instrs
[frame
->ip
].loc
, &char_pos
);
2806 WARN(") context %s line %u char %u\n", wine_dbgstr_longlong(frame
->bytecode
->source_context
), line
, char_pos
);
2808 if(!(frame
->flags
& EXEC_RETURN_TO_INTERP
)) {
2809 WARN("%u\t[native code]\n", depth
);
2815 static HRESULT
unwind_exception(script_ctx_t
*ctx
, HRESULT exception_hres
)
2817 except_frame_t
*except_frame
;
2818 jsexcept_t
*ei
= ctx
->ei
;
2819 call_frame_t
*frame
;
2824 if(WARN_ON(jscript
)) {
2825 jsdisp_t
*error_obj
;
2828 WARN("Exception %08x %s", exception_hres
, debugstr_jsval(ei
->valid_value
? ei
->value
: jsval_undefined()));
2829 if(ei
->valid_value
&& jsval_type(ei
->value
) == JSV_OBJECT
) {
2830 error_obj
= to_jsdisp(get_object(ei
->value
));
2832 hres
= jsdisp_propget_name(error_obj
, L
"message", &msg
);
2833 if(SUCCEEDED(hres
)) {
2834 WARN(" (message %s)", debugstr_jsval(msg
));
2841 print_backtrace(ctx
);
2844 frame
= ctx
->call_ctx
;
2845 if(exception_hres
!= DISP_E_EXCEPTION
)
2846 throw_error(ctx
, exception_hres
, NULL
);
2847 set_error_location(ei
, frame
->bytecode
, frame
->bytecode
->instrs
[frame
->ip
].loc
, IDS_RUNTIME_ERROR
, NULL
);
2849 while(!frame
->except_frame
) {
2852 while(frame
->scope
!= frame
->base_scope
)
2853 scope_pop(&frame
->scope
);
2855 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
2857 flags
= frame
->flags
;
2858 pop_call_frame(ctx
);
2859 if(!(flags
& EXEC_RETURN_TO_INTERP
))
2860 return DISP_E_EXCEPTION
;
2861 frame
= ctx
->call_ctx
;
2864 except_frame
= frame
->except_frame
;
2865 catch_off
= except_frame
->catch_off
;
2867 assert(except_frame
->stack_top
<= ctx
->stack_top
);
2868 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
2870 while(except_frame
->scope
!= frame
->scope
)
2871 scope_pop(&frame
->scope
);
2873 frame
->ip
= catch_off
? catch_off
: except_frame
->finally_off
;
2874 assert(!catch_off
|| frame
->bytecode
->instrs
[frame
->ip
].op
== OP_enter_catch
);
2876 if(ei
->valid_value
) {
2877 except_val
= ctx
->ei
->value
;
2878 ei
->valid_value
= FALSE
;
2881 if(!(err
= create_builtin_error(ctx
)))
2882 return E_OUTOFMEMORY
;
2883 except_val
= jsval_obj(err
);
2886 /* keep current except_frame if we're entering catch block with finally block associated */
2887 if(catch_off
&& except_frame
->finally_off
) {
2888 except_frame
->catch_off
= 0;
2890 frame
->except_frame
= except_frame
->next
;
2891 heap_free(except_frame
);
2894 hres
= stack_push(ctx
, except_val
);
2899 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2903 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, jsval_t
*r
)
2905 call_frame_t
*frame
;
2907 HRESULT hres
= S_OK
;
2912 frame
= ctx
->call_ctx
;
2913 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
2914 hres
= op_funcs
[op
](ctx
);
2916 hres
= unwind_exception(ctx
, hres
);
2919 }else if(frame
->ip
== -1) {
2920 const DWORD return_to_interp
= frame
->flags
& EXEC_RETURN_TO_INTERP
;
2922 assert(ctx
->stack_top
== frame
->stack_base
);
2923 assert(frame
->scope
== frame
->base_scope
);
2925 if(return_to_interp
) {
2926 jsval_release(ctx
->acc
);
2927 ctx
->acc
= steal_ret(frame
);
2929 *r
= steal_ret(frame
);
2931 pop_call_frame(ctx
);
2932 if(!return_to_interp
)
2935 frame
->ip
+= op_move
[op
];
2942 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
2944 IBindEventHandler
*target
;
2950 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
2954 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2958 if(!is_object_instance(v
)) {
2959 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
2963 disp
= get_object(v
);
2964 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
2965 if(SUCCEEDED(hres
)) {
2966 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
2967 IBindEventHandler_Release(target
);
2969 WARN("BindEvent failed: %08x\n", hres
);
2971 FIXME("No IBindEventHandler, not yet supported binding\n");
2974 IDispatch_Release(disp
);
2978 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
)
2980 const unsigned orig_stack
= ctx
->stack_top
;
2981 scope_chain_t
*scope
;
2986 /* If arguments are already on the stack, we may use them. */
2987 if(argv
+ argc
== ctx
->stack
+ ctx
->stack_top
) {
2988 frame
->arguments_off
= argv
- ctx
->stack
;
2991 frame
->arguments_off
= ctx
->stack_top
;
2992 for(i
= 0; i
< argc
; i
++) {
2993 hres
= jsval_copy(argv
[i
], &v
);
2995 hres
= stack_push(ctx
, v
);
3003 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
3004 for(; i
< frame
->function
->param_cnt
; i
++) {
3005 hres
= stack_push(ctx
, jsval_undefined());
3007 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3012 frame
->pop_locals
= ctx
->stack_top
- orig_stack
;
3014 frame
->variables_off
= ctx
->stack_top
;
3016 for(i
= 0; i
< frame
->function
->var_cnt
; i
++) {
3017 hres
= stack_push(ctx
, jsval_undefined());
3019 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3024 frame
->pop_variables
= i
;
3026 hres
= scope_push(scope_chain
, variable_object
, to_disp(variable_object
), &scope
);
3028 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3032 for(i
= 0; i
< frame
->function
->func_cnt
; i
++) {
3033 if(frame
->function
->funcs
[i
].name
&& !frame
->function
->funcs
[i
].event_target
) {
3037 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+i
, scope
, &func_obj
);
3039 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3040 scope_release(scope
);
3044 off
= local_off(frame
, frame
->function
->funcs
[i
].local_ref
);
3045 jsval_release(ctx
->stack
[off
]);
3046 ctx
->stack
[off
] = jsval_obj(func_obj
);
3050 scope
->frame
= frame
;
3051 frame
->base_scope
= frame
->scope
= scope
;
3055 HRESULT
exec_source(script_ctx_t
*ctx
, DWORD flags
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
3056 IDispatch
*this_obj
, jsdisp_t
*function_instance
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
3058 jsdisp_t
*variable_obj
;
3059 call_frame_t
*frame
;
3064 ctx
->stack
= heap_alloc(stack_size
* sizeof(*ctx
->stack
));
3066 return E_OUTOFMEMORY
;
3069 if(bytecode
->named_item
) {
3070 if(!bytecode
->named_item
->script_obj
) {
3071 hres
= create_named_item_script_obj(ctx
, bytecode
->named_item
);
3072 if(FAILED(hres
)) return hres
;
3076 if(!ctx
->ei
->enter_notified
) {
3077 ctx
->ei
->enter_notified
= TRUE
;
3078 IActiveScriptSite_OnEnterScript(ctx
->site
);
3081 for(i
= 0; i
< function
->func_cnt
; i
++) {
3084 if(!function
->funcs
[i
].event_target
)
3087 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+i
, scope
, &func_obj
);
3091 hres
= bind_event_target(ctx
, function
->funcs
+i
, func_obj
);
3092 jsdisp_release(func_obj
);
3097 if((flags
& EXEC_EVAL
) && ctx
->call_ctx
) {
3098 variable_obj
= jsdisp_addref(ctx
->call_ctx
->variable_obj
);
3099 }else if(!(flags
& (EXEC_GLOBAL
| EXEC_EVAL
))) {
3100 hres
= create_dispex(ctx
, NULL
, NULL
, &variable_obj
);
3101 if(FAILED(hres
)) return hres
;
3102 }else if(bytecode
->named_item
) {
3103 variable_obj
= jsdisp_addref(bytecode
->named_item
->script_obj
);
3105 variable_obj
= jsdisp_addref(ctx
->global
);
3108 if(flags
& (EXEC_GLOBAL
| EXEC_EVAL
)) {
3109 named_item_t
*item
= bytecode
->named_item
;
3112 for(i
=0; i
< function
->var_cnt
; i
++) {
3113 TRACE("[%d] %s %d\n", i
, debugstr_w(function
->variables
[i
].name
), function
->variables
[i
].func_id
);
3114 if(function
->variables
[i
].func_id
!= -1) {
3117 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+function
->variables
[i
].func_id
, scope
, &func_obj
);
3121 hres
= jsdisp_propput_name(variable_obj
, function
->variables
[i
].name
, jsval_obj(func_obj
));
3122 jsdisp_release(func_obj
);
3126 if(item
&& !(item
->flags
& SCRIPTITEM_CODEONLY
)
3127 && SUCCEEDED(disp_get_id(ctx
, item
->disp
, function
->variables
[i
].name
, function
->variables
[i
].name
, 0, &id
)))
3130 if(!item
&& (flags
& EXEC_GLOBAL
) && lookup_global_members(ctx
, function
->variables
[i
].name
, NULL
))
3133 hres
= jsdisp_get_id(variable_obj
, function
->variables
[i
].name
, fdexNameEnsure
, &id
);
3139 /* ECMA-262 3rd Edition 11.2.3.7 */
3143 jsthis
= iface_to_jsdisp(this_obj
);
3145 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
3147 jsdisp_release(jsthis
);
3151 if(ctx
->call_ctx
&& (flags
& EXEC_EVAL
)) {
3152 hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
3157 frame
= heap_alloc_zero(sizeof(*frame
));
3159 hres
= E_OUTOFMEMORY
;
3163 frame
->function
= function
;
3164 frame
->ret
= jsval_undefined();
3166 frame
->bytecode
= bytecode_addref(bytecode
);
3168 if(!(flags
& (EXEC_GLOBAL
|EXEC_EVAL
))) {
3169 hres
= setup_scope(ctx
, frame
, scope
, variable_obj
, argc
, argv
);
3171 release_bytecode(frame
->bytecode
);
3176 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
3179 frame
->ip
= function
->instr_off
;
3180 frame
->stack_base
= ctx
->stack_top
;
3182 frame
->this_obj
= this_obj
;
3183 IDispatch_AddRef(frame
->this_obj
);
3186 if(function_instance
)
3187 frame
->function_instance
= jsdisp_addref(function_instance
);
3189 frame
->flags
= flags
;
3190 frame
->variable_obj
= variable_obj
;
3192 frame
->prev_frame
= ctx
->call_ctx
;
3193 ctx
->call_ctx
= frame
;
3195 if(flags
& EXEC_RETURN_TO_INTERP
) {
3197 * We're called directly from interpreter, so we may just setup call frame and return.
3198 * Already running interpreter will take care of execution.
3201 *r
= jsval_undefined();
3205 return enter_bytecode(ctx
, r
);
3208 jsdisp_release(variable_obj
);