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 VARIANT *stack_pop(exec_ctx_t *ctx)
92 return ctx->stack + --ctx->top;
95 static void stack_popn(exec_ctx_t *ctx, unsigned n)
98 VariantClear(stack_pop(ctx));
101 static void exprval_release(exprval_t *val)
104 case EXPRVAL_VARIANT:
105 if(V_VT(&val->u.var) != VT_EMPTY)
106 VariantClear(&val->u.var);
109 if(val->u.idref.disp)
110 IDispatch_Release(val->u.idref.disp);
112 case EXPRVAL_NAMEREF:
113 if(val->u.nameref.disp)
114 IDispatch_Release(val->u.nameref.disp);
115 SysFreeString(val->u.nameref.name);
117 case EXPRVAL_INVALID:
118 SysFreeString(val->u.identifier);
122 /* ECMA-262 3rd Edition 8.7.1 */
123 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
125 V_VT(ret) = VT_EMPTY;
128 case EXPRVAL_VARIANT:
129 return VariantCopy(ret, &val->u.var);
131 if(!val->u.idref.disp) {
132 FIXME("throw ReferenceError\n");
136 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
137 case EXPRVAL_NAMEREF:
139 case EXPRVAL_INVALID:
140 return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
143 ERR("type %d\n", val->type);
147 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
149 if(val->type == EXPRVAL_VARIANT) {
151 V_VT(&val->u.var) = VT_EMPTY;
155 return exprval_value(ctx, val, ei, ret);
158 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
160 if(exprval->type != EXPRVAL_VARIANT) {
164 hres = exprval_to_value(ctx, exprval, ei, &val);
168 hres = to_boolean(&val, b);
173 return to_boolean(&exprval->u.var, b);
176 static void exprval_init(exprval_t *val)
178 val->type = EXPRVAL_VARIANT;
179 V_VT(&val->u.var) = VT_EMPTY;
182 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
184 val->type = EXPRVAL_IDREF;
185 val->u.idref.disp = disp;
186 val->u.idref.id = id;
189 IDispatch_AddRef(disp);
192 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
194 scope_chain_t *new_scope;
196 new_scope = heap_alloc(sizeof(scope_chain_t));
198 return E_OUTOFMEMORY;
203 new_scope->obj = obj;
207 new_scope->next = scope;
209 new_scope->next = NULL;
216 static void scope_pop(scope_chain_t **scope)
225 void scope_release(scope_chain_t *scope)
231 scope_release(scope->next);
233 jsdisp_release(scope->obj);
237 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
238 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
242 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
244 return E_OUTOFMEMORY;
247 ctx->is_global = is_global;
250 ctx->this_obj = this_obj;
251 else if(script_ctx->host_global)
252 ctx->this_obj = script_ctx->host_global;
254 ctx->this_obj = to_disp(script_ctx->global);
255 IDispatch_AddRef(ctx->this_obj);
257 jsdisp_addref(var_disp);
258 ctx->var_disp = var_disp;
262 ctx->scope_chain = scope;
269 void exec_release(exec_ctx_t *ctx)
275 scope_release(ctx->scope_chain);
277 jsdisp_release(ctx->var_disp);
279 IDispatch_Release(ctx->this_obj);
280 heap_free(ctx->stack);
284 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
289 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
291 TRACE("unsing IDispatch\n");
294 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
298 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
299 IDispatchEx_Release(dispex);
303 /* ECMA-262 3rd Edition 8.7.2 */
304 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
306 if(ref->type != EXPRVAL_IDREF)
307 return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
309 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
312 static inline BOOL is_null(const VARIANT *v)
314 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
317 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
319 IObjectIdentity *identity;
320 IUnknown *unk1, *unk2;
328 if(!disp1 || !disp2) {
333 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
337 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
339 IUnknown_Release(unk1);
346 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
347 if(SUCCEEDED(hres)) {
348 hres = IObjectIdentity_IsEqualObject(identity, unk2);
349 IObjectIdentity_Release(identity);
356 IUnknown_Release(unk1);
357 IUnknown_Release(unk2);
361 /* ECMA-262 3rd Edition 11.9.6 */
362 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
366 if(V_VT(lval) != V_VT(rval)) {
367 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
368 *ret = num_val(lval) == num_val(rval);
369 else if(is_null(lval))
370 *ret = is_null(rval);
382 *ret = V_I4(lval) == V_I4(rval);
385 *ret = V_R8(lval) == V_R8(rval);
389 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
390 else if(!V_BSTR(rval))
391 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
393 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
396 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
398 *ret = !V_BOOL(lval) == !V_BOOL(rval);
401 FIXME("unimplemented vt %d\n", V_VT(lval));
408 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
410 switch(literal->type) {
416 V_I4(v) = literal->u.lval;
420 V_R8(v) = literal->u.dval;
423 BSTR str = SysAllocString(literal->u.wstr);
425 return E_OUTOFMEMORY;
433 V_BOOL(v) = literal->u.bval;
439 hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
440 literal->u.regexp.flags, ®exp);
444 var_set_jsdisp(v, regexp);
451 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
457 for(item = ctx->named_items; item; item = item->next) {
458 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
459 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
460 if(SUCCEEDED(hres)) {
462 exprval_set_idref(ret, item->disp, id);
471 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
472 jsexcept_t *ei, VARIANT *retv)
474 script_ctx_t *script = parser->script;
475 function_declaration_t *func;
476 parser_ctx_t *prev_parser;
480 exec_ctx_t *prev_ctx;
484 for(func = source->functions; func; func = func->next) {
488 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
489 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
493 var_set_jsdisp(&var, func_obj);
494 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
495 jsdisp_release(func_obj);
500 for(var = source->variables; var; var = var->next) {
504 name = SysAllocString(var->identifier);
506 return E_OUTOFMEMORY;
508 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
509 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
515 prev_ctx = script->exec_ctx;
516 script->exec_ctx = ctx;
518 prev_parser = ctx->parser;
519 ctx->parser = parser;
521 V_VT(&val) = VT_EMPTY;
522 memset(&rt, 0, sizeof(rt));
525 for(stat = source->statement; stat; stat = stat->next) {
526 hres = stat_eval(script, stat, &rt, &tmp);
532 if(rt.type != RT_NORMAL)
536 script->exec_ctx = prev_ctx;
537 ctx->parser = prev_parser;
539 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
540 FIXME("wrong rt %d\n", rt.type);
550 if(!retv || (!from_eval && rt.type != RT_RETURN))
557 /* ECMA-262 3rd Edition 10.1.4 */
558 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
560 scope_chain_t *scope;
565 TRACE("%s\n", debugstr_w(identifier));
567 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
568 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
569 if(SUCCEEDED(hres)) {
570 exprval_set_idref(ret, to_disp(scope->obj), id);
575 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
576 if(SUCCEEDED(hres)) {
577 exprval_set_idref(ret, to_disp(ctx->global), id);
581 for(item = ctx->named_items; item; item = item->next) {
582 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
589 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
590 SCRIPTINFO_IUNKNOWN, &unk, NULL);
592 WARN("GetItemInfo failed: %08x\n", hres);
596 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
597 IUnknown_Release(unk);
599 WARN("object does not implement IDispatch\n");
604 ret->type = EXPRVAL_VARIANT;
605 V_VT(&ret->u.var) = VT_DISPATCH;
606 V_DISPATCH(&ret->u.var) = item->disp;
607 IDispatch_AddRef(item->disp);
612 if(lookup_global_members(ctx, identifier, ret))
615 if(flags & EXPR_NEWREF) {
616 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
620 exprval_set_idref(ret, to_disp(ctx->global), id);
624 ret->type = EXPRVAL_INVALID;
625 ret->u.identifier = SysAllocString(identifier);
626 if(!ret->u.identifier)
627 return E_OUTOFMEMORY;
632 /* ECMA-262 3rd Edition 12.1 */
633 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
635 block_statement_t *stat = (block_statement_t*)_stat;
642 V_VT(&val) = VT_EMPTY;
643 for(iter = stat->stat_list; iter; iter = iter->next) {
644 hres = stat_eval(ctx, iter, rt, &tmp);
650 if(rt->type != RT_NORMAL)
663 /* ECMA-262 3rd Edition 12.2 */
664 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
666 variable_declaration_t *iter;
669 for(iter = var_list; iter; iter = iter->next) {
676 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
680 hres = exprval_to_value(ctx, &exprval, ei, &val);
681 exprval_release(&exprval);
685 hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
694 /* ECMA-262 3rd Edition 12.2 */
695 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
697 var_statement_t *stat = (var_statement_t*)_stat;
702 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
706 V_VT(ret) = VT_EMPTY;
710 /* ECMA-262 3rd Edition 12.3 */
711 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
715 V_VT(ret) = VT_EMPTY;
719 /* ECMA-262 3rd Edition 12.4 */
720 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
722 expression_statement_t *stat = (expression_statement_t*)_stat;
729 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
733 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
734 exprval_release(&exprval);
739 TRACE("= %s\n", debugstr_variant(ret));
743 /* ECMA-262 3rd Edition 12.5 */
744 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
746 if_statement_t *stat = (if_statement_t*)_stat;
753 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
757 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
758 exprval_release(&exprval);
763 hres = stat_eval(ctx, stat->if_stat, rt, ret);
764 else if(stat->else_stat)
765 hres = stat_eval(ctx, stat->else_stat, rt, ret);
767 V_VT(ret) = VT_EMPTY;
772 /* ECMA-262 3rd Edition 12.6.2 */
773 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
775 while_statement_t *stat = (while_statement_t*)_stat;
784 V_VT(&val) = VT_EMPTY;
785 test_expr = !stat->do_while;
789 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
793 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
794 exprval_release(&exprval);
795 if(FAILED(hres) || !b)
801 hres = stat_eval(ctx, stat->statement, rt, &tmp);
808 if(rt->type == RT_CONTINUE)
809 rt->type = RT_NORMAL;
810 if(rt->type != RT_NORMAL)
819 if(rt->type == RT_BREAK)
820 rt->type = RT_NORMAL;
826 /* ECMA-262 3rd Edition 12.6.3 */
827 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
829 for_statement_t *stat = (for_statement_t*)_stat;
830 VARIANT val, tmp, retv;
837 if(stat->variable_list) {
838 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
841 }else if(stat->begin_expr) {
842 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
846 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
847 exprval_release(&exprval);
854 V_VT(&retv) = VT_EMPTY;
858 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
862 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
863 exprval_release(&exprval);
864 if(FAILED(hres) || !b)
868 hres = stat_eval(ctx, stat->statement, rt, &tmp);
875 if(rt->type == RT_CONTINUE)
876 rt->type = RT_NORMAL;
877 else if(rt->type != RT_NORMAL)
881 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
885 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
886 exprval_release(&exprval);
899 if(rt->type == RT_BREAK)
900 rt->type = RT_NORMAL;
906 /* ECMA-262 3rd Edition 12.6.4 */
907 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
909 forin_statement_t *stat = (forin_statement_t*)_stat;
910 VARIANT val, name, retv, tmp;
911 DISPID id = DISPID_STARTENUM;
912 BSTR str, identifier = NULL;
920 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
925 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
929 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
930 exprval_release(&exprval);
934 if(V_VT(&val) != VT_DISPATCH) {
935 TRACE("in vt %d\n", V_VT(&val));
937 V_VT(ret) = VT_EMPTY;
941 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
942 IDispatch_Release(V_DISPATCH(&val));
944 TRACE("Object doesn't support IDispatchEx\n");
945 V_VT(ret) = VT_EMPTY;
949 V_VT(&retv) = VT_EMPTY;
952 identifier = SysAllocString(stat->variable->identifier);
955 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
956 if(FAILED(hres) || hres == S_FALSE)
959 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
963 TRACE("iter %s\n", debugstr_w(str));
966 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
968 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
969 if(SUCCEEDED(hres)) {
970 V_VT(&name) = VT_BSTR;
972 hres = put_value(ctx, &exprval, &name, &rt->ei);
973 exprval_release(&exprval);
979 hres = stat_eval(ctx, stat->statement, rt, &tmp);
986 if(rt->type == RT_CONTINUE)
987 rt->type = RT_NORMAL;
988 else if(rt->type != RT_NORMAL)
992 SysFreeString(identifier);
993 IDispatchEx_Release(in_obj);
999 if(rt->type == RT_BREAK)
1000 rt->type = RT_NORMAL;
1006 /* ECMA-262 3rd Edition 12.7 */
1007 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1009 branch_statement_t *stat = (branch_statement_t*)_stat;
1013 if(stat->identifier) {
1014 FIXME("indentifier not implemented\n");
1018 rt->type = RT_CONTINUE;
1019 V_VT(ret) = VT_EMPTY;
1023 /* ECMA-262 3rd Edition 12.8 */
1024 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1026 branch_statement_t *stat = (branch_statement_t*)_stat;
1030 if(stat->identifier) {
1031 FIXME("indentifier not implemented\n");
1035 rt->type = RT_BREAK;
1036 V_VT(ret) = VT_EMPTY;
1040 /* ECMA-262 3rd Edition 12.9 */
1041 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1043 expression_statement_t *stat = (expression_statement_t*)_stat;
1051 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1055 hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1056 exprval_release(&exprval);
1060 V_VT(ret) = VT_EMPTY;
1063 TRACE("= %s\n", debugstr_variant(ret));
1064 rt->type = RT_RETURN;
1068 /* ECMA-262 3rd Edition 12.10 */
1069 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1071 with_statement_t *stat = (with_statement_t*)_stat;
1080 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1084 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1085 exprval_release(&exprval);
1089 hres = to_object(ctx, &val, &disp);
1094 obj = iface_to_jsdisp((IUnknown*)disp);
1095 IDispatch_Release(disp);
1097 FIXME("disp id not jsdisp\n");
1101 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1102 jsdisp_release(obj);
1106 hres = stat_eval(ctx, stat->statement, rt, ret);
1108 scope_pop(&ctx->exec_ctx->scope_chain);
1112 /* ECMA-262 3rd Edition 12.12 */
1113 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1119 /* ECMA-262 3rd Edition 12.13 */
1120 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1122 switch_statement_t *stat = (switch_statement_t*)_stat;
1123 case_clausule_t *iter, *default_clausule = NULL;
1124 statement_t *stat_iter;
1132 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1136 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1137 exprval_release(&exprval);
1141 for(iter = stat->case_list; iter; iter = iter->next) {
1143 default_clausule = iter;
1147 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1151 hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1152 exprval_release(&exprval);
1156 hres = equal2_values(&val, &cval, &b);
1157 VariantClear(&cval);
1158 if(FAILED(hres) || b)
1167 iter = default_clausule;
1169 V_VT(&val) = VT_EMPTY;
1173 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1174 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1181 if(rt->type != RT_NORMAL)
1191 if(rt->type == RT_BREAK)
1192 rt->type = RT_NORMAL;
1198 /* ECMA-262 3rd Edition 12.13 */
1199 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1201 expression_statement_t *stat = (expression_statement_t*)_stat;
1208 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1212 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1213 exprval_release(&exprval);
1218 return DISP_E_EXCEPTION;
1221 /* ECMA-262 3rd Edition 12.14 */
1222 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1229 memset(&rt->ei, 0, sizeof(jsexcept_t));
1231 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1232 if(SUCCEEDED(hres)) {
1233 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1234 if(SUCCEEDED(hres)) {
1235 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1236 if(SUCCEEDED(hres)) {
1237 hres = stat_eval(ctx, block->statement, rt, &val);
1238 scope_pop(&ctx->exec_ctx->scope_chain);
1242 jsdisp_release(var_disp);
1253 /* ECMA-262 3rd Edition 12.14 */
1254 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1256 try_statement_t *stat = (try_statement_t*)_stat;
1262 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1264 TRACE("EXCEPTION\n");
1265 if(!stat->catch_block)
1268 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1273 if(stat->finally_statement) {
1275 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1284 static HRESULT return_bool(exprval_t *ret, DWORD b)
1286 ret->type = EXPRVAL_VARIANT;
1287 V_VT(&ret->u.var) = VT_BOOL;
1288 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1293 static HRESULT get_binary_expr_values(script_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1298 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1302 hres = exprval_to_value(ctx, &exprval, ei, lval);
1303 exprval_release(&exprval);
1307 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1308 if(SUCCEEDED(hres)) {
1309 hres = exprval_to_value(ctx, &exprval, ei, rval);
1310 exprval_release(&exprval);
1321 typedef HRESULT (*oper_t)(script_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1323 static HRESULT binary_expr_eval(script_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1326 VARIANT lval, rval, retv;
1329 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1333 hres = oper(ctx, &lval, &rval, ei, &retv);
1334 VariantClear(&lval);
1335 VariantClear(&rval);
1339 ret->type = EXPRVAL_VARIANT;
1344 /* ECMA-262 3rd Edition 11.13.2 */
1345 static HRESULT assign_oper_eval(script_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1346 jsexcept_t *ei, exprval_t *ret)
1348 VARIANT retv, lval, rval;
1349 exprval_t exprval, exprvalr;
1352 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1356 hres = exprval_value(ctx, &exprval, ei, &lval);
1357 if(SUCCEEDED(hres)) {
1358 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1359 if(SUCCEEDED(hres)) {
1360 hres = exprval_value(ctx, &exprvalr, ei, &rval);
1361 exprval_release(&exprvalr);
1363 if(SUCCEEDED(hres)) {
1364 hres = oper(ctx, &lval, &rval, ei, &retv);
1365 VariantClear(&rval);
1367 VariantClear(&lval);
1370 if(SUCCEEDED(hres)) {
1371 hres = put_value(ctx, &exprval, &retv, ei);
1373 VariantClear(&retv);
1375 exprval_release(&exprval);
1380 ret->type = EXPRVAL_VARIANT;
1385 /* ECMA-262 3rd Edition 13 */
1386 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1388 function_expression_t *expr = (function_expression_t*)_expr;
1394 if(expr->identifier) {
1395 hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1401 hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1402 expr->src_str, expr->src_len, &dispex);
1406 var_set_jsdisp(&var, dispex);
1409 ret->type = EXPRVAL_VARIANT;
1414 /* ECMA-262 3rd Edition 11.12 */
1415 HRESULT conditional_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1417 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1424 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1428 hres = exprval_to_boolean(ctx, &exprval, ei, &b);
1429 exprval_release(&exprval);
1433 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1436 /* ECMA-262 3rd Edition 11.2.1 */
1437 HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1439 array_expression_t *expr = (array_expression_t*)_expr;
1441 VARIANT member, val;
1444 IDispatch *obj = NULL;
1449 hres = expr_eval(ctx, expr->member_expr, 0, ei, &exprval);
1453 hres = exprval_to_value(ctx, &exprval, ei, &member);
1454 exprval_release(&exprval);
1458 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1459 if(SUCCEEDED(hres)) {
1460 hres = exprval_to_value(ctx, &exprval, ei, &val);
1461 exprval_release(&exprval);
1464 if(SUCCEEDED(hres)) {
1465 hres = to_object(ctx, &member, &obj);
1469 VariantClear(&member);
1470 if(SUCCEEDED(hres)) {
1471 hres = to_string(ctx, &val, ei, &str);
1473 if(SUCCEEDED(hres)) {
1474 if(flags & EXPR_STRREF) {
1475 ret->type = EXPRVAL_NAMEREF;
1476 ret->u.nameref.disp = obj;
1477 ret->u.nameref.name = str;
1481 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1485 if(SUCCEEDED(hres)) {
1486 exprval_set_idref(ret, obj, id);
1487 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1492 IDispatch_Release(obj);
1498 /* ECMA-262 3rd Edition 11.2.1 */
1499 HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1501 member_expression_t *expr = (member_expression_t*)_expr;
1502 IDispatch *obj = NULL;
1511 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1515 hres = exprval_to_value(ctx, &exprval, ei, &member);
1516 exprval_release(&exprval);
1520 hres = to_object(ctx, &member, &obj);
1521 VariantClear(&member);
1525 str = SysAllocString(expr->identifier);
1526 if(flags & EXPR_STRREF) {
1527 ret->type = EXPRVAL_NAMEREF;
1528 ret->u.nameref.disp = obj;
1529 ret->u.nameref.name = str;
1533 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1535 if(SUCCEEDED(hres)) {
1536 exprval_set_idref(ret, obj, id);
1537 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1542 IDispatch_Release(obj);
1546 static void free_dp(DISPPARAMS *dp)
1550 for(i=0; i < dp->cArgs; i++)
1551 VariantClear(dp->rgvarg+i);
1552 heap_free(dp->rgvarg);
1555 static HRESULT args_to_param(script_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1561 HRESULT hres = S_OK;
1563 memset(dp, 0, sizeof(*dp));
1567 for(iter = args; iter; iter = iter->next)
1570 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1572 return E_OUTOFMEMORY;
1574 for(i = cnt, iter = args; iter; iter = iter->next) {
1575 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1579 hres = exprval_to_value(ctx, &exprval, ei, vargs + (--i));
1580 exprval_release(&exprval);
1595 /* ECMA-262 3rd Edition 11.2.2 */
1596 HRESULT new_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1598 call_expression_t *expr = (call_expression_t*)_expr;
1600 VARIANT constr, var;
1606 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1610 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1612 hres = exprval_to_value(ctx, &exprval, ei, &constr);
1613 exprval_release(&exprval);
1617 /* NOTE: Should use to_object here */
1619 if(V_VT(&constr) == VT_NULL) {
1620 VariantClear(&constr);
1621 return throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1622 } else if(V_VT(&constr) != VT_DISPATCH) {
1623 VariantClear(&constr);
1624 return throw_type_error(ctx, ei, JS_E_INVALID_ACTION, NULL);
1625 } else if(!V_DISPATCH(&constr)) {
1626 VariantClear(&constr);
1627 return throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1630 hres = disp_call(ctx, V_DISPATCH(&constr), DISPID_VALUE,
1631 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1632 IDispatch_Release(V_DISPATCH(&constr));
1637 ret->type = EXPRVAL_VARIANT;
1642 /* ECMA-262 3rd Edition 11.2.3 */
1643 HRESULT call_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1645 call_expression_t *expr = (call_expression_t*)_expr;
1653 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1657 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1658 if(SUCCEEDED(hres)) {
1659 switch(exprval.type) {
1660 case EXPRVAL_VARIANT:
1661 if(V_VT(&exprval.u.var) == VT_DISPATCH)
1662 hres = disp_call(ctx, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1663 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1665 hres = throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1668 hres = disp_call(ctx, exprval.u.idref.disp, exprval.u.idref.id,
1669 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1671 case EXPRVAL_INVALID:
1672 hres = throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1675 FIXME("unimplemented type %d\n", exprval.type);
1682 exprval_release(&exprval);
1686 ret->type = EXPRVAL_VARIANT;
1687 if(flags & EXPR_NOVAL) {
1688 V_VT(&ret->u.var) = VT_EMPTY;
1690 TRACE("= %s\n", debugstr_variant(&var));
1696 /* ECMA-262 3rd Edition 11.1.1 */
1697 HRESULT this_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1701 ret->type = EXPRVAL_VARIANT;
1702 V_VT(&ret->u.var) = VT_DISPATCH;
1703 V_DISPATCH(&ret->u.var) = ctx->exec_ctx->this_obj;
1704 IDispatch_AddRef(ctx->exec_ctx->this_obj);
1708 /* ECMA-262 3rd Edition 10.1.4 */
1709 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1711 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1717 identifier = SysAllocString(expr->identifier);
1719 return E_OUTOFMEMORY;
1721 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1723 SysFreeString(identifier);
1727 /* ECMA-262 3rd Edition 7.8.1 */
1728 HRESULT interp_null(exec_ctx_t *ctx)
1735 return stack_push(ctx, &v);
1738 /* ECMA-262 3rd Edition 7.8.2 */
1739 HRESULT interp_bool(exec_ctx_t *ctx)
1741 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1743 TRACE("%s\n", arg ? "true" : "false");
1745 return stack_push_bool(ctx, arg);
1748 /* ECMA-262 3rd Edition 7.8.3 */
1749 HRESULT interp_int(exec_ctx_t *ctx)
1751 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1758 return stack_push(ctx, &v);
1761 /* ECMA-262 3rd Edition 7.8.3 */
1762 HRESULT interp_double(exec_ctx_t *ctx)
1764 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1767 TRACE("%lf\n", arg);
1771 return stack_push(ctx, &v);
1774 /* ECMA-262 3rd Edition 7.8.4 */
1775 HRESULT interp_str(exec_ctx_t *ctx)
1777 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1780 TRACE("%s\n", debugstr_w(str));
1783 V_BSTR(&v) = SysAllocString(str);
1785 return E_OUTOFMEMORY;
1787 return stack_push(ctx, &v);
1790 /* ECMA-262 3rd Edition 7.8 */
1791 HRESULT interp_regexp(exec_ctx_t *ctx)
1793 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1794 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1799 TRACE("%s %x\n", debugstr_w(source), flags);
1801 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, ®exp);
1805 var_set_jsdisp(&v, regexp);
1806 return stack_push(ctx, &v);
1809 /* ECMA-262 3rd Edition 11.1.4 */
1810 HRESULT array_literal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1812 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1813 DWORD length = 0, i = 0;
1814 array_element_t *elem;
1822 for(elem = expr->element_list; elem; elem = elem->next)
1823 length += elem->elision+1;
1824 length += expr->length;
1826 hres = create_array(ctx, length, &array);
1830 for(elem = expr->element_list; elem; elem = elem->next) {
1833 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1837 hres = exprval_to_value(ctx, &exprval, ei, &val);
1838 exprval_release(&exprval);
1842 hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
1851 jsdisp_release(array);
1855 ret->type = EXPRVAL_VARIANT;
1856 var_set_jsdisp(&ret->u.var, array);
1860 /* ECMA-262 3rd Edition 11.1.5 */
1861 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1863 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1873 hres = create_object(ctx, NULL, &obj);
1877 for(iter = expr->property_list; iter; iter = iter->next) {
1878 hres = literal_to_var(ctx, iter->name, &tmp);
1882 hres = to_string(ctx, &tmp, ei, &name);
1887 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1888 if(SUCCEEDED(hres)) {
1889 hres = exprval_to_value(ctx, &exprval, ei, &val);
1890 exprval_release(&exprval);
1891 if(SUCCEEDED(hres)) {
1892 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
1897 SysFreeString(name);
1903 jsdisp_release(obj);
1907 ret->type = EXPRVAL_VARIANT;
1908 var_set_jsdisp(&ret->u.var, obj);
1912 /* ECMA-262 3rd Edition 11.14 */
1913 HRESULT comma_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1915 binary_expression_t *expr = (binary_expression_t*)_expr;
1921 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1925 VariantClear(&lval);
1927 ret->type = EXPRVAL_VARIANT;
1932 /* ECMA-262 3rd Edition 11.11 */
1933 HRESULT logical_or_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1935 binary_expression_t *expr = (binary_expression_t*)_expr;
1943 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1947 hres = exprval_to_value(ctx, &exprval, ei, &val);
1948 exprval_release(&exprval);
1952 hres = to_boolean(&val, &b);
1953 if(SUCCEEDED(hres) && b) {
1954 ret->type = EXPRVAL_VARIANT;
1963 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1967 hres = exprval_to_value(ctx, &exprval, ei, &val);
1968 exprval_release(&exprval);
1972 ret->type = EXPRVAL_VARIANT;
1977 /* ECMA-262 3rd Edition 11.11 */
1978 HRESULT logical_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1980 binary_expression_t *expr = (binary_expression_t*)_expr;
1988 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1992 hres = exprval_to_value(ctx, &exprval, ei, &val);
1993 exprval_release(&exprval);
1997 hres = to_boolean(&val, &b);
1998 if(SUCCEEDED(hres) && !b) {
1999 ret->type = EXPRVAL_VARIANT;
2008 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
2012 hres = exprval_to_value(ctx, &exprval, ei, &val);
2013 exprval_release(&exprval);
2017 ret->type = EXPRVAL_VARIANT;
2022 /* ECMA-262 3rd Edition 11.10 */
2023 static HRESULT bitor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2028 hres = to_int32(ctx, lval, ei, &li);
2032 hres = to_int32(ctx, rval, ei, &ri);
2041 /* ECMA-262 3rd Edition 11.10 */
2042 HRESULT binary_or_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2044 binary_expression_t *expr = (binary_expression_t*)_expr;
2048 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
2051 /* ECMA-262 3rd Edition 11.10 */
2052 static HRESULT xor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2057 hres = to_int32(ctx, lval, ei, &li);
2061 hres = to_int32(ctx, rval, ei, &ri);
2070 /* ECMA-262 3rd Edition 11.10 */
2071 HRESULT binary_xor_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2073 binary_expression_t *expr = (binary_expression_t*)_expr;
2077 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
2080 /* ECMA-262 3rd Edition 11.10 */
2081 static HRESULT bitand_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2086 hres = to_int32(ctx, lval, ei, &li);
2090 hres = to_int32(ctx, rval, ei, &ri);
2099 /* ECMA-262 3rd Edition 11.10 */
2100 HRESULT binary_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2102 binary_expression_t *expr = (binary_expression_t*)_expr;
2106 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
2109 /* ECMA-262 3rd Edition 11.8.6 */
2110 static HRESULT instanceof_eval(script_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
2112 jsdisp_t *obj, *iter, *tmp = NULL;
2113 VARIANT_BOOL ret = VARIANT_FALSE;
2118 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2120 if(V_VT(objv) != VT_DISPATCH || !V_DISPATCH(objv))
2121 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2123 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2125 FIXME("non-jsdisp objects not supported\n");
2129 if(is_class(obj, JSCLASS_FUNCTION)) {
2130 hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2132 hres = throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2134 jsdisp_release(obj);
2138 if(V_VT(&var) == VT_DISPATCH) {
2139 if(V_VT(inst) == VT_DISPATCH)
2140 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2141 for(iter = tmp; iter; iter = iter->prototype) {
2142 hres = disp_cmp(V_DISPATCH(&var), to_disp(iter), &b);
2152 jsdisp_release(tmp);
2154 FIXME("prototype is not an object\n");
2162 V_VT(retv) = VT_BOOL;
2167 /* ECMA-262 3rd Edition 11.8.6 */
2168 HRESULT instanceof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2170 binary_expression_t *expr = (binary_expression_t*)_expr;
2174 return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2177 /* ECMA-262 3rd Edition 11.8.7 */
2178 HRESULT interp_in(exec_ctx_t *ctx)
2188 obj = stack_pop(ctx);
2191 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2194 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2197 hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2200 IDispatch_Release(V_DISPATCH(obj));
2204 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2205 IDispatch_Release(V_DISPATCH(obj));
2209 else if(hres == DISP_E_UNKNOWNNAME)
2214 return stack_push_bool(ctx, ret);
2217 /* ECMA-262 3rd Edition 11.6.1 */
2218 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2223 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2227 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2233 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2234 BSTR lstr = NULL, rstr = NULL;
2236 if(V_VT(&l) == VT_BSTR)
2239 hres = to_string(ctx, &l, ei, &lstr);
2241 if(SUCCEEDED(hres)) {
2242 if(V_VT(&r) == VT_BSTR)
2245 hres = to_string(ctx, &r, ei, &rstr);
2248 if(SUCCEEDED(hres)) {
2251 len1 = SysStringLen(lstr);
2252 len2 = SysStringLen(rstr);
2254 V_VT(retv) = VT_BSTR;
2255 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2256 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2257 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2260 if(V_VT(&l) != VT_BSTR)
2261 SysFreeString(lstr);
2262 if(V_VT(&r) != VT_BSTR)
2263 SysFreeString(rstr);
2267 hres = to_number(ctx, &l, ei, &nl);
2268 if(SUCCEEDED(hres)) {
2269 hres = to_number(ctx, &r, ei, &nr);
2271 num_set_val(retv, num_val(&nl) + num_val(&nr));
2280 /* ECMA-262 3rd Edition 11.6.1 */
2281 HRESULT interp_add(exec_ctx_t *ctx)
2283 VARIANT *l, *r, ret;
2289 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2291 hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2295 return stack_push(ctx, &ret);
2298 /* ECMA-262 3rd Edition 11.6.2 */
2299 static HRESULT sub_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2304 hres = to_number(ctx, lval, ei, &lnum);
2308 hres = to_number(ctx, rval, ei, &rnum);
2312 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
2316 /* ECMA-262 3rd Edition 11.6.2 */
2317 HRESULT sub_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2319 binary_expression_t *expr = (binary_expression_t*)_expr;
2323 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
2326 /* ECMA-262 3rd Edition 11.5.1 */
2327 static HRESULT mul_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2332 hres = to_number(ctx, lval, ei, &lnum);
2336 hres = to_number(ctx, rval, ei, &rnum);
2340 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2344 /* ECMA-262 3rd Edition 11.5.1 */
2345 HRESULT mul_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2347 binary_expression_t *expr = (binary_expression_t*)_expr;
2351 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2354 /* ECMA-262 3rd Edition 11.5.2 */
2355 static HRESULT div_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2360 hres = to_number(ctx, lval, ei, &lnum);
2364 hres = to_number(ctx, rval, ei, &rnum);
2368 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2372 /* ECMA-262 3rd Edition 11.5.2 */
2373 HRESULT div_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2375 binary_expression_t *expr = (binary_expression_t*)_expr;
2379 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2382 /* ECMA-262 3rd Edition 11.5.3 */
2383 static HRESULT mod_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2388 hres = to_number(ctx, lval, ei, &lnum);
2392 hres = to_number(ctx, rval, ei, &rnum);
2396 num_set_val(retv, fmod(num_val(&lnum), num_val(&rnum)));
2400 /* ECMA-262 3rd Edition 11.5.3 */
2401 HRESULT mod_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2403 binary_expression_t *expr = (binary_expression_t*)_expr;
2407 return binary_expr_eval(ctx, expr, mod_eval, ei, ret);
2410 /* ECMA-262 3rd Edition 11.4.2 */
2411 HRESULT delete_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2413 unary_expression_t *expr = (unary_expression_t*)_expr;
2414 VARIANT_BOOL b = VARIANT_FALSE;
2420 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2424 switch(exprval.type) {
2425 case EXPRVAL_IDREF: {
2426 IDispatchEx *dispex;
2428 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2429 if(SUCCEEDED(hres)) {
2430 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2432 IDispatchEx_Release(dispex);
2436 case EXPRVAL_NAMEREF: {
2437 IDispatchEx *dispex;
2439 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2440 if(SUCCEEDED(hres)) {
2441 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name,
2442 make_grfdex(ctx, fdexNameCaseSensitive));
2444 IDispatchEx_Release(dispex);
2449 FIXME("unsupported type %d\n", exprval.type);
2453 exprval_release(&exprval);
2457 return return_bool(ret, b);
2460 /* ECMA-262 3rd Edition 11.4.2 */
2461 HRESULT void_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2463 unary_expression_t *expr = (unary_expression_t*)_expr;
2470 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2474 hres = exprval_to_value(ctx, &exprval, ei, &tmp);
2475 exprval_release(&exprval);
2481 ret->type = EXPRVAL_VARIANT;
2482 V_VT(&ret->u.var) = VT_EMPTY;
2486 /* ECMA-262 3rd Edition 11.4.3 */
2487 static HRESULT typeof_exprval(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2492 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2493 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2494 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2495 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2496 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2497 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2498 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
2500 if(exprval->type == EXPRVAL_INVALID) {
2505 hres = exprval_to_value(ctx, exprval, ei, &val);
2507 if(exprval->type == EXPRVAL_IDREF) {
2514 switch(V_VT(&val)) {
2534 if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2535 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2536 jsdisp_release(dispex);
2543 FIXME("unhandled vt %d\n", V_VT(&val));
2551 HRESULT typeof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2553 unary_expression_t *expr = (unary_expression_t*)_expr;
2554 const WCHAR *str = NULL;
2560 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2564 hres = typeof_exprval(ctx, &exprval, ei, &str);
2565 exprval_release(&exprval);
2569 ret->type = EXPRVAL_VARIANT;
2570 V_VT(&ret->u.var) = VT_BSTR;
2571 V_BSTR(&ret->u.var) = SysAllocString(str);
2572 if(!V_BSTR(&ret->u.var))
2573 return E_OUTOFMEMORY;
2578 /* ECMA-262 3rd Edition 11.4.7 */
2579 HRESULT minus_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2581 unary_expression_t *expr = (unary_expression_t*)_expr;
2588 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2592 hres = exprval_to_value(ctx, &exprval, ei, &val);
2593 exprval_release(&exprval);
2597 hres = to_number(ctx, &val, ei, &num);
2602 ret->type = EXPRVAL_VARIANT;
2603 num_set_val(&ret->u.var, -num_val(&num));
2607 /* ECMA-262 3rd Edition 11.4.6 */
2608 HRESULT interp_tonum(exec_ctx_t *ctx)
2616 hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2621 return stack_push(ctx, &num);
2624 /* ECMA-262 3rd Edition 11.3.1 */
2625 HRESULT post_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2627 unary_expression_t *expr = (unary_expression_t*)_expr;
2634 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2638 hres = exprval_value(ctx, &exprval, ei, &val);
2639 if(SUCCEEDED(hres)) {
2640 hres = to_number(ctx, &val, ei, &num);
2644 if(SUCCEEDED(hres)) {
2646 num_set_val(&inc, num_val(&num)+1.0);
2647 hres = put_value(ctx, &exprval, &inc, ei);
2650 exprval_release(&exprval);
2654 ret->type = EXPRVAL_VARIANT;
2659 /* ECMA-262 3rd Edition 11.3.2 */
2660 HRESULT post_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2662 unary_expression_t *expr = (unary_expression_t*)_expr;
2669 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2673 hres = exprval_value(ctx, &exprval, ei, &val);
2674 if(SUCCEEDED(hres)) {
2675 hres = to_number(ctx, &val, ei, &num);
2679 if(SUCCEEDED(hres)) {
2681 num_set_val(&dec, num_val(&num)-1.0);
2682 hres = put_value(ctx, &exprval, &dec, ei);
2685 exprval_release(&exprval);
2689 ret->type = EXPRVAL_VARIANT;
2694 /* ECMA-262 3rd Edition 11.4.4 */
2695 HRESULT pre_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2697 unary_expression_t *expr = (unary_expression_t*)_expr;
2704 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2708 hres = exprval_value(ctx, &exprval, ei, &val);
2709 if(SUCCEEDED(hres)) {
2710 hres = to_number(ctx, &val, ei, &num);
2714 if(SUCCEEDED(hres)) {
2715 num_set_val(&val, num_val(&num)+1.0);
2716 hres = put_value(ctx, &exprval, &val, ei);
2719 exprval_release(&exprval);
2723 ret->type = EXPRVAL_VARIANT;
2728 /* ECMA-262 3rd Edition 11.4.5 */
2729 HRESULT pre_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2731 unary_expression_t *expr = (unary_expression_t*)_expr;
2738 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2742 hres = exprval_value(ctx, &exprval, ei, &val);
2743 if(SUCCEEDED(hres)) {
2744 hres = to_number(ctx, &val, ei, &num);
2748 if(SUCCEEDED(hres)) {
2749 num_set_val(&val, num_val(&num)-1.0);
2750 hres = put_value(ctx, &exprval, &val, ei);
2753 exprval_release(&exprval);
2757 ret->type = EXPRVAL_VARIANT;
2762 /* ECMA-262 3rd Edition 11.9.3 */
2763 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2765 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2766 return equal2_values(lval, rval, ret);
2768 /* FIXME: NULL disps should be handled in more general way */
2769 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2772 return equal_values(ctx, &v, rval, ei, ret);
2775 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2778 return equal_values(ctx, lval, &v, ei, ret);
2781 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2782 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2787 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2791 hres = to_number(ctx, lval, ei, &v);
2795 return equal_values(ctx, &v, rval, ei, ret);
2798 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2802 hres = to_number(ctx, rval, ei, &v);
2806 return equal_values(ctx, lval, &v, ei, ret);
2809 if(V_VT(rval) == VT_BOOL) {
2813 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2814 return equal_values(ctx, lval, &v, ei, ret);
2817 if(V_VT(lval) == VT_BOOL) {
2821 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2822 return equal_values(ctx, &v, rval, ei, ret);
2826 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2830 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2834 hres = equal_values(ctx, lval, &v, ei, ret);
2841 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2845 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2849 hres = equal_values(ctx, &v, rval, ei, ret);
2860 /* ECMA-262 3rd Edition 11.9.1 */
2861 HRESULT equal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2863 binary_expression_t *expr = (binary_expression_t*)_expr;
2870 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2874 hres = equal_values(ctx, &rval, &lval, ei, &b);
2875 VariantClear(&lval);
2876 VariantClear(&rval);
2880 return return_bool(ret, b);
2883 /* ECMA-262 3rd Edition 11.9.4 */
2884 static HRESULT interp_eq2(exec_ctx_t *ctx)
2895 hres = equal2_values(r, l, &b);
2901 return stack_push_bool(ctx, b);
2904 /* ECMA-262 3rd Edition 11.9.2 */
2905 HRESULT not_equal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2907 binary_expression_t *expr = (binary_expression_t*)_expr;
2914 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2918 hres = equal_values(ctx, &lval, &rval, ei, &b);
2919 VariantClear(&lval);
2920 VariantClear(&rval);
2924 return return_bool(ret, !b);
2927 /* ECMA-262 3rd Edition 11.9.5 */
2928 static HRESULT interp_neq2(exec_ctx_t *ctx)
2939 hres = equal2_values(r, l, &b);
2945 return stack_push_bool(ctx, !b);
2948 /* ECMA-262 3rd Edition 11.8.5 */
2949 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2951 VARIANT l, r, ln, rn;
2954 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2958 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2964 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2965 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2966 SysFreeString(V_BSTR(&l));
2967 SysFreeString(V_BSTR(&r));
2971 hres = to_number(ctx, &l, ei, &ln);
2974 hres = to_number(ctx, &r, ei, &rn);
2979 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2980 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2982 DOUBLE ld = num_val(&ln);
2983 DOUBLE rd = num_val(&rn);
2985 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2991 /* ECMA-262 3rd Edition 11.8.1 */
2992 HRESULT less_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2994 binary_expression_t *expr = (binary_expression_t*)_expr;
3001 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3005 hres = less_eval(ctx, &lval, &rval, FALSE, ei, &b);
3006 VariantClear(&lval);
3007 VariantClear(&rval);
3011 return return_bool(ret, b);
3014 /* ECMA-262 3rd Edition 11.8.3 */
3015 HRESULT lesseq_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3017 binary_expression_t *expr = (binary_expression_t*)_expr;
3024 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3028 hres = less_eval(ctx, &rval, &lval, TRUE, ei, &b);
3029 VariantClear(&lval);
3030 VariantClear(&rval);
3034 return return_bool(ret, b);
3037 /* ECMA-262 3rd Edition 11.8.2 */
3038 HRESULT greater_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3040 binary_expression_t *expr = (binary_expression_t*)_expr;
3047 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3051 hres = less_eval(ctx, &rval, &lval, FALSE, ei, &b);
3052 VariantClear(&lval);
3053 VariantClear(&rval);
3057 return return_bool(ret, b);
3060 /* ECMA-262 3rd Edition 11.8.4 */
3061 HRESULT greatereq_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3063 binary_expression_t *expr = (binary_expression_t*)_expr;
3070 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3074 hres = less_eval(ctx, &lval, &rval, TRUE, ei, &b);
3075 VariantClear(&lval);
3076 VariantClear(&rval);
3080 return return_bool(ret, b);
3083 /* ECMA-262 3rd Edition 11.4.8 */
3084 HRESULT interp_bneg(exec_ctx_t *ctx)
3093 hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3100 return stack_push(ctx, &r);
3103 /* ECMA-262 3rd Edition 11.4.9 */
3104 HRESULT interp_neg(exec_ctx_t *ctx)
3113 hres = to_boolean(v, &b);
3118 return stack_push_bool(ctx, !b);
3121 /* ECMA-262 3rd Edition 11.7.1 */
3122 static HRESULT lshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3128 hres = to_int32(ctx, lval, ei, &li);
3132 hres = to_uint32(ctx, rval, ei, &ri);
3137 V_I4(retv) = li << (ri&0x1f);
3141 /* ECMA-262 3rd Edition 11.7.1 */
3142 HRESULT left_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3144 binary_expression_t *expr = (binary_expression_t*)_expr;
3148 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3151 /* ECMA-262 3rd Edition 11.7.2 */
3152 static HRESULT rshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3158 hres = to_int32(ctx, lval, ei, &li);
3162 hres = to_uint32(ctx, rval, ei, &ri);
3167 V_I4(retv) = li >> (ri&0x1f);
3171 /* ECMA-262 3rd Edition 11.7.2 */
3172 HRESULT right_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3174 binary_expression_t *expr = (binary_expression_t*)_expr;
3178 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3181 /* ECMA-262 3rd Edition 11.7.3 */
3182 static HRESULT rshift2_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3187 hres = to_uint32(ctx, lval, ei, &li);
3191 hres = to_uint32(ctx, rval, ei, &ri);
3196 V_I4(retv) = li >> (ri&0x1f);
3200 /* ECMA-262 3rd Edition 11.7.3 */
3201 HRESULT right2_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3203 binary_expression_t *expr = (binary_expression_t*)_expr;
3207 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3210 /* ECMA-262 3rd Edition 11.13.1 */
3211 HRESULT assign_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3213 binary_expression_t *expr = (binary_expression_t*)_expr;
3214 exprval_t exprval, exprvalr;
3220 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
3224 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
3225 if(SUCCEEDED(hres)) {
3226 hres = exprval_to_value(ctx, &exprvalr, ei, &rval);
3227 exprval_release(&exprvalr);
3230 if(SUCCEEDED(hres)) {
3231 hres = put_value(ctx, &exprval, &rval, ei);
3233 VariantClear(&rval);
3236 exprval_release(&exprval);
3240 ret->type = EXPRVAL_VARIANT;
3245 /* ECMA-262 3rd Edition 11.13.2 */
3246 HRESULT assign_lshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3248 binary_expression_t *expr = (binary_expression_t*)_expr;
3252 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3255 /* ECMA-262 3rd Edition 11.13.2 */
3256 HRESULT assign_rshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3258 binary_expression_t *expr = (binary_expression_t*)_expr;
3262 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3265 /* ECMA-262 3rd Edition 11.13.2 */
3266 HRESULT assign_rrshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3268 binary_expression_t *expr = (binary_expression_t*)_expr;
3272 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3275 /* ECMA-262 3rd Edition 11.13.2 */
3276 HRESULT assign_add_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3278 binary_expression_t *expr = (binary_expression_t*)_expr;
3282 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
3285 /* ECMA-262 3rd Edition 11.13.2 */
3286 HRESULT assign_sub_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3288 binary_expression_t *expr = (binary_expression_t*)_expr;
3292 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
3295 /* ECMA-262 3rd Edition 11.13.2 */
3296 HRESULT assign_mul_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3298 binary_expression_t *expr = (binary_expression_t*)_expr;
3302 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
3305 /* ECMA-262 3rd Edition 11.13.2 */
3306 HRESULT assign_div_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3308 binary_expression_t *expr = (binary_expression_t*)_expr;
3312 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
3315 /* ECMA-262 3rd Edition 11.13.2 */
3316 HRESULT assign_mod_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3318 binary_expression_t *expr = (binary_expression_t*)_expr;
3322 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mod_eval, ei, ret);
3325 /* ECMA-262 3rd Edition 11.13.2 */
3326 HRESULT assign_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3328 binary_expression_t *expr = (binary_expression_t*)_expr;
3332 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3335 /* ECMA-262 3rd Edition 11.13.2 */
3336 HRESULT assign_or_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3338 binary_expression_t *expr = (binary_expression_t*)_expr;
3342 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
3345 /* ECMA-262 3rd Edition 11.13.2 */
3346 HRESULT assign_xor_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3348 binary_expression_t *expr = (binary_expression_t*)_expr;
3352 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);
3355 static HRESULT interp_ret(exec_ctx_t *ctx)
3363 static HRESULT interp_tree(exec_ctx_t *ctx)
3365 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3372 hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3376 hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3380 return stack_push(ctx, &v);
3383 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3385 static const op_func_t op_funcs[] = {
3386 #define X(x,a,b,c) interp_##x,
3391 static const unsigned op_move[] = {
3392 #define X(a,x,b,c) x,
3397 HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3399 exec_ctx_t *exec_ctx = ctx->exec_ctx;
3400 unsigned prev_ip, prev_top;
3402 HRESULT hres = S_OK;
3406 prev_top = exec_ctx->top;
3407 prev_ip = exec_ctx->ip;
3408 exec_ctx->ip = expr->instr_off;
3410 while(exec_ctx->ip != -1) {
3411 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3412 hres = op_funcs[op](exec_ctx);
3415 exec_ctx->ip += op_move[op];
3418 exec_ctx->ip = prev_ip;
3421 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3423 memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3427 assert(exec_ctx->top == prev_top+1);
3429 ret->type = EXPRVAL_VARIANT;
3430 ret->u.var = *stack_pop(exec_ctx);
3434 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3440 hres = compile_subscript(ctx->exec_ctx->parser, expr, &expr->instr_off);
3444 return (expr->eval = interp_expression_eval)(ctx, expr, flags, ei, ret);