2 * Copyright 2008 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 _return_type_t
{
50 struct _except_frame_t
{
59 static HRESULT
expr_eval(script_ctx_t
*,expression_t
*,jsexcept_t
*,VARIANT
*);
61 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
63 if(!ctx
->stack_size
) {
64 ctx
->stack
= heap_alloc(16*sizeof(VARIANT
));
68 }else if(ctx
->stack_size
== ctx
->top
) {
71 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(VARIANT
));
77 ctx
->stack
= new_stack
;
81 ctx
->stack
[ctx
->top
++] = *v
;
85 static HRESULT
stack_push_bool(exec_ctx_t
*ctx
, BOOL b
)
90 V_BOOL(&v
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
91 return stack_push(ctx
, &v
);
94 static inline HRESULT
stack_push_number(exec_ctx_t
*ctx
, double number
)
98 num_set_val(&v
, number
);
99 return stack_push(ctx
, &v
);
102 static inline HRESULT
stack_push_int(exec_ctx_t
*ctx
, INT n
)
108 return stack_push(ctx
, &v
);
111 static inline HRESULT
stack_push_string(exec_ctx_t
*ctx
, const WCHAR
*str
)
116 V_BSTR(&v
) = SysAllocString(str
);
117 return V_BSTR(&v
) ? stack_push(ctx
, &v
) : E_OUTOFMEMORY
;
120 static HRESULT
stack_push_objid(exec_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
)
125 V_VT(&v
) = VT_DISPATCH
;
126 V_DISPATCH(&v
) = disp
;
127 hres
= stack_push(ctx
, &v
);
133 return stack_push(ctx
, &v
);
136 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
)
139 return ctx
->stack
+ ctx
->top
-1;
142 static inline VARIANT
*stack_topn(exec_ctx_t
*ctx
, unsigned n
)
144 assert(ctx
->top
> n
);
145 return ctx
->stack
+ ctx
->top
-1-n
;
148 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
151 return ctx
->stack
+ --ctx
->top
;
154 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
157 VariantClear(stack_pop(ctx
));
160 static HRESULT
stack_pop_number(exec_ctx_t
*ctx
, VARIANT
*r
)
166 hres
= to_number(ctx
->parser
->script
, v
, ctx
->ei
, r
);
171 static HRESULT
stack_pop_object(exec_ctx_t
*ctx
, IDispatch
**r
)
177 if(V_VT(v
) == VT_DISPATCH
) {
179 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_OBJECT_REQUIRED
, NULL
);
184 hres
= to_object(ctx
->parser
->script
, v
, r
);
189 static inline HRESULT
stack_pop_int(exec_ctx_t
*ctx
, INT
*r
)
191 return to_int32(ctx
->parser
->script
, stack_pop(ctx
), ctx
->ei
, r
);
194 static inline HRESULT
stack_pop_uint(exec_ctx_t
*ctx
, DWORD
*r
)
196 return to_uint32(ctx
->parser
->script
, stack_pop(ctx
), ctx
->ei
, r
);
199 static inline IDispatch
*stack_pop_objid(exec_ctx_t
*ctx
, DISPID
*id
)
201 assert(V_VT(stack_top(ctx
)) == VT_INT
&& V_VT(stack_topn(ctx
, 1)) == VT_DISPATCH
);
203 *id
= V_INT(stack_pop(ctx
));
204 return V_DISPATCH(stack_pop(ctx
));
207 static inline IDispatch
*stack_topn_objid(exec_ctx_t
*ctx
, unsigned n
, DISPID
*id
)
209 assert(V_VT(stack_topn(ctx
, n
)) == VT_INT
&& V_VT(stack_topn(ctx
, n
+1)) == VT_DISPATCH
);
211 *id
= V_INT(stack_topn(ctx
, n
));
212 return V_DISPATCH(stack_topn(ctx
, n
+1));
215 static void exprval_release(exprval_t
*val
)
218 case EXPRVAL_VARIANT
:
219 if(V_VT(&val
->u
.var
) != VT_EMPTY
)
220 VariantClear(&val
->u
.var
);
223 if(val
->u
.idref
.disp
)
224 IDispatch_Release(val
->u
.idref
.disp
);
226 case EXPRVAL_INVALID
:
231 /* ECMA-262 3rd Edition 8.7.1 */
232 static HRESULT
exprval_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
234 V_VT(ret
) = VT_EMPTY
;
237 case EXPRVAL_VARIANT
:
238 return VariantCopy(ret
, &val
->u
.var
);
240 if(!val
->u
.idref
.disp
) {
241 FIXME("throw ReferenceError\n");
245 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
, ei
, NULL
/*FIXME*/);
246 case EXPRVAL_INVALID
:
250 ERR("type %d\n", val
->type
);
254 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
256 if(val
->type
== EXPRVAL_VARIANT
) {
258 V_VT(&val
->u
.var
) = VT_EMPTY
;
262 return exprval_value(ctx
, val
, ei
, ret
);
265 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
267 val
->type
= EXPRVAL_IDREF
;
268 val
->u
.idref
.disp
= disp
;
269 val
->u
.idref
.id
= id
;
272 IDispatch_AddRef(disp
);
275 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*obj
, scope_chain_t
**ret
)
277 scope_chain_t
*new_scope
;
279 new_scope
= heap_alloc(sizeof(scope_chain_t
));
281 return E_OUTOFMEMORY
;
286 new_scope
->obj
= obj
;
290 new_scope
->next
= scope
;
292 new_scope
->next
= NULL
;
299 static void scope_pop(scope_chain_t
**scope
)
308 void scope_release(scope_chain_t
*scope
)
314 scope_release(scope
->next
);
316 jsdisp_release(scope
->obj
);
320 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, jsdisp_t
*var_disp
,
321 scope_chain_t
*scope
, BOOL is_global
, exec_ctx_t
**ret
)
325 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
327 return E_OUTOFMEMORY
;
330 ctx
->is_global
= is_global
;
333 ctx
->this_obj
= this_obj
;
334 else if(script_ctx
->host_global
)
335 ctx
->this_obj
= script_ctx
->host_global
;
337 ctx
->this_obj
= to_disp(script_ctx
->global
);
338 IDispatch_AddRef(ctx
->this_obj
);
340 jsdisp_addref(var_disp
);
341 ctx
->var_disp
= var_disp
;
345 ctx
->scope_chain
= scope
;
352 void exec_release(exec_ctx_t
*ctx
)
358 scope_release(ctx
->scope_chain
);
360 jsdisp_release(ctx
->var_disp
);
362 IDispatch_Release(ctx
->this_obj
);
363 heap_free(ctx
->stack
);
367 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, BSTR name
, DWORD flags
, DISPID
*id
)
372 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
374 TRACE("unsing IDispatch\n");
377 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
381 hres
= IDispatchEx_GetDispID(dispex
, name
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
382 IDispatchEx_Release(dispex
);
386 static inline BOOL
is_null(const VARIANT
*v
)
388 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
391 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
393 IObjectIdentity
*identity
;
394 IUnknown
*unk1
, *unk2
;
402 if(!disp1
|| !disp2
) {
407 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
411 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
413 IUnknown_Release(unk1
);
420 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
421 if(SUCCEEDED(hres
)) {
422 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
423 IObjectIdentity_Release(identity
);
430 IUnknown_Release(unk1
);
431 IUnknown_Release(unk2
);
435 /* ECMA-262 3rd Edition 11.9.6 */
436 static HRESULT
equal2_values(VARIANT
*lval
, VARIANT
*rval
, BOOL
*ret
)
440 if(V_VT(lval
) != V_VT(rval
)) {
441 if(is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
)))
442 *ret
= num_val(lval
) == num_val(rval
);
443 else if(is_null(lval
))
444 *ret
= is_null(rval
);
456 *ret
= V_I4(lval
) == V_I4(rval
);
459 *ret
= V_R8(lval
) == V_R8(rval
);
463 *ret
= SysStringLen(V_BSTR(rval
))?FALSE
:TRUE
;
464 else if(!V_BSTR(rval
))
465 *ret
= SysStringLen(V_BSTR(lval
))?FALSE
:TRUE
;
467 *ret
= !strcmpW(V_BSTR(lval
), V_BSTR(rval
));
470 return disp_cmp(V_DISPATCH(lval
), V_DISPATCH(rval
), ret
);
472 *ret
= !V_BOOL(lval
) == !V_BOOL(rval
);
475 FIXME("unimplemented vt %d\n", V_VT(lval
));
482 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
488 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
489 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
490 hres
= disp_get_id(ctx
, item
->disp
, identifier
, 0, &id
);
491 if(SUCCEEDED(hres
)) {
493 exprval_set_idref(ret
, item
->disp
, id
);
502 /* ECMA-262 3rd Edition 10.1.4 */
503 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
505 scope_chain_t
*scope
;
510 TRACE("%s\n", debugstr_w(identifier
));
512 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
513 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
514 if(SUCCEEDED(hres
)) {
515 exprval_set_idref(ret
, to_disp(scope
->obj
), id
);
520 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
521 if(SUCCEEDED(hres
)) {
522 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
526 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
527 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
534 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
535 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
537 WARN("GetItemInfo failed: %08x\n", hres
);
541 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
542 IUnknown_Release(unk
);
544 WARN("object does not implement IDispatch\n");
549 ret
->type
= EXPRVAL_VARIANT
;
550 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
551 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
552 IDispatch_AddRef(item
->disp
);
557 if(lookup_global_members(ctx
, identifier
, ret
))
560 if(flags
& fdexNameEnsure
) {
561 hres
= jsdisp_get_id(ctx
->global
, identifier
, fdexNameEnsure
, &id
);
565 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
569 ret
->type
= EXPRVAL_INVALID
;
573 /* ECMA-262 3rd Edition 12.2 */
574 static HRESULT
interp_var_set(exec_ctx_t
*ctx
)
576 const BSTR name
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
580 TRACE("%s\n", debugstr_w(name
));
583 hres
= jsdisp_propput_name(ctx
->var_disp
, name
, v
, ctx
->ei
, NULL
/*FIXME*/);
588 /* ECMA-262 3rd Edition 12.6.4 */
589 static HRESULT
interp_forin(exec_ctx_t
*ctx
)
591 const HRESULT arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
592 IDispatch
*var_obj
, *obj
= NULL
;
601 val
= stack_pop(ctx
);
603 assert(V_VT(stack_top(ctx
)) == VT_I4
);
604 id
= V_I4(stack_top(ctx
));
606 var_obj
= stack_topn_objid(ctx
, 1, &var_id
);
608 FIXME("invalid ref\n");
613 if(V_VT(stack_topn(ctx
, 3)) == VT_DISPATCH
)
614 obj
= V_DISPATCH(stack_topn(ctx
, 3));
617 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
618 if(SUCCEEDED(hres
)) {
619 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
621 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
622 IDispatchEx_Release(dispex
);
628 TRACE("No IDispatchEx\n");
637 V_I4(stack_top(ctx
)) = id
;
641 hres
= disp_propput(ctx
->parser
->script
, var_obj
, var_id
, &v
, ctx
->ei
, NULL
/*FIXME*/);
650 return stack_push(ctx
, val
);
655 /* ECMA-262 3rd Edition 12.7 */
656 HRESULT
continue_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
658 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
662 if(stat
->identifier
) {
663 FIXME("indentifier not implemented\n");
667 rt
->type
= RT_CONTINUE
;
668 V_VT(ret
) = VT_EMPTY
;
672 /* ECMA-262 3rd Edition 12.8 */
673 HRESULT
break_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
675 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
679 if(stat
->identifier
) {
680 FIXME("indentifier not implemented\n");
685 V_VT(ret
) = VT_EMPTY
;
689 /* ECMA-262 3rd Edition 12.9 */
690 HRESULT
return_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
692 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
698 hres
= expr_eval(ctx
, stat
->expr
, &rt
->ei
, ret
);
702 V_VT(ret
) = VT_EMPTY
;
705 TRACE("= %s\n", debugstr_variant(ret
));
706 rt
->type
= RT_RETURN
;
710 /* ECMA-262 3rd Edition 12.10 */
711 HRESULT
interp_push_scope(exec_ctx_t
*ctx
)
721 hres
= to_object(ctx
->parser
->script
, v
, &disp
);
726 obj
= to_jsdisp(disp
);
728 IDispatch_Release(disp
);
729 FIXME("disp is not jsdisp\n");
733 hres
= scope_push(ctx
->scope_chain
, obj
, &ctx
->scope_chain
);
738 /* ECMA-262 3rd Edition 12.10 */
739 HRESULT
interp_pop_scope(exec_ctx_t
*ctx
)
743 scope_pop(&ctx
->scope_chain
);
747 /* ECMA-262 3rd Edition 12.12 */
748 HRESULT
interp_label(exec_ctx_t
*ctx
)
754 /* ECMA-262 3rd Edition 12.13 */
755 static HRESULT
interp_case(exec_ctx_t
*ctx
)
757 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
765 hres
= equal2_values(stack_top(ctx
), v
, &b
);
779 /* ECMA-262 3rd Edition 12.13 */
780 static HRESULT
interp_throw(exec_ctx_t
*ctx
)
784 ctx
->rt
->ei
.var
= *stack_pop(ctx
);
785 return DISP_E_EXCEPTION
;
788 static HRESULT
interp_throw_ref(exec_ctx_t
*ctx
)
790 const HRESULT arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
792 TRACE("%08x\n", arg
);
794 return throw_reference_error(ctx
->parser
->script
, ctx
->ei
, arg
, NULL
);
797 static HRESULT
interp_throw_type(exec_ctx_t
*ctx
)
799 const HRESULT hres
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
800 const WCHAR
*str
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.str
;
802 TRACE("%08x %s\n", hres
, debugstr_w(str
));
804 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, hres
, str
);
807 /* ECMA-262 3rd Edition 12.14 */
808 static HRESULT
interp_push_except(exec_ctx_t
*ctx
)
810 const unsigned arg1
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
811 const BSTR arg2
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.bstr
;
812 except_frame_t
*except
;
817 stack_top
= ctx
->top
;
822 hres
= stack_push_bool(ctx
, TRUE
);
825 hres
= stack_push_bool(ctx
, TRUE
);
830 except
= heap_alloc(sizeof(*except
));
832 return E_OUTOFMEMORY
;
834 except
->stack_top
= stack_top
;
835 except
->scope
= ctx
->scope_chain
;
836 except
->catch_off
= arg1
;
837 except
->ident
= arg2
;
838 except
->next
= ctx
->except_frame
;
839 ctx
->except_frame
= except
;
843 /* ECMA-262 3rd Edition 12.14 */
844 static HRESULT
interp_pop_except(exec_ctx_t
*ctx
)
846 except_frame_t
*except
;
850 except
= ctx
->except_frame
;
851 assert(except
!= NULL
);
853 ctx
->except_frame
= except
->next
;
858 /* ECMA-262 3rd Edition 12.14 */
859 static HRESULT
interp_end_finally(exec_ctx_t
*ctx
)
867 assert(V_VT(stack_top(ctx
)) == VT_BOOL
);
868 if(!V_BOOL(stack_top(ctx
))) {
869 TRACE("passing exception\n");
873 ctx
->rt
->ei
.var
= *stack_pop(ctx
);
874 return DISP_E_EXCEPTION
;
878 return stack_push(ctx
, v
);
881 /* ECMA-262 3rd Edition 13 */
882 static HRESULT
interp_func(exec_ctx_t
*ctx
)
884 function_expression_t
*expr
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.func
;
891 hres
= create_source_function(ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->scope_chain
,
892 expr
->src_str
, expr
->src_len
, &dispex
);
896 var_set_jsdisp(&v
, dispex
);
897 return stack_push(ctx
, &v
);
900 /* ECMA-262 3rd Edition 11.2.1 */
901 static HRESULT
interp_array(exec_ctx_t
*ctx
)
911 namev
= stack_pop(ctx
);
913 hres
= stack_pop_object(ctx
, &obj
);
919 hres
= to_string(ctx
->parser
->script
, namev
, ctx
->ei
, &name
);
922 IDispatch_Release(obj
);
926 hres
= disp_get_id(ctx
->parser
->script
, obj
, name
, 0, &id
);
928 if(SUCCEEDED(hres
)) {
929 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, ctx
->ei
, NULL
/*FIXME*/);
930 }else if(hres
== DISP_E_UNKNOWNNAME
) {
934 IDispatch_Release(obj
);
938 return stack_push(ctx
, &v
);
941 /* ECMA-262 3rd Edition 11.2.1 */
942 static HRESULT
interp_member(exec_ctx_t
*ctx
)
944 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
952 hres
= stack_pop_object(ctx
, &obj
);
956 hres
= disp_get_id(ctx
->parser
->script
, obj
, arg
, 0, &id
);
957 if(SUCCEEDED(hres
)) {
959 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, ctx
->ei
, NULL
/*FIXME*/);
960 }else if(hres
== DISP_E_UNKNOWNNAME
) {
964 IDispatch_Release(obj
);
968 return stack_push(ctx
, &v
);
971 /* ECMA-262 3rd Edition 11.2.1 */
972 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
974 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
975 VARIANT
*objv
, *namev
;
983 namev
= stack_pop(ctx
);
984 objv
= stack_pop(ctx
);
986 hres
= to_object(ctx
->parser
->script
, objv
, &obj
);
988 if(SUCCEEDED(hres
)) {
989 hres
= to_string(ctx
->parser
->script
, namev
, ctx
->ei
, &name
);
991 IDispatch_Release(obj
);
997 hres
= disp_get_id(ctx
->parser
->script
, obj
, name
, arg
, &id
);
1000 IDispatch_Release(obj
);
1001 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1003 id
= JS_E_INVALID_PROPERTY
;
1009 return stack_push_objid(ctx
, obj
, id
);
1012 /* ECMA-262 3rd Edition 11.2.1 */
1013 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
1022 disp
= stack_topn_objid(ctx
, 0, &id
);
1024 return throw_reference_error(ctx
->parser
->script
, ctx
->ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
1026 hres
= disp_propget(ctx
->parser
->script
, disp
, id
, &v
, ctx
->ei
, NULL
/*FIXME*/);
1030 return stack_push(ctx
, &v
);
1033 static void jsstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, DISPPARAMS
*dp
)
1038 dp
->cArgs
= arg_cnt
;
1039 dp
->rgdispidNamedArgs
= NULL
;
1042 assert(ctx
->top
>= arg_cnt
);
1044 for(i
=1; i
*2 <= arg_cnt
; i
++) {
1045 tmp
= ctx
->stack
[ctx
->top
-i
];
1046 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
1047 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
1050 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-arg_cnt
;
1053 /* ECMA-262 3rd Edition 11.2.2 */
1054 static HRESULT
interp_new(exec_ctx_t
*ctx
)
1056 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1063 constr
= stack_topn(ctx
, arg
);
1065 /* NOTE: Should use to_object here */
1067 if(V_VT(constr
) == VT_NULL
)
1068 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1069 else if(V_VT(constr
) != VT_DISPATCH
)
1070 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_INVALID_ACTION
, NULL
);
1071 else if(!V_DISPATCH(constr
))
1072 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_INVALID_PROPERTY
, NULL
);
1074 jsstack_to_dp(ctx
, arg
, &dp
);
1075 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(constr
), DISPID_VALUE
,
1076 DISPATCH_CONSTRUCT
, &dp
, &v
, ctx
->ei
, NULL
/*FIXME*/);
1080 stack_popn(ctx
, arg
+1);
1081 return stack_push(ctx
, &v
);
1084 /* ECMA-262 3rd Edition 11.2.3 */
1085 static HRESULT
interp_call(exec_ctx_t
*ctx
)
1087 const unsigned argn
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1088 const int do_ret
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1093 TRACE("%d %d\n", argn
, do_ret
);
1095 objv
= stack_topn(ctx
, argn
);
1096 if(V_VT(objv
) != VT_DISPATCH
)
1097 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_INVALID_PROPERTY
, NULL
);
1099 jsstack_to_dp(ctx
, argn
, &dp
);
1100 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(objv
), DISPID_VALUE
, DISPATCH_METHOD
, &dp
,
1101 do_ret
? &v
: NULL
, ctx
->ei
, NULL
/*FIXME*/);
1105 stack_popn(ctx
, argn
+1);
1106 return do_ret
? stack_push(ctx
, &v
) : S_OK
;
1110 /* ECMA-262 3rd Edition 11.2.3 */
1111 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1113 const unsigned argn
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1114 const int do_ret
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1121 TRACE("%d %d\n", argn
, do_ret
);
1123 obj
= stack_topn_objid(ctx
, argn
, &id
);
1125 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, id
, NULL
);
1127 jsstack_to_dp(ctx
, argn
, &dp
);
1128 hres
= disp_call(ctx
->parser
->script
, obj
, id
, DISPATCH_METHOD
, &dp
, do_ret
? &v
: NULL
, ctx
->ei
, NULL
/*FIXME*/);
1132 stack_popn(ctx
, argn
+2);
1133 return do_ret
? stack_push(ctx
, &v
) : S_OK
;
1137 /* ECMA-262 3rd Edition 11.1.1 */
1138 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1144 V_VT(&v
) = VT_DISPATCH
;
1145 V_DISPATCH(&v
) = ctx
->this_obj
;
1146 IDispatch_AddRef(ctx
->this_obj
);
1147 return stack_push(ctx
, &v
);
1150 /* ECMA-262 3rd Edition 10.1.4 */
1151 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1153 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1158 TRACE("%s\n", debugstr_w(arg
));
1160 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, ctx
->ei
, &exprval
);
1164 if(exprval
.type
== EXPRVAL_INVALID
)
1165 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_UNDEFINED_VARIABLE
, arg
);
1167 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ctx
->ei
, &v
);
1168 exprval_release(&exprval
);
1172 return stack_push(ctx
, &v
);
1175 /* ECMA-262 3rd Edition 10.1.4 */
1176 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1178 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1179 const unsigned flags
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.uint
;
1183 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1185 hres
= identifier_eval(ctx
->parser
->script
, arg
, flags
, ctx
->ei
, &exprval
);
1189 if(exprval
.type
!= EXPRVAL_IDREF
) {
1190 WARN("invalid ref\n");
1191 exprval_release(&exprval
);
1192 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1195 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1198 /* ECMA-262 3rd Edition 7.8.1 */
1199 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1206 return stack_push(ctx
, &v
);
1209 /* ECMA-262 3rd Edition 7.8.2 */
1210 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1212 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1214 TRACE("%s\n", arg
? "true" : "false");
1216 return stack_push_bool(ctx
, arg
);
1219 /* ECMA-262 3rd Edition 7.8.3 */
1220 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1222 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1229 return stack_push(ctx
, &v
);
1232 /* ECMA-262 3rd Edition 7.8.3 */
1233 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1235 const double arg
= *ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.dbl
;
1238 TRACE("%lf\n", arg
);
1242 return stack_push(ctx
, &v
);
1245 /* ECMA-262 3rd Edition 7.8.4 */
1246 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1248 const WCHAR
*str
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.str
;
1251 TRACE("%s\n", debugstr_w(str
));
1254 V_BSTR(&v
) = SysAllocString(str
);
1256 return E_OUTOFMEMORY
;
1258 return stack_push(ctx
, &v
);
1261 /* ECMA-262 3rd Edition 7.8 */
1262 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1264 const WCHAR
*source
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.str
;
1265 const LONG flags
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1270 TRACE("%s %x\n", debugstr_w(source
), flags
);
1272 hres
= create_regexp(ctx
->parser
->script
, source
, strlenW(source
), flags
, ®exp
);
1276 var_set_jsdisp(&v
, regexp
);
1277 return stack_push(ctx
, &v
);
1280 /* ECMA-262 3rd Edition 11.1.4 */
1281 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1283 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1291 hres
= create_array(ctx
->parser
->script
, arg
, &array
);
1298 hres
= jsdisp_propput_idx(array
, i
, v
, ctx
->ei
, NULL
/*FIXME*/);
1301 jsdisp_release(array
);
1306 var_set_jsdisp(&r
, array
);
1307 return stack_push(ctx
, &r
);
1310 /* ECMA-262 3rd Edition 11.1.5 */
1311 HRESULT
interp_new_obj(exec_ctx_t
*ctx
)
1319 hres
= create_object(ctx
->parser
->script
, NULL
, &obj
);
1323 var_set_jsdisp(&v
, obj
);
1324 return stack_push(ctx
, &v
);
1327 /* ECMA-262 3rd Edition 11.1.5 */
1328 HRESULT
interp_obj_prop(exec_ctx_t
*ctx
)
1330 const BSTR name
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1335 TRACE("%s\n", debugstr_w(name
));
1339 assert(V_VT(stack_top(ctx
)) == VT_DISPATCH
);
1340 obj
= as_jsdisp(V_DISPATCH(stack_top(ctx
)));
1342 hres
= jsdisp_propput_name(obj
, name
, v
, ctx
->ei
, NULL
/*FIXME*/);
1347 /* ECMA-262 3rd Edition 11.11 */
1348 static HRESULT
interp_cnd_nz(exec_ctx_t
*ctx
)
1350 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1356 hres
= to_boolean(stack_top(ctx
), &b
);
1369 /* ECMA-262 3rd Edition 11.11 */
1370 static HRESULT
interp_cnd_z(exec_ctx_t
*ctx
)
1372 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1378 hres
= to_boolean(stack_top(ctx
), &b
);
1391 /* ECMA-262 3rd Edition 11.10 */
1392 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1399 hres
= stack_pop_int(ctx
, &r
);
1403 hres
= stack_pop_int(ctx
, &l
);
1407 return stack_push_int(ctx
, l
|r
);
1410 /* ECMA-262 3rd Edition 11.10 */
1411 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1418 hres
= stack_pop_int(ctx
, &r
);
1422 hres
= stack_pop_int(ctx
, &l
);
1426 return stack_push_int(ctx
, l
^r
);
1429 /* ECMA-262 3rd Edition 11.10 */
1430 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1437 hres
= stack_pop_int(ctx
, &r
);
1441 hres
= stack_pop_int(ctx
, &l
);
1445 return stack_push_int(ctx
, l
&r
);
1448 /* ECMA-262 3rd Edition 11.8.6 */
1449 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
1451 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1456 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1459 if(V_VT(v
) != VT_DISPATCH
|| !V_DISPATCH(v
)) {
1461 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
1464 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
1465 IDispatch_Release(V_DISPATCH(v
));
1467 FIXME("non-jsdisp objects not supported\n");
1471 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1472 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
, ctx
->ei
, NULL
/*FIXME*/);
1474 hres
= throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
1476 jsdisp_release(obj
);
1482 if(V_VT(&prot
) == VT_DISPATCH
) {
1483 if(V_VT(v
) == VT_DISPATCH
)
1484 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
1485 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1486 hres
= disp_cmp(V_DISPATCH(&prot
), to_disp(iter
), &ret
);
1492 jsdisp_release(tmp
);
1494 FIXME("prototype is not an object\n");
1498 VariantClear(&prot
);
1503 return stack_push_bool(ctx
, ret
);
1506 /* ECMA-262 3rd Edition 11.8.7 */
1507 static HRESULT
interp_in(exec_ctx_t
*ctx
)
1517 obj
= stack_pop(ctx
);
1520 if(V_VT(obj
) != VT_DISPATCH
|| !V_DISPATCH(obj
)) {
1523 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1526 hres
= to_string(ctx
->parser
->script
, v
, ctx
->ei
, &str
);
1529 IDispatch_Release(V_DISPATCH(obj
));
1533 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
1534 IDispatch_Release(V_DISPATCH(obj
));
1538 else if(hres
== DISP_E_UNKNOWNNAME
)
1543 return stack_push_bool(ctx
, ret
);
1546 /* ECMA-262 3rd Edition 11.6.1 */
1547 static HRESULT
add_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1552 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
1556 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
1562 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
1563 BSTR lstr
= NULL
, rstr
= NULL
;
1565 if(V_VT(&l
) == VT_BSTR
)
1568 hres
= to_string(ctx
, &l
, ei
, &lstr
);
1570 if(SUCCEEDED(hres
)) {
1571 if(V_VT(&r
) == VT_BSTR
)
1574 hres
= to_string(ctx
, &r
, ei
, &rstr
);
1577 if(SUCCEEDED(hres
)) {
1580 len1
= SysStringLen(lstr
);
1581 len2
= SysStringLen(rstr
);
1583 V_VT(retv
) = VT_BSTR
;
1584 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
1585 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
1586 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
1589 if(V_VT(&l
) != VT_BSTR
)
1590 SysFreeString(lstr
);
1591 if(V_VT(&r
) != VT_BSTR
)
1592 SysFreeString(rstr
);
1596 hres
= to_number(ctx
, &l
, ei
, &nl
);
1597 if(SUCCEEDED(hres
)) {
1598 hres
= to_number(ctx
, &r
, ei
, &nr
);
1600 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
1609 /* ECMA-262 3rd Edition 11.6.1 */
1610 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1612 VARIANT
*l
, *r
, ret
;
1618 TRACE("%s + %s\n", debugstr_variant(l
), debugstr_variant(r
));
1620 hres
= add_eval(ctx
->parser
->script
, l
, r
, ctx
->ei
, &ret
);
1626 return stack_push(ctx
, &ret
);
1629 /* ECMA-262 3rd Edition 11.6.2 */
1630 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1637 hres
= stack_pop_number(ctx
, &r
);
1641 hres
= stack_pop_number(ctx
, &l
);
1645 return stack_push_number(ctx
, num_val(&l
)-num_val(&r
));
1648 /* ECMA-262 3rd Edition 11.5.1 */
1649 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1656 hres
= stack_pop_number(ctx
, &r
);
1660 hres
= stack_pop_number(ctx
, &l
);
1664 return stack_push_number(ctx
, num_val(&l
)*num_val(&r
));
1667 /* ECMA-262 3rd Edition 11.5.2 */
1668 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1675 hres
= stack_pop_number(ctx
, &r
);
1679 hres
= stack_pop_number(ctx
, &l
);
1683 return stack_push_number(ctx
, num_val(&l
)/num_val(&r
));
1686 /* ECMA-262 3rd Edition 11.5.3 */
1687 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1694 hres
= stack_pop_number(ctx
, &r
);
1698 hres
= stack_pop_number(ctx
, &l
);
1702 return stack_push_number(ctx
, fmod(num_val(&l
), num_val(&r
)));
1705 /* ECMA-262 3rd Edition 11.4.2 */
1706 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
1708 VARIANT
*obj_var
, *name_var
;
1709 IDispatchEx
*dispex
;
1717 name_var
= stack_pop(ctx
);
1718 obj_var
= stack_pop(ctx
);
1720 hres
= to_object(ctx
->parser
->script
, obj_var
, &obj
);
1721 VariantClear(obj_var
);
1723 VariantClear(name_var
);
1727 hres
= to_string(ctx
->parser
->script
, name_var
, ctx
->ei
, &name
);
1728 VariantClear(name_var
);
1730 IDispatch_Release(obj
);
1734 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
1735 if(SUCCEEDED(hres
)) {
1736 hres
= IDispatchEx_DeleteMemberByName(dispex
, name
, make_grfdex(ctx
->parser
->script
, fdexNameCaseSensitive
));
1738 IDispatchEx_Release(dispex
);
1744 IDispatch_Release(obj
);
1745 SysFreeString(name
);
1749 return stack_push_bool(ctx
, ret
);
1752 /* ECMA-262 3rd Edition 11.4.2 */
1753 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
1755 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1756 IDispatchEx
*dispex
;
1761 TRACE("%s\n", debugstr_w(arg
));
1763 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, ctx
->ei
, &exprval
);
1767 if(exprval
.type
!= EXPRVAL_IDREF
) {
1768 FIXME("Unsupported exprval\n");
1769 exprval_release(&exprval
);
1773 hres
= IDispatch_QueryInterface(exprval
.u
.idref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
1774 IDispatch_Release(exprval
.u
.idref
.disp
);
1775 if(SUCCEEDED(hres
)) {
1776 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
1777 IDispatchEx_Release(dispex
);
1784 return stack_push_bool(ctx
, ret
);
1787 /* ECMA-262 3rd Edition 11.4.2 */
1788 static HRESULT
interp_void(exec_ctx_t
*ctx
)
1796 V_VT(&v
) = VT_EMPTY
;
1797 return stack_push(ctx
, &v
);
1800 /* ECMA-262 3rd Edition 11.4.3 */
1801 static HRESULT
typeof_string(VARIANT
*v
, const WCHAR
**ret
)
1823 if(V_DISPATCH(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
)))) {
1824 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1825 jsdisp_release(dispex
);
1832 FIXME("unhandled vt %d\n", V_VT(v
));
1839 /* ECMA-262 3rd Edition 11.4.3 */
1840 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
1848 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
1852 obj
= stack_pop_objid(ctx
, &id
);
1854 return stack_push_string(ctx
, undefinedW
);
1856 V_VT(&v
) = VT_EMPTY
;
1857 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, ctx
->ei
, NULL
/*FIXME*/);
1858 IDispatch_Release(obj
);
1860 return stack_push_string(ctx
, unknownW
);
1862 hres
= typeof_string(&v
, &ret
);
1867 return stack_push_string(ctx
, ret
);
1870 /* ECMA-262 3rd Edition 11.4.3 */
1871 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
1873 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1879 TRACE("%s\n", debugstr_w(arg
));
1881 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, ctx
->ei
, &exprval
);
1885 if(exprval
.type
== EXPRVAL_INVALID
) {
1886 hres
= stack_push_string(ctx
, undefinedW
);
1887 exprval_release(&exprval
);
1891 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ctx
->ei
, &v
);
1892 exprval_release(&exprval
);
1896 hres
= typeof_string(&v
, &ret
);
1901 return stack_push_string(ctx
, ret
);
1904 /* ECMA-262 3rd Edition 11.4.3 */
1905 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
1914 hres
= typeof_string(v
, &ret
);
1919 return stack_push_string(ctx
, ret
);
1922 /* ECMA-262 3rd Edition 11.4.7 */
1923 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
1930 hres
= stack_pop_number(ctx
, &n
);
1934 return stack_push_number(ctx
, -num_val(&n
));
1937 /* ECMA-262 3rd Edition 11.4.6 */
1938 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
1946 hres
= to_number(ctx
->parser
->script
, v
, ctx
->ei
, &num
);
1951 return stack_push(ctx
, &num
);
1954 /* ECMA-262 3rd Edition 11.3.1 */
1955 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
1957 const int arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1965 obj
= stack_pop_objid(ctx
, &id
);
1967 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1969 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, ctx
->ei
, NULL
/*FIXME*/);
1970 if(SUCCEEDED(hres
)) {
1973 hres
= to_number(ctx
->parser
->script
, &v
, ctx
->ei
, &n
);
1974 if(SUCCEEDED(hres
)) {
1975 num_set_val(&inc
, num_val(&n
)+(double)arg
);
1976 hres
= disp_propput(ctx
->parser
->script
, obj
, id
, &inc
, ctx
->ei
, NULL
/*FIXME*/);
1981 IDispatch_Release(obj
);
1985 return stack_push(ctx
, &v
);
1988 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1989 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
1991 const int arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1999 obj
= stack_pop_objid(ctx
, &id
);
2001 return throw_type_error(ctx
->parser
->script
, ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2003 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, ctx
->ei
, NULL
/*FIXME*/);
2004 if(SUCCEEDED(hres
)) {
2007 hres
= to_number(ctx
->parser
->script
, &v
, ctx
->ei
, &n
);
2009 if(SUCCEEDED(hres
)) {
2010 num_set_val(&v
, num_val(&n
)+(double)arg
);
2011 hres
= disp_propput(ctx
->parser
->script
, obj
, id
, &v
, ctx
->ei
, NULL
/*FIXME*/);
2014 IDispatch_Release(obj
);
2018 return stack_push(ctx
, &v
);
2021 /* ECMA-262 3rd Edition 11.9.3 */
2022 static HRESULT
equal_values(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2024 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2025 return equal2_values(lval
, rval
, ret
);
2027 /* FIXME: NULL disps should be handled in more general way */
2028 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2031 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2034 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2037 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2040 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2041 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2046 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2050 hres
= to_number(ctx
, lval
, ei
, &v
);
2054 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2057 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2061 hres
= to_number(ctx
, rval
, ei
, &v
);
2065 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2068 if(V_VT(rval
) == VT_BOOL
) {
2072 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2073 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2076 if(V_VT(lval
) == VT_BOOL
) {
2080 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2081 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2085 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2089 hres
= to_primitive(ctx
, rval
, ei
, &v
, NO_HINT
);
2093 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2100 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2104 hres
= to_primitive(ctx
, lval
, ei
, &v
, NO_HINT
);
2108 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2119 /* ECMA-262 3rd Edition 11.9.1 */
2120 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
2129 TRACE("%s == %s\n", debugstr_variant(l
), debugstr_variant(r
));
2131 hres
= equal_values(ctx
->parser
->script
, l
, r
, ctx
->ei
, &b
);
2137 return stack_push_bool(ctx
, b
);
2140 /* ECMA-262 3rd Edition 11.9.2 */
2141 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
2150 TRACE("%s != %s\n", debugstr_variant(l
), debugstr_variant(r
));
2152 hres
= equal_values(ctx
->parser
->script
, l
, r
, ctx
->ei
, &b
);
2158 return stack_push_bool(ctx
, !b
);
2161 /* ECMA-262 3rd Edition 11.9.4 */
2162 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2173 hres
= equal2_values(r
, l
, &b
);
2179 return stack_push_bool(ctx
, b
);
2182 /* ECMA-262 3rd Edition 11.9.5 */
2183 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2194 hres
= equal2_values(r
, l
, &b
);
2200 return stack_push_bool(ctx
, !b
);
2203 /* ECMA-262 3rd Edition 11.8.5 */
2204 static HRESULT
less_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2206 VARIANT l
, r
, ln
, rn
;
2209 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2213 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2219 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2220 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2221 SysFreeString(V_BSTR(&l
));
2222 SysFreeString(V_BSTR(&r
));
2226 hres
= to_number(ctx
, &l
, ei
, &ln
);
2229 hres
= to_number(ctx
, &r
, ei
, &rn
);
2234 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2235 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2237 DOUBLE ld
= num_val(&ln
);
2238 DOUBLE rd
= num_val(&rn
);
2240 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2246 /* ECMA-262 3rd Edition 11.8.1 */
2247 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2256 TRACE("%s < %s\n", debugstr_variant(l
), debugstr_variant(r
));
2258 hres
= less_eval(ctx
->parser
->script
, l
, r
, FALSE
, ctx
->ei
, &b
);
2264 return stack_push_bool(ctx
, b
);
2267 /* ECMA-262 3rd Edition 11.8.1 */
2268 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2277 TRACE("%s <= %s\n", debugstr_variant(l
), debugstr_variant(r
));
2279 hres
= less_eval(ctx
->parser
->script
, r
, l
, TRUE
, ctx
->ei
, &b
);
2285 return stack_push_bool(ctx
, b
);
2288 /* ECMA-262 3rd Edition 11.8.2 */
2289 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2298 TRACE("%s > %s\n", debugstr_variant(l
), debugstr_variant(r
));
2300 hres
= less_eval(ctx
->parser
->script
, r
, l
, FALSE
, ctx
->ei
, &b
);
2306 return stack_push_bool(ctx
, b
);
2309 /* ECMA-262 3rd Edition 11.8.4 */
2310 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2319 TRACE("%s >= %s\n", debugstr_variant(l
), debugstr_variant(r
));
2321 hres
= less_eval(ctx
->parser
->script
, l
, r
, TRUE
, ctx
->ei
, &b
);
2327 return stack_push_bool(ctx
, b
);
2330 /* ECMA-262 3rd Edition 11.4.8 */
2331 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
2340 hres
= to_int32(ctx
->parser
->script
, v
, ctx
->ei
, &i
);
2347 return stack_push(ctx
, &r
);
2350 /* ECMA-262 3rd Edition 11.4.9 */
2351 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2360 hres
= to_boolean(v
, &b
);
2365 return stack_push_bool(ctx
, !b
);
2368 /* ECMA-262 3rd Edition 11.7.1 */
2369 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
2375 hres
= stack_pop_uint(ctx
, &r
);
2379 hres
= stack_pop_int(ctx
, &l
);
2383 return stack_push_int(ctx
, l
<< (r
&0x1f));
2386 /* ECMA-262 3rd Edition 11.7.2 */
2387 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
2393 hres
= stack_pop_uint(ctx
, &r
);
2397 hres
= stack_pop_int(ctx
, &l
);
2401 return stack_push_int(ctx
, l
>> (r
&0x1f));
2404 /* ECMA-262 3rd Edition 11.7.3 */
2405 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
2410 hres
= stack_pop_uint(ctx
, &r
);
2414 hres
= stack_pop_uint(ctx
, &l
);
2418 return stack_push_int(ctx
, l
>> (r
&0x1f));
2421 /* ECMA-262 3rd Edition 11.13.1 */
2422 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
2432 disp
= stack_pop_objid(ctx
, &id
);
2435 return throw_reference_error(ctx
->parser
->script
, ctx
->ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2437 hres
= disp_propput(ctx
->parser
->script
, disp
, id
, v
, ctx
->ei
, NULL
/*FIXME*/);
2438 IDispatch_Release(disp
);
2444 return stack_push(ctx
, v
);
2447 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
2453 V_VT(&v
) = VT_EMPTY
;
2454 return stack_push(ctx
, &v
);
2457 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
2459 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
2467 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
2469 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
2477 hres
= to_boolean(v
, &b
);
2489 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
2497 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
2505 static HRESULT
interp_tree(exec_ctx_t
*ctx
)
2507 instr_t
*instr
= ctx
->parser
->code
->instrs
+ctx
->ip
;
2513 hres
= instr
->arg1
.stat
->eval(ctx
->parser
->script
, instr
->arg1
.stat
, ctx
->rt
, &v
);
2517 return stack_push(ctx
, &v
);
2520 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
2522 static const op_func_t op_funcs
[] = {
2523 #define X(x,a,b,c) interp_##x,
2528 static const unsigned op_move
[] = {
2529 #define X(a,x,b,c) x,
2534 static HRESULT
unwind_exception(exec_ctx_t
*ctx
)
2536 except_frame_t
*except_frame
;
2541 except_frame
= ctx
->except_frame
;
2542 ctx
->except_frame
= except_frame
->next
;
2544 assert(except_frame
->stack_top
<= ctx
->top
);
2545 stack_popn(ctx
, ctx
->top
- except_frame
->stack_top
);
2547 while(except_frame
->scope
!= ctx
->scope_chain
)
2548 scope_pop(&ctx
->scope_chain
);
2550 ctx
->ip
= except_frame
->catch_off
;
2552 assert(ctx
->rt
->type
== RT_NORMAL
);
2553 except_val
= ctx
->rt
->ei
.var
;
2554 memset(&ctx
->rt
->ei
, 0, sizeof(ctx
->rt
->ei
));
2556 ident
= except_frame
->ident
;
2557 heap_free(except_frame
);
2560 jsdisp_t
*scope_obj
;
2562 hres
= create_dispex(ctx
->parser
->script
, NULL
, NULL
, &scope_obj
);
2563 if(SUCCEEDED(hres
)) {
2564 hres
= jsdisp_propput_name(scope_obj
, ident
, &except_val
, &ctx
->rt
->ei
, NULL
/*FIXME*/);
2566 jsdisp_release(scope_obj
);
2568 VariantClear(&except_val
);
2572 hres
= scope_push(ctx
->scope_chain
, scope_obj
, &ctx
->scope_chain
);
2573 jsdisp_release(scope_obj
);
2577 hres
= stack_push(ctx
, &except_val
);
2581 hres
= stack_push_bool(ctx
, FALSE
);
2585 V_VT(&v
) = VT_EMPTY
;
2586 hres
= stack_push(ctx
, &v
);
2592 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, unsigned ip
, return_type_t
*rt
, VARIANT
*ret
)
2594 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
2595 except_frame_t
*prev_except_frame
;
2596 unsigned prev_ip
, prev_top
;
2597 scope_chain_t
*prev_scope
;
2598 return_type_t
*prev_rt
;
2599 jsexcept_t
*prev_ei
;
2601 HRESULT hres
= S_OK
;
2605 prev_rt
= exec_ctx
->rt
;
2606 prev_top
= exec_ctx
->top
;
2607 prev_scope
= exec_ctx
->scope_chain
;
2608 prev_except_frame
= exec_ctx
->except_frame
;
2609 prev_ip
= exec_ctx
->ip
;
2610 prev_ei
= exec_ctx
->ei
;
2613 exec_ctx
->ei
= &rt
->ei
;
2614 exec_ctx
->except_frame
= NULL
;
2616 while(exec_ctx
->ip
!= -1 && exec_ctx
->rt
->type
== RT_NORMAL
) {
2617 op
= exec_ctx
->parser
->code
->instrs
[exec_ctx
->ip
].op
;
2618 hres
= op_funcs
[op
](exec_ctx
);
2620 TRACE("EXCEPTION\n");
2622 if(!exec_ctx
->except_frame
)
2625 hres
= unwind_exception(exec_ctx
);
2629 exec_ctx
->ip
+= op_move
[op
];
2633 exec_ctx
->rt
= prev_rt
;
2634 exec_ctx
->ip
= prev_ip
;
2635 exec_ctx
->ei
= prev_ei
;
2636 exec_ctx
->except_frame
= prev_except_frame
;
2638 if(FAILED(hres
) || rt
->type
!= RT_NORMAL
) {
2639 while(exec_ctx
->scope_chain
!= prev_scope
)
2640 scope_pop(&exec_ctx
->scope_chain
);
2643 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
2647 assert(exec_ctx
->top
== prev_top
+1 || exec_ctx
->top
== prev_top
);
2648 assert(exec_ctx
->scope_chain
== prev_scope
);
2650 if(exec_ctx
->top
== prev_top
)
2651 V_VT(ret
) = VT_EMPTY
;
2653 *ret
= *stack_pop(exec_ctx
);
2657 static HRESULT
expr_eval(script_ctx_t
*ctx
, expression_t
*expr
, jsexcept_t
*ei
, VARIANT
*ret
)
2659 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
2660 unsigned prev_ip
, prev_top
;
2661 jsexcept_t
*prev_ei
;
2663 HRESULT hres
= S_OK
;
2667 if(expr
->instr_off
== -1) {
2668 hres
= compile_subscript(ctx
->exec_ctx
->parser
, expr
, &expr
->instr_off
);
2673 prev_top
= exec_ctx
->top
;
2674 prev_ip
= exec_ctx
->ip
;
2675 prev_ei
= exec_ctx
->ei
;
2676 exec_ctx
->ip
= expr
->instr_off
;
2679 while(exec_ctx
->ip
!= -1) {
2680 op
= exec_ctx
->parser
->code
->instrs
[exec_ctx
->ip
].op
;
2681 hres
= op_funcs
[op
](exec_ctx
);
2684 exec_ctx
->ip
+= op_move
[op
];
2687 exec_ctx
->ip
= prev_ip
;
2688 exec_ctx
->ei
= prev_ei
;
2691 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
2695 assert(exec_ctx
->top
== prev_top
+1);
2697 if(exec_ctx
->top
== prev_top
)
2698 V_VT(ret
) = VT_EMPTY
;
2700 *ret
= *stack_pop(exec_ctx
);
2704 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, BOOL from_eval
,
2705 jsexcept_t
*ei
, VARIANT
*retv
)
2707 script_ctx_t
*script
= parser
->script
;
2708 function_declaration_t
*func
;
2709 parser_ctx_t
*prev_parser
;
2712 exec_ctx_t
*prev_ctx
;
2714 HRESULT hres
= S_OK
;
2716 for(func
= source
->functions
; func
; func
= func
->next
) {
2720 hres
= create_source_function(parser
, func
->expr
->parameter_list
, func
->expr
->source_elements
,
2721 ctx
->scope_chain
, func
->expr
->src_str
, func
->expr
->src_len
, &func_obj
);
2725 var_set_jsdisp(&var
, func_obj
);
2726 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->expr
->identifier
, &var
, ei
, NULL
);
2727 jsdisp_release(func_obj
);
2732 for(var
= source
->variables
; var
; var
= var
->next
) {
2736 name
= SysAllocString(var
->identifier
);
2738 return E_OUTOFMEMORY
;
2740 if(!ctx
->is_global
|| !lookup_global_members(parser
->script
, name
, NULL
))
2741 hres
= jsdisp_get_id(ctx
->var_disp
, var
->identifier
, fdexNameEnsure
, &id
);
2742 SysFreeString(name
);
2747 prev_ctx
= script
->exec_ctx
;
2748 script
->exec_ctx
= ctx
;
2750 prev_parser
= ctx
->parser
;
2751 ctx
->parser
= parser
;
2753 V_VT(&val
) = VT_EMPTY
;
2754 memset(&rt
, 0, sizeof(rt
));
2755 rt
.type
= RT_NORMAL
;
2757 if(source
->statement
) {
2758 if(source
->statement
->instr_off
== -1)
2759 hres
= compile_subscript_stat(ctx
->parser
, source
->statement
, &source
->statement
->instr_off
);
2761 hres
= enter_bytecode(script
, source
->statement
->instr_off
, &rt
, &val
);
2764 script
->exec_ctx
= prev_ctx
;
2765 ctx
->parser
= prev_parser
;
2767 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
2768 FIXME("wrong rt %d\n", rt
.type
);
2778 if(!retv
|| (!from_eval
&& rt
.type
!= RT_RETURN
))