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
33 #define EXPR_NEWREF 0x0002
34 #define EXPR_STRREF 0x0004
36 struct _return_type_t {
46 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
48 return stat->eval(ctx, stat, rt, ret);
51 static inline HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
53 return expr->eval(ctx, expr, flags, ei, ret);
56 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
58 if(!ctx->stack_size) {
59 ctx->stack = heap_alloc(16*sizeof(VARIANT));
63 }else if(ctx->stack_size == ctx->top) {
66 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
72 ctx->stack = new_stack;
76 ctx->stack[ctx->top++] = *v;
80 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
85 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
86 return stack_push(ctx, &v);
89 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
93 num_set_val(&v, number);
94 return stack_push(ctx, &v);
97 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
103 return stack_push(ctx, &v);
106 static inline VARIANT *stack_top(exec_ctx_t *ctx)
109 return ctx->stack + ctx->top-1;
112 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
114 assert(ctx->top > n);
115 return ctx->stack + ctx->top-1-n;
118 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
121 return ctx->stack + --ctx->top;
124 static void stack_popn(exec_ctx_t *ctx, unsigned n)
127 VariantClear(stack_pop(ctx));
130 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
136 hres = to_number(ctx->parser->script, v, &ctx->ei, r);
141 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
143 return to_int32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
146 static void exprval_release(exprval_t *val)
149 case EXPRVAL_VARIANT:
150 if(V_VT(&val->u.var) != VT_EMPTY)
151 VariantClear(&val->u.var);
154 if(val->u.idref.disp)
155 IDispatch_Release(val->u.idref.disp);
157 case EXPRVAL_NAMEREF:
158 if(val->u.nameref.disp)
159 IDispatch_Release(val->u.nameref.disp);
160 SysFreeString(val->u.nameref.name);
162 case EXPRVAL_INVALID:
163 SysFreeString(val->u.identifier);
167 /* ECMA-262 3rd Edition 8.7.1 */
168 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
170 V_VT(ret) = VT_EMPTY;
173 case EXPRVAL_VARIANT:
174 return VariantCopy(ret, &val->u.var);
176 if(!val->u.idref.disp) {
177 FIXME("throw ReferenceError\n");
181 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
182 case EXPRVAL_NAMEREF:
184 case EXPRVAL_INVALID:
185 return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
188 ERR("type %d\n", val->type);
192 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
194 if(val->type == EXPRVAL_VARIANT) {
196 V_VT(&val->u.var) = VT_EMPTY;
200 return exprval_value(ctx, val, ei, ret);
203 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
205 if(exprval->type != EXPRVAL_VARIANT) {
209 hres = exprval_to_value(ctx, exprval, ei, &val);
213 hres = to_boolean(&val, b);
218 return to_boolean(&exprval->u.var, b);
221 static void exprval_init(exprval_t *val)
223 val->type = EXPRVAL_VARIANT;
224 V_VT(&val->u.var) = VT_EMPTY;
227 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
229 val->type = EXPRVAL_IDREF;
230 val->u.idref.disp = disp;
231 val->u.idref.id = id;
234 IDispatch_AddRef(disp);
237 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
239 scope_chain_t *new_scope;
241 new_scope = heap_alloc(sizeof(scope_chain_t));
243 return E_OUTOFMEMORY;
248 new_scope->obj = obj;
252 new_scope->next = scope;
254 new_scope->next = NULL;
261 static void scope_pop(scope_chain_t **scope)
270 void scope_release(scope_chain_t *scope)
276 scope_release(scope->next);
278 jsdisp_release(scope->obj);
282 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
283 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
287 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
289 return E_OUTOFMEMORY;
292 ctx->is_global = is_global;
295 ctx->this_obj = this_obj;
296 else if(script_ctx->host_global)
297 ctx->this_obj = script_ctx->host_global;
299 ctx->this_obj = to_disp(script_ctx->global);
300 IDispatch_AddRef(ctx->this_obj);
302 jsdisp_addref(var_disp);
303 ctx->var_disp = var_disp;
307 ctx->scope_chain = scope;
314 void exec_release(exec_ctx_t *ctx)
320 scope_release(ctx->scope_chain);
322 jsdisp_release(ctx->var_disp);
324 IDispatch_Release(ctx->this_obj);
325 heap_free(ctx->stack);
329 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
334 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
336 TRACE("unsing IDispatch\n");
339 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
343 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
344 IDispatchEx_Release(dispex);
348 /* ECMA-262 3rd Edition 8.7.2 */
349 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
351 if(ref->type != EXPRVAL_IDREF)
352 return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
354 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
357 static inline BOOL is_null(const VARIANT *v)
359 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
362 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
364 IObjectIdentity *identity;
365 IUnknown *unk1, *unk2;
373 if(!disp1 || !disp2) {
378 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
382 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
384 IUnknown_Release(unk1);
391 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
392 if(SUCCEEDED(hres)) {
393 hres = IObjectIdentity_IsEqualObject(identity, unk2);
394 IObjectIdentity_Release(identity);
401 IUnknown_Release(unk1);
402 IUnknown_Release(unk2);
406 /* ECMA-262 3rd Edition 11.9.6 */
407 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
411 if(V_VT(lval) != V_VT(rval)) {
412 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
413 *ret = num_val(lval) == num_val(rval);
414 else if(is_null(lval))
415 *ret = is_null(rval);
427 *ret = V_I4(lval) == V_I4(rval);
430 *ret = V_R8(lval) == V_R8(rval);
434 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
435 else if(!V_BSTR(rval))
436 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
438 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
441 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
443 *ret = !V_BOOL(lval) == !V_BOOL(rval);
446 FIXME("unimplemented vt %d\n", V_VT(lval));
453 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
455 switch(literal->type) {
461 V_I4(v) = literal->u.lval;
465 V_R8(v) = literal->u.dval;
468 BSTR str = SysAllocString(literal->u.wstr);
470 return E_OUTOFMEMORY;
478 V_BOOL(v) = literal->u.bval;
484 hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
485 literal->u.regexp.flags, ®exp);
489 var_set_jsdisp(v, regexp);
496 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
502 for(item = ctx->named_items; item; item = item->next) {
503 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
504 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
505 if(SUCCEEDED(hres)) {
507 exprval_set_idref(ret, item->disp, id);
516 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
517 jsexcept_t *ei, VARIANT *retv)
519 script_ctx_t *script = parser->script;
520 function_declaration_t *func;
521 parser_ctx_t *prev_parser;
525 exec_ctx_t *prev_ctx;
529 for(func = source->functions; func; func = func->next) {
533 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
534 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
538 var_set_jsdisp(&var, func_obj);
539 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
540 jsdisp_release(func_obj);
545 for(var = source->variables; var; var = var->next) {
549 name = SysAllocString(var->identifier);
551 return E_OUTOFMEMORY;
553 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
554 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
560 prev_ctx = script->exec_ctx;
561 script->exec_ctx = ctx;
563 prev_parser = ctx->parser;
564 ctx->parser = parser;
566 V_VT(&val) = VT_EMPTY;
567 memset(&rt, 0, sizeof(rt));
570 for(stat = source->statement; stat; stat = stat->next) {
571 hres = stat_eval(script, stat, &rt, &tmp);
577 if(rt.type != RT_NORMAL)
581 script->exec_ctx = prev_ctx;
582 ctx->parser = prev_parser;
584 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
585 FIXME("wrong rt %d\n", rt.type);
595 if(!retv || (!from_eval && rt.type != RT_RETURN))
602 /* ECMA-262 3rd Edition 10.1.4 */
603 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
605 scope_chain_t *scope;
610 TRACE("%s\n", debugstr_w(identifier));
612 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
613 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
614 if(SUCCEEDED(hres)) {
615 exprval_set_idref(ret, to_disp(scope->obj), id);
620 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
621 if(SUCCEEDED(hres)) {
622 exprval_set_idref(ret, to_disp(ctx->global), id);
626 for(item = ctx->named_items; item; item = item->next) {
627 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
634 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
635 SCRIPTINFO_IUNKNOWN, &unk, NULL);
637 WARN("GetItemInfo failed: %08x\n", hres);
641 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
642 IUnknown_Release(unk);
644 WARN("object does not implement IDispatch\n");
649 ret->type = EXPRVAL_VARIANT;
650 V_VT(&ret->u.var) = VT_DISPATCH;
651 V_DISPATCH(&ret->u.var) = item->disp;
652 IDispatch_AddRef(item->disp);
657 if(lookup_global_members(ctx, identifier, ret))
660 if(flags & EXPR_NEWREF) {
661 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
665 exprval_set_idref(ret, to_disp(ctx->global), id);
669 ret->type = EXPRVAL_INVALID;
670 ret->u.identifier = SysAllocString(identifier);
671 if(!ret->u.identifier)
672 return E_OUTOFMEMORY;
677 /* ECMA-262 3rd Edition 12.1 */
678 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
680 block_statement_t *stat = (block_statement_t*)_stat;
687 V_VT(&val) = VT_EMPTY;
688 for(iter = stat->stat_list; iter; iter = iter->next) {
689 hres = stat_eval(ctx, iter, rt, &tmp);
695 if(rt->type != RT_NORMAL)
708 /* ECMA-262 3rd Edition 12.2 */
709 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
711 variable_declaration_t *iter;
714 for(iter = var_list; iter; iter = iter->next) {
721 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
725 hres = exprval_to_value(ctx, &exprval, ei, &val);
726 exprval_release(&exprval);
730 hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
739 /* ECMA-262 3rd Edition 12.2 */
740 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
742 var_statement_t *stat = (var_statement_t*)_stat;
747 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
751 V_VT(ret) = VT_EMPTY;
755 /* ECMA-262 3rd Edition 12.3 */
756 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
760 V_VT(ret) = VT_EMPTY;
764 /* ECMA-262 3rd Edition 12.4 */
765 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
767 expression_statement_t *stat = (expression_statement_t*)_stat;
774 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
778 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
779 exprval_release(&exprval);
784 TRACE("= %s\n", debugstr_variant(ret));
788 /* ECMA-262 3rd Edition 12.5 */
789 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
791 if_statement_t *stat = (if_statement_t*)_stat;
798 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
802 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
803 exprval_release(&exprval);
808 hres = stat_eval(ctx, stat->if_stat, rt, ret);
809 else if(stat->else_stat)
810 hres = stat_eval(ctx, stat->else_stat, rt, ret);
812 V_VT(ret) = VT_EMPTY;
817 /* ECMA-262 3rd Edition 12.6.2 */
818 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
820 while_statement_t *stat = (while_statement_t*)_stat;
829 V_VT(&val) = VT_EMPTY;
830 test_expr = !stat->do_while;
834 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
838 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
839 exprval_release(&exprval);
840 if(FAILED(hres) || !b)
846 hres = stat_eval(ctx, stat->statement, rt, &tmp);
853 if(rt->type == RT_CONTINUE)
854 rt->type = RT_NORMAL;
855 if(rt->type != RT_NORMAL)
864 if(rt->type == RT_BREAK)
865 rt->type = RT_NORMAL;
871 /* ECMA-262 3rd Edition 12.6.3 */
872 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
874 for_statement_t *stat = (for_statement_t*)_stat;
875 VARIANT val, tmp, retv;
882 if(stat->variable_list) {
883 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
886 }else if(stat->begin_expr) {
887 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
891 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
892 exprval_release(&exprval);
899 V_VT(&retv) = VT_EMPTY;
903 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
907 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
908 exprval_release(&exprval);
909 if(FAILED(hres) || !b)
913 hres = stat_eval(ctx, stat->statement, rt, &tmp);
920 if(rt->type == RT_CONTINUE)
921 rt->type = RT_NORMAL;
922 else if(rt->type != RT_NORMAL)
926 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
930 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
931 exprval_release(&exprval);
944 if(rt->type == RT_BREAK)
945 rt->type = RT_NORMAL;
951 /* ECMA-262 3rd Edition 12.6.4 */
952 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
954 forin_statement_t *stat = (forin_statement_t*)_stat;
955 VARIANT val, name, retv, tmp;
956 DISPID id = DISPID_STARTENUM;
957 BSTR str, identifier = NULL;
965 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
970 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
974 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
975 exprval_release(&exprval);
979 if(V_VT(&val) != VT_DISPATCH) {
980 TRACE("in vt %d\n", V_VT(&val));
982 V_VT(ret) = VT_EMPTY;
986 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
987 IDispatch_Release(V_DISPATCH(&val));
989 TRACE("Object doesn't support IDispatchEx\n");
990 V_VT(ret) = VT_EMPTY;
994 V_VT(&retv) = VT_EMPTY;
997 identifier = SysAllocString(stat->variable->identifier);
1000 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
1001 if(FAILED(hres) || hres == S_FALSE)
1004 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
1008 TRACE("iter %s\n", debugstr_w(str));
1011 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
1013 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
1014 if(SUCCEEDED(hres)) {
1015 V_VT(&name) = VT_BSTR;
1016 V_BSTR(&name) = str;
1017 hres = put_value(ctx, &exprval, &name, &rt->ei);
1018 exprval_release(&exprval);
1024 hres = stat_eval(ctx, stat->statement, rt, &tmp);
1028 VariantClear(&retv);
1031 if(rt->type == RT_CONTINUE)
1032 rt->type = RT_NORMAL;
1033 else if(rt->type != RT_NORMAL)
1037 SysFreeString(identifier);
1038 IDispatchEx_Release(in_obj);
1040 VariantClear(&retv);
1044 if(rt->type == RT_BREAK)
1045 rt->type = RT_NORMAL;
1051 /* ECMA-262 3rd Edition 12.7 */
1052 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1054 branch_statement_t *stat = (branch_statement_t*)_stat;
1058 if(stat->identifier) {
1059 FIXME("indentifier not implemented\n");
1063 rt->type = RT_CONTINUE;
1064 V_VT(ret) = VT_EMPTY;
1068 /* ECMA-262 3rd Edition 12.8 */
1069 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1071 branch_statement_t *stat = (branch_statement_t*)_stat;
1075 if(stat->identifier) {
1076 FIXME("indentifier not implemented\n");
1080 rt->type = RT_BREAK;
1081 V_VT(ret) = VT_EMPTY;
1085 /* ECMA-262 3rd Edition 12.9 */
1086 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1088 expression_statement_t *stat = (expression_statement_t*)_stat;
1096 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1100 hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1101 exprval_release(&exprval);
1105 V_VT(ret) = VT_EMPTY;
1108 TRACE("= %s\n", debugstr_variant(ret));
1109 rt->type = RT_RETURN;
1113 /* ECMA-262 3rd Edition 12.10 */
1114 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1116 with_statement_t *stat = (with_statement_t*)_stat;
1125 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1129 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1130 exprval_release(&exprval);
1134 hres = to_object(ctx, &val, &disp);
1139 obj = iface_to_jsdisp((IUnknown*)disp);
1140 IDispatch_Release(disp);
1142 FIXME("disp id not jsdisp\n");
1146 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1147 jsdisp_release(obj);
1151 hres = stat_eval(ctx, stat->statement, rt, ret);
1153 scope_pop(&ctx->exec_ctx->scope_chain);
1157 /* ECMA-262 3rd Edition 12.12 */
1158 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1164 /* ECMA-262 3rd Edition 12.13 */
1165 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1167 switch_statement_t *stat = (switch_statement_t*)_stat;
1168 case_clausule_t *iter, *default_clausule = NULL;
1169 statement_t *stat_iter;
1177 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1181 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1182 exprval_release(&exprval);
1186 for(iter = stat->case_list; iter; iter = iter->next) {
1188 default_clausule = iter;
1192 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1196 hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1197 exprval_release(&exprval);
1201 hres = equal2_values(&val, &cval, &b);
1202 VariantClear(&cval);
1203 if(FAILED(hres) || b)
1212 iter = default_clausule;
1214 V_VT(&val) = VT_EMPTY;
1218 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1219 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1226 if(rt->type != RT_NORMAL)
1236 if(rt->type == RT_BREAK)
1237 rt->type = RT_NORMAL;
1243 /* ECMA-262 3rd Edition 12.13 */
1244 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1246 expression_statement_t *stat = (expression_statement_t*)_stat;
1253 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1257 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1258 exprval_release(&exprval);
1263 return DISP_E_EXCEPTION;
1266 /* ECMA-262 3rd Edition 12.14 */
1267 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1274 memset(&rt->ei, 0, sizeof(jsexcept_t));
1276 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1277 if(SUCCEEDED(hres)) {
1278 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1279 if(SUCCEEDED(hres)) {
1280 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1281 if(SUCCEEDED(hres)) {
1282 hres = stat_eval(ctx, block->statement, rt, &val);
1283 scope_pop(&ctx->exec_ctx->scope_chain);
1287 jsdisp_release(var_disp);
1298 /* ECMA-262 3rd Edition 12.14 */
1299 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1301 try_statement_t *stat = (try_statement_t*)_stat;
1307 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1309 TRACE("EXCEPTION\n");
1310 if(!stat->catch_block)
1313 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1318 if(stat->finally_statement) {
1320 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1329 static HRESULT return_bool(exprval_t *ret, DWORD b)
1331 ret->type = EXPRVAL_VARIANT;
1332 V_VT(&ret->u.var) = VT_BOOL;
1333 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1338 static HRESULT get_binary_expr_values(script_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1343 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1347 hres = exprval_to_value(ctx, &exprval, ei, lval);
1348 exprval_release(&exprval);
1352 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1353 if(SUCCEEDED(hres)) {
1354 hres = exprval_to_value(ctx, &exprval, ei, rval);
1355 exprval_release(&exprval);
1366 typedef HRESULT (*oper_t)(script_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1368 static HRESULT binary_expr_eval(script_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1371 VARIANT lval, rval, retv;
1374 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1378 hres = oper(ctx, &lval, &rval, ei, &retv);
1379 VariantClear(&lval);
1380 VariantClear(&rval);
1384 ret->type = EXPRVAL_VARIANT;
1389 /* ECMA-262 3rd Edition 11.13.2 */
1390 static HRESULT assign_oper_eval(script_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1391 jsexcept_t *ei, exprval_t *ret)
1393 VARIANT retv, lval, rval;
1394 exprval_t exprval, exprvalr;
1397 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1401 hres = exprval_value(ctx, &exprval, ei, &lval);
1402 if(SUCCEEDED(hres)) {
1403 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1404 if(SUCCEEDED(hres)) {
1405 hres = exprval_value(ctx, &exprvalr, ei, &rval);
1406 exprval_release(&exprvalr);
1408 if(SUCCEEDED(hres)) {
1409 hres = oper(ctx, &lval, &rval, ei, &retv);
1410 VariantClear(&rval);
1412 VariantClear(&lval);
1415 if(SUCCEEDED(hres)) {
1416 hres = put_value(ctx, &exprval, &retv, ei);
1418 VariantClear(&retv);
1420 exprval_release(&exprval);
1425 ret->type = EXPRVAL_VARIANT;
1430 /* ECMA-262 3rd Edition 13 */
1431 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1433 function_expression_t *expr = (function_expression_t*)_expr;
1439 if(expr->identifier) {
1440 hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1446 hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1447 expr->src_str, expr->src_len, &dispex);
1451 var_set_jsdisp(&var, dispex);
1454 ret->type = EXPRVAL_VARIANT;
1459 /* ECMA-262 3rd Edition 11.2.1 */
1460 HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1462 array_expression_t *expr = (array_expression_t*)_expr;
1464 VARIANT member, val;
1467 IDispatch *obj = NULL;
1472 hres = expr_eval(ctx, expr->member_expr, 0, ei, &exprval);
1476 hres = exprval_to_value(ctx, &exprval, ei, &member);
1477 exprval_release(&exprval);
1481 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1482 if(SUCCEEDED(hres)) {
1483 hres = exprval_to_value(ctx, &exprval, ei, &val);
1484 exprval_release(&exprval);
1487 if(SUCCEEDED(hres)) {
1488 hres = to_object(ctx, &member, &obj);
1492 VariantClear(&member);
1493 if(SUCCEEDED(hres)) {
1494 hres = to_string(ctx, &val, ei, &str);
1496 if(SUCCEEDED(hres)) {
1497 if(flags & EXPR_STRREF) {
1498 ret->type = EXPRVAL_NAMEREF;
1499 ret->u.nameref.disp = obj;
1500 ret->u.nameref.name = str;
1504 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1508 if(SUCCEEDED(hres)) {
1509 exprval_set_idref(ret, obj, id);
1510 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1515 IDispatch_Release(obj);
1521 /* ECMA-262 3rd Edition 11.2.1 */
1522 HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1524 member_expression_t *expr = (member_expression_t*)_expr;
1525 IDispatch *obj = NULL;
1534 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1538 hres = exprval_to_value(ctx, &exprval, ei, &member);
1539 exprval_release(&exprval);
1543 hres = to_object(ctx, &member, &obj);
1544 VariantClear(&member);
1548 str = SysAllocString(expr->identifier);
1549 if(flags & EXPR_STRREF) {
1550 ret->type = EXPRVAL_NAMEREF;
1551 ret->u.nameref.disp = obj;
1552 ret->u.nameref.name = str;
1556 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1558 if(SUCCEEDED(hres)) {
1559 exprval_set_idref(ret, obj, id);
1560 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1565 IDispatch_Release(obj);
1569 static void free_dp(DISPPARAMS *dp)
1573 for(i=0; i < dp->cArgs; i++)
1574 VariantClear(dp->rgvarg+i);
1575 heap_free(dp->rgvarg);
1578 static HRESULT args_to_param(script_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1584 HRESULT hres = S_OK;
1586 memset(dp, 0, sizeof(*dp));
1590 for(iter = args; iter; iter = iter->next)
1593 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1595 return E_OUTOFMEMORY;
1597 for(i = cnt, iter = args; iter; iter = iter->next) {
1598 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1602 hres = exprval_to_value(ctx, &exprval, ei, vargs + (--i));
1603 exprval_release(&exprval);
1618 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1623 dp->cArgs = arg_cnt;
1624 dp->rgdispidNamedArgs = NULL;
1627 assert(ctx->top >= arg_cnt);
1629 for(i=1; i*2 <= arg_cnt; i++) {
1630 tmp = ctx->stack[ctx->top-i];
1631 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1632 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1635 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1638 /* ECMA-262 3rd Edition 11.2.2 */
1639 HRESULT interp_new(exec_ctx_t *ctx)
1641 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1648 constr = stack_topn(ctx, arg);
1650 /* NOTE: Should use to_object here */
1652 if(V_VT(constr) == VT_NULL)
1653 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1654 else if(V_VT(constr) != VT_DISPATCH)
1655 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1656 else if(!V_DISPATCH(constr))
1657 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1659 jsstack_to_dp(ctx, arg, &dp);
1660 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1661 DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1665 stack_popn(ctx, arg+1);
1666 return stack_push(ctx, &v);
1669 /* ECMA-262 3rd Edition 11.2.3 */
1670 HRESULT call_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1672 call_expression_t *expr = (call_expression_t*)_expr;
1680 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1684 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1685 if(SUCCEEDED(hres)) {
1686 switch(exprval.type) {
1687 case EXPRVAL_VARIANT:
1688 if(V_VT(&exprval.u.var) == VT_DISPATCH)
1689 hres = disp_call(ctx, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1690 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1692 hres = throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1695 hres = disp_call(ctx, exprval.u.idref.disp, exprval.u.idref.id,
1696 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1698 case EXPRVAL_INVALID:
1699 hres = throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1702 FIXME("unimplemented type %d\n", exprval.type);
1709 exprval_release(&exprval);
1713 ret->type = EXPRVAL_VARIANT;
1714 if(flags & EXPR_NOVAL) {
1715 V_VT(&ret->u.var) = VT_EMPTY;
1717 TRACE("= %s\n", debugstr_variant(&var));
1723 /* ECMA-262 3rd Edition 11.1.1 */
1724 HRESULT interp_this(exec_ctx_t *ctx)
1730 V_VT(&v) = VT_DISPATCH;
1731 V_DISPATCH(&v) = ctx->this_obj;
1732 IDispatch_AddRef(ctx->this_obj);
1733 return stack_push(ctx, &v);
1736 /* ECMA-262 3rd Edition 10.1.4 */
1737 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1739 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1745 identifier = SysAllocString(expr->identifier);
1747 return E_OUTOFMEMORY;
1749 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1751 SysFreeString(identifier);
1755 /* ECMA-262 3rd Edition 10.1.4 */
1756 static HRESULT interp_ident(exec_ctx_t *ctx)
1758 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1763 TRACE("%s\n", debugstr_w(arg));
1765 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1769 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1770 exprval_release(&exprval);
1774 return stack_push(ctx, &v);
1777 /* ECMA-262 3rd Edition 7.8.1 */
1778 HRESULT interp_null(exec_ctx_t *ctx)
1785 return stack_push(ctx, &v);
1788 /* ECMA-262 3rd Edition 7.8.2 */
1789 HRESULT interp_bool(exec_ctx_t *ctx)
1791 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1793 TRACE("%s\n", arg ? "true" : "false");
1795 return stack_push_bool(ctx, arg);
1798 /* ECMA-262 3rd Edition 7.8.3 */
1799 HRESULT interp_int(exec_ctx_t *ctx)
1801 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1808 return stack_push(ctx, &v);
1811 /* ECMA-262 3rd Edition 7.8.3 */
1812 HRESULT interp_double(exec_ctx_t *ctx)
1814 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1817 TRACE("%lf\n", arg);
1821 return stack_push(ctx, &v);
1824 /* ECMA-262 3rd Edition 7.8.4 */
1825 HRESULT interp_str(exec_ctx_t *ctx)
1827 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1830 TRACE("%s\n", debugstr_w(str));
1833 V_BSTR(&v) = SysAllocString(str);
1835 return E_OUTOFMEMORY;
1837 return stack_push(ctx, &v);
1840 /* ECMA-262 3rd Edition 7.8 */
1841 HRESULT interp_regexp(exec_ctx_t *ctx)
1843 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1844 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1849 TRACE("%s %x\n", debugstr_w(source), flags);
1851 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, ®exp);
1855 var_set_jsdisp(&v, regexp);
1856 return stack_push(ctx, &v);
1859 /* ECMA-262 3rd Edition 11.1.4 */
1860 HRESULT array_literal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1862 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1863 DWORD length = 0, i = 0;
1864 array_element_t *elem;
1872 for(elem = expr->element_list; elem; elem = elem->next)
1873 length += elem->elision+1;
1874 length += expr->length;
1876 hres = create_array(ctx, length, &array);
1880 for(elem = expr->element_list; elem; elem = elem->next) {
1883 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1887 hres = exprval_to_value(ctx, &exprval, ei, &val);
1888 exprval_release(&exprval);
1892 hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
1901 jsdisp_release(array);
1905 ret->type = EXPRVAL_VARIANT;
1906 var_set_jsdisp(&ret->u.var, array);
1910 /* ECMA-262 3rd Edition 11.1.5 */
1911 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1913 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1923 hres = create_object(ctx, NULL, &obj);
1927 for(iter = expr->property_list; iter; iter = iter->next) {
1928 hres = literal_to_var(ctx, iter->name, &tmp);
1932 hres = to_string(ctx, &tmp, ei, &name);
1937 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1938 if(SUCCEEDED(hres)) {
1939 hres = exprval_to_value(ctx, &exprval, ei, &val);
1940 exprval_release(&exprval);
1941 if(SUCCEEDED(hres)) {
1942 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
1947 SysFreeString(name);
1953 jsdisp_release(obj);
1957 ret->type = EXPRVAL_VARIANT;
1958 var_set_jsdisp(&ret->u.var, obj);
1962 /* ECMA-262 3rd Edition 11.11 */
1963 HRESULT interp_jmp_nz(exec_ctx_t *ctx)
1965 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1971 hres = to_boolean(stack_top(ctx), &b);
1984 /* ECMA-262 3rd Edition 11.11 */
1985 HRESULT interp_jmp_z(exec_ctx_t *ctx)
1987 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1993 hres = to_boolean(stack_top(ctx), &b);
2006 /* ECMA-262 3rd Edition 11.10 */
2007 static HRESULT bitor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2012 hres = to_int32(ctx, lval, ei, &li);
2016 hres = to_int32(ctx, rval, ei, &ri);
2025 /* ECMA-262 3rd Edition 11.10 */
2026 static HRESULT interp_or(exec_ctx_t *ctx)
2033 hres = stack_pop_int(ctx, &r);
2037 hres = stack_pop_int(ctx, &l);
2041 return stack_push_int(ctx, l|r);
2044 /* ECMA-262 3rd Edition 11.10 */
2045 static HRESULT xor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2050 hres = to_int32(ctx, lval, ei, &li);
2054 hres = to_int32(ctx, rval, ei, &ri);
2063 /* ECMA-262 3rd Edition 11.10 */
2064 static HRESULT interp_xor(exec_ctx_t *ctx)
2071 hres = stack_pop_int(ctx, &r);
2075 hres = stack_pop_int(ctx, &l);
2079 return stack_push_int(ctx, l^r);
2082 /* ECMA-262 3rd Edition 11.10 */
2083 static HRESULT bitand_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2088 hres = to_int32(ctx, lval, ei, &li);
2092 hres = to_int32(ctx, rval, ei, &ri);
2101 /* ECMA-262 3rd Edition 11.10 */
2102 HRESULT binary_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2104 binary_expression_t *expr = (binary_expression_t*)_expr;
2108 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
2111 /* ECMA-262 3rd Edition 11.8.6 */
2112 static HRESULT instanceof_eval(script_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
2114 jsdisp_t *obj, *iter, *tmp = NULL;
2115 VARIANT_BOOL ret = VARIANT_FALSE;
2120 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2122 if(V_VT(objv) != VT_DISPATCH || !V_DISPATCH(objv))
2123 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2125 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2127 FIXME("non-jsdisp objects not supported\n");
2131 if(is_class(obj, JSCLASS_FUNCTION)) {
2132 hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2134 hres = throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2136 jsdisp_release(obj);
2140 if(V_VT(&var) == VT_DISPATCH) {
2141 if(V_VT(inst) == VT_DISPATCH)
2142 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2143 for(iter = tmp; iter; iter = iter->prototype) {
2144 hres = disp_cmp(V_DISPATCH(&var), to_disp(iter), &b);
2154 jsdisp_release(tmp);
2156 FIXME("prototype is not an object\n");
2164 V_VT(retv) = VT_BOOL;
2169 /* ECMA-262 3rd Edition 11.8.6 */
2170 HRESULT instanceof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2172 binary_expression_t *expr = (binary_expression_t*)_expr;
2176 return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2179 /* ECMA-262 3rd Edition 11.8.7 */
2180 HRESULT interp_in(exec_ctx_t *ctx)
2190 obj = stack_pop(ctx);
2193 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2196 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2199 hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2202 IDispatch_Release(V_DISPATCH(obj));
2206 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2207 IDispatch_Release(V_DISPATCH(obj));
2211 else if(hres == DISP_E_UNKNOWNNAME)
2216 return stack_push_bool(ctx, ret);
2219 /* ECMA-262 3rd Edition 11.6.1 */
2220 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2225 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2229 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2235 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2236 BSTR lstr = NULL, rstr = NULL;
2238 if(V_VT(&l) == VT_BSTR)
2241 hres = to_string(ctx, &l, ei, &lstr);
2243 if(SUCCEEDED(hres)) {
2244 if(V_VT(&r) == VT_BSTR)
2247 hres = to_string(ctx, &r, ei, &rstr);
2250 if(SUCCEEDED(hres)) {
2253 len1 = SysStringLen(lstr);
2254 len2 = SysStringLen(rstr);
2256 V_VT(retv) = VT_BSTR;
2257 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2258 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2259 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2262 if(V_VT(&l) != VT_BSTR)
2263 SysFreeString(lstr);
2264 if(V_VT(&r) != VT_BSTR)
2265 SysFreeString(rstr);
2269 hres = to_number(ctx, &l, ei, &nl);
2270 if(SUCCEEDED(hres)) {
2271 hres = to_number(ctx, &r, ei, &nr);
2273 num_set_val(retv, num_val(&nl) + num_val(&nr));
2282 /* ECMA-262 3rd Edition 11.6.1 */
2283 HRESULT interp_add(exec_ctx_t *ctx)
2285 VARIANT *l, *r, ret;
2291 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2293 hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2299 return stack_push(ctx, &ret);
2302 /* ECMA-262 3rd Edition 11.6.2 */
2303 static HRESULT sub_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2308 hres = to_number(ctx, lval, ei, &lnum);
2312 hres = to_number(ctx, rval, ei, &rnum);
2316 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
2320 /* ECMA-262 3rd Edition 11.6.2 */
2321 HRESULT interp_sub(exec_ctx_t *ctx)
2328 hres = stack_pop_number(ctx, &r);
2332 hres = stack_pop_number(ctx, &l);
2336 return stack_push_number(ctx, num_val(&l)-num_val(&r));
2339 /* ECMA-262 3rd Edition 11.5.1 */
2340 static HRESULT mul_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2345 hres = to_number(ctx, lval, ei, &lnum);
2349 hres = to_number(ctx, rval, ei, &rnum);
2353 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2357 /* ECMA-262 3rd Edition 11.5.1 */
2358 HRESULT interp_mul(exec_ctx_t *ctx)
2365 hres = stack_pop_number(ctx, &r);
2369 hres = stack_pop_number(ctx, &l);
2373 return stack_push_number(ctx, num_val(&l)*num_val(&r));
2376 /* ECMA-262 3rd Edition 11.5.2 */
2377 static HRESULT div_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2382 hres = to_number(ctx, lval, ei, &lnum);
2386 hres = to_number(ctx, rval, ei, &rnum);
2390 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2394 /* ECMA-262 3rd Edition 11.5.2 */
2395 static HRESULT interp_div(exec_ctx_t *ctx)
2402 hres = stack_pop_number(ctx, &r);
2406 hres = stack_pop_number(ctx, &l);
2410 return stack_push_number(ctx, num_val(&l)/num_val(&r));
2413 /* ECMA-262 3rd Edition 11.5.3 */
2414 static HRESULT mod_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2419 hres = to_number(ctx, lval, ei, &lnum);
2423 hres = to_number(ctx, rval, ei, &rnum);
2427 num_set_val(retv, fmod(num_val(&lnum), num_val(&rnum)));
2431 /* ECMA-262 3rd Edition 11.5.3 */
2432 static HRESULT interp_mod(exec_ctx_t *ctx)
2439 hres = stack_pop_number(ctx, &r);
2443 hres = stack_pop_number(ctx, &l);
2447 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2450 /* ECMA-262 3rd Edition 11.4.2 */
2451 HRESULT delete_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2453 unary_expression_t *expr = (unary_expression_t*)_expr;
2454 VARIANT_BOOL b = VARIANT_FALSE;
2460 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2464 switch(exprval.type) {
2465 case EXPRVAL_IDREF: {
2466 IDispatchEx *dispex;
2468 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2469 if(SUCCEEDED(hres)) {
2470 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2472 IDispatchEx_Release(dispex);
2476 case EXPRVAL_NAMEREF: {
2477 IDispatchEx *dispex;
2479 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2480 if(SUCCEEDED(hres)) {
2481 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name,
2482 make_grfdex(ctx, fdexNameCaseSensitive));
2484 IDispatchEx_Release(dispex);
2489 FIXME("unsupported type %d\n", exprval.type);
2493 exprval_release(&exprval);
2497 return return_bool(ret, b);
2500 /* ECMA-262 3rd Edition 11.4.2 */
2501 HRESULT interp_void(exec_ctx_t *ctx)
2509 V_VT(&v) = VT_EMPTY;
2510 return stack_push(ctx, &v);
2513 /* ECMA-262 3rd Edition 11.4.3 */
2514 static HRESULT typeof_exprval(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2519 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2520 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2521 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2522 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2523 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2524 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2525 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
2527 if(exprval->type == EXPRVAL_INVALID) {
2532 hres = exprval_to_value(ctx, exprval, ei, &val);
2534 if(exprval->type == EXPRVAL_IDREF) {
2541 switch(V_VT(&val)) {
2561 if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2562 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2563 jsdisp_release(dispex);
2570 FIXME("unhandled vt %d\n", V_VT(&val));
2578 HRESULT typeof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2580 unary_expression_t *expr = (unary_expression_t*)_expr;
2581 const WCHAR *str = NULL;
2587 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2591 hres = typeof_exprval(ctx, &exprval, ei, &str);
2592 exprval_release(&exprval);
2596 ret->type = EXPRVAL_VARIANT;
2597 V_VT(&ret->u.var) = VT_BSTR;
2598 V_BSTR(&ret->u.var) = SysAllocString(str);
2599 if(!V_BSTR(&ret->u.var))
2600 return E_OUTOFMEMORY;
2605 /* ECMA-262 3rd Edition 11.4.7 */
2606 HRESULT interp_minus(exec_ctx_t *ctx)
2613 hres = stack_pop_number(ctx, &n);
2617 return stack_push_number(ctx, -num_val(&n));
2620 /* ECMA-262 3rd Edition 11.4.6 */
2621 HRESULT interp_tonum(exec_ctx_t *ctx)
2629 hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2634 return stack_push(ctx, &num);
2637 /* ECMA-262 3rd Edition 11.3.1 */
2638 HRESULT post_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2640 unary_expression_t *expr = (unary_expression_t*)_expr;
2647 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2651 hres = exprval_value(ctx, &exprval, ei, &val);
2652 if(SUCCEEDED(hres)) {
2653 hres = to_number(ctx, &val, ei, &num);
2657 if(SUCCEEDED(hres)) {
2659 num_set_val(&inc, num_val(&num)+1.0);
2660 hres = put_value(ctx, &exprval, &inc, ei);
2663 exprval_release(&exprval);
2667 ret->type = EXPRVAL_VARIANT;
2672 /* ECMA-262 3rd Edition 11.3.2 */
2673 HRESULT post_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2675 unary_expression_t *expr = (unary_expression_t*)_expr;
2682 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2686 hres = exprval_value(ctx, &exprval, ei, &val);
2687 if(SUCCEEDED(hres)) {
2688 hres = to_number(ctx, &val, ei, &num);
2692 if(SUCCEEDED(hres)) {
2694 num_set_val(&dec, num_val(&num)-1.0);
2695 hres = put_value(ctx, &exprval, &dec, ei);
2698 exprval_release(&exprval);
2702 ret->type = EXPRVAL_VARIANT;
2707 /* ECMA-262 3rd Edition 11.4.4 */
2708 HRESULT pre_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2710 unary_expression_t *expr = (unary_expression_t*)_expr;
2717 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2721 hres = exprval_value(ctx, &exprval, ei, &val);
2722 if(SUCCEEDED(hres)) {
2723 hres = to_number(ctx, &val, ei, &num);
2727 if(SUCCEEDED(hres)) {
2728 num_set_val(&val, num_val(&num)+1.0);
2729 hres = put_value(ctx, &exprval, &val, ei);
2732 exprval_release(&exprval);
2736 ret->type = EXPRVAL_VARIANT;
2741 /* ECMA-262 3rd Edition 11.4.5 */
2742 HRESULT pre_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2744 unary_expression_t *expr = (unary_expression_t*)_expr;
2751 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2755 hres = exprval_value(ctx, &exprval, ei, &val);
2756 if(SUCCEEDED(hres)) {
2757 hres = to_number(ctx, &val, ei, &num);
2761 if(SUCCEEDED(hres)) {
2762 num_set_val(&val, num_val(&num)-1.0);
2763 hres = put_value(ctx, &exprval, &val, ei);
2766 exprval_release(&exprval);
2770 ret->type = EXPRVAL_VARIANT;
2775 /* ECMA-262 3rd Edition 11.9.3 */
2776 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2778 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2779 return equal2_values(lval, rval, ret);
2781 /* FIXME: NULL disps should be handled in more general way */
2782 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2785 return equal_values(ctx, &v, rval, ei, ret);
2788 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2791 return equal_values(ctx, lval, &v, ei, ret);
2794 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2795 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2800 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2804 hres = to_number(ctx, lval, ei, &v);
2808 return equal_values(ctx, &v, rval, ei, ret);
2811 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2815 hres = to_number(ctx, rval, ei, &v);
2819 return equal_values(ctx, lval, &v, ei, ret);
2822 if(V_VT(rval) == VT_BOOL) {
2826 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2827 return equal_values(ctx, lval, &v, ei, ret);
2830 if(V_VT(lval) == VT_BOOL) {
2834 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2835 return equal_values(ctx, &v, rval, ei, ret);
2839 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2843 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2847 hres = equal_values(ctx, lval, &v, ei, ret);
2854 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2858 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2862 hres = equal_values(ctx, &v, rval, ei, ret);
2873 /* ECMA-262 3rd Edition 11.9.1 */
2874 HRESULT interp_eq(exec_ctx_t *ctx)
2883 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2885 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2891 return stack_push_bool(ctx, b);
2894 /* ECMA-262 3rd Edition 11.9.2 */
2895 HRESULT interp_neq(exec_ctx_t *ctx)
2904 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2906 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2912 return stack_push_bool(ctx, !b);
2915 /* ECMA-262 3rd Edition 11.9.4 */
2916 static HRESULT interp_eq2(exec_ctx_t *ctx)
2927 hres = equal2_values(r, l, &b);
2933 return stack_push_bool(ctx, b);
2936 /* ECMA-262 3rd Edition 11.9.5 */
2937 static HRESULT interp_neq2(exec_ctx_t *ctx)
2948 hres = equal2_values(r, l, &b);
2954 return stack_push_bool(ctx, !b);
2957 /* ECMA-262 3rd Edition 11.8.5 */
2958 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2960 VARIANT l, r, ln, rn;
2963 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2967 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2973 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2974 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2975 SysFreeString(V_BSTR(&l));
2976 SysFreeString(V_BSTR(&r));
2980 hres = to_number(ctx, &l, ei, &ln);
2983 hres = to_number(ctx, &r, ei, &rn);
2988 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2989 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2991 DOUBLE ld = num_val(&ln);
2992 DOUBLE rd = num_val(&rn);
2994 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
3000 /* ECMA-262 3rd Edition 11.8.1 */
3001 HRESULT less_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3003 binary_expression_t *expr = (binary_expression_t*)_expr;
3010 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3014 hres = less_eval(ctx, &lval, &rval, FALSE, ei, &b);
3015 VariantClear(&lval);
3016 VariantClear(&rval);
3020 return return_bool(ret, b);
3023 /* ECMA-262 3rd Edition 11.8.3 */
3024 HRESULT lesseq_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3026 binary_expression_t *expr = (binary_expression_t*)_expr;
3033 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3037 hres = less_eval(ctx, &rval, &lval, TRUE, ei, &b);
3038 VariantClear(&lval);
3039 VariantClear(&rval);
3043 return return_bool(ret, b);
3046 /* ECMA-262 3rd Edition 11.8.2 */
3047 HRESULT greater_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3049 binary_expression_t *expr = (binary_expression_t*)_expr;
3056 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3060 hres = less_eval(ctx, &rval, &lval, FALSE, ei, &b);
3061 VariantClear(&lval);
3062 VariantClear(&rval);
3066 return return_bool(ret, b);
3069 /* ECMA-262 3rd Edition 11.8.4 */
3070 HRESULT greatereq_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3072 binary_expression_t *expr = (binary_expression_t*)_expr;
3079 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3083 hres = less_eval(ctx, &lval, &rval, TRUE, ei, &b);
3084 VariantClear(&lval);
3085 VariantClear(&rval);
3089 return return_bool(ret, b);
3092 /* ECMA-262 3rd Edition 11.4.8 */
3093 HRESULT interp_bneg(exec_ctx_t *ctx)
3102 hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3109 return stack_push(ctx, &r);
3112 /* ECMA-262 3rd Edition 11.4.9 */
3113 HRESULT interp_neg(exec_ctx_t *ctx)
3122 hres = to_boolean(v, &b);
3127 return stack_push_bool(ctx, !b);
3130 /* ECMA-262 3rd Edition 11.7.1 */
3131 static HRESULT lshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3137 hres = to_int32(ctx, lval, ei, &li);
3141 hres = to_uint32(ctx, rval, ei, &ri);
3146 V_I4(retv) = li << (ri&0x1f);
3150 /* ECMA-262 3rd Edition 11.7.1 */
3151 HRESULT left_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3153 binary_expression_t *expr = (binary_expression_t*)_expr;
3157 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3160 /* ECMA-262 3rd Edition 11.7.2 */
3161 static HRESULT rshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3167 hres = to_int32(ctx, lval, ei, &li);
3171 hres = to_uint32(ctx, rval, ei, &ri);
3176 V_I4(retv) = li >> (ri&0x1f);
3180 /* ECMA-262 3rd Edition 11.7.2 */
3181 HRESULT right_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3183 binary_expression_t *expr = (binary_expression_t*)_expr;
3187 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3190 /* ECMA-262 3rd Edition 11.7.3 */
3191 static HRESULT rshift2_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3196 hres = to_uint32(ctx, lval, ei, &li);
3200 hres = to_uint32(ctx, rval, ei, &ri);
3205 V_I4(retv) = li >> (ri&0x1f);
3209 /* ECMA-262 3rd Edition 11.7.3 */
3210 HRESULT right2_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3212 binary_expression_t *expr = (binary_expression_t*)_expr;
3216 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3219 /* ECMA-262 3rd Edition 11.13.1 */
3220 HRESULT assign_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3222 binary_expression_t *expr = (binary_expression_t*)_expr;
3223 exprval_t exprval, exprvalr;
3229 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
3233 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
3234 if(SUCCEEDED(hres)) {
3235 hres = exprval_to_value(ctx, &exprvalr, ei, &rval);
3236 exprval_release(&exprvalr);
3239 if(SUCCEEDED(hres)) {
3240 hres = put_value(ctx, &exprval, &rval, ei);
3242 VariantClear(&rval);
3245 exprval_release(&exprval);
3249 ret->type = EXPRVAL_VARIANT;
3254 /* ECMA-262 3rd Edition 11.13.2 */
3255 HRESULT assign_lshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3257 binary_expression_t *expr = (binary_expression_t*)_expr;
3261 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3264 /* ECMA-262 3rd Edition 11.13.2 */
3265 HRESULT assign_rshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3267 binary_expression_t *expr = (binary_expression_t*)_expr;
3271 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3274 /* ECMA-262 3rd Edition 11.13.2 */
3275 HRESULT assign_rrshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3277 binary_expression_t *expr = (binary_expression_t*)_expr;
3281 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3284 /* ECMA-262 3rd Edition 11.13.2 */
3285 HRESULT assign_add_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3287 binary_expression_t *expr = (binary_expression_t*)_expr;
3291 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
3294 /* ECMA-262 3rd Edition 11.13.2 */
3295 HRESULT assign_sub_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3297 binary_expression_t *expr = (binary_expression_t*)_expr;
3301 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
3304 /* ECMA-262 3rd Edition 11.13.2 */
3305 HRESULT assign_mul_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3307 binary_expression_t *expr = (binary_expression_t*)_expr;
3311 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
3314 /* ECMA-262 3rd Edition 11.13.2 */
3315 HRESULT assign_div_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3317 binary_expression_t *expr = (binary_expression_t*)_expr;
3321 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
3324 /* ECMA-262 3rd Edition 11.13.2 */
3325 HRESULT assign_mod_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3327 binary_expression_t *expr = (binary_expression_t*)_expr;
3331 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mod_eval, ei, ret);
3334 /* ECMA-262 3rd Edition 11.13.2 */
3335 HRESULT assign_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3337 binary_expression_t *expr = (binary_expression_t*)_expr;
3341 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3344 /* ECMA-262 3rd Edition 11.13.2 */
3345 HRESULT assign_or_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3347 binary_expression_t *expr = (binary_expression_t*)_expr;
3351 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
3354 /* ECMA-262 3rd Edition 11.13.2 */
3355 HRESULT assign_xor_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3357 binary_expression_t *expr = (binary_expression_t*)_expr;
3361 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);
3364 HRESULT interp_jmp(exec_ctx_t *ctx)
3366 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3374 static HRESULT interp_pop(exec_ctx_t *ctx)
3382 static HRESULT interp_ret(exec_ctx_t *ctx)
3390 static HRESULT interp_tree(exec_ctx_t *ctx)
3392 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3399 hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3403 hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3404 exprval_release(&val);
3408 return stack_push(ctx, &v);
3411 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3413 static const op_func_t op_funcs[] = {
3414 #define X(x,a,b,c) interp_##x,
3419 static const unsigned op_move[] = {
3420 #define X(a,x,b,c) x,
3425 HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3427 exec_ctx_t *exec_ctx = ctx->exec_ctx;
3428 unsigned prev_ip, prev_top;
3430 HRESULT hres = S_OK;
3434 prev_top = exec_ctx->top;
3435 prev_ip = exec_ctx->ip;
3436 exec_ctx->ip = expr->instr_off;
3438 while(exec_ctx->ip != -1) {
3439 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3440 hres = op_funcs[op](exec_ctx);
3443 exec_ctx->ip += op_move[op];
3446 exec_ctx->ip = prev_ip;
3449 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3451 memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3455 assert(exec_ctx->top == prev_top+1);
3457 ret->type = EXPRVAL_VARIANT;
3458 ret->u.var = *stack_pop(exec_ctx);
3462 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3468 hres = compile_subscript(ctx->exec_ctx->parser, expr, &expr->instr_off);
3472 return (expr->eval = interp_expression_eval)(ctx, expr, flags, ei, ret);