jscript: Use bytecode for bool literal implementation.
[wine/multimedia.git] / dlls / jscript / engine.c
blob1d35496b9328185c20200d2ad9dae198601ef1f9
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>
23 #include <assert.h>
25 #include "jscript.h"
26 #include "engine.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 #define EXPR_NOVAL 0x0001
33 #define EXPR_NEWREF 0x0002
34 #define EXPR_STRREF 0x0004
36 struct _return_type_t {
37 enum{
38 RT_NORMAL,
39 RT_RETURN,
40 RT_BREAK,
41 RT_CONTINUE
42 } type;
43 jsexcept_t ei;
46 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
48 return stat->eval(ctx, stat, rt, ret);
51 static inline HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
53 return expr->eval(ctx, expr, flags, ei, ret);
56 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
58 if(!ctx->stack_size) {
59 ctx->stack = heap_alloc(16*sizeof(VARIANT));
60 if(!ctx->stack)
61 return E_OUTOFMEMORY;
62 ctx->stack_size = 16;
63 }else if(ctx->stack_size == ctx->top) {
64 VARIANT *new_stack;
66 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
67 if(!new_stack) {
68 VariantClear(v);
69 return E_OUTOFMEMORY;
72 ctx->stack = new_stack;
73 ctx->stack_size *= 2;
76 ctx->stack[ctx->top++] = *v;
77 return S_OK;
80 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
82 VARIANT v;
84 V_VT(&v) = VT_BOOL;
85 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
86 return stack_push(ctx, &v);
89 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
91 assert(ctx->top);
92 return ctx->stack + --ctx->top;
95 static void stack_popn(exec_ctx_t *ctx, unsigned n)
97 while(n--)
98 VariantClear(stack_pop(ctx));
101 static void exprval_release(exprval_t *val)
103 switch(val->type) {
104 case EXPRVAL_VARIANT:
105 if(V_VT(&val->u.var) != VT_EMPTY)
106 VariantClear(&val->u.var);
107 return;
108 case EXPRVAL_IDREF:
109 if(val->u.idref.disp)
110 IDispatch_Release(val->u.idref.disp);
111 return;
112 case EXPRVAL_NAMEREF:
113 if(val->u.nameref.disp)
114 IDispatch_Release(val->u.nameref.disp);
115 SysFreeString(val->u.nameref.name);
116 return;
117 case EXPRVAL_INVALID:
118 SysFreeString(val->u.identifier);
122 /* ECMA-262 3rd Edition 8.7.1 */
123 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
125 V_VT(ret) = VT_EMPTY;
127 switch(val->type) {
128 case EXPRVAL_VARIANT:
129 return VariantCopy(ret, &val->u.var);
130 case EXPRVAL_IDREF:
131 if(!val->u.idref.disp) {
132 FIXME("throw ReferenceError\n");
133 return E_FAIL;
136 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
137 case EXPRVAL_NAMEREF:
138 break;
139 case EXPRVAL_INVALID:
140 return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
143 ERR("type %d\n", val->type);
144 return E_FAIL;
147 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
149 if(val->type == EXPRVAL_VARIANT) {
150 *ret = val->u.var;
151 V_VT(&val->u.var) = VT_EMPTY;
152 return S_OK;
155 return exprval_value(ctx, val, ei, ret);
158 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
160 if(exprval->type != EXPRVAL_VARIANT) {
161 VARIANT val;
162 HRESULT hres;
164 hres = exprval_to_value(ctx, exprval, ei, &val);
165 if(FAILED(hres))
166 return hres;
168 hres = to_boolean(&val, b);
169 VariantClear(&val);
170 return hres;
173 return to_boolean(&exprval->u.var, b);
176 static void exprval_init(exprval_t *val)
178 val->type = EXPRVAL_VARIANT;
179 V_VT(&val->u.var) = VT_EMPTY;
182 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
184 val->type = EXPRVAL_IDREF;
185 val->u.idref.disp = disp;
186 val->u.idref.id = id;
188 if(disp)
189 IDispatch_AddRef(disp);
192 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
194 scope_chain_t *new_scope;
196 new_scope = heap_alloc(sizeof(scope_chain_t));
197 if(!new_scope)
198 return E_OUTOFMEMORY;
200 new_scope->ref = 1;
202 jsdisp_addref(obj);
203 new_scope->obj = obj;
205 if(scope) {
206 scope_addref(scope);
207 new_scope->next = scope;
208 }else {
209 new_scope->next = NULL;
212 *ret = new_scope;
213 return S_OK;
216 static void scope_pop(scope_chain_t **scope)
218 scope_chain_t *tmp;
220 tmp = *scope;
221 *scope = tmp->next;
222 scope_release(tmp);
225 void scope_release(scope_chain_t *scope)
227 if(--scope->ref)
228 return;
230 if(scope->next)
231 scope_release(scope->next);
233 jsdisp_release(scope->obj);
234 heap_free(scope);
237 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
238 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
240 exec_ctx_t *ctx;
242 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
243 if(!ctx)
244 return E_OUTOFMEMORY;
246 ctx->ref = 1;
247 ctx->is_global = is_global;
249 if(this_obj)
250 ctx->this_obj = this_obj;
251 else if(script_ctx->host_global)
252 ctx->this_obj = script_ctx->host_global;
253 else
254 ctx->this_obj = to_disp(script_ctx->global);
255 IDispatch_AddRef(ctx->this_obj);
257 jsdisp_addref(var_disp);
258 ctx->var_disp = var_disp;
260 if(scope) {
261 scope_addref(scope);
262 ctx->scope_chain = scope;
265 *ret = ctx;
266 return S_OK;
269 void exec_release(exec_ctx_t *ctx)
271 if(--ctx->ref)
272 return;
274 if(ctx->scope_chain)
275 scope_release(ctx->scope_chain);
276 if(ctx->var_disp)
277 jsdisp_release(ctx->var_disp);
278 if(ctx->this_obj)
279 IDispatch_Release(ctx->this_obj);
280 heap_free(ctx->stack);
281 heap_free(ctx);
284 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
286 IDispatchEx *dispex;
287 HRESULT hres;
289 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
290 if(FAILED(hres)) {
291 TRACE("unsing IDispatch\n");
293 *id = 0;
294 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
297 *id = 0;
298 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
299 IDispatchEx_Release(dispex);
300 return hres;
303 /* ECMA-262 3rd Edition 8.7.2 */
304 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
306 if(ref->type != EXPRVAL_IDREF)
307 return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
309 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
312 static inline BOOL is_null(const VARIANT *v)
314 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
317 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
319 IObjectIdentity *identity;
320 IUnknown *unk1, *unk2;
321 HRESULT hres;
323 if(disp1 == disp2) {
324 *ret = TRUE;
325 return S_OK;
328 if(!disp1 || !disp2) {
329 *ret = FALSE;
330 return S_OK;
333 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
334 if(FAILED(hres))
335 return hres;
337 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
338 if(FAILED(hres)) {
339 IUnknown_Release(unk1);
340 return hres;
343 if(unk1 == unk2) {
344 *ret = TRUE;
345 }else {
346 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
347 if(SUCCEEDED(hres)) {
348 hres = IObjectIdentity_IsEqualObject(identity, unk2);
349 IObjectIdentity_Release(identity);
350 *ret = hres == S_OK;
351 }else {
352 *ret = FALSE;
356 IUnknown_Release(unk1);
357 IUnknown_Release(unk2);
358 return S_OK;
361 /* ECMA-262 3rd Edition 11.9.6 */
362 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
364 TRACE("\n");
366 if(V_VT(lval) != V_VT(rval)) {
367 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
368 *ret = num_val(lval) == num_val(rval);
369 else if(is_null(lval))
370 *ret = is_null(rval);
371 else
372 *ret = FALSE;
373 return S_OK;
376 switch(V_VT(lval)) {
377 case VT_EMPTY:
378 case VT_NULL:
379 *ret = VARIANT_TRUE;
380 break;
381 case VT_I4:
382 *ret = V_I4(lval) == V_I4(rval);
383 break;
384 case VT_R8:
385 *ret = V_R8(lval) == V_R8(rval);
386 break;
387 case VT_BSTR:
388 if(!V_BSTR(lval))
389 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
390 else if(!V_BSTR(rval))
391 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
392 else
393 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
394 break;
395 case VT_DISPATCH:
396 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
397 case VT_BOOL:
398 *ret = !V_BOOL(lval) == !V_BOOL(rval);
399 break;
400 default:
401 FIXME("unimplemented vt %d\n", V_VT(lval));
402 return E_NOTIMPL;
405 return S_OK;
408 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
410 switch(literal->type) {
411 case LT_NULL:
412 V_VT(v) = VT_NULL;
413 break;
414 case LT_INT:
415 V_VT(v) = VT_I4;
416 V_I4(v) = literal->u.lval;
417 break;
418 case LT_DOUBLE:
419 V_VT(v) = VT_R8;
420 V_R8(v) = literal->u.dval;
421 break;
422 case LT_STRING: {
423 BSTR str = SysAllocString(literal->u.wstr);
424 if(!str)
425 return E_OUTOFMEMORY;
427 V_VT(v) = VT_BSTR;
428 V_BSTR(v) = str;
429 break;
431 case LT_BOOL:
432 V_VT(v) = VT_BOOL;
433 V_BOOL(v) = literal->u.bval;
434 break;
435 case LT_REGEXP: {
436 jsdisp_t *regexp;
437 HRESULT hres;
439 hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
440 literal->u.regexp.flags, &regexp);
441 if(FAILED(hres))
442 return hres;
444 var_set_jsdisp(v, regexp);
448 return S_OK;
451 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
453 named_item_t *item;
454 DISPID id;
455 HRESULT hres;
457 for(item = ctx->named_items; item; item = item->next) {
458 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
459 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
460 if(SUCCEEDED(hres)) {
461 if(ret)
462 exprval_set_idref(ret, item->disp, id);
463 return TRUE;
468 return FALSE;
471 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
472 jsexcept_t *ei, VARIANT *retv)
474 script_ctx_t *script = parser->script;
475 function_declaration_t *func;
476 parser_ctx_t *prev_parser;
477 var_list_t *var;
478 VARIANT val, tmp;
479 statement_t *stat;
480 exec_ctx_t *prev_ctx;
481 return_type_t rt;
482 HRESULT hres = S_OK;
484 for(func = source->functions; func; func = func->next) {
485 jsdisp_t *func_obj;
486 VARIANT var;
488 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
489 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
490 if(FAILED(hres))
491 return hres;
493 var_set_jsdisp(&var, func_obj);
494 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
495 jsdisp_release(func_obj);
496 if(FAILED(hres))
497 return hres;
500 for(var = source->variables; var; var = var->next) {
501 DISPID id = 0;
502 BSTR name;
504 name = SysAllocString(var->identifier);
505 if(!name)
506 return E_OUTOFMEMORY;
508 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
509 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
510 SysFreeString(name);
511 if(FAILED(hres))
512 return hres;
515 prev_ctx = script->exec_ctx;
516 script->exec_ctx = ctx;
518 prev_parser = ctx->parser;
519 ctx->parser = parser;
521 V_VT(&val) = VT_EMPTY;
522 memset(&rt, 0, sizeof(rt));
523 rt.type = RT_NORMAL;
525 for(stat = source->statement; stat; stat = stat->next) {
526 hres = stat_eval(script, stat, &rt, &tmp);
527 if(FAILED(hres))
528 break;
530 VariantClear(&val);
531 val = tmp;
532 if(rt.type != RT_NORMAL)
533 break;
536 script->exec_ctx = prev_ctx;
537 ctx->parser = prev_parser;
539 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
540 FIXME("wrong rt %d\n", rt.type);
541 hres = E_FAIL;
544 *ei = rt.ei;
545 if(FAILED(hres)) {
546 VariantClear(&val);
547 return hres;
550 if(!retv || (!from_eval && rt.type != RT_RETURN))
551 VariantClear(&val);
552 if(retv)
553 *retv = val;
554 return S_OK;
557 /* ECMA-262 3rd Edition 10.1.4 */
558 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
560 scope_chain_t *scope;
561 named_item_t *item;
562 DISPID id = 0;
563 HRESULT hres;
565 TRACE("%s\n", debugstr_w(identifier));
567 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
568 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
569 if(SUCCEEDED(hres)) {
570 exprval_set_idref(ret, to_disp(scope->obj), id);
571 return S_OK;
575 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
576 if(SUCCEEDED(hres)) {
577 exprval_set_idref(ret, to_disp(ctx->global), id);
578 return S_OK;
581 for(item = ctx->named_items; item; item = item->next) {
582 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
583 if(!item->disp) {
584 IUnknown *unk;
586 if(!ctx->site)
587 break;
589 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
590 SCRIPTINFO_IUNKNOWN, &unk, NULL);
591 if(FAILED(hres)) {
592 WARN("GetItemInfo failed: %08x\n", hres);
593 break;
596 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
597 IUnknown_Release(unk);
598 if(FAILED(hres)) {
599 WARN("object does not implement IDispatch\n");
600 break;
604 ret->type = EXPRVAL_VARIANT;
605 V_VT(&ret->u.var) = VT_DISPATCH;
606 V_DISPATCH(&ret->u.var) = item->disp;
607 IDispatch_AddRef(item->disp);
608 return S_OK;
612 if(lookup_global_members(ctx, identifier, ret))
613 return S_OK;
615 if(flags & EXPR_NEWREF) {
616 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
617 if(FAILED(hres))
618 return hres;
620 exprval_set_idref(ret, to_disp(ctx->global), id);
621 return S_OK;
624 ret->type = EXPRVAL_INVALID;
625 ret->u.identifier = SysAllocString(identifier);
626 if(!ret->u.identifier)
627 return E_OUTOFMEMORY;
629 return S_OK;
632 /* ECMA-262 3rd Edition 12.1 */
633 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
635 block_statement_t *stat = (block_statement_t*)_stat;
636 VARIANT val, tmp;
637 statement_t *iter;
638 HRESULT hres = S_OK;
640 TRACE("\n");
642 V_VT(&val) = VT_EMPTY;
643 for(iter = stat->stat_list; iter; iter = iter->next) {
644 hres = stat_eval(ctx, iter, rt, &tmp);
645 if(FAILED(hres))
646 break;
648 VariantClear(&val);
649 val = tmp;
650 if(rt->type != RT_NORMAL)
651 break;
654 if(FAILED(hres)) {
655 VariantClear(&val);
656 return hres;
659 *ret = val;
660 return S_OK;
663 /* ECMA-262 3rd Edition 12.2 */
664 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
666 variable_declaration_t *iter;
667 HRESULT hres = S_OK;
669 for(iter = var_list; iter; iter = iter->next) {
670 exprval_t exprval;
671 VARIANT val;
673 if(!iter->expr)
674 continue;
676 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
677 if(FAILED(hres))
678 break;
680 hres = exprval_to_value(ctx, &exprval, ei, &val);
681 exprval_release(&exprval);
682 if(FAILED(hres))
683 break;
685 hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
686 VariantClear(&val);
687 if(FAILED(hres))
688 break;
691 return hres;
694 /* ECMA-262 3rd Edition 12.2 */
695 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
697 var_statement_t *stat = (var_statement_t*)_stat;
698 HRESULT hres;
700 TRACE("\n");
702 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
703 if(FAILED(hres))
704 return hres;
706 V_VT(ret) = VT_EMPTY;
707 return S_OK;
710 /* ECMA-262 3rd Edition 12.3 */
711 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
713 TRACE("\n");
715 V_VT(ret) = VT_EMPTY;
716 return S_OK;
719 /* ECMA-262 3rd Edition 12.4 */
720 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
722 expression_statement_t *stat = (expression_statement_t*)_stat;
723 exprval_t exprval;
724 VARIANT val;
725 HRESULT hres;
727 TRACE("\n");
729 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
730 if(FAILED(hres))
731 return hres;
733 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
734 exprval_release(&exprval);
735 if(FAILED(hres))
736 return hres;
738 *ret = val;
739 TRACE("= %s\n", debugstr_variant(ret));
740 return S_OK;
743 /* ECMA-262 3rd Edition 12.5 */
744 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
746 if_statement_t *stat = (if_statement_t*)_stat;
747 exprval_t exprval;
748 VARIANT_BOOL b;
749 HRESULT hres;
751 TRACE("\n");
753 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
754 if(FAILED(hres))
755 return hres;
757 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
758 exprval_release(&exprval);
759 if(FAILED(hres))
760 return hres;
762 if(b)
763 hres = stat_eval(ctx, stat->if_stat, rt, ret);
764 else if(stat->else_stat)
765 hres = stat_eval(ctx, stat->else_stat, rt, ret);
766 else
767 V_VT(ret) = VT_EMPTY;
769 return hres;
772 /* ECMA-262 3rd Edition 12.6.2 */
773 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
775 while_statement_t *stat = (while_statement_t*)_stat;
776 exprval_t exprval;
777 VARIANT val, tmp;
778 VARIANT_BOOL b;
779 BOOL test_expr;
780 HRESULT hres;
782 TRACE("\n");
784 V_VT(&val) = VT_EMPTY;
785 test_expr = !stat->do_while;
787 while(1) {
788 if(test_expr) {
789 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
790 if(FAILED(hres))
791 break;
793 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
794 exprval_release(&exprval);
795 if(FAILED(hres) || !b)
796 break;
797 }else {
798 test_expr = TRUE;
801 hres = stat_eval(ctx, stat->statement, rt, &tmp);
802 if(FAILED(hres))
803 break;
805 VariantClear(&val);
806 val = tmp;
808 if(rt->type == RT_CONTINUE)
809 rt->type = RT_NORMAL;
810 if(rt->type != RT_NORMAL)
811 break;
814 if(FAILED(hres)) {
815 VariantClear(&val);
816 return hres;
819 if(rt->type == RT_BREAK)
820 rt->type = RT_NORMAL;
822 *ret = val;
823 return S_OK;
826 /* ECMA-262 3rd Edition 12.6.3 */
827 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
829 for_statement_t *stat = (for_statement_t*)_stat;
830 VARIANT val, tmp, retv;
831 exprval_t exprval;
832 VARIANT_BOOL b;
833 HRESULT hres;
835 TRACE("\n");
837 if(stat->variable_list) {
838 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
839 if(FAILED(hres))
840 return hres;
841 }else if(stat->begin_expr) {
842 hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
843 if(FAILED(hres))
844 return hres;
846 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
847 exprval_release(&exprval);
848 if(FAILED(hres))
849 return hres;
851 VariantClear(&val);
854 V_VT(&retv) = VT_EMPTY;
856 while(1) {
857 if(stat->expr) {
858 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
859 if(FAILED(hres))
860 break;
862 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
863 exprval_release(&exprval);
864 if(FAILED(hres) || !b)
865 break;
868 hres = stat_eval(ctx, stat->statement, rt, &tmp);
869 if(FAILED(hres))
870 break;
872 VariantClear(&retv);
873 retv = tmp;
875 if(rt->type == RT_CONTINUE)
876 rt->type = RT_NORMAL;
877 else if(rt->type != RT_NORMAL)
878 break;
880 if(stat->end_expr) {
881 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
882 if(FAILED(hres))
883 break;
885 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
886 exprval_release(&exprval);
887 if(FAILED(hres))
888 break;
890 VariantClear(&val);
894 if(FAILED(hres)) {
895 VariantClear(&retv);
896 return hres;
899 if(rt->type == RT_BREAK)
900 rt->type = RT_NORMAL;
902 *ret = retv;
903 return S_OK;
906 /* ECMA-262 3rd Edition 12.6.4 */
907 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
909 forin_statement_t *stat = (forin_statement_t*)_stat;
910 VARIANT val, name, retv, tmp;
911 DISPID id = DISPID_STARTENUM;
912 BSTR str, identifier = NULL;
913 IDispatchEx *in_obj;
914 exprval_t exprval;
915 HRESULT hres;
917 TRACE("\n");
919 if(stat->variable) {
920 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
921 if(FAILED(hres))
922 return hres;
925 hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
926 if(FAILED(hres))
927 return hres;
929 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
930 exprval_release(&exprval);
931 if(FAILED(hres))
932 return hres;
934 if(V_VT(&val) != VT_DISPATCH) {
935 TRACE("in vt %d\n", V_VT(&val));
936 VariantClear(&val);
937 V_VT(ret) = VT_EMPTY;
938 return S_OK;
941 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
942 IDispatch_Release(V_DISPATCH(&val));
943 if(FAILED(hres)) {
944 TRACE("Object doesn't support IDispatchEx\n");
945 V_VT(ret) = VT_EMPTY;
946 return S_OK;
949 V_VT(&retv) = VT_EMPTY;
951 if(stat->variable)
952 identifier = SysAllocString(stat->variable->identifier);
954 while(1) {
955 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
956 if(FAILED(hres) || hres == S_FALSE)
957 break;
959 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
960 if(FAILED(hres))
961 break;
963 TRACE("iter %s\n", debugstr_w(str));
965 if(stat->variable)
966 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
967 else
968 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
969 if(SUCCEEDED(hres)) {
970 V_VT(&name) = VT_BSTR;
971 V_BSTR(&name) = str;
972 hres = put_value(ctx, &exprval, &name, &rt->ei);
973 exprval_release(&exprval);
975 SysFreeString(str);
976 if(FAILED(hres))
977 break;
979 hres = stat_eval(ctx, stat->statement, rt, &tmp);
980 if(FAILED(hres))
981 break;
983 VariantClear(&retv);
984 retv = tmp;
986 if(rt->type == RT_CONTINUE)
987 rt->type = RT_NORMAL;
988 else if(rt->type != RT_NORMAL)
989 break;
992 SysFreeString(identifier);
993 IDispatchEx_Release(in_obj);
994 if(FAILED(hres)) {
995 VariantClear(&retv);
996 return hres;
999 if(rt->type == RT_BREAK)
1000 rt->type = RT_NORMAL;
1002 *ret = retv;
1003 return S_OK;
1006 /* ECMA-262 3rd Edition 12.7 */
1007 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1009 branch_statement_t *stat = (branch_statement_t*)_stat;
1011 TRACE("\n");
1013 if(stat->identifier) {
1014 FIXME("indentifier not implemented\n");
1015 return E_NOTIMPL;
1018 rt->type = RT_CONTINUE;
1019 V_VT(ret) = VT_EMPTY;
1020 return S_OK;
1023 /* ECMA-262 3rd Edition 12.8 */
1024 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1026 branch_statement_t *stat = (branch_statement_t*)_stat;
1028 TRACE("\n");
1030 if(stat->identifier) {
1031 FIXME("indentifier not implemented\n");
1032 return E_NOTIMPL;
1035 rt->type = RT_BREAK;
1036 V_VT(ret) = VT_EMPTY;
1037 return S_OK;
1040 /* ECMA-262 3rd Edition 12.9 */
1041 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1043 expression_statement_t *stat = (expression_statement_t*)_stat;
1044 HRESULT hres;
1046 TRACE("\n");
1048 if(stat->expr) {
1049 exprval_t exprval;
1051 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1052 if(FAILED(hres))
1053 return hres;
1055 hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1056 exprval_release(&exprval);
1057 if(FAILED(hres))
1058 return hres;
1059 }else {
1060 V_VT(ret) = VT_EMPTY;
1063 TRACE("= %s\n", debugstr_variant(ret));
1064 rt->type = RT_RETURN;
1065 return S_OK;
1068 /* ECMA-262 3rd Edition 12.10 */
1069 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1071 with_statement_t *stat = (with_statement_t*)_stat;
1072 exprval_t exprval;
1073 IDispatch *disp;
1074 jsdisp_t *obj;
1075 VARIANT val;
1076 HRESULT hres;
1078 TRACE("\n");
1080 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1081 if(FAILED(hres))
1082 return hres;
1084 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1085 exprval_release(&exprval);
1086 if(FAILED(hres))
1087 return hres;
1089 hres = to_object(ctx, &val, &disp);
1090 VariantClear(&val);
1091 if(FAILED(hres))
1092 return hres;
1094 obj = iface_to_jsdisp((IUnknown*)disp);
1095 IDispatch_Release(disp);
1096 if(!obj) {
1097 FIXME("disp id not jsdisp\n");
1098 return E_NOTIMPL;
1101 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1102 jsdisp_release(obj);
1103 if(FAILED(hres))
1104 return hres;
1106 hres = stat_eval(ctx, stat->statement, rt, ret);
1108 scope_pop(&ctx->exec_ctx->scope_chain);
1109 return hres;
1112 /* ECMA-262 3rd Edition 12.12 */
1113 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1115 FIXME("\n");
1116 return E_NOTIMPL;
1119 /* ECMA-262 3rd Edition 12.13 */
1120 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1122 switch_statement_t *stat = (switch_statement_t*)_stat;
1123 case_clausule_t *iter, *default_clausule = NULL;
1124 statement_t *stat_iter;
1125 VARIANT val, cval;
1126 exprval_t exprval;
1127 BOOL b;
1128 HRESULT hres;
1130 TRACE("\n");
1132 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1133 if(FAILED(hres))
1134 return hres;
1136 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1137 exprval_release(&exprval);
1138 if(FAILED(hres))
1139 return hres;
1141 for(iter = stat->case_list; iter; iter = iter->next) {
1142 if(!iter->expr) {
1143 default_clausule = iter;
1144 continue;
1147 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1148 if(FAILED(hres))
1149 break;
1151 hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1152 exprval_release(&exprval);
1153 if(FAILED(hres))
1154 break;
1156 hres = equal2_values(&val, &cval, &b);
1157 VariantClear(&cval);
1158 if(FAILED(hres) || b)
1159 break;
1162 VariantClear(&val);
1163 if(FAILED(hres))
1164 return hres;
1166 if(!iter)
1167 iter = default_clausule;
1169 V_VT(&val) = VT_EMPTY;
1170 if(iter) {
1171 VARIANT tmp;
1173 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1174 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1175 if(FAILED(hres))
1176 break;
1178 VariantClear(&val);
1179 val = tmp;
1181 if(rt->type != RT_NORMAL)
1182 break;
1186 if(FAILED(hres)) {
1187 VariantClear(&val);
1188 return hres;
1191 if(rt->type == RT_BREAK)
1192 rt->type = RT_NORMAL;
1194 *ret = val;
1195 return S_OK;
1198 /* ECMA-262 3rd Edition 12.13 */
1199 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1201 expression_statement_t *stat = (expression_statement_t*)_stat;
1202 exprval_t exprval;
1203 VARIANT val;
1204 HRESULT hres;
1206 TRACE("\n");
1208 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1209 if(FAILED(hres))
1210 return hres;
1212 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1213 exprval_release(&exprval);
1214 if(FAILED(hres))
1215 return hres;
1217 rt->ei.var = val;
1218 return DISP_E_EXCEPTION;
1221 /* ECMA-262 3rd Edition 12.14 */
1222 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1224 jsdisp_t *var_disp;
1225 VARIANT ex, val;
1226 HRESULT hres;
1228 ex = rt->ei.var;
1229 memset(&rt->ei, 0, sizeof(jsexcept_t));
1231 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1232 if(SUCCEEDED(hres)) {
1233 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1234 if(SUCCEEDED(hres)) {
1235 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1236 if(SUCCEEDED(hres)) {
1237 hres = stat_eval(ctx, block->statement, rt, &val);
1238 scope_pop(&ctx->exec_ctx->scope_chain);
1242 jsdisp_release(var_disp);
1245 VariantClear(&ex);
1246 if(FAILED(hres))
1247 return hres;
1249 *ret = val;
1250 return S_OK;
1253 /* ECMA-262 3rd Edition 12.14 */
1254 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1256 try_statement_t *stat = (try_statement_t*)_stat;
1257 VARIANT val;
1258 HRESULT hres;
1260 TRACE("\n");
1262 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1263 if(FAILED(hres)) {
1264 TRACE("EXCEPTION\n");
1265 if(!stat->catch_block)
1266 return hres;
1268 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1269 if(FAILED(hres))
1270 return hres;
1273 if(stat->finally_statement) {
1274 VariantClear(&val);
1275 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1276 if(FAILED(hres))
1277 return hres;
1280 *ret = val;
1281 return S_OK;
1284 static HRESULT return_bool(exprval_t *ret, DWORD b)
1286 ret->type = EXPRVAL_VARIANT;
1287 V_VT(&ret->u.var) = VT_BOOL;
1288 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1290 return S_OK;
1293 static HRESULT get_binary_expr_values(script_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1295 exprval_t exprval;
1296 HRESULT hres;
1298 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1299 if(FAILED(hres))
1300 return hres;
1302 hres = exprval_to_value(ctx, &exprval, ei, lval);
1303 exprval_release(&exprval);
1304 if(FAILED(hres))
1305 return hres;
1307 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1308 if(SUCCEEDED(hres)) {
1309 hres = exprval_to_value(ctx, &exprval, ei, rval);
1310 exprval_release(&exprval);
1313 if(FAILED(hres)) {
1314 VariantClear(lval);
1315 return hres;
1318 return S_OK;
1321 typedef HRESULT (*oper_t)(script_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1323 static HRESULT binary_expr_eval(script_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1324 exprval_t *ret)
1326 VARIANT lval, rval, retv;
1327 HRESULT hres;
1329 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1330 if(FAILED(hres))
1331 return hres;
1333 hres = oper(ctx, &lval, &rval, ei, &retv);
1334 VariantClear(&lval);
1335 VariantClear(&rval);
1336 if(FAILED(hres))
1337 return hres;
1339 ret->type = EXPRVAL_VARIANT;
1340 ret->u.var = retv;
1341 return S_OK;
1344 /* ECMA-262 3rd Edition 11.13.2 */
1345 static HRESULT assign_oper_eval(script_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1346 jsexcept_t *ei, exprval_t *ret)
1348 VARIANT retv, lval, rval;
1349 exprval_t exprval, exprvalr;
1350 HRESULT hres;
1352 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1353 if(FAILED(hres))
1354 return hres;
1356 hres = exprval_value(ctx, &exprval, ei, &lval);
1357 if(SUCCEEDED(hres)) {
1358 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1359 if(SUCCEEDED(hres)) {
1360 hres = exprval_value(ctx, &exprvalr, ei, &rval);
1361 exprval_release(&exprvalr);
1363 if(SUCCEEDED(hres)) {
1364 hres = oper(ctx, &lval, &rval, ei, &retv);
1365 VariantClear(&rval);
1367 VariantClear(&lval);
1370 if(SUCCEEDED(hres)) {
1371 hres = put_value(ctx, &exprval, &retv, ei);
1372 if(FAILED(hres))
1373 VariantClear(&retv);
1375 exprval_release(&exprval);
1377 if(FAILED(hres))
1378 return hres;
1380 ret->type = EXPRVAL_VARIANT;
1381 ret->u.var = retv;
1382 return S_OK;
1385 /* ECMA-262 3rd Edition 13 */
1386 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1388 function_expression_t *expr = (function_expression_t*)_expr;
1389 VARIANT var;
1390 HRESULT hres;
1392 TRACE("\n");
1394 if(expr->identifier) {
1395 hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1396 if(FAILED(hres))
1397 return hres;
1398 }else {
1399 jsdisp_t *dispex;
1401 hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1402 expr->src_str, expr->src_len, &dispex);
1403 if(FAILED(hres))
1404 return hres;
1406 var_set_jsdisp(&var, dispex);
1409 ret->type = EXPRVAL_VARIANT;
1410 ret->u.var = var;
1411 return S_OK;
1414 /* ECMA-262 3rd Edition 11.12 */
1415 HRESULT conditional_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1417 conditional_expression_t *expr = (conditional_expression_t*)_expr;
1418 exprval_t exprval;
1419 VARIANT_BOOL b;
1420 HRESULT hres;
1422 TRACE("\n");
1424 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1425 if(FAILED(hres))
1426 return hres;
1428 hres = exprval_to_boolean(ctx, &exprval, ei, &b);
1429 exprval_release(&exprval);
1430 if(FAILED(hres))
1431 return hres;
1433 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
1436 /* ECMA-262 3rd Edition 11.2.1 */
1437 HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1439 array_expression_t *expr = (array_expression_t*)_expr;
1440 exprval_t exprval;
1441 VARIANT member, val;
1442 DISPID id;
1443 BSTR str;
1444 IDispatch *obj = NULL;
1445 HRESULT hres;
1447 TRACE("\n");
1449 hres = expr_eval(ctx, expr->member_expr, 0, ei, &exprval);
1450 if(FAILED(hres))
1451 return hres;
1453 hres = exprval_to_value(ctx, &exprval, ei, &member);
1454 exprval_release(&exprval);
1455 if(FAILED(hres))
1456 return hres;
1458 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1459 if(SUCCEEDED(hres)) {
1460 hres = exprval_to_value(ctx, &exprval, ei, &val);
1461 exprval_release(&exprval);
1464 if(SUCCEEDED(hres)) {
1465 hres = to_object(ctx, &member, &obj);
1466 if(FAILED(hres))
1467 VariantClear(&val);
1469 VariantClear(&member);
1470 if(SUCCEEDED(hres)) {
1471 hres = to_string(ctx, &val, ei, &str);
1472 VariantClear(&val);
1473 if(SUCCEEDED(hres)) {
1474 if(flags & EXPR_STRREF) {
1475 ret->type = EXPRVAL_NAMEREF;
1476 ret->u.nameref.disp = obj;
1477 ret->u.nameref.name = str;
1478 return S_OK;
1481 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1482 SysFreeString(str);
1485 if(SUCCEEDED(hres)) {
1486 exprval_set_idref(ret, obj, id);
1487 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1488 exprval_init(ret);
1489 hres = S_OK;
1492 IDispatch_Release(obj);
1495 return hres;
1498 /* ECMA-262 3rd Edition 11.2.1 */
1499 HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1501 member_expression_t *expr = (member_expression_t*)_expr;
1502 IDispatch *obj = NULL;
1503 exprval_t exprval;
1504 VARIANT member;
1505 DISPID id;
1506 BSTR str;
1507 HRESULT hres;
1509 TRACE("\n");
1511 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1512 if(FAILED(hres))
1513 return hres;
1515 hres = exprval_to_value(ctx, &exprval, ei, &member);
1516 exprval_release(&exprval);
1517 if(FAILED(hres))
1518 return hres;
1520 hres = to_object(ctx, &member, &obj);
1521 VariantClear(&member);
1522 if(FAILED(hres))
1523 return hres;
1525 str = SysAllocString(expr->identifier);
1526 if(flags & EXPR_STRREF) {
1527 ret->type = EXPRVAL_NAMEREF;
1528 ret->u.nameref.disp = obj;
1529 ret->u.nameref.name = str;
1530 return S_OK;
1533 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1534 SysFreeString(str);
1535 if(SUCCEEDED(hres)) {
1536 exprval_set_idref(ret, obj, id);
1537 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1538 exprval_init(ret);
1539 hres = S_OK;
1542 IDispatch_Release(obj);
1543 return hres;
1546 static void free_dp(DISPPARAMS *dp)
1548 DWORD i;
1550 for(i=0; i < dp->cArgs; i++)
1551 VariantClear(dp->rgvarg+i);
1552 heap_free(dp->rgvarg);
1555 static HRESULT args_to_param(script_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1557 VARIANTARG *vargs;
1558 exprval_t exprval;
1559 argument_t *iter;
1560 DWORD cnt = 0, i;
1561 HRESULT hres = S_OK;
1563 memset(dp, 0, sizeof(*dp));
1564 if(!args)
1565 return S_OK;
1567 for(iter = args; iter; iter = iter->next)
1568 cnt++;
1570 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1571 if(!vargs)
1572 return E_OUTOFMEMORY;
1574 for(i = cnt, iter = args; iter; iter = iter->next) {
1575 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1576 if(FAILED(hres))
1577 break;
1579 hres = exprval_to_value(ctx, &exprval, ei, vargs + (--i));
1580 exprval_release(&exprval);
1581 if(FAILED(hres))
1582 break;
1585 if(FAILED(hres)) {
1586 free_dp(dp);
1587 return hres;
1590 dp->rgvarg = vargs;
1591 dp->cArgs = cnt;
1592 return S_OK;
1595 /* ECMA-262 3rd Edition 11.2.2 */
1596 HRESULT new_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1598 call_expression_t *expr = (call_expression_t*)_expr;
1599 exprval_t exprval;
1600 VARIANT constr, var;
1601 DISPPARAMS dp;
1602 HRESULT hres;
1604 TRACE("\n");
1606 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1607 if(FAILED(hres))
1608 return hres;
1610 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1611 if(SUCCEEDED(hres))
1612 hres = exprval_to_value(ctx, &exprval, ei, &constr);
1613 exprval_release(&exprval);
1614 if(FAILED(hres))
1615 return hres;
1617 /* NOTE: Should use to_object here */
1619 if(V_VT(&constr) == VT_NULL) {
1620 VariantClear(&constr);
1621 return throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1622 } else if(V_VT(&constr) != VT_DISPATCH) {
1623 VariantClear(&constr);
1624 return throw_type_error(ctx, ei, JS_E_INVALID_ACTION, NULL);
1625 } else if(!V_DISPATCH(&constr)) {
1626 VariantClear(&constr);
1627 return throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1630 hres = disp_call(ctx, V_DISPATCH(&constr), DISPID_VALUE,
1631 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1632 IDispatch_Release(V_DISPATCH(&constr));
1633 free_dp(&dp);
1634 if(FAILED(hres))
1635 return hres;
1637 ret->type = EXPRVAL_VARIANT;
1638 ret->u.var = var;
1639 return S_OK;
1642 /* ECMA-262 3rd Edition 11.2.3 */
1643 HRESULT call_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1645 call_expression_t *expr = (call_expression_t*)_expr;
1646 VARIANT var;
1647 exprval_t exprval;
1648 DISPPARAMS dp;
1649 HRESULT hres;
1651 TRACE("\n");
1653 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1654 if(FAILED(hres))
1655 return hres;
1657 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1658 if(SUCCEEDED(hres)) {
1659 switch(exprval.type) {
1660 case EXPRVAL_VARIANT:
1661 if(V_VT(&exprval.u.var) == VT_DISPATCH)
1662 hres = disp_call(ctx, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1663 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1664 else
1665 hres = throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1666 break;
1667 case EXPRVAL_IDREF:
1668 hres = disp_call(ctx, exprval.u.idref.disp, exprval.u.idref.id,
1669 DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1670 break;
1671 case EXPRVAL_INVALID:
1672 hres = throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1673 break;
1674 default:
1675 FIXME("unimplemented type %d\n", exprval.type);
1676 hres = E_NOTIMPL;
1679 free_dp(&dp);
1682 exprval_release(&exprval);
1683 if(FAILED(hres))
1684 return hres;
1686 ret->type = EXPRVAL_VARIANT;
1687 if(flags & EXPR_NOVAL) {
1688 V_VT(&ret->u.var) = VT_EMPTY;
1689 }else {
1690 TRACE("= %s\n", debugstr_variant(&var));
1691 ret->u.var = var;
1693 return S_OK;
1696 /* ECMA-262 3rd Edition 11.1.1 */
1697 HRESULT this_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1699 TRACE("\n");
1701 ret->type = EXPRVAL_VARIANT;
1702 V_VT(&ret->u.var) = VT_DISPATCH;
1703 V_DISPATCH(&ret->u.var) = ctx->exec_ctx->this_obj;
1704 IDispatch_AddRef(ctx->exec_ctx->this_obj);
1705 return S_OK;
1708 /* ECMA-262 3rd Edition 10.1.4 */
1709 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1711 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1712 BSTR identifier;
1713 HRESULT hres;
1715 TRACE("\n");
1717 identifier = SysAllocString(expr->identifier);
1718 if(!identifier)
1719 return E_OUTOFMEMORY;
1721 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1723 SysFreeString(identifier);
1724 return hres;
1727 /* ECMA-262 3rd Edition 7.8.2 */
1728 HRESULT interp_bool(exec_ctx_t *ctx)
1730 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1732 TRACE("%s\n", arg ? "true" : "false");
1734 return stack_push_bool(ctx, arg);
1737 /* ECMA-262 3rd Edition 7.8.3 */
1738 HRESULT interp_int(exec_ctx_t *ctx)
1740 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1741 VARIANT v;
1743 TRACE("%d\n", arg);
1745 V_VT(&v) = VT_I4;
1746 V_I4(&v) = arg;
1747 return stack_push(ctx, &v);
1750 /* ECMA-262 3rd Edition 7.8 */
1751 HRESULT literal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1753 literal_expression_t *expr = (literal_expression_t*)_expr;
1754 VARIANT var;
1755 HRESULT hres;
1757 TRACE("\n");
1759 hres = literal_to_var(ctx, expr->literal, &var);
1760 if(FAILED(hres))
1761 return hres;
1763 ret->type = EXPRVAL_VARIANT;
1764 ret->u.var = var;
1765 return S_OK;
1768 /* ECMA-262 3rd Edition 11.1.4 */
1769 HRESULT array_literal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1771 array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
1772 DWORD length = 0, i = 0;
1773 array_element_t *elem;
1774 jsdisp_t *array;
1775 exprval_t exprval;
1776 VARIANT val;
1777 HRESULT hres;
1779 TRACE("\n");
1781 for(elem = expr->element_list; elem; elem = elem->next)
1782 length += elem->elision+1;
1783 length += expr->length;
1785 hres = create_array(ctx, length, &array);
1786 if(FAILED(hres))
1787 return hres;
1789 for(elem = expr->element_list; elem; elem = elem->next) {
1790 i += elem->elision;
1792 hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
1793 if(FAILED(hres))
1794 break;
1796 hres = exprval_to_value(ctx, &exprval, ei, &val);
1797 exprval_release(&exprval);
1798 if(FAILED(hres))
1799 break;
1801 hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
1802 VariantClear(&val);
1803 if(FAILED(hres))
1804 break;
1806 i++;
1809 if(FAILED(hres)) {
1810 jsdisp_release(array);
1811 return hres;
1814 ret->type = EXPRVAL_VARIANT;
1815 var_set_jsdisp(&ret->u.var, array);
1816 return S_OK;
1819 /* ECMA-262 3rd Edition 11.1.5 */
1820 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1822 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1823 VARIANT val, tmp;
1824 jsdisp_t *obj;
1825 prop_val_t *iter;
1826 exprval_t exprval;
1827 BSTR name;
1828 HRESULT hres;
1830 TRACE("\n");
1832 hres = create_object(ctx, NULL, &obj);
1833 if(FAILED(hres))
1834 return hres;
1836 for(iter = expr->property_list; iter; iter = iter->next) {
1837 hres = literal_to_var(ctx, iter->name, &tmp);
1838 if(FAILED(hres))
1839 break;
1841 hres = to_string(ctx, &tmp, ei, &name);
1842 VariantClear(&tmp);
1843 if(FAILED(hres))
1844 break;
1846 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1847 if(SUCCEEDED(hres)) {
1848 hres = exprval_to_value(ctx, &exprval, ei, &val);
1849 exprval_release(&exprval);
1850 if(SUCCEEDED(hres)) {
1851 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
1852 VariantClear(&val);
1856 SysFreeString(name);
1857 if(FAILED(hres))
1858 break;
1861 if(FAILED(hres)) {
1862 jsdisp_release(obj);
1863 return hres;
1866 ret->type = EXPRVAL_VARIANT;
1867 var_set_jsdisp(&ret->u.var, obj);
1868 return S_OK;
1871 /* ECMA-262 3rd Edition 11.14 */
1872 HRESULT comma_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1874 binary_expression_t *expr = (binary_expression_t*)_expr;
1875 VARIANT lval, rval;
1876 HRESULT hres;
1878 TRACE("\n");
1880 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1881 if(FAILED(hres))
1882 return hres;
1884 VariantClear(&lval);
1886 ret->type = EXPRVAL_VARIANT;
1887 ret->u.var = rval;
1888 return S_OK;
1891 /* ECMA-262 3rd Edition 11.11 */
1892 HRESULT logical_or_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1894 binary_expression_t *expr = (binary_expression_t*)_expr;
1895 exprval_t exprval;
1896 VARIANT_BOOL b;
1897 VARIANT val;
1898 HRESULT hres;
1900 TRACE("\n");
1902 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1903 if(FAILED(hres))
1904 return hres;
1906 hres = exprval_to_value(ctx, &exprval, ei, &val);
1907 exprval_release(&exprval);
1908 if(FAILED(hres))
1909 return hres;
1911 hres = to_boolean(&val, &b);
1912 if(SUCCEEDED(hres) && b) {
1913 ret->type = EXPRVAL_VARIANT;
1914 ret->u.var = val;
1915 return S_OK;
1918 VariantClear(&val);
1919 if(FAILED(hres))
1920 return hres;
1922 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1923 if(FAILED(hres))
1924 return hres;
1926 hres = exprval_to_value(ctx, &exprval, ei, &val);
1927 exprval_release(&exprval);
1928 if(FAILED(hres))
1929 return hres;
1931 ret->type = EXPRVAL_VARIANT;
1932 ret->u.var = val;
1933 return S_OK;
1936 /* ECMA-262 3rd Edition 11.11 */
1937 HRESULT logical_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1939 binary_expression_t *expr = (binary_expression_t*)_expr;
1940 exprval_t exprval;
1941 VARIANT_BOOL b;
1942 VARIANT val;
1943 HRESULT hres;
1945 TRACE("\n");
1947 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1948 if(FAILED(hres))
1949 return hres;
1951 hres = exprval_to_value(ctx, &exprval, ei, &val);
1952 exprval_release(&exprval);
1953 if(FAILED(hres))
1954 return hres;
1956 hres = to_boolean(&val, &b);
1957 if(SUCCEEDED(hres) && !b) {
1958 ret->type = EXPRVAL_VARIANT;
1959 ret->u.var = val;
1960 return S_OK;
1963 VariantClear(&val);
1964 if(FAILED(hres))
1965 return hres;
1967 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1968 if(FAILED(hres))
1969 return hres;
1971 hres = exprval_to_value(ctx, &exprval, ei, &val);
1972 exprval_release(&exprval);
1973 if(FAILED(hres))
1974 return hres;
1976 ret->type = EXPRVAL_VARIANT;
1977 ret->u.var = val;
1978 return S_OK;
1981 /* ECMA-262 3rd Edition 11.10 */
1982 static HRESULT bitor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1984 INT li, ri;
1985 HRESULT hres;
1987 hres = to_int32(ctx, lval, ei, &li);
1988 if(FAILED(hres))
1989 return hres;
1991 hres = to_int32(ctx, rval, ei, &ri);
1992 if(FAILED(hres))
1993 return hres;
1995 V_VT(retv) = VT_I4;
1996 V_I4(retv) = li|ri;
1997 return S_OK;
2000 /* ECMA-262 3rd Edition 11.10 */
2001 HRESULT binary_or_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2003 binary_expression_t *expr = (binary_expression_t*)_expr;
2005 TRACE("\n");
2007 return binary_expr_eval(ctx, expr, bitor_eval, ei, ret);
2010 /* ECMA-262 3rd Edition 11.10 */
2011 static HRESULT xor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2013 INT li, ri;
2014 HRESULT hres;
2016 hres = to_int32(ctx, lval, ei, &li);
2017 if(FAILED(hres))
2018 return hres;
2020 hres = to_int32(ctx, rval, ei, &ri);
2021 if(FAILED(hres))
2022 return hres;
2024 V_VT(retv) = VT_I4;
2025 V_I4(retv) = li^ri;
2026 return S_OK;
2029 /* ECMA-262 3rd Edition 11.10 */
2030 HRESULT binary_xor_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2032 binary_expression_t *expr = (binary_expression_t*)_expr;
2034 TRACE("\n");
2036 return binary_expr_eval(ctx, expr, xor_eval, ei, ret);
2039 /* ECMA-262 3rd Edition 11.10 */
2040 static HRESULT bitand_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2042 INT li, ri;
2043 HRESULT hres;
2045 hres = to_int32(ctx, lval, ei, &li);
2046 if(FAILED(hres))
2047 return hres;
2049 hres = to_int32(ctx, rval, ei, &ri);
2050 if(FAILED(hres))
2051 return hres;
2053 V_VT(retv) = VT_I4;
2054 V_I4(retv) = li&ri;
2055 return S_OK;
2058 /* ECMA-262 3rd Edition 11.10 */
2059 HRESULT binary_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2061 binary_expression_t *expr = (binary_expression_t*)_expr;
2063 TRACE("\n");
2065 return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
2068 /* ECMA-262 3rd Edition 11.8.6 */
2069 static HRESULT instanceof_eval(script_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
2071 jsdisp_t *obj, *iter, *tmp = NULL;
2072 VARIANT_BOOL ret = VARIANT_FALSE;
2073 BOOL b;
2074 VARIANT var;
2075 HRESULT hres;
2077 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2079 if(V_VT(objv) != VT_DISPATCH || !V_DISPATCH(objv))
2080 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2082 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2083 if(!obj) {
2084 FIXME("non-jsdisp objects not supported\n");
2085 return E_FAIL;
2088 if(is_class(obj, JSCLASS_FUNCTION)) {
2089 hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2090 }else {
2091 hres = throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2093 jsdisp_release(obj);
2094 if(FAILED(hres))
2095 return hres;
2097 if(V_VT(&var) == VT_DISPATCH) {
2098 if(V_VT(inst) == VT_DISPATCH)
2099 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2100 for(iter = tmp; iter; iter = iter->prototype) {
2101 hres = disp_cmp(V_DISPATCH(&var), to_disp(iter), &b);
2102 if(FAILED(hres))
2103 break;
2104 if(b) {
2105 ret = VARIANT_TRUE;
2106 break;
2110 if(tmp)
2111 jsdisp_release(tmp);
2112 }else {
2113 FIXME("prototype is not an object\n");
2114 hres = E_FAIL;
2117 VariantClear(&var);
2118 if(FAILED(hres))
2119 return hres;
2121 V_VT(retv) = VT_BOOL;
2122 V_BOOL(retv) = ret;
2123 return S_OK;
2126 /* ECMA-262 3rd Edition 11.8.6 */
2127 HRESULT instanceof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2129 binary_expression_t *expr = (binary_expression_t*)_expr;
2131 TRACE("\n");
2133 return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2136 /* ECMA-262 3rd Edition 11.8.7 */
2137 HRESULT interp_in(exec_ctx_t *ctx)
2139 VARIANT *obj, *v;
2140 DISPID id = 0;
2141 BOOL ret;
2142 BSTR str;
2143 HRESULT hres;
2145 TRACE("\n");
2147 obj = stack_pop(ctx);
2148 v = stack_pop(ctx);
2150 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2151 VariantClear(obj);
2152 VariantClear(v);
2153 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2156 hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2157 VariantClear(v);
2158 if(FAILED(hres)) {
2159 IDispatch_Release(V_DISPATCH(obj));
2160 return hres;
2163 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2164 IDispatch_Release(V_DISPATCH(obj));
2165 SysFreeString(str);
2166 if(SUCCEEDED(hres))
2167 ret = TRUE;
2168 else if(hres == DISP_E_UNKNOWNNAME)
2169 ret = FALSE;
2170 else
2171 return hres;
2173 return stack_push_bool(ctx, ret);
2176 /* ECMA-262 3rd Edition 11.6.1 */
2177 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2179 VARIANT r, l;
2180 HRESULT hres;
2182 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2183 if(FAILED(hres))
2184 return hres;
2186 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2187 if(FAILED(hres)) {
2188 VariantClear(&l);
2189 return hres;
2192 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2193 BSTR lstr = NULL, rstr = NULL;
2195 if(V_VT(&l) == VT_BSTR)
2196 lstr = V_BSTR(&l);
2197 else
2198 hres = to_string(ctx, &l, ei, &lstr);
2200 if(SUCCEEDED(hres)) {
2201 if(V_VT(&r) == VT_BSTR)
2202 rstr = V_BSTR(&r);
2203 else
2204 hres = to_string(ctx, &r, ei, &rstr);
2207 if(SUCCEEDED(hres)) {
2208 int len1, len2;
2210 len1 = SysStringLen(lstr);
2211 len2 = SysStringLen(rstr);
2213 V_VT(retv) = VT_BSTR;
2214 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2215 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2216 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2219 if(V_VT(&l) != VT_BSTR)
2220 SysFreeString(lstr);
2221 if(V_VT(&r) != VT_BSTR)
2222 SysFreeString(rstr);
2223 }else {
2224 VARIANT nl, nr;
2226 hres = to_number(ctx, &l, ei, &nl);
2227 if(SUCCEEDED(hres)) {
2228 hres = to_number(ctx, &r, ei, &nr);
2229 if(SUCCEEDED(hres))
2230 num_set_val(retv, num_val(&nl) + num_val(&nr));
2234 VariantClear(&r);
2235 VariantClear(&l);
2236 return hres;
2239 /* ECMA-262 3rd Edition 11.6.1 */
2240 HRESULT interp_add(exec_ctx_t *ctx)
2242 VARIANT *l, *r, ret;
2243 HRESULT hres;
2245 r = stack_pop(ctx);
2246 l = stack_pop(ctx);
2248 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2250 hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2251 if(FAILED(hres))
2252 return hres;
2254 return stack_push(ctx, &ret);
2257 /* ECMA-262 3rd Edition 11.6.2 */
2258 static HRESULT sub_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2260 VARIANT lnum, rnum;
2261 HRESULT hres;
2263 hres = to_number(ctx, lval, ei, &lnum);
2264 if(FAILED(hres))
2265 return hres;
2267 hres = to_number(ctx, rval, ei, &rnum);
2268 if(FAILED(hres))
2269 return hres;
2271 num_set_val(retv, num_val(&lnum) - num_val(&rnum));
2272 return S_OK;
2275 /* ECMA-262 3rd Edition 11.6.2 */
2276 HRESULT sub_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2278 binary_expression_t *expr = (binary_expression_t*)_expr;
2280 TRACE("\n");
2282 return binary_expr_eval(ctx, expr, sub_eval, ei, ret);
2285 /* ECMA-262 3rd Edition 11.5.1 */
2286 static HRESULT mul_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2288 VARIANT lnum, rnum;
2289 HRESULT hres;
2291 hres = to_number(ctx, lval, ei, &lnum);
2292 if(FAILED(hres))
2293 return hres;
2295 hres = to_number(ctx, rval, ei, &rnum);
2296 if(FAILED(hres))
2297 return hres;
2299 num_set_val(retv, num_val(&lnum) * num_val(&rnum));
2300 return S_OK;
2303 /* ECMA-262 3rd Edition 11.5.1 */
2304 HRESULT mul_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2306 binary_expression_t *expr = (binary_expression_t*)_expr;
2308 TRACE("\n");
2310 return binary_expr_eval(ctx, expr, mul_eval, ei, ret);
2313 /* ECMA-262 3rd Edition 11.5.2 */
2314 static HRESULT div_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2316 VARIANT lnum, rnum;
2317 HRESULT hres;
2319 hres = to_number(ctx, lval, ei, &lnum);
2320 if(FAILED(hres))
2321 return hres;
2323 hres = to_number(ctx, rval, ei, &rnum);
2324 if(FAILED(hres))
2325 return hres;
2327 num_set_val(retv, num_val(&lnum) / num_val(&rnum));
2328 return S_OK;
2331 /* ECMA-262 3rd Edition 11.5.2 */
2332 HRESULT div_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2334 binary_expression_t *expr = (binary_expression_t*)_expr;
2336 TRACE("\n");
2338 return binary_expr_eval(ctx, expr, div_eval, ei, ret);
2341 /* ECMA-262 3rd Edition 11.5.3 */
2342 static HRESULT mod_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2344 VARIANT lnum, rnum;
2345 HRESULT hres;
2347 hres = to_number(ctx, lval, ei, &lnum);
2348 if(FAILED(hres))
2349 return hres;
2351 hres = to_number(ctx, rval, ei, &rnum);
2352 if(FAILED(hres))
2353 return hres;
2355 num_set_val(retv, fmod(num_val(&lnum), num_val(&rnum)));
2356 return S_OK;
2359 /* ECMA-262 3rd Edition 11.5.3 */
2360 HRESULT mod_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2362 binary_expression_t *expr = (binary_expression_t*)_expr;
2364 TRACE("\n");
2366 return binary_expr_eval(ctx, expr, mod_eval, ei, ret);
2369 /* ECMA-262 3rd Edition 11.4.2 */
2370 HRESULT delete_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2372 unary_expression_t *expr = (unary_expression_t*)_expr;
2373 VARIANT_BOOL b = VARIANT_FALSE;
2374 exprval_t exprval;
2375 HRESULT hres;
2377 TRACE("\n");
2379 hres = expr_eval(ctx, expr->expression, EXPR_STRREF, ei, &exprval);
2380 if(FAILED(hres))
2381 return hres;
2383 switch(exprval.type) {
2384 case EXPRVAL_IDREF: {
2385 IDispatchEx *dispex;
2387 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2388 if(SUCCEEDED(hres)) {
2389 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2390 b = VARIANT_TRUE;
2391 IDispatchEx_Release(dispex);
2393 break;
2395 case EXPRVAL_NAMEREF: {
2396 IDispatchEx *dispex;
2398 hres = IDispatch_QueryInterface(exprval.u.nameref.disp, &IID_IDispatchEx, (void**)&dispex);
2399 if(SUCCEEDED(hres)) {
2400 hres = IDispatchEx_DeleteMemberByName(dispex, exprval.u.nameref.name,
2401 make_grfdex(ctx, fdexNameCaseSensitive));
2402 b = VARIANT_TRUE;
2403 IDispatchEx_Release(dispex);
2405 break;
2407 default:
2408 FIXME("unsupported type %d\n", exprval.type);
2409 hres = E_NOTIMPL;
2412 exprval_release(&exprval);
2413 if(FAILED(hres))
2414 return hres;
2416 return return_bool(ret, b);
2419 /* ECMA-262 3rd Edition 11.4.2 */
2420 HRESULT void_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2422 unary_expression_t *expr = (unary_expression_t*)_expr;
2423 exprval_t exprval;
2424 VARIANT tmp;
2425 HRESULT hres;
2427 TRACE("\n");
2429 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2430 if(FAILED(hres))
2431 return hres;
2433 hres = exprval_to_value(ctx, &exprval, ei, &tmp);
2434 exprval_release(&exprval);
2435 if(FAILED(hres))
2436 return hres;
2438 VariantClear(&tmp);
2440 ret->type = EXPRVAL_VARIANT;
2441 V_VT(&ret->u.var) = VT_EMPTY;
2442 return S_OK;
2445 /* ECMA-262 3rd Edition 11.4.3 */
2446 static HRESULT typeof_exprval(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2448 VARIANT val;
2449 HRESULT hres;
2451 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2452 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2453 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2454 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2455 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2456 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2457 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
2459 if(exprval->type == EXPRVAL_INVALID) {
2460 *ret = undefinedW;
2461 return S_OK;
2464 hres = exprval_to_value(ctx, exprval, ei, &val);
2465 if(FAILED(hres)) {
2466 if(exprval->type == EXPRVAL_IDREF) {
2467 *ret = unknownW;
2468 return S_OK;
2470 return hres;
2473 switch(V_VT(&val)) {
2474 case VT_EMPTY:
2475 *ret = undefinedW;
2476 break;
2477 case VT_NULL:
2478 *ret = objectW;
2479 break;
2480 case VT_BOOL:
2481 *ret = booleanW;
2482 break;
2483 case VT_I4:
2484 case VT_R8:
2485 *ret = numberW;
2486 break;
2487 case VT_BSTR:
2488 *ret = stringW;
2489 break;
2490 case VT_DISPATCH: {
2491 jsdisp_t *dispex;
2493 if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2494 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2495 jsdisp_release(dispex);
2496 }else {
2497 *ret = objectW;
2499 break;
2501 default:
2502 FIXME("unhandled vt %d\n", V_VT(&val));
2503 hres = E_NOTIMPL;
2506 VariantClear(&val);
2507 return hres;
2510 HRESULT typeof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2512 unary_expression_t *expr = (unary_expression_t*)_expr;
2513 const WCHAR *str = NULL;
2514 exprval_t exprval;
2515 HRESULT hres;
2517 TRACE("\n");
2519 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2520 if(FAILED(hres))
2521 return hres;
2523 hres = typeof_exprval(ctx, &exprval, ei, &str);
2524 exprval_release(&exprval);
2525 if(FAILED(hres))
2526 return hres;
2528 ret->type = EXPRVAL_VARIANT;
2529 V_VT(&ret->u.var) = VT_BSTR;
2530 V_BSTR(&ret->u.var) = SysAllocString(str);
2531 if(!V_BSTR(&ret->u.var))
2532 return E_OUTOFMEMORY;
2534 return S_OK;
2537 /* ECMA-262 3rd Edition 11.4.7 */
2538 HRESULT minus_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2540 unary_expression_t *expr = (unary_expression_t*)_expr;
2541 exprval_t exprval;
2542 VARIANT val, num;
2543 HRESULT hres;
2545 TRACE("\n");
2547 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2548 if(FAILED(hres))
2549 return hres;
2551 hres = exprval_to_value(ctx, &exprval, ei, &val);
2552 exprval_release(&exprval);
2553 if(FAILED(hres))
2554 return hres;
2556 hres = to_number(ctx, &val, ei, &num);
2557 VariantClear(&val);
2558 if(FAILED(hres))
2559 return hres;
2561 ret->type = EXPRVAL_VARIANT;
2562 num_set_val(&ret->u.var, -num_val(&num));
2563 return S_OK;
2566 /* ECMA-262 3rd Edition 11.4.6 */
2567 HRESULT interp_tonum(exec_ctx_t *ctx)
2569 VARIANT *v, num;
2570 HRESULT hres;
2572 TRACE("\n");
2574 v = stack_pop(ctx);
2575 hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2576 VariantClear(v);
2577 if(FAILED(hres))
2578 return hres;
2580 return stack_push(ctx, &num);
2583 /* ECMA-262 3rd Edition 11.3.1 */
2584 HRESULT post_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2586 unary_expression_t *expr = (unary_expression_t*)_expr;
2587 VARIANT val, num;
2588 exprval_t exprval;
2589 HRESULT hres;
2591 TRACE("\n");
2593 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2594 if(FAILED(hres))
2595 return hres;
2597 hres = exprval_value(ctx, &exprval, ei, &val);
2598 if(SUCCEEDED(hres)) {
2599 hres = to_number(ctx, &val, ei, &num);
2600 VariantClear(&val);
2603 if(SUCCEEDED(hres)) {
2604 VARIANT inc;
2605 num_set_val(&inc, num_val(&num)+1.0);
2606 hres = put_value(ctx, &exprval, &inc, ei);
2609 exprval_release(&exprval);
2610 if(FAILED(hres))
2611 return hres;
2613 ret->type = EXPRVAL_VARIANT;
2614 ret->u.var = num;
2615 return S_OK;
2618 /* ECMA-262 3rd Edition 11.3.2 */
2619 HRESULT post_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2621 unary_expression_t *expr = (unary_expression_t*)_expr;
2622 VARIANT val, num;
2623 exprval_t exprval;
2624 HRESULT hres;
2626 TRACE("\n");
2628 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2629 if(FAILED(hres))
2630 return hres;
2632 hres = exprval_value(ctx, &exprval, ei, &val);
2633 if(SUCCEEDED(hres)) {
2634 hres = to_number(ctx, &val, ei, &num);
2635 VariantClear(&val);
2638 if(SUCCEEDED(hres)) {
2639 VARIANT dec;
2640 num_set_val(&dec, num_val(&num)-1.0);
2641 hres = put_value(ctx, &exprval, &dec, ei);
2644 exprval_release(&exprval);
2645 if(FAILED(hres))
2646 return hres;
2648 ret->type = EXPRVAL_VARIANT;
2649 ret->u.var = num;
2650 return S_OK;
2653 /* ECMA-262 3rd Edition 11.4.4 */
2654 HRESULT pre_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2656 unary_expression_t *expr = (unary_expression_t*)_expr;
2657 VARIANT val, num;
2658 exprval_t exprval;
2659 HRESULT hres;
2661 TRACE("\n");
2663 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2664 if(FAILED(hres))
2665 return hres;
2667 hres = exprval_value(ctx, &exprval, ei, &val);
2668 if(SUCCEEDED(hres)) {
2669 hres = to_number(ctx, &val, ei, &num);
2670 VariantClear(&val);
2673 if(SUCCEEDED(hres)) {
2674 num_set_val(&val, num_val(&num)+1.0);
2675 hres = put_value(ctx, &exprval, &val, ei);
2678 exprval_release(&exprval);
2679 if(FAILED(hres))
2680 return hres;
2682 ret->type = EXPRVAL_VARIANT;
2683 ret->u.var = val;
2684 return S_OK;
2687 /* ECMA-262 3rd Edition 11.4.5 */
2688 HRESULT pre_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2690 unary_expression_t *expr = (unary_expression_t*)_expr;
2691 VARIANT val, num;
2692 exprval_t exprval;
2693 HRESULT hres;
2695 TRACE("\n");
2697 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2698 if(FAILED(hres))
2699 return hres;
2701 hres = exprval_value(ctx, &exprval, ei, &val);
2702 if(SUCCEEDED(hres)) {
2703 hres = to_number(ctx, &val, ei, &num);
2704 VariantClear(&val);
2707 if(SUCCEEDED(hres)) {
2708 num_set_val(&val, num_val(&num)-1.0);
2709 hres = put_value(ctx, &exprval, &val, ei);
2712 exprval_release(&exprval);
2713 if(FAILED(hres))
2714 return hres;
2716 ret->type = EXPRVAL_VARIANT;
2717 ret->u.var = val;
2718 return S_OK;
2721 /* ECMA-262 3rd Edition 11.9.3 */
2722 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2724 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2725 return equal2_values(lval, rval, ret);
2727 /* FIXME: NULL disps should be handled in more general way */
2728 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2729 VARIANT v;
2730 V_VT(&v) = VT_NULL;
2731 return equal_values(ctx, &v, rval, ei, ret);
2734 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2735 VARIANT v;
2736 V_VT(&v) = VT_NULL;
2737 return equal_values(ctx, lval, &v, ei, ret);
2740 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2741 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2742 *ret = TRUE;
2743 return S_OK;
2746 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2747 VARIANT v;
2748 HRESULT hres;
2750 hres = to_number(ctx, lval, ei, &v);
2751 if(FAILED(hres))
2752 return hres;
2754 return equal_values(ctx, &v, rval, ei, ret);
2757 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2758 VARIANT v;
2759 HRESULT hres;
2761 hres = to_number(ctx, rval, ei, &v);
2762 if(FAILED(hres))
2763 return hres;
2765 return equal_values(ctx, lval, &v, ei, ret);
2768 if(V_VT(rval) == VT_BOOL) {
2769 VARIANT v;
2771 V_VT(&v) = VT_I4;
2772 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2773 return equal_values(ctx, lval, &v, ei, ret);
2776 if(V_VT(lval) == VT_BOOL) {
2777 VARIANT v;
2779 V_VT(&v) = VT_I4;
2780 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2781 return equal_values(ctx, &v, rval, ei, ret);
2785 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2786 VARIANT v;
2787 HRESULT hres;
2789 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2790 if(FAILED(hres))
2791 return hres;
2793 hres = equal_values(ctx, lval, &v, ei, ret);
2795 VariantClear(&v);
2796 return hres;
2800 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2801 VARIANT v;
2802 HRESULT hres;
2804 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2805 if(FAILED(hres))
2806 return hres;
2808 hres = equal_values(ctx, &v, rval, ei, ret);
2810 VariantClear(&v);
2811 return hres;
2815 *ret = FALSE;
2816 return S_OK;
2819 /* ECMA-262 3rd Edition 11.9.1 */
2820 HRESULT equal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2822 binary_expression_t *expr = (binary_expression_t*)_expr;
2823 VARIANT rval, lval;
2824 BOOL b;
2825 HRESULT hres;
2827 TRACE("\n");
2829 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
2830 if(FAILED(hres))
2831 return hres;
2833 hres = equal_values(ctx, &rval, &lval, ei, &b);
2834 VariantClear(&lval);
2835 VariantClear(&rval);
2836 if(FAILED(hres))
2837 return hres;
2839 return return_bool(ret, b);
2842 /* ECMA-262 3rd Edition 11.9.4 */
2843 static HRESULT interp_eq2(exec_ctx_t *ctx)
2845 VARIANT *l, *r;
2846 BOOL b;
2847 HRESULT hres;
2849 TRACE("\n");
2851 r = stack_pop(ctx);
2852 l = stack_pop(ctx);
2854 hres = equal2_values(r, l, &b);
2855 VariantClear(l);
2856 VariantClear(r);
2857 if(FAILED(hres))
2858 return hres;
2860 return stack_push_bool(ctx, b);
2863 /* ECMA-262 3rd Edition 11.9.2 */
2864 HRESULT not_equal_expression_eval(script_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 = equal_values(ctx, &lval, &rval, 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.9.5 */
2887 static HRESULT interp_neq2(exec_ctx_t *ctx)
2889 VARIANT *l, *r;
2890 BOOL b;
2891 HRESULT hres;
2893 TRACE("\n");
2895 r = stack_pop(ctx);
2896 l = stack_pop(ctx);
2898 hres = equal2_values(r, l, &b);
2899 VariantClear(l);
2900 VariantClear(r);
2901 if(FAILED(hres))
2902 return hres;
2904 return stack_push_bool(ctx, !b);
2907 /* ECMA-262 3rd Edition 11.8.5 */
2908 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2910 VARIANT l, r, ln, rn;
2911 HRESULT hres;
2913 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2914 if(FAILED(hres))
2915 return hres;
2917 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2918 if(FAILED(hres)) {
2919 VariantClear(&l);
2920 return hres;
2923 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2924 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2925 SysFreeString(V_BSTR(&l));
2926 SysFreeString(V_BSTR(&r));
2927 return S_OK;
2930 hres = to_number(ctx, &l, ei, &ln);
2931 VariantClear(&l);
2932 if(SUCCEEDED(hres))
2933 hres = to_number(ctx, &r, ei, &rn);
2934 VariantClear(&r);
2935 if(FAILED(hres))
2936 return hres;
2938 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2939 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2940 }else {
2941 DOUBLE ld = num_val(&ln);
2942 DOUBLE rd = num_val(&rn);
2944 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2947 return S_OK;
2950 /* ECMA-262 3rd Edition 11.8.1 */
2951 HRESULT less_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2953 binary_expression_t *expr = (binary_expression_t*)_expr;
2954 VARIANT rval, lval;
2955 BOOL b;
2956 HRESULT hres;
2958 TRACE("\n");
2960 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2961 if(FAILED(hres))
2962 return hres;
2964 hres = less_eval(ctx, &lval, &rval, FALSE, ei, &b);
2965 VariantClear(&lval);
2966 VariantClear(&rval);
2967 if(FAILED(hres))
2968 return hres;
2970 return return_bool(ret, b);
2973 /* ECMA-262 3rd Edition 11.8.3 */
2974 HRESULT lesseq_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2976 binary_expression_t *expr = (binary_expression_t*)_expr;
2977 VARIANT rval, lval;
2978 BOOL b;
2979 HRESULT hres;
2981 TRACE("\n");
2983 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
2984 if(FAILED(hres))
2985 return hres;
2987 hres = less_eval(ctx, &rval, &lval, TRUE, ei, &b);
2988 VariantClear(&lval);
2989 VariantClear(&rval);
2990 if(FAILED(hres))
2991 return hres;
2993 return return_bool(ret, b);
2996 /* ECMA-262 3rd Edition 11.8.2 */
2997 HRESULT greater_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2999 binary_expression_t *expr = (binary_expression_t*)_expr;
3000 VARIANT rval, lval;
3001 BOOL b;
3002 HRESULT hres;
3004 TRACE("\n");
3006 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3007 if(FAILED(hres))
3008 return hres;
3010 hres = less_eval(ctx, &rval, &lval, FALSE, ei, &b);
3011 VariantClear(&lval);
3012 VariantClear(&rval);
3013 if(FAILED(hres))
3014 return hres;
3016 return return_bool(ret, b);
3019 /* ECMA-262 3rd Edition 11.8.4 */
3020 HRESULT greatereq_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3022 binary_expression_t *expr = (binary_expression_t*)_expr;
3023 VARIANT rval, lval;
3024 BOOL b;
3025 HRESULT hres;
3027 TRACE("\n");
3029 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
3030 if(FAILED(hres))
3031 return hres;
3033 hres = less_eval(ctx, &lval, &rval, TRUE, ei, &b);
3034 VariantClear(&lval);
3035 VariantClear(&rval);
3036 if(FAILED(hres))
3037 return hres;
3039 return return_bool(ret, b);
3042 /* ECMA-262 3rd Edition 11.4.8 */
3043 HRESULT interp_bneg(exec_ctx_t *ctx)
3045 VARIANT *v, r;
3046 INT i;
3047 HRESULT hres;
3049 TRACE("\n");
3051 v = stack_pop(ctx);
3052 hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3053 VariantClear(v);
3054 if(FAILED(hres))
3055 return hres;
3057 V_VT(&r) = VT_I4;
3058 V_I4(&r) = ~i;
3059 return stack_push(ctx, &r);
3062 /* ECMA-262 3rd Edition 11.4.9 */
3063 HRESULT interp_neg(exec_ctx_t *ctx)
3065 VARIANT *v;
3066 VARIANT_BOOL b;
3067 HRESULT hres;
3069 TRACE("\n");
3071 v = stack_pop(ctx);
3072 hres = to_boolean(v, &b);
3073 VariantClear(v);
3074 if(FAILED(hres))
3075 return hres;
3077 return stack_push_bool(ctx, !b);
3080 /* ECMA-262 3rd Edition 11.7.1 */
3081 static HRESULT lshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3083 DWORD ri;
3084 INT li;
3085 HRESULT hres;
3087 hres = to_int32(ctx, lval, ei, &li);
3088 if(FAILED(hres))
3089 return hres;
3091 hres = to_uint32(ctx, rval, ei, &ri);
3092 if(FAILED(hres))
3093 return hres;
3095 V_VT(retv) = VT_I4;
3096 V_I4(retv) = li << (ri&0x1f);
3097 return S_OK;
3100 /* ECMA-262 3rd Edition 11.7.1 */
3101 HRESULT left_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3103 binary_expression_t *expr = (binary_expression_t*)_expr;
3105 TRACE("\n");
3107 return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3110 /* ECMA-262 3rd Edition 11.7.2 */
3111 static HRESULT rshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3113 DWORD ri;
3114 INT li;
3115 HRESULT hres;
3117 hres = to_int32(ctx, lval, ei, &li);
3118 if(FAILED(hres))
3119 return hres;
3121 hres = to_uint32(ctx, rval, ei, &ri);
3122 if(FAILED(hres))
3123 return hres;
3125 V_VT(retv) = VT_I4;
3126 V_I4(retv) = li >> (ri&0x1f);
3127 return S_OK;
3130 /* ECMA-262 3rd Edition 11.7.2 */
3131 HRESULT right_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3133 binary_expression_t *expr = (binary_expression_t*)_expr;
3135 TRACE("\n");
3137 return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3140 /* ECMA-262 3rd Edition 11.7.3 */
3141 static HRESULT rshift2_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3143 DWORD li, ri;
3144 HRESULT hres;
3146 hres = to_uint32(ctx, lval, ei, &li);
3147 if(FAILED(hres))
3148 return hres;
3150 hres = to_uint32(ctx, rval, ei, &ri);
3151 if(FAILED(hres))
3152 return hres;
3154 V_VT(retv) = VT_I4;
3155 V_I4(retv) = li >> (ri&0x1f);
3156 return S_OK;
3159 /* ECMA-262 3rd Edition 11.7.3 */
3160 HRESULT right2_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3162 binary_expression_t *expr = (binary_expression_t*)_expr;
3164 TRACE("\n");
3166 return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3169 /* ECMA-262 3rd Edition 11.13.1 */
3170 HRESULT assign_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3172 binary_expression_t *expr = (binary_expression_t*)_expr;
3173 exprval_t exprval, exprvalr;
3174 VARIANT rval;
3175 HRESULT hres;
3177 TRACE("\n");
3179 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
3180 if(FAILED(hres))
3181 return hres;
3183 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
3184 if(SUCCEEDED(hres)) {
3185 hres = exprval_to_value(ctx, &exprvalr, ei, &rval);
3186 exprval_release(&exprvalr);
3189 if(SUCCEEDED(hres)) {
3190 hres = put_value(ctx, &exprval, &rval, ei);
3191 if(FAILED(hres))
3192 VariantClear(&rval);
3195 exprval_release(&exprval);
3196 if(FAILED(hres))
3197 return hres;
3199 ret->type = EXPRVAL_VARIANT;
3200 ret->u.var = rval;
3201 return S_OK;
3204 /* ECMA-262 3rd Edition 11.13.2 */
3205 HRESULT assign_lshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3207 binary_expression_t *expr = (binary_expression_t*)_expr;
3209 TRACE("\n");
3211 return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3214 /* ECMA-262 3rd Edition 11.13.2 */
3215 HRESULT assign_rshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3217 binary_expression_t *expr = (binary_expression_t*)_expr;
3219 TRACE("\n");
3221 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3224 /* ECMA-262 3rd Edition 11.13.2 */
3225 HRESULT assign_rrshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3227 binary_expression_t *expr = (binary_expression_t*)_expr;
3229 TRACE("\n");
3231 return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3234 /* ECMA-262 3rd Edition 11.13.2 */
3235 HRESULT assign_add_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3237 binary_expression_t *expr = (binary_expression_t*)_expr;
3239 TRACE("\n");
3241 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
3244 /* ECMA-262 3rd Edition 11.13.2 */
3245 HRESULT assign_sub_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3247 binary_expression_t *expr = (binary_expression_t*)_expr;
3249 TRACE("\n");
3251 return assign_oper_eval(ctx, expr->expression1, expr->expression2, sub_eval, ei, ret);
3254 /* ECMA-262 3rd Edition 11.13.2 */
3255 HRESULT assign_mul_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3257 binary_expression_t *expr = (binary_expression_t*)_expr;
3259 TRACE("\n");
3261 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mul_eval, ei, ret);
3264 /* ECMA-262 3rd Edition 11.13.2 */
3265 HRESULT assign_div_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3267 binary_expression_t *expr = (binary_expression_t*)_expr;
3269 TRACE("\n");
3271 return assign_oper_eval(ctx, expr->expression1, expr->expression2, div_eval, ei, ret);
3274 /* ECMA-262 3rd Edition 11.13.2 */
3275 HRESULT assign_mod_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3277 binary_expression_t *expr = (binary_expression_t*)_expr;
3279 TRACE("\n");
3281 return assign_oper_eval(ctx, expr->expression1, expr->expression2, mod_eval, ei, ret);
3284 /* ECMA-262 3rd Edition 11.13.2 */
3285 HRESULT assign_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3287 binary_expression_t *expr = (binary_expression_t*)_expr;
3289 TRACE("\n");
3291 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3294 /* ECMA-262 3rd Edition 11.13.2 */
3295 HRESULT assign_or_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3297 binary_expression_t *expr = (binary_expression_t*)_expr;
3299 TRACE("\n");
3301 return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
3304 /* ECMA-262 3rd Edition 11.13.2 */
3305 HRESULT assign_xor_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3307 binary_expression_t *expr = (binary_expression_t*)_expr;
3309 TRACE("\n");
3311 return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);
3314 static HRESULT interp_ret(exec_ctx_t *ctx)
3316 TRACE("\n");
3318 ctx->ip = -1;
3319 return S_OK;
3322 static HRESULT interp_tree(exec_ctx_t *ctx)
3324 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3325 exprval_t val;
3326 VARIANT v;
3327 HRESULT hres;
3329 TRACE("\n");
3331 hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3332 if(FAILED(hres))
3333 return hres;
3335 hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3336 if(FAILED(hres))
3337 return hres;
3339 return stack_push(ctx, &v);
3342 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3344 static const op_func_t op_funcs[] = {
3345 #define X(x,a,b) interp_##x,
3346 OP_LIST
3347 #undef X
3350 static const unsigned op_move[] = {
3351 #define X(a,x,b) x,
3352 OP_LIST
3353 #undef X
3356 HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3358 exec_ctx_t *exec_ctx = ctx->exec_ctx;
3359 unsigned prev_ip, prev_top;
3360 jsop_t op;
3361 HRESULT hres = S_OK;
3363 TRACE("\n");
3365 prev_top = exec_ctx->top;
3366 prev_ip = exec_ctx->ip;
3367 exec_ctx->ip = expr->instr_off;
3369 while(exec_ctx->ip != -1) {
3370 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3371 hres = op_funcs[op](exec_ctx);
3372 if(FAILED(hres))
3373 break;
3374 exec_ctx->ip += op_move[op];
3377 exec_ctx->ip = prev_ip;
3379 if(FAILED(hres)) {
3380 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3381 *ei = exec_ctx->ei;
3382 memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3383 return hres;
3386 assert(exec_ctx->top == prev_top+1);
3388 ret->type = EXPRVAL_VARIANT;
3389 ret->u.var = *stack_pop(exec_ctx);
3390 return S_OK;
3393 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3395 HRESULT hres;
3397 TRACE("\n");
3399 hres = compile_subscript(ctx->exec_ctx->parser, expr, &expr->instr_off);
3400 if(FAILED(hres))
3401 return hres;
3403 return (expr->eval = interp_expression_eval)(ctx, expr, flags, ei, ret);