2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26 #define EXPR_NOVAL 0x0001
27 #define EXPR_NEWREF 0x0002
28 #define EXPR_STRREF 0x0004
30 static inline HRESULT stat_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
32 return stat->eval(ctx, stat, rt, ret);
35 static inline HRESULT expr_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
37 return _expr->eval(ctx, _expr, flags, ei, ret);
40 static void exprval_release(exprval_t *val)
44 if(V_VT(&val->u.var) != VT_EMPTY)
45 VariantClear(&val->u.var);
49 IDispatch_Release(val->u.idref.disp);
52 if(val->u.nameref.disp)
53 IDispatch_Release(val->u.nameref.disp);
54 SysFreeString(val->u.nameref.name);
58 /* ECMA-262 3rd Edition 8.7.1 */
59 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
65 return VariantCopy(ret, &val->u.var);
67 if(!val->u.idref.disp) {
68 FIXME("throw ReferenceError\n");
72 return disp_propget(val->u.idref.disp, val->u.idref.id, ctx->lcid, ret, ei, NULL/*FIXME*/);
74 ERR("type %d\n", val->type);
79 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
81 if(val->type == EXPRVAL_VARIANT) {
83 V_VT(&val->u.var) = VT_EMPTY;
87 return exprval_value(ctx, val, ei, ret);
90 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
92 if(exprval->type != EXPRVAL_VARIANT) {
96 hres = exprval_to_value(ctx, exprval, ei, &val);
100 hres = to_boolean(&val, b);
105 return to_boolean(&exprval->u.var, b);
108 static void exprval_init(exprval_t *val)
110 val->type = EXPRVAL_VARIANT;
111 V_VT(&val->u.var) = VT_EMPTY;
114 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
116 val->type = EXPRVAL_IDREF;
117 val->u.idref.disp = disp;
118 val->u.idref.id = id;
121 IDispatch_AddRef(disp);
124 HRESULT scope_push(scope_chain_t *scope, DispatchEx *obj, scope_chain_t **ret)
126 scope_chain_t *new_scope;
128 new_scope = heap_alloc(sizeof(scope_chain_t));
130 return E_OUTOFMEMORY;
134 IDispatchEx_AddRef(_IDispatchEx_(obj));
135 new_scope->obj = obj;
139 new_scope->next = scope;
141 new_scope->next = NULL;
148 static void scope_pop(scope_chain_t **scope)
157 void scope_release(scope_chain_t *scope)
163 scope_release(scope->next);
165 IDispatchEx_Release(_IDispatchEx_(scope->obj));
169 HRESULT create_exec_ctx(IDispatch *this_obj, DispatchEx *var_disp, scope_chain_t *scope, exec_ctx_t **ret)
173 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
175 return E_OUTOFMEMORY;
177 IDispatch_AddRef(this_obj);
178 ctx->this_obj = this_obj;
180 IDispatchEx_AddRef(_IDispatchEx_(var_disp));
181 ctx->var_disp = var_disp;
185 ctx->scope_chain = scope;
192 void exec_release(exec_ctx_t *ctx)
198 scope_release(ctx->scope_chain);
200 IDispatchEx_Release(_IDispatchEx_(ctx->var_disp));
202 IDispatch_Release(ctx->this_obj);
206 static HRESULT disp_get_id(IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
211 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
213 TRACE("unsing IDispatch\n");
216 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
220 hres = IDispatchEx_GetDispID(dispex, name, flags|fdexNameCaseSensitive, id);
221 IDispatchEx_Release(dispex);
225 /* ECMA-262 3rd Edition 8.7.2 */
226 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
228 if(ref->type != EXPRVAL_IDREF) {
229 FIXME("throw ReferemceError\n");
233 return disp_propput(ref->u.idref.disp, ref->u.idref.id, ctx->lcid, v, ei, NULL/*FIXME*/);
236 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
238 IObjectIdentity *identity;
239 IUnknown *unk1, *unk2;
247 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
251 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
253 IUnknown_Release(unk1);
260 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
261 if(SUCCEEDED(hres)) {
262 hres = IObjectIdentity_IsEqualObject(identity, unk2);
263 IObjectIdentity_Release(identity);
270 IUnknown_Release(unk1);
271 IUnknown_Release(unk2);
275 /* ECMA-262 3rd Edition 11.9.6 */
276 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
280 if(V_VT(lval) != V_VT(rval)) {
281 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
282 *ret = num_val(lval) == num_val(rval);
296 *ret = V_I4(lval) == V_I4(rval);
299 *ret = V_R8(lval) == V_R8(rval);
302 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
305 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
307 *ret = !V_BOOL(lval) == !V_BOOL(rval);
310 FIXME("unimplemented vt %d\n", V_VT(lval));
317 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
319 V_VT(v) = literal->vt;
326 V_I4(v) = literal->u.lval;
329 V_R8(v) = literal->u.dval;
332 V_BSTR(v) = SysAllocString(literal->u.wstr);
335 V_BOOL(v) = literal->u.bval;
338 IDispatch_AddRef(literal->u.disp);
339 V_DISPATCH(v) = literal->u.disp;
342 ERR("wrong type %d\n", V_VT(v));
349 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
351 script_ctx_t *script = parser->script;
352 function_declaration_t *func;
353 parser_ctx_t *prev_parser;
356 exec_ctx_t *prev_ctx;
360 for(func = source->functions; func; func = func->next) {
361 DispatchEx *func_obj;
364 hres = create_source_function(parser, func->parameter_list, func->source_elements,
365 ctx->scope_chain, func->src_str, func->src_len, &func_obj);
369 V_VT(&var) = VT_DISPATCH;
370 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
371 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
372 IDispatchEx_Release(_IDispatchEx_(func_obj));
377 prev_ctx = script->exec_ctx;
378 script->exec_ctx = ctx;
380 prev_parser = ctx->parser;
381 ctx->parser = parser;
383 V_VT(&val) = VT_EMPTY;
384 memset(&rt, 0, sizeof(rt));
387 for(stat = source->statement; stat; stat = stat->next) {
388 hres = stat_eval(ctx, stat, &rt, &tmp);
394 if(rt.type != RT_NORMAL)
398 script->exec_ctx = prev_ctx;
399 ctx->parser = prev_parser;
401 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
402 FIXME("wrong rt %d\n", rt.type);
419 /* ECMA-262 3rd Edition 10.1.4 */
420 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
422 scope_chain_t *scope;
427 TRACE("%s\n", debugstr_w(identifier));
429 for(scope = ctx->scope_chain; scope; scope = scope->next) {
430 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
436 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
440 hres = jsdisp_get_id(ctx->parser->script->global, identifier, 0, &id);
441 if(SUCCEEDED(hres)) {
442 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
446 for(item = ctx->parser->script->named_items; item; item = item->next) {
447 hres = disp_get_id(item->disp, identifier, 0, &id);
453 exprval_set_idref(ret, (IDispatch*)item->disp, id);
457 hres = jsdisp_get_id(ctx->parser->script->script_disp, identifier, 0, &id);
458 if(SUCCEEDED(hres)) {
459 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
463 if(flags & EXPR_NEWREF) {
464 hres = jsdisp_get_id(ctx->var_disp, identifier, fdexNameEnsure, &id);
468 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
472 WARN("Could not find identifier %s\n", debugstr_w(identifier));
476 /* ECMA-262 3rd Edition 12.1 */
477 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
479 block_statement_t *stat = (block_statement_t*)_stat;
486 V_VT(&val) = VT_EMPTY;
487 for(iter = stat->stat_list; iter; iter = iter->next) {
488 hres = stat_eval(ctx, iter, rt, &tmp);
494 if(rt->type != RT_NORMAL)
507 /* ECMA-262 3rd Edition 12.2 */
508 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
510 variable_declaration_t *iter;
511 HRESULT hres = E_FAIL;
513 for(iter = var_list; iter; iter = iter->next) {
519 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
523 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
524 exprval_release(&exprval);
528 V_VT(&val) = VT_EMPTY;
531 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
540 /* ECMA-262 3rd Edition 12.2 */
541 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
543 var_statement_t *stat = (var_statement_t*)_stat;
548 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
552 V_VT(ret) = VT_EMPTY;
556 /* ECMA-262 3rd Edition 12.3 */
557 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
561 V_VT(ret) = VT_EMPTY;
565 /* ECMA-262 3rd Edition 12.4 */
566 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
568 expression_statement_t *stat = (expression_statement_t*)_stat;
575 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
579 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
580 exprval_release(&exprval);
585 TRACE("= %s\n", debugstr_variant(ret));
589 /* ECMA-262 3rd Edition 12.5 */
590 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
592 if_statement_t *stat = (if_statement_t*)_stat;
599 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
603 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
604 exprval_release(&exprval);
609 hres = stat_eval(ctx, stat->if_stat, rt, ret);
610 else if(stat->else_stat)
611 hres = stat_eval(ctx, stat->else_stat, rt, ret);
613 V_VT(ret) = VT_EMPTY;
618 /* ECMA-262 3rd Edition 12.6.2 */
619 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
621 while_statement_t *stat = (while_statement_t*)_stat;
630 V_VT(&val) = VT_EMPTY;
631 test_expr = !stat->do_while;
635 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
639 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
640 exprval_release(&exprval);
641 if(FAILED(hres) || !b)
647 hres = stat_eval(ctx, stat->statement, rt, &tmp);
654 if(rt->type == RT_CONTINUE)
655 rt->type = RT_NORMAL;
656 if(rt->type != RT_NORMAL)
665 if(rt->type == RT_BREAK)
666 rt->type = RT_NORMAL;
672 /* ECMA-262 3rd Edition 12.6.3 */
673 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
675 for_statement_t *stat = (for_statement_t*)_stat;
676 VARIANT val, tmp, retv;
683 if(stat->variable_list) {
684 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
687 }else if(stat->begin_expr) {
688 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
692 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
693 exprval_release(&exprval);
700 V_VT(&retv) = VT_EMPTY;
704 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
708 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
709 exprval_release(&exprval);
710 if(FAILED(hres) || !b)
714 hres = stat_eval(ctx, stat->statement, rt, &tmp);
721 if(rt->type == RT_CONTINUE)
722 rt->type = RT_NORMAL;
723 else if(rt->type != RT_NORMAL)
727 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
731 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
732 exprval_release(&exprval);
745 if(rt->type == RT_BREAK)
746 rt->type = RT_NORMAL;
752 /* ECMA-262 3rd Edition 12.6.4 */
753 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
755 forin_statement_t *stat = (forin_statement_t*)_stat;
756 VARIANT val, name, retv, tmp;
757 DISPID id = DISPID_STARTENUM;
758 BSTR str, identifier = NULL;
766 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
771 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
775 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
776 exprval_release(&exprval);
780 if(V_VT(&val) != VT_DISPATCH) {
781 TRACE("in vt %d\n", V_VT(&val));
783 V_VT(ret) = VT_EMPTY;
787 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
788 IDispatch_Release(V_DISPATCH(&val));
790 FIXME("Object doesn't support IDispatchEx\n");
794 V_VT(&retv) = VT_EMPTY;
797 identifier = SysAllocString(stat->variable->identifier);
800 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
801 if(FAILED(hres) || hres == S_FALSE)
804 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
808 TRACE("iter %s\n", debugstr_w(str));
811 hres = identifier_eval(ctx, identifier, 0, &exprval);
813 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
814 if(SUCCEEDED(hres)) {
815 V_VT(&name) = VT_BSTR;
817 hres = put_value(ctx->parser->script, &exprval, &name, &rt->ei);
818 exprval_release(&exprval);
824 hres = stat_eval(ctx, stat->statement, rt, &tmp);
831 if(rt->type == RT_CONTINUE)
832 rt->type = RT_NORMAL;
833 else if(rt->type != RT_NORMAL)
837 SysFreeString(identifier);
838 IDispatchEx_Release(in_obj);
844 if(rt->type == RT_BREAK)
845 rt->type = RT_NORMAL;
851 /* ECMA-262 3rd Edition 12.7 */
852 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
854 branch_statement_t *stat = (branch_statement_t*)_stat;
858 if(stat->identifier) {
859 FIXME("indentifier not implemented\n");
863 rt->type = RT_CONTINUE;
864 V_VT(ret) = VT_EMPTY;
868 /* ECMA-262 3rd Edition 12.8 */
869 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
871 branch_statement_t *stat = (branch_statement_t*)_stat;
875 if(stat->identifier) {
876 FIXME("indentifier not implemented\n");
881 V_VT(ret) = VT_EMPTY;
885 /* ECMA-262 3rd Edition 12.9 */
886 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
888 expression_statement_t *stat = (expression_statement_t*)_stat;
896 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
900 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
901 exprval_release(&exprval);
905 V_VT(ret) = VT_EMPTY;
908 TRACE("= %s\n", debugstr_variant(ret));
909 rt->type = RT_RETURN;
913 /* ECMA-262 3rd Edition 12.10 */
914 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
916 with_statement_t *stat = (with_statement_t*)_stat;
925 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
929 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
930 exprval_release(&exprval);
934 hres = to_object(ctx, &val, &disp);
939 obj = iface_to_jsdisp((IUnknown*)disp);
940 IDispatch_Release(disp);
942 FIXME("disp id not jsdisp\n");
946 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
951 hres = stat_eval(ctx, stat->statement, rt, ret);
953 scope_pop(&ctx->scope_chain);
957 /* ECMA-262 3rd Edition 12.12 */
958 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
964 /* ECMA-262 3rd Edition 12.13 */
965 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
967 switch_statement_t *stat = (switch_statement_t*)_stat;
968 case_clausule_t *iter, *default_clausule = NULL;
969 statement_t *stat_iter;
977 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
981 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
982 exprval_release(&exprval);
986 for(iter = stat->case_list; iter; iter = iter->next) {
988 default_clausule = iter;
992 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
996 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
997 exprval_release(&exprval);
1001 hres = equal2_values(&val, &cval, &b);
1002 VariantClear(&cval);
1003 if(FAILED(hres) || b)
1012 iter = default_clausule;
1014 V_VT(&val) = VT_EMPTY;
1018 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1019 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1026 if(rt->type != RT_NORMAL)
1036 if(rt->type == RT_BREAK)
1037 rt->type = RT_NORMAL;
1043 /* ECMA-262 3rd Edition 12.13 */
1044 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1046 expression_statement_t *stat = (expression_statement_t*)_stat;
1053 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1057 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1058 exprval_release(&exprval);
1063 return DISP_E_EXCEPTION;
1066 /* ECMA-262 3rd Edition 12.14 */
1067 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1069 DispatchEx *var_disp;
1074 memset(&rt->ei, 0, sizeof(jsexcept_t));
1076 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
1077 if(SUCCEEDED(hres)) {
1078 hres = jsdisp_propput_name(var_disp, block->identifier, ctx->parser->script->lcid,
1079 &ex, &rt->ei, NULL/*FIXME*/);
1080 if(SUCCEEDED(hres)) {
1081 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
1082 if(SUCCEEDED(hres)) {
1083 hres = stat_eval(ctx, block->statement, rt, &val);
1084 scope_pop(&ctx->scope_chain);
1088 jsdisp_release(var_disp);
1099 /* ECMA-262 3rd Edition 12.14 */
1100 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1102 try_statement_t *stat = (try_statement_t*)_stat;
1108 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1110 TRACE("EXCEPTION\n");
1111 if(!stat->catch_block)
1114 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1119 if(stat->finally_statement) {
1121 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1130 static HRESULT return_bool(exprval_t *ret, DWORD b)
1132 ret->type = EXPRVAL_VARIANT;
1133 V_VT(&ret->u.var) = VT_BOOL;
1134 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1139 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1144 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1148 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
1149 exprval_release(&exprval);
1153 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1154 if(SUCCEEDED(hres)) {
1155 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
1156 exprval_release(&exprval);
1167 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1169 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1172 VARIANT lval, rval, retv;
1175 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1179 hres = oper(ctx, &lval, &rval, ei, &retv);
1180 VariantClear(&lval);
1181 VariantClear(&rval);
1185 ret->type = EXPRVAL_VARIANT;
1190 /* ECMA-262 3rd Edition 11.13.2 */
1191 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1192 jsexcept_t *ei, exprval_t *ret)
1194 VARIANT retv, lval, rval;
1195 exprval_t exprval, exprvalr;
1198 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1202 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
1203 if(SUCCEEDED(hres)) {
1204 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1205 if(SUCCEEDED(hres)) {
1206 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
1207 exprval_release(&exprvalr);
1209 if(SUCCEEDED(hres)) {
1210 hres = oper(ctx, &lval, &rval, ei, &retv);
1211 VariantClear(&rval);
1213 VariantClear(&lval);
1216 if(SUCCEEDED(hres)) {
1217 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
1219 VariantClear(&retv);
1221 exprval_release(&exprval);
1226 ret->type = EXPRVAL_VARIANT;
1231 /* ECMA-262 3rd Edition 13 */
1232 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1234 function_expression_t *expr = (function_expression_t*)_expr;
1241 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1242 expr->src_str, expr->src_len, &dispex);
1246 V_VT(&var) = VT_DISPATCH;
1247 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1249 if(expr->identifier) {
1250 hres = jsdisp_propput_name(ctx->var_disp, expr->identifier, ctx->parser->script->lcid, &var, ei, NULL/*FIXME*/);
1252 jsdisp_release(dispex);
1257 ret->type = EXPRVAL_VARIANT;
1262 /* ECMA-262 3rd Edition 11.12 */
1263 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1265 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1272 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1276 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1277 exprval_release(&exprval);
1281 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1284 /* ECMA-262 3rd Edition 11.2.1 */
1285 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1287 array_expression_t *expr = (array_expression_t*)_expr;
1289 VARIANT member, val;
1292 IDispatch *obj = NULL;
1297 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
1301 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1302 exprval_release(&exprval);
1306 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1307 if(SUCCEEDED(hres)) {
1308 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1309 exprval_release(&exprval);
1313 hres = to_object(ctx, &member, &obj);
1314 VariantClear(&member);
1315 if(SUCCEEDED(hres)) {
1316 hres = to_string(ctx->parser->script, &val, ei, &str);
1317 if(SUCCEEDED(hres)) {
1318 if(flags & EXPR_STRREF) {
1319 ret->type = EXPRVAL_NAMEREF;
1320 ret->u.nameref.disp = obj;
1321 ret->u.nameref.name = str;
1325 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1328 if(SUCCEEDED(hres)) {
1329 exprval_set_idref(ret, obj, id);
1330 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1335 IDispatch_Release(obj);
1341 /* ECMA-262 3rd Edition 11.2.1 */
1342 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1344 member_expression_t *expr = (member_expression_t*)_expr;
1345 IDispatch *obj = NULL;
1354 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1358 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1359 exprval_release(&exprval);
1363 hres = to_object(ctx, &member, &obj);
1364 VariantClear(&member);
1368 str = SysAllocString(expr->identifier);
1369 if(flags & EXPR_STRREF) {
1370 ret->type = EXPRVAL_NAMEREF;
1371 ret->u.nameref.disp = obj;
1372 ret->u.nameref.name = str;
1376 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1378 if(SUCCEEDED(hres)) {
1379 exprval_set_idref(ret, obj, id);
1380 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1385 IDispatch_Release(obj);
1389 static void free_dp(DISPPARAMS *dp)
1393 for(i=0; i < dp->cArgs; i++)
1394 VariantClear(dp->rgvarg+i);
1395 heap_free(dp->rgvarg);
1398 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1404 HRESULT hres = S_OK;
1406 memset(dp, 0, sizeof(*dp));
1410 for(iter = args; iter; iter = iter->next)
1413 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1415 return E_OUTOFMEMORY;
1417 for(i = cnt, iter = args; iter; iter = iter->next) {
1418 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1422 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1423 exprval_release(&exprval);
1438 /* ECMA-262 3rd Edition 11.2.2 */
1439 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1441 call_expression_t *expr = (call_expression_t*)_expr;
1443 VARIANT constr, var;
1449 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1453 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1455 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1456 exprval_release(&exprval);
1460 if(V_VT(&constr) != VT_DISPATCH) {
1461 FIXME("throw TypeError\n");
1462 VariantClear(&constr);
1466 hres = disp_call(V_DISPATCH(&constr), DISPID_VALUE, ctx->parser->script->lcid,
1467 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1468 IDispatch_Release(V_DISPATCH(&constr));
1472 ret->type = EXPRVAL_VARIANT;
1477 /* ECMA-262 3rd Edition 11.2.3 */
1478 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1480 call_expression_t *expr = (call_expression_t*)_expr;
1488 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1492 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1493 if(SUCCEEDED(hres)) {
1494 switch(exprval.type) {
1496 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
1497 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1498 if(flags & EXPR_NOVAL)
1499 V_VT(&var) = VT_EMPTY;
1502 FIXME("unimplemented type %d\n", V_VT(&func));
1509 exprval_release(&exprval);
1513 TRACE("= %s\n", debugstr_variant(&var));
1514 ret->type = EXPRVAL_VARIANT;
1519 /* ECMA-262 3rd Edition 11.1.1 */
1520 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1524 ret->type = EXPRVAL_VARIANT;
1525 V_VT(&ret->u.var) = VT_DISPATCH;
1526 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1527 IDispatch_AddRef(ctx->this_obj);
1531 /* ECMA-262 3rd Edition 10.1.4 */
1532 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1534 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1540 identifier = SysAllocString(expr->identifier);
1542 return E_OUTOFMEMORY;
1544 hres = identifier_eval(ctx, identifier, flags, ret);
1546 SysFreeString(identifier);
1550 /* ECMA-262 3rd Edition 7.8 */
1551 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1553 literal_expression_t *expr = (literal_expression_t*)_expr;
1559 hres = literal_to_var(expr->literal, &var);
1563 ret->type = EXPRVAL_VARIANT;
1568 /* ECMA-262 3rd Edition 11.1.4 */
1569 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1571 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1572 DWORD length = 0, i = 0;
1573 array_element_t *elem;
1581 for(elem = expr->element_list; elem; elem = elem->next)
1582 length += elem->elision+1;
1583 length += expr->length;
1585 hres = create_array(ctx->parser->script, length, &array);
1589 for(elem = expr->element_list; elem; elem = elem->next) {
1592 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1596 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1597 exprval_release(&exprval);
1601 hres = jsdisp_propput_idx(array, i, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1610 jsdisp_release(array);
1614 ret->type = EXPRVAL_VARIANT;
1615 V_VT(&ret->u.var) = VT_DISPATCH;
1616 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(array);
1620 /* ECMA-262 3rd Edition 11.1.5 */
1621 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1623 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1633 hres = create_object(ctx->parser->script, NULL, &obj);
1637 for(iter = expr->property_list; iter; iter = iter->next) {
1638 hres = literal_to_var(iter->name, &tmp);
1642 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1647 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1648 if(SUCCEEDED(hres)) {
1649 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1650 exprval_release(&exprval);
1651 if(SUCCEEDED(hres)) {
1652 hres = jsdisp_propput_name(obj, name, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1657 SysFreeString(name);
1663 jsdisp_release(obj);
1667 ret->type = EXPRVAL_VARIANT;
1668 V_VT(&ret->u.var) = VT_DISPATCH;
1669 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1673 /* ECMA-262 3rd Edition 11.14 */
1674 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1676 binary_expression_t *expr = (binary_expression_t*)_expr;
1682 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1686 VariantClear(&lval);
1688 ret->type = EXPRVAL_VARIANT;
1693 /* ECMA-262 3rd Edition 11.11 */
1694 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1696 binary_expression_t *expr = (binary_expression_t*)_expr;
1704 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1708 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1709 exprval_release(&exprval);
1713 hres = to_boolean(&val, &b);
1714 if(SUCCEEDED(hres) && b) {
1715 ret->type = EXPRVAL_VARIANT;
1724 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1728 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1729 exprval_release(&exprval);
1733 ret->type = EXPRVAL_VARIANT;
1738 /* ECMA-262 3rd Edition 11.11 */
1739 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1741 binary_expression_t *expr = (binary_expression_t*)_expr;
1749 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1753 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1754 exprval_release(&exprval);
1758 hres = to_boolean(&val, &b);
1759 if(SUCCEEDED(hres) && !b) {
1760 ret->type = EXPRVAL_VARIANT;
1769 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1773 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1774 exprval_release(&exprval);
1778 ret->type = EXPRVAL_VARIANT;
1783 /* ECMA-262 3rd Edition 11.10 */
1784 static HRESULT bitor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1789 hres = to_int32(ctx->parser->script, lval, ei, &li);
1793 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1802 /* ECMA-262 3rd Edition 11.10 */
1803 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1805 binary_expression_t *expr = (binary_expression_t*)_expr;
1809 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
1812 /* ECMA-262 3rd Edition 11.10 */
1813 static HRESULT xor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1818 hres = to_int32(ctx->parser->script, lval, ei, &li);
1822 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1831 /* ECMA-262 3rd Edition 11.10 */
1832 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1834 binary_expression_t *expr = (binary_expression_t*)_expr;
1838 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
1841 /* ECMA-262 3rd Edition 11.10 */
1842 static HRESULT bitand_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1847 hres = to_int32(ctx->parser->script, lval, ei, &li);
1851 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1860 /* ECMA-262 3rd Edition 11.10 */
1861 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1863 binary_expression_t *expr = (binary_expression_t*)_expr;
1867 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
1870 /* ECMA-262 3rd Edition 11.8.6 */
1871 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1877 /* ECMA-262 3rd Edition 11.8.7 */
1878 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1884 /* ECMA-262 3rd Edition 11.6.1 */
1885 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1890 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1894 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1900 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1901 BSTR lstr = NULL, rstr = NULL;
1903 if(V_VT(&l) == VT_BSTR)
1906 hres = to_string(ctx->parser->script, &l, ei, &lstr);
1908 if(SUCCEEDED(hres)) {
1909 if(V_VT(&r) == VT_BSTR)
1912 hres = to_string(ctx->parser->script, &r, ei, &rstr);
1915 if(SUCCEEDED(hres)) {
1918 len1 = SysStringLen(lstr);
1919 len2 = SysStringLen(rstr);
1921 V_VT(retv) = VT_BSTR;
1922 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1923 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1924 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1927 if(V_VT(&l) != VT_BSTR)
1928 SysFreeString(lstr);
1929 if(V_VT(&r) != VT_BSTR)
1930 SysFreeString(rstr);
1934 hres = to_number(ctx->parser->script, &l, ei, &nl);
1935 if(SUCCEEDED(hres)) {
1936 hres = to_number(ctx->parser->script, &r, ei, &nr);
1938 num_set_val(retv, num_val(&nl) + num_val(&nr));
1947 /* ECMA-262 3rd Edition 11.6.1 */
1948 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1950 binary_expression_t *expr = (binary_expression_t*)_expr;
1954 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
1957 /* ECMA-262 3rd Edition 11.6.2 */
1958 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1963 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1967 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1971 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
1975 /* ECMA-262 3rd Edition 11.6.2 */
1976 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1978 binary_expression_t *expr = (binary_expression_t*)_expr;
1982 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
1985 /* ECMA-262 3rd Edition 11.5.1 */
1986 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1991 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1995 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1999 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2003 /* ECMA-262 3rd Edition 11.5.1 */
2004 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2006 binary_expression_t *expr = (binary_expression_t*)_expr;
2010 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2013 /* ECMA-262 3rd Edition 11.5.2 */
2014 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2019 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2023 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2027 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2031 /* ECMA-262 3rd Edition 11.5.2 */
2032 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2034 binary_expression_t *expr = (binary_expression_t*)_expr;
2038 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2041 /* ECMA-262 3rd Edition 11.5.3 */
2042 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2048 /* ECMA-262 3rd Edition 11.4.2 */
2049 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2051 unary_expression_t *expr = (unary_expression_t*)_expr;
2052 VARIANT_BOOL b = VARIANT_FALSE;
2058 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2062 switch(exprval.type) {
2063 case EXPRVAL_NAMEREF: {
2064 IDispatchEx *dispex;
2066 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2067 if(SUCCEEDED(hres)) {
2068 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name, fdexNameCaseSensitive);
2070 IDispatchEx_Release(dispex);
2075 FIXME("unsupported type %d\n", exprval.type);
2079 exprval_release(&exprval);
2083 return return_bool(ret, b);
2086 /* ECMA-262 3rd Edition 11.4.2 */
2087 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2089 unary_expression_t *expr = (unary_expression_t*)_expr;
2096 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2100 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &tmp);
2101 exprval_release(&exprval);
2107 ret->type = EXPRVAL_VARIANT;
2108 V_VT(&ret->u.var) = VT_EMPTY;
2112 /* ECMA-262 3rd Edition 11.4.3 */
2113 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2115 unary_expression_t *expr = (unary_expression_t*)_expr;
2121 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2122 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2123 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2124 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2125 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2126 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2130 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2134 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2135 exprval_release(&exprval);
2139 switch(V_VT(&val)) {
2159 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
2161 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
2162 IDispatchEx_Release(_IDispatchEx_(dispex));
2169 FIXME("unhandled vt %d\n", V_VT(&val));
2177 ret->type = EXPRVAL_VARIANT;
2178 V_VT(&ret->u.var) = VT_BSTR;
2179 V_BSTR(&ret->u.var) = SysAllocString(str);
2183 /* ECMA-262 3rd Edition 11.4.7 */
2184 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2186 unary_expression_t *expr = (unary_expression_t*)_expr;
2193 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2197 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2198 exprval_release(&exprval);
2202 hres = to_number(ctx->parser->script, &val, ei, &num);
2207 ret->type = EXPRVAL_VARIANT;
2208 num_set_val(&ret->u.var, -num_val(&num));
2212 /* ECMA-262 3rd Edition 11.4.6 */
2213 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2215 unary_expression_t *expr = (unary_expression_t*)_expr;
2222 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2226 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2227 exprval_release(&exprval);
2231 hres = to_number(ctx->parser->script, &val, ei, &num);
2235 ret->type = EXPRVAL_VARIANT;
2240 /* ECMA-262 3rd Edition 11.3.1 */
2241 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2243 unary_expression_t *expr = (unary_expression_t*)_expr;
2250 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2254 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2255 if(SUCCEEDED(hres)) {
2256 hres = to_number(ctx->parser->script, &val, ei, &num);
2260 if(SUCCEEDED(hres)) {
2262 num_set_val(&inc, num_val(&num)+1.0);
2263 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
2266 exprval_release(&exprval);
2270 ret->type = EXPRVAL_VARIANT;
2275 /* ECMA-262 3rd Edition 11.3.2 */
2276 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2278 unary_expression_t *expr = (unary_expression_t*)_expr;
2285 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2289 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2290 if(SUCCEEDED(hres)) {
2291 hres = to_number(ctx->parser->script, &val, ei, &num);
2295 if(SUCCEEDED(hres)) {
2297 num_set_val(&dec, num_val(&num)-1.0);
2298 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
2301 exprval_release(&exprval);
2305 ret->type = EXPRVAL_VARIANT;
2310 /* ECMA-262 3rd Edition 11.4.4 */
2311 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2313 unary_expression_t *expr = (unary_expression_t*)_expr;
2320 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2324 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2325 if(SUCCEEDED(hres)) {
2326 hres = to_number(ctx->parser->script, &val, ei, &num);
2330 if(SUCCEEDED(hres)) {
2331 num_set_val(&val, num_val(&num)+1.0);
2332 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2335 exprval_release(&exprval);
2339 ret->type = EXPRVAL_VARIANT;
2344 /* ECMA-262 3rd Edition 11.4.5 */
2345 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2347 unary_expression_t *expr = (unary_expression_t*)_expr;
2354 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2358 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2359 if(SUCCEEDED(hres)) {
2360 hres = to_number(ctx->parser->script, &val, ei, &num);
2364 if(SUCCEEDED(hres)) {
2365 num_set_val(&val, num_val(&num)-1.0);
2366 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2369 exprval_release(&exprval);
2373 ret->type = EXPRVAL_VARIANT;
2378 /* ECMA-262 3rd Edition 11.9.3 */
2379 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2381 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2382 return equal2_values(lval, rval, ret);
2384 /* FIXME: NULL disps should be handled in more general way */
2385 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2388 return equal_values(ctx, &v, rval, ei, ret);
2391 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2394 return equal_values(ctx, lval, &v, ei, ret);
2397 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2398 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2403 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2407 hres = to_number(ctx->parser->script, lval, ei, &v);
2411 return equal_values(ctx, &v, rval, ei, ret);
2414 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2418 hres = to_number(ctx->parser->script, rval, ei, &v);
2422 return equal_values(ctx, lval, &v, ei, ret);
2425 if(V_VT(rval) == VT_BOOL) {
2429 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2430 return equal_values(ctx, lval, &v, ei, ret);
2433 if(V_VT(lval) == VT_BOOL) {
2437 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2438 return equal_values(ctx, &v, rval, ei, ret);
2442 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2446 hres = to_primitive(ctx->parser->script, rval, ei, &v);
2450 hres = equal_values(ctx, lval, &v, ei, ret);
2457 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2461 hres = to_primitive(ctx->parser->script, lval, ei, &v);
2465 hres = equal_values(ctx, &v, rval, ei, ret);
2476 /* ECMA-262 3rd Edition 11.9.1 */
2477 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2479 binary_expression_t *expr = (binary_expression_t*)_expr;
2486 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2490 hres = equal_values(ctx, &rval, &lval, ei, &b);
2494 return return_bool(ret, b);
2497 /* ECMA-262 3rd Edition 11.9.4 */
2498 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2500 binary_expression_t *expr = (binary_expression_t*)_expr;
2507 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2511 hres = equal2_values(&rval, &lval, &b);
2515 return return_bool(ret, b);
2518 /* ECMA-262 3rd Edition 11.9.2 */
2519 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2521 binary_expression_t *expr = (binary_expression_t*)_expr;
2528 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2532 hres = equal_values(ctx, &lval, &rval, ei, &b);
2536 return return_bool(ret, !b);
2539 /* ECMA-262 3rd Edition 11.9.5 */
2540 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2542 binary_expression_t *expr = (binary_expression_t*)_expr;
2549 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2553 hres = equal2_values(&lval, &rval, &b);
2557 return return_bool(ret, !b);
2560 /* ECMA-262 3rd Edition 11.8.5 */
2561 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2563 VARIANT l, r, ln, rn;
2566 hres = to_primitive(ctx->parser->script, lval, ei, &l);
2570 hres = to_primitive(ctx->parser->script, rval, ei, &r);
2576 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2577 *ret = strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0;
2578 SysFreeString(V_BSTR(&l));
2579 SysFreeString(V_BSTR(&r));
2583 hres = to_number(ctx->parser->script, &l, ei, &ln);
2586 hres = to_number(ctx->parser->script, &r, ei, &rn);
2591 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4)
2592 *ret = V_I4(&ln) < V_I4(&rn);
2594 *ret = num_val(&ln) < num_val(&rn);
2599 /* ECMA-262 3rd Edition 11.8.1 */
2600 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2602 binary_expression_t *expr = (binary_expression_t*)_expr;
2609 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2613 hres = less_eval(ctx, &lval, &rval, ei, &b);
2614 VariantClear(&lval);
2615 VariantClear(&rval);
2619 return return_bool(ret, b);
2622 /* ECMA-262 3rd Edition 11.8.3 */
2623 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2625 binary_expression_t *expr = (binary_expression_t*)_expr;
2632 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2636 hres = less_eval(ctx, &rval, &lval, ei, &b);
2637 VariantClear(&lval);
2638 VariantClear(&rval);
2642 return return_bool(ret, !b);
2645 /* ECMA-262 3rd Edition 11.8.2 */
2646 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2648 binary_expression_t *expr = (binary_expression_t*)_expr;
2655 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2659 hres = less_eval(ctx, &rval, &lval, ei, &b);
2660 VariantClear(&lval);
2661 VariantClear(&rval);
2665 return return_bool(ret, b);
2668 /* ECMA-262 3rd Edition 11.8.4 */
2669 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2671 binary_expression_t *expr = (binary_expression_t*)_expr;
2678 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2682 hres = less_eval(ctx, &lval, &rval, ei, &b);
2683 VariantClear(&lval);
2684 VariantClear(&rval);
2688 return return_bool(ret, !b);
2691 /* ECMA-262 3rd Edition 11.4.8 */
2692 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2694 unary_expression_t *expr = (unary_expression_t*)_expr;
2702 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2706 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2707 exprval_release(&exprval);
2711 hres = to_int32(ctx->parser->script, &val, ei, &i);
2715 ret->type = EXPRVAL_VARIANT;
2716 V_VT(&ret->u.var) = VT_I4;
2717 V_I4(&ret->u.var) = ~i;
2721 /* ECMA-262 3rd Edition 11.4.9 */
2722 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2724 unary_expression_t *expr = (unary_expression_t*)_expr;
2731 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2735 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
2736 exprval_release(&exprval);
2740 return return_bool(ret, !b);
2743 /* ECMA-262 3rd Edition 11.7.1 */
2744 static HRESULT lshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2750 hres = to_int32(ctx->parser->script, lval, ei, &li);
2754 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2759 V_I4(retv) = li << (ri&0x1f);
2763 /* ECMA-262 3rd Edition 11.7.1 */
2764 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2766 binary_expression_t *expr = (binary_expression_t*)_expr;
2770 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
2773 /* ECMA-262 3rd Edition 11.7.2 */
2774 static HRESULT rshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2780 hres = to_int32(ctx->parser->script, lval, ei, &li);
2784 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2789 V_I4(retv) = li >> (ri&0x1f);
2793 /* ECMA-262 3rd Edition 11.7.2 */
2794 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2796 binary_expression_t *expr = (binary_expression_t*)_expr;
2800 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
2803 /* ECMA-262 3rd Edition 11.7.3 */
2804 static HRESULT rshift2_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2809 hres = to_uint32(ctx->parser->script, lval, ei, &li);
2813 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
2818 V_I4(retv) = li >> (ri&0x1f);
2822 /* ECMA-262 3rd Edition 11.7.3 */
2823 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2825 binary_expression_t *expr = (binary_expression_t*)_expr;
2829 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
2832 /* ECMA-262 3rd Edition 11.13.1 */
2833 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2835 binary_expression_t *expr = (binary_expression_t*)_expr;
2836 exprval_t exprval, exprvalr;
2842 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
2846 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
2847 if(SUCCEEDED(hres)) {
2848 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
2849 exprval_release(&exprvalr);
2853 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
2855 exprval_release(&exprval);
2857 VariantClear(&rval);
2861 ret->type = EXPRVAL_VARIANT;
2866 /* ECMA-262 3rd Edition 11.13.2 */
2867 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2869 binary_expression_t *expr = (binary_expression_t*)_expr;
2873 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
2876 /* ECMA-262 3rd Edition 11.13.2 */
2877 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2879 binary_expression_t *expr = (binary_expression_t*)_expr;
2883 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
2886 /* ECMA-262 3rd Edition 11.13.2 */
2887 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2889 binary_expression_t *expr = (binary_expression_t*)_expr;
2893 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
2896 /* ECMA-262 3rd Edition 11.13.2 */
2897 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2899 binary_expression_t *expr = (binary_expression_t*)_expr;
2903 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
2906 /* ECMA-262 3rd Edition 11.13.2 */
2907 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2909 binary_expression_t *expr = (binary_expression_t*)_expr;
2913 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
2916 /* ECMA-262 3rd Edition 11.13.2 */
2917 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2919 binary_expression_t *expr = (binary_expression_t*)_expr;
2923 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
2926 /* ECMA-262 3rd Edition 11.13.2 */
2927 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2929 binary_expression_t *expr = (binary_expression_t*)_expr;
2933 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
2936 /* ECMA-262 3rd Edition 11.13.2 */
2937 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2943 /* ECMA-262 3rd Edition 11.13.2 */
2944 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2946 binary_expression_t *expr = (binary_expression_t*)_expr;
2950 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
2953 /* ECMA-262 3rd Edition 11.13.2 */
2954 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2956 binary_expression_t *expr = (binary_expression_t*)_expr;
2960 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
2963 /* ECMA-262 3rd Edition 11.13.2 */
2964 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2966 binary_expression_t *expr = (binary_expression_t*)_expr;
2970 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);