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
35 struct _return_type_t {
45 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
47 return stat->eval(ctx, stat, rt, ret);
50 static inline HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
52 return expr->eval(ctx, expr, flags, ei, ret);
55 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
57 if(!ctx->stack_size) {
58 ctx->stack = heap_alloc(16*sizeof(VARIANT));
62 }else if(ctx->stack_size == ctx->top) {
65 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
71 ctx->stack = new_stack;
75 ctx->stack[ctx->top++] = *v;
79 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
84 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
85 return stack_push(ctx, &v);
88 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
92 num_set_val(&v, number);
93 return stack_push(ctx, &v);
96 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
102 return stack_push(ctx, &v);
105 static inline VARIANT *stack_top(exec_ctx_t *ctx)
108 return ctx->stack + ctx->top-1;
111 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
113 assert(ctx->top > n);
114 return ctx->stack + ctx->top-1-n;
117 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
120 return ctx->stack + --ctx->top;
123 static void stack_popn(exec_ctx_t *ctx, unsigned n)
126 VariantClear(stack_pop(ctx));
129 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
135 hres = to_number(ctx->parser->script, v, &ctx->ei, r);
140 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
142 return to_int32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
145 static void exprval_release(exprval_t *val)
148 case EXPRVAL_VARIANT:
149 if(V_VT(&val->u.var) != VT_EMPTY)
150 VariantClear(&val->u.var);
153 if(val->u.idref.disp)
154 IDispatch_Release(val->u.idref.disp);
156 case EXPRVAL_INVALID:
157 SysFreeString(val->u.identifier);
161 /* ECMA-262 3rd Edition 8.7.1 */
162 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
164 V_VT(ret) = VT_EMPTY;
167 case EXPRVAL_VARIANT:
168 return VariantCopy(ret, &val->u.var);
170 if(!val->u.idref.disp) {
171 FIXME("throw ReferenceError\n");
175 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
176 case EXPRVAL_INVALID:
177 return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
180 ERR("type %d\n", val->type);
184 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
186 if(val->type == EXPRVAL_VARIANT) {
188 V_VT(&val->u.var) = VT_EMPTY;
192 return exprval_value(ctx, val, ei, ret);
195 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
197 if(exprval->type != EXPRVAL_VARIANT) {
201 hres = exprval_to_value(ctx, exprval, ei, &val);
205 hres = to_boolean(&val, b);
210 return to_boolean(&exprval->u.var, b);
213 static void exprval_init(exprval_t *val)
215 val->type = EXPRVAL_VARIANT;
216 V_VT(&val->u.var) = VT_EMPTY;
219 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
221 val->type = EXPRVAL_IDREF;
222 val->u.idref.disp = disp;
223 val->u.idref.id = id;
226 IDispatch_AddRef(disp);
229 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
231 scope_chain_t *new_scope;
233 new_scope = heap_alloc(sizeof(scope_chain_t));
235 return E_OUTOFMEMORY;
240 new_scope->obj = obj;
244 new_scope->next = scope;
246 new_scope->next = NULL;
253 static void scope_pop(scope_chain_t **scope)
262 void scope_release(scope_chain_t *scope)
268 scope_release(scope->next);
270 jsdisp_release(scope->obj);
274 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
275 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
279 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
281 return E_OUTOFMEMORY;
284 ctx->is_global = is_global;
287 ctx->this_obj = this_obj;
288 else if(script_ctx->host_global)
289 ctx->this_obj = script_ctx->host_global;
291 ctx->this_obj = to_disp(script_ctx->global);
292 IDispatch_AddRef(ctx->this_obj);
294 jsdisp_addref(var_disp);
295 ctx->var_disp = var_disp;
299 ctx->scope_chain = scope;
306 void exec_release(exec_ctx_t *ctx)
312 scope_release(ctx->scope_chain);
314 jsdisp_release(ctx->var_disp);
316 IDispatch_Release(ctx->this_obj);
317 heap_free(ctx->stack);
321 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
326 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
328 TRACE("unsing IDispatch\n");
331 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
335 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
336 IDispatchEx_Release(dispex);
340 /* ECMA-262 3rd Edition 8.7.2 */
341 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
343 if(ref->type != EXPRVAL_IDREF)
344 return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
346 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
349 static inline BOOL is_null(const VARIANT *v)
351 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
354 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
356 IObjectIdentity *identity;
357 IUnknown *unk1, *unk2;
365 if(!disp1 || !disp2) {
370 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
374 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
376 IUnknown_Release(unk1);
383 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
384 if(SUCCEEDED(hres)) {
385 hres = IObjectIdentity_IsEqualObject(identity, unk2);
386 IObjectIdentity_Release(identity);
393 IUnknown_Release(unk1);
394 IUnknown_Release(unk2);
398 /* ECMA-262 3rd Edition 11.9.6 */
399 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
403 if(V_VT(lval) != V_VT(rval)) {
404 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
405 *ret = num_val(lval) == num_val(rval);
406 else if(is_null(lval))
407 *ret = is_null(rval);
419 *ret = V_I4(lval) == V_I4(rval);
422 *ret = V_R8(lval) == V_R8(rval);
426 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
427 else if(!V_BSTR(rval))
428 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
430 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
433 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
435 *ret = !V_BOOL(lval) == !V_BOOL(rval);
438 FIXME("unimplemented vt %d\n", V_VT(lval));
445 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
447 switch(literal->type) {
453 V_I4(v) = literal->u.lval;
457 V_R8(v) = literal->u.dval;
460 BSTR str = SysAllocString(literal->u.wstr);
462 return E_OUTOFMEMORY;
470 V_BOOL(v) = literal->u.bval;
476 hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
477 literal->u.regexp.flags, ®exp);
481 var_set_jsdisp(v, regexp);
488 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
494 for(item = ctx->named_items; item; item = item->next) {
495 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
496 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
497 if(SUCCEEDED(hres)) {
499 exprval_set_idref(ret, item->disp, id);
508 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
509 jsexcept_t *ei, VARIANT *retv)
511 script_ctx_t *script = parser->script;
512 function_declaration_t *func;
513 parser_ctx_t *prev_parser;
517 exec_ctx_t *prev_ctx;
521 for(func = source->functions; func; func = func->next) {
525 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
526 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
530 var_set_jsdisp(&var, func_obj);
531 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
532 jsdisp_release(func_obj);
537 for(var = source->variables; var; var = var->next) {
541 name = SysAllocString(var->identifier);
543 return E_OUTOFMEMORY;
545 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
546 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
552 prev_ctx = script->exec_ctx;
553 script->exec_ctx = ctx;
555 prev_parser = ctx->parser;
556 ctx->parser = parser;
558 V_VT(&val) = VT_EMPTY;
559 memset(&rt, 0, sizeof(rt));
562 for(stat = source->statement; stat; stat = stat->next) {
563 hres = stat_eval(script, stat, &rt, &tmp);
569 if(rt.type != RT_NORMAL)
573 script->exec_ctx = prev_ctx;
574 ctx->parser = prev_parser;
576 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
577 FIXME("wrong rt %d\n", rt.type);
587 if(!retv || (!from_eval && rt.type != RT_RETURN))
594 /* ECMA-262 3rd Edition 10.1.4 */
595 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
597 scope_chain_t *scope;
602 TRACE("%s\n", debugstr_w(identifier));
604 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
605 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
606 if(SUCCEEDED(hres)) {
607 exprval_set_idref(ret, to_disp(scope->obj), id);
612 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
613 if(SUCCEEDED(hres)) {
614 exprval_set_idref(ret, to_disp(ctx->global), id);
618 for(item = ctx->named_items; item; item = item->next) {
619 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
626 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
627 SCRIPTINFO_IUNKNOWN, &unk, NULL);
629 WARN("GetItemInfo failed: %08x\n", hres);
633 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
634 IUnknown_Release(unk);
636 WARN("object does not implement IDispatch\n");
641 ret->type = EXPRVAL_VARIANT;
642 V_VT(&ret->u.var) = VT_DISPATCH;
643 V_DISPATCH(&ret->u.var) = item->disp;
644 IDispatch_AddRef(item->disp);
649 if(lookup_global_members(ctx, identifier, ret))
652 if(flags & EXPR_NEWREF) {
653 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
657 exprval_set_idref(ret, to_disp(ctx->global), id);
661 ret->type = EXPRVAL_INVALID;
662 ret->u.identifier = SysAllocString(identifier);
663 if(!ret->u.identifier)
664 return E_OUTOFMEMORY;
669 /* ECMA-262 3rd Edition 12.1 */
670 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
672 block_statement_t *stat = (block_statement_t*)_stat;
679 V_VT(&val) = VT_EMPTY;
680 for(iter = stat->stat_list; iter; iter = iter->next) {
681 hres = stat_eval(ctx, iter, rt, &tmp);
687 if(rt->type != RT_NORMAL)
700 /* ECMA-262 3rd Edition 12.2 */
701 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
703 variable_declaration_t *iter;
706 for(iter = var_list; iter; iter = iter->next) {
713 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
717 hres = exprval_to_value(ctx, &exprval, ei, &val);
718 exprval_release(&exprval);
722 hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
731 /* ECMA-262 3rd Edition 12.2 */
732 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
734 var_statement_t *stat = (var_statement_t*)_stat;
739 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
743 V_VT(ret) = VT_EMPTY;
747 /* ECMA-262 3rd Edition 12.3 */
748 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
752 V_VT(ret) = VT_EMPTY;
756 /* ECMA-262 3rd Edition 12.4 */
757 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
759 expression_statement_t *stat = (expression_statement_t*)_stat;
766 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
770 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
771 exprval_release(&exprval);
776 TRACE("= %s\n", debugstr_variant(ret));
780 /* ECMA-262 3rd Edition 12.5 */
781 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
783 if_statement_t *stat = (if_statement_t*)_stat;
790 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
794 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
795 exprval_release(&exprval);
800 hres = stat_eval(ctx, stat->if_stat, rt, ret);
801 else if(stat->else_stat)
802 hres = stat_eval(ctx, stat->else_stat, rt, ret);
804 V_VT(ret) = VT_EMPTY;
809 /* ECMA-262 3rd Edition 12.6.2 */
810 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
812 while_statement_t *stat = (while_statement_t*)_stat;
821 V_VT(&val) = VT_EMPTY;
822 test_expr = !stat->do_while;
826 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
830 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
831 exprval_release(&exprval);
832 if(FAILED(hres) || !b)
838 hres = stat_eval(ctx, stat->statement, rt, &tmp);
845 if(rt->type == RT_CONTINUE)
846 rt->type = RT_NORMAL;
847 if(rt->type != RT_NORMAL)
856 if(rt->type == RT_BREAK)
857 rt->type = RT_NORMAL;
863 /* ECMA-262 3rd Edition 12.6.3 */
864 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
866 for_statement_t *stat = (for_statement_t*)_stat;
867 VARIANT val, tmp, retv;
874 if(stat->variable_list) {
875 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
878 }else if(stat->begin_expr) {
879 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
883 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
884 exprval_release(&exprval);
891 V_VT(&retv) = VT_EMPTY;
895 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
899 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
900 exprval_release(&exprval);
901 if(FAILED(hres) || !b)
905 hres = stat_eval(ctx, stat->statement, rt, &tmp);
912 if(rt->type == RT_CONTINUE)
913 rt->type = RT_NORMAL;
914 else if(rt->type != RT_NORMAL)
918 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
922 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
923 exprval_release(&exprval);
936 if(rt->type == RT_BREAK)
937 rt->type = RT_NORMAL;
943 /* ECMA-262 3rd Edition 12.6.4 */
944 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
946 forin_statement_t *stat = (forin_statement_t*)_stat;
947 VARIANT val, name, retv, tmp;
948 DISPID id = DISPID_STARTENUM;
949 BSTR str, identifier = NULL;
957 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
962 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
966 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
967 exprval_release(&exprval);
971 if(V_VT(&val) != VT_DISPATCH) {
972 TRACE("in vt %d\n", V_VT(&val));
974 V_VT(ret) = VT_EMPTY;
978 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
979 IDispatch_Release(V_DISPATCH(&val));
981 TRACE("Object doesn't support IDispatchEx\n");
982 V_VT(ret) = VT_EMPTY;
986 V_VT(&retv) = VT_EMPTY;
989 identifier = SysAllocString(stat->variable->identifier);
992 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
993 if(FAILED(hres) || hres == S_FALSE)
996 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
1000 TRACE("iter %s\n", debugstr_w(str));
1003 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
1005 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
1006 if(SUCCEEDED(hres)) {
1007 V_VT(&name) = VT_BSTR;
1008 V_BSTR(&name) = str;
1009 hres = put_value(ctx, &exprval, &name, &rt->ei);
1010 exprval_release(&exprval);
1016 hres = stat_eval(ctx, stat->statement, rt, &tmp);
1020 VariantClear(&retv);
1023 if(rt->type == RT_CONTINUE)
1024 rt->type = RT_NORMAL;
1025 else if(rt->type != RT_NORMAL)
1029 SysFreeString(identifier);
1030 IDispatchEx_Release(in_obj);
1032 VariantClear(&retv);
1036 if(rt->type == RT_BREAK)
1037 rt->type = RT_NORMAL;
1043 /* ECMA-262 3rd Edition 12.7 */
1044 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1046 branch_statement_t *stat = (branch_statement_t*)_stat;
1050 if(stat->identifier) {
1051 FIXME("indentifier not implemented\n");
1055 rt->type = RT_CONTINUE;
1056 V_VT(ret) = VT_EMPTY;
1060 /* ECMA-262 3rd Edition 12.8 */
1061 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1063 branch_statement_t *stat = (branch_statement_t*)_stat;
1067 if(stat->identifier) {
1068 FIXME("indentifier not implemented\n");
1072 rt->type = RT_BREAK;
1073 V_VT(ret) = VT_EMPTY;
1077 /* ECMA-262 3rd Edition 12.9 */
1078 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1080 expression_statement_t *stat = (expression_statement_t*)_stat;
1088 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1092 hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1093 exprval_release(&exprval);
1097 V_VT(ret) = VT_EMPTY;
1100 TRACE("= %s\n", debugstr_variant(ret));
1101 rt->type = RT_RETURN;
1105 /* ECMA-262 3rd Edition 12.10 */
1106 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1108 with_statement_t *stat = (with_statement_t*)_stat;
1117 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1121 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1122 exprval_release(&exprval);
1126 hres = to_object(ctx, &val, &disp);
1131 obj = iface_to_jsdisp((IUnknown*)disp);
1132 IDispatch_Release(disp);
1134 FIXME("disp id not jsdisp\n");
1138 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1139 jsdisp_release(obj);
1143 hres = stat_eval(ctx, stat->statement, rt, ret);
1145 scope_pop(&ctx->exec_ctx->scope_chain);
1149 /* ECMA-262 3rd Edition 12.12 */
1150 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1156 /* ECMA-262 3rd Edition 12.13 */
1157 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1159 switch_statement_t *stat = (switch_statement_t*)_stat;
1160 case_clausule_t *iter, *default_clausule = NULL;
1161 statement_t *stat_iter;
1169 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1173 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1174 exprval_release(&exprval);
1178 for(iter = stat->case_list; iter; iter = iter->next) {
1180 default_clausule = iter;
1184 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1188 hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1189 exprval_release(&exprval);
1193 hres = equal2_values(&val, &cval, &b);
1194 VariantClear(&cval);
1195 if(FAILED(hres) || b)
1204 iter = default_clausule;
1206 V_VT(&val) = VT_EMPTY;
1210 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1211 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1218 if(rt->type != RT_NORMAL)
1228 if(rt->type == RT_BREAK)
1229 rt->type = RT_NORMAL;
1235 /* ECMA-262 3rd Edition 12.13 */
1236 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1238 expression_statement_t *stat = (expression_statement_t*)_stat;
1245 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1249 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1250 exprval_release(&exprval);
1255 return DISP_E_EXCEPTION;
1258 /* ECMA-262 3rd Edition 12.14 */
1259 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1266 memset(&rt->ei, 0, sizeof(jsexcept_t));
1268 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1269 if(SUCCEEDED(hres)) {
1270 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1271 if(SUCCEEDED(hres)) {
1272 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1273 if(SUCCEEDED(hres)) {
1274 hres = stat_eval(ctx, block->statement, rt, &val);
1275 scope_pop(&ctx->exec_ctx->scope_chain);
1279 jsdisp_release(var_disp);
1290 /* ECMA-262 3rd Edition 12.14 */
1291 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1293 try_statement_t *stat = (try_statement_t*)_stat;
1299 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1301 TRACE("EXCEPTION\n");
1302 if(!stat->catch_block)
1305 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1310 if(stat->finally_statement) {
1312 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1321 static HRESULT return_bool(exprval_t *ret, DWORD b)
1323 ret->type = EXPRVAL_VARIANT;
1324 V_VT(&ret->u.var) = VT_BOOL;
1325 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1330 static HRESULT get_binary_expr_values(script_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1335 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1339 hres = exprval_to_value(ctx, &exprval, ei, lval);
1340 exprval_release(&exprval);
1344 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1345 if(SUCCEEDED(hres)) {
1346 hres = exprval_to_value(ctx, &exprval, ei, rval);
1347 exprval_release(&exprval);
1358 typedef HRESULT (*oper_t)(script_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1360 static HRESULT binary_expr_eval(script_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1363 VARIANT lval, rval, retv;
1366 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1370 hres = oper(ctx, &lval, &rval, ei, &retv);
1371 VariantClear(&lval);
1372 VariantClear(&rval);
1376 ret->type = EXPRVAL_VARIANT;
1381 /* ECMA-262 3rd Edition 11.13.2 */
1382 static HRESULT assign_oper_eval(script_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1383 jsexcept_t *ei, exprval_t *ret)
1385 VARIANT retv, lval, rval;
1386 exprval_t exprval, exprvalr;
1389 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1393 hres = exprval_value(ctx, &exprval, ei, &lval);
1394 if(SUCCEEDED(hres)) {
1395 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1396 if(SUCCEEDED(hres)) {
1397 hres = exprval_value(ctx, &exprvalr, ei, &rval);
1398 exprval_release(&exprvalr);
1400 if(SUCCEEDED(hres)) {
1401 hres = oper(ctx, &lval, &rval, ei, &retv);
1402 VariantClear(&rval);
1404 VariantClear(&lval);
1407 if(SUCCEEDED(hres)) {
1408 hres = put_value(ctx, &exprval, &retv, ei);
1410 VariantClear(&retv);
1412 exprval_release(&exprval);
1417 ret->type = EXPRVAL_VARIANT;
1422 /* ECMA-262 3rd Edition 13 */
1423 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1425 function_expression_t *expr = (function_expression_t*)_expr;
1431 if(expr->identifier) {
1432 hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1438 hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1439 expr->src_str, expr->src_len, &dispex);
1443 var_set_jsdisp(&var, dispex);
1446 ret->type = EXPRVAL_VARIANT;
1451 /* ECMA-262 3rd Edition 11.2.1 */
1452 HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1454 array_expression_t *expr = (array_expression_t*)_expr;
1456 VARIANT member, val;
1459 IDispatch *obj = NULL;
1464 hres = expr_eval(ctx, expr->member_expr, 0, ei, &exprval);
1468 hres = exprval_to_value(ctx, &exprval, ei, &member);
1469 exprval_release(&exprval);
1473 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1474 if(SUCCEEDED(hres)) {
1475 hres = exprval_to_value(ctx, &exprval, ei, &val);
1476 exprval_release(&exprval);
1479 if(SUCCEEDED(hres)) {
1480 hres = to_object(ctx, &member, &obj);
1484 VariantClear(&member);
1485 if(SUCCEEDED(hres)) {
1486 hres = to_string(ctx, &val, ei, &str);
1488 if(SUCCEEDED(hres)) {
1489 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1493 if(SUCCEEDED(hres)) {
1494 exprval_set_idref(ret, obj, id);
1495 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1500 IDispatch_Release(obj);
1506 /* ECMA-262 3rd Edition 11.2.1 */
1507 HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1509 member_expression_t *expr = (member_expression_t*)_expr;
1510 IDispatch *obj = NULL;
1519 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1523 hres = exprval_to_value(ctx, &exprval, ei, &member);
1524 exprval_release(&exprval);
1528 hres = to_object(ctx, &member, &obj);
1529 VariantClear(&member);
1533 str = SysAllocString(expr->identifier);
1535 IDispatch_Release(obj);
1536 return E_OUTOFMEMORY;
1539 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1541 if(SUCCEEDED(hres)) {
1542 exprval_set_idref(ret, obj, id);
1543 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1548 IDispatch_Release(obj);
1552 static void free_dp(DISPPARAMS *dp)
1556 for(i=0; i < dp->cArgs; i++)
1557 VariantClear(dp->rgvarg+i);
1558 heap_free(dp->rgvarg);
1561 static HRESULT args_to_param(script_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1567 HRESULT hres = S_OK;
1569 memset(dp, 0, sizeof(*dp));
1573 for(iter = args; iter; iter = iter->next)
1576 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1578 return E_OUTOFMEMORY;
1580 for(i = cnt, iter = args; iter; iter = iter->next) {
1581 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1585 hres = exprval_to_value(ctx, &exprval, ei, vargs + (--i));
1586 exprval_release(&exprval);
1601 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1606 dp->cArgs = arg_cnt;
1607 dp->rgdispidNamedArgs = NULL;
1610 assert(ctx->top >= arg_cnt);
1612 for(i=1; i*2 <= arg_cnt; i++) {
1613 tmp = ctx->stack[ctx->top-i];
1614 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1615 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1618 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1621 /* ECMA-262 3rd Edition 11.2.2 */
1622 static HRESULT interp_new(exec_ctx_t *ctx)
1624 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1631 constr = stack_topn(ctx, arg);
1633 /* NOTE: Should use to_object here */
1635 if(V_VT(constr) == VT_NULL)
1636 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1637 else if(V_VT(constr) != VT_DISPATCH)
1638 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1639 else if(!V_DISPATCH(constr))
1640 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1642 jsstack_to_dp(ctx, arg, &dp);
1643 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1644 DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1648 stack_popn(ctx, arg+1);
1649 return stack_push(ctx, &v);
1652 /* ECMA-262 3rd Edition 11.2.3 */
1653 HRESULT call_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1655 call_expression_t *expr = (call_expression_t*)_expr;
1663 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1667 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1668 if(SUCCEEDED(hres)) {
1669 switch(exprval.type) {
1670 case EXPRVAL_VARIANT:
1671 if(V_VT(&exprval.u.var) == VT_DISPATCH)
1672 hres = disp_call(ctx, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1673 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1675 hres = throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1678 hres = disp_call(ctx, exprval.u.idref.disp, exprval.u.idref.id,
1679 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1681 case EXPRVAL_INVALID:
1682 hres = throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1685 FIXME("unimplemented type %d\n", exprval.type);
1692 exprval_release(&exprval);
1696 ret->type = EXPRVAL_VARIANT;
1697 if(flags & EXPR_NOVAL) {
1698 V_VT(&ret->u.var) = VT_EMPTY;
1700 TRACE("= %s\n", debugstr_variant(&var));
1706 /* ECMA-262 3rd Edition 11.1.1 */
1707 static HRESULT interp_this(exec_ctx_t *ctx)
1713 V_VT(&v) = VT_DISPATCH;
1714 V_DISPATCH(&v) = ctx->this_obj;
1715 IDispatch_AddRef(ctx->this_obj);
1716 return stack_push(ctx, &v);
1719 /* ECMA-262 3rd Edition 10.1.4 */
1720 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1722 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1728 identifier = SysAllocString(expr->identifier);
1730 return E_OUTOFMEMORY;
1732 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1734 SysFreeString(identifier);
1738 /* ECMA-262 3rd Edition 10.1.4 */
1739 static HRESULT interp_ident(exec_ctx_t *ctx)
1741 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1746 TRACE("%s\n", debugstr_w(arg));
1748 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1752 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1753 exprval_release(&exprval);
1757 return stack_push(ctx, &v);
1760 /* ECMA-262 3rd Edition 7.8.1 */
1761 static HRESULT interp_null(exec_ctx_t *ctx)
1768 return stack_push(ctx, &v);
1771 /* ECMA-262 3rd Edition 7.8.2 */
1772 static HRESULT interp_bool(exec_ctx_t *ctx)
1774 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1776 TRACE("%s\n", arg ? "true" : "false");
1778 return stack_push_bool(ctx, arg);
1781 /* ECMA-262 3rd Edition 7.8.3 */
1782 static HRESULT interp_int(exec_ctx_t *ctx)
1784 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1791 return stack_push(ctx, &v);
1794 /* ECMA-262 3rd Edition 7.8.3 */
1795 static HRESULT interp_double(exec_ctx_t *ctx)
1797 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1800 TRACE("%lf\n", arg);
1804 return stack_push(ctx, &v);
1807 /* ECMA-262 3rd Edition 7.8.4 */
1808 static HRESULT interp_str(exec_ctx_t *ctx)
1810 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1813 TRACE("%s\n", debugstr_w(str));
1816 V_BSTR(&v) = SysAllocString(str);
1818 return E_OUTOFMEMORY;
1820 return stack_push(ctx, &v);
1823 /* ECMA-262 3rd Edition 7.8 */
1824 static HRESULT interp_regexp(exec_ctx_t *ctx)
1826 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1827 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1832 TRACE("%s %x\n", debugstr_w(source), flags);
1834 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, ®exp);
1838 var_set_jsdisp(&v, regexp);
1839 return stack_push(ctx, &v);
1842 /* ECMA-262 3rd Edition 11.1.4 */
1843 HRESULT array_literal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1845 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1846 DWORD length = 0, i = 0;
1847 array_element_t *elem;
1855 for(elem = expr->element_list; elem; elem = elem->next)
1856 length += elem->elision+1;
1857 length += expr->length;
1859 hres = create_array(ctx, length, &array);
1863 for(elem = expr->element_list; elem; elem = elem->next) {
1866 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1870 hres = exprval_to_value(ctx, &exprval, ei, &val);
1871 exprval_release(&exprval);
1875 hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
1884 jsdisp_release(array);
1888 ret->type = EXPRVAL_VARIANT;
1889 var_set_jsdisp(&ret->u.var, array);
1893 /* ECMA-262 3rd Edition 11.1.5 */
1894 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1896 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1906 hres = create_object(ctx, NULL, &obj);
1910 for(iter = expr->property_list; iter; iter = iter->next) {
1911 hres = literal_to_var(ctx, iter->name, &tmp);
1915 hres = to_string(ctx, &tmp, ei, &name);
1920 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1921 if(SUCCEEDED(hres)) {
1922 hres = exprval_to_value(ctx, &exprval, ei, &val);
1923 exprval_release(&exprval);
1924 if(SUCCEEDED(hres)) {
1925 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
1930 SysFreeString(name);
1936 jsdisp_release(obj);
1940 ret->type = EXPRVAL_VARIANT;
1941 var_set_jsdisp(&ret->u.var, obj);
1945 /* ECMA-262 3rd Edition 11.11 */
1946 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
1948 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1954 hres = to_boolean(stack_top(ctx), &b);
1967 /* ECMA-262 3rd Edition 11.11 */
1968 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
1970 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1976 hres = to_boolean(stack_top(ctx), &b);
1989 /* ECMA-262 3rd Edition 11.10 */
1990 static HRESULT bitor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1995 hres = to_int32(ctx, lval, ei, &li);
1999 hres = to_int32(ctx, rval, ei, &ri);
2008 /* ECMA-262 3rd Edition 11.10 */
2009 static HRESULT interp_or(exec_ctx_t *ctx)
2016 hres = stack_pop_int(ctx, &r);
2020 hres = stack_pop_int(ctx, &l);
2024 return stack_push_int(ctx, l|r);
2027 /* ECMA-262 3rd Edition 11.10 */
2028 static HRESULT xor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2033 hres = to_int32(ctx, lval, ei, &li);
2037 hres = to_int32(ctx, rval, ei, &ri);
2046 /* ECMA-262 3rd Edition 11.10 */
2047 static HRESULT interp_xor(exec_ctx_t *ctx)
2054 hres = stack_pop_int(ctx, &r);
2058 hres = stack_pop_int(ctx, &l);
2062 return stack_push_int(ctx, l^r);
2065 /* ECMA-262 3rd Edition 11.10 */
2066 static HRESULT bitand_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2071 hres = to_int32(ctx, lval, ei, &li);
2075 hres = to_int32(ctx, rval, ei, &ri);
2084 /* ECMA-262 3rd Edition 11.10 */
2085 HRESULT binary_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2087 binary_expression_t *expr = (binary_expression_t*)_expr;
2091 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
2094 /* ECMA-262 3rd Edition 11.8.6 */
2095 static HRESULT instanceof_eval(script_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
2097 jsdisp_t *obj, *iter, *tmp = NULL;
2098 VARIANT_BOOL ret = VARIANT_FALSE;
2103 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2105 if(V_VT(objv) != VT_DISPATCH || !V_DISPATCH(objv))
2106 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2108 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2110 FIXME("non-jsdisp objects not supported\n");
2114 if(is_class(obj, JSCLASS_FUNCTION)) {
2115 hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2117 hres = throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2119 jsdisp_release(obj);
2123 if(V_VT(&var) == VT_DISPATCH) {
2124 if(V_VT(inst) == VT_DISPATCH)
2125 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2126 for(iter = tmp; iter; iter = iter->prototype) {
2127 hres = disp_cmp(V_DISPATCH(&var), to_disp(iter), &b);
2137 jsdisp_release(tmp);
2139 FIXME("prototype is not an object\n");
2147 V_VT(retv) = VT_BOOL;
2152 /* ECMA-262 3rd Edition 11.8.6 */
2153 HRESULT instanceof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2155 binary_expression_t *expr = (binary_expression_t*)_expr;
2159 return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2162 /* ECMA-262 3rd Edition 11.8.7 */
2163 static HRESULT interp_in(exec_ctx_t *ctx)
2173 obj = stack_pop(ctx);
2176 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2179 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2182 hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2185 IDispatch_Release(V_DISPATCH(obj));
2189 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2190 IDispatch_Release(V_DISPATCH(obj));
2194 else if(hres == DISP_E_UNKNOWNNAME)
2199 return stack_push_bool(ctx, ret);
2202 /* ECMA-262 3rd Edition 11.6.1 */
2203 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2208 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2212 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2218 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2219 BSTR lstr = NULL, rstr = NULL;
2221 if(V_VT(&l) == VT_BSTR)
2224 hres = to_string(ctx, &l, ei, &lstr);
2226 if(SUCCEEDED(hres)) {
2227 if(V_VT(&r) == VT_BSTR)
2230 hres = to_string(ctx, &r, ei, &rstr);
2233 if(SUCCEEDED(hres)) {
2236 len1 = SysStringLen(lstr);
2237 len2 = SysStringLen(rstr);
2239 V_VT(retv) = VT_BSTR;
2240 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2241 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2242 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2245 if(V_VT(&l) != VT_BSTR)
2246 SysFreeString(lstr);
2247 if(V_VT(&r) != VT_BSTR)
2248 SysFreeString(rstr);
2252 hres = to_number(ctx, &l, ei, &nl);
2253 if(SUCCEEDED(hres)) {
2254 hres = to_number(ctx, &r, ei, &nr);
2256 num_set_val(retv, num_val(&nl) + num_val(&nr));
2265 /* ECMA-262 3rd Edition 11.6.1 */
2266 static HRESULT interp_add(exec_ctx_t *ctx)
2268 VARIANT *l, *r, ret;
2274 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2276 hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2282 return stack_push(ctx, &ret);
2285 /* ECMA-262 3rd Edition 11.6.2 */
2286 static HRESULT sub_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2291 hres = to_number(ctx, lval, ei, &lnum);
2295 hres = to_number(ctx, rval, ei, &rnum);
2299 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
2303 /* ECMA-262 3rd Edition 11.6.2 */
2304 static HRESULT interp_sub(exec_ctx_t *ctx)
2311 hres = stack_pop_number(ctx, &r);
2315 hres = stack_pop_number(ctx, &l);
2319 return stack_push_number(ctx, num_val(&l)-num_val(&r));
2322 /* ECMA-262 3rd Edition 11.5.1 */
2323 static HRESULT mul_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2328 hres = to_number(ctx, lval, ei, &lnum);
2332 hres = to_number(ctx, rval, ei, &rnum);
2336 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2340 /* ECMA-262 3rd Edition 11.5.1 */
2341 static HRESULT interp_mul(exec_ctx_t *ctx)
2348 hres = stack_pop_number(ctx, &r);
2352 hres = stack_pop_number(ctx, &l);
2356 return stack_push_number(ctx, num_val(&l)*num_val(&r));
2359 /* ECMA-262 3rd Edition 11.5.2 */
2360 static HRESULT div_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2365 hres = to_number(ctx, lval, ei, &lnum);
2369 hres = to_number(ctx, rval, ei, &rnum);
2373 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2377 /* ECMA-262 3rd Edition 11.5.2 */
2378 static HRESULT interp_div(exec_ctx_t *ctx)
2385 hres = stack_pop_number(ctx, &r);
2389 hres = stack_pop_number(ctx, &l);
2393 return stack_push_number(ctx, num_val(&l)/num_val(&r));
2396 /* ECMA-262 3rd Edition 11.5.3 */
2397 static HRESULT mod_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2402 hres = to_number(ctx, lval, ei, &lnum);
2406 hres = to_number(ctx, rval, ei, &rnum);
2410 num_set_val(retv, fmod(num_val(&lnum), num_val(&rnum)));
2414 /* ECMA-262 3rd Edition 11.5.3 */
2415 static HRESULT interp_mod(exec_ctx_t *ctx)
2422 hres = stack_pop_number(ctx, &r);
2426 hres = stack_pop_number(ctx, &l);
2430 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2433 /* ECMA-262 3rd Edition 11.4.2 */
2434 HRESULT delete_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2436 unary_expression_t *expr = (unary_expression_t*)_expr;
2437 VARIANT_BOOL b = VARIANT_FALSE;
2443 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2447 switch(exprval.type) {
2448 case EXPRVAL_IDREF: {
2449 IDispatchEx *dispex;
2451 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2452 if(SUCCEEDED(hres)) {
2453 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2455 IDispatchEx_Release(dispex);
2460 FIXME("unsupported type %d\n", exprval.type);
2464 exprval_release(&exprval);
2468 return return_bool(ret, b);
2471 /* ECMA-262 3rd Edition 11.4.2 */
2472 static HRESULT interp_delete(exec_ctx_t *ctx)
2474 VARIANT *obj_var, *name_var;
2475 IDispatchEx *dispex;
2483 name_var = stack_pop(ctx);
2484 obj_var = stack_pop(ctx);
2486 hres = to_object(ctx->parser->script, obj_var, &obj);
2487 VariantClear(obj_var);
2489 VariantClear(name_var);
2493 hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2494 VariantClear(name_var);
2496 IDispatch_Release(obj);
2500 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2501 if(SUCCEEDED(hres)) {
2502 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2504 IDispatchEx_Release(dispex);
2510 IDispatch_Release(obj);
2511 SysFreeString(name);
2515 return stack_push_bool(ctx, ret);
2518 /* ECMA-262 3rd Edition 11.4.2 */
2519 static HRESULT interp_void(exec_ctx_t *ctx)
2527 V_VT(&v) = VT_EMPTY;
2528 return stack_push(ctx, &v);
2531 /* ECMA-262 3rd Edition 11.4.3 */
2532 static HRESULT typeof_exprval(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2537 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2538 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2539 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2540 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2541 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2542 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2543 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
2545 if(exprval->type == EXPRVAL_INVALID) {
2550 hres = exprval_to_value(ctx, exprval, ei, &val);
2552 if(exprval->type == EXPRVAL_IDREF) {
2559 switch(V_VT(&val)) {
2579 if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2580 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2581 jsdisp_release(dispex);
2588 FIXME("unhandled vt %d\n", V_VT(&val));
2596 HRESULT typeof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2598 unary_expression_t *expr = (unary_expression_t*)_expr;
2599 const WCHAR *str = NULL;
2605 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2609 hres = typeof_exprval(ctx, &exprval, ei, &str);
2610 exprval_release(&exprval);
2614 ret->type = EXPRVAL_VARIANT;
2615 V_VT(&ret->u.var) = VT_BSTR;
2616 V_BSTR(&ret->u.var) = SysAllocString(str);
2617 if(!V_BSTR(&ret->u.var))
2618 return E_OUTOFMEMORY;
2623 /* ECMA-262 3rd Edition 11.4.7 */
2624 static HRESULT interp_minus(exec_ctx_t *ctx)
2631 hres = stack_pop_number(ctx, &n);
2635 return stack_push_number(ctx, -num_val(&n));
2638 /* ECMA-262 3rd Edition 11.4.6 */
2639 static HRESULT interp_tonum(exec_ctx_t *ctx)
2647 hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2652 return stack_push(ctx, &num);
2655 /* ECMA-262 3rd Edition 11.3.1 */
2656 HRESULT post_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2658 unary_expression_t *expr = (unary_expression_t*)_expr;
2665 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2669 hres = exprval_value(ctx, &exprval, ei, &val);
2670 if(SUCCEEDED(hres)) {
2671 hres = to_number(ctx, &val, ei, &num);
2675 if(SUCCEEDED(hres)) {
2677 num_set_val(&inc, num_val(&num)+1.0);
2678 hres = put_value(ctx, &exprval, &inc, ei);
2681 exprval_release(&exprval);
2685 ret->type = EXPRVAL_VARIANT;
2690 /* ECMA-262 3rd Edition 11.3.2 */
2691 HRESULT post_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2693 unary_expression_t *expr = (unary_expression_t*)_expr;
2700 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2704 hres = exprval_value(ctx, &exprval, ei, &val);
2705 if(SUCCEEDED(hres)) {
2706 hres = to_number(ctx, &val, ei, &num);
2710 if(SUCCEEDED(hres)) {
2712 num_set_val(&dec, num_val(&num)-1.0);
2713 hres = put_value(ctx, &exprval, &dec, ei);
2716 exprval_release(&exprval);
2720 ret->type = EXPRVAL_VARIANT;
2725 /* ECMA-262 3rd Edition 11.4.4 */
2726 HRESULT pre_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2728 unary_expression_t *expr = (unary_expression_t*)_expr;
2735 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2739 hres = exprval_value(ctx, &exprval, ei, &val);
2740 if(SUCCEEDED(hres)) {
2741 hres = to_number(ctx, &val, ei, &num);
2745 if(SUCCEEDED(hres)) {
2746 num_set_val(&val, num_val(&num)+1.0);
2747 hres = put_value(ctx, &exprval, &val, ei);
2750 exprval_release(&exprval);
2754 ret->type = EXPRVAL_VARIANT;
2759 /* ECMA-262 3rd Edition 11.4.5 */
2760 HRESULT pre_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2762 unary_expression_t *expr = (unary_expression_t*)_expr;
2769 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2773 hres = exprval_value(ctx, &exprval, ei, &val);
2774 if(SUCCEEDED(hres)) {
2775 hres = to_number(ctx, &val, ei, &num);
2779 if(SUCCEEDED(hres)) {
2780 num_set_val(&val, num_val(&num)-1.0);
2781 hres = put_value(ctx, &exprval, &val, ei);
2784 exprval_release(&exprval);
2788 ret->type = EXPRVAL_VARIANT;
2793 /* ECMA-262 3rd Edition 11.9.3 */
2794 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2796 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2797 return equal2_values(lval, rval, ret);
2799 /* FIXME: NULL disps should be handled in more general way */
2800 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2803 return equal_values(ctx, &v, rval, ei, ret);
2806 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2809 return equal_values(ctx, lval, &v, ei, ret);
2812 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2813 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2818 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2822 hres = to_number(ctx, lval, ei, &v);
2826 return equal_values(ctx, &v, rval, ei, ret);
2829 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2833 hres = to_number(ctx, rval, ei, &v);
2837 return equal_values(ctx, lval, &v, ei, ret);
2840 if(V_VT(rval) == VT_BOOL) {
2844 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2845 return equal_values(ctx, lval, &v, ei, ret);
2848 if(V_VT(lval) == VT_BOOL) {
2852 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2853 return equal_values(ctx, &v, rval, ei, ret);
2857 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2861 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2865 hres = equal_values(ctx, lval, &v, ei, ret);
2872 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2876 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2880 hres = equal_values(ctx, &v, rval, ei, ret);
2891 /* ECMA-262 3rd Edition 11.9.1 */
2892 static HRESULT interp_eq(exec_ctx_t *ctx)
2901 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2903 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2909 return stack_push_bool(ctx, b);
2912 /* ECMA-262 3rd Edition 11.9.2 */
2913 static HRESULT interp_neq(exec_ctx_t *ctx)
2922 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2924 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2930 return stack_push_bool(ctx, !b);
2933 /* ECMA-262 3rd Edition 11.9.4 */
2934 static HRESULT interp_eq2(exec_ctx_t *ctx)
2945 hres = equal2_values(r, l, &b);
2951 return stack_push_bool(ctx, b);
2954 /* ECMA-262 3rd Edition 11.9.5 */
2955 static HRESULT interp_neq2(exec_ctx_t *ctx)
2966 hres = equal2_values(r, l, &b);
2972 return stack_push_bool(ctx, !b);
2975 /* ECMA-262 3rd Edition 11.8.5 */
2976 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2978 VARIANT l, r, ln, rn;
2981 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2985 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2991 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2992 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2993 SysFreeString(V_BSTR(&l));
2994 SysFreeString(V_BSTR(&r));
2998 hres = to_number(ctx, &l, ei, &ln);
3001 hres = to_number(ctx, &r, ei, &rn);
3006 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
3007 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
3009 DOUBLE ld = num_val(&ln);
3010 DOUBLE rd = num_val(&rn);
3012 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
3018 /* ECMA-262 3rd Edition 11.8.1 */
3019 static HRESULT interp_lt(exec_ctx_t *ctx)
3028 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
3030 hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
3036 return stack_push_bool(ctx, b);
3039 /* ECMA-262 3rd Edition 11.8.1 */
3040 static HRESULT interp_lteq(exec_ctx_t *ctx)
3049 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
3051 hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
3057 return stack_push_bool(ctx, b);
3060 /* ECMA-262 3rd Edition 11.8.2 */
3061 static HRESULT interp_gt(exec_ctx_t *ctx)
3070 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
3072 hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
3078 return stack_push_bool(ctx, b);
3081 /* ECMA-262 3rd Edition 11.8.4 */
3082 static HRESULT interp_gteq(exec_ctx_t *ctx)
3091 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
3093 hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
3099 return stack_push_bool(ctx, b);
3102 /* ECMA-262 3rd Edition 11.4.8 */
3103 static HRESULT interp_bneg(exec_ctx_t *ctx)
3112 hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3119 return stack_push(ctx, &r);
3122 /* ECMA-262 3rd Edition 11.4.9 */
3123 static HRESULT interp_neg(exec_ctx_t *ctx)
3132 hres = to_boolean(v, &b);
3137 return stack_push_bool(ctx, !b);
3140 /* ECMA-262 3rd Edition 11.7.1 */
3141 static HRESULT lshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3147 hres = to_int32(ctx, lval, ei, &li);
3151 hres = to_uint32(ctx, rval, ei, &ri);
3156 V_I4(retv) = li << (ri&0x1f);
3160 /* ECMA-262 3rd Edition 11.7.1 */
3161 HRESULT left_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3163 binary_expression_t *expr = (binary_expression_t*)_expr;
3167 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3170 /* ECMA-262 3rd Edition 11.7.2 */
3171 static HRESULT rshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3177 hres = to_int32(ctx, lval, ei, &li);
3181 hres = to_uint32(ctx, rval, ei, &ri);
3186 V_I4(retv) = li >> (ri&0x1f);
3190 /* ECMA-262 3rd Edition 11.7.2 */
3191 HRESULT right_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3193 binary_expression_t *expr = (binary_expression_t*)_expr;
3197 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3200 /* ECMA-262 3rd Edition 11.7.3 */
3201 static HRESULT rshift2_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3206 hres = to_uint32(ctx, lval, ei, &li);
3210 hres = to_uint32(ctx, rval, ei, &ri);
3215 V_I4(retv) = li >> (ri&0x1f);
3219 /* ECMA-262 3rd Edition 11.7.3 */
3220 HRESULT right2_shift_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;
3226 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3229 /* ECMA-262 3rd Edition 11.13.1 */
3230 HRESULT assign_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3232 binary_expression_t *expr = (binary_expression_t*)_expr;
3233 exprval_t exprval, exprvalr;
3239 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
3243 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
3244 if(SUCCEEDED(hres)) {
3245 hres = exprval_to_value(ctx, &exprvalr, ei, &rval);
3246 exprval_release(&exprvalr);
3249 if(SUCCEEDED(hres)) {
3250 hres = put_value(ctx, &exprval, &rval, ei);
3252 VariantClear(&rval);
3255 exprval_release(&exprval);
3259 ret->type = EXPRVAL_VARIANT;
3264 /* ECMA-262 3rd Edition 11.13.2 */
3265 HRESULT assign_lshift_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, lshift_eval, ei, ret);
3274 /* ECMA-262 3rd Edition 11.13.2 */
3275 HRESULT assign_rshift_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, rshift_eval, ei, ret);
3284 /* ECMA-262 3rd Edition 11.13.2 */
3285 HRESULT assign_rrshift_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, rshift2_eval, ei, ret);
3294 /* ECMA-262 3rd Edition 11.13.2 */
3295 HRESULT assign_add_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, add_eval, ei, ret);
3304 /* ECMA-262 3rd Edition 11.13.2 */
3305 HRESULT assign_sub_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, sub_eval, ei, ret);
3314 /* ECMA-262 3rd Edition 11.13.2 */
3315 HRESULT assign_mul_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, mul_eval, ei, ret);
3324 /* ECMA-262 3rd Edition 11.13.2 */
3325 HRESULT assign_div_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, div_eval, ei, ret);
3334 /* ECMA-262 3rd Edition 11.13.2 */
3335 HRESULT assign_mod_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, mod_eval, ei, ret);
3344 /* ECMA-262 3rd Edition 11.13.2 */
3345 HRESULT assign_and_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, bitand_eval, ei, ret);
3354 /* ECMA-262 3rd Edition 11.13.2 */
3355 HRESULT assign_or_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, bitor_eval, ei, ret);
3364 /* ECMA-262 3rd Edition 11.13.2 */
3365 HRESULT assign_xor_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3367 binary_expression_t *expr = (binary_expression_t*)_expr;
3371 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);
3374 static HRESULT interp_jmp(exec_ctx_t *ctx)
3376 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3384 static HRESULT interp_pop(exec_ctx_t *ctx)
3392 static HRESULT interp_ret(exec_ctx_t *ctx)
3400 static HRESULT interp_tree(exec_ctx_t *ctx)
3402 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3409 hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3413 hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3414 exprval_release(&val);
3418 return stack_push(ctx, &v);
3421 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3423 static const op_func_t op_funcs[] = {
3424 #define X(x,a,b,c) interp_##x,
3429 static const unsigned op_move[] = {
3430 #define X(a,x,b,c) x,
3435 HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3437 exec_ctx_t *exec_ctx = ctx->exec_ctx;
3438 unsigned prev_ip, prev_top;
3440 HRESULT hres = S_OK;
3444 prev_top = exec_ctx->top;
3445 prev_ip = exec_ctx->ip;
3446 exec_ctx->ip = expr->instr_off;
3448 while(exec_ctx->ip != -1) {
3449 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3450 hres = op_funcs[op](exec_ctx);
3453 exec_ctx->ip += op_move[op];
3456 exec_ctx->ip = prev_ip;
3459 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3461 memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3465 assert(exec_ctx->top == prev_top+1);
3467 ret->type = EXPRVAL_VARIANT;
3468 ret->u.var = *stack_pop(exec_ctx);
3472 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3478 hres = compile_subscript(ctx->exec_ctx->parser, expr, &expr->instr_off);
3482 if(expr->eval == compiled_expression_eval)
3483 expr->eval = interp_expression_eval;
3485 return expr->eval(ctx, expr, flags, ei, ret);