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(script_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
->stack_top
) {
74 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*new_stack
));
80 ctx
->stack
= new_stack
;
84 ctx
->stack
[ctx
->stack_top
++] = v
;
88 static inline HRESULT
stack_push_string(script_ctx_t
*ctx
, const WCHAR
*str
)
96 return stack_push(ctx
, jsval_string(v
));
99 static HRESULT
stack_push_objid(script_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(script_ctx_t
*ctx
)
112 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
113 return ctx
->stack
[ctx
->stack_top
-1];
116 static inline jsval_t
stack_topn(script_ctx_t
*ctx
, unsigned n
)
118 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
);
119 return ctx
->stack
[ctx
->stack_top
-1-n
];
122 static inline jsval_t
*stack_args(script_ctx_t
*ctx
, unsigned n
)
126 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
-1);
127 return ctx
->stack
+ ctx
->stack_top
-n
;
130 static inline jsval_t
stack_pop(script_ctx_t
*ctx
)
132 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
133 return ctx
->stack
[--ctx
->stack_top
];
136 static void stack_popn(script_ctx_t
*ctx
, unsigned n
)
139 jsval_release(stack_pop(ctx
));
142 static HRESULT
stack_pop_number(script_ctx_t
*ctx
, double *r
)
148 hres
= to_number(ctx
, v
, r
);
153 static HRESULT
stack_pop_object(script_ctx_t
*ctx
, IDispatch
**r
)
159 if(is_object_instance(v
)) {
161 return throw_type_error(ctx
, JS_E_OBJECT_REQUIRED
, NULL
);
166 hres
= to_object(ctx
, v
, r
);
171 static inline HRESULT
stack_pop_int(script_ctx_t
*ctx
, INT
*r
)
173 return to_int32(ctx
, stack_pop(ctx
), r
);
176 static inline HRESULT
stack_pop_uint(script_ctx_t
*ctx
, DWORD
*r
)
178 return to_uint32(ctx
, stack_pop(ctx
), r
);
181 static inline IDispatch
*stack_pop_objid(script_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(script_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 inline jsval_t
steal_ret(call_frame_t
*frame
)
199 jsval_t r
= frame
->ret
;
200 frame
->ret
= jsval_undefined();
204 static inline void clear_ret(call_frame_t
*frame
)
206 jsval_release(steal_ret(frame
));
209 static void exprval_release(exprval_t
*val
)
213 jsval_release(val
->u
.val
);
216 if(val
->u
.idref
.disp
)
217 IDispatch_Release(val
->u
.idref
.disp
);
219 case EXPRVAL_INVALID
:
224 /* ECMA-262 3rd Edition 8.7.1 */
225 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsval_t
*ret
)
230 val
->u
.val
= jsval_undefined();
233 if(!val
->u
.idref
.disp
) {
234 FIXME("throw ReferenceError\n");
238 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
);
239 case EXPRVAL_INVALID
:
243 ERR("type %d\n", val
->type
);
247 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
249 val
->type
= EXPRVAL_IDREF
;
250 val
->u
.idref
.disp
= disp
;
251 val
->u
.idref
.id
= id
;
254 IDispatch_AddRef(disp
);
257 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
259 scope_chain_t
*new_scope
;
261 new_scope
= heap_alloc(sizeof(scope_chain_t
));
263 return E_OUTOFMEMORY
;
267 IDispatch_AddRef(obj
);
268 new_scope
->jsobj
= jsobj
;
269 new_scope
->obj
= obj
;
273 new_scope
->next
= scope
;
275 new_scope
->next
= NULL
;
282 static void scope_pop(scope_chain_t
**scope
)
291 void clear_ei(script_ctx_t
*ctx
)
293 memset(&ctx
->ei
.ei
, 0, sizeof(ctx
->ei
.ei
));
294 jsval_release(ctx
->ei
.val
);
295 ctx
->ei
.val
= jsval_undefined();
298 void scope_release(scope_chain_t
*scope
)
304 scope_release(scope
->next
);
306 IDispatch_Release(scope
->obj
);
310 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
317 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
319 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
320 jsdisp_release(jsdisp
);
327 bstr
= SysAllocString(name
);
329 return E_OUTOFMEMORY
;
333 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
334 if(SUCCEEDED(hres
)) {
335 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
336 IDispatchEx_Release(dispex
);
338 TRACE("using IDispatch\n");
339 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
342 if(name_bstr
!= bstr
)
347 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
349 IObjectIdentity
*identity
;
350 IUnknown
*unk1
, *unk2
;
358 if(!disp1
|| !disp2
) {
363 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
367 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
369 IUnknown_Release(unk1
);
376 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
377 if(SUCCEEDED(hres
)) {
378 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
379 IObjectIdentity_Release(identity
);
386 IUnknown_Release(unk1
);
387 IUnknown_Release(unk2
);
391 /* ECMA-262 3rd Edition 11.9.6 */
392 static HRESULT
equal2_values(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
394 jsval_type_t type
= jsval_type(lval
);
398 if(type
!= jsval_type(rval
)) {
399 if(is_null_instance(lval
))
400 *ret
= is_null_instance(rval
);
412 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
414 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
417 *ret
= get_number(lval
) == get_number(rval
);
420 *ret
= !get_bool(lval
) == !get_bool(rval
);
423 FIXME("VARIANT not implemented\n");
430 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
436 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
437 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
438 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
439 if(SUCCEEDED(hres
)) {
441 exprval_set_idref(ret
, item
->disp
, id
);
450 /* ECMA-262 3rd Edition 10.1.4 */
451 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
453 scope_chain_t
*scope
;
458 TRACE("%s\n", debugstr_w(identifier
));
461 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
463 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
465 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
466 if(SUCCEEDED(hres
)) {
467 exprval_set_idref(ret
, scope
->obj
, id
);
473 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
474 if(SUCCEEDED(hres
)) {
475 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
479 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
480 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
487 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
488 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
490 WARN("GetItemInfo failed: %08x\n", hres
);
494 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
495 IUnknown_Release(unk
);
497 WARN("object does not implement IDispatch\n");
502 IDispatch_AddRef(item
->disp
);
503 ret
->type
= EXPRVAL_JSVAL
;
504 ret
->u
.val
= jsval_disp(item
->disp
);
509 if(lookup_global_members(ctx
, identifier
, ret
))
512 ret
->type
= EXPRVAL_INVALID
;
516 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
518 call_frame_t
*frame
= ctx
->call_ctx
;
519 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
522 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
524 call_frame_t
*frame
= ctx
->call_ctx
;
525 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
528 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
530 call_frame_t
*frame
= ctx
->call_ctx
;
531 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
534 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
536 call_frame_t
*frame
= ctx
->call_ctx
;
537 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
540 static inline double get_op_double(script_ctx_t
*ctx
)
542 call_frame_t
*frame
= ctx
->call_ctx
;
543 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
546 static inline void jmp_next(script_ctx_t
*ctx
)
551 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
553 ctx
->call_ctx
->ip
= dst
;
556 /* ECMA-262 3rd Edition 12.2 */
557 static HRESULT
interp_var_set(script_ctx_t
*ctx
)
559 const BSTR name
= get_op_bstr(ctx
, 0);
563 TRACE("%s\n", debugstr_w(name
));
565 val
= stack_pop(ctx
);
566 hres
= jsdisp_propput_name(ctx
->call_ctx
->variable_obj
, name
, val
);
571 /* ECMA-262 3rd Edition 12.6.4 */
572 static HRESULT
interp_forin(script_ctx_t
*ctx
)
574 const HRESULT arg
= get_op_uint(ctx
, 0);
575 IDispatch
*var_obj
, *obj
= NULL
;
583 assert(is_number(stack_top(ctx
)));
584 id
= get_number(stack_top(ctx
));
586 var_obj
= stack_topn_objid(ctx
, 1, &var_id
);
588 FIXME("invalid ref\n");
592 if(is_object_instance(stack_topn(ctx
, 3)))
593 obj
= get_object(stack_topn(ctx
, 3));
596 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
597 if(SUCCEEDED(hres
)) {
598 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
600 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
601 IDispatchEx_Release(dispex
);
605 TRACE("No IDispatchEx\n");
612 str
= jsstr_alloc_len(name
, SysStringLen(name
));
615 return E_OUTOFMEMORY
;
618 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
620 hres
= disp_propput(ctx
, var_obj
, var_id
, jsval_string(str
));
633 /* ECMA-262 3rd Edition 12.10 */
634 static HRESULT
interp_push_scope(script_ctx_t
*ctx
)
643 hres
= to_object(ctx
, v
, &disp
);
648 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
649 IDispatch_Release(disp
);
653 /* ECMA-262 3rd Edition 12.10 */
654 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
658 scope_pop(&ctx
->call_ctx
->scope
);
662 /* ECMA-262 3rd Edition 12.13 */
663 static HRESULT
interp_case(script_ctx_t
*ctx
)
665 const unsigned arg
= get_op_uint(ctx
, 0);
673 hres
= equal2_values(stack_top(ctx
), v
, &b
);
687 /* ECMA-262 3rd Edition 12.13 */
688 static HRESULT
interp_throw(script_ctx_t
*ctx
)
692 jsval_release(ctx
->ei
.val
);
693 ctx
->ei
.val
= stack_pop(ctx
);
694 return DISP_E_EXCEPTION
;
697 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
699 const HRESULT arg
= get_op_uint(ctx
, 0);
701 TRACE("%08x\n", arg
);
703 return throw_reference_error(ctx
, arg
, NULL
);
706 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
708 const HRESULT hres
= get_op_uint(ctx
, 0);
709 jsstr_t
*str
= get_op_str(ctx
, 1);
712 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
714 ptr
= jsstr_flatten(str
);
715 return ptr
? throw_type_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
718 /* ECMA-262 3rd Edition 12.14 */
719 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
721 const unsigned arg1
= get_op_uint(ctx
, 0);
722 const BSTR arg2
= get_op_bstr(ctx
, 1);
723 call_frame_t
*frame
= ctx
->call_ctx
;
724 except_frame_t
*except
;
729 stack_top
= ctx
->stack_top
;
734 hres
= stack_push(ctx
, jsval_bool(TRUE
));
737 hres
= stack_push(ctx
, jsval_bool(TRUE
));
742 except
= heap_alloc(sizeof(*except
));
744 return E_OUTOFMEMORY
;
746 except
->stack_top
= stack_top
;
747 except
->scope
= frame
->scope
;
748 except
->catch_off
= arg1
;
749 except
->ident
= arg2
;
750 except
->next
= frame
->except_frame
;
751 frame
->except_frame
= except
;
755 /* ECMA-262 3rd Edition 12.14 */
756 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
758 call_frame_t
*frame
= ctx
->call_ctx
;
759 except_frame_t
*except
;
763 except
= frame
->except_frame
;
764 assert(except
!= NULL
);
766 frame
->except_frame
= except
->next
;
771 /* ECMA-262 3rd Edition 12.14 */
772 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
782 TRACE("passing exception\n");
784 ctx
->ei
.val
= stack_pop(ctx
);
785 return DISP_E_EXCEPTION
;
792 /* ECMA-262 3rd Edition 13 */
793 static HRESULT
interp_func(script_ctx_t
*ctx
)
795 unsigned func_idx
= get_op_uint(ctx
, 0);
796 call_frame_t
*frame
= ctx
->call_ctx
;
800 TRACE("%d\n", func_idx
);
802 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
803 frame
->scope
, &dispex
);
807 return stack_push(ctx
, jsval_obj(dispex
));
810 /* ECMA-262 3rd Edition 11.2.1 */
811 static HRESULT
interp_array(script_ctx_t
*ctx
)
822 namev
= stack_pop(ctx
);
824 hres
= stack_pop_object(ctx
, &obj
);
826 jsval_release(namev
);
830 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
831 jsval_release(namev
);
833 IDispatch_Release(obj
);
837 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
838 jsstr_release(name_str
);
839 if(SUCCEEDED(hres
)) {
840 hres
= disp_propget(ctx
, obj
, id
, &v
);
841 }else if(hres
== DISP_E_UNKNOWNNAME
) {
842 v
= jsval_undefined();
845 IDispatch_Release(obj
);
849 return stack_push(ctx
, v
);
852 /* ECMA-262 3rd Edition 11.2.1 */
853 static HRESULT
interp_member(script_ctx_t
*ctx
)
855 const BSTR arg
= get_op_bstr(ctx
, 0);
863 hres
= stack_pop_object(ctx
, &obj
);
867 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
868 if(SUCCEEDED(hres
)) {
869 hres
= disp_propget(ctx
, obj
, id
, &v
);
870 }else if(hres
== DISP_E_UNKNOWNNAME
) {
871 v
= jsval_undefined();
874 IDispatch_Release(obj
);
878 return stack_push(ctx
, v
);
881 /* ECMA-262 3rd Edition 11.2.1 */
882 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
884 const unsigned arg
= get_op_uint(ctx
, 0);
894 namev
= stack_pop(ctx
);
895 objv
= stack_pop(ctx
);
897 hres
= to_object(ctx
, objv
, &obj
);
899 if(SUCCEEDED(hres
)) {
900 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
902 IDispatch_Release(obj
);
904 jsval_release(namev
);
908 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
909 jsstr_release(name_str
);
911 IDispatch_Release(obj
);
912 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
914 id
= JS_E_INVALID_PROPERTY
;
916 ERR("failed %08x\n", hres
);
921 return stack_push_objid(ctx
, obj
, id
);
924 /* ECMA-262 3rd Edition 11.2.1 */
925 static HRESULT
interp_refval(script_ctx_t
*ctx
)
934 disp
= stack_topn_objid(ctx
, 0, &id
);
936 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
938 hres
= disp_propget(ctx
, disp
, id
, &v
);
942 return stack_push(ctx
, v
);
945 /* ECMA-262 3rd Edition 11.2.2 */
946 static HRESULT
interp_new(script_ctx_t
*ctx
)
948 const unsigned argc
= get_op_uint(ctx
, 0);
949 call_frame_t
*frame
= ctx
->call_ctx
;
954 constr
= stack_topn(ctx
, argc
);
956 /* NOTE: Should use to_object here */
959 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
960 else if(!is_object_instance(constr
))
961 return throw_type_error(ctx
, JS_E_INVALID_ACTION
, NULL
);
962 else if(!get_object(constr
))
963 return throw_type_error(ctx
, JS_E_INVALID_PROPERTY
, NULL
);
966 return disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
967 argc
, stack_args(ctx
, argc
), &frame
->ret
);
970 /* ECMA-262 3rd Edition 11.2.3 */
971 static HRESULT
interp_call(script_ctx_t
*ctx
)
973 const unsigned argn
= get_op_uint(ctx
, 0);
974 const int do_ret
= get_op_int(ctx
, 1);
975 call_frame_t
*frame
= ctx
->call_ctx
;
978 TRACE("%d %d\n", argn
, do_ret
);
980 obj
= stack_topn(ctx
, argn
);
981 if(!is_object_instance(obj
))
982 return throw_type_error(ctx
, JS_E_INVALID_PROPERTY
, NULL
);
985 return disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
986 argn
, stack_args(ctx
, argn
), do_ret
? &frame
->ret
: NULL
);
989 /* ECMA-262 3rd Edition 11.2.3 */
990 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
992 const unsigned argn
= get_op_uint(ctx
, 0);
993 const int do_ret
= get_op_int(ctx
, 1);
994 call_frame_t
*frame
= ctx
->call_ctx
;
998 TRACE("%d %d\n", argn
, do_ret
);
1000 obj
= stack_topn_objid(ctx
, argn
, &id
);
1002 return throw_type_error(ctx
, id
, NULL
);
1005 return disp_call(ctx
, obj
, id
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1006 argn
, stack_args(ctx
, argn
), do_ret
? &frame
->ret
: NULL
);
1009 /* ECMA-262 3rd Edition 11.1.1 */
1010 static HRESULT
interp_this(script_ctx_t
*ctx
)
1012 call_frame_t
*frame
= ctx
->call_ctx
;
1016 IDispatch_AddRef(frame
->this_obj
);
1017 return stack_push(ctx
, jsval_disp(frame
->this_obj
));
1020 /* ECMA-262 3rd Edition 10.1.4 */
1021 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1023 const BSTR arg
= get_op_bstr(ctx
, 0);
1028 TRACE("%s\n", debugstr_w(arg
));
1030 hres
= identifier_eval(ctx
, arg
, &exprval
);
1034 if(exprval
.type
== EXPRVAL_INVALID
)
1035 return throw_type_error(ctx
, JS_E_UNDEFINED_VARIABLE
, arg
);
1037 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1038 exprval_release(&exprval
);
1042 return stack_push(ctx
, v
);
1045 /* ECMA-262 3rd Edition 10.1.4 */
1046 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1048 const BSTR arg
= get_op_bstr(ctx
, 0);
1049 const unsigned flags
= get_op_uint(ctx
, 1);
1053 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1055 hres
= identifier_eval(ctx
, arg
, &exprval
);
1059 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1062 hres
= jsdisp_get_id(ctx
->global
, arg
, fdexNameEnsure
, &id
);
1066 exprval_set_idref(&exprval
, to_disp(ctx
->global
), id
);
1069 if(exprval
.type
!= EXPRVAL_IDREF
) {
1070 WARN("invalid ref\n");
1071 exprval_release(&exprval
);
1072 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1075 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1078 /* ECMA-262 3rd Edition 7.8.1 */
1079 static HRESULT
interp_null(script_ctx_t
*ctx
)
1083 return stack_push(ctx
, jsval_null());
1086 /* ECMA-262 3rd Edition 7.8.2 */
1087 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1089 const int arg
= get_op_int(ctx
, 0);
1091 TRACE("%s\n", arg
? "true" : "false");
1093 return stack_push(ctx
, jsval_bool(arg
));
1096 /* ECMA-262 3rd Edition 7.8.3 */
1097 static HRESULT
interp_int(script_ctx_t
*ctx
)
1099 const int arg
= get_op_int(ctx
, 0);
1103 return stack_push(ctx
, jsval_number(arg
));
1106 /* ECMA-262 3rd Edition 7.8.3 */
1107 static HRESULT
interp_double(script_ctx_t
*ctx
)
1109 const double arg
= get_op_double(ctx
);
1111 TRACE("%lf\n", arg
);
1113 return stack_push(ctx
, jsval_number(arg
));
1116 /* ECMA-262 3rd Edition 7.8.4 */
1117 static HRESULT
interp_str(script_ctx_t
*ctx
)
1119 jsstr_t
*str
= get_op_str(ctx
, 0);
1121 TRACE("%s\n", debugstr_jsstr(str
));
1123 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1126 /* ECMA-262 3rd Edition 7.8 */
1127 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1129 jsstr_t
*source
= get_op_str(ctx
, 0);
1130 const unsigned flags
= get_op_uint(ctx
, 1);
1134 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1136 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1140 return stack_push(ctx
, jsval_obj(regexp
));
1143 /* ECMA-262 3rd Edition 11.1.4 */
1144 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1146 const unsigned arg
= get_op_uint(ctx
, 0);
1154 hres
= create_array(ctx
, arg
, &array
);
1160 val
= stack_pop(ctx
);
1161 hres
= jsdisp_propput_idx(array
, i
, val
);
1164 jsdisp_release(array
);
1169 return stack_push(ctx
, jsval_obj(array
));
1172 /* ECMA-262 3rd Edition 11.1.5 */
1173 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1180 hres
= create_object(ctx
, NULL
, &obj
);
1184 return stack_push(ctx
, jsval_obj(obj
));
1187 /* ECMA-262 3rd Edition 11.1.5 */
1188 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1190 const BSTR name
= get_op_bstr(ctx
, 0);
1195 TRACE("%s\n", debugstr_w(name
));
1197 val
= stack_pop(ctx
);
1199 assert(is_object_instance(stack_top(ctx
)));
1200 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1202 hres
= jsdisp_propput_name(obj
, name
, val
);
1207 /* ECMA-262 3rd Edition 11.11 */
1208 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1210 const unsigned arg
= get_op_uint(ctx
, 0);
1216 hres
= to_boolean(stack_top(ctx
), &b
);
1229 /* ECMA-262 3rd Edition 11.11 */
1230 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1232 const unsigned arg
= get_op_uint(ctx
, 0);
1238 hres
= to_boolean(stack_top(ctx
), &b
);
1251 /* ECMA-262 3rd Edition 11.10 */
1252 static HRESULT
interp_or(script_ctx_t
*ctx
)
1259 hres
= stack_pop_int(ctx
, &r
);
1263 hres
= stack_pop_int(ctx
, &l
);
1267 return stack_push(ctx
, jsval_number(l
|r
));
1270 /* ECMA-262 3rd Edition 11.10 */
1271 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1278 hres
= stack_pop_int(ctx
, &r
);
1282 hres
= stack_pop_int(ctx
, &l
);
1286 return stack_push(ctx
, jsval_number(l
^r
));
1289 /* ECMA-262 3rd Edition 11.10 */
1290 static HRESULT
interp_and(script_ctx_t
*ctx
)
1297 hres
= stack_pop_int(ctx
, &r
);
1301 hres
= stack_pop_int(ctx
, &l
);
1305 return stack_push(ctx
, jsval_number(l
&r
));
1308 /* ECMA-262 3rd Edition 11.8.6 */
1309 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1311 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1316 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1319 if(!is_object_instance(v
) || !get_object(v
)) {
1321 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1324 obj
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1325 IDispatch_Release(get_object(v
));
1327 FIXME("non-jsdisp objects not supported\n");
1331 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1332 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1334 hres
= throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1336 jsdisp_release(obj
);
1342 if(is_object_instance(prot
)) {
1343 if(is_object_instance(v
))
1344 tmp
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1345 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1346 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1352 jsdisp_release(tmp
);
1354 FIXME("prototype is not an object\n");
1358 jsval_release(prot
);
1363 return stack_push(ctx
, jsval_bool(ret
));
1366 /* ECMA-262 3rd Edition 11.8.7 */
1367 static HRESULT
interp_in(script_ctx_t
*ctx
)
1378 obj
= stack_pop(ctx
);
1379 if(!is_object_instance(obj
) || !get_object(obj
)) {
1381 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1385 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1388 IDispatch_Release(get_object(obj
));
1392 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1393 IDispatch_Release(get_object(obj
));
1394 jsstr_release(jsstr
);
1397 else if(hres
== DISP_E_UNKNOWNNAME
)
1402 return stack_push(ctx
, jsval_bool(ret
));
1405 /* ECMA-262 3rd Edition 11.6.1 */
1406 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1411 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1415 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1421 if(is_string(l
) || is_string(r
)) {
1422 jsstr_t
*lstr
, *rstr
= NULL
;
1424 hres
= to_string(ctx
, l
, &lstr
);
1426 hres
= to_string(ctx
, r
, &rstr
);
1428 if(SUCCEEDED(hres
)) {
1431 ret_str
= jsstr_concat(lstr
, rstr
);
1433 *ret
= jsval_string(ret_str
);
1435 hres
= E_OUTOFMEMORY
;
1438 jsstr_release(lstr
);
1440 jsstr_release(rstr
);
1444 hres
= to_number(ctx
, l
, &nl
);
1445 if(SUCCEEDED(hres
)) {
1446 hres
= to_number(ctx
, r
, &nr
);
1448 *ret
= jsval_number(nl
+nr
);
1457 /* ECMA-262 3rd Edition 11.6.1 */
1458 static HRESULT
interp_add(script_ctx_t
*ctx
)
1466 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1468 hres
= add_eval(ctx
, l
, r
, &ret
);
1474 return stack_push(ctx
, ret
);
1477 /* ECMA-262 3rd Edition 11.6.2 */
1478 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1485 hres
= stack_pop_number(ctx
, &r
);
1489 hres
= stack_pop_number(ctx
, &l
);
1493 return stack_push(ctx
, jsval_number(l
-r
));
1496 /* ECMA-262 3rd Edition 11.5.1 */
1497 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1504 hres
= stack_pop_number(ctx
, &r
);
1508 hres
= stack_pop_number(ctx
, &l
);
1512 return stack_push(ctx
, jsval_number(l
*r
));
1515 /* ECMA-262 3rd Edition 11.5.2 */
1516 static HRESULT
interp_div(script_ctx_t
*ctx
)
1523 hres
= stack_pop_number(ctx
, &r
);
1527 hres
= stack_pop_number(ctx
, &l
);
1531 return stack_push(ctx
, jsval_number(l
/r
));
1534 /* ECMA-262 3rd Edition 11.5.3 */
1535 static HRESULT
interp_mod(script_ctx_t
*ctx
)
1542 hres
= stack_pop_number(ctx
, &r
);
1546 hres
= stack_pop_number(ctx
, &l
);
1550 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1553 /* ECMA-262 3rd Edition 11.4.2 */
1554 static HRESULT
interp_delete(script_ctx_t
*ctx
)
1556 jsval_t objv
, namev
;
1564 namev
= stack_pop(ctx
);
1565 objv
= stack_pop(ctx
);
1567 hres
= to_object(ctx
, objv
, &obj
);
1568 jsval_release(objv
);
1570 jsval_release(namev
);
1574 hres
= to_string(ctx
, namev
, &name
);
1575 jsval_release(namev
);
1577 IDispatch_Release(obj
);
1581 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
1582 IDispatch_Release(obj
);
1583 jsstr_release(name
);
1587 return stack_push(ctx
, jsval_bool(ret
));
1590 /* ECMA-262 3rd Edition 11.4.2 */
1591 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
1593 const BSTR arg
= get_op_bstr(ctx
, 0);
1598 TRACE("%s\n", debugstr_w(arg
));
1600 hres
= identifier_eval(ctx
, arg
, &exprval
);
1604 switch(exprval
.type
) {
1606 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1607 IDispatch_Release(exprval
.u
.idref
.disp
);
1611 case EXPRVAL_INVALID
:
1615 FIXME("Unsupported exprval\n");
1616 exprval_release(&exprval
);
1621 return stack_push(ctx
, jsval_bool(ret
));
1624 /* ECMA-262 3rd Edition 11.4.2 */
1625 static HRESULT
interp_void(script_ctx_t
*ctx
)
1630 return stack_push(ctx
, jsval_undefined());
1633 /* ECMA-262 3rd Edition 11.4.3 */
1634 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1636 switch(jsval_type(v
)) {
1646 if(get_object(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)get_object(v
)))) {
1647 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1648 jsdisp_release(dispex
);
1664 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1671 /* ECMA-262 3rd Edition 11.4.3 */
1672 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
1682 obj
= stack_pop_objid(ctx
, &id
);
1684 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1686 hres
= disp_propget(ctx
, obj
, id
, &v
);
1687 IDispatch_Release(obj
);
1689 return stack_push_string(ctx
, unknownW
);
1691 hres
= typeof_string(v
, &ret
);
1696 return stack_push_string(ctx
, ret
);
1699 /* ECMA-262 3rd Edition 11.4.3 */
1700 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
1702 const BSTR arg
= get_op_bstr(ctx
, 0);
1708 TRACE("%s\n", debugstr_w(arg
));
1710 hres
= identifier_eval(ctx
, arg
, &exprval
);
1714 if(exprval
.type
== EXPRVAL_INVALID
) {
1715 hres
= stack_push(ctx
, jsval_string(jsstr_undefined()));
1716 exprval_release(&exprval
);
1720 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1721 exprval_release(&exprval
);
1725 hres
= typeof_string(v
, &ret
);
1730 return stack_push_string(ctx
, ret
);
1733 /* ECMA-262 3rd Edition 11.4.3 */
1734 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
1743 hres
= typeof_string(v
, &ret
);
1748 return stack_push_string(ctx
, ret
);
1751 /* ECMA-262 3rd Edition 11.4.7 */
1752 static HRESULT
interp_minus(script_ctx_t
*ctx
)
1759 hres
= stack_pop_number(ctx
, &n
);
1763 return stack_push(ctx
, jsval_number(-n
));
1766 /* ECMA-262 3rd Edition 11.4.6 */
1767 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
1776 hres
= to_number(ctx
, v
, &n
);
1781 return stack_push(ctx
, jsval_number(n
));
1784 /* ECMA-262 3rd Edition 11.3.1 */
1785 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
1787 const int arg
= get_op_int(ctx
, 0);
1795 obj
= stack_pop_objid(ctx
, &id
);
1797 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1799 hres
= disp_propget(ctx
, obj
, id
, &v
);
1800 if(SUCCEEDED(hres
)) {
1803 hres
= to_number(ctx
, v
, &n
);
1805 hres
= disp_propput(ctx
, obj
, id
, jsval_number(n
+(double)arg
));
1809 IDispatch_Release(obj
);
1813 return stack_push(ctx
, v
);
1816 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1817 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
1819 const int arg
= get_op_int(ctx
, 0);
1828 obj
= stack_pop_objid(ctx
, &id
);
1830 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1832 hres
= disp_propget(ctx
, obj
, id
, &v
);
1833 if(SUCCEEDED(hres
)) {
1836 hres
= to_number(ctx
, v
, &n
);
1838 if(SUCCEEDED(hres
)) {
1839 ret
= n
+(double)arg
;
1840 hres
= disp_propput(ctx
, obj
, id
, jsval_number(ret
));
1843 IDispatch_Release(obj
);
1847 return stack_push(ctx
, jsval_number(ret
));
1850 /* ECMA-262 3rd Edition 11.9.3 */
1851 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
1853 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
1854 return equal2_values(lval
, rval
, ret
);
1856 /* FIXME: NULL disps should be handled in more general way */
1857 if(is_object_instance(lval
) && !get_object(lval
))
1858 return equal_values(ctx
, jsval_null(), rval
, ret
);
1859 if(is_object_instance(rval
) && !get_object(rval
))
1860 return equal_values(ctx
, lval
, jsval_null(), ret
);
1862 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
1867 if(is_string(lval
) && is_number(rval
)) {
1871 hres
= to_number(ctx
, lval
, &n
);
1875 /* FIXME: optimize */
1876 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
1879 if(is_string(rval
) && is_number(lval
)) {
1883 hres
= to_number(ctx
, rval
, &n
);
1887 /* FIXME: optimize */
1888 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
1892 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
1895 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
1898 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
1902 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
1906 hres
= equal_values(ctx
, lval
, prim
, ret
);
1907 jsval_release(prim
);
1912 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
1916 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
1920 hres
= equal_values(ctx
, prim
, rval
, ret
);
1921 jsval_release(prim
);
1930 /* ECMA-262 3rd Edition 11.9.1 */
1931 static HRESULT
interp_eq(script_ctx_t
*ctx
)
1940 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1942 hres
= equal_values(ctx
, l
, r
, &b
);
1948 return stack_push(ctx
, jsval_bool(b
));
1951 /* ECMA-262 3rd Edition 11.9.2 */
1952 static HRESULT
interp_neq(script_ctx_t
*ctx
)
1961 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1963 hres
= equal_values(ctx
, l
, r
, &b
);
1969 return stack_push(ctx
, jsval_bool(!b
));
1972 /* ECMA-262 3rd Edition 11.9.4 */
1973 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
1982 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1984 hres
= equal2_values(r
, l
, &b
);
1990 return stack_push(ctx
, jsval_bool(b
));
1993 /* ECMA-262 3rd Edition 11.9.5 */
1994 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2005 hres
= equal2_values(r
, l
, &b
);
2011 return stack_push(ctx
, jsval_bool(!b
));
2014 /* ECMA-262 3rd Edition 11.8.5 */
2015 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2021 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2025 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2031 if(is_string(l
) && is_string(r
)) {
2032 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2033 jsstr_release(get_string(l
));
2034 jsstr_release(get_string(r
));
2038 hres
= to_number(ctx
, l
, &ln
);
2041 hres
= to_number(ctx
, r
, &rn
);
2046 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2050 /* ECMA-262 3rd Edition 11.8.1 */
2051 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2060 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2062 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2068 return stack_push(ctx
, jsval_bool(b
));
2071 /* ECMA-262 3rd Edition 11.8.1 */
2072 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2081 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2083 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2089 return stack_push(ctx
, jsval_bool(b
));
2092 /* ECMA-262 3rd Edition 11.8.2 */
2093 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2102 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2104 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2110 return stack_push(ctx
, jsval_bool(b
));
2113 /* ECMA-262 3rd Edition 11.8.4 */
2114 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2123 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2125 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2131 return stack_push(ctx
, jsval_bool(b
));
2134 /* ECMA-262 3rd Edition 11.4.8 */
2135 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2144 hres
= to_int32(ctx
, v
, &i
);
2149 return stack_push(ctx
, jsval_number(~i
));
2152 /* ECMA-262 3rd Edition 11.4.9 */
2153 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2162 hres
= to_boolean(v
, &b
);
2167 return stack_push(ctx
, jsval_bool(!b
));
2170 /* ECMA-262 3rd Edition 11.7.1 */
2171 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2177 hres
= stack_pop_uint(ctx
, &r
);
2181 hres
= stack_pop_int(ctx
, &l
);
2185 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2188 /* ECMA-262 3rd Edition 11.7.2 */
2189 static HRESULT
interp_rshift(script_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.3 */
2207 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2212 hres
= stack_pop_uint(ctx
, &r
);
2216 hres
= stack_pop_uint(ctx
, &l
);
2220 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2223 /* ECMA-262 3rd Edition 11.13.1 */
2224 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2235 disp
= stack_pop_objid(ctx
, &id
);
2238 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2241 hres
= disp_propput(ctx
, disp
, id
, v
);
2242 IDispatch_Release(disp
);
2248 return stack_push(ctx
, v
);
2251 /* JScript extension */
2252 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2254 const unsigned argc
= get_op_uint(ctx
, 0);
2260 TRACE("%u\n", argc
);
2262 disp
= stack_topn_objid(ctx
, argc
+1, &id
);
2264 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2266 hres
= disp_call(ctx
, disp
, id
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2271 stack_popn(ctx
, argc
+2);
2272 return stack_push(ctx
, v
);
2275 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2279 return stack_push(ctx
, jsval_undefined());
2282 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2284 const unsigned arg
= get_op_uint(ctx
, 0);
2292 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2294 const unsigned arg
= get_op_uint(ctx
, 0);
2302 hres
= to_boolean(v
, &b
);
2314 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2316 const unsigned arg
= get_op_uint(ctx
, 0);
2320 stack_popn(ctx
, arg
);
2324 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2326 const unsigned clear_ret
= get_op_uint(ctx
, 0);
2327 call_frame_t
*frame
= ctx
->call_ctx
;
2332 jsval_release(steal_ret(frame
));
2334 if((frame
->flags
& EXEC_CONSTRUCTOR
) && !is_object_instance(frame
->ret
)) {
2335 jsval_release(frame
->ret
);
2336 IDispatch_AddRef(frame
->this_obj
);
2337 frame
->ret
= jsval_disp(frame
->this_obj
);
2344 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2346 call_frame_t
*frame
= ctx
->call_ctx
;
2350 jsval_release(frame
->ret
);
2351 frame
->ret
= stack_pop(ctx
);
2355 static HRESULT
interp_push_ret(script_ctx_t
*ctx
)
2357 call_frame_t
*frame
= ctx
->call_ctx
;
2362 hres
= stack_push(ctx
, frame
->ret
);
2364 frame
->ret
= jsval_undefined();
2368 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2370 static const op_func_t op_funcs
[] = {
2371 #define X(x,a,b,c) interp_##x,
2376 static const unsigned op_move
[] = {
2377 #define X(a,x,b,c) x,
2382 static void release_call_frame(call_frame_t
*frame
)
2384 if(frame
->arguments_obj
) {
2385 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
2386 * their own arguments property, it's impossible to use prototype's one during name lookup */
2387 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
2388 jsdisp_propput_name(frame
->variable_obj
, argumentsW
, jsval_undefined());
2389 jsdisp_release(frame
->arguments_obj
);
2391 if(frame
->function_instance
)
2392 jsdisp_release(frame
->function_instance
);
2393 if(frame
->variable_obj
)
2394 jsdisp_release(frame
->variable_obj
);
2396 IDispatch_Release(frame
->this_obj
);
2398 scope_release(frame
->scope
);
2399 jsval_release(frame
->ret
);
2400 release_bytecode(frame
->bytecode
);
2404 static HRESULT
unwind_exception(script_ctx_t
*ctx
, HRESULT exception_hres
)
2406 except_frame_t
*except_frame
;
2407 call_frame_t
*frame
;
2412 for(frame
= ctx
->call_ctx
; !frame
->except_frame
; frame
= ctx
->call_ctx
) {
2415 while(frame
->scope
!= frame
->base_scope
)
2416 scope_pop(&frame
->scope
);
2418 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
2420 ctx
->call_ctx
= frame
->prev_frame
;
2421 flags
= frame
->flags
;
2422 release_call_frame(frame
);
2423 if(!(flags
& EXEC_RETURN_TO_INTERP
))
2424 return exception_hres
;
2427 except_frame
= frame
->except_frame
;
2428 frame
->except_frame
= except_frame
->next
;
2430 assert(except_frame
->stack_top
<= ctx
->stack_top
);
2431 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
2433 while(except_frame
->scope
!= frame
->scope
)
2434 scope_pop(&frame
->scope
);
2436 frame
->ip
= except_frame
->catch_off
;
2438 except_val
= ctx
->ei
.val
;
2439 ctx
->ei
.val
= jsval_undefined();
2442 ident
= except_frame
->ident
;
2443 heap_free(except_frame
);
2446 jsdisp_t
*scope_obj
;
2448 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
2449 if(SUCCEEDED(hres
)) {
2450 hres
= jsdisp_propput_name(scope_obj
, ident
, except_val
);
2452 jsdisp_release(scope_obj
);
2454 jsval_release(except_val
);
2458 hres
= scope_push(frame
->scope
, scope_obj
, to_disp(scope_obj
), &frame
->scope
);
2459 jsdisp_release(scope_obj
);
2461 hres
= stack_push(ctx
, except_val
);
2465 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2471 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, jsval_t
*r
)
2473 call_frame_t
*frame
;
2475 HRESULT hres
= S_OK
;
2480 frame
= ctx
->call_ctx
;
2481 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
2482 hres
= op_funcs
[op
](ctx
);
2484 TRACE("EXCEPTION %08x\n", hres
);
2486 hres
= unwind_exception(ctx
, hres
);
2489 }else if(frame
->ip
== -1) {
2490 const DWORD return_to_interp
= frame
->flags
& EXEC_RETURN_TO_INTERP
;
2492 assert(ctx
->stack_top
== frame
->stack_base
);
2493 assert(frame
->scope
== frame
->base_scope
);
2495 ctx
->call_ctx
= frame
->prev_frame
;
2496 if(return_to_interp
) {
2497 clear_ret(ctx
->call_ctx
);
2498 ctx
->call_ctx
->ret
= steal_ret(frame
);
2500 *r
= steal_ret(frame
);
2502 release_call_frame(frame
);
2503 if(!return_to_interp
)
2506 frame
->ip
+= op_move
[op
];
2513 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
2515 IBindEventHandler
*target
;
2521 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
2525 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2526 exprval_release(&exprval
);
2530 if(!is_object_instance(v
)) {
2531 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
2535 disp
= get_object(v
);
2536 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
2537 if(SUCCEEDED(hres
)) {
2538 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
2539 IBindEventHandler_Release(target
);
2541 WARN("BindEvent failed: %08x\n", hres
);
2543 FIXME("No IBindEventHandler, not yet supported binding\n");
2546 IDispatch_Release(disp
);
2550 HRESULT
exec_source(script_ctx_t
*ctx
, DWORD flags
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
2551 IDispatch
*this_obj
, jsdisp_t
*function_instance
, jsdisp_t
*variable_obj
, jsdisp_t
*arguments_obj
, jsval_t
*r
)
2553 call_frame_t
*frame
;
2557 for(i
= 0; i
< function
->func_cnt
; i
++) {
2560 if(!function
->funcs
[i
].name
)
2563 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+i
, scope
, &func_obj
);
2567 if(function
->funcs
[i
].event_target
)
2568 hres
= bind_event_target(ctx
, function
->funcs
+i
, func_obj
);
2570 hres
= jsdisp_propput_name(variable_obj
, function
->funcs
[i
].name
, jsval_obj(func_obj
));
2571 jsdisp_release(func_obj
);
2576 for(i
=0; i
< function
->var_cnt
; i
++) {
2577 if(!(flags
& EXEC_GLOBAL
) || !lookup_global_members(ctx
, function
->variables
[i
], NULL
)) {
2580 hres
= jsdisp_get_id(variable_obj
, function
->variables
[i
], fdexNameEnsure
, &id
);
2586 /* ECMA-262 3rd Edition 11.2.3.7 */
2590 jsthis
= iface_to_jsdisp((IUnknown
*)this_obj
);
2592 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
2594 jsdisp_release(jsthis
);
2598 frame
= heap_alloc_zero(sizeof(*frame
));
2600 return E_OUTOFMEMORY
;
2602 frame
->bytecode
= bytecode_addref(bytecode
);
2603 frame
->function
= function
;
2604 frame
->ip
= function
->instr_off
;
2605 frame
->stack_base
= ctx
->stack_top
;
2606 frame
->ret
= jsval_undefined();
2608 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
2611 frame
->this_obj
= this_obj
;
2612 else if(ctx
->host_global
)
2613 frame
->this_obj
= ctx
->host_global
;
2615 frame
->this_obj
= to_disp(ctx
->global
);
2616 IDispatch_AddRef(frame
->this_obj
);
2618 if(function_instance
)
2619 frame
->function_instance
= jsdisp_addref(function_instance
);
2621 frame
->arguments_obj
= jsdisp_addref(arguments_obj
);
2623 frame
->flags
= flags
;
2624 frame
->variable_obj
= jsdisp_addref(variable_obj
);
2626 frame
->prev_frame
= ctx
->call_ctx
;
2627 ctx
->call_ctx
= frame
;
2629 if(flags
& EXEC_RETURN_TO_INTERP
) {
2631 * We're called directly from interpreter, so we may just setup call frame and return.
2632 * Already running interpreter will take care of execution.
2635 *r
= jsval_undefined();
2639 return enter_bytecode(ctx
, r
);