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 #define EXPR_NOVAL 0x0001
34 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
35 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
36 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
37 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
38 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
39 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
40 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
42 struct _return_type_t {
52 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
54 return stat->eval(ctx, stat, rt, ret);
57 static inline HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
59 return expr->eval(ctx, expr, flags, ei, ret);
62 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
64 if(!ctx->stack_size) {
65 ctx->stack = heap_alloc(16*sizeof(VARIANT));
69 }else if(ctx->stack_size == ctx->top) {
72 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
78 ctx->stack = new_stack;
82 ctx->stack[ctx->top++] = *v;
86 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
91 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
92 return stack_push(ctx, &v);
95 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
99 num_set_val(&v, number);
100 return stack_push(ctx, &v);
103 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
109 return stack_push(ctx, &v);
112 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
117 V_BSTR(&v) = SysAllocString(str);
118 return V_BSTR(&v) ? stack_push(ctx, &v) : E_OUTOFMEMORY;
121 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
126 V_VT(&v) = VT_DISPATCH;
127 V_DISPATCH(&v) = disp;
128 hres = stack_push(ctx, &v);
134 return stack_push(ctx, &v);
137 static inline VARIANT *stack_top(exec_ctx_t *ctx)
140 return ctx->stack + ctx->top-1;
143 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
145 assert(ctx->top > n);
146 return ctx->stack + ctx->top-1-n;
149 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
152 return ctx->stack + --ctx->top;
155 static void stack_popn(exec_ctx_t *ctx, unsigned n)
158 VariantClear(stack_pop(ctx));
161 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
167 hres = to_number(ctx->parser->script, v, &ctx->ei, r);
172 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
178 if(V_VT(v) == VT_DISPATCH) {
180 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
185 hres = to_object(ctx->parser->script, v, r);
190 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
192 return to_int32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
195 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
197 return to_uint32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
200 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
202 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
204 *id = V_INT(stack_pop(ctx));
205 return V_DISPATCH(stack_pop(ctx));
208 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
210 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
212 *id = V_INT(stack_topn(ctx, n));
213 return V_DISPATCH(stack_topn(ctx, n+1));
216 static void exprval_release(exprval_t *val)
219 case EXPRVAL_VARIANT:
220 if(V_VT(&val->u.var) != VT_EMPTY)
221 VariantClear(&val->u.var);
224 if(val->u.idref.disp)
225 IDispatch_Release(val->u.idref.disp);
227 case EXPRVAL_INVALID:
228 SysFreeString(val->u.identifier);
232 /* ECMA-262 3rd Edition 8.7.1 */
233 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
235 V_VT(ret) = VT_EMPTY;
238 case EXPRVAL_VARIANT:
239 return VariantCopy(ret, &val->u.var);
241 if(!val->u.idref.disp) {
242 FIXME("throw ReferenceError\n");
246 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
247 case EXPRVAL_INVALID:
248 return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
251 ERR("type %d\n", val->type);
255 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
257 if(val->type == EXPRVAL_VARIANT) {
259 V_VT(&val->u.var) = VT_EMPTY;
263 return exprval_value(ctx, val, ei, ret);
266 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
268 if(exprval->type != EXPRVAL_VARIANT) {
272 hres = exprval_to_value(ctx, exprval, ei, &val);
276 hres = to_boolean(&val, b);
281 return to_boolean(&exprval->u.var, b);
284 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
286 val->type = EXPRVAL_IDREF;
287 val->u.idref.disp = disp;
288 val->u.idref.id = id;
291 IDispatch_AddRef(disp);
294 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
296 scope_chain_t *new_scope;
298 new_scope = heap_alloc(sizeof(scope_chain_t));
300 return E_OUTOFMEMORY;
305 new_scope->obj = obj;
309 new_scope->next = scope;
311 new_scope->next = NULL;
318 static void scope_pop(scope_chain_t **scope)
327 void scope_release(scope_chain_t *scope)
333 scope_release(scope->next);
335 jsdisp_release(scope->obj);
339 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
340 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
344 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
346 return E_OUTOFMEMORY;
349 ctx->is_global = is_global;
352 ctx->this_obj = this_obj;
353 else if(script_ctx->host_global)
354 ctx->this_obj = script_ctx->host_global;
356 ctx->this_obj = to_disp(script_ctx->global);
357 IDispatch_AddRef(ctx->this_obj);
359 jsdisp_addref(var_disp);
360 ctx->var_disp = var_disp;
364 ctx->scope_chain = scope;
371 void exec_release(exec_ctx_t *ctx)
377 scope_release(ctx->scope_chain);
379 jsdisp_release(ctx->var_disp);
381 IDispatch_Release(ctx->this_obj);
382 heap_free(ctx->stack);
386 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
391 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
393 TRACE("unsing IDispatch\n");
396 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
400 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
401 IDispatchEx_Release(dispex);
405 /* ECMA-262 3rd Edition 8.7.2 */
406 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
408 if(ref->type != EXPRVAL_IDREF)
409 return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
411 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
414 static inline BOOL is_null(const VARIANT *v)
416 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
419 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
421 IObjectIdentity *identity;
422 IUnknown *unk1, *unk2;
430 if(!disp1 || !disp2) {
435 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
439 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
441 IUnknown_Release(unk1);
448 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
449 if(SUCCEEDED(hres)) {
450 hres = IObjectIdentity_IsEqualObject(identity, unk2);
451 IObjectIdentity_Release(identity);
458 IUnknown_Release(unk1);
459 IUnknown_Release(unk2);
463 /* ECMA-262 3rd Edition 11.9.6 */
464 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
468 if(V_VT(lval) != V_VT(rval)) {
469 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
470 *ret = num_val(lval) == num_val(rval);
471 else if(is_null(lval))
472 *ret = is_null(rval);
484 *ret = V_I4(lval) == V_I4(rval);
487 *ret = V_R8(lval) == V_R8(rval);
491 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
492 else if(!V_BSTR(rval))
493 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
495 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
498 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
500 *ret = !V_BOOL(lval) == !V_BOOL(rval);
503 FIXME("unimplemented vt %d\n", V_VT(lval));
510 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
512 switch(literal->type) {
518 V_I4(v) = literal->u.lval;
522 V_R8(v) = literal->u.dval;
525 BSTR str = SysAllocString(literal->u.wstr);
527 return E_OUTOFMEMORY;
535 V_BOOL(v) = literal->u.bval;
541 hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
542 literal->u.regexp.flags, ®exp);
546 var_set_jsdisp(v, regexp);
553 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
559 for(item = ctx->named_items; item; item = item->next) {
560 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
561 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
562 if(SUCCEEDED(hres)) {
564 exprval_set_idref(ret, item->disp, id);
573 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
574 jsexcept_t *ei, VARIANT *retv)
576 script_ctx_t *script = parser->script;
577 function_declaration_t *func;
578 parser_ctx_t *prev_parser;
582 exec_ctx_t *prev_ctx;
586 for(func = source->functions; func; func = func->next) {
590 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
591 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
595 var_set_jsdisp(&var, func_obj);
596 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
597 jsdisp_release(func_obj);
602 for(var = source->variables; var; var = var->next) {
606 name = SysAllocString(var->identifier);
608 return E_OUTOFMEMORY;
610 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
611 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
617 prev_ctx = script->exec_ctx;
618 script->exec_ctx = ctx;
620 prev_parser = ctx->parser;
621 ctx->parser = parser;
623 V_VT(&val) = VT_EMPTY;
624 memset(&rt, 0, sizeof(rt));
627 for(stat = source->statement; stat; stat = stat->next) {
628 hres = stat_eval(script, stat, &rt, &tmp);
634 if(rt.type != RT_NORMAL)
638 script->exec_ctx = prev_ctx;
639 ctx->parser = prev_parser;
641 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
642 FIXME("wrong rt %d\n", rt.type);
652 if(!retv || (!from_eval && rt.type != RT_RETURN))
659 /* ECMA-262 3rd Edition 10.1.4 */
660 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
662 scope_chain_t *scope;
667 TRACE("%s\n", debugstr_w(identifier));
669 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
670 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
671 if(SUCCEEDED(hres)) {
672 exprval_set_idref(ret, to_disp(scope->obj), id);
677 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
678 if(SUCCEEDED(hres)) {
679 exprval_set_idref(ret, to_disp(ctx->global), id);
683 for(item = ctx->named_items; item; item = item->next) {
684 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
691 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
692 SCRIPTINFO_IUNKNOWN, &unk, NULL);
694 WARN("GetItemInfo failed: %08x\n", hres);
698 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
699 IUnknown_Release(unk);
701 WARN("object does not implement IDispatch\n");
706 ret->type = EXPRVAL_VARIANT;
707 V_VT(&ret->u.var) = VT_DISPATCH;
708 V_DISPATCH(&ret->u.var) = item->disp;
709 IDispatch_AddRef(item->disp);
714 if(lookup_global_members(ctx, identifier, ret))
717 if(flags & fdexNameEnsure) {
718 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
722 exprval_set_idref(ret, to_disp(ctx->global), id);
726 ret->type = EXPRVAL_INVALID;
727 ret->u.identifier = SysAllocString(identifier);
728 if(!ret->u.identifier)
729 return E_OUTOFMEMORY;
734 /* ECMA-262 3rd Edition 12.1 */
735 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
737 block_statement_t *stat = (block_statement_t*)_stat;
744 V_VT(&val) = VT_EMPTY;
745 for(iter = stat->stat_list; iter; iter = iter->next) {
746 hres = stat_eval(ctx, iter, rt, &tmp);
752 if(rt->type != RT_NORMAL)
765 /* ECMA-262 3rd Edition 12.2 */
766 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
768 variable_declaration_t *iter;
771 for(iter = var_list; iter; iter = iter->next) {
778 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
782 hres = exprval_to_value(ctx, &exprval, ei, &val);
783 exprval_release(&exprval);
787 hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
796 /* ECMA-262 3rd Edition 12.2 */
797 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
799 var_statement_t *stat = (var_statement_t*)_stat;
804 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
808 V_VT(ret) = VT_EMPTY;
812 /* ECMA-262 3rd Edition 12.3 */
813 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
817 V_VT(ret) = VT_EMPTY;
821 /* ECMA-262 3rd Edition 12.4 */
822 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
824 expression_statement_t *stat = (expression_statement_t*)_stat;
831 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
835 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
836 exprval_release(&exprval);
841 TRACE("= %s\n", debugstr_variant(ret));
845 /* ECMA-262 3rd Edition 12.5 */
846 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
848 if_statement_t *stat = (if_statement_t*)_stat;
855 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
859 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
860 exprval_release(&exprval);
865 hres = stat_eval(ctx, stat->if_stat, rt, ret);
866 else if(stat->else_stat)
867 hres = stat_eval(ctx, stat->else_stat, rt, ret);
869 V_VT(ret) = VT_EMPTY;
874 /* ECMA-262 3rd Edition 12.6.2 */
875 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
877 while_statement_t *stat = (while_statement_t*)_stat;
886 V_VT(&val) = VT_EMPTY;
887 test_expr = !stat->do_while;
891 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
895 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
896 exprval_release(&exprval);
897 if(FAILED(hres) || !b)
903 hres = stat_eval(ctx, stat->statement, rt, &tmp);
910 if(rt->type == RT_CONTINUE)
911 rt->type = RT_NORMAL;
912 if(rt->type != RT_NORMAL)
921 if(rt->type == RT_BREAK)
922 rt->type = RT_NORMAL;
928 /* ECMA-262 3rd Edition 12.6.3 */
929 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
931 for_statement_t *stat = (for_statement_t*)_stat;
932 VARIANT val, tmp, retv;
939 if(stat->variable_list) {
940 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
943 }else if(stat->begin_expr) {
944 hres = expr_eval(ctx, stat->begin_expr, 0, &rt->ei, &exprval);
948 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
949 exprval_release(&exprval);
956 V_VT(&retv) = VT_EMPTY;
960 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
964 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
965 exprval_release(&exprval);
966 if(FAILED(hres) || !b)
970 hres = stat_eval(ctx, stat->statement, rt, &tmp);
977 if(rt->type == RT_CONTINUE)
978 rt->type = RT_NORMAL;
979 else if(rt->type != RT_NORMAL)
983 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
987 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
988 exprval_release(&exprval);
1001 if(rt->type == RT_BREAK)
1002 rt->type = RT_NORMAL;
1008 static HRESULT array_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
1009 static HRESULT member_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
1010 static HRESULT identifier_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
1012 /* ECMA-262 3rd Edition 12.6.4 */
1013 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1015 forin_statement_t *stat = (forin_statement_t*)_stat;
1016 VARIANT val, name, retv, tmp;
1017 DISPID id = DISPID_STARTENUM;
1018 BSTR str, identifier = NULL;
1019 IDispatchEx *in_obj;
1025 if(stat->variable) {
1026 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
1031 hres = expr_eval(ctx, stat->in_expr, 0, &rt->ei, &exprval);
1035 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1036 exprval_release(&exprval);
1040 if(V_VT(&val) != VT_DISPATCH) {
1041 TRACE("in vt %d\n", V_VT(&val));
1043 V_VT(ret) = VT_EMPTY;
1047 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
1048 IDispatch_Release(V_DISPATCH(&val));
1050 TRACE("Object doesn't support IDispatchEx\n");
1051 V_VT(ret) = VT_EMPTY;
1055 V_VT(&retv) = VT_EMPTY;
1058 identifier = SysAllocString(stat->variable->identifier);
1061 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
1062 if(FAILED(hres) || hres == S_FALSE)
1065 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
1069 TRACE("iter %s\n", debugstr_w(str));
1071 if(stat->variable) {
1072 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
1074 switch(stat->expr->type) {
1076 hres = array_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
1079 hres = identifier_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
1082 hres = member_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
1085 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1088 if(SUCCEEDED(hres)) {
1089 V_VT(&name) = VT_BSTR;
1090 V_BSTR(&name) = str;
1091 hres = put_value(ctx, &exprval, &name, &rt->ei);
1092 exprval_release(&exprval);
1098 hres = stat_eval(ctx, stat->statement, rt, &tmp);
1102 VariantClear(&retv);
1105 if(rt->type == RT_CONTINUE)
1106 rt->type = RT_NORMAL;
1107 else if(rt->type != RT_NORMAL)
1111 SysFreeString(identifier);
1112 IDispatchEx_Release(in_obj);
1114 VariantClear(&retv);
1118 if(rt->type == RT_BREAK)
1119 rt->type = RT_NORMAL;
1125 /* ECMA-262 3rd Edition 12.7 */
1126 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1128 branch_statement_t *stat = (branch_statement_t*)_stat;
1132 if(stat->identifier) {
1133 FIXME("indentifier not implemented\n");
1137 rt->type = RT_CONTINUE;
1138 V_VT(ret) = VT_EMPTY;
1142 /* ECMA-262 3rd Edition 12.8 */
1143 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1145 branch_statement_t *stat = (branch_statement_t*)_stat;
1149 if(stat->identifier) {
1150 FIXME("indentifier not implemented\n");
1154 rt->type = RT_BREAK;
1155 V_VT(ret) = VT_EMPTY;
1159 /* ECMA-262 3rd Edition 12.9 */
1160 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1162 expression_statement_t *stat = (expression_statement_t*)_stat;
1170 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1174 hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1175 exprval_release(&exprval);
1179 V_VT(ret) = VT_EMPTY;
1182 TRACE("= %s\n", debugstr_variant(ret));
1183 rt->type = RT_RETURN;
1187 /* ECMA-262 3rd Edition 12.10 */
1188 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1190 with_statement_t *stat = (with_statement_t*)_stat;
1199 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1203 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1204 exprval_release(&exprval);
1208 hres = to_object(ctx, &val, &disp);
1213 obj = iface_to_jsdisp((IUnknown*)disp);
1214 IDispatch_Release(disp);
1216 FIXME("disp id not jsdisp\n");
1220 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1221 jsdisp_release(obj);
1225 hres = stat_eval(ctx, stat->statement, rt, ret);
1227 scope_pop(&ctx->exec_ctx->scope_chain);
1231 /* ECMA-262 3rd Edition 12.12 */
1232 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1238 /* ECMA-262 3rd Edition 12.13 */
1239 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1241 switch_statement_t *stat = (switch_statement_t*)_stat;
1242 case_clausule_t *iter, *default_clausule = NULL;
1243 statement_t *stat_iter;
1251 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1255 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1256 exprval_release(&exprval);
1260 for(iter = stat->case_list; iter; iter = iter->next) {
1262 default_clausule = iter;
1266 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1270 hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1271 exprval_release(&exprval);
1275 hres = equal2_values(&val, &cval, &b);
1276 VariantClear(&cval);
1277 if(FAILED(hres) || b)
1286 iter = default_clausule;
1288 V_VT(&val) = VT_EMPTY;
1292 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1293 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1300 if(rt->type != RT_NORMAL)
1310 if(rt->type == RT_BREAK)
1311 rt->type = RT_NORMAL;
1317 /* ECMA-262 3rd Edition 12.13 */
1318 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1320 expression_statement_t *stat = (expression_statement_t*)_stat;
1327 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1331 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1332 exprval_release(&exprval);
1337 return DISP_E_EXCEPTION;
1340 static HRESULT interp_throw(exec_ctx_t *ctx)
1342 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1344 TRACE("%08x\n", arg);
1346 return throw_reference_error(ctx->parser->script, &ctx->ei, arg, NULL);
1349 static HRESULT interp_throw_type(exec_ctx_t *ctx)
1351 const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1352 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
1354 TRACE("%08x %s\n", hres, debugstr_w(str));
1356 return throw_type_error(ctx->parser->script, &ctx->ei, hres, str);
1359 /* ECMA-262 3rd Edition 12.14 */
1360 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1367 memset(&rt->ei, 0, sizeof(jsexcept_t));
1369 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1370 if(SUCCEEDED(hres)) {
1371 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1372 if(SUCCEEDED(hres)) {
1373 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1374 if(SUCCEEDED(hres)) {
1375 hres = stat_eval(ctx, block->statement, rt, &val);
1376 scope_pop(&ctx->exec_ctx->scope_chain);
1380 jsdisp_release(var_disp);
1391 /* ECMA-262 3rd Edition 12.14 */
1392 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1394 try_statement_t *stat = (try_statement_t*)_stat;
1400 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1402 TRACE("EXCEPTION\n");
1403 if(!stat->catch_block)
1406 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1411 if(stat->finally_statement) {
1413 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1422 /* ECMA-262 3rd Edition 13 */
1423 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1425 function_expression_t *expr = (function_expression_t*)_expr;
1431 if(expr->identifier) {
1432 hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1438 hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1439 expr->src_str, expr->src_len, &dispex);
1443 var_set_jsdisp(&var, dispex);
1446 ret->type = EXPRVAL_VARIANT;
1451 /* ECMA-262 3rd Edition 11.2.1 */
1452 static HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1454 binary_expression_t *expr = (binary_expression_t*)_expr;
1456 VARIANT member, val;
1459 IDispatch *obj = NULL;
1464 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1468 hres = exprval_to_value(ctx, &exprval, ei, &member);
1469 exprval_release(&exprval);
1473 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1474 if(SUCCEEDED(hres)) {
1475 hres = exprval_to_value(ctx, &exprval, ei, &val);
1476 exprval_release(&exprval);
1479 if(SUCCEEDED(hres)) {
1480 hres = to_object(ctx, &member, &obj);
1484 VariantClear(&member);
1485 if(SUCCEEDED(hres)) {
1486 hres = to_string(ctx, &val, ei, &str);
1488 if(SUCCEEDED(hres)) {
1489 hres = disp_get_id(ctx, obj, str, fdexNameEnsure, &id);
1494 exprval_set_idref(ret, obj, id);
1496 IDispatch_Release(obj);
1502 /* ECMA-262 3rd Edition 11.2.1 */
1503 static HRESULT interp_array(exec_ctx_t *ctx)
1513 namev = stack_pop(ctx);
1515 hres = stack_pop_object(ctx, &obj);
1517 VariantClear(namev);
1521 hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1522 VariantClear(namev);
1524 IDispatch_Release(obj);
1528 hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
1529 SysFreeString(name);
1530 if(SUCCEEDED(hres)) {
1531 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1532 }else if(hres == DISP_E_UNKNOWNNAME) {
1533 V_VT(&v) = VT_EMPTY;
1536 IDispatch_Release(obj);
1540 return stack_push(ctx, &v);
1543 /* ECMA-262 3rd Edition 11.2.1 */
1544 static HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1546 member_expression_t *expr = (member_expression_t*)_expr;
1547 IDispatch *obj = NULL;
1556 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1560 hres = exprval_to_value(ctx, &exprval, ei, &member);
1561 exprval_release(&exprval);
1565 hres = to_object(ctx, &member, &obj);
1566 VariantClear(&member);
1570 str = SysAllocString(expr->identifier);
1572 IDispatch_Release(obj);
1573 return E_OUTOFMEMORY;
1576 hres = disp_get_id(ctx, obj, str, fdexNameEnsure, &id);
1579 exprval_set_idref(ret, obj, id);
1581 IDispatch_Release(obj);
1585 /* ECMA-262 3rd Edition 11.2.1 */
1586 static HRESULT interp_member(exec_ctx_t *ctx)
1588 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1596 hres = stack_pop_object(ctx, &obj);
1600 hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
1601 if(SUCCEEDED(hres)) {
1602 V_VT(&v) = VT_EMPTY;
1603 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1604 }else if(hres == DISP_E_UNKNOWNNAME) {
1605 V_VT(&v) = VT_EMPTY;
1608 IDispatch_Release(obj);
1612 return stack_push(ctx, &v);
1615 /* ECMA-262 3rd Edition 11.2.1 */
1616 static HRESULT interp_memberid(exec_ctx_t *ctx)
1618 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1619 VARIANT *objv, *namev;
1627 namev = stack_pop(ctx);
1628 objv = stack_pop(ctx);
1630 hres = to_object(ctx->parser->script, objv, &obj);
1632 if(SUCCEEDED(hres)) {
1633 hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1635 IDispatch_Release(obj);
1637 VariantClear(namev);
1641 hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
1642 SysFreeString(name);
1644 IDispatch_Release(obj);
1645 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1647 id = JS_E_INVALID_PROPERTY;
1653 return stack_push_objid(ctx, obj, id);
1656 /* ECMA-262 3rd Edition 11.2.1 */
1657 static HRESULT interp_refval(exec_ctx_t *ctx)
1666 disp = stack_topn_objid(ctx, 0, &id);
1668 return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1670 hres = disp_propget(ctx->parser->script, disp, id, &v, &ctx->ei, NULL/*FIXME*/);
1674 return stack_push(ctx, &v);
1677 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1682 dp->cArgs = arg_cnt;
1683 dp->rgdispidNamedArgs = NULL;
1686 assert(ctx->top >= arg_cnt);
1688 for(i=1; i*2 <= arg_cnt; i++) {
1689 tmp = ctx->stack[ctx->top-i];
1690 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1691 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1694 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1697 /* ECMA-262 3rd Edition 11.2.2 */
1698 static HRESULT interp_new(exec_ctx_t *ctx)
1700 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1707 constr = stack_topn(ctx, arg);
1709 /* NOTE: Should use to_object here */
1711 if(V_VT(constr) == VT_NULL)
1712 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1713 else if(V_VT(constr) != VT_DISPATCH)
1714 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1715 else if(!V_DISPATCH(constr))
1716 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1718 jsstack_to_dp(ctx, arg, &dp);
1719 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1720 DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1724 stack_popn(ctx, arg+1);
1725 return stack_push(ctx, &v);
1728 /* ECMA-262 3rd Edition 11.2.3 */
1729 static HRESULT interp_call(exec_ctx_t *ctx)
1731 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1732 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1737 TRACE("%d %d\n", argn, do_ret);
1739 objv = stack_topn(ctx, argn);
1740 if(V_VT(objv) != VT_DISPATCH)
1741 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1743 jsstack_to_dp(ctx, argn, &dp);
1744 hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1745 do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1749 stack_popn(ctx, argn+1);
1750 return do_ret ? stack_push(ctx, &v) : S_OK;
1754 /* ECMA-262 3rd Edition 11.2.3 */
1755 static HRESULT interp_call_member(exec_ctx_t *ctx)
1757 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1758 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1765 TRACE("%d %d\n", argn, do_ret);
1767 obj = stack_topn_objid(ctx, argn, &id);
1769 return throw_type_error(ctx->parser->script, &ctx->ei, id, NULL);
1771 jsstack_to_dp(ctx, argn, &dp);
1772 hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1776 stack_popn(ctx, argn+2);
1777 return do_ret ? stack_push(ctx, &v) : S_OK;
1781 /* ECMA-262 3rd Edition 11.1.1 */
1782 static HRESULT interp_this(exec_ctx_t *ctx)
1788 V_VT(&v) = VT_DISPATCH;
1789 V_DISPATCH(&v) = ctx->this_obj;
1790 IDispatch_AddRef(ctx->this_obj);
1791 return stack_push(ctx, &v);
1794 /* ECMA-262 3rd Edition 10.1.4 */
1795 static HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1797 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1803 identifier = SysAllocString(expr->identifier);
1805 return E_OUTOFMEMORY;
1807 hres = identifier_eval(ctx, identifier, fdexNameEnsure, ei, ret);
1809 SysFreeString(identifier);
1813 /* ECMA-262 3rd Edition 10.1.4 */
1814 static HRESULT interp_ident(exec_ctx_t *ctx)
1816 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1821 TRACE("%s\n", debugstr_w(arg));
1823 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1827 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1828 exprval_release(&exprval);
1832 return stack_push(ctx, &v);
1835 /* ECMA-262 3rd Edition 10.1.4 */
1836 static HRESULT interp_identid(exec_ctx_t *ctx)
1838 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1839 const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1843 TRACE("%s %x\n", debugstr_w(arg), flags);
1845 hres = identifier_eval(ctx->parser->script, arg, flags, &ctx->ei, &exprval);
1849 if(exprval.type != EXPRVAL_IDREF) {
1850 WARN("invalid ref\n");
1851 exprval_release(&exprval);
1852 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1855 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1858 /* ECMA-262 3rd Edition 7.8.1 */
1859 static HRESULT interp_null(exec_ctx_t *ctx)
1866 return stack_push(ctx, &v);
1869 /* ECMA-262 3rd Edition 7.8.2 */
1870 static HRESULT interp_bool(exec_ctx_t *ctx)
1872 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1874 TRACE("%s\n", arg ? "true" : "false");
1876 return stack_push_bool(ctx, arg);
1879 /* ECMA-262 3rd Edition 7.8.3 */
1880 static HRESULT interp_int(exec_ctx_t *ctx)
1882 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1889 return stack_push(ctx, &v);
1892 /* ECMA-262 3rd Edition 7.8.3 */
1893 static HRESULT interp_double(exec_ctx_t *ctx)
1895 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1898 TRACE("%lf\n", arg);
1902 return stack_push(ctx, &v);
1905 /* ECMA-262 3rd Edition 7.8.4 */
1906 static HRESULT interp_str(exec_ctx_t *ctx)
1908 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1911 TRACE("%s\n", debugstr_w(str));
1914 V_BSTR(&v) = SysAllocString(str);
1916 return E_OUTOFMEMORY;
1918 return stack_push(ctx, &v);
1921 /* ECMA-262 3rd Edition 7.8 */
1922 static HRESULT interp_regexp(exec_ctx_t *ctx)
1924 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1925 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1930 TRACE("%s %x\n", debugstr_w(source), flags);
1932 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, ®exp);
1936 var_set_jsdisp(&v, regexp);
1937 return stack_push(ctx, &v);
1940 /* ECMA-262 3rd Edition 11.1.4 */
1941 static HRESULT interp_carray(exec_ctx_t *ctx)
1943 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1951 hres = create_array(ctx->parser->script, arg, &array);
1958 hres = jsdisp_propput_idx(array, i, v, &ctx->ei, NULL/*FIXME*/);
1961 jsdisp_release(array);
1966 var_set_jsdisp(&r, array);
1967 return stack_push(ctx, &r);
1970 /* ECMA-262 3rd Edition 11.1.5 */
1971 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1973 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1983 hres = create_object(ctx, NULL, &obj);
1987 for(iter = expr->property_list; iter; iter = iter->next) {
1988 hres = literal_to_var(ctx, iter->name, &tmp);
1992 hres = to_string(ctx, &tmp, ei, &name);
1997 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1998 if(SUCCEEDED(hres)) {
1999 hres = exprval_to_value(ctx, &exprval, ei, &val);
2000 exprval_release(&exprval);
2001 if(SUCCEEDED(hres)) {
2002 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
2007 SysFreeString(name);
2013 jsdisp_release(obj);
2017 ret->type = EXPRVAL_VARIANT;
2018 var_set_jsdisp(&ret->u.var, obj);
2022 /* ECMA-262 3rd Edition 11.11 */
2023 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
2025 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2031 hres = to_boolean(stack_top(ctx), &b);
2044 /* ECMA-262 3rd Edition 11.11 */
2045 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2047 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2053 hres = to_boolean(stack_top(ctx), &b);
2066 /* ECMA-262 3rd Edition 11.10 */
2067 static HRESULT interp_or(exec_ctx_t *ctx)
2074 hres = stack_pop_int(ctx, &r);
2078 hres = stack_pop_int(ctx, &l);
2082 return stack_push_int(ctx, l|r);
2085 /* ECMA-262 3rd Edition 11.10 */
2086 static HRESULT interp_xor(exec_ctx_t *ctx)
2093 hres = stack_pop_int(ctx, &r);
2097 hres = stack_pop_int(ctx, &l);
2101 return stack_push_int(ctx, l^r);
2104 /* ECMA-262 3rd Edition 11.10 */
2105 static HRESULT interp_and(exec_ctx_t *ctx)
2112 hres = stack_pop_int(ctx, &r);
2116 hres = stack_pop_int(ctx, &l);
2120 return stack_push_int(ctx, l&r);
2123 /* ECMA-262 3rd Edition 11.8.6 */
2124 static HRESULT interp_instanceof(exec_ctx_t *ctx)
2126 jsdisp_t *obj, *iter, *tmp = NULL;
2131 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2134 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
2136 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
2139 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
2140 IDispatch_Release(V_DISPATCH(v));
2142 FIXME("non-jsdisp objects not supported\n");
2146 if(is_class(obj, JSCLASS_FUNCTION)) {
2147 hres = jsdisp_propget_name(obj, prototypeW, &prot, &ctx->ei, NULL/*FIXME*/);
2149 hres = throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
2151 jsdisp_release(obj);
2157 if(V_VT(&prot) == VT_DISPATCH) {
2158 if(V_VT(v) == VT_DISPATCH)
2159 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
2160 for(iter = tmp; !ret && iter; iter = iter->prototype) {
2161 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
2167 jsdisp_release(tmp);
2169 FIXME("prototype is not an object\n");
2173 VariantClear(&prot);
2178 return stack_push_bool(ctx, ret);
2181 /* ECMA-262 3rd Edition 11.8.7 */
2182 static HRESULT interp_in(exec_ctx_t *ctx)
2192 obj = stack_pop(ctx);
2195 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2198 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2201 hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2204 IDispatch_Release(V_DISPATCH(obj));
2208 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2209 IDispatch_Release(V_DISPATCH(obj));
2213 else if(hres == DISP_E_UNKNOWNNAME)
2218 return stack_push_bool(ctx, ret);
2221 /* ECMA-262 3rd Edition 11.6.1 */
2222 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2227 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2231 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2237 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2238 BSTR lstr = NULL, rstr = NULL;
2240 if(V_VT(&l) == VT_BSTR)
2243 hres = to_string(ctx, &l, ei, &lstr);
2245 if(SUCCEEDED(hres)) {
2246 if(V_VT(&r) == VT_BSTR)
2249 hres = to_string(ctx, &r, ei, &rstr);
2252 if(SUCCEEDED(hres)) {
2255 len1 = SysStringLen(lstr);
2256 len2 = SysStringLen(rstr);
2258 V_VT(retv) = VT_BSTR;
2259 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2260 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2261 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2264 if(V_VT(&l) != VT_BSTR)
2265 SysFreeString(lstr);
2266 if(V_VT(&r) != VT_BSTR)
2267 SysFreeString(rstr);
2271 hres = to_number(ctx, &l, ei, &nl);
2272 if(SUCCEEDED(hres)) {
2273 hres = to_number(ctx, &r, ei, &nr);
2275 num_set_val(retv, num_val(&nl) + num_val(&nr));
2284 /* ECMA-262 3rd Edition 11.6.1 */
2285 static HRESULT interp_add(exec_ctx_t *ctx)
2287 VARIANT *l, *r, ret;
2293 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2295 hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2301 return stack_push(ctx, &ret);
2304 /* ECMA-262 3rd Edition 11.6.2 */
2305 static HRESULT interp_sub(exec_ctx_t *ctx)
2312 hres = stack_pop_number(ctx, &r);
2316 hres = stack_pop_number(ctx, &l);
2320 return stack_push_number(ctx, num_val(&l)-num_val(&r));
2323 /* ECMA-262 3rd Edition 11.5.1 */
2324 static HRESULT interp_mul(exec_ctx_t *ctx)
2331 hres = stack_pop_number(ctx, &r);
2335 hres = stack_pop_number(ctx, &l);
2339 return stack_push_number(ctx, num_val(&l)*num_val(&r));
2342 /* ECMA-262 3rd Edition 11.5.2 */
2343 static HRESULT interp_div(exec_ctx_t *ctx)
2350 hres = stack_pop_number(ctx, &r);
2354 hres = stack_pop_number(ctx, &l);
2358 return stack_push_number(ctx, num_val(&l)/num_val(&r));
2361 /* ECMA-262 3rd Edition 11.5.3 */
2362 static HRESULT interp_mod(exec_ctx_t *ctx)
2369 hres = stack_pop_number(ctx, &r);
2373 hres = stack_pop_number(ctx, &l);
2377 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2380 /* ECMA-262 3rd Edition 11.4.2 */
2381 static HRESULT interp_delete(exec_ctx_t *ctx)
2383 VARIANT *obj_var, *name_var;
2384 IDispatchEx *dispex;
2392 name_var = stack_pop(ctx);
2393 obj_var = stack_pop(ctx);
2395 hres = to_object(ctx->parser->script, obj_var, &obj);
2396 VariantClear(obj_var);
2398 VariantClear(name_var);
2402 hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2403 VariantClear(name_var);
2405 IDispatch_Release(obj);
2409 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2410 if(SUCCEEDED(hres)) {
2411 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2413 IDispatchEx_Release(dispex);
2419 IDispatch_Release(obj);
2420 SysFreeString(name);
2424 return stack_push_bool(ctx, ret);
2427 /* ECMA-262 3rd Edition 11.4.2 */
2428 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
2430 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2431 IDispatchEx *dispex;
2436 TRACE("%s\n", debugstr_w(arg));
2438 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2442 if(exprval.type != EXPRVAL_IDREF) {
2443 FIXME("Unsupported exprval\n");
2444 exprval_release(&exprval);
2448 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2449 IDispatch_Release(exprval.u.idref.disp);
2450 if(SUCCEEDED(hres)) {
2451 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2452 IDispatchEx_Release(dispex);
2459 return stack_push_bool(ctx, ret);
2462 /* ECMA-262 3rd Edition 11.4.2 */
2463 static HRESULT interp_void(exec_ctx_t *ctx)
2471 V_VT(&v) = VT_EMPTY;
2472 return stack_push(ctx, &v);
2475 /* ECMA-262 3rd Edition 11.4.3 */
2476 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
2498 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
2499 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2500 jsdisp_release(dispex);
2507 FIXME("unhandled vt %d\n", V_VT(v));
2514 /* ECMA-262 3rd Edition 11.4.3 */
2515 static HRESULT interp_typeofid(exec_ctx_t *ctx)
2523 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2527 obj = stack_pop_objid(ctx, &id);
2529 return stack_push_string(ctx, undefinedW);
2531 V_VT(&v) = VT_EMPTY;
2532 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2533 IDispatch_Release(obj);
2535 return stack_push_string(ctx, unknownW);
2537 hres = typeof_string(&v, &ret);
2542 return stack_push_string(ctx, ret);
2545 /* ECMA-262 3rd Edition 11.4.3 */
2546 static HRESULT interp_typeofident(exec_ctx_t *ctx)
2548 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2554 TRACE("%s\n", debugstr_w(arg));
2556 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2560 if(exprval.type == EXPRVAL_INVALID) {
2561 hres = stack_push_string(ctx, undefinedW);
2562 exprval_release(&exprval);
2566 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
2567 exprval_release(&exprval);
2571 hres = typeof_string(&v, &ret);
2576 return stack_push_string(ctx, ret);
2579 /* ECMA-262 3rd Edition 11.4.3 */
2580 static HRESULT interp_typeof(exec_ctx_t *ctx)
2589 hres = typeof_string(v, &ret);
2594 return stack_push_string(ctx, ret);
2597 /* ECMA-262 3rd Edition 11.4.7 */
2598 static HRESULT interp_minus(exec_ctx_t *ctx)
2605 hres = stack_pop_number(ctx, &n);
2609 return stack_push_number(ctx, -num_val(&n));
2612 /* ECMA-262 3rd Edition 11.4.6 */
2613 static HRESULT interp_tonum(exec_ctx_t *ctx)
2621 hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2626 return stack_push(ctx, &num);
2629 /* ECMA-262 3rd Edition 11.3.1 */
2630 static HRESULT interp_postinc(exec_ctx_t *ctx)
2632 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2640 obj = stack_pop_objid(ctx, &id);
2642 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2644 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2645 if(SUCCEEDED(hres)) {
2648 hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2649 if(SUCCEEDED(hres)) {
2650 num_set_val(&inc, num_val(&n)+(double)arg);
2651 hres = disp_propput(ctx->parser->script, obj, id, &inc, &ctx->ei, NULL/*FIXME*/);
2656 IDispatch_Release(obj);
2660 return stack_push(ctx, &v);
2663 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2664 static HRESULT interp_preinc(exec_ctx_t *ctx)
2666 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2674 obj = stack_pop_objid(ctx, &id);
2676 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2678 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2679 if(SUCCEEDED(hres)) {
2682 hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2684 if(SUCCEEDED(hres)) {
2685 num_set_val(&v, num_val(&n)+(double)arg);
2686 hres = disp_propput(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2689 IDispatch_Release(obj);
2693 return stack_push(ctx, &v);
2696 /* ECMA-262 3rd Edition 11.9.3 */
2697 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2699 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2700 return equal2_values(lval, rval, ret);
2702 /* FIXME: NULL disps should be handled in more general way */
2703 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2706 return equal_values(ctx, &v, rval, ei, ret);
2709 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2712 return equal_values(ctx, lval, &v, ei, ret);
2715 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2716 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2721 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2725 hres = to_number(ctx, lval, ei, &v);
2729 return equal_values(ctx, &v, rval, ei, ret);
2732 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2736 hres = to_number(ctx, rval, ei, &v);
2740 return equal_values(ctx, lval, &v, ei, ret);
2743 if(V_VT(rval) == VT_BOOL) {
2747 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2748 return equal_values(ctx, lval, &v, ei, ret);
2751 if(V_VT(lval) == VT_BOOL) {
2755 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2756 return equal_values(ctx, &v, rval, ei, ret);
2760 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2764 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2768 hres = equal_values(ctx, lval, &v, ei, ret);
2775 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2779 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2783 hres = equal_values(ctx, &v, rval, ei, ret);
2794 /* ECMA-262 3rd Edition 11.9.1 */
2795 static HRESULT interp_eq(exec_ctx_t *ctx)
2804 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2806 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2812 return stack_push_bool(ctx, b);
2815 /* ECMA-262 3rd Edition 11.9.2 */
2816 static HRESULT interp_neq(exec_ctx_t *ctx)
2825 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2827 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2833 return stack_push_bool(ctx, !b);
2836 /* ECMA-262 3rd Edition 11.9.4 */
2837 static HRESULT interp_eq2(exec_ctx_t *ctx)
2848 hres = equal2_values(r, l, &b);
2854 return stack_push_bool(ctx, b);
2857 /* ECMA-262 3rd Edition 11.9.5 */
2858 static HRESULT interp_neq2(exec_ctx_t *ctx)
2869 hres = equal2_values(r, l, &b);
2875 return stack_push_bool(ctx, !b);
2878 /* ECMA-262 3rd Edition 11.8.5 */
2879 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2881 VARIANT l, r, ln, rn;
2884 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2888 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2894 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2895 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2896 SysFreeString(V_BSTR(&l));
2897 SysFreeString(V_BSTR(&r));
2901 hres = to_number(ctx, &l, ei, &ln);
2904 hres = to_number(ctx, &r, ei, &rn);
2909 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2910 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2912 DOUBLE ld = num_val(&ln);
2913 DOUBLE rd = num_val(&rn);
2915 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2921 /* ECMA-262 3rd Edition 11.8.1 */
2922 static HRESULT interp_lt(exec_ctx_t *ctx)
2931 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2933 hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
2939 return stack_push_bool(ctx, b);
2942 /* ECMA-262 3rd Edition 11.8.1 */
2943 static HRESULT interp_lteq(exec_ctx_t *ctx)
2952 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2954 hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
2960 return stack_push_bool(ctx, b);
2963 /* ECMA-262 3rd Edition 11.8.2 */
2964 static HRESULT interp_gt(exec_ctx_t *ctx)
2973 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2975 hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
2981 return stack_push_bool(ctx, b);
2984 /* ECMA-262 3rd Edition 11.8.4 */
2985 static HRESULT interp_gteq(exec_ctx_t *ctx)
2994 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2996 hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
3002 return stack_push_bool(ctx, b);
3005 /* ECMA-262 3rd Edition 11.4.8 */
3006 static HRESULT interp_bneg(exec_ctx_t *ctx)
3015 hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3022 return stack_push(ctx, &r);
3025 /* ECMA-262 3rd Edition 11.4.9 */
3026 static HRESULT interp_neg(exec_ctx_t *ctx)
3035 hres = to_boolean(v, &b);
3040 return stack_push_bool(ctx, !b);
3043 /* ECMA-262 3rd Edition 11.7.1 */
3044 static HRESULT interp_lshift(exec_ctx_t *ctx)
3050 hres = stack_pop_uint(ctx, &r);
3054 hres = stack_pop_int(ctx, &l);
3058 return stack_push_int(ctx, l << (r&0x1f));
3061 /* ECMA-262 3rd Edition 11.7.2 */
3062 static HRESULT interp_rshift(exec_ctx_t *ctx)
3068 hres = stack_pop_uint(ctx, &r);
3072 hres = stack_pop_int(ctx, &l);
3076 return stack_push_int(ctx, l >> (r&0x1f));
3079 /* ECMA-262 3rd Edition 11.7.3 */
3080 static HRESULT interp_rshift2(exec_ctx_t *ctx)
3085 hres = stack_pop_uint(ctx, &r);
3089 hres = stack_pop_uint(ctx, &l);
3093 return stack_push_int(ctx, l >> (r&0x1f));
3096 /* ECMA-262 3rd Edition 11.13.1 */
3097 static HRESULT interp_assign(exec_ctx_t *ctx)
3107 disp = stack_pop_objid(ctx, &id);
3110 return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
3112 hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
3113 IDispatch_Release(disp);
3119 return stack_push(ctx, v);
3122 static HRESULT interp_undefined(exec_ctx_t *ctx)
3128 V_VT(&v) = VT_EMPTY;
3129 return stack_push(ctx, &v);
3132 static HRESULT interp_jmp(exec_ctx_t *ctx)
3134 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3142 static HRESULT interp_pop(exec_ctx_t *ctx)
3150 static HRESULT interp_ret(exec_ctx_t *ctx)
3158 static HRESULT interp_tree(exec_ctx_t *ctx)
3160 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3167 hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3171 hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3172 exprval_release(&val);
3176 return stack_push(ctx, &v);
3179 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3181 static const op_func_t op_funcs[] = {
3182 #define X(x,a,b,c) interp_##x,
3187 static const unsigned op_move[] = {
3188 #define X(a,x,b,c) x,
3193 static HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3195 exec_ctx_t *exec_ctx = ctx->exec_ctx;
3196 unsigned prev_ip, prev_top;
3198 HRESULT hres = S_OK;
3202 prev_top = exec_ctx->top;
3203 prev_ip = exec_ctx->ip;
3204 exec_ctx->ip = expr->instr_off;
3206 while(exec_ctx->ip != -1) {
3207 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3208 hres = op_funcs[op](exec_ctx);
3211 exec_ctx->ip += op_move[op];
3214 exec_ctx->ip = prev_ip;
3217 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3219 memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3223 assert(exec_ctx->top == prev_top+1 || ((flags&EXPR_NOVAL) && exec_ctx->top == prev_top));
3225 ret->type = EXPRVAL_VARIANT;
3226 if(exec_ctx->top == prev_top)
3227 V_VT(&ret->u.var) = VT_EMPTY;
3229 ret->u.var = *stack_pop(exec_ctx);
3233 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3239 hres = compile_subscript(ctx->exec_ctx->parser, expr, !(flags & EXPR_NOVAL), &expr->instr_off);
3243 if(expr->eval == compiled_expression_eval)
3244 expr->eval = interp_expression_eval;
3246 return expr->eval(ctx, expr, flags, ei, ret);