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 /* ECMA-262 3rd Edition 11.9.6 */
282 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
286 if(V_VT(lval) != V_VT(rval)) {
287 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
288 *ret = num_val(lval) == num_val(rval);
302 *ret = V_I4(lval) == V_I4(rval);
305 *ret = V_R8(lval) == V_R8(rval);
308 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
311 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
313 *ret = !V_BOOL(lval) == !V_BOOL(rval);
316 FIXME("unimplemented vt %d\n", V_VT(lval));
323 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
325 V_VT(v) = literal->vt;
332 V_I4(v) = literal->u.lval;
335 V_R8(v) = literal->u.dval;
338 V_BSTR(v) = SysAllocString(literal->u.wstr);
341 V_BOOL(v) = literal->u.bval;
344 IDispatch_AddRef(literal->u.disp);
345 V_DISPATCH(v) = literal->u.disp;
348 ERR("wrong type %d\n", V_VT(v));
355 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
357 script_ctx_t *script = parser->script;
358 function_declaration_t *func;
359 parser_ctx_t *prev_parser;
362 exec_ctx_t *prev_ctx;
366 for(func = source->functions; func; func = func->next) {
367 DispatchEx *func_obj;
370 hres = create_source_function(parser, func->parameter_list, func->source_elements,
371 ctx->scope_chain, func->src_str, func->src_len, &func_obj);
375 V_VT(&var) = VT_DISPATCH;
376 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
377 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
378 IDispatchEx_Release(_IDispatchEx_(func_obj));
383 prev_ctx = script->exec_ctx;
384 script->exec_ctx = ctx;
386 prev_parser = ctx->parser;
387 ctx->parser = parser;
389 V_VT(&val) = VT_EMPTY;
390 memset(&rt, 0, sizeof(rt));
393 for(stat = source->statement; stat; stat = stat->next) {
394 hres = stat_eval(ctx, stat, &rt, &tmp);
400 if(rt.type != RT_NORMAL)
404 script->exec_ctx = prev_ctx;
405 ctx->parser = prev_parser;
407 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
408 FIXME("wrong rt %d\n", rt.type);
425 /* ECMA-262 3rd Edition 10.1.4 */
426 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
428 scope_chain_t *scope;
433 TRACE("%s\n", debugstr_w(identifier));
435 for(scope = ctx->scope_chain; scope; scope = scope->next) {
436 hres = dispex_get_id(_IDispatchEx_(scope->obj), identifier, 0, &id);
442 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
446 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->global), identifier, 0, &id);
447 if(SUCCEEDED(hres)) {
448 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
452 for(item = ctx->parser->script->named_items; item; item = item->next) {
453 hres = disp_get_id(item->disp, identifier, 0, &id);
459 exprval_set_idref(ret, (IDispatch*)item->disp, id);
463 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->script_disp), identifier, 0, &id);
464 if(SUCCEEDED(hres)) {
465 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
469 if(flags & EXPR_NEWREF) {
470 hres = dispex_get_id(_IDispatchEx_(ctx->var_disp), identifier, fdexNameEnsure, &id);
474 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
478 WARN("Could not find identifier %s\n", debugstr_w(identifier));
482 /* ECMA-262 3rd Edition 12.1 */
483 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
485 block_statement_t *stat = (block_statement_t*)_stat;
492 V_VT(&val) = VT_EMPTY;
493 for(iter = stat->stat_list; iter; iter = iter->next) {
494 hres = stat_eval(ctx, iter, rt, &tmp);
500 if(rt->type != RT_NORMAL)
513 /* ECMA-262 3rd Edition 12.2 */
514 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
516 variable_declaration_t *iter;
517 HRESULT hres = E_FAIL;
519 for(iter = var_list; iter; iter = iter->next) {
525 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
529 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
530 exprval_release(&exprval);
534 V_VT(&val) = VT_EMPTY;
537 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
546 /* ECMA-262 3rd Edition 12.2 */
547 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
549 var_statement_t *stat = (var_statement_t*)_stat;
554 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
558 V_VT(ret) = VT_EMPTY;
562 /* ECMA-262 3rd Edition 12.3 */
563 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
567 V_VT(ret) = VT_EMPTY;
571 /* ECMA-262 3rd Edition 12.4 */
572 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
574 expression_statement_t *stat = (expression_statement_t*)_stat;
581 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
585 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
586 exprval_release(&exprval);
591 TRACE("= %s\n", debugstr_variant(ret));
595 /* ECMA-262 3rd Edition 12.5 */
596 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
598 if_statement_t *stat = (if_statement_t*)_stat;
605 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
609 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
610 exprval_release(&exprval);
615 hres = stat_eval(ctx, stat->if_stat, rt, ret);
616 else if(stat->else_stat)
617 hres = stat_eval(ctx, stat->else_stat, rt, ret);
619 V_VT(ret) = VT_EMPTY;
624 /* ECMA-262 3rd Edition 12.6.2 */
625 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
627 while_statement_t *stat = (while_statement_t*)_stat;
636 V_VT(&val) = VT_EMPTY;
637 test_expr = !stat->do_while;
641 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
645 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
646 exprval_release(&exprval);
647 if(FAILED(hres) || !b)
653 hres = stat_eval(ctx, stat->statement, rt, &tmp);
660 if(rt->type == RT_CONTINUE)
661 rt->type = RT_NORMAL;
662 if(rt->type != RT_NORMAL)
671 if(rt->type == RT_BREAK)
672 rt->type = RT_NORMAL;
678 /* ECMA-262 3rd Edition 12.6.3 */
679 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
681 for_statement_t *stat = (for_statement_t*)_stat;
682 VARIANT val, tmp, retv;
689 if(stat->variable_list) {
690 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
693 }else if(stat->begin_expr) {
694 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
698 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
699 exprval_release(&exprval);
706 V_VT(&retv) = VT_EMPTY;
710 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
714 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
715 exprval_release(&exprval);
716 if(FAILED(hres) || !b)
720 hres = stat_eval(ctx, stat->statement, rt, &tmp);
727 if(rt->type == RT_CONTINUE)
728 rt->type = RT_NORMAL;
729 else if(rt->type != RT_NORMAL)
733 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
737 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
738 exprval_release(&exprval);
751 if(rt->type == RT_BREAK)
752 rt->type = RT_NORMAL;
758 /* ECMA-262 3rd Edition 12.6.4 */
759 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
761 forin_statement_t *stat = (forin_statement_t*)_stat;
762 VARIANT val, name, retv, tmp;
763 DISPID id = DISPID_STARTENUM;
764 BSTR str, identifier = NULL;
772 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
777 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
781 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
782 exprval_release(&exprval);
786 if(V_VT(&val) != VT_DISPATCH) {
787 TRACE("in vt %d\n", V_VT(&val));
789 V_VT(ret) = VT_EMPTY;
793 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
794 IDispatch_Release(V_DISPATCH(&val));
796 FIXME("Object doesn't support IDispatchEx\n");
800 V_VT(&retv) = VT_EMPTY;
803 identifier = SysAllocString(stat->variable->identifier);
806 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
807 if(FAILED(hres) || hres == S_FALSE)
810 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
814 TRACE("iter %s\n", debugstr_w(str));
817 hres = identifier_eval(ctx, identifier, 0, &exprval);
819 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
820 if(SUCCEEDED(hres)) {
821 V_VT(&name) = VT_BSTR;
823 hres = put_value(ctx->parser->script, &exprval, &name, &rt->ei);
824 exprval_release(&exprval);
830 hres = stat_eval(ctx, stat->statement, rt, &tmp);
837 if(rt->type == RT_CONTINUE)
838 rt->type = RT_NORMAL;
839 else if(rt->type != RT_NORMAL)
843 SysFreeString(identifier);
844 IDispatchEx_Release(in_obj);
850 if(rt->type == RT_BREAK)
851 rt->type = RT_NORMAL;
857 /* ECMA-262 3rd Edition 12.7 */
858 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
860 branch_statement_t *stat = (branch_statement_t*)_stat;
864 if(stat->identifier) {
865 FIXME("indentifier not implemented\n");
869 rt->type = RT_CONTINUE;
870 V_VT(ret) = VT_EMPTY;
874 /* ECMA-262 3rd Edition 12.8 */
875 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
877 branch_statement_t *stat = (branch_statement_t*)_stat;
881 if(stat->identifier) {
882 FIXME("indentifier not implemented\n");
887 V_VT(ret) = VT_EMPTY;
891 /* ECMA-262 3rd Edition 12.9 */
892 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
894 expression_statement_t *stat = (expression_statement_t*)_stat;
902 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
906 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
907 exprval_release(&exprval);
911 V_VT(ret) = VT_EMPTY;
914 TRACE("= %s\n", debugstr_variant(ret));
915 rt->type = RT_RETURN;
919 /* ECMA-262 3rd Edition 12.10 */
920 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
922 with_statement_t *stat = (with_statement_t*)_stat;
931 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
935 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
936 exprval_release(&exprval);
940 hres = to_object(ctx, &val, &disp);
945 obj = iface_to_jsdisp((IUnknown*)disp);
946 IDispatch_Release(disp);
948 FIXME("disp id not jsdisp\n");
952 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
957 hres = stat_eval(ctx, stat->statement, rt, ret);
959 scope_pop(&ctx->scope_chain);
963 /* ECMA-262 3rd Edition 12.12 */
964 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
970 /* ECMA-262 3rd Edition 12.13 */
971 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
973 switch_statement_t *stat = (switch_statement_t*)_stat;
974 case_clausule_t *iter, *default_clausule = NULL;
975 statement_t *stat_iter;
983 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
987 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
988 exprval_release(&exprval);
992 for(iter = stat->case_list; iter; iter = iter->next) {
994 default_clausule = iter;
998 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1002 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
1003 exprval_release(&exprval);
1007 hres = equal2_values(&val, &cval, &b);
1008 VariantClear(&cval);
1009 if(FAILED(hres) || b)
1018 iter = default_clausule;
1020 V_VT(&val) = VT_EMPTY;
1024 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1025 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1032 if(rt->type != RT_NORMAL)
1042 if(rt->type == RT_BREAK)
1043 rt->type = RT_NORMAL;
1049 /* ECMA-262 3rd Edition 12.13 */
1050 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1052 expression_statement_t *stat = (expression_statement_t*)_stat;
1059 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1063 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1064 exprval_release(&exprval);
1069 return DISP_E_EXCEPTION;
1072 /* ECMA-262 3rd Edition 12.14 */
1073 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1075 DispatchEx *var_disp;
1080 memset(&rt->ei, 0, sizeof(jsexcept_t));
1082 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
1083 if(SUCCEEDED(hres)) {
1084 hres = jsdisp_propput_name(var_disp, block->identifier, ctx->parser->script->lcid,
1085 &ex, &rt->ei, NULL/*FIXME*/);
1086 if(SUCCEEDED(hres)) {
1087 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
1088 if(SUCCEEDED(hres)) {
1089 hres = stat_eval(ctx, block->statement, rt, &val);
1090 scope_pop(&ctx->scope_chain);
1094 jsdisp_release(var_disp);
1105 /* ECMA-262 3rd Edition 12.14 */
1106 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1108 try_statement_t *stat = (try_statement_t*)_stat;
1114 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1116 TRACE("EXCEPTION\n");
1117 if(!stat->catch_block)
1120 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1125 if(stat->finally_statement) {
1127 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1136 static HRESULT return_bool(exprval_t *ret, DWORD b)
1138 ret->type = EXPRVAL_VARIANT;
1139 V_VT(&ret->u.var) = VT_BOOL;
1140 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1145 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1150 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1154 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
1155 exprval_release(&exprval);
1159 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1160 if(SUCCEEDED(hres)) {
1161 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
1162 exprval_release(&exprval);
1173 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1175 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1178 VARIANT lval, rval, retv;
1181 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1185 hres = oper(ctx, &lval, &rval, ei, &retv);
1186 VariantClear(&lval);
1187 VariantClear(&rval);
1191 ret->type = EXPRVAL_VARIANT;
1196 /* ECMA-262 3rd Edition 11.13.2 */
1197 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1198 jsexcept_t *ei, exprval_t *ret)
1200 VARIANT retv, lval, rval;
1201 exprval_t exprval, exprvalr;
1204 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1208 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
1209 if(SUCCEEDED(hres)) {
1210 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1211 if(SUCCEEDED(hres)) {
1212 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
1213 exprval_release(&exprvalr);
1215 if(SUCCEEDED(hres)) {
1216 hres = oper(ctx, &lval, &rval, ei, &retv);
1217 VariantClear(&rval);
1219 VariantClear(&lval);
1222 if(SUCCEEDED(hres)) {
1223 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
1225 VariantClear(&retv);
1227 exprval_release(&exprval);
1232 ret->type = EXPRVAL_VARIANT;
1237 /* ECMA-262 3rd Edition 13 */
1238 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1240 function_expression_t *expr = (function_expression_t*)_expr;
1247 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1248 expr->src_str, expr->src_len, &dispex);
1252 V_VT(&var) = VT_DISPATCH;
1253 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1255 if(expr->identifier) {
1256 hres = jsdisp_propput_name(ctx->var_disp, expr->identifier, ctx->parser->script->lcid, &var, ei, NULL/*FIXME*/);
1258 jsdisp_release(dispex);
1263 ret->type = EXPRVAL_VARIANT;
1268 /* ECMA-262 3rd Edition 11.12 */
1269 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1271 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1278 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1282 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1283 exprval_release(&exprval);
1287 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1290 /* ECMA-262 3rd Edition 11.2.1 */
1291 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1293 array_expression_t *expr = (array_expression_t*)_expr;
1295 VARIANT member, val;
1298 IDispatch *obj = NULL;
1303 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
1307 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1308 exprval_release(&exprval);
1312 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1313 if(SUCCEEDED(hres)) {
1314 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1315 exprval_release(&exprval);
1319 hres = to_object(ctx, &member, &obj);
1320 VariantClear(&member);
1321 if(SUCCEEDED(hres)) {
1322 hres = to_string(ctx->parser->script, &val, ei, &str);
1323 if(SUCCEEDED(hres)) {
1324 if(flags & EXPR_STRREF) {
1325 ret->type = EXPRVAL_NAMEREF;
1326 ret->u.nameref.disp = obj;
1327 ret->u.nameref.name = str;
1331 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1334 if(SUCCEEDED(hres)) {
1335 exprval_set_idref(ret, obj, id);
1336 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1341 IDispatch_Release(obj);
1347 /* ECMA-262 3rd Edition 11.2.1 */
1348 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1350 member_expression_t *expr = (member_expression_t*)_expr;
1351 IDispatch *obj = NULL;
1360 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1364 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1365 exprval_release(&exprval);
1369 hres = to_object(ctx, &member, &obj);
1370 VariantClear(&member);
1374 str = SysAllocString(expr->identifier);
1375 if(flags & EXPR_STRREF) {
1376 ret->type = EXPRVAL_NAMEREF;
1377 ret->u.nameref.disp = obj;
1378 ret->u.nameref.name = str;
1382 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1384 if(SUCCEEDED(hres)) {
1385 exprval_set_idref(ret, obj, id);
1386 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1391 IDispatch_Release(obj);
1395 static void free_dp(DISPPARAMS *dp)
1399 for(i=0; i < dp->cArgs; i++)
1400 VariantClear(dp->rgvarg+i);
1401 heap_free(dp->rgvarg);
1404 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1410 HRESULT hres = S_OK;
1412 memset(dp, 0, sizeof(*dp));
1416 for(iter = args; iter; iter = iter->next)
1419 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1421 return E_OUTOFMEMORY;
1423 for(i = cnt, iter = args; iter; iter = iter->next) {
1424 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1428 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1429 exprval_release(&exprval);
1444 /* ECMA-262 3rd Edition 11.2.2 */
1445 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1447 call_expression_t *expr = (call_expression_t*)_expr;
1449 VARIANT constr, var;
1455 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1459 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1461 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1462 exprval_release(&exprval);
1466 if(V_VT(&constr) != VT_DISPATCH) {
1467 FIXME("throw TypeError\n");
1468 VariantClear(&constr);
1472 hres = disp_call(V_DISPATCH(&constr), DISPID_VALUE, ctx->parser->script->lcid,
1473 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1474 IDispatch_Release(V_DISPATCH(&constr));
1478 ret->type = EXPRVAL_VARIANT;
1483 /* ECMA-262 3rd Edition 11.2.3 */
1484 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1486 call_expression_t *expr = (call_expression_t*)_expr;
1494 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1498 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1499 if(SUCCEEDED(hres)) {
1500 switch(exprval.type) {
1502 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
1503 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1504 if(flags & EXPR_NOVAL)
1505 V_VT(&var) = VT_EMPTY;
1508 FIXME("unimplemented type %d\n", V_VT(&func));
1515 exprval_release(&exprval);
1519 TRACE("= %s\n", debugstr_variant(&var));
1520 ret->type = EXPRVAL_VARIANT;
1525 /* ECMA-262 3rd Edition 11.1.1 */
1526 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1530 ret->type = EXPRVAL_VARIANT;
1531 V_VT(&ret->u.var) = VT_DISPATCH;
1532 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1533 IDispatch_AddRef(ctx->this_obj);
1537 /* ECMA-262 3rd Edition 10.1.4 */
1538 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1540 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1546 identifier = SysAllocString(expr->identifier);
1548 return E_OUTOFMEMORY;
1550 hres = identifier_eval(ctx, identifier, flags, ret);
1552 SysFreeString(identifier);
1556 /* ECMA-262 3rd Edition 7.8 */
1557 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1559 literal_expression_t *expr = (literal_expression_t*)_expr;
1565 hres = literal_to_var(expr->literal, &var);
1569 ret->type = EXPRVAL_VARIANT;
1574 /* ECMA-262 3rd Edition 11.1.4 */
1575 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1577 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1578 DWORD length = 0, i = 0;
1579 array_element_t *elem;
1587 for(elem = expr->element_list; elem; elem = elem->next)
1588 length += elem->elision+1;
1589 length += expr->length;
1591 hres = create_array(ctx->parser->script, length, &array);
1595 for(elem = expr->element_list; elem; elem = elem->next) {
1598 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1602 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1603 exprval_release(&exprval);
1607 hres = jsdisp_propput_idx(array, i, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1616 jsdisp_release(array);
1620 ret->type = EXPRVAL_VARIANT;
1621 V_VT(&ret->u.var) = VT_DISPATCH;
1622 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(array);
1626 /* ECMA-262 3rd Edition 11.1.5 */
1627 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1629 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1639 hres = create_object(ctx->parser->script, NULL, &obj);
1643 for(iter = expr->property_list; iter; iter = iter->next) {
1644 hres = literal_to_var(iter->name, &tmp);
1648 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1653 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1654 if(SUCCEEDED(hres)) {
1655 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1656 exprval_release(&exprval);
1657 if(SUCCEEDED(hres)) {
1658 hres = jsdisp_propput_name(obj, name, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1663 SysFreeString(name);
1669 jsdisp_release(obj);
1673 ret->type = EXPRVAL_VARIANT;
1674 V_VT(&ret->u.var) = VT_DISPATCH;
1675 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1679 /* ECMA-262 3rd Edition 11.14 */
1680 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1682 binary_expression_t *expr = (binary_expression_t*)_expr;
1688 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1692 VariantClear(&lval);
1694 ret->type = EXPRVAL_VARIANT;
1699 /* ECMA-262 3rd Edition 11.11 */
1700 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1702 binary_expression_t *expr = (binary_expression_t*)_expr;
1710 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1714 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1715 exprval_release(&exprval);
1719 hres = to_boolean(&val, &b);
1720 if(SUCCEEDED(hres) && b) {
1721 ret->type = EXPRVAL_VARIANT;
1730 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1734 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1735 exprval_release(&exprval);
1739 ret->type = EXPRVAL_VARIANT;
1744 /* ECMA-262 3rd Edition 11.11 */
1745 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1747 binary_expression_t *expr = (binary_expression_t*)_expr;
1755 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1759 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1760 exprval_release(&exprval);
1764 hres = to_boolean(&val, &b);
1765 if(SUCCEEDED(hres) && !b) {
1766 ret->type = EXPRVAL_VARIANT;
1775 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1779 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1780 exprval_release(&exprval);
1784 ret->type = EXPRVAL_VARIANT;
1789 /* ECMA-262 3rd Edition 11.10 */
1790 static HRESULT bitor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1795 hres = to_int32(ctx->parser->script, lval, ei, &li);
1799 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1808 /* ECMA-262 3rd Edition 11.10 */
1809 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1811 binary_expression_t *expr = (binary_expression_t*)_expr;
1815 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
1818 /* ECMA-262 3rd Edition 11.10 */
1819 static HRESULT xor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1824 hres = to_int32(ctx->parser->script, lval, ei, &li);
1828 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1837 /* ECMA-262 3rd Edition 11.10 */
1838 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1840 binary_expression_t *expr = (binary_expression_t*)_expr;
1844 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
1847 /* ECMA-262 3rd Edition 11.10 */
1848 static HRESULT bitand_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1853 hres = to_int32(ctx->parser->script, lval, ei, &li);
1857 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1866 /* ECMA-262 3rd Edition 11.10 */
1867 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1869 binary_expression_t *expr = (binary_expression_t*)_expr;
1873 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
1876 /* ECMA-262 3rd Edition 11.8.6 */
1877 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1883 /* ECMA-262 3rd Edition 11.8.7 */
1884 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1890 /* ECMA-262 3rd Edition 11.6.1 */
1891 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1896 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1900 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1906 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1907 BSTR lstr = NULL, rstr = NULL;
1909 if(V_VT(&l) == VT_BSTR)
1912 hres = to_string(ctx->parser->script, &l, ei, &lstr);
1914 if(SUCCEEDED(hres)) {
1915 if(V_VT(&r) == VT_BSTR)
1918 hres = to_string(ctx->parser->script, &r, ei, &rstr);
1921 if(SUCCEEDED(hres)) {
1924 len1 = SysStringLen(lstr);
1925 len2 = SysStringLen(rstr);
1927 V_VT(retv) = VT_BSTR;
1928 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1929 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1930 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1933 if(lstr && V_VT(&l) != VT_BSTR)
1934 SysFreeString(lstr);
1935 if(rstr && V_VT(&r) != VT_BSTR)
1936 SysFreeString(rstr);
1940 hres = to_number(ctx->parser->script, &l, ei, &nl);
1941 if(SUCCEEDED(hres)) {
1942 hres = to_number(ctx->parser->script, &r, ei, &nr);
1944 num_set_val(retv, num_val(&nl) + num_val(&nr));
1953 /* ECMA-262 3rd Edition 11.6.1 */
1954 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1956 binary_expression_t *expr = (binary_expression_t*)_expr;
1960 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
1963 /* ECMA-262 3rd Edition 11.6.2 */
1964 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1969 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1973 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1977 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
1981 /* ECMA-262 3rd Edition 11.6.2 */
1982 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1984 binary_expression_t *expr = (binary_expression_t*)_expr;
1988 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
1991 /* ECMA-262 3rd Edition 11.5.1 */
1992 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1997 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2001 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2005 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2009 /* ECMA-262 3rd Edition 11.5.1 */
2010 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2012 binary_expression_t *expr = (binary_expression_t*)_expr;
2016 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2019 /* ECMA-262 3rd Edition 11.5.2 */
2020 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2025 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2029 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2033 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2037 /* ECMA-262 3rd Edition 11.5.2 */
2038 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2040 binary_expression_t *expr = (binary_expression_t*)_expr;
2044 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2047 /* ECMA-262 3rd Edition 11.5.3 */
2048 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2054 /* ECMA-262 3rd Edition 11.4.2 */
2055 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2057 unary_expression_t *expr = (unary_expression_t*)_expr;
2058 VARIANT_BOOL b = VARIANT_FALSE;
2064 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2068 switch(exprval.type) {
2069 case EXPRVAL_NAMEREF: {
2070 IDispatchEx *dispex;
2072 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2073 if(SUCCEEDED(hres)) {
2074 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name, fdexNameCaseSensitive);
2076 IDispatchEx_Release(dispex);
2081 FIXME("unsupported type %d\n", exprval.type);
2085 exprval_release(&exprval);
2089 return return_bool(ret, b);
2092 /* ECMA-262 3rd Edition 11.4.2 */
2093 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2095 unary_expression_t *expr = (unary_expression_t*)_expr;
2102 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2106 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &tmp);
2107 exprval_release(&exprval);
2113 ret->type = EXPRVAL_VARIANT;
2114 V_VT(&ret->u.var) = VT_EMPTY;
2118 /* ECMA-262 3rd Edition 11.4.3 */
2119 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2121 unary_expression_t *expr = (unary_expression_t*)_expr;
2127 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2128 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2129 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2130 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2131 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2132 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2136 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2140 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2141 exprval_release(&exprval);
2145 switch(V_VT(&val)) {
2165 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
2167 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
2168 IDispatchEx_Release(_IDispatchEx_(dispex));
2175 FIXME("unhandled vt %d\n", V_VT(&val));
2183 ret->type = EXPRVAL_VARIANT;
2184 V_VT(&ret->u.var) = VT_BSTR;
2185 V_BSTR(&ret->u.var) = SysAllocString(str);
2189 /* ECMA-262 3rd Edition 11.4.7 */
2190 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2192 unary_expression_t *expr = (unary_expression_t*)_expr;
2199 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2203 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2204 exprval_release(&exprval);
2208 hres = to_number(ctx->parser->script, &val, ei, &num);
2213 ret->type = EXPRVAL_VARIANT;
2214 num_set_val(&ret->u.var, -num_val(&num));
2218 /* ECMA-262 3rd Edition 11.4.6 */
2219 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2221 unary_expression_t *expr = (unary_expression_t*)_expr;
2228 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2232 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2233 exprval_release(&exprval);
2237 hres = to_number(ctx->parser->script, &val, ei, &num);
2241 ret->type = EXPRVAL_VARIANT;
2246 /* ECMA-262 3rd Edition 11.3.1 */
2247 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2249 unary_expression_t *expr = (unary_expression_t*)_expr;
2256 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2260 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2261 if(SUCCEEDED(hres)) {
2262 hres = to_number(ctx->parser->script, &val, ei, &num);
2266 if(SUCCEEDED(hres)) {
2268 num_set_val(&inc, num_val(&num)+1.0);
2269 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
2272 exprval_release(&exprval);
2276 ret->type = EXPRVAL_VARIANT;
2281 /* ECMA-262 3rd Edition 11.3.2 */
2282 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2284 unary_expression_t *expr = (unary_expression_t*)_expr;
2291 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2295 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2296 if(SUCCEEDED(hres)) {
2297 hres = to_number(ctx->parser->script, &val, ei, &num);
2301 if(SUCCEEDED(hres)) {
2303 num_set_val(&dec, num_val(&num)-1.0);
2304 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
2307 exprval_release(&exprval);
2311 ret->type = EXPRVAL_VARIANT;
2316 /* ECMA-262 3rd Edition 11.4.4 */
2317 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2319 unary_expression_t *expr = (unary_expression_t*)_expr;
2326 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2330 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2331 if(SUCCEEDED(hres)) {
2332 hres = to_number(ctx->parser->script, &val, ei, &num);
2336 if(SUCCEEDED(hres)) {
2337 num_set_val(&val, num_val(&num)+1.0);
2338 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2341 exprval_release(&exprval);
2345 ret->type = EXPRVAL_VARIANT;
2350 /* ECMA-262 3rd Edition 11.4.5 */
2351 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2353 unary_expression_t *expr = (unary_expression_t*)_expr;
2360 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2364 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2365 if(SUCCEEDED(hres)) {
2366 hres = to_number(ctx->parser->script, &val, ei, &num);
2370 if(SUCCEEDED(hres)) {
2371 num_set_val(&val, num_val(&num)-1.0);
2372 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2375 exprval_release(&exprval);
2379 ret->type = EXPRVAL_VARIANT;
2384 /* ECMA-262 3rd Edition 11.9.3 */
2385 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2387 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2388 return equal2_values(lval, rval, ret);
2390 /* FIXME: NULL disps should be handled in more general way */
2391 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2394 return equal_values(ctx, &v, rval, ei, ret);
2397 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2400 return equal_values(ctx, lval, &v, ei, ret);
2403 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2404 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2409 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2413 hres = to_number(ctx->parser->script, lval, ei, &v);
2417 return equal_values(ctx, &v, rval, ei, ret);
2420 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2424 hres = to_number(ctx->parser->script, rval, ei, &v);
2428 return equal_values(ctx, lval, &v, ei, ret);
2431 if(V_VT(rval) == VT_BOOL) {
2435 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2436 return equal_values(ctx, lval, &v, ei, ret);
2439 if(V_VT(lval) == VT_BOOL) {
2443 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2444 return equal_values(ctx, &v, rval, ei, ret);
2448 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2452 hres = to_primitive(ctx->parser->script, rval, ei, &v);
2456 hres = equal_values(ctx, lval, &v, ei, ret);
2463 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2467 hres = to_primitive(ctx->parser->script, lval, ei, &v);
2471 hres = equal_values(ctx, &v, rval, ei, ret);
2482 /* ECMA-262 3rd Edition 11.9.1 */
2483 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2485 binary_expression_t *expr = (binary_expression_t*)_expr;
2492 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2496 hres = equal_values(ctx, &rval, &lval, ei, &b);
2500 return return_bool(ret, b);
2503 /* ECMA-262 3rd Edition 11.9.4 */
2504 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2506 binary_expression_t *expr = (binary_expression_t*)_expr;
2513 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2517 hres = equal2_values(&rval, &lval, &b);
2521 return return_bool(ret, b);
2524 /* ECMA-262 3rd Edition 11.9.2 */
2525 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2527 binary_expression_t *expr = (binary_expression_t*)_expr;
2534 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2538 hres = equal_values(ctx, &lval, &rval, ei, &b);
2542 return return_bool(ret, !b);
2545 /* ECMA-262 3rd Edition 11.9.5 */
2546 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2548 binary_expression_t *expr = (binary_expression_t*)_expr;
2555 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2559 hres = equal2_values(&lval, &rval, &b);
2563 return return_bool(ret, !b);
2566 /* ECMA-262 3rd Edition 11.8.5 */
2567 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2569 VARIANT l, r, ln, rn;
2572 hres = to_primitive(ctx->parser->script, lval, ei, &l);
2576 hres = to_primitive(ctx->parser->script, rval, ei, &r);
2582 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2583 *ret = strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0;
2584 SysFreeString(V_BSTR(&l));
2585 SysFreeString(V_BSTR(&r));
2589 hres = to_number(ctx->parser->script, &l, ei, &ln);
2592 hres = to_number(ctx->parser->script, &r, ei, &rn);
2597 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4)
2598 *ret = V_I4(&ln) < V_I4(&rn);
2600 *ret = num_val(&ln) < num_val(&rn);
2605 /* ECMA-262 3rd Edition 11.8.1 */
2606 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2608 binary_expression_t *expr = (binary_expression_t*)_expr;
2615 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2619 hres = less_eval(ctx, &lval, &rval, ei, &b);
2620 VariantClear(&lval);
2621 VariantClear(&rval);
2625 return return_bool(ret, b);
2628 /* ECMA-262 3rd Edition 11.8.3 */
2629 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2631 binary_expression_t *expr = (binary_expression_t*)_expr;
2638 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2642 hres = less_eval(ctx, &rval, &lval, ei, &b);
2643 VariantClear(&lval);
2644 VariantClear(&rval);
2648 return return_bool(ret, !b);
2651 /* ECMA-262 3rd Edition 11.8.2 */
2652 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2654 binary_expression_t *expr = (binary_expression_t*)_expr;
2661 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2665 hres = less_eval(ctx, &rval, &lval, ei, &b);
2666 VariantClear(&lval);
2667 VariantClear(&rval);
2671 return return_bool(ret, b);
2674 /* ECMA-262 3rd Edition 11.8.4 */
2675 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2677 binary_expression_t *expr = (binary_expression_t*)_expr;
2684 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2688 hres = less_eval(ctx, &lval, &rval, ei, &b);
2689 VariantClear(&lval);
2690 VariantClear(&rval);
2694 return return_bool(ret, !b);
2697 /* ECMA-262 3rd Edition 11.4.8 */
2698 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2700 unary_expression_t *expr = (unary_expression_t*)_expr;
2708 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2712 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2713 exprval_release(&exprval);
2717 hres = to_int32(ctx->parser->script, &val, ei, &i);
2721 ret->type = EXPRVAL_VARIANT;
2722 V_VT(&ret->u.var) = VT_I4;
2723 V_I4(&ret->u.var) = ~i;
2727 /* ECMA-262 3rd Edition 11.4.9 */
2728 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2730 unary_expression_t *expr = (unary_expression_t*)_expr;
2737 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2741 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
2742 exprval_release(&exprval);
2746 return return_bool(ret, !b);
2749 /* ECMA-262 3rd Edition 11.7.1 */
2750 static HRESULT lshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2756 hres = to_int32(ctx->parser->script, lval, ei, &li);
2760 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2765 V_I4(retv) = li << (ri&0x1f);
2769 /* ECMA-262 3rd Edition 11.7.1 */
2770 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2772 binary_expression_t *expr = (binary_expression_t*)_expr;
2776 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
2779 /* ECMA-262 3rd Edition 11.7.2 */
2780 static HRESULT rshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2786 hres = to_int32(ctx->parser->script, lval, ei, &li);
2790 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2795 V_I4(retv) = li >> (ri&0x1f);
2799 /* ECMA-262 3rd Edition 11.7.2 */
2800 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2802 binary_expression_t *expr = (binary_expression_t*)_expr;
2806 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
2809 /* ECMA-262 3rd Edition 11.7.3 */
2810 static HRESULT rshift2_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2815 hres = to_uint32(ctx->parser->script, lval, ei, &li);
2819 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2824 V_I4(retv) = li >> (ri&0x1f);
2828 /* ECMA-262 3rd Edition 11.7.3 */
2829 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2831 binary_expression_t *expr = (binary_expression_t*)_expr;
2835 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
2838 /* ECMA-262 3rd Edition 11.13.1 */
2839 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2841 binary_expression_t *expr = (binary_expression_t*)_expr;
2842 exprval_t exprval, exprvalr;
2848 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
2852 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
2853 if(SUCCEEDED(hres)) {
2854 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
2855 exprval_release(&exprvalr);
2859 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
2861 exprval_release(&exprval);
2863 VariantClear(&rval);
2867 ret->type = EXPRVAL_VARIANT;
2872 /* ECMA-262 3rd Edition 11.13.2 */
2873 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2875 binary_expression_t *expr = (binary_expression_t*)_expr;
2879 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
2882 /* ECMA-262 3rd Edition 11.13.2 */
2883 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2885 binary_expression_t *expr = (binary_expression_t*)_expr;
2889 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
2892 /* ECMA-262 3rd Edition 11.13.2 */
2893 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2895 binary_expression_t *expr = (binary_expression_t*)_expr;
2899 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
2902 /* ECMA-262 3rd Edition 11.13.2 */
2903 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2905 binary_expression_t *expr = (binary_expression_t*)_expr;
2909 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
2912 /* ECMA-262 3rd Edition 11.13.2 */
2913 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2915 binary_expression_t *expr = (binary_expression_t*)_expr;
2919 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
2922 /* ECMA-262 3rd Edition 11.13.2 */
2923 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2925 binary_expression_t *expr = (binary_expression_t*)_expr;
2929 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
2932 /* ECMA-262 3rd Edition 11.13.2 */
2933 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2935 binary_expression_t *expr = (binary_expression_t*)_expr;
2939 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
2942 /* ECMA-262 3rd Edition 11.13.2 */
2943 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2949 /* ECMA-262 3rd Edition 11.13.2 */
2950 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2952 binary_expression_t *expr = (binary_expression_t*)_expr;
2956 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
2959 /* ECMA-262 3rd Edition 11.13.2 */
2960 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2962 binary_expression_t *expr = (binary_expression_t*)_expr;
2966 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
2969 /* ECMA-262 3rd Edition 11.13.2 */
2970 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2972 binary_expression_t *expr = (binary_expression_t*)_expr;
2976 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);