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 HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
403 IObjectIdentity
*identity
;
404 IUnknown
*unk1
, *unk2
;
412 if(!disp1
|| !disp2
) {
417 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
421 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
423 IUnknown_Release(unk1
);
430 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
431 if(SUCCEEDED(hres
)) {
432 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
433 IObjectIdentity_Release(identity
);
440 IUnknown_Release(unk1
);
441 IUnknown_Release(unk2
);
445 /* ECMA-262 3rd Edition 11.9.6 */
446 static HRESULT
equal2_values(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
448 jsval_type_t type
= jsval_type(lval
);
452 if(type
!= jsval_type(rval
)) {
453 if(is_null_instance(lval
))
454 *ret
= is_null_instance(rval
);
466 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
468 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
471 *ret
= get_number(lval
) == get_number(rval
);
474 *ret
= !get_bool(lval
) == !get_bool(rval
);
477 FIXME("VARIANT not implemented\n");
484 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
490 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
491 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
492 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
493 if(SUCCEEDED(hres
)) {
495 exprval_set_idref(ret
, item
->disp
, id
);
504 /* ECMA-262 3rd Edition 10.1.4 */
505 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
507 scope_chain_t
*scope
;
512 TRACE("%s\n", debugstr_w(identifier
));
515 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
517 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
519 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
520 if(SUCCEEDED(hres
)) {
521 exprval_set_idref(ret
, scope
->obj
, id
);
527 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
528 if(SUCCEEDED(hres
)) {
529 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
533 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
534 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
541 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
542 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
544 WARN("GetItemInfo failed: %08x\n", hres
);
548 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
549 IUnknown_Release(unk
);
551 WARN("object does not implement IDispatch\n");
556 IDispatch_AddRef(item
->disp
);
557 ret
->type
= EXPRVAL_JSVAL
;
558 ret
->u
.val
= jsval_disp(item
->disp
);
563 if(lookup_global_members(ctx
, identifier
, ret
))
566 ret
->type
= EXPRVAL_INVALID
;
570 static inline BSTR
get_op_bstr(exec_ctx_t
*ctx
, int i
){
571 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].bstr
;
574 static inline unsigned get_op_uint(exec_ctx_t
*ctx
, int i
){
575 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].uint
;
578 static inline unsigned get_op_int(exec_ctx_t
*ctx
, int i
){
579 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].lng
;
582 static inline jsstr_t
*get_op_str(exec_ctx_t
*ctx
, int i
){
583 return ctx
->code
->instrs
[ctx
->ip
].u
.arg
[i
].str
;
586 static inline double get_op_double(exec_ctx_t
*ctx
){
587 return ctx
->code
->instrs
[ctx
->ip
].u
.dbl
;
590 /* ECMA-262 3rd Edition 12.2 */
591 static HRESULT
interp_var_set(exec_ctx_t
*ctx
)
593 const BSTR name
= get_op_bstr(ctx
, 0);
597 TRACE("%s\n", debugstr_w(name
));
599 val
= stack_pop(ctx
);
600 hres
= jsdisp_propput_name(ctx
->var_disp
, name
, val
);
605 /* ECMA-262 3rd Edition 12.6.4 */
606 static HRESULT
interp_forin(exec_ctx_t
*ctx
)
608 const HRESULT arg
= get_op_uint(ctx
, 0);
609 IDispatch
*var_obj
, *obj
= NULL
;
617 assert(is_number(stack_top(ctx
)));
618 id
= get_number(stack_top(ctx
));
620 var_obj
= stack_topn_objid(ctx
, 1, &var_id
);
622 FIXME("invalid ref\n");
626 if(is_object_instance(stack_topn(ctx
, 3)))
627 obj
= get_object(stack_topn(ctx
, 3));
630 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
631 if(SUCCEEDED(hres
)) {
632 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
634 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
635 IDispatchEx_Release(dispex
);
639 TRACE("No IDispatchEx\n");
646 str
= jsstr_alloc_len(name
, SysStringLen(name
));
649 return E_OUTOFMEMORY
;
652 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
654 hres
= disp_propput(ctx
->script
, var_obj
, var_id
, jsval_string(str
));
667 /* ECMA-262 3rd Edition 12.10 */
668 static HRESULT
interp_push_scope(exec_ctx_t
*ctx
)
677 hres
= to_object(ctx
->script
, v
, &disp
);
682 hres
= scope_push(ctx
->scope_chain
, to_jsdisp(disp
), disp
, &ctx
->scope_chain
);
683 IDispatch_Release(disp
);
687 /* ECMA-262 3rd Edition 12.10 */
688 static HRESULT
interp_pop_scope(exec_ctx_t
*ctx
)
692 scope_pop(&ctx
->scope_chain
);
696 /* ECMA-262 3rd Edition 12.13 */
697 static HRESULT
interp_case(exec_ctx_t
*ctx
)
699 const unsigned arg
= get_op_uint(ctx
, 0);
707 hres
= equal2_values(stack_top(ctx
), v
, &b
);
721 /* ECMA-262 3rd Edition 12.13 */
722 static HRESULT
interp_throw(exec_ctx_t
*ctx
)
726 jsval_release(ctx
->script
->ei
.val
);
727 ctx
->script
->ei
.val
= stack_pop(ctx
);
728 return DISP_E_EXCEPTION
;
731 static HRESULT
interp_throw_ref(exec_ctx_t
*ctx
)
733 const HRESULT arg
= get_op_uint(ctx
, 0);
735 TRACE("%08x\n", arg
);
737 return throw_reference_error(ctx
->script
, arg
, NULL
);
740 static HRESULT
interp_throw_type(exec_ctx_t
*ctx
)
742 const HRESULT hres
= get_op_uint(ctx
, 0);
743 jsstr_t
*str
= get_op_str(ctx
, 1);
746 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
748 ptr
= jsstr_flatten(str
);
749 return ptr
? throw_type_error(ctx
->script
, hres
, ptr
) : E_OUTOFMEMORY
;
752 /* ECMA-262 3rd Edition 12.14 */
753 static HRESULT
interp_push_except(exec_ctx_t
*ctx
)
755 const unsigned arg1
= get_op_uint(ctx
, 0);
756 const BSTR arg2
= get_op_bstr(ctx
, 1);
757 except_frame_t
*except
;
762 stack_top
= ctx
->top
;
767 hres
= stack_push(ctx
, jsval_bool(TRUE
));
770 hres
= stack_push(ctx
, jsval_bool(TRUE
));
775 except
= heap_alloc(sizeof(*except
));
777 return E_OUTOFMEMORY
;
779 except
->stack_top
= stack_top
;
780 except
->scope
= ctx
->scope_chain
;
781 except
->catch_off
= arg1
;
782 except
->ident
= arg2
;
783 except
->next
= ctx
->except_frame
;
784 ctx
->except_frame
= except
;
788 /* ECMA-262 3rd Edition 12.14 */
789 static HRESULT
interp_pop_except(exec_ctx_t
*ctx
)
791 except_frame_t
*except
;
795 except
= ctx
->except_frame
;
796 assert(except
!= NULL
);
798 ctx
->except_frame
= except
->next
;
803 /* ECMA-262 3rd Edition 12.14 */
804 static HRESULT
interp_end_finally(exec_ctx_t
*ctx
)
814 TRACE("passing exception\n");
816 ctx
->script
->ei
.val
= stack_pop(ctx
);
817 return DISP_E_EXCEPTION
;
824 /* ECMA-262 3rd Edition 13 */
825 static HRESULT
interp_func(exec_ctx_t
*ctx
)
827 unsigned func_idx
= get_op_uint(ctx
, 0);
831 TRACE("%d\n", func_idx
);
833 hres
= create_source_function(ctx
->script
, ctx
->code
, ctx
->func_code
->funcs
+func_idx
,
834 ctx
->scope_chain
, &dispex
);
838 return stack_push(ctx
, jsval_obj(dispex
));
841 /* ECMA-262 3rd Edition 11.2.1 */
842 static HRESULT
interp_array(exec_ctx_t
*ctx
)
853 namev
= stack_pop(ctx
);
855 hres
= stack_pop_object(ctx
, &obj
);
857 jsval_release(namev
);
861 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
862 jsval_release(namev
);
864 IDispatch_Release(obj
);
868 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, 0, &id
);
869 jsstr_release(name_str
);
870 if(SUCCEEDED(hres
)) {
871 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
872 }else if(hres
== DISP_E_UNKNOWNNAME
) {
873 v
= jsval_undefined();
876 IDispatch_Release(obj
);
880 return stack_push(ctx
, v
);
883 /* ECMA-262 3rd Edition 11.2.1 */
884 static HRESULT
interp_member(exec_ctx_t
*ctx
)
886 const BSTR arg
= get_op_bstr(ctx
, 0);
894 hres
= stack_pop_object(ctx
, &obj
);
898 hres
= disp_get_id(ctx
->script
, obj
, arg
, arg
, 0, &id
);
899 if(SUCCEEDED(hres
)) {
900 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
901 }else if(hres
== DISP_E_UNKNOWNNAME
) {
902 v
= jsval_undefined();
905 IDispatch_Release(obj
);
909 return stack_push(ctx
, v
);
912 /* ECMA-262 3rd Edition 11.2.1 */
913 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
915 const unsigned arg
= get_op_uint(ctx
, 0);
925 namev
= stack_pop(ctx
);
926 objv
= stack_pop(ctx
);
928 hres
= to_object(ctx
->script
, objv
, &obj
);
930 if(SUCCEEDED(hres
)) {
931 hres
= to_flat_string(ctx
->script
, namev
, &name_str
, &name
);
933 IDispatch_Release(obj
);
935 jsval_release(namev
);
939 hres
= disp_get_id(ctx
->script
, obj
, name
, NULL
, arg
, &id
);
940 jsstr_release(name_str
);
942 IDispatch_Release(obj
);
943 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
945 id
= JS_E_INVALID_PROPERTY
;
947 ERR("failed %08x\n", hres
);
952 return stack_push_objid(ctx
, obj
, id
);
955 /* ECMA-262 3rd Edition 11.2.1 */
956 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
965 disp
= stack_topn_objid(ctx
, 0, &id
);
967 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
969 hres
= disp_propget(ctx
->script
, disp
, id
, &v
);
973 return stack_push(ctx
, v
);
976 /* ECMA-262 3rd Edition 11.2.2 */
977 static HRESULT
interp_new(exec_ctx_t
*ctx
)
979 const unsigned argc
= get_op_uint(ctx
, 0);
985 constr
= stack_topn(ctx
, argc
);
987 /* NOTE: Should use to_object here */
990 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
991 else if(!is_object_instance(constr
))
992 return throw_type_error(ctx
->script
, JS_E_INVALID_ACTION
, NULL
);
993 else if(!get_object(constr
))
994 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
996 hres
= disp_call_value(ctx
->script
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
, argc
, stack_args(ctx
, argc
), &r
);
1000 stack_popn(ctx
, argc
+1);
1001 return stack_push(ctx
, r
);
1004 /* ECMA-262 3rd Edition 11.2.3 */
1005 static HRESULT
interp_call(exec_ctx_t
*ctx
)
1007 const unsigned argn
= get_op_uint(ctx
, 0);
1008 const int do_ret
= get_op_int(ctx
, 1);
1012 TRACE("%d %d\n", argn
, do_ret
);
1014 obj
= stack_topn(ctx
, argn
);
1015 if(!is_object_instance(obj
))
1016 return throw_type_error(ctx
->script
, JS_E_INVALID_PROPERTY
, NULL
);
1018 hres
= disp_call_value(ctx
->script
, get_object(obj
), NULL
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
),
1019 do_ret
? &r
: NULL
);
1023 stack_popn(ctx
, argn
+1);
1024 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1027 /* ECMA-262 3rd Edition 11.2.3 */
1028 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1030 const unsigned argn
= get_op_uint(ctx
, 0);
1031 const int do_ret
= get_op_int(ctx
, 1);
1037 TRACE("%d %d\n", argn
, do_ret
);
1039 obj
= stack_topn_objid(ctx
, argn
, &id
);
1041 return throw_type_error(ctx
->script
, id
, NULL
);
1043 hres
= disp_call(ctx
->script
, obj
, id
, DISPATCH_METHOD
, argn
, stack_args(ctx
, argn
), do_ret
? &r
: NULL
);
1047 stack_popn(ctx
, argn
+2);
1048 return do_ret
? stack_push(ctx
, r
) : S_OK
;
1052 /* ECMA-262 3rd Edition 11.1.1 */
1053 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1057 IDispatch_AddRef(ctx
->this_obj
);
1058 return stack_push(ctx
, jsval_disp(ctx
->this_obj
));
1061 /* ECMA-262 3rd Edition 10.1.4 */
1062 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1064 const BSTR arg
= get_op_bstr(ctx
, 0);
1069 TRACE("%s\n", debugstr_w(arg
));
1071 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1075 if(exprval
.type
== EXPRVAL_INVALID
)
1076 return throw_type_error(ctx
->script
, JS_E_UNDEFINED_VARIABLE
, arg
);
1078 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1079 exprval_release(&exprval
);
1083 return stack_push(ctx
, v
);
1086 /* ECMA-262 3rd Edition 10.1.4 */
1087 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1089 const BSTR arg
= get_op_bstr(ctx
, 0);
1090 const unsigned flags
= get_op_uint(ctx
, 1);
1094 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1096 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1100 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1103 hres
= jsdisp_get_id(ctx
->script
->global
, arg
, fdexNameEnsure
, &id
);
1107 exprval_set_idref(&exprval
, to_disp(ctx
->script
->global
), id
);
1110 if(exprval
.type
!= EXPRVAL_IDREF
) {
1111 WARN("invalid ref\n");
1112 exprval_release(&exprval
);
1113 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1116 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1119 /* ECMA-262 3rd Edition 7.8.1 */
1120 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1124 return stack_push(ctx
, jsval_null());
1127 /* ECMA-262 3rd Edition 7.8.2 */
1128 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1130 const int arg
= get_op_int(ctx
, 0);
1132 TRACE("%s\n", arg
? "true" : "false");
1134 return stack_push(ctx
, jsval_bool(arg
));
1137 /* ECMA-262 3rd Edition 7.8.3 */
1138 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1140 const int arg
= get_op_int(ctx
, 0);
1144 return stack_push(ctx
, jsval_number(arg
));
1147 /* ECMA-262 3rd Edition 7.8.3 */
1148 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1150 const double arg
= get_op_double(ctx
);
1152 TRACE("%lf\n", arg
);
1154 return stack_push(ctx
, jsval_number(arg
));
1157 /* ECMA-262 3rd Edition 7.8.4 */
1158 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1160 jsstr_t
*str
= get_op_str(ctx
, 0);
1162 TRACE("%s\n", debugstr_jsstr(str
));
1164 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1167 /* ECMA-262 3rd Edition 7.8 */
1168 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1170 jsstr_t
*source
= get_op_str(ctx
, 0);
1171 const unsigned flags
= get_op_uint(ctx
, 1);
1175 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1177 hres
= create_regexp(ctx
->script
, source
, flags
, ®exp
);
1181 return stack_push(ctx
, jsval_obj(regexp
));
1184 /* ECMA-262 3rd Edition 11.1.4 */
1185 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1187 const unsigned arg
= get_op_uint(ctx
, 0);
1195 hres
= create_array(ctx
->script
, arg
, &array
);
1201 val
= stack_pop(ctx
);
1202 hres
= jsdisp_propput_idx(array
, i
, val
);
1205 jsdisp_release(array
);
1210 return stack_push(ctx
, jsval_obj(array
));
1213 /* ECMA-262 3rd Edition 11.1.5 */
1214 static HRESULT
interp_new_obj(exec_ctx_t
*ctx
)
1221 hres
= create_object(ctx
->script
, NULL
, &obj
);
1225 return stack_push(ctx
, jsval_obj(obj
));
1228 /* ECMA-262 3rd Edition 11.1.5 */
1229 static HRESULT
interp_obj_prop(exec_ctx_t
*ctx
)
1231 const BSTR name
= get_op_bstr(ctx
, 0);
1236 TRACE("%s\n", debugstr_w(name
));
1238 val
= stack_pop(ctx
);
1240 assert(is_object_instance(stack_top(ctx
)));
1241 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1243 hres
= jsdisp_propput_name(obj
, name
, val
);
1248 /* ECMA-262 3rd Edition 11.11 */
1249 static HRESULT
interp_cnd_nz(exec_ctx_t
*ctx
)
1251 const unsigned arg
= get_op_uint(ctx
, 0);
1257 hres
= to_boolean(stack_top(ctx
), &b
);
1270 /* ECMA-262 3rd Edition 11.11 */
1271 static HRESULT
interp_cnd_z(exec_ctx_t
*ctx
)
1273 const unsigned arg
= get_op_uint(ctx
, 0);
1279 hres
= to_boolean(stack_top(ctx
), &b
);
1292 /* ECMA-262 3rd Edition 11.10 */
1293 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1300 hres
= stack_pop_int(ctx
, &r
);
1304 hres
= stack_pop_int(ctx
, &l
);
1308 return stack_push(ctx
, jsval_number(l
|r
));
1311 /* ECMA-262 3rd Edition 11.10 */
1312 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1319 hres
= stack_pop_int(ctx
, &r
);
1323 hres
= stack_pop_int(ctx
, &l
);
1327 return stack_push(ctx
, jsval_number(l
^r
));
1330 /* ECMA-262 3rd Edition 11.10 */
1331 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1338 hres
= stack_pop_int(ctx
, &r
);
1342 hres
= stack_pop_int(ctx
, &l
);
1346 return stack_push(ctx
, jsval_number(l
&r
));
1349 /* ECMA-262 3rd Edition 11.8.6 */
1350 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
1352 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1357 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1360 if(!is_object_instance(v
) || !get_object(v
)) {
1362 return throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1365 obj
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1366 IDispatch_Release(get_object(v
));
1368 FIXME("non-jsdisp objects not supported\n");
1372 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1373 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1375 hres
= throw_type_error(ctx
->script
, JS_E_FUNCTION_EXPECTED
, NULL
);
1377 jsdisp_release(obj
);
1383 if(is_object_instance(prot
)) {
1384 if(is_object_instance(v
))
1385 tmp
= iface_to_jsdisp((IUnknown
*)get_object(v
));
1386 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1387 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1393 jsdisp_release(tmp
);
1395 FIXME("prototype is not an object\n");
1399 jsval_release(prot
);
1404 return stack_push(ctx
, jsval_bool(ret
));
1407 /* ECMA-262 3rd Edition 11.8.7 */
1408 static HRESULT
interp_in(exec_ctx_t
*ctx
)
1419 obj
= stack_pop(ctx
);
1420 if(!is_object_instance(obj
) || !get_object(obj
)) {
1422 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1426 hres
= to_flat_string(ctx
->script
, v
, &jsstr
, &str
);
1429 IDispatch_Release(get_object(obj
));
1433 hres
= disp_get_id(ctx
->script
, get_object(obj
), str
, NULL
, 0, &id
);
1434 IDispatch_Release(get_object(obj
));
1435 jsstr_release(jsstr
);
1438 else if(hres
== DISP_E_UNKNOWNNAME
)
1443 return stack_push(ctx
, jsval_bool(ret
));
1446 /* ECMA-262 3rd Edition 11.6.1 */
1447 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1452 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1456 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1462 if(is_string(l
) || is_string(r
)) {
1463 jsstr_t
*lstr
, *rstr
= NULL
;
1465 hres
= to_string(ctx
, l
, &lstr
);
1467 hres
= to_string(ctx
, r
, &rstr
);
1469 if(SUCCEEDED(hres
)) {
1472 ret_str
= jsstr_concat(lstr
, rstr
);
1474 *ret
= jsval_string(ret_str
);
1476 hres
= E_OUTOFMEMORY
;
1479 jsstr_release(lstr
);
1481 jsstr_release(rstr
);
1485 hres
= to_number(ctx
, l
, &nl
);
1486 if(SUCCEEDED(hres
)) {
1487 hres
= to_number(ctx
, r
, &nr
);
1489 *ret
= jsval_number(nl
+nr
);
1498 /* ECMA-262 3rd Edition 11.6.1 */
1499 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1507 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1509 hres
= add_eval(ctx
->script
, l
, r
, &ret
);
1515 return stack_push(ctx
, ret
);
1518 /* ECMA-262 3rd Edition 11.6.2 */
1519 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1526 hres
= stack_pop_number(ctx
, &r
);
1530 hres
= stack_pop_number(ctx
, &l
);
1534 return stack_push(ctx
, jsval_number(l
-r
));
1537 /* ECMA-262 3rd Edition 11.5.1 */
1538 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1545 hres
= stack_pop_number(ctx
, &r
);
1549 hres
= stack_pop_number(ctx
, &l
);
1553 return stack_push(ctx
, jsval_number(l
*r
));
1556 /* ECMA-262 3rd Edition 11.5.2 */
1557 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1564 hres
= stack_pop_number(ctx
, &r
);
1568 hres
= stack_pop_number(ctx
, &l
);
1572 return stack_push(ctx
, jsval_number(l
/r
));
1575 /* ECMA-262 3rd Edition 11.5.3 */
1576 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1583 hres
= stack_pop_number(ctx
, &r
);
1587 hres
= stack_pop_number(ctx
, &l
);
1591 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1594 /* ECMA-262 3rd Edition 11.4.2 */
1595 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
1597 jsval_t objv
, namev
;
1605 namev
= stack_pop(ctx
);
1606 objv
= stack_pop(ctx
);
1608 hres
= to_object(ctx
->script
, objv
, &obj
);
1609 jsval_release(objv
);
1611 jsval_release(namev
);
1615 hres
= to_string(ctx
->script
, namev
, &name
);
1616 jsval_release(namev
);
1618 IDispatch_Release(obj
);
1622 hres
= disp_delete_name(ctx
->script
, obj
, name
, &ret
);
1623 IDispatch_Release(obj
);
1624 jsstr_release(name
);
1628 return stack_push(ctx
, jsval_bool(ret
));
1631 /* ECMA-262 3rd Edition 11.4.2 */
1632 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
1634 const BSTR arg
= get_op_bstr(ctx
, 0);
1639 TRACE("%s\n", debugstr_w(arg
));
1641 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1645 switch(exprval
.type
) {
1647 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1648 IDispatch_Release(exprval
.u
.idref
.disp
);
1652 case EXPRVAL_INVALID
:
1656 FIXME("Unsupported exprval\n");
1657 exprval_release(&exprval
);
1662 return stack_push(ctx
, jsval_bool(ret
));
1665 /* ECMA-262 3rd Edition 11.4.2 */
1666 static HRESULT
interp_void(exec_ctx_t
*ctx
)
1671 return stack_push(ctx
, jsval_undefined());
1674 /* ECMA-262 3rd Edition 11.4.3 */
1675 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1677 switch(jsval_type(v
)) {
1687 if(get_object(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)get_object(v
)))) {
1688 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1689 jsdisp_release(dispex
);
1705 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1712 /* ECMA-262 3rd Edition 11.4.3 */
1713 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
1723 obj
= stack_pop_objid(ctx
, &id
);
1725 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1727 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1728 IDispatch_Release(obj
);
1730 return stack_push_string(ctx
, unknownW
);
1732 hres
= typeof_string(v
, &ret
);
1737 return stack_push_string(ctx
, ret
);
1740 /* ECMA-262 3rd Edition 11.4.3 */
1741 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
1743 const BSTR arg
= get_op_bstr(ctx
, 0);
1749 TRACE("%s\n", debugstr_w(arg
));
1751 hres
= identifier_eval(ctx
->script
, arg
, &exprval
);
1755 if(exprval
.type
== EXPRVAL_INVALID
) {
1756 hres
= stack_push(ctx
, jsval_string(jsstr_undefined()));
1757 exprval_release(&exprval
);
1761 hres
= exprval_to_value(ctx
->script
, &exprval
, &v
);
1762 exprval_release(&exprval
);
1766 hres
= typeof_string(v
, &ret
);
1771 return stack_push_string(ctx
, ret
);
1774 /* ECMA-262 3rd Edition 11.4.3 */
1775 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
1784 hres
= typeof_string(v
, &ret
);
1789 return stack_push_string(ctx
, ret
);
1792 /* ECMA-262 3rd Edition 11.4.7 */
1793 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
1800 hres
= stack_pop_number(ctx
, &n
);
1804 return stack_push(ctx
, jsval_number(-n
));
1807 /* ECMA-262 3rd Edition 11.4.6 */
1808 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
1817 hres
= to_number(ctx
->script
, v
, &n
);
1822 return stack_push(ctx
, jsval_number(n
));
1825 /* ECMA-262 3rd Edition 11.3.1 */
1826 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
1828 const int arg
= get_op_int(ctx
, 0);
1836 obj
= stack_pop_objid(ctx
, &id
);
1838 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1840 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1841 if(SUCCEEDED(hres
)) {
1844 hres
= to_number(ctx
->script
, v
, &n
);
1846 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(n
+(double)arg
));
1850 IDispatch_Release(obj
);
1854 return stack_push(ctx
, v
);
1857 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1858 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
1860 const int arg
= get_op_int(ctx
, 0);
1869 obj
= stack_pop_objid(ctx
, &id
);
1871 return throw_type_error(ctx
->script
, JS_E_OBJECT_EXPECTED
, NULL
);
1873 hres
= disp_propget(ctx
->script
, obj
, id
, &v
);
1874 if(SUCCEEDED(hres
)) {
1877 hres
= to_number(ctx
->script
, v
, &n
);
1879 if(SUCCEEDED(hres
)) {
1880 ret
= n
+(double)arg
;
1881 hres
= disp_propput(ctx
->script
, obj
, id
, jsval_number(ret
));
1884 IDispatch_Release(obj
);
1888 return stack_push(ctx
, jsval_number(ret
));
1891 /* ECMA-262 3rd Edition 11.9.3 */
1892 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
1894 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
1895 return equal2_values(lval
, rval
, ret
);
1897 /* FIXME: NULL disps should be handled in more general way */
1898 if(is_object_instance(lval
) && !get_object(lval
))
1899 return equal_values(ctx
, jsval_null(), rval
, ret
);
1900 if(is_object_instance(rval
) && !get_object(rval
))
1901 return equal_values(ctx
, lval
, jsval_null(), ret
);
1903 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
1908 if(is_string(lval
) && is_number(rval
)) {
1912 hres
= to_number(ctx
, lval
, &n
);
1916 /* FIXME: optimize */
1917 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
1920 if(is_string(rval
) && is_number(lval
)) {
1924 hres
= to_number(ctx
, rval
, &n
);
1928 /* FIXME: optimize */
1929 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
1933 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
1936 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
1939 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
1943 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
1947 hres
= equal_values(ctx
, lval
, prim
, ret
);
1948 jsval_release(prim
);
1953 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
1957 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
1961 hres
= equal_values(ctx
, prim
, rval
, ret
);
1962 jsval_release(prim
);
1971 /* ECMA-262 3rd Edition 11.9.1 */
1972 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
1981 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1983 hres
= equal_values(ctx
->script
, l
, r
, &b
);
1989 return stack_push(ctx
, jsval_bool(b
));
1992 /* ECMA-262 3rd Edition 11.9.2 */
1993 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
2002 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2004 hres
= equal_values(ctx
->script
, l
, r
, &b
);
2010 return stack_push(ctx
, jsval_bool(!b
));
2013 /* ECMA-262 3rd Edition 11.9.4 */
2014 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2023 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2025 hres
= equal2_values(r
, l
, &b
);
2031 return stack_push(ctx
, jsval_bool(b
));
2034 /* ECMA-262 3rd Edition 11.9.5 */
2035 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2046 hres
= equal2_values(r
, l
, &b
);
2052 return stack_push(ctx
, jsval_bool(!b
));
2055 /* ECMA-262 3rd Edition 11.8.5 */
2056 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2062 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2066 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2072 if(is_string(l
) && is_string(r
)) {
2073 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2074 jsstr_release(get_string(l
));
2075 jsstr_release(get_string(r
));
2079 hres
= to_number(ctx
, l
, &ln
);
2082 hres
= to_number(ctx
, r
, &rn
);
2087 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2091 /* ECMA-262 3rd Edition 11.8.1 */
2092 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2101 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2103 hres
= less_eval(ctx
->script
, l
, r
, FALSE
, &b
);
2109 return stack_push(ctx
, jsval_bool(b
));
2112 /* ECMA-262 3rd Edition 11.8.1 */
2113 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2122 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2124 hres
= less_eval(ctx
->script
, r
, l
, TRUE
, &b
);
2130 return stack_push(ctx
, jsval_bool(b
));
2133 /* ECMA-262 3rd Edition 11.8.2 */
2134 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2143 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2145 hres
= less_eval(ctx
->script
, r
, l
, FALSE
, &b
);
2151 return stack_push(ctx
, jsval_bool(b
));
2154 /* ECMA-262 3rd Edition 11.8.4 */
2155 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2164 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2166 hres
= less_eval(ctx
->script
, l
, r
, TRUE
, &b
);
2172 return stack_push(ctx
, jsval_bool(b
));
2175 /* ECMA-262 3rd Edition 11.4.8 */
2176 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
2185 hres
= to_int32(ctx
->script
, v
, &i
);
2190 return stack_push(ctx
, jsval_number(~i
));
2193 /* ECMA-262 3rd Edition 11.4.9 */
2194 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2203 hres
= to_boolean(v
, &b
);
2208 return stack_push(ctx
, jsval_bool(!b
));
2211 /* ECMA-262 3rd Edition 11.7.1 */
2212 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
2218 hres
= stack_pop_uint(ctx
, &r
);
2222 hres
= stack_pop_int(ctx
, &l
);
2226 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2229 /* ECMA-262 3rd Edition 11.7.2 */
2230 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
2236 hres
= stack_pop_uint(ctx
, &r
);
2240 hres
= stack_pop_int(ctx
, &l
);
2244 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2247 /* ECMA-262 3rd Edition 11.7.3 */
2248 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
2253 hres
= stack_pop_uint(ctx
, &r
);
2257 hres
= stack_pop_uint(ctx
, &l
);
2261 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2264 /* ECMA-262 3rd Edition 11.13.1 */
2265 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
2276 disp
= stack_pop_objid(ctx
, &id
);
2279 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2282 hres
= disp_propput(ctx
->script
, disp
, id
, v
);
2283 IDispatch_Release(disp
);
2289 return stack_push(ctx
, v
);
2292 /* JScript extension */
2293 static HRESULT
interp_assign_call(exec_ctx_t
*ctx
)
2295 const unsigned argc
= get_op_uint(ctx
, 0);
2301 TRACE("%u\n", argc
);
2303 disp
= stack_topn_objid(ctx
, argc
+1, &id
);
2305 return throw_reference_error(ctx
->script
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2307 hres
= disp_call(ctx
->script
, disp
, id
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2312 stack_popn(ctx
, argc
+2);
2313 return stack_push(ctx
, v
);
2316 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
2320 return stack_push(ctx
, jsval_undefined());
2323 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
2325 const unsigned arg
= get_op_uint(ctx
, 0);
2333 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
2335 const unsigned arg
= get_op_uint(ctx
, 0);
2343 hres
= to_boolean(v
, &b
);
2355 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
2357 const unsigned arg
= get_op_uint(ctx
, 0);
2361 stack_popn(ctx
, arg
);
2365 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
2373 static HRESULT
interp_setret(exec_ctx_t
*ctx
)
2377 jsval_release(ctx
->ret
);
2378 ctx
->ret
= stack_pop(ctx
);
2382 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
2384 static const op_func_t op_funcs
[] = {
2385 #define X(x,a,b,c) interp_##x,
2390 static const unsigned op_move
[] = {
2391 #define X(a,x,b,c) x,
2396 static HRESULT
unwind_exception(exec_ctx_t
*ctx
)
2398 except_frame_t
*except_frame
;
2403 except_frame
= ctx
->except_frame
;
2404 ctx
->except_frame
= except_frame
->next
;
2406 assert(except_frame
->stack_top
<= ctx
->top
);
2407 stack_popn(ctx
, ctx
->top
- except_frame
->stack_top
);
2409 while(except_frame
->scope
!= ctx
->scope_chain
)
2410 scope_pop(&ctx
->scope_chain
);
2412 ctx
->ip
= except_frame
->catch_off
;
2414 except_val
= ctx
->script
->ei
.val
;
2415 ctx
->script
->ei
.val
= jsval_undefined();
2416 clear_ei(ctx
->script
);
2418 ident
= except_frame
->ident
;
2419 heap_free(except_frame
);
2422 jsdisp_t
*scope_obj
;
2424 hres
= create_dispex(ctx
->script
, NULL
, NULL
, &scope_obj
);
2425 if(SUCCEEDED(hres
)) {
2426 hres
= jsdisp_propput_name(scope_obj
, ident
, except_val
);
2428 jsdisp_release(scope_obj
);
2430 jsval_release(except_val
);
2434 hres
= scope_push(ctx
->scope_chain
, scope_obj
, to_disp(scope_obj
), &ctx
->scope_chain
);
2435 jsdisp_release(scope_obj
);
2437 hres
= stack_push(ctx
, except_val
);
2441 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2447 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, jsval_t
*ret
)
2449 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
2450 except_frame_t
*prev_except_frame
;
2451 function_code_t
*prev_func
;
2452 unsigned prev_ip
, prev_top
;
2453 scope_chain_t
*prev_scope
;
2454 bytecode_t
*prev_code
;
2456 HRESULT hres
= S_OK
;
2460 prev_top
= exec_ctx
->top
;
2461 prev_scope
= exec_ctx
->scope_chain
;
2462 prev_except_frame
= exec_ctx
->except_frame
;
2463 prev_ip
= exec_ctx
->ip
;
2464 prev_code
= exec_ctx
->code
;
2465 prev_func
= exec_ctx
->func_code
;
2466 exec_ctx
->ip
= func
->instr_off
;
2467 exec_ctx
->except_frame
= NULL
;
2468 exec_ctx
->code
= code
;
2469 exec_ctx
->func_code
= func
;
2471 while(exec_ctx
->ip
!= -1) {
2472 op
= code
->instrs
[exec_ctx
->ip
].op
;
2473 hres
= op_funcs
[op
](exec_ctx
);
2475 TRACE("EXCEPTION %08x\n", hres
);
2477 if(!exec_ctx
->except_frame
)
2480 hres
= unwind_exception(exec_ctx
);
2484 exec_ctx
->ip
+= op_move
[op
];
2488 exec_ctx
->ip
= prev_ip
;
2489 exec_ctx
->except_frame
= prev_except_frame
;
2490 exec_ctx
->code
= prev_code
;
2491 exec_ctx
->func_code
= prev_func
;
2494 while(exec_ctx
->scope_chain
!= prev_scope
)
2495 scope_pop(&exec_ctx
->scope_chain
);
2496 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
2500 assert(exec_ctx
->top
== prev_top
+1 || exec_ctx
->top
== prev_top
);
2501 assert(exec_ctx
->scope_chain
== prev_scope
);
2502 assert(exec_ctx
->top
== prev_top
);
2504 *ret
= exec_ctx
->ret
;
2505 exec_ctx
->ret
= jsval_undefined();
2509 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
2511 IBindEventHandler
*target
;
2517 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
2521 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2522 exprval_release(&exprval
);
2526 if(!is_object_instance(v
)) {
2527 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
2531 disp
= get_object(v
);
2532 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
2533 if(SUCCEEDED(hres
)) {
2534 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
2535 IBindEventHandler_Release(target
);
2537 WARN("BindEvent failed: %08x\n", hres
);
2539 FIXME("No IBindEventHandler, not yet supported binding\n");
2542 IDispatch_Release(disp
);
2546 HRESULT
exec_source(exec_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func
, BOOL from_eval
, jsval_t
*ret
)
2548 exec_ctx_t
*prev_ctx
;
2551 HRESULT hres
= S_OK
;
2553 for(i
= 0; i
< func
->func_cnt
; i
++) {
2556 if(!func
->funcs
[i
].name
)
2559 hres
= create_source_function(ctx
->script
, code
, func
->funcs
+i
, ctx
->scope_chain
, &func_obj
);
2563 if(func
->funcs
[i
].event_target
)
2564 hres
= bind_event_target(ctx
->script
, func
->funcs
+i
, func_obj
);
2566 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->funcs
[i
].name
, jsval_obj(func_obj
));
2567 jsdisp_release(func_obj
);
2572 for(i
=0; i
< func
->var_cnt
; i
++) {
2573 if(!ctx
->is_global
|| !lookup_global_members(ctx
->script
, func
->variables
[i
], NULL
)) {
2576 hres
= jsdisp_get_id(ctx
->var_disp
, func
->variables
[i
], fdexNameEnsure
, &id
);
2582 prev_ctx
= ctx
->script
->exec_ctx
;
2583 ctx
->script
->exec_ctx
= ctx
;
2585 hres
= enter_bytecode(ctx
->script
, code
, func
, &val
);
2586 assert(ctx
->script
->exec_ctx
== ctx
);
2587 ctx
->script
->exec_ctx
= prev_ctx
;