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
21 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
22 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
23 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
24 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
25 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
26 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
27 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
29 struct _except_frame_t
{
38 static HRESULT
stack_push(exec_ctx_t
*ctx
, jsval_t v
)
40 if(!ctx
->stack_size
) {
41 ctx
->stack
= heap_alloc(16*sizeof(*ctx
->stack
));
45 }else if(ctx
->stack_size
== ctx
->top
) {
48 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*new_stack
));
54 ctx
->stack
= new_stack
;
58 ctx
->stack
[ctx
->top
++] = v
;
62 static inline HRESULT
stack_push_string(exec_ctx_t
*ctx
, const WCHAR
*str
)
70 return stack_push(ctx
, jsval_string(v
));
73 static HRESULT
stack_push_objid(exec_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
)
77 hres
= stack_push(ctx
, jsval_disp(disp
));
81 return stack_push(ctx
, jsval_number(id
));
84 static inline jsval_t
stack_top(exec_ctx_t
*ctx
)
87 return ctx
->stack
[ctx
->top
-1];
90 static inline jsval_t
stack_topn(exec_ctx_t
*ctx
, unsigned n
)
93 return ctx
->stack
[ctx
->top
-1-n
];
96 static inline jsval_t
*stack_args(exec_ctx_t
*ctx
, unsigned n
)
100 assert(ctx
->top
> n
-1);
101 return ctx
->stack
+ ctx
->top
-n
;
104 static inline jsval_t
stack_pop(exec_ctx_t
*ctx
)
107 return ctx
->stack
[--ctx
->top
];
110 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
113 jsval_release(stack_pop(ctx
));
116 static HRESULT
stack_pop_number(exec_ctx_t
*ctx
, double *r
)
122 hres
= to_number(ctx
->script
, v
, r
);
127 static HRESULT
stack_pop_object(exec_ctx_t
*ctx
, IDispatch
**r
)
133 if(is_object_instance(v
)) {
135 return throw_type_error(ctx
->script
, JS_E_OBJECT_REQUIRED
, NULL
);
140 hres
= to_object(ctx
->script
, v
, r
);
145 static inline HRESULT
stack_pop_int(exec_ctx_t
*ctx
, INT
*r
)
147 return to_int32(ctx
->script
, stack_pop(ctx
), r
);
150 static inline HRESULT
stack_pop_uint(exec_ctx_t
*ctx
, DWORD
*r
)
152 return to_uint32(ctx
->script
, stack_pop(ctx
), r
);
155 static inline IDispatch
*stack_pop_objid(exec_ctx_t
*ctx
, DISPID
*id
)
157 assert(is_number(stack_top(ctx
)) && is_object_instance(stack_topn(ctx
, 1)));
159 *id
= get_number(stack_pop(ctx
));
160 return get_object(stack_pop(ctx
));
163 static inline IDispatch
*stack_topn_objid(exec_ctx_t
*ctx
, unsigned n
, DISPID
*id
)
165 assert(is_number(stack_topn(ctx
, n
)) && is_object_instance(stack_topn(ctx
, n
+1)));
167 *id
= get_number(stack_topn(ctx
, n
));
168 return get_object(stack_topn(ctx
, n
+1));
171 static void exprval_release(exprval_t
*val
)
175 jsval_release(val
->u
.val
);
178 if(val
->u
.idref
.disp
)
179 IDispatch_Release(val
->u
.idref
.disp
);
181 case EXPRVAL_INVALID
:
186 /* ECMA-262 3rd Edition 8.7.1 */
187 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsval_t
*ret
)
192 val
->u
.val
= jsval_undefined();
195 if(!val
->u
.idref
.disp
) {
196 FIXME("throw ReferenceError\n");
200 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
);
201 case EXPRVAL_INVALID
:
205 ERR("type %d\n", val
->type
);
209 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
211 val
->type
= EXPRVAL_IDREF
;
212 val
->u
.idref
.disp
= disp
;
213 val
->u
.idref
.id
= id
;
216 IDispatch_AddRef(disp
);
219 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
221 scope_chain_t
*new_scope
;
223 new_scope
= heap_alloc(sizeof(scope_chain_t
));
225 return E_OUTOFMEMORY
;
229 IDispatch_AddRef(obj
);
230 new_scope
->jsobj
= jsobj
;
231 new_scope
->obj
= obj
;
235 new_scope
->next
= scope
;
237 new_scope
->next
= NULL
;
244 static void scope_pop(scope_chain_t
**scope
)
253 void clear_ei(script_ctx_t
*ctx
)
255 memset(&ctx
->ei
.ei
, 0, sizeof(ctx
->ei
.ei
));
256 jsval_release(ctx
->ei
.val
);
257 ctx
->ei
.val
= jsval_undefined();
260 void scope_release(scope_chain_t
*scope
)
266 scope_release(scope
->next
);
268 IDispatch_Release(scope
->obj
);
272 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, jsdisp_t
*var_disp
,
273 scope_chain_t
*scope
, BOOL is_global
, exec_ctx_t
**ret
)
277 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
279 return E_OUTOFMEMORY
;
282 ctx
->is_global
= is_global
;
283 ctx
->ret
= jsval_undefined();
285 /* ECMA-262 3rd Edition 11.2.3.7 */
289 jsthis
= iface_to_jsdisp((IUnknown
*)this_obj
);
291 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
293 jsdisp_release(jsthis
);
298 ctx
->this_obj
= this_obj
;
299 else if(script_ctx
->host_global
)
300 ctx
->this_obj
= script_ctx
->host_global
;
302 ctx
->this_obj
= to_disp(script_ctx
->global
);
303 IDispatch_AddRef(ctx
->this_obj
);
305 jsdisp_addref(var_disp
);
306 ctx
->var_disp
= var_disp
;
308 script_addref(script_ctx
);
309 ctx
->script
= script_ctx
;
313 ctx
->scope_chain
= scope
;
320 void exec_release(exec_ctx_t
*ctx
)
326 scope_release(ctx
->scope_chain
);
328 jsdisp_release(ctx
->var_disp
);
330 IDispatch_Release(ctx
->this_obj
);
332 script_release(ctx
->script
);
333 jsval_release(ctx
->ret
);
334 heap_free(ctx
->stack
);
338 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
345 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
347 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
348 jsdisp_release(jsdisp
);
355 bstr
= SysAllocString(name
);
357 return E_OUTOFMEMORY
;
361 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
362 if(SUCCEEDED(hres
)) {
363 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
364 IDispatchEx_Release(dispex
);
366 TRACE("using IDispatch\n");
367 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
370 if(name_bstr
!= bstr
)
375 static inline BOOL
var_is_null(const VARIANT
*v
)
377 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
380 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
382 IObjectIdentity
*identity
;
383 IUnknown
*unk1
, *unk2
;
391 if(!disp1
|| !disp2
) {
396 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
400 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
402 IUnknown_Release(unk1
);
409 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
410 if(SUCCEEDED(hres
)) {
411 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
412 IObjectIdentity_Release(identity
);
419 IUnknown_Release(unk1
);
420 IUnknown_Release(unk2
);
424 /* ECMA-262 3rd Edition 11.9.6 */
425 static HRESULT
equal2_values(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
427 jsval_type_t type
= jsval_type(lval
);
431 if(type
!= jsval_type(rval
)) {
432 if(is_null_instance(lval
))
433 *ret
= is_null_instance(rval
);
445 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
447 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
450 *ret
= get_number(lval
) == get_number(rval
);
453 *ret
= !get_bool(lval
) == !get_bool(rval
);
456 FIXME("VARIANT not implemented\n");
463 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
469 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
470 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
471 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
472 if(SUCCEEDED(hres
)) {
474 exprval_set_idref(ret
, item
->disp
, id
);
483 /* ECMA-262 3rd Edition 10.1.4 */
484 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
486 scope_chain_t
*scope
;
491 TRACE("%s\n", debugstr_w(identifier
));
493 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
495 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
497 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
498 if(SUCCEEDED(hres
)) {
499 exprval_set_idref(ret
, scope
->obj
, id
);
504 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
505 if(SUCCEEDED(hres
)) {
506 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
510 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
511 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
518 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
519 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
521 WARN("GetItemInfo failed: %08x\n", hres
);
525 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
526 IUnknown_Release(unk
);
528 WARN("object does not implement IDispatch\n");
533 IDispatch_AddRef(item
->disp
);
534 ret
->type
= EXPRVAL_JSVAL
;
535 ret
->u
.val
= jsval_disp(item
->disp
);
540 if(lookup_global_members(ctx
, identifier
, ret
))
543 ret
->type
= EXPRVAL_INVALID
;
547 static inline BSTR
get_op_bstr(exec_ctx_t
*ctx
, int i
){
548 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].bstr
;
551 static inline unsigned get_op_uint(exec_ctx_t
*ctx
, int i
){
552 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].uint
;
555 static inline unsigned get_op_int(exec_ctx_t
*ctx
, int i
){
556 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].lng
;
559 static inline jsstr_t
*get_op_str(exec_ctx_t
*ctx
, int i
){
560 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].str
;
563 static inline double get_op_double(exec_ctx_t
*ctx
){
564 return ctx
->code
->instrs
[ctx
->ip
].u
.dbl
;
567 /* ECMA-262 3rd Edition 12.2 */
568 static HRESULT
interp_var_set(exec_ctx_t
*ctx
)
570 const BSTR name
= get_op_bstr(ctx
, 0);
574 TRACE("%s\n", debugstr_w(name
));
576 val
= stack_pop(ctx
);
577 hres
= jsdisp_propput_name(ctx
->var_disp
, name
, val
);
582 /* ECMA-262 3rd Edition 12.6.4 */
583 static HRESULT
interp_forin(exec_ctx_t
*ctx
)
585 const HRESULT arg
= get_op_uint(ctx
, 0);
586 IDispatch
*var_obj
, *obj
= NULL
;
594 assert(is_number(stack_top(ctx
)));
595 id
= get_number(stack_top(ctx
));
597 var_obj
= stack_topn_objid(ctx
, 1, &var_id
);
599 FIXME("invalid ref\n");
603 if(is_object_instance(stack_topn(ctx
, 3)))
604 obj
= get_object(stack_topn(ctx
, 3));
607 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
608 if(SUCCEEDED(hres
)) {
609 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
611 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
612 IDispatchEx_Release(dispex
);
616 TRACE("No IDispatchEx\n");
623 str
= jsstr_alloc_len(name
, SysStringLen(name
));
626 return E_OUTOFMEMORY
;
629 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
631 hres
= disp_propput(ctx
->script
, var_obj
, var_id
, jsval_string(str
));
644 /* ECMA-262 3rd Edition 12.10 */
645 static HRESULT
interp_push_scope(exec_ctx_t
*ctx
)
654 hres
= to_object(ctx
->script
, v
, &disp
);
659 hres
= scope_push(ctx
->scope_chain
, to_jsdisp(disp
), disp
, &ctx
->scope_chain
);
660 IDispatch_Release(disp
);
664 /* ECMA-262 3rd Edition 12.10 */
665 static HRESULT
interp_pop_scope(exec_ctx_t
*ctx
)
669 scope_pop(&ctx
->scope_chain
);
673 /* ECMA-262 3rd Edition 12.13 */
674 static HRESULT
interp_case(exec_ctx_t
*ctx
)
676 const unsigned arg
= get_op_uint(ctx
, 0);
684 hres
= equal2_values(stack_top(ctx
), v
, &b
);
698 /* ECMA-262 3rd Edition 12.13 */
699 static HRESULT
interp_throw(exec_ctx_t
*ctx
)
703 jsval_release(ctx
->script
->ei
.val
);
704 ctx
->script
->ei
.val
= stack_pop(ctx
);
705 return DISP_E_EXCEPTION
;
708 static HRESULT
interp_throw_ref(exec_ctx_t
*ctx
)
710 const HRESULT arg
= get_op_uint(ctx
, 0);
712 TRACE("%08x\n", arg
);
714 return throw_reference_error(ctx
->script
, arg
, NULL
);
717 static HRESULT
interp_throw_type(exec_ctx_t
*ctx
)
719 const HRESULT hres
= get_op_uint(ctx
, 0);
720 jsstr_t
*str
= get_op_str(ctx
, 1);
723 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
725 ptr
= jsstr_flatten(str
);
726 return ptr
? throw_type_error(ctx
->script
, hres
, ptr
) : E_OUTOFMEMORY
;
729 /* ECMA-262 3rd Edition 12.14 */
730 static HRESULT
interp_push_except(exec_ctx_t
*ctx
)
732 const unsigned arg1
= get_op_uint(ctx
, 0);
733 const BSTR arg2
= get_op_bstr(ctx
, 1);
734 except_frame_t
*except
;
739 stack_top
= ctx
->top
;
744 hres
= stack_push(ctx
, jsval_bool(TRUE
));
747 hres
= stack_push(ctx
, jsval_bool(TRUE
));
752 except
= heap_alloc(sizeof(*except
));
754 return E_OUTOFMEMORY
;
756 except
->stack_top
= stack_top
;
757 except
->scope
= ctx
->scope_chain
;
758 except
->catch_off
= arg1
;
759 except
->ident
= arg2
;
760 except
->next
= ctx
->except_frame
;
761 ctx
->except_frame
= except
;
765 /* ECMA-262 3rd Edition 12.14 */
766 static HRESULT
interp_pop_except(exec_ctx_t
*ctx
)
768 except_frame_t
*except
;
772 except
= ctx
->except_frame
;
773 assert(except
!= NULL
);
775 ctx
->except_frame
= except
->next
;
780 /* ECMA-262 3rd Edition 12.14 */
781 static HRESULT
interp_end_finally(exec_ctx_t
*ctx
)
787 assert(is_bool(stack_top(ctx
)));
788 if(!get_bool(stack_top(ctx
))) {
789 TRACE("passing exception\n");
794 ctx
->script
->ei
.val
= stack_pop(ctx
);
795 return DISP_E_EXCEPTION
;
802 /* ECMA-262 3rd Edition 13 */
803 static HRESULT
interp_func(exec_ctx_t
*ctx
)
805 unsigned func_idx
= get_op_uint(ctx
, 0);
809 TRACE("%d\n", func_idx
);
811 hres
= create_source_function(ctx
->script
, ctx
->code
, ctx
->func_code
->funcs
+func_idx
,
812 ctx
->scope_chain
, &dispex
);
816 return stack_push(ctx
, jsval_obj(dispex
));
819 /* ECMA-262 3rd Edition 11.2.1 */
820 static HRESULT
interp_array(exec_ctx_t
*ctx
)
831 namev
= stack_pop(ctx
);
833 hres
= stack_pop_object(ctx
, &obj
);
835 jsval_release(namev
);
839 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
840 jsval_release(namev
);
842 IDispatch_Release(obj
);
846 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, 0, &id
);
847 jsstr_release(name_str
);
848 if(SUCCEEDED(hres
)) {
849 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
850 }else if(hres
== DISP_E_UNKNOWNNAME
) {
851 v
= jsval_undefined();
854 IDispatch_Release(obj
);
858 return stack_push(ctx
, v
);
861 /* ECMA-262 3rd Edition 11.2.1 */
862 static HRESULT
interp_member(exec_ctx_t
*ctx
)
864 const BSTR arg
= get_op_bstr(ctx
, 0);
872 hres
= stack_pop_object(ctx
, &obj
);
876 hres
= disp_get_id(ctx
->script
, obj
, arg
, arg
, 0, &id
);
877 if(SUCCEEDED(hres
)) {
878 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
879 }else if(hres
== DISP_E_UNKNOWNNAME
) {
880 v
= jsval_undefined();
883 IDispatch_Release(obj
);
887 return stack_push(ctx
, v
);
890 /* ECMA-262 3rd Edition 11.2.1 */
891 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
893 const unsigned arg
= get_op_uint(ctx
, 0);
903 namev
= stack_pop(ctx
);
904 objv
= stack_pop(ctx
);
906 hres
= to_object(ctx
->script
, objv
, &obj
);
908 if(SUCCEEDED(hres
)) {
909 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
911 IDispatch_Release(obj
);
913 jsval_release(namev
);
917 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, arg
, &id
);
918 jsstr_release(name_str
);
920 IDispatch_Release(obj
);
921 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
923 id
= JS_E_INVALID_PROPERTY
;
925 ERR("failed %08x\n", hres
);
930 return stack_push_objid(ctx
, obj
, id
);
933 /* ECMA-262 3rd Edition 11.2.1 */
934 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
943 disp
= stack_topn_objid(ctx
, 0, &id
);
945 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
947 hres
= disp_propget(ctx
->script
, disp
, id
, &v
);
951 return stack_push(ctx
, v
);
954 /* ECMA-262 3rd Edition 11.2.2 */
955 static HRESULT
interp_new(exec_ctx_t
*ctx
)
957 const unsigned argc
= get_op_uint(ctx
, 0);
963 constr
= stack_topn(ctx
, argc
);
965 /* NOTE: Should use to_object here */
968 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
969 else if(!is_object_instance(constr
))
970 return throw_type_error(ctx
->script
, JS_E_INVALID_ACTION
, NULL
);
971 else if(!get_object(constr
))
972 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
974 hres
= disp_call_value(ctx
->script
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
, argc
, stack_args(ctx
, argc
), &r
);
978 stack_popn(ctx
, argc
+1);
979 return stack_push(ctx
, r
);
982 /* ECMA-262 3rd Edition 11.2.3 */
983 static HRESULT
interp_call(exec_ctx_t
*ctx
)
985 const unsigned argn
= get_op_uint(ctx
, 0);
986 const int do_ret
= get_op_int(ctx
, 1);
990 TRACE("%d %d\n", argn
, do_ret
);
992 obj
= stack_topn(ctx
, argn
);
993 if(!is_object_instance(obj
))
994 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
996 hres
= disp_call_value(ctx
->script
, get_object(obj
), NULL
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
),
1001 stack_popn(ctx
, argn
+1);
1002 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1005 /* ECMA-262 3rd Edition 11.2.3 */
1006 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1008 const unsigned argn
= get_op_uint(ctx
, 0);
1009 const int do_ret
= get_op_int(ctx
, 1);
1015 TRACE("%d %d\n", argn
, do_ret
);
1017 obj
= stack_topn_objid(ctx
, argn
, &id
);
1019 return throw_type_error(ctx
->script
, id
, NULL
);
1021 hres
= disp_call(ctx
->script
, obj
, id
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
), do_ret
? &r
: NULL
);
1025 stack_popn(ctx
, argn
+2);
1026 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1030 /* ECMA-262 3rd Edition 11.1.1 */
1031 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1035 IDispatch_AddRef(ctx
->this_obj
);
1036 return stack_push(ctx
, jsval_disp(ctx
->this_obj
));
1039 /* ECMA-262 3rd Edition 10.1.4 */
1040 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1042 const BSTR arg
= get_op_bstr(ctx
, 0);
1047 TRACE("%s\n", debugstr_w(arg
));
1049 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1053 if(exprval
.type
== EXPRVAL_INVALID
)
1054 return throw_type_error(ctx
->script
, JS_E_UNDEFINED_VARIABLE
, arg
);
1056 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1057 exprval_release(&exprval
);
1061 return stack_push(ctx
, v
);
1064 /* ECMA-262 3rd Edition 10.1.4 */
1065 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1067 const BSTR arg
= get_op_bstr(ctx
, 0);
1068 const unsigned flags
= get_op_uint(ctx
, 1);
1072 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1074 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1078 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1081 hres
= jsdisp_get_id(ctx
->script
->global
, arg
, fdexNameEnsure
, &id
);
1085 exprval_set_idref(&exprval
, to_disp(ctx
->script
->global
), id
);
1088 if(exprval
.type
!= EXPRVAL_IDREF
) {
1089 WARN("invalid ref\n");
1090 exprval_release(&exprval
);
1091 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1094 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1097 /* ECMA-262 3rd Edition 7.8.1 */
1098 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1102 return stack_push(ctx
, jsval_null());
1105 /* ECMA-262 3rd Edition 7.8.2 */
1106 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1108 const int arg
= get_op_int(ctx
, 0);
1110 TRACE("%s\n", arg
? "true" : "false");
1112 return stack_push(ctx
, jsval_bool(arg
));
1115 /* ECMA-262 3rd Edition 7.8.3 */
1116 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1118 const int arg
= get_op_int(ctx
, 0);
1122 return stack_push(ctx
, jsval_number(arg
));
1125 /* ECMA-262 3rd Edition 7.8.3 */
1126 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1128 const double arg
= get_op_double(ctx
);
1130 TRACE("%lf\n", arg
);
1132 return stack_push(ctx
, jsval_number(arg
));
1135 /* ECMA-262 3rd Edition 7.8.4 */
1136 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1138 jsstr_t
*str
= get_op_str(ctx
, 0);
1140 TRACE("%s\n", debugstr_jsstr(str
));
1142 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1145 /* ECMA-262 3rd Edition 7.8 */
1146 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1148 jsstr_t
*source
= get_op_str(ctx
, 0);
1149 const unsigned flags
= get_op_uint(ctx
, 1);
1153 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1155 hres
= create_regexp(ctx
->script
, source
, flags
, ®exp
);
1159 return stack_push(ctx
, jsval_obj(regexp
));
1162 /* ECMA-262 3rd Edition 11.1.4 */
1163 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1165 const unsigned arg
= get_op_uint(ctx
, 0);
1173 hres
= create_array(ctx
->script
, arg
, &array
);
1179 val
= stack_pop(ctx
);
1180 hres
= jsdisp_propput_idx(array
, i
, val
);
1183 jsdisp_release(array
);
1188 return stack_push(ctx
, jsval_obj(array
));
1191 /* ECMA-262 3rd Edition 11.1.5 */
1192 static HRESULT
interp_new_obj(exec_ctx_t
*ctx
)
1199 hres
= create_object(ctx
->script
, NULL
, &obj
);
1203 return stack_push(ctx
, jsval_obj(obj
));
1206 /* ECMA-262 3rd Edition 11.1.5 */
1207 static HRESULT
interp_obj_prop(exec_ctx_t
*ctx
)
1209 const BSTR name
= get_op_bstr(ctx
, 0);
1214 TRACE("%s\n", debugstr_w(name
));
1216 val
= stack_pop(ctx
);
1218 assert(is_object_instance(stack_top(ctx
)));
1219 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1221 hres
= jsdisp_propput_name(obj
, name
, val
);
1226 /* ECMA-262 3rd Edition 11.11 */
1227 static HRESULT
interp_cnd_nz(exec_ctx_t
*ctx
)
1229 const unsigned arg
= get_op_uint(ctx
, 0);
1235 hres
= to_boolean(stack_top(ctx
), &b
);
1248 /* ECMA-262 3rd Edition 11.11 */
1249 static HRESULT
interp_cnd_z(exec_ctx_t
*ctx
)
1251 const unsigned arg
= get_op_uint(ctx
, 0);
1257 hres
= to_boolean(stack_top(ctx
), &b
);
1270 /* ECMA-262 3rd Edition 11.10 */
1271 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1278 hres
= stack_pop_int(ctx
, &r
);
1282 hres
= stack_pop_int(ctx
, &l
);
1286 return stack_push(ctx
, jsval_number(l
|r
));
1289 /* ECMA-262 3rd Edition 11.10 */
1290 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1297 hres
= stack_pop_int(ctx
, &r
);
1301 hres
= stack_pop_int(ctx
, &l
);
1305 return stack_push(ctx
, jsval_number(l
^r
));
1308 /* ECMA-262 3rd Edition 11.10 */
1309 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1316 hres
= stack_pop_int(ctx
, &r
);
1320 hres
= stack_pop_int(ctx
, &l
);
1324 return stack_push(ctx
, jsval_number(l
&r
));
1327 /* ECMA-262 3rd Edition 11.8.6 */
1328 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
1330 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1335 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1338 if(!is_object_instance(v
) || !get_object(v
)) {
1340 return throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1343 obj
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1344 IDispatch_Release(get_object(v
));
1346 FIXME("non-jsdisp objects not supported\n");
1350 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1351 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1353 hres
= throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1355 jsdisp_release(obj
);
1361 if(is_object_instance(prot
)) {
1362 if(is_object_instance(v
))
1363 tmp
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1364 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1365 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1371 jsdisp_release(tmp
);
1373 FIXME("prototype is not an object\n");
1377 jsval_release(prot
);
1382 return stack_push(ctx
, jsval_bool(ret
));
1385 /* ECMA-262 3rd Edition 11.8.7 */
1386 static HRESULT
interp_in(exec_ctx_t
*ctx
)
1397 obj
= stack_pop(ctx
);
1398 if(!is_object_instance(obj
) || !get_object(obj
)) {
1400 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1404 hres
= to_flat_string(ctx
->script
, v
, &jsstr
, &str
);
1407 IDispatch_Release(get_object(obj
));
1411 hres
= disp_get_id(ctx
->script
, get_object(obj
), str
, NULL
, 0, &id
);
1412 IDispatch_Release(get_object(obj
));
1413 jsstr_release(jsstr
);
1416 else if(hres
== DISP_E_UNKNOWNNAME
)
1421 return stack_push(ctx
, jsval_bool(ret
));
1424 /* ECMA-262 3rd Edition 11.6.1 */
1425 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1430 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1434 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1440 if(is_string(l
) || is_string(r
)) {
1441 jsstr_t
*lstr
, *rstr
= NULL
;
1443 hres
= to_string(ctx
, l
, &lstr
);
1445 hres
= to_string(ctx
, r
, &rstr
);
1447 if(SUCCEEDED(hres
)) {
1450 ret_str
= jsstr_concat(lstr
, rstr
);
1452 *ret
= jsval_string(ret_str
);
1454 hres
= E_OUTOFMEMORY
;
1457 jsstr_release(lstr
);
1459 jsstr_release(rstr
);
1463 hres
= to_number(ctx
, l
, &nl
);
1464 if(SUCCEEDED(hres
)) {
1465 hres
= to_number(ctx
, r
, &nr
);
1467 *ret
= jsval_number(nl
+nr
);
1476 /* ECMA-262 3rd Edition 11.6.1 */
1477 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1485 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1487 hres
= add_eval(ctx
->script
, l
, r
, &ret
);
1493 return stack_push(ctx
, ret
);
1496 /* ECMA-262 3rd Edition 11.6.2 */
1497 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1504 hres
= stack_pop_number(ctx
, &r
);
1508 hres
= stack_pop_number(ctx
, &l
);
1512 return stack_push(ctx
, jsval_number(l
-r
));
1515 /* ECMA-262 3rd Edition 11.5.1 */
1516 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1523 hres
= stack_pop_number(ctx
, &r
);
1527 hres
= stack_pop_number(ctx
, &l
);
1531 return stack_push(ctx
, jsval_number(l
*r
));
1534 /* ECMA-262 3rd Edition 11.5.2 */
1535 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1542 hres
= stack_pop_number(ctx
, &r
);
1546 hres
= stack_pop_number(ctx
, &l
);
1550 return stack_push(ctx
, jsval_number(l
/r
));
1553 /* ECMA-262 3rd Edition 11.5.3 */
1554 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1561 hres
= stack_pop_number(ctx
, &r
);
1565 hres
= stack_pop_number(ctx
, &l
);
1569 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1572 /* ECMA-262 3rd Edition 11.4.2 */
1573 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
1575 jsval_t objv
, namev
;
1583 namev
= stack_pop(ctx
);
1584 objv
= stack_pop(ctx
);
1586 hres
= to_object(ctx
->script
, objv
, &obj
);
1587 jsval_release(objv
);
1589 jsval_release(namev
);
1593 hres
= to_string(ctx
->script
, namev
, &name
);
1594 jsval_release(namev
);
1596 IDispatch_Release(obj
);
1600 hres
= disp_delete_name(ctx
->script
, obj
, name
, &ret
);
1601 IDispatch_Release(obj
);
1602 jsstr_release(name
);
1606 return stack_push(ctx
, jsval_bool(ret
));
1609 /* ECMA-262 3rd Edition 11.4.2 */
1610 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
1612 const BSTR arg
= get_op_bstr(ctx
, 0);
1617 TRACE("%s\n", debugstr_w(arg
));
1619 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1623 switch(exprval
.type
) {
1625 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1626 IDispatch_Release(exprval
.u
.idref
.disp
);
1630 case EXPRVAL_INVALID
:
1634 FIXME("Unsupported exprval\n");
1635 exprval_release(&exprval
);
1640 return stack_push(ctx
, jsval_bool(ret
));
1643 /* ECMA-262 3rd Edition 11.4.2 */
1644 static HRESULT
interp_void(exec_ctx_t
*ctx
)
1649 return stack_push(ctx
, jsval_undefined());
1652 /* ECMA-262 3rd Edition 11.4.3 */
1653 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1655 switch(jsval_type(v
)) {
1665 if(get_object(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)get_object(v
)))) {
1666 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1667 jsdisp_release(dispex
);
1683 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1690 /* ECMA-262 3rd Edition 11.4.3 */
1691 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
1701 obj
= stack_pop_objid(ctx
, &id
);
1703 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1705 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1706 IDispatch_Release(obj
);
1708 return stack_push_string(ctx
, unknownW
);
1710 hres
= typeof_string(v
, &ret
);
1715 return stack_push_string(ctx
, ret
);
1718 /* ECMA-262 3rd Edition 11.4.3 */
1719 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
1721 const BSTR arg
= get_op_bstr(ctx
, 0);
1727 TRACE("%s\n", debugstr_w(arg
));
1729 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1733 if(exprval
.type
== EXPRVAL_INVALID
) {
1734 hres
= stack_push(ctx
, jsval_string(jsstr_undefined()));
1735 exprval_release(&exprval
);
1739 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1740 exprval_release(&exprval
);
1744 hres
= typeof_string(v
, &ret
);
1749 return stack_push_string(ctx
, ret
);
1752 /* ECMA-262 3rd Edition 11.4.3 */
1753 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
1762 hres
= typeof_string(v
, &ret
);
1767 return stack_push_string(ctx
, ret
);
1770 /* ECMA-262 3rd Edition 11.4.7 */
1771 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
1778 hres
= stack_pop_number(ctx
, &n
);
1782 return stack_push(ctx
, jsval_number(-n
));
1785 /* ECMA-262 3rd Edition 11.4.6 */
1786 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
1795 hres
= to_number(ctx
->script
, v
, &n
);
1800 return stack_push(ctx
, jsval_number(n
));
1803 /* ECMA-262 3rd Edition 11.3.1 */
1804 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
1806 const int arg
= get_op_int(ctx
, 0);
1814 obj
= stack_pop_objid(ctx
, &id
);
1816 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1818 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1819 if(SUCCEEDED(hres
)) {
1822 hres
= to_number(ctx
->script
, v
, &n
);
1824 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(n
+(double)arg
));
1828 IDispatch_Release(obj
);
1832 return stack_push(ctx
, v
);
1835 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1836 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
1838 const int arg
= get_op_int(ctx
, 0);
1847 obj
= stack_pop_objid(ctx
, &id
);
1849 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1851 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1852 if(SUCCEEDED(hres
)) {
1855 hres
= to_number(ctx
->script
, v
, &n
);
1857 if(SUCCEEDED(hres
)) {
1858 ret
= n
+(double)arg
;
1859 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(ret
));
1862 IDispatch_Release(obj
);
1866 return stack_push(ctx
, jsval_number(ret
));
1869 /* ECMA-262 3rd Edition 11.9.3 */
1870 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
1872 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
1873 return equal2_values(lval
, rval
, ret
);
1875 /* FIXME: NULL disps should be handled in more general way */
1876 if(is_object_instance(lval
) && !get_object(lval
))
1877 return equal_values(ctx
, jsval_null(), rval
, ret
);
1878 if(is_object_instance(rval
) && !get_object(rval
))
1879 return equal_values(ctx
, lval
, jsval_null(), ret
);
1881 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
1886 if(is_string(lval
) && is_number(rval
)) {
1890 hres
= to_number(ctx
, lval
, &n
);
1894 /* FIXME: optimize */
1895 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
1898 if(is_string(rval
) && is_number(lval
)) {
1902 hres
= to_number(ctx
, rval
, &n
);
1906 /* FIXME: optimize */
1907 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
1911 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
1914 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
1917 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
1921 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
1925 hres
= equal_values(ctx
, lval
, prim
, ret
);
1926 jsval_release(prim
);
1931 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
1935 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
1939 hres
= equal_values(ctx
, prim
, rval
, ret
);
1940 jsval_release(prim
);
1949 /* ECMA-262 3rd Edition 11.9.1 */
1950 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
1959 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1961 hres
= equal_values(ctx
->script
, l
, r
, &b
);
1967 return stack_push(ctx
, jsval_bool(b
));
1970 /* ECMA-262 3rd Edition 11.9.2 */
1971 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
1980 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1982 hres
= equal_values(ctx
->script
, l
, r
, &b
);
1988 return stack_push(ctx
, jsval_bool(!b
));
1991 /* ECMA-262 3rd Edition 11.9.4 */
1992 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2001 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2003 hres
= equal2_values(r
, l
, &b
);
2009 return stack_push(ctx
, jsval_bool(b
));
2012 /* ECMA-262 3rd Edition 11.9.5 */
2013 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2024 hres
= equal2_values(r
, l
, &b
);
2030 return stack_push(ctx
, jsval_bool(!b
));
2033 /* ECMA-262 3rd Edition 11.8.5 */
2034 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2040 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2044 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2050 if(is_string(l
) && is_string(r
)) {
2051 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2052 jsstr_release(get_string(l
));
2053 jsstr_release(get_string(r
));
2057 hres
= to_number(ctx
, l
, &ln
);
2060 hres
= to_number(ctx
, r
, &rn
);
2065 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2069 /* ECMA-262 3rd Edition 11.8.1 */
2070 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2079 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2081 hres
= less_eval(ctx
->script
, l
, r
, FALSE
, &b
);
2087 return stack_push(ctx
, jsval_bool(b
));
2090 /* ECMA-262 3rd Edition 11.8.1 */
2091 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2100 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2102 hres
= less_eval(ctx
->script
, r
, l
, TRUE
, &b
);
2108 return stack_push(ctx
, jsval_bool(b
));
2111 /* ECMA-262 3rd Edition 11.8.2 */
2112 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2121 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2123 hres
= less_eval(ctx
->script
, r
, l
, FALSE
, &b
);
2129 return stack_push(ctx
, jsval_bool(b
));
2132 /* ECMA-262 3rd Edition 11.8.4 */
2133 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2142 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2144 hres
= less_eval(ctx
->script
, l
, r
, TRUE
, &b
);
2150 return stack_push(ctx
, jsval_bool(b
));
2153 /* ECMA-262 3rd Edition 11.4.8 */
2154 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
2163 hres
= to_int32(ctx
->script
, v
, &i
);
2168 return stack_push(ctx
, jsval_number(~i
));
2171 /* ECMA-262 3rd Edition 11.4.9 */
2172 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2181 hres
= to_boolean(v
, &b
);
2186 return stack_push(ctx
, jsval_bool(!b
));
2189 /* ECMA-262 3rd Edition 11.7.1 */
2190 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
2196 hres
= stack_pop_uint(ctx
, &r
);
2200 hres
= stack_pop_int(ctx
, &l
);
2204 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2207 /* ECMA-262 3rd Edition 11.7.2 */
2208 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
2214 hres
= stack_pop_uint(ctx
, &r
);
2218 hres
= stack_pop_int(ctx
, &l
);
2222 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2225 /* ECMA-262 3rd Edition 11.7.3 */
2226 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
2231 hres
= stack_pop_uint(ctx
, &r
);
2235 hres
= stack_pop_uint(ctx
, &l
);
2239 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2242 /* ECMA-262 3rd Edition 11.13.1 */
2243 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
2254 disp
= stack_pop_objid(ctx
, &id
);
2257 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2260 hres
= disp_propput(ctx
->script
, disp
, id
, v
);
2261 IDispatch_Release(disp
);
2267 return stack_push(ctx
, v
);
2270 /* JScript extension */
2271 static HRESULT
interp_assign_call(exec_ctx_t
*ctx
)
2273 const unsigned argc
= get_op_uint(ctx
, 0);
2279 TRACE("%u\n", argc
);
2281 disp
= stack_topn_objid(ctx
, argc
+1, &id
);
2283 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2285 hres
= disp_call(ctx
->script
, disp
, id
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2290 stack_popn(ctx
, argc
+2);
2291 return stack_push(ctx
, v
);
2294 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
2298 return stack_push(ctx
, jsval_undefined());
2301 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
2303 const unsigned arg
= get_op_uint(ctx
, 0);
2311 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
2313 const unsigned arg
= get_op_uint(ctx
, 0);
2321 hres
= to_boolean(v
, &b
);
2333 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
2335 const unsigned arg
= get_op_uint(ctx
, 0);
2339 stack_popn(ctx
, arg
);
2343 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
2351 static HRESULT
interp_setret(exec_ctx_t
*ctx
)
2355 jsval_release(ctx
->ret
);
2356 ctx
->ret
= stack_pop(ctx
);
2360 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
2362 static const op_func_t op_funcs
[] = {
2363 #define X(x,a,b,c) interp_##x,
2368 static const unsigned op_move
[] = {
2369 #define X(a,x,b,c) x,
2374 static HRESULT
unwind_exception(exec_ctx_t
*ctx
)
2376 except_frame_t
*except_frame
;
2381 except_frame
= ctx
->except_frame
;
2382 ctx
->except_frame
= except_frame
->next
;
2384 assert(except_frame
->stack_top
<= ctx
->top
);
2385 stack_popn(ctx
, ctx
->top
- except_frame
->stack_top
);
2387 while(except_frame
->scope
!= ctx
->scope_chain
)
2388 scope_pop(&ctx
->scope_chain
);
2390 ctx
->ip
= except_frame
->catch_off
;
2392 except_val
= ctx
->script
->ei
.val
;
2393 ctx
->script
->ei
.val
= jsval_undefined();
2394 clear_ei(ctx
->script
);
2396 ident
= except_frame
->ident
;
2397 heap_free(except_frame
);
2400 jsdisp_t
*scope_obj
;
2402 hres
= create_dispex(ctx
->script
, NULL
, NULL
, &scope_obj
);
2403 if(SUCCEEDED(hres
)) {
2404 hres
= jsdisp_propput_name(scope_obj
, ident
, except_val
);
2406 jsdisp_release(scope_obj
);
2408 jsval_release(except_val
);
2412 hres
= scope_push(ctx
->scope_chain
, scope_obj
, to_disp(scope_obj
), &ctx
->scope_chain
);
2413 jsdisp_release(scope_obj
);
2415 hres
= stack_push(ctx
, except_val
);
2419 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2425 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, jsval_t
*ret
)
2427 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
2428 except_frame_t
*prev_except_frame
;
2429 function_code_t
*prev_func
;
2430 unsigned prev_ip
, prev_top
;
2431 scope_chain_t
*prev_scope
;
2432 bytecode_t
*prev_code
;
2434 HRESULT hres
= S_OK
;
2438 prev_top
= exec_ctx
->top
;
2439 prev_scope
= exec_ctx
->scope_chain
;
2440 prev_except_frame
= exec_ctx
->except_frame
;
2441 prev_ip
= exec_ctx
->ip
;
2442 prev_code
= exec_ctx
->code
;
2443 prev_func
= exec_ctx
->func_code
;
2444 exec_ctx
->ip
= func
->instr_off
;
2445 exec_ctx
->except_frame
= NULL
;
2446 exec_ctx
->code
= code
;
2447 exec_ctx
->func_code
= func
;
2449 while(exec_ctx
->ip
!= -1) {
2450 op
= code
->instrs
[exec_ctx
->ip
].op
;
2451 hres
= op_funcs
[op
](exec_ctx
);
2453 TRACE("EXCEPTION %08x\n", hres
);
2455 if(!exec_ctx
->except_frame
)
2458 hres
= unwind_exception(exec_ctx
);
2462 exec_ctx
->ip
+= op_move
[op
];
2466 exec_ctx
->ip
= prev_ip
;
2467 exec_ctx
->except_frame
= prev_except_frame
;
2468 exec_ctx
->code
= prev_code
;
2469 exec_ctx
->func_code
= prev_func
;
2472 while(exec_ctx
->scope_chain
!= prev_scope
)
2473 scope_pop(&exec_ctx
->scope_chain
);
2474 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
2478 assert(exec_ctx
->top
== prev_top
+1 || exec_ctx
->top
== prev_top
);
2479 assert(exec_ctx
->scope_chain
== prev_scope
);
2480 assert(exec_ctx
->top
== prev_top
);
2482 *ret
= exec_ctx
->ret
;
2483 exec_ctx
->ret
= jsval_undefined();
2487 HRESULT
exec_source(exec_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, BOOL from_eval
, jsval_t
*ret
)
2489 exec_ctx_t
*prev_ctx
;
2492 HRESULT hres
= S_OK
;
2494 for(i
= 0; i
< func
->func_cnt
; i
++) {
2497 if(!func
->funcs
[i
].name
)
2500 hres
= create_source_function(ctx
->script
, code
, func
->funcs
+i
, ctx
->scope_chain
, &func_obj
);
2504 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->funcs
[i
].name
, jsval_obj(func_obj
));
2505 jsdisp_release(func_obj
);
2510 for(i
=0; i
< func
->var_cnt
; i
++) {
2511 if(!ctx
->is_global
|| !lookup_global_members(ctx
->script
, func
->variables
[i
], NULL
)) {
2514 hres
= jsdisp_get_id(ctx
->var_disp
, func
->variables
[i
], fdexNameEnsure
, &id
);
2520 prev_ctx
= ctx
->script
->exec_ctx
;
2521 ctx
->script
->exec_ctx
= ctx
;
2523 hres
= enter_bytecode(ctx
->script
, code
, func
, &val
);
2524 assert(ctx
->script
->exec_ctx
== ctx
);
2525 ctx
->script
->exec_ctx
= prev_ctx
;