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 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
, BOOL is_global
, exec_ctx_t
**ret
)
302 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
304 return E_OUTOFMEMORY
;
307 ctx
->is_global
= is_global
;
309 script_addref(script_ctx
);
310 ctx
->script
= script_ctx
;
316 void exec_release(exec_ctx_t
*ctx
)
322 script_release(ctx
->script
);
326 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
333 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
335 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
336 jsdisp_release(jsdisp
);
343 bstr
= SysAllocString(name
);
345 return E_OUTOFMEMORY
;
349 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
350 if(SUCCEEDED(hres
)) {
351 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
352 IDispatchEx_Release(dispex
);
354 TRACE("using IDispatch\n");
355 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
358 if(name_bstr
!= bstr
)
363 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
365 IObjectIdentity
*identity
;
366 IUnknown
*unk1
, *unk2
;
374 if(!disp1
|| !disp2
) {
379 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
383 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
385 IUnknown_Release(unk1
);
392 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
393 if(SUCCEEDED(hres
)) {
394 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
395 IObjectIdentity_Release(identity
);
402 IUnknown_Release(unk1
);
403 IUnknown_Release(unk2
);
407 /* ECMA-262 3rd Edition 11.9.6 */
408 static HRESULT
equal2_values(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
410 jsval_type_t type
= jsval_type(lval
);
414 if(type
!= jsval_type(rval
)) {
415 if(is_null_instance(lval
))
416 *ret
= is_null_instance(rval
);
428 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
430 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
433 *ret
= get_number(lval
) == get_number(rval
);
436 *ret
= !get_bool(lval
) == !get_bool(rval
);
439 FIXME("VARIANT not implemented\n");
446 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
452 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
453 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
454 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
455 if(SUCCEEDED(hres
)) {
457 exprval_set_idref(ret
, item
->disp
, id
);
466 /* ECMA-262 3rd Edition 10.1.4 */
467 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
469 scope_chain_t
*scope
;
474 TRACE("%s\n", debugstr_w(identifier
));
477 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
479 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
481 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
482 if(SUCCEEDED(hres
)) {
483 exprval_set_idref(ret
, scope
->obj
, id
);
489 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
490 if(SUCCEEDED(hres
)) {
491 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
495 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
496 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
503 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
504 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
506 WARN("GetItemInfo failed: %08x\n", hres
);
510 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
511 IUnknown_Release(unk
);
513 WARN("object does not implement IDispatch\n");
518 IDispatch_AddRef(item
->disp
);
519 ret
->type
= EXPRVAL_JSVAL
;
520 ret
->u
.val
= jsval_disp(item
->disp
);
525 if(lookup_global_members(ctx
, identifier
, ret
))
528 ret
->type
= EXPRVAL_INVALID
;
532 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
534 call_frame_t
*frame
= ctx
->call_ctx
;
535 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
538 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
540 call_frame_t
*frame
= ctx
->call_ctx
;
541 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
544 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
546 call_frame_t
*frame
= ctx
->call_ctx
;
547 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
550 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
552 call_frame_t
*frame
= ctx
->call_ctx
;
553 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
556 static inline double get_op_double(script_ctx_t
*ctx
)
558 call_frame_t
*frame
= ctx
->call_ctx
;
559 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
562 static inline void jmp_next(script_ctx_t
*ctx
)
567 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
569 ctx
->call_ctx
->ip
= dst
;
572 /* ECMA-262 3rd Edition 12.2 */
573 static HRESULT
interp_var_set(script_ctx_t
*ctx
)
575 const BSTR name
= get_op_bstr(ctx
, 0);
579 TRACE("%s\n", debugstr_w(name
));
581 val
= stack_pop(ctx
);
582 hres
= jsdisp_propput_name(ctx
->call_ctx
->variable_obj
, name
, val
);
587 /* ECMA-262 3rd Edition 12.6.4 */
588 static HRESULT
interp_forin(script_ctx_t
*ctx
)
590 const HRESULT arg
= get_op_uint(ctx
, 0);
591 IDispatch
*var_obj
, *obj
= NULL
;
599 assert(is_number(stack_top(ctx
)));
600 id
= get_number(stack_top(ctx
));
602 var_obj
= stack_topn_objid(ctx
, 1, &var_id
);
604 FIXME("invalid ref\n");
608 if(is_object_instance(stack_topn(ctx
, 3)))
609 obj
= get_object(stack_topn(ctx
, 3));
612 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
613 if(SUCCEEDED(hres
)) {
614 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
616 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
617 IDispatchEx_Release(dispex
);
621 TRACE("No IDispatchEx\n");
628 str
= jsstr_alloc_len(name
, SysStringLen(name
));
631 return E_OUTOFMEMORY
;
634 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
636 hres
= disp_propput(ctx
, var_obj
, var_id
, jsval_string(str
));
649 /* ECMA-262 3rd Edition 12.10 */
650 static HRESULT
interp_push_scope(script_ctx_t
*ctx
)
659 hres
= to_object(ctx
, v
, &disp
);
664 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
665 IDispatch_Release(disp
);
669 /* ECMA-262 3rd Edition 12.10 */
670 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
674 scope_pop(&ctx
->call_ctx
->scope
);
678 /* ECMA-262 3rd Edition 12.13 */
679 static HRESULT
interp_case(script_ctx_t
*ctx
)
681 const unsigned arg
= get_op_uint(ctx
, 0);
689 hres
= equal2_values(stack_top(ctx
), v
, &b
);
703 /* ECMA-262 3rd Edition 12.13 */
704 static HRESULT
interp_throw(script_ctx_t
*ctx
)
708 jsval_release(ctx
->ei
.val
);
709 ctx
->ei
.val
= stack_pop(ctx
);
710 return DISP_E_EXCEPTION
;
713 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
715 const HRESULT arg
= get_op_uint(ctx
, 0);
717 TRACE("%08x\n", arg
);
719 return throw_reference_error(ctx
, arg
, NULL
);
722 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
724 const HRESULT hres
= get_op_uint(ctx
, 0);
725 jsstr_t
*str
= get_op_str(ctx
, 1);
728 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
730 ptr
= jsstr_flatten(str
);
731 return ptr
? throw_type_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
734 /* ECMA-262 3rd Edition 12.14 */
735 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
737 const unsigned arg1
= get_op_uint(ctx
, 0);
738 const BSTR arg2
= get_op_bstr(ctx
, 1);
739 call_frame_t
*frame
= ctx
->call_ctx
;
740 except_frame_t
*except
;
745 stack_top
= ctx
->stack_top
;
750 hres
= stack_push(ctx
, jsval_bool(TRUE
));
753 hres
= stack_push(ctx
, jsval_bool(TRUE
));
758 except
= heap_alloc(sizeof(*except
));
760 return E_OUTOFMEMORY
;
762 except
->stack_top
= stack_top
;
763 except
->scope
= frame
->scope
;
764 except
->catch_off
= arg1
;
765 except
->ident
= arg2
;
766 except
->next
= frame
->except_frame
;
767 frame
->except_frame
= except
;
771 /* ECMA-262 3rd Edition 12.14 */
772 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
774 call_frame_t
*frame
= ctx
->call_ctx
;
775 except_frame_t
*except
;
779 except
= frame
->except_frame
;
780 assert(except
!= NULL
);
782 frame
->except_frame
= except
->next
;
787 /* ECMA-262 3rd Edition 12.14 */
788 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
798 TRACE("passing exception\n");
800 ctx
->ei
.val
= stack_pop(ctx
);
801 return DISP_E_EXCEPTION
;
808 /* ECMA-262 3rd Edition 13 */
809 static HRESULT
interp_func(script_ctx_t
*ctx
)
811 unsigned func_idx
= get_op_uint(ctx
, 0);
812 call_frame_t
*frame
= ctx
->call_ctx
;
816 TRACE("%d\n", func_idx
);
818 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
819 frame
->scope
, &dispex
);
823 return stack_push(ctx
, jsval_obj(dispex
));
826 /* ECMA-262 3rd Edition 11.2.1 */
827 static HRESULT
interp_array(script_ctx_t
*ctx
)
838 namev
= stack_pop(ctx
);
840 hres
= stack_pop_object(ctx
, &obj
);
842 jsval_release(namev
);
846 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
847 jsval_release(namev
);
849 IDispatch_Release(obj
);
853 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
854 jsstr_release(name_str
);
855 if(SUCCEEDED(hres
)) {
856 hres
= disp_propget(ctx
, obj
, id
, &v
);
857 }else if(hres
== DISP_E_UNKNOWNNAME
) {
858 v
= jsval_undefined();
861 IDispatch_Release(obj
);
865 return stack_push(ctx
, v
);
868 /* ECMA-262 3rd Edition 11.2.1 */
869 static HRESULT
interp_member(script_ctx_t
*ctx
)
871 const BSTR arg
= get_op_bstr(ctx
, 0);
879 hres
= stack_pop_object(ctx
, &obj
);
883 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
884 if(SUCCEEDED(hres
)) {
885 hres
= disp_propget(ctx
, obj
, id
, &v
);
886 }else if(hres
== DISP_E_UNKNOWNNAME
) {
887 v
= jsval_undefined();
890 IDispatch_Release(obj
);
894 return stack_push(ctx
, v
);
897 /* ECMA-262 3rd Edition 11.2.1 */
898 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
900 const unsigned arg
= get_op_uint(ctx
, 0);
910 namev
= stack_pop(ctx
);
911 objv
= stack_pop(ctx
);
913 hres
= to_object(ctx
, objv
, &obj
);
915 if(SUCCEEDED(hres
)) {
916 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
918 IDispatch_Release(obj
);
920 jsval_release(namev
);
924 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
925 jsstr_release(name_str
);
927 IDispatch_Release(obj
);
928 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
930 id
= JS_E_INVALID_PROPERTY
;
932 ERR("failed %08x\n", hres
);
937 return stack_push_objid(ctx
, obj
, id
);
940 /* ECMA-262 3rd Edition 11.2.1 */
941 static HRESULT
interp_refval(script_ctx_t
*ctx
)
950 disp
= stack_topn_objid(ctx
, 0, &id
);
952 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
954 hres
= disp_propget(ctx
, disp
, id
, &v
);
958 return stack_push(ctx
, v
);
961 /* ECMA-262 3rd Edition 11.2.2 */
962 static HRESULT
interp_new(script_ctx_t
*ctx
)
964 const unsigned argc
= get_op_uint(ctx
, 0);
970 constr
= stack_topn(ctx
, argc
);
972 /* NOTE: Should use to_object here */
975 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
976 else if(!is_object_instance(constr
))
977 return throw_type_error(ctx
, JS_E_INVALID_ACTION
, NULL
);
978 else if(!get_object(constr
))
979 return throw_type_error(ctx
, JS_E_INVALID_PROPERTY
, NULL
);
981 hres
= disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
, argc
, stack_args(ctx
, argc
), &r
);
985 stack_popn(ctx
, argc
+1);
986 return stack_push(ctx
, r
);
989 /* ECMA-262 3rd Edition 11.2.3 */
990 static HRESULT
interp_call(script_ctx_t
*ctx
)
992 const unsigned argn
= get_op_uint(ctx
, 0);
993 const int do_ret
= get_op_int(ctx
, 1);
997 TRACE("%d %d\n", argn
, do_ret
);
999 obj
= stack_topn(ctx
, argn
);
1000 if(!is_object_instance(obj
))
1001 return throw_type_error(ctx
, JS_E_INVALID_PROPERTY
, NULL
);
1003 hres
= disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
),
1004 do_ret
? &r
: NULL
);
1008 stack_popn(ctx
, argn
+1);
1009 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1012 /* ECMA-262 3rd Edition 11.2.3 */
1013 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
1015 const unsigned argn
= get_op_uint(ctx
, 0);
1016 const int do_ret
= get_op_int(ctx
, 1);
1022 TRACE("%d %d\n", argn
, do_ret
);
1024 obj
= stack_topn_objid(ctx
, argn
, &id
);
1026 return throw_type_error(ctx
, id
, NULL
);
1028 hres
= disp_call(ctx
, obj
, id
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
), do_ret
? &r
: NULL
);
1032 stack_popn(ctx
, argn
+2);
1033 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1037 /* ECMA-262 3rd Edition 11.1.1 */
1038 static HRESULT
interp_this(script_ctx_t
*ctx
)
1040 call_frame_t
*frame
= ctx
->call_ctx
;
1044 IDispatch_AddRef(frame
->this_obj
);
1045 return stack_push(ctx
, jsval_disp(frame
->this_obj
));
1048 /* ECMA-262 3rd Edition 10.1.4 */
1049 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1051 const BSTR arg
= get_op_bstr(ctx
, 0);
1056 TRACE("%s\n", debugstr_w(arg
));
1058 hres
= identifier_eval(ctx
, arg
, &exprval
);
1062 if(exprval
.type
== EXPRVAL_INVALID
)
1063 return throw_type_error(ctx
, JS_E_UNDEFINED_VARIABLE
, arg
);
1065 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1066 exprval_release(&exprval
);
1070 return stack_push(ctx
, v
);
1073 /* ECMA-262 3rd Edition 10.1.4 */
1074 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1076 const BSTR arg
= get_op_bstr(ctx
, 0);
1077 const unsigned flags
= get_op_uint(ctx
, 1);
1081 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1083 hres
= identifier_eval(ctx
, arg
, &exprval
);
1087 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1090 hres
= jsdisp_get_id(ctx
->global
, arg
, fdexNameEnsure
, &id
);
1094 exprval_set_idref(&exprval
, to_disp(ctx
->global
), id
);
1097 if(exprval
.type
!= EXPRVAL_IDREF
) {
1098 WARN("invalid ref\n");
1099 exprval_release(&exprval
);
1100 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1103 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1106 /* ECMA-262 3rd Edition 7.8.1 */
1107 static HRESULT
interp_null(script_ctx_t
*ctx
)
1111 return stack_push(ctx
, jsval_null());
1114 /* ECMA-262 3rd Edition 7.8.2 */
1115 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1117 const int arg
= get_op_int(ctx
, 0);
1119 TRACE("%s\n", arg
? "true" : "false");
1121 return stack_push(ctx
, jsval_bool(arg
));
1124 /* ECMA-262 3rd Edition 7.8.3 */
1125 static HRESULT
interp_int(script_ctx_t
*ctx
)
1127 const int arg
= get_op_int(ctx
, 0);
1131 return stack_push(ctx
, jsval_number(arg
));
1134 /* ECMA-262 3rd Edition 7.8.3 */
1135 static HRESULT
interp_double(script_ctx_t
*ctx
)
1137 const double arg
= get_op_double(ctx
);
1139 TRACE("%lf\n", arg
);
1141 return stack_push(ctx
, jsval_number(arg
));
1144 /* ECMA-262 3rd Edition 7.8.4 */
1145 static HRESULT
interp_str(script_ctx_t
*ctx
)
1147 jsstr_t
*str
= get_op_str(ctx
, 0);
1149 TRACE("%s\n", debugstr_jsstr(str
));
1151 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1154 /* ECMA-262 3rd Edition 7.8 */
1155 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1157 jsstr_t
*source
= get_op_str(ctx
, 0);
1158 const unsigned flags
= get_op_uint(ctx
, 1);
1162 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1164 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1168 return stack_push(ctx
, jsval_obj(regexp
));
1171 /* ECMA-262 3rd Edition 11.1.4 */
1172 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1174 const unsigned arg
= get_op_uint(ctx
, 0);
1182 hres
= create_array(ctx
, arg
, &array
);
1188 val
= stack_pop(ctx
);
1189 hres
= jsdisp_propput_idx(array
, i
, val
);
1192 jsdisp_release(array
);
1197 return stack_push(ctx
, jsval_obj(array
));
1200 /* ECMA-262 3rd Edition 11.1.5 */
1201 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1208 hres
= create_object(ctx
, NULL
, &obj
);
1212 return stack_push(ctx
, jsval_obj(obj
));
1215 /* ECMA-262 3rd Edition 11.1.5 */
1216 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1218 const BSTR name
= get_op_bstr(ctx
, 0);
1223 TRACE("%s\n", debugstr_w(name
));
1225 val
= stack_pop(ctx
);
1227 assert(is_object_instance(stack_top(ctx
)));
1228 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1230 hres
= jsdisp_propput_name(obj
, name
, val
);
1235 /* ECMA-262 3rd Edition 11.11 */
1236 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1238 const unsigned arg
= get_op_uint(ctx
, 0);
1244 hres
= to_boolean(stack_top(ctx
), &b
);
1257 /* ECMA-262 3rd Edition 11.11 */
1258 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1260 const unsigned arg
= get_op_uint(ctx
, 0);
1266 hres
= to_boolean(stack_top(ctx
), &b
);
1279 /* ECMA-262 3rd Edition 11.10 */
1280 static HRESULT
interp_or(script_ctx_t
*ctx
)
1287 hres
= stack_pop_int(ctx
, &r
);
1291 hres
= stack_pop_int(ctx
, &l
);
1295 return stack_push(ctx
, jsval_number(l
|r
));
1298 /* ECMA-262 3rd Edition 11.10 */
1299 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1306 hres
= stack_pop_int(ctx
, &r
);
1310 hres
= stack_pop_int(ctx
, &l
);
1314 return stack_push(ctx
, jsval_number(l
^r
));
1317 /* ECMA-262 3rd Edition 11.10 */
1318 static HRESULT
interp_and(script_ctx_t
*ctx
)
1325 hres
= stack_pop_int(ctx
, &r
);
1329 hres
= stack_pop_int(ctx
, &l
);
1333 return stack_push(ctx
, jsval_number(l
&r
));
1336 /* ECMA-262 3rd Edition 11.8.6 */
1337 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1339 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1344 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1347 if(!is_object_instance(v
) || !get_object(v
)) {
1349 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1352 obj
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1353 IDispatch_Release(get_object(v
));
1355 FIXME("non-jsdisp objects not supported\n");
1359 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1360 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1362 hres
= throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1364 jsdisp_release(obj
);
1370 if(is_object_instance(prot
)) {
1371 if(is_object_instance(v
))
1372 tmp
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1373 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1374 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1380 jsdisp_release(tmp
);
1382 FIXME("prototype is not an object\n");
1386 jsval_release(prot
);
1391 return stack_push(ctx
, jsval_bool(ret
));
1394 /* ECMA-262 3rd Edition 11.8.7 */
1395 static HRESULT
interp_in(script_ctx_t
*ctx
)
1406 obj
= stack_pop(ctx
);
1407 if(!is_object_instance(obj
) || !get_object(obj
)) {
1409 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1413 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1416 IDispatch_Release(get_object(obj
));
1420 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1421 IDispatch_Release(get_object(obj
));
1422 jsstr_release(jsstr
);
1425 else if(hres
== DISP_E_UNKNOWNNAME
)
1430 return stack_push(ctx
, jsval_bool(ret
));
1433 /* ECMA-262 3rd Edition 11.6.1 */
1434 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1439 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1443 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1449 if(is_string(l
) || is_string(r
)) {
1450 jsstr_t
*lstr
, *rstr
= NULL
;
1452 hres
= to_string(ctx
, l
, &lstr
);
1454 hres
= to_string(ctx
, r
, &rstr
);
1456 if(SUCCEEDED(hres
)) {
1459 ret_str
= jsstr_concat(lstr
, rstr
);
1461 *ret
= jsval_string(ret_str
);
1463 hres
= E_OUTOFMEMORY
;
1466 jsstr_release(lstr
);
1468 jsstr_release(rstr
);
1472 hres
= to_number(ctx
, l
, &nl
);
1473 if(SUCCEEDED(hres
)) {
1474 hres
= to_number(ctx
, r
, &nr
);
1476 *ret
= jsval_number(nl
+nr
);
1485 /* ECMA-262 3rd Edition 11.6.1 */
1486 static HRESULT
interp_add(script_ctx_t
*ctx
)
1494 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1496 hres
= add_eval(ctx
, l
, r
, &ret
);
1502 return stack_push(ctx
, ret
);
1505 /* ECMA-262 3rd Edition 11.6.2 */
1506 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1513 hres
= stack_pop_number(ctx
, &r
);
1517 hres
= stack_pop_number(ctx
, &l
);
1521 return stack_push(ctx
, jsval_number(l
-r
));
1524 /* ECMA-262 3rd Edition 11.5.1 */
1525 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1532 hres
= stack_pop_number(ctx
, &r
);
1536 hres
= stack_pop_number(ctx
, &l
);
1540 return stack_push(ctx
, jsval_number(l
*r
));
1543 /* ECMA-262 3rd Edition 11.5.2 */
1544 static HRESULT
interp_div(script_ctx_t
*ctx
)
1551 hres
= stack_pop_number(ctx
, &r
);
1555 hres
= stack_pop_number(ctx
, &l
);
1559 return stack_push(ctx
, jsval_number(l
/r
));
1562 /* ECMA-262 3rd Edition 11.5.3 */
1563 static HRESULT
interp_mod(script_ctx_t
*ctx
)
1570 hres
= stack_pop_number(ctx
, &r
);
1574 hres
= stack_pop_number(ctx
, &l
);
1578 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1581 /* ECMA-262 3rd Edition 11.4.2 */
1582 static HRESULT
interp_delete(script_ctx_t
*ctx
)
1584 jsval_t objv
, namev
;
1592 namev
= stack_pop(ctx
);
1593 objv
= stack_pop(ctx
);
1595 hres
= to_object(ctx
, objv
, &obj
);
1596 jsval_release(objv
);
1598 jsval_release(namev
);
1602 hres
= to_string(ctx
, namev
, &name
);
1603 jsval_release(namev
);
1605 IDispatch_Release(obj
);
1609 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
1610 IDispatch_Release(obj
);
1611 jsstr_release(name
);
1615 return stack_push(ctx
, jsval_bool(ret
));
1618 /* ECMA-262 3rd Edition 11.4.2 */
1619 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
1621 const BSTR arg
= get_op_bstr(ctx
, 0);
1626 TRACE("%s\n", debugstr_w(arg
));
1628 hres
= identifier_eval(ctx
, arg
, &exprval
);
1632 switch(exprval
.type
) {
1634 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1635 IDispatch_Release(exprval
.u
.idref
.disp
);
1639 case EXPRVAL_INVALID
:
1643 FIXME("Unsupported exprval\n");
1644 exprval_release(&exprval
);
1649 return stack_push(ctx
, jsval_bool(ret
));
1652 /* ECMA-262 3rd Edition 11.4.2 */
1653 static HRESULT
interp_void(script_ctx_t
*ctx
)
1658 return stack_push(ctx
, jsval_undefined());
1661 /* ECMA-262 3rd Edition 11.4.3 */
1662 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1664 switch(jsval_type(v
)) {
1674 if(get_object(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)get_object(v
)))) {
1675 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1676 jsdisp_release(dispex
);
1692 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1699 /* ECMA-262 3rd Edition 11.4.3 */
1700 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
1710 obj
= stack_pop_objid(ctx
, &id
);
1712 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1714 hres
= disp_propget(ctx
, obj
, id
, &v
);
1715 IDispatch_Release(obj
);
1717 return stack_push_string(ctx
, unknownW
);
1719 hres
= typeof_string(v
, &ret
);
1724 return stack_push_string(ctx
, ret
);
1727 /* ECMA-262 3rd Edition 11.4.3 */
1728 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
1730 const BSTR arg
= get_op_bstr(ctx
, 0);
1736 TRACE("%s\n", debugstr_w(arg
));
1738 hres
= identifier_eval(ctx
, arg
, &exprval
);
1742 if(exprval
.type
== EXPRVAL_INVALID
) {
1743 hres
= stack_push(ctx
, jsval_string(jsstr_undefined()));
1744 exprval_release(&exprval
);
1748 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1749 exprval_release(&exprval
);
1753 hres
= typeof_string(v
, &ret
);
1758 return stack_push_string(ctx
, ret
);
1761 /* ECMA-262 3rd Edition 11.4.3 */
1762 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
1771 hres
= typeof_string(v
, &ret
);
1776 return stack_push_string(ctx
, ret
);
1779 /* ECMA-262 3rd Edition 11.4.7 */
1780 static HRESULT
interp_minus(script_ctx_t
*ctx
)
1787 hres
= stack_pop_number(ctx
, &n
);
1791 return stack_push(ctx
, jsval_number(-n
));
1794 /* ECMA-262 3rd Edition 11.4.6 */
1795 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
1804 hres
= to_number(ctx
, v
, &n
);
1809 return stack_push(ctx
, jsval_number(n
));
1812 /* ECMA-262 3rd Edition 11.3.1 */
1813 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
1815 const int arg
= get_op_int(ctx
, 0);
1823 obj
= stack_pop_objid(ctx
, &id
);
1825 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1827 hres
= disp_propget(ctx
, obj
, id
, &v
);
1828 if(SUCCEEDED(hres
)) {
1831 hres
= to_number(ctx
, v
, &n
);
1833 hres
= disp_propput(ctx
, obj
, id
, jsval_number(n
+(double)arg
));
1837 IDispatch_Release(obj
);
1841 return stack_push(ctx
, v
);
1844 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1845 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
1847 const int arg
= get_op_int(ctx
, 0);
1856 obj
= stack_pop_objid(ctx
, &id
);
1858 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1860 hres
= disp_propget(ctx
, obj
, id
, &v
);
1861 if(SUCCEEDED(hres
)) {
1864 hres
= to_number(ctx
, v
, &n
);
1866 if(SUCCEEDED(hres
)) {
1867 ret
= n
+(double)arg
;
1868 hres
= disp_propput(ctx
, obj
, id
, jsval_number(ret
));
1871 IDispatch_Release(obj
);
1875 return stack_push(ctx
, jsval_number(ret
));
1878 /* ECMA-262 3rd Edition 11.9.3 */
1879 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
1881 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
1882 return equal2_values(lval
, rval
, ret
);
1884 /* FIXME: NULL disps should be handled in more general way */
1885 if(is_object_instance(lval
) && !get_object(lval
))
1886 return equal_values(ctx
, jsval_null(), rval
, ret
);
1887 if(is_object_instance(rval
) && !get_object(rval
))
1888 return equal_values(ctx
, lval
, jsval_null(), ret
);
1890 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
1895 if(is_string(lval
) && is_number(rval
)) {
1899 hres
= to_number(ctx
, lval
, &n
);
1903 /* FIXME: optimize */
1904 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
1907 if(is_string(rval
) && is_number(lval
)) {
1911 hres
= to_number(ctx
, rval
, &n
);
1915 /* FIXME: optimize */
1916 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
1920 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
1923 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
1926 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
1930 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
1934 hres
= equal_values(ctx
, lval
, prim
, ret
);
1935 jsval_release(prim
);
1940 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
1944 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
1948 hres
= equal_values(ctx
, prim
, rval
, ret
);
1949 jsval_release(prim
);
1958 /* ECMA-262 3rd Edition 11.9.1 */
1959 static HRESULT
interp_eq(script_ctx_t
*ctx
)
1968 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1970 hres
= equal_values(ctx
, l
, r
, &b
);
1976 return stack_push(ctx
, jsval_bool(b
));
1979 /* ECMA-262 3rd Edition 11.9.2 */
1980 static HRESULT
interp_neq(script_ctx_t
*ctx
)
1989 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1991 hres
= equal_values(ctx
, l
, r
, &b
);
1997 return stack_push(ctx
, jsval_bool(!b
));
2000 /* ECMA-262 3rd Edition 11.9.4 */
2001 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
2010 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2012 hres
= equal2_values(r
, l
, &b
);
2018 return stack_push(ctx
, jsval_bool(b
));
2021 /* ECMA-262 3rd Edition 11.9.5 */
2022 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2033 hres
= equal2_values(r
, l
, &b
);
2039 return stack_push(ctx
, jsval_bool(!b
));
2042 /* ECMA-262 3rd Edition 11.8.5 */
2043 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2049 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2053 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2059 if(is_string(l
) && is_string(r
)) {
2060 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2061 jsstr_release(get_string(l
));
2062 jsstr_release(get_string(r
));
2066 hres
= to_number(ctx
, l
, &ln
);
2069 hres
= to_number(ctx
, r
, &rn
);
2074 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2078 /* ECMA-262 3rd Edition 11.8.1 */
2079 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2088 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2090 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2096 return stack_push(ctx
, jsval_bool(b
));
2099 /* ECMA-262 3rd Edition 11.8.1 */
2100 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2109 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2111 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2117 return stack_push(ctx
, jsval_bool(b
));
2120 /* ECMA-262 3rd Edition 11.8.2 */
2121 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2130 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2132 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2138 return stack_push(ctx
, jsval_bool(b
));
2141 /* ECMA-262 3rd Edition 11.8.4 */
2142 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2151 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2153 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2159 return stack_push(ctx
, jsval_bool(b
));
2162 /* ECMA-262 3rd Edition 11.4.8 */
2163 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2172 hres
= to_int32(ctx
, v
, &i
);
2177 return stack_push(ctx
, jsval_number(~i
));
2180 /* ECMA-262 3rd Edition 11.4.9 */
2181 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2190 hres
= to_boolean(v
, &b
);
2195 return stack_push(ctx
, jsval_bool(!b
));
2198 /* ECMA-262 3rd Edition 11.7.1 */
2199 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2205 hres
= stack_pop_uint(ctx
, &r
);
2209 hres
= stack_pop_int(ctx
, &l
);
2213 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2216 /* ECMA-262 3rd Edition 11.7.2 */
2217 static HRESULT
interp_rshift(script_ctx_t
*ctx
)
2223 hres
= stack_pop_uint(ctx
, &r
);
2227 hres
= stack_pop_int(ctx
, &l
);
2231 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2234 /* ECMA-262 3rd Edition 11.7.3 */
2235 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2240 hres
= stack_pop_uint(ctx
, &r
);
2244 hres
= stack_pop_uint(ctx
, &l
);
2248 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2251 /* ECMA-262 3rd Edition 11.13.1 */
2252 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2263 disp
= stack_pop_objid(ctx
, &id
);
2266 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2269 hres
= disp_propput(ctx
, disp
, id
, v
);
2270 IDispatch_Release(disp
);
2276 return stack_push(ctx
, v
);
2279 /* JScript extension */
2280 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2282 const unsigned argc
= get_op_uint(ctx
, 0);
2288 TRACE("%u\n", argc
);
2290 disp
= stack_topn_objid(ctx
, argc
+1, &id
);
2292 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2294 hres
= disp_call(ctx
, disp
, id
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2299 stack_popn(ctx
, argc
+2);
2300 return stack_push(ctx
, v
);
2303 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2307 return stack_push(ctx
, jsval_undefined());
2310 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2312 const unsigned arg
= get_op_uint(ctx
, 0);
2320 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2322 const unsigned arg
= get_op_uint(ctx
, 0);
2330 hres
= to_boolean(v
, &b
);
2342 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2344 const unsigned arg
= get_op_uint(ctx
, 0);
2348 stack_popn(ctx
, arg
);
2352 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2360 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2362 call_frame_t
*frame
= ctx
->call_ctx
;
2366 jsval_release(frame
->ret
);
2367 frame
->ret
= stack_pop(ctx
);
2371 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2373 static const op_func_t op_funcs
[] = {
2374 #define X(x,a,b,c) interp_##x,
2379 static const unsigned op_move
[] = {
2380 #define X(a,x,b,c) x,
2385 static void release_call_frame(call_frame_t
*frame
)
2387 if(frame
->variable_obj
)
2388 jsdisp_release(frame
->variable_obj
);
2390 IDispatch_Release(frame
->this_obj
);
2392 scope_release(frame
->scope
);
2393 jsval_release(frame
->ret
);
2397 static HRESULT
unwind_exception(script_ctx_t
*ctx
)
2399 call_frame_t
*frame
= ctx
->call_ctx
;
2400 except_frame_t
*except_frame
;
2405 except_frame
= frame
->except_frame
;
2406 frame
->except_frame
= except_frame
->next
;
2408 assert(except_frame
->stack_top
<= ctx
->stack_top
);
2409 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
2411 while(except_frame
->scope
!= frame
->scope
)
2412 scope_pop(&frame
->scope
);
2414 frame
->ip
= except_frame
->catch_off
;
2416 except_val
= ctx
->ei
.val
;
2417 ctx
->ei
.val
= jsval_undefined();
2420 ident
= except_frame
->ident
;
2421 heap_free(except_frame
);
2424 jsdisp_t
*scope_obj
;
2426 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
2427 if(SUCCEEDED(hres
)) {
2428 hres
= jsdisp_propput_name(scope_obj
, ident
, except_val
);
2430 jsdisp_release(scope_obj
);
2432 jsval_release(except_val
);
2436 hres
= scope_push(frame
->scope
, scope_obj
, to_disp(scope_obj
), &frame
->scope
);
2437 jsdisp_release(scope_obj
);
2439 hres
= stack_push(ctx
, except_val
);
2443 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2449 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, function_code_t
*func
, jsval_t
*ret
)
2451 call_frame_t
*frame
;
2453 HRESULT hres
= S_OK
;
2457 frame
= ctx
->call_ctx
;
2459 while(frame
->ip
!= -1) {
2460 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
2461 hres
= op_funcs
[op
](ctx
);
2463 TRACE("EXCEPTION %08x\n", hres
);
2465 if(!frame
->except_frame
)
2468 hres
= unwind_exception(ctx
);
2472 frame
->ip
+= op_move
[op
];
2476 assert(ctx
->call_ctx
== frame
);
2479 while(frame
->scope
!= frame
->base_scope
)
2480 scope_pop(&frame
->scope
);
2481 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
2484 assert(ctx
->stack_top
== frame
->stack_base
);
2485 assert(frame
->scope
== frame
->base_scope
);
2486 ctx
->call_ctx
= frame
->prev_frame
;
2488 if(SUCCEEDED(hres
)) {
2490 frame
->ret
= jsval_undefined();
2493 release_call_frame(frame
);
2497 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
2499 IBindEventHandler
*target
;
2505 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
2509 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2510 exprval_release(&exprval
);
2514 if(!is_object_instance(v
)) {
2515 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
2519 disp
= get_object(v
);
2520 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
2521 if(SUCCEEDED(hres
)) {
2522 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
2523 IBindEventHandler_Release(target
);
2525 WARN("BindEvent failed: %08x\n", hres
);
2527 FIXME("No IBindEventHandler, not yet supported binding\n");
2530 IDispatch_Release(disp
);
2534 static HRESULT
setup_call_frame(exec_ctx_t
*ctx
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
2535 IDispatch
*this_obj
, jsdisp_t
*variable_obj
)
2537 call_frame_t
*frame
;
2539 /* ECMA-262 3rd Edition 11.2.3.7 */
2543 jsthis
= iface_to_jsdisp((IUnknown
*)this_obj
);
2545 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
2547 jsdisp_release(jsthis
);
2551 frame
= heap_alloc_zero(sizeof(*frame
));
2553 return E_OUTOFMEMORY
;
2555 frame
->bytecode
= bytecode
;
2556 frame
->function
= function
;
2557 frame
->ip
= function
->instr_off
;
2558 frame
->stack_base
= ctx
->script
->stack_top
;
2559 frame
->ret
= jsval_undefined();
2562 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
2565 frame
->this_obj
= this_obj
;
2566 else if(ctx
->script
->host_global
)
2567 frame
->this_obj
= ctx
->script
->host_global
;
2569 frame
->this_obj
= to_disp(ctx
->script
->global
);
2570 IDispatch_AddRef(frame
->this_obj
);
2572 frame
->variable_obj
= jsdisp_addref(variable_obj
);
2574 frame
->exec_ctx
= ctx
;
2576 frame
->prev_frame
= ctx
->script
->call_ctx
;
2577 ctx
->script
->call_ctx
= frame
;
2581 HRESULT
exec_source(exec_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, scope_chain_t
*scope
,
2582 IDispatch
*this_obj
, jsdisp_t
*variable_obj
, jsval_t
*ret
)
2586 HRESULT hres
= S_OK
;
2588 for(i
= 0; i
< func
->func_cnt
; i
++) {
2591 if(!func
->funcs
[i
].name
)
2594 hres
= create_source_function(ctx
->script
, code
, func
->funcs
+i
, scope
, &func_obj
);
2598 if(func
->funcs
[i
].event_target
)
2599 hres
= bind_event_target(ctx
->script
, func
->funcs
+i
, func_obj
);
2601 hres
= jsdisp_propput_name(variable_obj
, func
->funcs
[i
].name
, jsval_obj(func_obj
));
2602 jsdisp_release(func_obj
);
2607 for(i
=0; i
< func
->var_cnt
; i
++) {
2608 if(!ctx
->is_global
|| !lookup_global_members(ctx
->script
, func
->variables
[i
], NULL
)) {
2611 hres
= jsdisp_get_id(variable_obj
, func
->variables
[i
], fdexNameEnsure
, &id
);
2617 hres
= setup_call_frame(ctx
, code
, func
, scope
, this_obj
, variable_obj
);
2621 hres
= enter_bytecode(ctx
->script
, func
, &val
);