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
)
791 TRACE("passing exception\n");
793 ctx
->script
->ei
.val
= stack_pop(ctx
);
794 return DISP_E_EXCEPTION
;
801 /* ECMA-262 3rd Edition 13 */
802 static HRESULT
interp_func(exec_ctx_t
*ctx
)
804 unsigned func_idx
= get_op_uint(ctx
, 0);
808 TRACE("%d\n", func_idx
);
810 hres
= create_source_function(ctx
->script
, ctx
->code
, ctx
->func_code
->funcs
+func_idx
,
811 ctx
->scope_chain
, &dispex
);
815 return stack_push(ctx
, jsval_obj(dispex
));
818 /* ECMA-262 3rd Edition 11.2.1 */
819 static HRESULT
interp_array(exec_ctx_t
*ctx
)
830 namev
= stack_pop(ctx
);
832 hres
= stack_pop_object(ctx
, &obj
);
834 jsval_release(namev
);
838 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
839 jsval_release(namev
);
841 IDispatch_Release(obj
);
845 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, 0, &id
);
846 jsstr_release(name_str
);
847 if(SUCCEEDED(hres
)) {
848 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
849 }else if(hres
== DISP_E_UNKNOWNNAME
) {
850 v
= jsval_undefined();
853 IDispatch_Release(obj
);
857 return stack_push(ctx
, v
);
860 /* ECMA-262 3rd Edition 11.2.1 */
861 static HRESULT
interp_member(exec_ctx_t
*ctx
)
863 const BSTR arg
= get_op_bstr(ctx
, 0);
871 hres
= stack_pop_object(ctx
, &obj
);
875 hres
= disp_get_id(ctx
->script
, obj
, arg
, arg
, 0, &id
);
876 if(SUCCEEDED(hres
)) {
877 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
878 }else if(hres
== DISP_E_UNKNOWNNAME
) {
879 v
= jsval_undefined();
882 IDispatch_Release(obj
);
886 return stack_push(ctx
, v
);
889 /* ECMA-262 3rd Edition 11.2.1 */
890 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
892 const unsigned arg
= get_op_uint(ctx
, 0);
902 namev
= stack_pop(ctx
);
903 objv
= stack_pop(ctx
);
905 hres
= to_object(ctx
->script
, objv
, &obj
);
907 if(SUCCEEDED(hres
)) {
908 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
910 IDispatch_Release(obj
);
912 jsval_release(namev
);
916 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, arg
, &id
);
917 jsstr_release(name_str
);
919 IDispatch_Release(obj
);
920 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
922 id
= JS_E_INVALID_PROPERTY
;
924 ERR("failed %08x\n", hres
);
929 return stack_push_objid(ctx
, obj
, id
);
932 /* ECMA-262 3rd Edition 11.2.1 */
933 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
942 disp
= stack_topn_objid(ctx
, 0, &id
);
944 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
946 hres
= disp_propget(ctx
->script
, disp
, id
, &v
);
950 return stack_push(ctx
, v
);
953 /* ECMA-262 3rd Edition 11.2.2 */
954 static HRESULT
interp_new(exec_ctx_t
*ctx
)
956 const unsigned argc
= get_op_uint(ctx
, 0);
962 constr
= stack_topn(ctx
, argc
);
964 /* NOTE: Should use to_object here */
967 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
968 else if(!is_object_instance(constr
))
969 return throw_type_error(ctx
->script
, JS_E_INVALID_ACTION
, NULL
);
970 else if(!get_object(constr
))
971 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
973 hres
= disp_call_value(ctx
->script
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
, argc
, stack_args(ctx
, argc
), &r
);
977 stack_popn(ctx
, argc
+1);
978 return stack_push(ctx
, r
);
981 /* ECMA-262 3rd Edition 11.2.3 */
982 static HRESULT
interp_call(exec_ctx_t
*ctx
)
984 const unsigned argn
= get_op_uint(ctx
, 0);
985 const int do_ret
= get_op_int(ctx
, 1);
989 TRACE("%d %d\n", argn
, do_ret
);
991 obj
= stack_topn(ctx
, argn
);
992 if(!is_object_instance(obj
))
993 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
995 hres
= disp_call_value(ctx
->script
, get_object(obj
), NULL
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
),
1000 stack_popn(ctx
, argn
+1);
1001 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1004 /* ECMA-262 3rd Edition 11.2.3 */
1005 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1007 const unsigned argn
= get_op_uint(ctx
, 0);
1008 const int do_ret
= get_op_int(ctx
, 1);
1014 TRACE("%d %d\n", argn
, do_ret
);
1016 obj
= stack_topn_objid(ctx
, argn
, &id
);
1018 return throw_type_error(ctx
->script
, id
, NULL
);
1020 hres
= disp_call(ctx
->script
, obj
, id
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
), do_ret
? &r
: NULL
);
1024 stack_popn(ctx
, argn
+2);
1025 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1029 /* ECMA-262 3rd Edition 11.1.1 */
1030 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1034 IDispatch_AddRef(ctx
->this_obj
);
1035 return stack_push(ctx
, jsval_disp(ctx
->this_obj
));
1038 /* ECMA-262 3rd Edition 10.1.4 */
1039 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1041 const BSTR arg
= get_op_bstr(ctx
, 0);
1046 TRACE("%s\n", debugstr_w(arg
));
1048 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1052 if(exprval
.type
== EXPRVAL_INVALID
)
1053 return throw_type_error(ctx
->script
, JS_E_UNDEFINED_VARIABLE
, arg
);
1055 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1056 exprval_release(&exprval
);
1060 return stack_push(ctx
, v
);
1063 /* ECMA-262 3rd Edition 10.1.4 */
1064 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1066 const BSTR arg
= get_op_bstr(ctx
, 0);
1067 const unsigned flags
= get_op_uint(ctx
, 1);
1071 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1073 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1077 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1080 hres
= jsdisp_get_id(ctx
->script
->global
, arg
, fdexNameEnsure
, &id
);
1084 exprval_set_idref(&exprval
, to_disp(ctx
->script
->global
), id
);
1087 if(exprval
.type
!= EXPRVAL_IDREF
) {
1088 WARN("invalid ref\n");
1089 exprval_release(&exprval
);
1090 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1093 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1096 /* ECMA-262 3rd Edition 7.8.1 */
1097 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1101 return stack_push(ctx
, jsval_null());
1104 /* ECMA-262 3rd Edition 7.8.2 */
1105 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1107 const int arg
= get_op_int(ctx
, 0);
1109 TRACE("%s\n", arg
? "true" : "false");
1111 return stack_push(ctx
, jsval_bool(arg
));
1114 /* ECMA-262 3rd Edition 7.8.3 */
1115 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1117 const int arg
= get_op_int(ctx
, 0);
1121 return stack_push(ctx
, jsval_number(arg
));
1124 /* ECMA-262 3rd Edition 7.8.3 */
1125 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1127 const double arg
= get_op_double(ctx
);
1129 TRACE("%lf\n", arg
);
1131 return stack_push(ctx
, jsval_number(arg
));
1134 /* ECMA-262 3rd Edition 7.8.4 */
1135 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1137 jsstr_t
*str
= get_op_str(ctx
, 0);
1139 TRACE("%s\n", debugstr_jsstr(str
));
1141 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1144 /* ECMA-262 3rd Edition 7.8 */
1145 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1147 jsstr_t
*source
= get_op_str(ctx
, 0);
1148 const unsigned flags
= get_op_uint(ctx
, 1);
1152 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1154 hres
= create_regexp(ctx
->script
, source
, flags
, ®exp
);
1158 return stack_push(ctx
, jsval_obj(regexp
));
1161 /* ECMA-262 3rd Edition 11.1.4 */
1162 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1164 const unsigned arg
= get_op_uint(ctx
, 0);
1172 hres
= create_array(ctx
->script
, arg
, &array
);
1178 val
= stack_pop(ctx
);
1179 hres
= jsdisp_propput_idx(array
, i
, val
);
1182 jsdisp_release(array
);
1187 return stack_push(ctx
, jsval_obj(array
));
1190 /* ECMA-262 3rd Edition 11.1.5 */
1191 static HRESULT
interp_new_obj(exec_ctx_t
*ctx
)
1198 hres
= create_object(ctx
->script
, NULL
, &obj
);
1202 return stack_push(ctx
, jsval_obj(obj
));
1205 /* ECMA-262 3rd Edition 11.1.5 */
1206 static HRESULT
interp_obj_prop(exec_ctx_t
*ctx
)
1208 const BSTR name
= get_op_bstr(ctx
, 0);
1213 TRACE("%s\n", debugstr_w(name
));
1215 val
= stack_pop(ctx
);
1217 assert(is_object_instance(stack_top(ctx
)));
1218 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1220 hres
= jsdisp_propput_name(obj
, name
, val
);
1225 /* ECMA-262 3rd Edition 11.11 */
1226 static HRESULT
interp_cnd_nz(exec_ctx_t
*ctx
)
1228 const unsigned arg
= get_op_uint(ctx
, 0);
1234 hres
= to_boolean(stack_top(ctx
), &b
);
1247 /* ECMA-262 3rd Edition 11.11 */
1248 static HRESULT
interp_cnd_z(exec_ctx_t
*ctx
)
1250 const unsigned arg
= get_op_uint(ctx
, 0);
1256 hres
= to_boolean(stack_top(ctx
), &b
);
1269 /* ECMA-262 3rd Edition 11.10 */
1270 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1277 hres
= stack_pop_int(ctx
, &r
);
1281 hres
= stack_pop_int(ctx
, &l
);
1285 return stack_push(ctx
, jsval_number(l
|r
));
1288 /* ECMA-262 3rd Edition 11.10 */
1289 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1296 hres
= stack_pop_int(ctx
, &r
);
1300 hres
= stack_pop_int(ctx
, &l
);
1304 return stack_push(ctx
, jsval_number(l
^r
));
1307 /* ECMA-262 3rd Edition 11.10 */
1308 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1315 hres
= stack_pop_int(ctx
, &r
);
1319 hres
= stack_pop_int(ctx
, &l
);
1323 return stack_push(ctx
, jsval_number(l
&r
));
1326 /* ECMA-262 3rd Edition 11.8.6 */
1327 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
1329 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1334 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1337 if(!is_object_instance(v
) || !get_object(v
)) {
1339 return throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1342 obj
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1343 IDispatch_Release(get_object(v
));
1345 FIXME("non-jsdisp objects not supported\n");
1349 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1350 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1352 hres
= throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1354 jsdisp_release(obj
);
1360 if(is_object_instance(prot
)) {
1361 if(is_object_instance(v
))
1362 tmp
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1363 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1364 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1370 jsdisp_release(tmp
);
1372 FIXME("prototype is not an object\n");
1376 jsval_release(prot
);
1381 return stack_push(ctx
, jsval_bool(ret
));
1384 /* ECMA-262 3rd Edition 11.8.7 */
1385 static HRESULT
interp_in(exec_ctx_t
*ctx
)
1396 obj
= stack_pop(ctx
);
1397 if(!is_object_instance(obj
) || !get_object(obj
)) {
1399 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1403 hres
= to_flat_string(ctx
->script
, v
, &jsstr
, &str
);
1406 IDispatch_Release(get_object(obj
));
1410 hres
= disp_get_id(ctx
->script
, get_object(obj
), str
, NULL
, 0, &id
);
1411 IDispatch_Release(get_object(obj
));
1412 jsstr_release(jsstr
);
1415 else if(hres
== DISP_E_UNKNOWNNAME
)
1420 return stack_push(ctx
, jsval_bool(ret
));
1423 /* ECMA-262 3rd Edition 11.6.1 */
1424 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1429 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1433 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1439 if(is_string(l
) || is_string(r
)) {
1440 jsstr_t
*lstr
, *rstr
= NULL
;
1442 hres
= to_string(ctx
, l
, &lstr
);
1444 hres
= to_string(ctx
, r
, &rstr
);
1446 if(SUCCEEDED(hres
)) {
1449 ret_str
= jsstr_concat(lstr
, rstr
);
1451 *ret
= jsval_string(ret_str
);
1453 hres
= E_OUTOFMEMORY
;
1456 jsstr_release(lstr
);
1458 jsstr_release(rstr
);
1462 hres
= to_number(ctx
, l
, &nl
);
1463 if(SUCCEEDED(hres
)) {
1464 hres
= to_number(ctx
, r
, &nr
);
1466 *ret
= jsval_number(nl
+nr
);
1475 /* ECMA-262 3rd Edition 11.6.1 */
1476 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1484 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1486 hres
= add_eval(ctx
->script
, l
, r
, &ret
);
1492 return stack_push(ctx
, ret
);
1495 /* ECMA-262 3rd Edition 11.6.2 */
1496 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1503 hres
= stack_pop_number(ctx
, &r
);
1507 hres
= stack_pop_number(ctx
, &l
);
1511 return stack_push(ctx
, jsval_number(l
-r
));
1514 /* ECMA-262 3rd Edition 11.5.1 */
1515 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1522 hres
= stack_pop_number(ctx
, &r
);
1526 hres
= stack_pop_number(ctx
, &l
);
1530 return stack_push(ctx
, jsval_number(l
*r
));
1533 /* ECMA-262 3rd Edition 11.5.2 */
1534 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1541 hres
= stack_pop_number(ctx
, &r
);
1545 hres
= stack_pop_number(ctx
, &l
);
1549 return stack_push(ctx
, jsval_number(l
/r
));
1552 /* ECMA-262 3rd Edition 11.5.3 */
1553 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1560 hres
= stack_pop_number(ctx
, &r
);
1564 hres
= stack_pop_number(ctx
, &l
);
1568 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1571 /* ECMA-262 3rd Edition 11.4.2 */
1572 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
1574 jsval_t objv
, namev
;
1582 namev
= stack_pop(ctx
);
1583 objv
= stack_pop(ctx
);
1585 hres
= to_object(ctx
->script
, objv
, &obj
);
1586 jsval_release(objv
);
1588 jsval_release(namev
);
1592 hres
= to_string(ctx
->script
, namev
, &name
);
1593 jsval_release(namev
);
1595 IDispatch_Release(obj
);
1599 hres
= disp_delete_name(ctx
->script
, obj
, name
, &ret
);
1600 IDispatch_Release(obj
);
1601 jsstr_release(name
);
1605 return stack_push(ctx
, jsval_bool(ret
));
1608 /* ECMA-262 3rd Edition 11.4.2 */
1609 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
1611 const BSTR arg
= get_op_bstr(ctx
, 0);
1616 TRACE("%s\n", debugstr_w(arg
));
1618 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1622 switch(exprval
.type
) {
1624 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1625 IDispatch_Release(exprval
.u
.idref
.disp
);
1629 case EXPRVAL_INVALID
:
1633 FIXME("Unsupported exprval\n");
1634 exprval_release(&exprval
);
1639 return stack_push(ctx
, jsval_bool(ret
));
1642 /* ECMA-262 3rd Edition 11.4.2 */
1643 static HRESULT
interp_void(exec_ctx_t
*ctx
)
1648 return stack_push(ctx
, jsval_undefined());
1651 /* ECMA-262 3rd Edition 11.4.3 */
1652 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1654 switch(jsval_type(v
)) {
1664 if(get_object(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)get_object(v
)))) {
1665 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1666 jsdisp_release(dispex
);
1682 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1689 /* ECMA-262 3rd Edition 11.4.3 */
1690 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
1700 obj
= stack_pop_objid(ctx
, &id
);
1702 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1704 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1705 IDispatch_Release(obj
);
1707 return stack_push_string(ctx
, unknownW
);
1709 hres
= typeof_string(v
, &ret
);
1714 return stack_push_string(ctx
, ret
);
1717 /* ECMA-262 3rd Edition 11.4.3 */
1718 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
1720 const BSTR arg
= get_op_bstr(ctx
, 0);
1726 TRACE("%s\n", debugstr_w(arg
));
1728 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1732 if(exprval
.type
== EXPRVAL_INVALID
) {
1733 hres
= stack_push(ctx
, jsval_string(jsstr_undefined()));
1734 exprval_release(&exprval
);
1738 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1739 exprval_release(&exprval
);
1743 hres
= typeof_string(v
, &ret
);
1748 return stack_push_string(ctx
, ret
);
1751 /* ECMA-262 3rd Edition 11.4.3 */
1752 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
1761 hres
= typeof_string(v
, &ret
);
1766 return stack_push_string(ctx
, ret
);
1769 /* ECMA-262 3rd Edition 11.4.7 */
1770 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
1777 hres
= stack_pop_number(ctx
, &n
);
1781 return stack_push(ctx
, jsval_number(-n
));
1784 /* ECMA-262 3rd Edition 11.4.6 */
1785 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
1794 hres
= to_number(ctx
->script
, v
, &n
);
1799 return stack_push(ctx
, jsval_number(n
));
1802 /* ECMA-262 3rd Edition 11.3.1 */
1803 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
1805 const int arg
= get_op_int(ctx
, 0);
1813 obj
= stack_pop_objid(ctx
, &id
);
1815 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1817 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1818 if(SUCCEEDED(hres
)) {
1821 hres
= to_number(ctx
->script
, v
, &n
);
1823 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(n
+(double)arg
));
1827 IDispatch_Release(obj
);
1831 return stack_push(ctx
, v
);
1834 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1835 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
1837 const int arg
= get_op_int(ctx
, 0);
1846 obj
= stack_pop_objid(ctx
, &id
);
1848 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1850 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1851 if(SUCCEEDED(hres
)) {
1854 hres
= to_number(ctx
->script
, v
, &n
);
1856 if(SUCCEEDED(hres
)) {
1857 ret
= n
+(double)arg
;
1858 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(ret
));
1861 IDispatch_Release(obj
);
1865 return stack_push(ctx
, jsval_number(ret
));
1868 /* ECMA-262 3rd Edition 11.9.3 */
1869 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
1871 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
1872 return equal2_values(lval
, rval
, ret
);
1874 /* FIXME: NULL disps should be handled in more general way */
1875 if(is_object_instance(lval
) && !get_object(lval
))
1876 return equal_values(ctx
, jsval_null(), rval
, ret
);
1877 if(is_object_instance(rval
) && !get_object(rval
))
1878 return equal_values(ctx
, lval
, jsval_null(), ret
);
1880 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
1885 if(is_string(lval
) && is_number(rval
)) {
1889 hres
= to_number(ctx
, lval
, &n
);
1893 /* FIXME: optimize */
1894 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
1897 if(is_string(rval
) && is_number(lval
)) {
1901 hres
= to_number(ctx
, rval
, &n
);
1905 /* FIXME: optimize */
1906 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
1910 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
1913 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
1916 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
1920 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
1924 hres
= equal_values(ctx
, lval
, prim
, ret
);
1925 jsval_release(prim
);
1930 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
1934 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
1938 hres
= equal_values(ctx
, prim
, rval
, ret
);
1939 jsval_release(prim
);
1948 /* ECMA-262 3rd Edition 11.9.1 */
1949 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
1958 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1960 hres
= equal_values(ctx
->script
, l
, r
, &b
);
1966 return stack_push(ctx
, jsval_bool(b
));
1969 /* ECMA-262 3rd Edition 11.9.2 */
1970 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
1979 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1981 hres
= equal_values(ctx
->script
, l
, r
, &b
);
1987 return stack_push(ctx
, jsval_bool(!b
));
1990 /* ECMA-262 3rd Edition 11.9.4 */
1991 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2000 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2002 hres
= equal2_values(r
, l
, &b
);
2008 return stack_push(ctx
, jsval_bool(b
));
2011 /* ECMA-262 3rd Edition 11.9.5 */
2012 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2023 hres
= equal2_values(r
, l
, &b
);
2029 return stack_push(ctx
, jsval_bool(!b
));
2032 /* ECMA-262 3rd Edition 11.8.5 */
2033 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2039 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2043 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2049 if(is_string(l
) && is_string(r
)) {
2050 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2051 jsstr_release(get_string(l
));
2052 jsstr_release(get_string(r
));
2056 hres
= to_number(ctx
, l
, &ln
);
2059 hres
= to_number(ctx
, r
, &rn
);
2064 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2068 /* ECMA-262 3rd Edition 11.8.1 */
2069 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2078 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2080 hres
= less_eval(ctx
->script
, l
, r
, FALSE
, &b
);
2086 return stack_push(ctx
, jsval_bool(b
));
2089 /* ECMA-262 3rd Edition 11.8.1 */
2090 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2099 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2101 hres
= less_eval(ctx
->script
, r
, l
, TRUE
, &b
);
2107 return stack_push(ctx
, jsval_bool(b
));
2110 /* ECMA-262 3rd Edition 11.8.2 */
2111 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2120 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2122 hres
= less_eval(ctx
->script
, r
, l
, FALSE
, &b
);
2128 return stack_push(ctx
, jsval_bool(b
));
2131 /* ECMA-262 3rd Edition 11.8.4 */
2132 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2141 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2143 hres
= less_eval(ctx
->script
, l
, r
, TRUE
, &b
);
2149 return stack_push(ctx
, jsval_bool(b
));
2152 /* ECMA-262 3rd Edition 11.4.8 */
2153 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
2162 hres
= to_int32(ctx
->script
, v
, &i
);
2167 return stack_push(ctx
, jsval_number(~i
));
2170 /* ECMA-262 3rd Edition 11.4.9 */
2171 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2180 hres
= to_boolean(v
, &b
);
2185 return stack_push(ctx
, jsval_bool(!b
));
2188 /* ECMA-262 3rd Edition 11.7.1 */
2189 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
2195 hres
= stack_pop_uint(ctx
, &r
);
2199 hres
= stack_pop_int(ctx
, &l
);
2203 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2206 /* ECMA-262 3rd Edition 11.7.2 */
2207 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
2213 hres
= stack_pop_uint(ctx
, &r
);
2217 hres
= stack_pop_int(ctx
, &l
);
2221 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2224 /* ECMA-262 3rd Edition 11.7.3 */
2225 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
2230 hres
= stack_pop_uint(ctx
, &r
);
2234 hres
= stack_pop_uint(ctx
, &l
);
2238 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2241 /* ECMA-262 3rd Edition 11.13.1 */
2242 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
2253 disp
= stack_pop_objid(ctx
, &id
);
2256 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2259 hres
= disp_propput(ctx
->script
, disp
, id
, v
);
2260 IDispatch_Release(disp
);
2266 return stack_push(ctx
, v
);
2269 /* JScript extension */
2270 static HRESULT
interp_assign_call(exec_ctx_t
*ctx
)
2272 const unsigned argc
= get_op_uint(ctx
, 0);
2278 TRACE("%u\n", argc
);
2280 disp
= stack_topn_objid(ctx
, argc
+1, &id
);
2282 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2284 hres
= disp_call(ctx
->script
, disp
, id
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2289 stack_popn(ctx
, argc
+2);
2290 return stack_push(ctx
, v
);
2293 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
2297 return stack_push(ctx
, jsval_undefined());
2300 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
2302 const unsigned arg
= get_op_uint(ctx
, 0);
2310 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
2312 const unsigned arg
= get_op_uint(ctx
, 0);
2320 hres
= to_boolean(v
, &b
);
2332 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
2334 const unsigned arg
= get_op_uint(ctx
, 0);
2338 stack_popn(ctx
, arg
);
2342 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
2350 static HRESULT
interp_setret(exec_ctx_t
*ctx
)
2354 jsval_release(ctx
->ret
);
2355 ctx
->ret
= stack_pop(ctx
);
2359 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
2361 static const op_func_t op_funcs
[] = {
2362 #define X(x,a,b,c) interp_##x,
2367 static const unsigned op_move
[] = {
2368 #define X(a,x,b,c) x,
2373 static HRESULT
unwind_exception(exec_ctx_t
*ctx
)
2375 except_frame_t
*except_frame
;
2380 except_frame
= ctx
->except_frame
;
2381 ctx
->except_frame
= except_frame
->next
;
2383 assert(except_frame
->stack_top
<= ctx
->top
);
2384 stack_popn(ctx
, ctx
->top
- except_frame
->stack_top
);
2386 while(except_frame
->scope
!= ctx
->scope_chain
)
2387 scope_pop(&ctx
->scope_chain
);
2389 ctx
->ip
= except_frame
->catch_off
;
2391 except_val
= ctx
->script
->ei
.val
;
2392 ctx
->script
->ei
.val
= jsval_undefined();
2393 clear_ei(ctx
->script
);
2395 ident
= except_frame
->ident
;
2396 heap_free(except_frame
);
2399 jsdisp_t
*scope_obj
;
2401 hres
= create_dispex(ctx
->script
, NULL
, NULL
, &scope_obj
);
2402 if(SUCCEEDED(hres
)) {
2403 hres
= jsdisp_propput_name(scope_obj
, ident
, except_val
);
2405 jsdisp_release(scope_obj
);
2407 jsval_release(except_val
);
2411 hres
= scope_push(ctx
->scope_chain
, scope_obj
, to_disp(scope_obj
), &ctx
->scope_chain
);
2412 jsdisp_release(scope_obj
);
2414 hres
= stack_push(ctx
, except_val
);
2418 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2424 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, jsval_t
*ret
)
2426 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
2427 except_frame_t
*prev_except_frame
;
2428 function_code_t
*prev_func
;
2429 unsigned prev_ip
, prev_top
;
2430 scope_chain_t
*prev_scope
;
2431 bytecode_t
*prev_code
;
2433 HRESULT hres
= S_OK
;
2437 prev_top
= exec_ctx
->top
;
2438 prev_scope
= exec_ctx
->scope_chain
;
2439 prev_except_frame
= exec_ctx
->except_frame
;
2440 prev_ip
= exec_ctx
->ip
;
2441 prev_code
= exec_ctx
->code
;
2442 prev_func
= exec_ctx
->func_code
;
2443 exec_ctx
->ip
= func
->instr_off
;
2444 exec_ctx
->except_frame
= NULL
;
2445 exec_ctx
->code
= code
;
2446 exec_ctx
->func_code
= func
;
2448 while(exec_ctx
->ip
!= -1) {
2449 op
= code
->instrs
[exec_ctx
->ip
].op
;
2450 hres
= op_funcs
[op
](exec_ctx
);
2452 TRACE("EXCEPTION %08x\n", hres
);
2454 if(!exec_ctx
->except_frame
)
2457 hres
= unwind_exception(exec_ctx
);
2461 exec_ctx
->ip
+= op_move
[op
];
2465 exec_ctx
->ip
= prev_ip
;
2466 exec_ctx
->except_frame
= prev_except_frame
;
2467 exec_ctx
->code
= prev_code
;
2468 exec_ctx
->func_code
= prev_func
;
2471 while(exec_ctx
->scope_chain
!= prev_scope
)
2472 scope_pop(&exec_ctx
->scope_chain
);
2473 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
2477 assert(exec_ctx
->top
== prev_top
+1 || exec_ctx
->top
== prev_top
);
2478 assert(exec_ctx
->scope_chain
== prev_scope
);
2479 assert(exec_ctx
->top
== prev_top
);
2481 *ret
= exec_ctx
->ret
;
2482 exec_ctx
->ret
= jsval_undefined();
2486 HRESULT
exec_source(exec_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, BOOL from_eval
, jsval_t
*ret
)
2488 exec_ctx_t
*prev_ctx
;
2491 HRESULT hres
= S_OK
;
2493 for(i
= 0; i
< func
->func_cnt
; i
++) {
2496 if(!func
->funcs
[i
].name
)
2499 hres
= create_source_function(ctx
->script
, code
, func
->funcs
+i
, ctx
->scope_chain
, &func_obj
);
2503 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->funcs
[i
].name
, jsval_obj(func_obj
));
2504 jsdisp_release(func_obj
);
2509 for(i
=0; i
< func
->var_cnt
; i
++) {
2510 if(!ctx
->is_global
|| !lookup_global_members(ctx
->script
, func
->variables
[i
], NULL
)) {
2513 hres
= jsdisp_get_id(ctx
->var_disp
, func
->variables
[i
], fdexNameEnsure
, &id
);
2519 prev_ctx
= ctx
->script
->exec_ctx
;
2520 ctx
->script
->exec_ctx
= ctx
;
2522 hres
= enter_bytecode(ctx
->script
, code
, func
, &val
);
2523 assert(ctx
->script
->exec_ctx
== ctx
);
2524 ctx
->script
->exec_ctx
= prev_ctx
;