comctl32: Implement imagelist drag functions, GetOverlayImage.
[wine/multimedia.git] / dlls / jscript / engine.c
blob15ef90552af4db6290335c1d8d7805de5d0e958a
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_UNDEFINED:
354 V_VT(v) = VT_EMPTY;
355 break;
356 case LT_NULL:
357 V_VT(v) = VT_NULL;
358 break;
359 case LT_INT:
360 V_VT(v) = VT_I4;
361 V_I4(v) = literal->u.lval;
362 break;
363 case LT_DOUBLE:
364 V_VT(v) = VT_R8;
365 V_R8(v) = literal->u.dval;
366 break;
367 case LT_STRING: {
368 BSTR str = SysAllocString(literal->u.wstr);
369 if(!str)
370 return E_OUTOFMEMORY;
372 V_VT(v) = VT_BSTR;
373 V_BSTR(v) = str;
374 break;
376 case LT_BOOL:
377 V_VT(v) = VT_BOOL;
378 V_BOOL(v) = literal->u.bval;
379 break;
380 case LT_REGEXP: {
381 DispatchEx *regexp;
382 HRESULT hres;
384 hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
385 literal->u.regexp.flags, &regexp);
386 if(FAILED(hres))
387 return hres;
389 V_VT(v) = VT_DISPATCH;
390 V_DISPATCH(v) = (IDispatch*)_IDispatchEx_(regexp);
394 return S_OK;
397 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
399 named_item_t *item;
400 DISPID id;
401 HRESULT hres;
403 for(item = ctx->named_items; item; item = item->next) {
404 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
405 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
406 if(SUCCEEDED(hres)) {
407 if(ret)
408 exprval_set_idref(ret, item->disp, id);
409 return TRUE;
414 return FALSE;
417 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, exec_type_t exec_type,
418 jsexcept_t *ei, VARIANT *retv)
420 script_ctx_t *script = parser->script;
421 function_declaration_t *func;
422 parser_ctx_t *prev_parser;
423 var_list_t *var;
424 VARIANT val, tmp;
425 statement_t *stat;
426 exec_ctx_t *prev_ctx;
427 return_type_t rt;
428 HRESULT hres = S_OK;
430 for(func = source->functions; func; func = func->next) {
431 DispatchEx *func_obj;
432 VARIANT var;
434 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
435 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
436 if(FAILED(hres))
437 return hres;
439 V_VT(&var) = VT_DISPATCH;
440 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
441 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
442 jsdisp_release(func_obj);
443 if(FAILED(hres))
444 return hres;
447 for(var = source->variables; var; var = var->next) {
448 DISPID id = 0;
449 BSTR name;
451 name = SysAllocString(var->identifier);
452 if(!name)
453 return E_OUTOFMEMORY;
455 if(!lookup_global_members(parser->script, name, NULL))
456 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
457 SysFreeString(name);
458 if(FAILED(hres))
459 return hres;
462 prev_ctx = script->exec_ctx;
463 script->exec_ctx = ctx;
465 prev_parser = ctx->parser;
466 ctx->parser = parser;
468 V_VT(&val) = VT_EMPTY;
469 memset(&rt, 0, sizeof(rt));
470 rt.type = RT_NORMAL;
472 for(stat = source->statement; stat; stat = stat->next) {
473 hres = stat_eval(ctx, stat, &rt, &tmp);
474 if(FAILED(hres))
475 break;
477 VariantClear(&val);
478 val = tmp;
479 if(rt.type != RT_NORMAL)
480 break;
483 script->exec_ctx = prev_ctx;
484 ctx->parser = prev_parser;
486 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
487 FIXME("wrong rt %d\n", rt.type);
488 hres = E_FAIL;
491 *ei = rt.ei;
492 if(FAILED(hres)) {
493 VariantClear(&val);
494 return hres;
497 if(retv && (exec_type == EXECT_EVAL || rt.type == RT_RETURN))
498 *retv = val;
499 else
500 VariantClear(&val);
501 return S_OK;
504 /* ECMA-262 3rd Edition 10.1.4 */
505 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
507 scope_chain_t *scope;
508 named_item_t *item;
509 DISPID id = 0;
510 HRESULT hres;
512 TRACE("%s\n", debugstr_w(identifier));
514 for(scope = ctx->scope_chain; scope; scope = scope->next) {
515 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
516 if(SUCCEEDED(hres))
517 break;
520 if(scope) {
521 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
522 return S_OK;
525 hres = jsdisp_get_id(ctx->parser->script->global, identifier, 0, &id);
526 if(SUCCEEDED(hres)) {
527 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
528 return S_OK;
531 for(item = ctx->parser->script->named_items; item; item = item->next) {
532 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
533 if(!item->disp) {
534 IUnknown *unk;
536 if(!ctx->parser->script->site)
537 break;
539 hres = IActiveScriptSite_GetItemInfo(ctx->parser->script->site, identifier,
540 SCRIPTINFO_IUNKNOWN, &unk, NULL);
541 if(FAILED(hres)) {
542 WARN("GetItemInfo failed: %08x\n", hres);
543 break;
546 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
547 IUnknown_Release(unk);
548 if(FAILED(hres)) {
549 WARN("object does not implement IDispatch\n");
550 break;
554 ret->type = EXPRVAL_VARIANT;
555 V_VT(&ret->u.var) = VT_DISPATCH;
556 V_DISPATCH(&ret->u.var) = item->disp;
557 IDispatch_AddRef(item->disp);
558 return S_OK;
562 if(lookup_global_members(ctx->parser->script, identifier, ret))
563 return S_OK;
565 if(flags & EXPR_NEWREF) {
566 hres = jsdisp_get_id(ctx->parser->script->global, identifier, fdexNameEnsure, &id);
567 if(FAILED(hres))
568 return hres;
570 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
571 return S_OK;
574 ret->type = EXPRVAL_INVALID;
575 ret->u.identifier = SysAllocString(identifier);
576 if(!ret->u.identifier)
577 return E_OUTOFMEMORY;
579 return S_OK;
582 /* ECMA-262 3rd Edition 12.1 */
583 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
585 block_statement_t *stat = (block_statement_t*)_stat;
586 VARIANT val, tmp;
587 statement_t *iter;
588 HRESULT hres = S_OK;
590 TRACE("\n");
592 V_VT(&val) = VT_EMPTY;
593 for(iter = stat->stat_list; iter; iter = iter->next) {
594 hres = stat_eval(ctx, iter, rt, &tmp);
595 if(FAILED(hres))
596 break;
598 VariantClear(&val);
599 val = tmp;
600 if(rt->type != RT_NORMAL)
601 break;
604 if(FAILED(hres)) {
605 VariantClear(&val);
606 return hres;
609 *ret = val;
610 return S_OK;
613 /* ECMA-262 3rd Edition 12.2 */
614 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
616 variable_declaration_t *iter;
617 HRESULT hres = S_OK;
619 for(iter = var_list; iter; iter = iter->next) {
620 exprval_t exprval;
621 VARIANT val;
623 if(!iter->expr)
624 continue;
626 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
627 if(FAILED(hres))
628 break;
630 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
631 exprval_release(&exprval);
632 if(FAILED(hres))
633 break;
635 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
636 VariantClear(&val);
637 if(FAILED(hres))
638 break;
641 return hres;
644 /* ECMA-262 3rd Edition 12.2 */
645 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
647 var_statement_t *stat = (var_statement_t*)_stat;
648 HRESULT hres;
650 TRACE("\n");
652 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
653 if(FAILED(hres))
654 return hres;
656 V_VT(ret) = VT_EMPTY;
657 return S_OK;
660 /* ECMA-262 3rd Edition 12.3 */
661 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
663 TRACE("\n");
665 V_VT(ret) = VT_EMPTY;
666 return S_OK;
669 /* ECMA-262 3rd Edition 12.4 */
670 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
672 expression_statement_t *stat = (expression_statement_t*)_stat;
673 exprval_t exprval;
674 VARIANT val;
675 HRESULT hres;
677 TRACE("\n");
679 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
680 if(FAILED(hres))
681 return hres;
683 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
684 exprval_release(&exprval);
685 if(FAILED(hres))
686 return hres;
688 *ret = val;
689 TRACE("= %s\n", debugstr_variant(ret));
690 return S_OK;
693 /* ECMA-262 3rd Edition 12.5 */
694 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
696 if_statement_t *stat = (if_statement_t*)_stat;
697 exprval_t exprval;
698 VARIANT_BOOL b;
699 HRESULT hres;
701 TRACE("\n");
703 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
704 if(FAILED(hres))
705 return hres;
707 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
708 exprval_release(&exprval);
709 if(FAILED(hres))
710 return hres;
712 if(b)
713 hres = stat_eval(ctx, stat->if_stat, rt, ret);
714 else if(stat->else_stat)
715 hres = stat_eval(ctx, stat->else_stat, rt, ret);
716 else
717 V_VT(ret) = VT_EMPTY;
719 return hres;
722 /* ECMA-262 3rd Edition 12.6.2 */
723 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
725 while_statement_t *stat = (while_statement_t*)_stat;
726 exprval_t exprval;
727 VARIANT val, tmp;
728 VARIANT_BOOL b;
729 BOOL test_expr;
730 HRESULT hres;
732 TRACE("\n");
734 V_VT(&val) = VT_EMPTY;
735 test_expr = !stat->do_while;
737 while(1) {
738 if(test_expr) {
739 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
740 if(FAILED(hres))
741 break;
743 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
744 exprval_release(&exprval);
745 if(FAILED(hres) || !b)
746 break;
747 }else {
748 test_expr = TRUE;
751 hres = stat_eval(ctx, stat->statement, rt, &tmp);
752 if(FAILED(hres))
753 break;
755 VariantClear(&val);
756 val = tmp;
758 if(rt->type == RT_CONTINUE)
759 rt->type = RT_NORMAL;
760 if(rt->type != RT_NORMAL)
761 break;
764 if(FAILED(hres)) {
765 VariantClear(&val);
766 return hres;
769 if(rt->type == RT_BREAK)
770 rt->type = RT_NORMAL;
772 *ret = val;
773 return S_OK;
776 /* ECMA-262 3rd Edition 12.6.3 */
777 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
779 for_statement_t *stat = (for_statement_t*)_stat;
780 VARIANT val, tmp, retv;
781 exprval_t exprval;
782 VARIANT_BOOL b;
783 HRESULT hres;
785 TRACE("\n");
787 if(stat->variable_list) {
788 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
789 if(FAILED(hres))
790 return hres;
791 }else if(stat->begin_expr) {
792 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
793 if(FAILED(hres))
794 return hres;
796 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
797 exprval_release(&exprval);
798 if(FAILED(hres))
799 return hres;
801 VariantClear(&val);
804 V_VT(&retv) = VT_EMPTY;
806 while(1) {
807 if(stat->expr) {
808 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
809 if(FAILED(hres))
810 break;
812 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
813 exprval_release(&exprval);
814 if(FAILED(hres) || !b)
815 break;
818 hres = stat_eval(ctx, stat->statement, rt, &tmp);
819 if(FAILED(hres))
820 break;
822 VariantClear(&retv);
823 retv = tmp;
825 if(rt->type == RT_CONTINUE)
826 rt->type = RT_NORMAL;
827 else if(rt->type != RT_NORMAL)
828 break;
830 if(stat->end_expr) {
831 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
832 if(FAILED(hres))
833 break;
835 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
836 exprval_release(&exprval);
837 if(FAILED(hres))
838 break;
840 VariantClear(&val);
844 if(FAILED(hres)) {
845 VariantClear(&retv);
846 return hres;
849 if(rt->type == RT_BREAK)
850 rt->type = RT_NORMAL;
852 *ret = retv;
853 return S_OK;
856 /* ECMA-262 3rd Edition 12.6.4 */
857 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
859 forin_statement_t *stat = (forin_statement_t*)_stat;
860 VARIANT val, name, retv, tmp;
861 DISPID id = DISPID_STARTENUM;
862 BSTR str, identifier = NULL;
863 IDispatchEx *in_obj;
864 exprval_t exprval;
865 HRESULT hres;
867 TRACE("\n");
869 if(stat->variable) {
870 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
871 if(FAILED(hres))
872 return hres;
875 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
876 if(FAILED(hres))
877 return hres;
879 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
880 exprval_release(&exprval);
881 if(FAILED(hres))
882 return hres;
884 if(V_VT(&val) != VT_DISPATCH) {
885 TRACE("in vt %d\n", V_VT(&val));
886 VariantClear(&val);
887 V_VT(ret) = VT_EMPTY;
888 return S_OK;
891 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
892 IDispatch_Release(V_DISPATCH(&val));
893 if(FAILED(hres)) {
894 FIXME("Object doesn't support IDispatchEx\n");
895 return E_NOTIMPL;
898 V_VT(&retv) = VT_EMPTY;
900 if(stat->variable)
901 identifier = SysAllocString(stat->variable->identifier);
903 while(1) {
904 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
905 if(FAILED(hres) || hres == S_FALSE)
906 break;
908 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
909 if(FAILED(hres))
910 break;
912 TRACE("iter %s\n", debugstr_w(str));
914 if(stat->variable)
915 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
916 else
917 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
918 if(SUCCEEDED(hres)) {
919 V_VT(&name) = VT_BSTR;
920 V_BSTR(&name) = str;
921 hres = put_value(ctx->parser->script, &exprval, &name, &rt->ei);
922 exprval_release(&exprval);
924 SysFreeString(str);
925 if(FAILED(hres))
926 break;
928 hres = stat_eval(ctx, stat->statement, rt, &tmp);
929 if(FAILED(hres))
930 break;
932 VariantClear(&retv);
933 retv = tmp;
935 if(rt->type == RT_CONTINUE)
936 rt->type = RT_NORMAL;
937 else if(rt->type != RT_NORMAL)
938 break;
941 SysFreeString(identifier);
942 IDispatchEx_Release(in_obj);
943 if(FAILED(hres)) {
944 VariantClear(&retv);
945 return hres;
948 if(rt->type == RT_BREAK)
949 rt->type = RT_NORMAL;
951 *ret = retv;
952 return S_OK;
955 /* ECMA-262 3rd Edition 12.7 */
956 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
958 branch_statement_t *stat = (branch_statement_t*)_stat;
960 TRACE("\n");
962 if(stat->identifier) {
963 FIXME("indentifier not implemented\n");
964 return E_NOTIMPL;
967 rt->type = RT_CONTINUE;
968 V_VT(ret) = VT_EMPTY;
969 return S_OK;
972 /* ECMA-262 3rd Edition 12.8 */
973 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
975 branch_statement_t *stat = (branch_statement_t*)_stat;
977 TRACE("\n");
979 if(stat->identifier) {
980 FIXME("indentifier not implemented\n");
981 return E_NOTIMPL;
984 rt->type = RT_BREAK;
985 V_VT(ret) = VT_EMPTY;
986 return S_OK;
989 /* ECMA-262 3rd Edition 12.9 */
990 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
992 expression_statement_t *stat = (expression_statement_t*)_stat;
993 HRESULT hres;
995 TRACE("\n");
997 if(stat->expr) {
998 exprval_t exprval;
1000 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1001 if(FAILED(hres))
1002 return hres;
1004 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
1005 exprval_release(&exprval);
1006 if(FAILED(hres))
1007 return hres;
1008 }else {
1009 V_VT(ret) = VT_EMPTY;
1012 TRACE("= %s\n", debugstr_variant(ret));
1013 rt->type = RT_RETURN;
1014 return S_OK;
1017 /* ECMA-262 3rd Edition 12.10 */
1018 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1020 with_statement_t *stat = (with_statement_t*)_stat;
1021 exprval_t exprval;
1022 IDispatch *disp;
1023 DispatchEx *obj;
1024 VARIANT val;
1025 HRESULT hres;
1027 TRACE("\n");
1029 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1030 if(FAILED(hres))
1031 return hres;
1033 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1034 exprval_release(&exprval);
1035 if(FAILED(hres))
1036 return hres;
1038 hres = to_object(ctx->parser->script, &val, &disp);
1039 VariantClear(&val);
1040 if(FAILED(hres))
1041 return hres;
1043 obj = iface_to_jsdisp((IUnknown*)disp);
1044 IDispatch_Release(disp);
1045 if(!obj) {
1046 FIXME("disp id not jsdisp\n");
1047 return E_NOTIMPL;
1050 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
1051 jsdisp_release(obj);
1052 if(FAILED(hres))
1053 return hres;
1055 hres = stat_eval(ctx, stat->statement, rt, ret);
1057 scope_pop(&ctx->scope_chain);
1058 return hres;
1061 /* ECMA-262 3rd Edition 12.12 */
1062 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1064 FIXME("\n");
1065 return E_NOTIMPL;
1068 /* ECMA-262 3rd Edition 12.13 */
1069 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1071 switch_statement_t *stat = (switch_statement_t*)_stat;
1072 case_clausule_t *iter, *default_clausule = NULL;
1073 statement_t *stat_iter;
1074 VARIANT val, cval;
1075 exprval_t exprval;
1076 BOOL b;
1077 HRESULT hres;
1079 TRACE("\n");
1081 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1082 if(FAILED(hres))
1083 return hres;
1085 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1086 exprval_release(&exprval);
1087 if(FAILED(hres))
1088 return hres;
1090 for(iter = stat->case_list; iter; iter = iter->next) {
1091 if(!iter->expr) {
1092 default_clausule = iter;
1093 continue;
1096 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1097 if(FAILED(hres))
1098 break;
1100 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &cval);
1101 exprval_release(&exprval);
1102 if(FAILED(hres))
1103 break;
1105 hres = equal2_values(&val, &cval, &b);
1106 VariantClear(&cval);
1107 if(FAILED(hres) || b)
1108 break;
1111 VariantClear(&val);
1112 if(FAILED(hres))
1113 return hres;
1115 if(!iter)
1116 iter = default_clausule;
1118 V_VT(&val) = VT_EMPTY;
1119 if(iter) {
1120 VARIANT tmp;
1122 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1123 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1124 if(FAILED(hres))
1125 break;
1127 VariantClear(&val);
1128 val = tmp;
1130 if(rt->type != RT_NORMAL)
1131 break;
1135 if(FAILED(hres)) {
1136 VariantClear(&val);
1137 return hres;
1140 if(rt->type == RT_BREAK)
1141 rt->type = RT_NORMAL;
1143 *ret = val;
1144 return S_OK;
1147 /* ECMA-262 3rd Edition 12.13 */
1148 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1150 expression_statement_t *stat = (expression_statement_t*)_stat;
1151 exprval_t exprval;
1152 VARIANT val;
1153 HRESULT hres;
1155 TRACE("\n");
1157 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1158 if(FAILED(hres))
1159 return hres;
1161 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
1162 exprval_release(&exprval);
1163 if(FAILED(hres))
1164 return hres;
1166 rt->ei.var = val;
1167 return DISP_E_EXCEPTION;
1170 /* ECMA-262 3rd Edition 12.14 */
1171 static HRESULT catch_eval(exec_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1173 DispatchEx *var_disp;
1174 VARIANT ex, val;
1175 HRESULT hres;
1177 ex = rt->ei.var;
1178 memset(&rt->ei, 0, sizeof(jsexcept_t));
1180 hres = create_dispex(ctx->parser->script, NULL, NULL, &var_disp);
1181 if(SUCCEEDED(hres)) {
1182 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1183 if(SUCCEEDED(hres)) {
1184 hres = scope_push(ctx->scope_chain, var_disp, &ctx->scope_chain);
1185 if(SUCCEEDED(hres)) {
1186 hres = stat_eval(ctx, block->statement, rt, &val);
1187 scope_pop(&ctx->scope_chain);
1191 jsdisp_release(var_disp);
1194 VariantClear(&ex);
1195 if(FAILED(hres))
1196 return hres;
1198 *ret = val;
1199 return S_OK;
1202 /* ECMA-262 3rd Edition 12.14 */
1203 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1205 try_statement_t *stat = (try_statement_t*)_stat;
1206 VARIANT val;
1207 HRESULT hres;
1209 TRACE("\n");
1211 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1212 if(FAILED(hres)) {
1213 TRACE("EXCEPTION\n");
1214 if(!stat->catch_block)
1215 return hres;
1217 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1218 if(FAILED(hres))
1219 return hres;
1222 if(stat->finally_statement) {
1223 VariantClear(&val);
1224 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1225 if(FAILED(hres))
1226 return hres;
1229 *ret = val;
1230 return S_OK;
1233 static HRESULT return_bool(exprval_t *ret, DWORD b)
1235 ret->type = EXPRVAL_VARIANT;
1236 V_VT(&ret->u.var) = VT_BOOL;
1237 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1239 return S_OK;
1242 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1244 exprval_t exprval;
1245 HRESULT hres;
1247 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1248 if(FAILED(hres))
1249 return hres;
1251 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
1252 exprval_release(&exprval);
1253 if(FAILED(hres))
1254 return hres;
1256 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1257 if(SUCCEEDED(hres)) {
1258 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
1259 exprval_release(&exprval);
1262 if(FAILED(hres)) {
1263 VariantClear(lval);
1264 return hres;
1267 return S_OK;
1270 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1272 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1273 exprval_t *ret)
1275 VARIANT lval, rval, retv;
1276 HRESULT hres;
1278 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1279 if(FAILED(hres))
1280 return hres;
1282 hres = oper(ctx, &lval, &rval, ei, &retv);
1283 VariantClear(&lval);
1284 VariantClear(&rval);
1285 if(FAILED(hres))
1286 return hres;
1288 ret->type = EXPRVAL_VARIANT;
1289 ret->u.var = retv;
1290 return S_OK;
1293 /* ECMA-262 3rd Edition 11.13.2 */
1294 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1295 jsexcept_t *ei, exprval_t *ret)
1297 VARIANT retv, lval, rval;
1298 exprval_t exprval, exprvalr;
1299 HRESULT hres;
1301 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1302 if(FAILED(hres))
1303 return hres;
1305 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
1306 if(SUCCEEDED(hres)) {
1307 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1308 if(SUCCEEDED(hres)) {
1309 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
1310 exprval_release(&exprvalr);
1312 if(SUCCEEDED(hres)) {
1313 hres = oper(ctx, &lval, &rval, ei, &retv);
1314 VariantClear(&rval);
1316 VariantClear(&lval);
1319 if(SUCCEEDED(hres)) {
1320 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
1321 if(FAILED(hres))
1322 VariantClear(&retv);
1324 exprval_release(&exprval);
1326 if(FAILED(hres))
1327 return hres;
1329 ret->type = EXPRVAL_VARIANT;
1330 ret->u.var = retv;
1331 return S_OK;
1334 /* ECMA-262 3rd Edition 13 */
1335 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1337 function_expression_t *expr = (function_expression_t*)_expr;
1338 VARIANT var;
1339 HRESULT hres;
1341 TRACE("\n");
1343 if(expr->identifier) {
1344 hres = jsdisp_propget_name(ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1345 if(FAILED(hres))
1346 return hres;
1347 }else {
1348 DispatchEx *dispex;
1350 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1351 expr->src_str, expr->src_len, &dispex);
1352 if(FAILED(hres))
1353 return hres;
1355 V_VT(&var) = VT_DISPATCH;
1356 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
1359 ret->type = EXPRVAL_VARIANT;
1360 ret->u.var = var;
1361 return S_OK;
1364 /* ECMA-262 3rd Edition 11.12 */
1365 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1367 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1368 exprval_t exprval;
1369 VARIANT_BOOL b;
1370 HRESULT hres;
1372 TRACE("\n");
1374 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1375 if(FAILED(hres))
1376 return hres;
1378 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1379 exprval_release(&exprval);
1380 if(FAILED(hres))
1381 return hres;
1383 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1386 /* ECMA-262 3rd Edition 11.2.1 */
1387 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1389 array_expression_t *expr = (array_expression_t*)_expr;
1390 exprval_t exprval;
1391 VARIANT member, val;
1392 DISPID id;
1393 BSTR str;
1394 IDispatch *obj = NULL;
1395 HRESULT hres;
1397 TRACE("\n");
1399 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
1400 if(FAILED(hres))
1401 return hres;
1403 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1404 exprval_release(&exprval);
1405 if(FAILED(hres))
1406 return hres;
1408 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1409 if(SUCCEEDED(hres)) {
1410 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1411 exprval_release(&exprval);
1414 if(SUCCEEDED(hres))
1415 hres = to_object(ctx->parser->script, &member, &obj);
1416 VariantClear(&member);
1417 if(SUCCEEDED(hres)) {
1418 hres = to_string(ctx->parser->script, &val, ei, &str);
1419 if(SUCCEEDED(hres)) {
1420 if(flags & EXPR_STRREF) {
1421 ret->type = EXPRVAL_NAMEREF;
1422 ret->u.nameref.disp = obj;
1423 ret->u.nameref.name = str;
1424 return S_OK;
1427 hres = disp_get_id(ctx->parser->script, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1430 if(SUCCEEDED(hres)) {
1431 exprval_set_idref(ret, obj, id);
1432 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1433 exprval_init(ret);
1434 hres = S_OK;
1437 IDispatch_Release(obj);
1440 return hres;
1443 /* ECMA-262 3rd Edition 11.2.1 */
1444 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1446 member_expression_t *expr = (member_expression_t*)_expr;
1447 IDispatch *obj = NULL;
1448 exprval_t exprval;
1449 VARIANT member;
1450 DISPID id;
1451 BSTR str;
1452 HRESULT hres;
1454 TRACE("\n");
1456 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1457 if(FAILED(hres))
1458 return hres;
1460 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
1461 exprval_release(&exprval);
1462 if(FAILED(hres))
1463 return hres;
1465 hres = to_object(ctx->parser->script, &member, &obj);
1466 VariantClear(&member);
1467 if(FAILED(hres))
1468 return hres;
1470 str = SysAllocString(expr->identifier);
1471 if(flags & EXPR_STRREF) {
1472 ret->type = EXPRVAL_NAMEREF;
1473 ret->u.nameref.disp = obj;
1474 ret->u.nameref.name = str;
1475 return S_OK;
1478 hres = disp_get_id(ctx->parser->script, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1479 SysFreeString(str);
1480 if(SUCCEEDED(hres)) {
1481 exprval_set_idref(ret, obj, id);
1482 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1483 exprval_init(ret);
1484 hres = S_OK;
1487 IDispatch_Release(obj);
1488 return hres;
1491 static void free_dp(DISPPARAMS *dp)
1493 DWORD i;
1495 for(i=0; i < dp->cArgs; i++)
1496 VariantClear(dp->rgvarg+i);
1497 heap_free(dp->rgvarg);
1500 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1502 VARIANTARG *vargs;
1503 exprval_t exprval;
1504 argument_t *iter;
1505 DWORD cnt = 0, i;
1506 HRESULT hres = S_OK;
1508 memset(dp, 0, sizeof(*dp));
1509 if(!args)
1510 return S_OK;
1512 for(iter = args; iter; iter = iter->next)
1513 cnt++;
1515 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1516 if(!vargs)
1517 return E_OUTOFMEMORY;
1519 for(i = cnt, iter = args; iter; iter = iter->next) {
1520 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1521 if(FAILED(hres))
1522 break;
1524 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1525 exprval_release(&exprval);
1526 if(FAILED(hres))
1527 break;
1530 if(FAILED(hres)) {
1531 free_dp(dp);
1532 return hres;
1535 dp->rgvarg = vargs;
1536 dp->cArgs = cnt;
1537 return S_OK;
1540 /* ECMA-262 3rd Edition 11.2.2 */
1541 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1543 call_expression_t *expr = (call_expression_t*)_expr;
1544 exprval_t exprval;
1545 VARIANT constr, var;
1546 DISPPARAMS dp;
1547 HRESULT hres;
1549 TRACE("\n");
1551 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1552 if(FAILED(hres))
1553 return hres;
1555 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1556 if(SUCCEEDED(hres))
1557 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1558 exprval_release(&exprval);
1559 if(FAILED(hres))
1560 return hres;
1562 if(V_VT(&constr) != VT_DISPATCH) {
1563 FIXME("throw TypeError\n");
1564 VariantClear(&constr);
1565 return E_FAIL;
1568 hres = disp_call(ctx->parser->script, V_DISPATCH(&constr), DISPID_VALUE,
1569 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1570 IDispatch_Release(V_DISPATCH(&constr));
1571 if(FAILED(hres))
1572 return hres;
1574 ret->type = EXPRVAL_VARIANT;
1575 ret->u.var = var;
1576 return S_OK;
1579 /* ECMA-262 3rd Edition 11.2.3 */
1580 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1582 call_expression_t *expr = (call_expression_t*)_expr;
1583 VARIANT var;
1584 exprval_t exprval;
1585 DISPPARAMS dp;
1586 HRESULT hres;
1588 TRACE("\n");
1590 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1591 if(FAILED(hres))
1592 return hres;
1594 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1595 if(SUCCEEDED(hres)) {
1596 switch(exprval.type) {
1597 case EXPRVAL_VARIANT:
1598 if(V_VT(&exprval.u.var) == VT_DISPATCH)
1599 hres = disp_call(ctx->parser->script, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1600 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1601 else
1602 hres = throw_type_error(ctx->parser->script, ei, IDS_NO_PROPERTY, NULL);
1603 break;
1604 case EXPRVAL_IDREF:
1605 hres = disp_call(ctx->parser->script, exprval.u.idref.disp, exprval.u.idref.id,
1606 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1607 break;
1608 case EXPRVAL_INVALID:
1609 hres = throw_type_error(ctx->parser->script, ei, IDS_OBJECT_EXPECTED, NULL);
1610 break;
1611 default:
1612 FIXME("unimplemented type %d\n", exprval.type);
1613 hres = E_NOTIMPL;
1616 free_dp(&dp);
1619 exprval_release(&exprval);
1620 if(FAILED(hres))
1621 return hres;
1623 ret->type = EXPRVAL_VARIANT;
1624 if(flags & EXPR_NOVAL) {
1625 V_VT(&ret->u.var) = VT_EMPTY;
1626 }else {
1627 TRACE("= %s\n", debugstr_variant(&var));
1628 ret->u.var = var;
1630 return S_OK;
1633 /* ECMA-262 3rd Edition 11.1.1 */
1634 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1636 TRACE("\n");
1638 ret->type = EXPRVAL_VARIANT;
1639 V_VT(&ret->u.var) = VT_DISPATCH;
1640 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1641 IDispatch_AddRef(ctx->this_obj);
1642 return S_OK;
1645 /* ECMA-262 3rd Edition 10.1.4 */
1646 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1648 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1649 BSTR identifier;
1650 HRESULT hres;
1652 TRACE("\n");
1654 identifier = SysAllocString(expr->identifier);
1655 if(!identifier)
1656 return E_OUTOFMEMORY;
1658 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1660 SysFreeString(identifier);
1661 return hres;
1664 /* ECMA-262 3rd Edition 7.8 */
1665 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1667 literal_expression_t *expr = (literal_expression_t*)_expr;
1668 VARIANT var;
1669 HRESULT hres;
1671 TRACE("\n");
1673 hres = literal_to_var(ctx->parser->script, expr->literal, &var);
1674 if(FAILED(hres))
1675 return hres;
1677 ret->type = EXPRVAL_VARIANT;
1678 ret->u.var = var;
1679 return S_OK;
1682 /* ECMA-262 3rd Edition 11.1.4 */
1683 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1685 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1686 DWORD length = 0, i = 0;
1687 array_element_t *elem;
1688 DispatchEx *array;
1689 exprval_t exprval;
1690 VARIANT val;
1691 HRESULT hres;
1693 TRACE("\n");
1695 for(elem = expr->element_list; elem; elem = elem->next)
1696 length += elem->elision+1;
1697 length += expr->length;
1699 hres = create_array(ctx->parser->script, length, &array);
1700 if(FAILED(hres))
1701 return hres;
1703 for(elem = expr->element_list; elem; elem = elem->next) {
1704 i += elem->elision;
1706 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1707 if(FAILED(hres))
1708 break;
1710 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1711 exprval_release(&exprval);
1712 if(FAILED(hres))
1713 break;
1715 hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
1716 VariantClear(&val);
1717 if(FAILED(hres))
1718 break;
1720 i++;
1723 if(FAILED(hres)) {
1724 jsdisp_release(array);
1725 return hres;
1728 ret->type = EXPRVAL_VARIANT;
1729 V_VT(&ret->u.var) = VT_DISPATCH;
1730 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(array);
1731 return S_OK;
1734 /* ECMA-262 3rd Edition 11.1.5 */
1735 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1737 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1738 VARIANT val, tmp;
1739 DispatchEx *obj;
1740 prop_val_t *iter;
1741 exprval_t exprval;
1742 BSTR name;
1743 HRESULT hres;
1745 TRACE("\n");
1747 hres = create_object(ctx->parser->script, NULL, &obj);
1748 if(FAILED(hres))
1749 return hres;
1751 for(iter = expr->property_list; iter; iter = iter->next) {
1752 hres = literal_to_var(ctx->parser->script, iter->name, &tmp);
1753 if(FAILED(hres))
1754 break;
1756 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1757 VariantClear(&tmp);
1758 if(FAILED(hres))
1759 break;
1761 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1762 if(SUCCEEDED(hres)) {
1763 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1764 exprval_release(&exprval);
1765 if(SUCCEEDED(hres)) {
1766 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
1767 VariantClear(&val);
1771 SysFreeString(name);
1772 if(FAILED(hres))
1773 break;
1776 if(FAILED(hres)) {
1777 jsdisp_release(obj);
1778 return hres;
1781 ret->type = EXPRVAL_VARIANT;
1782 V_VT(&ret->u.var) = VT_DISPATCH;
1783 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1784 return S_OK;
1787 /* ECMA-262 3rd Edition 11.14 */
1788 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1790 binary_expression_t *expr = (binary_expression_t*)_expr;
1791 VARIANT lval, rval;
1792 HRESULT hres;
1794 TRACE("\n");
1796 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1797 if(FAILED(hres))
1798 return hres;
1800 VariantClear(&lval);
1802 ret->type = EXPRVAL_VARIANT;
1803 ret->u.var = rval;
1804 return S_OK;
1807 /* ECMA-262 3rd Edition 11.11 */
1808 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1810 binary_expression_t *expr = (binary_expression_t*)_expr;
1811 exprval_t exprval;
1812 VARIANT_BOOL b;
1813 VARIANT val;
1814 HRESULT hres;
1816 TRACE("\n");
1818 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1819 if(FAILED(hres))
1820 return hres;
1822 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1823 exprval_release(&exprval);
1824 if(FAILED(hres))
1825 return hres;
1827 hres = to_boolean(&val, &b);
1828 if(SUCCEEDED(hres) && b) {
1829 ret->type = EXPRVAL_VARIANT;
1830 ret->u.var = val;
1831 return S_OK;
1834 VariantClear(&val);
1835 if(FAILED(hres))
1836 return hres;
1838 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1839 if(FAILED(hres))
1840 return hres;
1842 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1843 exprval_release(&exprval);
1844 if(FAILED(hres))
1845 return hres;
1847 ret->type = EXPRVAL_VARIANT;
1848 ret->u.var = val;
1849 return S_OK;
1852 /* ECMA-262 3rd Edition 11.11 */
1853 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1855 binary_expression_t *expr = (binary_expression_t*)_expr;
1856 exprval_t exprval;
1857 VARIANT_BOOL b;
1858 VARIANT val;
1859 HRESULT hres;
1861 TRACE("\n");
1863 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1864 if(FAILED(hres))
1865 return hres;
1867 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1868 exprval_release(&exprval);
1869 if(FAILED(hres))
1870 return hres;
1872 hres = to_boolean(&val, &b);
1873 if(SUCCEEDED(hres) && !b) {
1874 ret->type = EXPRVAL_VARIANT;
1875 ret->u.var = val;
1876 return S_OK;
1879 VariantClear(&val);
1880 if(FAILED(hres))
1881 return hres;
1883 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1884 if(FAILED(hres))
1885 return hres;
1887 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1888 exprval_release(&exprval);
1889 if(FAILED(hres))
1890 return hres;
1892 ret->type = EXPRVAL_VARIANT;
1893 ret->u.var = val;
1894 return S_OK;
1897 /* ECMA-262 3rd Edition 11.10 */
1898 static HRESULT bitor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1900 INT li, ri;
1901 HRESULT hres;
1903 hres = to_int32(ctx->parser->script, lval, ei, &li);
1904 if(FAILED(hres))
1905 return hres;
1907 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1908 if(FAILED(hres))
1909 return hres;
1911 V_VT(retv) = VT_I4;
1912 V_I4(retv) = li|ri;
1913 return S_OK;
1916 /* ECMA-262 3rd Edition 11.10 */
1917 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1919 binary_expression_t *expr = (binary_expression_t*)_expr;
1921 TRACE("\n");
1923 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
1926 /* ECMA-262 3rd Edition 11.10 */
1927 static HRESULT xor_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1929 INT li, ri;
1930 HRESULT hres;
1932 hres = to_int32(ctx->parser->script, lval, ei, &li);
1933 if(FAILED(hres))
1934 return hres;
1936 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1937 if(FAILED(hres))
1938 return hres;
1940 V_VT(retv) = VT_I4;
1941 V_I4(retv) = li^ri;
1942 return S_OK;
1945 /* ECMA-262 3rd Edition 11.10 */
1946 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1948 binary_expression_t *expr = (binary_expression_t*)_expr;
1950 TRACE("\n");
1952 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
1955 /* ECMA-262 3rd Edition 11.10 */
1956 static HRESULT bitand_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1958 INT li, ri;
1959 HRESULT hres;
1961 hres = to_int32(ctx->parser->script, lval, ei, &li);
1962 if(FAILED(hres))
1963 return hres;
1965 hres = to_int32(ctx->parser->script, rval, ei, &ri);
1966 if(FAILED(hres))
1967 return hres;
1969 V_VT(retv) = VT_I4;
1970 V_I4(retv) = li&ri;
1971 return S_OK;
1974 /* ECMA-262 3rd Edition 11.10 */
1975 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1977 binary_expression_t *expr = (binary_expression_t*)_expr;
1979 TRACE("\n");
1981 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
1984 /* ECMA-262 3rd Edition 11.8.6 */
1985 static HRESULT instanceof_eval(exec_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
1987 DispatchEx *obj, *iter, *tmp = NULL;
1988 VARIANT_BOOL ret = VARIANT_FALSE;
1989 BOOL b;
1990 VARIANT var;
1991 HRESULT hres;
1993 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1995 if(V_VT(objv) != VT_DISPATCH) {
1996 FIXME("throw TypeError\n");
1997 return E_FAIL;
2000 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2001 if(!obj) {
2002 FIXME("throw TypeError\n");
2003 return E_FAIL;
2006 if(is_class(obj, JSCLASS_FUNCTION)) {
2007 hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2008 }else {
2009 FIXME("throw TypeError\n");
2010 hres = E_FAIL;
2012 jsdisp_release(obj);
2013 if(FAILED(hres))
2014 return hres;
2016 if(V_VT(&var) == VT_DISPATCH) {
2017 if(V_VT(inst) == VT_DISPATCH)
2018 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2019 for(iter = tmp; iter; iter = iter->prototype) {
2020 hres = disp_cmp(V_DISPATCH(&var), (IDispatch*)_IDispatchEx_(iter), &b);
2021 if(FAILED(hres))
2022 break;
2023 if(b) {
2024 ret = VARIANT_TRUE;
2025 break;
2029 if(tmp)
2030 jsdisp_release(tmp);
2031 }else {
2032 FIXME("prototype is not an object\n");
2033 hres = E_FAIL;
2036 VariantClear(&var);
2037 if(FAILED(hres))
2038 return hres;
2040 V_VT(retv) = VT_BOOL;
2041 V_BOOL(retv) = ret;
2042 return S_OK;
2045 /* ECMA-262 3rd Edition 11.8.6 */
2046 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2048 binary_expression_t *expr = (binary_expression_t*)_expr;
2050 TRACE("\n");
2052 return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2055 /* ECMA-262 3rd Edition 11.8.7 */
2056 static HRESULT in_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *obj, jsexcept_t *ei, VARIANT *retv)
2058 VARIANT_BOOL ret;
2059 DISPID id;
2060 BSTR str;
2061 HRESULT hres;
2063 if(V_VT(obj) != VT_DISPATCH) {
2064 FIXME("throw TypeError\n");
2065 return E_FAIL;
2068 hres = to_string(ctx->parser->script, lval, ei, &str);
2069 if(FAILED(hres))
2070 return hres;
2072 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2073 SysFreeString(str);
2074 if(SUCCEEDED(hres))
2075 ret = VARIANT_TRUE;
2076 else if(hres == DISP_E_UNKNOWNNAME)
2077 ret = VARIANT_FALSE;
2078 else
2079 return hres;
2081 V_VT(retv) = VT_BOOL;
2082 V_BOOL(retv) = ret;
2083 return S_OK;
2086 /* ECMA-262 3rd Edition 11.8.7 */
2087 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2089 binary_expression_t *expr = (binary_expression_t*)_expr;
2091 TRACE("\n");
2093 return binary_expr_eval(ctx, expr, in_eval, ei, ret);
2096 /* ECMA-262 3rd Edition 11.6.1 */
2097 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2099 VARIANT r, l;
2100 HRESULT hres;
2102 hres = to_primitive(ctx->parser->script, lval, ei, &l, NO_HINT);
2103 if(FAILED(hres))
2104 return hres;
2106 hres = to_primitive(ctx->parser->script, rval, ei, &r, NO_HINT);
2107 if(FAILED(hres)) {
2108 VariantClear(&l);
2109 return hres;
2112 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2113 BSTR lstr = NULL, rstr = NULL;
2115 if(V_VT(&l) == VT_BSTR)
2116 lstr = V_BSTR(&l);
2117 else
2118 hres = to_string(ctx->parser->script, &l, ei, &lstr);
2120 if(SUCCEEDED(hres)) {
2121 if(V_VT(&r) == VT_BSTR)
2122 rstr = V_BSTR(&r);
2123 else
2124 hres = to_string(ctx->parser->script, &r, ei, &rstr);
2127 if(SUCCEEDED(hres)) {
2128 int len1, len2;
2130 len1 = SysStringLen(lstr);
2131 len2 = SysStringLen(rstr);
2133 V_VT(retv) = VT_BSTR;
2134 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2135 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2136 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2139 if(V_VT(&l) != VT_BSTR)
2140 SysFreeString(lstr);
2141 if(V_VT(&r) != VT_BSTR)
2142 SysFreeString(rstr);
2143 }else {
2144 VARIANT nl, nr;
2146 hres = to_number(ctx->parser->script, &l, ei, &nl);
2147 if(SUCCEEDED(hres)) {
2148 hres = to_number(ctx->parser->script, &r, ei, &nr);
2149 if(SUCCEEDED(hres))
2150 num_set_val(retv, num_val(&nl) + num_val(&nr));
2154 VariantClear(&r);
2155 VariantClear(&l);
2156 return hres;
2159 /* ECMA-262 3rd Edition 11.6.1 */
2160 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2162 binary_expression_t *expr = (binary_expression_t*)_expr;
2164 TRACE("\n");
2166 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
2169 /* ECMA-262 3rd Edition 11.6.2 */
2170 static HRESULT sub_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2172 VARIANT lnum, rnum;
2173 HRESULT hres;
2175 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2176 if(FAILED(hres))
2177 return hres;
2179 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2180 if(FAILED(hres))
2181 return hres;
2183 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
2184 return S_OK;
2187 /* ECMA-262 3rd Edition 11.6.2 */
2188 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2190 binary_expression_t *expr = (binary_expression_t*)_expr;
2192 TRACE("\n");
2194 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
2197 /* ECMA-262 3rd Edition 11.5.1 */
2198 static HRESULT mul_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2200 VARIANT lnum, rnum;
2201 HRESULT hres;
2203 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2204 if(FAILED(hres))
2205 return hres;
2207 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2208 if(FAILED(hres))
2209 return hres;
2211 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2212 return S_OK;
2215 /* ECMA-262 3rd Edition 11.5.1 */
2216 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2218 binary_expression_t *expr = (binary_expression_t*)_expr;
2220 TRACE("\n");
2222 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2225 /* ECMA-262 3rd Edition 11.5.2 */
2226 static HRESULT div_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2228 VARIANT lnum, rnum;
2229 HRESULT hres;
2231 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2232 if(FAILED(hres))
2233 return hres;
2235 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2236 if(FAILED(hres))
2237 return hres;
2239 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2240 return S_OK;
2243 /* ECMA-262 3rd Edition 11.5.2 */
2244 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2246 binary_expression_t *expr = (binary_expression_t*)_expr;
2248 TRACE("\n");
2250 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2253 /* ECMA-262 3rd Edition 11.5.3 */
2254 static HRESULT mod_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2256 VARIANT lnum, rnum;
2257 HRESULT hres;
2259 hres = to_number(ctx->parser->script, lval, ei, &lnum);
2260 if(FAILED(hres))
2261 return hres;
2263 hres = to_number(ctx->parser->script, rval, ei, &rnum);
2264 if(FAILED(hres))
2265 return hres;
2267 num_set_val(retv, fmod(num_val(&lnum), num_val(&rnum)));
2268 return S_OK;
2271 /* ECMA-262 3rd Edition 11.5.3 */
2272 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2274 binary_expression_t *expr = (binary_expression_t*)_expr;
2276 TRACE("\n");
2278 return binary_expr_eval(ctx, expr, mod_eval, ei, ret);
2281 /* ECMA-262 3rd Edition 11.4.2 */
2282 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2284 unary_expression_t *expr = (unary_expression_t*)_expr;
2285 VARIANT_BOOL b = VARIANT_FALSE;
2286 exprval_t exprval;
2287 HRESULT hres;
2289 TRACE("\n");
2291 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2292 if(FAILED(hres))
2293 return hres;
2295 switch(exprval.type) {
2296 case EXPRVAL_IDREF: {
2297 IDispatchEx *dispex;
2299 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2300 if(SUCCEEDED(hres)) {
2301 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2302 b = VARIANT_TRUE;
2303 IDispatchEx_Release(dispex);
2305 break;
2307 case EXPRVAL_NAMEREF: {
2308 IDispatchEx *dispex;
2310 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2311 if(SUCCEEDED(hres)) {
2312 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name,
2313 make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2314 b = VARIANT_TRUE;
2315 IDispatchEx_Release(dispex);
2317 break;
2319 default:
2320 FIXME("unsupported type %d\n", exprval.type);
2321 hres = E_NOTIMPL;
2324 exprval_release(&exprval);
2325 if(FAILED(hres))
2326 return hres;
2328 return return_bool(ret, b);
2331 /* ECMA-262 3rd Edition 11.4.2 */
2332 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2334 unary_expression_t *expr = (unary_expression_t*)_expr;
2335 exprval_t exprval;
2336 VARIANT tmp;
2337 HRESULT hres;
2339 TRACE("\n");
2341 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2342 if(FAILED(hres))
2343 return hres;
2345 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &tmp);
2346 exprval_release(&exprval);
2347 if(FAILED(hres))
2348 return hres;
2350 VariantClear(&tmp);
2352 ret->type = EXPRVAL_VARIANT;
2353 V_VT(&ret->u.var) = VT_EMPTY;
2354 return S_OK;
2357 /* ECMA-262 3rd Edition 11.4.3 */
2358 static HRESULT typeof_exprval(exec_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2360 VARIANT val;
2361 HRESULT hres;
2363 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2364 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2365 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2366 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2367 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2368 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2370 if(exprval->type == EXPRVAL_INVALID) {
2371 *ret = undefinedW;
2372 return S_OK;
2375 hres = exprval_to_value(ctx->parser->script, exprval, ei, &val);
2376 if(FAILED(hres))
2377 return hres;
2379 switch(V_VT(&val)) {
2380 case VT_EMPTY:
2381 *ret = undefinedW;
2382 break;
2383 case VT_NULL:
2384 *ret = objectW;
2385 break;
2386 case VT_BOOL:
2387 *ret = booleanW;
2388 break;
2389 case VT_I4:
2390 case VT_R8:
2391 *ret = numberW;
2392 break;
2393 case VT_BSTR:
2394 *ret = stringW;
2395 break;
2396 case VT_DISPATCH: {
2397 DispatchEx *dispex;
2399 if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2400 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2401 jsdisp_release(dispex);
2402 }else {
2403 *ret = objectW;
2405 break;
2407 default:
2408 FIXME("unhandled vt %d\n", V_VT(&val));
2409 hres = E_NOTIMPL;
2412 VariantClear(&val);
2413 return S_OK;
2416 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2418 unary_expression_t *expr = (unary_expression_t*)_expr;
2419 const WCHAR *str = NULL;
2420 exprval_t exprval;
2421 HRESULT hres;
2423 TRACE("\n");
2425 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2426 if(FAILED(hres))
2427 return hres;
2429 hres = typeof_exprval(ctx, &exprval, ei, &str);
2430 exprval_release(&exprval);
2431 if(FAILED(hres))
2432 return hres;
2434 ret->type = EXPRVAL_VARIANT;
2435 V_VT(&ret->u.var) = VT_BSTR;
2436 V_BSTR(&ret->u.var) = SysAllocString(str);
2437 if(!V_BSTR(&ret->u.var))
2438 return E_OUTOFMEMORY;
2440 return S_OK;
2443 /* ECMA-262 3rd Edition 11.4.7 */
2444 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2446 unary_expression_t *expr = (unary_expression_t*)_expr;
2447 exprval_t exprval;
2448 VARIANT val, num;
2449 HRESULT hres;
2451 TRACE("\n");
2453 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2454 if(FAILED(hres))
2455 return hres;
2457 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2458 exprval_release(&exprval);
2459 if(FAILED(hres))
2460 return hres;
2462 hres = to_number(ctx->parser->script, &val, ei, &num);
2463 VariantClear(&val);
2464 if(FAILED(hres))
2465 return hres;
2467 ret->type = EXPRVAL_VARIANT;
2468 num_set_val(&ret->u.var, -num_val(&num));
2469 return S_OK;
2472 /* ECMA-262 3rd Edition 11.4.6 */
2473 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2475 unary_expression_t *expr = (unary_expression_t*)_expr;
2476 exprval_t exprval;
2477 VARIANT val, num;
2478 HRESULT hres;
2480 TRACE("\n");
2482 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2483 if(FAILED(hres))
2484 return hres;
2486 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2487 exprval_release(&exprval);
2488 if(FAILED(hres))
2489 return hres;
2491 hres = to_number(ctx->parser->script, &val, ei, &num);
2492 if(FAILED(hres))
2493 return hres;
2495 ret->type = EXPRVAL_VARIANT;
2496 ret->u.var = num;
2497 return S_OK;
2500 /* ECMA-262 3rd Edition 11.3.1 */
2501 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2503 unary_expression_t *expr = (unary_expression_t*)_expr;
2504 VARIANT val, num;
2505 exprval_t exprval;
2506 HRESULT hres;
2508 TRACE("\n");
2510 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2511 if(FAILED(hres))
2512 return hres;
2514 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2515 if(SUCCEEDED(hres)) {
2516 hres = to_number(ctx->parser->script, &val, ei, &num);
2517 VariantClear(&val);
2520 if(SUCCEEDED(hres)) {
2521 VARIANT inc;
2522 num_set_val(&inc, num_val(&num)+1.0);
2523 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
2526 exprval_release(&exprval);
2527 if(FAILED(hres))
2528 return hres;
2530 ret->type = EXPRVAL_VARIANT;
2531 ret->u.var = num;
2532 return S_OK;
2535 /* ECMA-262 3rd Edition 11.3.2 */
2536 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2538 unary_expression_t *expr = (unary_expression_t*)_expr;
2539 VARIANT val, num;
2540 exprval_t exprval;
2541 HRESULT hres;
2543 TRACE("\n");
2545 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2546 if(FAILED(hres))
2547 return hres;
2549 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2550 if(SUCCEEDED(hres)) {
2551 hres = to_number(ctx->parser->script, &val, ei, &num);
2552 VariantClear(&val);
2555 if(SUCCEEDED(hres)) {
2556 VARIANT dec;
2557 num_set_val(&dec, num_val(&num)-1.0);
2558 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
2561 exprval_release(&exprval);
2562 if(FAILED(hres))
2563 return hres;
2565 ret->type = EXPRVAL_VARIANT;
2566 ret->u.var = num;
2567 return S_OK;
2570 /* ECMA-262 3rd Edition 11.4.4 */
2571 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2573 unary_expression_t *expr = (unary_expression_t*)_expr;
2574 VARIANT val, num;
2575 exprval_t exprval;
2576 HRESULT hres;
2578 TRACE("\n");
2580 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2581 if(FAILED(hres))
2582 return hres;
2584 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2585 if(SUCCEEDED(hres)) {
2586 hres = to_number(ctx->parser->script, &val, ei, &num);
2587 VariantClear(&val);
2590 if(SUCCEEDED(hres)) {
2591 num_set_val(&val, num_val(&num)+1.0);
2592 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2595 exprval_release(&exprval);
2596 if(FAILED(hres))
2597 return hres;
2599 ret->type = EXPRVAL_VARIANT;
2600 ret->u.var = val;
2601 return S_OK;
2604 /* ECMA-262 3rd Edition 11.4.5 */
2605 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2607 unary_expression_t *expr = (unary_expression_t*)_expr;
2608 VARIANT val, num;
2609 exprval_t exprval;
2610 HRESULT hres;
2612 TRACE("\n");
2614 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2615 if(FAILED(hres))
2616 return hres;
2618 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
2619 if(SUCCEEDED(hres)) {
2620 hres = to_number(ctx->parser->script, &val, ei, &num);
2621 VariantClear(&val);
2624 if(SUCCEEDED(hres)) {
2625 num_set_val(&val, num_val(&num)-1.0);
2626 hres = put_value(ctx->parser->script, &exprval, &val, ei);
2629 exprval_release(&exprval);
2630 if(FAILED(hres))
2631 return hres;
2633 ret->type = EXPRVAL_VARIANT;
2634 ret->u.var = val;
2635 return S_OK;
2638 /* ECMA-262 3rd Edition 11.9.3 */
2639 static HRESULT equal_values(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2641 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2642 return equal2_values(lval, rval, ret);
2644 /* FIXME: NULL disps should be handled in more general way */
2645 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2646 VARIANT v;
2647 V_VT(&v) = VT_NULL;
2648 return equal_values(ctx, &v, rval, ei, ret);
2651 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2652 VARIANT v;
2653 V_VT(&v) = VT_NULL;
2654 return equal_values(ctx, lval, &v, ei, ret);
2657 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2658 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2659 *ret = TRUE;
2660 return S_OK;
2663 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2664 VARIANT v;
2665 HRESULT hres;
2667 hres = to_number(ctx->parser->script, lval, ei, &v);
2668 if(FAILED(hres))
2669 return hres;
2671 return equal_values(ctx, &v, rval, ei, ret);
2674 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2675 VARIANT v;
2676 HRESULT hres;
2678 hres = to_number(ctx->parser->script, rval, ei, &v);
2679 if(FAILED(hres))
2680 return hres;
2682 return equal_values(ctx, lval, &v, ei, ret);
2685 if(V_VT(rval) == VT_BOOL) {
2686 VARIANT v;
2688 V_VT(&v) = VT_I4;
2689 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2690 return equal_values(ctx, lval, &v, ei, ret);
2693 if(V_VT(lval) == VT_BOOL) {
2694 VARIANT v;
2696 V_VT(&v) = VT_I4;
2697 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2698 return equal_values(ctx, &v, rval, ei, ret);
2702 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2703 VARIANT v;
2704 HRESULT hres;
2706 hres = to_primitive(ctx->parser->script, rval, ei, &v, NO_HINT);
2707 if(FAILED(hres))
2708 return hres;
2710 hres = equal_values(ctx, lval, &v, ei, ret);
2712 VariantClear(&v);
2713 return hres;
2717 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2718 VARIANT v;
2719 HRESULT hres;
2721 hres = to_primitive(ctx->parser->script, lval, ei, &v, NO_HINT);
2722 if(FAILED(hres))
2723 return hres;
2725 hres = equal_values(ctx, &v, rval, ei, ret);
2727 VariantClear(&v);
2728 return hres;
2732 *ret = FALSE;
2733 return S_OK;
2736 /* ECMA-262 3rd Edition 11.9.1 */
2737 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2739 binary_expression_t *expr = (binary_expression_t*)_expr;
2740 VARIANT rval, lval;
2741 BOOL b;
2742 HRESULT hres;
2744 TRACE("\n");
2746 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2747 if(FAILED(hres))
2748 return hres;
2750 hres = equal_values(ctx, &rval, &lval, ei, &b);
2751 if(FAILED(hres))
2752 return hres;
2754 return return_bool(ret, b);
2757 /* ECMA-262 3rd Edition 11.9.4 */
2758 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2760 binary_expression_t *expr = (binary_expression_t*)_expr;
2761 VARIANT rval, lval;
2762 BOOL b;
2763 HRESULT hres;
2765 TRACE("\n");
2767 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2768 if(FAILED(hres))
2769 return hres;
2771 hres = equal2_values(&rval, &lval, &b);
2772 if(FAILED(hres))
2773 return hres;
2775 return return_bool(ret, b);
2778 /* ECMA-262 3rd Edition 11.9.2 */
2779 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2781 binary_expression_t *expr = (binary_expression_t*)_expr;
2782 VARIANT rval, lval;
2783 BOOL b;
2784 HRESULT hres;
2786 TRACE("\n");
2788 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2789 if(FAILED(hres))
2790 return hres;
2792 hres = equal_values(ctx, &lval, &rval, ei, &b);
2793 if(FAILED(hres))
2794 return hres;
2796 return return_bool(ret, !b);
2799 /* ECMA-262 3rd Edition 11.9.5 */
2800 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2802 binary_expression_t *expr = (binary_expression_t*)_expr;
2803 VARIANT rval, lval;
2804 BOOL b;
2805 HRESULT hres;
2807 TRACE("\n");
2809 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2810 if(FAILED(hres))
2811 return hres;
2813 hres = equal2_values(&lval, &rval, &b);
2814 if(FAILED(hres))
2815 return hres;
2817 return return_bool(ret, !b);
2820 /* ECMA-262 3rd Edition 11.8.5 */
2821 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2823 VARIANT l, r, ln, rn;
2824 HRESULT hres;
2826 hres = to_primitive(ctx->parser->script, lval, ei, &l, NO_HINT);
2827 if(FAILED(hres))
2828 return hres;
2830 hres = to_primitive(ctx->parser->script, rval, ei, &r, NO_HINT);
2831 if(FAILED(hres)) {
2832 VariantClear(&l);
2833 return hres;
2836 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2837 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2838 SysFreeString(V_BSTR(&l));
2839 SysFreeString(V_BSTR(&r));
2840 return S_OK;
2843 hres = to_number(ctx->parser->script, &l, ei, &ln);
2844 VariantClear(&l);
2845 if(SUCCEEDED(hres))
2846 hres = to_number(ctx->parser->script, &r, ei, &rn);
2847 VariantClear(&r);
2848 if(FAILED(hres))
2849 return hres;
2851 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2852 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2853 }else {
2854 DOUBLE ld = num_val(&ln);
2855 DOUBLE rd = num_val(&rn);
2857 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2860 return S_OK;
2863 /* ECMA-262 3rd Edition 11.8.1 */
2864 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2866 binary_expression_t *expr = (binary_expression_t*)_expr;
2867 VARIANT rval, lval;
2868 BOOL b;
2869 HRESULT hres;
2871 TRACE("\n");
2873 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2874 if(FAILED(hres))
2875 return hres;
2877 hres = less_eval(ctx, &lval, &rval, FALSE, ei, &b);
2878 VariantClear(&lval);
2879 VariantClear(&rval);
2880 if(FAILED(hres))
2881 return hres;
2883 return return_bool(ret, b);
2886 /* ECMA-262 3rd Edition 11.8.3 */
2887 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2889 binary_expression_t *expr = (binary_expression_t*)_expr;
2890 VARIANT rval, lval;
2891 BOOL b;
2892 HRESULT hres;
2894 TRACE("\n");
2896 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2897 if(FAILED(hres))
2898 return hres;
2900 hres = less_eval(ctx, &rval, &lval, TRUE, ei, &b);
2901 VariantClear(&lval);
2902 VariantClear(&rval);
2903 if(FAILED(hres))
2904 return hres;
2906 return return_bool(ret, b);
2909 /* ECMA-262 3rd Edition 11.8.2 */
2910 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2912 binary_expression_t *expr = (binary_expression_t*)_expr;
2913 VARIANT rval, lval;
2914 BOOL b;
2915 HRESULT hres;
2917 TRACE("\n");
2919 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2920 if(FAILED(hres))
2921 return hres;
2923 hres = less_eval(ctx, &rval, &lval, FALSE, ei, &b);
2924 VariantClear(&lval);
2925 VariantClear(&rval);
2926 if(FAILED(hres))
2927 return hres;
2929 return return_bool(ret, b);
2932 /* ECMA-262 3rd Edition 11.8.4 */
2933 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2935 binary_expression_t *expr = (binary_expression_t*)_expr;
2936 VARIANT rval, lval;
2937 BOOL b;
2938 HRESULT hres;
2940 TRACE("\n");
2942 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2943 if(FAILED(hres))
2944 return hres;
2946 hres = less_eval(ctx, &lval, &rval, TRUE, ei, &b);
2947 VariantClear(&lval);
2948 VariantClear(&rval);
2949 if(FAILED(hres))
2950 return hres;
2952 return return_bool(ret, b);
2955 /* ECMA-262 3rd Edition 11.4.8 */
2956 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2958 unary_expression_t *expr = (unary_expression_t*)_expr;
2959 exprval_t exprval;
2960 VARIANT val;
2961 INT i;
2962 HRESULT hres;
2964 TRACE("\n");
2966 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2967 if(FAILED(hres))
2968 return hres;
2970 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
2971 exprval_release(&exprval);
2972 if(FAILED(hres))
2973 return hres;
2975 hres = to_int32(ctx->parser->script, &val, ei, &i);
2976 if(FAILED(hres))
2977 return hres;
2979 ret->type = EXPRVAL_VARIANT;
2980 V_VT(&ret->u.var) = VT_I4;
2981 V_I4(&ret->u.var) = ~i;
2982 return S_OK;
2985 /* ECMA-262 3rd Edition 11.4.9 */
2986 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2988 unary_expression_t *expr = (unary_expression_t*)_expr;
2989 exprval_t exprval;
2990 VARIANT_BOOL b;
2991 HRESULT hres;
2993 TRACE("\n");
2995 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2996 if(FAILED(hres))
2997 return hres;
2999 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
3000 exprval_release(&exprval);
3001 if(FAILED(hres))
3002 return hres;
3004 return return_bool(ret, !b);
3007 /* ECMA-262 3rd Edition 11.7.1 */
3008 static HRESULT lshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3010 DWORD ri;
3011 INT li;
3012 HRESULT hres;
3014 hres = to_int32(ctx->parser->script, lval, ei, &li);
3015 if(FAILED(hres))
3016 return hres;
3018 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3019 if(FAILED(hres))
3020 return hres;
3022 V_VT(retv) = VT_I4;
3023 V_I4(retv) = li << (ri&0x1f);
3024 return S_OK;
3027 /* ECMA-262 3rd Edition 11.7.1 */
3028 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3030 binary_expression_t *expr = (binary_expression_t*)_expr;
3032 TRACE("\n");
3034 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3037 /* ECMA-262 3rd Edition 11.7.2 */
3038 static HRESULT rshift_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3040 DWORD ri;
3041 INT li;
3042 HRESULT hres;
3044 hres = to_int32(ctx->parser->script, lval, ei, &li);
3045 if(FAILED(hres))
3046 return hres;
3048 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3049 if(FAILED(hres))
3050 return hres;
3052 V_VT(retv) = VT_I4;
3053 V_I4(retv) = li >> (ri&0x1f);
3054 return S_OK;
3057 /* ECMA-262 3rd Edition 11.7.2 */
3058 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3060 binary_expression_t *expr = (binary_expression_t*)_expr;
3062 TRACE("\n");
3064 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3067 /* ECMA-262 3rd Edition 11.7.3 */
3068 static HRESULT rshift2_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3070 DWORD li, ri;
3071 HRESULT hres;
3073 hres = to_uint32(ctx->parser->script, lval, ei, &li);
3074 if(FAILED(hres))
3075 return hres;
3077 hres = to_uint32(ctx->parser->script, rval, ei, &ri);
3078 if(FAILED(hres))
3079 return hres;
3081 V_VT(retv) = VT_I4;
3082 V_I4(retv) = li >> (ri&0x1f);
3083 return S_OK;
3086 /* ECMA-262 3rd Edition 11.7.3 */
3087 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3089 binary_expression_t *expr = (binary_expression_t*)_expr;
3091 TRACE("\n");
3093 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3096 /* ECMA-262 3rd Edition 11.13.1 */
3097 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3099 binary_expression_t *expr = (binary_expression_t*)_expr;
3100 exprval_t exprval, exprvalr;
3101 VARIANT rval;
3102 HRESULT hres;
3104 TRACE("\n");
3106 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
3107 if(FAILED(hres))
3108 return hres;
3110 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
3111 if(SUCCEEDED(hres)) {
3112 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
3113 exprval_release(&exprvalr);
3116 if(SUCCEEDED(hres))
3117 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
3119 exprval_release(&exprval);
3120 if(FAILED(hres))
3121 return hres;
3123 ret->type = EXPRVAL_VARIANT;
3124 ret->u.var = rval;
3125 return S_OK;
3128 /* ECMA-262 3rd Edition 11.13.2 */
3129 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3131 binary_expression_t *expr = (binary_expression_t*)_expr;
3133 TRACE("\n");
3135 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3138 /* ECMA-262 3rd Edition 11.13.2 */
3139 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3141 binary_expression_t *expr = (binary_expression_t*)_expr;
3143 TRACE("\n");
3145 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3148 /* ECMA-262 3rd Edition 11.13.2 */
3149 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3151 binary_expression_t *expr = (binary_expression_t*)_expr;
3153 TRACE("\n");
3155 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3158 /* ECMA-262 3rd Edition 11.13.2 */
3159 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3161 binary_expression_t *expr = (binary_expression_t*)_expr;
3163 TRACE("\n");
3165 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
3168 /* ECMA-262 3rd Edition 11.13.2 */
3169 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3171 binary_expression_t *expr = (binary_expression_t*)_expr;
3173 TRACE("\n");
3175 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
3178 /* ECMA-262 3rd Edition 11.13.2 */
3179 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3181 binary_expression_t *expr = (binary_expression_t*)_expr;
3183 TRACE("\n");
3185 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
3188 /* ECMA-262 3rd Edition 11.13.2 */
3189 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3191 binary_expression_t *expr = (binary_expression_t*)_expr;
3193 TRACE("\n");
3195 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
3198 /* ECMA-262 3rd Edition 11.13.2 */
3199 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3201 binary_expression_t *expr = (binary_expression_t*)_expr;
3203 TRACE("\n");
3205 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mod_eval, ei, ret);
3208 /* ECMA-262 3rd Edition 11.13.2 */
3209 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3211 binary_expression_t *expr = (binary_expression_t*)_expr;
3213 TRACE("\n");
3215 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3218 /* ECMA-262 3rd Edition 11.13.2 */
3219 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3221 binary_expression_t *expr = (binary_expression_t*)_expr;
3223 TRACE("\n");
3225 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
3228 /* ECMA-262 3rd Edition 11.13.2 */
3229 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3231 binary_expression_t *expr = (binary_expression_t*)_expr;
3233 TRACE("\n");
3235 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);