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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26 #define EXPR_NOVAL 0x0001
27 #define EXPR_NEWREF 0x0002
28 #define EXPR_STRREF 0x0004
30 static inline HRESULT stat_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
32 return stat->eval(ctx, stat, rt, ret);
35 static inline HRESULT expr_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
37 return _expr->eval(ctx, _expr, flags, ei, ret);
40 static void exprval_release(exprval_t *val)
44 if(V_VT(&val->u.var) != VT_EMPTY)
45 VariantClear(&val->u.var);
49 IDispatch_Release(val->u.idref.disp);
52 if(val->u.nameref.disp)
53 IDispatch_Release(val->u.nameref.disp);
54 SysFreeString(val->u.nameref.name);
58 /* ECMA-262 3rd Edition 8.7.1 */
59 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
65 return VariantCopy(ret, &val->u.var);
67 if(!val->u.idref.disp) {
68 FIXME("throw ReferenceError\n");
72 return disp_propget(val->u.idref.disp, val->u.idref.id, ctx->lcid, ret, ei, NULL/*FIXME*/);
74 ERR("type %d\n", val->type);
79 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
81 if(val->type == EXPRVAL_VARIANT) {
83 V_VT(&val->u.var) = VT_EMPTY;
87 return exprval_value(ctx, val, ei, ret);
90 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
92 if(exprval->type != EXPRVAL_VARIANT) {
96 hres = exprval_to_value(ctx, exprval, ei, &val);
100 hres = to_boolean(&val, b);
105 return to_boolean(&exprval->u.var, b);
108 static void exprval_init(exprval_t *val)
110 val->type = EXPRVAL_VARIANT;
111 V_VT(&val->u.var) = VT_EMPTY;
114 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
116 val->type = EXPRVAL_IDREF;
117 val->u.idref.disp = disp;
118 val->u.idref.id = id;
121 IDispatch_AddRef(disp);
124 HRESULT scope_push(scope_chain_t *scope, DispatchEx *obj, scope_chain_t **ret)
126 scope_chain_t *new_scope;
128 new_scope = heap_alloc(sizeof(scope_chain_t));
130 return E_OUTOFMEMORY;
134 IDispatchEx_AddRef(_IDispatchEx_(obj));
135 new_scope->obj = obj;
139 new_scope->next = scope;
141 new_scope->next = NULL;
148 static void scope_pop(scope_chain_t **scope)
157 void scope_release(scope_chain_t *scope)
163 scope_release(scope->next);
165 IDispatchEx_Release(_IDispatchEx_(scope->obj));
169 HRESULT create_exec_ctx(IDispatch *this_obj, DispatchEx *var_disp, scope_chain_t *scope, exec_ctx_t **ret)
173 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
175 return E_OUTOFMEMORY;
177 IDispatch_AddRef(this_obj);
178 ctx->this_obj = this_obj;
180 IDispatchEx_AddRef(_IDispatchEx_(var_disp));
181 ctx->var_disp = var_disp;
185 ctx->scope_chain = scope;
192 void exec_release(exec_ctx_t *ctx)
198 scope_release(ctx->scope_chain);
200 IDispatchEx_Release(_IDispatchEx_(ctx->var_disp));
202 IDispatch_Release(ctx->this_obj);
206 static HRESULT disp_get_id(IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
211 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
213 TRACE("unsing IDispatch\n");
216 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
220 hres = IDispatchEx_GetDispID(dispex, name, flags|fdexNameCaseSensitive, id);
221 IDispatchEx_Release(dispex);
225 /* ECMA-262 3rd Edition 8.7.2 */
226 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
228 if(ref->type != EXPRVAL_IDREF) {
229 FIXME("throw ReferemceError\n");
233 return disp_propput(ref->u.idref.disp, ref->u.idref.id, ctx->lcid, v, ei, NULL/*FIXME*/);
236 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
238 IObjectIdentity *identity;
239 IUnknown *unk1, *unk2;
247 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
251 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
253 IUnknown_Release(unk1);
260 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
261 if(SUCCEEDED(hres)) {
262 hres = IObjectIdentity_IsEqualObject(identity, unk2);
263 IObjectIdentity_Release(identity);
270 IUnknown_Release(unk1);
271 IUnknown_Release(unk2);
275 /* ECMA-262 3rd Edition 11.9.6 */
276 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
280 if(V_VT(lval) != V_VT(rval)) {
281 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
282 *ret = num_val(lval) == num_val(rval);
296 *ret = V_I4(lval) == V_I4(rval);
299 *ret = V_R8(lval) == V_R8(rval);
302 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
305 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
307 *ret = !V_BOOL(lval) == !V_BOOL(rval);
310 FIXME("unimplemented vt %d\n", V_VT(lval));
317 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
319 V_VT(v) = literal->vt;
326 V_I4(v) = literal->u.lval;
329 V_R8(v) = literal->u.dval;
332 V_BSTR(v) = SysAllocString(literal->u.wstr);
335 V_BOOL(v) = literal->u.bval;
338 IDispatch_AddRef(literal->u.disp);
339 V_DISPATCH(v) = literal->u.disp;
342 ERR("wrong type %d\n", V_VT(v));
349 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
351 script_ctx_t *script = parser->script;
352 function_declaration_t *func;
353 parser_ctx_t *prev_parser;
357 exec_ctx_t *prev_ctx;
361 for(func = source->functions; func; func = func->next) {
362 DispatchEx *func_obj;
365 hres = create_source_function(parser, func->parameter_list, func->source_elements,
366 ctx->scope_chain, func->src_str, func->src_len, &func_obj);
370 V_VT(&var) = VT_DISPATCH;
371 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
372 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
373 jsdisp_release(func_obj);
378 for(var = source->variables; var; var = var->next) {
381 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
386 prev_ctx = script->exec_ctx;
387 script->exec_ctx = ctx;
389 prev_parser = ctx->parser;
390 ctx->parser = parser;
392 V_VT(&val) = VT_EMPTY;
393 memset(&rt, 0, sizeof(rt));
396 for(stat = source->statement; stat; stat = stat->next) {
397 hres = stat_eval(ctx, stat, &rt, &tmp);
403 if(rt.type != RT_NORMAL)
407 script->exec_ctx = prev_ctx;
408 ctx->parser = prev_parser;
410 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
411 FIXME("wrong rt %d\n", rt.type);
428 /* ECMA-262 3rd Edition 10.1.4 */
429 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
431 scope_chain_t *scope;
436 TRACE("%s\n", debugstr_w(identifier));
438 for(scope = ctx->scope_chain; scope; scope = scope->next) {
439 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
445 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
449 hres = jsdisp_get_id(ctx->parser->script->global, identifier, 0, &id);
450 if(SUCCEEDED(hres)) {
451 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
455 for(item = ctx->parser->script->named_items; item; item = item->next) {
456 hres = disp_get_id(item->disp, identifier, 0, &id);
462 exprval_set_idref(ret, (IDispatch*)item->disp, id);
466 hres = jsdisp_get_id(ctx->parser->script->script_disp, identifier, 0, &id);
467 if(SUCCEEDED(hres)) {
468 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
472 if(flags & EXPR_NEWREF) {
473 hres = jsdisp_get_id(ctx->var_disp, identifier, fdexNameEnsure, &id);
477 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
481 WARN("Could not find identifier %s\n", debugstr_w(identifier));
485 /* ECMA-262 3rd Edition 12.1 */
486 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
488 block_statement_t *stat = (block_statement_t*)_stat;
495 V_VT(&val) = VT_EMPTY;
496 for(iter = stat->stat_list; iter; iter = iter->next) {
497 hres = stat_eval(ctx, iter, rt, &tmp);
503 if(rt->type != RT_NORMAL)
516 /* ECMA-262 3rd Edition 12.2 */
517 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
519 variable_declaration_t *iter;
520 HRESULT hres = E_FAIL;
522 for(iter = var_list; iter; iter = iter->next) {
528 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
532 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
533 exprval_release(&exprval);
537 V_VT(&val) = VT_EMPTY;
540 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
549 /* ECMA-262 3rd Edition 12.2 */
550 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
552 var_statement_t *stat = (var_statement_t*)_stat;
557 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
561 V_VT(ret) = VT_EMPTY;
565 /* ECMA-262 3rd Edition 12.3 */
566 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
570 V_VT(ret) = VT_EMPTY;
574 /* ECMA-262 3rd Edition 12.4 */
575 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
577 expression_statement_t *stat = (expression_statement_t*)_stat;
584 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
588 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
589 exprval_release(&exprval);
594 TRACE("= %s\n", debugstr_variant(ret));
598 /* ECMA-262 3rd Edition 12.5 */
599 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
601 if_statement_t *stat = (if_statement_t*)_stat;
608 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
612 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
613 exprval_release(&exprval);
618 hres = stat_eval(ctx, stat->if_stat, rt, ret);
619 else if(stat->else_stat)
620 hres = stat_eval(ctx, stat->else_stat, rt, ret);
622 V_VT(ret) = VT_EMPTY;
627 /* ECMA-262 3rd Edition 12.6.2 */
628 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
630 while_statement_t *stat = (while_statement_t*)_stat;
639 V_VT(&val) = VT_EMPTY;
640 test_expr = !stat->do_while;
644 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
648 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
649 exprval_release(&exprval);
650 if(FAILED(hres) || !b)
656 hres = stat_eval(ctx, stat->statement, rt, &tmp);
663 if(rt->type == RT_CONTINUE)
664 rt->type = RT_NORMAL;
665 if(rt->type != RT_NORMAL)
674 if(rt->type == RT_BREAK)
675 rt->type = RT_NORMAL;
681 /* ECMA-262 3rd Edition 12.6.3 */
682 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
684 for_statement_t *stat = (for_statement_t*)_stat;
685 VARIANT val, tmp, retv;
692 if(stat->variable_list) {
693 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
696 }else if(stat->begin_expr) {
697 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
701 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
702 exprval_release(&exprval);
709 V_VT(&retv) = VT_EMPTY;
713 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
717 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
718 exprval_release(&exprval);
719 if(FAILED(hres) || !b)
723 hres = stat_eval(ctx, stat->statement, rt, &tmp);
730 if(rt->type == RT_CONTINUE)
731 rt->type = RT_NORMAL;
732 else if(rt->type != RT_NORMAL)
736 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
740 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
741 exprval_release(&exprval);
754 if(rt->type == RT_BREAK)
755 rt->type = RT_NORMAL;
761 /* ECMA-262 3rd Edition 12.6.4 */
762 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
764 forin_statement_t *stat = (forin_statement_t*)_stat;
765 VARIANT val, name, retv, tmp;
766 DISPID id = DISPID_STARTENUM;
767 BSTR str, identifier = NULL;
775 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
780 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
784 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
785 exprval_release(&exprval);
789 if(V_VT(&val) != VT_DISPATCH) {
790 TRACE("in vt %d\n", V_VT(&val));
792 V_VT(ret) = VT_EMPTY;
796 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
797 IDispatch_Release(V_DISPATCH(&val));
799 FIXME("Object doesn't support IDispatchEx\n");
803 V_VT(&retv) = VT_EMPTY;
806 identifier = SysAllocString(stat->variable->identifier);
809 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
810 if(FAILED(hres) || hres == S_FALSE)
813 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
817 TRACE("iter %s\n", debugstr_w(str));
820 hres = identifier_eval(ctx, identifier, 0, &exprval);
822 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
823 if(SUCCEEDED(hres)) {
824 V_VT(&name) = VT_BSTR;
826 hres = put_value(ctx->parser->script, &exprval, &name, &rt->ei);
827 exprval_release(&exprval);
833 hres = stat_eval(ctx, stat->statement, rt, &tmp);
840 if(rt->type == RT_CONTINUE)
841 rt->type = RT_NORMAL;
842 else if(rt->type != RT_NORMAL)
846 SysFreeString(identifier);
847 IDispatchEx_Release(in_obj);
853 if(rt->type == RT_BREAK)
854 rt->type = RT_NORMAL;
860 /* ECMA-262 3rd Edition 12.7 */
861 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
863 branch_statement_t *stat = (branch_statement_t*)_stat;
867 if(stat->identifier) {
868 FIXME("indentifier not implemented\n");
872 rt->type = RT_CONTINUE;
873 V_VT(ret) = VT_EMPTY;
877 /* ECMA-262 3rd Edition 12.8 */
878 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
880 branch_statement_t *stat = (branch_statement_t*)_stat;
884 if(stat->identifier) {
885 FIXME("indentifier not implemented\n");
890 V_VT(ret) = VT_EMPTY;
894 /* ECMA-262 3rd Edition 12.9 */
895 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
897 expression_statement_t *stat = (expression_statement_t*)_stat;
905 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
909 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
910 exprval_release(&exprval);
914 V_VT(ret) = VT_EMPTY;
917 TRACE("= %s\n", debugstr_variant(ret));
918 rt->type = RT_RETURN;
922 /* ECMA-262 3rd Edition 12.10 */
923 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
925 with_statement_t *stat = (with_statement_t*)_stat;
934 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
938 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
939 exprval_release(&exprval);
943 hres = to_object(ctx, &val, &disp);
948 obj = iface_to_jsdisp((IUnknown*)disp);
949 IDispatch_Release(disp);
951 FIXME("disp id not jsdisp\n");
955 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
960 hres = stat_eval(ctx, stat->statement, rt, ret);
962 scope_pop(&ctx->scope_chain);
966 /* ECMA-262 3rd Edition 12.12 */
967 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
973 /* ECMA-262 3rd Edition 12.13 */
974 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
976 switch_statement_t *stat = (switch_statement_t*)_stat;
977 case_clausule_t *iter, *default_clausule = NULL;
978 statement_t *stat_iter;
986 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
990 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
991 exprval_release(&exprval);
995 for(iter = stat->case_list; iter; iter = iter->next) {
997 default_clausule = iter;
1001 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1005 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
1006 exprval_release(&exprval);
1010 hres = equal2_values(&val, &cval, &b);
1011 VariantClear(&cval);
1012 if(FAILED(hres) || b)
1021 iter = default_clausule;
1023 V_VT(&val) = VT_EMPTY;
1027 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1028 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1035 if(rt->type != RT_NORMAL)
1045 if(rt->type == RT_BREAK)
1046 rt->type = RT_NORMAL;
1052 /* ECMA-262 3rd Edition 12.13 */
1053 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1055 expression_statement_t *stat = (expression_statement_t*)_stat;
1062 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1066 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1067 exprval_release(&exprval);
1072 return DISP_E_EXCEPTION;
1075 /* ECMA-262 3rd Edition 12.14 */
1076 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1078 DispatchEx *var_disp;
1083 memset(&rt->ei, 0, sizeof(jsexcept_t));
1085 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
1086 if(SUCCEEDED(hres)) {
1087 hres = jsdisp_propput_name(var_disp, block->identifier, ctx->parser->script->lcid,
1088 &ex, &rt->ei, NULL/*FIXME*/);
1089 if(SUCCEEDED(hres)) {
1090 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
1091 if(SUCCEEDED(hres)) {
1092 hres = stat_eval(ctx, block->statement, rt, &val);
1093 scope_pop(&ctx->scope_chain);
1097 jsdisp_release(var_disp);
1108 /* ECMA-262 3rd Edition 12.14 */
1109 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1111 try_statement_t *stat = (try_statement_t*)_stat;
1117 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1119 TRACE("EXCEPTION\n");
1120 if(!stat->catch_block)
1123 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1128 if(stat->finally_statement) {
1130 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1139 static HRESULT return_bool(exprval_t *ret, DWORD b)
1141 ret->type = EXPRVAL_VARIANT;
1142 V_VT(&ret->u.var) = VT_BOOL;
1143 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1148 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1153 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1157 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
1158 exprval_release(&exprval);
1162 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1163 if(SUCCEEDED(hres)) {
1164 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
1165 exprval_release(&exprval);
1176 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1178 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1181 VARIANT lval, rval, retv;
1184 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1188 hres = oper(ctx, &lval, &rval, ei, &retv);
1189 VariantClear(&lval);
1190 VariantClear(&rval);
1194 ret->type = EXPRVAL_VARIANT;
1199 /* ECMA-262 3rd Edition 11.13.2 */
1200 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1201 jsexcept_t *ei, exprval_t *ret)
1203 VARIANT retv, lval, rval;
1204 exprval_t exprval, exprvalr;
1207 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1211 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
1212 if(SUCCEEDED(hres)) {
1213 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1214 if(SUCCEEDED(hres)) {
1215 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
1216 exprval_release(&exprvalr);
1218 if(SUCCEEDED(hres)) {
1219 hres = oper(ctx, &lval, &rval, ei, &retv);
1220 VariantClear(&rval);
1222 VariantClear(&lval);
1225 if(SUCCEEDED(hres)) {
1226 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
1228 VariantClear(&retv);
1230 exprval_release(&exprval);
1235 ret->type = EXPRVAL_VARIANT;
1240 /* ECMA-262 3rd Edition 13 */
1241 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1243 function_expression_t *expr = (function_expression_t*)_expr;
1250 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1251 expr->src_str, expr->src_len, &dispex);
1255 V_VT(&var) = VT_DISPATCH;
1256 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1258 if(expr->identifier) {
1259 hres = jsdisp_propput_name(ctx->var_disp, expr->identifier, ctx->parser->script->lcid, &var, ei, NULL/*FIXME*/);
1261 jsdisp_release(dispex);
1266 ret->type = EXPRVAL_VARIANT;
1271 /* ECMA-262 3rd Edition 11.12 */
1272 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1274 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1281 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1285 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1286 exprval_release(&exprval);
1290 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1293 /* ECMA-262 3rd Edition 11.2.1 */
1294 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1296 array_expression_t *expr = (array_expression_t*)_expr;
1298 VARIANT member, val;
1301 IDispatch *obj = NULL;
1306 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
1310 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1311 exprval_release(&exprval);
1315 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1316 if(SUCCEEDED(hres)) {
1317 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1318 exprval_release(&exprval);
1322 hres = to_object(ctx, &member, &obj);
1323 VariantClear(&member);
1324 if(SUCCEEDED(hres)) {
1325 hres = to_string(ctx->parser->script, &val, ei, &str);
1326 if(SUCCEEDED(hres)) {
1327 if(flags & EXPR_STRREF) {
1328 ret->type = EXPRVAL_NAMEREF;
1329 ret->u.nameref.disp = obj;
1330 ret->u.nameref.name = str;
1334 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1337 if(SUCCEEDED(hres)) {
1338 exprval_set_idref(ret, obj, id);
1339 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1344 IDispatch_Release(obj);
1350 /* ECMA-262 3rd Edition 11.2.1 */
1351 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1353 member_expression_t *expr = (member_expression_t*)_expr;
1354 IDispatch *obj = NULL;
1363 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1367 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1368 exprval_release(&exprval);
1372 hres = to_object(ctx, &member, &obj);
1373 VariantClear(&member);
1377 str = SysAllocString(expr->identifier);
1378 if(flags & EXPR_STRREF) {
1379 ret->type = EXPRVAL_NAMEREF;
1380 ret->u.nameref.disp = obj;
1381 ret->u.nameref.name = str;
1385 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1387 if(SUCCEEDED(hres)) {
1388 exprval_set_idref(ret, obj, id);
1389 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1394 IDispatch_Release(obj);
1398 static void free_dp(DISPPARAMS *dp)
1402 for(i=0; i < dp->cArgs; i++)
1403 VariantClear(dp->rgvarg+i);
1404 heap_free(dp->rgvarg);
1407 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1413 HRESULT hres = S_OK;
1415 memset(dp, 0, sizeof(*dp));
1419 for(iter = args; iter; iter = iter->next)
1422 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1424 return E_OUTOFMEMORY;
1426 for(i = cnt, iter = args; iter; iter = iter->next) {
1427 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1431 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1432 exprval_release(&exprval);
1447 /* ECMA-262 3rd Edition 11.2.2 */
1448 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1450 call_expression_t *expr = (call_expression_t*)_expr;
1452 VARIANT constr, var;
1458 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1462 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1464 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1465 exprval_release(&exprval);
1469 if(V_VT(&constr) != VT_DISPATCH) {
1470 FIXME("throw TypeError\n");
1471 VariantClear(&constr);
1475 hres = disp_call(V_DISPATCH(&constr), DISPID_VALUE, ctx->parser->script->lcid,
1476 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1477 IDispatch_Release(V_DISPATCH(&constr));
1481 ret->type = EXPRVAL_VARIANT;
1486 /* ECMA-262 3rd Edition 11.2.3 */
1487 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1489 call_expression_t *expr = (call_expression_t*)_expr;
1497 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1501 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1502 if(SUCCEEDED(hres)) {
1503 switch(exprval.type) {
1505 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
1506 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1507 if(flags & EXPR_NOVAL)
1508 V_VT(&var) = VT_EMPTY;
1511 FIXME("unimplemented type %d\n", V_VT(&func));
1518 exprval_release(&exprval);
1522 TRACE("= %s\n", debugstr_variant(&var));
1523 ret->type = EXPRVAL_VARIANT;
1528 /* ECMA-262 3rd Edition 11.1.1 */
1529 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1533 ret->type = EXPRVAL_VARIANT;
1534 V_VT(&ret->u.var) = VT_DISPATCH;
1535 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1536 IDispatch_AddRef(ctx->this_obj);
1540 /* ECMA-262 3rd Edition 10.1.4 */
1541 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1543 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1549 identifier = SysAllocString(expr->identifier);
1551 return E_OUTOFMEMORY;
1553 hres = identifier_eval(ctx, identifier, flags, ret);
1555 SysFreeString(identifier);
1559 /* ECMA-262 3rd Edition 7.8 */
1560 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1562 literal_expression_t *expr = (literal_expression_t*)_expr;
1568 hres = literal_to_var(expr->literal, &var);
1572 ret->type = EXPRVAL_VARIANT;
1577 /* ECMA-262 3rd Edition 11.1.4 */
1578 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1580 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1581 DWORD length = 0, i = 0;
1582 array_element_t *elem;
1590 for(elem = expr->element_list; elem; elem = elem->next)
1591 length += elem->elision+1;
1592 length += expr->length;
1594 hres = create_array(ctx->parser->script, length, &array);
1598 for(elem = expr->element_list; elem; elem = elem->next) {
1601 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1605 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1606 exprval_release(&exprval);
1610 hres = jsdisp_propput_idx(array, i, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1619 jsdisp_release(array);
1623 ret->type = EXPRVAL_VARIANT;
1624 V_VT(&ret->u.var) = VT_DISPATCH;
1625 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(array);
1629 /* ECMA-262 3rd Edition 11.1.5 */
1630 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1632 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1642 hres = create_object(ctx->parser->script, NULL, &obj);
1646 for(iter = expr->property_list; iter; iter = iter->next) {
1647 hres = literal_to_var(iter->name, &tmp);
1651 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1656 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1657 if(SUCCEEDED(hres)) {
1658 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1659 exprval_release(&exprval);
1660 if(SUCCEEDED(hres)) {
1661 hres = jsdisp_propput_name(obj, name, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1666 SysFreeString(name);
1672 jsdisp_release(obj);
1676 ret->type = EXPRVAL_VARIANT;
1677 V_VT(&ret->u.var) = VT_DISPATCH;
1678 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1682 /* ECMA-262 3rd Edition 11.14 */
1683 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1685 binary_expression_t *expr = (binary_expression_t*)_expr;
1691 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1695 VariantClear(&lval);
1697 ret->type = EXPRVAL_VARIANT;
1702 /* ECMA-262 3rd Edition 11.11 */
1703 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1705 binary_expression_t *expr = (binary_expression_t*)_expr;
1713 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1717 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1718 exprval_release(&exprval);
1722 hres = to_boolean(&val, &b);
1723 if(SUCCEEDED(hres) && b) {
1724 ret->type = EXPRVAL_VARIANT;
1733 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1737 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1738 exprval_release(&exprval);
1742 ret->type = EXPRVAL_VARIANT;
1747 /* ECMA-262 3rd Edition 11.11 */
1748 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1750 binary_expression_t *expr = (binary_expression_t*)_expr;
1758 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1762 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1763 exprval_release(&exprval);
1767 hres = to_boolean(&val, &b);
1768 if(SUCCEEDED(hres) && !b) {
1769 ret->type = EXPRVAL_VARIANT;
1778 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1782 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1783 exprval_release(&exprval);
1787 ret->type = EXPRVAL_VARIANT;
1792 /* ECMA-262 3rd Edition 11.10 */
1793 static HRESULT bitor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1798 hres = to_int32(ctx->parser->script, lval, ei, &li);
1802 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1811 /* ECMA-262 3rd Edition 11.10 */
1812 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1814 binary_expression_t *expr = (binary_expression_t*)_expr;
1818 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
1821 /* ECMA-262 3rd Edition 11.10 */
1822 static HRESULT xor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1827 hres = to_int32(ctx->parser->script, lval, ei, &li);
1831 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1840 /* ECMA-262 3rd Edition 11.10 */
1841 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1843 binary_expression_t *expr = (binary_expression_t*)_expr;
1847 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
1850 /* ECMA-262 3rd Edition 11.10 */
1851 static HRESULT bitand_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1856 hres = to_int32(ctx->parser->script, lval, ei, &li);
1860 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1869 /* ECMA-262 3rd Edition 11.10 */
1870 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1872 binary_expression_t *expr = (binary_expression_t*)_expr;
1876 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
1879 /* ECMA-262 3rd Edition 11.8.6 */
1880 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1886 /* ECMA-262 3rd Edition 11.8.7 */
1887 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1893 /* ECMA-262 3rd Edition 11.6.1 */
1894 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1899 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1903 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1909 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1910 BSTR lstr = NULL, rstr = NULL;
1912 if(V_VT(&l) == VT_BSTR)
1915 hres = to_string(ctx->parser->script, &l, ei, &lstr);
1917 if(SUCCEEDED(hres)) {
1918 if(V_VT(&r) == VT_BSTR)
1921 hres = to_string(ctx->parser->script, &r, ei, &rstr);
1924 if(SUCCEEDED(hres)) {
1927 len1 = SysStringLen(lstr);
1928 len2 = SysStringLen(rstr);
1930 V_VT(retv) = VT_BSTR;
1931 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1932 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1933 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1936 if(V_VT(&l) != VT_BSTR)
1937 SysFreeString(lstr);
1938 if(V_VT(&r) != VT_BSTR)
1939 SysFreeString(rstr);
1943 hres = to_number(ctx->parser->script, &l, ei, &nl);
1944 if(SUCCEEDED(hres)) {
1945 hres = to_number(ctx->parser->script, &r, ei, &nr);
1947 num_set_val(retv, num_val(&nl) + num_val(&nr));
1956 /* ECMA-262 3rd Edition 11.6.1 */
1957 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1959 binary_expression_t *expr = (binary_expression_t*)_expr;
1963 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
1966 /* ECMA-262 3rd Edition 11.6.2 */
1967 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1972 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1976 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1980 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
1984 /* ECMA-262 3rd Edition 11.6.2 */
1985 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1987 binary_expression_t *expr = (binary_expression_t*)_expr;
1991 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
1994 /* ECMA-262 3rd Edition 11.5.1 */
1995 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2000 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2004 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2008 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2012 /* ECMA-262 3rd Edition 11.5.1 */
2013 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2015 binary_expression_t *expr = (binary_expression_t*)_expr;
2019 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2022 /* ECMA-262 3rd Edition 11.5.2 */
2023 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2028 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2032 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2036 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2040 /* ECMA-262 3rd Edition 11.5.2 */
2041 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2043 binary_expression_t *expr = (binary_expression_t*)_expr;
2047 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2050 /* ECMA-262 3rd Edition 11.5.3 */
2051 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2057 /* ECMA-262 3rd Edition 11.4.2 */
2058 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2060 unary_expression_t *expr = (unary_expression_t*)_expr;
2061 VARIANT_BOOL b = VARIANT_FALSE;
2067 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2071 switch(exprval.type) {
2072 case EXPRVAL_NAMEREF: {
2073 IDispatchEx *dispex;
2075 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2076 if(SUCCEEDED(hres)) {
2077 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name, fdexNameCaseSensitive);
2079 IDispatchEx_Release(dispex);
2084 FIXME("unsupported type %d\n", exprval.type);
2088 exprval_release(&exprval);
2092 return return_bool(ret, b);
2095 /* ECMA-262 3rd Edition 11.4.2 */
2096 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2098 unary_expression_t *expr = (unary_expression_t*)_expr;
2105 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2109 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &tmp);
2110 exprval_release(&exprval);
2116 ret->type = EXPRVAL_VARIANT;
2117 V_VT(&ret->u.var) = VT_EMPTY;
2121 /* ECMA-262 3rd Edition 11.4.3 */
2122 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2124 unary_expression_t *expr = (unary_expression_t*)_expr;
2130 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2131 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2132 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2133 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2134 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2135 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2139 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2143 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2144 exprval_release(&exprval);
2148 switch(V_VT(&val)) {
2168 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
2170 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
2171 IDispatchEx_Release(_IDispatchEx_(dispex));
2178 FIXME("unhandled vt %d\n", V_VT(&val));
2186 ret->type = EXPRVAL_VARIANT;
2187 V_VT(&ret->u.var) = VT_BSTR;
2188 V_BSTR(&ret->u.var) = SysAllocString(str);
2192 /* ECMA-262 3rd Edition 11.4.7 */
2193 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2195 unary_expression_t *expr = (unary_expression_t*)_expr;
2202 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2206 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2207 exprval_release(&exprval);
2211 hres = to_number(ctx->parser->script, &val, ei, &num);
2216 ret->type = EXPRVAL_VARIANT;
2217 num_set_val(&ret->u.var, -num_val(&num));
2221 /* ECMA-262 3rd Edition 11.4.6 */
2222 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2224 unary_expression_t *expr = (unary_expression_t*)_expr;
2231 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2235 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2236 exprval_release(&exprval);
2240 hres = to_number(ctx->parser->script, &val, ei, &num);
2244 ret->type = EXPRVAL_VARIANT;
2249 /* ECMA-262 3rd Edition 11.3.1 */
2250 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2252 unary_expression_t *expr = (unary_expression_t*)_expr;
2259 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2263 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2264 if(SUCCEEDED(hres)) {
2265 hres = to_number(ctx->parser->script, &val, ei, &num);
2269 if(SUCCEEDED(hres)) {
2271 num_set_val(&inc, num_val(&num)+1.0);
2272 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
2275 exprval_release(&exprval);
2279 ret->type = EXPRVAL_VARIANT;
2284 /* ECMA-262 3rd Edition 11.3.2 */
2285 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2287 unary_expression_t *expr = (unary_expression_t*)_expr;
2294 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2298 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2299 if(SUCCEEDED(hres)) {
2300 hres = to_number(ctx->parser->script, &val, ei, &num);
2304 if(SUCCEEDED(hres)) {
2306 num_set_val(&dec, num_val(&num)-1.0);
2307 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
2310 exprval_release(&exprval);
2314 ret->type = EXPRVAL_VARIANT;
2319 /* ECMA-262 3rd Edition 11.4.4 */
2320 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2322 unary_expression_t *expr = (unary_expression_t*)_expr;
2329 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2333 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2334 if(SUCCEEDED(hres)) {
2335 hres = to_number(ctx->parser->script, &val, ei, &num);
2339 if(SUCCEEDED(hres)) {
2340 num_set_val(&val, num_val(&num)+1.0);
2341 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2344 exprval_release(&exprval);
2348 ret->type = EXPRVAL_VARIANT;
2353 /* ECMA-262 3rd Edition 11.4.5 */
2354 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2356 unary_expression_t *expr = (unary_expression_t*)_expr;
2363 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2367 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2368 if(SUCCEEDED(hres)) {
2369 hres = to_number(ctx->parser->script, &val, ei, &num);
2373 if(SUCCEEDED(hres)) {
2374 num_set_val(&val, num_val(&num)-1.0);
2375 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2378 exprval_release(&exprval);
2382 ret->type = EXPRVAL_VARIANT;
2387 /* ECMA-262 3rd Edition 11.9.3 */
2388 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2390 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2391 return equal2_values(lval, rval, ret);
2393 /* FIXME: NULL disps should be handled in more general way */
2394 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2397 return equal_values(ctx, &v, rval, ei, ret);
2400 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2403 return equal_values(ctx, lval, &v, ei, ret);
2406 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2407 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2412 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2416 hres = to_number(ctx->parser->script, lval, ei, &v);
2420 return equal_values(ctx, &v, rval, ei, ret);
2423 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2427 hres = to_number(ctx->parser->script, rval, ei, &v);
2431 return equal_values(ctx, lval, &v, ei, ret);
2434 if(V_VT(rval) == VT_BOOL) {
2438 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2439 return equal_values(ctx, lval, &v, ei, ret);
2442 if(V_VT(lval) == VT_BOOL) {
2446 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2447 return equal_values(ctx, &v, rval, ei, ret);
2451 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2455 hres = to_primitive(ctx->parser->script, rval, ei, &v);
2459 hres = equal_values(ctx, lval, &v, ei, ret);
2466 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2470 hres = to_primitive(ctx->parser->script, lval, ei, &v);
2474 hres = equal_values(ctx, &v, rval, ei, ret);
2485 /* ECMA-262 3rd Edition 11.9.1 */
2486 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2488 binary_expression_t *expr = (binary_expression_t*)_expr;
2495 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2499 hres = equal_values(ctx, &rval, &lval, ei, &b);
2503 return return_bool(ret, b);
2506 /* ECMA-262 3rd Edition 11.9.4 */
2507 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2509 binary_expression_t *expr = (binary_expression_t*)_expr;
2516 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2520 hres = equal2_values(&rval, &lval, &b);
2524 return return_bool(ret, b);
2527 /* ECMA-262 3rd Edition 11.9.2 */
2528 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2530 binary_expression_t *expr = (binary_expression_t*)_expr;
2537 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2541 hres = equal_values(ctx, &lval, &rval, ei, &b);
2545 return return_bool(ret, !b);
2548 /* ECMA-262 3rd Edition 11.9.5 */
2549 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2551 binary_expression_t *expr = (binary_expression_t*)_expr;
2558 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2562 hres = equal2_values(&lval, &rval, &b);
2566 return return_bool(ret, !b);
2569 /* ECMA-262 3rd Edition 11.8.5 */
2570 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2572 VARIANT l, r, ln, rn;
2575 hres = to_primitive(ctx->parser->script, lval, ei, &l);
2579 hres = to_primitive(ctx->parser->script, rval, ei, &r);
2585 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2586 *ret = strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0;
2587 SysFreeString(V_BSTR(&l));
2588 SysFreeString(V_BSTR(&r));
2592 hres = to_number(ctx->parser->script, &l, ei, &ln);
2595 hres = to_number(ctx->parser->script, &r, ei, &rn);
2600 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4)
2601 *ret = V_I4(&ln) < V_I4(&rn);
2603 *ret = num_val(&ln) < num_val(&rn);
2608 /* ECMA-262 3rd Edition 11.8.1 */
2609 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2611 binary_expression_t *expr = (binary_expression_t*)_expr;
2618 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2622 hres = less_eval(ctx, &lval, &rval, ei, &b);
2623 VariantClear(&lval);
2624 VariantClear(&rval);
2628 return return_bool(ret, b);
2631 /* ECMA-262 3rd Edition 11.8.3 */
2632 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2634 binary_expression_t *expr = (binary_expression_t*)_expr;
2641 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2645 hres = less_eval(ctx, &rval, &lval, ei, &b);
2646 VariantClear(&lval);
2647 VariantClear(&rval);
2651 return return_bool(ret, !b);
2654 /* ECMA-262 3rd Edition 11.8.2 */
2655 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2657 binary_expression_t *expr = (binary_expression_t*)_expr;
2664 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2668 hres = less_eval(ctx, &rval, &lval, ei, &b);
2669 VariantClear(&lval);
2670 VariantClear(&rval);
2674 return return_bool(ret, b);
2677 /* ECMA-262 3rd Edition 11.8.4 */
2678 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2680 binary_expression_t *expr = (binary_expression_t*)_expr;
2687 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2691 hres = less_eval(ctx, &lval, &rval, ei, &b);
2692 VariantClear(&lval);
2693 VariantClear(&rval);
2697 return return_bool(ret, !b);
2700 /* ECMA-262 3rd Edition 11.4.8 */
2701 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2703 unary_expression_t *expr = (unary_expression_t*)_expr;
2711 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2715 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2716 exprval_release(&exprval);
2720 hres = to_int32(ctx->parser->script, &val, ei, &i);
2724 ret->type = EXPRVAL_VARIANT;
2725 V_VT(&ret->u.var) = VT_I4;
2726 V_I4(&ret->u.var) = ~i;
2730 /* ECMA-262 3rd Edition 11.4.9 */
2731 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2733 unary_expression_t *expr = (unary_expression_t*)_expr;
2740 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2744 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
2745 exprval_release(&exprval);
2749 return return_bool(ret, !b);
2752 /* ECMA-262 3rd Edition 11.7.1 */
2753 static HRESULT lshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2759 hres = to_int32(ctx->parser->script, lval, ei, &li);
2763 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2768 V_I4(retv) = li << (ri&0x1f);
2772 /* ECMA-262 3rd Edition 11.7.1 */
2773 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2775 binary_expression_t *expr = (binary_expression_t*)_expr;
2779 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
2782 /* ECMA-262 3rd Edition 11.7.2 */
2783 static HRESULT rshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2789 hres = to_int32(ctx->parser->script, lval, ei, &li);
2793 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2798 V_I4(retv) = li >> (ri&0x1f);
2802 /* ECMA-262 3rd Edition 11.7.2 */
2803 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2805 binary_expression_t *expr = (binary_expression_t*)_expr;
2809 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
2812 /* ECMA-262 3rd Edition 11.7.3 */
2813 static HRESULT rshift2_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2818 hres = to_uint32(ctx->parser->script, lval, ei, &li);
2822 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2827 V_I4(retv) = li >> (ri&0x1f);
2831 /* ECMA-262 3rd Edition 11.7.3 */
2832 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2834 binary_expression_t *expr = (binary_expression_t*)_expr;
2838 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
2841 /* ECMA-262 3rd Edition 11.13.1 */
2842 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2844 binary_expression_t *expr = (binary_expression_t*)_expr;
2845 exprval_t exprval, exprvalr;
2851 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
2855 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
2856 if(SUCCEEDED(hres)) {
2857 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
2858 exprval_release(&exprvalr);
2862 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
2864 exprval_release(&exprval);
2866 VariantClear(&rval);
2870 ret->type = EXPRVAL_VARIANT;
2875 /* ECMA-262 3rd Edition 11.13.2 */
2876 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2878 binary_expression_t *expr = (binary_expression_t*)_expr;
2882 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
2885 /* ECMA-262 3rd Edition 11.13.2 */
2886 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2888 binary_expression_t *expr = (binary_expression_t*)_expr;
2892 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
2895 /* ECMA-262 3rd Edition 11.13.2 */
2896 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2898 binary_expression_t *expr = (binary_expression_t*)_expr;
2902 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
2905 /* ECMA-262 3rd Edition 11.13.2 */
2906 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2908 binary_expression_t *expr = (binary_expression_t*)_expr;
2912 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
2915 /* ECMA-262 3rd Edition 11.13.2 */
2916 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2918 binary_expression_t *expr = (binary_expression_t*)_expr;
2922 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
2925 /* ECMA-262 3rd Edition 11.13.2 */
2926 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2928 binary_expression_t *expr = (binary_expression_t*)_expr;
2932 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
2935 /* ECMA-262 3rd Edition 11.13.2 */
2936 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2938 binary_expression_t *expr = (binary_expression_t*)_expr;
2942 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
2945 /* ECMA-262 3rd Edition 11.13.2 */
2946 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2952 /* ECMA-262 3rd Edition 11.13.2 */
2953 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2955 binary_expression_t *expr = (binary_expression_t*)_expr;
2959 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
2962 /* ECMA-262 3rd Edition 11.13.2 */
2963 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2965 binary_expression_t *expr = (binary_expression_t*)_expr;
2969 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
2972 /* ECMA-262 3rd Edition 11.13.2 */
2973 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2975 binary_expression_t *expr = (binary_expression_t*)_expr;
2979 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);