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 /* ECMA-262 3rd Edition 11.9.6 */
292 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
296 if(V_VT(lval) != V_VT(rval)) {
297 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
298 *ret = num_val(lval) == num_val(rval);
312 *ret = V_I4(lval) == V_I4(rval);
315 *ret = V_R8(lval) == V_R8(rval);
318 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
321 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
323 *ret = !V_BOOL(lval) == !V_BOOL(rval);
326 FIXME("unimplemented vt %d\n", V_VT(lval));
333 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
335 V_VT(v) = literal->vt;
342 V_I4(v) = literal->u.lval;
345 V_R8(v) = literal->u.dval;
348 V_BSTR(v) = SysAllocString(literal->u.wstr);
351 V_BOOL(v) = literal->u.bval;
354 IDispatch_AddRef(literal->u.disp);
355 V_DISPATCH(v) = literal->u.disp;
358 ERR("wrong type %d\n", V_VT(v));
365 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
367 script_ctx_t *script = parser->script;
368 function_declaration_t *func;
369 parser_ctx_t *prev_parser;
372 exec_ctx_t *prev_ctx;
376 for(func = source->functions; func; func = func->next) {
377 DispatchEx *func_obj;
380 hres = create_source_function(parser, func->parameter_list, func->source_elements, ctx->scope_chain, &func_obj);
384 V_VT(&var) = VT_DISPATCH;
385 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
386 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
387 IDispatchEx_Release(_IDispatchEx_(func_obj));
392 prev_ctx = script->exec_ctx;
393 script->exec_ctx = ctx;
395 prev_parser = ctx->parser;
396 ctx->parser = parser;
398 V_VT(&val) = VT_EMPTY;
399 memset(&rt, 0, sizeof(rt));
402 for(stat = source->statement; stat; stat = stat->next) {
403 hres = stat_eval(ctx, stat, &rt, &tmp);
409 if(rt.type != RT_NORMAL)
413 script->exec_ctx = prev_ctx;
414 ctx->parser = prev_parser;
416 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
417 FIXME("wrong rt %d\n", rt.type);
434 /* ECMA-262 3rd Edition 10.1.4 */
435 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
437 scope_chain_t *scope;
442 TRACE("%s\n", debugstr_w(identifier));
444 for(scope = ctx->scope_chain; scope; scope = scope->next) {
445 hres = dispex_get_id(_IDispatchEx_(scope->obj), identifier, 0, &id);
451 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
455 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->global), identifier, 0, &id);
456 if(SUCCEEDED(hres)) {
457 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
461 for(item = ctx->parser->script->named_items; item; item = item->next) {
462 hres = disp_get_id(item->disp, identifier, 0, &id);
468 exprval_set_idref(ret, (IDispatch*)item->disp, id);
472 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->script_disp), identifier, 0, &id);
473 if(SUCCEEDED(hres)) {
474 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
478 if(flags & EXPR_NEWREF) {
479 hres = dispex_get_id(_IDispatchEx_(ctx->var_disp), identifier, fdexNameEnsure, &id);
483 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
487 WARN("Could not find identifier %s\n", debugstr_w(identifier));
491 /* ECMA-262 3rd Edition 12.1 */
492 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
494 block_statement_t *stat = (block_statement_t*)_stat;
501 V_VT(&val) = VT_EMPTY;
502 for(iter = stat->stat_list; iter; iter = iter->next) {
503 hres = stat_eval(ctx, iter, rt, &tmp);
509 if(rt->type != RT_NORMAL)
522 /* ECMA-262 3rd Edition 12.2 */
523 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
525 variable_declaration_t *iter;
526 HRESULT hres = E_FAIL;
528 for(iter = var_list; iter; iter = iter->next) {
534 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
538 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
539 exprval_release(&exprval);
543 V_VT(&val) = VT_EMPTY;
546 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
555 /* ECMA-262 3rd Edition 12.2 */
556 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
558 var_statement_t *stat = (var_statement_t*)_stat;
563 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
567 V_VT(ret) = VT_EMPTY;
571 /* ECMA-262 3rd Edition 12.3 */
572 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
576 V_VT(ret) = VT_EMPTY;
580 /* ECMA-262 3rd Edition 12.4 */
581 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
583 expression_statement_t *stat = (expression_statement_t*)_stat;
590 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
594 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
595 exprval_release(&exprval);
600 TRACE("= %s\n", debugstr_variant(ret));
604 /* ECMA-262 3rd Edition 12.5 */
605 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
607 if_statement_t *stat = (if_statement_t*)_stat;
614 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
618 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
619 exprval_release(&exprval);
624 hres = stat_eval(ctx, stat->if_stat, rt, ret);
625 else if(stat->else_stat)
626 hres = stat_eval(ctx, stat->else_stat, rt, ret);
628 V_VT(ret) = VT_EMPTY;
633 /* ECMA-262 3rd Edition 12.6.2 */
634 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
636 while_statement_t *stat = (while_statement_t*)_stat;
645 V_VT(&val) = VT_EMPTY;
646 test_expr = !stat->do_while;
650 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
654 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
655 exprval_release(&exprval);
656 if(FAILED(hres) || !b)
662 hres = stat_eval(ctx, stat->statement, rt, &tmp);
669 if(rt->type == RT_CONTINUE)
670 rt->type = RT_NORMAL;
671 if(rt->type != RT_NORMAL)
680 if(rt->type == RT_BREAK)
681 rt->type = RT_NORMAL;
687 /* ECMA-262 3rd Edition 12.6.3 */
688 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
690 for_statement_t *stat = (for_statement_t*)_stat;
691 VARIANT val, tmp, retv;
698 if(stat->variable_list) {
699 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
702 }else if(stat->begin_expr) {
703 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
707 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
708 exprval_release(&exprval);
715 V_VT(&retv) = VT_EMPTY;
719 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
723 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
724 exprval_release(&exprval);
725 if(FAILED(hres) || !b)
729 hres = stat_eval(ctx, stat->statement, rt, &tmp);
736 if(rt->type == RT_CONTINUE)
737 rt->type = RT_NORMAL;
738 else if(rt->type != RT_NORMAL)
742 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
746 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
747 exprval_release(&exprval);
760 if(rt->type == RT_BREAK)
761 rt->type = RT_NORMAL;
767 /* ECMA-262 3rd Edition 12.6.4 */
768 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
770 forin_statement_t *stat = (forin_statement_t*)_stat;
771 VARIANT val, name, retv, tmp;
772 DISPID id = DISPID_STARTENUM;
773 BSTR str, identifier = NULL;
781 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
786 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
790 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
791 exprval_release(&exprval);
795 if(V_VT(&val) != VT_DISPATCH) {
796 FIXME("in vt %d\n", V_VT(&val));
801 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
802 IDispatch_Release(V_DISPATCH(&val));
804 FIXME("Object doesn't support IDispatchEx\n");
808 V_VT(&retv) = VT_EMPTY;
811 identifier = SysAllocString(stat->variable->identifier);
814 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
815 if(FAILED(hres) || hres == S_FALSE)
818 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
822 TRACE("iter %s\n", debugstr_w(str));
825 hres = identifier_eval(ctx, identifier, 0, &exprval);
827 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
828 if(SUCCEEDED(hres)) {
829 V_VT(&name) = VT_BSTR;
831 hres = put_value(ctx->parser->script, &exprval, &name, &rt->ei);
832 exprval_release(&exprval);
838 hres = stat_eval(ctx, stat->statement, rt, &tmp);
845 if(rt->type == RT_CONTINUE)
846 rt->type = RT_NORMAL;
847 else if(rt->type != RT_NORMAL)
851 SysFreeString(identifier);
852 IDispatchEx_Release(in_obj);
858 if(rt->type == RT_BREAK)
859 rt->type = RT_NORMAL;
865 /* ECMA-262 3rd Edition 12.7 */
866 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
868 branch_statement_t *stat = (branch_statement_t*)_stat;
872 if(stat->identifier) {
873 FIXME("indentifier not implemented\n");
877 rt->type = RT_CONTINUE;
878 V_VT(ret) = VT_EMPTY;
882 /* ECMA-262 3rd Edition 12.8 */
883 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
885 branch_statement_t *stat = (branch_statement_t*)_stat;
889 if(stat->identifier) {
890 FIXME("indentifier not implemented\n");
895 V_VT(ret) = VT_EMPTY;
899 /* ECMA-262 3rd Edition 12.9 */
900 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
902 expression_statement_t *stat = (expression_statement_t*)_stat;
910 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
914 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
915 exprval_release(&exprval);
919 V_VT(ret) = VT_EMPTY;
922 TRACE("= %s\n", debugstr_variant(ret));
923 rt->type = RT_RETURN;
927 /* ECMA-262 3rd Edition 12.10 */
928 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
930 with_statement_t *stat = (with_statement_t*)_stat;
939 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
943 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
944 exprval_release(&exprval);
948 hres = to_object(ctx, &val, &disp);
953 obj = iface_to_jsdisp((IUnknown*)disp);
954 IDispatch_Release(disp);
956 FIXME("disp id not jsdisp\n");
960 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
964 hres = stat_eval(ctx, stat->statement, rt, ret);
966 scope_pop(&ctx->scope_chain);
970 /* ECMA-262 3rd Edition 12.12 */
971 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
977 /* ECMA-262 3rd Edition 12.13 */
978 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
980 switch_statement_t *stat = (switch_statement_t*)_stat;
981 case_clausule_t *iter, *default_clausule = NULL;
982 statement_t *stat_iter;
990 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
994 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
995 exprval_release(&exprval);
999 for(iter = stat->case_list; iter; iter = iter->next) {
1001 default_clausule = iter;
1005 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1009 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
1010 exprval_release(&exprval);
1014 hres = equal2_values(&val, &cval, &b);
1015 VariantClear(&cval);
1016 if(FAILED(hres) || b)
1025 iter = default_clausule;
1027 V_VT(&val) = VT_EMPTY;
1031 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1032 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1039 if(rt->type != RT_NORMAL)
1049 if(rt->type == RT_BREAK)
1050 rt->type = RT_NORMAL;
1056 /* ECMA-262 3rd Edition 12.13 */
1057 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1059 expression_statement_t *stat = (expression_statement_t*)_stat;
1066 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1070 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1071 exprval_release(&exprval);
1076 return DISP_E_EXCEPTION;
1079 /* ECMA-262 3rd Edition 12.14 */
1080 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1082 DispatchEx *var_disp;
1087 memset(&rt->ei, 0, sizeof(jsexcept_t));
1089 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
1090 if(SUCCEEDED(hres)) {
1091 hres = jsdisp_propput_name(var_disp, block->identifier, ctx->parser->script->lcid,
1092 &ex, &rt->ei, NULL/*FIXME*/);
1093 if(SUCCEEDED(hres)) {
1094 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
1095 if(SUCCEEDED(hres)) {
1096 hres = stat_eval(ctx, block->statement, rt, &val);
1097 scope_pop(&ctx->scope_chain);
1101 jsdisp_release(var_disp);
1112 /* ECMA-262 3rd Edition 12.14 */
1113 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1115 try_statement_t *stat = (try_statement_t*)_stat;
1121 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1123 TRACE("EXCEPTION\n");
1124 if(!stat->catch_block)
1127 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1132 if(stat->finally_statement) {
1134 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1143 static HRESULT return_bool(exprval_t *ret, DWORD b)
1145 ret->type = EXPRVAL_VARIANT;
1146 V_VT(&ret->u.var) = VT_BOOL;
1147 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1152 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1157 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1161 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
1162 exprval_release(&exprval);
1166 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1167 if(SUCCEEDED(hres)) {
1168 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
1169 exprval_release(&exprval);
1180 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1182 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1185 VARIANT lval, rval, retv;
1188 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1192 hres = oper(ctx, &lval, &rval, ei, &retv);
1193 VariantClear(&lval);
1194 VariantClear(&rval);
1198 ret->type = EXPRVAL_VARIANT;
1203 /* ECMA-262 3rd Edition 11.13.2 */
1204 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1205 jsexcept_t *ei, exprval_t *ret)
1207 VARIANT retv, lval, rval;
1208 exprval_t exprval, exprvalr;
1211 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1215 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
1216 if(SUCCEEDED(hres)) {
1217 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1218 if(SUCCEEDED(hres)) {
1219 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
1220 exprval_release(&exprvalr);
1222 if(SUCCEEDED(hres)) {
1223 hres = oper(ctx, &lval, &rval, ei, &retv);
1224 VariantClear(&rval);
1226 VariantClear(&lval);
1229 if(SUCCEEDED(hres)) {
1230 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
1232 VariantClear(&retv);
1234 exprval_release(&exprval);
1239 ret->type = EXPRVAL_VARIANT;
1244 /* ECMA-262 3rd Edition 13 */
1245 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1247 function_expression_t *expr = (function_expression_t*)_expr;
1254 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain, &dispex);
1258 V_VT(&var) = VT_DISPATCH;
1259 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1261 if(expr->identifier) {
1262 hres = jsdisp_propput_name(ctx->var_disp, expr->identifier, ctx->parser->script->lcid, &var, ei, NULL/*FIXME*/);
1264 jsdisp_release(dispex);
1269 ret->type = EXPRVAL_VARIANT;
1274 /* ECMA-262 3rd Edition 11.12 */
1275 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1277 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1284 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1288 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1289 exprval_release(&exprval);
1293 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1296 /* ECMA-262 3rd Edition 11.2.1 */
1297 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1299 array_expression_t *expr = (array_expression_t*)_expr;
1301 VARIANT member, val;
1304 IDispatch *obj = NULL;
1309 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
1313 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1314 exprval_release(&exprval);
1318 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1319 if(SUCCEEDED(hres)) {
1320 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1321 exprval_release(&exprval);
1325 hres = to_object(ctx, &member, &obj);
1326 VariantClear(&member);
1327 if(SUCCEEDED(hres)) {
1328 hres = to_string(ctx->parser->script, &val, ei, &str);
1329 if(SUCCEEDED(hres)) {
1330 if(flags & EXPR_STRREF) {
1331 ret->type = EXPRVAL_NAMEREF;
1332 ret->u.nameref.disp = obj;
1333 ret->u.nameref.name = str;
1337 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1340 if(SUCCEEDED(hres)) {
1341 exprval_set_idref(ret, obj, id);
1342 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1347 IDispatch_Release(obj);
1353 /* ECMA-262 3rd Edition 11.2.1 */
1354 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1356 member_expression_t *expr = (member_expression_t*)_expr;
1357 IDispatch *obj = NULL;
1366 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1370 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1371 exprval_release(&exprval);
1375 hres = to_object(ctx, &member, &obj);
1376 VariantClear(&member);
1380 str = SysAllocString(expr->identifier);
1381 if(flags & EXPR_STRREF) {
1382 ret->type = EXPRVAL_NAMEREF;
1383 ret->u.nameref.disp = obj;
1384 ret->u.nameref.name = str;
1388 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1390 if(SUCCEEDED(hres)) {
1391 exprval_set_idref(ret, obj, id);
1392 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1397 IDispatch_Release(obj);
1401 static void free_dp(DISPPARAMS *dp)
1405 for(i=0; i < dp->cArgs; i++)
1406 VariantClear(dp->rgvarg+i);
1407 heap_free(dp->rgvarg);
1410 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1416 HRESULT hres = S_OK;
1418 memset(dp, 0, sizeof(*dp));
1422 for(iter = args; iter; iter = iter->next)
1425 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1427 return E_OUTOFMEMORY;
1429 for(i = cnt, iter = args; iter; iter = iter->next) {
1430 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1434 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1435 exprval_release(&exprval);
1450 /* ECMA-262 3rd Edition 11.2.2 */
1451 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1453 call_expression_t *expr = (call_expression_t*)_expr;
1455 VARIANT constr, var;
1461 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1465 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1467 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1468 exprval_release(&exprval);
1472 if(V_VT(&constr) != VT_DISPATCH) {
1473 FIXME("throw TypeError\n");
1474 VariantClear(&constr);
1478 hres = disp_call(V_DISPATCH(&constr), DISPID_VALUE, ctx->parser->script->lcid,
1479 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1480 IDispatch_Release(V_DISPATCH(&constr));
1484 ret->type = EXPRVAL_VARIANT;
1489 /* ECMA-262 3rd Edition 11.2.3 */
1490 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1492 call_expression_t *expr = (call_expression_t*)_expr;
1500 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1504 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1505 if(SUCCEEDED(hres)) {
1506 switch(exprval.type) {
1508 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
1509 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1510 if(flags & EXPR_NOVAL)
1511 V_VT(&var) = VT_EMPTY;
1514 FIXME("unimplemented type %d\n", V_VT(&func));
1521 exprval_release(&exprval);
1525 TRACE("= %s\n", debugstr_variant(&var));
1526 ret->type = EXPRVAL_VARIANT;
1531 /* ECMA-262 3rd Edition 11.1.1 */
1532 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1536 ret->type = EXPRVAL_VARIANT;
1537 V_VT(&ret->u.var) = VT_DISPATCH;
1538 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1539 IDispatch_AddRef(ctx->this_obj);
1543 /* ECMA-262 3rd Edition 10.1.4 */
1544 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1546 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1552 identifier = SysAllocString(expr->identifier);
1554 return E_OUTOFMEMORY;
1556 hres = identifier_eval(ctx, identifier, flags, ret);
1558 SysFreeString(identifier);
1562 /* ECMA-262 3rd Edition 7.8 */
1563 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1565 literal_expression_t *expr = (literal_expression_t*)_expr;
1571 hres = literal_to_var(expr->literal, &var);
1575 ret->type = EXPRVAL_VARIANT;
1580 /* ECMA-262 3rd Edition 11.1.4 */
1581 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1583 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1584 DWORD length = 0, i = 0;
1585 array_element_t *elem;
1593 for(elem = expr->element_list; elem; elem = elem->next)
1594 length += elem->elision+1;
1595 length += expr->length;
1597 hres = create_array(ctx->parser->script, length, &array);
1601 for(elem = expr->element_list; elem; elem = elem->next) {
1604 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1608 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1609 exprval_release(&exprval);
1613 hres = jsdisp_propput_idx(array, i, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1622 jsdisp_release(array);
1626 ret->type = EXPRVAL_VARIANT;
1627 V_VT(&ret->u.var) = VT_DISPATCH;
1628 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(array);
1632 /* ECMA-262 3rd Edition 11.1.5 */
1633 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1635 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1645 hres = create_object(ctx->parser->script, NULL, &obj);
1649 for(iter = expr->property_list; iter; iter = iter->next) {
1650 hres = literal_to_var(iter->name, &tmp);
1654 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1659 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1660 if(SUCCEEDED(hres)) {
1661 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1662 exprval_release(&exprval);
1663 if(SUCCEEDED(hres)) {
1664 hres = jsdisp_propput_name(obj, name, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1669 SysFreeString(name);
1675 jsdisp_release(obj);
1679 ret->type = EXPRVAL_VARIANT;
1680 V_VT(&ret->u.var) = VT_DISPATCH;
1681 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1685 /* ECMA-262 3rd Edition 11.14 */
1686 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1688 binary_expression_t *expr = (binary_expression_t*)_expr;
1694 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1698 VariantClear(&lval);
1700 ret->type = EXPRVAL_VARIANT;
1705 /* ECMA-262 3rd Edition 11.11 */
1706 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1708 binary_expression_t *expr = (binary_expression_t*)_expr;
1716 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1720 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1721 exprval_release(&exprval);
1725 hres = to_boolean(&val, &b);
1726 if(SUCCEEDED(hres) && b) {
1727 ret->type = EXPRVAL_VARIANT;
1736 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1740 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1741 exprval_release(&exprval);
1745 ret->type = EXPRVAL_VARIANT;
1750 /* ECMA-262 3rd Edition 11.11 */
1751 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1753 binary_expression_t *expr = (binary_expression_t*)_expr;
1761 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1765 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1766 exprval_release(&exprval);
1770 hres = to_boolean(&val, &b);
1771 if(SUCCEEDED(hres) && !b) {
1772 ret->type = EXPRVAL_VARIANT;
1781 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1785 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1786 exprval_release(&exprval);
1790 ret->type = EXPRVAL_VARIANT;
1795 /* ECMA-262 3rd Edition 11.10 */
1796 static HRESULT bitor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1801 hres = to_int32(ctx->parser->script, lval, ei, &li);
1805 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1814 /* ECMA-262 3rd Edition 11.10 */
1815 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1817 binary_expression_t *expr = (binary_expression_t*)_expr;
1821 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
1824 /* ECMA-262 3rd Edition 11.10 */
1825 static HRESULT xor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1830 hres = to_int32(ctx->parser->script, lval, ei, &li);
1834 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1843 /* ECMA-262 3rd Edition 11.10 */
1844 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1846 binary_expression_t *expr = (binary_expression_t*)_expr;
1850 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
1853 /* ECMA-262 3rd Edition 11.10 */
1854 static HRESULT bitand_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1859 hres = to_int32(ctx->parser->script, lval, ei, &li);
1863 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1872 /* ECMA-262 3rd Edition 11.10 */
1873 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1875 binary_expression_t *expr = (binary_expression_t*)_expr;
1879 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
1882 /* ECMA-262 3rd Edition 11.8.6 */
1883 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1889 /* ECMA-262 3rd Edition 11.8.7 */
1890 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1896 /* ECMA-262 3rd Edition 11.6.1 */
1897 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1902 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1906 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1912 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1913 BSTR lstr = NULL, rstr = NULL;
1915 if(V_VT(&l) == VT_BSTR)
1918 hres = to_string(ctx->parser->script, &l, ei, &lstr);
1920 if(SUCCEEDED(hres)) {
1921 if(V_VT(&r) == VT_BSTR)
1924 hres = to_string(ctx->parser->script, &r, ei, &rstr);
1927 if(SUCCEEDED(hres)) {
1930 len1 = SysStringLen(lstr);
1931 len2 = SysStringLen(rstr);
1933 V_VT(retv) = VT_BSTR;
1934 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1935 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1936 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1939 if(lstr && V_VT(&l) != VT_BSTR)
1940 SysFreeString(lstr);
1941 if(rstr && V_VT(&r) != VT_BSTR)
1942 SysFreeString(rstr);
1946 hres = to_number(ctx->parser->script, &l, ei, &nl);
1947 if(SUCCEEDED(hres)) {
1948 hres = to_number(ctx->parser->script, &r, ei, &nr);
1950 num_set_val(retv, num_val(&nl) + num_val(&nr));
1959 /* ECMA-262 3rd Edition 11.6.1 */
1960 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1962 binary_expression_t *expr = (binary_expression_t*)_expr;
1966 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
1969 /* ECMA-262 3rd Edition 11.6.2 */
1970 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1975 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1979 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1983 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
1987 /* ECMA-262 3rd Edition 11.6.2 */
1988 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1990 binary_expression_t *expr = (binary_expression_t*)_expr;
1994 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
1997 /* ECMA-262 3rd Edition 11.5.1 */
1998 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2003 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2007 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2011 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2015 /* ECMA-262 3rd Edition 11.5.1 */
2016 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2018 binary_expression_t *expr = (binary_expression_t*)_expr;
2022 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2025 /* ECMA-262 3rd Edition 11.5.2 */
2026 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2031 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2035 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2039 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2043 /* ECMA-262 3rd Edition 11.5.2 */
2044 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2046 binary_expression_t *expr = (binary_expression_t*)_expr;
2050 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2053 /* ECMA-262 3rd Edition 11.5.3 */
2054 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2060 /* ECMA-262 3rd Edition 11.4.2 */
2061 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2063 unary_expression_t *expr = (unary_expression_t*)_expr;
2064 VARIANT_BOOL b = VARIANT_FALSE;
2070 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2074 switch(exprval.type) {
2075 case EXPRVAL_NAMEREF: {
2076 IDispatchEx *dispex;
2078 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2079 if(SUCCEEDED(hres)) {
2080 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name, fdexNameCaseSensitive);
2082 IDispatchEx_Release(dispex);
2087 FIXME("unsupported type %d\n", exprval.type);
2091 exprval_release(&exprval);
2095 return return_bool(ret, b);
2098 /* ECMA-262 3rd Edition 11.4.2 */
2099 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2101 unary_expression_t *expr = (unary_expression_t*)_expr;
2108 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2112 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &tmp);
2113 exprval_release(&exprval);
2119 ret->type = EXPRVAL_VARIANT;
2120 V_VT(&ret->u.var) = VT_EMPTY;
2124 /* ECMA-262 3rd Edition 11.4.3 */
2125 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2127 unary_expression_t *expr = (unary_expression_t*)_expr;
2133 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2134 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2135 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2136 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2137 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2138 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2142 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2146 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2147 exprval_release(&exprval);
2151 switch(V_VT(&val)) {
2171 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
2173 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
2174 IDispatchEx_Release(_IDispatchEx_(dispex));
2181 FIXME("unhandled vt %d\n", V_VT(&val));
2189 ret->type = EXPRVAL_VARIANT;
2190 V_VT(&ret->u.var) = VT_BSTR;
2191 V_BSTR(&ret->u.var) = SysAllocString(str);
2195 /* ECMA-262 3rd Edition 11.4.7 */
2196 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2198 unary_expression_t *expr = (unary_expression_t*)_expr;
2205 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2209 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2210 exprval_release(&exprval);
2214 hres = to_number(ctx->parser->script, &val, ei, &num);
2219 ret->type = EXPRVAL_VARIANT;
2220 num_set_val(&ret->u.var, -num_val(&num));
2224 /* ECMA-262 3rd Edition 11.4.6 */
2225 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2227 unary_expression_t *expr = (unary_expression_t*)_expr;
2234 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2238 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2239 exprval_release(&exprval);
2243 hres = to_number(ctx->parser->script, &val, ei, &num);
2247 ret->type = EXPRVAL_VARIANT;
2252 /* ECMA-262 3rd Edition 11.3.1 */
2253 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2255 unary_expression_t *expr = (unary_expression_t*)_expr;
2262 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2266 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2267 if(SUCCEEDED(hres)) {
2268 hres = to_number(ctx->parser->script, &val, ei, &num);
2272 if(SUCCEEDED(hres)) {
2274 num_set_val(&inc, num_val(&num)+1.0);
2275 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
2278 exprval_release(&exprval);
2282 ret->type = EXPRVAL_VARIANT;
2287 /* ECMA-262 3rd Edition 11.3.2 */
2288 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2290 unary_expression_t *expr = (unary_expression_t*)_expr;
2297 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2301 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2302 if(SUCCEEDED(hres)) {
2303 hres = to_number(ctx->parser->script, &val, ei, &num);
2307 if(SUCCEEDED(hres)) {
2309 num_set_val(&dec, num_val(&num)-1.0);
2310 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
2313 exprval_release(&exprval);
2317 ret->type = EXPRVAL_VARIANT;
2322 /* ECMA-262 3rd Edition 11.4.4 */
2323 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2325 unary_expression_t *expr = (unary_expression_t*)_expr;
2332 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2336 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2337 if(SUCCEEDED(hres)) {
2338 hres = to_number(ctx->parser->script, &val, ei, &num);
2342 if(SUCCEEDED(hres)) {
2343 num_set_val(&val, num_val(&num)+1.0);
2344 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2347 exprval_release(&exprval);
2351 ret->type = EXPRVAL_VARIANT;
2356 /* ECMA-262 3rd Edition 11.4.5 */
2357 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2359 unary_expression_t *expr = (unary_expression_t*)_expr;
2366 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2370 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2371 if(SUCCEEDED(hres)) {
2372 hres = to_number(ctx->parser->script, &val, ei, &num);
2376 if(SUCCEEDED(hres)) {
2377 num_set_val(&val, num_val(&num)-1.0);
2378 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2381 exprval_release(&exprval);
2385 ret->type = EXPRVAL_VARIANT;
2390 /* ECMA-262 3rd Edition 11.9.3 */
2391 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2393 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2394 return equal2_values(lval, rval, ret);
2396 /* FIXME: NULL disps should be handled in more general way */
2397 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2400 return equal_values(ctx, &v, rval, ei, ret);
2403 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2406 return equal_values(ctx, lval, &v, ei, ret);
2409 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2410 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2415 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2419 hres = to_number(ctx->parser->script, lval, ei, &v);
2423 return equal_values(ctx, &v, rval, ei, ret);
2426 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2430 hres = to_number(ctx->parser->script, rval, ei, &v);
2434 return equal_values(ctx, lval, &v, ei, ret);
2437 if(V_VT(rval) == VT_BOOL) {
2441 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2442 return equal_values(ctx, lval, &v, ei, ret);
2445 if(V_VT(lval) == VT_BOOL) {
2449 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2450 return equal_values(ctx, &v, rval, ei, ret);
2454 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2458 hres = to_primitive(ctx->parser->script, rval, ei, &v);
2462 hres = equal_values(ctx, lval, &v, ei, ret);
2469 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2473 hres = to_primitive(ctx->parser->script, lval, ei, &v);
2477 hres = equal_values(ctx, &v, rval, ei, ret);
2488 /* ECMA-262 3rd Edition 11.9.1 */
2489 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2491 binary_expression_t *expr = (binary_expression_t*)_expr;
2498 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2502 hres = equal_values(ctx, &rval, &lval, ei, &b);
2506 return return_bool(ret, b);
2509 /* ECMA-262 3rd Edition 11.9.4 */
2510 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2512 binary_expression_t *expr = (binary_expression_t*)_expr;
2519 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2523 hres = equal2_values(&rval, &lval, &b);
2527 return return_bool(ret, b);
2530 /* ECMA-262 3rd Edition 11.9.2 */
2531 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2533 binary_expression_t *expr = (binary_expression_t*)_expr;
2540 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2544 hres = equal_values(ctx, &lval, &rval, ei, &b);
2548 return return_bool(ret, !b);
2551 /* ECMA-262 3rd Edition 11.9.5 */
2552 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2554 binary_expression_t *expr = (binary_expression_t*)_expr;
2561 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2565 hres = equal2_values(&lval, &rval, &b);
2569 return return_bool(ret, !b);
2572 /* ECMA-262 3rd Edition 11.8.5 */
2573 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2575 VARIANT l, r, ln, rn;
2578 hres = to_primitive(ctx->parser->script, lval, ei, &l);
2582 hres = to_primitive(ctx->parser->script, rval, ei, &r);
2588 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2589 *ret = strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0;
2590 SysFreeString(V_BSTR(&l));
2591 SysFreeString(V_BSTR(&r));
2595 hres = to_number(ctx->parser->script, &l, ei, &ln);
2598 hres = to_number(ctx->parser->script, &r, ei, &rn);
2603 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4)
2604 *ret = V_I4(&ln) < V_I4(&rn);
2606 *ret = num_val(&ln) < num_val(&rn);
2611 /* ECMA-262 3rd Edition 11.8.1 */
2612 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2614 binary_expression_t *expr = (binary_expression_t*)_expr;
2621 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2625 hres = less_eval(ctx, &lval, &rval, ei, &b);
2626 VariantClear(&lval);
2627 VariantClear(&rval);
2631 return return_bool(ret, b);
2634 /* ECMA-262 3rd Edition 11.8.3 */
2635 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2637 binary_expression_t *expr = (binary_expression_t*)_expr;
2644 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2648 hres = less_eval(ctx, &rval, &lval, ei, &b);
2649 VariantClear(&lval);
2650 VariantClear(&rval);
2654 return return_bool(ret, !b);
2657 /* ECMA-262 3rd Edition 11.8.2 */
2658 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2660 binary_expression_t *expr = (binary_expression_t*)_expr;
2667 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2671 hres = less_eval(ctx, &rval, &lval, ei, &b);
2672 VariantClear(&lval);
2673 VariantClear(&rval);
2677 return return_bool(ret, b);
2680 /* ECMA-262 3rd Edition 11.8.4 */
2681 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2683 binary_expression_t *expr = (binary_expression_t*)_expr;
2690 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2694 hres = less_eval(ctx, &lval, &rval, ei, &b);
2695 VariantClear(&lval);
2696 VariantClear(&rval);
2700 return return_bool(ret, !b);
2703 /* ECMA-262 3rd Edition 11.4.8 */
2704 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2706 unary_expression_t *expr = (unary_expression_t*)_expr;
2714 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2718 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2719 exprval_release(&exprval);
2723 hres = to_int32(ctx->parser->script, &val, ei, &i);
2727 ret->type = EXPRVAL_VARIANT;
2728 V_VT(&ret->u.var) = VT_I4;
2729 V_I4(&ret->u.var) = ~i;
2733 /* ECMA-262 3rd Edition 11.4.9 */
2734 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2736 unary_expression_t *expr = (unary_expression_t*)_expr;
2743 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2747 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
2748 exprval_release(&exprval);
2752 return return_bool(ret, !b);
2755 /* ECMA-262 3rd Edition 11.7.1 */
2756 static HRESULT lshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2762 hres = to_int32(ctx->parser->script, lval, ei, &li);
2766 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2771 V_I4(retv) = li << (ri&0x1f);
2775 /* ECMA-262 3rd Edition 11.7.1 */
2776 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2778 binary_expression_t *expr = (binary_expression_t*)_expr;
2782 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
2785 /* ECMA-262 3rd Edition 11.7.2 */
2786 static HRESULT rshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2792 hres = to_int32(ctx->parser->script, lval, ei, &li);
2796 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2801 V_I4(retv) = li >> (ri&0x1f);
2805 /* ECMA-262 3rd Edition 11.7.2 */
2806 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2808 binary_expression_t *expr = (binary_expression_t*)_expr;
2812 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
2815 /* ECMA-262 3rd Edition 11.7.3 */
2816 static HRESULT rshift2_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2821 hres = to_uint32(ctx->parser->script, lval, ei, &li);
2825 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2830 V_I4(retv) = li >> (ri&0x1f);
2834 /* ECMA-262 3rd Edition 11.7.3 */
2835 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2837 binary_expression_t *expr = (binary_expression_t*)_expr;
2841 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
2844 /* ECMA-262 3rd Edition 11.13.1 */
2845 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2847 binary_expression_t *expr = (binary_expression_t*)_expr;
2848 exprval_t exprval, exprvalr;
2854 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
2858 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
2859 if(SUCCEEDED(hres)) {
2860 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
2861 exprval_release(&exprvalr);
2865 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
2867 exprval_release(&exprval);
2869 VariantClear(&rval);
2873 ret->type = EXPRVAL_VARIANT;
2878 /* ECMA-262 3rd Edition 11.13.2 */
2879 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2881 binary_expression_t *expr = (binary_expression_t*)_expr;
2885 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
2888 /* ECMA-262 3rd Edition 11.13.2 */
2889 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2891 binary_expression_t *expr = (binary_expression_t*)_expr;
2895 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
2898 /* ECMA-262 3rd Edition 11.13.2 */
2899 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2901 binary_expression_t *expr = (binary_expression_t*)_expr;
2905 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
2908 /* ECMA-262 3rd Edition 11.13.2 */
2909 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2911 binary_expression_t *expr = (binary_expression_t*)_expr;
2915 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
2918 /* ECMA-262 3rd Edition 11.13.2 */
2919 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2921 binary_expression_t *expr = (binary_expression_t*)_expr;
2925 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
2928 /* ECMA-262 3rd Edition 11.13.2 */
2929 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2931 binary_expression_t *expr = (binary_expression_t*)_expr;
2935 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
2938 /* ECMA-262 3rd Edition 11.13.2 */
2939 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2941 binary_expression_t *expr = (binary_expression_t*)_expr;
2945 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
2948 /* ECMA-262 3rd Edition 11.13.2 */
2949 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2955 /* ECMA-262 3rd Edition 11.13.2 */
2956 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2958 binary_expression_t *expr = (binary_expression_t*)_expr;
2962 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
2965 /* ECMA-262 3rd Edition 11.13.2 */
2966 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2968 binary_expression_t *expr = (binary_expression_t*)_expr;
2972 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
2975 /* ECMA-262 3rd Edition 11.13.2 */
2976 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2978 binary_expression_t *expr = (binary_expression_t*)_expr;
2982 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);