jscript: Throw type error on invalid delete.
[wine.git] / dlls / jscript / engine.c
blob46bdffb62addc6709fb9506cddc2dff0fee3e1bb
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 hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
1078 if(SUCCEEDED(hres)) {
1079 V_VT(&name) = VT_BSTR;
1080 V_BSTR(&name) = str;
1081 hres = put_value(ctx, &exprval, &name, &rt->ei);
1082 exprval_release(&exprval);
1084 SysFreeString(str);
1085 if(FAILED(hres))
1086 break;
1088 hres = stat_eval(ctx, stat->statement, rt, &tmp);
1089 if(FAILED(hres))
1090 break;
1092 VariantClear(&retv);
1093 retv = tmp;
1095 if(rt->type == RT_CONTINUE)
1096 rt->type = RT_NORMAL;
1097 else if(rt->type != RT_NORMAL)
1098 break;
1101 SysFreeString(identifier);
1102 IDispatchEx_Release(in_obj);
1103 if(FAILED(hres)) {
1104 VariantClear(&retv);
1105 return hres;
1108 if(rt->type == RT_BREAK)
1109 rt->type = RT_NORMAL;
1111 *ret = retv;
1112 return S_OK;
1115 /* ECMA-262 3rd Edition 12.7 */
1116 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1118 branch_statement_t *stat = (branch_statement_t*)_stat;
1120 TRACE("\n");
1122 if(stat->identifier) {
1123 FIXME("indentifier not implemented\n");
1124 return E_NOTIMPL;
1127 rt->type = RT_CONTINUE;
1128 V_VT(ret) = VT_EMPTY;
1129 return S_OK;
1132 /* ECMA-262 3rd Edition 12.8 */
1133 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1135 branch_statement_t *stat = (branch_statement_t*)_stat;
1137 TRACE("\n");
1139 if(stat->identifier) {
1140 FIXME("indentifier not implemented\n");
1141 return E_NOTIMPL;
1144 rt->type = RT_BREAK;
1145 V_VT(ret) = VT_EMPTY;
1146 return S_OK;
1149 /* ECMA-262 3rd Edition 12.9 */
1150 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1152 expression_statement_t *stat = (expression_statement_t*)_stat;
1153 HRESULT hres;
1155 TRACE("\n");
1157 if(stat->expr) {
1158 exprval_t exprval;
1160 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1161 if(FAILED(hres))
1162 return hres;
1164 hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1165 exprval_release(&exprval);
1166 if(FAILED(hres))
1167 return hres;
1168 }else {
1169 V_VT(ret) = VT_EMPTY;
1172 TRACE("= %s\n", debugstr_variant(ret));
1173 rt->type = RT_RETURN;
1174 return S_OK;
1177 /* ECMA-262 3rd Edition 12.10 */
1178 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1180 with_statement_t *stat = (with_statement_t*)_stat;
1181 exprval_t exprval;
1182 IDispatch *disp;
1183 jsdisp_t *obj;
1184 VARIANT val;
1185 HRESULT hres;
1187 TRACE("\n");
1189 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1190 if(FAILED(hres))
1191 return hres;
1193 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1194 exprval_release(&exprval);
1195 if(FAILED(hres))
1196 return hres;
1198 hres = to_object(ctx, &val, &disp);
1199 VariantClear(&val);
1200 if(FAILED(hres))
1201 return hres;
1203 obj = iface_to_jsdisp((IUnknown*)disp);
1204 IDispatch_Release(disp);
1205 if(!obj) {
1206 FIXME("disp id not jsdisp\n");
1207 return E_NOTIMPL;
1210 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1211 jsdisp_release(obj);
1212 if(FAILED(hres))
1213 return hres;
1215 hres = stat_eval(ctx, stat->statement, rt, ret);
1217 scope_pop(&ctx->exec_ctx->scope_chain);
1218 return hres;
1221 /* ECMA-262 3rd Edition 12.12 */
1222 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1224 FIXME("\n");
1225 return E_NOTIMPL;
1228 /* ECMA-262 3rd Edition 12.13 */
1229 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1231 switch_statement_t *stat = (switch_statement_t*)_stat;
1232 case_clausule_t *iter, *default_clausule = NULL;
1233 statement_t *stat_iter;
1234 VARIANT val, cval;
1235 exprval_t exprval;
1236 BOOL b;
1237 HRESULT hres;
1239 TRACE("\n");
1241 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1242 if(FAILED(hres))
1243 return hres;
1245 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1246 exprval_release(&exprval);
1247 if(FAILED(hres))
1248 return hres;
1250 for(iter = stat->case_list; iter; iter = iter->next) {
1251 if(!iter->expr) {
1252 default_clausule = iter;
1253 continue;
1256 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1257 if(FAILED(hres))
1258 break;
1260 hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1261 exprval_release(&exprval);
1262 if(FAILED(hres))
1263 break;
1265 hres = equal2_values(&val, &cval, &b);
1266 VariantClear(&cval);
1267 if(FAILED(hres) || b)
1268 break;
1271 VariantClear(&val);
1272 if(FAILED(hres))
1273 return hres;
1275 if(!iter)
1276 iter = default_clausule;
1278 V_VT(&val) = VT_EMPTY;
1279 if(iter) {
1280 VARIANT tmp;
1282 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1283 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1284 if(FAILED(hres))
1285 break;
1287 VariantClear(&val);
1288 val = tmp;
1290 if(rt->type != RT_NORMAL)
1291 break;
1295 if(FAILED(hres)) {
1296 VariantClear(&val);
1297 return hres;
1300 if(rt->type == RT_BREAK)
1301 rt->type = RT_NORMAL;
1303 *ret = val;
1304 return S_OK;
1307 /* ECMA-262 3rd Edition 12.13 */
1308 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1310 expression_statement_t *stat = (expression_statement_t*)_stat;
1311 exprval_t exprval;
1312 VARIANT val;
1313 HRESULT hres;
1315 TRACE("\n");
1317 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1318 if(FAILED(hres))
1319 return hres;
1321 hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1322 exprval_release(&exprval);
1323 if(FAILED(hres))
1324 return hres;
1326 rt->ei.var = val;
1327 return DISP_E_EXCEPTION;
1330 static HRESULT interp_throw(exec_ctx_t *ctx)
1332 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1334 TRACE("%08x\n", arg);
1336 return throw_reference_error(ctx->parser->script, &ctx->ei, arg, NULL);
1339 static HRESULT interp_throw_type(exec_ctx_t *ctx)
1341 const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1342 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
1344 TRACE("%08x %s\n", hres, debugstr_w(str));
1346 return throw_type_error(ctx->parser->script, &ctx->ei, hres, str);
1349 /* ECMA-262 3rd Edition 12.14 */
1350 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1352 jsdisp_t *var_disp;
1353 VARIANT ex, val;
1354 HRESULT hres;
1356 ex = rt->ei.var;
1357 memset(&rt->ei, 0, sizeof(jsexcept_t));
1359 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1360 if(SUCCEEDED(hres)) {
1361 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1362 if(SUCCEEDED(hres)) {
1363 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1364 if(SUCCEEDED(hres)) {
1365 hres = stat_eval(ctx, block->statement, rt, &val);
1366 scope_pop(&ctx->exec_ctx->scope_chain);
1370 jsdisp_release(var_disp);
1373 VariantClear(&ex);
1374 if(FAILED(hres))
1375 return hres;
1377 *ret = val;
1378 return S_OK;
1381 /* ECMA-262 3rd Edition 12.14 */
1382 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1384 try_statement_t *stat = (try_statement_t*)_stat;
1385 VARIANT val;
1386 HRESULT hres;
1388 TRACE("\n");
1390 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1391 if(FAILED(hres)) {
1392 TRACE("EXCEPTION\n");
1393 if(!stat->catch_block)
1394 return hres;
1396 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1397 if(FAILED(hres))
1398 return hres;
1401 if(stat->finally_statement) {
1402 VariantClear(&val);
1403 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1404 if(FAILED(hres))
1405 return hres;
1408 *ret = val;
1409 return S_OK;
1412 /* ECMA-262 3rd Edition 13 */
1413 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1415 function_expression_t *expr = (function_expression_t*)_expr;
1416 VARIANT var;
1417 HRESULT hres;
1419 TRACE("\n");
1421 if(expr->identifier) {
1422 hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1423 if(FAILED(hres))
1424 return hres;
1425 }else {
1426 jsdisp_t *dispex;
1428 hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1429 expr->src_str, expr->src_len, &dispex);
1430 if(FAILED(hres))
1431 return hres;
1433 var_set_jsdisp(&var, dispex);
1436 ret->type = EXPRVAL_VARIANT;
1437 ret->u.var = var;
1438 return S_OK;
1441 /* ECMA-262 3rd Edition 11.2.1 */
1442 HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1444 binary_expression_t *expr = (binary_expression_t*)_expr;
1445 exprval_t exprval;
1446 VARIANT member, val;
1447 DISPID id;
1448 BSTR str;
1449 IDispatch *obj = NULL;
1450 HRESULT hres;
1452 TRACE("\n");
1454 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1455 if(FAILED(hres))
1456 return hres;
1458 hres = exprval_to_value(ctx, &exprval, ei, &member);
1459 exprval_release(&exprval);
1460 if(FAILED(hres))
1461 return hres;
1463 hres = expr_eval(ctx, expr->expression2, EXPR_NEWREF, ei, &exprval);
1464 if(SUCCEEDED(hres)) {
1465 hres = exprval_to_value(ctx, &exprval, ei, &val);
1466 exprval_release(&exprval);
1469 if(SUCCEEDED(hres)) {
1470 hres = to_object(ctx, &member, &obj);
1471 if(FAILED(hres))
1472 VariantClear(&val);
1474 VariantClear(&member);
1475 if(SUCCEEDED(hres)) {
1476 hres = to_string(ctx, &val, ei, &str);
1477 VariantClear(&val);
1478 if(SUCCEEDED(hres)) {
1479 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1480 SysFreeString(str);
1483 if(SUCCEEDED(hres)) {
1484 exprval_set_idref(ret, obj, id);
1485 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1486 exprval_init(ret);
1487 hres = S_OK;
1490 IDispatch_Release(obj);
1493 return hres;
1496 /* ECMA-262 3rd Edition 11.2.1 */
1497 static HRESULT interp_array(exec_ctx_t *ctx)
1499 VARIANT v, *namev;
1500 IDispatch *obj;
1501 DISPID id;
1502 BSTR name;
1503 HRESULT hres;
1505 TRACE("\n");
1507 namev = stack_pop(ctx);
1509 hres = stack_pop_object(ctx, &obj);
1510 if(FAILED(hres)) {
1511 VariantClear(namev);
1512 return hres;
1515 hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1516 VariantClear(namev);
1517 if(FAILED(hres)) {
1518 IDispatch_Release(obj);
1519 return hres;
1522 hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
1523 SysFreeString(name);
1524 if(SUCCEEDED(hres)) {
1525 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1526 }else if(hres == DISP_E_UNKNOWNNAME) {
1527 V_VT(&v) = VT_EMPTY;
1528 hres = S_OK;
1530 IDispatch_Release(obj);
1531 if(FAILED(hres))
1532 return hres;
1534 return stack_push(ctx, &v);
1537 /* ECMA-262 3rd Edition 11.2.1 */
1538 HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1540 member_expression_t *expr = (member_expression_t*)_expr;
1541 IDispatch *obj = NULL;
1542 exprval_t exprval;
1543 VARIANT member;
1544 DISPID id;
1545 BSTR str;
1546 HRESULT hres;
1548 TRACE("\n");
1550 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1551 if(FAILED(hres))
1552 return hres;
1554 hres = exprval_to_value(ctx, &exprval, ei, &member);
1555 exprval_release(&exprval);
1556 if(FAILED(hres))
1557 return hres;
1559 hres = to_object(ctx, &member, &obj);
1560 VariantClear(&member);
1561 if(FAILED(hres))
1562 return hres;
1564 str = SysAllocString(expr->identifier);
1565 if(!str) {
1566 IDispatch_Release(obj);
1567 return E_OUTOFMEMORY;
1570 hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1571 SysFreeString(str);
1572 if(SUCCEEDED(hres)) {
1573 exprval_set_idref(ret, obj, id);
1574 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1575 exprval_init(ret);
1576 hres = S_OK;
1579 IDispatch_Release(obj);
1580 return hres;
1583 /* ECMA-262 3rd Edition 11.2.1 */
1584 static HRESULT interp_member(exec_ctx_t *ctx)
1586 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1587 IDispatch *obj;
1588 VARIANT v;
1589 DISPID id;
1590 HRESULT hres;
1592 TRACE("\n");
1594 hres = stack_pop_object(ctx, &obj);
1595 if(FAILED(hres))
1596 return hres;
1598 hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
1599 if(SUCCEEDED(hres)) {
1600 V_VT(&v) = VT_EMPTY;
1601 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1602 }else if(hres == DISP_E_UNKNOWNNAME) {
1603 V_VT(&v) = VT_EMPTY;
1604 hres = S_OK;
1606 IDispatch_Release(obj);
1607 if(FAILED(hres))
1608 return hres;
1610 return stack_push(ctx, &v);
1613 /* ECMA-262 3rd Edition 11.2.1 */
1614 static HRESULT interp_memberid(exec_ctx_t *ctx)
1616 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1617 VARIANT *objv, *namev;
1618 IDispatch *obj;
1619 BSTR name;
1620 DISPID id;
1621 HRESULT hres;
1623 TRACE("%x\n", arg);
1625 namev = stack_pop(ctx);
1626 objv = stack_pop(ctx);
1628 hres = to_object(ctx->parser->script, objv, &obj);
1629 VariantClear(objv);
1630 if(SUCCEEDED(hres)) {
1631 hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1632 if(FAILED(hres))
1633 IDispatch_Release(obj);
1635 VariantClear(namev);
1636 if(FAILED(hres))
1637 return hres;
1639 hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
1640 SysFreeString(name);
1641 if(FAILED(hres)) {
1642 IDispatch_Release(obj);
1643 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1644 obj = NULL;
1645 id = JS_E_INVALID_PROPERTY;
1646 }else {
1647 return hres;
1651 return stack_push_objid(ctx, obj, id);
1654 /* ECMA-262 3rd Edition 11.2.1 */
1655 static HRESULT interp_refval(exec_ctx_t *ctx)
1657 IDispatch *disp;
1658 VARIANT v;
1659 DISPID id;
1660 HRESULT hres;
1662 TRACE("\n");
1664 disp = stack_topn_objid(ctx, 0, &id);
1665 if(!disp)
1666 return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1668 hres = disp_propget(ctx->parser->script, disp, id, &v, &ctx->ei, NULL/*FIXME*/);
1669 if(FAILED(hres))
1670 return hres;
1672 return stack_push(ctx, &v);
1675 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1677 VARIANT tmp;
1678 unsigned i;
1680 dp->cArgs = arg_cnt;
1681 dp->rgdispidNamedArgs = NULL;
1682 dp->cNamedArgs = 0;
1684 assert(ctx->top >= arg_cnt);
1686 for(i=1; i*2 <= arg_cnt; i++) {
1687 tmp = ctx->stack[ctx->top-i];
1688 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1689 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1692 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1695 /* ECMA-262 3rd Edition 11.2.2 */
1696 static HRESULT interp_new(exec_ctx_t *ctx)
1698 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1699 VARIANT *constr, v;
1700 DISPPARAMS dp;
1701 HRESULT hres;
1703 TRACE("%d\n", arg);
1705 constr = stack_topn(ctx, arg);
1707 /* NOTE: Should use to_object here */
1709 if(V_VT(constr) == VT_NULL)
1710 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1711 else if(V_VT(constr) != VT_DISPATCH)
1712 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1713 else if(!V_DISPATCH(constr))
1714 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1716 jsstack_to_dp(ctx, arg, &dp);
1717 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1718 DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1719 if(FAILED(hres))
1720 return hres;
1722 stack_popn(ctx, arg+1);
1723 return stack_push(ctx, &v);
1726 /* ECMA-262 3rd Edition 11.2.3 */
1727 static HRESULT interp_call(exec_ctx_t *ctx)
1729 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1730 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1731 VARIANT v, *objv;
1732 DISPPARAMS dp;
1733 HRESULT hres;
1735 TRACE("%d %d\n", argn, do_ret);
1737 objv = stack_topn(ctx, argn);
1738 if(V_VT(objv) != VT_DISPATCH)
1739 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1741 jsstack_to_dp(ctx, argn, &dp);
1742 hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1743 do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1744 if(FAILED(hres))
1745 return hres;
1747 stack_popn(ctx, argn+1);
1748 return do_ret ? stack_push(ctx, &v) : S_OK;
1752 /* ECMA-262 3rd Edition 11.2.3 */
1753 static HRESULT interp_call_member(exec_ctx_t *ctx)
1755 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1756 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1757 IDispatch *obj;
1758 DISPPARAMS dp;
1759 VARIANT v;
1760 DISPID id;
1761 HRESULT hres;
1763 TRACE("%d %d\n", argn, do_ret);
1765 obj = stack_topn_objid(ctx, argn, &id);
1766 if(!obj)
1767 return throw_type_error(ctx->parser->script, &ctx->ei, id, NULL);
1769 jsstack_to_dp(ctx, argn, &dp);
1770 hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1771 if(FAILED(hres))
1772 return hres;
1774 stack_popn(ctx, argn+2);
1775 return do_ret ? stack_push(ctx, &v) : S_OK;
1779 /* ECMA-262 3rd Edition 11.1.1 */
1780 static HRESULT interp_this(exec_ctx_t *ctx)
1782 VARIANT v;
1784 TRACE("\n");
1786 V_VT(&v) = VT_DISPATCH;
1787 V_DISPATCH(&v) = ctx->this_obj;
1788 IDispatch_AddRef(ctx->this_obj);
1789 return stack_push(ctx, &v);
1792 /* ECMA-262 3rd Edition 10.1.4 */
1793 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1795 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1796 BSTR identifier;
1797 HRESULT hres;
1799 TRACE("\n");
1801 identifier = SysAllocString(expr->identifier);
1802 if(!identifier)
1803 return E_OUTOFMEMORY;
1805 hres = identifier_eval(ctx, identifier, flags, ei, ret);
1807 SysFreeString(identifier);
1808 return hres;
1811 /* ECMA-262 3rd Edition 10.1.4 */
1812 static HRESULT interp_ident(exec_ctx_t *ctx)
1814 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1815 exprval_t exprval;
1816 VARIANT v;
1817 HRESULT hres;
1819 TRACE("%s\n", debugstr_w(arg));
1821 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1822 if(FAILED(hres))
1823 return hres;
1825 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1826 exprval_release(&exprval);
1827 if(FAILED(hres))
1828 return hres;
1830 return stack_push(ctx, &v);
1833 /* ECMA-262 3rd Edition 10.1.4 */
1834 static HRESULT interp_identid(exec_ctx_t *ctx)
1836 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1837 const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1838 exprval_t exprval;
1839 HRESULT hres;
1841 TRACE("%s %x\n", debugstr_w(arg), flags);
1843 hres = identifier_eval(ctx->parser->script, arg, (flags&fdexNameEnsure) ? EXPR_NEWREF : 0, &ctx->ei, &exprval);
1844 if(FAILED(hres))
1845 return hres;
1847 if(exprval.type != EXPRVAL_IDREF) {
1848 WARN("invalid ref\n");
1849 exprval_release(&exprval);
1850 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1853 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1856 /* ECMA-262 3rd Edition 7.8.1 */
1857 static HRESULT interp_null(exec_ctx_t *ctx)
1859 VARIANT v;
1861 TRACE("\n");
1863 V_VT(&v) = VT_NULL;
1864 return stack_push(ctx, &v);
1867 /* ECMA-262 3rd Edition 7.8.2 */
1868 static HRESULT interp_bool(exec_ctx_t *ctx)
1870 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1872 TRACE("%s\n", arg ? "true" : "false");
1874 return stack_push_bool(ctx, arg);
1877 /* ECMA-262 3rd Edition 7.8.3 */
1878 static HRESULT interp_int(exec_ctx_t *ctx)
1880 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1881 VARIANT v;
1883 TRACE("%d\n", arg);
1885 V_VT(&v) = VT_I4;
1886 V_I4(&v) = arg;
1887 return stack_push(ctx, &v);
1890 /* ECMA-262 3rd Edition 7.8.3 */
1891 static HRESULT interp_double(exec_ctx_t *ctx)
1893 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1894 VARIANT v;
1896 TRACE("%lf\n", arg);
1898 V_VT(&v) = VT_R8;
1899 V_R8(&v) = arg;
1900 return stack_push(ctx, &v);
1903 /* ECMA-262 3rd Edition 7.8.4 */
1904 static HRESULT interp_str(exec_ctx_t *ctx)
1906 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1907 VARIANT v;
1909 TRACE("%s\n", debugstr_w(str));
1911 V_VT(&v) = VT_BSTR;
1912 V_BSTR(&v) = SysAllocString(str);
1913 if(!V_BSTR(&v))
1914 return E_OUTOFMEMORY;
1916 return stack_push(ctx, &v);
1919 /* ECMA-262 3rd Edition 7.8 */
1920 static HRESULT interp_regexp(exec_ctx_t *ctx)
1922 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1923 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1924 jsdisp_t *regexp;
1925 VARIANT v;
1926 HRESULT hres;
1928 TRACE("%s %x\n", debugstr_w(source), flags);
1930 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, &regexp);
1931 if(FAILED(hres))
1932 return hres;
1934 var_set_jsdisp(&v, regexp);
1935 return stack_push(ctx, &v);
1938 /* ECMA-262 3rd Edition 11.1.4 */
1939 static HRESULT interp_carray(exec_ctx_t *ctx)
1941 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1942 jsdisp_t *array;
1943 VARIANT *v, r;
1944 unsigned i;
1945 HRESULT hres;
1947 TRACE("%u\n", arg);
1949 hres = create_array(ctx->parser->script, arg, &array);
1950 if(FAILED(hres))
1951 return hres;
1953 i = arg;
1954 while(i--) {
1955 v = stack_pop(ctx);
1956 hres = jsdisp_propput_idx(array, i, v, &ctx->ei, NULL/*FIXME*/);
1957 VariantClear(v);
1958 if(FAILED(hres)) {
1959 jsdisp_release(array);
1960 return hres;
1964 var_set_jsdisp(&r, array);
1965 return stack_push(ctx, &r);
1968 /* ECMA-262 3rd Edition 11.1.5 */
1969 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1971 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1972 VARIANT val, tmp;
1973 jsdisp_t *obj;
1974 prop_val_t *iter;
1975 exprval_t exprval;
1976 BSTR name;
1977 HRESULT hres;
1979 TRACE("\n");
1981 hres = create_object(ctx, NULL, &obj);
1982 if(FAILED(hres))
1983 return hres;
1985 for(iter = expr->property_list; iter; iter = iter->next) {
1986 hres = literal_to_var(ctx, iter->name, &tmp);
1987 if(FAILED(hres))
1988 break;
1990 hres = to_string(ctx, &tmp, ei, &name);
1991 VariantClear(&tmp);
1992 if(FAILED(hres))
1993 break;
1995 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1996 if(SUCCEEDED(hres)) {
1997 hres = exprval_to_value(ctx, &exprval, ei, &val);
1998 exprval_release(&exprval);
1999 if(SUCCEEDED(hres)) {
2000 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
2001 VariantClear(&val);
2005 SysFreeString(name);
2006 if(FAILED(hres))
2007 break;
2010 if(FAILED(hres)) {
2011 jsdisp_release(obj);
2012 return hres;
2015 ret->type = EXPRVAL_VARIANT;
2016 var_set_jsdisp(&ret->u.var, obj);
2017 return S_OK;
2020 /* ECMA-262 3rd Edition 11.11 */
2021 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
2023 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2024 VARIANT_BOOL b;
2025 HRESULT hres;
2027 TRACE("\n");
2029 hres = to_boolean(stack_top(ctx), &b);
2030 if(FAILED(hres))
2031 return hres;
2033 if(b) {
2034 ctx->ip = arg;
2035 }else {
2036 stack_popn(ctx, 1);
2037 ctx->ip++;
2039 return S_OK;
2042 /* ECMA-262 3rd Edition 11.11 */
2043 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2045 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2046 VARIANT_BOOL b;
2047 HRESULT hres;
2049 TRACE("\n");
2051 hres = to_boolean(stack_top(ctx), &b);
2052 if(FAILED(hres))
2053 return hres;
2055 if(b) {
2056 stack_popn(ctx, 1);
2057 ctx->ip++;
2058 }else {
2059 ctx->ip = arg;
2061 return S_OK;
2064 /* ECMA-262 3rd Edition 11.10 */
2065 static HRESULT interp_or(exec_ctx_t *ctx)
2067 INT l, r;
2068 HRESULT hres;
2070 TRACE("\n");
2072 hres = stack_pop_int(ctx, &r);
2073 if(FAILED(hres))
2074 return hres;
2076 hres = stack_pop_int(ctx, &l);
2077 if(FAILED(hres))
2078 return hres;
2080 return stack_push_int(ctx, l|r);
2083 /* ECMA-262 3rd Edition 11.10 */
2084 static HRESULT interp_xor(exec_ctx_t *ctx)
2086 INT l, r;
2087 HRESULT hres;
2089 TRACE("\n");
2091 hres = stack_pop_int(ctx, &r);
2092 if(FAILED(hres))
2093 return hres;
2095 hres = stack_pop_int(ctx, &l);
2096 if(FAILED(hres))
2097 return hres;
2099 return stack_push_int(ctx, l^r);
2102 /* ECMA-262 3rd Edition 11.10 */
2103 static HRESULT interp_and(exec_ctx_t *ctx)
2105 INT l, r;
2106 HRESULT hres;
2108 TRACE("\n");
2110 hres = stack_pop_int(ctx, &r);
2111 if(FAILED(hres))
2112 return hres;
2114 hres = stack_pop_int(ctx, &l);
2115 if(FAILED(hres))
2116 return hres;
2118 return stack_push_int(ctx, l&r);
2121 /* ECMA-262 3rd Edition 11.8.6 */
2122 static HRESULT interp_instanceof(exec_ctx_t *ctx)
2124 jsdisp_t *obj, *iter, *tmp = NULL;
2125 VARIANT prot, *v;
2126 BOOL ret = FALSE;
2127 HRESULT hres;
2129 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2131 v = stack_pop(ctx);
2132 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
2133 VariantClear(v);
2134 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
2137 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
2138 IDispatch_Release(V_DISPATCH(v));
2139 if(!obj) {
2140 FIXME("non-jsdisp objects not supported\n");
2141 return E_FAIL;
2144 if(is_class(obj, JSCLASS_FUNCTION)) {
2145 hres = jsdisp_propget_name(obj, prototypeW, &prot, &ctx->ei, NULL/*FIXME*/);
2146 }else {
2147 hres = throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
2149 jsdisp_release(obj);
2150 if(FAILED(hres))
2151 return hres;
2153 v = stack_pop(ctx);
2155 if(V_VT(&prot) == VT_DISPATCH) {
2156 if(V_VT(v) == VT_DISPATCH)
2157 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
2158 for(iter = tmp; !ret && iter; iter = iter->prototype) {
2159 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
2160 if(FAILED(hres))
2161 break;
2164 if(tmp)
2165 jsdisp_release(tmp);
2166 }else {
2167 FIXME("prototype is not an object\n");
2168 hres = E_FAIL;
2171 VariantClear(&prot);
2172 VariantClear(v);
2173 if(FAILED(hres))
2174 return hres;
2176 return stack_push_bool(ctx, ret);
2179 /* ECMA-262 3rd Edition 11.8.7 */
2180 static HRESULT interp_in(exec_ctx_t *ctx)
2182 VARIANT *obj, *v;
2183 DISPID id = 0;
2184 BOOL ret;
2185 BSTR str;
2186 HRESULT hres;
2188 TRACE("\n");
2190 obj = stack_pop(ctx);
2191 v = stack_pop(ctx);
2193 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2194 VariantClear(obj);
2195 VariantClear(v);
2196 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2199 hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2200 VariantClear(v);
2201 if(FAILED(hres)) {
2202 IDispatch_Release(V_DISPATCH(obj));
2203 return hres;
2206 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2207 IDispatch_Release(V_DISPATCH(obj));
2208 SysFreeString(str);
2209 if(SUCCEEDED(hres))
2210 ret = TRUE;
2211 else if(hres == DISP_E_UNKNOWNNAME)
2212 ret = FALSE;
2213 else
2214 return hres;
2216 return stack_push_bool(ctx, ret);
2219 /* ECMA-262 3rd Edition 11.6.1 */
2220 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2222 VARIANT r, l;
2223 HRESULT hres;
2225 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2226 if(FAILED(hres))
2227 return hres;
2229 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2230 if(FAILED(hres)) {
2231 VariantClear(&l);
2232 return hres;
2235 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2236 BSTR lstr = NULL, rstr = NULL;
2238 if(V_VT(&l) == VT_BSTR)
2239 lstr = V_BSTR(&l);
2240 else
2241 hres = to_string(ctx, &l, ei, &lstr);
2243 if(SUCCEEDED(hres)) {
2244 if(V_VT(&r) == VT_BSTR)
2245 rstr = V_BSTR(&r);
2246 else
2247 hres = to_string(ctx, &r, ei, &rstr);
2250 if(SUCCEEDED(hres)) {
2251 int len1, len2;
2253 len1 = SysStringLen(lstr);
2254 len2 = SysStringLen(rstr);
2256 V_VT(retv) = VT_BSTR;
2257 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2258 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2259 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2262 if(V_VT(&l) != VT_BSTR)
2263 SysFreeString(lstr);
2264 if(V_VT(&r) != VT_BSTR)
2265 SysFreeString(rstr);
2266 }else {
2267 VARIANT nl, nr;
2269 hres = to_number(ctx, &l, ei, &nl);
2270 if(SUCCEEDED(hres)) {
2271 hres = to_number(ctx, &r, ei, &nr);
2272 if(SUCCEEDED(hres))
2273 num_set_val(retv, num_val(&nl) + num_val(&nr));
2277 VariantClear(&r);
2278 VariantClear(&l);
2279 return hres;
2282 /* ECMA-262 3rd Edition 11.6.1 */
2283 static HRESULT interp_add(exec_ctx_t *ctx)
2285 VARIANT *l, *r, ret;
2286 HRESULT hres;
2288 r = stack_pop(ctx);
2289 l = stack_pop(ctx);
2291 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2293 hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2294 VariantClear(l);
2295 VariantClear(r);
2296 if(FAILED(hres))
2297 return hres;
2299 return stack_push(ctx, &ret);
2302 /* ECMA-262 3rd Edition 11.6.2 */
2303 static HRESULT interp_sub(exec_ctx_t *ctx)
2305 VARIANT l, r;
2306 HRESULT hres;
2308 TRACE("\n");
2310 hres = stack_pop_number(ctx, &r);
2311 if(FAILED(hres))
2312 return hres;
2314 hres = stack_pop_number(ctx, &l);
2315 if(FAILED(hres))
2316 return hres;
2318 return stack_push_number(ctx, num_val(&l)-num_val(&r));
2321 /* ECMA-262 3rd Edition 11.5.1 */
2322 static HRESULT interp_mul(exec_ctx_t *ctx)
2324 VARIANT l, r;
2325 HRESULT hres;
2327 TRACE("\n");
2329 hres = stack_pop_number(ctx, &r);
2330 if(FAILED(hres))
2331 return hres;
2333 hres = stack_pop_number(ctx, &l);
2334 if(FAILED(hres))
2335 return hres;
2337 return stack_push_number(ctx, num_val(&l)*num_val(&r));
2340 /* ECMA-262 3rd Edition 11.5.2 */
2341 static HRESULT interp_div(exec_ctx_t *ctx)
2343 VARIANT l, r;
2344 HRESULT hres;
2346 TRACE("\n");
2348 hres = stack_pop_number(ctx, &r);
2349 if(FAILED(hres))
2350 return hres;
2352 hres = stack_pop_number(ctx, &l);
2353 if(FAILED(hres))
2354 return hres;
2356 return stack_push_number(ctx, num_val(&l)/num_val(&r));
2359 /* ECMA-262 3rd Edition 11.5.3 */
2360 static HRESULT interp_mod(exec_ctx_t *ctx)
2362 VARIANT l, r;
2363 HRESULT hres;
2365 TRACE("\n");
2367 hres = stack_pop_number(ctx, &r);
2368 if(FAILED(hres))
2369 return hres;
2371 hres = stack_pop_number(ctx, &l);
2372 if(FAILED(hres))
2373 return hres;
2375 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2378 /* ECMA-262 3rd Edition 11.4.2 */
2379 static HRESULT interp_delete(exec_ctx_t *ctx)
2381 VARIANT *obj_var, *name_var;
2382 IDispatchEx *dispex;
2383 IDispatch *obj;
2384 BSTR name;
2385 BOOL ret;
2386 HRESULT hres;
2388 TRACE("\n");
2390 name_var = stack_pop(ctx);
2391 obj_var = stack_pop(ctx);
2393 hres = to_object(ctx->parser->script, obj_var, &obj);
2394 VariantClear(obj_var);
2395 if(FAILED(hres)) {
2396 VariantClear(name_var);
2397 return hres;
2400 hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2401 VariantClear(name_var);
2402 if(FAILED(hres)) {
2403 IDispatch_Release(obj);
2404 return hres;
2407 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2408 if(SUCCEEDED(hres)) {
2409 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2410 ret = TRUE;
2411 IDispatchEx_Release(dispex);
2412 }else {
2413 hres = S_OK;
2414 ret = FALSE;
2417 IDispatch_Release(obj);
2418 SysFreeString(name);
2419 if(FAILED(hres))
2420 return hres;
2422 return stack_push_bool(ctx, ret);
2425 /* ECMA-262 3rd Edition 11.4.2 */
2426 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
2428 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2429 IDispatchEx *dispex;
2430 exprval_t exprval;
2431 BOOL ret = FALSE;
2432 HRESULT hres;
2434 TRACE("%s\n", debugstr_w(arg));
2436 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2437 if(FAILED(hres))
2438 return hres;
2440 if(exprval.type != EXPRVAL_IDREF) {
2441 FIXME("Unsupported exprval\n");
2442 exprval_release(&exprval);
2443 return E_NOTIMPL;
2446 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2447 IDispatch_Release(exprval.u.idref.disp);
2448 if(SUCCEEDED(hres)) {
2449 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2450 IDispatchEx_Release(dispex);
2451 if(FAILED(hres))
2452 return hres;
2454 ret = TRUE;
2457 return stack_push_bool(ctx, ret);
2460 /* ECMA-262 3rd Edition 11.4.2 */
2461 static HRESULT interp_void(exec_ctx_t *ctx)
2463 VARIANT v;
2465 TRACE("\n");
2467 stack_popn(ctx, 1);
2469 V_VT(&v) = VT_EMPTY;
2470 return stack_push(ctx, &v);
2473 /* ECMA-262 3rd Edition 11.4.3 */
2474 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
2476 switch(V_VT(v)) {
2477 case VT_EMPTY:
2478 *ret = undefinedW;
2479 break;
2480 case VT_NULL:
2481 *ret = objectW;
2482 break;
2483 case VT_BOOL:
2484 *ret = booleanW;
2485 break;
2486 case VT_I4:
2487 case VT_R8:
2488 *ret = numberW;
2489 break;
2490 case VT_BSTR:
2491 *ret = stringW;
2492 break;
2493 case VT_DISPATCH: {
2494 jsdisp_t *dispex;
2496 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
2497 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2498 jsdisp_release(dispex);
2499 }else {
2500 *ret = objectW;
2502 break;
2504 default:
2505 FIXME("unhandled vt %d\n", V_VT(v));
2506 return E_NOTIMPL;
2509 return S_OK;
2512 /* ECMA-262 3rd Edition 11.4.3 */
2513 static HRESULT interp_typeofid(exec_ctx_t *ctx)
2515 const WCHAR *ret;
2516 IDispatch *obj;
2517 VARIANT v;
2518 DISPID id;
2519 HRESULT hres;
2521 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2523 TRACE("\n");
2525 obj = stack_pop_objid(ctx, &id);
2526 if(!obj)
2527 return stack_push_string(ctx, undefinedW);
2529 V_VT(&v) = VT_EMPTY;
2530 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2531 IDispatch_Release(obj);
2532 if(FAILED(hres))
2533 return stack_push_string(ctx, unknownW);
2535 hres = typeof_string(&v, &ret);
2536 VariantClear(&v);
2537 if(FAILED(hres))
2538 return hres;
2540 return stack_push_string(ctx, ret);
2543 /* ECMA-262 3rd Edition 11.4.3 */
2544 static HRESULT interp_typeofident(exec_ctx_t *ctx)
2546 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2547 exprval_t exprval;
2548 const WCHAR *ret;
2549 VARIANT v;
2550 HRESULT hres;
2552 TRACE("%s\n", debugstr_w(arg));
2554 hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2555 if(FAILED(hres))
2556 return hres;
2558 if(exprval.type == EXPRVAL_INVALID) {
2559 hres = stack_push_string(ctx, undefinedW);
2560 exprval_release(&exprval);
2561 return hres;
2564 hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
2565 exprval_release(&exprval);
2566 if(FAILED(hres))
2567 return hres;
2569 hres = typeof_string(&v, &ret);
2570 VariantClear(&v);
2571 if(FAILED(hres))
2572 return hres;
2574 return stack_push_string(ctx, ret);
2577 /* ECMA-262 3rd Edition 11.4.3 */
2578 static HRESULT interp_typeof(exec_ctx_t *ctx)
2580 const WCHAR *ret;
2581 VARIANT *v;
2582 HRESULT hres;
2584 TRACE("\n");
2586 v = stack_pop(ctx);
2587 hres = typeof_string(v, &ret);
2588 VariantClear(v);
2589 if(FAILED(hres))
2590 return hres;
2592 return stack_push_string(ctx, ret);
2595 /* ECMA-262 3rd Edition 11.4.7 */
2596 static HRESULT interp_minus(exec_ctx_t *ctx)
2598 VARIANT n;
2599 HRESULT hres;
2601 TRACE("\n");
2603 hres = stack_pop_number(ctx, &n);
2604 if(FAILED(hres))
2605 return hres;
2607 return stack_push_number(ctx, -num_val(&n));
2610 /* ECMA-262 3rd Edition 11.4.6 */
2611 static HRESULT interp_tonum(exec_ctx_t *ctx)
2613 VARIANT *v, num;
2614 HRESULT hres;
2616 TRACE("\n");
2618 v = stack_pop(ctx);
2619 hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2620 VariantClear(v);
2621 if(FAILED(hres))
2622 return hres;
2624 return stack_push(ctx, &num);
2627 /* ECMA-262 3rd Edition 11.3.1 */
2628 static HRESULT interp_postinc(exec_ctx_t *ctx)
2630 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2631 IDispatch *obj;
2632 DISPID id;
2633 VARIANT v;
2634 HRESULT hres;
2636 TRACE("%d\n", arg);
2638 obj = stack_pop_objid(ctx, &id);
2639 if(!obj)
2640 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2642 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2643 if(SUCCEEDED(hres)) {
2644 VARIANT n, inc;
2646 hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2647 if(SUCCEEDED(hres)) {
2648 num_set_val(&inc, num_val(&n)+(double)arg);
2649 hres = disp_propput(ctx->parser->script, obj, id, &inc, &ctx->ei, NULL/*FIXME*/);
2651 if(FAILED(hres))
2652 VariantClear(&v);
2654 IDispatch_Release(obj);
2655 if(FAILED(hres))
2656 return hres;
2658 return stack_push(ctx, &v);
2661 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2662 static HRESULT interp_preinc(exec_ctx_t *ctx)
2664 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2665 IDispatch *obj;
2666 DISPID id;
2667 VARIANT v;
2668 HRESULT hres;
2670 TRACE("%d\n", arg);
2672 obj = stack_pop_objid(ctx, &id);
2673 if(!obj)
2674 return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2676 hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2677 if(SUCCEEDED(hres)) {
2678 VARIANT n;
2680 hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2681 VariantClear(&v);
2682 if(SUCCEEDED(hres)) {
2683 num_set_val(&v, num_val(&n)+(double)arg);
2684 hres = disp_propput(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2687 IDispatch_Release(obj);
2688 if(FAILED(hres))
2689 return hres;
2691 return stack_push(ctx, &v);
2694 /* ECMA-262 3rd Edition 11.9.3 */
2695 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2697 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2698 return equal2_values(lval, rval, ret);
2700 /* FIXME: NULL disps should be handled in more general way */
2701 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2702 VARIANT v;
2703 V_VT(&v) = VT_NULL;
2704 return equal_values(ctx, &v, rval, ei, ret);
2707 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2708 VARIANT v;
2709 V_VT(&v) = VT_NULL;
2710 return equal_values(ctx, lval, &v, ei, ret);
2713 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2714 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2715 *ret = TRUE;
2716 return S_OK;
2719 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2720 VARIANT v;
2721 HRESULT hres;
2723 hres = to_number(ctx, lval, ei, &v);
2724 if(FAILED(hres))
2725 return hres;
2727 return equal_values(ctx, &v, rval, ei, ret);
2730 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2731 VARIANT v;
2732 HRESULT hres;
2734 hres = to_number(ctx, rval, ei, &v);
2735 if(FAILED(hres))
2736 return hres;
2738 return equal_values(ctx, lval, &v, ei, ret);
2741 if(V_VT(rval) == VT_BOOL) {
2742 VARIANT v;
2744 V_VT(&v) = VT_I4;
2745 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2746 return equal_values(ctx, lval, &v, ei, ret);
2749 if(V_VT(lval) == VT_BOOL) {
2750 VARIANT v;
2752 V_VT(&v) = VT_I4;
2753 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2754 return equal_values(ctx, &v, rval, ei, ret);
2758 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2759 VARIANT v;
2760 HRESULT hres;
2762 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2763 if(FAILED(hres))
2764 return hres;
2766 hres = equal_values(ctx, lval, &v, ei, ret);
2768 VariantClear(&v);
2769 return hres;
2773 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2774 VARIANT v;
2775 HRESULT hres;
2777 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2778 if(FAILED(hres))
2779 return hres;
2781 hres = equal_values(ctx, &v, rval, ei, ret);
2783 VariantClear(&v);
2784 return hres;
2788 *ret = FALSE;
2789 return S_OK;
2792 /* ECMA-262 3rd Edition 11.9.1 */
2793 static HRESULT interp_eq(exec_ctx_t *ctx)
2795 VARIANT *l, *r;
2796 BOOL b;
2797 HRESULT hres;
2799 r = stack_pop(ctx);
2800 l = stack_pop(ctx);
2802 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2804 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2805 VariantClear(l);
2806 VariantClear(r);
2807 if(FAILED(hres))
2808 return hres;
2810 return stack_push_bool(ctx, b);
2813 /* ECMA-262 3rd Edition 11.9.2 */
2814 static HRESULT interp_neq(exec_ctx_t *ctx)
2816 VARIANT *l, *r;
2817 BOOL b;
2818 HRESULT hres;
2820 r = stack_pop(ctx);
2821 l = stack_pop(ctx);
2823 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2825 hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2826 VariantClear(l);
2827 VariantClear(r);
2828 if(FAILED(hres))
2829 return hres;
2831 return stack_push_bool(ctx, !b);
2834 /* ECMA-262 3rd Edition 11.9.4 */
2835 static HRESULT interp_eq2(exec_ctx_t *ctx)
2837 VARIANT *l, *r;
2838 BOOL b;
2839 HRESULT hres;
2841 TRACE("\n");
2843 r = stack_pop(ctx);
2844 l = stack_pop(ctx);
2846 hres = equal2_values(r, l, &b);
2847 VariantClear(l);
2848 VariantClear(r);
2849 if(FAILED(hres))
2850 return hres;
2852 return stack_push_bool(ctx, b);
2855 /* ECMA-262 3rd Edition 11.9.5 */
2856 static HRESULT interp_neq2(exec_ctx_t *ctx)
2858 VARIANT *l, *r;
2859 BOOL b;
2860 HRESULT hres;
2862 TRACE("\n");
2864 r = stack_pop(ctx);
2865 l = stack_pop(ctx);
2867 hres = equal2_values(r, l, &b);
2868 VariantClear(l);
2869 VariantClear(r);
2870 if(FAILED(hres))
2871 return hres;
2873 return stack_push_bool(ctx, !b);
2876 /* ECMA-262 3rd Edition 11.8.5 */
2877 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2879 VARIANT l, r, ln, rn;
2880 HRESULT hres;
2882 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2883 if(FAILED(hres))
2884 return hres;
2886 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2887 if(FAILED(hres)) {
2888 VariantClear(&l);
2889 return hres;
2892 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2893 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2894 SysFreeString(V_BSTR(&l));
2895 SysFreeString(V_BSTR(&r));
2896 return S_OK;
2899 hres = to_number(ctx, &l, ei, &ln);
2900 VariantClear(&l);
2901 if(SUCCEEDED(hres))
2902 hres = to_number(ctx, &r, ei, &rn);
2903 VariantClear(&r);
2904 if(FAILED(hres))
2905 return hres;
2907 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2908 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2909 }else {
2910 DOUBLE ld = num_val(&ln);
2911 DOUBLE rd = num_val(&rn);
2913 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2916 return S_OK;
2919 /* ECMA-262 3rd Edition 11.8.1 */
2920 static HRESULT interp_lt(exec_ctx_t *ctx)
2922 VARIANT *l, *r;
2923 BOOL b;
2924 HRESULT hres;
2926 r = stack_pop(ctx);
2927 l = stack_pop(ctx);
2929 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2931 hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
2932 VariantClear(l);
2933 VariantClear(r);
2934 if(FAILED(hres))
2935 return hres;
2937 return stack_push_bool(ctx, b);
2940 /* ECMA-262 3rd Edition 11.8.1 */
2941 static HRESULT interp_lteq(exec_ctx_t *ctx)
2943 VARIANT *l, *r;
2944 BOOL b;
2945 HRESULT hres;
2947 r = stack_pop(ctx);
2948 l = stack_pop(ctx);
2950 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2952 hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
2953 VariantClear(l);
2954 VariantClear(r);
2955 if(FAILED(hres))
2956 return hres;
2958 return stack_push_bool(ctx, b);
2961 /* ECMA-262 3rd Edition 11.8.2 */
2962 static HRESULT interp_gt(exec_ctx_t *ctx)
2964 VARIANT *l, *r;
2965 BOOL b;
2966 HRESULT hres;
2968 r = stack_pop(ctx);
2969 l = stack_pop(ctx);
2971 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2973 hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
2974 VariantClear(l);
2975 VariantClear(r);
2976 if(FAILED(hres))
2977 return hres;
2979 return stack_push_bool(ctx, b);
2982 /* ECMA-262 3rd Edition 11.8.4 */
2983 static HRESULT interp_gteq(exec_ctx_t *ctx)
2985 VARIANT *l, *r;
2986 BOOL b;
2987 HRESULT hres;
2989 r = stack_pop(ctx);
2990 l = stack_pop(ctx);
2992 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2994 hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
2995 VariantClear(l);
2996 VariantClear(r);
2997 if(FAILED(hres))
2998 return hres;
3000 return stack_push_bool(ctx, b);
3003 /* ECMA-262 3rd Edition 11.4.8 */
3004 static HRESULT interp_bneg(exec_ctx_t *ctx)
3006 VARIANT *v, r;
3007 INT i;
3008 HRESULT hres;
3010 TRACE("\n");
3012 v = stack_pop(ctx);
3013 hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3014 VariantClear(v);
3015 if(FAILED(hres))
3016 return hres;
3018 V_VT(&r) = VT_I4;
3019 V_I4(&r) = ~i;
3020 return stack_push(ctx, &r);
3023 /* ECMA-262 3rd Edition 11.4.9 */
3024 static HRESULT interp_neg(exec_ctx_t *ctx)
3026 VARIANT *v;
3027 VARIANT_BOOL b;
3028 HRESULT hres;
3030 TRACE("\n");
3032 v = stack_pop(ctx);
3033 hres = to_boolean(v, &b);
3034 VariantClear(v);
3035 if(FAILED(hres))
3036 return hres;
3038 return stack_push_bool(ctx, !b);
3041 /* ECMA-262 3rd Edition 11.7.1 */
3042 static HRESULT interp_lshift(exec_ctx_t *ctx)
3044 DWORD r;
3045 INT l;
3046 HRESULT hres;
3048 hres = stack_pop_uint(ctx, &r);
3049 if(FAILED(hres))
3050 return hres;
3052 hres = stack_pop_int(ctx, &l);
3053 if(FAILED(hres))
3054 return hres;
3056 return stack_push_int(ctx, l << (r&0x1f));
3059 /* ECMA-262 3rd Edition 11.7.2 */
3060 static HRESULT interp_rshift(exec_ctx_t *ctx)
3062 DWORD r;
3063 INT l;
3064 HRESULT hres;
3066 hres = stack_pop_uint(ctx, &r);
3067 if(FAILED(hres))
3068 return hres;
3070 hres = stack_pop_int(ctx, &l);
3071 if(FAILED(hres))
3072 return hres;
3074 return stack_push_int(ctx, l >> (r&0x1f));
3077 /* ECMA-262 3rd Edition 11.7.3 */
3078 static HRESULT interp_rshift2(exec_ctx_t *ctx)
3080 DWORD r, l;
3081 HRESULT hres;
3083 hres = stack_pop_uint(ctx, &r);
3084 if(FAILED(hres))
3085 return hres;
3087 hres = stack_pop_uint(ctx, &l);
3088 if(FAILED(hres))
3089 return hres;
3091 return stack_push_int(ctx, l >> (r&0x1f));
3094 /* ECMA-262 3rd Edition 11.13.1 */
3095 static HRESULT interp_assign(exec_ctx_t *ctx)
3097 IDispatch *disp;
3098 DISPID id;
3099 VARIANT *v;
3100 HRESULT hres;
3102 TRACE("\n");
3104 v = stack_pop(ctx);
3105 disp = stack_pop_objid(ctx, &id);
3107 if(!disp)
3108 return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
3110 hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
3111 IDispatch_Release(disp);
3112 if(FAILED(hres)) {
3113 VariantClear(v);
3114 return hres;
3117 return stack_push(ctx, v);
3120 static HRESULT interp_undefined(exec_ctx_t *ctx)
3122 VARIANT v;
3124 TRACE("\n");
3126 V_VT(&v) = VT_EMPTY;
3127 return stack_push(ctx, &v);
3130 static HRESULT interp_jmp(exec_ctx_t *ctx)
3132 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3134 TRACE("\n");
3136 ctx->ip = arg;
3137 return S_OK;
3140 static HRESULT interp_pop(exec_ctx_t *ctx)
3142 TRACE("\n");
3144 stack_popn(ctx, 1);
3145 return S_OK;
3148 static HRESULT interp_ret(exec_ctx_t *ctx)
3150 TRACE("\n");
3152 ctx->ip = -1;
3153 return S_OK;
3156 static HRESULT interp_tree(exec_ctx_t *ctx)
3158 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3159 exprval_t val;
3160 VARIANT v;
3161 HRESULT hres;
3163 TRACE("\n");
3165 hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3166 if(FAILED(hres))
3167 return hres;
3169 hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3170 exprval_release(&val);
3171 if(FAILED(hres))
3172 return hres;
3174 return stack_push(ctx, &v);
3177 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3179 static const op_func_t op_funcs[] = {
3180 #define X(x,a,b,c) interp_##x,
3181 OP_LIST
3182 #undef X
3185 static const unsigned op_move[] = {
3186 #define X(a,x,b,c) x,
3187 OP_LIST
3188 #undef X
3191 static HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3193 exec_ctx_t *exec_ctx = ctx->exec_ctx;
3194 unsigned prev_ip, prev_top;
3195 jsop_t op;
3196 HRESULT hres = S_OK;
3198 TRACE("\n");
3200 prev_top = exec_ctx->top;
3201 prev_ip = exec_ctx->ip;
3202 exec_ctx->ip = expr->instr_off;
3204 while(exec_ctx->ip != -1) {
3205 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3206 hres = op_funcs[op](exec_ctx);
3207 if(FAILED(hres))
3208 break;
3209 exec_ctx->ip += op_move[op];
3212 exec_ctx->ip = prev_ip;
3214 if(FAILED(hres)) {
3215 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3216 *ei = exec_ctx->ei;
3217 memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3218 return hres;
3221 assert(exec_ctx->top == prev_top+1 || ((flags&EXPR_NOVAL) && exec_ctx->top == prev_top));
3223 ret->type = EXPRVAL_VARIANT;
3224 if(exec_ctx->top == prev_top)
3225 V_VT(&ret->u.var) = VT_EMPTY;
3226 else
3227 ret->u.var = *stack_pop(exec_ctx);
3228 return S_OK;
3231 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3233 HRESULT hres;
3235 TRACE("\n");
3237 hres = compile_subscript(ctx->exec_ctx->parser, expr, !(flags & EXPR_NOVAL), &expr->instr_off);
3238 if(FAILED(hres))
3239 return hres;
3241 if(expr->eval == compiled_expression_eval)
3242 expr->eval = interp_expression_eval;
3244 return expr->eval(ctx, expr, flags, ei, ret);