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
{
64 static HRESULT
stack_push(exec_ctx_t
*ctx
, jsval_t v
)
66 if(!ctx
->stack_size
) {
67 ctx
->stack
= heap_alloc(16*sizeof(*ctx
->stack
));
71 }else if(ctx
->stack_size
== ctx
->top
) {
74 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*new_stack
));
80 ctx
->stack
= new_stack
;
84 ctx
->stack
[ctx
->top
++] = v
;
88 static inline HRESULT
stack_push_string(exec_ctx_t
*ctx
, const WCHAR
*str
)
96 return stack_push(ctx
, jsval_string(v
));
99 static HRESULT
stack_push_objid(exec_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
)
103 hres
= stack_push(ctx
, jsval_disp(disp
));
107 return stack_push(ctx
, jsval_number(id
));
110 static inline jsval_t
stack_top(exec_ctx_t
*ctx
)
113 return ctx
->stack
[ctx
->top
-1];
116 static inline jsval_t
stack_topn(exec_ctx_t
*ctx
, unsigned n
)
118 assert(ctx
->top
> n
);
119 return ctx
->stack
[ctx
->top
-1-n
];
122 static inline jsval_t
*stack_args(exec_ctx_t
*ctx
, unsigned n
)
126 assert(ctx
->top
> n
-1);
127 return ctx
->stack
+ ctx
->top
-n
;
130 static inline jsval_t
stack_pop(exec_ctx_t
*ctx
)
133 return ctx
->stack
[--ctx
->top
];
136 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
139 jsval_release(stack_pop(ctx
));
142 static HRESULT
stack_pop_number(exec_ctx_t
*ctx
, double *r
)
148 hres
= to_number(ctx
->script
, v
, r
);
153 static HRESULT
stack_pop_object(exec_ctx_t
*ctx
, IDispatch
**r
)
159 if(is_object_instance(v
)) {
161 return throw_type_error(ctx
->script
, JS_E_OBJECT_REQUIRED
, NULL
);
166 hres
= to_object(ctx
->script
, v
, r
);
171 static inline HRESULT
stack_pop_int(exec_ctx_t
*ctx
, INT
*r
)
173 return to_int32(ctx
->script
, stack_pop(ctx
), r
);
176 static inline HRESULT
stack_pop_uint(exec_ctx_t
*ctx
, DWORD
*r
)
178 return to_uint32(ctx
->script
, stack_pop(ctx
), r
);
181 static inline IDispatch
*stack_pop_objid(exec_ctx_t
*ctx
, DISPID
*id
)
183 assert(is_number(stack_top(ctx
)) && is_object_instance(stack_topn(ctx
, 1)));
185 *id
= get_number(stack_pop(ctx
));
186 return get_object(stack_pop(ctx
));
189 static inline IDispatch
*stack_topn_objid(exec_ctx_t
*ctx
, unsigned n
, DISPID
*id
)
191 assert(is_number(stack_topn(ctx
, n
)) && is_object_instance(stack_topn(ctx
, n
+1)));
193 *id
= get_number(stack_topn(ctx
, n
));
194 return get_object(stack_topn(ctx
, n
+1));
197 static void exprval_release(exprval_t
*val
)
201 jsval_release(val
->u
.val
);
204 if(val
->u
.idref
.disp
)
205 IDispatch_Release(val
->u
.idref
.disp
);
207 case EXPRVAL_INVALID
:
212 /* ECMA-262 3rd Edition 8.7.1 */
213 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsval_t
*ret
)
218 val
->u
.val
= jsval_undefined();
221 if(!val
->u
.idref
.disp
) {
222 FIXME("throw ReferenceError\n");
226 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
);
227 case EXPRVAL_INVALID
:
231 ERR("type %d\n", val
->type
);
235 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
237 val
->type
= EXPRVAL_IDREF
;
238 val
->u
.idref
.disp
= disp
;
239 val
->u
.idref
.id
= id
;
242 IDispatch_AddRef(disp
);
245 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
247 scope_chain_t
*new_scope
;
249 new_scope
= heap_alloc(sizeof(scope_chain_t
));
251 return E_OUTOFMEMORY
;
255 IDispatch_AddRef(obj
);
256 new_scope
->jsobj
= jsobj
;
257 new_scope
->obj
= obj
;
261 new_scope
->next
= scope
;
263 new_scope
->next
= NULL
;
270 static void scope_pop(scope_chain_t
**scope
)
279 void clear_ei(script_ctx_t
*ctx
)
281 memset(&ctx
->ei
.ei
, 0, sizeof(ctx
->ei
.ei
));
282 jsval_release(ctx
->ei
.val
);
283 ctx
->ei
.val
= jsval_undefined();
286 void scope_release(scope_chain_t
*scope
)
292 scope_release(scope
->next
);
294 IDispatch_Release(scope
->obj
);
298 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, jsdisp_t
*var_disp
,
299 scope_chain_t
*scope
, BOOL is_global
, exec_ctx_t
**ret
)
303 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
305 return E_OUTOFMEMORY
;
308 ctx
->is_global
= is_global
;
309 ctx
->ret
= jsval_undefined();
311 /* ECMA-262 3rd Edition 11.2.3.7 */
315 jsthis
= iface_to_jsdisp((IUnknown
*)this_obj
);
317 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
319 jsdisp_release(jsthis
);
324 ctx
->this_obj
= this_obj
;
325 else if(script_ctx
->host_global
)
326 ctx
->this_obj
= script_ctx
->host_global
;
328 ctx
->this_obj
= to_disp(script_ctx
->global
);
329 IDispatch_AddRef(ctx
->this_obj
);
331 jsdisp_addref(var_disp
);
332 ctx
->var_disp
= var_disp
;
334 script_addref(script_ctx
);
335 ctx
->script
= script_ctx
;
339 ctx
->scope_chain
= scope
;
346 void exec_release(exec_ctx_t
*ctx
)
352 scope_release(ctx
->scope_chain
);
354 jsdisp_release(ctx
->var_disp
);
356 IDispatch_Release(ctx
->this_obj
);
358 script_release(ctx
->script
);
359 jsval_release(ctx
->ret
);
360 heap_free(ctx
->stack
);
364 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
371 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
373 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
374 jsdisp_release(jsdisp
);
381 bstr
= SysAllocString(name
);
383 return E_OUTOFMEMORY
;
387 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
388 if(SUCCEEDED(hres
)) {
389 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
390 IDispatchEx_Release(dispex
);
392 TRACE("using IDispatch\n");
393 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
396 if(name_bstr
!= bstr
)
401 static inline BOOL
var_is_null(const VARIANT
*v
)
403 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
406 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
408 IObjectIdentity
*identity
;
409 IUnknown
*unk1
, *unk2
;
417 if(!disp1
|| !disp2
) {
422 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
426 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
428 IUnknown_Release(unk1
);
435 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
436 if(SUCCEEDED(hres
)) {
437 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
438 IObjectIdentity_Release(identity
);
445 IUnknown_Release(unk1
);
446 IUnknown_Release(unk2
);
450 /* ECMA-262 3rd Edition 11.9.6 */
451 static HRESULT
equal2_values(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
453 jsval_type_t type
= jsval_type(lval
);
457 if(type
!= jsval_type(rval
)) {
458 if(is_null_instance(lval
))
459 *ret
= is_null_instance(rval
);
471 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
473 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
476 *ret
= get_number(lval
) == get_number(rval
);
479 *ret
= !get_bool(lval
) == !get_bool(rval
);
482 FIXME("VARIANT not implemented\n");
489 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
495 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
496 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
497 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
498 if(SUCCEEDED(hres
)) {
500 exprval_set_idref(ret
, item
->disp
, id
);
509 /* ECMA-262 3rd Edition 10.1.4 */
510 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
512 scope_chain_t
*scope
;
517 TRACE("%s\n", debugstr_w(identifier
));
519 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
521 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
523 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
524 if(SUCCEEDED(hres
)) {
525 exprval_set_idref(ret
, scope
->obj
, id
);
530 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
531 if(SUCCEEDED(hres
)) {
532 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
536 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
537 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
544 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
545 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
547 WARN("GetItemInfo failed: %08x\n", hres
);
551 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
552 IUnknown_Release(unk
);
554 WARN("object does not implement IDispatch\n");
559 IDispatch_AddRef(item
->disp
);
560 ret
->type
= EXPRVAL_JSVAL
;
561 ret
->u
.val
= jsval_disp(item
->disp
);
566 if(lookup_global_members(ctx
, identifier
, ret
))
569 ret
->type
= EXPRVAL_INVALID
;
573 static inline BSTR
get_op_bstr(exec_ctx_t
*ctx
, int i
){
574 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].bstr
;
577 static inline unsigned get_op_uint(exec_ctx_t
*ctx
, int i
){
578 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].uint
;
581 static inline unsigned get_op_int(exec_ctx_t
*ctx
, int i
){
582 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].lng
;
585 static inline jsstr_t
*get_op_str(exec_ctx_t
*ctx
, int i
){
586 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].str
;
589 static inline double get_op_double(exec_ctx_t
*ctx
){
590 return ctx
->code
->instrs
[ctx
->ip
].u
.dbl
;
593 /* ECMA-262 3rd Edition 12.2 */
594 static HRESULT
interp_var_set(exec_ctx_t
*ctx
)
596 const BSTR name
= get_op_bstr(ctx
, 0);
600 TRACE("%s\n", debugstr_w(name
));
602 val
= stack_pop(ctx
);
603 hres
= jsdisp_propput_name(ctx
->var_disp
, name
, val
);
608 /* ECMA-262 3rd Edition 12.6.4 */
609 static HRESULT
interp_forin(exec_ctx_t
*ctx
)
611 const HRESULT arg
= get_op_uint(ctx
, 0);
612 IDispatch
*var_obj
, *obj
= NULL
;
620 assert(is_number(stack_top(ctx
)));
621 id
= get_number(stack_top(ctx
));
623 var_obj
= stack_topn_objid(ctx
, 1, &var_id
);
625 FIXME("invalid ref\n");
629 if(is_object_instance(stack_topn(ctx
, 3)))
630 obj
= get_object(stack_topn(ctx
, 3));
633 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
634 if(SUCCEEDED(hres
)) {
635 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
637 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
638 IDispatchEx_Release(dispex
);
642 TRACE("No IDispatchEx\n");
649 str
= jsstr_alloc_len(name
, SysStringLen(name
));
652 return E_OUTOFMEMORY
;
655 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
657 hres
= disp_propput(ctx
->script
, var_obj
, var_id
, jsval_string(str
));
670 /* ECMA-262 3rd Edition 12.10 */
671 static HRESULT
interp_push_scope(exec_ctx_t
*ctx
)
680 hres
= to_object(ctx
->script
, v
, &disp
);
685 hres
= scope_push(ctx
->scope_chain
, to_jsdisp(disp
), disp
, &ctx
->scope_chain
);
686 IDispatch_Release(disp
);
690 /* ECMA-262 3rd Edition 12.10 */
691 static HRESULT
interp_pop_scope(exec_ctx_t
*ctx
)
695 scope_pop(&ctx
->scope_chain
);
699 /* ECMA-262 3rd Edition 12.13 */
700 static HRESULT
interp_case(exec_ctx_t
*ctx
)
702 const unsigned arg
= get_op_uint(ctx
, 0);
710 hres
= equal2_values(stack_top(ctx
), v
, &b
);
724 /* ECMA-262 3rd Edition 12.13 */
725 static HRESULT
interp_throw(exec_ctx_t
*ctx
)
729 jsval_release(ctx
->script
->ei
.val
);
730 ctx
->script
->ei
.val
= stack_pop(ctx
);
731 return DISP_E_EXCEPTION
;
734 static HRESULT
interp_throw_ref(exec_ctx_t
*ctx
)
736 const HRESULT arg
= get_op_uint(ctx
, 0);
738 TRACE("%08x\n", arg
);
740 return throw_reference_error(ctx
->script
, arg
, NULL
);
743 static HRESULT
interp_throw_type(exec_ctx_t
*ctx
)
745 const HRESULT hres
= get_op_uint(ctx
, 0);
746 jsstr_t
*str
= get_op_str(ctx
, 1);
749 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
751 ptr
= jsstr_flatten(str
);
752 return ptr
? throw_type_error(ctx
->script
, hres
, ptr
) : E_OUTOFMEMORY
;
755 /* ECMA-262 3rd Edition 12.14 */
756 static HRESULT
interp_push_except(exec_ctx_t
*ctx
)
758 const unsigned arg1
= get_op_uint(ctx
, 0);
759 const BSTR arg2
= get_op_bstr(ctx
, 1);
760 except_frame_t
*except
;
765 stack_top
= ctx
->top
;
770 hres
= stack_push(ctx
, jsval_bool(TRUE
));
773 hres
= stack_push(ctx
, jsval_bool(TRUE
));
778 except
= heap_alloc(sizeof(*except
));
780 return E_OUTOFMEMORY
;
782 except
->stack_top
= stack_top
;
783 except
->scope
= ctx
->scope_chain
;
784 except
->catch_off
= arg1
;
785 except
->ident
= arg2
;
786 except
->next
= ctx
->except_frame
;
787 ctx
->except_frame
= except
;
791 /* ECMA-262 3rd Edition 12.14 */
792 static HRESULT
interp_pop_except(exec_ctx_t
*ctx
)
794 except_frame_t
*except
;
798 except
= ctx
->except_frame
;
799 assert(except
!= NULL
);
801 ctx
->except_frame
= except
->next
;
806 /* ECMA-262 3rd Edition 12.14 */
807 static HRESULT
interp_end_finally(exec_ctx_t
*ctx
)
817 TRACE("passing exception\n");
819 ctx
->script
->ei
.val
= stack_pop(ctx
);
820 return DISP_E_EXCEPTION
;
827 /* ECMA-262 3rd Edition 13 */
828 static HRESULT
interp_func(exec_ctx_t
*ctx
)
830 unsigned func_idx
= get_op_uint(ctx
, 0);
834 TRACE("%d\n", func_idx
);
836 hres
= create_source_function(ctx
->script
, ctx
->code
, ctx
->func_code
->funcs
+func_idx
,
837 ctx
->scope_chain
, &dispex
);
841 return stack_push(ctx
, jsval_obj(dispex
));
844 /* ECMA-262 3rd Edition 11.2.1 */
845 static HRESULT
interp_array(exec_ctx_t
*ctx
)
856 namev
= stack_pop(ctx
);
858 hres
= stack_pop_object(ctx
, &obj
);
860 jsval_release(namev
);
864 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
865 jsval_release(namev
);
867 IDispatch_Release(obj
);
871 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, 0, &id
);
872 jsstr_release(name_str
);
873 if(SUCCEEDED(hres
)) {
874 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
875 }else if(hres
== DISP_E_UNKNOWNNAME
) {
876 v
= jsval_undefined();
879 IDispatch_Release(obj
);
883 return stack_push(ctx
, v
);
886 /* ECMA-262 3rd Edition 11.2.1 */
887 static HRESULT
interp_member(exec_ctx_t
*ctx
)
889 const BSTR arg
= get_op_bstr(ctx
, 0);
897 hres
= stack_pop_object(ctx
, &obj
);
901 hres
= disp_get_id(ctx
->script
, obj
, arg
, arg
, 0, &id
);
902 if(SUCCEEDED(hres
)) {
903 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
904 }else if(hres
== DISP_E_UNKNOWNNAME
) {
905 v
= jsval_undefined();
908 IDispatch_Release(obj
);
912 return stack_push(ctx
, v
);
915 /* ECMA-262 3rd Edition 11.2.1 */
916 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
918 const unsigned arg
= get_op_uint(ctx
, 0);
928 namev
= stack_pop(ctx
);
929 objv
= stack_pop(ctx
);
931 hres
= to_object(ctx
->script
, objv
, &obj
);
933 if(SUCCEEDED(hres
)) {
934 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
936 IDispatch_Release(obj
);
938 jsval_release(namev
);
942 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, arg
, &id
);
943 jsstr_release(name_str
);
945 IDispatch_Release(obj
);
946 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
948 id
= JS_E_INVALID_PROPERTY
;
950 ERR("failed %08x\n", hres
);
955 return stack_push_objid(ctx
, obj
, id
);
958 /* ECMA-262 3rd Edition 11.2.1 */
959 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
968 disp
= stack_topn_objid(ctx
, 0, &id
);
970 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
972 hres
= disp_propget(ctx
->script
, disp
, id
, &v
);
976 return stack_push(ctx
, v
);
979 /* ECMA-262 3rd Edition 11.2.2 */
980 static HRESULT
interp_new(exec_ctx_t
*ctx
)
982 const unsigned argc
= get_op_uint(ctx
, 0);
988 constr
= stack_topn(ctx
, argc
);
990 /* NOTE: Should use to_object here */
993 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
994 else if(!is_object_instance(constr
))
995 return throw_type_error(ctx
->script
, JS_E_INVALID_ACTION
, NULL
);
996 else if(!get_object(constr
))
997 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
999 hres
= disp_call_value(ctx
->script
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
, argc
, stack_args(ctx
, argc
), &r
);
1003 stack_popn(ctx
, argc
+1);
1004 return stack_push(ctx
, r
);
1007 /* ECMA-262 3rd Edition 11.2.3 */
1008 static HRESULT
interp_call(exec_ctx_t
*ctx
)
1010 const unsigned argn
= get_op_uint(ctx
, 0);
1011 const int do_ret
= get_op_int(ctx
, 1);
1015 TRACE("%d %d\n", argn
, do_ret
);
1017 obj
= stack_topn(ctx
, argn
);
1018 if(!is_object_instance(obj
))
1019 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
1021 hres
= disp_call_value(ctx
->script
, get_object(obj
), NULL
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
),
1022 do_ret
? &r
: NULL
);
1026 stack_popn(ctx
, argn
+1);
1027 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1030 /* ECMA-262 3rd Edition 11.2.3 */
1031 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1033 const unsigned argn
= get_op_uint(ctx
, 0);
1034 const int do_ret
= get_op_int(ctx
, 1);
1040 TRACE("%d %d\n", argn
, do_ret
);
1042 obj
= stack_topn_objid(ctx
, argn
, &id
);
1044 return throw_type_error(ctx
->script
, id
, NULL
);
1046 hres
= disp_call(ctx
->script
, obj
, id
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
), do_ret
? &r
: NULL
);
1050 stack_popn(ctx
, argn
+2);
1051 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1055 /* ECMA-262 3rd Edition 11.1.1 */
1056 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1060 IDispatch_AddRef(ctx
->this_obj
);
1061 return stack_push(ctx
, jsval_disp(ctx
->this_obj
));
1064 /* ECMA-262 3rd Edition 10.1.4 */
1065 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1067 const BSTR arg
= get_op_bstr(ctx
, 0);
1072 TRACE("%s\n", debugstr_w(arg
));
1074 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1078 if(exprval
.type
== EXPRVAL_INVALID
)
1079 return throw_type_error(ctx
->script
, JS_E_UNDEFINED_VARIABLE
, arg
);
1081 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1082 exprval_release(&exprval
);
1086 return stack_push(ctx
, v
);
1089 /* ECMA-262 3rd Edition 10.1.4 */
1090 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1092 const BSTR arg
= get_op_bstr(ctx
, 0);
1093 const unsigned flags
= get_op_uint(ctx
, 1);
1097 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1099 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1103 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1106 hres
= jsdisp_get_id(ctx
->script
->global
, arg
, fdexNameEnsure
, &id
);
1110 exprval_set_idref(&exprval
, to_disp(ctx
->script
->global
), id
);
1113 if(exprval
.type
!= EXPRVAL_IDREF
) {
1114 WARN("invalid ref\n");
1115 exprval_release(&exprval
);
1116 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1119 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1122 /* ECMA-262 3rd Edition 7.8.1 */
1123 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1127 return stack_push(ctx
, jsval_null());
1130 /* ECMA-262 3rd Edition 7.8.2 */
1131 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1133 const int arg
= get_op_int(ctx
, 0);
1135 TRACE("%s\n", arg
? "true" : "false");
1137 return stack_push(ctx
, jsval_bool(arg
));
1140 /* ECMA-262 3rd Edition 7.8.3 */
1141 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1143 const int arg
= get_op_int(ctx
, 0);
1147 return stack_push(ctx
, jsval_number(arg
));
1150 /* ECMA-262 3rd Edition 7.8.3 */
1151 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1153 const double arg
= get_op_double(ctx
);
1155 TRACE("%lf\n", arg
);
1157 return stack_push(ctx
, jsval_number(arg
));
1160 /* ECMA-262 3rd Edition 7.8.4 */
1161 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1163 jsstr_t
*str
= get_op_str(ctx
, 0);
1165 TRACE("%s\n", debugstr_jsstr(str
));
1167 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1170 /* ECMA-262 3rd Edition 7.8 */
1171 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1173 jsstr_t
*source
= get_op_str(ctx
, 0);
1174 const unsigned flags
= get_op_uint(ctx
, 1);
1178 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1180 hres
= create_regexp(ctx
->script
, source
, flags
, ®exp
);
1184 return stack_push(ctx
, jsval_obj(regexp
));
1187 /* ECMA-262 3rd Edition 11.1.4 */
1188 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1190 const unsigned arg
= get_op_uint(ctx
, 0);
1198 hres
= create_array(ctx
->script
, arg
, &array
);
1204 val
= stack_pop(ctx
);
1205 hres
= jsdisp_propput_idx(array
, i
, val
);
1208 jsdisp_release(array
);
1213 return stack_push(ctx
, jsval_obj(array
));
1216 /* ECMA-262 3rd Edition 11.1.5 */
1217 static HRESULT
interp_new_obj(exec_ctx_t
*ctx
)
1224 hres
= create_object(ctx
->script
, NULL
, &obj
);
1228 return stack_push(ctx
, jsval_obj(obj
));
1231 /* ECMA-262 3rd Edition 11.1.5 */
1232 static HRESULT
interp_obj_prop(exec_ctx_t
*ctx
)
1234 const BSTR name
= get_op_bstr(ctx
, 0);
1239 TRACE("%s\n", debugstr_w(name
));
1241 val
= stack_pop(ctx
);
1243 assert(is_object_instance(stack_top(ctx
)));
1244 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1246 hres
= jsdisp_propput_name(obj
, name
, val
);
1251 /* ECMA-262 3rd Edition 11.11 */
1252 static HRESULT
interp_cnd_nz(exec_ctx_t
*ctx
)
1254 const unsigned arg
= get_op_uint(ctx
, 0);
1260 hres
= to_boolean(stack_top(ctx
), &b
);
1273 /* ECMA-262 3rd Edition 11.11 */
1274 static HRESULT
interp_cnd_z(exec_ctx_t
*ctx
)
1276 const unsigned arg
= get_op_uint(ctx
, 0);
1282 hres
= to_boolean(stack_top(ctx
), &b
);
1295 /* ECMA-262 3rd Edition 11.10 */
1296 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1303 hres
= stack_pop_int(ctx
, &r
);
1307 hres
= stack_pop_int(ctx
, &l
);
1311 return stack_push(ctx
, jsval_number(l
|r
));
1314 /* ECMA-262 3rd Edition 11.10 */
1315 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1322 hres
= stack_pop_int(ctx
, &r
);
1326 hres
= stack_pop_int(ctx
, &l
);
1330 return stack_push(ctx
, jsval_number(l
^r
));
1333 /* ECMA-262 3rd Edition 11.10 */
1334 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1341 hres
= stack_pop_int(ctx
, &r
);
1345 hres
= stack_pop_int(ctx
, &l
);
1349 return stack_push(ctx
, jsval_number(l
&r
));
1352 /* ECMA-262 3rd Edition 11.8.6 */
1353 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
1355 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1360 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1363 if(!is_object_instance(v
) || !get_object(v
)) {
1365 return throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1368 obj
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1369 IDispatch_Release(get_object(v
));
1371 FIXME("non-jsdisp objects not supported\n");
1375 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1376 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1378 hres
= throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1380 jsdisp_release(obj
);
1386 if(is_object_instance(prot
)) {
1387 if(is_object_instance(v
))
1388 tmp
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1389 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1390 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1396 jsdisp_release(tmp
);
1398 FIXME("prototype is not an object\n");
1402 jsval_release(prot
);
1407 return stack_push(ctx
, jsval_bool(ret
));
1410 /* ECMA-262 3rd Edition 11.8.7 */
1411 static HRESULT
interp_in(exec_ctx_t
*ctx
)
1422 obj
= stack_pop(ctx
);
1423 if(!is_object_instance(obj
) || !get_object(obj
)) {
1425 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1429 hres
= to_flat_string(ctx
->script
, v
, &jsstr
, &str
);
1432 IDispatch_Release(get_object(obj
));
1436 hres
= disp_get_id(ctx
->script
, get_object(obj
), str
, NULL
, 0, &id
);
1437 IDispatch_Release(get_object(obj
));
1438 jsstr_release(jsstr
);
1441 else if(hres
== DISP_E_UNKNOWNNAME
)
1446 return stack_push(ctx
, jsval_bool(ret
));
1449 /* ECMA-262 3rd Edition 11.6.1 */
1450 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1455 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1459 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1465 if(is_string(l
) || is_string(r
)) {
1466 jsstr_t
*lstr
, *rstr
= NULL
;
1468 hres
= to_string(ctx
, l
, &lstr
);
1470 hres
= to_string(ctx
, r
, &rstr
);
1472 if(SUCCEEDED(hres
)) {
1475 ret_str
= jsstr_concat(lstr
, rstr
);
1477 *ret
= jsval_string(ret_str
);
1479 hres
= E_OUTOFMEMORY
;
1482 jsstr_release(lstr
);
1484 jsstr_release(rstr
);
1488 hres
= to_number(ctx
, l
, &nl
);
1489 if(SUCCEEDED(hres
)) {
1490 hres
= to_number(ctx
, r
, &nr
);
1492 *ret
= jsval_number(nl
+nr
);
1501 /* ECMA-262 3rd Edition 11.6.1 */
1502 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1510 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1512 hres
= add_eval(ctx
->script
, l
, r
, &ret
);
1518 return stack_push(ctx
, ret
);
1521 /* ECMA-262 3rd Edition 11.6.2 */
1522 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1529 hres
= stack_pop_number(ctx
, &r
);
1533 hres
= stack_pop_number(ctx
, &l
);
1537 return stack_push(ctx
, jsval_number(l
-r
));
1540 /* ECMA-262 3rd Edition 11.5.1 */
1541 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1548 hres
= stack_pop_number(ctx
, &r
);
1552 hres
= stack_pop_number(ctx
, &l
);
1556 return stack_push(ctx
, jsval_number(l
*r
));
1559 /* ECMA-262 3rd Edition 11.5.2 */
1560 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1567 hres
= stack_pop_number(ctx
, &r
);
1571 hres
= stack_pop_number(ctx
, &l
);
1575 return stack_push(ctx
, jsval_number(l
/r
));
1578 /* ECMA-262 3rd Edition 11.5.3 */
1579 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1586 hres
= stack_pop_number(ctx
, &r
);
1590 hres
= stack_pop_number(ctx
, &l
);
1594 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1597 /* ECMA-262 3rd Edition 11.4.2 */
1598 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
1600 jsval_t objv
, namev
;
1608 namev
= stack_pop(ctx
);
1609 objv
= stack_pop(ctx
);
1611 hres
= to_object(ctx
->script
, objv
, &obj
);
1612 jsval_release(objv
);
1614 jsval_release(namev
);
1618 hres
= to_string(ctx
->script
, namev
, &name
);
1619 jsval_release(namev
);
1621 IDispatch_Release(obj
);
1625 hres
= disp_delete_name(ctx
->script
, obj
, name
, &ret
);
1626 IDispatch_Release(obj
);
1627 jsstr_release(name
);
1631 return stack_push(ctx
, jsval_bool(ret
));
1634 /* ECMA-262 3rd Edition 11.4.2 */
1635 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
1637 const BSTR arg
= get_op_bstr(ctx
, 0);
1642 TRACE("%s\n", debugstr_w(arg
));
1644 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1648 switch(exprval
.type
) {
1650 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1651 IDispatch_Release(exprval
.u
.idref
.disp
);
1655 case EXPRVAL_INVALID
:
1659 FIXME("Unsupported exprval\n");
1660 exprval_release(&exprval
);
1665 return stack_push(ctx
, jsval_bool(ret
));
1668 /* ECMA-262 3rd Edition 11.4.2 */
1669 static HRESULT
interp_void(exec_ctx_t
*ctx
)
1674 return stack_push(ctx
, jsval_undefined());
1677 /* ECMA-262 3rd Edition 11.4.3 */
1678 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1680 switch(jsval_type(v
)) {
1690 if(get_object(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)get_object(v
)))) {
1691 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1692 jsdisp_release(dispex
);
1708 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1715 /* ECMA-262 3rd Edition 11.4.3 */
1716 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
1726 obj
= stack_pop_objid(ctx
, &id
);
1728 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1730 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1731 IDispatch_Release(obj
);
1733 return stack_push_string(ctx
, unknownW
);
1735 hres
= typeof_string(v
, &ret
);
1740 return stack_push_string(ctx
, ret
);
1743 /* ECMA-262 3rd Edition 11.4.3 */
1744 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
1746 const BSTR arg
= get_op_bstr(ctx
, 0);
1752 TRACE("%s\n", debugstr_w(arg
));
1754 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1758 if(exprval
.type
== EXPRVAL_INVALID
) {
1759 hres
= stack_push(ctx
, jsval_string(jsstr_undefined()));
1760 exprval_release(&exprval
);
1764 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1765 exprval_release(&exprval
);
1769 hres
= typeof_string(v
, &ret
);
1774 return stack_push_string(ctx
, ret
);
1777 /* ECMA-262 3rd Edition 11.4.3 */
1778 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
1787 hres
= typeof_string(v
, &ret
);
1792 return stack_push_string(ctx
, ret
);
1795 /* ECMA-262 3rd Edition 11.4.7 */
1796 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
1803 hres
= stack_pop_number(ctx
, &n
);
1807 return stack_push(ctx
, jsval_number(-n
));
1810 /* ECMA-262 3rd Edition 11.4.6 */
1811 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
1820 hres
= to_number(ctx
->script
, v
, &n
);
1825 return stack_push(ctx
, jsval_number(n
));
1828 /* ECMA-262 3rd Edition 11.3.1 */
1829 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
1831 const int arg
= get_op_int(ctx
, 0);
1839 obj
= stack_pop_objid(ctx
, &id
);
1841 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1843 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1844 if(SUCCEEDED(hres
)) {
1847 hres
= to_number(ctx
->script
, v
, &n
);
1849 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(n
+(double)arg
));
1853 IDispatch_Release(obj
);
1857 return stack_push(ctx
, v
);
1860 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1861 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
1863 const int arg
= get_op_int(ctx
, 0);
1872 obj
= stack_pop_objid(ctx
, &id
);
1874 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1876 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1877 if(SUCCEEDED(hres
)) {
1880 hres
= to_number(ctx
->script
, v
, &n
);
1882 if(SUCCEEDED(hres
)) {
1883 ret
= n
+(double)arg
;
1884 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(ret
));
1887 IDispatch_Release(obj
);
1891 return stack_push(ctx
, jsval_number(ret
));
1894 /* ECMA-262 3rd Edition 11.9.3 */
1895 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
1897 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
1898 return equal2_values(lval
, rval
, ret
);
1900 /* FIXME: NULL disps should be handled in more general way */
1901 if(is_object_instance(lval
) && !get_object(lval
))
1902 return equal_values(ctx
, jsval_null(), rval
, ret
);
1903 if(is_object_instance(rval
) && !get_object(rval
))
1904 return equal_values(ctx
, lval
, jsval_null(), ret
);
1906 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
1911 if(is_string(lval
) && is_number(rval
)) {
1915 hres
= to_number(ctx
, lval
, &n
);
1919 /* FIXME: optimize */
1920 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
1923 if(is_string(rval
) && is_number(lval
)) {
1927 hres
= to_number(ctx
, rval
, &n
);
1931 /* FIXME: optimize */
1932 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
1936 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
1939 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
1942 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
1946 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
1950 hres
= equal_values(ctx
, lval
, prim
, ret
);
1951 jsval_release(prim
);
1956 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
1960 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
1964 hres
= equal_values(ctx
, prim
, rval
, ret
);
1965 jsval_release(prim
);
1974 /* ECMA-262 3rd Edition 11.9.1 */
1975 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
1984 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1986 hres
= equal_values(ctx
->script
, l
, r
, &b
);
1992 return stack_push(ctx
, jsval_bool(b
));
1995 /* ECMA-262 3rd Edition 11.9.2 */
1996 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
2005 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2007 hres
= equal_values(ctx
->script
, l
, r
, &b
);
2013 return stack_push(ctx
, jsval_bool(!b
));
2016 /* ECMA-262 3rd Edition 11.9.4 */
2017 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2026 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2028 hres
= equal2_values(r
, l
, &b
);
2034 return stack_push(ctx
, jsval_bool(b
));
2037 /* ECMA-262 3rd Edition 11.9.5 */
2038 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2049 hres
= equal2_values(r
, l
, &b
);
2055 return stack_push(ctx
, jsval_bool(!b
));
2058 /* ECMA-262 3rd Edition 11.8.5 */
2059 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2065 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2069 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2075 if(is_string(l
) && is_string(r
)) {
2076 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2077 jsstr_release(get_string(l
));
2078 jsstr_release(get_string(r
));
2082 hres
= to_number(ctx
, l
, &ln
);
2085 hres
= to_number(ctx
, r
, &rn
);
2090 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2094 /* ECMA-262 3rd Edition 11.8.1 */
2095 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2104 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2106 hres
= less_eval(ctx
->script
, l
, r
, FALSE
, &b
);
2112 return stack_push(ctx
, jsval_bool(b
));
2115 /* ECMA-262 3rd Edition 11.8.1 */
2116 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2125 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2127 hres
= less_eval(ctx
->script
, r
, l
, TRUE
, &b
);
2133 return stack_push(ctx
, jsval_bool(b
));
2136 /* ECMA-262 3rd Edition 11.8.2 */
2137 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2146 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2148 hres
= less_eval(ctx
->script
, r
, l
, FALSE
, &b
);
2154 return stack_push(ctx
, jsval_bool(b
));
2157 /* ECMA-262 3rd Edition 11.8.4 */
2158 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2167 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2169 hres
= less_eval(ctx
->script
, l
, r
, TRUE
, &b
);
2175 return stack_push(ctx
, jsval_bool(b
));
2178 /* ECMA-262 3rd Edition 11.4.8 */
2179 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
2188 hres
= to_int32(ctx
->script
, v
, &i
);
2193 return stack_push(ctx
, jsval_number(~i
));
2196 /* ECMA-262 3rd Edition 11.4.9 */
2197 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2206 hres
= to_boolean(v
, &b
);
2211 return stack_push(ctx
, jsval_bool(!b
));
2214 /* ECMA-262 3rd Edition 11.7.1 */
2215 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
2221 hres
= stack_pop_uint(ctx
, &r
);
2225 hres
= stack_pop_int(ctx
, &l
);
2229 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2232 /* ECMA-262 3rd Edition 11.7.2 */
2233 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
2239 hres
= stack_pop_uint(ctx
, &r
);
2243 hres
= stack_pop_int(ctx
, &l
);
2247 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2250 /* ECMA-262 3rd Edition 11.7.3 */
2251 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
2256 hres
= stack_pop_uint(ctx
, &r
);
2260 hres
= stack_pop_uint(ctx
, &l
);
2264 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2267 /* ECMA-262 3rd Edition 11.13.1 */
2268 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
2279 disp
= stack_pop_objid(ctx
, &id
);
2282 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2285 hres
= disp_propput(ctx
->script
, disp
, id
, v
);
2286 IDispatch_Release(disp
);
2292 return stack_push(ctx
, v
);
2295 /* JScript extension */
2296 static HRESULT
interp_assign_call(exec_ctx_t
*ctx
)
2298 const unsigned argc
= get_op_uint(ctx
, 0);
2304 TRACE("%u\n", argc
);
2306 disp
= stack_topn_objid(ctx
, argc
+1, &id
);
2308 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2310 hres
= disp_call(ctx
->script
, disp
, id
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2315 stack_popn(ctx
, argc
+2);
2316 return stack_push(ctx
, v
);
2319 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
2323 return stack_push(ctx
, jsval_undefined());
2326 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
2328 const unsigned arg
= get_op_uint(ctx
, 0);
2336 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
2338 const unsigned arg
= get_op_uint(ctx
, 0);
2346 hres
= to_boolean(v
, &b
);
2358 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
2360 const unsigned arg
= get_op_uint(ctx
, 0);
2364 stack_popn(ctx
, arg
);
2368 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
2376 static HRESULT
interp_setret(exec_ctx_t
*ctx
)
2380 jsval_release(ctx
->ret
);
2381 ctx
->ret
= stack_pop(ctx
);
2385 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
2387 static const op_func_t op_funcs
[] = {
2388 #define X(x,a,b,c) interp_##x,
2393 static const unsigned op_move
[] = {
2394 #define X(a,x,b,c) x,
2399 static HRESULT
unwind_exception(exec_ctx_t
*ctx
)
2401 except_frame_t
*except_frame
;
2406 except_frame
= ctx
->except_frame
;
2407 ctx
->except_frame
= except_frame
->next
;
2409 assert(except_frame
->stack_top
<= ctx
->top
);
2410 stack_popn(ctx
, ctx
->top
- except_frame
->stack_top
);
2412 while(except_frame
->scope
!= ctx
->scope_chain
)
2413 scope_pop(&ctx
->scope_chain
);
2415 ctx
->ip
= except_frame
->catch_off
;
2417 except_val
= ctx
->script
->ei
.val
;
2418 ctx
->script
->ei
.val
= jsval_undefined();
2419 clear_ei(ctx
->script
);
2421 ident
= except_frame
->ident
;
2422 heap_free(except_frame
);
2425 jsdisp_t
*scope_obj
;
2427 hres
= create_dispex(ctx
->script
, NULL
, NULL
, &scope_obj
);
2428 if(SUCCEEDED(hres
)) {
2429 hres
= jsdisp_propput_name(scope_obj
, ident
, except_val
);
2431 jsdisp_release(scope_obj
);
2433 jsval_release(except_val
);
2437 hres
= scope_push(ctx
->scope_chain
, scope_obj
, to_disp(scope_obj
), &ctx
->scope_chain
);
2438 jsdisp_release(scope_obj
);
2440 hres
= stack_push(ctx
, except_val
);
2444 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2450 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, jsval_t
*ret
)
2452 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
2453 except_frame_t
*prev_except_frame
;
2454 function_code_t
*prev_func
;
2455 unsigned prev_ip
, prev_top
;
2456 scope_chain_t
*prev_scope
;
2457 bytecode_t
*prev_code
;
2459 HRESULT hres
= S_OK
;
2463 prev_top
= exec_ctx
->top
;
2464 prev_scope
= exec_ctx
->scope_chain
;
2465 prev_except_frame
= exec_ctx
->except_frame
;
2466 prev_ip
= exec_ctx
->ip
;
2467 prev_code
= exec_ctx
->code
;
2468 prev_func
= exec_ctx
->func_code
;
2469 exec_ctx
->ip
= func
->instr_off
;
2470 exec_ctx
->except_frame
= NULL
;
2471 exec_ctx
->code
= code
;
2472 exec_ctx
->func_code
= func
;
2474 while(exec_ctx
->ip
!= -1) {
2475 op
= code
->instrs
[exec_ctx
->ip
].op
;
2476 hres
= op_funcs
[op
](exec_ctx
);
2478 TRACE("EXCEPTION %08x\n", hres
);
2480 if(!exec_ctx
->except_frame
)
2483 hres
= unwind_exception(exec_ctx
);
2487 exec_ctx
->ip
+= op_move
[op
];
2491 exec_ctx
->ip
= prev_ip
;
2492 exec_ctx
->except_frame
= prev_except_frame
;
2493 exec_ctx
->code
= prev_code
;
2494 exec_ctx
->func_code
= prev_func
;
2497 while(exec_ctx
->scope_chain
!= prev_scope
)
2498 scope_pop(&exec_ctx
->scope_chain
);
2499 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
2503 assert(exec_ctx
->top
== prev_top
+1 || exec_ctx
->top
== prev_top
);
2504 assert(exec_ctx
->scope_chain
== prev_scope
);
2505 assert(exec_ctx
->top
== prev_top
);
2507 *ret
= exec_ctx
->ret
;
2508 exec_ctx
->ret
= jsval_undefined();
2512 HRESULT
exec_source(exec_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, BOOL from_eval
, jsval_t
*ret
)
2514 exec_ctx_t
*prev_ctx
;
2517 HRESULT hres
= S_OK
;
2519 for(i
= 0; i
< func
->func_cnt
; i
++) {
2522 if(!func
->funcs
[i
].name
)
2525 hres
= create_source_function(ctx
->script
, code
, func
->funcs
+i
, ctx
->scope_chain
, &func_obj
);
2529 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->funcs
[i
].name
, jsval_obj(func_obj
));
2530 jsdisp_release(func_obj
);
2535 for(i
=0; i
< func
->var_cnt
; i
++) {
2536 if(!ctx
->is_global
|| !lookup_global_members(ctx
->script
, func
->variables
[i
], NULL
)) {
2539 hres
= jsdisp_get_id(ctx
->var_disp
, func
->variables
[i
], fdexNameEnsure
, &id
);
2545 prev_ctx
= ctx
->script
->exec_ctx
;
2546 ctx
->script
->exec_ctx
= ctx
;
2548 hres
= enter_bytecode(ctx
->script
, code
, func
, &val
);
2549 assert(ctx
->script
->exec_ctx
== ctx
);
2550 ctx
->script
->exec_ctx
= prev_ctx
;