jscript: Call expressions needling EXPR_NEWREF flag directly.
[wine/multimedia.git] / dlls / jscript / engine.c
blobc7ed6d7dcb0fcf4fe6cc48b4291139e113a95548
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
35 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
36 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
37 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
38 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
39 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
40 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
41 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
43 struct _return_type_t {
44 enum{
45 RT_NORMAL,
46 RT_RETURN,
47 RT_BREAK,
48 RT_CONTINUE
49 } type;
50 jsexcept_t ei;
53 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
55 return stat->eval(ctx, stat, rt, ret);
58 static inline HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
60 return expr->eval(ctx, expr, flags, ei, ret);
63 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
65 if(!ctx->stack_size) {
66 ctx->stack = heap_alloc(16*sizeof(VARIANT));
67 if(!ctx->stack)
68 return E_OUTOFMEMORY;
69 ctx->stack_size = 16;
70 }else if(ctx->stack_size == ctx->top) {
71 VARIANT *new_stack;
73 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
74 if(!new_stack) {
75 VariantClear(v);
76 return E_OUTOFMEMORY;
79 ctx->stack = new_stack;
80 ctx->stack_size *= 2;
83 ctx->stack[ctx->top++] = *v;
84 return S_OK;
87 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
89 VARIANT v;
91 V_VT(&v) = VT_BOOL;
92 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
93 return stack_push(ctx, &v);
96 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
98 VARIANT v;
100 num_set_val(&v, number);
101 return stack_push(ctx, &v);
104 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
106 VARIANT v;
108 V_VT(&v) = VT_I4;
109 V_I4(&v) = n;
110 return stack_push(ctx, &v);
113 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
115 VARIANT v;
117 V_VT(&v) = VT_BSTR;
118 V_BSTR(&v) = SysAllocString(str);
119 return V_BSTR(&v) ? stack_push(ctx, &v) : E_OUTOFMEMORY;
122 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
124 VARIANT v;
125 HRESULT hres;
127 V_VT(&v) = VT_DISPATCH;
128 V_DISPATCH(&v) = disp;
129 hres = stack_push(ctx, &v);
130 if(FAILED(hres))
131 return hres;
133 V_VT(&v) = VT_INT;
134 V_INT(&v) = id;
135 return stack_push(ctx, &v);
138 static inline VARIANT *stack_top(exec_ctx_t *ctx)
140 assert(ctx->top);
141 return ctx->stack + ctx->top-1;
144 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
146 assert(ctx->top > n);
147 return ctx->stack + ctx->top-1-n;
150 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
152 assert(ctx->top);
153 return ctx->stack + --ctx->top;
156 static void stack_popn(exec_ctx_t *ctx, unsigned n)
158 while(n--)
159 VariantClear(stack_pop(ctx));
162 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
164 VARIANT *v;
165 HRESULT hres;
167 v = stack_pop(ctx);
168 hres = to_number(ctx->parser->script, v, &ctx->ei, r);
169 VariantClear(v);
170 return hres;
173 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
175 VARIANT *v;
176 HRESULT hres;
178 v = stack_pop(ctx);
179 if(V_VT(v) == VT_DISPATCH) {
180 if(!V_DISPATCH(v))
181 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
182 *r = V_DISPATCH(v);
183 return S_OK;
186 hres = to_object(ctx->parser->script, v, r);
187 VariantClear(v);
188 return hres;
191 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
193 return to_int32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
196 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
198 return to_uint32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
201 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
203 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
205 *id = V_INT(stack_pop(ctx));
206 return V_DISPATCH(stack_pop(ctx));
209 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
211 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
213 *id = V_INT(stack_topn(ctx, n));
214 return V_DISPATCH(stack_topn(ctx, n+1));
217 static void exprval_release(exprval_t *val)
219 switch(val->type) {
220 case EXPRVAL_VARIANT:
221 if(V_VT(&val->u.var) != VT_EMPTY)
222 VariantClear(&val->u.var);
223 return;
224 case EXPRVAL_IDREF:
225 if(val->u.idref.disp)
226 IDispatch_Release(val->u.idref.disp);
227 return;
228 case EXPRVAL_INVALID:
229 SysFreeString(val->u.identifier);
233 /* ECMA-262 3rd Edition 8.7.1 */
234 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
236 V_VT(ret) = VT_EMPTY;
238 switch(val->type) {
239 case EXPRVAL_VARIANT:
240 return VariantCopy(ret, &val->u.var);
241 case EXPRVAL_IDREF:
242 if(!val->u.idref.disp) {
243 FIXME("throw ReferenceError\n");
244 return E_FAIL;
247 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
248 case EXPRVAL_INVALID:
249 return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
252 ERR("type %d\n", val->type);
253 return E_FAIL;
256 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
258 if(val->type == EXPRVAL_VARIANT) {
259 *ret = val->u.var;
260 V_VT(&val->u.var) = VT_EMPTY;
261 return S_OK;
264 return exprval_value(ctx, val, ei, ret);
267 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
269 if(exprval->type != EXPRVAL_VARIANT) {
270 VARIANT val;
271 HRESULT hres;
273 hres = exprval_to_value(ctx, exprval, ei, &val);
274 if(FAILED(hres))
275 return hres;
277 hres = to_boolean(&val, b);
278 VariantClear(&val);
279 return hres;
282 return to_boolean(&exprval->u.var, b);
285 static void exprval_init(exprval_t *val)
287 val->type = EXPRVAL_VARIANT;
288 V_VT(&val->u.var) = VT_EMPTY;
291 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
293 val->type = EXPRVAL_IDREF;
294 val->u.idref.disp = disp;
295 val->u.idref.id = id;
297 if(disp)
298 IDispatch_AddRef(disp);
301 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
303 scope_chain_t *new_scope;
305 new_scope = heap_alloc(sizeof(scope_chain_t));
306 if(!new_scope)
307 return E_OUTOFMEMORY;
309 new_scope->ref = 1;
311 jsdisp_addref(obj);
312 new_scope->obj = obj;
314 if(scope) {
315 scope_addref(scope);
316 new_scope->next = scope;
317 }else {
318 new_scope->next = NULL;
321 *ret = new_scope;
322 return S_OK;
325 static void scope_pop(scope_chain_t **scope)
327 scope_chain_t *tmp;
329 tmp = *scope;
330 *scope = tmp->next;
331 scope_release(tmp);
334 void scope_release(scope_chain_t *scope)
336 if(--scope->ref)
337 return;
339 if(scope->next)
340 scope_release(scope->next);
342 jsdisp_release(scope->obj);
343 heap_free(scope);
346 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
347 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
349 exec_ctx_t *ctx;
351 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
352 if(!ctx)
353 return E_OUTOFMEMORY;
355 ctx->ref = 1;
356 ctx->is_global = is_global;
358 if(this_obj)
359 ctx->this_obj = this_obj;
360 else if(script_ctx->host_global)
361 ctx->this_obj = script_ctx->host_global;
362 else
363 ctx->this_obj = to_disp(script_ctx->global);
364 IDispatch_AddRef(ctx->this_obj);
366 jsdisp_addref(var_disp);
367 ctx->var_disp = var_disp;
369 if(scope) {
370 scope_addref(scope);
371 ctx->scope_chain = scope;
374 *ret = ctx;
375 return S_OK;
378 void exec_release(exec_ctx_t *ctx)
380 if(--ctx->ref)
381 return;
383 if(ctx->scope_chain)
384 scope_release(ctx->scope_chain);
385 if(ctx->var_disp)
386 jsdisp_release(ctx->var_disp);
387 if(ctx->this_obj)
388 IDispatch_Release(ctx->this_obj);
389 heap_free(ctx->stack);
390 heap_free(ctx);
393 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
395 IDispatchEx *dispex;
396 HRESULT hres;
398 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
399 if(FAILED(hres)) {
400 TRACE("unsing IDispatch\n");
402 *id = 0;
403 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
406 *id = 0;
407 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
408 IDispatchEx_Release(dispex);
409 return hres;
412 /* ECMA-262 3rd Edition 8.7.2 */
413 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
415 if(ref->type != EXPRVAL_IDREF)
416 return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
418 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
421 static inline BOOL is_null(const VARIANT *v)
423 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
426 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
428 IObjectIdentity *identity;
429 IUnknown *unk1, *unk2;
430 HRESULT hres;
432 if(disp1 == disp2) {
433 *ret = TRUE;
434 return S_OK;
437 if(!disp1 || !disp2) {
438 *ret = FALSE;
439 return S_OK;
442 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
443 if(FAILED(hres))
444 return hres;
446 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
447 if(FAILED(hres)) {
448 IUnknown_Release(unk1);
449 return hres;
452 if(unk1 == unk2) {
453 *ret = TRUE;
454 }else {
455 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
456 if(SUCCEEDED(hres)) {
457 hres = IObjectIdentity_IsEqualObject(identity, unk2);
458 IObjectIdentity_Release(identity);
459 *ret = hres == S_OK;
460 }else {
461 *ret = FALSE;
465 IUnknown_Release(unk1);
466 IUnknown_Release(unk2);
467 return S_OK;
470 /* ECMA-262 3rd Edition 11.9.6 */
471 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
473 TRACE("\n");
475 if(V_VT(lval) != V_VT(rval)) {
476 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
477 *ret = num_val(lval) == num_val(rval);
478 else if(is_null(lval))
479 *ret = is_null(rval);
480 else
481 *ret = FALSE;
482 return S_OK;
485 switch(V_VT(lval)) {
486 case VT_EMPTY:
487 case VT_NULL:
488 *ret = VARIANT_TRUE;
489 break;
490 case VT_I4:
491 *ret = V_I4(lval) == V_I4(rval);
492 break;
493 case VT_R8:
494 *ret = V_R8(lval) == V_R8(rval);
495 break;
496 case VT_BSTR:
497 if(!V_BSTR(lval))
498 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
499 else if(!V_BSTR(rval))
500 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
501 else
502 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
503 break;
504 case VT_DISPATCH:
505 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
506 case VT_BOOL:
507 *ret = !V_BOOL(lval) == !V_BOOL(rval);
508 break;
509 default:
510 FIXME("unimplemented vt %d\n", V_VT(lval));
511 return E_NOTIMPL;
514 return S_OK;
517 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
519 switch(literal->type) {
520 case LT_NULL:
521 V_VT(v) = VT_NULL;
522 break;
523 case LT_INT:
524 V_VT(v) = VT_I4;
525 V_I4(v) = literal->u.lval;
526 break;
527 case LT_DOUBLE:
528 V_VT(v) = VT_R8;
529 V_R8(v) = literal->u.dval;
530 break;
531 case LT_STRING: {
532 BSTR str = SysAllocString(literal->u.wstr);
533 if(!str)
534 return E_OUTOFMEMORY;
536 V_VT(v) = VT_BSTR;
537 V_BSTR(v) = str;
538 break;
540 case LT_BOOL:
541 V_VT(v) = VT_BOOL;
542 V_BOOL(v) = literal->u.bval;
543 break;
544 case LT_REGEXP: {
545 jsdisp_t *regexp;
546 HRESULT hres;
548 hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
549 literal->u.regexp.flags, &regexp);
550 if(FAILED(hres))
551 return hres;
553 var_set_jsdisp(v, regexp);
557 return S_OK;
560 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
562 named_item_t *item;
563 DISPID id;
564 HRESULT hres;
566 for(item = ctx->named_items; item; item = item->next) {
567 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
568 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
569 if(SUCCEEDED(hres)) {
570 if(ret)
571 exprval_set_idref(ret, item->disp, id);
572 return TRUE;
577 return FALSE;
580 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
581 jsexcept_t *ei, VARIANT *retv)
583 script_ctx_t *script = parser->script;
584 function_declaration_t *func;
585 parser_ctx_t *prev_parser;
586 var_list_t *var;
587 VARIANT val, tmp;
588 statement_t *stat;
589 exec_ctx_t *prev_ctx;
590 return_type_t rt;
591 HRESULT hres = S_OK;
593 for(func = source->functions; func; func = func->next) {
594 jsdisp_t *func_obj;
595 VARIANT var;
597 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
598 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
599 if(FAILED(hres))
600 return hres;
602 var_set_jsdisp(&var, func_obj);
603 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
604 jsdisp_release(func_obj);
605 if(FAILED(hres))
606 return hres;
609 for(var = source->variables; var; var = var->next) {
610 DISPID id = 0;
611 BSTR name;
613 name = SysAllocString(var->identifier);
614 if(!name)
615 return E_OUTOFMEMORY;
617 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
618 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
619 SysFreeString(name);
620 if(FAILED(hres))
621 return hres;
624 prev_ctx = script->exec_ctx;
625 script->exec_ctx = ctx;
627 prev_parser = ctx->parser;
628 ctx->parser = parser;
630 V_VT(&val) = VT_EMPTY;
631 memset(&rt, 0, sizeof(rt));
632 rt.type = RT_NORMAL;
634 for(stat = source->statement; stat; stat = stat->next) {
635 hres = stat_eval(script, stat, &rt, &tmp);
636 if(FAILED(hres))
637 break;
639 VariantClear(&val);
640 val = tmp;
641 if(rt.type != RT_NORMAL)
642 break;
645 script->exec_ctx = prev_ctx;
646 ctx->parser = prev_parser;
648 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
649 FIXME("wrong rt %d\n", rt.type);
650 hres = E_FAIL;
653 *ei = rt.ei;
654 if(FAILED(hres)) {
655 VariantClear(&val);
656 return hres;
659 if(!retv || (!from_eval && rt.type != RT_RETURN))
660 VariantClear(&val);
661 if(retv)
662 *retv = val;
663 return S_OK;
666 /* ECMA-262 3rd Edition 10.1.4 */
667 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
669 scope_chain_t *scope;
670 named_item_t *item;
671 DISPID id = 0;
672 HRESULT hres;
674 TRACE("%s\n", debugstr_w(identifier));
676 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
677 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
678 if(SUCCEEDED(hres)) {
679 exprval_set_idref(ret, to_disp(scope->obj), id);
680 return S_OK;
684 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
685 if(SUCCEEDED(hres)) {
686 exprval_set_idref(ret, to_disp(ctx->global), id);
687 return S_OK;
690 for(item = ctx->named_items; item; item = item->next) {
691 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
692 if(!item->disp) {
693 IUnknown *unk;
695 if(!ctx->site)
696 break;
698 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
699 SCRIPTINFO_IUNKNOWN, &unk, NULL);
700 if(FAILED(hres)) {
701 WARN("GetItemInfo failed: %08x\n", hres);
702 break;
705 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
706 IUnknown_Release(unk);
707 if(FAILED(hres)) {
708 WARN("object does not implement IDispatch\n");
709 break;
713 ret->type = EXPRVAL_VARIANT;
714 V_VT(&ret->u.var) = VT_DISPATCH;
715 V_DISPATCH(&ret->u.var) = item->disp;
716 IDispatch_AddRef(item->disp);
717 return S_OK;
721 if(lookup_global_members(ctx, identifier, ret))
722 return S_OK;
724 if(flags & EXPR_NEWREF) {
725 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
726 if(FAILED(hres))
727 return hres;
729 exprval_set_idref(ret, to_disp(ctx->global), id);
730 return S_OK;
733 ret->type = EXPRVAL_INVALID;
734 ret->u.identifier = SysAllocString(identifier);
735 if(!ret->u.identifier)
736 return E_OUTOFMEMORY;
738 return S_OK;
741 /* ECMA-262 3rd Edition 12.1 */
742 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
744 block_statement_t *stat = (block_statement_t*)_stat;
745 VARIANT val, tmp;
746 statement_t *iter;
747 HRESULT hres = S_OK;
749 TRACE("\n");
751 V_VT(&val) = VT_EMPTY;
752 for(iter = stat->stat_list; iter; iter = iter->next) {
753 hres = stat_eval(ctx, iter, rt, &tmp);
754 if(FAILED(hres))
755 break;
757 VariantClear(&val);
758 val = tmp;
759 if(rt->type != RT_NORMAL)
760 break;
763 if(FAILED(hres)) {
764 VariantClear(&val);
765 return hres;
768 *ret = val;
769 return S_OK;
772 /* ECMA-262 3rd Edition 12.2 */
773 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
775 variable_declaration_t *iter;
776 HRESULT hres = S_OK;
778 for(iter = var_list; iter; iter = iter->next) {
779 exprval_t exprval;
780 VARIANT val;
782 if(!iter->expr)
783 continue;
785 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
786 if(FAILED(hres))
787 break;
789 hres = exprval_to_value(ctx, &exprval, ei, &val);
790 exprval_release(&exprval);
791 if(FAILED(hres))
792 break;
794 hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
795 VariantClear(&val);
796 if(FAILED(hres))
797 break;
800 return hres;
803 /* ECMA-262 3rd Edition 12.2 */
804 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
806 var_statement_t *stat = (var_statement_t*)_stat;
807 HRESULT hres;
809 TRACE("\n");
811 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
812 if(FAILED(hres))
813 return hres;
815 V_VT(ret) = VT_EMPTY;
816 return S_OK;
819 /* ECMA-262 3rd Edition 12.3 */
820 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
822 TRACE("\n");
824 V_VT(ret) = VT_EMPTY;
825 return S_OK;
828 /* ECMA-262 3rd Edition 12.4 */
829 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
831 expression_statement_t *stat = (expression_statement_t*)_stat;
832 exprval_t exprval;
833 VARIANT val;
834 HRESULT hres;
836 TRACE("\n");
838 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
839 if(FAILED(hres))
840 return hres;
842 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
843 exprval_release(&exprval);
844 if(FAILED(hres))
845 return hres;
847 *ret = val;
848 TRACE("= %s\n", debugstr_variant(ret));
849 return S_OK;
852 /* ECMA-262 3rd Edition 12.5 */
853 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
855 if_statement_t *stat = (if_statement_t*)_stat;
856 exprval_t exprval;
857 VARIANT_BOOL b;
858 HRESULT hres;
860 TRACE("\n");
862 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
863 if(FAILED(hres))
864 return hres;
866 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
867 exprval_release(&exprval);
868 if(FAILED(hres))
869 return hres;
871 if(b)
872 hres = stat_eval(ctx, stat->if_stat, rt, ret);
873 else if(stat->else_stat)
874 hres = stat_eval(ctx, stat->else_stat, rt, ret);
875 else
876 V_VT(ret) = VT_EMPTY;
878 return hres;
881 /* ECMA-262 3rd Edition 12.6.2 */
882 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
884 while_statement_t *stat = (while_statement_t*)_stat;
885 exprval_t exprval;
886 VARIANT val, tmp;
887 VARIANT_BOOL b;
888 BOOL test_expr;
889 HRESULT hres;
891 TRACE("\n");
893 V_VT(&val) = VT_EMPTY;
894 test_expr = !stat->do_while;
896 while(1) {
897 if(test_expr) {
898 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
899 if(FAILED(hres))
900 break;
902 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
903 exprval_release(&exprval);
904 if(FAILED(hres) || !b)
905 break;
906 }else {
907 test_expr = TRUE;
910 hres = stat_eval(ctx, stat->statement, rt, &tmp);
911 if(FAILED(hres))
912 break;
914 VariantClear(&val);
915 val = tmp;
917 if(rt->type == RT_CONTINUE)
918 rt->type = RT_NORMAL;
919 if(rt->type != RT_NORMAL)
920 break;
923 if(FAILED(hres)) {
924 VariantClear(&val);
925 return hres;
928 if(rt->type == RT_BREAK)
929 rt->type = RT_NORMAL;
931 *ret = val;
932 return S_OK;
935 /* ECMA-262 3rd Edition 12.6.3 */
936 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
938 for_statement_t *stat = (for_statement_t*)_stat;
939 VARIANT val, tmp, retv;
940 exprval_t exprval;
941 VARIANT_BOOL b;
942 HRESULT hres;
944 TRACE("\n");
946 if(stat->variable_list) {
947 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
948 if(FAILED(hres))
949 return hres;
950 }else if(stat->begin_expr) {
951 hres = expr_eval(ctx, stat->begin_expr, 0, &rt->ei, &exprval);
952 if(FAILED(hres))
953 return hres;
955 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
956 exprval_release(&exprval);
957 if(FAILED(hres))
958 return hres;
960 VariantClear(&val);
963 V_VT(&retv) = VT_EMPTY;
965 while(1) {
966 if(stat->expr) {
967 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
968 if(FAILED(hres))
969 break;
971 hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
972 exprval_release(&exprval);
973 if(FAILED(hres) || !b)
974 break;
977 hres = stat_eval(ctx, stat->statement, rt, &tmp);
978 if(FAILED(hres))
979 break;
981 VariantClear(&retv);
982 retv = tmp;
984 if(rt->type == RT_CONTINUE)
985 rt->type = RT_NORMAL;
986 else if(rt->type != RT_NORMAL)
987 break;
989 if(stat->end_expr) {
990 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
991 if(FAILED(hres))
992 break;
994 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
995 exprval_release(&exprval);
996 if(FAILED(hres))
997 break;
999 VariantClear(&val);
1003 if(FAILED(hres)) {
1004 VariantClear(&retv);
1005 return hres;
1008 if(rt->type == RT_BREAK)
1009 rt->type = RT_NORMAL;
1011 *ret = retv;
1012 return S_OK;
1015 /* ECMA-262 3rd Edition 12.6.4 */
1016 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1018 forin_statement_t *stat = (forin_statement_t*)_stat;
1019 VARIANT val, name, retv, tmp;
1020 DISPID id = DISPID_STARTENUM;
1021 BSTR str, identifier = NULL;
1022 IDispatchEx *in_obj;
1023 exprval_t exprval;
1024 HRESULT hres;
1026 TRACE("\n");
1028 if(stat->variable) {
1029 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
1030 if(FAILED(hres))
1031 return hres;
1034 hres = expr_eval(ctx, stat->in_expr, 0, &rt->ei, &exprval);
1035 if(FAILED(hres))
1036 return hres;
1038 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1039 exprval_release(&exprval);
1040 if(FAILED(hres))
1041 return hres;
1043 if(V_VT(&val) != VT_DISPATCH) {
1044 TRACE("in vt %d\n", V_VT(&val));
1045 VariantClear(&val);
1046 V_VT(ret) = VT_EMPTY;
1047 return S_OK;
1050 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
1051 IDispatch_Release(V_DISPATCH(&val));
1052 if(FAILED(hres)) {
1053 TRACE("Object doesn't support IDispatchEx\n");
1054 V_VT(ret) = VT_EMPTY;
1055 return S_OK;
1058 V_VT(&retv) = VT_EMPTY;
1060 if(stat->variable)
1061 identifier = SysAllocString(stat->variable->identifier);
1063 while(1) {
1064 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
1065 if(FAILED(hres) || hres == S_FALSE)
1066 break;
1068 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
1069 if(FAILED(hres))
1070 break;
1072 TRACE("iter %s\n", debugstr_w(str));
1074 if(stat->variable) {
1075 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
1076 }else {
1077 switch(stat->expr->type) {
1078 case EXPR_ARRAY:
1079 hres = array_expression_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
1080 break;
1081 case EXPR_IDENT:
1082 hres = identifier_expression_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
1083 break;
1084 case EXPR_MEMBER:
1085 hres = member_expression_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
1086 break;
1087 default:
1088 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1091 if(SUCCEEDED(hres)) {
1092 V_VT(&name) = VT_BSTR;
1093 V_BSTR(&name) = str;
1094 hres = put_value(ctx, &exprval, &name, &rt->ei);
1095 exprval_release(&exprval);
1097 SysFreeString(str);
1098 if(FAILED(hres))
1099 break;
1101 hres = stat_eval(ctx, stat->statement, rt, &tmp);
1102 if(FAILED(hres))
1103 break;
1105 VariantClear(&retv);
1106 retv = tmp;
1108 if(rt->type == RT_CONTINUE)
1109 rt->type = RT_NORMAL;
1110 else if(rt->type != RT_NORMAL)
1111 break;
1114 SysFreeString(identifier);
1115 IDispatchEx_Release(in_obj);
1116 if(FAILED(hres)) {
1117 VariantClear(&retv);
1118 return hres;
1121 if(rt->type == RT_BREAK)
1122 rt->type = RT_NORMAL;
1124 *ret = retv;
1125 return S_OK;
1128 /* ECMA-262 3rd Edition 12.7 */
1129 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1131 branch_statement_t *stat = (branch_statement_t*)_stat;
1133 TRACE("\n");
1135 if(stat->identifier) {
1136 FIXME("indentifier not implemented\n");
1137 return E_NOTIMPL;
1140 rt->type = RT_CONTINUE;
1141 V_VT(ret) = VT_EMPTY;
1142 return S_OK;
1145 /* ECMA-262 3rd Edition 12.8 */
1146 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1148 branch_statement_t *stat = (branch_statement_t*)_stat;
1150 TRACE("\n");
1152 if(stat->identifier) {
1153 FIXME("indentifier not implemented\n");
1154 return E_NOTIMPL;
1157 rt->type = RT_BREAK;
1158 V_VT(ret) = VT_EMPTY;
1159 return S_OK;
1162 /* ECMA-262 3rd Edition 12.9 */
1163 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1165 expression_statement_t *stat = (expression_statement_t*)_stat;
1166 HRESULT hres;
1168 TRACE("\n");
1170 if(stat->expr) {
1171 exprval_t exprval;
1173 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1174 if(FAILED(hres))
1175 return hres;
1177 hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1178 exprval_release(&exprval);
1179 if(FAILED(hres))
1180 return hres;
1181 }else {
1182 V_VT(ret) = VT_EMPTY;
1185 TRACE("= %s\n", debugstr_variant(ret));
1186 rt->type = RT_RETURN;
1187 return S_OK;
1190 /* ECMA-262 3rd Edition 12.10 */
1191 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1193 with_statement_t *stat = (with_statement_t*)_stat;
1194 exprval_t exprval;
1195 IDispatch *disp;
1196 jsdisp_t *obj;
1197 VARIANT val;
1198 HRESULT hres;
1200 TRACE("\n");
1202 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1203 if(FAILED(hres))
1204 return hres;
1206 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1207 exprval_release(&exprval);
1208 if(FAILED(hres))
1209 return hres;
1211 hres = to_object(ctx, &val, &disp);
1212 VariantClear(&val);
1213 if(FAILED(hres))
1214 return hres;
1216 obj = iface_to_jsdisp((IUnknown*)disp);
1217 IDispatch_Release(disp);
1218 if(!obj) {
1219 FIXME("disp id not jsdisp\n");
1220 return E_NOTIMPL;
1223 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1224 jsdisp_release(obj);
1225 if(FAILED(hres))
1226 return hres;
1228 hres = stat_eval(ctx, stat->statement, rt, ret);
1230 scope_pop(&ctx->exec_ctx->scope_chain);
1231 return hres;
1234 /* ECMA-262 3rd Edition 12.12 */
1235 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1237 FIXME("\n");
1238 return E_NOTIMPL;
1241 /* ECMA-262 3rd Edition 12.13 */
1242 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1244 switch_statement_t *stat = (switch_statement_t*)_stat;
1245 case_clausule_t *iter, *default_clausule = NULL;
1246 statement_t *stat_iter;
1247 VARIANT val, cval;
1248 exprval_t exprval;
1249 BOOL b;
1250 HRESULT hres;
1252 TRACE("\n");
1254 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1255 if(FAILED(hres))
1256 return hres;
1258 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1259 exprval_release(&exprval);
1260 if(FAILED(hres))
1261 return hres;
1263 for(iter = stat->case_list; iter; iter = iter->next) {
1264 if(!iter->expr) {
1265 default_clausule = iter;
1266 continue;
1269 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1270 if(FAILED(hres))
1271 break;
1273 hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1274 exprval_release(&exprval);
1275 if(FAILED(hres))
1276 break;
1278 hres = equal2_values(&val, &cval, &b);
1279 VariantClear(&cval);
1280 if(FAILED(hres) || b)
1281 break;
1284 VariantClear(&val);
1285 if(FAILED(hres))
1286 return hres;
1288 if(!iter)
1289 iter = default_clausule;
1291 V_VT(&val) = VT_EMPTY;
1292 if(iter) {
1293 VARIANT tmp;
1295 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1296 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1297 if(FAILED(hres))
1298 break;
1300 VariantClear(&val);
1301 val = tmp;
1303 if(rt->type != RT_NORMAL)
1304 break;
1308 if(FAILED(hres)) {
1309 VariantClear(&val);
1310 return hres;
1313 if(rt->type == RT_BREAK)
1314 rt->type = RT_NORMAL;
1316 *ret = val;
1317 return S_OK;
1320 /* ECMA-262 3rd Edition 12.13 */
1321 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1323 expression_statement_t *stat = (expression_statement_t*)_stat;
1324 exprval_t exprval;
1325 VARIANT val;
1326 HRESULT hres;
1328 TRACE("\n");
1330 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1331 if(FAILED(hres))
1332 return hres;
1334 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1335 exprval_release(&exprval);
1336 if(FAILED(hres))
1337 return hres;
1339 rt->ei.var = val;
1340 return DISP_E_EXCEPTION;
1343 static HRESULT interp_throw(exec_ctx_t *ctx)
1345 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1347 TRACE("%08x\n", arg);
1349 return throw_reference_error(ctx->parser->script, &ctx->ei, arg, NULL);
1352 static HRESULT interp_throw_type(exec_ctx_t *ctx)
1354 const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1355 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
1357 TRACE("%08x %s\n", hres, debugstr_w(str));
1359 return throw_type_error(ctx->parser->script, &ctx->ei, hres, str);
1362 /* ECMA-262 3rd Edition 12.14 */
1363 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1365 jsdisp_t *var_disp;
1366 VARIANT ex, val;
1367 HRESULT hres;
1369 ex = rt->ei.var;
1370 memset(&rt->ei, 0, sizeof(jsexcept_t));
1372 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1373 if(SUCCEEDED(hres)) {
1374 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1375 if(SUCCEEDED(hres)) {
1376 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1377 if(SUCCEEDED(hres)) {
1378 hres = stat_eval(ctx, block->statement, rt, &val);
1379 scope_pop(&ctx->exec_ctx->scope_chain);
1383 jsdisp_release(var_disp);
1386 VariantClear(&ex);
1387 if(FAILED(hres))
1388 return hres;
1390 *ret = val;
1391 return S_OK;
1394 /* ECMA-262 3rd Edition 12.14 */
1395 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1397 try_statement_t *stat = (try_statement_t*)_stat;
1398 VARIANT val;
1399 HRESULT hres;
1401 TRACE("\n");
1403 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1404 if(FAILED(hres)) {
1405 TRACE("EXCEPTION\n");
1406 if(!stat->catch_block)
1407 return hres;
1409 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1410 if(FAILED(hres))
1411 return hres;
1414 if(stat->finally_statement) {
1415 VariantClear(&val);
1416 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1417 if(FAILED(hres))
1418 return hres;
1421 *ret = val;
1422 return S_OK;
1425 /* ECMA-262 3rd Edition 13 */
1426 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1428 function_expression_t *expr = (function_expression_t*)_expr;
1429 VARIANT var;
1430 HRESULT hres;
1432 TRACE("\n");
1434 if(expr->identifier) {
1435 hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1436 if(FAILED(hres))
1437 return hres;
1438 }else {
1439 jsdisp_t *dispex;
1441 hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1442 expr->src_str, expr->src_len, &dispex);
1443 if(FAILED(hres))
1444 return hres;
1446 var_set_jsdisp(&var, dispex);
1449 ret->type = EXPRVAL_VARIANT;
1450 ret->u.var = var;
1451 return S_OK;
1454 /* ECMA-262 3rd Edition 11.2.1 */
1455 HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1457 binary_expression_t *expr = (binary_expression_t*)_expr;
1458 exprval_t exprval;
1459 VARIANT member, val;
1460 DISPID id;
1461 BSTR str;
1462 IDispatch *obj = NULL;
1463 HRESULT hres;
1465 TRACE("\n");
1467 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1468 if(FAILED(hres))
1469 return hres;
1471 hres = exprval_to_value(ctx, &exprval, ei, &member);
1472 exprval_release(&exprval);
1473 if(FAILED(hres))
1474 return hres;
1476 hres = expr_eval(ctx, expr->expression2, EXPR_NEWREF, ei, &exprval);
1477 if(SUCCEEDED(hres)) {
1478 hres = exprval_to_value(ctx, &exprval, ei, &val);
1479 exprval_release(&exprval);
1482 if(SUCCEEDED(hres)) {
1483 hres = to_object(ctx, &member, &obj);
1484 if(FAILED(hres))
1485 VariantClear(&val);
1487 VariantClear(&member);
1488 if(SUCCEEDED(hres)) {
1489 hres = to_string(ctx, &val, ei, &str);
1490 VariantClear(&val);
1491 if(SUCCEEDED(hres)) {
1492 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1493 SysFreeString(str);
1496 if(SUCCEEDED(hres)) {
1497 exprval_set_idref(ret, obj, id);
1498 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1499 exprval_init(ret);
1500 hres = S_OK;
1503 IDispatch_Release(obj);
1506 return hres;
1509 /* ECMA-262 3rd Edition 11.2.1 */
1510 static HRESULT interp_array(exec_ctx_t *ctx)
1512 VARIANT v, *namev;
1513 IDispatch *obj;
1514 DISPID id;
1515 BSTR name;
1516 HRESULT hres;
1518 TRACE("\n");
1520 namev = stack_pop(ctx);
1522 hres = stack_pop_object(ctx, &obj);
1523 if(FAILED(hres)) {
1524 VariantClear(namev);
1525 return hres;
1528 hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1529 VariantClear(namev);
1530 if(FAILED(hres)) {
1531 IDispatch_Release(obj);
1532 return hres;
1535 hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
1536 SysFreeString(name);
1537 if(SUCCEEDED(hres)) {
1538 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1539 }else if(hres == DISP_E_UNKNOWNNAME) {
1540 V_VT(&v) = VT_EMPTY;
1541 hres = S_OK;
1543 IDispatch_Release(obj);
1544 if(FAILED(hres))
1545 return hres;
1547 return stack_push(ctx, &v);
1550 /* ECMA-262 3rd Edition 11.2.1 */
1551 HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1553 member_expression_t *expr = (member_expression_t*)_expr;
1554 IDispatch *obj = NULL;
1555 exprval_t exprval;
1556 VARIANT member;
1557 DISPID id;
1558 BSTR str;
1559 HRESULT hres;
1561 TRACE("\n");
1563 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1564 if(FAILED(hres))
1565 return hres;
1567 hres = exprval_to_value(ctx, &exprval, ei, &member);
1568 exprval_release(&exprval);
1569 if(FAILED(hres))
1570 return hres;
1572 hres = to_object(ctx, &member, &obj);
1573 VariantClear(&member);
1574 if(FAILED(hres))
1575 return hres;
1577 str = SysAllocString(expr->identifier);
1578 if(!str) {
1579 IDispatch_Release(obj);
1580 return E_OUTOFMEMORY;
1583 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1584 SysFreeString(str);
1585 if(SUCCEEDED(hres)) {
1586 exprval_set_idref(ret, obj, id);
1587 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1588 exprval_init(ret);
1589 hres = S_OK;
1592 IDispatch_Release(obj);
1593 return hres;
1596 /* ECMA-262 3rd Edition 11.2.1 */
1597 static HRESULT interp_member(exec_ctx_t *ctx)
1599 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1600 IDispatch *obj;
1601 VARIANT v;
1602 DISPID id;
1603 HRESULT hres;
1605 TRACE("\n");
1607 hres = stack_pop_object(ctx, &obj);
1608 if(FAILED(hres))
1609 return hres;
1611 hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
1612 if(SUCCEEDED(hres)) {
1613 V_VT(&v) = VT_EMPTY;
1614 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1615 }else if(hres == DISP_E_UNKNOWNNAME) {
1616 V_VT(&v) = VT_EMPTY;
1617 hres = S_OK;
1619 IDispatch_Release(obj);
1620 if(FAILED(hres))
1621 return hres;
1623 return stack_push(ctx, &v);
1626 /* ECMA-262 3rd Edition 11.2.1 */
1627 static HRESULT interp_memberid(exec_ctx_t *ctx)
1629 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1630 VARIANT *objv, *namev;
1631 IDispatch *obj;
1632 BSTR name;
1633 DISPID id;
1634 HRESULT hres;
1636 TRACE("%x\n", arg);
1638 namev = stack_pop(ctx);
1639 objv = stack_pop(ctx);
1641 hres = to_object(ctx->parser->script, objv, &obj);
1642 VariantClear(objv);
1643 if(SUCCEEDED(hres)) {
1644 hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1645 if(FAILED(hres))
1646 IDispatch_Release(obj);
1648 VariantClear(namev);
1649 if(FAILED(hres))
1650 return hres;
1652 hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
1653 SysFreeString(name);
1654 if(FAILED(hres)) {
1655 IDispatch_Release(obj);
1656 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1657 obj = NULL;
1658 id = JS_E_INVALID_PROPERTY;
1659 }else {
1660 return hres;
1664 return stack_push_objid(ctx, obj, id);
1667 /* ECMA-262 3rd Edition 11.2.1 */
1668 static HRESULT interp_refval(exec_ctx_t *ctx)
1670 IDispatch *disp;
1671 VARIANT v;
1672 DISPID id;
1673 HRESULT hres;
1675 TRACE("\n");
1677 disp = stack_topn_objid(ctx, 0, &id);
1678 if(!disp)
1679 return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1681 hres = disp_propget(ctx->parser->script, disp, id, &v, &ctx->ei, NULL/*FIXME*/);
1682 if(FAILED(hres))
1683 return hres;
1685 return stack_push(ctx, &v);
1688 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1690 VARIANT tmp;
1691 unsigned i;
1693 dp->cArgs = arg_cnt;
1694 dp->rgdispidNamedArgs = NULL;
1695 dp->cNamedArgs = 0;
1697 assert(ctx->top >= arg_cnt);
1699 for(i=1; i*2 <= arg_cnt; i++) {
1700 tmp = ctx->stack[ctx->top-i];
1701 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1702 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1705 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1708 /* ECMA-262 3rd Edition 11.2.2 */
1709 static HRESULT interp_new(exec_ctx_t *ctx)
1711 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1712 VARIANT *constr, v;
1713 DISPPARAMS dp;
1714 HRESULT hres;
1716 TRACE("%d\n", arg);
1718 constr = stack_topn(ctx, arg);
1720 /* NOTE: Should use to_object here */
1722 if(V_VT(constr) == VT_NULL)
1723 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1724 else if(V_VT(constr) != VT_DISPATCH)
1725 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1726 else if(!V_DISPATCH(constr))
1727 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1729 jsstack_to_dp(ctx, arg, &dp);
1730 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1731 DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1732 if(FAILED(hres))
1733 return hres;
1735 stack_popn(ctx, arg+1);
1736 return stack_push(ctx, &v);
1739 /* ECMA-262 3rd Edition 11.2.3 */
1740 static HRESULT interp_call(exec_ctx_t *ctx)
1742 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1743 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1744 VARIANT v, *objv;
1745 DISPPARAMS dp;
1746 HRESULT hres;
1748 TRACE("%d %d\n", argn, do_ret);
1750 objv = stack_topn(ctx, argn);
1751 if(V_VT(objv) != VT_DISPATCH)
1752 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1754 jsstack_to_dp(ctx, argn, &dp);
1755 hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1756 do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1757 if(FAILED(hres))
1758 return hres;
1760 stack_popn(ctx, argn+1);
1761 return do_ret ? stack_push(ctx, &v) : S_OK;
1765 /* ECMA-262 3rd Edition 11.2.3 */
1766 static HRESULT interp_call_member(exec_ctx_t *ctx)
1768 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1769 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1770 IDispatch *obj;
1771 DISPPARAMS dp;
1772 VARIANT v;
1773 DISPID id;
1774 HRESULT hres;
1776 TRACE("%d %d\n", argn, do_ret);
1778 obj = stack_topn_objid(ctx, argn, &id);
1779 if(!obj)
1780 return throw_type_error(ctx->parser->script, &ctx->ei, id, NULL);
1782 jsstack_to_dp(ctx, argn, &dp);
1783 hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1784 if(FAILED(hres))
1785 return hres;
1787 stack_popn(ctx, argn+2);
1788 return do_ret ? stack_push(ctx, &v) : S_OK;
1792 /* ECMA-262 3rd Edition 11.1.1 */
1793 static HRESULT interp_this(exec_ctx_t *ctx)
1795 VARIANT v;
1797 TRACE("\n");
1799 V_VT(&v) = VT_DISPATCH;
1800 V_DISPATCH(&v) = ctx->this_obj;
1801 IDispatch_AddRef(ctx->this_obj);
1802 return stack_push(ctx, &v);
1805 /* ECMA-262 3rd Edition 10.1.4 */
1806 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1808 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1809 BSTR identifier;
1810 HRESULT hres;
1812 TRACE("\n");
1814 identifier = SysAllocString(expr->identifier);
1815 if(!identifier)
1816 return E_OUTOFMEMORY;
1818 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1820 SysFreeString(identifier);
1821 return hres;
1824 /* ECMA-262 3rd Edition 10.1.4 */
1825 static HRESULT interp_ident(exec_ctx_t *ctx)
1827 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1828 exprval_t exprval;
1829 VARIANT v;
1830 HRESULT hres;
1832 TRACE("%s\n", debugstr_w(arg));
1834 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1835 if(FAILED(hres))
1836 return hres;
1838 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1839 exprval_release(&exprval);
1840 if(FAILED(hres))
1841 return hres;
1843 return stack_push(ctx, &v);
1846 /* ECMA-262 3rd Edition 10.1.4 */
1847 static HRESULT interp_identid(exec_ctx_t *ctx)
1849 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1850 const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1851 exprval_t exprval;
1852 HRESULT hres;
1854 TRACE("%s %x\n", debugstr_w(arg), flags);
1856 hres = identifier_eval(ctx->parser->script, arg, (flags&fdexNameEnsure) ? EXPR_NEWREF : 0, &ctx->ei, &exprval);
1857 if(FAILED(hres))
1858 return hres;
1860 if(exprval.type != EXPRVAL_IDREF) {
1861 WARN("invalid ref\n");
1862 exprval_release(&exprval);
1863 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1866 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1869 /* ECMA-262 3rd Edition 7.8.1 */
1870 static HRESULT interp_null(exec_ctx_t *ctx)
1872 VARIANT v;
1874 TRACE("\n");
1876 V_VT(&v) = VT_NULL;
1877 return stack_push(ctx, &v);
1880 /* ECMA-262 3rd Edition 7.8.2 */
1881 static HRESULT interp_bool(exec_ctx_t *ctx)
1883 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1885 TRACE("%s\n", arg ? "true" : "false");
1887 return stack_push_bool(ctx, arg);
1890 /* ECMA-262 3rd Edition 7.8.3 */
1891 static HRESULT interp_int(exec_ctx_t *ctx)
1893 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1894 VARIANT v;
1896 TRACE("%d\n", arg);
1898 V_VT(&v) = VT_I4;
1899 V_I4(&v) = arg;
1900 return stack_push(ctx, &v);
1903 /* ECMA-262 3rd Edition 7.8.3 */
1904 static HRESULT interp_double(exec_ctx_t *ctx)
1906 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1907 VARIANT v;
1909 TRACE("%lf\n", arg);
1911 V_VT(&v) = VT_R8;
1912 V_R8(&v) = arg;
1913 return stack_push(ctx, &v);
1916 /* ECMA-262 3rd Edition 7.8.4 */
1917 static HRESULT interp_str(exec_ctx_t *ctx)
1919 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1920 VARIANT v;
1922 TRACE("%s\n", debugstr_w(str));
1924 V_VT(&v) = VT_BSTR;
1925 V_BSTR(&v) = SysAllocString(str);
1926 if(!V_BSTR(&v))
1927 return E_OUTOFMEMORY;
1929 return stack_push(ctx, &v);
1932 /* ECMA-262 3rd Edition 7.8 */
1933 static HRESULT interp_regexp(exec_ctx_t *ctx)
1935 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1936 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1937 jsdisp_t *regexp;
1938 VARIANT v;
1939 HRESULT hres;
1941 TRACE("%s %x\n", debugstr_w(source), flags);
1943 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, &regexp);
1944 if(FAILED(hres))
1945 return hres;
1947 var_set_jsdisp(&v, regexp);
1948 return stack_push(ctx, &v);
1951 /* ECMA-262 3rd Edition 11.1.4 */
1952 static HRESULT interp_carray(exec_ctx_t *ctx)
1954 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1955 jsdisp_t *array;
1956 VARIANT *v, r;
1957 unsigned i;
1958 HRESULT hres;
1960 TRACE("%u\n", arg);
1962 hres = create_array(ctx->parser->script, arg, &array);
1963 if(FAILED(hres))
1964 return hres;
1966 i = arg;
1967 while(i--) {
1968 v = stack_pop(ctx);
1969 hres = jsdisp_propput_idx(array, i, v, &ctx->ei, NULL/*FIXME*/);
1970 VariantClear(v);
1971 if(FAILED(hres)) {
1972 jsdisp_release(array);
1973 return hres;
1977 var_set_jsdisp(&r, array);
1978 return stack_push(ctx, &r);
1981 /* ECMA-262 3rd Edition 11.1.5 */
1982 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1984 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1985 VARIANT val, tmp;
1986 jsdisp_t *obj;
1987 prop_val_t *iter;
1988 exprval_t exprval;
1989 BSTR name;
1990 HRESULT hres;
1992 TRACE("\n");
1994 hres = create_object(ctx, NULL, &obj);
1995 if(FAILED(hres))
1996 return hres;
1998 for(iter = expr->property_list; iter; iter = iter->next) {
1999 hres = literal_to_var(ctx, iter->name, &tmp);
2000 if(FAILED(hres))
2001 break;
2003 hres = to_string(ctx, &tmp, ei, &name);
2004 VariantClear(&tmp);
2005 if(FAILED(hres))
2006 break;
2008 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
2009 if(SUCCEEDED(hres)) {
2010 hres = exprval_to_value(ctx, &exprval, ei, &val);
2011 exprval_release(&exprval);
2012 if(SUCCEEDED(hres)) {
2013 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
2014 VariantClear(&val);
2018 SysFreeString(name);
2019 if(FAILED(hres))
2020 break;
2023 if(FAILED(hres)) {
2024 jsdisp_release(obj);
2025 return hres;
2028 ret->type = EXPRVAL_VARIANT;
2029 var_set_jsdisp(&ret->u.var, obj);
2030 return S_OK;
2033 /* ECMA-262 3rd Edition 11.11 */
2034 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
2036 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2037 VARIANT_BOOL b;
2038 HRESULT hres;
2040 TRACE("\n");
2042 hres = to_boolean(stack_top(ctx), &b);
2043 if(FAILED(hres))
2044 return hres;
2046 if(b) {
2047 ctx->ip = arg;
2048 }else {
2049 stack_popn(ctx, 1);
2050 ctx->ip++;
2052 return S_OK;
2055 /* ECMA-262 3rd Edition 11.11 */
2056 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2058 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2059 VARIANT_BOOL b;
2060 HRESULT hres;
2062 TRACE("\n");
2064 hres = to_boolean(stack_top(ctx), &b);
2065 if(FAILED(hres))
2066 return hres;
2068 if(b) {
2069 stack_popn(ctx, 1);
2070 ctx->ip++;
2071 }else {
2072 ctx->ip = arg;
2074 return S_OK;
2077 /* ECMA-262 3rd Edition 11.10 */
2078 static HRESULT interp_or(exec_ctx_t *ctx)
2080 INT l, r;
2081 HRESULT hres;
2083 TRACE("\n");
2085 hres = stack_pop_int(ctx, &r);
2086 if(FAILED(hres))
2087 return hres;
2089 hres = stack_pop_int(ctx, &l);
2090 if(FAILED(hres))
2091 return hres;
2093 return stack_push_int(ctx, l|r);
2096 /* ECMA-262 3rd Edition 11.10 */
2097 static HRESULT interp_xor(exec_ctx_t *ctx)
2099 INT l, r;
2100 HRESULT hres;
2102 TRACE("\n");
2104 hres = stack_pop_int(ctx, &r);
2105 if(FAILED(hres))
2106 return hres;
2108 hres = stack_pop_int(ctx, &l);
2109 if(FAILED(hres))
2110 return hres;
2112 return stack_push_int(ctx, l^r);
2115 /* ECMA-262 3rd Edition 11.10 */
2116 static HRESULT interp_and(exec_ctx_t *ctx)
2118 INT l, r;
2119 HRESULT hres;
2121 TRACE("\n");
2123 hres = stack_pop_int(ctx, &r);
2124 if(FAILED(hres))
2125 return hres;
2127 hres = stack_pop_int(ctx, &l);
2128 if(FAILED(hres))
2129 return hres;
2131 return stack_push_int(ctx, l&r);
2134 /* ECMA-262 3rd Edition 11.8.6 */
2135 static HRESULT interp_instanceof(exec_ctx_t *ctx)
2137 jsdisp_t *obj, *iter, *tmp = NULL;
2138 VARIANT prot, *v;
2139 BOOL ret = FALSE;
2140 HRESULT hres;
2142 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2144 v = stack_pop(ctx);
2145 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
2146 VariantClear(v);
2147 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
2150 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
2151 IDispatch_Release(V_DISPATCH(v));
2152 if(!obj) {
2153 FIXME("non-jsdisp objects not supported\n");
2154 return E_FAIL;
2157 if(is_class(obj, JSCLASS_FUNCTION)) {
2158 hres = jsdisp_propget_name(obj, prototypeW, &prot, &ctx->ei, NULL/*FIXME*/);
2159 }else {
2160 hres = throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
2162 jsdisp_release(obj);
2163 if(FAILED(hres))
2164 return hres;
2166 v = stack_pop(ctx);
2168 if(V_VT(&prot) == VT_DISPATCH) {
2169 if(V_VT(v) == VT_DISPATCH)
2170 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
2171 for(iter = tmp; !ret && iter; iter = iter->prototype) {
2172 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
2173 if(FAILED(hres))
2174 break;
2177 if(tmp)
2178 jsdisp_release(tmp);
2179 }else {
2180 FIXME("prototype is not an object\n");
2181 hres = E_FAIL;
2184 VariantClear(&prot);
2185 VariantClear(v);
2186 if(FAILED(hres))
2187 return hres;
2189 return stack_push_bool(ctx, ret);
2192 /* ECMA-262 3rd Edition 11.8.7 */
2193 static HRESULT interp_in(exec_ctx_t *ctx)
2195 VARIANT *obj, *v;
2196 DISPID id = 0;
2197 BOOL ret;
2198 BSTR str;
2199 HRESULT hres;
2201 TRACE("\n");
2203 obj = stack_pop(ctx);
2204 v = stack_pop(ctx);
2206 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2207 VariantClear(obj);
2208 VariantClear(v);
2209 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2212 hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2213 VariantClear(v);
2214 if(FAILED(hres)) {
2215 IDispatch_Release(V_DISPATCH(obj));
2216 return hres;
2219 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2220 IDispatch_Release(V_DISPATCH(obj));
2221 SysFreeString(str);
2222 if(SUCCEEDED(hres))
2223 ret = TRUE;
2224 else if(hres == DISP_E_UNKNOWNNAME)
2225 ret = FALSE;
2226 else
2227 return hres;
2229 return stack_push_bool(ctx, ret);
2232 /* ECMA-262 3rd Edition 11.6.1 */
2233 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2235 VARIANT r, l;
2236 HRESULT hres;
2238 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2239 if(FAILED(hres))
2240 return hres;
2242 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2243 if(FAILED(hres)) {
2244 VariantClear(&l);
2245 return hres;
2248 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2249 BSTR lstr = NULL, rstr = NULL;
2251 if(V_VT(&l) == VT_BSTR)
2252 lstr = V_BSTR(&l);
2253 else
2254 hres = to_string(ctx, &l, ei, &lstr);
2256 if(SUCCEEDED(hres)) {
2257 if(V_VT(&r) == VT_BSTR)
2258 rstr = V_BSTR(&r);
2259 else
2260 hres = to_string(ctx, &r, ei, &rstr);
2263 if(SUCCEEDED(hres)) {
2264 int len1, len2;
2266 len1 = SysStringLen(lstr);
2267 len2 = SysStringLen(rstr);
2269 V_VT(retv) = VT_BSTR;
2270 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2271 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2272 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2275 if(V_VT(&l) != VT_BSTR)
2276 SysFreeString(lstr);
2277 if(V_VT(&r) != VT_BSTR)
2278 SysFreeString(rstr);
2279 }else {
2280 VARIANT nl, nr;
2282 hres = to_number(ctx, &l, ei, &nl);
2283 if(SUCCEEDED(hres)) {
2284 hres = to_number(ctx, &r, ei, &nr);
2285 if(SUCCEEDED(hres))
2286 num_set_val(retv, num_val(&nl) + num_val(&nr));
2290 VariantClear(&r);
2291 VariantClear(&l);
2292 return hres;
2295 /* ECMA-262 3rd Edition 11.6.1 */
2296 static HRESULT interp_add(exec_ctx_t *ctx)
2298 VARIANT *l, *r, ret;
2299 HRESULT hres;
2301 r = stack_pop(ctx);
2302 l = stack_pop(ctx);
2304 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2306 hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2307 VariantClear(l);
2308 VariantClear(r);
2309 if(FAILED(hres))
2310 return hres;
2312 return stack_push(ctx, &ret);
2315 /* ECMA-262 3rd Edition 11.6.2 */
2316 static HRESULT interp_sub(exec_ctx_t *ctx)
2318 VARIANT l, r;
2319 HRESULT hres;
2321 TRACE("\n");
2323 hres = stack_pop_number(ctx, &r);
2324 if(FAILED(hres))
2325 return hres;
2327 hres = stack_pop_number(ctx, &l);
2328 if(FAILED(hres))
2329 return hres;
2331 return stack_push_number(ctx, num_val(&l)-num_val(&r));
2334 /* ECMA-262 3rd Edition 11.5.1 */
2335 static HRESULT interp_mul(exec_ctx_t *ctx)
2337 VARIANT l, r;
2338 HRESULT hres;
2340 TRACE("\n");
2342 hres = stack_pop_number(ctx, &r);
2343 if(FAILED(hres))
2344 return hres;
2346 hres = stack_pop_number(ctx, &l);
2347 if(FAILED(hres))
2348 return hres;
2350 return stack_push_number(ctx, num_val(&l)*num_val(&r));
2353 /* ECMA-262 3rd Edition 11.5.2 */
2354 static HRESULT interp_div(exec_ctx_t *ctx)
2356 VARIANT l, r;
2357 HRESULT hres;
2359 TRACE("\n");
2361 hres = stack_pop_number(ctx, &r);
2362 if(FAILED(hres))
2363 return hres;
2365 hres = stack_pop_number(ctx, &l);
2366 if(FAILED(hres))
2367 return hres;
2369 return stack_push_number(ctx, num_val(&l)/num_val(&r));
2372 /* ECMA-262 3rd Edition 11.5.3 */
2373 static HRESULT interp_mod(exec_ctx_t *ctx)
2375 VARIANT l, r;
2376 HRESULT hres;
2378 TRACE("\n");
2380 hres = stack_pop_number(ctx, &r);
2381 if(FAILED(hres))
2382 return hres;
2384 hres = stack_pop_number(ctx, &l);
2385 if(FAILED(hres))
2386 return hres;
2388 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2391 /* ECMA-262 3rd Edition 11.4.2 */
2392 static HRESULT interp_delete(exec_ctx_t *ctx)
2394 VARIANT *obj_var, *name_var;
2395 IDispatchEx *dispex;
2396 IDispatch *obj;
2397 BSTR name;
2398 BOOL ret;
2399 HRESULT hres;
2401 TRACE("\n");
2403 name_var = stack_pop(ctx);
2404 obj_var = stack_pop(ctx);
2406 hres = to_object(ctx->parser->script, obj_var, &obj);
2407 VariantClear(obj_var);
2408 if(FAILED(hres)) {
2409 VariantClear(name_var);
2410 return hres;
2413 hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2414 VariantClear(name_var);
2415 if(FAILED(hres)) {
2416 IDispatch_Release(obj);
2417 return hres;
2420 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2421 if(SUCCEEDED(hres)) {
2422 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2423 ret = TRUE;
2424 IDispatchEx_Release(dispex);
2425 }else {
2426 hres = S_OK;
2427 ret = FALSE;
2430 IDispatch_Release(obj);
2431 SysFreeString(name);
2432 if(FAILED(hres))
2433 return hres;
2435 return stack_push_bool(ctx, ret);
2438 /* ECMA-262 3rd Edition 11.4.2 */
2439 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
2441 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2442 IDispatchEx *dispex;
2443 exprval_t exprval;
2444 BOOL ret = FALSE;
2445 HRESULT hres;
2447 TRACE("%s\n", debugstr_w(arg));
2449 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2450 if(FAILED(hres))
2451 return hres;
2453 if(exprval.type != EXPRVAL_IDREF) {
2454 FIXME("Unsupported exprval\n");
2455 exprval_release(&exprval);
2456 return E_NOTIMPL;
2459 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2460 IDispatch_Release(exprval.u.idref.disp);
2461 if(SUCCEEDED(hres)) {
2462 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2463 IDispatchEx_Release(dispex);
2464 if(FAILED(hres))
2465 return hres;
2467 ret = TRUE;
2470 return stack_push_bool(ctx, ret);
2473 /* ECMA-262 3rd Edition 11.4.2 */
2474 static HRESULT interp_void(exec_ctx_t *ctx)
2476 VARIANT v;
2478 TRACE("\n");
2480 stack_popn(ctx, 1);
2482 V_VT(&v) = VT_EMPTY;
2483 return stack_push(ctx, &v);
2486 /* ECMA-262 3rd Edition 11.4.3 */
2487 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
2489 switch(V_VT(v)) {
2490 case VT_EMPTY:
2491 *ret = undefinedW;
2492 break;
2493 case VT_NULL:
2494 *ret = objectW;
2495 break;
2496 case VT_BOOL:
2497 *ret = booleanW;
2498 break;
2499 case VT_I4:
2500 case VT_R8:
2501 *ret = numberW;
2502 break;
2503 case VT_BSTR:
2504 *ret = stringW;
2505 break;
2506 case VT_DISPATCH: {
2507 jsdisp_t *dispex;
2509 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
2510 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2511 jsdisp_release(dispex);
2512 }else {
2513 *ret = objectW;
2515 break;
2517 default:
2518 FIXME("unhandled vt %d\n", V_VT(v));
2519 return E_NOTIMPL;
2522 return S_OK;
2525 /* ECMA-262 3rd Edition 11.4.3 */
2526 static HRESULT interp_typeofid(exec_ctx_t *ctx)
2528 const WCHAR *ret;
2529 IDispatch *obj;
2530 VARIANT v;
2531 DISPID id;
2532 HRESULT hres;
2534 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2536 TRACE("\n");
2538 obj = stack_pop_objid(ctx, &id);
2539 if(!obj)
2540 return stack_push_string(ctx, undefinedW);
2542 V_VT(&v) = VT_EMPTY;
2543 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2544 IDispatch_Release(obj);
2545 if(FAILED(hres))
2546 return stack_push_string(ctx, unknownW);
2548 hres = typeof_string(&v, &ret);
2549 VariantClear(&v);
2550 if(FAILED(hres))
2551 return hres;
2553 return stack_push_string(ctx, ret);
2556 /* ECMA-262 3rd Edition 11.4.3 */
2557 static HRESULT interp_typeofident(exec_ctx_t *ctx)
2559 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2560 exprval_t exprval;
2561 const WCHAR *ret;
2562 VARIANT v;
2563 HRESULT hres;
2565 TRACE("%s\n", debugstr_w(arg));
2567 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2568 if(FAILED(hres))
2569 return hres;
2571 if(exprval.type == EXPRVAL_INVALID) {
2572 hres = stack_push_string(ctx, undefinedW);
2573 exprval_release(&exprval);
2574 return hres;
2577 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
2578 exprval_release(&exprval);
2579 if(FAILED(hres))
2580 return hres;
2582 hres = typeof_string(&v, &ret);
2583 VariantClear(&v);
2584 if(FAILED(hres))
2585 return hres;
2587 return stack_push_string(ctx, ret);
2590 /* ECMA-262 3rd Edition 11.4.3 */
2591 static HRESULT interp_typeof(exec_ctx_t *ctx)
2593 const WCHAR *ret;
2594 VARIANT *v;
2595 HRESULT hres;
2597 TRACE("\n");
2599 v = stack_pop(ctx);
2600 hres = typeof_string(v, &ret);
2601 VariantClear(v);
2602 if(FAILED(hres))
2603 return hres;
2605 return stack_push_string(ctx, ret);
2608 /* ECMA-262 3rd Edition 11.4.7 */
2609 static HRESULT interp_minus(exec_ctx_t *ctx)
2611 VARIANT n;
2612 HRESULT hres;
2614 TRACE("\n");
2616 hres = stack_pop_number(ctx, &n);
2617 if(FAILED(hres))
2618 return hres;
2620 return stack_push_number(ctx, -num_val(&n));
2623 /* ECMA-262 3rd Edition 11.4.6 */
2624 static HRESULT interp_tonum(exec_ctx_t *ctx)
2626 VARIANT *v, num;
2627 HRESULT hres;
2629 TRACE("\n");
2631 v = stack_pop(ctx);
2632 hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2633 VariantClear(v);
2634 if(FAILED(hres))
2635 return hres;
2637 return stack_push(ctx, &num);
2640 /* ECMA-262 3rd Edition 11.3.1 */
2641 static HRESULT interp_postinc(exec_ctx_t *ctx)
2643 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2644 IDispatch *obj;
2645 DISPID id;
2646 VARIANT v;
2647 HRESULT hres;
2649 TRACE("%d\n", arg);
2651 obj = stack_pop_objid(ctx, &id);
2652 if(!obj)
2653 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2655 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2656 if(SUCCEEDED(hres)) {
2657 VARIANT n, inc;
2659 hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2660 if(SUCCEEDED(hres)) {
2661 num_set_val(&inc, num_val(&n)+(double)arg);
2662 hres = disp_propput(ctx->parser->script, obj, id, &inc, &ctx->ei, NULL/*FIXME*/);
2664 if(FAILED(hres))
2665 VariantClear(&v);
2667 IDispatch_Release(obj);
2668 if(FAILED(hres))
2669 return hres;
2671 return stack_push(ctx, &v);
2674 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2675 static HRESULT interp_preinc(exec_ctx_t *ctx)
2677 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2678 IDispatch *obj;
2679 DISPID id;
2680 VARIANT v;
2681 HRESULT hres;
2683 TRACE("%d\n", arg);
2685 obj = stack_pop_objid(ctx, &id);
2686 if(!obj)
2687 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2689 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2690 if(SUCCEEDED(hres)) {
2691 VARIANT n;
2693 hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2694 VariantClear(&v);
2695 if(SUCCEEDED(hres)) {
2696 num_set_val(&v, num_val(&n)+(double)arg);
2697 hres = disp_propput(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2700 IDispatch_Release(obj);
2701 if(FAILED(hres))
2702 return hres;
2704 return stack_push(ctx, &v);
2707 /* ECMA-262 3rd Edition 11.9.3 */
2708 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2710 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2711 return equal2_values(lval, rval, ret);
2713 /* FIXME: NULL disps should be handled in more general way */
2714 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2715 VARIANT v;
2716 V_VT(&v) = VT_NULL;
2717 return equal_values(ctx, &v, rval, ei, ret);
2720 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2721 VARIANT v;
2722 V_VT(&v) = VT_NULL;
2723 return equal_values(ctx, lval, &v, ei, ret);
2726 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2727 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2728 *ret = TRUE;
2729 return S_OK;
2732 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2733 VARIANT v;
2734 HRESULT hres;
2736 hres = to_number(ctx, lval, ei, &v);
2737 if(FAILED(hres))
2738 return hres;
2740 return equal_values(ctx, &v, rval, ei, ret);
2743 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2744 VARIANT v;
2745 HRESULT hres;
2747 hres = to_number(ctx, rval, ei, &v);
2748 if(FAILED(hres))
2749 return hres;
2751 return equal_values(ctx, lval, &v, ei, ret);
2754 if(V_VT(rval) == VT_BOOL) {
2755 VARIANT v;
2757 V_VT(&v) = VT_I4;
2758 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2759 return equal_values(ctx, lval, &v, ei, ret);
2762 if(V_VT(lval) == VT_BOOL) {
2763 VARIANT v;
2765 V_VT(&v) = VT_I4;
2766 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2767 return equal_values(ctx, &v, rval, ei, ret);
2771 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2772 VARIANT v;
2773 HRESULT hres;
2775 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2776 if(FAILED(hres))
2777 return hres;
2779 hres = equal_values(ctx, lval, &v, ei, ret);
2781 VariantClear(&v);
2782 return hres;
2786 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2787 VARIANT v;
2788 HRESULT hres;
2790 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2791 if(FAILED(hres))
2792 return hres;
2794 hres = equal_values(ctx, &v, rval, ei, ret);
2796 VariantClear(&v);
2797 return hres;
2801 *ret = FALSE;
2802 return S_OK;
2805 /* ECMA-262 3rd Edition 11.9.1 */
2806 static HRESULT interp_eq(exec_ctx_t *ctx)
2808 VARIANT *l, *r;
2809 BOOL b;
2810 HRESULT hres;
2812 r = stack_pop(ctx);
2813 l = stack_pop(ctx);
2815 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2817 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2818 VariantClear(l);
2819 VariantClear(r);
2820 if(FAILED(hres))
2821 return hres;
2823 return stack_push_bool(ctx, b);
2826 /* ECMA-262 3rd Edition 11.9.2 */
2827 static HRESULT interp_neq(exec_ctx_t *ctx)
2829 VARIANT *l, *r;
2830 BOOL b;
2831 HRESULT hres;
2833 r = stack_pop(ctx);
2834 l = stack_pop(ctx);
2836 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2838 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2839 VariantClear(l);
2840 VariantClear(r);
2841 if(FAILED(hres))
2842 return hres;
2844 return stack_push_bool(ctx, !b);
2847 /* ECMA-262 3rd Edition 11.9.4 */
2848 static HRESULT interp_eq2(exec_ctx_t *ctx)
2850 VARIANT *l, *r;
2851 BOOL b;
2852 HRESULT hres;
2854 TRACE("\n");
2856 r = stack_pop(ctx);
2857 l = stack_pop(ctx);
2859 hres = equal2_values(r, l, &b);
2860 VariantClear(l);
2861 VariantClear(r);
2862 if(FAILED(hres))
2863 return hres;
2865 return stack_push_bool(ctx, b);
2868 /* ECMA-262 3rd Edition 11.9.5 */
2869 static HRESULT interp_neq2(exec_ctx_t *ctx)
2871 VARIANT *l, *r;
2872 BOOL b;
2873 HRESULT hres;
2875 TRACE("\n");
2877 r = stack_pop(ctx);
2878 l = stack_pop(ctx);
2880 hres = equal2_values(r, l, &b);
2881 VariantClear(l);
2882 VariantClear(r);
2883 if(FAILED(hres))
2884 return hres;
2886 return stack_push_bool(ctx, !b);
2889 /* ECMA-262 3rd Edition 11.8.5 */
2890 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2892 VARIANT l, r, ln, rn;
2893 HRESULT hres;
2895 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2896 if(FAILED(hres))
2897 return hres;
2899 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2900 if(FAILED(hres)) {
2901 VariantClear(&l);
2902 return hres;
2905 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2906 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2907 SysFreeString(V_BSTR(&l));
2908 SysFreeString(V_BSTR(&r));
2909 return S_OK;
2912 hres = to_number(ctx, &l, ei, &ln);
2913 VariantClear(&l);
2914 if(SUCCEEDED(hres))
2915 hres = to_number(ctx, &r, ei, &rn);
2916 VariantClear(&r);
2917 if(FAILED(hres))
2918 return hres;
2920 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2921 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2922 }else {
2923 DOUBLE ld = num_val(&ln);
2924 DOUBLE rd = num_val(&rn);
2926 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2929 return S_OK;
2932 /* ECMA-262 3rd Edition 11.8.1 */
2933 static HRESULT interp_lt(exec_ctx_t *ctx)
2935 VARIANT *l, *r;
2936 BOOL b;
2937 HRESULT hres;
2939 r = stack_pop(ctx);
2940 l = stack_pop(ctx);
2942 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2944 hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
2945 VariantClear(l);
2946 VariantClear(r);
2947 if(FAILED(hres))
2948 return hres;
2950 return stack_push_bool(ctx, b);
2953 /* ECMA-262 3rd Edition 11.8.1 */
2954 static HRESULT interp_lteq(exec_ctx_t *ctx)
2956 VARIANT *l, *r;
2957 BOOL b;
2958 HRESULT hres;
2960 r = stack_pop(ctx);
2961 l = stack_pop(ctx);
2963 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2965 hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
2966 VariantClear(l);
2967 VariantClear(r);
2968 if(FAILED(hres))
2969 return hres;
2971 return stack_push_bool(ctx, b);
2974 /* ECMA-262 3rd Edition 11.8.2 */
2975 static HRESULT interp_gt(exec_ctx_t *ctx)
2977 VARIANT *l, *r;
2978 BOOL b;
2979 HRESULT hres;
2981 r = stack_pop(ctx);
2982 l = stack_pop(ctx);
2984 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2986 hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
2987 VariantClear(l);
2988 VariantClear(r);
2989 if(FAILED(hres))
2990 return hres;
2992 return stack_push_bool(ctx, b);
2995 /* ECMA-262 3rd Edition 11.8.4 */
2996 static HRESULT interp_gteq(exec_ctx_t *ctx)
2998 VARIANT *l, *r;
2999 BOOL b;
3000 HRESULT hres;
3002 r = stack_pop(ctx);
3003 l = stack_pop(ctx);
3005 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
3007 hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
3008 VariantClear(l);
3009 VariantClear(r);
3010 if(FAILED(hres))
3011 return hres;
3013 return stack_push_bool(ctx, b);
3016 /* ECMA-262 3rd Edition 11.4.8 */
3017 static HRESULT interp_bneg(exec_ctx_t *ctx)
3019 VARIANT *v, r;
3020 INT i;
3021 HRESULT hres;
3023 TRACE("\n");
3025 v = stack_pop(ctx);
3026 hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3027 VariantClear(v);
3028 if(FAILED(hres))
3029 return hres;
3031 V_VT(&r) = VT_I4;
3032 V_I4(&r) = ~i;
3033 return stack_push(ctx, &r);
3036 /* ECMA-262 3rd Edition 11.4.9 */
3037 static HRESULT interp_neg(exec_ctx_t *ctx)
3039 VARIANT *v;
3040 VARIANT_BOOL b;
3041 HRESULT hres;
3043 TRACE("\n");
3045 v = stack_pop(ctx);
3046 hres = to_boolean(v, &b);
3047 VariantClear(v);
3048 if(FAILED(hres))
3049 return hres;
3051 return stack_push_bool(ctx, !b);
3054 /* ECMA-262 3rd Edition 11.7.1 */
3055 static HRESULT interp_lshift(exec_ctx_t *ctx)
3057 DWORD r;
3058 INT l;
3059 HRESULT hres;
3061 hres = stack_pop_uint(ctx, &r);
3062 if(FAILED(hres))
3063 return hres;
3065 hres = stack_pop_int(ctx, &l);
3066 if(FAILED(hres))
3067 return hres;
3069 return stack_push_int(ctx, l << (r&0x1f));
3072 /* ECMA-262 3rd Edition 11.7.2 */
3073 static HRESULT interp_rshift(exec_ctx_t *ctx)
3075 DWORD r;
3076 INT l;
3077 HRESULT hres;
3079 hres = stack_pop_uint(ctx, &r);
3080 if(FAILED(hres))
3081 return hres;
3083 hres = stack_pop_int(ctx, &l);
3084 if(FAILED(hres))
3085 return hres;
3087 return stack_push_int(ctx, l >> (r&0x1f));
3090 /* ECMA-262 3rd Edition 11.7.3 */
3091 static HRESULT interp_rshift2(exec_ctx_t *ctx)
3093 DWORD r, l;
3094 HRESULT hres;
3096 hres = stack_pop_uint(ctx, &r);
3097 if(FAILED(hres))
3098 return hres;
3100 hres = stack_pop_uint(ctx, &l);
3101 if(FAILED(hres))
3102 return hres;
3104 return stack_push_int(ctx, l >> (r&0x1f));
3107 /* ECMA-262 3rd Edition 11.13.1 */
3108 static HRESULT interp_assign(exec_ctx_t *ctx)
3110 IDispatch *disp;
3111 DISPID id;
3112 VARIANT *v;
3113 HRESULT hres;
3115 TRACE("\n");
3117 v = stack_pop(ctx);
3118 disp = stack_pop_objid(ctx, &id);
3120 if(!disp)
3121 return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
3123 hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
3124 IDispatch_Release(disp);
3125 if(FAILED(hres)) {
3126 VariantClear(v);
3127 return hres;
3130 return stack_push(ctx, v);
3133 static HRESULT interp_undefined(exec_ctx_t *ctx)
3135 VARIANT v;
3137 TRACE("\n");
3139 V_VT(&v) = VT_EMPTY;
3140 return stack_push(ctx, &v);
3143 static HRESULT interp_jmp(exec_ctx_t *ctx)
3145 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3147 TRACE("\n");
3149 ctx->ip = arg;
3150 return S_OK;
3153 static HRESULT interp_pop(exec_ctx_t *ctx)
3155 TRACE("\n");
3157 stack_popn(ctx, 1);
3158 return S_OK;
3161 static HRESULT interp_ret(exec_ctx_t *ctx)
3163 TRACE("\n");
3165 ctx->ip = -1;
3166 return S_OK;
3169 static HRESULT interp_tree(exec_ctx_t *ctx)
3171 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3172 exprval_t val;
3173 VARIANT v;
3174 HRESULT hres;
3176 TRACE("\n");
3178 hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3179 if(FAILED(hres))
3180 return hres;
3182 hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3183 exprval_release(&val);
3184 if(FAILED(hres))
3185 return hres;
3187 return stack_push(ctx, &v);
3190 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3192 static const op_func_t op_funcs[] = {
3193 #define X(x,a,b,c) interp_##x,
3194 OP_LIST
3195 #undef X
3198 static const unsigned op_move[] = {
3199 #define X(a,x,b,c) x,
3200 OP_LIST
3201 #undef X
3204 static HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3206 exec_ctx_t *exec_ctx = ctx->exec_ctx;
3207 unsigned prev_ip, prev_top;
3208 jsop_t op;
3209 HRESULT hres = S_OK;
3211 TRACE("\n");
3213 prev_top = exec_ctx->top;
3214 prev_ip = exec_ctx->ip;
3215 exec_ctx->ip = expr->instr_off;
3217 while(exec_ctx->ip != -1) {
3218 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3219 hres = op_funcs[op](exec_ctx);
3220 if(FAILED(hres))
3221 break;
3222 exec_ctx->ip += op_move[op];
3225 exec_ctx->ip = prev_ip;
3227 if(FAILED(hres)) {
3228 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3229 *ei = exec_ctx->ei;
3230 memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3231 return hres;
3234 assert(exec_ctx->top == prev_top+1 || ((flags&EXPR_NOVAL) && exec_ctx->top == prev_top));
3236 ret->type = EXPRVAL_VARIANT;
3237 if(exec_ctx->top == prev_top)
3238 V_VT(&ret->u.var) = VT_EMPTY;
3239 else
3240 ret->u.var = *stack_pop(exec_ctx);
3241 return S_OK;
3244 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3246 HRESULT hres;
3248 TRACE("\n");
3250 hres = compile_subscript(ctx->exec_ctx->parser, expr, !(flags & EXPR_NOVAL), &expr->instr_off);
3251 if(FAILED(hres))
3252 return hres;
3254 if(expr->eval == compiled_expression_eval)
3255 expr->eval = interp_expression_eval;
3257 return expr->eval(ctx, expr, flags, ei, ret);