2 * Copyright 2008,2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
32 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
33 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
34 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
35 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
36 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
37 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
38 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
40 struct _except_frame_t
{
49 static HRESULT
stack_push(exec_ctx_t
*ctx
, jsval_t v
)
51 if(!ctx
->stack_size
) {
52 ctx
->stack
= heap_alloc(16*sizeof(*ctx
->stack
));
56 }else if(ctx
->stack_size
== ctx
->top
) {
59 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*new_stack
));
65 ctx
->stack
= new_stack
;
69 ctx
->stack
[ctx
->top
++] = v
;
73 static inline HRESULT
stack_push_string(exec_ctx_t
*ctx
, const WCHAR
*str
)
81 return stack_push(ctx
, jsval_string(v
));
84 static HRESULT
stack_push_objid(exec_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
)
88 hres
= stack_push(ctx
, jsval_disp(disp
));
92 return stack_push(ctx
, jsval_number(id
));
95 static inline jsval_t
stack_top(exec_ctx_t
*ctx
)
98 return ctx
->stack
[ctx
->top
-1];
101 static inline jsval_t
stack_topn(exec_ctx_t
*ctx
, unsigned n
)
103 assert(ctx
->top
> n
);
104 return ctx
->stack
[ctx
->top
-1-n
];
107 static inline jsval_t
*stack_args(exec_ctx_t
*ctx
, unsigned n
)
111 assert(ctx
->top
> n
-1);
112 return ctx
->stack
+ ctx
->top
-n
;
115 static inline jsval_t
stack_pop(exec_ctx_t
*ctx
)
118 return ctx
->stack
[--ctx
->top
];
121 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
124 jsval_release(stack_pop(ctx
));
127 static HRESULT
stack_pop_number(exec_ctx_t
*ctx
, double *r
)
133 hres
= to_number(ctx
->script
, v
, r
);
138 static HRESULT
stack_pop_object(exec_ctx_t
*ctx
, IDispatch
**r
)
144 if(is_object_instance(v
)) {
146 return throw_type_error(ctx
->script
, JS_E_OBJECT_REQUIRED
, NULL
);
151 hres
= to_object(ctx
->script
, v
, r
);
156 static inline HRESULT
stack_pop_int(exec_ctx_t
*ctx
, INT
*r
)
158 return to_int32(ctx
->script
, stack_pop(ctx
), r
);
161 static inline HRESULT
stack_pop_uint(exec_ctx_t
*ctx
, DWORD
*r
)
163 return to_uint32(ctx
->script
, stack_pop(ctx
), r
);
166 static inline IDispatch
*stack_pop_objid(exec_ctx_t
*ctx
, DISPID
*id
)
168 assert(is_number(stack_top(ctx
)) && is_object_instance(stack_topn(ctx
, 1)));
170 *id
= get_number(stack_pop(ctx
));
171 return get_object(stack_pop(ctx
));
174 static inline IDispatch
*stack_topn_objid(exec_ctx_t
*ctx
, unsigned n
, DISPID
*id
)
176 assert(is_number(stack_topn(ctx
, n
)) && is_object_instance(stack_topn(ctx
, n
+1)));
178 *id
= get_number(stack_topn(ctx
, n
));
179 return get_object(stack_topn(ctx
, n
+1));
182 static void exprval_release(exprval_t
*val
)
186 jsval_release(val
->u
.val
);
189 if(val
->u
.idref
.disp
)
190 IDispatch_Release(val
->u
.idref
.disp
);
192 case EXPRVAL_INVALID
:
197 /* ECMA-262 3rd Edition 8.7.1 */
198 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsval_t
*ret
)
203 val
->u
.val
= jsval_undefined();
206 if(!val
->u
.idref
.disp
) {
207 FIXME("throw ReferenceError\n");
211 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
);
212 case EXPRVAL_INVALID
:
216 ERR("type %d\n", val
->type
);
220 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
222 val
->type
= EXPRVAL_IDREF
;
223 val
->u
.idref
.disp
= disp
;
224 val
->u
.idref
.id
= id
;
227 IDispatch_AddRef(disp
);
230 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
232 scope_chain_t
*new_scope
;
234 new_scope
= heap_alloc(sizeof(scope_chain_t
));
236 return E_OUTOFMEMORY
;
240 IDispatch_AddRef(obj
);
241 new_scope
->jsobj
= jsobj
;
242 new_scope
->obj
= obj
;
246 new_scope
->next
= scope
;
248 new_scope
->next
= NULL
;
255 static void scope_pop(scope_chain_t
**scope
)
264 void clear_ei(script_ctx_t
*ctx
)
266 memset(&ctx
->ei
.ei
, 0, sizeof(ctx
->ei
.ei
));
267 jsval_release(ctx
->ei
.val
);
268 ctx
->ei
.val
= jsval_undefined();
271 void scope_release(scope_chain_t
*scope
)
277 scope_release(scope
->next
);
279 IDispatch_Release(scope
->obj
);
283 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, jsdisp_t
*var_disp
,
284 scope_chain_t
*scope
, BOOL is_global
, exec_ctx_t
**ret
)
288 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
290 return E_OUTOFMEMORY
;
293 ctx
->is_global
= is_global
;
294 ctx
->ret
= jsval_undefined();
296 /* ECMA-262 3rd Edition 11.2.3.7 */
300 jsthis
= iface_to_jsdisp((IUnknown
*)this_obj
);
302 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
304 jsdisp_release(jsthis
);
309 ctx
->this_obj
= this_obj
;
310 else if(script_ctx
->host_global
)
311 ctx
->this_obj
= script_ctx
->host_global
;
313 ctx
->this_obj
= to_disp(script_ctx
->global
);
314 IDispatch_AddRef(ctx
->this_obj
);
316 jsdisp_addref(var_disp
);
317 ctx
->var_disp
= var_disp
;
319 script_addref(script_ctx
);
320 ctx
->script
= script_ctx
;
324 ctx
->scope_chain
= scope
;
331 void exec_release(exec_ctx_t
*ctx
)
337 scope_release(ctx
->scope_chain
);
339 jsdisp_release(ctx
->var_disp
);
341 IDispatch_Release(ctx
->this_obj
);
343 script_release(ctx
->script
);
344 jsval_release(ctx
->ret
);
345 heap_free(ctx
->stack
);
349 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
356 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
358 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
359 jsdisp_release(jsdisp
);
366 bstr
= SysAllocString(name
);
368 return E_OUTOFMEMORY
;
372 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
373 if(SUCCEEDED(hres
)) {
374 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
375 IDispatchEx_Release(dispex
);
377 TRACE("using IDispatch\n");
378 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
381 if(name_bstr
!= bstr
)
386 static inline BOOL
var_is_null(const VARIANT
*v
)
388 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
391 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
393 IObjectIdentity
*identity
;
394 IUnknown
*unk1
, *unk2
;
402 if(!disp1
|| !disp2
) {
407 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
411 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
413 IUnknown_Release(unk1
);
420 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
421 if(SUCCEEDED(hres
)) {
422 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
423 IObjectIdentity_Release(identity
);
430 IUnknown_Release(unk1
);
431 IUnknown_Release(unk2
);
435 /* ECMA-262 3rd Edition 11.9.6 */
436 static HRESULT
equal2_values(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
438 jsval_type_t type
= jsval_type(lval
);
442 if(type
!= jsval_type(rval
)) {
443 if(is_null_instance(lval
))
444 *ret
= is_null_instance(rval
);
456 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
458 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
461 *ret
= get_number(lval
) == get_number(rval
);
464 *ret
= !get_bool(lval
) == !get_bool(rval
);
467 FIXME("VARIANT not implemented\n");
474 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
480 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
481 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
482 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
483 if(SUCCEEDED(hres
)) {
485 exprval_set_idref(ret
, item
->disp
, id
);
494 /* ECMA-262 3rd Edition 10.1.4 */
495 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
497 scope_chain_t
*scope
;
502 TRACE("%s\n", debugstr_w(identifier
));
504 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
506 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
508 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
509 if(SUCCEEDED(hres
)) {
510 exprval_set_idref(ret
, scope
->obj
, id
);
515 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
516 if(SUCCEEDED(hres
)) {
517 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
521 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
522 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
529 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
530 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
532 WARN("GetItemInfo failed: %08x\n", hres
);
536 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
537 IUnknown_Release(unk
);
539 WARN("object does not implement IDispatch\n");
544 IDispatch_AddRef(item
->disp
);
545 ret
->type
= EXPRVAL_JSVAL
;
546 ret
->u
.val
= jsval_disp(item
->disp
);
551 if(lookup_global_members(ctx
, identifier
, ret
))
554 ret
->type
= EXPRVAL_INVALID
;
558 static inline BSTR
get_op_bstr(exec_ctx_t
*ctx
, int i
){
559 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].bstr
;
562 static inline unsigned get_op_uint(exec_ctx_t
*ctx
, int i
){
563 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].uint
;
566 static inline unsigned get_op_int(exec_ctx_t
*ctx
, int i
){
567 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].lng
;
570 static inline jsstr_t
*get_op_str(exec_ctx_t
*ctx
, int i
){
571 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].str
;
574 static inline double get_op_double(exec_ctx_t
*ctx
){
575 return ctx
->code
->instrs
[ctx
->ip
].u
.dbl
;
578 /* ECMA-262 3rd Edition 12.2 */
579 static HRESULT
interp_var_set(exec_ctx_t
*ctx
)
581 const BSTR name
= get_op_bstr(ctx
, 0);
585 TRACE("%s\n", debugstr_w(name
));
587 val
= stack_pop(ctx
);
588 hres
= jsdisp_propput_name(ctx
->var_disp
, name
, val
);
593 /* ECMA-262 3rd Edition 12.6.4 */
594 static HRESULT
interp_forin(exec_ctx_t
*ctx
)
596 const HRESULT arg
= get_op_uint(ctx
, 0);
597 IDispatch
*var_obj
, *obj
= NULL
;
605 assert(is_number(stack_top(ctx
)));
606 id
= get_number(stack_top(ctx
));
608 var_obj
= stack_topn_objid(ctx
, 1, &var_id
);
610 FIXME("invalid ref\n");
614 if(is_object_instance(stack_topn(ctx
, 3)))
615 obj
= get_object(stack_topn(ctx
, 3));
618 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
619 if(SUCCEEDED(hres
)) {
620 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
622 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
623 IDispatchEx_Release(dispex
);
627 TRACE("No IDispatchEx\n");
634 str
= jsstr_alloc_len(name
, SysStringLen(name
));
637 return E_OUTOFMEMORY
;
640 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
642 hres
= disp_propput(ctx
->script
, var_obj
, var_id
, jsval_string(str
));
655 /* ECMA-262 3rd Edition 12.10 */
656 static HRESULT
interp_push_scope(exec_ctx_t
*ctx
)
665 hres
= to_object(ctx
->script
, v
, &disp
);
670 hres
= scope_push(ctx
->scope_chain
, to_jsdisp(disp
), disp
, &ctx
->scope_chain
);
671 IDispatch_Release(disp
);
675 /* ECMA-262 3rd Edition 12.10 */
676 static HRESULT
interp_pop_scope(exec_ctx_t
*ctx
)
680 scope_pop(&ctx
->scope_chain
);
684 /* ECMA-262 3rd Edition 12.13 */
685 static HRESULT
interp_case(exec_ctx_t
*ctx
)
687 const unsigned arg
= get_op_uint(ctx
, 0);
695 hres
= equal2_values(stack_top(ctx
), v
, &b
);
709 /* ECMA-262 3rd Edition 12.13 */
710 static HRESULT
interp_throw(exec_ctx_t
*ctx
)
714 jsval_release(ctx
->script
->ei
.val
);
715 ctx
->script
->ei
.val
= stack_pop(ctx
);
716 return DISP_E_EXCEPTION
;
719 static HRESULT
interp_throw_ref(exec_ctx_t
*ctx
)
721 const HRESULT arg
= get_op_uint(ctx
, 0);
723 TRACE("%08x\n", arg
);
725 return throw_reference_error(ctx
->script
, arg
, NULL
);
728 static HRESULT
interp_throw_type(exec_ctx_t
*ctx
)
730 const HRESULT hres
= get_op_uint(ctx
, 0);
731 jsstr_t
*str
= get_op_str(ctx
, 1);
734 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
736 ptr
= jsstr_flatten(str
);
737 return ptr
? throw_type_error(ctx
->script
, hres
, ptr
) : E_OUTOFMEMORY
;
740 /* ECMA-262 3rd Edition 12.14 */
741 static HRESULT
interp_push_except(exec_ctx_t
*ctx
)
743 const unsigned arg1
= get_op_uint(ctx
, 0);
744 const BSTR arg2
= get_op_bstr(ctx
, 1);
745 except_frame_t
*except
;
750 stack_top
= ctx
->top
;
755 hres
= stack_push(ctx
, jsval_bool(TRUE
));
758 hres
= stack_push(ctx
, jsval_bool(TRUE
));
763 except
= heap_alloc(sizeof(*except
));
765 return E_OUTOFMEMORY
;
767 except
->stack_top
= stack_top
;
768 except
->scope
= ctx
->scope_chain
;
769 except
->catch_off
= arg1
;
770 except
->ident
= arg2
;
771 except
->next
= ctx
->except_frame
;
772 ctx
->except_frame
= except
;
776 /* ECMA-262 3rd Edition 12.14 */
777 static HRESULT
interp_pop_except(exec_ctx_t
*ctx
)
779 except_frame_t
*except
;
783 except
= ctx
->except_frame
;
784 assert(except
!= NULL
);
786 ctx
->except_frame
= except
->next
;
791 /* ECMA-262 3rd Edition 12.14 */
792 static HRESULT
interp_end_finally(exec_ctx_t
*ctx
)
802 TRACE("passing exception\n");
804 ctx
->script
->ei
.val
= stack_pop(ctx
);
805 return DISP_E_EXCEPTION
;
812 /* ECMA-262 3rd Edition 13 */
813 static HRESULT
interp_func(exec_ctx_t
*ctx
)
815 unsigned func_idx
= get_op_uint(ctx
, 0);
819 TRACE("%d\n", func_idx
);
821 hres
= create_source_function(ctx
->script
, ctx
->code
, ctx
->func_code
->funcs
+func_idx
,
822 ctx
->scope_chain
, &dispex
);
826 return stack_push(ctx
, jsval_obj(dispex
));
829 /* ECMA-262 3rd Edition 11.2.1 */
830 static HRESULT
interp_array(exec_ctx_t
*ctx
)
841 namev
= stack_pop(ctx
);
843 hres
= stack_pop_object(ctx
, &obj
);
845 jsval_release(namev
);
849 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
850 jsval_release(namev
);
852 IDispatch_Release(obj
);
856 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, 0, &id
);
857 jsstr_release(name_str
);
858 if(SUCCEEDED(hres
)) {
859 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
860 }else if(hres
== DISP_E_UNKNOWNNAME
) {
861 v
= jsval_undefined();
864 IDispatch_Release(obj
);
868 return stack_push(ctx
, v
);
871 /* ECMA-262 3rd Edition 11.2.1 */
872 static HRESULT
interp_member(exec_ctx_t
*ctx
)
874 const BSTR arg
= get_op_bstr(ctx
, 0);
882 hres
= stack_pop_object(ctx
, &obj
);
886 hres
= disp_get_id(ctx
->script
, obj
, arg
, arg
, 0, &id
);
887 if(SUCCEEDED(hres
)) {
888 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
889 }else if(hres
== DISP_E_UNKNOWNNAME
) {
890 v
= jsval_undefined();
893 IDispatch_Release(obj
);
897 return stack_push(ctx
, v
);
900 /* ECMA-262 3rd Edition 11.2.1 */
901 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
903 const unsigned arg
= get_op_uint(ctx
, 0);
913 namev
= stack_pop(ctx
);
914 objv
= stack_pop(ctx
);
916 hres
= to_object(ctx
->script
, objv
, &obj
);
918 if(SUCCEEDED(hres
)) {
919 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
921 IDispatch_Release(obj
);
923 jsval_release(namev
);
927 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, arg
, &id
);
928 jsstr_release(name_str
);
930 IDispatch_Release(obj
);
931 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
933 id
= JS_E_INVALID_PROPERTY
;
935 ERR("failed %08x\n", hres
);
940 return stack_push_objid(ctx
, obj
, id
);
943 /* ECMA-262 3rd Edition 11.2.1 */
944 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
953 disp
= stack_topn_objid(ctx
, 0, &id
);
955 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
957 hres
= disp_propget(ctx
->script
, disp
, id
, &v
);
961 return stack_push(ctx
, v
);
964 /* ECMA-262 3rd Edition 11.2.2 */
965 static HRESULT
interp_new(exec_ctx_t
*ctx
)
967 const unsigned argc
= get_op_uint(ctx
, 0);
973 constr
= stack_topn(ctx
, argc
);
975 /* NOTE: Should use to_object here */
978 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
979 else if(!is_object_instance(constr
))
980 return throw_type_error(ctx
->script
, JS_E_INVALID_ACTION
, NULL
);
981 else if(!get_object(constr
))
982 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
984 hres
= disp_call_value(ctx
->script
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
, argc
, stack_args(ctx
, argc
), &r
);
988 stack_popn(ctx
, argc
+1);
989 return stack_push(ctx
, r
);
992 /* ECMA-262 3rd Edition 11.2.3 */
993 static HRESULT
interp_call(exec_ctx_t
*ctx
)
995 const unsigned argn
= get_op_uint(ctx
, 0);
996 const int do_ret
= get_op_int(ctx
, 1);
1000 TRACE("%d %d\n", argn
, do_ret
);
1002 obj
= stack_topn(ctx
, argn
);
1003 if(!is_object_instance(obj
))
1004 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
1006 hres
= disp_call_value(ctx
->script
, get_object(obj
), NULL
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
),
1007 do_ret
? &r
: NULL
);
1011 stack_popn(ctx
, argn
+1);
1012 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1015 /* ECMA-262 3rd Edition 11.2.3 */
1016 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1018 const unsigned argn
= get_op_uint(ctx
, 0);
1019 const int do_ret
= get_op_int(ctx
, 1);
1025 TRACE("%d %d\n", argn
, do_ret
);
1027 obj
= stack_topn_objid(ctx
, argn
, &id
);
1029 return throw_type_error(ctx
->script
, id
, NULL
);
1031 hres
= disp_call(ctx
->script
, obj
, id
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
), do_ret
? &r
: NULL
);
1035 stack_popn(ctx
, argn
+2);
1036 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1040 /* ECMA-262 3rd Edition 11.1.1 */
1041 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1045 IDispatch_AddRef(ctx
->this_obj
);
1046 return stack_push(ctx
, jsval_disp(ctx
->this_obj
));
1049 /* ECMA-262 3rd Edition 10.1.4 */
1050 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1052 const BSTR arg
= get_op_bstr(ctx
, 0);
1057 TRACE("%s\n", debugstr_w(arg
));
1059 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1063 if(exprval
.type
== EXPRVAL_INVALID
)
1064 return throw_type_error(ctx
->script
, JS_E_UNDEFINED_VARIABLE
, arg
);
1066 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1067 exprval_release(&exprval
);
1071 return stack_push(ctx
, v
);
1074 /* ECMA-262 3rd Edition 10.1.4 */
1075 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1077 const BSTR arg
= get_op_bstr(ctx
, 0);
1078 const unsigned flags
= get_op_uint(ctx
, 1);
1082 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1084 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1088 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1091 hres
= jsdisp_get_id(ctx
->script
->global
, arg
, fdexNameEnsure
, &id
);
1095 exprval_set_idref(&exprval
, to_disp(ctx
->script
->global
), id
);
1098 if(exprval
.type
!= EXPRVAL_IDREF
) {
1099 WARN("invalid ref\n");
1100 exprval_release(&exprval
);
1101 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1104 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1107 /* ECMA-262 3rd Edition 7.8.1 */
1108 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1112 return stack_push(ctx
, jsval_null());
1115 /* ECMA-262 3rd Edition 7.8.2 */
1116 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1118 const int arg
= get_op_int(ctx
, 0);
1120 TRACE("%s\n", arg
? "true" : "false");
1122 return stack_push(ctx
, jsval_bool(arg
));
1125 /* ECMA-262 3rd Edition 7.8.3 */
1126 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1128 const int arg
= get_op_int(ctx
, 0);
1132 return stack_push(ctx
, jsval_number(arg
));
1135 /* ECMA-262 3rd Edition 7.8.3 */
1136 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1138 const double arg
= get_op_double(ctx
);
1140 TRACE("%lf\n", arg
);
1142 return stack_push(ctx
, jsval_number(arg
));
1145 /* ECMA-262 3rd Edition 7.8.4 */
1146 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1148 jsstr_t
*str
= get_op_str(ctx
, 0);
1150 TRACE("%s\n", debugstr_jsstr(str
));
1152 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1155 /* ECMA-262 3rd Edition 7.8 */
1156 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1158 jsstr_t
*source
= get_op_str(ctx
, 0);
1159 const unsigned flags
= get_op_uint(ctx
, 1);
1163 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1165 hres
= create_regexp(ctx
->script
, source
, flags
, ®exp
);
1169 return stack_push(ctx
, jsval_obj(regexp
));
1172 /* ECMA-262 3rd Edition 11.1.4 */
1173 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1175 const unsigned arg
= get_op_uint(ctx
, 0);
1183 hres
= create_array(ctx
->script
, arg
, &array
);
1189 val
= stack_pop(ctx
);
1190 hres
= jsdisp_propput_idx(array
, i
, val
);
1193 jsdisp_release(array
);
1198 return stack_push(ctx
, jsval_obj(array
));
1201 /* ECMA-262 3rd Edition 11.1.5 */
1202 static HRESULT
interp_new_obj(exec_ctx_t
*ctx
)
1209 hres
= create_object(ctx
->script
, NULL
, &obj
);
1213 return stack_push(ctx
, jsval_obj(obj
));
1216 /* ECMA-262 3rd Edition 11.1.5 */
1217 static HRESULT
interp_obj_prop(exec_ctx_t
*ctx
)
1219 const BSTR name
= get_op_bstr(ctx
, 0);
1224 TRACE("%s\n", debugstr_w(name
));
1226 val
= stack_pop(ctx
);
1228 assert(is_object_instance(stack_top(ctx
)));
1229 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1231 hres
= jsdisp_propput_name(obj
, name
, val
);
1236 /* ECMA-262 3rd Edition 11.11 */
1237 static HRESULT
interp_cnd_nz(exec_ctx_t
*ctx
)
1239 const unsigned arg
= get_op_uint(ctx
, 0);
1245 hres
= to_boolean(stack_top(ctx
), &b
);
1258 /* ECMA-262 3rd Edition 11.11 */
1259 static HRESULT
interp_cnd_z(exec_ctx_t
*ctx
)
1261 const unsigned arg
= get_op_uint(ctx
, 0);
1267 hres
= to_boolean(stack_top(ctx
), &b
);
1280 /* ECMA-262 3rd Edition 11.10 */
1281 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1288 hres
= stack_pop_int(ctx
, &r
);
1292 hres
= stack_pop_int(ctx
, &l
);
1296 return stack_push(ctx
, jsval_number(l
|r
));
1299 /* ECMA-262 3rd Edition 11.10 */
1300 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1307 hres
= stack_pop_int(ctx
, &r
);
1311 hres
= stack_pop_int(ctx
, &l
);
1315 return stack_push(ctx
, jsval_number(l
^r
));
1318 /* ECMA-262 3rd Edition 11.10 */
1319 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1326 hres
= stack_pop_int(ctx
, &r
);
1330 hres
= stack_pop_int(ctx
, &l
);
1334 return stack_push(ctx
, jsval_number(l
&r
));
1337 /* ECMA-262 3rd Edition 11.8.6 */
1338 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
1340 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1345 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1348 if(!is_object_instance(v
) || !get_object(v
)) {
1350 return throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1353 obj
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1354 IDispatch_Release(get_object(v
));
1356 FIXME("non-jsdisp objects not supported\n");
1360 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1361 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1363 hres
= throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1365 jsdisp_release(obj
);
1371 if(is_object_instance(prot
)) {
1372 if(is_object_instance(v
))
1373 tmp
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1374 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1375 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1381 jsdisp_release(tmp
);
1383 FIXME("prototype is not an object\n");
1387 jsval_release(prot
);
1392 return stack_push(ctx
, jsval_bool(ret
));
1395 /* ECMA-262 3rd Edition 11.8.7 */
1396 static HRESULT
interp_in(exec_ctx_t
*ctx
)
1407 obj
= stack_pop(ctx
);
1408 if(!is_object_instance(obj
) || !get_object(obj
)) {
1410 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1414 hres
= to_flat_string(ctx
->script
, v
, &jsstr
, &str
);
1417 IDispatch_Release(get_object(obj
));
1421 hres
= disp_get_id(ctx
->script
, get_object(obj
), str
, NULL
, 0, &id
);
1422 IDispatch_Release(get_object(obj
));
1423 jsstr_release(jsstr
);
1426 else if(hres
== DISP_E_UNKNOWNNAME
)
1431 return stack_push(ctx
, jsval_bool(ret
));
1434 /* ECMA-262 3rd Edition 11.6.1 */
1435 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1440 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1444 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1450 if(is_string(l
) || is_string(r
)) {
1451 jsstr_t
*lstr
, *rstr
= NULL
;
1453 hres
= to_string(ctx
, l
, &lstr
);
1455 hres
= to_string(ctx
, r
, &rstr
);
1457 if(SUCCEEDED(hres
)) {
1460 ret_str
= jsstr_concat(lstr
, rstr
);
1462 *ret
= jsval_string(ret_str
);
1464 hres
= E_OUTOFMEMORY
;
1467 jsstr_release(lstr
);
1469 jsstr_release(rstr
);
1473 hres
= to_number(ctx
, l
, &nl
);
1474 if(SUCCEEDED(hres
)) {
1475 hres
= to_number(ctx
, r
, &nr
);
1477 *ret
= jsval_number(nl
+nr
);
1486 /* ECMA-262 3rd Edition 11.6.1 */
1487 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1495 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1497 hres
= add_eval(ctx
->script
, l
, r
, &ret
);
1503 return stack_push(ctx
, ret
);
1506 /* ECMA-262 3rd Edition 11.6.2 */
1507 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1514 hres
= stack_pop_number(ctx
, &r
);
1518 hres
= stack_pop_number(ctx
, &l
);
1522 return stack_push(ctx
, jsval_number(l
-r
));
1525 /* ECMA-262 3rd Edition 11.5.1 */
1526 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1533 hres
= stack_pop_number(ctx
, &r
);
1537 hres
= stack_pop_number(ctx
, &l
);
1541 return stack_push(ctx
, jsval_number(l
*r
));
1544 /* ECMA-262 3rd Edition 11.5.2 */
1545 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1552 hres
= stack_pop_number(ctx
, &r
);
1556 hres
= stack_pop_number(ctx
, &l
);
1560 return stack_push(ctx
, jsval_number(l
/r
));
1563 /* ECMA-262 3rd Edition 11.5.3 */
1564 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1571 hres
= stack_pop_number(ctx
, &r
);
1575 hres
= stack_pop_number(ctx
, &l
);
1579 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1582 /* ECMA-262 3rd Edition 11.4.2 */
1583 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
1585 jsval_t objv
, namev
;
1593 namev
= stack_pop(ctx
);
1594 objv
= stack_pop(ctx
);
1596 hres
= to_object(ctx
->script
, objv
, &obj
);
1597 jsval_release(objv
);
1599 jsval_release(namev
);
1603 hres
= to_string(ctx
->script
, namev
, &name
);
1604 jsval_release(namev
);
1606 IDispatch_Release(obj
);
1610 hres
= disp_delete_name(ctx
->script
, obj
, name
, &ret
);
1611 IDispatch_Release(obj
);
1612 jsstr_release(name
);
1616 return stack_push(ctx
, jsval_bool(ret
));
1619 /* ECMA-262 3rd Edition 11.4.2 */
1620 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
1622 const BSTR arg
= get_op_bstr(ctx
, 0);
1627 TRACE("%s\n", debugstr_w(arg
));
1629 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1633 switch(exprval
.type
) {
1635 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1636 IDispatch_Release(exprval
.u
.idref
.disp
);
1640 case EXPRVAL_INVALID
:
1644 FIXME("Unsupported exprval\n");
1645 exprval_release(&exprval
);
1650 return stack_push(ctx
, jsval_bool(ret
));
1653 /* ECMA-262 3rd Edition 11.4.2 */
1654 static HRESULT
interp_void(exec_ctx_t
*ctx
)
1659 return stack_push(ctx
, jsval_undefined());
1662 /* ECMA-262 3rd Edition 11.4.3 */
1663 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1665 switch(jsval_type(v
)) {
1675 if(get_object(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)get_object(v
)))) {
1676 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1677 jsdisp_release(dispex
);
1693 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1700 /* ECMA-262 3rd Edition 11.4.3 */
1701 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
1711 obj
= stack_pop_objid(ctx
, &id
);
1713 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1715 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1716 IDispatch_Release(obj
);
1718 return stack_push_string(ctx
, unknownW
);
1720 hres
= typeof_string(v
, &ret
);
1725 return stack_push_string(ctx
, ret
);
1728 /* ECMA-262 3rd Edition 11.4.3 */
1729 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
1731 const BSTR arg
= get_op_bstr(ctx
, 0);
1737 TRACE("%s\n", debugstr_w(arg
));
1739 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1743 if(exprval
.type
== EXPRVAL_INVALID
) {
1744 hres
= stack_push(ctx
, jsval_string(jsstr_undefined()));
1745 exprval_release(&exprval
);
1749 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1750 exprval_release(&exprval
);
1754 hres
= typeof_string(v
, &ret
);
1759 return stack_push_string(ctx
, ret
);
1762 /* ECMA-262 3rd Edition 11.4.3 */
1763 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
1772 hres
= typeof_string(v
, &ret
);
1777 return stack_push_string(ctx
, ret
);
1780 /* ECMA-262 3rd Edition 11.4.7 */
1781 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
1788 hres
= stack_pop_number(ctx
, &n
);
1792 return stack_push(ctx
, jsval_number(-n
));
1795 /* ECMA-262 3rd Edition 11.4.6 */
1796 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
1805 hres
= to_number(ctx
->script
, v
, &n
);
1810 return stack_push(ctx
, jsval_number(n
));
1813 /* ECMA-262 3rd Edition 11.3.1 */
1814 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
1816 const int arg
= get_op_int(ctx
, 0);
1824 obj
= stack_pop_objid(ctx
, &id
);
1826 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1828 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1829 if(SUCCEEDED(hres
)) {
1832 hres
= to_number(ctx
->script
, v
, &n
);
1834 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(n
+(double)arg
));
1838 IDispatch_Release(obj
);
1842 return stack_push(ctx
, v
);
1845 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1846 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
1848 const int arg
= get_op_int(ctx
, 0);
1857 obj
= stack_pop_objid(ctx
, &id
);
1859 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1861 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1862 if(SUCCEEDED(hres
)) {
1865 hres
= to_number(ctx
->script
, v
, &n
);
1867 if(SUCCEEDED(hres
)) {
1868 ret
= n
+(double)arg
;
1869 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(ret
));
1872 IDispatch_Release(obj
);
1876 return stack_push(ctx
, jsval_number(ret
));
1879 /* ECMA-262 3rd Edition 11.9.3 */
1880 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
1882 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
1883 return equal2_values(lval
, rval
, ret
);
1885 /* FIXME: NULL disps should be handled in more general way */
1886 if(is_object_instance(lval
) && !get_object(lval
))
1887 return equal_values(ctx
, jsval_null(), rval
, ret
);
1888 if(is_object_instance(rval
) && !get_object(rval
))
1889 return equal_values(ctx
, lval
, jsval_null(), ret
);
1891 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
1896 if(is_string(lval
) && is_number(rval
)) {
1900 hres
= to_number(ctx
, lval
, &n
);
1904 /* FIXME: optimize */
1905 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
1908 if(is_string(rval
) && is_number(lval
)) {
1912 hres
= to_number(ctx
, rval
, &n
);
1916 /* FIXME: optimize */
1917 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
1921 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
1924 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
1927 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
1931 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
1935 hres
= equal_values(ctx
, lval
, prim
, ret
);
1936 jsval_release(prim
);
1941 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
1945 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
1949 hres
= equal_values(ctx
, prim
, rval
, ret
);
1950 jsval_release(prim
);
1959 /* ECMA-262 3rd Edition 11.9.1 */
1960 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
1969 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1971 hres
= equal_values(ctx
->script
, l
, r
, &b
);
1977 return stack_push(ctx
, jsval_bool(b
));
1980 /* ECMA-262 3rd Edition 11.9.2 */
1981 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
1990 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1992 hres
= equal_values(ctx
->script
, l
, r
, &b
);
1998 return stack_push(ctx
, jsval_bool(!b
));
2001 /* ECMA-262 3rd Edition 11.9.4 */
2002 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2011 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2013 hres
= equal2_values(r
, l
, &b
);
2019 return stack_push(ctx
, jsval_bool(b
));
2022 /* ECMA-262 3rd Edition 11.9.5 */
2023 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2034 hres
= equal2_values(r
, l
, &b
);
2040 return stack_push(ctx
, jsval_bool(!b
));
2043 /* ECMA-262 3rd Edition 11.8.5 */
2044 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2050 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2054 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2060 if(is_string(l
) && is_string(r
)) {
2061 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2062 jsstr_release(get_string(l
));
2063 jsstr_release(get_string(r
));
2067 hres
= to_number(ctx
, l
, &ln
);
2070 hres
= to_number(ctx
, r
, &rn
);
2075 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2079 /* ECMA-262 3rd Edition 11.8.1 */
2080 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2089 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2091 hres
= less_eval(ctx
->script
, l
, r
, FALSE
, &b
);
2097 return stack_push(ctx
, jsval_bool(b
));
2100 /* ECMA-262 3rd Edition 11.8.1 */
2101 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2110 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2112 hres
= less_eval(ctx
->script
, r
, l
, TRUE
, &b
);
2118 return stack_push(ctx
, jsval_bool(b
));
2121 /* ECMA-262 3rd Edition 11.8.2 */
2122 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2131 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2133 hres
= less_eval(ctx
->script
, r
, l
, FALSE
, &b
);
2139 return stack_push(ctx
, jsval_bool(b
));
2142 /* ECMA-262 3rd Edition 11.8.4 */
2143 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2152 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2154 hres
= less_eval(ctx
->script
, l
, r
, TRUE
, &b
);
2160 return stack_push(ctx
, jsval_bool(b
));
2163 /* ECMA-262 3rd Edition 11.4.8 */
2164 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
2173 hres
= to_int32(ctx
->script
, v
, &i
);
2178 return stack_push(ctx
, jsval_number(~i
));
2181 /* ECMA-262 3rd Edition 11.4.9 */
2182 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2191 hres
= to_boolean(v
, &b
);
2196 return stack_push(ctx
, jsval_bool(!b
));
2199 /* ECMA-262 3rd Edition 11.7.1 */
2200 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
2206 hres
= stack_pop_uint(ctx
, &r
);
2210 hres
= stack_pop_int(ctx
, &l
);
2214 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2217 /* ECMA-262 3rd Edition 11.7.2 */
2218 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
2224 hres
= stack_pop_uint(ctx
, &r
);
2228 hres
= stack_pop_int(ctx
, &l
);
2232 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2235 /* ECMA-262 3rd Edition 11.7.3 */
2236 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
2241 hres
= stack_pop_uint(ctx
, &r
);
2245 hres
= stack_pop_uint(ctx
, &l
);
2249 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2252 /* ECMA-262 3rd Edition 11.13.1 */
2253 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
2264 disp
= stack_pop_objid(ctx
, &id
);
2267 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2270 hres
= disp_propput(ctx
->script
, disp
, id
, v
);
2271 IDispatch_Release(disp
);
2277 return stack_push(ctx
, v
);
2280 /* JScript extension */
2281 static HRESULT
interp_assign_call(exec_ctx_t
*ctx
)
2283 const unsigned argc
= get_op_uint(ctx
, 0);
2289 TRACE("%u\n", argc
);
2291 disp
= stack_topn_objid(ctx
, argc
+1, &id
);
2293 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2295 hres
= disp_call(ctx
->script
, disp
, id
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2300 stack_popn(ctx
, argc
+2);
2301 return stack_push(ctx
, v
);
2304 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
2308 return stack_push(ctx
, jsval_undefined());
2311 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
2313 const unsigned arg
= get_op_uint(ctx
, 0);
2321 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
2323 const unsigned arg
= get_op_uint(ctx
, 0);
2331 hres
= to_boolean(v
, &b
);
2343 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
2345 const unsigned arg
= get_op_uint(ctx
, 0);
2349 stack_popn(ctx
, arg
);
2353 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
2361 static HRESULT
interp_setret(exec_ctx_t
*ctx
)
2365 jsval_release(ctx
->ret
);
2366 ctx
->ret
= stack_pop(ctx
);
2370 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
2372 static const op_func_t op_funcs
[] = {
2373 #define X(x,a,b,c) interp_##x,
2378 static const unsigned op_move
[] = {
2379 #define X(a,x,b,c) x,
2384 static HRESULT
unwind_exception(exec_ctx_t
*ctx
)
2386 except_frame_t
*except_frame
;
2391 except_frame
= ctx
->except_frame
;
2392 ctx
->except_frame
= except_frame
->next
;
2394 assert(except_frame
->stack_top
<= ctx
->top
);
2395 stack_popn(ctx
, ctx
->top
- except_frame
->stack_top
);
2397 while(except_frame
->scope
!= ctx
->scope_chain
)
2398 scope_pop(&ctx
->scope_chain
);
2400 ctx
->ip
= except_frame
->catch_off
;
2402 except_val
= ctx
->script
->ei
.val
;
2403 ctx
->script
->ei
.val
= jsval_undefined();
2404 clear_ei(ctx
->script
);
2406 ident
= except_frame
->ident
;
2407 heap_free(except_frame
);
2410 jsdisp_t
*scope_obj
;
2412 hres
= create_dispex(ctx
->script
, NULL
, NULL
, &scope_obj
);
2413 if(SUCCEEDED(hres
)) {
2414 hres
= jsdisp_propput_name(scope_obj
, ident
, except_val
);
2416 jsdisp_release(scope_obj
);
2418 jsval_release(except_val
);
2422 hres
= scope_push(ctx
->scope_chain
, scope_obj
, to_disp(scope_obj
), &ctx
->scope_chain
);
2423 jsdisp_release(scope_obj
);
2425 hres
= stack_push(ctx
, except_val
);
2429 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2435 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, jsval_t
*ret
)
2437 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
2438 except_frame_t
*prev_except_frame
;
2439 function_code_t
*prev_func
;
2440 unsigned prev_ip
, prev_top
;
2441 scope_chain_t
*prev_scope
;
2442 bytecode_t
*prev_code
;
2444 HRESULT hres
= S_OK
;
2448 prev_top
= exec_ctx
->top
;
2449 prev_scope
= exec_ctx
->scope_chain
;
2450 prev_except_frame
= exec_ctx
->except_frame
;
2451 prev_ip
= exec_ctx
->ip
;
2452 prev_code
= exec_ctx
->code
;
2453 prev_func
= exec_ctx
->func_code
;
2454 exec_ctx
->ip
= func
->instr_off
;
2455 exec_ctx
->except_frame
= NULL
;
2456 exec_ctx
->code
= code
;
2457 exec_ctx
->func_code
= func
;
2459 while(exec_ctx
->ip
!= -1) {
2460 op
= code
->instrs
[exec_ctx
->ip
].op
;
2461 hres
= op_funcs
[op
](exec_ctx
);
2463 TRACE("EXCEPTION %08x\n", hres
);
2465 if(!exec_ctx
->except_frame
)
2468 hres
= unwind_exception(exec_ctx
);
2472 exec_ctx
->ip
+= op_move
[op
];
2476 exec_ctx
->ip
= prev_ip
;
2477 exec_ctx
->except_frame
= prev_except_frame
;
2478 exec_ctx
->code
= prev_code
;
2479 exec_ctx
->func_code
= prev_func
;
2482 while(exec_ctx
->scope_chain
!= prev_scope
)
2483 scope_pop(&exec_ctx
->scope_chain
);
2484 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
2488 assert(exec_ctx
->top
== prev_top
+1 || exec_ctx
->top
== prev_top
);
2489 assert(exec_ctx
->scope_chain
== prev_scope
);
2490 assert(exec_ctx
->top
== prev_top
);
2492 *ret
= exec_ctx
->ret
;
2493 exec_ctx
->ret
= jsval_undefined();
2497 HRESULT
exec_source(exec_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, BOOL from_eval
, jsval_t
*ret
)
2499 exec_ctx_t
*prev_ctx
;
2502 HRESULT hres
= S_OK
;
2504 for(i
= 0; i
< func
->func_cnt
; i
++) {
2507 if(!func
->funcs
[i
].name
)
2510 hres
= create_source_function(ctx
->script
, code
, func
->funcs
+i
, ctx
->scope_chain
, &func_obj
);
2514 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->funcs
[i
].name
, jsval_obj(func_obj
));
2515 jsdisp_release(func_obj
);
2520 for(i
=0; i
< func
->var_cnt
; i
++) {
2521 if(!ctx
->is_global
|| !lookup_global_members(ctx
->script
, func
->variables
[i
], NULL
)) {
2524 hres
= jsdisp_get_id(ctx
->var_disp
, func
->variables
[i
], fdexNameEnsure
, &id
);
2530 prev_ctx
= ctx
->script
->exec_ctx
;
2531 ctx
->script
->exec_ctx
= ctx
;
2533 hres
= enter_bytecode(ctx
->script
, code
, func
, &val
);
2534 assert(ctx
->script
->exec_ctx
== ctx
);
2535 ctx
->script
->exec_ctx
= prev_ctx
;