Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / jscript / engine.c
blobb40a88eb591923941fd7ef81ac46d3bf467f815d
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
24 #include "jscript.h"
25 #include "engine.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 #define EXPR_NOVAL 0x0001
32 #define EXPR_NEWREF 0x0002
33 #define EXPR_STRREF 0x0004
35 static inline HRESULT stat_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
37 return stat->eval(ctx, stat, rt, ret);
40 static inline HRESULT expr_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
42 return _expr->eval(ctx, _expr, flags, ei, ret);
45 static void exprval_release(exprval_t *val)
47 switch(val->type) {
48 case EXPRVAL_VARIANT:
49 if(V_VT(&val->u.var) != VT_EMPTY)
50 VariantClear(&val->u.var);
51 return;
52 case EXPRVAL_IDREF:
53 if(val->u.idref.disp)
54 IDispatch_Release(val->u.idref.disp);
55 return;
56 case EXPRVAL_NAMEREF:
57 if(val->u.nameref.disp)
58 IDispatch_Release(val->u.nameref.disp);
59 SysFreeString(val->u.nameref.name);
60 return;
61 case EXPRVAL_INVALID:
62 SysFreeString(val->u.identifier);
66 /* ECMA-262 3rd Edition 8.7.1 */
67 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
69 V_VT(ret) = VT_EMPTY;
71 switch(val->type) {
72 case EXPRVAL_VARIANT:
73 return VariantCopy(ret, &val->u.var);
74 case EXPRVAL_IDREF:
75 if(!val->u.idref.disp) {
76 FIXME("throw ReferenceError\n");
77 return E_FAIL;
80 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
81 case EXPRVAL_NAMEREF:
82 break;
83 case EXPRVAL_INVALID:
84 return throw_type_error(ctx, ei, IDS_UNDEFINED, val->u.identifier);
87 ERR("type %d\n", val->type);
88 return E_FAIL;
91 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
93 if(val->type == EXPRVAL_VARIANT) {
94 *ret = val->u.var;
95 V_VT(&val->u.var) = VT_EMPTY;
96 return S_OK;
99 return exprval_value(ctx, val, ei, ret);
102 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
104 if(exprval->type != EXPRVAL_VARIANT) {
105 VARIANT val;
106 HRESULT hres;
108 hres = exprval_to_value(ctx, exprval, ei, &val);
109 if(FAILED(hres))
110 return hres;
112 hres = to_boolean(&val, b);
113 VariantClear(&val);
114 return hres;
117 return to_boolean(&exprval->u.var, b);
120 static void exprval_init(exprval_t *val)
122 val->type = EXPRVAL_VARIANT;
123 V_VT(&val->u.var) = VT_EMPTY;
126 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
128 val->type = EXPRVAL_IDREF;
129 val->u.idref.disp = disp;
130 val->u.idref.id = id;
132 if(disp)
133 IDispatch_AddRef(disp);
136 HRESULT scope_push(scope_chain_t *scope, DispatchEx *obj, scope_chain_t **ret)
138 scope_chain_t *new_scope;
140 new_scope = heap_alloc(sizeof(scope_chain_t));
141 if(!new_scope)
142 return E_OUTOFMEMORY;
144 new_scope->ref = 1;
146 IDispatchEx_AddRef(_IDispatchEx_(obj));
147 new_scope->obj = obj;
149 if(scope) {
150 scope_addref(scope);
151 new_scope->next = scope;
152 }else {
153 new_scope->next = NULL;
156 *ret = new_scope;
157 return S_OK;
160 static void scope_pop(scope_chain_t **scope)
162 scope_chain_t *tmp;
164 tmp = *scope;
165 *scope = tmp->next;
166 scope_release(tmp);
169 void scope_release(scope_chain_t *scope)
171 if(--scope->ref)
172 return;
174 if(scope->next)
175 scope_release(scope->next);
177 jsdisp_release(scope->obj);
178 heap_free(scope);
181 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, DispatchEx *var_disp,
182 scope_chain_t *scope, exec_ctx_t **ret)
184 exec_ctx_t *ctx;
186 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
187 if(!ctx)
188 return E_OUTOFMEMORY;
190 ctx->ref = 1;
192 if(this_obj)
193 ctx->this_obj = this_obj;
194 else if(script_ctx->host_global)
195 ctx->this_obj = script_ctx->host_global;
196 else
197 ctx->this_obj = (IDispatch*)_IDispatchEx_(script_ctx->global);
198 IDispatch_AddRef(ctx->this_obj);
200 IDispatchEx_AddRef(_IDispatchEx_(var_disp));
201 ctx->var_disp = var_disp;
203 if(scope) {
204 scope_addref(scope);
205 ctx->scope_chain = scope;
208 *ret = ctx;
209 return S_OK;
212 void exec_release(exec_ctx_t *ctx)
214 if(--ctx->ref)
215 return;
217 if(ctx->scope_chain)
218 scope_release(ctx->scope_chain);
219 if(ctx->var_disp)
220 jsdisp_release(ctx->var_disp);
221 if(ctx->this_obj)
222 IDispatch_Release(ctx->this_obj);
223 heap_free(ctx);
226 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
228 IDispatchEx *dispex;
229 HRESULT hres;
231 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
232 if(FAILED(hres)) {
233 TRACE("unsing IDispatch\n");
235 *id = 0;
236 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
239 *id = 0;
240 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
241 IDispatchEx_Release(dispex);
242 return hres;
245 /* ECMA-262 3rd Edition 8.7.2 */
246 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
248 if(ref->type != EXPRVAL_IDREF)
249 return throw_reference_error(ctx, ei, IDS_ILLEGAL_ASSIGN, NULL);
251 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
254 static inline BOOL is_null(const VARIANT *v)
256 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
259 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
261 IObjectIdentity *identity;
262 IUnknown *unk1, *unk2;
263 HRESULT hres;
265 if(disp1 == disp2) {
266 *ret = TRUE;
267 return S_OK;
270 if(!disp1 || !disp2) {
271 *ret = FALSE;
272 return S_OK;
275 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
276 if(FAILED(hres))
277 return hres;
279 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
280 if(FAILED(hres)) {
281 IUnknown_Release(unk1);
282 return hres;
285 if(unk1 == unk2) {
286 *ret = TRUE;
287 }else {
288 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
289 if(SUCCEEDED(hres)) {
290 hres = IObjectIdentity_IsEqualObject(identity, unk2);
291 IObjectIdentity_Release(identity);
292 *ret = hres == S_OK;
293 }else {
294 *ret = FALSE;
298 IUnknown_Release(unk1);
299 IUnknown_Release(unk2);
300 return S_OK;
303 /* ECMA-262 3rd Edition 11.9.6 */
304 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
306 TRACE("\n");
308 if(V_VT(lval) != V_VT(rval)) {
309 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
310 *ret = num_val(lval) == num_val(rval);
311 else if(is_null(lval))
312 *ret = is_null(rval);
313 else
314 *ret = FALSE;
315 return S_OK;
318 switch(V_VT(lval)) {
319 case VT_EMPTY:
320 case VT_NULL:
321 *ret = VARIANT_TRUE;
322 break;
323 case VT_I4:
324 *ret = V_I4(lval) == V_I4(rval);
325 break;
326 case VT_R8:
327 *ret = V_R8(lval) == V_R8(rval);
328 break;
329 case VT_BSTR:
330 if(!V_BSTR(lval))
331 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
332 else if(!V_BSTR(rval))
333 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
334 else
335 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
336 break;
337 case VT_DISPATCH:
338 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
339 case VT_BOOL:
340 *ret = !V_BOOL(lval) == !V_BOOL(rval);
341 break;
342 default:
343 FIXME("unimplemented vt %d\n", V_VT(lval));
344 return E_NOTIMPL;
347 return S_OK;
350 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
352 switch(literal->type) {
353 case LT_NULL:
354 V_VT(v) = VT_NULL;
355 break;
356 case LT_INT:
357 V_VT(v) = VT_I4;
358 V_I4(v) = literal->u.lval;
359 break;
360 case LT_DOUBLE:
361 V_VT(v) = VT_R8;
362 V_R8(v) = literal->u.dval;
363 break;
364 case LT_STRING: {
365 BSTR str = SysAllocString(literal->u.wstr);
366 if(!str)
367 return E_OUTOFMEMORY;
369 V_VT(v) = VT_BSTR;
370 V_BSTR(v) = str;
371 break;
373 case LT_BOOL:
374 V_VT(v) = VT_BOOL;
375 V_BOOL(v) = literal->u.bval;
376 break;
377 case LT_REGEXP: {
378 DispatchEx *regexp;
379 HRESULT hres;
381 hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
382 literal->u.regexp.flags, &regexp);
383 if(FAILED(hres))
384 return hres;
386 V_VT(v) = VT_DISPATCH;
387 V_DISPATCH(v) = (IDispatch*)_IDispatchEx_(regexp);
391 return S_OK;
394 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
396 named_item_t *item;
397 DISPID id;
398 HRESULT hres;
400 for(item = ctx->named_items; item; item = item->next) {
401 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
402 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
403 if(SUCCEEDED(hres)) {
404 if(ret)
405 exprval_set_idref(ret, item->disp, id);
406 return TRUE;
411 return FALSE;
414 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, exec_type_t exec_type,
415 jsexcept_t *ei, VARIANT *retv)
417 script_ctx_t *script = parser->script;
418 function_declaration_t *func;
419 parser_ctx_t *prev_parser;
420 var_list_t *var;
421 VARIANT val, tmp;
422 statement_t *stat;
423 exec_ctx_t *prev_ctx;
424 return_type_t rt;
425 HRESULT hres = S_OK;
427 for(func = source->functions; func; func = func->next) {
428 DispatchEx *func_obj;
429 VARIANT var;
431 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
432 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
433 if(FAILED(hres))
434 return hres;
436 V_VT(&var) = VT_DISPATCH;
437 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
438 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
439 jsdisp_release(func_obj);
440 if(FAILED(hres))
441 return hres;
444 for(var = source->variables; var; var = var->next) {
445 DISPID id = 0;
446 BSTR name;
448 name = SysAllocString(var->identifier);
449 if(!name)
450 return E_OUTOFMEMORY;
452 if(!lookup_global_members(parser->script, name, NULL))
453 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
454 SysFreeString(name);
455 if(FAILED(hres))
456 return hres;
459 prev_ctx = script->exec_ctx;
460 script->exec_ctx = ctx;
462 prev_parser = ctx->parser;
463 ctx->parser = parser;
465 V_VT(&val) = VT_EMPTY;
466 memset(&rt, 0, sizeof(rt));
467 rt.type = RT_NORMAL;
469 for(stat = source->statement; stat; stat = stat->next) {
470 hres = stat_eval(ctx, stat, &rt, &tmp);
471 if(FAILED(hres))
472 break;
474 VariantClear(&val);
475 val = tmp;
476 if(rt.type != RT_NORMAL)
477 break;
480 script->exec_ctx = prev_ctx;
481 ctx->parser = prev_parser;
483 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
484 FIXME("wrong rt %d\n", rt.type);
485 hres = E_FAIL;
488 *ei = rt.ei;
489 if(FAILED(hres)) {
490 VariantClear(&val);
491 return hres;
494 if(retv && (exec_type == EXECT_EVAL || rt.type == RT_RETURN))
495 *retv = val;
496 else {
497 if (retv) {
498 VariantInit(retv);
500 VariantClear(&val);
502 return S_OK;
505 /* ECMA-262 3rd Edition 10.1.4 */
506 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
508 scope_chain_t *scope;
509 named_item_t *item;
510 DISPID id = 0;
511 HRESULT hres;
513 TRACE("%s\n", debugstr_w(identifier));
515 for(scope = ctx->scope_chain; scope; scope = scope->next) {
516 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
517 if(SUCCEEDED(hres))
518 break;
521 if(scope) {
522 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
523 return S_OK;
526 hres = jsdisp_get_id(ctx->parser->script->global, identifier, 0, &id);
527 if(SUCCEEDED(hres)) {
528 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
529 return S_OK;
532 for(item = ctx->parser->script->named_items; item; item = item->next) {
533 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
534 if(!item->disp) {
535 IUnknown *unk;
537 if(!ctx->parser->script->site)
538 break;
540 hres = IActiveScriptSite_GetItemInfo(ctx->parser->script->site, identifier,
541 SCRIPTINFO_IUNKNOWN, &unk, NULL);
542 if(FAILED(hres)) {
543 WARN("GetItemInfo failed: %08x\n", hres);
544 break;
547 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
548 IUnknown_Release(unk);
549 if(FAILED(hres)) {
550 WARN("object does not implement IDispatch\n");
551 break;
555 ret->type = EXPRVAL_VARIANT;
556 V_VT(&ret->u.var) = VT_DISPATCH;
557 V_DISPATCH(&ret->u.var) = item->disp;
558 IDispatch_AddRef(item->disp);
559 return S_OK;
563 if(lookup_global_members(ctx->parser->script, identifier, ret))
564 return S_OK;
566 if(flags & EXPR_NEWREF) {
567 hres = jsdisp_get_id(ctx->parser->script->global, identifier, fdexNameEnsure, &id);
568 if(FAILED(hres))
569 return hres;
571 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
572 return S_OK;
575 ret->type = EXPRVAL_INVALID;
576 ret->u.identifier = SysAllocString(identifier);
577 if(!ret->u.identifier)
578 return E_OUTOFMEMORY;
580 return S_OK;
583 /* ECMA-262 3rd Edition 12.1 */
584 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
586 block_statement_t *stat = (block_statement_t*)_stat;
587 VARIANT val, tmp;
588 statement_t *iter;
589 HRESULT hres = S_OK;
591 TRACE("\n");
593 V_VT(&val) = VT_EMPTY;
594 for(iter = stat->stat_list; iter; iter = iter->next) {
595 hres = stat_eval(ctx, iter, rt, &tmp);
596 if(FAILED(hres))
597 break;
599 VariantClear(&val);
600 val = tmp;
601 if(rt->type != RT_NORMAL)
602 break;
605 if(FAILED(hres)) {
606 VariantClear(&val);
607 return hres;
610 *ret = val;
611 return S_OK;
614 /* ECMA-262 3rd Edition 12.2 */
615 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
617 variable_declaration_t *iter;
618 HRESULT hres = S_OK;
620 for(iter = var_list; iter; iter = iter->next) {
621 exprval_t exprval;
622 VARIANT val;
624 if(!iter->expr)
625 continue;
627 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
628 if(FAILED(hres))
629 break;
631 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
632 exprval_release(&exprval);
633 if(FAILED(hres))
634 break;
636 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
637 VariantClear(&val);
638 if(FAILED(hres))
639 break;
642 return hres;
645 /* ECMA-262 3rd Edition 12.2 */
646 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
648 var_statement_t *stat = (var_statement_t*)_stat;
649 HRESULT hres;
651 TRACE("\n");
653 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
654 if(FAILED(hres))
655 return hres;
657 V_VT(ret) = VT_EMPTY;
658 return S_OK;
661 /* ECMA-262 3rd Edition 12.3 */
662 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
664 TRACE("\n");
666 V_VT(ret) = VT_EMPTY;
667 return S_OK;
670 /* ECMA-262 3rd Edition 12.4 */
671 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
673 expression_statement_t *stat = (expression_statement_t*)_stat;
674 exprval_t exprval;
675 VARIANT val;
676 HRESULT hres;
678 TRACE("\n");
680 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
681 if(FAILED(hres))
682 return hres;
684 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
685 exprval_release(&exprval);
686 if(FAILED(hres))
687 return hres;
689 *ret = val;
690 TRACE("= %s\n", debugstr_variant(ret));
691 return S_OK;
694 /* ECMA-262 3rd Edition 12.5 */
695 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
697 if_statement_t *stat = (if_statement_t*)_stat;
698 exprval_t exprval;
699 VARIANT_BOOL b;
700 HRESULT hres;
702 TRACE("\n");
704 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
705 if(FAILED(hres))
706 return hres;
708 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
709 exprval_release(&exprval);
710 if(FAILED(hres))
711 return hres;
713 if(b)
714 hres = stat_eval(ctx, stat->if_stat, rt, ret);
715 else if(stat->else_stat)
716 hres = stat_eval(ctx, stat->else_stat, rt, ret);
717 else
718 V_VT(ret) = VT_EMPTY;
720 return hres;
723 /* ECMA-262 3rd Edition 12.6.2 */
724 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
726 while_statement_t *stat = (while_statement_t*)_stat;
727 exprval_t exprval;
728 VARIANT val, tmp;
729 VARIANT_BOOL b;
730 BOOL test_expr;
731 HRESULT hres;
733 TRACE("\n");
735 V_VT(&val) = VT_EMPTY;
736 test_expr = !stat->do_while;
738 while(1) {
739 if(test_expr) {
740 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
741 if(FAILED(hres))
742 break;
744 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
745 exprval_release(&exprval);
746 if(FAILED(hres) || !b)
747 break;
748 }else {
749 test_expr = TRUE;
752 hres = stat_eval(ctx, stat->statement, rt, &tmp);
753 if(FAILED(hres))
754 break;
756 VariantClear(&val);
757 val = tmp;
759 if(rt->type == RT_CONTINUE)
760 rt->type = RT_NORMAL;
761 if(rt->type != RT_NORMAL)
762 break;
765 if(FAILED(hres)) {
766 VariantClear(&val);
767 return hres;
770 if(rt->type == RT_BREAK)
771 rt->type = RT_NORMAL;
773 *ret = val;
774 return S_OK;
777 /* ECMA-262 3rd Edition 12.6.3 */
778 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
780 for_statement_t *stat = (for_statement_t*)_stat;
781 VARIANT val, tmp, retv;
782 exprval_t exprval;
783 VARIANT_BOOL b;
784 HRESULT hres;
786 TRACE("\n");
788 if(stat->variable_list) {
789 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
790 if(FAILED(hres))
791 return hres;
792 }else if(stat->begin_expr) {
793 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
794 if(FAILED(hres))
795 return hres;
797 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
798 exprval_release(&exprval);
799 if(FAILED(hres))
800 return hres;
802 VariantClear(&val);
805 V_VT(&retv) = VT_EMPTY;
807 while(1) {
808 if(stat->expr) {
809 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
810 if(FAILED(hres))
811 break;
813 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
814 exprval_release(&exprval);
815 if(FAILED(hres) || !b)
816 break;
819 hres = stat_eval(ctx, stat->statement, rt, &tmp);
820 if(FAILED(hres))
821 break;
823 VariantClear(&retv);
824 retv = tmp;
826 if(rt->type == RT_CONTINUE)
827 rt->type = RT_NORMAL;
828 else if(rt->type != RT_NORMAL)
829 break;
831 if(stat->end_expr) {
832 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
833 if(FAILED(hres))
834 break;
836 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
837 exprval_release(&exprval);
838 if(FAILED(hres))
839 break;
841 VariantClear(&val);
845 if(FAILED(hres)) {
846 VariantClear(&retv);
847 return hres;
850 if(rt->type == RT_BREAK)
851 rt->type = RT_NORMAL;
853 *ret = retv;
854 return S_OK;
857 /* ECMA-262 3rd Edition 12.6.4 */
858 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
860 forin_statement_t *stat = (forin_statement_t*)_stat;
861 VARIANT val, name, retv, tmp;
862 DISPID id = DISPID_STARTENUM;
863 BSTR str, identifier = NULL;
864 IDispatchEx *in_obj;
865 exprval_t exprval;
866 HRESULT hres;
868 TRACE("\n");
870 if(stat->variable) {
871 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
872 if(FAILED(hres))
873 return hres;
876 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
877 if(FAILED(hres))
878 return hres;
880 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
881 exprval_release(&exprval);
882 if(FAILED(hres))
883 return hres;
885 if(V_VT(&val) != VT_DISPATCH) {
886 TRACE("in vt %d\n", V_VT(&val));
887 VariantClear(&val);
888 V_VT(ret) = VT_EMPTY;
889 return S_OK;
892 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
893 IDispatch_Release(V_DISPATCH(&val));
894 if(FAILED(hres)) {
895 FIXME("Object doesn't support IDispatchEx\n");
896 return E_NOTIMPL;
899 V_VT(&retv) = VT_EMPTY;
901 if(stat->variable)
902 identifier = SysAllocString(stat->variable->identifier);
904 while(1) {
905 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
906 if(FAILED(hres) || hres == S_FALSE)
907 break;
909 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
910 if(FAILED(hres))
911 break;
913 TRACE("iter %s\n", debugstr_w(str));
915 if(stat->variable)
916 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
917 else
918 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
919 if(SUCCEEDED(hres)) {
920 V_VT(&name) = VT_BSTR;
921 V_BSTR(&name) = str;
922 hres = put_value(ctx->parser->script, &exprval, &name, &rt->ei);
923 exprval_release(&exprval);
925 SysFreeString(str);
926 if(FAILED(hres))
927 break;
929 hres = stat_eval(ctx, stat->statement, rt, &tmp);
930 if(FAILED(hres))
931 break;
933 VariantClear(&retv);
934 retv = tmp;
936 if(rt->type == RT_CONTINUE)
937 rt->type = RT_NORMAL;
938 else if(rt->type != RT_NORMAL)
939 break;
942 SysFreeString(identifier);
943 IDispatchEx_Release(in_obj);
944 if(FAILED(hres)) {
945 VariantClear(&retv);
946 return hres;
949 if(rt->type == RT_BREAK)
950 rt->type = RT_NORMAL;
952 *ret = retv;
953 return S_OK;
956 /* ECMA-262 3rd Edition 12.7 */
957 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
959 branch_statement_t *stat = (branch_statement_t*)_stat;
961 TRACE("\n");
963 if(stat->identifier) {
964 FIXME("indentifier not implemented\n");
965 return E_NOTIMPL;
968 rt->type = RT_CONTINUE;
969 V_VT(ret) = VT_EMPTY;
970 return S_OK;
973 /* ECMA-262 3rd Edition 12.8 */
974 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
976 branch_statement_t *stat = (branch_statement_t*)_stat;
978 TRACE("\n");
980 if(stat->identifier) {
981 FIXME("indentifier not implemented\n");
982 return E_NOTIMPL;
985 rt->type = RT_BREAK;
986 V_VT(ret) = VT_EMPTY;
987 return S_OK;
990 /* ECMA-262 3rd Edition 12.9 */
991 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
993 expression_statement_t *stat = (expression_statement_t*)_stat;
994 HRESULT hres;
996 TRACE("\n");
998 if(stat->expr) {
999 exprval_t exprval;
1001 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1002 if(FAILED(hres))
1003 return hres;
1005 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
1006 exprval_release(&exprval);
1007 if(FAILED(hres))
1008 return hres;
1009 }else {
1010 V_VT(ret) = VT_EMPTY;
1013 TRACE("= %s\n", debugstr_variant(ret));
1014 rt->type = RT_RETURN;
1015 return S_OK;
1018 /* ECMA-262 3rd Edition 12.10 */
1019 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1021 with_statement_t *stat = (with_statement_t*)_stat;
1022 exprval_t exprval;
1023 IDispatch *disp;
1024 DispatchEx *obj;
1025 VARIANT val;
1026 HRESULT hres;
1028 TRACE("\n");
1030 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1031 if(FAILED(hres))
1032 return hres;
1034 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1035 exprval_release(&exprval);
1036 if(FAILED(hres))
1037 return hres;
1039 hres = to_object(ctx->parser->script, &val, &disp);
1040 VariantClear(&val);
1041 if(FAILED(hres))
1042 return hres;
1044 obj = iface_to_jsdisp((IUnknown*)disp);
1045 IDispatch_Release(disp);
1046 if(!obj) {
1047 FIXME("disp id not jsdisp\n");
1048 return E_NOTIMPL;
1051 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
1052 jsdisp_release(obj);
1053 if(FAILED(hres))
1054 return hres;
1056 hres = stat_eval(ctx, stat->statement, rt, ret);
1058 scope_pop(&ctx->scope_chain);
1059 return hres;
1062 /* ECMA-262 3rd Edition 12.12 */
1063 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1065 FIXME("\n");
1066 return E_NOTIMPL;
1069 /* ECMA-262 3rd Edition 12.13 */
1070 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1072 switch_statement_t *stat = (switch_statement_t*)_stat;
1073 case_clausule_t *iter, *default_clausule = NULL;
1074 statement_t *stat_iter;
1075 VARIANT val, cval;
1076 exprval_t exprval;
1077 BOOL b;
1078 HRESULT hres;
1080 TRACE("\n");
1082 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1083 if(FAILED(hres))
1084 return hres;
1086 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1087 exprval_release(&exprval);
1088 if(FAILED(hres))
1089 return hres;
1091 for(iter = stat->case_list; iter; iter = iter->next) {
1092 if(!iter->expr) {
1093 default_clausule = iter;
1094 continue;
1097 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1098 if(FAILED(hres))
1099 break;
1101 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
1102 exprval_release(&exprval);
1103 if(FAILED(hres))
1104 break;
1106 hres = equal2_values(&val, &cval, &b);
1107 VariantClear(&cval);
1108 if(FAILED(hres) || b)
1109 break;
1112 VariantClear(&val);
1113 if(FAILED(hres))
1114 return hres;
1116 if(!iter)
1117 iter = default_clausule;
1119 V_VT(&val) = VT_EMPTY;
1120 if(iter) {
1121 VARIANT tmp;
1123 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1124 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1125 if(FAILED(hres))
1126 break;
1128 VariantClear(&val);
1129 val = tmp;
1131 if(rt->type != RT_NORMAL)
1132 break;
1136 if(FAILED(hres)) {
1137 VariantClear(&val);
1138 return hres;
1141 if(rt->type == RT_BREAK)
1142 rt->type = RT_NORMAL;
1144 *ret = val;
1145 return S_OK;
1148 /* ECMA-262 3rd Edition 12.13 */
1149 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1151 expression_statement_t *stat = (expression_statement_t*)_stat;
1152 exprval_t exprval;
1153 VARIANT val;
1154 HRESULT hres;
1156 TRACE("\n");
1158 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1159 if(FAILED(hres))
1160 return hres;
1162 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1163 exprval_release(&exprval);
1164 if(FAILED(hres))
1165 return hres;
1167 rt->ei.var = val;
1168 return DISP_E_EXCEPTION;
1171 /* ECMA-262 3rd Edition 12.14 */
1172 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1174 DispatchEx *var_disp;
1175 VARIANT ex, val;
1176 HRESULT hres;
1178 ex = rt->ei.var;
1179 memset(&rt->ei, 0, sizeof(jsexcept_t));
1181 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
1182 if(SUCCEEDED(hres)) {
1183 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1184 if(SUCCEEDED(hres)) {
1185 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
1186 if(SUCCEEDED(hres)) {
1187 hres = stat_eval(ctx, block->statement, rt, &val);
1188 scope_pop(&ctx->scope_chain);
1192 jsdisp_release(var_disp);
1195 VariantClear(&ex);
1196 if(FAILED(hres))
1197 return hres;
1199 *ret = val;
1200 return S_OK;
1203 /* ECMA-262 3rd Edition 12.14 */
1204 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1206 try_statement_t *stat = (try_statement_t*)_stat;
1207 VARIANT val;
1208 HRESULT hres;
1210 TRACE("\n");
1212 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1213 if(FAILED(hres)) {
1214 TRACE("EXCEPTION\n");
1215 if(!stat->catch_block)
1216 return hres;
1218 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1219 if(FAILED(hres))
1220 return hres;
1223 if(stat->finally_statement) {
1224 VariantClear(&val);
1225 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1226 if(FAILED(hres))
1227 return hres;
1230 *ret = val;
1231 return S_OK;
1234 static HRESULT return_bool(exprval_t *ret, DWORD b)
1236 ret->type = EXPRVAL_VARIANT;
1237 V_VT(&ret->u.var) = VT_BOOL;
1238 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1240 return S_OK;
1243 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1245 exprval_t exprval;
1246 HRESULT hres;
1248 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1249 if(FAILED(hres))
1250 return hres;
1252 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
1253 exprval_release(&exprval);
1254 if(FAILED(hres))
1255 return hres;
1257 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1258 if(SUCCEEDED(hres)) {
1259 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
1260 exprval_release(&exprval);
1263 if(FAILED(hres)) {
1264 VariantClear(lval);
1265 return hres;
1268 return S_OK;
1271 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1273 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1274 exprval_t *ret)
1276 VARIANT lval, rval, retv;
1277 HRESULT hres;
1279 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1280 if(FAILED(hres))
1281 return hres;
1283 hres = oper(ctx, &lval, &rval, ei, &retv);
1284 VariantClear(&lval);
1285 VariantClear(&rval);
1286 if(FAILED(hres))
1287 return hres;
1289 ret->type = EXPRVAL_VARIANT;
1290 ret->u.var = retv;
1291 return S_OK;
1294 /* ECMA-262 3rd Edition 11.13.2 */
1295 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1296 jsexcept_t *ei, exprval_t *ret)
1298 VARIANT retv, lval, rval;
1299 exprval_t exprval, exprvalr;
1300 HRESULT hres;
1302 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1303 if(FAILED(hres))
1304 return hres;
1306 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
1307 if(SUCCEEDED(hres)) {
1308 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1309 if(SUCCEEDED(hres)) {
1310 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
1311 exprval_release(&exprvalr);
1313 if(SUCCEEDED(hres)) {
1314 hres = oper(ctx, &lval, &rval, ei, &retv);
1315 VariantClear(&rval);
1317 VariantClear(&lval);
1320 if(SUCCEEDED(hres)) {
1321 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
1322 if(FAILED(hres))
1323 VariantClear(&retv);
1325 exprval_release(&exprval);
1327 if(FAILED(hres))
1328 return hres;
1330 ret->type = EXPRVAL_VARIANT;
1331 ret->u.var = retv;
1332 return S_OK;
1335 /* ECMA-262 3rd Edition 13 */
1336 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1338 function_expression_t *expr = (function_expression_t*)_expr;
1339 VARIANT var;
1340 HRESULT hres;
1342 TRACE("\n");
1344 if(expr->identifier) {
1345 hres = jsdisp_propget_name(ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1346 if(FAILED(hres))
1347 return hres;
1348 }else {
1349 DispatchEx *dispex;
1351 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1352 expr->src_str, expr->src_len, &dispex);
1353 if(FAILED(hres))
1354 return hres;
1356 V_VT(&var) = VT_DISPATCH;
1357 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1360 ret->type = EXPRVAL_VARIANT;
1361 ret->u.var = var;
1362 return S_OK;
1365 /* ECMA-262 3rd Edition 11.12 */
1366 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1368 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1369 exprval_t exprval;
1370 VARIANT_BOOL b;
1371 HRESULT hres;
1373 TRACE("\n");
1375 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1376 if(FAILED(hres))
1377 return hres;
1379 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1380 exprval_release(&exprval);
1381 if(FAILED(hres))
1382 return hres;
1384 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1387 /* ECMA-262 3rd Edition 11.2.1 */
1388 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1390 array_expression_t *expr = (array_expression_t*)_expr;
1391 exprval_t exprval;
1392 VARIANT member, val;
1393 DISPID id;
1394 BSTR str;
1395 IDispatch *obj = NULL;
1396 HRESULT hres;
1398 TRACE("\n");
1400 hres = expr_eval(ctx, expr->member_expr, 0, ei, &exprval);
1401 if(FAILED(hres))
1402 return hres;
1404 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1405 exprval_release(&exprval);
1406 if(FAILED(hres))
1407 return hres;
1409 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1410 if(SUCCEEDED(hres)) {
1411 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1412 exprval_release(&exprval);
1415 if(SUCCEEDED(hres)) {
1416 hres = to_object(ctx->parser->script, &member, &obj);
1417 if(FAILED(hres))
1418 VariantClear(&val);
1420 VariantClear(&member);
1421 if(SUCCEEDED(hres)) {
1422 hres = to_string(ctx->parser->script, &val, ei, &str);
1423 VariantClear(&val);
1424 if(SUCCEEDED(hres)) {
1425 if(flags & EXPR_STRREF) {
1426 ret->type = EXPRVAL_NAMEREF;
1427 ret->u.nameref.disp = obj;
1428 ret->u.nameref.name = str;
1429 return S_OK;
1432 hres = disp_get_id(ctx->parser->script, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1433 SysFreeString(str);
1436 if(SUCCEEDED(hres)) {
1437 exprval_set_idref(ret, obj, id);
1438 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1439 exprval_init(ret);
1440 hres = S_OK;
1443 IDispatch_Release(obj);
1446 return hres;
1449 /* ECMA-262 3rd Edition 11.2.1 */
1450 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1452 member_expression_t *expr = (member_expression_t*)_expr;
1453 IDispatch *obj = NULL;
1454 exprval_t exprval;
1455 VARIANT member;
1456 DISPID id;
1457 BSTR str;
1458 HRESULT hres;
1460 TRACE("\n");
1462 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1463 if(FAILED(hres))
1464 return hres;
1466 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1467 exprval_release(&exprval);
1468 if(FAILED(hres))
1469 return hres;
1471 hres = to_object(ctx->parser->script, &member, &obj);
1472 VariantClear(&member);
1473 if(FAILED(hres))
1474 return hres;
1476 str = SysAllocString(expr->identifier);
1477 if(flags & EXPR_STRREF) {
1478 ret->type = EXPRVAL_NAMEREF;
1479 ret->u.nameref.disp = obj;
1480 ret->u.nameref.name = str;
1481 return S_OK;
1484 hres = disp_get_id(ctx->parser->script, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1485 SysFreeString(str);
1486 if(SUCCEEDED(hres)) {
1487 exprval_set_idref(ret, obj, id);
1488 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1489 exprval_init(ret);
1490 hres = S_OK;
1493 IDispatch_Release(obj);
1494 return hres;
1497 static void free_dp(DISPPARAMS *dp)
1499 DWORD i;
1501 for(i=0; i < dp->cArgs; i++)
1502 VariantClear(dp->rgvarg+i);
1503 heap_free(dp->rgvarg);
1506 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1508 VARIANTARG *vargs;
1509 exprval_t exprval;
1510 argument_t *iter;
1511 DWORD cnt = 0, i;
1512 HRESULT hres = S_OK;
1514 memset(dp, 0, sizeof(*dp));
1515 if(!args)
1516 return S_OK;
1518 for(iter = args; iter; iter = iter->next)
1519 cnt++;
1521 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1522 if(!vargs)
1523 return E_OUTOFMEMORY;
1525 for(i = cnt, iter = args; iter; iter = iter->next) {
1526 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1527 if(FAILED(hres))
1528 break;
1530 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1531 exprval_release(&exprval);
1532 if(FAILED(hres))
1533 break;
1536 if(FAILED(hres)) {
1537 free_dp(dp);
1538 return hres;
1541 dp->rgvarg = vargs;
1542 dp->cArgs = cnt;
1543 return S_OK;
1546 /* ECMA-262 3rd Edition 11.2.2 */
1547 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1549 call_expression_t *expr = (call_expression_t*)_expr;
1550 exprval_t exprval;
1551 VARIANT constr, var;
1552 DISPPARAMS dp;
1553 HRESULT hres;
1555 TRACE("\n");
1557 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1558 if(FAILED(hres))
1559 return hres;
1561 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1562 if(SUCCEEDED(hres))
1563 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1564 exprval_release(&exprval);
1565 if(FAILED(hres))
1566 return hres;
1568 if(V_VT(&constr) != VT_DISPATCH) {
1569 FIXME("throw TypeError\n");
1570 VariantClear(&constr);
1571 return E_FAIL;
1574 hres = disp_call(ctx->parser->script, V_DISPATCH(&constr), DISPID_VALUE,
1575 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1576 IDispatch_Release(V_DISPATCH(&constr));
1577 free_dp(&dp);
1578 if(FAILED(hres))
1579 return hres;
1581 ret->type = EXPRVAL_VARIANT;
1582 ret->u.var = var;
1583 return S_OK;
1586 /* ECMA-262 3rd Edition 11.2.3 */
1587 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1589 call_expression_t *expr = (call_expression_t*)_expr;
1590 VARIANT var;
1591 exprval_t exprval;
1592 DISPPARAMS dp;
1593 HRESULT hres;
1595 TRACE("\n");
1597 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1598 if(FAILED(hres))
1599 return hres;
1601 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1602 if(SUCCEEDED(hres)) {
1603 switch(exprval.type) {
1604 case EXPRVAL_VARIANT:
1605 if(V_VT(&exprval.u.var) == VT_DISPATCH)
1606 hres = disp_call(ctx->parser->script, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1607 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1608 else
1609 hres = throw_type_error(ctx->parser->script, ei, IDS_NO_PROPERTY, NULL);
1610 break;
1611 case EXPRVAL_IDREF:
1612 hres = disp_call(ctx->parser->script, exprval.u.idref.disp, exprval.u.idref.id,
1613 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1614 break;
1615 case EXPRVAL_INVALID:
1616 hres = throw_type_error(ctx->parser->script, ei, IDS_OBJECT_EXPECTED, NULL);
1617 break;
1618 default:
1619 FIXME("unimplemented type %d\n", exprval.type);
1620 hres = E_NOTIMPL;
1623 free_dp(&dp);
1626 exprval_release(&exprval);
1627 if(FAILED(hres))
1628 return hres;
1630 ret->type = EXPRVAL_VARIANT;
1631 if(flags & EXPR_NOVAL) {
1632 V_VT(&ret->u.var) = VT_EMPTY;
1633 }else {
1634 TRACE("= %s\n", debugstr_variant(&var));
1635 ret->u.var = var;
1637 return S_OK;
1640 /* ECMA-262 3rd Edition 11.1.1 */
1641 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1643 TRACE("\n");
1645 ret->type = EXPRVAL_VARIANT;
1646 V_VT(&ret->u.var) = VT_DISPATCH;
1647 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1648 IDispatch_AddRef(ctx->this_obj);
1649 return S_OK;
1652 /* ECMA-262 3rd Edition 10.1.4 */
1653 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1655 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1656 BSTR identifier;
1657 HRESULT hres;
1659 TRACE("\n");
1661 identifier = SysAllocString(expr->identifier);
1662 if(!identifier)
1663 return E_OUTOFMEMORY;
1665 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1667 SysFreeString(identifier);
1668 return hres;
1671 /* ECMA-262 3rd Edition 7.8 */
1672 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1674 literal_expression_t *expr = (literal_expression_t*)_expr;
1675 VARIANT var;
1676 HRESULT hres;
1678 TRACE("\n");
1680 hres = literal_to_var(ctx->parser->script, expr->literal, &var);
1681 if(FAILED(hres))
1682 return hres;
1684 ret->type = EXPRVAL_VARIANT;
1685 ret->u.var = var;
1686 return S_OK;
1689 /* ECMA-262 3rd Edition 11.1.4 */
1690 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1692 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1693 DWORD length = 0, i = 0;
1694 array_element_t *elem;
1695 DispatchEx *array;
1696 exprval_t exprval;
1697 VARIANT val;
1698 HRESULT hres;
1700 TRACE("\n");
1702 for(elem = expr->element_list; elem; elem = elem->next)
1703 length += elem->elision+1;
1704 length += expr->length;
1706 hres = create_array(ctx->parser->script, length, &array);
1707 if(FAILED(hres))
1708 return hres;
1710 for(elem = expr->element_list; elem; elem = elem->next) {
1711 i += elem->elision;
1713 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1714 if(FAILED(hres))
1715 break;
1717 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1718 exprval_release(&exprval);
1719 if(FAILED(hres))
1720 break;
1722 hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
1723 VariantClear(&val);
1724 if(FAILED(hres))
1725 break;
1727 i++;
1730 if(FAILED(hres)) {
1731 jsdisp_release(array);
1732 return hres;
1735 ret->type = EXPRVAL_VARIANT;
1736 V_VT(&ret->u.var) = VT_DISPATCH;
1737 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(array);
1738 return S_OK;
1741 /* ECMA-262 3rd Edition 11.1.5 */
1742 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1744 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1745 VARIANT val, tmp;
1746 DispatchEx *obj;
1747 prop_val_t *iter;
1748 exprval_t exprval;
1749 BSTR name;
1750 HRESULT hres;
1752 TRACE("\n");
1754 hres = create_object(ctx->parser->script, NULL, &obj);
1755 if(FAILED(hres))
1756 return hres;
1758 for(iter = expr->property_list; iter; iter = iter->next) {
1759 hres = literal_to_var(ctx->parser->script, iter->name, &tmp);
1760 if(FAILED(hres))
1761 break;
1763 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1764 VariantClear(&tmp);
1765 if(FAILED(hres))
1766 break;
1768 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1769 if(SUCCEEDED(hres)) {
1770 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1771 exprval_release(&exprval);
1772 if(SUCCEEDED(hres)) {
1773 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
1774 VariantClear(&val);
1778 SysFreeString(name);
1779 if(FAILED(hres))
1780 break;
1783 if(FAILED(hres)) {
1784 jsdisp_release(obj);
1785 return hres;
1788 ret->type = EXPRVAL_VARIANT;
1789 V_VT(&ret->u.var) = VT_DISPATCH;
1790 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1791 return S_OK;
1794 /* ECMA-262 3rd Edition 11.14 */
1795 HRESULT comma_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;
1798 VARIANT lval, rval;
1799 HRESULT hres;
1801 TRACE("\n");
1803 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1804 if(FAILED(hres))
1805 return hres;
1807 VariantClear(&lval);
1809 ret->type = EXPRVAL_VARIANT;
1810 ret->u.var = rval;
1811 return S_OK;
1814 /* ECMA-262 3rd Edition 11.11 */
1815 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1817 binary_expression_t *expr = (binary_expression_t*)_expr;
1818 exprval_t exprval;
1819 VARIANT_BOOL b;
1820 VARIANT val;
1821 HRESULT hres;
1823 TRACE("\n");
1825 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1826 if(FAILED(hres))
1827 return hres;
1829 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1830 exprval_release(&exprval);
1831 if(FAILED(hres))
1832 return hres;
1834 hres = to_boolean(&val, &b);
1835 if(SUCCEEDED(hres) && b) {
1836 ret->type = EXPRVAL_VARIANT;
1837 ret->u.var = val;
1838 return S_OK;
1841 VariantClear(&val);
1842 if(FAILED(hres))
1843 return hres;
1845 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1846 if(FAILED(hres))
1847 return hres;
1849 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1850 exprval_release(&exprval);
1851 if(FAILED(hres))
1852 return hres;
1854 ret->type = EXPRVAL_VARIANT;
1855 ret->u.var = val;
1856 return S_OK;
1859 /* ECMA-262 3rd Edition 11.11 */
1860 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1862 binary_expression_t *expr = (binary_expression_t*)_expr;
1863 exprval_t exprval;
1864 VARIANT_BOOL b;
1865 VARIANT val;
1866 HRESULT hres;
1868 TRACE("\n");
1870 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1871 if(FAILED(hres))
1872 return hres;
1874 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1875 exprval_release(&exprval);
1876 if(FAILED(hres))
1877 return hres;
1879 hres = to_boolean(&val, &b);
1880 if(SUCCEEDED(hres) && !b) {
1881 ret->type = EXPRVAL_VARIANT;
1882 ret->u.var = val;
1883 return S_OK;
1886 VariantClear(&val);
1887 if(FAILED(hres))
1888 return hres;
1890 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1891 if(FAILED(hres))
1892 return hres;
1894 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1895 exprval_release(&exprval);
1896 if(FAILED(hres))
1897 return hres;
1899 ret->type = EXPRVAL_VARIANT;
1900 ret->u.var = val;
1901 return S_OK;
1904 /* ECMA-262 3rd Edition 11.10 */
1905 static HRESULT bitor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1907 INT li, ri;
1908 HRESULT hres;
1910 hres = to_int32(ctx->parser->script, lval, ei, &li);
1911 if(FAILED(hres))
1912 return hres;
1914 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1915 if(FAILED(hres))
1916 return hres;
1918 V_VT(retv) = VT_I4;
1919 V_I4(retv) = li|ri;
1920 return S_OK;
1923 /* ECMA-262 3rd Edition 11.10 */
1924 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1926 binary_expression_t *expr = (binary_expression_t*)_expr;
1928 TRACE("\n");
1930 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
1933 /* ECMA-262 3rd Edition 11.10 */
1934 static HRESULT xor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1936 INT li, ri;
1937 HRESULT hres;
1939 hres = to_int32(ctx->parser->script, lval, ei, &li);
1940 if(FAILED(hres))
1941 return hres;
1943 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1944 if(FAILED(hres))
1945 return hres;
1947 V_VT(retv) = VT_I4;
1948 V_I4(retv) = li^ri;
1949 return S_OK;
1952 /* ECMA-262 3rd Edition 11.10 */
1953 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1955 binary_expression_t *expr = (binary_expression_t*)_expr;
1957 TRACE("\n");
1959 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
1962 /* ECMA-262 3rd Edition 11.10 */
1963 static HRESULT bitand_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1965 INT li, ri;
1966 HRESULT hres;
1968 hres = to_int32(ctx->parser->script, lval, ei, &li);
1969 if(FAILED(hres))
1970 return hres;
1972 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1973 if(FAILED(hres))
1974 return hres;
1976 V_VT(retv) = VT_I4;
1977 V_I4(retv) = li&ri;
1978 return S_OK;
1981 /* ECMA-262 3rd Edition 11.10 */
1982 HRESULT binary_and_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;
1986 TRACE("\n");
1988 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
1991 /* ECMA-262 3rd Edition 11.8.6 */
1992 static HRESULT instanceof_eval(exec_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
1994 DispatchEx *obj, *iter, *tmp = NULL;
1995 VARIANT_BOOL ret = VARIANT_FALSE;
1996 BOOL b;
1997 VARIANT var;
1998 HRESULT hres;
2000 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2002 if(V_VT(objv) != VT_DISPATCH) {
2003 FIXME("throw TypeError\n");
2004 return E_FAIL;
2007 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2008 if(!obj) {
2009 FIXME("throw TypeError\n");
2010 return E_FAIL;
2013 if(is_class(obj, JSCLASS_FUNCTION)) {
2014 hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2015 }else {
2016 FIXME("throw TypeError\n");
2017 hres = E_FAIL;
2019 jsdisp_release(obj);
2020 if(FAILED(hres))
2021 return hres;
2023 if(V_VT(&var) == VT_DISPATCH) {
2024 if(V_VT(inst) == VT_DISPATCH)
2025 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2026 for(iter = tmp; iter; iter = iter->prototype) {
2027 hres = disp_cmp(V_DISPATCH(&var), (IDispatch*)_IDispatchEx_(iter), &b);
2028 if(FAILED(hres))
2029 break;
2030 if(b) {
2031 ret = VARIANT_TRUE;
2032 break;
2036 if(tmp)
2037 jsdisp_release(tmp);
2038 }else {
2039 FIXME("prototype is not an object\n");
2040 hres = E_FAIL;
2043 VariantClear(&var);
2044 if(FAILED(hres))
2045 return hres;
2047 V_VT(retv) = VT_BOOL;
2048 V_BOOL(retv) = ret;
2049 return S_OK;
2052 /* ECMA-262 3rd Edition 11.8.6 */
2053 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2055 binary_expression_t *expr = (binary_expression_t*)_expr;
2057 TRACE("\n");
2059 return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2062 /* ECMA-262 3rd Edition 11.8.7 */
2063 static HRESULT in_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *obj, jsexcept_t *ei, VARIANT *retv)
2065 VARIANT_BOOL ret;
2066 DISPID id;
2067 BSTR str;
2068 HRESULT hres;
2070 if(V_VT(obj) != VT_DISPATCH) {
2071 FIXME("throw TypeError\n");
2072 return E_FAIL;
2075 hres = to_string(ctx->parser->script, lval, ei, &str);
2076 if(FAILED(hres))
2077 return hres;
2079 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2080 SysFreeString(str);
2081 if(SUCCEEDED(hres))
2082 ret = VARIANT_TRUE;
2083 else if(hres == DISP_E_UNKNOWNNAME)
2084 ret = VARIANT_FALSE;
2085 else
2086 return hres;
2088 V_VT(retv) = VT_BOOL;
2089 V_BOOL(retv) = ret;
2090 return S_OK;
2093 /* ECMA-262 3rd Edition 11.8.7 */
2094 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2096 binary_expression_t *expr = (binary_expression_t*)_expr;
2098 TRACE("\n");
2100 return binary_expr_eval(ctx, expr, in_eval, ei, ret);
2103 /* ECMA-262 3rd Edition 11.6.1 */
2104 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2106 VARIANT r, l;
2107 HRESULT hres;
2109 hres = to_primitive(ctx->parser->script, lval, ei, &l, NO_HINT);
2110 if(FAILED(hres))
2111 return hres;
2113 hres = to_primitive(ctx->parser->script, rval, ei, &r, NO_HINT);
2114 if(FAILED(hres)) {
2115 VariantClear(&l);
2116 return hres;
2119 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2120 BSTR lstr = NULL, rstr = NULL;
2122 if(V_VT(&l) == VT_BSTR)
2123 lstr = V_BSTR(&l);
2124 else
2125 hres = to_string(ctx->parser->script, &l, ei, &lstr);
2127 if(SUCCEEDED(hres)) {
2128 if(V_VT(&r) == VT_BSTR)
2129 rstr = V_BSTR(&r);
2130 else
2131 hres = to_string(ctx->parser->script, &r, ei, &rstr);
2134 if(SUCCEEDED(hres)) {
2135 int len1, len2;
2137 len1 = SysStringLen(lstr);
2138 len2 = SysStringLen(rstr);
2140 V_VT(retv) = VT_BSTR;
2141 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2142 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2143 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2146 if(V_VT(&l) != VT_BSTR)
2147 SysFreeString(lstr);
2148 if(V_VT(&r) != VT_BSTR)
2149 SysFreeString(rstr);
2150 }else {
2151 VARIANT nl, nr;
2153 hres = to_number(ctx->parser->script, &l, ei, &nl);
2154 if(SUCCEEDED(hres)) {
2155 hres = to_number(ctx->parser->script, &r, ei, &nr);
2156 if(SUCCEEDED(hres))
2157 num_set_val(retv, num_val(&nl) + num_val(&nr));
2161 VariantClear(&r);
2162 VariantClear(&l);
2163 return hres;
2166 /* ECMA-262 3rd Edition 11.6.1 */
2167 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2169 binary_expression_t *expr = (binary_expression_t*)_expr;
2171 TRACE("\n");
2173 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
2176 /* ECMA-262 3rd Edition 11.6.2 */
2177 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2179 VARIANT lnum, rnum;
2180 HRESULT hres;
2182 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2183 if(FAILED(hres))
2184 return hres;
2186 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2187 if(FAILED(hres))
2188 return hres;
2190 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
2191 return S_OK;
2194 /* ECMA-262 3rd Edition 11.6.2 */
2195 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2197 binary_expression_t *expr = (binary_expression_t*)_expr;
2199 TRACE("\n");
2201 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
2204 /* ECMA-262 3rd Edition 11.5.1 */
2205 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2207 VARIANT lnum, rnum;
2208 HRESULT hres;
2210 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2211 if(FAILED(hres))
2212 return hres;
2214 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2215 if(FAILED(hres))
2216 return hres;
2218 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2219 return S_OK;
2222 /* ECMA-262 3rd Edition 11.5.1 */
2223 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2225 binary_expression_t *expr = (binary_expression_t*)_expr;
2227 TRACE("\n");
2229 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2232 /* ECMA-262 3rd Edition 11.5.2 */
2233 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2235 VARIANT lnum, rnum;
2236 HRESULT hres;
2238 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2239 if(FAILED(hres))
2240 return hres;
2242 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2243 if(FAILED(hres))
2244 return hres;
2246 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2247 return S_OK;
2250 /* ECMA-262 3rd Edition 11.5.2 */
2251 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2253 binary_expression_t *expr = (binary_expression_t*)_expr;
2255 TRACE("\n");
2257 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2260 /* ECMA-262 3rd Edition 11.5.3 */
2261 static HRESULT mod_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2263 VARIANT lnum, rnum;
2264 HRESULT hres;
2266 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2267 if(FAILED(hres))
2268 return hres;
2270 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2271 if(FAILED(hres))
2272 return hres;
2274 num_set_val(retv, fmod(num_val(&lnum), num_val(&rnum)));
2275 return S_OK;
2278 /* ECMA-262 3rd Edition 11.5.3 */
2279 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2281 binary_expression_t *expr = (binary_expression_t*)_expr;
2283 TRACE("\n");
2285 return binary_expr_eval(ctx, expr, mod_eval, ei, ret);
2288 /* ECMA-262 3rd Edition 11.4.2 */
2289 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2291 unary_expression_t *expr = (unary_expression_t*)_expr;
2292 VARIANT_BOOL b = VARIANT_FALSE;
2293 exprval_t exprval;
2294 HRESULT hres;
2296 TRACE("\n");
2298 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2299 if(FAILED(hres))
2300 return hres;
2302 switch(exprval.type) {
2303 case EXPRVAL_IDREF: {
2304 IDispatchEx *dispex;
2306 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2307 if(SUCCEEDED(hres)) {
2308 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2309 b = VARIANT_TRUE;
2310 IDispatchEx_Release(dispex);
2312 break;
2314 case EXPRVAL_NAMEREF: {
2315 IDispatchEx *dispex;
2317 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2318 if(SUCCEEDED(hres)) {
2319 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name,
2320 make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2321 b = VARIANT_TRUE;
2322 IDispatchEx_Release(dispex);
2324 break;
2326 default:
2327 FIXME("unsupported type %d\n", exprval.type);
2328 hres = E_NOTIMPL;
2331 exprval_release(&exprval);
2332 if(FAILED(hres))
2333 return hres;
2335 return return_bool(ret, b);
2338 /* ECMA-262 3rd Edition 11.4.2 */
2339 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2341 unary_expression_t *expr = (unary_expression_t*)_expr;
2342 exprval_t exprval;
2343 VARIANT tmp;
2344 HRESULT hres;
2346 TRACE("\n");
2348 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2349 if(FAILED(hres))
2350 return hres;
2352 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &tmp);
2353 exprval_release(&exprval);
2354 if(FAILED(hres))
2355 return hres;
2357 VariantClear(&tmp);
2359 ret->type = EXPRVAL_VARIANT;
2360 V_VT(&ret->u.var) = VT_EMPTY;
2361 return S_OK;
2364 /* ECMA-262 3rd Edition 11.4.3 */
2365 static HRESULT typeof_exprval(exec_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2367 VARIANT val;
2368 HRESULT hres;
2370 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2371 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2372 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2373 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2374 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2375 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2377 if(exprval->type == EXPRVAL_INVALID) {
2378 *ret = undefinedW;
2379 return S_OK;
2382 hres = exprval_to_value(ctx->parser->script, exprval, ei, &val);
2383 if(FAILED(hres))
2384 return hres;
2386 switch(V_VT(&val)) {
2387 case VT_EMPTY:
2388 *ret = undefinedW;
2389 break;
2390 case VT_NULL:
2391 *ret = objectW;
2392 break;
2393 case VT_BOOL:
2394 *ret = booleanW;
2395 break;
2396 case VT_I4:
2397 case VT_R8:
2398 *ret = numberW;
2399 break;
2400 case VT_BSTR:
2401 *ret = stringW;
2402 break;
2403 case VT_DISPATCH: {
2404 DispatchEx *dispex;
2406 if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2407 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2408 jsdisp_release(dispex);
2409 }else {
2410 *ret = objectW;
2412 break;
2414 default:
2415 FIXME("unhandled vt %d\n", V_VT(&val));
2416 hres = E_NOTIMPL;
2419 VariantClear(&val);
2420 return S_OK;
2423 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2425 unary_expression_t *expr = (unary_expression_t*)_expr;
2426 const WCHAR *str = NULL;
2427 exprval_t exprval;
2428 HRESULT hres;
2430 TRACE("\n");
2432 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2433 if(FAILED(hres))
2434 return hres;
2436 hres = typeof_exprval(ctx, &exprval, ei, &str);
2437 exprval_release(&exprval);
2438 if(FAILED(hres))
2439 return hres;
2441 ret->type = EXPRVAL_VARIANT;
2442 V_VT(&ret->u.var) = VT_BSTR;
2443 V_BSTR(&ret->u.var) = SysAllocString(str);
2444 if(!V_BSTR(&ret->u.var))
2445 return E_OUTOFMEMORY;
2447 return S_OK;
2450 /* ECMA-262 3rd Edition 11.4.7 */
2451 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2453 unary_expression_t *expr = (unary_expression_t*)_expr;
2454 exprval_t exprval;
2455 VARIANT val, num;
2456 HRESULT hres;
2458 TRACE("\n");
2460 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2461 if(FAILED(hres))
2462 return hres;
2464 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2465 exprval_release(&exprval);
2466 if(FAILED(hres))
2467 return hres;
2469 hres = to_number(ctx->parser->script, &val, ei, &num);
2470 VariantClear(&val);
2471 if(FAILED(hres))
2472 return hres;
2474 ret->type = EXPRVAL_VARIANT;
2475 num_set_val(&ret->u.var, -num_val(&num));
2476 return S_OK;
2479 /* ECMA-262 3rd Edition 11.4.6 */
2480 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2482 unary_expression_t *expr = (unary_expression_t*)_expr;
2483 exprval_t exprval;
2484 VARIANT val, num;
2485 HRESULT hres;
2487 TRACE("\n");
2489 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2490 if(FAILED(hres))
2491 return hres;
2493 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2494 exprval_release(&exprval);
2495 if(FAILED(hres))
2496 return hres;
2498 hres = to_number(ctx->parser->script, &val, ei, &num);
2499 VariantClear(&val);
2500 if(FAILED(hres))
2501 return hres;
2503 ret->type = EXPRVAL_VARIANT;
2504 ret->u.var = num;
2505 return S_OK;
2508 /* ECMA-262 3rd Edition 11.3.1 */
2509 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2511 unary_expression_t *expr = (unary_expression_t*)_expr;
2512 VARIANT val, num;
2513 exprval_t exprval;
2514 HRESULT hres;
2516 TRACE("\n");
2518 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2519 if(FAILED(hres))
2520 return hres;
2522 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2523 if(SUCCEEDED(hres)) {
2524 hres = to_number(ctx->parser->script, &val, ei, &num);
2525 VariantClear(&val);
2528 if(SUCCEEDED(hres)) {
2529 VARIANT inc;
2530 num_set_val(&inc, num_val(&num)+1.0);
2531 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
2534 exprval_release(&exprval);
2535 if(FAILED(hres))
2536 return hres;
2538 ret->type = EXPRVAL_VARIANT;
2539 ret->u.var = num;
2540 return S_OK;
2543 /* ECMA-262 3rd Edition 11.3.2 */
2544 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2546 unary_expression_t *expr = (unary_expression_t*)_expr;
2547 VARIANT val, num;
2548 exprval_t exprval;
2549 HRESULT hres;
2551 TRACE("\n");
2553 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2554 if(FAILED(hres))
2555 return hres;
2557 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2558 if(SUCCEEDED(hres)) {
2559 hres = to_number(ctx->parser->script, &val, ei, &num);
2560 VariantClear(&val);
2563 if(SUCCEEDED(hres)) {
2564 VARIANT dec;
2565 num_set_val(&dec, num_val(&num)-1.0);
2566 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
2569 exprval_release(&exprval);
2570 if(FAILED(hres))
2571 return hres;
2573 ret->type = EXPRVAL_VARIANT;
2574 ret->u.var = num;
2575 return S_OK;
2578 /* ECMA-262 3rd Edition 11.4.4 */
2579 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2581 unary_expression_t *expr = (unary_expression_t*)_expr;
2582 VARIANT val, num;
2583 exprval_t exprval;
2584 HRESULT hres;
2586 TRACE("\n");
2588 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2589 if(FAILED(hres))
2590 return hres;
2592 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2593 if(SUCCEEDED(hres)) {
2594 hres = to_number(ctx->parser->script, &val, ei, &num);
2595 VariantClear(&val);
2598 if(SUCCEEDED(hres)) {
2599 num_set_val(&val, num_val(&num)+1.0);
2600 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2603 exprval_release(&exprval);
2604 if(FAILED(hres))
2605 return hres;
2607 ret->type = EXPRVAL_VARIANT;
2608 ret->u.var = val;
2609 return S_OK;
2612 /* ECMA-262 3rd Edition 11.4.5 */
2613 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2615 unary_expression_t *expr = (unary_expression_t*)_expr;
2616 VARIANT val, num;
2617 exprval_t exprval;
2618 HRESULT hres;
2620 TRACE("\n");
2622 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2623 if(FAILED(hres))
2624 return hres;
2626 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2627 if(SUCCEEDED(hres)) {
2628 hres = to_number(ctx->parser->script, &val, ei, &num);
2629 VariantClear(&val);
2632 if(SUCCEEDED(hres)) {
2633 num_set_val(&val, num_val(&num)-1.0);
2634 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2637 exprval_release(&exprval);
2638 if(FAILED(hres))
2639 return hres;
2641 ret->type = EXPRVAL_VARIANT;
2642 ret->u.var = val;
2643 return S_OK;
2646 /* ECMA-262 3rd Edition 11.9.3 */
2647 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2649 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2650 return equal2_values(lval, rval, ret);
2652 /* FIXME: NULL disps should be handled in more general way */
2653 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2654 VARIANT v;
2655 V_VT(&v) = VT_NULL;
2656 return equal_values(ctx, &v, rval, ei, ret);
2659 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2660 VARIANT v;
2661 V_VT(&v) = VT_NULL;
2662 return equal_values(ctx, lval, &v, ei, ret);
2665 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2666 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2667 *ret = TRUE;
2668 return S_OK;
2671 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2672 VARIANT v;
2673 HRESULT hres;
2675 hres = to_number(ctx->parser->script, lval, ei, &v);
2676 if(FAILED(hres))
2677 return hres;
2679 return equal_values(ctx, &v, rval, ei, ret);
2682 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2683 VARIANT v;
2684 HRESULT hres;
2686 hres = to_number(ctx->parser->script, rval, ei, &v);
2687 if(FAILED(hres))
2688 return hres;
2690 return equal_values(ctx, lval, &v, ei, ret);
2693 if(V_VT(rval) == VT_BOOL) {
2694 VARIANT v;
2696 V_VT(&v) = VT_I4;
2697 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2698 return equal_values(ctx, lval, &v, ei, ret);
2701 if(V_VT(lval) == VT_BOOL) {
2702 VARIANT v;
2704 V_VT(&v) = VT_I4;
2705 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2706 return equal_values(ctx, &v, rval, ei, ret);
2710 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2711 VARIANT v;
2712 HRESULT hres;
2714 hres = to_primitive(ctx->parser->script, rval, ei, &v, NO_HINT);
2715 if(FAILED(hres))
2716 return hres;
2718 hres = equal_values(ctx, lval, &v, ei, ret);
2720 VariantClear(&v);
2721 return hres;
2725 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2726 VARIANT v;
2727 HRESULT hres;
2729 hres = to_primitive(ctx->parser->script, lval, ei, &v, NO_HINT);
2730 if(FAILED(hres))
2731 return hres;
2733 hres = equal_values(ctx, &v, rval, ei, ret);
2735 VariantClear(&v);
2736 return hres;
2740 *ret = FALSE;
2741 return S_OK;
2744 /* ECMA-262 3rd Edition 11.9.1 */
2745 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2747 binary_expression_t *expr = (binary_expression_t*)_expr;
2748 VARIANT rval, lval;
2749 BOOL b;
2750 HRESULT hres;
2752 TRACE("\n");
2754 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2755 if(FAILED(hres))
2756 return hres;
2758 hres = equal_values(ctx, &rval, &lval, ei, &b);
2759 VariantClear(&lval);
2760 VariantClear(&rval);
2761 if(FAILED(hres))
2762 return hres;
2764 return return_bool(ret, b);
2767 /* ECMA-262 3rd Edition 11.9.4 */
2768 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2770 binary_expression_t *expr = (binary_expression_t*)_expr;
2771 VARIANT rval, lval;
2772 BOOL b;
2773 HRESULT hres;
2775 TRACE("\n");
2777 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2778 if(FAILED(hres))
2779 return hres;
2781 hres = equal2_values(&rval, &lval, &b);
2782 VariantClear(&lval);
2783 VariantClear(&rval);
2784 if(FAILED(hres))
2785 return hres;
2787 return return_bool(ret, b);
2790 /* ECMA-262 3rd Edition 11.9.2 */
2791 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2793 binary_expression_t *expr = (binary_expression_t*)_expr;
2794 VARIANT rval, lval;
2795 BOOL b;
2796 HRESULT hres;
2798 TRACE("\n");
2800 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2801 if(FAILED(hres))
2802 return hres;
2804 hres = equal_values(ctx, &lval, &rval, ei, &b);
2805 VariantClear(&lval);
2806 VariantClear(&rval);
2807 if(FAILED(hres))
2808 return hres;
2810 return return_bool(ret, !b);
2813 /* ECMA-262 3rd Edition 11.9.5 */
2814 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2816 binary_expression_t *expr = (binary_expression_t*)_expr;
2817 VARIANT rval, lval;
2818 BOOL b;
2819 HRESULT hres;
2821 TRACE("\n");
2823 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2824 if(FAILED(hres))
2825 return hres;
2827 hres = equal2_values(&lval, &rval, &b);
2828 VariantClear(&lval);
2829 VariantClear(&rval);
2830 if(FAILED(hres))
2831 return hres;
2833 return return_bool(ret, !b);
2836 /* ECMA-262 3rd Edition 11.8.5 */
2837 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2839 VARIANT l, r, ln, rn;
2840 HRESULT hres;
2842 hres = to_primitive(ctx->parser->script, lval, ei, &l, NO_HINT);
2843 if(FAILED(hres))
2844 return hres;
2846 hres = to_primitive(ctx->parser->script, rval, ei, &r, NO_HINT);
2847 if(FAILED(hres)) {
2848 VariantClear(&l);
2849 return hres;
2852 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2853 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2854 SysFreeString(V_BSTR(&l));
2855 SysFreeString(V_BSTR(&r));
2856 return S_OK;
2859 hres = to_number(ctx->parser->script, &l, ei, &ln);
2860 VariantClear(&l);
2861 if(SUCCEEDED(hres))
2862 hres = to_number(ctx->parser->script, &r, ei, &rn);
2863 VariantClear(&r);
2864 if(FAILED(hres))
2865 return hres;
2867 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2868 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2869 }else {
2870 DOUBLE ld = num_val(&ln);
2871 DOUBLE rd = num_val(&rn);
2873 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2876 return S_OK;
2879 /* ECMA-262 3rd Edition 11.8.1 */
2880 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2882 binary_expression_t *expr = (binary_expression_t*)_expr;
2883 VARIANT rval, lval;
2884 BOOL b;
2885 HRESULT hres;
2887 TRACE("\n");
2889 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2890 if(FAILED(hres))
2891 return hres;
2893 hres = less_eval(ctx, &lval, &rval, FALSE, ei, &b);
2894 VariantClear(&lval);
2895 VariantClear(&rval);
2896 if(FAILED(hres))
2897 return hres;
2899 return return_bool(ret, b);
2902 /* ECMA-262 3rd Edition 11.8.3 */
2903 HRESULT lesseq_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;
2906 VARIANT rval, lval;
2907 BOOL b;
2908 HRESULT hres;
2910 TRACE("\n");
2912 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2913 if(FAILED(hres))
2914 return hres;
2916 hres = less_eval(ctx, &rval, &lval, TRUE, ei, &b);
2917 VariantClear(&lval);
2918 VariantClear(&rval);
2919 if(FAILED(hres))
2920 return hres;
2922 return return_bool(ret, b);
2925 /* ECMA-262 3rd Edition 11.8.2 */
2926 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2928 binary_expression_t *expr = (binary_expression_t*)_expr;
2929 VARIANT rval, lval;
2930 BOOL b;
2931 HRESULT hres;
2933 TRACE("\n");
2935 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2936 if(FAILED(hres))
2937 return hres;
2939 hres = less_eval(ctx, &rval, &lval, FALSE, ei, &b);
2940 VariantClear(&lval);
2941 VariantClear(&rval);
2942 if(FAILED(hres))
2943 return hres;
2945 return return_bool(ret, b);
2948 /* ECMA-262 3rd Edition 11.8.4 */
2949 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2951 binary_expression_t *expr = (binary_expression_t*)_expr;
2952 VARIANT rval, lval;
2953 BOOL b;
2954 HRESULT hres;
2956 TRACE("\n");
2958 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2959 if(FAILED(hres))
2960 return hres;
2962 hres = less_eval(ctx, &lval, &rval, TRUE, ei, &b);
2963 VariantClear(&lval);
2964 VariantClear(&rval);
2965 if(FAILED(hres))
2966 return hres;
2968 return return_bool(ret, b);
2971 /* ECMA-262 3rd Edition 11.4.8 */
2972 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2974 unary_expression_t *expr = (unary_expression_t*)_expr;
2975 exprval_t exprval;
2976 VARIANT val;
2977 INT i;
2978 HRESULT hres;
2980 TRACE("\n");
2982 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2983 if(FAILED(hres))
2984 return hres;
2986 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2987 exprval_release(&exprval);
2988 if(FAILED(hres))
2989 return hres;
2991 hres = to_int32(ctx->parser->script, &val, ei, &i);
2992 if(FAILED(hres))
2993 return hres;
2995 ret->type = EXPRVAL_VARIANT;
2996 V_VT(&ret->u.var) = VT_I4;
2997 V_I4(&ret->u.var) = ~i;
2998 return S_OK;
3001 /* ECMA-262 3rd Edition 11.4.9 */
3002 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3004 unary_expression_t *expr = (unary_expression_t*)_expr;
3005 exprval_t exprval;
3006 VARIANT_BOOL b;
3007 HRESULT hres;
3009 TRACE("\n");
3011 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
3012 if(FAILED(hres))
3013 return hres;
3015 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
3016 exprval_release(&exprval);
3017 if(FAILED(hres))
3018 return hres;
3020 return return_bool(ret, !b);
3023 /* ECMA-262 3rd Edition 11.7.1 */
3024 static HRESULT lshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3026 DWORD ri;
3027 INT li;
3028 HRESULT hres;
3030 hres = to_int32(ctx->parser->script, lval, ei, &li);
3031 if(FAILED(hres))
3032 return hres;
3034 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3035 if(FAILED(hres))
3036 return hres;
3038 V_VT(retv) = VT_I4;
3039 V_I4(retv) = li << (ri&0x1f);
3040 return S_OK;
3043 /* ECMA-262 3rd Edition 11.7.1 */
3044 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3046 binary_expression_t *expr = (binary_expression_t*)_expr;
3048 TRACE("\n");
3050 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3053 /* ECMA-262 3rd Edition 11.7.2 */
3054 static HRESULT rshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3056 DWORD ri;
3057 INT li;
3058 HRESULT hres;
3060 hres = to_int32(ctx->parser->script, lval, ei, &li);
3061 if(FAILED(hres))
3062 return hres;
3064 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3065 if(FAILED(hres))
3066 return hres;
3068 V_VT(retv) = VT_I4;
3069 V_I4(retv) = li >> (ri&0x1f);
3070 return S_OK;
3073 /* ECMA-262 3rd Edition 11.7.2 */
3074 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3076 binary_expression_t *expr = (binary_expression_t*)_expr;
3078 TRACE("\n");
3080 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3083 /* ECMA-262 3rd Edition 11.7.3 */
3084 static HRESULT rshift2_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3086 DWORD li, ri;
3087 HRESULT hres;
3089 hres = to_uint32(ctx->parser->script, lval, ei, &li);
3090 if(FAILED(hres))
3091 return hres;
3093 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3094 if(FAILED(hres))
3095 return hres;
3097 V_VT(retv) = VT_I4;
3098 V_I4(retv) = li >> (ri&0x1f);
3099 return S_OK;
3102 /* ECMA-262 3rd Edition 11.7.3 */
3103 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3105 binary_expression_t *expr = (binary_expression_t*)_expr;
3107 TRACE("\n");
3109 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3112 /* ECMA-262 3rd Edition 11.13.1 */
3113 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3115 binary_expression_t *expr = (binary_expression_t*)_expr;
3116 exprval_t exprval, exprvalr;
3117 VARIANT rval;
3118 HRESULT hres;
3120 TRACE("\n");
3122 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
3123 if(FAILED(hres))
3124 return hres;
3126 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
3127 if(SUCCEEDED(hres)) {
3128 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
3129 exprval_release(&exprvalr);
3132 if(SUCCEEDED(hres)) {
3133 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
3134 if(FAILED(hres))
3135 VariantClear(&rval);
3138 exprval_release(&exprval);
3139 if(FAILED(hres))
3140 return hres;
3142 ret->type = EXPRVAL_VARIANT;
3143 ret->u.var = rval;
3144 return S_OK;
3147 /* ECMA-262 3rd Edition 11.13.2 */
3148 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3150 binary_expression_t *expr = (binary_expression_t*)_expr;
3152 TRACE("\n");
3154 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3157 /* ECMA-262 3rd Edition 11.13.2 */
3158 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3160 binary_expression_t *expr = (binary_expression_t*)_expr;
3162 TRACE("\n");
3164 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3167 /* ECMA-262 3rd Edition 11.13.2 */
3168 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3170 binary_expression_t *expr = (binary_expression_t*)_expr;
3172 TRACE("\n");
3174 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3177 /* ECMA-262 3rd Edition 11.13.2 */
3178 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3180 binary_expression_t *expr = (binary_expression_t*)_expr;
3182 TRACE("\n");
3184 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
3187 /* ECMA-262 3rd Edition 11.13.2 */
3188 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3190 binary_expression_t *expr = (binary_expression_t*)_expr;
3192 TRACE("\n");
3194 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
3197 /* ECMA-262 3rd Edition 11.13.2 */
3198 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3200 binary_expression_t *expr = (binary_expression_t*)_expr;
3202 TRACE("\n");
3204 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
3207 /* ECMA-262 3rd Edition 11.13.2 */
3208 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3210 binary_expression_t *expr = (binary_expression_t*)_expr;
3212 TRACE("\n");
3214 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
3217 /* ECMA-262 3rd Edition 11.13.2 */
3218 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3220 binary_expression_t *expr = (binary_expression_t*)_expr;
3222 TRACE("\n");
3224 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mod_eval, ei, ret);
3227 /* ECMA-262 3rd Edition 11.13.2 */
3228 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3230 binary_expression_t *expr = (binary_expression_t*)_expr;
3232 TRACE("\n");
3234 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3237 /* ECMA-262 3rd Edition 11.13.2 */
3238 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3240 binary_expression_t *expr = (binary_expression_t*)_expr;
3242 TRACE("\n");
3244 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
3247 /* ECMA-262 3rd Edition 11.13.2 */
3248 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3250 binary_expression_t *expr = (binary_expression_t*)_expr;
3252 TRACE("\n");
3254 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);