jscript: Invoke bytecode directly from expr_eval.
[wine/multimedia.git] / dlls / jscript / engine.c
blob00035f9822e55154c557fced774823968912f529
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
34 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
35 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
36 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
37 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
38 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
39 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
40 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
42 struct _return_type_t {
43 enum{
44 RT_NORMAL,
45 RT_RETURN,
46 RT_BREAK,
47 RT_CONTINUE
48 } type;
49 jsexcept_t ei;
52 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
54 return stat->eval(ctx, stat, rt, ret);
57 static HRESULT expr_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
59 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
61 if(!ctx->stack_size) {
62 ctx->stack = heap_alloc(16*sizeof(VARIANT));
63 if(!ctx->stack)
64 return E_OUTOFMEMORY;
65 ctx->stack_size = 16;
66 }else if(ctx->stack_size == ctx->top) {
67 VARIANT *new_stack;
69 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
70 if(!new_stack) {
71 VariantClear(v);
72 return E_OUTOFMEMORY;
75 ctx->stack = new_stack;
76 ctx->stack_size *= 2;
79 ctx->stack[ctx->top++] = *v;
80 return S_OK;
83 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
85 VARIANT v;
87 V_VT(&v) = VT_BOOL;
88 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
89 return stack_push(ctx, &v);
92 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
94 VARIANT v;
96 num_set_val(&v, number);
97 return stack_push(ctx, &v);
100 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
102 VARIANT v;
104 V_VT(&v) = VT_I4;
105 V_I4(&v) = n;
106 return stack_push(ctx, &v);
109 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
111 VARIANT v;
113 V_VT(&v) = VT_BSTR;
114 V_BSTR(&v) = SysAllocString(str);
115 return V_BSTR(&v) ? stack_push(ctx, &v) : E_OUTOFMEMORY;
118 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
120 VARIANT v;
121 HRESULT hres;
123 V_VT(&v) = VT_DISPATCH;
124 V_DISPATCH(&v) = disp;
125 hres = stack_push(ctx, &v);
126 if(FAILED(hres))
127 return hres;
129 V_VT(&v) = VT_INT;
130 V_INT(&v) = id;
131 return stack_push(ctx, &v);
134 static inline VARIANT *stack_top(exec_ctx_t *ctx)
136 assert(ctx->top);
137 return ctx->stack + ctx->top-1;
140 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
142 assert(ctx->top > n);
143 return ctx->stack + ctx->top-1-n;
146 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
148 assert(ctx->top);
149 return ctx->stack + --ctx->top;
152 static void stack_popn(exec_ctx_t *ctx, unsigned n)
154 while(n--)
155 VariantClear(stack_pop(ctx));
158 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
160 VARIANT *v;
161 HRESULT hres;
163 v = stack_pop(ctx);
164 hres = to_number(ctx->parser->script, v, &ctx->ei, r);
165 VariantClear(v);
166 return hres;
169 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
171 VARIANT *v;
172 HRESULT hres;
174 v = stack_pop(ctx);
175 if(V_VT(v) == VT_DISPATCH) {
176 if(!V_DISPATCH(v))
177 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
178 *r = V_DISPATCH(v);
179 return S_OK;
182 hres = to_object(ctx->parser->script, v, r);
183 VariantClear(v);
184 return hres;
187 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
189 return to_int32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
192 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
194 return to_uint32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
197 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
199 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
201 *id = V_INT(stack_pop(ctx));
202 return V_DISPATCH(stack_pop(ctx));
205 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
207 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
209 *id = V_INT(stack_topn(ctx, n));
210 return V_DISPATCH(stack_topn(ctx, n+1));
213 static void exprval_release(exprval_t *val)
215 switch(val->type) {
216 case EXPRVAL_VARIANT:
217 if(V_VT(&val->u.var) != VT_EMPTY)
218 VariantClear(&val->u.var);
219 return;
220 case EXPRVAL_IDREF:
221 if(val->u.idref.disp)
222 IDispatch_Release(val->u.idref.disp);
223 return;
224 case EXPRVAL_INVALID:
225 SysFreeString(val->u.identifier);
229 /* ECMA-262 3rd Edition 8.7.1 */
230 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
232 V_VT(ret) = VT_EMPTY;
234 switch(val->type) {
235 case EXPRVAL_VARIANT:
236 return VariantCopy(ret, &val->u.var);
237 case EXPRVAL_IDREF:
238 if(!val->u.idref.disp) {
239 FIXME("throw ReferenceError\n");
240 return E_FAIL;
243 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
244 case EXPRVAL_INVALID:
245 return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
248 ERR("type %d\n", val->type);
249 return E_FAIL;
252 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
254 if(val->type == EXPRVAL_VARIANT) {
255 *ret = val->u.var;
256 V_VT(&val->u.var) = VT_EMPTY;
257 return S_OK;
260 return exprval_value(ctx, val, ei, ret);
263 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
265 if(exprval->type != EXPRVAL_VARIANT) {
266 VARIANT val;
267 HRESULT hres;
269 hres = exprval_to_value(ctx, exprval, ei, &val);
270 if(FAILED(hres))
271 return hres;
273 hres = to_boolean(&val, b);
274 VariantClear(&val);
275 return hres;
278 return to_boolean(&exprval->u.var, b);
281 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
283 val->type = EXPRVAL_IDREF;
284 val->u.idref.disp = disp;
285 val->u.idref.id = id;
287 if(disp)
288 IDispatch_AddRef(disp);
291 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
293 scope_chain_t *new_scope;
295 new_scope = heap_alloc(sizeof(scope_chain_t));
296 if(!new_scope)
297 return E_OUTOFMEMORY;
299 new_scope->ref = 1;
301 jsdisp_addref(obj);
302 new_scope->obj = obj;
304 if(scope) {
305 scope_addref(scope);
306 new_scope->next = scope;
307 }else {
308 new_scope->next = NULL;
311 *ret = new_scope;
312 return S_OK;
315 static void scope_pop(scope_chain_t **scope)
317 scope_chain_t *tmp;
319 tmp = *scope;
320 *scope = tmp->next;
321 scope_release(tmp);
324 void scope_release(scope_chain_t *scope)
326 if(--scope->ref)
327 return;
329 if(scope->next)
330 scope_release(scope->next);
332 jsdisp_release(scope->obj);
333 heap_free(scope);
336 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
337 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
339 exec_ctx_t *ctx;
341 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
342 if(!ctx)
343 return E_OUTOFMEMORY;
345 ctx->ref = 1;
346 ctx->is_global = is_global;
348 if(this_obj)
349 ctx->this_obj = this_obj;
350 else if(script_ctx->host_global)
351 ctx->this_obj = script_ctx->host_global;
352 else
353 ctx->this_obj = to_disp(script_ctx->global);
354 IDispatch_AddRef(ctx->this_obj);
356 jsdisp_addref(var_disp);
357 ctx->var_disp = var_disp;
359 if(scope) {
360 scope_addref(scope);
361 ctx->scope_chain = scope;
364 *ret = ctx;
365 return S_OK;
368 void exec_release(exec_ctx_t *ctx)
370 if(--ctx->ref)
371 return;
373 if(ctx->scope_chain)
374 scope_release(ctx->scope_chain);
375 if(ctx->var_disp)
376 jsdisp_release(ctx->var_disp);
377 if(ctx->this_obj)
378 IDispatch_Release(ctx->this_obj);
379 heap_free(ctx->stack);
380 heap_free(ctx);
383 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
385 IDispatchEx *dispex;
386 HRESULT hres;
388 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
389 if(FAILED(hres)) {
390 TRACE("unsing IDispatch\n");
392 *id = 0;
393 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
396 *id = 0;
397 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
398 IDispatchEx_Release(dispex);
399 return hres;
402 /* ECMA-262 3rd Edition 8.7.2 */
403 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
405 if(ref->type != EXPRVAL_IDREF)
406 return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
408 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
411 static inline BOOL is_null(const VARIANT *v)
413 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
416 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
418 IObjectIdentity *identity;
419 IUnknown *unk1, *unk2;
420 HRESULT hres;
422 if(disp1 == disp2) {
423 *ret = TRUE;
424 return S_OK;
427 if(!disp1 || !disp2) {
428 *ret = FALSE;
429 return S_OK;
432 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
433 if(FAILED(hres))
434 return hres;
436 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
437 if(FAILED(hres)) {
438 IUnknown_Release(unk1);
439 return hres;
442 if(unk1 == unk2) {
443 *ret = TRUE;
444 }else {
445 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
446 if(SUCCEEDED(hres)) {
447 hres = IObjectIdentity_IsEqualObject(identity, unk2);
448 IObjectIdentity_Release(identity);
449 *ret = hres == S_OK;
450 }else {
451 *ret = FALSE;
455 IUnknown_Release(unk1);
456 IUnknown_Release(unk2);
457 return S_OK;
460 /* ECMA-262 3rd Edition 11.9.6 */
461 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
463 TRACE("\n");
465 if(V_VT(lval) != V_VT(rval)) {
466 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
467 *ret = num_val(lval) == num_val(rval);
468 else if(is_null(lval))
469 *ret = is_null(rval);
470 else
471 *ret = FALSE;
472 return S_OK;
475 switch(V_VT(lval)) {
476 case VT_EMPTY:
477 case VT_NULL:
478 *ret = VARIANT_TRUE;
479 break;
480 case VT_I4:
481 *ret = V_I4(lval) == V_I4(rval);
482 break;
483 case VT_R8:
484 *ret = V_R8(lval) == V_R8(rval);
485 break;
486 case VT_BSTR:
487 if(!V_BSTR(lval))
488 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
489 else if(!V_BSTR(rval))
490 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
491 else
492 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
493 break;
494 case VT_DISPATCH:
495 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
496 case VT_BOOL:
497 *ret = !V_BOOL(lval) == !V_BOOL(rval);
498 break;
499 default:
500 FIXME("unimplemented vt %d\n", V_VT(lval));
501 return E_NOTIMPL;
504 return S_OK;
507 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
509 named_item_t *item;
510 DISPID id;
511 HRESULT hres;
513 for(item = ctx->named_items; item; item = item->next) {
514 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
515 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
516 if(SUCCEEDED(hres)) {
517 if(ret)
518 exprval_set_idref(ret, item->disp, id);
519 return TRUE;
524 return FALSE;
527 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
528 jsexcept_t *ei, VARIANT *retv)
530 script_ctx_t *script = parser->script;
531 function_declaration_t *func;
532 parser_ctx_t *prev_parser;
533 var_list_t *var;
534 VARIANT val, tmp;
535 statement_t *stat;
536 exec_ctx_t *prev_ctx;
537 return_type_t rt;
538 HRESULT hres = S_OK;
540 for(func = source->functions; func; func = func->next) {
541 jsdisp_t *func_obj;
542 VARIANT var;
544 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
545 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
546 if(FAILED(hres))
547 return hres;
549 var_set_jsdisp(&var, func_obj);
550 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
551 jsdisp_release(func_obj);
552 if(FAILED(hres))
553 return hres;
556 for(var = source->variables; var; var = var->next) {
557 DISPID id = 0;
558 BSTR name;
560 name = SysAllocString(var->identifier);
561 if(!name)
562 return E_OUTOFMEMORY;
564 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
565 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
566 SysFreeString(name);
567 if(FAILED(hres))
568 return hres;
571 prev_ctx = script->exec_ctx;
572 script->exec_ctx = ctx;
574 prev_parser = ctx->parser;
575 ctx->parser = parser;
577 V_VT(&val) = VT_EMPTY;
578 memset(&rt, 0, sizeof(rt));
579 rt.type = RT_NORMAL;
581 for(stat = source->statement; stat; stat = stat->next) {
582 hres = stat_eval(script, stat, &rt, &tmp);
583 if(FAILED(hres))
584 break;
586 VariantClear(&val);
587 val = tmp;
588 if(rt.type != RT_NORMAL)
589 break;
592 script->exec_ctx = prev_ctx;
593 ctx->parser = prev_parser;
595 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
596 FIXME("wrong rt %d\n", rt.type);
597 hres = E_FAIL;
600 *ei = rt.ei;
601 if(FAILED(hres)) {
602 VariantClear(&val);
603 return hres;
606 if(!retv || (!from_eval && rt.type != RT_RETURN))
607 VariantClear(&val);
608 if(retv)
609 *retv = val;
610 return S_OK;
613 /* ECMA-262 3rd Edition 10.1.4 */
614 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
616 scope_chain_t *scope;
617 named_item_t *item;
618 DISPID id = 0;
619 HRESULT hres;
621 TRACE("%s\n", debugstr_w(identifier));
623 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
624 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
625 if(SUCCEEDED(hres)) {
626 exprval_set_idref(ret, to_disp(scope->obj), id);
627 return S_OK;
631 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
632 if(SUCCEEDED(hres)) {
633 exprval_set_idref(ret, to_disp(ctx->global), id);
634 return S_OK;
637 for(item = ctx->named_items; item; item = item->next) {
638 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
639 if(!item->disp) {
640 IUnknown *unk;
642 if(!ctx->site)
643 break;
645 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
646 SCRIPTINFO_IUNKNOWN, &unk, NULL);
647 if(FAILED(hres)) {
648 WARN("GetItemInfo failed: %08x\n", hres);
649 break;
652 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
653 IUnknown_Release(unk);
654 if(FAILED(hres)) {
655 WARN("object does not implement IDispatch\n");
656 break;
660 ret->type = EXPRVAL_VARIANT;
661 V_VT(&ret->u.var) = VT_DISPATCH;
662 V_DISPATCH(&ret->u.var) = item->disp;
663 IDispatch_AddRef(item->disp);
664 return S_OK;
668 if(lookup_global_members(ctx, identifier, ret))
669 return S_OK;
671 if(flags & fdexNameEnsure) {
672 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
673 if(FAILED(hres))
674 return hres;
676 exprval_set_idref(ret, to_disp(ctx->global), id);
677 return S_OK;
680 ret->type = EXPRVAL_INVALID;
681 ret->u.identifier = SysAllocString(identifier);
682 if(!ret->u.identifier)
683 return E_OUTOFMEMORY;
685 return S_OK;
688 /* ECMA-262 3rd Edition 12.1 */
689 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
691 block_statement_t *stat = (block_statement_t*)_stat;
692 VARIANT val, tmp;
693 statement_t *iter;
694 HRESULT hres = S_OK;
696 TRACE("\n");
698 V_VT(&val) = VT_EMPTY;
699 for(iter = stat->stat_list; iter; iter = iter->next) {
700 hres = stat_eval(ctx, iter, rt, &tmp);
701 if(FAILED(hres))
702 break;
704 VariantClear(&val);
705 val = tmp;
706 if(rt->type != RT_NORMAL)
707 break;
710 if(FAILED(hres)) {
711 VariantClear(&val);
712 return hres;
715 *ret = val;
716 return S_OK;
719 /* ECMA-262 3rd Edition 12.2 */
720 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
722 variable_declaration_t *iter;
723 HRESULT hres = S_OK;
725 for(iter = var_list; iter; iter = iter->next) {
726 exprval_t exprval;
727 VARIANT val;
729 if(!iter->expr)
730 continue;
732 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
733 if(FAILED(hres))
734 break;
736 hres = exprval_to_value(ctx, &exprval, ei, &val);
737 exprval_release(&exprval);
738 if(FAILED(hres))
739 break;
741 hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
742 VariantClear(&val);
743 if(FAILED(hres))
744 break;
747 return hres;
750 /* ECMA-262 3rd Edition 12.2 */
751 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
753 var_statement_t *stat = (var_statement_t*)_stat;
754 HRESULT hres;
756 TRACE("\n");
758 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
759 if(FAILED(hres))
760 return hres;
762 V_VT(ret) = VT_EMPTY;
763 return S_OK;
766 /* ECMA-262 3rd Edition 12.3 */
767 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
769 TRACE("\n");
771 V_VT(ret) = VT_EMPTY;
772 return S_OK;
775 /* ECMA-262 3rd Edition 12.4 */
776 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
778 expression_statement_t *stat = (expression_statement_t*)_stat;
779 exprval_t exprval;
780 VARIANT val;
781 HRESULT hres;
783 TRACE("\n");
785 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
786 if(FAILED(hres))
787 return hres;
789 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
790 exprval_release(&exprval);
791 if(FAILED(hres))
792 return hres;
794 *ret = val;
795 TRACE("= %s\n", debugstr_variant(ret));
796 return S_OK;
799 /* ECMA-262 3rd Edition 12.5 */
800 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
802 if_statement_t *stat = (if_statement_t*)_stat;
803 exprval_t exprval;
804 VARIANT_BOOL b;
805 HRESULT hres;
807 TRACE("\n");
809 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
810 if(FAILED(hres))
811 return hres;
813 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
814 exprval_release(&exprval);
815 if(FAILED(hres))
816 return hres;
818 if(b)
819 hres = stat_eval(ctx, stat->if_stat, rt, ret);
820 else if(stat->else_stat)
821 hres = stat_eval(ctx, stat->else_stat, rt, ret);
822 else
823 V_VT(ret) = VT_EMPTY;
825 return hres;
828 /* ECMA-262 3rd Edition 12.6.2 */
829 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
831 while_statement_t *stat = (while_statement_t*)_stat;
832 exprval_t exprval;
833 VARIANT val, tmp;
834 VARIANT_BOOL b;
835 BOOL test_expr;
836 HRESULT hres;
838 TRACE("\n");
840 V_VT(&val) = VT_EMPTY;
841 test_expr = !stat->do_while;
843 while(1) {
844 if(test_expr) {
845 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
846 if(FAILED(hres))
847 break;
849 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
850 exprval_release(&exprval);
851 if(FAILED(hres) || !b)
852 break;
853 }else {
854 test_expr = TRUE;
857 hres = stat_eval(ctx, stat->statement, rt, &tmp);
858 if(FAILED(hres))
859 break;
861 VariantClear(&val);
862 val = tmp;
864 if(rt->type == RT_CONTINUE)
865 rt->type = RT_NORMAL;
866 if(rt->type != RT_NORMAL)
867 break;
870 if(FAILED(hres)) {
871 VariantClear(&val);
872 return hres;
875 if(rt->type == RT_BREAK)
876 rt->type = RT_NORMAL;
878 *ret = val;
879 return S_OK;
882 /* ECMA-262 3rd Edition 12.6.3 */
883 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
885 for_statement_t *stat = (for_statement_t*)_stat;
886 VARIANT val, tmp, retv;
887 exprval_t exprval;
888 VARIANT_BOOL b;
889 HRESULT hres;
891 TRACE("\n");
893 if(stat->variable_list) {
894 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
895 if(FAILED(hres))
896 return hres;
897 }else if(stat->begin_expr) {
898 hres = expr_eval(ctx, stat->begin_expr, 0, &rt->ei, &exprval);
899 if(FAILED(hres))
900 return hres;
902 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
903 exprval_release(&exprval);
904 if(FAILED(hres))
905 return hres;
907 VariantClear(&val);
910 V_VT(&retv) = VT_EMPTY;
912 while(1) {
913 if(stat->expr) {
914 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
915 if(FAILED(hres))
916 break;
918 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
919 exprval_release(&exprval);
920 if(FAILED(hres) || !b)
921 break;
924 hres = stat_eval(ctx, stat->statement, rt, &tmp);
925 if(FAILED(hres))
926 break;
928 VariantClear(&retv);
929 retv = tmp;
931 if(rt->type == RT_CONTINUE)
932 rt->type = RT_NORMAL;
933 else if(rt->type != RT_NORMAL)
934 break;
936 if(stat->end_expr) {
937 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
938 if(FAILED(hres))
939 break;
941 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
942 exprval_release(&exprval);
943 if(FAILED(hres))
944 break;
946 VariantClear(&val);
950 if(FAILED(hres)) {
951 VariantClear(&retv);
952 return hres;
955 if(rt->type == RT_BREAK)
956 rt->type = RT_NORMAL;
958 *ret = retv;
959 return S_OK;
962 static HRESULT array_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
963 static HRESULT member_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
964 static HRESULT identifier_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
966 /* ECMA-262 3rd Edition 12.6.4 */
967 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
969 forin_statement_t *stat = (forin_statement_t*)_stat;
970 VARIANT val, name, retv, tmp;
971 DISPID id = DISPID_STARTENUM;
972 BSTR str, identifier = NULL;
973 IDispatchEx *in_obj;
974 exprval_t exprval;
975 HRESULT hres;
977 TRACE("\n");
979 if(stat->variable) {
980 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
981 if(FAILED(hres))
982 return hres;
985 hres = expr_eval(ctx, stat->in_expr, 0, &rt->ei, &exprval);
986 if(FAILED(hres))
987 return hres;
989 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
990 exprval_release(&exprval);
991 if(FAILED(hres))
992 return hres;
994 if(V_VT(&val) != VT_DISPATCH) {
995 TRACE("in vt %d\n", V_VT(&val));
996 VariantClear(&val);
997 V_VT(ret) = VT_EMPTY;
998 return S_OK;
1001 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
1002 IDispatch_Release(V_DISPATCH(&val));
1003 if(FAILED(hres)) {
1004 TRACE("Object doesn't support IDispatchEx\n");
1005 V_VT(ret) = VT_EMPTY;
1006 return S_OK;
1009 V_VT(&retv) = VT_EMPTY;
1011 if(stat->variable)
1012 identifier = SysAllocString(stat->variable->identifier);
1014 while(1) {
1015 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
1016 if(FAILED(hres) || hres == S_FALSE)
1017 break;
1019 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
1020 if(FAILED(hres))
1021 break;
1023 TRACE("iter %s\n", debugstr_w(str));
1025 if(stat->variable) {
1026 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
1027 }else {
1028 switch(stat->expr->type) {
1029 case EXPR_ARRAY:
1030 hres = array_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
1031 break;
1032 case EXPR_IDENT:
1033 hres = identifier_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
1034 break;
1035 case EXPR_MEMBER:
1036 hres = member_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
1037 break;
1038 default:
1039 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1042 if(SUCCEEDED(hres)) {
1043 V_VT(&name) = VT_BSTR;
1044 V_BSTR(&name) = str;
1045 hres = put_value(ctx, &exprval, &name, &rt->ei);
1046 exprval_release(&exprval);
1048 SysFreeString(str);
1049 if(FAILED(hres))
1050 break;
1052 hres = stat_eval(ctx, stat->statement, rt, &tmp);
1053 if(FAILED(hres))
1054 break;
1056 VariantClear(&retv);
1057 retv = tmp;
1059 if(rt->type == RT_CONTINUE)
1060 rt->type = RT_NORMAL;
1061 else if(rt->type != RT_NORMAL)
1062 break;
1065 SysFreeString(identifier);
1066 IDispatchEx_Release(in_obj);
1067 if(FAILED(hres)) {
1068 VariantClear(&retv);
1069 return hres;
1072 if(rt->type == RT_BREAK)
1073 rt->type = RT_NORMAL;
1075 *ret = retv;
1076 return S_OK;
1079 /* ECMA-262 3rd Edition 12.7 */
1080 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1082 branch_statement_t *stat = (branch_statement_t*)_stat;
1084 TRACE("\n");
1086 if(stat->identifier) {
1087 FIXME("indentifier not implemented\n");
1088 return E_NOTIMPL;
1091 rt->type = RT_CONTINUE;
1092 V_VT(ret) = VT_EMPTY;
1093 return S_OK;
1096 /* ECMA-262 3rd Edition 12.8 */
1097 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1099 branch_statement_t *stat = (branch_statement_t*)_stat;
1101 TRACE("\n");
1103 if(stat->identifier) {
1104 FIXME("indentifier not implemented\n");
1105 return E_NOTIMPL;
1108 rt->type = RT_BREAK;
1109 V_VT(ret) = VT_EMPTY;
1110 return S_OK;
1113 /* ECMA-262 3rd Edition 12.9 */
1114 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1116 expression_statement_t *stat = (expression_statement_t*)_stat;
1117 HRESULT hres;
1119 TRACE("\n");
1121 if(stat->expr) {
1122 exprval_t exprval;
1124 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1125 if(FAILED(hres))
1126 return hres;
1128 hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1129 exprval_release(&exprval);
1130 if(FAILED(hres))
1131 return hres;
1132 }else {
1133 V_VT(ret) = VT_EMPTY;
1136 TRACE("= %s\n", debugstr_variant(ret));
1137 rt->type = RT_RETURN;
1138 return S_OK;
1141 /* ECMA-262 3rd Edition 12.10 */
1142 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1144 with_statement_t *stat = (with_statement_t*)_stat;
1145 exprval_t exprval;
1146 IDispatch *disp;
1147 jsdisp_t *obj;
1148 VARIANT val;
1149 HRESULT hres;
1151 TRACE("\n");
1153 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1154 if(FAILED(hres))
1155 return hres;
1157 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1158 exprval_release(&exprval);
1159 if(FAILED(hres))
1160 return hres;
1162 hres = to_object(ctx, &val, &disp);
1163 VariantClear(&val);
1164 if(FAILED(hres))
1165 return hres;
1167 obj = iface_to_jsdisp((IUnknown*)disp);
1168 IDispatch_Release(disp);
1169 if(!obj) {
1170 FIXME("disp id not jsdisp\n");
1171 return E_NOTIMPL;
1174 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1175 jsdisp_release(obj);
1176 if(FAILED(hres))
1177 return hres;
1179 hres = stat_eval(ctx, stat->statement, rt, ret);
1181 scope_pop(&ctx->exec_ctx->scope_chain);
1182 return hres;
1185 /* ECMA-262 3rd Edition 12.12 */
1186 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1188 FIXME("\n");
1189 return E_NOTIMPL;
1192 /* ECMA-262 3rd Edition 12.13 */
1193 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1195 switch_statement_t *stat = (switch_statement_t*)_stat;
1196 case_clausule_t *iter, *default_clausule = NULL;
1197 statement_t *stat_iter;
1198 VARIANT val, cval;
1199 exprval_t exprval;
1200 BOOL b;
1201 HRESULT hres;
1203 TRACE("\n");
1205 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1206 if(FAILED(hres))
1207 return hres;
1209 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1210 exprval_release(&exprval);
1211 if(FAILED(hres))
1212 return hres;
1214 for(iter = stat->case_list; iter; iter = iter->next) {
1215 if(!iter->expr) {
1216 default_clausule = iter;
1217 continue;
1220 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1221 if(FAILED(hres))
1222 break;
1224 hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1225 exprval_release(&exprval);
1226 if(FAILED(hres))
1227 break;
1229 hres = equal2_values(&val, &cval, &b);
1230 VariantClear(&cval);
1231 if(FAILED(hres) || b)
1232 break;
1235 VariantClear(&val);
1236 if(FAILED(hres))
1237 return hres;
1239 if(!iter)
1240 iter = default_clausule;
1242 V_VT(&val) = VT_EMPTY;
1243 if(iter) {
1244 VARIANT tmp;
1246 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1247 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1248 if(FAILED(hres))
1249 break;
1251 VariantClear(&val);
1252 val = tmp;
1254 if(rt->type != RT_NORMAL)
1255 break;
1259 if(FAILED(hres)) {
1260 VariantClear(&val);
1261 return hres;
1264 if(rt->type == RT_BREAK)
1265 rt->type = RT_NORMAL;
1267 *ret = val;
1268 return S_OK;
1271 /* ECMA-262 3rd Edition 12.13 */
1272 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1274 expression_statement_t *stat = (expression_statement_t*)_stat;
1275 exprval_t exprval;
1276 VARIANT val;
1277 HRESULT hres;
1279 TRACE("\n");
1281 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1282 if(FAILED(hres))
1283 return hres;
1285 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1286 exprval_release(&exprval);
1287 if(FAILED(hres))
1288 return hres;
1290 rt->ei.var = val;
1291 return DISP_E_EXCEPTION;
1294 static HRESULT interp_throw(exec_ctx_t *ctx)
1296 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1298 TRACE("%08x\n", arg);
1300 return throw_reference_error(ctx->parser->script, &ctx->ei, arg, NULL);
1303 static HRESULT interp_throw_type(exec_ctx_t *ctx)
1305 const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1306 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
1308 TRACE("%08x %s\n", hres, debugstr_w(str));
1310 return throw_type_error(ctx->parser->script, &ctx->ei, hres, str);
1313 /* ECMA-262 3rd Edition 12.14 */
1314 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1316 jsdisp_t *var_disp;
1317 VARIANT ex, val;
1318 HRESULT hres;
1320 ex = rt->ei.var;
1321 memset(&rt->ei, 0, sizeof(jsexcept_t));
1323 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1324 if(SUCCEEDED(hres)) {
1325 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1326 if(SUCCEEDED(hres)) {
1327 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1328 if(SUCCEEDED(hres)) {
1329 hres = stat_eval(ctx, block->statement, rt, &val);
1330 scope_pop(&ctx->exec_ctx->scope_chain);
1334 jsdisp_release(var_disp);
1337 VariantClear(&ex);
1338 if(FAILED(hres))
1339 return hres;
1341 *ret = val;
1342 return S_OK;
1345 /* ECMA-262 3rd Edition 12.14 */
1346 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1348 try_statement_t *stat = (try_statement_t*)_stat;
1349 VARIANT val;
1350 HRESULT hres;
1352 TRACE("\n");
1354 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1355 if(FAILED(hres)) {
1356 TRACE("EXCEPTION\n");
1357 if(!stat->catch_block)
1358 return hres;
1360 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1361 if(FAILED(hres))
1362 return hres;
1365 if(stat->finally_statement) {
1366 VariantClear(&val);
1367 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1368 if(FAILED(hres))
1369 return hres;
1372 *ret = val;
1373 return S_OK;
1376 /* ECMA-262 3rd Edition 13 */
1377 static HRESULT interp_func(exec_ctx_t *ctx)
1379 function_expression_t *expr = ctx->parser->code->instrs[ctx->ip].arg1.func;
1380 jsdisp_t *dispex;
1381 VARIANT v;
1382 HRESULT hres;
1384 TRACE("\n");
1386 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1387 expr->src_str, expr->src_len, &dispex);
1388 if(FAILED(hres))
1389 return hres;
1391 var_set_jsdisp(&v, dispex);
1392 return stack_push(ctx, &v);
1395 /* ECMA-262 3rd Edition 11.2.1 */
1396 static HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1398 binary_expression_t *expr = (binary_expression_t*)_expr;
1399 exprval_t exprval;
1400 VARIANT member, val;
1401 DISPID id;
1402 BSTR str;
1403 IDispatch *obj = NULL;
1404 HRESULT hres;
1406 TRACE("\n");
1408 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1409 if(FAILED(hres))
1410 return hres;
1412 hres = exprval_to_value(ctx, &exprval, ei, &member);
1413 exprval_release(&exprval);
1414 if(FAILED(hres))
1415 return hres;
1417 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1418 if(SUCCEEDED(hres)) {
1419 hres = exprval_to_value(ctx, &exprval, ei, &val);
1420 exprval_release(&exprval);
1423 if(SUCCEEDED(hres)) {
1424 hres = to_object(ctx, &member, &obj);
1425 if(FAILED(hres))
1426 VariantClear(&val);
1428 VariantClear(&member);
1429 if(SUCCEEDED(hres)) {
1430 hres = to_string(ctx, &val, ei, &str);
1431 VariantClear(&val);
1432 if(SUCCEEDED(hres)) {
1433 hres = disp_get_id(ctx, obj, str, fdexNameEnsure, &id);
1434 SysFreeString(str);
1437 if(SUCCEEDED(hres))
1438 exprval_set_idref(ret, obj, id);
1440 IDispatch_Release(obj);
1443 return hres;
1446 /* ECMA-262 3rd Edition 11.2.1 */
1447 static HRESULT interp_array(exec_ctx_t *ctx)
1449 VARIANT v, *namev;
1450 IDispatch *obj;
1451 DISPID id;
1452 BSTR name;
1453 HRESULT hres;
1455 TRACE("\n");
1457 namev = stack_pop(ctx);
1459 hres = stack_pop_object(ctx, &obj);
1460 if(FAILED(hres)) {
1461 VariantClear(namev);
1462 return hres;
1465 hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1466 VariantClear(namev);
1467 if(FAILED(hres)) {
1468 IDispatch_Release(obj);
1469 return hres;
1472 hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
1473 SysFreeString(name);
1474 if(SUCCEEDED(hres)) {
1475 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1476 }else if(hres == DISP_E_UNKNOWNNAME) {
1477 V_VT(&v) = VT_EMPTY;
1478 hres = S_OK;
1480 IDispatch_Release(obj);
1481 if(FAILED(hres))
1482 return hres;
1484 return stack_push(ctx, &v);
1487 /* ECMA-262 3rd Edition 11.2.1 */
1488 static HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1490 member_expression_t *expr = (member_expression_t*)_expr;
1491 IDispatch *obj = NULL;
1492 exprval_t exprval;
1493 VARIANT member;
1494 DISPID id;
1495 BSTR str;
1496 HRESULT hres;
1498 TRACE("\n");
1500 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1501 if(FAILED(hres))
1502 return hres;
1504 hres = exprval_to_value(ctx, &exprval, ei, &member);
1505 exprval_release(&exprval);
1506 if(FAILED(hres))
1507 return hres;
1509 hres = to_object(ctx, &member, &obj);
1510 VariantClear(&member);
1511 if(FAILED(hres))
1512 return hres;
1514 str = SysAllocString(expr->identifier);
1515 if(!str) {
1516 IDispatch_Release(obj);
1517 return E_OUTOFMEMORY;
1520 hres = disp_get_id(ctx, obj, str, fdexNameEnsure, &id);
1521 SysFreeString(str);
1522 if(SUCCEEDED(hres))
1523 exprval_set_idref(ret, obj, id);
1525 IDispatch_Release(obj);
1526 return hres;
1529 /* ECMA-262 3rd Edition 11.2.1 */
1530 static HRESULT interp_member(exec_ctx_t *ctx)
1532 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1533 IDispatch *obj;
1534 VARIANT v;
1535 DISPID id;
1536 HRESULT hres;
1538 TRACE("\n");
1540 hres = stack_pop_object(ctx, &obj);
1541 if(FAILED(hres))
1542 return hres;
1544 hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
1545 if(SUCCEEDED(hres)) {
1546 V_VT(&v) = VT_EMPTY;
1547 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1548 }else if(hres == DISP_E_UNKNOWNNAME) {
1549 V_VT(&v) = VT_EMPTY;
1550 hres = S_OK;
1552 IDispatch_Release(obj);
1553 if(FAILED(hres))
1554 return hres;
1556 return stack_push(ctx, &v);
1559 /* ECMA-262 3rd Edition 11.2.1 */
1560 static HRESULT interp_memberid(exec_ctx_t *ctx)
1562 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1563 VARIANT *objv, *namev;
1564 IDispatch *obj;
1565 BSTR name;
1566 DISPID id;
1567 HRESULT hres;
1569 TRACE("%x\n", arg);
1571 namev = stack_pop(ctx);
1572 objv = stack_pop(ctx);
1574 hres = to_object(ctx->parser->script, objv, &obj);
1575 VariantClear(objv);
1576 if(SUCCEEDED(hres)) {
1577 hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1578 if(FAILED(hres))
1579 IDispatch_Release(obj);
1581 VariantClear(namev);
1582 if(FAILED(hres))
1583 return hres;
1585 hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
1586 SysFreeString(name);
1587 if(FAILED(hres)) {
1588 IDispatch_Release(obj);
1589 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1590 obj = NULL;
1591 id = JS_E_INVALID_PROPERTY;
1592 }else {
1593 return hres;
1597 return stack_push_objid(ctx, obj, id);
1600 /* ECMA-262 3rd Edition 11.2.1 */
1601 static HRESULT interp_refval(exec_ctx_t *ctx)
1603 IDispatch *disp;
1604 VARIANT v;
1605 DISPID id;
1606 HRESULT hres;
1608 TRACE("\n");
1610 disp = stack_topn_objid(ctx, 0, &id);
1611 if(!disp)
1612 return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1614 hres = disp_propget(ctx->parser->script, disp, id, &v, &ctx->ei, NULL/*FIXME*/);
1615 if(FAILED(hres))
1616 return hres;
1618 return stack_push(ctx, &v);
1621 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1623 VARIANT tmp;
1624 unsigned i;
1626 dp->cArgs = arg_cnt;
1627 dp->rgdispidNamedArgs = NULL;
1628 dp->cNamedArgs = 0;
1630 assert(ctx->top >= arg_cnt);
1632 for(i=1; i*2 <= arg_cnt; i++) {
1633 tmp = ctx->stack[ctx->top-i];
1634 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1635 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1638 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1641 /* ECMA-262 3rd Edition 11.2.2 */
1642 static HRESULT interp_new(exec_ctx_t *ctx)
1644 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1645 VARIANT *constr, v;
1646 DISPPARAMS dp;
1647 HRESULT hres;
1649 TRACE("%d\n", arg);
1651 constr = stack_topn(ctx, arg);
1653 /* NOTE: Should use to_object here */
1655 if(V_VT(constr) == VT_NULL)
1656 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1657 else if(V_VT(constr) != VT_DISPATCH)
1658 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1659 else if(!V_DISPATCH(constr))
1660 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1662 jsstack_to_dp(ctx, arg, &dp);
1663 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1664 DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1665 if(FAILED(hres))
1666 return hres;
1668 stack_popn(ctx, arg+1);
1669 return stack_push(ctx, &v);
1672 /* ECMA-262 3rd Edition 11.2.3 */
1673 static HRESULT interp_call(exec_ctx_t *ctx)
1675 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1676 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1677 VARIANT v, *objv;
1678 DISPPARAMS dp;
1679 HRESULT hres;
1681 TRACE("%d %d\n", argn, do_ret);
1683 objv = stack_topn(ctx, argn);
1684 if(V_VT(objv) != VT_DISPATCH)
1685 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1687 jsstack_to_dp(ctx, argn, &dp);
1688 hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1689 do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1690 if(FAILED(hres))
1691 return hres;
1693 stack_popn(ctx, argn+1);
1694 return do_ret ? stack_push(ctx, &v) : S_OK;
1698 /* ECMA-262 3rd Edition 11.2.3 */
1699 static HRESULT interp_call_member(exec_ctx_t *ctx)
1701 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1702 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1703 IDispatch *obj;
1704 DISPPARAMS dp;
1705 VARIANT v;
1706 DISPID id;
1707 HRESULT hres;
1709 TRACE("%d %d\n", argn, do_ret);
1711 obj = stack_topn_objid(ctx, argn, &id);
1712 if(!obj)
1713 return throw_type_error(ctx->parser->script, &ctx->ei, id, NULL);
1715 jsstack_to_dp(ctx, argn, &dp);
1716 hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1717 if(FAILED(hres))
1718 return hres;
1720 stack_popn(ctx, argn+2);
1721 return do_ret ? stack_push(ctx, &v) : S_OK;
1725 /* ECMA-262 3rd Edition 11.1.1 */
1726 static HRESULT interp_this(exec_ctx_t *ctx)
1728 VARIANT v;
1730 TRACE("\n");
1732 V_VT(&v) = VT_DISPATCH;
1733 V_DISPATCH(&v) = ctx->this_obj;
1734 IDispatch_AddRef(ctx->this_obj);
1735 return stack_push(ctx, &v);
1738 /* ECMA-262 3rd Edition 10.1.4 */
1739 static HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1741 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1742 BSTR identifier;
1743 HRESULT hres;
1745 TRACE("\n");
1747 identifier = SysAllocString(expr->identifier);
1748 if(!identifier)
1749 return E_OUTOFMEMORY;
1751 hres = identifier_eval(ctx, identifier, fdexNameEnsure, ei, ret);
1753 SysFreeString(identifier);
1754 return hres;
1757 /* ECMA-262 3rd Edition 10.1.4 */
1758 static HRESULT interp_ident(exec_ctx_t *ctx)
1760 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1761 exprval_t exprval;
1762 VARIANT v;
1763 HRESULT hres;
1765 TRACE("%s\n", debugstr_w(arg));
1767 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1768 if(FAILED(hres))
1769 return hres;
1771 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1772 exprval_release(&exprval);
1773 if(FAILED(hres))
1774 return hres;
1776 return stack_push(ctx, &v);
1779 /* ECMA-262 3rd Edition 10.1.4 */
1780 static HRESULT interp_identid(exec_ctx_t *ctx)
1782 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1783 const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1784 exprval_t exprval;
1785 HRESULT hres;
1787 TRACE("%s %x\n", debugstr_w(arg), flags);
1789 hres = identifier_eval(ctx->parser->script, arg, flags, &ctx->ei, &exprval);
1790 if(FAILED(hres))
1791 return hres;
1793 if(exprval.type != EXPRVAL_IDREF) {
1794 WARN("invalid ref\n");
1795 exprval_release(&exprval);
1796 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1799 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1802 /* ECMA-262 3rd Edition 7.8.1 */
1803 static HRESULT interp_null(exec_ctx_t *ctx)
1805 VARIANT v;
1807 TRACE("\n");
1809 V_VT(&v) = VT_NULL;
1810 return stack_push(ctx, &v);
1813 /* ECMA-262 3rd Edition 7.8.2 */
1814 static HRESULT interp_bool(exec_ctx_t *ctx)
1816 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1818 TRACE("%s\n", arg ? "true" : "false");
1820 return stack_push_bool(ctx, arg);
1823 /* ECMA-262 3rd Edition 7.8.3 */
1824 static HRESULT interp_int(exec_ctx_t *ctx)
1826 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1827 VARIANT v;
1829 TRACE("%d\n", arg);
1831 V_VT(&v) = VT_I4;
1832 V_I4(&v) = arg;
1833 return stack_push(ctx, &v);
1836 /* ECMA-262 3rd Edition 7.8.3 */
1837 static HRESULT interp_double(exec_ctx_t *ctx)
1839 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1840 VARIANT v;
1842 TRACE("%lf\n", arg);
1844 V_VT(&v) = VT_R8;
1845 V_R8(&v) = arg;
1846 return stack_push(ctx, &v);
1849 /* ECMA-262 3rd Edition 7.8.4 */
1850 static HRESULT interp_str(exec_ctx_t *ctx)
1852 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1853 VARIANT v;
1855 TRACE("%s\n", debugstr_w(str));
1857 V_VT(&v) = VT_BSTR;
1858 V_BSTR(&v) = SysAllocString(str);
1859 if(!V_BSTR(&v))
1860 return E_OUTOFMEMORY;
1862 return stack_push(ctx, &v);
1865 /* ECMA-262 3rd Edition 7.8 */
1866 static HRESULT interp_regexp(exec_ctx_t *ctx)
1868 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1869 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1870 jsdisp_t *regexp;
1871 VARIANT v;
1872 HRESULT hres;
1874 TRACE("%s %x\n", debugstr_w(source), flags);
1876 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, &regexp);
1877 if(FAILED(hres))
1878 return hres;
1880 var_set_jsdisp(&v, regexp);
1881 return stack_push(ctx, &v);
1884 /* ECMA-262 3rd Edition 11.1.4 */
1885 static HRESULT interp_carray(exec_ctx_t *ctx)
1887 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1888 jsdisp_t *array;
1889 VARIANT *v, r;
1890 unsigned i;
1891 HRESULT hres;
1893 TRACE("%u\n", arg);
1895 hres = create_array(ctx->parser->script, arg, &array);
1896 if(FAILED(hres))
1897 return hres;
1899 i = arg;
1900 while(i--) {
1901 v = stack_pop(ctx);
1902 hres = jsdisp_propput_idx(array, i, v, &ctx->ei, NULL/*FIXME*/);
1903 VariantClear(v);
1904 if(FAILED(hres)) {
1905 jsdisp_release(array);
1906 return hres;
1910 var_set_jsdisp(&r, array);
1911 return stack_push(ctx, &r);
1914 /* ECMA-262 3rd Edition 11.1.5 */
1915 HRESULT interp_new_obj(exec_ctx_t *ctx)
1917 jsdisp_t *obj;
1918 VARIANT v;
1919 HRESULT hres;
1921 TRACE("\n");
1923 hres = create_object(ctx->parser->script, NULL, &obj);
1924 if(FAILED(hres))
1925 return hres;
1927 var_set_jsdisp(&v, obj);
1928 return stack_push(ctx, &v);
1931 /* ECMA-262 3rd Edition 11.1.5 */
1932 HRESULT interp_obj_prop(exec_ctx_t *ctx)
1934 const BSTR name = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1935 jsdisp_t *obj;
1936 VARIANT *v;
1937 HRESULT hres;
1939 TRACE("%s\n", debugstr_w(name));
1941 v = stack_pop(ctx);
1943 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1944 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1946 hres = jsdisp_propput_name(obj, name, v, &ctx->ei, NULL/*FIXME*/);
1947 VariantClear(v);
1948 return hres;
1951 /* ECMA-262 3rd Edition 11.11 */
1952 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
1954 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1955 VARIANT_BOOL b;
1956 HRESULT hres;
1958 TRACE("\n");
1960 hres = to_boolean(stack_top(ctx), &b);
1961 if(FAILED(hres))
1962 return hres;
1964 if(b) {
1965 ctx->ip = arg;
1966 }else {
1967 stack_popn(ctx, 1);
1968 ctx->ip++;
1970 return S_OK;
1973 /* ECMA-262 3rd Edition 11.11 */
1974 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
1976 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1977 VARIANT_BOOL b;
1978 HRESULT hres;
1980 TRACE("\n");
1982 hres = to_boolean(stack_top(ctx), &b);
1983 if(FAILED(hres))
1984 return hres;
1986 if(b) {
1987 stack_popn(ctx, 1);
1988 ctx->ip++;
1989 }else {
1990 ctx->ip = arg;
1992 return S_OK;
1995 /* ECMA-262 3rd Edition 11.10 */
1996 static HRESULT interp_or(exec_ctx_t *ctx)
1998 INT l, r;
1999 HRESULT hres;
2001 TRACE("\n");
2003 hres = stack_pop_int(ctx, &r);
2004 if(FAILED(hres))
2005 return hres;
2007 hres = stack_pop_int(ctx, &l);
2008 if(FAILED(hres))
2009 return hres;
2011 return stack_push_int(ctx, l|r);
2014 /* ECMA-262 3rd Edition 11.10 */
2015 static HRESULT interp_xor(exec_ctx_t *ctx)
2017 INT l, r;
2018 HRESULT hres;
2020 TRACE("\n");
2022 hres = stack_pop_int(ctx, &r);
2023 if(FAILED(hres))
2024 return hres;
2026 hres = stack_pop_int(ctx, &l);
2027 if(FAILED(hres))
2028 return hres;
2030 return stack_push_int(ctx, l^r);
2033 /* ECMA-262 3rd Edition 11.10 */
2034 static HRESULT interp_and(exec_ctx_t *ctx)
2036 INT l, r;
2037 HRESULT hres;
2039 TRACE("\n");
2041 hres = stack_pop_int(ctx, &r);
2042 if(FAILED(hres))
2043 return hres;
2045 hres = stack_pop_int(ctx, &l);
2046 if(FAILED(hres))
2047 return hres;
2049 return stack_push_int(ctx, l&r);
2052 /* ECMA-262 3rd Edition 11.8.6 */
2053 static HRESULT interp_instanceof(exec_ctx_t *ctx)
2055 jsdisp_t *obj, *iter, *tmp = NULL;
2056 VARIANT prot, *v;
2057 BOOL ret = FALSE;
2058 HRESULT hres;
2060 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2062 v = stack_pop(ctx);
2063 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
2064 VariantClear(v);
2065 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
2068 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
2069 IDispatch_Release(V_DISPATCH(v));
2070 if(!obj) {
2071 FIXME("non-jsdisp objects not supported\n");
2072 return E_FAIL;
2075 if(is_class(obj, JSCLASS_FUNCTION)) {
2076 hres = jsdisp_propget_name(obj, prototypeW, &prot, &ctx->ei, NULL/*FIXME*/);
2077 }else {
2078 hres = throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
2080 jsdisp_release(obj);
2081 if(FAILED(hres))
2082 return hres;
2084 v = stack_pop(ctx);
2086 if(V_VT(&prot) == VT_DISPATCH) {
2087 if(V_VT(v) == VT_DISPATCH)
2088 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
2089 for(iter = tmp; !ret && iter; iter = iter->prototype) {
2090 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
2091 if(FAILED(hres))
2092 break;
2095 if(tmp)
2096 jsdisp_release(tmp);
2097 }else {
2098 FIXME("prototype is not an object\n");
2099 hres = E_FAIL;
2102 VariantClear(&prot);
2103 VariantClear(v);
2104 if(FAILED(hres))
2105 return hres;
2107 return stack_push_bool(ctx, ret);
2110 /* ECMA-262 3rd Edition 11.8.7 */
2111 static HRESULT interp_in(exec_ctx_t *ctx)
2113 VARIANT *obj, *v;
2114 DISPID id = 0;
2115 BOOL ret;
2116 BSTR str;
2117 HRESULT hres;
2119 TRACE("\n");
2121 obj = stack_pop(ctx);
2122 v = stack_pop(ctx);
2124 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2125 VariantClear(obj);
2126 VariantClear(v);
2127 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2130 hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2131 VariantClear(v);
2132 if(FAILED(hres)) {
2133 IDispatch_Release(V_DISPATCH(obj));
2134 return hres;
2137 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2138 IDispatch_Release(V_DISPATCH(obj));
2139 SysFreeString(str);
2140 if(SUCCEEDED(hres))
2141 ret = TRUE;
2142 else if(hres == DISP_E_UNKNOWNNAME)
2143 ret = FALSE;
2144 else
2145 return hres;
2147 return stack_push_bool(ctx, ret);
2150 /* ECMA-262 3rd Edition 11.6.1 */
2151 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2153 VARIANT r, l;
2154 HRESULT hres;
2156 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2157 if(FAILED(hres))
2158 return hres;
2160 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2161 if(FAILED(hres)) {
2162 VariantClear(&l);
2163 return hres;
2166 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2167 BSTR lstr = NULL, rstr = NULL;
2169 if(V_VT(&l) == VT_BSTR)
2170 lstr = V_BSTR(&l);
2171 else
2172 hres = to_string(ctx, &l, ei, &lstr);
2174 if(SUCCEEDED(hres)) {
2175 if(V_VT(&r) == VT_BSTR)
2176 rstr = V_BSTR(&r);
2177 else
2178 hres = to_string(ctx, &r, ei, &rstr);
2181 if(SUCCEEDED(hres)) {
2182 int len1, len2;
2184 len1 = SysStringLen(lstr);
2185 len2 = SysStringLen(rstr);
2187 V_VT(retv) = VT_BSTR;
2188 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2189 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2190 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2193 if(V_VT(&l) != VT_BSTR)
2194 SysFreeString(lstr);
2195 if(V_VT(&r) != VT_BSTR)
2196 SysFreeString(rstr);
2197 }else {
2198 VARIANT nl, nr;
2200 hres = to_number(ctx, &l, ei, &nl);
2201 if(SUCCEEDED(hres)) {
2202 hres = to_number(ctx, &r, ei, &nr);
2203 if(SUCCEEDED(hres))
2204 num_set_val(retv, num_val(&nl) + num_val(&nr));
2208 VariantClear(&r);
2209 VariantClear(&l);
2210 return hres;
2213 /* ECMA-262 3rd Edition 11.6.1 */
2214 static HRESULT interp_add(exec_ctx_t *ctx)
2216 VARIANT *l, *r, ret;
2217 HRESULT hres;
2219 r = stack_pop(ctx);
2220 l = stack_pop(ctx);
2222 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2224 hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2225 VariantClear(l);
2226 VariantClear(r);
2227 if(FAILED(hres))
2228 return hres;
2230 return stack_push(ctx, &ret);
2233 /* ECMA-262 3rd Edition 11.6.2 */
2234 static HRESULT interp_sub(exec_ctx_t *ctx)
2236 VARIANT l, r;
2237 HRESULT hres;
2239 TRACE("\n");
2241 hres = stack_pop_number(ctx, &r);
2242 if(FAILED(hres))
2243 return hres;
2245 hres = stack_pop_number(ctx, &l);
2246 if(FAILED(hres))
2247 return hres;
2249 return stack_push_number(ctx, num_val(&l)-num_val(&r));
2252 /* ECMA-262 3rd Edition 11.5.1 */
2253 static HRESULT interp_mul(exec_ctx_t *ctx)
2255 VARIANT l, r;
2256 HRESULT hres;
2258 TRACE("\n");
2260 hres = stack_pop_number(ctx, &r);
2261 if(FAILED(hres))
2262 return hres;
2264 hres = stack_pop_number(ctx, &l);
2265 if(FAILED(hres))
2266 return hres;
2268 return stack_push_number(ctx, num_val(&l)*num_val(&r));
2271 /* ECMA-262 3rd Edition 11.5.2 */
2272 static HRESULT interp_div(exec_ctx_t *ctx)
2274 VARIANT l, r;
2275 HRESULT hres;
2277 TRACE("\n");
2279 hres = stack_pop_number(ctx, &r);
2280 if(FAILED(hres))
2281 return hres;
2283 hres = stack_pop_number(ctx, &l);
2284 if(FAILED(hres))
2285 return hres;
2287 return stack_push_number(ctx, num_val(&l)/num_val(&r));
2290 /* ECMA-262 3rd Edition 11.5.3 */
2291 static HRESULT interp_mod(exec_ctx_t *ctx)
2293 VARIANT l, r;
2294 HRESULT hres;
2296 TRACE("\n");
2298 hres = stack_pop_number(ctx, &r);
2299 if(FAILED(hres))
2300 return hres;
2302 hres = stack_pop_number(ctx, &l);
2303 if(FAILED(hres))
2304 return hres;
2306 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2309 /* ECMA-262 3rd Edition 11.4.2 */
2310 static HRESULT interp_delete(exec_ctx_t *ctx)
2312 VARIANT *obj_var, *name_var;
2313 IDispatchEx *dispex;
2314 IDispatch *obj;
2315 BSTR name;
2316 BOOL ret;
2317 HRESULT hres;
2319 TRACE("\n");
2321 name_var = stack_pop(ctx);
2322 obj_var = stack_pop(ctx);
2324 hres = to_object(ctx->parser->script, obj_var, &obj);
2325 VariantClear(obj_var);
2326 if(FAILED(hres)) {
2327 VariantClear(name_var);
2328 return hres;
2331 hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2332 VariantClear(name_var);
2333 if(FAILED(hres)) {
2334 IDispatch_Release(obj);
2335 return hres;
2338 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2339 if(SUCCEEDED(hres)) {
2340 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2341 ret = TRUE;
2342 IDispatchEx_Release(dispex);
2343 }else {
2344 hres = S_OK;
2345 ret = FALSE;
2348 IDispatch_Release(obj);
2349 SysFreeString(name);
2350 if(FAILED(hres))
2351 return hres;
2353 return stack_push_bool(ctx, ret);
2356 /* ECMA-262 3rd Edition 11.4.2 */
2357 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
2359 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2360 IDispatchEx *dispex;
2361 exprval_t exprval;
2362 BOOL ret = FALSE;
2363 HRESULT hres;
2365 TRACE("%s\n", debugstr_w(arg));
2367 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2368 if(FAILED(hres))
2369 return hres;
2371 if(exprval.type != EXPRVAL_IDREF) {
2372 FIXME("Unsupported exprval\n");
2373 exprval_release(&exprval);
2374 return E_NOTIMPL;
2377 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2378 IDispatch_Release(exprval.u.idref.disp);
2379 if(SUCCEEDED(hres)) {
2380 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2381 IDispatchEx_Release(dispex);
2382 if(FAILED(hres))
2383 return hres;
2385 ret = TRUE;
2388 return stack_push_bool(ctx, ret);
2391 /* ECMA-262 3rd Edition 11.4.2 */
2392 static HRESULT interp_void(exec_ctx_t *ctx)
2394 VARIANT v;
2396 TRACE("\n");
2398 stack_popn(ctx, 1);
2400 V_VT(&v) = VT_EMPTY;
2401 return stack_push(ctx, &v);
2404 /* ECMA-262 3rd Edition 11.4.3 */
2405 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
2407 switch(V_VT(v)) {
2408 case VT_EMPTY:
2409 *ret = undefinedW;
2410 break;
2411 case VT_NULL:
2412 *ret = objectW;
2413 break;
2414 case VT_BOOL:
2415 *ret = booleanW;
2416 break;
2417 case VT_I4:
2418 case VT_R8:
2419 *ret = numberW;
2420 break;
2421 case VT_BSTR:
2422 *ret = stringW;
2423 break;
2424 case VT_DISPATCH: {
2425 jsdisp_t *dispex;
2427 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
2428 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2429 jsdisp_release(dispex);
2430 }else {
2431 *ret = objectW;
2433 break;
2435 default:
2436 FIXME("unhandled vt %d\n", V_VT(v));
2437 return E_NOTIMPL;
2440 return S_OK;
2443 /* ECMA-262 3rd Edition 11.4.3 */
2444 static HRESULT interp_typeofid(exec_ctx_t *ctx)
2446 const WCHAR *ret;
2447 IDispatch *obj;
2448 VARIANT v;
2449 DISPID id;
2450 HRESULT hres;
2452 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2454 TRACE("\n");
2456 obj = stack_pop_objid(ctx, &id);
2457 if(!obj)
2458 return stack_push_string(ctx, undefinedW);
2460 V_VT(&v) = VT_EMPTY;
2461 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2462 IDispatch_Release(obj);
2463 if(FAILED(hres))
2464 return stack_push_string(ctx, unknownW);
2466 hres = typeof_string(&v, &ret);
2467 VariantClear(&v);
2468 if(FAILED(hres))
2469 return hres;
2471 return stack_push_string(ctx, ret);
2474 /* ECMA-262 3rd Edition 11.4.3 */
2475 static HRESULT interp_typeofident(exec_ctx_t *ctx)
2477 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2478 exprval_t exprval;
2479 const WCHAR *ret;
2480 VARIANT v;
2481 HRESULT hres;
2483 TRACE("%s\n", debugstr_w(arg));
2485 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2486 if(FAILED(hres))
2487 return hres;
2489 if(exprval.type == EXPRVAL_INVALID) {
2490 hres = stack_push_string(ctx, undefinedW);
2491 exprval_release(&exprval);
2492 return hres;
2495 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
2496 exprval_release(&exprval);
2497 if(FAILED(hres))
2498 return hres;
2500 hres = typeof_string(&v, &ret);
2501 VariantClear(&v);
2502 if(FAILED(hres))
2503 return hres;
2505 return stack_push_string(ctx, ret);
2508 /* ECMA-262 3rd Edition 11.4.3 */
2509 static HRESULT interp_typeof(exec_ctx_t *ctx)
2511 const WCHAR *ret;
2512 VARIANT *v;
2513 HRESULT hres;
2515 TRACE("\n");
2517 v = stack_pop(ctx);
2518 hres = typeof_string(v, &ret);
2519 VariantClear(v);
2520 if(FAILED(hres))
2521 return hres;
2523 return stack_push_string(ctx, ret);
2526 /* ECMA-262 3rd Edition 11.4.7 */
2527 static HRESULT interp_minus(exec_ctx_t *ctx)
2529 VARIANT n;
2530 HRESULT hres;
2532 TRACE("\n");
2534 hres = stack_pop_number(ctx, &n);
2535 if(FAILED(hres))
2536 return hres;
2538 return stack_push_number(ctx, -num_val(&n));
2541 /* ECMA-262 3rd Edition 11.4.6 */
2542 static HRESULT interp_tonum(exec_ctx_t *ctx)
2544 VARIANT *v, num;
2545 HRESULT hres;
2547 TRACE("\n");
2549 v = stack_pop(ctx);
2550 hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2551 VariantClear(v);
2552 if(FAILED(hres))
2553 return hres;
2555 return stack_push(ctx, &num);
2558 /* ECMA-262 3rd Edition 11.3.1 */
2559 static HRESULT interp_postinc(exec_ctx_t *ctx)
2561 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2562 IDispatch *obj;
2563 DISPID id;
2564 VARIANT v;
2565 HRESULT hres;
2567 TRACE("%d\n", arg);
2569 obj = stack_pop_objid(ctx, &id);
2570 if(!obj)
2571 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2573 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2574 if(SUCCEEDED(hres)) {
2575 VARIANT n, inc;
2577 hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2578 if(SUCCEEDED(hres)) {
2579 num_set_val(&inc, num_val(&n)+(double)arg);
2580 hres = disp_propput(ctx->parser->script, obj, id, &inc, &ctx->ei, NULL/*FIXME*/);
2582 if(FAILED(hres))
2583 VariantClear(&v);
2585 IDispatch_Release(obj);
2586 if(FAILED(hres))
2587 return hres;
2589 return stack_push(ctx, &v);
2592 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2593 static HRESULT interp_preinc(exec_ctx_t *ctx)
2595 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2596 IDispatch *obj;
2597 DISPID id;
2598 VARIANT v;
2599 HRESULT hres;
2601 TRACE("%d\n", arg);
2603 obj = stack_pop_objid(ctx, &id);
2604 if(!obj)
2605 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2607 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2608 if(SUCCEEDED(hres)) {
2609 VARIANT n;
2611 hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2612 VariantClear(&v);
2613 if(SUCCEEDED(hres)) {
2614 num_set_val(&v, num_val(&n)+(double)arg);
2615 hres = disp_propput(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2618 IDispatch_Release(obj);
2619 if(FAILED(hres))
2620 return hres;
2622 return stack_push(ctx, &v);
2625 /* ECMA-262 3rd Edition 11.9.3 */
2626 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2628 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2629 return equal2_values(lval, rval, ret);
2631 /* FIXME: NULL disps should be handled in more general way */
2632 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2633 VARIANT v;
2634 V_VT(&v) = VT_NULL;
2635 return equal_values(ctx, &v, rval, ei, ret);
2638 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2639 VARIANT v;
2640 V_VT(&v) = VT_NULL;
2641 return equal_values(ctx, lval, &v, ei, ret);
2644 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2645 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2646 *ret = TRUE;
2647 return S_OK;
2650 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2651 VARIANT v;
2652 HRESULT hres;
2654 hres = to_number(ctx, lval, ei, &v);
2655 if(FAILED(hres))
2656 return hres;
2658 return equal_values(ctx, &v, rval, ei, ret);
2661 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2662 VARIANT v;
2663 HRESULT hres;
2665 hres = to_number(ctx, rval, ei, &v);
2666 if(FAILED(hres))
2667 return hres;
2669 return equal_values(ctx, lval, &v, ei, ret);
2672 if(V_VT(rval) == VT_BOOL) {
2673 VARIANT v;
2675 V_VT(&v) = VT_I4;
2676 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2677 return equal_values(ctx, lval, &v, ei, ret);
2680 if(V_VT(lval) == VT_BOOL) {
2681 VARIANT v;
2683 V_VT(&v) = VT_I4;
2684 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2685 return equal_values(ctx, &v, rval, ei, ret);
2689 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2690 VARIANT v;
2691 HRESULT hres;
2693 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2694 if(FAILED(hres))
2695 return hres;
2697 hres = equal_values(ctx, lval, &v, ei, ret);
2699 VariantClear(&v);
2700 return hres;
2704 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2705 VARIANT v;
2706 HRESULT hres;
2708 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2709 if(FAILED(hres))
2710 return hres;
2712 hres = equal_values(ctx, &v, rval, ei, ret);
2714 VariantClear(&v);
2715 return hres;
2719 *ret = FALSE;
2720 return S_OK;
2723 /* ECMA-262 3rd Edition 11.9.1 */
2724 static HRESULT interp_eq(exec_ctx_t *ctx)
2726 VARIANT *l, *r;
2727 BOOL b;
2728 HRESULT hres;
2730 r = stack_pop(ctx);
2731 l = stack_pop(ctx);
2733 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2735 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2736 VariantClear(l);
2737 VariantClear(r);
2738 if(FAILED(hres))
2739 return hres;
2741 return stack_push_bool(ctx, b);
2744 /* ECMA-262 3rd Edition 11.9.2 */
2745 static HRESULT interp_neq(exec_ctx_t *ctx)
2747 VARIANT *l, *r;
2748 BOOL b;
2749 HRESULT hres;
2751 r = stack_pop(ctx);
2752 l = stack_pop(ctx);
2754 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2756 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2757 VariantClear(l);
2758 VariantClear(r);
2759 if(FAILED(hres))
2760 return hres;
2762 return stack_push_bool(ctx, !b);
2765 /* ECMA-262 3rd Edition 11.9.4 */
2766 static HRESULT interp_eq2(exec_ctx_t *ctx)
2768 VARIANT *l, *r;
2769 BOOL b;
2770 HRESULT hres;
2772 TRACE("\n");
2774 r = stack_pop(ctx);
2775 l = stack_pop(ctx);
2777 hres = equal2_values(r, l, &b);
2778 VariantClear(l);
2779 VariantClear(r);
2780 if(FAILED(hres))
2781 return hres;
2783 return stack_push_bool(ctx, b);
2786 /* ECMA-262 3rd Edition 11.9.5 */
2787 static HRESULT interp_neq2(exec_ctx_t *ctx)
2789 VARIANT *l, *r;
2790 BOOL b;
2791 HRESULT hres;
2793 TRACE("\n");
2795 r = stack_pop(ctx);
2796 l = stack_pop(ctx);
2798 hres = equal2_values(r, l, &b);
2799 VariantClear(l);
2800 VariantClear(r);
2801 if(FAILED(hres))
2802 return hres;
2804 return stack_push_bool(ctx, !b);
2807 /* ECMA-262 3rd Edition 11.8.5 */
2808 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2810 VARIANT l, r, ln, rn;
2811 HRESULT hres;
2813 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2814 if(FAILED(hres))
2815 return hres;
2817 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2818 if(FAILED(hres)) {
2819 VariantClear(&l);
2820 return hres;
2823 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2824 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2825 SysFreeString(V_BSTR(&l));
2826 SysFreeString(V_BSTR(&r));
2827 return S_OK;
2830 hres = to_number(ctx, &l, ei, &ln);
2831 VariantClear(&l);
2832 if(SUCCEEDED(hres))
2833 hres = to_number(ctx, &r, ei, &rn);
2834 VariantClear(&r);
2835 if(FAILED(hres))
2836 return hres;
2838 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2839 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2840 }else {
2841 DOUBLE ld = num_val(&ln);
2842 DOUBLE rd = num_val(&rn);
2844 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2847 return S_OK;
2850 /* ECMA-262 3rd Edition 11.8.1 */
2851 static HRESULT interp_lt(exec_ctx_t *ctx)
2853 VARIANT *l, *r;
2854 BOOL b;
2855 HRESULT hres;
2857 r = stack_pop(ctx);
2858 l = stack_pop(ctx);
2860 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2862 hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
2863 VariantClear(l);
2864 VariantClear(r);
2865 if(FAILED(hres))
2866 return hres;
2868 return stack_push_bool(ctx, b);
2871 /* ECMA-262 3rd Edition 11.8.1 */
2872 static HRESULT interp_lteq(exec_ctx_t *ctx)
2874 VARIANT *l, *r;
2875 BOOL b;
2876 HRESULT hres;
2878 r = stack_pop(ctx);
2879 l = stack_pop(ctx);
2881 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2883 hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
2884 VariantClear(l);
2885 VariantClear(r);
2886 if(FAILED(hres))
2887 return hres;
2889 return stack_push_bool(ctx, b);
2892 /* ECMA-262 3rd Edition 11.8.2 */
2893 static HRESULT interp_gt(exec_ctx_t *ctx)
2895 VARIANT *l, *r;
2896 BOOL b;
2897 HRESULT hres;
2899 r = stack_pop(ctx);
2900 l = stack_pop(ctx);
2902 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2904 hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
2905 VariantClear(l);
2906 VariantClear(r);
2907 if(FAILED(hres))
2908 return hres;
2910 return stack_push_bool(ctx, b);
2913 /* ECMA-262 3rd Edition 11.8.4 */
2914 static HRESULT interp_gteq(exec_ctx_t *ctx)
2916 VARIANT *l, *r;
2917 BOOL b;
2918 HRESULT hres;
2920 r = stack_pop(ctx);
2921 l = stack_pop(ctx);
2923 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2925 hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
2926 VariantClear(l);
2927 VariantClear(r);
2928 if(FAILED(hres))
2929 return hres;
2931 return stack_push_bool(ctx, b);
2934 /* ECMA-262 3rd Edition 11.4.8 */
2935 static HRESULT interp_bneg(exec_ctx_t *ctx)
2937 VARIANT *v, r;
2938 INT i;
2939 HRESULT hres;
2941 TRACE("\n");
2943 v = stack_pop(ctx);
2944 hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
2945 VariantClear(v);
2946 if(FAILED(hres))
2947 return hres;
2949 V_VT(&r) = VT_I4;
2950 V_I4(&r) = ~i;
2951 return stack_push(ctx, &r);
2954 /* ECMA-262 3rd Edition 11.4.9 */
2955 static HRESULT interp_neg(exec_ctx_t *ctx)
2957 VARIANT *v;
2958 VARIANT_BOOL b;
2959 HRESULT hres;
2961 TRACE("\n");
2963 v = stack_pop(ctx);
2964 hres = to_boolean(v, &b);
2965 VariantClear(v);
2966 if(FAILED(hres))
2967 return hres;
2969 return stack_push_bool(ctx, !b);
2972 /* ECMA-262 3rd Edition 11.7.1 */
2973 static HRESULT interp_lshift(exec_ctx_t *ctx)
2975 DWORD r;
2976 INT l;
2977 HRESULT hres;
2979 hres = stack_pop_uint(ctx, &r);
2980 if(FAILED(hres))
2981 return hres;
2983 hres = stack_pop_int(ctx, &l);
2984 if(FAILED(hres))
2985 return hres;
2987 return stack_push_int(ctx, l << (r&0x1f));
2990 /* ECMA-262 3rd Edition 11.7.2 */
2991 static HRESULT interp_rshift(exec_ctx_t *ctx)
2993 DWORD r;
2994 INT l;
2995 HRESULT hres;
2997 hres = stack_pop_uint(ctx, &r);
2998 if(FAILED(hres))
2999 return hres;
3001 hres = stack_pop_int(ctx, &l);
3002 if(FAILED(hres))
3003 return hres;
3005 return stack_push_int(ctx, l >> (r&0x1f));
3008 /* ECMA-262 3rd Edition 11.7.3 */
3009 static HRESULT interp_rshift2(exec_ctx_t *ctx)
3011 DWORD r, l;
3012 HRESULT hres;
3014 hres = stack_pop_uint(ctx, &r);
3015 if(FAILED(hres))
3016 return hres;
3018 hres = stack_pop_uint(ctx, &l);
3019 if(FAILED(hres))
3020 return hres;
3022 return stack_push_int(ctx, l >> (r&0x1f));
3025 /* ECMA-262 3rd Edition 11.13.1 */
3026 static HRESULT interp_assign(exec_ctx_t *ctx)
3028 IDispatch *disp;
3029 DISPID id;
3030 VARIANT *v;
3031 HRESULT hres;
3033 TRACE("\n");
3035 v = stack_pop(ctx);
3036 disp = stack_pop_objid(ctx, &id);
3038 if(!disp)
3039 return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
3041 hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
3042 IDispatch_Release(disp);
3043 if(FAILED(hres)) {
3044 VariantClear(v);
3045 return hres;
3048 return stack_push(ctx, v);
3051 static HRESULT interp_undefined(exec_ctx_t *ctx)
3053 VARIANT v;
3055 TRACE("\n");
3057 V_VT(&v) = VT_EMPTY;
3058 return stack_push(ctx, &v);
3061 static HRESULT interp_jmp(exec_ctx_t *ctx)
3063 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3065 TRACE("\n");
3067 ctx->ip = arg;
3068 return S_OK;
3071 static HRESULT interp_pop(exec_ctx_t *ctx)
3073 TRACE("\n");
3075 stack_popn(ctx, 1);
3076 return S_OK;
3079 static HRESULT interp_ret(exec_ctx_t *ctx)
3081 TRACE("\n");
3083 ctx->ip = -1;
3084 return S_OK;
3087 static HRESULT interp_tree(exec_ctx_t *ctx)
3089 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3090 exprval_t val;
3091 VARIANT v;
3092 HRESULT hres;
3094 TRACE("\n");
3096 hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3097 if(FAILED(hres))
3098 return hres;
3100 hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3101 exprval_release(&val);
3102 if(FAILED(hres))
3103 return hres;
3105 return stack_push(ctx, &v);
3108 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3110 static const op_func_t op_funcs[] = {
3111 #define X(x,a,b,c) interp_##x,
3112 OP_LIST
3113 #undef X
3116 static const unsigned op_move[] = {
3117 #define X(a,x,b,c) x,
3118 OP_LIST
3119 #undef X
3122 static HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3124 exec_ctx_t *exec_ctx = ctx->exec_ctx;
3125 unsigned prev_ip, prev_top;
3126 jsop_t op;
3127 HRESULT hres = S_OK;
3129 TRACE("\n");
3131 if(expr->instr_off == -1) {
3132 hres = compile_subscript(ctx->exec_ctx->parser, expr, !(flags & EXPR_NOVAL), &expr->instr_off);
3133 if(FAILED(hres))
3134 return hres;
3137 prev_top = exec_ctx->top;
3138 prev_ip = exec_ctx->ip;
3139 exec_ctx->ip = expr->instr_off;
3141 while(exec_ctx->ip != -1) {
3142 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3143 hres = op_funcs[op](exec_ctx);
3144 if(FAILED(hres))
3145 break;
3146 exec_ctx->ip += op_move[op];
3149 exec_ctx->ip = prev_ip;
3151 if(FAILED(hres)) {
3152 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3153 *ei = exec_ctx->ei;
3154 memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3155 return hres;
3158 assert(exec_ctx->top == prev_top+1 || ((flags&EXPR_NOVAL) && exec_ctx->top == prev_top));
3160 ret->type = EXPRVAL_VARIANT;
3161 if(exec_ctx->top == prev_top)
3162 V_VT(&ret->u.var) = VT_EMPTY;
3163 else
3164 ret->u.var = *stack_pop(exec_ctx);
3165 return S_OK;