jscript: Ignore an attempt to set visible host object identifier's value in ES5 mode.
[wine/zf.git] / dlls / jscript / engine.c
blob06b0fd673b3f7a8e715527bf505b596b5d80b88c
1 /*
2 * Copyright 2008,2011 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
20 #include <math.h>
21 #include <assert.h>
23 #include "jscript.h"
24 #include "engine.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 struct _except_frame_t {
31 unsigned stack_top;
32 scope_chain_t *scope;
33 unsigned catch_off;
34 unsigned finally_off;
36 except_frame_t *next;
39 typedef struct {
40 enum {
41 EXPRVAL_JSVAL,
42 EXPRVAL_IDREF,
43 EXPRVAL_STACK_REF,
44 EXPRVAL_INVALID
45 } type;
46 union {
47 jsval_t val;
48 struct {
49 IDispatch *disp;
50 DISPID id;
51 } idref;
52 unsigned off;
53 HRESULT hres;
54 } u;
55 } exprval_t;
57 static const size_t stack_size = 0x40000;
59 static HRESULT stack_push(script_ctx_t *ctx, jsval_t v)
61 if(ctx->stack_top == stack_size)
62 return JS_E_STACK_OVERFLOW;
64 ctx->stack[ctx->stack_top++] = v;
65 return S_OK;
68 static inline HRESULT stack_push_string(script_ctx_t *ctx, const WCHAR *str)
70 jsstr_t *v;
72 v = jsstr_alloc(str);
73 if(!v)
74 return E_OUTOFMEMORY;
76 return stack_push(ctx, jsval_string(v));
79 static inline jsval_t stack_top(script_ctx_t *ctx)
81 assert(ctx->stack_top > ctx->call_ctx->stack_base);
82 return ctx->stack[ctx->stack_top-1];
85 static inline jsval_t *stack_top_ref(script_ctx_t *ctx, unsigned n)
87 assert(ctx->stack_top > ctx->call_ctx->stack_base+n);
88 return ctx->stack+ctx->stack_top-1-n;
91 static inline jsval_t stack_topn(script_ctx_t *ctx, unsigned n)
93 return *stack_top_ref(ctx, n);
96 static inline jsval_t *stack_args(script_ctx_t *ctx, unsigned n)
98 if(!n)
99 return NULL;
100 assert(ctx->stack_top > ctx->call_ctx->stack_base+n-1);
101 return ctx->stack + ctx->stack_top-n;
104 static inline jsval_t stack_pop(script_ctx_t *ctx)
106 assert(ctx->stack_top > ctx->call_ctx->stack_base);
107 return ctx->stack[--ctx->stack_top];
110 static void stack_popn(script_ctx_t *ctx, unsigned n)
112 while(n--)
113 jsval_release(stack_pop(ctx));
116 static HRESULT stack_pop_number(script_ctx_t *ctx, double *r)
118 jsval_t v;
119 HRESULT hres;
121 v = stack_pop(ctx);
122 hres = to_number(ctx, v, r);
123 jsval_release(v);
124 return hres;
127 static HRESULT stack_pop_object(script_ctx_t *ctx, IDispatch **r)
129 jsval_t v;
130 HRESULT hres;
132 v = stack_pop(ctx);
133 if(is_object_instance(v)) {
134 if(!get_object(v))
135 return JS_E_OBJECT_REQUIRED;
136 *r = get_object(v);
137 return S_OK;
140 hres = to_object(ctx, v, r);
141 jsval_release(v);
142 return hres;
145 static inline HRESULT stack_pop_int(script_ctx_t *ctx, INT *r)
147 return to_int32(ctx, stack_pop(ctx), r);
150 static inline HRESULT stack_pop_uint(script_ctx_t *ctx, DWORD *r)
152 return to_uint32(ctx, stack_pop(ctx), r);
155 static inline unsigned local_off(call_frame_t *frame, int ref)
157 return ref < 0
158 ? frame->arguments_off - ref-1
159 : frame->variables_off + ref;
162 static inline BSTR local_name(call_frame_t *frame, int ref)
164 return ref < 0 ? frame->function->params[-ref-1] : frame->function->variables[ref].name;
167 /* Steals input reference even on failure. */
168 static HRESULT stack_push_exprval(script_ctx_t *ctx, exprval_t *val)
170 HRESULT hres;
172 switch(val->type) {
173 case EXPRVAL_JSVAL:
174 hres = stack_push(ctx, jsval_null());
175 if(SUCCEEDED(hres))
176 hres = stack_push(ctx, val->u.val);
177 return hres;
178 case EXPRVAL_IDREF:
179 hres = stack_push(ctx, jsval_disp(val->u.idref.disp));
180 if(SUCCEEDED(hres))
181 hres = stack_push(ctx, jsval_number(val->u.idref.id));
182 else
183 IDispatch_Release(val->u.idref.disp);
184 return hres;
185 case EXPRVAL_STACK_REF:
186 hres = stack_push(ctx, jsval_number(val->u.off));
187 if(SUCCEEDED(hres))
188 hres = stack_push(ctx, jsval_undefined());
189 return hres;
190 case EXPRVAL_INVALID:
191 hres = stack_push(ctx, jsval_undefined());
192 if(SUCCEEDED(hres))
193 hres = stack_push(ctx, jsval_number(val->u.hres));
194 return hres;
197 assert(0);
198 return E_FAIL;
201 static BOOL stack_topn_exprval(script_ctx_t *ctx, unsigned n, exprval_t *r)
203 jsval_t v = stack_topn(ctx, n+1);
205 switch(jsval_type(v)) {
206 case JSV_NUMBER: {
207 call_frame_t *frame = ctx->call_ctx;
208 unsigned off = get_number(v);
210 if(!frame->base_scope->frame && off >= frame->arguments_off) {
211 DISPID id;
212 BSTR name;
213 HRESULT hres;
215 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
217 assert(off < frame->variables_off + frame->function->var_cnt);
218 name = off >= frame->variables_off
219 ? frame->function->variables[off - frame->variables_off].name
220 : frame->function->params[off - frame->arguments_off];
221 hres = jsdisp_get_id(ctx->call_ctx->base_scope->jsobj, name, 0, &id);
222 if(FAILED(hres)) {
223 r->type = EXPRVAL_INVALID;
224 r->u.hres = hres;
225 return FALSE;
228 *stack_top_ref(ctx, n+1) = jsval_obj(jsdisp_addref(frame->base_scope->jsobj));
229 *stack_top_ref(ctx, n) = jsval_number(id);
230 r->type = EXPRVAL_IDREF;
231 r->u.idref.disp = frame->base_scope->obj;
232 r->u.idref.id = id;
233 return TRUE;
236 r->type = EXPRVAL_STACK_REF;
237 r->u.off = off;
238 return TRUE;
240 case JSV_OBJECT:
241 r->type = EXPRVAL_IDREF;
242 r->u.idref.disp = get_object(v);
243 assert(is_number(stack_topn(ctx, n)));
244 r->u.idref.id = get_number(stack_topn(ctx, n));
245 return TRUE;
246 case JSV_UNDEFINED:
247 r->type = EXPRVAL_INVALID;
248 assert(is_number(stack_topn(ctx, n)));
249 r->u.hres = get_number(stack_topn(ctx, n));
250 return FALSE;
251 case JSV_NULL:
252 r->type = EXPRVAL_JSVAL;
253 r->u.val = stack_topn(ctx, n);
254 return TRUE;
255 default:
256 assert(0);
257 return FALSE;
261 static inline BOOL stack_pop_exprval(script_ctx_t *ctx, exprval_t *r)
263 BOOL ret = stack_topn_exprval(ctx, 0, r);
264 ctx->stack_top -= 2;
265 return ret;
268 static HRESULT exprval_propput(script_ctx_t *ctx, exprval_t *ref, jsval_t v)
270 switch(ref->type) {
271 case EXPRVAL_STACK_REF: {
272 jsval_t *r = ctx->stack + ref->u.off;
273 jsval_release(*r);
274 return jsval_copy(v, r);
276 case EXPRVAL_IDREF:
277 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v);
278 case EXPRVAL_JSVAL:
279 WARN("ignoring an attempt to set value reference\n");
280 return S_OK;
281 default:
282 assert(0);
283 return E_FAIL;
287 static HRESULT exprval_propget(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
289 switch(ref->type) {
290 case EXPRVAL_STACK_REF:
291 return jsval_copy(ctx->stack[ref->u.off], r);
292 case EXPRVAL_IDREF:
293 return disp_propget(ctx, ref->u.idref.disp, ref->u.idref.id, r);
294 case EXPRVAL_JSVAL:
295 return jsval_copy(ref->u.val, r);
296 default:
297 assert(0);
298 return E_FAIL;
302 static HRESULT exprval_call(script_ctx_t *ctx, exprval_t *ref, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
304 switch(ref->type) {
305 case EXPRVAL_STACK_REF: {
306 jsval_t v = ctx->stack[ref->u.off];
308 if(!is_object_instance(v)) {
309 FIXME("invoke %s\n", debugstr_jsval(v));
310 return E_FAIL;
313 return disp_call_value(ctx, get_object(v), NULL, flags, argc, argv, r);
315 case EXPRVAL_IDREF:
316 return disp_call(ctx, ref->u.idref.disp, ref->u.idref.id, flags, argc, argv, r);
317 case EXPRVAL_JSVAL: {
318 IDispatch *obj;
319 HRESULT hres;
321 hres = to_object(ctx, ref->u.val, &obj);
322 if(SUCCEEDED(hres)) {
323 hres = disp_call_value(ctx, obj, NULL, flags, argc, argv, r);
324 IDispatch_Release(obj);
326 return hres;
328 default:
329 assert(0);
330 return E_FAIL;
334 /* ECMA-262 3rd Edition 8.7.1 */
335 /* Steals input reference. */
336 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
338 HRESULT hres;
340 if(ref->type == EXPRVAL_JSVAL) {
341 *r = ref->u.val;
342 return S_OK;
345 hres = exprval_propget(ctx, ref, r);
347 if(ref->type == EXPRVAL_IDREF)
348 IDispatch_Release(ref->u.idref.disp);
349 return hres;
352 static void exprval_release(exprval_t *val)
354 switch(val->type) {
355 case EXPRVAL_JSVAL:
356 jsval_release(val->u.val);
357 return;
358 case EXPRVAL_IDREF:
359 if(val->u.idref.disp)
360 IDispatch_Release(val->u.idref.disp);
361 return;
362 case EXPRVAL_STACK_REF:
363 case EXPRVAL_INVALID:
364 return;
368 static inline void exprval_set_exception(exprval_t *val, HRESULT hres)
370 val->type = EXPRVAL_INVALID;
371 val->u.hres = hres;
374 static inline void exprval_set_disp_ref(exprval_t *ref, IDispatch *obj, DISPID id)
376 ref->type = EXPRVAL_IDREF;
377 IDispatch_AddRef(ref->u.idref.disp = obj);
378 ref->u.idref.id = id;
381 static inline jsval_t steal_ret(call_frame_t *frame)
383 jsval_t r = frame->ret;
384 frame->ret = jsval_undefined();
385 return r;
388 static inline void clear_acc(script_ctx_t *ctx)
390 jsval_release(ctx->acc);
391 ctx->acc = jsval_undefined();
394 static HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
396 scope_chain_t *new_scope;
398 new_scope = heap_alloc(sizeof(scope_chain_t));
399 if(!new_scope)
400 return E_OUTOFMEMORY;
402 new_scope->ref = 1;
404 IDispatch_AddRef(obj);
405 new_scope->jsobj = jsobj;
406 new_scope->obj = obj;
407 new_scope->frame = NULL;
408 new_scope->next = scope ? scope_addref(scope) : NULL;
410 *ret = new_scope;
411 return S_OK;
414 static void scope_pop(scope_chain_t **scope)
416 scope_chain_t *tmp;
418 tmp = *scope;
419 *scope = tmp->next;
420 scope_release(tmp);
423 void scope_release(scope_chain_t *scope)
425 if(--scope->ref)
426 return;
428 if(scope->next)
429 scope_release(scope->next);
431 IDispatch_Release(scope->obj);
432 heap_free(scope);
435 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, const WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
437 IDispatchEx *dispex;
438 jsdisp_t *jsdisp;
439 BSTR bstr;
440 HRESULT hres;
442 jsdisp = iface_to_jsdisp(disp);
443 if(jsdisp) {
444 hres = jsdisp_get_id(jsdisp, name, flags, id);
445 jsdisp_release(jsdisp);
446 return hres;
449 if(name_bstr) {
450 bstr = name_bstr;
451 }else {
452 bstr = SysAllocString(name);
453 if(!bstr)
454 return E_OUTOFMEMORY;
457 *id = 0;
458 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
459 if(SUCCEEDED(hres)) {
460 hres = IDispatchEx_GetDispID(dispex, bstr, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
461 IDispatchEx_Release(dispex);
462 }else {
463 TRACE("using IDispatch\n");
464 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &bstr, 1, 0, id);
467 if(name_bstr != bstr)
468 SysFreeString(bstr);
469 return hres;
472 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
474 IObjectIdentity *identity;
475 IUnknown *unk1, *unk2;
476 HRESULT hres;
478 if(disp1 == disp2) {
479 *ret = TRUE;
480 return S_OK;
483 if(!disp1 || !disp2) {
484 *ret = FALSE;
485 return S_OK;
488 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
489 if(FAILED(hres))
490 return hres;
492 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
493 if(FAILED(hres)) {
494 IUnknown_Release(unk1);
495 return hres;
498 if(unk1 == unk2) {
499 *ret = TRUE;
500 }else {
501 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
502 if(SUCCEEDED(hres)) {
503 hres = IObjectIdentity_IsEqualObject(identity, unk2);
504 IObjectIdentity_Release(identity);
505 *ret = hres == S_OK;
506 }else {
507 *ret = FALSE;
511 IUnknown_Release(unk1);
512 IUnknown_Release(unk2);
513 return S_OK;
516 /* ECMA-262 3rd Edition 11.9.6 */
517 HRESULT jsval_strict_equal(jsval_t lval, jsval_t rval, BOOL *ret)
519 jsval_type_t type = jsval_type(lval);
521 TRACE("\n");
523 if(type != jsval_type(rval)) {
524 if(is_null_instance(lval))
525 *ret = is_null_instance(rval);
526 else
527 *ret = FALSE;
528 return S_OK;
531 switch(type) {
532 case JSV_UNDEFINED:
533 case JSV_NULL:
534 *ret = TRUE;
535 break;
536 case JSV_OBJECT:
537 return disp_cmp(get_object(lval), get_object(rval), ret);
538 case JSV_STRING:
539 *ret = jsstr_eq(get_string(lval), get_string(rval));
540 break;
541 case JSV_NUMBER:
542 *ret = get_number(lval) == get_number(rval);
543 break;
544 case JSV_BOOL:
545 *ret = !get_bool(lval) == !get_bool(rval);
546 break;
547 case JSV_VARIANT:
548 WARN("VARIANT type, returning false\n");
549 *ret = FALSE;
550 return S_OK;
553 return S_OK;
557 * Transfers local variables from stack to variable object.
558 * It's slow, so we want to avoid it as much as possible.
560 static HRESULT detach_variable_object(script_ctx_t *ctx, call_frame_t *frame, BOOL from_release)
562 unsigned i;
563 HRESULT hres;
565 if(!frame->base_scope || !frame->base_scope->frame)
566 return S_OK;
568 TRACE("detaching %p\n", frame);
570 assert(frame == frame->base_scope->frame);
571 assert(frame->variable_obj == frame->base_scope->jsobj);
573 if(!from_release && !frame->arguments_obj) {
574 hres = setup_arguments_object(ctx, frame);
575 if(FAILED(hres))
576 return hres;
579 frame->base_scope->frame = NULL;
581 for(i = 0; i < frame->function->locals_cnt; i++) {
582 hres = jsdisp_propput_name(frame->variable_obj, frame->function->locals[i].name,
583 ctx->stack[local_off(frame, frame->function->locals[i].ref)]);
584 if(FAILED(hres))
585 return hres;
588 return S_OK;
591 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
593 named_item_t *item;
594 DISPID id;
595 HRESULT hres;
597 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
598 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
599 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
600 if(SUCCEEDED(hres)) {
601 if(ret)
602 exprval_set_disp_ref(ret, item->disp, id);
603 return TRUE;
608 return FALSE;
611 IDispatch *lookup_global_host(script_ctx_t *ctx)
613 IDispatch *disp = NULL;
614 named_item_t *item;
616 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
617 if(!(item->flags & SCRIPTITEM_GLOBALMEMBERS)) continue;
618 disp = item->disp;
619 break;
621 if(!disp) disp = to_disp(ctx->global);
623 return disp;
626 static int __cdecl local_ref_cmp(const void *key, const void *ref)
628 return wcscmp((const WCHAR*)key, ((const local_ref_t*)ref)->name);
631 local_ref_t *lookup_local(const function_code_t *function, const WCHAR *identifier)
633 return bsearch(identifier, function->locals, function->locals_cnt, sizeof(*function->locals), local_ref_cmp);
636 /* ECMA-262 3rd Edition 10.1.4 */
637 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
639 scope_chain_t *scope;
640 named_item_t *item;
641 DISPID id = 0;
642 HRESULT hres;
644 TRACE("%s\n", debugstr_w(identifier));
646 if(ctx->call_ctx) {
647 for(scope = ctx->call_ctx->scope; scope; scope = scope->next) {
648 if(scope->frame) {
649 function_code_t *func = scope->frame->function;
650 local_ref_t *ref = lookup_local(func, identifier);
652 if(ref) {
653 ret->type = EXPRVAL_STACK_REF;
654 ret->u.off = local_off(scope->frame, ref->ref);
655 TRACE("returning ref %d for %d\n", ret->u.off, ref->ref);
656 return S_OK;
659 if(!wcscmp(identifier, L"arguments")) {
660 hres = detach_variable_object(ctx, scope->frame, FALSE);
661 if(FAILED(hres))
662 return hres;
665 if(scope->jsobj)
666 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
667 else
668 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
669 if(SUCCEEDED(hres)) {
670 exprval_set_disp_ref(ret, scope->obj, id);
671 return S_OK;
675 item = ctx->call_ctx->bytecode->named_item;
676 if(item) {
677 hres = jsdisp_get_id(item->script_obj, identifier, 0, &id);
678 if(SUCCEEDED(hres)) {
679 exprval_set_disp_ref(ret, to_disp(item->script_obj), id);
680 return S_OK;
682 if(!(item->flags & SCRIPTITEM_CODEONLY)) {
683 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
684 if(SUCCEEDED(hres)) {
685 exprval_set_disp_ref(ret, item->disp, id);
686 return S_OK;
692 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
693 if(SUCCEEDED(hres)) {
694 exprval_set_disp_ref(ret, to_disp(ctx->global), id);
695 return S_OK;
698 item = lookup_named_item(ctx, identifier, SCRIPTITEM_ISVISIBLE);
699 if(item) {
700 IDispatch_AddRef(item->disp);
701 ret->type = EXPRVAL_JSVAL;
702 ret->u.val = jsval_disp(item->disp);
703 return S_OK;
706 if(lookup_global_members(ctx, identifier, ret))
707 return S_OK;
709 exprval_set_exception(ret, JS_E_UNDEFINED_VARIABLE);
710 return S_OK;
713 static inline BSTR get_op_bstr(script_ctx_t *ctx, int i)
715 call_frame_t *frame = ctx->call_ctx;
716 return frame->bytecode->instrs[frame->ip].u.arg[i].bstr;
719 static inline unsigned get_op_uint(script_ctx_t *ctx, int i)
721 call_frame_t *frame = ctx->call_ctx;
722 return frame->bytecode->instrs[frame->ip].u.arg[i].uint;
725 static inline unsigned get_op_int(script_ctx_t *ctx, int i)
727 call_frame_t *frame = ctx->call_ctx;
728 return frame->bytecode->instrs[frame->ip].u.arg[i].lng;
731 static inline jsstr_t *get_op_str(script_ctx_t *ctx, int i)
733 call_frame_t *frame = ctx->call_ctx;
734 return frame->bytecode->instrs[frame->ip].u.arg[i].str;
737 static inline double get_op_double(script_ctx_t *ctx)
739 call_frame_t *frame = ctx->call_ctx;
740 return frame->bytecode->instrs[frame->ip].u.dbl;
743 static inline void jmp_next(script_ctx_t *ctx)
745 ctx->call_ctx->ip++;
748 static inline void jmp_abs(script_ctx_t *ctx, unsigned dst)
750 ctx->call_ctx->ip = dst;
753 /* ECMA-262 3rd Edition 12.6.4 */
754 static HRESULT interp_forin(script_ctx_t *ctx)
756 const HRESULT arg = get_op_uint(ctx, 0);
757 IDispatch *obj = NULL;
758 IDispatchEx *dispex;
759 exprval_t prop_ref;
760 DISPID id;
761 BSTR name = NULL;
762 HRESULT hres;
764 TRACE("\n");
766 assert(is_number(stack_top(ctx)));
767 id = get_number(stack_top(ctx));
769 if(!stack_topn_exprval(ctx, 1, &prop_ref)) {
770 FIXME("invalid ref: %08x\n", prop_ref.u.hres);
771 return E_FAIL;
774 if(is_object_instance(stack_topn(ctx, 3)))
775 obj = get_object(stack_topn(ctx, 3));
777 if(obj) {
778 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
779 if(SUCCEEDED(hres)) {
780 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
781 if(hres == S_OK)
782 hres = IDispatchEx_GetMemberName(dispex, id, &name);
783 IDispatchEx_Release(dispex);
784 if(FAILED(hres))
785 return hres;
786 }else {
787 TRACE("No IDispatchEx\n");
791 if(name) {
792 jsstr_t *str;
794 str = jsstr_alloc_len(name, SysStringLen(name));
795 SysFreeString(name);
796 if(!str)
797 return E_OUTOFMEMORY;
799 stack_pop(ctx);
800 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
802 hres = exprval_propput(ctx, &prop_ref, jsval_string(str));
803 jsstr_release(str);
804 if(FAILED(hres))
805 return hres;
807 jmp_next(ctx);
808 }else {
809 stack_popn(ctx, 4);
810 jmp_abs(ctx, arg);
812 return S_OK;
815 /* ECMA-262 3rd Edition 12.10 */
816 static HRESULT interp_push_scope(script_ctx_t *ctx)
818 IDispatch *disp;
819 jsval_t v;
820 HRESULT hres;
822 TRACE("\n");
824 v = stack_pop(ctx);
825 hres = to_object(ctx, v, &disp);
826 jsval_release(v);
827 if(FAILED(hres))
828 return hres;
830 hres = scope_push(ctx->call_ctx->scope, to_jsdisp(disp), disp, &ctx->call_ctx->scope);
831 IDispatch_Release(disp);
832 return hres;
835 /* ECMA-262 3rd Edition 12.10 */
836 static HRESULT interp_pop_scope(script_ctx_t *ctx)
838 TRACE("\n");
840 scope_pop(&ctx->call_ctx->scope);
841 return S_OK;
844 /* ECMA-262 3rd Edition 12.13 */
845 static HRESULT interp_case(script_ctx_t *ctx)
847 const unsigned arg = get_op_uint(ctx, 0);
848 jsval_t v;
849 BOOL b;
850 HRESULT hres;
852 TRACE("\n");
854 v = stack_pop(ctx);
855 hres = jsval_strict_equal(stack_top(ctx), v, &b);
856 jsval_release(v);
857 if(FAILED(hres))
858 return hres;
860 if(b) {
861 stack_popn(ctx, 1);
862 jmp_abs(ctx, arg);
863 }else {
864 jmp_next(ctx);
866 return S_OK;
869 static void set_error_value(script_ctx_t *ctx, jsval_t value)
871 jsexcept_t *ei = ctx->ei;
872 jsdisp_t *obj;
874 reset_ei(ei);
875 ei->error = JS_E_EXCEPTION_THROWN;
876 ei->valid_value = TRUE;
877 ei->value = value;
879 if(is_object_instance(value) && get_object(value) && (obj = to_jsdisp(get_object(value)))) {
880 UINT32 number;
881 jsstr_t *str;
882 jsval_t v;
883 HRESULT hres;
885 /* FIXME: We should check if object is an error instance */
887 hres = jsdisp_propget_name(obj, L"number", &v);
888 if(SUCCEEDED(hres)) {
889 hres = to_uint32(ctx, v, &number);
890 if(SUCCEEDED(hres))
891 ei->error = FAILED(number) ? number : E_FAIL;
892 jsval_release(v);
895 hres = jsdisp_propget_name(obj, L"description", &v);
896 if(SUCCEEDED(hres)) {
897 hres = to_string(ctx, v, &str);
898 if(SUCCEEDED(hres))
899 ei->message = str;
900 jsval_release(v);
905 /* ECMA-262 3rd Edition 12.13 */
906 static HRESULT interp_throw(script_ctx_t *ctx)
908 TRACE("\n");
910 set_error_value(ctx, stack_pop(ctx));
911 return DISP_E_EXCEPTION;
914 static HRESULT interp_throw_ref(script_ctx_t *ctx)
916 const HRESULT arg = get_op_uint(ctx, 0);
918 TRACE("%08x\n", arg);
920 return arg;
923 static HRESULT interp_throw_type(script_ctx_t *ctx)
925 const HRESULT hres = get_op_uint(ctx, 0);
926 jsstr_t *str = get_op_str(ctx, 1);
927 const WCHAR *ptr;
929 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
931 ptr = jsstr_flatten(str);
932 return ptr ? throw_error(ctx, hres, ptr) : E_OUTOFMEMORY;
935 /* ECMA-262 3rd Edition 12.14 */
936 static HRESULT interp_push_except(script_ctx_t *ctx)
938 const unsigned catch_off = get_op_uint(ctx, 0);
939 const unsigned finally_off = get_op_uint(ctx, 1);
940 call_frame_t *frame = ctx->call_ctx;
941 except_frame_t *except;
943 TRACE("\n");
945 except = heap_alloc(sizeof(*except));
946 if(!except)
947 return E_OUTOFMEMORY;
949 except->stack_top = ctx->stack_top;
950 except->scope = frame->scope;
951 except->catch_off = catch_off;
952 except->finally_off = finally_off;
953 except->next = frame->except_frame;
954 frame->except_frame = except;
955 return S_OK;
958 /* ECMA-262 3rd Edition 12.14 */
959 static HRESULT interp_pop_except(script_ctx_t *ctx)
961 const unsigned ret_off = get_op_uint(ctx, 0);
962 call_frame_t *frame = ctx->call_ctx;
963 except_frame_t *except;
964 unsigned finally_off;
966 TRACE("%u\n", ret_off);
968 except = frame->except_frame;
969 assert(except != NULL);
971 finally_off = except->finally_off;
972 frame->except_frame = except->next;
973 heap_free(except);
975 if(finally_off) {
976 HRESULT hres;
978 hres = stack_push(ctx, jsval_number(ret_off));
979 if(FAILED(hres))
980 return hres;
981 hres = stack_push(ctx, jsval_bool(TRUE));
982 if(FAILED(hres))
983 return hres;
984 frame->ip = finally_off;
985 }else {
986 frame->ip = ret_off;
989 return S_OK;
992 /* ECMA-262 3rd Edition 12.14 */
993 static HRESULT interp_end_finally(script_ctx_t *ctx)
995 call_frame_t *frame = ctx->call_ctx;
996 jsval_t v;
998 TRACE("\n");
1000 v = stack_pop(ctx);
1001 assert(is_bool(v));
1003 if(!get_bool(v)) {
1004 TRACE("passing exception\n");
1006 set_error_value(ctx, stack_pop(ctx));
1007 return DISP_E_EXCEPTION;
1010 v = stack_pop(ctx);
1011 assert(is_number(v));
1012 frame->ip = get_number(v);
1013 return S_OK;
1016 static HRESULT interp_enter_catch(script_ctx_t *ctx)
1018 const BSTR ident = get_op_bstr(ctx, 0);
1019 jsdisp_t *scope_obj;
1020 jsval_t v;
1021 HRESULT hres;
1023 hres = create_dispex(ctx, NULL, NULL, &scope_obj);
1024 if(FAILED(hres))
1025 return hres;
1027 v = stack_pop(ctx);
1028 hres = jsdisp_propput_name(scope_obj, ident, v);
1029 jsval_release(v);
1030 if(SUCCEEDED(hres))
1031 hres = scope_push(ctx->call_ctx->scope, scope_obj, to_disp(scope_obj), &ctx->call_ctx->scope);
1032 jsdisp_release(scope_obj);
1033 return hres;
1036 /* ECMA-262 3rd Edition 13 */
1037 static HRESULT interp_func(script_ctx_t *ctx)
1039 unsigned func_idx = get_op_uint(ctx, 0);
1040 call_frame_t *frame = ctx->call_ctx;
1041 jsdisp_t *dispex;
1042 HRESULT hres;
1044 TRACE("%d\n", func_idx);
1046 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+func_idx,
1047 frame->scope, &dispex);
1048 if(FAILED(hres))
1049 return hres;
1051 return stack_push(ctx, jsval_obj(dispex));
1054 /* ECMA-262 3rd Edition 11.2.1 */
1055 static HRESULT interp_array(script_ctx_t *ctx)
1057 jsstr_t *name_str;
1058 const WCHAR *name;
1059 jsval_t v, namev;
1060 IDispatch *obj;
1061 DISPID id;
1062 HRESULT hres;
1064 TRACE("\n");
1066 namev = stack_pop(ctx);
1068 hres = stack_pop_object(ctx, &obj);
1069 if(FAILED(hres)) {
1070 jsval_release(namev);
1071 return hres;
1074 hres = to_flat_string(ctx, namev, &name_str, &name);
1075 jsval_release(namev);
1076 if(FAILED(hres)) {
1077 IDispatch_Release(obj);
1078 return hres;
1081 hres = disp_get_id(ctx, obj, name, NULL, 0, &id);
1082 jsstr_release(name_str);
1083 if(SUCCEEDED(hres)) {
1084 hres = disp_propget(ctx, obj, id, &v);
1085 }else if(hres == DISP_E_UNKNOWNNAME) {
1086 v = jsval_undefined();
1087 hres = S_OK;
1089 IDispatch_Release(obj);
1090 if(FAILED(hres))
1091 return hres;
1093 return stack_push(ctx, v);
1096 /* ECMA-262 3rd Edition 11.2.1 */
1097 static HRESULT interp_member(script_ctx_t *ctx)
1099 const BSTR arg = get_op_bstr(ctx, 0);
1100 IDispatch *obj;
1101 jsval_t v;
1102 DISPID id;
1103 HRESULT hres;
1105 TRACE("\n");
1107 hres = stack_pop_object(ctx, &obj);
1108 if(FAILED(hres))
1109 return hres;
1111 hres = disp_get_id(ctx, obj, arg, arg, 0, &id);
1112 if(SUCCEEDED(hres)) {
1113 hres = disp_propget(ctx, obj, id, &v);
1114 }else if(hres == DISP_E_UNKNOWNNAME) {
1115 v = jsval_undefined();
1116 hres = S_OK;
1118 IDispatch_Release(obj);
1119 if(FAILED(hres))
1120 return hres;
1122 return stack_push(ctx, v);
1125 /* ECMA-262 3rd Edition 11.2.1 */
1126 static HRESULT interp_memberid(script_ctx_t *ctx)
1128 const unsigned arg = get_op_uint(ctx, 0);
1129 jsval_t objv, namev;
1130 const WCHAR *name;
1131 jsstr_t *name_str;
1132 IDispatch *obj;
1133 exprval_t ref;
1134 DISPID id;
1135 HRESULT hres;
1137 TRACE("%x\n", arg);
1139 namev = stack_pop(ctx);
1140 objv = stack_pop(ctx);
1142 hres = to_object(ctx, objv, &obj);
1143 jsval_release(objv);
1144 if(SUCCEEDED(hres)) {
1145 hres = to_flat_string(ctx, namev, &name_str, &name);
1146 if(FAILED(hres))
1147 IDispatch_Release(obj);
1149 jsval_release(namev);
1150 if(FAILED(hres))
1151 return hres;
1153 hres = disp_get_id(ctx, obj, name, NULL, arg, &id);
1154 jsstr_release(name_str);
1155 if(SUCCEEDED(hres)) {
1156 ref.type = EXPRVAL_IDREF;
1157 ref.u.idref.disp = obj;
1158 ref.u.idref.id = id;
1159 }else {
1160 IDispatch_Release(obj);
1161 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1162 exprval_set_exception(&ref, JS_E_INVALID_PROPERTY);
1163 hres = S_OK;
1164 }else {
1165 ERR("failed %08x\n", hres);
1166 return hres;
1170 return stack_push_exprval(ctx, &ref);
1173 /* ECMA-262 3rd Edition 11.2.1 */
1174 static HRESULT interp_refval(script_ctx_t *ctx)
1176 exprval_t ref;
1177 jsval_t v;
1178 HRESULT hres;
1180 TRACE("\n");
1182 if(!stack_topn_exprval(ctx, 0, &ref))
1183 return JS_E_ILLEGAL_ASSIGN;
1185 hres = exprval_propget(ctx, &ref, &v);
1186 if(FAILED(hres))
1187 return hres;
1189 return stack_push(ctx, v);
1192 /* ECMA-262 3rd Edition 11.2.2 */
1193 static HRESULT interp_new(script_ctx_t *ctx)
1195 const unsigned argc = get_op_uint(ctx, 0);
1196 jsval_t constr;
1198 TRACE("%d\n", argc);
1200 constr = stack_topn(ctx, argc);
1202 /* NOTE: Should use to_object here */
1204 if(is_null(constr))
1205 return JS_E_OBJECT_EXPECTED;
1206 else if(!is_object_instance(constr))
1207 return JS_E_INVALID_ACTION;
1208 else if(!get_object(constr))
1209 return JS_E_INVALID_PROPERTY;
1211 clear_acc(ctx);
1212 return disp_call_value(ctx, get_object(constr), NULL, DISPATCH_CONSTRUCT | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1213 argc, stack_args(ctx, argc), &ctx->acc);
1216 /* ECMA-262 3rd Edition 11.2.3 */
1217 static HRESULT interp_call(script_ctx_t *ctx)
1219 const unsigned argn = get_op_uint(ctx, 0);
1220 const int do_ret = get_op_int(ctx, 1);
1221 jsval_t obj;
1223 TRACE("%d %d\n", argn, do_ret);
1225 obj = stack_topn(ctx, argn);
1226 if(!is_object_instance(obj))
1227 return JS_E_INVALID_PROPERTY;
1229 clear_acc(ctx);
1230 return disp_call_value(ctx, get_object(obj), NULL, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1231 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1234 /* ECMA-262 3rd Edition 11.2.3 */
1235 static HRESULT interp_call_member(script_ctx_t *ctx)
1237 const unsigned argn = get_op_uint(ctx, 0);
1238 const int do_ret = get_op_int(ctx, 1);
1239 exprval_t ref;
1241 TRACE("%d %d\n", argn, do_ret);
1243 if(!stack_topn_exprval(ctx, argn, &ref))
1244 return ref.u.hres;
1246 clear_acc(ctx);
1247 return exprval_call(ctx, &ref, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1248 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1251 /* ECMA-262 3rd Edition 11.1.1 */
1252 static HRESULT interp_this(script_ctx_t *ctx)
1254 IDispatch *this_obj = ctx->call_ctx->this_obj;
1256 TRACE("\n");
1258 if(!this_obj) {
1259 named_item_t *item = ctx->call_ctx->bytecode->named_item;
1261 if(item)
1262 this_obj = (item->flags & SCRIPTITEM_CODEONLY) ? to_disp(item->script_obj) : item->disp;
1263 else
1264 this_obj = lookup_global_host(ctx);
1267 IDispatch_AddRef(this_obj);
1268 return stack_push(ctx, jsval_disp(this_obj));
1271 static HRESULT interp_identifier_ref(script_ctx_t *ctx, BSTR identifier, unsigned flags)
1273 exprval_t exprval;
1274 HRESULT hres;
1276 hres = identifier_eval(ctx, identifier, &exprval);
1277 if(FAILED(hres))
1278 return hres;
1280 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1281 jsdisp_t *script_obj = ctx->global;
1282 DISPID id;
1284 if(ctx->call_ctx->bytecode->named_item)
1285 script_obj = ctx->call_ctx->bytecode->named_item->script_obj;
1287 hres = jsdisp_get_id(script_obj, identifier, fdexNameEnsure, &id);
1288 if(FAILED(hres))
1289 return hres;
1291 exprval_set_disp_ref(&exprval, to_disp(script_obj), id);
1294 if(exprval.type == EXPRVAL_INVALID ||
1295 (exprval.type == EXPRVAL_JSVAL && ctx->version < SCRIPTLANGUAGEVERSION_ES5)) {
1296 WARN("invalid ref\n");
1297 exprval_release(&exprval);
1298 exprval_set_exception(&exprval, JS_E_OBJECT_EXPECTED);
1301 return stack_push_exprval(ctx, &exprval);
1304 static HRESULT identifier_value(script_ctx_t *ctx, BSTR identifier)
1306 exprval_t exprval;
1307 jsval_t v;
1308 HRESULT hres;
1310 hres = identifier_eval(ctx, identifier, &exprval);
1311 if(FAILED(hres))
1312 return hres;
1314 if(exprval.type == EXPRVAL_INVALID)
1315 return throw_error(ctx, exprval.u.hres, identifier);
1317 hres = exprval_to_value(ctx, &exprval, &v);
1318 if(FAILED(hres))
1319 return hres;
1321 return stack_push(ctx, v);
1324 static HRESULT interp_local_ref(script_ctx_t *ctx)
1326 const int arg = get_op_int(ctx, 0);
1327 const unsigned flags = get_op_uint(ctx, 1);
1328 call_frame_t *frame = ctx->call_ctx;
1329 exprval_t ref;
1331 TRACE("%s\n", debugstr_w(local_name(frame, arg)));
1333 if(!frame->base_scope || !frame->base_scope->frame)
1334 return interp_identifier_ref(ctx, local_name(frame, arg), flags);
1336 ref.type = EXPRVAL_STACK_REF;
1337 ref.u.off = local_off(frame, arg);
1338 return stack_push_exprval(ctx, &ref);
1341 static HRESULT interp_local(script_ctx_t *ctx)
1343 const int arg = get_op_int(ctx, 0);
1344 call_frame_t *frame = ctx->call_ctx;
1345 jsval_t copy;
1346 HRESULT hres;
1348 if(!frame->base_scope || !frame->base_scope->frame) {
1349 TRACE("%s\n", debugstr_w(local_name(frame, arg)));
1350 return identifier_value(ctx, local_name(frame, arg));
1353 hres = jsval_copy(ctx->stack[local_off(frame, arg)], &copy);
1354 if(FAILED(hres))
1355 return hres;
1357 TRACE("%s: %s\n", debugstr_w(local_name(frame, arg)), debugstr_jsval(copy));
1358 return stack_push(ctx, copy);
1361 /* ECMA-262 3rd Edition 10.1.4 */
1362 static HRESULT interp_ident(script_ctx_t *ctx)
1364 const BSTR arg = get_op_bstr(ctx, 0);
1366 TRACE("%s\n", debugstr_w(arg));
1368 return identifier_value(ctx, arg);
1371 /* ECMA-262 3rd Edition 10.1.4 */
1372 static HRESULT interp_identid(script_ctx_t *ctx)
1374 const BSTR arg = get_op_bstr(ctx, 0);
1375 const unsigned flags = get_op_uint(ctx, 1);
1377 TRACE("%s %x\n", debugstr_w(arg), flags);
1379 return interp_identifier_ref(ctx, arg, flags);
1382 /* ECMA-262 3rd Edition 7.8.1 */
1383 static HRESULT interp_null(script_ctx_t *ctx)
1385 TRACE("\n");
1387 return stack_push(ctx, jsval_null());
1390 /* ECMA-262 3rd Edition 7.8.2 */
1391 static HRESULT interp_bool(script_ctx_t *ctx)
1393 const int arg = get_op_int(ctx, 0);
1395 TRACE("%s\n", arg ? "true" : "false");
1397 return stack_push(ctx, jsval_bool(arg));
1400 /* ECMA-262 3rd Edition 7.8.3 */
1401 static HRESULT interp_int(script_ctx_t *ctx)
1403 const int arg = get_op_int(ctx, 0);
1405 TRACE("%d\n", arg);
1407 return stack_push(ctx, jsval_number(arg));
1410 /* ECMA-262 3rd Edition 7.8.3 */
1411 static HRESULT interp_double(script_ctx_t *ctx)
1413 const double arg = get_op_double(ctx);
1415 TRACE("%lf\n", arg);
1417 return stack_push(ctx, jsval_number(arg));
1420 /* ECMA-262 3rd Edition 7.8.4 */
1421 static HRESULT interp_str(script_ctx_t *ctx)
1423 jsstr_t *str = get_op_str(ctx, 0);
1425 TRACE("%s\n", debugstr_jsstr(str));
1427 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1430 /* ECMA-262 3rd Edition 7.8 */
1431 static HRESULT interp_regexp(script_ctx_t *ctx)
1433 jsstr_t *source = get_op_str(ctx, 0);
1434 const unsigned flags = get_op_uint(ctx, 1);
1435 jsdisp_t *regexp;
1436 HRESULT hres;
1438 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1440 hres = create_regexp(ctx, source, flags, &regexp);
1441 if(FAILED(hres))
1442 return hres;
1444 return stack_push(ctx, jsval_obj(regexp));
1447 /* ECMA-262 3rd Edition 11.1.4 */
1448 static HRESULT interp_carray(script_ctx_t *ctx)
1450 const unsigned arg = get_op_uint(ctx, 0);
1451 jsdisp_t *array;
1452 HRESULT hres;
1454 TRACE("%u\n", arg);
1456 hres = create_array(ctx, arg, &array);
1457 if(FAILED(hres))
1458 return hres;
1460 return stack_push(ctx, jsval_obj(array));
1463 static HRESULT interp_carray_set(script_ctx_t *ctx)
1465 const unsigned index = get_op_uint(ctx, 0);
1466 jsval_t value, array;
1467 HRESULT hres;
1469 value = stack_pop(ctx);
1471 TRACE("[%u] = %s\n", index, debugstr_jsval(value));
1473 array = stack_top(ctx);
1474 assert(is_object_instance(array));
1476 hres = jsdisp_propput_idx(iface_to_jsdisp(get_object(array)), index, value);
1477 jsval_release(value);
1478 return hres;
1481 /* ECMA-262 3rd Edition 11.1.5 */
1482 static HRESULT interp_new_obj(script_ctx_t *ctx)
1484 jsdisp_t *obj;
1485 HRESULT hres;
1487 TRACE("\n");
1489 hres = create_object(ctx, NULL, &obj);
1490 if(FAILED(hres))
1491 return hres;
1493 return stack_push(ctx, jsval_obj(obj));
1496 /* ECMA-262 3rd Edition 11.1.5 */
1497 static HRESULT interp_obj_prop(script_ctx_t *ctx)
1499 jsstr_t *name_arg = get_op_str(ctx, 0);
1500 unsigned type = get_op_uint(ctx, 1);
1501 const WCHAR *name;
1502 jsdisp_t *obj;
1503 jsval_t val;
1504 HRESULT hres;
1506 TRACE("%s\n", debugstr_jsstr(name_arg));
1508 val = stack_pop(ctx);
1510 /* FIXME: we should pass it as jsstr_t */
1511 name = jsstr_flatten(name_arg);
1513 assert(is_object_instance(stack_top(ctx)));
1514 obj = as_jsdisp(get_object(stack_top(ctx)));
1516 if(type == PROPERTY_DEFINITION_VALUE) {
1517 hres = jsdisp_propput_name(obj, name, val);
1518 }else {
1519 property_desc_t desc = {PROPF_ENUMERABLE | PROPF_CONFIGURABLE};
1520 jsdisp_t *func;
1522 assert(is_object_instance(val));
1523 func = iface_to_jsdisp(get_object(val));
1525 desc.mask = desc.flags;
1526 if(type == PROPERTY_DEFINITION_GETTER) {
1527 desc.explicit_getter = TRUE;
1528 desc.getter = func;
1529 }else {
1530 desc.explicit_setter = TRUE;
1531 desc.setter = func;
1534 hres = jsdisp_define_property(obj, name, &desc);
1535 jsdisp_release(func);
1538 jsval_release(val);
1539 return hres;
1542 /* ECMA-262 3rd Edition 11.11 */
1543 static HRESULT interp_cnd_nz(script_ctx_t *ctx)
1545 const unsigned arg = get_op_uint(ctx, 0);
1546 BOOL b;
1547 HRESULT hres;
1549 TRACE("\n");
1551 hres = to_boolean(stack_top(ctx), &b);
1552 if(FAILED(hres))
1553 return hres;
1555 if(b) {
1556 jmp_abs(ctx, arg);
1557 }else {
1558 stack_popn(ctx, 1);
1559 jmp_next(ctx);
1561 return S_OK;
1564 /* ECMA-262 3rd Edition 11.11 */
1565 static HRESULT interp_cnd_z(script_ctx_t *ctx)
1567 const unsigned arg = get_op_uint(ctx, 0);
1568 BOOL b;
1569 HRESULT hres;
1571 TRACE("\n");
1573 hres = to_boolean(stack_top(ctx), &b);
1574 if(FAILED(hres))
1575 return hres;
1577 if(b) {
1578 stack_popn(ctx, 1);
1579 jmp_next(ctx);
1580 }else {
1581 jmp_abs(ctx, arg);
1583 return S_OK;
1586 /* ECMA-262 3rd Edition 11.10 */
1587 static HRESULT interp_or(script_ctx_t *ctx)
1589 INT l, r;
1590 HRESULT hres;
1592 TRACE("\n");
1594 hres = stack_pop_int(ctx, &r);
1595 if(FAILED(hres))
1596 return hres;
1598 hres = stack_pop_int(ctx, &l);
1599 if(FAILED(hres))
1600 return hres;
1602 return stack_push(ctx, jsval_number(l|r));
1605 /* ECMA-262 3rd Edition 11.10 */
1606 static HRESULT interp_xor(script_ctx_t *ctx)
1608 INT l, r;
1609 HRESULT hres;
1611 TRACE("\n");
1613 hres = stack_pop_int(ctx, &r);
1614 if(FAILED(hres))
1615 return hres;
1617 hres = stack_pop_int(ctx, &l);
1618 if(FAILED(hres))
1619 return hres;
1621 return stack_push(ctx, jsval_number(l^r));
1624 /* ECMA-262 3rd Edition 11.10 */
1625 static HRESULT interp_and(script_ctx_t *ctx)
1627 INT l, r;
1628 HRESULT hres;
1630 TRACE("\n");
1632 hres = stack_pop_int(ctx, &r);
1633 if(FAILED(hres))
1634 return hres;
1636 hres = stack_pop_int(ctx, &l);
1637 if(FAILED(hres))
1638 return hres;
1640 return stack_push(ctx, jsval_number(l&r));
1643 /* ECMA-262 3rd Edition 11.8.6 */
1644 static HRESULT interp_instanceof(script_ctx_t *ctx)
1646 jsdisp_t *obj, *iter, *tmp = NULL;
1647 jsval_t prot, v;
1648 BOOL ret = FALSE;
1649 HRESULT hres;
1651 v = stack_pop(ctx);
1652 if(!is_object_instance(v) || !get_object(v)) {
1653 jsval_release(v);
1654 return JS_E_FUNCTION_EXPECTED;
1657 obj = iface_to_jsdisp(get_object(v));
1658 IDispatch_Release(get_object(v));
1659 if(!obj) {
1660 FIXME("non-jsdisp objects not supported\n");
1661 return E_FAIL;
1664 if(is_class(obj, JSCLASS_FUNCTION)) {
1665 hres = jsdisp_propget_name(obj, L"prototype", &prot);
1666 }else {
1667 hres = JS_E_FUNCTION_EXPECTED;
1669 jsdisp_release(obj);
1670 if(FAILED(hres))
1671 return hres;
1673 v = stack_pop(ctx);
1675 if(is_object_instance(prot)) {
1676 if(is_object_instance(v))
1677 tmp = iface_to_jsdisp(get_object(v));
1678 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1679 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1680 if(FAILED(hres))
1681 break;
1684 if(tmp)
1685 jsdisp_release(tmp);
1686 }else {
1687 FIXME("prototype is not an object\n");
1688 hres = E_FAIL;
1691 jsval_release(prot);
1692 jsval_release(v);
1693 if(FAILED(hres))
1694 return hres;
1696 return stack_push(ctx, jsval_bool(ret));
1699 /* ECMA-262 3rd Edition 11.8.7 */
1700 static HRESULT interp_in(script_ctx_t *ctx)
1702 const WCHAR *str;
1703 jsstr_t *jsstr;
1704 jsval_t obj, v;
1705 DISPID id = 0;
1706 BOOL ret;
1707 HRESULT hres;
1709 TRACE("\n");
1711 obj = stack_pop(ctx);
1712 if(!is_object_instance(obj) || !get_object(obj)) {
1713 jsval_release(obj);
1714 return JS_E_OBJECT_EXPECTED;
1717 v = stack_pop(ctx);
1718 hres = to_flat_string(ctx, v, &jsstr, &str);
1719 jsval_release(v);
1720 if(FAILED(hres)) {
1721 IDispatch_Release(get_object(obj));
1722 return hres;
1725 hres = disp_get_id(ctx, get_object(obj), str, NULL, 0, &id);
1726 IDispatch_Release(get_object(obj));
1727 jsstr_release(jsstr);
1728 if(SUCCEEDED(hres))
1729 ret = TRUE;
1730 else if(hres == DISP_E_UNKNOWNNAME)
1731 ret = FALSE;
1732 else
1733 return hres;
1735 return stack_push(ctx, jsval_bool(ret));
1738 /* ECMA-262 3rd Edition 11.6.1 */
1739 static HRESULT interp_add(script_ctx_t *ctx)
1741 jsval_t l, r, lval, rval, ret;
1742 HRESULT hres;
1744 rval = stack_pop(ctx);
1745 lval = stack_pop(ctx);
1747 TRACE("%s + %s\n", debugstr_jsval(lval), debugstr_jsval(rval));
1749 hres = to_primitive(ctx, lval, &l, NO_HINT);
1750 if(SUCCEEDED(hres)) {
1751 hres = to_primitive(ctx, rval, &r, NO_HINT);
1752 if(FAILED(hres))
1753 jsval_release(l);
1755 jsval_release(lval);
1756 jsval_release(rval);
1757 if(FAILED(hres))
1758 return hres;
1760 if(is_string(l) || is_string(r)) {
1761 jsstr_t *lstr, *rstr = NULL;
1763 hres = to_string(ctx, l, &lstr);
1764 if(SUCCEEDED(hres))
1765 hres = to_string(ctx, r, &rstr);
1767 if(SUCCEEDED(hres)) {
1768 jsstr_t *ret_str;
1770 ret_str = jsstr_concat(lstr, rstr);
1771 if(ret_str)
1772 ret = jsval_string(ret_str);
1773 else
1774 hres = E_OUTOFMEMORY;
1777 jsstr_release(lstr);
1778 if(rstr)
1779 jsstr_release(rstr);
1780 }else {
1781 double nl, nr;
1783 hres = to_number(ctx, l, &nl);
1784 if(SUCCEEDED(hres)) {
1785 hres = to_number(ctx, r, &nr);
1786 if(SUCCEEDED(hres))
1787 ret = jsval_number(nl+nr);
1791 jsval_release(r);
1792 jsval_release(l);
1793 if(FAILED(hres))
1794 return hres;
1796 return stack_push(ctx, ret);
1799 /* ECMA-262 3rd Edition 11.6.2 */
1800 static HRESULT interp_sub(script_ctx_t *ctx)
1802 double l, r;
1803 HRESULT hres;
1805 TRACE("\n");
1807 hres = stack_pop_number(ctx, &r);
1808 if(FAILED(hres))
1809 return hres;
1811 hres = stack_pop_number(ctx, &l);
1812 if(FAILED(hres))
1813 return hres;
1815 return stack_push(ctx, jsval_number(l-r));
1818 /* ECMA-262 3rd Edition 11.5.1 */
1819 static HRESULT interp_mul(script_ctx_t *ctx)
1821 double l, r;
1822 HRESULT hres;
1824 TRACE("\n");
1826 hres = stack_pop_number(ctx, &r);
1827 if(FAILED(hres))
1828 return hres;
1830 hres = stack_pop_number(ctx, &l);
1831 if(FAILED(hres))
1832 return hres;
1834 return stack_push(ctx, jsval_number(l*r));
1837 /* ECMA-262 3rd Edition 11.5.2 */
1838 static HRESULT interp_div(script_ctx_t *ctx)
1840 double l, r;
1841 HRESULT hres;
1843 TRACE("\n");
1845 hres = stack_pop_number(ctx, &r);
1846 if(FAILED(hres))
1847 return hres;
1849 hres = stack_pop_number(ctx, &l);
1850 if(FAILED(hres))
1851 return hres;
1853 return stack_push(ctx, jsval_number(l/r));
1856 /* ECMA-262 3rd Edition 11.5.3 */
1857 static HRESULT interp_mod(script_ctx_t *ctx)
1859 double l, r;
1860 HRESULT hres;
1862 TRACE("\n");
1864 hres = stack_pop_number(ctx, &r);
1865 if(FAILED(hres))
1866 return hres;
1868 hres = stack_pop_number(ctx, &l);
1869 if(FAILED(hres))
1870 return hres;
1872 return stack_push(ctx, jsval_number(fmod(l, r)));
1875 /* ECMA-262 3rd Edition 11.4.2 */
1876 static HRESULT interp_delete(script_ctx_t *ctx)
1878 jsval_t objv, namev;
1879 IDispatch *obj;
1880 jsstr_t *name;
1881 BOOL ret;
1882 HRESULT hres;
1884 TRACE("\n");
1886 namev = stack_pop(ctx);
1887 objv = stack_pop(ctx);
1889 hres = to_object(ctx, objv, &obj);
1890 jsval_release(objv);
1891 if(FAILED(hres)) {
1892 jsval_release(namev);
1893 return hres;
1896 hres = to_string(ctx, namev, &name);
1897 jsval_release(namev);
1898 if(FAILED(hres)) {
1899 IDispatch_Release(obj);
1900 return hres;
1903 hres = disp_delete_name(ctx, obj, name, &ret);
1904 IDispatch_Release(obj);
1905 jsstr_release(name);
1906 if(FAILED(hres))
1907 return hres;
1909 return stack_push(ctx, jsval_bool(ret));
1912 /* ECMA-262 3rd Edition 11.4.2 */
1913 static HRESULT interp_delete_ident(script_ctx_t *ctx)
1915 const BSTR arg = get_op_bstr(ctx, 0);
1916 exprval_t exprval;
1917 BOOL ret;
1918 HRESULT hres;
1920 TRACE("%s\n", debugstr_w(arg));
1922 hres = identifier_eval(ctx, arg, &exprval);
1923 if(FAILED(hres))
1924 return hres;
1926 switch(exprval.type) {
1927 case EXPRVAL_STACK_REF:
1928 ret = FALSE;
1929 break;
1930 case EXPRVAL_IDREF:
1931 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1932 IDispatch_Release(exprval.u.idref.disp);
1933 if(FAILED(hres))
1934 return hres;
1935 break;
1936 case EXPRVAL_INVALID:
1937 ret = TRUE;
1938 break;
1939 default:
1940 FIXME("Unsupported exprval\n");
1941 exprval_release(&exprval);
1942 return E_NOTIMPL;
1946 return stack_push(ctx, jsval_bool(ret));
1949 /* ECMA-262 3rd Edition 11.4.2 */
1950 static HRESULT interp_void(script_ctx_t *ctx)
1952 TRACE("\n");
1954 stack_popn(ctx, 1);
1955 return stack_push(ctx, jsval_undefined());
1958 /* ECMA-262 3rd Edition 11.4.3 */
1959 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1961 switch(jsval_type(v)) {
1962 case JSV_UNDEFINED:
1963 *ret = L"undefined";
1964 break;
1965 case JSV_NULL:
1966 *ret = L"object";
1967 break;
1968 case JSV_OBJECT: {
1969 jsdisp_t *dispex;
1971 if(get_object(v) && (dispex = iface_to_jsdisp(get_object(v)))) {
1972 *ret = is_class(dispex, JSCLASS_FUNCTION) ? L"function" : L"object";
1973 jsdisp_release(dispex);
1974 }else {
1975 *ret = L"object";
1977 break;
1979 case JSV_STRING:
1980 *ret = L"string";
1981 break;
1982 case JSV_NUMBER:
1983 *ret = L"number";
1984 break;
1985 case JSV_BOOL:
1986 *ret = L"boolean";
1987 break;
1988 case JSV_VARIANT:
1989 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1990 return E_NOTIMPL;
1993 return S_OK;
1996 /* ECMA-262 3rd Edition 11.4.3 */
1997 static HRESULT interp_typeofid(script_ctx_t *ctx)
1999 const WCHAR *ret;
2000 exprval_t ref;
2001 jsval_t v;
2002 HRESULT hres;
2004 TRACE("\n");
2006 if(!stack_pop_exprval(ctx, &ref))
2007 return stack_push(ctx, jsval_string(jsstr_undefined()));
2009 hres = exprval_propget(ctx, &ref, &v);
2010 exprval_release(&ref);
2011 if(FAILED(hres))
2012 return stack_push_string(ctx, L"unknown");
2014 hres = typeof_string(v, &ret);
2015 jsval_release(v);
2016 if(FAILED(hres))
2017 return hres;
2019 return stack_push_string(ctx, ret);
2022 /* ECMA-262 3rd Edition 11.4.3 */
2023 static HRESULT interp_typeofident(script_ctx_t *ctx)
2025 const BSTR arg = get_op_bstr(ctx, 0);
2026 exprval_t exprval;
2027 const WCHAR *ret;
2028 jsval_t v;
2029 HRESULT hres;
2031 TRACE("%s\n", debugstr_w(arg));
2033 hres = identifier_eval(ctx, arg, &exprval);
2034 if(FAILED(hres))
2035 return hres;
2037 if(exprval.type == EXPRVAL_INVALID)
2038 return stack_push(ctx, jsval_string(jsstr_undefined()));
2040 hres = exprval_to_value(ctx, &exprval, &v);
2041 if(FAILED(hres))
2042 return hres;
2044 hres = typeof_string(v, &ret);
2045 jsval_release(v);
2046 if(FAILED(hres))
2047 return hres;
2049 return stack_push_string(ctx, ret);
2052 /* ECMA-262 3rd Edition 11.4.3 */
2053 static HRESULT interp_typeof(script_ctx_t *ctx)
2055 const WCHAR *ret;
2056 jsval_t v;
2057 HRESULT hres;
2059 TRACE("\n");
2061 v = stack_pop(ctx);
2062 hres = typeof_string(v, &ret);
2063 jsval_release(v);
2064 if(FAILED(hres))
2065 return hres;
2067 return stack_push_string(ctx, ret);
2070 /* ECMA-262 3rd Edition 11.4.7 */
2071 static HRESULT interp_minus(script_ctx_t *ctx)
2073 double n;
2074 HRESULT hres;
2076 TRACE("\n");
2078 hres = stack_pop_number(ctx, &n);
2079 if(FAILED(hres))
2080 return hres;
2082 return stack_push(ctx, jsval_number(-n));
2085 /* ECMA-262 3rd Edition 11.4.6 */
2086 static HRESULT interp_tonum(script_ctx_t *ctx)
2088 jsval_t v;
2089 double n;
2090 HRESULT hres;
2092 TRACE("\n");
2094 v = stack_pop(ctx);
2095 hres = to_number(ctx, v, &n);
2096 jsval_release(v);
2097 if(FAILED(hres))
2098 return hres;
2100 return stack_push(ctx, jsval_number(n));
2103 /* ECMA-262 3rd Edition 11.3.1 */
2104 static HRESULT interp_postinc(script_ctx_t *ctx)
2106 const int arg = get_op_int(ctx, 0);
2107 exprval_t ref;
2108 jsval_t v;
2109 HRESULT hres;
2111 TRACE("%d\n", arg);
2113 if(!stack_pop_exprval(ctx, &ref))
2114 return JS_E_OBJECT_EXPECTED;
2116 hres = exprval_propget(ctx, &ref, &v);
2117 if(SUCCEEDED(hres)) {
2118 double n;
2120 hres = to_number(ctx, v, &n);
2121 if(SUCCEEDED(hres))
2122 hres = exprval_propput(ctx, &ref, jsval_number(n+(double)arg));
2123 if(FAILED(hres))
2124 jsval_release(v);
2126 exprval_release(&ref);
2127 if(FAILED(hres))
2128 return hres;
2130 return stack_push(ctx, v);
2133 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2134 static HRESULT interp_preinc(script_ctx_t *ctx)
2136 const int arg = get_op_int(ctx, 0);
2137 exprval_t ref;
2138 double ret;
2139 jsval_t v;
2140 HRESULT hres;
2142 TRACE("%d\n", arg);
2144 if(!stack_pop_exprval(ctx, &ref))
2145 return JS_E_OBJECT_EXPECTED;
2147 hres = exprval_propget(ctx, &ref, &v);
2148 if(SUCCEEDED(hres)) {
2149 double n;
2151 hres = to_number(ctx, v, &n);
2152 jsval_release(v);
2153 if(SUCCEEDED(hres)) {
2154 ret = n+(double)arg;
2155 hres = exprval_propput(ctx, &ref, jsval_number(ret));
2158 exprval_release(&ref);
2159 if(FAILED(hres))
2160 return hres;
2162 return stack_push(ctx, jsval_number(ret));
2165 /* ECMA-262 3rd Edition 11.9.3 */
2166 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
2168 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
2169 return jsval_strict_equal(lval, rval, ret);
2171 /* FIXME: NULL disps should be handled in more general way */
2172 if(is_object_instance(lval) && !get_object(lval))
2173 return equal_values(ctx, jsval_null(), rval, ret);
2174 if(is_object_instance(rval) && !get_object(rval))
2175 return equal_values(ctx, lval, jsval_null(), ret);
2177 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
2178 *ret = TRUE;
2179 return S_OK;
2182 if(is_string(lval) && is_number(rval)) {
2183 double n;
2184 HRESULT hres;
2186 hres = to_number(ctx, lval, &n);
2187 if(FAILED(hres))
2188 return hres;
2190 /* FIXME: optimize */
2191 return equal_values(ctx, jsval_number(n), rval, ret);
2194 if(is_string(rval) && is_number(lval)) {
2195 double n;
2196 HRESULT hres;
2198 hres = to_number(ctx, rval, &n);
2199 if(FAILED(hres))
2200 return hres;
2202 /* FIXME: optimize */
2203 return equal_values(ctx, lval, jsval_number(n), ret);
2206 if(is_bool(rval))
2207 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
2209 if(is_bool(lval))
2210 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
2213 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
2214 jsval_t prim;
2215 HRESULT hres;
2217 hres = to_primitive(ctx, rval, &prim, NO_HINT);
2218 if(FAILED(hres))
2219 return hres;
2221 hres = equal_values(ctx, lval, prim, ret);
2222 jsval_release(prim);
2223 return hres;
2227 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
2228 jsval_t prim;
2229 HRESULT hres;
2231 hres = to_primitive(ctx, lval, &prim, NO_HINT);
2232 if(FAILED(hres))
2233 return hres;
2235 hres = equal_values(ctx, prim, rval, ret);
2236 jsval_release(prim);
2237 return hres;
2241 *ret = FALSE;
2242 return S_OK;
2245 /* ECMA-262 3rd Edition 11.9.1 */
2246 static HRESULT interp_eq(script_ctx_t *ctx)
2248 jsval_t l, r;
2249 BOOL b;
2250 HRESULT hres;
2252 r = stack_pop(ctx);
2253 l = stack_pop(ctx);
2255 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
2257 hres = equal_values(ctx, l, r, &b);
2258 jsval_release(l);
2259 jsval_release(r);
2260 if(FAILED(hres))
2261 return hres;
2263 return stack_push(ctx, jsval_bool(b));
2266 /* ECMA-262 3rd Edition 11.9.2 */
2267 static HRESULT interp_neq(script_ctx_t *ctx)
2269 jsval_t l, r;
2270 BOOL b;
2271 HRESULT hres;
2273 r = stack_pop(ctx);
2274 l = stack_pop(ctx);
2276 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2278 hres = equal_values(ctx, l, r, &b);
2279 jsval_release(l);
2280 jsval_release(r);
2281 if(FAILED(hres))
2282 return hres;
2284 return stack_push(ctx, jsval_bool(!b));
2287 /* ECMA-262 3rd Edition 11.9.4 */
2288 static HRESULT interp_eq2(script_ctx_t *ctx)
2290 jsval_t l, r;
2291 BOOL b;
2292 HRESULT hres;
2294 r = stack_pop(ctx);
2295 l = stack_pop(ctx);
2297 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2299 hres = jsval_strict_equal(r, l, &b);
2300 jsval_release(l);
2301 jsval_release(r);
2302 if(FAILED(hres))
2303 return hres;
2305 return stack_push(ctx, jsval_bool(b));
2308 /* ECMA-262 3rd Edition 11.9.5 */
2309 static HRESULT interp_neq2(script_ctx_t *ctx)
2311 jsval_t l, r;
2312 BOOL b;
2313 HRESULT hres;
2315 TRACE("\n");
2317 r = stack_pop(ctx);
2318 l = stack_pop(ctx);
2320 hres = jsval_strict_equal(r, l, &b);
2321 jsval_release(l);
2322 jsval_release(r);
2323 if(FAILED(hres))
2324 return hres;
2326 return stack_push(ctx, jsval_bool(!b));
2329 /* ECMA-262 3rd Edition 11.8.5 */
2330 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2332 double ln, rn;
2333 jsval_t l, r;
2334 HRESULT hres;
2336 hres = to_primitive(ctx, lval, &l, NO_HINT);
2337 if(FAILED(hres))
2338 return hres;
2340 hres = to_primitive(ctx, rval, &r, NO_HINT);
2341 if(FAILED(hres)) {
2342 jsval_release(l);
2343 return hres;
2346 if(is_string(l) && is_string(r)) {
2347 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2348 jsstr_release(get_string(l));
2349 jsstr_release(get_string(r));
2350 return S_OK;
2353 hres = to_number(ctx, l, &ln);
2354 jsval_release(l);
2355 if(SUCCEEDED(hres))
2356 hres = to_number(ctx, r, &rn);
2357 jsval_release(r);
2358 if(FAILED(hres))
2359 return hres;
2361 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2362 return S_OK;
2365 /* ECMA-262 3rd Edition 11.8.1 */
2366 static HRESULT interp_lt(script_ctx_t *ctx)
2368 jsval_t l, r;
2369 BOOL b;
2370 HRESULT hres;
2372 r = stack_pop(ctx);
2373 l = stack_pop(ctx);
2375 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2377 hres = less_eval(ctx, l, r, FALSE, &b);
2378 jsval_release(l);
2379 jsval_release(r);
2380 if(FAILED(hres))
2381 return hres;
2383 return stack_push(ctx, jsval_bool(b));
2386 /* ECMA-262 3rd Edition 11.8.1 */
2387 static HRESULT interp_lteq(script_ctx_t *ctx)
2389 jsval_t l, r;
2390 BOOL b;
2391 HRESULT hres;
2393 r = stack_pop(ctx);
2394 l = stack_pop(ctx);
2396 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2398 hres = less_eval(ctx, r, l, TRUE, &b);
2399 jsval_release(l);
2400 jsval_release(r);
2401 if(FAILED(hres))
2402 return hres;
2404 return stack_push(ctx, jsval_bool(b));
2407 /* ECMA-262 3rd Edition 11.8.2 */
2408 static HRESULT interp_gt(script_ctx_t *ctx)
2410 jsval_t l, r;
2411 BOOL b;
2412 HRESULT hres;
2414 r = stack_pop(ctx);
2415 l = stack_pop(ctx);
2417 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2419 hres = less_eval(ctx, r, l, FALSE, &b);
2420 jsval_release(l);
2421 jsval_release(r);
2422 if(FAILED(hres))
2423 return hres;
2425 return stack_push(ctx, jsval_bool(b));
2428 /* ECMA-262 3rd Edition 11.8.4 */
2429 static HRESULT interp_gteq(script_ctx_t *ctx)
2431 jsval_t l, r;
2432 BOOL b;
2433 HRESULT hres;
2435 r = stack_pop(ctx);
2436 l = stack_pop(ctx);
2438 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2440 hres = less_eval(ctx, l, r, TRUE, &b);
2441 jsval_release(l);
2442 jsval_release(r);
2443 if(FAILED(hres))
2444 return hres;
2446 return stack_push(ctx, jsval_bool(b));
2449 /* ECMA-262 3rd Edition 11.4.8 */
2450 static HRESULT interp_bneg(script_ctx_t *ctx)
2452 jsval_t v;
2453 INT i;
2454 HRESULT hres;
2456 TRACE("\n");
2458 v = stack_pop(ctx);
2459 hres = to_int32(ctx, v, &i);
2460 jsval_release(v);
2461 if(FAILED(hres))
2462 return hres;
2464 return stack_push(ctx, jsval_number(~i));
2467 /* ECMA-262 3rd Edition 11.4.9 */
2468 static HRESULT interp_neg(script_ctx_t *ctx)
2470 jsval_t v;
2471 BOOL b;
2472 HRESULT hres;
2474 TRACE("\n");
2476 v = stack_pop(ctx);
2477 hres = to_boolean(v, &b);
2478 jsval_release(v);
2479 if(FAILED(hres))
2480 return hres;
2482 return stack_push(ctx, jsval_bool(!b));
2485 /* ECMA-262 3rd Edition 11.7.1 */
2486 static HRESULT interp_lshift(script_ctx_t *ctx)
2488 DWORD r;
2489 INT l;
2490 HRESULT hres;
2492 hres = stack_pop_uint(ctx, &r);
2493 if(FAILED(hres))
2494 return hres;
2496 hres = stack_pop_int(ctx, &l);
2497 if(FAILED(hres))
2498 return hres;
2500 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2503 /* ECMA-262 3rd Edition 11.7.2 */
2504 static HRESULT interp_rshift(script_ctx_t *ctx)
2506 DWORD r;
2507 INT l;
2508 HRESULT hres;
2510 hres = stack_pop_uint(ctx, &r);
2511 if(FAILED(hres))
2512 return hres;
2514 hres = stack_pop_int(ctx, &l);
2515 if(FAILED(hres))
2516 return hres;
2518 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2521 /* ECMA-262 3rd Edition 11.7.3 */
2522 static HRESULT interp_rshift2(script_ctx_t *ctx)
2524 DWORD r, l;
2525 HRESULT hres;
2527 hres = stack_pop_uint(ctx, &r);
2528 if(FAILED(hres))
2529 return hres;
2531 hres = stack_pop_uint(ctx, &l);
2532 if(FAILED(hres))
2533 return hres;
2535 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2538 /* ECMA-262 3rd Edition 9.8 */
2539 static HRESULT interp_to_string(script_ctx_t *ctx)
2541 jsstr_t *str;
2542 jsval_t v;
2543 HRESULT hres;
2545 v = stack_pop(ctx);
2546 TRACE("%s\n", debugstr_jsval(v));
2547 hres = to_string(ctx, v, &str);
2548 jsval_release(v);
2549 if(FAILED(hres)) {
2550 WARN("failed %08x\n", hres);
2551 return hres;
2554 return stack_push(ctx, jsval_string(str));
2557 /* ECMA-262 3rd Edition 11.13.1 */
2558 static HRESULT interp_assign(script_ctx_t *ctx)
2560 exprval_t ref;
2561 jsval_t v;
2562 HRESULT hres;
2564 TRACE("\n");
2566 v = stack_pop(ctx);
2568 if(!stack_pop_exprval(ctx, &ref)) {
2569 jsval_release(v);
2570 return JS_E_ILLEGAL_ASSIGN;
2573 hres = exprval_propput(ctx, &ref, v);
2574 exprval_release(&ref);
2575 if(FAILED(hres)) {
2576 jsval_release(v);
2577 return hres;
2580 return stack_push(ctx, v);
2583 /* ECMA-262 3rd Edition 11.13.1 */
2584 static HRESULT interp_set_member(script_ctx_t *ctx)
2586 jsval_t objv, namev, value;
2587 const WCHAR *name;
2588 IDispatch *obj;
2589 HRESULT hres;
2591 value = stack_pop(ctx);
2592 namev = stack_pop(ctx);
2593 assert(is_string(namev));
2594 objv = stack_pop(ctx);
2596 TRACE("%s.%s = %s\n", debugstr_jsval(objv), debugstr_jsval(namev), debugstr_jsval(value));
2598 hres = to_object(ctx, objv, &obj);
2599 jsval_release(objv);
2600 if(SUCCEEDED(hres) && !(name = jsstr_flatten(get_string(namev)))) {
2601 IDispatch_Release(obj);
2602 hres = E_OUTOFMEMORY;
2604 if(SUCCEEDED(hres)) {
2605 hres = disp_propput_name(ctx, obj, name, value);
2606 IDispatch_Release(obj);
2607 jsstr_release(get_string(namev));
2609 if(FAILED(hres)) {
2610 WARN("failed %08x\n", hres);
2611 jsval_release(value);
2612 return hres;
2615 return stack_push(ctx, value);
2618 /* JScript extension */
2619 static HRESULT interp_assign_call(script_ctx_t *ctx)
2621 const unsigned argc = get_op_uint(ctx, 0);
2622 exprval_t ref;
2623 jsval_t v;
2624 HRESULT hres;
2626 TRACE("%u\n", argc);
2628 if(!stack_topn_exprval(ctx, argc+1, &ref))
2629 return JS_E_ILLEGAL_ASSIGN;
2631 hres = exprval_call(ctx, &ref, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2632 if(FAILED(hres))
2633 return hres;
2635 v = stack_pop(ctx);
2636 stack_popn(ctx, argc+2);
2637 return stack_push(ctx, v);
2640 static HRESULT interp_undefined(script_ctx_t *ctx)
2642 TRACE("\n");
2644 return stack_push(ctx, jsval_undefined());
2647 static HRESULT interp_jmp(script_ctx_t *ctx)
2649 const unsigned arg = get_op_uint(ctx, 0);
2651 TRACE("%u\n", arg);
2653 jmp_abs(ctx, arg);
2654 return S_OK;
2657 static HRESULT interp_jmp_z(script_ctx_t *ctx)
2659 const unsigned arg = get_op_uint(ctx, 0);
2660 BOOL b;
2661 jsval_t v;
2662 HRESULT hres;
2664 TRACE("\n");
2666 v = stack_pop(ctx);
2667 hres = to_boolean(v, &b);
2668 jsval_release(v);
2669 if(FAILED(hres))
2670 return hres;
2672 if(b)
2673 jmp_next(ctx);
2674 else
2675 jmp_abs(ctx, arg);
2676 return S_OK;
2679 static HRESULT interp_pop(script_ctx_t *ctx)
2681 const unsigned arg = get_op_uint(ctx, 0);
2683 TRACE("%u\n", arg);
2685 stack_popn(ctx, arg);
2686 return S_OK;
2689 static HRESULT interp_ret(script_ctx_t *ctx)
2691 const unsigned clear_ret = get_op_uint(ctx, 0);
2692 call_frame_t *frame = ctx->call_ctx;
2694 TRACE("\n");
2696 if(clear_ret)
2697 jsval_release(steal_ret(frame));
2699 if((frame->flags & EXEC_CONSTRUCTOR) && !is_object_instance(frame->ret)) {
2700 jsval_release(frame->ret);
2701 IDispatch_AddRef(frame->this_obj);
2702 frame->ret = jsval_disp(frame->this_obj);
2705 jmp_abs(ctx, -1);
2706 return S_OK;
2709 static HRESULT interp_setret(script_ctx_t *ctx)
2711 call_frame_t *frame = ctx->call_ctx;
2713 TRACE("\n");
2715 jsval_release(frame->ret);
2716 frame->ret = stack_pop(ctx);
2717 return S_OK;
2720 static HRESULT interp_push_acc(script_ctx_t *ctx)
2722 HRESULT hres;
2724 TRACE("\n");
2726 hres = stack_push(ctx, ctx->acc);
2727 if(SUCCEEDED(hres))
2728 ctx->acc = jsval_undefined();
2729 return hres;
2732 typedef HRESULT (*op_func_t)(script_ctx_t*);
2734 static const op_func_t op_funcs[] = {
2735 #define X(x,a,b,c) interp_##x,
2736 OP_LIST
2737 #undef X
2740 static const unsigned op_move[] = {
2741 #define X(a,x,b,c) x,
2742 OP_LIST
2743 #undef X
2746 static void pop_call_frame(script_ctx_t *ctx)
2748 call_frame_t *frame = ctx->call_ctx;
2750 frame->stack_base -= frame->pop_locals + frame->pop_variables;
2752 assert(frame->scope == frame->base_scope);
2754 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2755 if(frame->scope && frame->scope->ref > 1) {
2756 HRESULT hres = detach_variable_object(ctx, frame, TRUE);
2757 if(FAILED(hres))
2758 ERR("Failed to detach variable object: %08x\n", hres);
2761 if(frame->arguments_obj)
2762 detach_arguments_object(frame->arguments_obj);
2763 if(frame->scope)
2764 scope_release(frame->scope);
2766 if(frame->pop_variables)
2767 stack_popn(ctx, frame->pop_variables);
2768 stack_popn(ctx, frame->pop_locals);
2770 ctx->call_ctx = frame->prev_frame;
2772 if(frame->function_instance)
2773 jsdisp_release(frame->function_instance);
2774 if(frame->variable_obj)
2775 jsdisp_release(frame->variable_obj);
2776 if(frame->this_obj)
2777 IDispatch_Release(frame->this_obj);
2778 jsval_release(frame->ret);
2779 release_bytecode(frame->bytecode);
2780 heap_free(frame);
2783 static void print_backtrace(script_ctx_t *ctx)
2785 unsigned depth = 0, i, line, char_pos;
2786 call_frame_t *frame;
2788 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
2789 WARN("%u\t", depth);
2790 depth++;
2792 if(frame->this_obj)
2793 WARN("%p->", frame->this_obj);
2794 WARN("%s(", frame->function->name ? debugstr_w(frame->function->name) : "[unnamed]");
2795 if(frame->base_scope && frame->base_scope->frame) {
2796 for(i=0; i < frame->argc; i++) {
2797 if(i < frame->function->param_cnt)
2798 WARN("%s%s=%s", i ? ", " : "", debugstr_w(frame->function->params[i]),
2799 debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2800 else
2801 WARN("%s%s", i ? ", " : "", debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2803 }else {
2804 WARN("[detached frame]");
2806 line = get_location_line(frame->bytecode, frame->bytecode->instrs[frame->ip].loc, &char_pos);
2807 WARN(") context %s line %u char %u\n", wine_dbgstr_longlong(frame->bytecode->source_context), line, char_pos);
2809 if(!(frame->flags & EXEC_RETURN_TO_INTERP)) {
2810 WARN("%u\t[native code]\n", depth);
2811 depth++;
2816 static HRESULT unwind_exception(script_ctx_t *ctx, HRESULT exception_hres)
2818 except_frame_t *except_frame;
2819 jsexcept_t *ei = ctx->ei;
2820 call_frame_t *frame;
2821 jsval_t except_val;
2822 unsigned catch_off;
2823 HRESULT hres;
2825 if(WARN_ON(jscript)) {
2826 jsdisp_t *error_obj;
2827 jsval_t msg;
2829 WARN("Exception %08x %s", exception_hres, debugstr_jsval(ei->valid_value ? ei->value : jsval_undefined()));
2830 if(ei->valid_value && jsval_type(ei->value) == JSV_OBJECT) {
2831 error_obj = to_jsdisp(get_object(ei->value));
2832 if(error_obj) {
2833 hres = jsdisp_propget_name(error_obj, L"message", &msg);
2834 if(SUCCEEDED(hres)) {
2835 WARN(" (message %s)", debugstr_jsval(msg));
2836 jsval_release(msg);
2840 WARN(" in:\n");
2842 print_backtrace(ctx);
2845 frame = ctx->call_ctx;
2846 if(exception_hres != DISP_E_EXCEPTION)
2847 throw_error(ctx, exception_hres, NULL);
2848 set_error_location(ei, frame->bytecode, frame->bytecode->instrs[frame->ip].loc, IDS_RUNTIME_ERROR, NULL);
2850 while(!frame->except_frame) {
2851 DWORD flags;
2853 while(frame->scope != frame->base_scope)
2854 scope_pop(&frame->scope);
2856 stack_popn(ctx, ctx->stack_top-frame->stack_base);
2858 flags = frame->flags;
2859 pop_call_frame(ctx);
2860 if(!(flags & EXEC_RETURN_TO_INTERP))
2861 return DISP_E_EXCEPTION;
2862 frame = ctx->call_ctx;
2865 except_frame = frame->except_frame;
2866 catch_off = except_frame->catch_off;
2868 assert(except_frame->stack_top <= ctx->stack_top);
2869 stack_popn(ctx, ctx->stack_top - except_frame->stack_top);
2871 while(except_frame->scope != frame->scope)
2872 scope_pop(&frame->scope);
2874 frame->ip = catch_off ? catch_off : except_frame->finally_off;
2875 assert(!catch_off || frame->bytecode->instrs[frame->ip].op == OP_enter_catch);
2877 if(ei->valid_value) {
2878 except_val = ctx->ei->value;
2879 ei->valid_value = FALSE;
2880 }else {
2881 jsdisp_t *err;
2882 if(!(err = create_builtin_error(ctx)))
2883 return E_OUTOFMEMORY;
2884 except_val = jsval_obj(err);
2887 /* keep current except_frame if we're entering catch block with finally block associated */
2888 if(catch_off && except_frame->finally_off) {
2889 except_frame->catch_off = 0;
2890 }else {
2891 frame->except_frame = except_frame->next;
2892 heap_free(except_frame);
2895 hres = stack_push(ctx, except_val);
2896 if(FAILED(hres))
2897 return hres;
2899 if(!catch_off)
2900 hres = stack_push(ctx, jsval_bool(FALSE));
2901 return hres;
2904 static HRESULT enter_bytecode(script_ctx_t *ctx, jsval_t *r)
2906 call_frame_t *frame;
2907 jsop_t op;
2908 HRESULT hres = S_OK;
2910 TRACE("\n");
2912 while(1) {
2913 frame = ctx->call_ctx;
2914 op = frame->bytecode->instrs[frame->ip].op;
2915 hres = op_funcs[op](ctx);
2916 if(FAILED(hres)) {
2917 hres = unwind_exception(ctx, hres);
2918 if(FAILED(hres))
2919 return hres;
2920 }else if(frame->ip == -1) {
2921 const DWORD return_to_interp = frame->flags & EXEC_RETURN_TO_INTERP;
2923 assert(ctx->stack_top == frame->stack_base);
2924 assert(frame->scope == frame->base_scope);
2926 if(return_to_interp) {
2927 jsval_release(ctx->acc);
2928 ctx->acc = steal_ret(frame);
2929 }else if(r) {
2930 *r = steal_ret(frame);
2932 pop_call_frame(ctx);
2933 if(!return_to_interp)
2934 break;
2935 }else {
2936 frame->ip += op_move[op];
2940 return S_OK;
2943 static HRESULT bind_event_target(script_ctx_t *ctx, function_code_t *func, jsdisp_t *func_obj)
2945 IBindEventHandler *target;
2946 exprval_t exprval;
2947 IDispatch *disp;
2948 jsval_t v;
2949 HRESULT hres;
2951 hres = identifier_eval(ctx, func->event_target, &exprval);
2952 if(FAILED(hres))
2953 return hres;
2955 hres = exprval_to_value(ctx, &exprval, &v);
2956 if(FAILED(hres))
2957 return hres;
2959 if(!is_object_instance(v)) {
2960 FIXME("Can't bind to %s\n", debugstr_jsval(v));
2961 jsval_release(v);
2964 disp = get_object(v);
2965 hres = IDispatch_QueryInterface(disp, &IID_IBindEventHandler, (void**)&target);
2966 if(SUCCEEDED(hres)) {
2967 hres = IBindEventHandler_BindHandler(target, func->name, (IDispatch*)&func_obj->IDispatchEx_iface);
2968 IBindEventHandler_Release(target);
2969 if(FAILED(hres))
2970 WARN("BindEvent failed: %08x\n", hres);
2971 }else {
2972 FIXME("No IBindEventHandler, not yet supported binding\n");
2975 IDispatch_Release(disp);
2976 return hres;
2979 static HRESULT setup_scope(script_ctx_t *ctx, call_frame_t *frame, scope_chain_t *scope_chain, jsdisp_t *variable_object, unsigned argc, jsval_t *argv)
2981 const unsigned orig_stack = ctx->stack_top;
2982 scope_chain_t *scope;
2983 unsigned i;
2984 jsval_t v;
2985 HRESULT hres;
2987 /* If arguments are already on the stack, we may use them. */
2988 if(argv + argc == ctx->stack + ctx->stack_top) {
2989 frame->arguments_off = argv - ctx->stack;
2990 i = argc;
2991 }else {
2992 frame->arguments_off = ctx->stack_top;
2993 for(i = 0; i < argc; i++) {
2994 hres = jsval_copy(argv[i], &v);
2995 if(SUCCEEDED(hres))
2996 hres = stack_push(ctx, v);
2997 if(FAILED(hres)) {
2998 stack_popn(ctx, i);
2999 return hres;
3004 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
3005 for(; i < frame->function->param_cnt; i++) {
3006 hres = stack_push(ctx, jsval_undefined());
3007 if(FAILED(hres)) {
3008 stack_popn(ctx, ctx->stack_top - orig_stack);
3009 return hres;
3013 frame->pop_locals = ctx->stack_top - orig_stack;
3015 frame->variables_off = ctx->stack_top;
3017 for(i = 0; i < frame->function->var_cnt; i++) {
3018 hres = stack_push(ctx, jsval_undefined());
3019 if(FAILED(hres)) {
3020 stack_popn(ctx, ctx->stack_top - orig_stack);
3021 return hres;
3025 frame->pop_variables = i;
3027 hres = scope_push(scope_chain, variable_object, to_disp(variable_object), &scope);
3028 if(FAILED(hres)) {
3029 stack_popn(ctx, ctx->stack_top - orig_stack);
3030 return hres;
3033 for(i = 0; i < frame->function->func_cnt; i++) {
3034 if(frame->function->funcs[i].name && !frame->function->funcs[i].event_target) {
3035 jsdisp_t *func_obj;
3036 unsigned off;
3038 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+i, scope, &func_obj);
3039 if(FAILED(hres)) {
3040 stack_popn(ctx, ctx->stack_top - orig_stack);
3041 scope_release(scope);
3042 return hres;
3045 off = local_off(frame, frame->function->funcs[i].local_ref);
3046 jsval_release(ctx->stack[off]);
3047 ctx->stack[off] = jsval_obj(func_obj);
3051 scope->frame = frame;
3052 frame->base_scope = frame->scope = scope;
3053 return S_OK;
3056 HRESULT exec_source(script_ctx_t *ctx, DWORD flags, bytecode_t *bytecode, function_code_t *function, scope_chain_t *scope,
3057 IDispatch *this_obj, jsdisp_t *function_instance, unsigned argc, jsval_t *argv, jsval_t *r)
3059 jsdisp_t *variable_obj;
3060 call_frame_t *frame;
3061 unsigned i;
3062 HRESULT hres;
3064 if(!ctx->stack) {
3065 ctx->stack = heap_alloc(stack_size * sizeof(*ctx->stack));
3066 if(!ctx->stack)
3067 return E_OUTOFMEMORY;
3070 if(bytecode->named_item) {
3071 if(!bytecode->named_item->script_obj) {
3072 hres = create_named_item_script_obj(ctx, bytecode->named_item);
3073 if(FAILED(hres)) return hres;
3077 if(!ctx->ei->enter_notified) {
3078 ctx->ei->enter_notified = TRUE;
3079 IActiveScriptSite_OnEnterScript(ctx->site);
3082 for(i = 0; i < function->func_cnt; i++) {
3083 jsdisp_t *func_obj;
3085 if(!function->funcs[i].event_target)
3086 continue;
3088 hres = create_source_function(ctx, bytecode, function->funcs+i, scope, &func_obj);
3089 if(FAILED(hres))
3090 return hres;
3092 hres = bind_event_target(ctx, function->funcs+i, func_obj);
3093 jsdisp_release(func_obj);
3094 if(FAILED(hres))
3095 return hres;
3098 if((flags & EXEC_EVAL) && ctx->call_ctx) {
3099 variable_obj = jsdisp_addref(ctx->call_ctx->variable_obj);
3100 }else if(!(flags & (EXEC_GLOBAL | EXEC_EVAL))) {
3101 hres = create_dispex(ctx, NULL, NULL, &variable_obj);
3102 if(FAILED(hres)) return hres;
3103 }else if(bytecode->named_item) {
3104 variable_obj = jsdisp_addref(bytecode->named_item->script_obj);
3105 }else {
3106 variable_obj = jsdisp_addref(ctx->global);
3109 if(flags & (EXEC_GLOBAL | EXEC_EVAL)) {
3110 named_item_t *item = bytecode->named_item;
3111 DISPID id;
3113 for(i=0; i < function->var_cnt; i++) {
3114 TRACE("[%d] %s %d\n", i, debugstr_w(function->variables[i].name), function->variables[i].func_id);
3115 if(function->variables[i].func_id != -1) {
3116 jsdisp_t *func_obj;
3118 hres = create_source_function(ctx, bytecode, function->funcs+function->variables[i].func_id, scope, &func_obj);
3119 if(FAILED(hres))
3120 goto fail;
3122 hres = jsdisp_propput_name(variable_obj, function->variables[i].name, jsval_obj(func_obj));
3123 jsdisp_release(func_obj);
3124 continue;
3127 if(item && !(item->flags & SCRIPTITEM_CODEONLY)
3128 && SUCCEEDED(disp_get_id(ctx, item->disp, function->variables[i].name, function->variables[i].name, 0, &id)))
3129 continue;
3131 if(!item && (flags & EXEC_GLOBAL) && lookup_global_members(ctx, function->variables[i].name, NULL))
3132 continue;
3134 hres = jsdisp_get_id(variable_obj, function->variables[i].name, fdexNameEnsure, &id);
3135 if(FAILED(hres))
3136 goto fail;
3140 /* ECMA-262 3rd Edition 11.2.3.7 */
3141 if(this_obj) {
3142 jsdisp_t *jsthis;
3144 jsthis = iface_to_jsdisp(this_obj);
3145 if(jsthis) {
3146 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
3147 this_obj = NULL;
3148 jsdisp_release(jsthis);
3152 if(ctx->call_ctx && (flags & EXEC_EVAL)) {
3153 hres = detach_variable_object(ctx, ctx->call_ctx, FALSE);
3154 if(FAILED(hres))
3155 goto fail;
3158 frame = heap_alloc_zero(sizeof(*frame));
3159 if(!frame) {
3160 hres = E_OUTOFMEMORY;
3161 goto fail;
3164 frame->function = function;
3165 frame->ret = jsval_undefined();
3166 frame->argc = argc;
3167 frame->bytecode = bytecode_addref(bytecode);
3169 if(!(flags & (EXEC_GLOBAL|EXEC_EVAL))) {
3170 hres = setup_scope(ctx, frame, scope, variable_obj, argc, argv);
3171 if(FAILED(hres)) {
3172 release_bytecode(frame->bytecode);
3173 heap_free(frame);
3174 goto fail;
3176 }else if(scope) {
3177 frame->base_scope = frame->scope = scope_addref(scope);
3180 frame->ip = function->instr_off;
3181 frame->stack_base = ctx->stack_top;
3182 if(this_obj) {
3183 frame->this_obj = this_obj;
3184 IDispatch_AddRef(frame->this_obj);
3187 if(function_instance)
3188 frame->function_instance = jsdisp_addref(function_instance);
3190 frame->flags = flags;
3191 frame->variable_obj = variable_obj;
3193 frame->prev_frame = ctx->call_ctx;
3194 ctx->call_ctx = frame;
3196 if(flags & EXEC_RETURN_TO_INTERP) {
3198 * We're called directly from interpreter, so we may just setup call frame and return.
3199 * Already running interpreter will take care of execution.
3201 if(r)
3202 *r = jsval_undefined();
3203 return S_OK;
3206 return enter_bytecode(ctx, r);
3208 fail:
3209 jsdisp_release(variable_obj);
3210 return hres;