kernel32/tests: Init pointers in common way, add some win_skip() while skipping.
[wine/hramrach.git] / dlls / jscript / engine.c
blobb9c40ae23c1ef3a692f7d69e91d458ebc119a58e
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 VariantClear(&val);
498 return S_OK;
501 /* ECMA-262 3rd Edition 10.1.4 */
502 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
504 scope_chain_t *scope;
505 named_item_t *item;
506 DISPID id = 0;
507 HRESULT hres;
509 TRACE("%s\n", debugstr_w(identifier));
511 for(scope = ctx->scope_chain; scope; scope = scope->next) {
512 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
513 if(SUCCEEDED(hres))
514 break;
517 if(scope) {
518 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
519 return S_OK;
522 hres = jsdisp_get_id(ctx->parser->script->global, identifier, 0, &id);
523 if(SUCCEEDED(hres)) {
524 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
525 return S_OK;
528 for(item = ctx->parser->script->named_items; item; item = item->next) {
529 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
530 if(!item->disp) {
531 IUnknown *unk;
533 if(!ctx->parser->script->site)
534 break;
536 hres = IActiveScriptSite_GetItemInfo(ctx->parser->script->site, identifier,
537 SCRIPTINFO_IUNKNOWN, &unk, NULL);
538 if(FAILED(hres)) {
539 WARN("GetItemInfo failed: %08x\n", hres);
540 break;
543 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
544 IUnknown_Release(unk);
545 if(FAILED(hres)) {
546 WARN("object does not implement IDispatch\n");
547 break;
551 ret->type = EXPRVAL_VARIANT;
552 V_VT(&ret->u.var) = VT_DISPATCH;
553 V_DISPATCH(&ret->u.var) = item->disp;
554 IDispatch_AddRef(item->disp);
555 return S_OK;
559 if(lookup_global_members(ctx->parser->script, identifier, ret))
560 return S_OK;
562 if(flags & EXPR_NEWREF) {
563 hres = jsdisp_get_id(ctx->parser->script->global, identifier, fdexNameEnsure, &id);
564 if(FAILED(hres))
565 return hres;
567 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
568 return S_OK;
571 ret->type = EXPRVAL_INVALID;
572 ret->u.identifier = SysAllocString(identifier);
573 if(!ret->u.identifier)
574 return E_OUTOFMEMORY;
576 return S_OK;
579 /* ECMA-262 3rd Edition 12.1 */
580 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
582 block_statement_t *stat = (block_statement_t*)_stat;
583 VARIANT val, tmp;
584 statement_t *iter;
585 HRESULT hres = S_OK;
587 TRACE("\n");
589 V_VT(&val) = VT_EMPTY;
590 for(iter = stat->stat_list; iter; iter = iter->next) {
591 hres = stat_eval(ctx, iter, rt, &tmp);
592 if(FAILED(hres))
593 break;
595 VariantClear(&val);
596 val = tmp;
597 if(rt->type != RT_NORMAL)
598 break;
601 if(FAILED(hres)) {
602 VariantClear(&val);
603 return hres;
606 *ret = val;
607 return S_OK;
610 /* ECMA-262 3rd Edition 12.2 */
611 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
613 variable_declaration_t *iter;
614 HRESULT hres = S_OK;
616 for(iter = var_list; iter; iter = iter->next) {
617 exprval_t exprval;
618 VARIANT val;
620 if(!iter->expr)
621 continue;
623 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
624 if(FAILED(hres))
625 break;
627 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
628 exprval_release(&exprval);
629 if(FAILED(hres))
630 break;
632 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
633 VariantClear(&val);
634 if(FAILED(hres))
635 break;
638 return hres;
641 /* ECMA-262 3rd Edition 12.2 */
642 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
644 var_statement_t *stat = (var_statement_t*)_stat;
645 HRESULT hres;
647 TRACE("\n");
649 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
650 if(FAILED(hres))
651 return hres;
653 V_VT(ret) = VT_EMPTY;
654 return S_OK;
657 /* ECMA-262 3rd Edition 12.3 */
658 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
660 TRACE("\n");
662 V_VT(ret) = VT_EMPTY;
663 return S_OK;
666 /* ECMA-262 3rd Edition 12.4 */
667 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
669 expression_statement_t *stat = (expression_statement_t*)_stat;
670 exprval_t exprval;
671 VARIANT val;
672 HRESULT hres;
674 TRACE("\n");
676 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
677 if(FAILED(hres))
678 return hres;
680 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
681 exprval_release(&exprval);
682 if(FAILED(hres))
683 return hres;
685 *ret = val;
686 TRACE("= %s\n", debugstr_variant(ret));
687 return S_OK;
690 /* ECMA-262 3rd Edition 12.5 */
691 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
693 if_statement_t *stat = (if_statement_t*)_stat;
694 exprval_t exprval;
695 VARIANT_BOOL b;
696 HRESULT hres;
698 TRACE("\n");
700 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
701 if(FAILED(hres))
702 return hres;
704 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
705 exprval_release(&exprval);
706 if(FAILED(hres))
707 return hres;
709 if(b)
710 hres = stat_eval(ctx, stat->if_stat, rt, ret);
711 else if(stat->else_stat)
712 hres = stat_eval(ctx, stat->else_stat, rt, ret);
713 else
714 V_VT(ret) = VT_EMPTY;
716 return hres;
719 /* ECMA-262 3rd Edition 12.6.2 */
720 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
722 while_statement_t *stat = (while_statement_t*)_stat;
723 exprval_t exprval;
724 VARIANT val, tmp;
725 VARIANT_BOOL b;
726 BOOL test_expr;
727 HRESULT hres;
729 TRACE("\n");
731 V_VT(&val) = VT_EMPTY;
732 test_expr = !stat->do_while;
734 while(1) {
735 if(test_expr) {
736 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
737 if(FAILED(hres))
738 break;
740 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
741 exprval_release(&exprval);
742 if(FAILED(hres) || !b)
743 break;
744 }else {
745 test_expr = TRUE;
748 hres = stat_eval(ctx, stat->statement, rt, &tmp);
749 if(FAILED(hres))
750 break;
752 VariantClear(&val);
753 val = tmp;
755 if(rt->type == RT_CONTINUE)
756 rt->type = RT_NORMAL;
757 if(rt->type != RT_NORMAL)
758 break;
761 if(FAILED(hres)) {
762 VariantClear(&val);
763 return hres;
766 if(rt->type == RT_BREAK)
767 rt->type = RT_NORMAL;
769 *ret = val;
770 return S_OK;
773 /* ECMA-262 3rd Edition 12.6.3 */
774 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
776 for_statement_t *stat = (for_statement_t*)_stat;
777 VARIANT val, tmp, retv;
778 exprval_t exprval;
779 VARIANT_BOOL b;
780 HRESULT hres;
782 TRACE("\n");
784 if(stat->variable_list) {
785 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
786 if(FAILED(hres))
787 return hres;
788 }else if(stat->begin_expr) {
789 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
790 if(FAILED(hres))
791 return hres;
793 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
794 exprval_release(&exprval);
795 if(FAILED(hres))
796 return hres;
798 VariantClear(&val);
801 V_VT(&retv) = VT_EMPTY;
803 while(1) {
804 if(stat->expr) {
805 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
806 if(FAILED(hres))
807 break;
809 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
810 exprval_release(&exprval);
811 if(FAILED(hres) || !b)
812 break;
815 hres = stat_eval(ctx, stat->statement, rt, &tmp);
816 if(FAILED(hres))
817 break;
819 VariantClear(&retv);
820 retv = tmp;
822 if(rt->type == RT_CONTINUE)
823 rt->type = RT_NORMAL;
824 else if(rt->type != RT_NORMAL)
825 break;
827 if(stat->end_expr) {
828 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
829 if(FAILED(hres))
830 break;
832 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
833 exprval_release(&exprval);
834 if(FAILED(hres))
835 break;
837 VariantClear(&val);
841 if(FAILED(hres)) {
842 VariantClear(&retv);
843 return hres;
846 if(rt->type == RT_BREAK)
847 rt->type = RT_NORMAL;
849 *ret = retv;
850 return S_OK;
853 /* ECMA-262 3rd Edition 12.6.4 */
854 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
856 forin_statement_t *stat = (forin_statement_t*)_stat;
857 VARIANT val, name, retv, tmp;
858 DISPID id = DISPID_STARTENUM;
859 BSTR str, identifier = NULL;
860 IDispatchEx *in_obj;
861 exprval_t exprval;
862 HRESULT hres;
864 TRACE("\n");
866 if(stat->variable) {
867 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
868 if(FAILED(hres))
869 return hres;
872 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
873 if(FAILED(hres))
874 return hres;
876 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
877 exprval_release(&exprval);
878 if(FAILED(hres))
879 return hres;
881 if(V_VT(&val) != VT_DISPATCH) {
882 TRACE("in vt %d\n", V_VT(&val));
883 VariantClear(&val);
884 V_VT(ret) = VT_EMPTY;
885 return S_OK;
888 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
889 IDispatch_Release(V_DISPATCH(&val));
890 if(FAILED(hres)) {
891 FIXME("Object doesn't support IDispatchEx\n");
892 return E_NOTIMPL;
895 V_VT(&retv) = VT_EMPTY;
897 if(stat->variable)
898 identifier = SysAllocString(stat->variable->identifier);
900 while(1) {
901 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
902 if(FAILED(hres) || hres == S_FALSE)
903 break;
905 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
906 if(FAILED(hres))
907 break;
909 TRACE("iter %s\n", debugstr_w(str));
911 if(stat->variable)
912 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
913 else
914 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
915 if(SUCCEEDED(hres)) {
916 V_VT(&name) = VT_BSTR;
917 V_BSTR(&name) = str;
918 hres = put_value(ctx->parser->script, &exprval, &name, &rt->ei);
919 exprval_release(&exprval);
921 SysFreeString(str);
922 if(FAILED(hres))
923 break;
925 hres = stat_eval(ctx, stat->statement, rt, &tmp);
926 if(FAILED(hres))
927 break;
929 VariantClear(&retv);
930 retv = tmp;
932 if(rt->type == RT_CONTINUE)
933 rt->type = RT_NORMAL;
934 else if(rt->type != RT_NORMAL)
935 break;
938 SysFreeString(identifier);
939 IDispatchEx_Release(in_obj);
940 if(FAILED(hres)) {
941 VariantClear(&retv);
942 return hres;
945 if(rt->type == RT_BREAK)
946 rt->type = RT_NORMAL;
948 *ret = retv;
949 return S_OK;
952 /* ECMA-262 3rd Edition 12.7 */
953 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
955 branch_statement_t *stat = (branch_statement_t*)_stat;
957 TRACE("\n");
959 if(stat->identifier) {
960 FIXME("indentifier not implemented\n");
961 return E_NOTIMPL;
964 rt->type = RT_CONTINUE;
965 V_VT(ret) = VT_EMPTY;
966 return S_OK;
969 /* ECMA-262 3rd Edition 12.8 */
970 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
972 branch_statement_t *stat = (branch_statement_t*)_stat;
974 TRACE("\n");
976 if(stat->identifier) {
977 FIXME("indentifier not implemented\n");
978 return E_NOTIMPL;
981 rt->type = RT_BREAK;
982 V_VT(ret) = VT_EMPTY;
983 return S_OK;
986 /* ECMA-262 3rd Edition 12.9 */
987 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
989 expression_statement_t *stat = (expression_statement_t*)_stat;
990 HRESULT hres;
992 TRACE("\n");
994 if(stat->expr) {
995 exprval_t exprval;
997 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
998 if(FAILED(hres))
999 return hres;
1001 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
1002 exprval_release(&exprval);
1003 if(FAILED(hres))
1004 return hres;
1005 }else {
1006 V_VT(ret) = VT_EMPTY;
1009 TRACE("= %s\n", debugstr_variant(ret));
1010 rt->type = RT_RETURN;
1011 return S_OK;
1014 /* ECMA-262 3rd Edition 12.10 */
1015 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1017 with_statement_t *stat = (with_statement_t*)_stat;
1018 exprval_t exprval;
1019 IDispatch *disp;
1020 DispatchEx *obj;
1021 VARIANT val;
1022 HRESULT hres;
1024 TRACE("\n");
1026 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1027 if(FAILED(hres))
1028 return hres;
1030 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1031 exprval_release(&exprval);
1032 if(FAILED(hres))
1033 return hres;
1035 hres = to_object(ctx->parser->script, &val, &disp);
1036 VariantClear(&val);
1037 if(FAILED(hres))
1038 return hres;
1040 obj = iface_to_jsdisp((IUnknown*)disp);
1041 IDispatch_Release(disp);
1042 if(!obj) {
1043 FIXME("disp id not jsdisp\n");
1044 return E_NOTIMPL;
1047 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
1048 jsdisp_release(obj);
1049 if(FAILED(hres))
1050 return hres;
1052 hres = stat_eval(ctx, stat->statement, rt, ret);
1054 scope_pop(&ctx->scope_chain);
1055 return hres;
1058 /* ECMA-262 3rd Edition 12.12 */
1059 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1061 FIXME("\n");
1062 return E_NOTIMPL;
1065 /* ECMA-262 3rd Edition 12.13 */
1066 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1068 switch_statement_t *stat = (switch_statement_t*)_stat;
1069 case_clausule_t *iter, *default_clausule = NULL;
1070 statement_t *stat_iter;
1071 VARIANT val, cval;
1072 exprval_t exprval;
1073 BOOL b;
1074 HRESULT hres;
1076 TRACE("\n");
1078 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1079 if(FAILED(hres))
1080 return hres;
1082 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1083 exprval_release(&exprval);
1084 if(FAILED(hres))
1085 return hres;
1087 for(iter = stat->case_list; iter; iter = iter->next) {
1088 if(!iter->expr) {
1089 default_clausule = iter;
1090 continue;
1093 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1094 if(FAILED(hres))
1095 break;
1097 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
1098 exprval_release(&exprval);
1099 if(FAILED(hres))
1100 break;
1102 hres = equal2_values(&val, &cval, &b);
1103 VariantClear(&cval);
1104 if(FAILED(hres) || b)
1105 break;
1108 VariantClear(&val);
1109 if(FAILED(hres))
1110 return hres;
1112 if(!iter)
1113 iter = default_clausule;
1115 V_VT(&val) = VT_EMPTY;
1116 if(iter) {
1117 VARIANT tmp;
1119 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1120 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1121 if(FAILED(hres))
1122 break;
1124 VariantClear(&val);
1125 val = tmp;
1127 if(rt->type != RT_NORMAL)
1128 break;
1132 if(FAILED(hres)) {
1133 VariantClear(&val);
1134 return hres;
1137 if(rt->type == RT_BREAK)
1138 rt->type = RT_NORMAL;
1140 *ret = val;
1141 return S_OK;
1144 /* ECMA-262 3rd Edition 12.13 */
1145 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1147 expression_statement_t *stat = (expression_statement_t*)_stat;
1148 exprval_t exprval;
1149 VARIANT val;
1150 HRESULT hres;
1152 TRACE("\n");
1154 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1155 if(FAILED(hres))
1156 return hres;
1158 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1159 exprval_release(&exprval);
1160 if(FAILED(hres))
1161 return hres;
1163 rt->ei.var = val;
1164 return DISP_E_EXCEPTION;
1167 /* ECMA-262 3rd Edition 12.14 */
1168 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1170 DispatchEx *var_disp;
1171 VARIANT ex, val;
1172 HRESULT hres;
1174 ex = rt->ei.var;
1175 memset(&rt->ei, 0, sizeof(jsexcept_t));
1177 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
1178 if(SUCCEEDED(hres)) {
1179 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1180 if(SUCCEEDED(hres)) {
1181 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
1182 if(SUCCEEDED(hres)) {
1183 hres = stat_eval(ctx, block->statement, rt, &val);
1184 scope_pop(&ctx->scope_chain);
1188 jsdisp_release(var_disp);
1191 VariantClear(&ex);
1192 if(FAILED(hres))
1193 return hres;
1195 *ret = val;
1196 return S_OK;
1199 /* ECMA-262 3rd Edition 12.14 */
1200 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1202 try_statement_t *stat = (try_statement_t*)_stat;
1203 VARIANT val;
1204 HRESULT hres;
1206 TRACE("\n");
1208 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1209 if(FAILED(hres)) {
1210 TRACE("EXCEPTION\n");
1211 if(!stat->catch_block)
1212 return hres;
1214 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1215 if(FAILED(hres))
1216 return hres;
1219 if(stat->finally_statement) {
1220 VariantClear(&val);
1221 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1222 if(FAILED(hres))
1223 return hres;
1226 *ret = val;
1227 return S_OK;
1230 static HRESULT return_bool(exprval_t *ret, DWORD b)
1232 ret->type = EXPRVAL_VARIANT;
1233 V_VT(&ret->u.var) = VT_BOOL;
1234 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1236 return S_OK;
1239 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1241 exprval_t exprval;
1242 HRESULT hres;
1244 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1245 if(FAILED(hres))
1246 return hres;
1248 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
1249 exprval_release(&exprval);
1250 if(FAILED(hres))
1251 return hres;
1253 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1254 if(SUCCEEDED(hres)) {
1255 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
1256 exprval_release(&exprval);
1259 if(FAILED(hres)) {
1260 VariantClear(lval);
1261 return hres;
1264 return S_OK;
1267 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1269 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1270 exprval_t *ret)
1272 VARIANT lval, rval, retv;
1273 HRESULT hres;
1275 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1276 if(FAILED(hres))
1277 return hres;
1279 hres = oper(ctx, &lval, &rval, ei, &retv);
1280 VariantClear(&lval);
1281 VariantClear(&rval);
1282 if(FAILED(hres))
1283 return hres;
1285 ret->type = EXPRVAL_VARIANT;
1286 ret->u.var = retv;
1287 return S_OK;
1290 /* ECMA-262 3rd Edition 11.13.2 */
1291 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1292 jsexcept_t *ei, exprval_t *ret)
1294 VARIANT retv, lval, rval;
1295 exprval_t exprval, exprvalr;
1296 HRESULT hres;
1298 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1299 if(FAILED(hres))
1300 return hres;
1302 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
1303 if(SUCCEEDED(hres)) {
1304 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1305 if(SUCCEEDED(hres)) {
1306 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
1307 exprval_release(&exprvalr);
1309 if(SUCCEEDED(hres)) {
1310 hres = oper(ctx, &lval, &rval, ei, &retv);
1311 VariantClear(&rval);
1313 VariantClear(&lval);
1316 if(SUCCEEDED(hres)) {
1317 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
1318 if(FAILED(hres))
1319 VariantClear(&retv);
1321 exprval_release(&exprval);
1323 if(FAILED(hres))
1324 return hres;
1326 ret->type = EXPRVAL_VARIANT;
1327 ret->u.var = retv;
1328 return S_OK;
1331 /* ECMA-262 3rd Edition 13 */
1332 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1334 function_expression_t *expr = (function_expression_t*)_expr;
1335 VARIANT var;
1336 HRESULT hres;
1338 TRACE("\n");
1340 if(expr->identifier) {
1341 hres = jsdisp_propget_name(ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1342 if(FAILED(hres))
1343 return hres;
1344 }else {
1345 DispatchEx *dispex;
1347 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1348 expr->src_str, expr->src_len, &dispex);
1349 if(FAILED(hres))
1350 return hres;
1352 V_VT(&var) = VT_DISPATCH;
1353 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1356 ret->type = EXPRVAL_VARIANT;
1357 ret->u.var = var;
1358 return S_OK;
1361 /* ECMA-262 3rd Edition 11.12 */
1362 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1364 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1365 exprval_t exprval;
1366 VARIANT_BOOL b;
1367 HRESULT hres;
1369 TRACE("\n");
1371 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1372 if(FAILED(hres))
1373 return hres;
1375 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1376 exprval_release(&exprval);
1377 if(FAILED(hres))
1378 return hres;
1380 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1383 /* ECMA-262 3rd Edition 11.2.1 */
1384 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1386 array_expression_t *expr = (array_expression_t*)_expr;
1387 exprval_t exprval;
1388 VARIANT member, val;
1389 DISPID id;
1390 BSTR str;
1391 IDispatch *obj = NULL;
1392 HRESULT hres;
1394 TRACE("\n");
1396 hres = expr_eval(ctx, expr->member_expr, 0, ei, &exprval);
1397 if(FAILED(hres))
1398 return hres;
1400 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1401 exprval_release(&exprval);
1402 if(FAILED(hres))
1403 return hres;
1405 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1406 if(SUCCEEDED(hres)) {
1407 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1408 exprval_release(&exprval);
1411 if(SUCCEEDED(hres))
1412 hres = to_object(ctx->parser->script, &member, &obj);
1413 VariantClear(&member);
1414 if(SUCCEEDED(hres)) {
1415 hres = to_string(ctx->parser->script, &val, ei, &str);
1416 if(SUCCEEDED(hres)) {
1417 if(flags & EXPR_STRREF) {
1418 ret->type = EXPRVAL_NAMEREF;
1419 ret->u.nameref.disp = obj;
1420 ret->u.nameref.name = str;
1421 return S_OK;
1424 hres = disp_get_id(ctx->parser->script, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1427 if(SUCCEEDED(hres)) {
1428 exprval_set_idref(ret, obj, id);
1429 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1430 exprval_init(ret);
1431 hres = S_OK;
1434 IDispatch_Release(obj);
1437 return hres;
1440 /* ECMA-262 3rd Edition 11.2.1 */
1441 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1443 member_expression_t *expr = (member_expression_t*)_expr;
1444 IDispatch *obj = NULL;
1445 exprval_t exprval;
1446 VARIANT member;
1447 DISPID id;
1448 BSTR str;
1449 HRESULT hres;
1451 TRACE("\n");
1453 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1454 if(FAILED(hres))
1455 return hres;
1457 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1458 exprval_release(&exprval);
1459 if(FAILED(hres))
1460 return hres;
1462 hres = to_object(ctx->parser->script, &member, &obj);
1463 VariantClear(&member);
1464 if(FAILED(hres))
1465 return hres;
1467 str = SysAllocString(expr->identifier);
1468 if(flags & EXPR_STRREF) {
1469 ret->type = EXPRVAL_NAMEREF;
1470 ret->u.nameref.disp = obj;
1471 ret->u.nameref.name = str;
1472 return S_OK;
1475 hres = disp_get_id(ctx->parser->script, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1476 SysFreeString(str);
1477 if(SUCCEEDED(hres)) {
1478 exprval_set_idref(ret, obj, id);
1479 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1480 exprval_init(ret);
1481 hres = S_OK;
1484 IDispatch_Release(obj);
1485 return hres;
1488 static void free_dp(DISPPARAMS *dp)
1490 DWORD i;
1492 for(i=0; i < dp->cArgs; i++)
1493 VariantClear(dp->rgvarg+i);
1494 heap_free(dp->rgvarg);
1497 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1499 VARIANTARG *vargs;
1500 exprval_t exprval;
1501 argument_t *iter;
1502 DWORD cnt = 0, i;
1503 HRESULT hres = S_OK;
1505 memset(dp, 0, sizeof(*dp));
1506 if(!args)
1507 return S_OK;
1509 for(iter = args; iter; iter = iter->next)
1510 cnt++;
1512 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1513 if(!vargs)
1514 return E_OUTOFMEMORY;
1516 for(i = cnt, iter = args; iter; iter = iter->next) {
1517 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1518 if(FAILED(hres))
1519 break;
1521 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1522 exprval_release(&exprval);
1523 if(FAILED(hres))
1524 break;
1527 if(FAILED(hres)) {
1528 free_dp(dp);
1529 return hres;
1532 dp->rgvarg = vargs;
1533 dp->cArgs = cnt;
1534 return S_OK;
1537 /* ECMA-262 3rd Edition 11.2.2 */
1538 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1540 call_expression_t *expr = (call_expression_t*)_expr;
1541 exprval_t exprval;
1542 VARIANT constr, var;
1543 DISPPARAMS dp;
1544 HRESULT hres;
1546 TRACE("\n");
1548 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1549 if(FAILED(hres))
1550 return hres;
1552 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1553 if(SUCCEEDED(hres))
1554 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1555 exprval_release(&exprval);
1556 if(FAILED(hres))
1557 return hres;
1559 if(V_VT(&constr) != VT_DISPATCH) {
1560 FIXME("throw TypeError\n");
1561 VariantClear(&constr);
1562 return E_FAIL;
1565 hres = disp_call(ctx->parser->script, V_DISPATCH(&constr), DISPID_VALUE,
1566 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1567 IDispatch_Release(V_DISPATCH(&constr));
1568 if(FAILED(hres))
1569 return hres;
1571 ret->type = EXPRVAL_VARIANT;
1572 ret->u.var = var;
1573 return S_OK;
1576 /* ECMA-262 3rd Edition 11.2.3 */
1577 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1579 call_expression_t *expr = (call_expression_t*)_expr;
1580 VARIANT var;
1581 exprval_t exprval;
1582 DISPPARAMS dp;
1583 HRESULT hres;
1585 TRACE("\n");
1587 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1588 if(FAILED(hres))
1589 return hres;
1591 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1592 if(SUCCEEDED(hres)) {
1593 switch(exprval.type) {
1594 case EXPRVAL_VARIANT:
1595 if(V_VT(&exprval.u.var) == VT_DISPATCH)
1596 hres = disp_call(ctx->parser->script, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1597 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1598 else
1599 hres = throw_type_error(ctx->parser->script, ei, IDS_NO_PROPERTY, NULL);
1600 break;
1601 case EXPRVAL_IDREF:
1602 hres = disp_call(ctx->parser->script, exprval.u.idref.disp, exprval.u.idref.id,
1603 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1604 break;
1605 case EXPRVAL_INVALID:
1606 hres = throw_type_error(ctx->parser->script, ei, IDS_OBJECT_EXPECTED, NULL);
1607 break;
1608 default:
1609 FIXME("unimplemented type %d\n", exprval.type);
1610 hres = E_NOTIMPL;
1613 free_dp(&dp);
1616 exprval_release(&exprval);
1617 if(FAILED(hres))
1618 return hres;
1620 ret->type = EXPRVAL_VARIANT;
1621 if(flags & EXPR_NOVAL) {
1622 V_VT(&ret->u.var) = VT_EMPTY;
1623 }else {
1624 TRACE("= %s\n", debugstr_variant(&var));
1625 ret->u.var = var;
1627 return S_OK;
1630 /* ECMA-262 3rd Edition 11.1.1 */
1631 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1633 TRACE("\n");
1635 ret->type = EXPRVAL_VARIANT;
1636 V_VT(&ret->u.var) = VT_DISPATCH;
1637 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1638 IDispatch_AddRef(ctx->this_obj);
1639 return S_OK;
1642 /* ECMA-262 3rd Edition 10.1.4 */
1643 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1645 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1646 BSTR identifier;
1647 HRESULT hres;
1649 TRACE("\n");
1651 identifier = SysAllocString(expr->identifier);
1652 if(!identifier)
1653 return E_OUTOFMEMORY;
1655 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1657 SysFreeString(identifier);
1658 return hres;
1661 /* ECMA-262 3rd Edition 7.8 */
1662 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1664 literal_expression_t *expr = (literal_expression_t*)_expr;
1665 VARIANT var;
1666 HRESULT hres;
1668 TRACE("\n");
1670 hres = literal_to_var(ctx->parser->script, expr->literal, &var);
1671 if(FAILED(hres))
1672 return hres;
1674 ret->type = EXPRVAL_VARIANT;
1675 ret->u.var = var;
1676 return S_OK;
1679 /* ECMA-262 3rd Edition 11.1.4 */
1680 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1682 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1683 DWORD length = 0, i = 0;
1684 array_element_t *elem;
1685 DispatchEx *array;
1686 exprval_t exprval;
1687 VARIANT val;
1688 HRESULT hres;
1690 TRACE("\n");
1692 for(elem = expr->element_list; elem; elem = elem->next)
1693 length += elem->elision+1;
1694 length += expr->length;
1696 hres = create_array(ctx->parser->script, length, &array);
1697 if(FAILED(hres))
1698 return hres;
1700 for(elem = expr->element_list; elem; elem = elem->next) {
1701 i += elem->elision;
1703 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1704 if(FAILED(hres))
1705 break;
1707 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1708 exprval_release(&exprval);
1709 if(FAILED(hres))
1710 break;
1712 hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
1713 VariantClear(&val);
1714 if(FAILED(hres))
1715 break;
1717 i++;
1720 if(FAILED(hres)) {
1721 jsdisp_release(array);
1722 return hres;
1725 ret->type = EXPRVAL_VARIANT;
1726 V_VT(&ret->u.var) = VT_DISPATCH;
1727 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(array);
1728 return S_OK;
1731 /* ECMA-262 3rd Edition 11.1.5 */
1732 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1734 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1735 VARIANT val, tmp;
1736 DispatchEx *obj;
1737 prop_val_t *iter;
1738 exprval_t exprval;
1739 BSTR name;
1740 HRESULT hres;
1742 TRACE("\n");
1744 hres = create_object(ctx->parser->script, NULL, &obj);
1745 if(FAILED(hres))
1746 return hres;
1748 for(iter = expr->property_list; iter; iter = iter->next) {
1749 hres = literal_to_var(ctx->parser->script, iter->name, &tmp);
1750 if(FAILED(hres))
1751 break;
1753 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1754 VariantClear(&tmp);
1755 if(FAILED(hres))
1756 break;
1758 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1759 if(SUCCEEDED(hres)) {
1760 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1761 exprval_release(&exprval);
1762 if(SUCCEEDED(hres)) {
1763 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
1764 VariantClear(&val);
1768 SysFreeString(name);
1769 if(FAILED(hres))
1770 break;
1773 if(FAILED(hres)) {
1774 jsdisp_release(obj);
1775 return hres;
1778 ret->type = EXPRVAL_VARIANT;
1779 V_VT(&ret->u.var) = VT_DISPATCH;
1780 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1781 return S_OK;
1784 /* ECMA-262 3rd Edition 11.14 */
1785 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1787 binary_expression_t *expr = (binary_expression_t*)_expr;
1788 VARIANT lval, rval;
1789 HRESULT hres;
1791 TRACE("\n");
1793 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1794 if(FAILED(hres))
1795 return hres;
1797 VariantClear(&lval);
1799 ret->type = EXPRVAL_VARIANT;
1800 ret->u.var = rval;
1801 return S_OK;
1804 /* ECMA-262 3rd Edition 11.11 */
1805 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1807 binary_expression_t *expr = (binary_expression_t*)_expr;
1808 exprval_t exprval;
1809 VARIANT_BOOL b;
1810 VARIANT val;
1811 HRESULT hres;
1813 TRACE("\n");
1815 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1816 if(FAILED(hres))
1817 return hres;
1819 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1820 exprval_release(&exprval);
1821 if(FAILED(hres))
1822 return hres;
1824 hres = to_boolean(&val, &b);
1825 if(SUCCEEDED(hres) && b) {
1826 ret->type = EXPRVAL_VARIANT;
1827 ret->u.var = val;
1828 return S_OK;
1831 VariantClear(&val);
1832 if(FAILED(hres))
1833 return hres;
1835 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1836 if(FAILED(hres))
1837 return hres;
1839 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1840 exprval_release(&exprval);
1841 if(FAILED(hres))
1842 return hres;
1844 ret->type = EXPRVAL_VARIANT;
1845 ret->u.var = val;
1846 return S_OK;
1849 /* ECMA-262 3rd Edition 11.11 */
1850 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1852 binary_expression_t *expr = (binary_expression_t*)_expr;
1853 exprval_t exprval;
1854 VARIANT_BOOL b;
1855 VARIANT val;
1856 HRESULT hres;
1858 TRACE("\n");
1860 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1861 if(FAILED(hres))
1862 return hres;
1864 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1865 exprval_release(&exprval);
1866 if(FAILED(hres))
1867 return hres;
1869 hres = to_boolean(&val, &b);
1870 if(SUCCEEDED(hres) && !b) {
1871 ret->type = EXPRVAL_VARIANT;
1872 ret->u.var = val;
1873 return S_OK;
1876 VariantClear(&val);
1877 if(FAILED(hres))
1878 return hres;
1880 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1881 if(FAILED(hres))
1882 return hres;
1884 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1885 exprval_release(&exprval);
1886 if(FAILED(hres))
1887 return hres;
1889 ret->type = EXPRVAL_VARIANT;
1890 ret->u.var = val;
1891 return S_OK;
1894 /* ECMA-262 3rd Edition 11.10 */
1895 static HRESULT bitor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1897 INT li, ri;
1898 HRESULT hres;
1900 hres = to_int32(ctx->parser->script, lval, ei, &li);
1901 if(FAILED(hres))
1902 return hres;
1904 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1905 if(FAILED(hres))
1906 return hres;
1908 V_VT(retv) = VT_I4;
1909 V_I4(retv) = li|ri;
1910 return S_OK;
1913 /* ECMA-262 3rd Edition 11.10 */
1914 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1916 binary_expression_t *expr = (binary_expression_t*)_expr;
1918 TRACE("\n");
1920 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
1923 /* ECMA-262 3rd Edition 11.10 */
1924 static HRESULT xor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1926 INT li, ri;
1927 HRESULT hres;
1929 hres = to_int32(ctx->parser->script, lval, ei, &li);
1930 if(FAILED(hres))
1931 return hres;
1933 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1934 if(FAILED(hres))
1935 return hres;
1937 V_VT(retv) = VT_I4;
1938 V_I4(retv) = li^ri;
1939 return S_OK;
1942 /* ECMA-262 3rd Edition 11.10 */
1943 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1945 binary_expression_t *expr = (binary_expression_t*)_expr;
1947 TRACE("\n");
1949 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
1952 /* ECMA-262 3rd Edition 11.10 */
1953 static HRESULT bitand_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1955 INT li, ri;
1956 HRESULT hres;
1958 hres = to_int32(ctx->parser->script, lval, ei, &li);
1959 if(FAILED(hres))
1960 return hres;
1962 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1963 if(FAILED(hres))
1964 return hres;
1966 V_VT(retv) = VT_I4;
1967 V_I4(retv) = li&ri;
1968 return S_OK;
1971 /* ECMA-262 3rd Edition 11.10 */
1972 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1974 binary_expression_t *expr = (binary_expression_t*)_expr;
1976 TRACE("\n");
1978 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
1981 /* ECMA-262 3rd Edition 11.8.6 */
1982 static HRESULT instanceof_eval(exec_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
1984 DispatchEx *obj, *iter, *tmp = NULL;
1985 VARIANT_BOOL ret = VARIANT_FALSE;
1986 BOOL b;
1987 VARIANT var;
1988 HRESULT hres;
1990 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1992 if(V_VT(objv) != VT_DISPATCH) {
1993 FIXME("throw TypeError\n");
1994 return E_FAIL;
1997 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
1998 if(!obj) {
1999 FIXME("throw TypeError\n");
2000 return E_FAIL;
2003 if(is_class(obj, JSCLASS_FUNCTION)) {
2004 hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2005 }else {
2006 FIXME("throw TypeError\n");
2007 hres = E_FAIL;
2009 jsdisp_release(obj);
2010 if(FAILED(hres))
2011 return hres;
2013 if(V_VT(&var) == VT_DISPATCH) {
2014 if(V_VT(inst) == VT_DISPATCH)
2015 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2016 for(iter = tmp; iter; iter = iter->prototype) {
2017 hres = disp_cmp(V_DISPATCH(&var), (IDispatch*)_IDispatchEx_(iter), &b);
2018 if(FAILED(hres))
2019 break;
2020 if(b) {
2021 ret = VARIANT_TRUE;
2022 break;
2026 if(tmp)
2027 jsdisp_release(tmp);
2028 }else {
2029 FIXME("prototype is not an object\n");
2030 hres = E_FAIL;
2033 VariantClear(&var);
2034 if(FAILED(hres))
2035 return hres;
2037 V_VT(retv) = VT_BOOL;
2038 V_BOOL(retv) = ret;
2039 return S_OK;
2042 /* ECMA-262 3rd Edition 11.8.6 */
2043 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2045 binary_expression_t *expr = (binary_expression_t*)_expr;
2047 TRACE("\n");
2049 return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2052 /* ECMA-262 3rd Edition 11.8.7 */
2053 static HRESULT in_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *obj, jsexcept_t *ei, VARIANT *retv)
2055 VARIANT_BOOL ret;
2056 DISPID id;
2057 BSTR str;
2058 HRESULT hres;
2060 if(V_VT(obj) != VT_DISPATCH) {
2061 FIXME("throw TypeError\n");
2062 return E_FAIL;
2065 hres = to_string(ctx->parser->script, lval, ei, &str);
2066 if(FAILED(hres))
2067 return hres;
2069 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2070 SysFreeString(str);
2071 if(SUCCEEDED(hres))
2072 ret = VARIANT_TRUE;
2073 else if(hres == DISP_E_UNKNOWNNAME)
2074 ret = VARIANT_FALSE;
2075 else
2076 return hres;
2078 V_VT(retv) = VT_BOOL;
2079 V_BOOL(retv) = ret;
2080 return S_OK;
2083 /* ECMA-262 3rd Edition 11.8.7 */
2084 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2086 binary_expression_t *expr = (binary_expression_t*)_expr;
2088 TRACE("\n");
2090 return binary_expr_eval(ctx, expr, in_eval, ei, ret);
2093 /* ECMA-262 3rd Edition 11.6.1 */
2094 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2096 VARIANT r, l;
2097 HRESULT hres;
2099 hres = to_primitive(ctx->parser->script, lval, ei, &l, NO_HINT);
2100 if(FAILED(hres))
2101 return hres;
2103 hres = to_primitive(ctx->parser->script, rval, ei, &r, NO_HINT);
2104 if(FAILED(hres)) {
2105 VariantClear(&l);
2106 return hres;
2109 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2110 BSTR lstr = NULL, rstr = NULL;
2112 if(V_VT(&l) == VT_BSTR)
2113 lstr = V_BSTR(&l);
2114 else
2115 hres = to_string(ctx->parser->script, &l, ei, &lstr);
2117 if(SUCCEEDED(hres)) {
2118 if(V_VT(&r) == VT_BSTR)
2119 rstr = V_BSTR(&r);
2120 else
2121 hres = to_string(ctx->parser->script, &r, ei, &rstr);
2124 if(SUCCEEDED(hres)) {
2125 int len1, len2;
2127 len1 = SysStringLen(lstr);
2128 len2 = SysStringLen(rstr);
2130 V_VT(retv) = VT_BSTR;
2131 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2132 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2133 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2136 if(V_VT(&l) != VT_BSTR)
2137 SysFreeString(lstr);
2138 if(V_VT(&r) != VT_BSTR)
2139 SysFreeString(rstr);
2140 }else {
2141 VARIANT nl, nr;
2143 hres = to_number(ctx->parser->script, &l, ei, &nl);
2144 if(SUCCEEDED(hres)) {
2145 hres = to_number(ctx->parser->script, &r, ei, &nr);
2146 if(SUCCEEDED(hres))
2147 num_set_val(retv, num_val(&nl) + num_val(&nr));
2151 VariantClear(&r);
2152 VariantClear(&l);
2153 return hres;
2156 /* ECMA-262 3rd Edition 11.6.1 */
2157 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2159 binary_expression_t *expr = (binary_expression_t*)_expr;
2161 TRACE("\n");
2163 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
2166 /* ECMA-262 3rd Edition 11.6.2 */
2167 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2169 VARIANT lnum, rnum;
2170 HRESULT hres;
2172 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2173 if(FAILED(hres))
2174 return hres;
2176 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2177 if(FAILED(hres))
2178 return hres;
2180 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
2181 return S_OK;
2184 /* ECMA-262 3rd Edition 11.6.2 */
2185 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2187 binary_expression_t *expr = (binary_expression_t*)_expr;
2189 TRACE("\n");
2191 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
2194 /* ECMA-262 3rd Edition 11.5.1 */
2195 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2197 VARIANT lnum, rnum;
2198 HRESULT hres;
2200 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2201 if(FAILED(hres))
2202 return hres;
2204 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2205 if(FAILED(hres))
2206 return hres;
2208 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2209 return S_OK;
2212 /* ECMA-262 3rd Edition 11.5.1 */
2213 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2215 binary_expression_t *expr = (binary_expression_t*)_expr;
2217 TRACE("\n");
2219 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2222 /* ECMA-262 3rd Edition 11.5.2 */
2223 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2225 VARIANT lnum, rnum;
2226 HRESULT hres;
2228 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2229 if(FAILED(hres))
2230 return hres;
2232 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2233 if(FAILED(hres))
2234 return hres;
2236 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2237 return S_OK;
2240 /* ECMA-262 3rd Edition 11.5.2 */
2241 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2243 binary_expression_t *expr = (binary_expression_t*)_expr;
2245 TRACE("\n");
2247 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2250 /* ECMA-262 3rd Edition 11.5.3 */
2251 static HRESULT mod_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2253 VARIANT lnum, rnum;
2254 HRESULT hres;
2256 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2257 if(FAILED(hres))
2258 return hres;
2260 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2261 if(FAILED(hres))
2262 return hres;
2264 num_set_val(retv, fmod(num_val(&lnum), num_val(&rnum)));
2265 return S_OK;
2268 /* ECMA-262 3rd Edition 11.5.3 */
2269 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2271 binary_expression_t *expr = (binary_expression_t*)_expr;
2273 TRACE("\n");
2275 return binary_expr_eval(ctx, expr, mod_eval, ei, ret);
2278 /* ECMA-262 3rd Edition 11.4.2 */
2279 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2281 unary_expression_t *expr = (unary_expression_t*)_expr;
2282 VARIANT_BOOL b = VARIANT_FALSE;
2283 exprval_t exprval;
2284 HRESULT hres;
2286 TRACE("\n");
2288 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2289 if(FAILED(hres))
2290 return hres;
2292 switch(exprval.type) {
2293 case EXPRVAL_IDREF: {
2294 IDispatchEx *dispex;
2296 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2297 if(SUCCEEDED(hres)) {
2298 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2299 b = VARIANT_TRUE;
2300 IDispatchEx_Release(dispex);
2302 break;
2304 case EXPRVAL_NAMEREF: {
2305 IDispatchEx *dispex;
2307 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2308 if(SUCCEEDED(hres)) {
2309 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name,
2310 make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2311 b = VARIANT_TRUE;
2312 IDispatchEx_Release(dispex);
2314 break;
2316 default:
2317 FIXME("unsupported type %d\n", exprval.type);
2318 hres = E_NOTIMPL;
2321 exprval_release(&exprval);
2322 if(FAILED(hres))
2323 return hres;
2325 return return_bool(ret, b);
2328 /* ECMA-262 3rd Edition 11.4.2 */
2329 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2331 unary_expression_t *expr = (unary_expression_t*)_expr;
2332 exprval_t exprval;
2333 VARIANT tmp;
2334 HRESULT hres;
2336 TRACE("\n");
2338 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2339 if(FAILED(hres))
2340 return hres;
2342 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &tmp);
2343 exprval_release(&exprval);
2344 if(FAILED(hres))
2345 return hres;
2347 VariantClear(&tmp);
2349 ret->type = EXPRVAL_VARIANT;
2350 V_VT(&ret->u.var) = VT_EMPTY;
2351 return S_OK;
2354 /* ECMA-262 3rd Edition 11.4.3 */
2355 static HRESULT typeof_exprval(exec_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2357 VARIANT val;
2358 HRESULT hres;
2360 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2361 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2362 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2363 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2364 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2365 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2367 if(exprval->type == EXPRVAL_INVALID) {
2368 *ret = undefinedW;
2369 return S_OK;
2372 hres = exprval_to_value(ctx->parser->script, exprval, ei, &val);
2373 if(FAILED(hres))
2374 return hres;
2376 switch(V_VT(&val)) {
2377 case VT_EMPTY:
2378 *ret = undefinedW;
2379 break;
2380 case VT_NULL:
2381 *ret = objectW;
2382 break;
2383 case VT_BOOL:
2384 *ret = booleanW;
2385 break;
2386 case VT_I4:
2387 case VT_R8:
2388 *ret = numberW;
2389 break;
2390 case VT_BSTR:
2391 *ret = stringW;
2392 break;
2393 case VT_DISPATCH: {
2394 DispatchEx *dispex;
2396 if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2397 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2398 jsdisp_release(dispex);
2399 }else {
2400 *ret = objectW;
2402 break;
2404 default:
2405 FIXME("unhandled vt %d\n", V_VT(&val));
2406 hres = E_NOTIMPL;
2409 VariantClear(&val);
2410 return S_OK;
2413 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2415 unary_expression_t *expr = (unary_expression_t*)_expr;
2416 const WCHAR *str = NULL;
2417 exprval_t exprval;
2418 HRESULT hres;
2420 TRACE("\n");
2422 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2423 if(FAILED(hres))
2424 return hres;
2426 hres = typeof_exprval(ctx, &exprval, ei, &str);
2427 exprval_release(&exprval);
2428 if(FAILED(hres))
2429 return hres;
2431 ret->type = EXPRVAL_VARIANT;
2432 V_VT(&ret->u.var) = VT_BSTR;
2433 V_BSTR(&ret->u.var) = SysAllocString(str);
2434 if(!V_BSTR(&ret->u.var))
2435 return E_OUTOFMEMORY;
2437 return S_OK;
2440 /* ECMA-262 3rd Edition 11.4.7 */
2441 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2443 unary_expression_t *expr = (unary_expression_t*)_expr;
2444 exprval_t exprval;
2445 VARIANT val, num;
2446 HRESULT hres;
2448 TRACE("\n");
2450 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2451 if(FAILED(hres))
2452 return hres;
2454 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2455 exprval_release(&exprval);
2456 if(FAILED(hres))
2457 return hres;
2459 hres = to_number(ctx->parser->script, &val, ei, &num);
2460 VariantClear(&val);
2461 if(FAILED(hres))
2462 return hres;
2464 ret->type = EXPRVAL_VARIANT;
2465 num_set_val(&ret->u.var, -num_val(&num));
2466 return S_OK;
2469 /* ECMA-262 3rd Edition 11.4.6 */
2470 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2472 unary_expression_t *expr = (unary_expression_t*)_expr;
2473 exprval_t exprval;
2474 VARIANT val, num;
2475 HRESULT hres;
2477 TRACE("\n");
2479 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2480 if(FAILED(hres))
2481 return hres;
2483 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2484 exprval_release(&exprval);
2485 if(FAILED(hres))
2486 return hres;
2488 hres = to_number(ctx->parser->script, &val, ei, &num);
2489 if(FAILED(hres))
2490 return hres;
2492 ret->type = EXPRVAL_VARIANT;
2493 ret->u.var = num;
2494 return S_OK;
2497 /* ECMA-262 3rd Edition 11.3.1 */
2498 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2500 unary_expression_t *expr = (unary_expression_t*)_expr;
2501 VARIANT val, num;
2502 exprval_t exprval;
2503 HRESULT hres;
2505 TRACE("\n");
2507 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2508 if(FAILED(hres))
2509 return hres;
2511 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2512 if(SUCCEEDED(hres)) {
2513 hres = to_number(ctx->parser->script, &val, ei, &num);
2514 VariantClear(&val);
2517 if(SUCCEEDED(hres)) {
2518 VARIANT inc;
2519 num_set_val(&inc, num_val(&num)+1.0);
2520 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
2523 exprval_release(&exprval);
2524 if(FAILED(hres))
2525 return hres;
2527 ret->type = EXPRVAL_VARIANT;
2528 ret->u.var = num;
2529 return S_OK;
2532 /* ECMA-262 3rd Edition 11.3.2 */
2533 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2535 unary_expression_t *expr = (unary_expression_t*)_expr;
2536 VARIANT val, num;
2537 exprval_t exprval;
2538 HRESULT hres;
2540 TRACE("\n");
2542 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2543 if(FAILED(hres))
2544 return hres;
2546 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2547 if(SUCCEEDED(hres)) {
2548 hres = to_number(ctx->parser->script, &val, ei, &num);
2549 VariantClear(&val);
2552 if(SUCCEEDED(hres)) {
2553 VARIANT dec;
2554 num_set_val(&dec, num_val(&num)-1.0);
2555 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
2558 exprval_release(&exprval);
2559 if(FAILED(hres))
2560 return hres;
2562 ret->type = EXPRVAL_VARIANT;
2563 ret->u.var = num;
2564 return S_OK;
2567 /* ECMA-262 3rd Edition 11.4.4 */
2568 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2570 unary_expression_t *expr = (unary_expression_t*)_expr;
2571 VARIANT val, num;
2572 exprval_t exprval;
2573 HRESULT hres;
2575 TRACE("\n");
2577 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2578 if(FAILED(hres))
2579 return hres;
2581 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2582 if(SUCCEEDED(hres)) {
2583 hres = to_number(ctx->parser->script, &val, ei, &num);
2584 VariantClear(&val);
2587 if(SUCCEEDED(hres)) {
2588 num_set_val(&val, num_val(&num)+1.0);
2589 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2592 exprval_release(&exprval);
2593 if(FAILED(hres))
2594 return hres;
2596 ret->type = EXPRVAL_VARIANT;
2597 ret->u.var = val;
2598 return S_OK;
2601 /* ECMA-262 3rd Edition 11.4.5 */
2602 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2604 unary_expression_t *expr = (unary_expression_t*)_expr;
2605 VARIANT val, num;
2606 exprval_t exprval;
2607 HRESULT hres;
2609 TRACE("\n");
2611 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2612 if(FAILED(hres))
2613 return hres;
2615 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2616 if(SUCCEEDED(hres)) {
2617 hres = to_number(ctx->parser->script, &val, ei, &num);
2618 VariantClear(&val);
2621 if(SUCCEEDED(hres)) {
2622 num_set_val(&val, num_val(&num)-1.0);
2623 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2626 exprval_release(&exprval);
2627 if(FAILED(hres))
2628 return hres;
2630 ret->type = EXPRVAL_VARIANT;
2631 ret->u.var = val;
2632 return S_OK;
2635 /* ECMA-262 3rd Edition 11.9.3 */
2636 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2638 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2639 return equal2_values(lval, rval, ret);
2641 /* FIXME: NULL disps should be handled in more general way */
2642 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2643 VARIANT v;
2644 V_VT(&v) = VT_NULL;
2645 return equal_values(ctx, &v, rval, ei, ret);
2648 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2649 VARIANT v;
2650 V_VT(&v) = VT_NULL;
2651 return equal_values(ctx, lval, &v, ei, ret);
2654 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2655 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2656 *ret = TRUE;
2657 return S_OK;
2660 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2661 VARIANT v;
2662 HRESULT hres;
2664 hres = to_number(ctx->parser->script, lval, ei, &v);
2665 if(FAILED(hres))
2666 return hres;
2668 return equal_values(ctx, &v, rval, ei, ret);
2671 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2672 VARIANT v;
2673 HRESULT hres;
2675 hres = to_number(ctx->parser->script, rval, ei, &v);
2676 if(FAILED(hres))
2677 return hres;
2679 return equal_values(ctx, lval, &v, ei, ret);
2682 if(V_VT(rval) == VT_BOOL) {
2683 VARIANT v;
2685 V_VT(&v) = VT_I4;
2686 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2687 return equal_values(ctx, lval, &v, ei, ret);
2690 if(V_VT(lval) == VT_BOOL) {
2691 VARIANT v;
2693 V_VT(&v) = VT_I4;
2694 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2695 return equal_values(ctx, &v, rval, ei, ret);
2699 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2700 VARIANT v;
2701 HRESULT hres;
2703 hres = to_primitive(ctx->parser->script, rval, ei, &v, NO_HINT);
2704 if(FAILED(hres))
2705 return hres;
2707 hres = equal_values(ctx, lval, &v, ei, ret);
2709 VariantClear(&v);
2710 return hres;
2714 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2715 VARIANT v;
2716 HRESULT hres;
2718 hres = to_primitive(ctx->parser->script, lval, ei, &v, NO_HINT);
2719 if(FAILED(hres))
2720 return hres;
2722 hres = equal_values(ctx, &v, rval, ei, ret);
2724 VariantClear(&v);
2725 return hres;
2729 *ret = FALSE;
2730 return S_OK;
2733 /* ECMA-262 3rd Edition 11.9.1 */
2734 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2736 binary_expression_t *expr = (binary_expression_t*)_expr;
2737 VARIANT rval, lval;
2738 BOOL b;
2739 HRESULT hres;
2741 TRACE("\n");
2743 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2744 if(FAILED(hres))
2745 return hres;
2747 hres = equal_values(ctx, &rval, &lval, ei, &b);
2748 if(FAILED(hres))
2749 return hres;
2751 return return_bool(ret, b);
2754 /* ECMA-262 3rd Edition 11.9.4 */
2755 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2757 binary_expression_t *expr = (binary_expression_t*)_expr;
2758 VARIANT rval, lval;
2759 BOOL b;
2760 HRESULT hres;
2762 TRACE("\n");
2764 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2765 if(FAILED(hres))
2766 return hres;
2768 hres = equal2_values(&rval, &lval, &b);
2769 if(FAILED(hres))
2770 return hres;
2772 return return_bool(ret, b);
2775 /* ECMA-262 3rd Edition 11.9.2 */
2776 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2778 binary_expression_t *expr = (binary_expression_t*)_expr;
2779 VARIANT rval, lval;
2780 BOOL b;
2781 HRESULT hres;
2783 TRACE("\n");
2785 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2786 if(FAILED(hres))
2787 return hres;
2789 hres = equal_values(ctx, &lval, &rval, ei, &b);
2790 if(FAILED(hres))
2791 return hres;
2793 return return_bool(ret, !b);
2796 /* ECMA-262 3rd Edition 11.9.5 */
2797 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2799 binary_expression_t *expr = (binary_expression_t*)_expr;
2800 VARIANT rval, lval;
2801 BOOL b;
2802 HRESULT hres;
2804 TRACE("\n");
2806 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2807 if(FAILED(hres))
2808 return hres;
2810 hres = equal2_values(&lval, &rval, &b);
2811 if(FAILED(hres))
2812 return hres;
2814 return return_bool(ret, !b);
2817 /* ECMA-262 3rd Edition 11.8.5 */
2818 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2820 VARIANT l, r, ln, rn;
2821 HRESULT hres;
2823 hres = to_primitive(ctx->parser->script, lval, ei, &l, NO_HINT);
2824 if(FAILED(hres))
2825 return hres;
2827 hres = to_primitive(ctx->parser->script, rval, ei, &r, NO_HINT);
2828 if(FAILED(hres)) {
2829 VariantClear(&l);
2830 return hres;
2833 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2834 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2835 SysFreeString(V_BSTR(&l));
2836 SysFreeString(V_BSTR(&r));
2837 return S_OK;
2840 hres = to_number(ctx->parser->script, &l, ei, &ln);
2841 VariantClear(&l);
2842 if(SUCCEEDED(hres))
2843 hres = to_number(ctx->parser->script, &r, ei, &rn);
2844 VariantClear(&r);
2845 if(FAILED(hres))
2846 return hres;
2848 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2849 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2850 }else {
2851 DOUBLE ld = num_val(&ln);
2852 DOUBLE rd = num_val(&rn);
2854 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2857 return S_OK;
2860 /* ECMA-262 3rd Edition 11.8.1 */
2861 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2863 binary_expression_t *expr = (binary_expression_t*)_expr;
2864 VARIANT rval, lval;
2865 BOOL b;
2866 HRESULT hres;
2868 TRACE("\n");
2870 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2871 if(FAILED(hres))
2872 return hres;
2874 hres = less_eval(ctx, &lval, &rval, FALSE, ei, &b);
2875 VariantClear(&lval);
2876 VariantClear(&rval);
2877 if(FAILED(hres))
2878 return hres;
2880 return return_bool(ret, b);
2883 /* ECMA-262 3rd Edition 11.8.3 */
2884 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2886 binary_expression_t *expr = (binary_expression_t*)_expr;
2887 VARIANT rval, lval;
2888 BOOL b;
2889 HRESULT hres;
2891 TRACE("\n");
2893 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2894 if(FAILED(hres))
2895 return hres;
2897 hres = less_eval(ctx, &rval, &lval, TRUE, ei, &b);
2898 VariantClear(&lval);
2899 VariantClear(&rval);
2900 if(FAILED(hres))
2901 return hres;
2903 return return_bool(ret, b);
2906 /* ECMA-262 3rd Edition 11.8.2 */
2907 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2909 binary_expression_t *expr = (binary_expression_t*)_expr;
2910 VARIANT rval, lval;
2911 BOOL b;
2912 HRESULT hres;
2914 TRACE("\n");
2916 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2917 if(FAILED(hres))
2918 return hres;
2920 hres = less_eval(ctx, &rval, &lval, FALSE, ei, &b);
2921 VariantClear(&lval);
2922 VariantClear(&rval);
2923 if(FAILED(hres))
2924 return hres;
2926 return return_bool(ret, b);
2929 /* ECMA-262 3rd Edition 11.8.4 */
2930 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2932 binary_expression_t *expr = (binary_expression_t*)_expr;
2933 VARIANT rval, lval;
2934 BOOL b;
2935 HRESULT hres;
2937 TRACE("\n");
2939 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2940 if(FAILED(hres))
2941 return hres;
2943 hres = less_eval(ctx, &lval, &rval, TRUE, ei, &b);
2944 VariantClear(&lval);
2945 VariantClear(&rval);
2946 if(FAILED(hres))
2947 return hres;
2949 return return_bool(ret, b);
2952 /* ECMA-262 3rd Edition 11.4.8 */
2953 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2955 unary_expression_t *expr = (unary_expression_t*)_expr;
2956 exprval_t exprval;
2957 VARIANT val;
2958 INT i;
2959 HRESULT hres;
2961 TRACE("\n");
2963 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2964 if(FAILED(hres))
2965 return hres;
2967 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2968 exprval_release(&exprval);
2969 if(FAILED(hres))
2970 return hres;
2972 hres = to_int32(ctx->parser->script, &val, ei, &i);
2973 if(FAILED(hres))
2974 return hres;
2976 ret->type = EXPRVAL_VARIANT;
2977 V_VT(&ret->u.var) = VT_I4;
2978 V_I4(&ret->u.var) = ~i;
2979 return S_OK;
2982 /* ECMA-262 3rd Edition 11.4.9 */
2983 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2985 unary_expression_t *expr = (unary_expression_t*)_expr;
2986 exprval_t exprval;
2987 VARIANT_BOOL b;
2988 HRESULT hres;
2990 TRACE("\n");
2992 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2993 if(FAILED(hres))
2994 return hres;
2996 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
2997 exprval_release(&exprval);
2998 if(FAILED(hres))
2999 return hres;
3001 return return_bool(ret, !b);
3004 /* ECMA-262 3rd Edition 11.7.1 */
3005 static HRESULT lshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3007 DWORD ri;
3008 INT li;
3009 HRESULT hres;
3011 hres = to_int32(ctx->parser->script, lval, ei, &li);
3012 if(FAILED(hres))
3013 return hres;
3015 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3016 if(FAILED(hres))
3017 return hres;
3019 V_VT(retv) = VT_I4;
3020 V_I4(retv) = li << (ri&0x1f);
3021 return S_OK;
3024 /* ECMA-262 3rd Edition 11.7.1 */
3025 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3027 binary_expression_t *expr = (binary_expression_t*)_expr;
3029 TRACE("\n");
3031 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3034 /* ECMA-262 3rd Edition 11.7.2 */
3035 static HRESULT rshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3037 DWORD ri;
3038 INT li;
3039 HRESULT hres;
3041 hres = to_int32(ctx->parser->script, lval, ei, &li);
3042 if(FAILED(hres))
3043 return hres;
3045 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3046 if(FAILED(hres))
3047 return hres;
3049 V_VT(retv) = VT_I4;
3050 V_I4(retv) = li >> (ri&0x1f);
3051 return S_OK;
3054 /* ECMA-262 3rd Edition 11.7.2 */
3055 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3057 binary_expression_t *expr = (binary_expression_t*)_expr;
3059 TRACE("\n");
3061 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3064 /* ECMA-262 3rd Edition 11.7.3 */
3065 static HRESULT rshift2_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3067 DWORD li, ri;
3068 HRESULT hres;
3070 hres = to_uint32(ctx->parser->script, lval, ei, &li);
3071 if(FAILED(hres))
3072 return hres;
3074 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3075 if(FAILED(hres))
3076 return hres;
3078 V_VT(retv) = VT_I4;
3079 V_I4(retv) = li >> (ri&0x1f);
3080 return S_OK;
3083 /* ECMA-262 3rd Edition 11.7.3 */
3084 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3086 binary_expression_t *expr = (binary_expression_t*)_expr;
3088 TRACE("\n");
3090 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3093 /* ECMA-262 3rd Edition 11.13.1 */
3094 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3096 binary_expression_t *expr = (binary_expression_t*)_expr;
3097 exprval_t exprval, exprvalr;
3098 VARIANT rval;
3099 HRESULT hres;
3101 TRACE("\n");
3103 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
3104 if(FAILED(hres))
3105 return hres;
3107 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
3108 if(SUCCEEDED(hres)) {
3109 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
3110 exprval_release(&exprvalr);
3113 if(SUCCEEDED(hres))
3114 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
3116 exprval_release(&exprval);
3117 if(FAILED(hres))
3118 return hres;
3120 ret->type = EXPRVAL_VARIANT;
3121 ret->u.var = rval;
3122 return S_OK;
3125 /* ECMA-262 3rd Edition 11.13.2 */
3126 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3128 binary_expression_t *expr = (binary_expression_t*)_expr;
3130 TRACE("\n");
3132 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3135 /* ECMA-262 3rd Edition 11.13.2 */
3136 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3138 binary_expression_t *expr = (binary_expression_t*)_expr;
3140 TRACE("\n");
3142 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3145 /* ECMA-262 3rd Edition 11.13.2 */
3146 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3148 binary_expression_t *expr = (binary_expression_t*)_expr;
3150 TRACE("\n");
3152 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3155 /* ECMA-262 3rd Edition 11.13.2 */
3156 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3158 binary_expression_t *expr = (binary_expression_t*)_expr;
3160 TRACE("\n");
3162 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
3165 /* ECMA-262 3rd Edition 11.13.2 */
3166 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3168 binary_expression_t *expr = (binary_expression_t*)_expr;
3170 TRACE("\n");
3172 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
3175 /* ECMA-262 3rd Edition 11.13.2 */
3176 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3178 binary_expression_t *expr = (binary_expression_t*)_expr;
3180 TRACE("\n");
3182 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
3185 /* ECMA-262 3rd Edition 11.13.2 */
3186 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3188 binary_expression_t *expr = (binary_expression_t*)_expr;
3190 TRACE("\n");
3192 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
3195 /* ECMA-262 3rd Edition 11.13.2 */
3196 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3198 binary_expression_t *expr = (binary_expression_t*)_expr;
3200 TRACE("\n");
3202 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mod_eval, ei, ret);
3205 /* ECMA-262 3rd Edition 11.13.2 */
3206 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3208 binary_expression_t *expr = (binary_expression_t*)_expr;
3210 TRACE("\n");
3212 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3215 /* ECMA-262 3rd Edition 11.13.2 */
3216 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3218 binary_expression_t *expr = (binary_expression_t*)_expr;
3220 TRACE("\n");
3222 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
3225 /* ECMA-262 3rd Edition 11.13.2 */
3226 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3228 binary_expression_t *expr = (binary_expression_t*)_expr;
3230 TRACE("\n");
3232 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);