jscript: Support passing EXPRVAL_JSVAL through JS stack.
[wine/zf.git] / dlls / jscript / engine.c
blob106fdabd81c32ad24d99f8f415f0250958788889
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_JSVAL || exprval.type == EXPRVAL_INVALID) {
1295 WARN("invalid ref\n");
1296 exprval_release(&exprval);
1297 exprval_set_exception(&exprval, JS_E_OBJECT_EXPECTED);
1300 return stack_push_exprval(ctx, &exprval);
1303 static HRESULT identifier_value(script_ctx_t *ctx, BSTR identifier)
1305 exprval_t exprval;
1306 jsval_t v;
1307 HRESULT hres;
1309 hres = identifier_eval(ctx, identifier, &exprval);
1310 if(FAILED(hres))
1311 return hres;
1313 if(exprval.type == EXPRVAL_INVALID)
1314 return throw_error(ctx, exprval.u.hres, identifier);
1316 hres = exprval_to_value(ctx, &exprval, &v);
1317 if(FAILED(hres))
1318 return hres;
1320 return stack_push(ctx, v);
1323 static HRESULT interp_local_ref(script_ctx_t *ctx)
1325 const int arg = get_op_int(ctx, 0);
1326 const unsigned flags = get_op_uint(ctx, 1);
1327 call_frame_t *frame = ctx->call_ctx;
1328 exprval_t ref;
1330 TRACE("%s\n", debugstr_w(local_name(frame, arg)));
1332 if(!frame->base_scope || !frame->base_scope->frame)
1333 return interp_identifier_ref(ctx, local_name(frame, arg), flags);
1335 ref.type = EXPRVAL_STACK_REF;
1336 ref.u.off = local_off(frame, arg);
1337 return stack_push_exprval(ctx, &ref);
1340 static HRESULT interp_local(script_ctx_t *ctx)
1342 const int arg = get_op_int(ctx, 0);
1343 call_frame_t *frame = ctx->call_ctx;
1344 jsval_t copy;
1345 HRESULT hres;
1347 if(!frame->base_scope || !frame->base_scope->frame) {
1348 TRACE("%s\n", debugstr_w(local_name(frame, arg)));
1349 return identifier_value(ctx, local_name(frame, arg));
1352 hres = jsval_copy(ctx->stack[local_off(frame, arg)], &copy);
1353 if(FAILED(hres))
1354 return hres;
1356 TRACE("%s: %s\n", debugstr_w(local_name(frame, arg)), debugstr_jsval(copy));
1357 return stack_push(ctx, copy);
1360 /* ECMA-262 3rd Edition 10.1.4 */
1361 static HRESULT interp_ident(script_ctx_t *ctx)
1363 const BSTR arg = get_op_bstr(ctx, 0);
1365 TRACE("%s\n", debugstr_w(arg));
1367 return identifier_value(ctx, arg);
1370 /* ECMA-262 3rd Edition 10.1.4 */
1371 static HRESULT interp_identid(script_ctx_t *ctx)
1373 const BSTR arg = get_op_bstr(ctx, 0);
1374 const unsigned flags = get_op_uint(ctx, 1);
1376 TRACE("%s %x\n", debugstr_w(arg), flags);
1378 return interp_identifier_ref(ctx, arg, flags);
1381 /* ECMA-262 3rd Edition 7.8.1 */
1382 static HRESULT interp_null(script_ctx_t *ctx)
1384 TRACE("\n");
1386 return stack_push(ctx, jsval_null());
1389 /* ECMA-262 3rd Edition 7.8.2 */
1390 static HRESULT interp_bool(script_ctx_t *ctx)
1392 const int arg = get_op_int(ctx, 0);
1394 TRACE("%s\n", arg ? "true" : "false");
1396 return stack_push(ctx, jsval_bool(arg));
1399 /* ECMA-262 3rd Edition 7.8.3 */
1400 static HRESULT interp_int(script_ctx_t *ctx)
1402 const int arg = get_op_int(ctx, 0);
1404 TRACE("%d\n", arg);
1406 return stack_push(ctx, jsval_number(arg));
1409 /* ECMA-262 3rd Edition 7.8.3 */
1410 static HRESULT interp_double(script_ctx_t *ctx)
1412 const double arg = get_op_double(ctx);
1414 TRACE("%lf\n", arg);
1416 return stack_push(ctx, jsval_number(arg));
1419 /* ECMA-262 3rd Edition 7.8.4 */
1420 static HRESULT interp_str(script_ctx_t *ctx)
1422 jsstr_t *str = get_op_str(ctx, 0);
1424 TRACE("%s\n", debugstr_jsstr(str));
1426 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1429 /* ECMA-262 3rd Edition 7.8 */
1430 static HRESULT interp_regexp(script_ctx_t *ctx)
1432 jsstr_t *source = get_op_str(ctx, 0);
1433 const unsigned flags = get_op_uint(ctx, 1);
1434 jsdisp_t *regexp;
1435 HRESULT hres;
1437 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1439 hres = create_regexp(ctx, source, flags, &regexp);
1440 if(FAILED(hres))
1441 return hres;
1443 return stack_push(ctx, jsval_obj(regexp));
1446 /* ECMA-262 3rd Edition 11.1.4 */
1447 static HRESULT interp_carray(script_ctx_t *ctx)
1449 const unsigned arg = get_op_uint(ctx, 0);
1450 jsdisp_t *array;
1451 HRESULT hres;
1453 TRACE("%u\n", arg);
1455 hres = create_array(ctx, arg, &array);
1456 if(FAILED(hres))
1457 return hres;
1459 return stack_push(ctx, jsval_obj(array));
1462 static HRESULT interp_carray_set(script_ctx_t *ctx)
1464 const unsigned index = get_op_uint(ctx, 0);
1465 jsval_t value, array;
1466 HRESULT hres;
1468 value = stack_pop(ctx);
1470 TRACE("[%u] = %s\n", index, debugstr_jsval(value));
1472 array = stack_top(ctx);
1473 assert(is_object_instance(array));
1475 hres = jsdisp_propput_idx(iface_to_jsdisp(get_object(array)), index, value);
1476 jsval_release(value);
1477 return hres;
1480 /* ECMA-262 3rd Edition 11.1.5 */
1481 static HRESULT interp_new_obj(script_ctx_t *ctx)
1483 jsdisp_t *obj;
1484 HRESULT hres;
1486 TRACE("\n");
1488 hres = create_object(ctx, NULL, &obj);
1489 if(FAILED(hres))
1490 return hres;
1492 return stack_push(ctx, jsval_obj(obj));
1495 /* ECMA-262 3rd Edition 11.1.5 */
1496 static HRESULT interp_obj_prop(script_ctx_t *ctx)
1498 jsstr_t *name_arg = get_op_str(ctx, 0);
1499 unsigned type = get_op_uint(ctx, 1);
1500 const WCHAR *name;
1501 jsdisp_t *obj;
1502 jsval_t val;
1503 HRESULT hres;
1505 TRACE("%s\n", debugstr_jsstr(name_arg));
1507 val = stack_pop(ctx);
1509 /* FIXME: we should pass it as jsstr_t */
1510 name = jsstr_flatten(name_arg);
1512 assert(is_object_instance(stack_top(ctx)));
1513 obj = as_jsdisp(get_object(stack_top(ctx)));
1515 if(type == PROPERTY_DEFINITION_VALUE) {
1516 hres = jsdisp_propput_name(obj, name, val);
1517 }else {
1518 property_desc_t desc = {PROPF_ENUMERABLE | PROPF_CONFIGURABLE};
1519 jsdisp_t *func;
1521 assert(is_object_instance(val));
1522 func = iface_to_jsdisp(get_object(val));
1524 desc.mask = desc.flags;
1525 if(type == PROPERTY_DEFINITION_GETTER) {
1526 desc.explicit_getter = TRUE;
1527 desc.getter = func;
1528 }else {
1529 desc.explicit_setter = TRUE;
1530 desc.setter = func;
1533 hres = jsdisp_define_property(obj, name, &desc);
1534 jsdisp_release(func);
1537 jsval_release(val);
1538 return hres;
1541 /* ECMA-262 3rd Edition 11.11 */
1542 static HRESULT interp_cnd_nz(script_ctx_t *ctx)
1544 const unsigned arg = get_op_uint(ctx, 0);
1545 BOOL b;
1546 HRESULT hres;
1548 TRACE("\n");
1550 hres = to_boolean(stack_top(ctx), &b);
1551 if(FAILED(hres))
1552 return hres;
1554 if(b) {
1555 jmp_abs(ctx, arg);
1556 }else {
1557 stack_popn(ctx, 1);
1558 jmp_next(ctx);
1560 return S_OK;
1563 /* ECMA-262 3rd Edition 11.11 */
1564 static HRESULT interp_cnd_z(script_ctx_t *ctx)
1566 const unsigned arg = get_op_uint(ctx, 0);
1567 BOOL b;
1568 HRESULT hres;
1570 TRACE("\n");
1572 hres = to_boolean(stack_top(ctx), &b);
1573 if(FAILED(hres))
1574 return hres;
1576 if(b) {
1577 stack_popn(ctx, 1);
1578 jmp_next(ctx);
1579 }else {
1580 jmp_abs(ctx, arg);
1582 return S_OK;
1585 /* ECMA-262 3rd Edition 11.10 */
1586 static HRESULT interp_or(script_ctx_t *ctx)
1588 INT l, r;
1589 HRESULT hres;
1591 TRACE("\n");
1593 hres = stack_pop_int(ctx, &r);
1594 if(FAILED(hres))
1595 return hres;
1597 hres = stack_pop_int(ctx, &l);
1598 if(FAILED(hres))
1599 return hres;
1601 return stack_push(ctx, jsval_number(l|r));
1604 /* ECMA-262 3rd Edition 11.10 */
1605 static HRESULT interp_xor(script_ctx_t *ctx)
1607 INT l, r;
1608 HRESULT hres;
1610 TRACE("\n");
1612 hres = stack_pop_int(ctx, &r);
1613 if(FAILED(hres))
1614 return hres;
1616 hres = stack_pop_int(ctx, &l);
1617 if(FAILED(hres))
1618 return hres;
1620 return stack_push(ctx, jsval_number(l^r));
1623 /* ECMA-262 3rd Edition 11.10 */
1624 static HRESULT interp_and(script_ctx_t *ctx)
1626 INT l, r;
1627 HRESULT hres;
1629 TRACE("\n");
1631 hres = stack_pop_int(ctx, &r);
1632 if(FAILED(hres))
1633 return hres;
1635 hres = stack_pop_int(ctx, &l);
1636 if(FAILED(hres))
1637 return hres;
1639 return stack_push(ctx, jsval_number(l&r));
1642 /* ECMA-262 3rd Edition 11.8.6 */
1643 static HRESULT interp_instanceof(script_ctx_t *ctx)
1645 jsdisp_t *obj, *iter, *tmp = NULL;
1646 jsval_t prot, v;
1647 BOOL ret = FALSE;
1648 HRESULT hres;
1650 v = stack_pop(ctx);
1651 if(!is_object_instance(v) || !get_object(v)) {
1652 jsval_release(v);
1653 return JS_E_FUNCTION_EXPECTED;
1656 obj = iface_to_jsdisp(get_object(v));
1657 IDispatch_Release(get_object(v));
1658 if(!obj) {
1659 FIXME("non-jsdisp objects not supported\n");
1660 return E_FAIL;
1663 if(is_class(obj, JSCLASS_FUNCTION)) {
1664 hres = jsdisp_propget_name(obj, L"prototype", &prot);
1665 }else {
1666 hres = JS_E_FUNCTION_EXPECTED;
1668 jsdisp_release(obj);
1669 if(FAILED(hres))
1670 return hres;
1672 v = stack_pop(ctx);
1674 if(is_object_instance(prot)) {
1675 if(is_object_instance(v))
1676 tmp = iface_to_jsdisp(get_object(v));
1677 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1678 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1679 if(FAILED(hres))
1680 break;
1683 if(tmp)
1684 jsdisp_release(tmp);
1685 }else {
1686 FIXME("prototype is not an object\n");
1687 hres = E_FAIL;
1690 jsval_release(prot);
1691 jsval_release(v);
1692 if(FAILED(hres))
1693 return hres;
1695 return stack_push(ctx, jsval_bool(ret));
1698 /* ECMA-262 3rd Edition 11.8.7 */
1699 static HRESULT interp_in(script_ctx_t *ctx)
1701 const WCHAR *str;
1702 jsstr_t *jsstr;
1703 jsval_t obj, v;
1704 DISPID id = 0;
1705 BOOL ret;
1706 HRESULT hres;
1708 TRACE("\n");
1710 obj = stack_pop(ctx);
1711 if(!is_object_instance(obj) || !get_object(obj)) {
1712 jsval_release(obj);
1713 return JS_E_OBJECT_EXPECTED;
1716 v = stack_pop(ctx);
1717 hres = to_flat_string(ctx, v, &jsstr, &str);
1718 jsval_release(v);
1719 if(FAILED(hres)) {
1720 IDispatch_Release(get_object(obj));
1721 return hres;
1724 hres = disp_get_id(ctx, get_object(obj), str, NULL, 0, &id);
1725 IDispatch_Release(get_object(obj));
1726 jsstr_release(jsstr);
1727 if(SUCCEEDED(hres))
1728 ret = TRUE;
1729 else if(hres == DISP_E_UNKNOWNNAME)
1730 ret = FALSE;
1731 else
1732 return hres;
1734 return stack_push(ctx, jsval_bool(ret));
1737 /* ECMA-262 3rd Edition 11.6.1 */
1738 static HRESULT interp_add(script_ctx_t *ctx)
1740 jsval_t l, r, lval, rval, ret;
1741 HRESULT hres;
1743 rval = stack_pop(ctx);
1744 lval = stack_pop(ctx);
1746 TRACE("%s + %s\n", debugstr_jsval(lval), debugstr_jsval(rval));
1748 hres = to_primitive(ctx, lval, &l, NO_HINT);
1749 if(SUCCEEDED(hres)) {
1750 hres = to_primitive(ctx, rval, &r, NO_HINT);
1751 if(FAILED(hres))
1752 jsval_release(l);
1754 jsval_release(lval);
1755 jsval_release(rval);
1756 if(FAILED(hres))
1757 return hres;
1759 if(is_string(l) || is_string(r)) {
1760 jsstr_t *lstr, *rstr = NULL;
1762 hres = to_string(ctx, l, &lstr);
1763 if(SUCCEEDED(hres))
1764 hres = to_string(ctx, r, &rstr);
1766 if(SUCCEEDED(hres)) {
1767 jsstr_t *ret_str;
1769 ret_str = jsstr_concat(lstr, rstr);
1770 if(ret_str)
1771 ret = jsval_string(ret_str);
1772 else
1773 hres = E_OUTOFMEMORY;
1776 jsstr_release(lstr);
1777 if(rstr)
1778 jsstr_release(rstr);
1779 }else {
1780 double nl, nr;
1782 hres = to_number(ctx, l, &nl);
1783 if(SUCCEEDED(hres)) {
1784 hres = to_number(ctx, r, &nr);
1785 if(SUCCEEDED(hres))
1786 ret = jsval_number(nl+nr);
1790 jsval_release(r);
1791 jsval_release(l);
1792 if(FAILED(hres))
1793 return hres;
1795 return stack_push(ctx, ret);
1798 /* ECMA-262 3rd Edition 11.6.2 */
1799 static HRESULT interp_sub(script_ctx_t *ctx)
1801 double l, r;
1802 HRESULT hres;
1804 TRACE("\n");
1806 hres = stack_pop_number(ctx, &r);
1807 if(FAILED(hres))
1808 return hres;
1810 hres = stack_pop_number(ctx, &l);
1811 if(FAILED(hres))
1812 return hres;
1814 return stack_push(ctx, jsval_number(l-r));
1817 /* ECMA-262 3rd Edition 11.5.1 */
1818 static HRESULT interp_mul(script_ctx_t *ctx)
1820 double l, r;
1821 HRESULT hres;
1823 TRACE("\n");
1825 hres = stack_pop_number(ctx, &r);
1826 if(FAILED(hres))
1827 return hres;
1829 hres = stack_pop_number(ctx, &l);
1830 if(FAILED(hres))
1831 return hres;
1833 return stack_push(ctx, jsval_number(l*r));
1836 /* ECMA-262 3rd Edition 11.5.2 */
1837 static HRESULT interp_div(script_ctx_t *ctx)
1839 double l, r;
1840 HRESULT hres;
1842 TRACE("\n");
1844 hres = stack_pop_number(ctx, &r);
1845 if(FAILED(hres))
1846 return hres;
1848 hres = stack_pop_number(ctx, &l);
1849 if(FAILED(hres))
1850 return hres;
1852 return stack_push(ctx, jsval_number(l/r));
1855 /* ECMA-262 3rd Edition 11.5.3 */
1856 static HRESULT interp_mod(script_ctx_t *ctx)
1858 double l, r;
1859 HRESULT hres;
1861 TRACE("\n");
1863 hres = stack_pop_number(ctx, &r);
1864 if(FAILED(hres))
1865 return hres;
1867 hres = stack_pop_number(ctx, &l);
1868 if(FAILED(hres))
1869 return hres;
1871 return stack_push(ctx, jsval_number(fmod(l, r)));
1874 /* ECMA-262 3rd Edition 11.4.2 */
1875 static HRESULT interp_delete(script_ctx_t *ctx)
1877 jsval_t objv, namev;
1878 IDispatch *obj;
1879 jsstr_t *name;
1880 BOOL ret;
1881 HRESULT hres;
1883 TRACE("\n");
1885 namev = stack_pop(ctx);
1886 objv = stack_pop(ctx);
1888 hres = to_object(ctx, objv, &obj);
1889 jsval_release(objv);
1890 if(FAILED(hres)) {
1891 jsval_release(namev);
1892 return hres;
1895 hres = to_string(ctx, namev, &name);
1896 jsval_release(namev);
1897 if(FAILED(hres)) {
1898 IDispatch_Release(obj);
1899 return hres;
1902 hres = disp_delete_name(ctx, obj, name, &ret);
1903 IDispatch_Release(obj);
1904 jsstr_release(name);
1905 if(FAILED(hres))
1906 return hres;
1908 return stack_push(ctx, jsval_bool(ret));
1911 /* ECMA-262 3rd Edition 11.4.2 */
1912 static HRESULT interp_delete_ident(script_ctx_t *ctx)
1914 const BSTR arg = get_op_bstr(ctx, 0);
1915 exprval_t exprval;
1916 BOOL ret;
1917 HRESULT hres;
1919 TRACE("%s\n", debugstr_w(arg));
1921 hres = identifier_eval(ctx, arg, &exprval);
1922 if(FAILED(hres))
1923 return hres;
1925 switch(exprval.type) {
1926 case EXPRVAL_STACK_REF:
1927 ret = FALSE;
1928 break;
1929 case EXPRVAL_IDREF:
1930 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1931 IDispatch_Release(exprval.u.idref.disp);
1932 if(FAILED(hres))
1933 return hres;
1934 break;
1935 case EXPRVAL_INVALID:
1936 ret = TRUE;
1937 break;
1938 default:
1939 FIXME("Unsupported exprval\n");
1940 exprval_release(&exprval);
1941 return E_NOTIMPL;
1945 return stack_push(ctx, jsval_bool(ret));
1948 /* ECMA-262 3rd Edition 11.4.2 */
1949 static HRESULT interp_void(script_ctx_t *ctx)
1951 TRACE("\n");
1953 stack_popn(ctx, 1);
1954 return stack_push(ctx, jsval_undefined());
1957 /* ECMA-262 3rd Edition 11.4.3 */
1958 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1960 switch(jsval_type(v)) {
1961 case JSV_UNDEFINED:
1962 *ret = L"undefined";
1963 break;
1964 case JSV_NULL:
1965 *ret = L"object";
1966 break;
1967 case JSV_OBJECT: {
1968 jsdisp_t *dispex;
1970 if(get_object(v) && (dispex = iface_to_jsdisp(get_object(v)))) {
1971 *ret = is_class(dispex, JSCLASS_FUNCTION) ? L"function" : L"object";
1972 jsdisp_release(dispex);
1973 }else {
1974 *ret = L"object";
1976 break;
1978 case JSV_STRING:
1979 *ret = L"string";
1980 break;
1981 case JSV_NUMBER:
1982 *ret = L"number";
1983 break;
1984 case JSV_BOOL:
1985 *ret = L"boolean";
1986 break;
1987 case JSV_VARIANT:
1988 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1989 return E_NOTIMPL;
1992 return S_OK;
1995 /* ECMA-262 3rd Edition 11.4.3 */
1996 static HRESULT interp_typeofid(script_ctx_t *ctx)
1998 const WCHAR *ret;
1999 exprval_t ref;
2000 jsval_t v;
2001 HRESULT hres;
2003 TRACE("\n");
2005 if(!stack_pop_exprval(ctx, &ref))
2006 return stack_push(ctx, jsval_string(jsstr_undefined()));
2008 hres = exprval_propget(ctx, &ref, &v);
2009 exprval_release(&ref);
2010 if(FAILED(hres))
2011 return stack_push_string(ctx, L"unknown");
2013 hres = typeof_string(v, &ret);
2014 jsval_release(v);
2015 if(FAILED(hres))
2016 return hres;
2018 return stack_push_string(ctx, ret);
2021 /* ECMA-262 3rd Edition 11.4.3 */
2022 static HRESULT interp_typeofident(script_ctx_t *ctx)
2024 const BSTR arg = get_op_bstr(ctx, 0);
2025 exprval_t exprval;
2026 const WCHAR *ret;
2027 jsval_t v;
2028 HRESULT hres;
2030 TRACE("%s\n", debugstr_w(arg));
2032 hres = identifier_eval(ctx, arg, &exprval);
2033 if(FAILED(hres))
2034 return hres;
2036 if(exprval.type == EXPRVAL_INVALID)
2037 return stack_push(ctx, jsval_string(jsstr_undefined()));
2039 hres = exprval_to_value(ctx, &exprval, &v);
2040 if(FAILED(hres))
2041 return hres;
2043 hres = typeof_string(v, &ret);
2044 jsval_release(v);
2045 if(FAILED(hres))
2046 return hres;
2048 return stack_push_string(ctx, ret);
2051 /* ECMA-262 3rd Edition 11.4.3 */
2052 static HRESULT interp_typeof(script_ctx_t *ctx)
2054 const WCHAR *ret;
2055 jsval_t v;
2056 HRESULT hres;
2058 TRACE("\n");
2060 v = stack_pop(ctx);
2061 hres = typeof_string(v, &ret);
2062 jsval_release(v);
2063 if(FAILED(hres))
2064 return hres;
2066 return stack_push_string(ctx, ret);
2069 /* ECMA-262 3rd Edition 11.4.7 */
2070 static HRESULT interp_minus(script_ctx_t *ctx)
2072 double n;
2073 HRESULT hres;
2075 TRACE("\n");
2077 hres = stack_pop_number(ctx, &n);
2078 if(FAILED(hres))
2079 return hres;
2081 return stack_push(ctx, jsval_number(-n));
2084 /* ECMA-262 3rd Edition 11.4.6 */
2085 static HRESULT interp_tonum(script_ctx_t *ctx)
2087 jsval_t v;
2088 double n;
2089 HRESULT hres;
2091 TRACE("\n");
2093 v = stack_pop(ctx);
2094 hres = to_number(ctx, v, &n);
2095 jsval_release(v);
2096 if(FAILED(hres))
2097 return hres;
2099 return stack_push(ctx, jsval_number(n));
2102 /* ECMA-262 3rd Edition 11.3.1 */
2103 static HRESULT interp_postinc(script_ctx_t *ctx)
2105 const int arg = get_op_int(ctx, 0);
2106 exprval_t ref;
2107 jsval_t v;
2108 HRESULT hres;
2110 TRACE("%d\n", arg);
2112 if(!stack_pop_exprval(ctx, &ref))
2113 return JS_E_OBJECT_EXPECTED;
2115 hres = exprval_propget(ctx, &ref, &v);
2116 if(SUCCEEDED(hres)) {
2117 double n;
2119 hres = to_number(ctx, v, &n);
2120 if(SUCCEEDED(hres))
2121 hres = exprval_propput(ctx, &ref, jsval_number(n+(double)arg));
2122 if(FAILED(hres))
2123 jsval_release(v);
2125 exprval_release(&ref);
2126 if(FAILED(hres))
2127 return hres;
2129 return stack_push(ctx, v);
2132 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2133 static HRESULT interp_preinc(script_ctx_t *ctx)
2135 const int arg = get_op_int(ctx, 0);
2136 exprval_t ref;
2137 double ret;
2138 jsval_t v;
2139 HRESULT hres;
2141 TRACE("%d\n", arg);
2143 if(!stack_pop_exprval(ctx, &ref))
2144 return JS_E_OBJECT_EXPECTED;
2146 hres = exprval_propget(ctx, &ref, &v);
2147 if(SUCCEEDED(hres)) {
2148 double n;
2150 hres = to_number(ctx, v, &n);
2151 jsval_release(v);
2152 if(SUCCEEDED(hres)) {
2153 ret = n+(double)arg;
2154 hres = exprval_propput(ctx, &ref, jsval_number(ret));
2157 exprval_release(&ref);
2158 if(FAILED(hres))
2159 return hres;
2161 return stack_push(ctx, jsval_number(ret));
2164 /* ECMA-262 3rd Edition 11.9.3 */
2165 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
2167 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
2168 return jsval_strict_equal(lval, rval, ret);
2170 /* FIXME: NULL disps should be handled in more general way */
2171 if(is_object_instance(lval) && !get_object(lval))
2172 return equal_values(ctx, jsval_null(), rval, ret);
2173 if(is_object_instance(rval) && !get_object(rval))
2174 return equal_values(ctx, lval, jsval_null(), ret);
2176 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
2177 *ret = TRUE;
2178 return S_OK;
2181 if(is_string(lval) && is_number(rval)) {
2182 double n;
2183 HRESULT hres;
2185 hres = to_number(ctx, lval, &n);
2186 if(FAILED(hres))
2187 return hres;
2189 /* FIXME: optimize */
2190 return equal_values(ctx, jsval_number(n), rval, ret);
2193 if(is_string(rval) && is_number(lval)) {
2194 double n;
2195 HRESULT hres;
2197 hres = to_number(ctx, rval, &n);
2198 if(FAILED(hres))
2199 return hres;
2201 /* FIXME: optimize */
2202 return equal_values(ctx, lval, jsval_number(n), ret);
2205 if(is_bool(rval))
2206 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
2208 if(is_bool(lval))
2209 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
2212 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
2213 jsval_t prim;
2214 HRESULT hres;
2216 hres = to_primitive(ctx, rval, &prim, NO_HINT);
2217 if(FAILED(hres))
2218 return hres;
2220 hres = equal_values(ctx, lval, prim, ret);
2221 jsval_release(prim);
2222 return hres;
2226 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
2227 jsval_t prim;
2228 HRESULT hres;
2230 hres = to_primitive(ctx, lval, &prim, NO_HINT);
2231 if(FAILED(hres))
2232 return hres;
2234 hres = equal_values(ctx, prim, rval, ret);
2235 jsval_release(prim);
2236 return hres;
2240 *ret = FALSE;
2241 return S_OK;
2244 /* ECMA-262 3rd Edition 11.9.1 */
2245 static HRESULT interp_eq(script_ctx_t *ctx)
2247 jsval_t l, r;
2248 BOOL b;
2249 HRESULT hres;
2251 r = stack_pop(ctx);
2252 l = stack_pop(ctx);
2254 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
2256 hres = equal_values(ctx, l, r, &b);
2257 jsval_release(l);
2258 jsval_release(r);
2259 if(FAILED(hres))
2260 return hres;
2262 return stack_push(ctx, jsval_bool(b));
2265 /* ECMA-262 3rd Edition 11.9.2 */
2266 static HRESULT interp_neq(script_ctx_t *ctx)
2268 jsval_t l, r;
2269 BOOL b;
2270 HRESULT hres;
2272 r = stack_pop(ctx);
2273 l = stack_pop(ctx);
2275 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2277 hres = equal_values(ctx, l, r, &b);
2278 jsval_release(l);
2279 jsval_release(r);
2280 if(FAILED(hres))
2281 return hres;
2283 return stack_push(ctx, jsval_bool(!b));
2286 /* ECMA-262 3rd Edition 11.9.4 */
2287 static HRESULT interp_eq2(script_ctx_t *ctx)
2289 jsval_t l, r;
2290 BOOL b;
2291 HRESULT hres;
2293 r = stack_pop(ctx);
2294 l = stack_pop(ctx);
2296 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2298 hres = jsval_strict_equal(r, l, &b);
2299 jsval_release(l);
2300 jsval_release(r);
2301 if(FAILED(hres))
2302 return hres;
2304 return stack_push(ctx, jsval_bool(b));
2307 /* ECMA-262 3rd Edition 11.9.5 */
2308 static HRESULT interp_neq2(script_ctx_t *ctx)
2310 jsval_t l, r;
2311 BOOL b;
2312 HRESULT hres;
2314 TRACE("\n");
2316 r = stack_pop(ctx);
2317 l = stack_pop(ctx);
2319 hres = jsval_strict_equal(r, l, &b);
2320 jsval_release(l);
2321 jsval_release(r);
2322 if(FAILED(hres))
2323 return hres;
2325 return stack_push(ctx, jsval_bool(!b));
2328 /* ECMA-262 3rd Edition 11.8.5 */
2329 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2331 double ln, rn;
2332 jsval_t l, r;
2333 HRESULT hres;
2335 hres = to_primitive(ctx, lval, &l, NO_HINT);
2336 if(FAILED(hres))
2337 return hres;
2339 hres = to_primitive(ctx, rval, &r, NO_HINT);
2340 if(FAILED(hres)) {
2341 jsval_release(l);
2342 return hres;
2345 if(is_string(l) && is_string(r)) {
2346 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2347 jsstr_release(get_string(l));
2348 jsstr_release(get_string(r));
2349 return S_OK;
2352 hres = to_number(ctx, l, &ln);
2353 jsval_release(l);
2354 if(SUCCEEDED(hres))
2355 hres = to_number(ctx, r, &rn);
2356 jsval_release(r);
2357 if(FAILED(hres))
2358 return hres;
2360 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2361 return S_OK;
2364 /* ECMA-262 3rd Edition 11.8.1 */
2365 static HRESULT interp_lt(script_ctx_t *ctx)
2367 jsval_t l, r;
2368 BOOL b;
2369 HRESULT hres;
2371 r = stack_pop(ctx);
2372 l = stack_pop(ctx);
2374 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2376 hres = less_eval(ctx, l, r, FALSE, &b);
2377 jsval_release(l);
2378 jsval_release(r);
2379 if(FAILED(hres))
2380 return hres;
2382 return stack_push(ctx, jsval_bool(b));
2385 /* ECMA-262 3rd Edition 11.8.1 */
2386 static HRESULT interp_lteq(script_ctx_t *ctx)
2388 jsval_t l, r;
2389 BOOL b;
2390 HRESULT hres;
2392 r = stack_pop(ctx);
2393 l = stack_pop(ctx);
2395 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2397 hres = less_eval(ctx, r, l, TRUE, &b);
2398 jsval_release(l);
2399 jsval_release(r);
2400 if(FAILED(hres))
2401 return hres;
2403 return stack_push(ctx, jsval_bool(b));
2406 /* ECMA-262 3rd Edition 11.8.2 */
2407 static HRESULT interp_gt(script_ctx_t *ctx)
2409 jsval_t l, r;
2410 BOOL b;
2411 HRESULT hres;
2413 r = stack_pop(ctx);
2414 l = stack_pop(ctx);
2416 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2418 hres = less_eval(ctx, r, l, FALSE, &b);
2419 jsval_release(l);
2420 jsval_release(r);
2421 if(FAILED(hres))
2422 return hres;
2424 return stack_push(ctx, jsval_bool(b));
2427 /* ECMA-262 3rd Edition 11.8.4 */
2428 static HRESULT interp_gteq(script_ctx_t *ctx)
2430 jsval_t l, r;
2431 BOOL b;
2432 HRESULT hres;
2434 r = stack_pop(ctx);
2435 l = stack_pop(ctx);
2437 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2439 hres = less_eval(ctx, l, r, TRUE, &b);
2440 jsval_release(l);
2441 jsval_release(r);
2442 if(FAILED(hres))
2443 return hres;
2445 return stack_push(ctx, jsval_bool(b));
2448 /* ECMA-262 3rd Edition 11.4.8 */
2449 static HRESULT interp_bneg(script_ctx_t *ctx)
2451 jsval_t v;
2452 INT i;
2453 HRESULT hres;
2455 TRACE("\n");
2457 v = stack_pop(ctx);
2458 hres = to_int32(ctx, v, &i);
2459 jsval_release(v);
2460 if(FAILED(hres))
2461 return hres;
2463 return stack_push(ctx, jsval_number(~i));
2466 /* ECMA-262 3rd Edition 11.4.9 */
2467 static HRESULT interp_neg(script_ctx_t *ctx)
2469 jsval_t v;
2470 BOOL b;
2471 HRESULT hres;
2473 TRACE("\n");
2475 v = stack_pop(ctx);
2476 hres = to_boolean(v, &b);
2477 jsval_release(v);
2478 if(FAILED(hres))
2479 return hres;
2481 return stack_push(ctx, jsval_bool(!b));
2484 /* ECMA-262 3rd Edition 11.7.1 */
2485 static HRESULT interp_lshift(script_ctx_t *ctx)
2487 DWORD r;
2488 INT l;
2489 HRESULT hres;
2491 hres = stack_pop_uint(ctx, &r);
2492 if(FAILED(hres))
2493 return hres;
2495 hres = stack_pop_int(ctx, &l);
2496 if(FAILED(hres))
2497 return hres;
2499 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2502 /* ECMA-262 3rd Edition 11.7.2 */
2503 static HRESULT interp_rshift(script_ctx_t *ctx)
2505 DWORD r;
2506 INT l;
2507 HRESULT hres;
2509 hres = stack_pop_uint(ctx, &r);
2510 if(FAILED(hres))
2511 return hres;
2513 hres = stack_pop_int(ctx, &l);
2514 if(FAILED(hres))
2515 return hres;
2517 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2520 /* ECMA-262 3rd Edition 11.7.3 */
2521 static HRESULT interp_rshift2(script_ctx_t *ctx)
2523 DWORD r, l;
2524 HRESULT hres;
2526 hres = stack_pop_uint(ctx, &r);
2527 if(FAILED(hres))
2528 return hres;
2530 hres = stack_pop_uint(ctx, &l);
2531 if(FAILED(hres))
2532 return hres;
2534 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2537 /* ECMA-262 3rd Edition 9.8 */
2538 static HRESULT interp_to_string(script_ctx_t *ctx)
2540 jsstr_t *str;
2541 jsval_t v;
2542 HRESULT hres;
2544 v = stack_pop(ctx);
2545 TRACE("%s\n", debugstr_jsval(v));
2546 hres = to_string(ctx, v, &str);
2547 jsval_release(v);
2548 if(FAILED(hres)) {
2549 WARN("failed %08x\n", hres);
2550 return hres;
2553 return stack_push(ctx, jsval_string(str));
2556 /* ECMA-262 3rd Edition 11.13.1 */
2557 static HRESULT interp_assign(script_ctx_t *ctx)
2559 exprval_t ref;
2560 jsval_t v;
2561 HRESULT hres;
2563 TRACE("\n");
2565 v = stack_pop(ctx);
2567 if(!stack_pop_exprval(ctx, &ref)) {
2568 jsval_release(v);
2569 return JS_E_ILLEGAL_ASSIGN;
2572 hres = exprval_propput(ctx, &ref, v);
2573 exprval_release(&ref);
2574 if(FAILED(hres)) {
2575 jsval_release(v);
2576 return hres;
2579 return stack_push(ctx, v);
2582 /* ECMA-262 3rd Edition 11.13.1 */
2583 static HRESULT interp_set_member(script_ctx_t *ctx)
2585 jsval_t objv, namev, value;
2586 const WCHAR *name;
2587 IDispatch *obj;
2588 HRESULT hres;
2590 value = stack_pop(ctx);
2591 namev = stack_pop(ctx);
2592 assert(is_string(namev));
2593 objv = stack_pop(ctx);
2595 TRACE("%s.%s = %s\n", debugstr_jsval(objv), debugstr_jsval(namev), debugstr_jsval(value));
2597 hres = to_object(ctx, objv, &obj);
2598 jsval_release(objv);
2599 if(SUCCEEDED(hres) && !(name = jsstr_flatten(get_string(namev)))) {
2600 IDispatch_Release(obj);
2601 hres = E_OUTOFMEMORY;
2603 if(SUCCEEDED(hres)) {
2604 hres = disp_propput_name(ctx, obj, name, value);
2605 IDispatch_Release(obj);
2606 jsstr_release(get_string(namev));
2608 if(FAILED(hres)) {
2609 WARN("failed %08x\n", hres);
2610 jsval_release(value);
2611 return hres;
2614 return stack_push(ctx, value);
2617 /* JScript extension */
2618 static HRESULT interp_assign_call(script_ctx_t *ctx)
2620 const unsigned argc = get_op_uint(ctx, 0);
2621 exprval_t ref;
2622 jsval_t v;
2623 HRESULT hres;
2625 TRACE("%u\n", argc);
2627 if(!stack_topn_exprval(ctx, argc+1, &ref))
2628 return JS_E_ILLEGAL_ASSIGN;
2630 hres = exprval_call(ctx, &ref, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2631 if(FAILED(hres))
2632 return hres;
2634 v = stack_pop(ctx);
2635 stack_popn(ctx, argc+2);
2636 return stack_push(ctx, v);
2639 static HRESULT interp_undefined(script_ctx_t *ctx)
2641 TRACE("\n");
2643 return stack_push(ctx, jsval_undefined());
2646 static HRESULT interp_jmp(script_ctx_t *ctx)
2648 const unsigned arg = get_op_uint(ctx, 0);
2650 TRACE("%u\n", arg);
2652 jmp_abs(ctx, arg);
2653 return S_OK;
2656 static HRESULT interp_jmp_z(script_ctx_t *ctx)
2658 const unsigned arg = get_op_uint(ctx, 0);
2659 BOOL b;
2660 jsval_t v;
2661 HRESULT hres;
2663 TRACE("\n");
2665 v = stack_pop(ctx);
2666 hres = to_boolean(v, &b);
2667 jsval_release(v);
2668 if(FAILED(hres))
2669 return hres;
2671 if(b)
2672 jmp_next(ctx);
2673 else
2674 jmp_abs(ctx, arg);
2675 return S_OK;
2678 static HRESULT interp_pop(script_ctx_t *ctx)
2680 const unsigned arg = get_op_uint(ctx, 0);
2682 TRACE("%u\n", arg);
2684 stack_popn(ctx, arg);
2685 return S_OK;
2688 static HRESULT interp_ret(script_ctx_t *ctx)
2690 const unsigned clear_ret = get_op_uint(ctx, 0);
2691 call_frame_t *frame = ctx->call_ctx;
2693 TRACE("\n");
2695 if(clear_ret)
2696 jsval_release(steal_ret(frame));
2698 if((frame->flags & EXEC_CONSTRUCTOR) && !is_object_instance(frame->ret)) {
2699 jsval_release(frame->ret);
2700 IDispatch_AddRef(frame->this_obj);
2701 frame->ret = jsval_disp(frame->this_obj);
2704 jmp_abs(ctx, -1);
2705 return S_OK;
2708 static HRESULT interp_setret(script_ctx_t *ctx)
2710 call_frame_t *frame = ctx->call_ctx;
2712 TRACE("\n");
2714 jsval_release(frame->ret);
2715 frame->ret = stack_pop(ctx);
2716 return S_OK;
2719 static HRESULT interp_push_acc(script_ctx_t *ctx)
2721 HRESULT hres;
2723 TRACE("\n");
2725 hres = stack_push(ctx, ctx->acc);
2726 if(SUCCEEDED(hres))
2727 ctx->acc = jsval_undefined();
2728 return hres;
2731 typedef HRESULT (*op_func_t)(script_ctx_t*);
2733 static const op_func_t op_funcs[] = {
2734 #define X(x,a,b,c) interp_##x,
2735 OP_LIST
2736 #undef X
2739 static const unsigned op_move[] = {
2740 #define X(a,x,b,c) x,
2741 OP_LIST
2742 #undef X
2745 static void pop_call_frame(script_ctx_t *ctx)
2747 call_frame_t *frame = ctx->call_ctx;
2749 frame->stack_base -= frame->pop_locals + frame->pop_variables;
2751 assert(frame->scope == frame->base_scope);
2753 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2754 if(frame->scope && frame->scope->ref > 1) {
2755 HRESULT hres = detach_variable_object(ctx, frame, TRUE);
2756 if(FAILED(hres))
2757 ERR("Failed to detach variable object: %08x\n", hres);
2760 if(frame->arguments_obj)
2761 detach_arguments_object(frame->arguments_obj);
2762 if(frame->scope)
2763 scope_release(frame->scope);
2765 if(frame->pop_variables)
2766 stack_popn(ctx, frame->pop_variables);
2767 stack_popn(ctx, frame->pop_locals);
2769 ctx->call_ctx = frame->prev_frame;
2771 if(frame->function_instance)
2772 jsdisp_release(frame->function_instance);
2773 if(frame->variable_obj)
2774 jsdisp_release(frame->variable_obj);
2775 if(frame->this_obj)
2776 IDispatch_Release(frame->this_obj);
2777 jsval_release(frame->ret);
2778 release_bytecode(frame->bytecode);
2779 heap_free(frame);
2782 static void print_backtrace(script_ctx_t *ctx)
2784 unsigned depth = 0, i, line, char_pos;
2785 call_frame_t *frame;
2787 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
2788 WARN("%u\t", depth);
2789 depth++;
2791 if(frame->this_obj)
2792 WARN("%p->", frame->this_obj);
2793 WARN("%s(", frame->function->name ? debugstr_w(frame->function->name) : "[unnamed]");
2794 if(frame->base_scope && frame->base_scope->frame) {
2795 for(i=0; i < frame->argc; i++) {
2796 if(i < frame->function->param_cnt)
2797 WARN("%s%s=%s", i ? ", " : "", debugstr_w(frame->function->params[i]),
2798 debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2799 else
2800 WARN("%s%s", i ? ", " : "", debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2802 }else {
2803 WARN("[detached frame]");
2805 line = get_location_line(frame->bytecode, frame->bytecode->instrs[frame->ip].loc, &char_pos);
2806 WARN(") context %s line %u char %u\n", wine_dbgstr_longlong(frame->bytecode->source_context), line, char_pos);
2808 if(!(frame->flags & EXEC_RETURN_TO_INTERP)) {
2809 WARN("%u\t[native code]\n", depth);
2810 depth++;
2815 static HRESULT unwind_exception(script_ctx_t *ctx, HRESULT exception_hres)
2817 except_frame_t *except_frame;
2818 jsexcept_t *ei = ctx->ei;
2819 call_frame_t *frame;
2820 jsval_t except_val;
2821 unsigned catch_off;
2822 HRESULT hres;
2824 if(WARN_ON(jscript)) {
2825 jsdisp_t *error_obj;
2826 jsval_t msg;
2828 WARN("Exception %08x %s", exception_hres, debugstr_jsval(ei->valid_value ? ei->value : jsval_undefined()));
2829 if(ei->valid_value && jsval_type(ei->value) == JSV_OBJECT) {
2830 error_obj = to_jsdisp(get_object(ei->value));
2831 if(error_obj) {
2832 hres = jsdisp_propget_name(error_obj, L"message", &msg);
2833 if(SUCCEEDED(hres)) {
2834 WARN(" (message %s)", debugstr_jsval(msg));
2835 jsval_release(msg);
2839 WARN(" in:\n");
2841 print_backtrace(ctx);
2844 frame = ctx->call_ctx;
2845 if(exception_hres != DISP_E_EXCEPTION)
2846 throw_error(ctx, exception_hres, NULL);
2847 set_error_location(ei, frame->bytecode, frame->bytecode->instrs[frame->ip].loc, IDS_RUNTIME_ERROR, NULL);
2849 while(!frame->except_frame) {
2850 DWORD flags;
2852 while(frame->scope != frame->base_scope)
2853 scope_pop(&frame->scope);
2855 stack_popn(ctx, ctx->stack_top-frame->stack_base);
2857 flags = frame->flags;
2858 pop_call_frame(ctx);
2859 if(!(flags & EXEC_RETURN_TO_INTERP))
2860 return DISP_E_EXCEPTION;
2861 frame = ctx->call_ctx;
2864 except_frame = frame->except_frame;
2865 catch_off = except_frame->catch_off;
2867 assert(except_frame->stack_top <= ctx->stack_top);
2868 stack_popn(ctx, ctx->stack_top - except_frame->stack_top);
2870 while(except_frame->scope != frame->scope)
2871 scope_pop(&frame->scope);
2873 frame->ip = catch_off ? catch_off : except_frame->finally_off;
2874 assert(!catch_off || frame->bytecode->instrs[frame->ip].op == OP_enter_catch);
2876 if(ei->valid_value) {
2877 except_val = ctx->ei->value;
2878 ei->valid_value = FALSE;
2879 }else {
2880 jsdisp_t *err;
2881 if(!(err = create_builtin_error(ctx)))
2882 return E_OUTOFMEMORY;
2883 except_val = jsval_obj(err);
2886 /* keep current except_frame if we're entering catch block with finally block associated */
2887 if(catch_off && except_frame->finally_off) {
2888 except_frame->catch_off = 0;
2889 }else {
2890 frame->except_frame = except_frame->next;
2891 heap_free(except_frame);
2894 hres = stack_push(ctx, except_val);
2895 if(FAILED(hres))
2896 return hres;
2898 if(!catch_off)
2899 hres = stack_push(ctx, jsval_bool(FALSE));
2900 return hres;
2903 static HRESULT enter_bytecode(script_ctx_t *ctx, jsval_t *r)
2905 call_frame_t *frame;
2906 jsop_t op;
2907 HRESULT hres = S_OK;
2909 TRACE("\n");
2911 while(1) {
2912 frame = ctx->call_ctx;
2913 op = frame->bytecode->instrs[frame->ip].op;
2914 hres = op_funcs[op](ctx);
2915 if(FAILED(hres)) {
2916 hres = unwind_exception(ctx, hres);
2917 if(FAILED(hres))
2918 return hres;
2919 }else if(frame->ip == -1) {
2920 const DWORD return_to_interp = frame->flags & EXEC_RETURN_TO_INTERP;
2922 assert(ctx->stack_top == frame->stack_base);
2923 assert(frame->scope == frame->base_scope);
2925 if(return_to_interp) {
2926 jsval_release(ctx->acc);
2927 ctx->acc = steal_ret(frame);
2928 }else if(r) {
2929 *r = steal_ret(frame);
2931 pop_call_frame(ctx);
2932 if(!return_to_interp)
2933 break;
2934 }else {
2935 frame->ip += op_move[op];
2939 return S_OK;
2942 static HRESULT bind_event_target(script_ctx_t *ctx, function_code_t *func, jsdisp_t *func_obj)
2944 IBindEventHandler *target;
2945 exprval_t exprval;
2946 IDispatch *disp;
2947 jsval_t v;
2948 HRESULT hres;
2950 hres = identifier_eval(ctx, func->event_target, &exprval);
2951 if(FAILED(hres))
2952 return hres;
2954 hres = exprval_to_value(ctx, &exprval, &v);
2955 if(FAILED(hres))
2956 return hres;
2958 if(!is_object_instance(v)) {
2959 FIXME("Can't bind to %s\n", debugstr_jsval(v));
2960 jsval_release(v);
2963 disp = get_object(v);
2964 hres = IDispatch_QueryInterface(disp, &IID_IBindEventHandler, (void**)&target);
2965 if(SUCCEEDED(hres)) {
2966 hres = IBindEventHandler_BindHandler(target, func->name, (IDispatch*)&func_obj->IDispatchEx_iface);
2967 IBindEventHandler_Release(target);
2968 if(FAILED(hres))
2969 WARN("BindEvent failed: %08x\n", hres);
2970 }else {
2971 FIXME("No IBindEventHandler, not yet supported binding\n");
2974 IDispatch_Release(disp);
2975 return hres;
2978 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)
2980 const unsigned orig_stack = ctx->stack_top;
2981 scope_chain_t *scope;
2982 unsigned i;
2983 jsval_t v;
2984 HRESULT hres;
2986 /* If arguments are already on the stack, we may use them. */
2987 if(argv + argc == ctx->stack + ctx->stack_top) {
2988 frame->arguments_off = argv - ctx->stack;
2989 i = argc;
2990 }else {
2991 frame->arguments_off = ctx->stack_top;
2992 for(i = 0; i < argc; i++) {
2993 hres = jsval_copy(argv[i], &v);
2994 if(SUCCEEDED(hres))
2995 hres = stack_push(ctx, v);
2996 if(FAILED(hres)) {
2997 stack_popn(ctx, i);
2998 return hres;
3003 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
3004 for(; i < frame->function->param_cnt; i++) {
3005 hres = stack_push(ctx, jsval_undefined());
3006 if(FAILED(hres)) {
3007 stack_popn(ctx, ctx->stack_top - orig_stack);
3008 return hres;
3012 frame->pop_locals = ctx->stack_top - orig_stack;
3014 frame->variables_off = ctx->stack_top;
3016 for(i = 0; i < frame->function->var_cnt; i++) {
3017 hres = stack_push(ctx, jsval_undefined());
3018 if(FAILED(hres)) {
3019 stack_popn(ctx, ctx->stack_top - orig_stack);
3020 return hres;
3024 frame->pop_variables = i;
3026 hres = scope_push(scope_chain, variable_object, to_disp(variable_object), &scope);
3027 if(FAILED(hres)) {
3028 stack_popn(ctx, ctx->stack_top - orig_stack);
3029 return hres;
3032 for(i = 0; i < frame->function->func_cnt; i++) {
3033 if(frame->function->funcs[i].name && !frame->function->funcs[i].event_target) {
3034 jsdisp_t *func_obj;
3035 unsigned off;
3037 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+i, scope, &func_obj);
3038 if(FAILED(hres)) {
3039 stack_popn(ctx, ctx->stack_top - orig_stack);
3040 scope_release(scope);
3041 return hres;
3044 off = local_off(frame, frame->function->funcs[i].local_ref);
3045 jsval_release(ctx->stack[off]);
3046 ctx->stack[off] = jsval_obj(func_obj);
3050 scope->frame = frame;
3051 frame->base_scope = frame->scope = scope;
3052 return S_OK;
3055 HRESULT exec_source(script_ctx_t *ctx, DWORD flags, bytecode_t *bytecode, function_code_t *function, scope_chain_t *scope,
3056 IDispatch *this_obj, jsdisp_t *function_instance, unsigned argc, jsval_t *argv, jsval_t *r)
3058 jsdisp_t *variable_obj;
3059 call_frame_t *frame;
3060 unsigned i;
3061 HRESULT hres;
3063 if(!ctx->stack) {
3064 ctx->stack = heap_alloc(stack_size * sizeof(*ctx->stack));
3065 if(!ctx->stack)
3066 return E_OUTOFMEMORY;
3069 if(bytecode->named_item) {
3070 if(!bytecode->named_item->script_obj) {
3071 hres = create_named_item_script_obj(ctx, bytecode->named_item);
3072 if(FAILED(hres)) return hres;
3076 if(!ctx->ei->enter_notified) {
3077 ctx->ei->enter_notified = TRUE;
3078 IActiveScriptSite_OnEnterScript(ctx->site);
3081 for(i = 0; i < function->func_cnt; i++) {
3082 jsdisp_t *func_obj;
3084 if(!function->funcs[i].event_target)
3085 continue;
3087 hres = create_source_function(ctx, bytecode, function->funcs+i, scope, &func_obj);
3088 if(FAILED(hres))
3089 return hres;
3091 hres = bind_event_target(ctx, function->funcs+i, func_obj);
3092 jsdisp_release(func_obj);
3093 if(FAILED(hres))
3094 return hres;
3097 if((flags & EXEC_EVAL) && ctx->call_ctx) {
3098 variable_obj = jsdisp_addref(ctx->call_ctx->variable_obj);
3099 }else if(!(flags & (EXEC_GLOBAL | EXEC_EVAL))) {
3100 hres = create_dispex(ctx, NULL, NULL, &variable_obj);
3101 if(FAILED(hres)) return hres;
3102 }else if(bytecode->named_item) {
3103 variable_obj = jsdisp_addref(bytecode->named_item->script_obj);
3104 }else {
3105 variable_obj = jsdisp_addref(ctx->global);
3108 if(flags & (EXEC_GLOBAL | EXEC_EVAL)) {
3109 named_item_t *item = bytecode->named_item;
3110 DISPID id;
3112 for(i=0; i < function->var_cnt; i++) {
3113 TRACE("[%d] %s %d\n", i, debugstr_w(function->variables[i].name), function->variables[i].func_id);
3114 if(function->variables[i].func_id != -1) {
3115 jsdisp_t *func_obj;
3117 hres = create_source_function(ctx, bytecode, function->funcs+function->variables[i].func_id, scope, &func_obj);
3118 if(FAILED(hres))
3119 goto fail;
3121 hres = jsdisp_propput_name(variable_obj, function->variables[i].name, jsval_obj(func_obj));
3122 jsdisp_release(func_obj);
3123 continue;
3126 if(item && !(item->flags & SCRIPTITEM_CODEONLY)
3127 && SUCCEEDED(disp_get_id(ctx, item->disp, function->variables[i].name, function->variables[i].name, 0, &id)))
3128 continue;
3130 if(!item && (flags & EXEC_GLOBAL) && lookup_global_members(ctx, function->variables[i].name, NULL))
3131 continue;
3133 hres = jsdisp_get_id(variable_obj, function->variables[i].name, fdexNameEnsure, &id);
3134 if(FAILED(hres))
3135 goto fail;
3139 /* ECMA-262 3rd Edition 11.2.3.7 */
3140 if(this_obj) {
3141 jsdisp_t *jsthis;
3143 jsthis = iface_to_jsdisp(this_obj);
3144 if(jsthis) {
3145 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
3146 this_obj = NULL;
3147 jsdisp_release(jsthis);
3151 if(ctx->call_ctx && (flags & EXEC_EVAL)) {
3152 hres = detach_variable_object(ctx, ctx->call_ctx, FALSE);
3153 if(FAILED(hres))
3154 goto fail;
3157 frame = heap_alloc_zero(sizeof(*frame));
3158 if(!frame) {
3159 hres = E_OUTOFMEMORY;
3160 goto fail;
3163 frame->function = function;
3164 frame->ret = jsval_undefined();
3165 frame->argc = argc;
3166 frame->bytecode = bytecode_addref(bytecode);
3168 if(!(flags & (EXEC_GLOBAL|EXEC_EVAL))) {
3169 hres = setup_scope(ctx, frame, scope, variable_obj, argc, argv);
3170 if(FAILED(hres)) {
3171 release_bytecode(frame->bytecode);
3172 heap_free(frame);
3173 goto fail;
3175 }else if(scope) {
3176 frame->base_scope = frame->scope = scope_addref(scope);
3179 frame->ip = function->instr_off;
3180 frame->stack_base = ctx->stack_top;
3181 if(this_obj) {
3182 frame->this_obj = this_obj;
3183 IDispatch_AddRef(frame->this_obj);
3186 if(function_instance)
3187 frame->function_instance = jsdisp_addref(function_instance);
3189 frame->flags = flags;
3190 frame->variable_obj = variable_obj;
3192 frame->prev_frame = ctx->call_ctx;
3193 ctx->call_ctx = frame;
3195 if(flags & EXEC_RETURN_TO_INTERP) {
3197 * We're called directly from interpreter, so we may just setup call frame and return.
3198 * Already running interpreter will take care of execution.
3200 if(r)
3201 *r = jsval_undefined();
3202 return S_OK;
3205 return enter_bytecode(ctx, r);
3207 fail:
3208 jsdisp_release(variable_obj);
3209 return hres;