jscript: Added '&=' expression implementation.
[wine/hacks.git] / dlls / jscript / engine.c
blob0da2d6e3293939a925c12bb0ef69f200f90e458f
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 "jscript.h"
20 #include "engine.h"
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)
42 switch(val->type) {
43 case EXPRVAL_VARIANT:
44 if(V_VT(&val->u.var) != VT_EMPTY)
45 VariantClear(&val->u.var);
46 return;
47 case EXPRVAL_IDREF:
48 if(val->u.idref.disp)
49 IDispatch_Release(val->u.idref.disp);
50 return;
51 case EXPRVAL_NAMEREF:
52 if(val->u.nameref.disp)
53 IDispatch_Release(val->u.nameref.disp);
54 SysFreeString(val->u.nameref.name);
58 /* ECMA-262 3rd Edition 8.7.1 */
59 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
61 V_VT(ret) = VT_EMPTY;
63 switch(val->type) {
64 case EXPRVAL_VARIANT:
65 return VariantCopy(ret, &val->u.var);
66 case EXPRVAL_IDREF:
67 if(!val->u.idref.disp) {
68 FIXME("throw ReferenceError\n");
69 return E_FAIL;
72 return disp_propget(val->u.idref.disp, val->u.idref.id, ctx->lcid, ret, ei, NULL/*FIXME*/);
73 default:
74 ERR("type %d\n", val->type);
75 return E_FAIL;
79 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
81 if(val->type == EXPRVAL_VARIANT) {
82 *ret = val->u.var;
83 V_VT(&val->u.var) = VT_EMPTY;
84 return S_OK;
87 return exprval_value(ctx, val, ei, ret);
90 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
92 if(exprval->type != EXPRVAL_VARIANT) {
93 VARIANT val;
94 HRESULT hres;
96 hres = exprval_to_value(ctx, exprval, ei, &val);
97 if(FAILED(hres))
98 return hres;
100 hres = to_boolean(&val, b);
101 VariantClear(&val);
102 return hres;
105 return to_boolean(&exprval->u.var, b);
108 static void exprval_init(exprval_t *val)
110 val->type = EXPRVAL_VARIANT;
111 V_VT(&val->u.var) = VT_EMPTY;
114 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
116 val->type = EXPRVAL_IDREF;
117 val->u.idref.disp = disp;
118 val->u.idref.id = id;
120 if(disp)
121 IDispatch_AddRef(disp);
124 HRESULT scope_push(scope_chain_t *scope, DispatchEx *obj, scope_chain_t **ret)
126 scope_chain_t *new_scope;
128 new_scope = heap_alloc(sizeof(scope_chain_t));
129 if(!new_scope)
130 return E_OUTOFMEMORY;
132 new_scope->ref = 1;
134 IDispatchEx_AddRef(_IDispatchEx_(obj));
135 new_scope->obj = obj;
137 if(scope) {
138 scope_addref(scope);
139 new_scope->next = scope;
140 }else {
141 new_scope->next = NULL;
144 *ret = new_scope;
145 return S_OK;
148 static void scope_pop(scope_chain_t **scope)
150 scope_chain_t *tmp;
152 tmp = *scope;
153 *scope = tmp->next;
154 scope_release(tmp);
157 void scope_release(scope_chain_t *scope)
159 if(--scope->ref)
160 return;
162 if(scope->next)
163 scope_release(scope->next);
165 IDispatchEx_Release(_IDispatchEx_(scope->obj));
166 heap_free(scope);
169 HRESULT create_exec_ctx(IDispatch *this_obj, DispatchEx *var_disp, scope_chain_t *scope, exec_ctx_t **ret)
171 exec_ctx_t *ctx;
173 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
174 if(!ctx)
175 return E_OUTOFMEMORY;
177 IDispatch_AddRef(this_obj);
178 ctx->this_obj = this_obj;
180 IDispatchEx_AddRef(_IDispatchEx_(var_disp));
181 ctx->var_disp = var_disp;
183 if(scope) {
184 scope_addref(scope);
185 ctx->scope_chain = scope;
188 *ret = ctx;
189 return S_OK;
192 void exec_release(exec_ctx_t *ctx)
194 if(--ctx->ref)
195 return;
197 if(ctx->scope_chain)
198 scope_release(ctx->scope_chain);
199 if(ctx->var_disp)
200 IDispatchEx_Release(_IDispatchEx_(ctx->var_disp));
201 if(ctx->this_obj)
202 IDispatch_Release(ctx->this_obj);
203 heap_free(ctx);
206 static HRESULT dispex_get_id(IDispatchEx *dispex, BSTR name, DWORD flags, DISPID *id)
208 *id = 0;
210 return IDispatchEx_GetDispID(dispex, name, flags|fdexNameCaseSensitive, id);
213 static HRESULT disp_get_id(IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
215 IDispatchEx *dispex;
216 HRESULT hres;
218 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
219 if(FAILED(hres)) {
220 TRACE("unsing IDispatch\n");
222 *id = 0;
223 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
226 hres = dispex_get_id(dispex, name, flags, id);
227 IDispatchEx_Release(dispex);
228 return hres;
231 /* ECMA-262 3rd Edition 8.7.2 */
232 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
234 if(ref->type != EXPRVAL_IDREF) {
235 FIXME("throw ReferemceError\n");
236 return E_FAIL;
239 return disp_propput(ref->u.idref.disp, ref->u.idref.id, ctx->lcid, v, ei, NULL/*FIXME*/);
242 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
244 IObjectIdentity *identity;
245 IUnknown *unk1, *unk2;
246 HRESULT hres;
248 if(disp1 == disp2) {
249 *ret = TRUE;
250 return S_OK;
253 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
254 if(FAILED(hres))
255 return hres;
257 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
258 if(FAILED(hres)) {
259 IUnknown_Release(unk1);
260 return hres;
263 if(unk1 == unk2) {
264 *ret = TRUE;
265 }else {
266 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
267 if(SUCCEEDED(hres)) {
268 hres = IObjectIdentity_IsEqualObject(identity, unk2);
269 IObjectIdentity_Release(identity);
270 *ret = hres == S_OK;
271 }else {
272 *ret = FALSE;
276 IUnknown_Release(unk1);
277 IUnknown_Release(unk2);
278 return S_OK;
281 static inline BOOL is_num_vt(enum VARENUM vt)
283 return vt == VT_I4 || vt == VT_R8;
286 static inline DOUBLE num_val(const VARIANT *v)
288 return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
291 static inline void num_set_val(VARIANT *v, DOUBLE d)
293 if(d == (DOUBLE)(INT)d) {
294 V_VT(v) = VT_I4;
295 V_I4(v) = d;
296 }else {
297 V_VT(v) = VT_R8;
298 V_R8(v) = d;
302 /* ECMA-262 3rd Edition 11.9.6 */
303 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
305 TRACE("\n");
307 if(V_VT(lval) != V_VT(rval)) {
308 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
309 *ret = num_val(lval) == num_val(rval);
310 return S_OK;
313 *ret = FALSE;
314 return S_OK;
317 switch(V_VT(lval)) {
318 case VT_EMPTY:
319 case VT_NULL:
320 *ret = VARIANT_TRUE;
321 break;
322 case VT_I4:
323 *ret = V_I4(lval) == V_I4(rval);
324 break;
325 case VT_R8:
326 *ret = V_R8(lval) == V_R8(rval);
327 break;
328 case VT_BSTR:
329 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
330 break;
331 case VT_DISPATCH:
332 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
333 case VT_BOOL:
334 *ret = !V_BOOL(lval) == !V_BOOL(rval);
335 break;
336 default:
337 FIXME("unimplemented vt %d\n", V_VT(lval));
338 return E_NOTIMPL;
341 return S_OK;
344 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
346 V_VT(v) = literal->vt;
348 switch(V_VT(v)) {
349 case VT_EMPTY:
350 case VT_NULL:
351 break;
352 case VT_I4:
353 V_I4(v) = literal->u.lval;
354 break;
355 case VT_R8:
356 V_R8(v) = literal->u.dval;
357 break;
358 case VT_BSTR:
359 V_BSTR(v) = SysAllocString(literal->u.wstr);
360 break;
361 case VT_BOOL:
362 V_BOOL(v) = literal->u.bval;
363 break;
364 case VT_DISPATCH:
365 IDispatch_AddRef(literal->u.disp);
366 V_DISPATCH(v) = literal->u.disp;
367 break;
368 default:
369 ERR("wrong type %d\n", V_VT(v));
370 return E_NOTIMPL;
373 return S_OK;
376 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
378 script_ctx_t *script = parser->script;
379 function_declaration_t *func;
380 parser_ctx_t *prev_parser;
381 VARIANT val, tmp;
382 statement_t *stat;
383 exec_ctx_t *prev_ctx;
384 return_type_t rt;
385 HRESULT hres = S_OK;
387 for(func = source->functions; func; func = func->next) {
388 DispatchEx *func_obj;
389 VARIANT var;
391 hres = create_source_function(parser, func->parameter_list, func->source_elements, ctx->scope_chain, &func_obj);
392 if(FAILED(hres))
393 return hres;
395 V_VT(&var) = VT_DISPATCH;
396 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
397 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
398 IDispatchEx_Release(_IDispatchEx_(func_obj));
399 if(FAILED(hres))
400 return hres;
403 prev_ctx = script->exec_ctx;
404 script->exec_ctx = ctx;
406 prev_parser = ctx->parser;
407 ctx->parser = parser;
409 V_VT(&val) = VT_EMPTY;
410 memset(&rt, 0, sizeof(rt));
411 rt.type = RT_NORMAL;
413 for(stat = source->statement; stat; stat = stat->next) {
414 hres = stat_eval(ctx, stat, &rt, &tmp);
415 if(FAILED(hres))
416 break;
418 VariantClear(&val);
419 val = tmp;
420 if(rt.type != RT_NORMAL)
421 break;
424 script->exec_ctx = prev_ctx;
425 ctx->parser = prev_parser;
427 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
428 FIXME("wrong rt %d\n", rt.type);
429 hres = E_FAIL;
432 *ei = rt.ei;
433 if(FAILED(hres)) {
434 VariantClear(&val);
435 return hres;
438 if(retv)
439 *retv = val;
440 else
441 VariantClear(&val);
442 return S_OK;
445 /* ECMA-262 3rd Edition 10.1.4 */
446 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
448 scope_chain_t *scope;
449 named_item_t *item;
450 DISPID id = 0;
451 HRESULT hres;
453 TRACE("%s\n", debugstr_w(identifier));
455 for(scope = ctx->scope_chain; scope; scope = scope->next) {
456 hres = dispex_get_id(_IDispatchEx_(scope->obj), identifier, 0, &id);
457 if(SUCCEEDED(hres))
458 break;
461 if(scope) {
462 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
463 return S_OK;
466 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->global), identifier, 0, &id);
467 if(SUCCEEDED(hres)) {
468 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
469 return S_OK;
472 for(item = ctx->parser->script->named_items; item; item = item->next) {
473 hres = disp_get_id(item->disp, identifier, 0, &id);
474 if(SUCCEEDED(hres))
475 break;
478 if(item) {
479 exprval_set_idref(ret, (IDispatch*)item->disp, id);
480 return S_OK;
483 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->script_disp), identifier, 0, &id);
484 if(SUCCEEDED(hres)) {
485 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
486 return S_OK;
489 if(flags & EXPR_NEWREF) {
490 hres = dispex_get_id(_IDispatchEx_(ctx->var_disp), identifier, fdexNameEnsure, &id);
491 if(FAILED(hres))
492 return hres;
494 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
495 return S_OK;
498 WARN("Could not find identifier %s\n", debugstr_w(identifier));
499 return E_FAIL;
502 /* ECMA-262 3rd Edition 12.1 */
503 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
505 block_statement_t *stat = (block_statement_t*)_stat;
506 VARIANT val, tmp;
507 statement_t *iter;
508 HRESULT hres = S_OK;
510 TRACE("\n");
512 V_VT(&val) = VT_EMPTY;
513 for(iter = stat->stat_list; iter; iter = iter->next) {
514 hres = stat_eval(ctx, iter, rt, &tmp);
515 if(FAILED(hres))
516 break;
518 VariantClear(&val);
519 val = tmp;
520 if(rt->type != RT_NORMAL)
521 break;
524 if(FAILED(hres)) {
525 VariantClear(&val);
526 return hres;
529 *ret = val;
530 return S_OK;
533 /* ECMA-262 3rd Edition 12.2 */
534 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
536 variable_declaration_t *iter;
537 HRESULT hres = E_FAIL;
539 for(iter = var_list; iter; iter = iter->next) {
540 VARIANT val;
542 if(iter->expr) {
543 exprval_t exprval;
545 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
546 if(FAILED(hres))
547 break;
549 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
550 exprval_release(&exprval);
551 if(FAILED(hres))
552 break;
553 }else {
554 V_VT(&val) = VT_EMPTY;
557 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
558 VariantClear(&val);
559 if(FAILED(hres))
560 break;
563 return hres;
566 /* ECMA-262 3rd Edition 12.2 */
567 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
569 var_statement_t *stat = (var_statement_t*)_stat;
570 HRESULT hres;
572 TRACE("\n");
574 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
575 if(FAILED(hres))
576 return hres;
578 V_VT(ret) = VT_EMPTY;
579 return S_OK;
582 /* ECMA-262 3rd Edition 12.3 */
583 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
585 TRACE("\n");
587 V_VT(ret) = VT_EMPTY;
588 return S_OK;
591 /* ECMA-262 3rd Edition 12.4 */
592 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
594 expression_statement_t *stat = (expression_statement_t*)_stat;
595 exprval_t exprval;
596 VARIANT val;
597 HRESULT hres;
599 TRACE("\n");
601 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
602 if(FAILED(hres))
603 return hres;
605 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
606 exprval_release(&exprval);
607 if(FAILED(hres))
608 return hres;
610 *ret = val;
611 TRACE("= %s\n", debugstr_variant(ret));
612 return S_OK;
615 /* ECMA-262 3rd Edition 12.5 */
616 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
618 if_statement_t *stat = (if_statement_t*)_stat;
619 exprval_t exprval;
620 VARIANT_BOOL b;
621 HRESULT hres;
623 TRACE("\n");
625 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
626 if(FAILED(hres))
627 return hres;
629 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
630 exprval_release(&exprval);
631 if(FAILED(hres))
632 return hres;
634 if(b)
635 hres = stat_eval(ctx, stat->if_stat, rt, ret);
636 else if(stat->else_stat)
637 hres = stat_eval(ctx, stat->else_stat, rt, ret);
638 else
639 V_VT(ret) = VT_EMPTY;
641 return hres;
644 HRESULT dowhile_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
646 FIXME("\n");
647 return E_NOTIMPL;
650 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
652 FIXME("\n");
653 return E_NOTIMPL;
656 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
658 FIXME("\n");
659 return E_NOTIMPL;
662 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
664 FIXME("\n");
665 return E_NOTIMPL;
668 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
670 FIXME("\n");
671 return E_NOTIMPL;
674 /* ECMA-262 3rd Edition 12.8 */
675 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
677 branch_statement_t *stat = (branch_statement_t*)_stat;
679 TRACE("\n");
681 if(stat->identifier) {
682 FIXME("indentifier not implemented\n");
683 return E_NOTIMPL;
686 rt->type = RT_BREAK;
687 V_VT(ret) = VT_EMPTY;
688 return S_OK;
691 /* ECMA-262 3rd Edition 12.9 */
692 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
694 expression_statement_t *stat = (expression_statement_t*)_stat;
695 HRESULT hres;
697 TRACE("\n");
699 if(stat->expr) {
700 exprval_t exprval;
702 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
703 if(FAILED(hres))
704 return hres;
706 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
707 exprval_release(&exprval);
708 if(FAILED(hres))
709 return hres;
710 }else {
711 V_VT(ret) = VT_EMPTY;
714 TRACE("= %s\n", debugstr_variant(ret));
715 rt->type = RT_RETURN;
716 return S_OK;
719 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
721 FIXME("\n");
722 return E_NOTIMPL;
725 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
727 FIXME("\n");
728 return E_NOTIMPL;
731 /* ECMA-262 3rd Edition 12.13 */
732 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
734 switch_statement_t *stat = (switch_statement_t*)_stat;
735 case_clausule_t *iter, *default_clausule = NULL;
736 statement_t *stat_iter;
737 VARIANT val, cval;
738 exprval_t exprval;
739 BOOL b;
740 HRESULT hres;
742 TRACE("\n");
744 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
745 if(FAILED(hres))
746 return hres;
748 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
749 exprval_release(&exprval);
750 if(FAILED(hres))
751 return hres;
753 for(iter = stat->case_list; iter; iter = iter->next) {
754 if(!iter->expr) {
755 default_clausule = iter;
756 continue;
759 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
760 if(FAILED(hres))
761 break;
763 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
764 exprval_release(&exprval);
765 if(FAILED(hres))
766 break;
768 hres = equal2_values(&val, &cval, &b);
769 VariantClear(&cval);
770 if(FAILED(hres) || b)
771 break;
774 VariantClear(&val);
775 if(FAILED(hres))
776 return hres;
778 if(!iter)
779 iter = default_clausule;
781 V_VT(&val) = VT_EMPTY;
782 if(iter) {
783 VARIANT tmp;
785 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
786 hres = stat_eval(ctx, stat_iter, rt, &tmp);
787 if(FAILED(hres))
788 break;
790 VariantClear(&val);
791 val = tmp;
793 if(rt->type != RT_NORMAL)
794 break;
798 if(FAILED(hres)) {
799 VariantClear(&val);
800 return hres;
803 if(rt->type == RT_BREAK)
804 rt->type = RT_NORMAL;
806 *ret = val;
807 return S_OK;
810 /* ECMA-262 3rd Edition 12.13 */
811 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
813 expression_statement_t *stat = (expression_statement_t*)_stat;
814 exprval_t exprval;
815 VARIANT val;
816 HRESULT hres;
818 TRACE("\n");
820 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
821 if(FAILED(hres))
822 return hres;
824 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
825 exprval_release(&exprval);
826 if(FAILED(hres))
827 return hres;
829 rt->ei.var = val;
830 return DISP_E_EXCEPTION;
833 /* ECMA-262 3rd Edition 12.14 */
834 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
836 DispatchEx *var_disp;
837 VARIANT ex, val;
838 HRESULT hres;
840 ex = rt->ei.var;
841 memset(&rt->ei, 0, sizeof(jsexcept_t));
843 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
844 if(SUCCEEDED(hres)) {
845 hres = jsdisp_propput_name(var_disp, block->identifier, ctx->parser->script->lcid,
846 &ex, &rt->ei, NULL/*FIXME*/);
847 if(SUCCEEDED(hres)) {
848 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
849 if(SUCCEEDED(hres)) {
850 hres = stat_eval(ctx, block->statement, rt, &val);
851 scope_pop(&ctx->scope_chain);
855 jsdisp_release(var_disp);
858 VariantClear(&ex);
859 if(FAILED(hres))
860 return hres;
862 *ret = val;
863 return S_OK;
866 /* ECMA-262 3rd Edition 12.14 */
867 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
869 try_statement_t *stat = (try_statement_t*)_stat;
870 VARIANT val;
871 HRESULT hres;
873 TRACE("\n");
875 hres = stat_eval(ctx, stat->try_statement, rt, &val);
876 if(FAILED(hres)) {
877 TRACE("EXCEPTION\n");
878 if(!stat->catch_block)
879 return hres;
881 hres = catch_eval(ctx, stat->catch_block, rt, &val);
882 if(FAILED(hres))
883 return hres;
886 if(stat->finally_statement) {
887 VariantClear(&val);
888 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
889 if(FAILED(hres))
890 return hres;
893 *ret = val;
894 return S_OK;
897 static HRESULT return_bool(exprval_t *ret, DWORD b)
899 ret->type = EXPRVAL_VARIANT;
900 V_VT(&ret->u.var) = VT_BOOL;
901 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
903 return S_OK;
906 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
908 exprval_t exprval;
909 HRESULT hres;
911 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
912 if(FAILED(hres))
913 return hres;
915 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
916 exprval_release(&exprval);
917 if(FAILED(hres))
918 return hres;
920 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
921 if(SUCCEEDED(hres)) {
922 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
923 exprval_release(&exprval);
926 if(FAILED(hres)) {
927 VariantClear(lval);
928 return hres;
931 return S_OK;
934 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
936 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
937 exprval_t *ret)
939 VARIANT lval, rval, retv;
940 HRESULT hres;
942 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
943 if(FAILED(hres))
944 return hres;
946 hres = oper(ctx, &lval, &rval, ei, &retv);
947 VariantClear(&lval);
948 VariantClear(&rval);
949 if(FAILED(hres))
950 return hres;
952 ret->type = EXPRVAL_VARIANT;
953 ret->u.var = retv;
954 return S_OK;
957 /* ECMA-262 3rd Edition 11.13.2 */
958 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
959 jsexcept_t *ei, exprval_t *ret)
961 VARIANT retv, lval, rval;
962 exprval_t exprval, exprvalr;
963 HRESULT hres;
965 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
966 if(FAILED(hres))
967 return hres;
969 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
970 if(SUCCEEDED(hres)) {
971 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
972 if(SUCCEEDED(hres)) {
973 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
974 exprval_release(&exprvalr);
976 if(SUCCEEDED(hres)) {
977 hres = oper(ctx, &lval, &rval, ei, &retv);
978 VariantClear(&rval);
980 VariantClear(&lval);
983 if(SUCCEEDED(hres)) {
984 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
985 if(FAILED(hres))
986 VariantClear(&retv);
988 exprval_release(&exprval);
990 if(FAILED(hres))
991 return hres;
993 ret->type = EXPRVAL_VARIANT;
994 ret->u.var = retv;
995 return S_OK;
998 /* ECMA-262 3rd Edition 13 */
999 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1001 function_expression_t *expr = (function_expression_t*)_expr;
1002 DispatchEx *dispex;
1003 VARIANT var;
1004 HRESULT hres;
1006 TRACE("\n");
1008 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain, &dispex);
1009 if(FAILED(hres))
1010 return hres;
1012 V_VT(&var) = VT_DISPATCH;
1013 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1015 if(expr->identifier) {
1016 hres = jsdisp_propput_name(ctx->var_disp, expr->identifier, ctx->parser->script->lcid, &var, ei, NULL/*FIXME*/);
1017 if(FAILED(hres)) {
1018 jsdisp_release(dispex);
1019 return hres;
1023 ret->type = EXPRVAL_VARIANT;
1024 ret->u.var = var;
1025 return S_OK;
1028 /* ECMA-262 3rd Edition 11.12 */
1029 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1031 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1032 exprval_t exprval;
1033 VARIANT_BOOL b;
1034 HRESULT hres;
1036 TRACE("\n");
1038 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1039 if(FAILED(hres))
1040 return hres;
1042 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1043 exprval_release(&exprval);
1044 if(FAILED(hres))
1045 return hres;
1047 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1050 /* ECMA-262 3rd Edition 11.2.1 */
1051 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1053 array_expression_t *expr = (array_expression_t*)_expr;
1054 exprval_t exprval;
1055 VARIANT member, val;
1056 DISPID id;
1057 BSTR str;
1058 IDispatch *obj = NULL;
1059 HRESULT hres;
1061 TRACE("\n");
1063 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
1064 if(FAILED(hres))
1065 return hres;
1067 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1068 exprval_release(&exprval);
1069 if(FAILED(hres))
1070 return hres;
1072 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1073 if(SUCCEEDED(hres)) {
1074 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1075 exprval_release(&exprval);
1078 if(SUCCEEDED(hres))
1079 hres = to_object(ctx, &member, &obj);
1080 VariantClear(&member);
1081 if(SUCCEEDED(hres)) {
1082 hres = to_string(ctx->parser->script, &val, ei, &str);
1083 if(SUCCEEDED(hres)) {
1084 if(flags & EXPR_STRREF) {
1085 ret->type = EXPRVAL_NAMEREF;
1086 ret->u.nameref.disp = obj;
1087 ret->u.nameref.name = str;
1088 return S_OK;
1091 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1094 if(SUCCEEDED(hres)) {
1095 exprval_set_idref(ret, obj, id);
1096 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1097 exprval_init(ret);
1098 hres = S_OK;
1101 IDispatch_Release(obj);
1104 return hres;
1107 /* ECMA-262 3rd Edition 11.2.1 */
1108 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1110 member_expression_t *expr = (member_expression_t*)_expr;
1111 IDispatch *obj = NULL;
1112 exprval_t exprval;
1113 VARIANT member;
1114 DISPID id;
1115 BSTR str;
1116 HRESULT hres;
1118 TRACE("\n");
1120 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1121 if(FAILED(hres))
1122 return hres;
1124 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1125 exprval_release(&exprval);
1126 if(FAILED(hres))
1127 return hres;
1129 hres = to_object(ctx, &member, &obj);
1130 VariantClear(&member);
1131 if(FAILED(hres))
1132 return hres;
1134 str = SysAllocString(expr->identifier);
1135 if(flags & EXPR_STRREF) {
1136 ret->type = EXPRVAL_NAMEREF;
1137 ret->u.nameref.disp = obj;
1138 ret->u.nameref.name = str;
1139 return S_OK;
1142 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1143 SysFreeString(str);
1144 if(SUCCEEDED(hres)) {
1145 exprval_set_idref(ret, obj, id);
1146 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1147 exprval_init(ret);
1148 hres = S_OK;
1151 IDispatch_Release(obj);
1152 return hres;
1155 static void free_dp(DISPPARAMS *dp)
1157 DWORD i;
1159 for(i=0; i < dp->cArgs; i++)
1160 VariantClear(dp->rgvarg+i);
1161 heap_free(dp->rgvarg);
1164 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1166 VARIANTARG *vargs;
1167 exprval_t exprval;
1168 argument_t *iter;
1169 DWORD cnt = 0, i;
1170 HRESULT hres = S_OK;
1172 memset(dp, 0, sizeof(*dp));
1173 if(!args)
1174 return S_OK;
1176 for(iter = args; iter; iter = iter->next)
1177 cnt++;
1179 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1180 if(!vargs)
1181 return E_OUTOFMEMORY;
1183 for(i = cnt, iter = args; iter; iter = iter->next) {
1184 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1185 if(FAILED(hres))
1186 break;
1188 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1189 exprval_release(&exprval);
1190 if(FAILED(hres))
1191 break;
1194 if(FAILED(hres)) {
1195 free_dp(dp);
1196 return hres;
1199 dp->rgvarg = vargs;
1200 dp->cArgs = cnt;
1201 return S_OK;
1204 /* ECMA-262 3rd Edition 11.2.2 */
1205 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1207 call_expression_t *expr = (call_expression_t*)_expr;
1208 exprval_t exprval;
1209 VARIANT constr, var;
1210 DISPPARAMS dp;
1211 HRESULT hres;
1213 TRACE("\n");
1215 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1216 if(FAILED(hres))
1217 return hres;
1219 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1220 if(SUCCEEDED(hres))
1221 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1222 exprval_release(&exprval);
1223 if(FAILED(hres))
1224 return hres;
1226 if(V_VT(&constr) != VT_DISPATCH) {
1227 FIXME("throw TypeError\n");
1228 VariantClear(&constr);
1229 return E_FAIL;
1232 hres = disp_call(V_DISPATCH(&constr), DISPID_VALUE, ctx->parser->script->lcid,
1233 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1234 IDispatch_Release(V_DISPATCH(&constr));
1235 if(FAILED(hres))
1236 return hres;
1238 ret->type = EXPRVAL_VARIANT;
1239 ret->u.var = var;
1240 return S_OK;
1243 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1245 call_expression_t *expr = (call_expression_t*)_expr;
1246 VARIANT func, var;
1247 exprval_t exprval;
1248 DISPPARAMS dp;
1249 HRESULT hres;
1251 TRACE("\n");
1253 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1254 if(FAILED(hres))
1255 return hres;
1257 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1258 if(SUCCEEDED(hres)) {
1259 switch(exprval.type) {
1260 case EXPRVAL_IDREF:
1261 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
1262 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1263 if(flags & EXPR_NOVAL)
1264 V_VT(&var) = VT_EMPTY;
1265 break;
1266 default:
1267 FIXME("unimplemented type %d\n", V_VT(&func));
1268 hres = E_NOTIMPL;
1271 free_dp(&dp);
1274 exprval_release(&exprval);
1275 if(FAILED(hres))
1276 return hres;
1278 TRACE("= %s\n", debugstr_variant(&var));
1279 ret->type = EXPRVAL_VARIANT;
1280 ret->u.var = var;
1281 return S_OK;
1284 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1286 TRACE("\n");
1288 ret->type = EXPRVAL_VARIANT;
1289 V_VT(&ret->u.var) = VT_DISPATCH;
1290 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1291 IDispatch_AddRef(ctx->this_obj);
1292 return S_OK;
1295 /* ECMA-262 3rd Edition 10.1.4 */
1296 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1298 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1299 BSTR identifier;
1300 HRESULT hres;
1302 TRACE("\n");
1304 identifier = SysAllocString(expr->identifier);
1305 if(!identifier)
1306 return E_OUTOFMEMORY;
1308 hres = identifier_eval(ctx, identifier, flags, ret);
1310 SysFreeString(identifier);
1311 return hres;
1314 /* ECMA-262 3rd Edition 7.8 */
1315 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1317 literal_expression_t *expr = (literal_expression_t*)_expr;
1318 VARIANT var;
1319 HRESULT hres;
1321 TRACE("\n");
1323 hres = literal_to_var(expr->literal, &var);
1324 if(FAILED(hres))
1325 return hres;
1327 ret->type = EXPRVAL_VARIANT;
1328 ret->u.var = var;
1329 return S_OK;
1332 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1334 FIXME("\n");
1335 return E_NOTIMPL;
1338 /* ECMA-262 3rd Edition 11.1.5 */
1339 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1341 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1342 VARIANT val, tmp;
1343 DispatchEx *obj;
1344 prop_val_t *iter;
1345 exprval_t exprval;
1346 BSTR name;
1347 HRESULT hres;
1349 TRACE("\n");
1351 hres = create_object(ctx->parser->script, NULL, &obj);
1352 if(FAILED(hres))
1353 return hres;
1355 for(iter = expr->property_list; iter; iter = iter->next) {
1356 hres = literal_to_var(iter->name, &tmp);
1357 if(FAILED(hres))
1358 break;
1360 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1361 VariantClear(&tmp);
1362 if(FAILED(hres))
1363 break;
1365 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1366 if(SUCCEEDED(hres)) {
1367 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1368 exprval_release(&exprval);
1369 if(SUCCEEDED(hres)) {
1370 hres = jsdisp_propput_name(obj, name, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1371 VariantClear(&val);
1375 SysFreeString(name);
1376 if(FAILED(hres))
1377 break;
1380 if(FAILED(hres)) {
1381 jsdisp_release(obj);
1382 return hres;
1385 ret->type = EXPRVAL_VARIANT;
1386 V_VT(&ret->u.var) = VT_DISPATCH;
1387 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1388 return S_OK;
1391 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1393 FIXME("\n");
1394 return E_NOTIMPL;
1397 /* ECMA-262 3rd Edition 11.11 */
1398 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1400 binary_expression_t *expr = (binary_expression_t*)_expr;
1401 exprval_t exprval;
1402 VARIANT_BOOL b;
1403 VARIANT val;
1404 HRESULT hres;
1406 TRACE("\n");
1408 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1409 if(FAILED(hres))
1410 return hres;
1412 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1413 exprval_release(&exprval);
1414 if(FAILED(hres))
1415 return hres;
1417 hres = to_boolean(&val, &b);
1418 if(SUCCEEDED(hres) && b) {
1419 ret->type = EXPRVAL_VARIANT;
1420 ret->u.var = val;
1421 return S_OK;
1424 VariantClear(&val);
1425 if(FAILED(hres))
1426 return hres;
1428 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1429 if(FAILED(hres))
1430 return hres;
1432 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1433 exprval_release(&exprval);
1434 if(FAILED(hres))
1435 return hres;
1437 ret->type = EXPRVAL_VARIANT;
1438 ret->u.var = val;
1439 return S_OK;
1442 /* ECMA-262 3rd Edition 11.11 */
1443 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1445 binary_expression_t *expr = (binary_expression_t*)_expr;
1446 exprval_t exprval;
1447 VARIANT_BOOL b;
1448 VARIANT val;
1449 HRESULT hres;
1451 TRACE("\n");
1453 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1454 if(FAILED(hres))
1455 return hres;
1457 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1458 exprval_release(&exprval);
1459 if(FAILED(hres))
1460 return hres;
1462 hres = to_boolean(&val, &b);
1463 if(SUCCEEDED(hres) && !b) {
1464 ret->type = EXPRVAL_VARIANT;
1465 ret->u.var = val;
1466 return S_OK;
1469 VariantClear(&val);
1470 if(FAILED(hres))
1471 return hres;
1473 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1474 if(FAILED(hres))
1475 return hres;
1477 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1478 exprval_release(&exprval);
1479 if(FAILED(hres))
1480 return hres;
1482 ret->type = EXPRVAL_VARIANT;
1483 ret->u.var = val;
1484 return S_OK;
1487 /* ECMA-262 3rd Edition 11.10 */
1488 static HRESULT bitor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1490 INT li, ri;
1491 HRESULT hres;
1493 hres = to_int32(ctx->parser->script, lval, ei, &li);
1494 if(FAILED(hres))
1495 return hres;
1497 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1498 if(FAILED(hres))
1499 return hres;
1501 V_VT(retv) = VT_I4;
1502 V_I4(retv) = li|ri;
1503 return S_OK;
1506 /* ECMA-262 3rd Edition 11.10 */
1507 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1509 binary_expression_t *expr = (binary_expression_t*)_expr;
1511 TRACE("\n");
1513 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
1516 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1518 FIXME("\n");
1519 return E_NOTIMPL;
1522 /* ECMA-262 3rd Edition 11.10 */
1523 static HRESULT bitand_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1525 INT li, ri;
1526 HRESULT hres;
1528 hres = to_int32(ctx->parser->script, lval, ei, &li);
1529 if(FAILED(hres))
1530 return hres;
1532 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1533 if(FAILED(hres))
1534 return hres;
1536 V_VT(retv) = VT_I4;
1537 V_I4(retv) = li&ri;
1538 return S_OK;
1541 /* ECMA-262 3rd Edition 11.10 */
1542 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1544 binary_expression_t *expr = (binary_expression_t*)_expr;
1546 TRACE("\n");
1548 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
1551 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1553 FIXME("\n");
1554 return E_NOTIMPL;
1557 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1559 FIXME("\n");
1560 return E_NOTIMPL;
1563 /* ECMA-262 3rd Edition 11.6.1 */
1564 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1566 VARIANT r, l;
1567 HRESULT hres;
1569 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1570 if(FAILED(hres))
1571 return hres;
1573 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1574 if(FAILED(hres)) {
1575 VariantClear(&l);
1576 return hres;
1579 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1580 BSTR lstr = NULL, rstr = NULL;
1582 if(V_VT(&l) == VT_BSTR)
1583 lstr = V_BSTR(&l);
1584 else
1585 hres = to_string(ctx->parser->script, &l, ei, &lstr);
1587 if(SUCCEEDED(hres)) {
1588 if(V_VT(&r) == VT_BSTR)
1589 rstr = V_BSTR(&r);
1590 else
1591 hres = to_string(ctx->parser->script, &r, ei, &rstr);
1594 if(SUCCEEDED(hres)) {
1595 int len1, len2;
1597 len1 = SysStringLen(lstr);
1598 len2 = SysStringLen(rstr);
1600 V_VT(retv) = VT_BSTR;
1601 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1602 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1603 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1606 if(lstr && V_VT(&l) != VT_BSTR)
1607 SysFreeString(lstr);
1608 if(rstr && V_VT(&r) != VT_BSTR)
1609 SysFreeString(rstr);
1610 }else {
1611 VARIANT nl, nr;
1613 hres = to_number(ctx->parser->script, &l, ei, &nl);
1614 if(SUCCEEDED(hres)) {
1615 hres = to_number(ctx->parser->script, &r, ei, &nr);
1616 if(SUCCEEDED(hres))
1617 num_set_val(retv, num_val(&nl) + num_val(&nr));
1621 VariantClear(&r);
1622 VariantClear(&l);
1623 return hres;
1626 /* ECMA-262 3rd Edition 11.6.1 */
1627 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1629 binary_expression_t *expr = (binary_expression_t*)_expr;
1631 TRACE("\n");
1633 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
1636 /* ECMA-262 3rd Edition 11.6.2 */
1637 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1639 VARIANT lnum, rnum;
1640 HRESULT hres;
1642 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1643 if(FAILED(hres))
1644 return hres;
1646 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1647 if(FAILED(hres))
1648 return hres;
1650 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
1651 return S_OK;
1654 /* ECMA-262 3rd Edition 11.6.2 */
1655 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1657 binary_expression_t *expr = (binary_expression_t*)_expr;
1659 TRACE("\n");
1661 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
1664 /* ECMA-262 3rd Edition 11.5.1 */
1665 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1667 VARIANT lnum, rnum;
1668 HRESULT hres;
1670 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1671 if(FAILED(hres))
1672 return hres;
1674 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1675 if(FAILED(hres))
1676 return hres;
1678 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
1679 return S_OK;
1682 /* ECMA-262 3rd Edition 11.5.1 */
1683 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1685 binary_expression_t *expr = (binary_expression_t*)_expr;
1687 TRACE("\n");
1689 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
1692 /* ECMA-262 3rd Edition 11.5.2 */
1693 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1695 VARIANT lnum, rnum;
1696 HRESULT hres;
1698 hres = to_number(ctx->parser->script, lval, ei, &lnum);
1699 if(FAILED(hres))
1700 return hres;
1702 hres = to_number(ctx->parser->script, rval, ei, &rnum);
1703 if(FAILED(hres))
1704 return hres;
1706 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
1707 return S_OK;
1710 /* ECMA-262 3rd Edition 11.5.2 */
1711 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1713 binary_expression_t *expr = (binary_expression_t*)_expr;
1715 TRACE("\n");
1717 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
1720 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1722 FIXME("\n");
1723 return E_NOTIMPL;
1726 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1728 FIXME("\n");
1729 return E_NOTIMPL;
1732 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1734 FIXME("\n");
1735 return E_NOTIMPL;
1738 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1740 unary_expression_t *expr = (unary_expression_t*)_expr;
1741 const WCHAR *str;
1742 exprval_t exprval;
1743 VARIANT val;
1744 HRESULT hres;
1746 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
1747 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
1748 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
1749 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
1750 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
1751 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1753 TRACE("\n");
1755 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1756 if(FAILED(hres))
1757 return hres;
1759 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1760 exprval_release(&exprval);
1761 if(FAILED(hres))
1762 return hres;
1764 switch(V_VT(&val)) {
1765 case VT_EMPTY:
1766 str = undefinedW;
1767 break;
1768 case VT_NULL:
1769 str = objectW;
1770 break;
1771 case VT_BOOL:
1772 str = booleanW;
1773 break;
1774 case VT_I4:
1775 case VT_R8:
1776 str = numberW;
1777 break;
1778 case VT_BSTR:
1779 str = stringW;
1780 break;
1781 case VT_DISPATCH: {
1782 DispatchEx *dispex;
1784 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
1785 if(dispex) {
1786 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
1787 IDispatchEx_Release(_IDispatchEx_(dispex));
1788 }else {
1789 str = objectW;
1791 break;
1793 default:
1794 FIXME("unhandled vt %d\n", V_VT(&val));
1795 hres = E_NOTIMPL;
1798 VariantClear(&val);
1799 if(FAILED(hres))
1800 return hres;
1802 ret->type = EXPRVAL_VARIANT;
1803 V_VT(&ret->u.var) = VT_BSTR;
1804 V_BSTR(&ret->u.var) = SysAllocString(str);
1805 return S_OK;
1808 /* ECMA-262 3rd Edition 11.4.7 */
1809 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1811 unary_expression_t *expr = (unary_expression_t*)_expr;
1812 exprval_t exprval;
1813 VARIANT val, num;
1814 HRESULT hres;
1816 TRACE("\n");
1818 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1819 if(FAILED(hres))
1820 return hres;
1822 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1823 exprval_release(&exprval);
1824 if(FAILED(hres))
1825 return hres;
1827 hres = to_number(ctx->parser->script, &val, ei, &num);
1828 VariantClear(&val);
1829 if(FAILED(hres))
1830 return hres;
1832 ret->type = EXPRVAL_VARIANT;
1833 num_set_val(&ret->u.var, -num_val(&num));
1834 return S_OK;
1837 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1839 FIXME("\n");
1840 return E_NOTIMPL;
1843 /* ECMA-262 3rd Edition 11.3.1 */
1844 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1846 unary_expression_t *expr = (unary_expression_t*)_expr;
1847 VARIANT val, num;
1848 exprval_t exprval;
1849 HRESULT hres;
1851 TRACE("\n");
1853 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1854 if(FAILED(hres))
1855 return hres;
1857 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1858 if(SUCCEEDED(hres)) {
1859 hres = to_number(ctx->parser->script, &val, ei, &num);
1860 VariantClear(&val);
1863 if(SUCCEEDED(hres)) {
1864 VARIANT inc;
1865 num_set_val(&inc, num_val(&num)+1.0);
1866 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
1869 exprval_release(&exprval);
1870 if(FAILED(hres))
1871 return hres;
1873 ret->type = EXPRVAL_VARIANT;
1874 ret->u.var = num;
1875 return S_OK;
1878 /* ECMA-262 3rd Edition 11.3.2 */
1879 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1881 unary_expression_t *expr = (unary_expression_t*)_expr;
1882 VARIANT val, num;
1883 exprval_t exprval;
1884 HRESULT hres;
1886 TRACE("\n");
1888 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1889 if(FAILED(hres))
1890 return hres;
1892 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1893 if(SUCCEEDED(hres)) {
1894 hres = to_number(ctx->parser->script, &val, ei, &num);
1895 VariantClear(&val);
1898 if(SUCCEEDED(hres)) {
1899 VARIANT dec;
1900 num_set_val(&dec, num_val(&num)-1.0);
1901 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
1904 exprval_release(&exprval);
1905 if(FAILED(hres))
1906 return hres;
1908 ret->type = EXPRVAL_VARIANT;
1909 ret->u.var = num;
1910 return S_OK;
1913 /* ECMA-262 3rd Edition 11.4.4 */
1914 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1916 unary_expression_t *expr = (unary_expression_t*)_expr;
1917 VARIANT val, num;
1918 exprval_t exprval;
1919 HRESULT hres;
1921 TRACE("\n");
1923 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1924 if(FAILED(hres))
1925 return hres;
1927 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1928 if(SUCCEEDED(hres)) {
1929 hres = to_number(ctx->parser->script, &val, ei, &num);
1930 VariantClear(&val);
1933 if(SUCCEEDED(hres)) {
1934 num_set_val(&val, num_val(&num)+1.0);
1935 hres = put_value(ctx->parser->script, &exprval, &val, ei);
1938 exprval_release(&exprval);
1939 if(FAILED(hres))
1940 return hres;
1942 ret->type = EXPRVAL_VARIANT;
1943 ret->u.var = val;
1944 return S_OK;
1947 /* ECMA-262 3rd Edition 11.4.5 */
1948 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1950 unary_expression_t *expr = (unary_expression_t*)_expr;
1951 VARIANT val, num;
1952 exprval_t exprval;
1953 HRESULT hres;
1955 TRACE("\n");
1957 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1958 if(FAILED(hres))
1959 return hres;
1961 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1962 if(SUCCEEDED(hres)) {
1963 hres = to_number(ctx->parser->script, &val, ei, &num);
1964 VariantClear(&val);
1967 if(SUCCEEDED(hres)) {
1968 num_set_val(&val, num_val(&num)-1.0);
1969 hres = put_value(ctx->parser->script, &exprval, &val, ei);
1972 exprval_release(&exprval);
1973 if(FAILED(hres))
1974 return hres;
1976 ret->type = EXPRVAL_VARIANT;
1977 ret->u.var = val;
1978 return S_OK;
1981 /* ECMA-262 3rd Edition 11.9.3 */
1982 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1984 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1985 return equal2_values(lval, rval, ret);
1987 /* FIXME: NULL disps should be handled in more general way */
1988 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1989 VARIANT v;
1990 V_VT(&v) = VT_NULL;
1991 return equal_values(ctx, &v, rval, ei, ret);
1994 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1995 VARIANT v;
1996 V_VT(&v) = VT_NULL;
1997 return equal_values(ctx, lval, &v, ei, ret);
2000 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2001 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2002 *ret = TRUE;
2003 return S_OK;
2006 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2007 VARIANT v;
2008 HRESULT hres;
2010 hres = to_number(ctx->parser->script, lval, ei, &v);
2011 if(FAILED(hres))
2012 return hres;
2014 return equal_values(ctx, &v, rval, ei, ret);
2017 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2018 VARIANT v;
2019 HRESULT hres;
2021 hres = to_number(ctx->parser->script, rval, ei, &v);
2022 if(FAILED(hres))
2023 return hres;
2025 return equal_values(ctx, lval, &v, ei, ret);
2028 if(V_VT(rval) == VT_BOOL) {
2029 VARIANT v;
2031 V_VT(&v) = VT_I4;
2032 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2033 return equal_values(ctx, lval, &v, ei, ret);
2036 if(V_VT(lval) == VT_BOOL) {
2037 VARIANT v;
2039 V_VT(&v) = VT_I4;
2040 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2041 return equal_values(ctx, &v, rval, ei, ret);
2045 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2046 VARIANT v;
2047 HRESULT hres;
2049 hres = to_primitive(ctx->parser->script, rval, ei, &v);
2050 if(FAILED(hres))
2051 return hres;
2053 hres = equal_values(ctx, lval, &v, ei, ret);
2055 VariantClear(&v);
2056 return hres;
2060 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2061 VARIANT v;
2062 HRESULT hres;
2064 hres = to_primitive(ctx->parser->script, lval, ei, &v);
2065 if(FAILED(hres))
2066 return hres;
2068 hres = equal_values(ctx, &v, rval, ei, ret);
2070 VariantClear(&v);
2071 return hres;
2075 *ret = FALSE;
2076 return S_OK;
2079 /* ECMA-262 3rd Edition 11.9.1 */
2080 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2082 binary_expression_t *expr = (binary_expression_t*)_expr;
2083 VARIANT rval, lval;
2084 BOOL b;
2085 HRESULT hres;
2087 TRACE("\n");
2089 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2090 if(FAILED(hres))
2091 return hres;
2093 hres = equal_values(ctx, &rval, &lval, ei, &b);
2094 if(FAILED(hres))
2095 return hres;
2097 return return_bool(ret, b);
2100 /* ECMA-262 3rd Edition 11.9.4 */
2101 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2103 binary_expression_t *expr = (binary_expression_t*)_expr;
2104 VARIANT rval, lval;
2105 BOOL b;
2106 HRESULT hres;
2108 TRACE("\n");
2110 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2111 if(FAILED(hres))
2112 return hres;
2114 hres = equal2_values(&rval, &lval, &b);
2115 if(FAILED(hres))
2116 return hres;
2118 return return_bool(ret, b);
2121 /* ECMA-262 3rd Edition 11.9.2 */
2122 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2124 binary_expression_t *expr = (binary_expression_t*)_expr;
2125 VARIANT rval, lval;
2126 BOOL b;
2127 HRESULT hres;
2129 TRACE("\n");
2131 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2132 if(FAILED(hres))
2133 return hres;
2135 hres = equal_values(ctx, &lval, &rval, ei, &b);
2136 if(FAILED(hres))
2137 return hres;
2139 return return_bool(ret, !b);
2142 /* ECMA-262 3rd Edition 11.9.5 */
2143 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2145 binary_expression_t *expr = (binary_expression_t*)_expr;
2146 VARIANT rval, lval;
2147 BOOL b;
2148 HRESULT hres;
2150 TRACE("\n");
2152 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2153 if(FAILED(hres))
2154 return hres;
2156 hres = equal2_values(&rval, &lval, &b);
2157 if(FAILED(hres))
2158 return hres;
2160 return return_bool(ret, !b);
2163 /* ECMA-262 3rd Edition 11.8.5 */
2164 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2166 VARIANT l, r, ln, rn;
2167 HRESULT hres;
2169 hres = to_primitive(ctx->parser->script, lval, ei, &l);
2170 if(FAILED(hres))
2171 return hres;
2173 hres = to_primitive(ctx->parser->script, rval, ei, &r);
2174 if(FAILED(hres)) {
2175 VariantClear(&l);
2176 return hres;
2179 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2180 *ret = strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0;
2181 SysFreeString(V_BSTR(&l));
2182 SysFreeString(V_BSTR(&r));
2183 return S_OK;
2186 hres = to_number(ctx->parser->script, &l, ei, &ln);
2187 VariantClear(&l);
2188 if(SUCCEEDED(hres))
2189 hres = to_number(ctx->parser->script, &r, ei, &rn);
2190 VariantClear(&r);
2191 if(FAILED(hres))
2192 return hres;
2194 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4)
2195 *ret = V_I4(&ln) < V_I4(&rn);
2196 else
2197 *ret = num_val(&ln) < num_val(&rn);
2199 return S_OK;
2202 /* ECMA-262 3rd Edition 11.8.1 */
2203 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2205 binary_expression_t *expr = (binary_expression_t*)_expr;
2206 VARIANT rval, lval;
2207 BOOL b;
2208 HRESULT hres;
2210 TRACE("\n");
2212 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2213 if(FAILED(hres))
2214 return hres;
2216 hres = less_eval(ctx, &lval, &rval, ei, &b);
2217 VariantClear(&lval);
2218 VariantClear(&rval);
2219 if(FAILED(hres))
2220 return hres;
2222 return return_bool(ret, b);
2225 /* ECMA-262 3rd Edition 11.8.3 */
2226 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2228 binary_expression_t *expr = (binary_expression_t*)_expr;
2229 VARIANT rval, lval;
2230 BOOL b;
2231 HRESULT hres;
2233 TRACE("\n");
2235 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2236 if(FAILED(hres))
2237 return hres;
2239 hres = less_eval(ctx, &rval, &lval, ei, &b);
2240 VariantClear(&lval);
2241 VariantClear(&rval);
2242 if(FAILED(hres))
2243 return hres;
2245 return return_bool(ret, !b);
2248 /* ECMA-262 3rd Edition 11.8.2 */
2249 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2251 binary_expression_t *expr = (binary_expression_t*)_expr;
2252 VARIANT rval, lval;
2253 BOOL b;
2254 HRESULT hres;
2256 TRACE("\n");
2258 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2259 if(FAILED(hres))
2260 return hres;
2262 hres = less_eval(ctx, &rval, &lval, ei, &b);
2263 VariantClear(&lval);
2264 VariantClear(&rval);
2265 if(FAILED(hres))
2266 return hres;
2268 return return_bool(ret, b);
2271 /* ECMA-262 3rd Edition 11.8.4 */
2272 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2274 binary_expression_t *expr = (binary_expression_t*)_expr;
2275 VARIANT rval, lval;
2276 BOOL b;
2277 HRESULT hres;
2279 TRACE("\n");
2281 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2282 if(FAILED(hres))
2283 return hres;
2285 hres = less_eval(ctx, &lval, &rval, ei, &b);
2286 VariantClear(&lval);
2287 VariantClear(&rval);
2288 if(FAILED(hres))
2289 return hres;
2291 return return_bool(ret, !b);
2294 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2296 FIXME("\n");
2297 return E_NOTIMPL;
2300 /* ECMA-262 3rd Edition 11.4.9 */
2301 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2303 unary_expression_t *expr = (unary_expression_t*)_expr;
2304 exprval_t exprval;
2305 VARIANT_BOOL b;
2306 HRESULT hres;
2308 TRACE("\n");
2310 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2311 if(FAILED(hres))
2312 return hres;
2314 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
2315 exprval_release(&exprval);
2316 if(FAILED(hres))
2317 return hres;
2319 return return_bool(ret, !b);
2322 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2324 FIXME("\n");
2325 return E_NOTIMPL;
2328 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2330 FIXME("\n");
2331 return E_NOTIMPL;
2334 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2336 FIXME("\n");
2337 return E_NOTIMPL;
2340 /* ECMA-262 3rd Edition 11.13.1 */
2341 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2343 binary_expression_t *expr = (binary_expression_t*)_expr;
2344 exprval_t exprval, exprvalr;
2345 VARIANT rval;
2346 HRESULT hres;
2348 TRACE("\n");
2350 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
2351 if(FAILED(hres))
2352 return hres;
2354 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
2355 if(SUCCEEDED(hres)) {
2356 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
2357 exprval_release(&exprvalr);
2360 if(SUCCEEDED(hres))
2361 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
2363 exprval_release(&exprval);
2364 if(FAILED(hres)) {
2365 VariantClear(&rval);
2366 return hres;
2369 ret->type = EXPRVAL_VARIANT;
2370 ret->u.var = rval;
2371 return S_OK;
2374 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2376 FIXME("\n");
2377 return E_NOTIMPL;
2380 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2382 FIXME("\n");
2383 return E_NOTIMPL;
2386 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2388 FIXME("\n");
2389 return E_NOTIMPL;
2392 /* ECMA-262 3rd Edition 11.13.2 */
2393 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2395 binary_expression_t *expr = (binary_expression_t*)_expr;
2397 TRACE("\n");
2399 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
2402 /* ECMA-262 3rd Edition 11.13.2 */
2403 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2405 binary_expression_t *expr = (binary_expression_t*)_expr;
2407 TRACE("\n");
2409 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
2412 /* ECMA-262 3rd Edition 11.13.2 */
2413 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2415 binary_expression_t *expr = (binary_expression_t*)_expr;
2417 TRACE("\n");
2419 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
2422 /* ECMA-262 3rd Edition 11.13.2 */
2423 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2425 binary_expression_t *expr = (binary_expression_t*)_expr;
2427 TRACE("\n");
2429 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
2432 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2434 FIXME("\n");
2435 return E_NOTIMPL;
2438 /* ECMA-262 3rd Edition 11.13.2 */
2439 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2441 binary_expression_t *expr = (binary_expression_t*)_expr;
2443 TRACE("\n");
2445 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
2448 /* ECMA-262 3rd Edition 11.13.2 */
2449 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2451 binary_expression_t *expr = (binary_expression_t*)_expr;
2453 TRACE("\n");
2455 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
2458 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2460 FIXME("\n");
2461 return E_NOTIMPL;