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 dispex_get_id(IDispatchEx *dispex, BSTR name, DWORD flags, DISPID *id)
210 return IDispatchEx_GetDispID(dispex, name, flags|fdexNameCaseSensitive, id);
213 static HRESULT disp_get_id(IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
218 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
220 TRACE("unsing IDispatch\n");
223 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
226 hres = dispex_get_id(dispex, name, flags, id);
227 IDispatchEx_Release(dispex);
231 /* ECMA-262 3rd Edition 8.7.2 */
232 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
234 if(ref->type != EXPRVAL_IDREF) {
235 FIXME("throw ReferemceError\n");
239 return disp_propput(ref->u.idref.disp, ref->u.idref.id, ctx->lcid, v, ei, NULL/*FIXME*/);
242 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
244 IObjectIdentity *identity;
245 IUnknown *unk1, *unk2;
253 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
257 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
259 IUnknown_Release(unk1);
266 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
267 if(SUCCEEDED(hres)) {
268 hres = IObjectIdentity_IsEqualObject(identity, unk2);
269 IObjectIdentity_Release(identity);
276 IUnknown_Release(unk1);
277 IUnknown_Release(unk2);
281 static inline BOOL is_num_vt(enum VARENUM vt)
283 return vt == VT_I4 || vt == VT_R8;
286 static inline DOUBLE num_val(const VARIANT *v)
288 return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
291 static inline void num_set_val(VARIANT *v, DOUBLE d)
293 if(d == (DOUBLE)(INT)d) {
302 /* ECMA-262 3rd Edition 11.9.6 */
303 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
307 if(V_VT(lval) != V_VT(rval)) {
308 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
309 *ret = num_val(lval) == num_val(rval);
323 *ret = V_I4(lval) == V_I4(rval);
326 *ret = V_R8(lval) == V_R8(rval);
329 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
332 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
334 *ret = !V_BOOL(lval) == !V_BOOL(rval);
337 FIXME("unimplemented vt %d\n", V_VT(lval));
344 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
346 V_VT(v) = literal->vt;
353 V_I4(v) = literal->u.lval;
356 V_R8(v) = literal->u.dval;
359 V_BSTR(v) = SysAllocString(literal->u.wstr);
362 V_BOOL(v) = literal->u.bval;
365 IDispatch_AddRef(literal->u.disp);
366 V_DISPATCH(v) = literal->u.disp;
369 ERR("wrong type %d\n", V_VT(v));
376 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
378 script_ctx_t *script = parser->script;
379 function_declaration_t *func;
380 parser_ctx_t *prev_parser;
383 exec_ctx_t *prev_ctx;
387 for(func = source->functions; func; func = func->next) {
388 DispatchEx *func_obj;
391 hres = create_source_function(parser, func->parameter_list, func->source_elements, ctx->scope_chain, &func_obj);
395 V_VT(&var) = VT_DISPATCH;
396 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
397 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
398 IDispatchEx_Release(_IDispatchEx_(func_obj));
403 prev_ctx = script->exec_ctx;
404 script->exec_ctx = ctx;
406 prev_parser = ctx->parser;
407 ctx->parser = parser;
409 V_VT(&val) = VT_EMPTY;
410 memset(&rt, 0, sizeof(rt));
413 for(stat = source->statement; stat; stat = stat->next) {
414 hres = stat_eval(ctx, stat, &rt, &tmp);
420 if(rt.type != RT_NORMAL)
424 script->exec_ctx = prev_ctx;
425 ctx->parser = prev_parser;
427 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
428 FIXME("wrong rt %d\n", rt.type);
445 /* ECMA-262 3rd Edition 10.1.4 */
446 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
448 scope_chain_t *scope;
453 TRACE("%s\n", debugstr_w(identifier));
455 for(scope = ctx->scope_chain; scope; scope = scope->next) {
456 hres = dispex_get_id(_IDispatchEx_(scope->obj), identifier, 0, &id);
462 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
466 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->global), identifier, 0, &id);
467 if(SUCCEEDED(hres)) {
468 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
472 for(item = ctx->parser->script->named_items; item; item = item->next) {
473 hres = disp_get_id(item->disp, identifier, 0, &id);
479 exprval_set_idref(ret, (IDispatch*)item->disp, id);
483 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->script_disp), identifier, 0, &id);
484 if(SUCCEEDED(hres)) {
485 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
489 if(flags & EXPR_NEWREF) {
490 hres = dispex_get_id(_IDispatchEx_(ctx->var_disp), identifier, fdexNameEnsure, &id);
494 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
498 WARN("Could not find identifier %s\n", debugstr_w(identifier));
502 /* ECMA-262 3rd Edition 12.1 */
503 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
505 block_statement_t *stat = (block_statement_t*)_stat;
512 V_VT(&val) = VT_EMPTY;
513 for(iter = stat->stat_list; iter; iter = iter->next) {
514 hres = stat_eval(ctx, iter, rt, &tmp);
520 if(rt->type != RT_NORMAL)
533 /* ECMA-262 3rd Edition 12.2 */
534 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
536 variable_declaration_t *iter;
537 HRESULT hres = E_FAIL;
539 for(iter = var_list; iter; iter = iter->next) {
545 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
549 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
550 exprval_release(&exprval);
554 V_VT(&val) = VT_EMPTY;
557 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
566 /* ECMA-262 3rd Edition 12.2 */
567 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
569 var_statement_t *stat = (var_statement_t*)_stat;
574 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
578 V_VT(ret) = VT_EMPTY;
582 /* ECMA-262 3rd Edition 12.3 */
583 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
587 V_VT(ret) = VT_EMPTY;
591 /* ECMA-262 3rd Edition 12.4 */
592 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
594 expression_statement_t *stat = (expression_statement_t*)_stat;
601 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
605 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
606 exprval_release(&exprval);
611 TRACE("= %s\n", debugstr_variant(ret));
615 /* ECMA-262 3rd Edition 12.5 */
616 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
618 if_statement_t *stat = (if_statement_t*)_stat;
625 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
629 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
630 exprval_release(&exprval);
635 hres = stat_eval(ctx, stat->if_stat, rt, ret);
636 else if(stat->else_stat)
637 hres = stat_eval(ctx, stat->else_stat, rt, ret);
639 V_VT(ret) = VT_EMPTY;
644 HRESULT dowhile_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
650 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
656 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
662 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
668 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
674 /* ECMA-262 3rd Edition 12.8 */
675 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
677 branch_statement_t *stat = (branch_statement_t*)_stat;
681 if(stat->identifier) {
682 FIXME("indentifier not implemented\n");
687 V_VT(ret) = VT_EMPTY;
691 /* ECMA-262 3rd Edition 12.9 */
692 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
694 expression_statement_t *stat = (expression_statement_t*)_stat;
702 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
706 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
707 exprval_release(&exprval);
711 V_VT(ret) = VT_EMPTY;
714 TRACE("= %s\n", debugstr_variant(ret));
715 rt->type = RT_RETURN;
719 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
725 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
731 /* ECMA-262 3rd Edition 12.13 */
732 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
734 switch_statement_t *stat = (switch_statement_t*)_stat;
735 case_clausule_t *iter, *default_clausule = NULL;
736 statement_t *stat_iter;
744 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
748 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
749 exprval_release(&exprval);
753 for(iter = stat->case_list; iter; iter = iter->next) {
755 default_clausule = iter;
759 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
763 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
764 exprval_release(&exprval);
768 hres = equal2_values(&val, &cval, &b);
770 if(FAILED(hres) || b)
779 iter = default_clausule;
781 V_VT(&val) = VT_EMPTY;
785 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
786 hres = stat_eval(ctx, stat_iter, rt, &tmp);
793 if(rt->type != RT_NORMAL)
803 if(rt->type == RT_BREAK)
804 rt->type = RT_NORMAL;
810 /* ECMA-262 3rd Edition 12.13 */
811 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
813 expression_statement_t *stat = (expression_statement_t*)_stat;
820 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
824 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
825 exprval_release(&exprval);
830 return DISP_E_EXCEPTION;
833 /* ECMA-262 3rd Edition 12.14 */
834 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
836 DispatchEx *var_disp;
841 memset(&rt->ei, 0, sizeof(jsexcept_t));
843 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
844 if(SUCCEEDED(hres)) {
845 hres = jsdisp_propput_name(var_disp, block->identifier, ctx->parser->script->lcid,
846 &ex, &rt->ei, NULL/*FIXME*/);
847 if(SUCCEEDED(hres)) {
848 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
849 if(SUCCEEDED(hres)) {
850 hres = stat_eval(ctx, block->statement, rt, &val);
851 scope_pop(&ctx->scope_chain);
855 jsdisp_release(var_disp);
866 /* ECMA-262 3rd Edition 12.14 */
867 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
869 try_statement_t *stat = (try_statement_t*)_stat;
875 hres = stat_eval(ctx, stat->try_statement, rt, &val);
877 TRACE("EXCEPTION\n");
878 if(!stat->catch_block)
881 hres = catch_eval(ctx, stat->catch_block, rt, &val);
886 if(stat->finally_statement) {
888 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
897 static HRESULT return_bool(exprval_t *ret, DWORD b)
899 ret->type = EXPRVAL_VARIANT;
900 V_VT(&ret->u.var) = VT_BOOL;
901 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
906 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
911 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
915 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
916 exprval_release(&exprval);
920 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
921 if(SUCCEEDED(hres)) {
922 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
923 exprval_release(&exprval);
934 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
936 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
939 VARIANT lval, rval, retv;
942 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
946 hres = oper(ctx, &lval, &rval, ei, &retv);
952 ret->type = EXPRVAL_VARIANT;
957 /* ECMA-262 3rd Edition 11.13.2 */
958 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
959 jsexcept_t *ei, exprval_t *ret)
961 VARIANT retv, lval, rval;
962 exprval_t exprval, exprvalr;
965 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
969 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
970 if(SUCCEEDED(hres)) {
971 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
972 if(SUCCEEDED(hres)) {
973 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
974 exprval_release(&exprvalr);
976 if(SUCCEEDED(hres)) {
977 hres = oper(ctx, &lval, &rval, ei, &retv);
983 if(SUCCEEDED(hres)) {
984 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
988 exprval_release(&exprval);
993 ret->type = EXPRVAL_VARIANT;
998 /* ECMA-262 3rd Edition 13 */
999 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1001 function_expression_t *expr = (function_expression_t*)_expr;
1008 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain, &dispex);
1012 V_VT(&var) = VT_DISPATCH;
1013 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1015 if(expr->identifier) {
1016 hres = jsdisp_propput_name(ctx->var_disp, expr->identifier, ctx->parser->script->lcid, &var, ei, NULL/*FIXME*/);
1018 jsdisp_release(dispex);
1023 ret->type = EXPRVAL_VARIANT;
1028 /* ECMA-262 3rd Edition 11.12 */
1029 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1031 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1038 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1042 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1043 exprval_release(&exprval);
1047 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1050 /* ECMA-262 3rd Edition 11.2.1 */
1051 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1053 array_expression_t *expr = (array_expression_t*)_expr;
1055 VARIANT member, val;
1058 IDispatch *obj = NULL;
1063 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
1067 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1068 exprval_release(&exprval);
1072 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1073 if(SUCCEEDED(hres)) {
1074 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1075 exprval_release(&exprval);
1079 hres = to_object(ctx, &member, &obj);
1080 VariantClear(&member);
1081 if(SUCCEEDED(hres)) {
1082 hres = to_string(ctx->parser->script, &val, ei, &str);
1083 if(SUCCEEDED(hres)) {
1084 if(flags & EXPR_STRREF) {
1085 ret->type = EXPRVAL_NAMEREF;
1086 ret->u.nameref.disp = obj;
1087 ret->u.nameref.name = str;
1091 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1094 if(SUCCEEDED(hres)) {
1095 exprval_set_idref(ret, obj, id);
1096 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1101 IDispatch_Release(obj);
1107 /* ECMA-262 3rd Edition 11.2.1 */
1108 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1110 member_expression_t *expr = (member_expression_t*)_expr;
1111 IDispatch *obj = NULL;
1120 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1124 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1125 exprval_release(&exprval);
1129 hres = to_object(ctx, &member, &obj);
1130 VariantClear(&member);
1134 str = SysAllocString(expr->identifier);
1135 if(flags & EXPR_STRREF) {
1136 ret->type = EXPRVAL_NAMEREF;
1137 ret->u.nameref.disp = obj;
1138 ret->u.nameref.name = str;
1142 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1144 if(SUCCEEDED(hres)) {
1145 exprval_set_idref(ret, obj, id);
1146 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1151 IDispatch_Release(obj);
1155 static void free_dp(DISPPARAMS *dp)
1159 for(i=0; i < dp->cArgs; i++)
1160 VariantClear(dp->rgvarg+i);
1161 heap_free(dp->rgvarg);
1164 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1170 HRESULT hres = S_OK;
1172 memset(dp, 0, sizeof(*dp));
1176 for(iter = args; iter; iter = iter->next)
1179 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1181 return E_OUTOFMEMORY;
1183 for(i = cnt, iter = args; iter; iter = iter->next) {
1184 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1188 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1189 exprval_release(&exprval);
1204 /* ECMA-262 3rd Edition 11.2.2 */
1205 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1207 call_expression_t *expr = (call_expression_t*)_expr;
1209 VARIANT constr, var;
1215 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1219 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1221 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1222 exprval_release(&exprval);
1226 if(V_VT(&constr) != VT_DISPATCH) {
1227 FIXME("throw TypeError\n");
1228 VariantClear(&constr);
1232 hres = disp_call(V_DISPATCH(&constr), DISPID_VALUE, ctx->parser->script->lcid,
1233 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1234 IDispatch_Release(V_DISPATCH(&constr));
1238 ret->type = EXPRVAL_VARIANT;
1243 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1245 call_expression_t *expr = (call_expression_t*)_expr;
1253 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1257 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1258 if(SUCCEEDED(hres)) {
1259 switch(exprval.type) {
1261 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
1262 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1263 if(flags & EXPR_NOVAL)
1264 V_VT(&var) = VT_EMPTY;
1267 FIXME("unimplemented type %d\n", V_VT(&func));
1274 exprval_release(&exprval);
1278 TRACE("= %s\n", debugstr_variant(&var));
1279 ret->type = EXPRVAL_VARIANT;
1284 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1288 ret->type = EXPRVAL_VARIANT;
1289 V_VT(&ret->u.var) = VT_DISPATCH;
1290 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1291 IDispatch_AddRef(ctx->this_obj);
1295 /* ECMA-262 3rd Edition 10.1.4 */
1296 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1298 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1304 identifier = SysAllocString(expr->identifier);
1306 return E_OUTOFMEMORY;
1308 hres = identifier_eval(ctx, identifier, flags, ret);
1310 SysFreeString(identifier);
1314 /* ECMA-262 3rd Edition 7.8 */
1315 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1317 literal_expression_t *expr = (literal_expression_t*)_expr;
1323 hres = literal_to_var(expr->literal, &var);
1327 ret->type = EXPRVAL_VARIANT;
1332 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1338 /* ECMA-262 3rd Edition 11.1.5 */
1339 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1341 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1351 hres = create_object(ctx->parser->script, NULL, &obj);
1355 for(iter = expr->property_list; iter; iter = iter->next) {
1356 hres = literal_to_var(iter->name, &tmp);
1360 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1365 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1366 if(SUCCEEDED(hres)) {
1367 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1368 exprval_release(&exprval);
1369 if(SUCCEEDED(hres)) {
1370 hres = jsdisp_propput_name(obj, name, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1375 SysFreeString(name);
1381 jsdisp_release(obj);
1385 ret->type = EXPRVAL_VARIANT;
1386 V_VT(&ret->u.var) = VT_DISPATCH;
1387 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1391 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1397 /* ECMA-262 3rd Edition 11.11 */
1398 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1400 binary_expression_t *expr = (binary_expression_t*)_expr;
1408 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1412 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1413 exprval_release(&exprval);
1417 hres = to_boolean(&val, &b);
1418 if(SUCCEEDED(hres) && b) {
1419 ret->type = EXPRVAL_VARIANT;
1428 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1432 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1433 exprval_release(&exprval);
1437 ret->type = EXPRVAL_VARIANT;
1442 /* ECMA-262 3rd Edition 11.11 */
1443 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1445 binary_expression_t *expr = (binary_expression_t*)_expr;
1453 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1457 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1458 exprval_release(&exprval);
1462 hres = to_boolean(&val, &b);
1463 if(SUCCEEDED(hres) && !b) {
1464 ret->type = EXPRVAL_VARIANT;
1473 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1477 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1478 exprval_release(&exprval);
1482 ret->type = EXPRVAL_VARIANT;
1487 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1493 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1499 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1505 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1511 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1517 /* ECMA-262 3rd Edition 11.6.1 */
1518 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1523 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1527 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1533 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1534 BSTR lstr = NULL, rstr = NULL;
1536 if(V_VT(&l) == VT_BSTR)
1539 hres = to_string(ctx->parser->script, &l, ei, &lstr);
1541 if(SUCCEEDED(hres)) {
1542 if(V_VT(&r) == VT_BSTR)
1545 hres = to_string(ctx->parser->script, &r, ei, &rstr);
1548 if(SUCCEEDED(hres)) {
1551 len1 = SysStringLen(lstr);
1552 len2 = SysStringLen(rstr);
1554 V_VT(retv) = VT_BSTR;
1555 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1556 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1557 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1560 if(lstr && V_VT(&l) != VT_BSTR)
1561 SysFreeString(lstr);
1562 if(rstr && V_VT(&r) != VT_BSTR)
1563 SysFreeString(rstr);
1567 hres = to_number(ctx->parser->script, &l, ei, &nl);
1568 if(SUCCEEDED(hres)) {
1569 hres = to_number(ctx->parser->script, &r, ei, &nr);
1571 num_set_val(retv, num_val(&nl) + num_val(&nr));
1580 /* ECMA-262 3rd Edition 11.6.1 */
1581 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1583 binary_expression_t *expr = (binary_expression_t*)_expr;
1587 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
1590 /* ECMA-262 3rd Edition 11.6.2 */
1591 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1596 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1600 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1604 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
1608 /* ECMA-262 3rd Edition 11.6.2 */
1609 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1611 binary_expression_t *expr = (binary_expression_t*)_expr;
1615 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
1618 /* ECMA-262 3rd Edition 11.5.1 */
1619 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1624 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1628 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1632 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
1636 /* ECMA-262 3rd Edition 11.5.1 */
1637 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1639 binary_expression_t *expr = (binary_expression_t*)_expr;
1643 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
1646 /* ECMA-262 3rd Edition 11.5.2 */
1647 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1652 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1656 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1660 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
1664 /* ECMA-262 3rd Edition 11.5.2 */
1665 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1667 binary_expression_t *expr = (binary_expression_t*)_expr;
1671 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
1674 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1680 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1686 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1692 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1694 unary_expression_t *expr = (unary_expression_t*)_expr;
1700 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
1701 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
1702 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
1703 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
1704 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
1705 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1709 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1713 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1714 exprval_release(&exprval);
1718 switch(V_VT(&val)) {
1738 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
1740 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
1741 IDispatchEx_Release(_IDispatchEx_(dispex));
1748 FIXME("unhandled vt %d\n", V_VT(&val));
1756 ret->type = EXPRVAL_VARIANT;
1757 V_VT(&ret->u.var) = VT_BSTR;
1758 V_BSTR(&ret->u.var) = SysAllocString(str);
1762 /* ECMA-262 3rd Edition 11.4.7 */
1763 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1765 unary_expression_t *expr = (unary_expression_t*)_expr;
1772 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1776 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1777 exprval_release(&exprval);
1781 hres = to_number(ctx->parser->script, &val, ei, &num);
1786 ret->type = EXPRVAL_VARIANT;
1787 num_set_val(&ret->u.var, -num_val(&num));
1791 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1797 /* ECMA-262 3rd Edition 11.3.1 */
1798 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1800 unary_expression_t *expr = (unary_expression_t*)_expr;
1807 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1811 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1812 if(SUCCEEDED(hres)) {
1813 hres = to_number(ctx->parser->script, &val, ei, &num);
1817 if(SUCCEEDED(hres)) {
1819 num_set_val(&inc, num_val(&num)+1.0);
1820 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
1823 exprval_release(&exprval);
1827 ret->type = EXPRVAL_VARIANT;
1832 /* ECMA-262 3rd Edition 11.3.2 */
1833 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1835 unary_expression_t *expr = (unary_expression_t*)_expr;
1842 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1846 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1847 if(SUCCEEDED(hres)) {
1848 hres = to_number(ctx->parser->script, &val, ei, &num);
1852 if(SUCCEEDED(hres)) {
1854 num_set_val(&dec, num_val(&num)-1.0);
1855 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
1858 exprval_release(&exprval);
1862 ret->type = EXPRVAL_VARIANT;
1867 /* ECMA-262 3rd Edition 11.4.4 */
1868 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1870 unary_expression_t *expr = (unary_expression_t*)_expr;
1877 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1881 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1882 if(SUCCEEDED(hres)) {
1883 hres = to_number(ctx->parser->script, &val, ei, &num);
1887 if(SUCCEEDED(hres)) {
1888 num_set_val(&val, num_val(&num)+1.0);
1889 hres = put_value(ctx->parser->script, &exprval, &val, ei);
1892 exprval_release(&exprval);
1896 ret->type = EXPRVAL_VARIANT;
1901 /* ECMA-262 3rd Edition 11.4.5 */
1902 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1904 unary_expression_t *expr = (unary_expression_t*)_expr;
1911 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1915 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1916 if(SUCCEEDED(hres)) {
1917 hres = to_number(ctx->parser->script, &val, ei, &num);
1921 if(SUCCEEDED(hres)) {
1922 num_set_val(&val, num_val(&num)-1.0);
1923 hres = put_value(ctx->parser->script, &exprval, &val, ei);
1926 exprval_release(&exprval);
1930 ret->type = EXPRVAL_VARIANT;
1935 /* ECMA-262 3rd Edition 11.9.3 */
1936 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1938 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1939 return equal2_values(lval, rval, ret);
1941 /* FIXME: NULL disps should be handled in more general way */
1942 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1945 return equal_values(ctx, &v, rval, ei, ret);
1948 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1951 return equal_values(ctx, lval, &v, ei, ret);
1954 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1955 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1960 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1964 hres = to_number(ctx->parser->script, lval, ei, &v);
1968 return equal_values(ctx, &v, rval, ei, ret);
1971 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
1975 hres = to_number(ctx->parser->script, rval, ei, &v);
1979 return equal_values(ctx, lval, &v, ei, ret);
1982 if(V_VT(rval) == VT_BOOL) {
1986 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
1987 return equal_values(ctx, lval, &v, ei, ret);
1990 if(V_VT(lval) == VT_BOOL) {
1994 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
1995 return equal_values(ctx, &v, rval, ei, ret);
1999 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2003 hres = to_primitive(ctx->parser->script, rval, ei, &v);
2007 hres = equal_values(ctx, lval, &v, ei, ret);
2014 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2018 hres = to_primitive(ctx->parser->script, lval, ei, &v);
2022 hres = equal_values(ctx, &v, rval, ei, ret);
2033 /* ECMA-262 3rd Edition 11.9.1 */
2034 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2036 binary_expression_t *expr = (binary_expression_t*)_expr;
2043 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2047 hres = equal_values(ctx, &rval, &lval, ei, &b);
2051 return return_bool(ret, b);
2054 /* ECMA-262 3rd Edition 11.9.4 */
2055 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2057 binary_expression_t *expr = (binary_expression_t*)_expr;
2064 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2068 hres = equal2_values(&rval, &lval, &b);
2072 return return_bool(ret, b);
2075 /* ECMA-262 3rd Edition 11.9.2 */
2076 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2078 binary_expression_t *expr = (binary_expression_t*)_expr;
2085 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2089 hres = equal_values(ctx, &lval, &rval, ei, &b);
2093 return return_bool(ret, !b);
2096 /* ECMA-262 3rd Edition 11.9.5 */
2097 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2099 binary_expression_t *expr = (binary_expression_t*)_expr;
2106 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2110 hres = equal2_values(&rval, &lval, &b);
2114 return return_bool(ret, !b);
2117 /* ECMA-262 3rd Edition 11.8.5 */
2118 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2120 VARIANT l, r, ln, rn;
2123 hres = to_primitive(ctx->parser->script, lval, ei, &l);
2127 hres = to_primitive(ctx->parser->script, rval, ei, &r);
2133 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2134 *ret = strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0;
2135 SysFreeString(V_BSTR(&l));
2136 SysFreeString(V_BSTR(&r));
2140 hres = to_number(ctx->parser->script, &l, ei, &ln);
2143 hres = to_number(ctx->parser->script, &r, ei, &rn);
2148 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4)
2149 *ret = V_I4(&ln) < V_I4(&rn);
2151 *ret = num_val(&ln) < num_val(&rn);
2156 /* ECMA-262 3rd Edition 11.8.1 */
2157 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2159 binary_expression_t *expr = (binary_expression_t*)_expr;
2166 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2170 hres = less_eval(ctx, &lval, &rval, ei, &b);
2171 VariantClear(&lval);
2172 VariantClear(&rval);
2176 return return_bool(ret, b);
2179 /* ECMA-262 3rd Edition 11.8.3 */
2180 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2182 binary_expression_t *expr = (binary_expression_t*)_expr;
2189 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2193 hres = less_eval(ctx, &rval, &lval, ei, &b);
2194 VariantClear(&lval);
2195 VariantClear(&rval);
2199 return return_bool(ret, !b);
2202 /* ECMA-262 3rd Edition 11.8.2 */
2203 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2205 binary_expression_t *expr = (binary_expression_t*)_expr;
2212 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2216 hres = less_eval(ctx, &rval, &lval, ei, &b);
2217 VariantClear(&lval);
2218 VariantClear(&rval);
2222 return return_bool(ret, b);
2225 /* ECMA-262 3rd Edition 11.8.4 */
2226 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2228 binary_expression_t *expr = (binary_expression_t*)_expr;
2235 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2239 hres = less_eval(ctx, &lval, &rval, ei, &b);
2240 VariantClear(&lval);
2241 VariantClear(&rval);
2245 return return_bool(ret, !b);
2248 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2254 /* ECMA-262 3rd Edition 11.4.9 */
2255 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2257 unary_expression_t *expr = (unary_expression_t*)_expr;
2264 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2268 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
2269 exprval_release(&exprval);
2273 return return_bool(ret, !b);
2276 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2282 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2288 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2294 /* ECMA-262 3rd Edition 11.13.1 */
2295 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2297 binary_expression_t *expr = (binary_expression_t*)_expr;
2298 exprval_t exprval, exprvalr;
2304 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
2308 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
2309 if(SUCCEEDED(hres)) {
2310 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
2311 exprval_release(&exprvalr);
2315 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
2317 exprval_release(&exprval);
2319 VariantClear(&rval);
2323 ret->type = EXPRVAL_VARIANT;
2328 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2334 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2340 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2346 /* ECMA-262 3rd Edition 11.13.2 */
2347 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2349 binary_expression_t *expr = (binary_expression_t*)_expr;
2353 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
2356 /* ECMA-262 3rd Edition 11.13.2 */
2357 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2359 binary_expression_t *expr = (binary_expression_t*)_expr;
2363 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
2366 /* ECMA-262 3rd Edition 11.13.2 */
2367 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2369 binary_expression_t *expr = (binary_expression_t*)_expr;
2373 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
2376 /* ECMA-262 3rd Edition 11.13.2 */
2377 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2379 binary_expression_t *expr = (binary_expression_t*)_expr;
2383 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
2386 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2392 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2398 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2404 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)