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 VariantClear(&val->u.var);
48 IDispatch_Release(val->u.idref.disp);
51 if(val->u.nameref.disp)
52 IDispatch_Release(val->u.nameref.disp);
53 SysFreeString(val->u.nameref.name);
57 /* ECMA-262 3rd Edition 8.7.1 */
58 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
64 return VariantCopy(ret, &val->u.var);
66 if(!val->u.idref.disp) {
67 FIXME("throw ReferenceError\n");
71 return disp_propget(val->u.idref.disp, val->u.idref.id, ctx->lcid, ret, ei, NULL/*FIXME*/);
73 ERR("type %d\n", val->type);
78 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
80 if(val->type == EXPRVAL_VARIANT) {
82 V_VT(&val->u.var) = VT_EMPTY;
86 return exprval_value(ctx, val, ei, ret);
89 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
91 if(exprval->type != EXPRVAL_VARIANT) {
95 hres = exprval_to_value(ctx, exprval, ei, &val);
99 hres = to_boolean(&val, b);
104 return to_boolean(&exprval->u.var, b);
107 static void exprval_init(exprval_t *val)
109 val->type = EXPRVAL_VARIANT;
110 V_VT(&val->u.var) = VT_EMPTY;
113 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
115 val->type = EXPRVAL_IDREF;
116 val->u.idref.disp = disp;
117 val->u.idref.id = id;
120 IDispatch_AddRef(disp);
123 HRESULT scope_push(scope_chain_t *scope, DispatchEx *obj, scope_chain_t **ret)
125 scope_chain_t *new_scope;
127 new_scope = heap_alloc(sizeof(scope_chain_t));
129 return E_OUTOFMEMORY;
133 IDispatchEx_AddRef(_IDispatchEx_(obj));
134 new_scope->obj = obj;
138 new_scope->next = scope;
140 new_scope->next = NULL;
147 void scope_release(scope_chain_t *scope)
153 scope_release(scope->next);
155 IDispatchEx_Release(_IDispatchEx_(scope->obj));
159 HRESULT create_exec_ctx(IDispatch *this_obj, DispatchEx *var_disp, scope_chain_t *scope, exec_ctx_t **ret)
163 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
165 return E_OUTOFMEMORY;
167 IDispatch_AddRef(this_obj);
168 ctx->this_obj = this_obj;
170 IDispatchEx_AddRef(_IDispatchEx_(var_disp));
171 ctx->var_disp = var_disp;
175 ctx->scope_chain = scope;
182 void exec_release(exec_ctx_t *ctx)
188 scope_release(ctx->scope_chain);
190 IDispatchEx_Release(_IDispatchEx_(ctx->var_disp));
192 IDispatch_Release(ctx->this_obj);
196 static HRESULT dispex_get_id(IDispatchEx *dispex, BSTR name, DWORD flags, DISPID *id)
200 return IDispatchEx_GetDispID(dispex, name, flags|fdexNameCaseSensitive, id);
203 static HRESULT disp_get_id(IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
208 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
210 TRACE("unsing IDispatch\n");
213 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
216 hres = dispex_get_id(dispex, name, flags, id);
217 IDispatchEx_Release(dispex);
221 /* ECMA-262 3rd Edition 8.7.2 */
222 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
224 if(ref->type != EXPRVAL_IDREF) {
225 FIXME("throw ReferemceError\n");
229 return disp_propput(ref->u.idref.disp, ref->u.idref.id, ctx->lcid, v, ei, NULL/*FIXME*/);
232 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
234 IObjectIdentity *identity;
235 IUnknown *unk1, *unk2;
243 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
247 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
249 IUnknown_Release(unk1);
256 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
257 if(SUCCEEDED(hres)) {
258 hres = IObjectIdentity_IsEqualObject(identity, unk2);
259 IObjectIdentity_Release(identity);
266 IUnknown_Release(unk1);
267 IUnknown_Release(unk2);
271 static inline BOOL is_num_vt(enum VARENUM vt)
273 return vt == VT_I4 || vt == VT_R8;
276 static inline DOUBLE num_val(const VARIANT *v)
278 return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
281 static inline void num_set_val(VARIANT *v, DOUBLE d)
283 if(d == (DOUBLE)(INT)d) {
292 /* ECMA-262 3rd Edition 11.9.6 */
293 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
297 if(V_VT(lval) != V_VT(rval)) {
298 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
299 *ret = num_val(lval) == num_val(rval);
313 *ret = V_I4(lval) == V_I4(rval);
316 *ret = V_R8(lval) == V_R8(rval);
319 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
322 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
324 *ret = !V_BOOL(lval) == !V_BOOL(rval);
327 FIXME("unimplemented vt %d\n", V_VT(lval));
334 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
336 V_VT(v) = literal->vt;
343 V_I4(v) = literal->u.lval;
346 V_R8(v) = literal->u.dval;
349 V_BSTR(v) = SysAllocString(literal->u.wstr);
352 V_BOOL(v) = literal->u.bval;
355 IDispatch_AddRef(literal->u.disp);
356 V_DISPATCH(v) = literal->u.disp;
359 ERR("wrong type %d\n", V_VT(v));
366 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
368 script_ctx_t *script = parser->script;
369 function_declaration_t *func;
370 parser_ctx_t *prev_parser;
373 exec_ctx_t *prev_ctx;
377 for(func = source->functions; func; func = func->next) {
378 DispatchEx *func_obj;
381 hres = create_source_function(parser, func->parameter_list, func->source_elements, ctx->scope_chain, &func_obj);
385 V_VT(&var) = VT_DISPATCH;
386 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
387 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
388 IDispatchEx_Release(_IDispatchEx_(func_obj));
393 prev_ctx = script->exec_ctx;
394 script->exec_ctx = ctx;
396 prev_parser = ctx->parser;
397 ctx->parser = parser;
399 V_VT(&val) = VT_EMPTY;
400 memset(&rt, 0, sizeof(rt));
403 for(stat = source->statement; stat; stat = stat->next) {
404 hres = stat_eval(ctx, stat, &rt, &tmp);
410 if(rt.type != RT_NORMAL)
414 script->exec_ctx = prev_ctx;
415 ctx->parser = prev_parser;
417 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
418 FIXME("wrong rt %d\n", rt.type);
435 /* ECMA-262 3rd Edition 10.1.4 */
436 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
438 scope_chain_t *scope;
443 TRACE("%s\n", debugstr_w(identifier));
445 for(scope = ctx->scope_chain; scope; scope = scope->next) {
446 hres = dispex_get_id(_IDispatchEx_(scope->obj), identifier, 0, &id);
452 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
456 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->global), identifier, 0, &id);
457 if(SUCCEEDED(hres)) {
458 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
462 for(item = ctx->parser->script->named_items; item; item = item->next) {
463 hres = disp_get_id(item->disp, identifier, 0, &id);
469 exprval_set_idref(ret, (IDispatch*)item->disp, id);
473 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->script_disp), identifier, 0, &id);
474 if(SUCCEEDED(hres)) {
475 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
479 if(flags & EXPR_NEWREF) {
480 hres = dispex_get_id(_IDispatchEx_(ctx->var_disp), identifier, fdexNameEnsure, &id);
484 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
488 WARN("Could not find identifier %s\n", debugstr_w(identifier));
492 /* ECMA-262 3rd Edition 12.1 */
493 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
495 block_statement_t *stat = (block_statement_t*)_stat;
502 V_VT(&val) = VT_EMPTY;
503 for(iter = stat->stat_list; iter; iter = iter->next) {
504 hres = stat_eval(ctx, iter, rt, &tmp);
510 if(rt->type != RT_NORMAL)
523 /* ECMA-262 3rd Edition 12.2 */
524 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
526 variable_declaration_t *iter;
529 for(iter = var_list; iter; iter = iter->next) {
535 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
539 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
540 exprval_release(&exprval);
544 V_VT(&val) = VT_EMPTY;
547 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
556 /* ECMA-262 3rd Edition 12.2 */
557 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
559 var_statement_t *stat = (var_statement_t*)_stat;
564 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
568 V_VT(ret) = VT_EMPTY;
572 /* ECMA-262 3rd Edition 12.3 */
573 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
577 V_VT(ret) = VT_EMPTY;
581 /* ECMA-262 3rd Edition 12.4 */
582 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
584 expression_statement_t *stat = (expression_statement_t*)_stat;
591 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
595 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
596 exprval_release(&exprval);
601 TRACE("= %s\n", debugstr_variant(ret));
605 /* ECMA-262 3rd Edition 12.5 */
606 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
608 if_statement_t *stat = (if_statement_t*)_stat;
615 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
619 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
620 exprval_release(&exprval);
625 hres = stat_eval(ctx, stat->if_stat, rt, ret);
626 else if(stat->else_stat)
627 hres = stat_eval(ctx, stat->else_stat, rt, ret);
629 V_VT(ret) = VT_EMPTY;
634 HRESULT dowhile_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
640 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
646 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
652 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
658 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
664 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
670 /* ECMA-262 3rd Edition 12.9 */
671 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
673 expression_statement_t *stat = (expression_statement_t*)_stat;
681 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
685 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
686 exprval_release(&exprval);
690 V_VT(ret) = VT_EMPTY;
693 TRACE("= %s\n", debugstr_variant(ret));
694 rt->type = RT_RETURN;
698 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
704 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
710 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
716 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
722 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
728 static HRESULT return_bool(exprval_t *ret, DWORD b)
730 ret->type = EXPRVAL_VARIANT;
731 V_VT(&ret->u.var) = VT_BOOL;
732 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
737 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
742 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
746 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
747 exprval_release(&exprval);
751 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
752 if(SUCCEEDED(hres)) {
753 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
754 exprval_release(&exprval);
765 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
767 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
770 VARIANT lval, rval, retv;
773 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
777 hres = oper(ctx, &lval, &rval, ei, &retv);
783 ret->type = EXPRVAL_VARIANT;
788 /* ECMA-262 3rd Edition 11.13.2 */
789 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
790 jsexcept_t *ei, exprval_t *ret)
792 VARIANT retv, lval, rval;
793 exprval_t exprval, exprvalr;
796 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
800 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
801 if(SUCCEEDED(hres)) {
802 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
803 if(SUCCEEDED(hres)) {
804 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
805 exprval_release(&exprvalr);
807 if(SUCCEEDED(hres)) {
808 hres = oper(ctx, &lval, &rval, ei, &retv);
814 if(SUCCEEDED(hres)) {
815 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
819 exprval_release(&exprval);
824 ret->type = EXPRVAL_VARIANT;
829 /* ECMA-262 3rd Edition 13 */
830 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
832 function_expression_t *expr = (function_expression_t*)_expr;
839 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain, &dispex);
843 V_VT(&var) = VT_DISPATCH;
844 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
846 if(expr->identifier) {
847 hres = jsdisp_propput_name(ctx->var_disp, expr->identifier, ctx->parser->script->lcid, &var, ei, NULL/*FIXME*/);
849 jsdisp_release(dispex);
854 ret->type = EXPRVAL_VARIANT;
859 /* ECMA-262 3rd Edition 11.12 */
860 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
862 conditional_expression_t *expr = (conditional_expression_t*)_expr;
869 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
873 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
874 exprval_release(&exprval);
878 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
881 /* ECMA-262 3rd Edition 11.2.1 */
882 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
884 array_expression_t *expr = (array_expression_t*)_expr;
889 IDispatch *obj = NULL;
894 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
898 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
899 exprval_release(&exprval);
903 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
904 if(SUCCEEDED(hres)) {
905 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
906 exprval_release(&exprval);
910 hres = to_object(ctx, &member, &obj);
911 VariantClear(&member);
912 if(SUCCEEDED(hres)) {
913 hres = to_string(ctx->parser->script, &val, ei, &str);
914 if(SUCCEEDED(hres)) {
915 if(flags & EXPR_STRREF) {
916 ret->type = EXPRVAL_NAMEREF;
917 ret->u.nameref.disp = obj;
918 ret->u.nameref.name = str;
922 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
925 if(SUCCEEDED(hres)) {
926 exprval_set_idref(ret, obj, id);
927 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
932 IDispatch_Release(obj);
938 /* ECMA-262 3rd Edition 11.2.1 */
939 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
941 member_expression_t *expr = (member_expression_t*)_expr;
942 IDispatch *obj = NULL;
951 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
955 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
956 exprval_release(&exprval);
960 hres = to_object(ctx, &member, &obj);
961 VariantClear(&member);
965 str = SysAllocString(expr->identifier);
966 if(flags & EXPR_STRREF) {
967 ret->type = EXPRVAL_NAMEREF;
968 ret->u.nameref.disp = obj;
969 ret->u.nameref.name = str;
973 hres = disp_get_id(obj, str, flags & EXPR_NEW ? fdexNameEnsure : 0, &id);
975 if(SUCCEEDED(hres)) {
976 exprval_set_idref(ret, obj, id);
977 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
982 IDispatch_Release(obj);
986 static void free_dp(DISPPARAMS *dp)
990 for(i=0; i < dp->cArgs; i++)
991 VariantClear(dp->rgvarg+i);
992 heap_free(dp->rgvarg);
995 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1001 HRESULT hres = S_OK;
1003 memset(dp, 0, sizeof(*dp));
1005 for(iter = args; iter; iter = iter->next)
1010 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1012 return E_OUTOFMEMORY;
1014 for(i = cnt, iter = args; iter; iter = iter->next) {
1015 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1019 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1020 exprval_release(&exprval);
1035 /* ECMA-262 3rd Edition 11.2.2 */
1036 HRESULT member_new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1038 call_expression_t *expr = (call_expression_t*)_expr;
1040 VARIANT constr, var;
1046 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1050 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1052 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1053 exprval_release(&exprval);
1057 if(V_VT(&constr) != VT_DISPATCH) {
1058 FIXME("throw TypeError\n");
1059 VariantClear(&constr);
1063 hres = disp_call(V_DISPATCH(&constr), DISPID_VALUE, ctx->parser->script->lcid,
1064 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1065 IDispatch_Release(V_DISPATCH(&constr));
1069 ret->type = EXPRVAL_VARIANT;
1074 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1076 call_expression_t *expr = (call_expression_t*)_expr;
1084 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1088 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1089 if(SUCCEEDED(hres)) {
1090 switch(exprval.type) {
1092 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
1093 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1094 if(flags & EXPR_NOVAL)
1095 V_VT(&var) = VT_EMPTY;
1098 FIXME("unimplemented type %d\n", V_VT(&func));
1105 exprval_release(&exprval);
1109 TRACE("= %s\n", debugstr_variant(&var));
1110 ret->type = EXPRVAL_VARIANT;
1115 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1119 ret->type = EXPRVAL_VARIANT;
1120 V_VT(&ret->u.var) = VT_DISPATCH;
1121 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1122 IDispatch_AddRef(ctx->this_obj);
1126 /* ECMA-262 3rd Edition 10.1.4 */
1127 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1129 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1135 identifier = SysAllocString(expr->identifier);
1137 return E_OUTOFMEMORY;
1139 hres = identifier_eval(ctx, identifier, flags, ret);
1141 SysFreeString(identifier);
1145 /* ECMA-262 3rd Edition 7.8 */
1146 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1148 literal_expression_t *expr = (literal_expression_t*)_expr;
1154 hres = literal_to_var(expr->literal, &var);
1158 ret->type = EXPRVAL_VARIANT;
1163 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1169 /* ECMA-262 3rd Edition 11.1.5 */
1170 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1172 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1182 hres = create_object(ctx->parser->script, NULL, &obj);
1186 for(iter = expr->property_list; iter; iter = iter->next) {
1187 hres = literal_to_var(iter->name, &tmp);
1191 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1196 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1197 if(SUCCEEDED(hres)) {
1198 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1199 exprval_release(&exprval);
1200 if(SUCCEEDED(hres)) {
1201 hres = jsdisp_propput_name(obj, name, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1206 SysFreeString(name);
1212 jsdisp_release(obj);
1216 ret->type = EXPRVAL_VARIANT;
1217 V_VT(&ret->u.var) = VT_DISPATCH;
1218 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1222 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1228 /* ECMA-262 3rd Edition 11.11 */
1229 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1231 binary_expression_t *expr = (binary_expression_t*)_expr;
1239 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1243 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1244 exprval_release(&exprval);
1248 hres = to_boolean(&val, &b);
1249 if(SUCCEEDED(hres) && b) {
1250 ret->type = EXPRVAL_VARIANT;
1259 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1263 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1264 exprval_release(&exprval);
1268 ret->type = EXPRVAL_VARIANT;
1273 /* ECMA-262 3rd Edition 11.11 */
1274 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1276 binary_expression_t *expr = (binary_expression_t*)_expr;
1284 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1288 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1289 exprval_release(&exprval);
1293 hres = to_boolean(&val, &b);
1294 if(SUCCEEDED(hres) && !b) {
1295 ret->type = EXPRVAL_VARIANT;
1304 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1308 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1309 exprval_release(&exprval);
1313 ret->type = EXPRVAL_VARIANT;
1318 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1324 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1330 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1336 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1342 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1348 /* ECMA-262 3rd Edition 11.6.1 */
1349 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1354 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1358 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1364 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1365 BSTR lstr = NULL, rstr = NULL;
1367 if(V_VT(&l) == VT_BSTR)
1370 hres = to_string(ctx->parser->script, &l, ei, &lstr);
1372 if(SUCCEEDED(hres)) {
1373 if(V_VT(&r) == VT_BSTR)
1376 hres = to_string(ctx->parser->script, &r, ei, &rstr);
1379 if(SUCCEEDED(hres)) {
1382 len1 = SysStringLen(lstr);
1383 len2 = SysStringLen(rstr);
1385 V_VT(retv) = VT_BSTR;
1386 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1387 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1388 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1391 if(lstr && V_VT(&l) != VT_BSTR)
1392 SysFreeString(lstr);
1393 if(rstr && V_VT(&r) != VT_BSTR)
1394 SysFreeString(rstr);
1398 hres = to_number(ctx->parser->script, &l, ei, &nl);
1399 if(SUCCEEDED(hres)) {
1400 hres = to_number(ctx->parser->script, &r, ei, &nr);
1402 num_set_val(retv, num_val(&nl) + num_val(&nr));
1411 /* ECMA-262 3rd Edition 11.6.1 */
1412 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1414 binary_expression_t *expr = (binary_expression_t*)_expr;
1418 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
1421 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1427 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1433 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1439 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1445 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1451 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1457 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1459 unary_expression_t *expr = (unary_expression_t*)_expr;
1465 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
1466 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
1467 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
1468 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
1469 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
1470 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1474 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1478 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1479 exprval_release(&exprval);
1483 switch(V_VT(&val)) {
1503 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
1505 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
1506 IDispatchEx_Release(_IDispatchEx_(dispex));
1513 FIXME("unhandled vt %d\n", V_VT(&val));
1521 ret->type = EXPRVAL_VARIANT;
1522 V_VT(&ret->u.var) = VT_BSTR;
1523 V_BSTR(&ret->u.var) = SysAllocString(str);
1527 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1533 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1539 /* ECMA-262 3rd Edition 11.3.1 */
1540 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1542 unary_expression_t *expr = (unary_expression_t*)_expr;
1549 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1553 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1554 if(SUCCEEDED(hres)) {
1555 hres = to_number(ctx->parser->script, &val, ei, &num);
1559 if(SUCCEEDED(hres)) {
1561 num_set_val(&inc, num_val(&num)+1.0);
1562 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
1565 exprval_release(&exprval);
1569 ret->type = EXPRVAL_VARIANT;
1574 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1580 /* ECMA-262 3rd Edition 11.4.4 */
1581 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1583 unary_expression_t *expr = (unary_expression_t*)_expr;
1590 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1594 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1595 if(SUCCEEDED(hres)) {
1596 hres = to_number(ctx->parser->script, &val, ei, &num);
1600 if(SUCCEEDED(hres)) {
1601 num_set_val(&val, num_val(&num)+1.0);
1602 hres = put_value(ctx->parser->script, &exprval, &val, ei);
1605 exprval_release(&exprval);
1609 ret->type = EXPRVAL_VARIANT;
1614 /* ECMA-262 3rd Edition 11.4.5 */
1615 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1617 unary_expression_t *expr = (unary_expression_t*)_expr;
1624 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1628 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1629 if(SUCCEEDED(hres)) {
1630 hres = to_number(ctx->parser->script, &val, ei, &num);
1634 if(SUCCEEDED(hres)) {
1635 num_set_val(&val, num_val(&num)-1.0);
1636 hres = put_value(ctx->parser->script, &exprval, &val, ei);
1639 exprval_release(&exprval);
1643 ret->type = EXPRVAL_VARIANT;
1648 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1654 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1660 /* ECMA-262 3rd Edition 11.9.4 */
1661 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1663 binary_expression_t *expr = (binary_expression_t*)_expr;
1670 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
1674 hres = equal2_values(&rval, &lval, &b);
1678 return return_bool(ret, b);
1681 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1688 /* ECMA-262 3rd Edition 11.9.5 */
1689 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1691 binary_expression_t *expr = (binary_expression_t*)_expr;
1698 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
1702 hres = equal2_values(&rval, &lval, &b);
1706 return return_bool(ret, !b);
1709 /* ECMA-262 3rd Edition 11.8.5 */
1710 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1712 VARIANT l, r, ln, rn;
1715 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1719 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1725 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
1726 *ret = strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0;
1727 SysFreeString(V_BSTR(&l));
1728 SysFreeString(V_BSTR(&r));
1732 hres = to_number(ctx->parser->script, &l, ei, &ln);
1735 hres = to_number(ctx->parser->script, &r, ei, &rn);
1740 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4)
1741 *ret = V_I4(&ln) < V_I4(&rn);
1743 *ret = num_val(&ln) < num_val(&rn);
1748 /* ECMA-262 3rd Edition 11.8.1 */
1749 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1751 binary_expression_t *expr = (binary_expression_t*)_expr;
1758 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1762 hres = less_eval(ctx, &lval, &rval, ei, &b);
1763 VariantClear(&lval);
1764 VariantClear(&rval);
1768 return return_bool(ret, b);
1771 /* ECMA-262 3rd Edition 11.8.3 */
1772 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1774 binary_expression_t *expr = (binary_expression_t*)_expr;
1781 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1785 hres = less_eval(ctx, &rval, &lval, ei, &b);
1786 VariantClear(&lval);
1787 VariantClear(&rval);
1791 return return_bool(ret, !b);
1794 /* ECMA-262 3rd Edition 11.8.2 */
1795 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1797 binary_expression_t *expr = (binary_expression_t*)_expr;
1804 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1808 hres = less_eval(ctx, &rval, &lval, ei, &b);
1809 VariantClear(&lval);
1810 VariantClear(&rval);
1814 return return_bool(ret, b);
1817 /* ECMA-262 3rd Edition 11.8.4 */
1818 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1820 binary_expression_t *expr = (binary_expression_t*)_expr;
1827 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1831 hres = less_eval(ctx, &lval, &rval, ei, &b);
1832 VariantClear(&lval);
1833 VariantClear(&rval);
1837 return return_bool(ret, !b);
1840 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1846 /* ECMA-262 3rd Edition 11.4.9 */
1847 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1849 unary_expression_t *expr = (unary_expression_t*)_expr;
1856 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1860 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1861 exprval_release(&exprval);
1865 return return_bool(ret, !b);
1868 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1874 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1880 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1886 /* ECMA-262 3rd Edition 11.13.1 */
1887 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1889 binary_expression_t *expr = (binary_expression_t*)_expr;
1890 exprval_t exprval, exprvalr;
1896 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
1900 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
1901 if(SUCCEEDED(hres)) {
1902 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
1903 exprval_release(&exprvalr);
1907 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
1909 exprval_release(&exprval);
1911 VariantClear(&rval);
1915 ret->type = EXPRVAL_VARIANT;
1920 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1926 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1932 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1938 /* ECMA-262 3rd Edition 11.13.2 */
1939 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1941 binary_expression_t *expr = (binary_expression_t*)_expr;
1945 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
1948 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1954 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1960 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1966 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1972 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1978 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1984 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)