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