jscript: Lookup host global object on demand instead of storing it in script context.
[wine.git] / dlls / jscript / engine.c
blobce7fbbc572207a7f38404a9c6ebe59f7ffcd10ba
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 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
31 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
32 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
33 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
34 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
35 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
36 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
38 struct _except_frame_t {
39 unsigned stack_top;
40 scope_chain_t *scope;
41 unsigned catch_off;
42 unsigned finally_off;
44 except_frame_t *next;
47 typedef struct {
48 enum {
49 EXPRVAL_JSVAL,
50 EXPRVAL_IDREF,
51 EXPRVAL_STACK_REF,
52 EXPRVAL_INVALID
53 } type;
54 union {
55 jsval_t val;
56 struct {
57 IDispatch *disp;
58 DISPID id;
59 } idref;
60 unsigned off;
61 HRESULT hres;
62 } u;
63 } exprval_t;
65 static HRESULT stack_push(script_ctx_t *ctx, jsval_t v)
67 if(!ctx->stack_size) {
68 ctx->stack = heap_alloc(16*sizeof(*ctx->stack));
69 if(!ctx->stack)
70 return E_OUTOFMEMORY;
71 ctx->stack_size = 16;
72 }else if(ctx->stack_size == ctx->stack_top) {
73 jsval_t *new_stack;
75 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*new_stack));
76 if(!new_stack) {
77 jsval_release(v);
78 return E_OUTOFMEMORY;
81 ctx->stack = new_stack;
82 ctx->stack_size *= 2;
85 ctx->stack[ctx->stack_top++] = v;
86 return S_OK;
89 static inline HRESULT stack_push_string(script_ctx_t *ctx, const WCHAR *str)
91 jsstr_t *v;
93 v = jsstr_alloc(str);
94 if(!v)
95 return E_OUTOFMEMORY;
97 return stack_push(ctx, jsval_string(v));
100 static inline jsval_t stack_top(script_ctx_t *ctx)
102 assert(ctx->stack_top > ctx->call_ctx->stack_base);
103 return ctx->stack[ctx->stack_top-1];
106 static inline jsval_t *stack_top_ref(script_ctx_t *ctx, unsigned n)
108 assert(ctx->stack_top > ctx->call_ctx->stack_base+n);
109 return ctx->stack+ctx->stack_top-1-n;
112 static inline jsval_t stack_topn(script_ctx_t *ctx, unsigned n)
114 return *stack_top_ref(ctx, n);
117 static inline jsval_t *stack_args(script_ctx_t *ctx, unsigned n)
119 if(!n)
120 return NULL;
121 assert(ctx->stack_top > ctx->call_ctx->stack_base+n-1);
122 return ctx->stack + ctx->stack_top-n;
125 static inline jsval_t stack_pop(script_ctx_t *ctx)
127 assert(ctx->stack_top > ctx->call_ctx->stack_base);
128 return ctx->stack[--ctx->stack_top];
131 static void stack_popn(script_ctx_t *ctx, unsigned n)
133 while(n--)
134 jsval_release(stack_pop(ctx));
137 static HRESULT stack_pop_number(script_ctx_t *ctx, double *r)
139 jsval_t v;
140 HRESULT hres;
142 v = stack_pop(ctx);
143 hres = to_number(ctx, v, r);
144 jsval_release(v);
145 return hres;
148 static HRESULT stack_pop_object(script_ctx_t *ctx, IDispatch **r)
150 jsval_t v;
151 HRESULT hres;
153 v = stack_pop(ctx);
154 if(is_object_instance(v)) {
155 if(!get_object(v))
156 return JS_E_OBJECT_REQUIRED;
157 *r = get_object(v);
158 return S_OK;
161 hres = to_object(ctx, v, r);
162 jsval_release(v);
163 return hres;
166 static inline HRESULT stack_pop_int(script_ctx_t *ctx, INT *r)
168 return to_int32(ctx, stack_pop(ctx), r);
171 static inline HRESULT stack_pop_uint(script_ctx_t *ctx, DWORD *r)
173 return to_uint32(ctx, stack_pop(ctx), r);
176 static inline unsigned local_off(call_frame_t *frame, int ref)
178 return ref < 0
179 ? frame->arguments_off - ref-1
180 : frame->variables_off + ref;
183 static inline BSTR local_name(call_frame_t *frame, int ref)
185 return ref < 0 ? frame->function->params[-ref-1] : frame->function->variables[ref].name;
188 /* Steals input reference even on failure. */
189 static HRESULT stack_push_exprval(script_ctx_t *ctx, exprval_t *val)
191 HRESULT hres;
193 switch(val->type) {
194 case EXPRVAL_JSVAL:
195 assert(0);
196 case EXPRVAL_IDREF:
197 hres = stack_push(ctx, jsval_disp(val->u.idref.disp));
198 if(SUCCEEDED(hres))
199 hres = stack_push(ctx, jsval_number(val->u.idref.id));
200 else
201 IDispatch_Release(val->u.idref.disp);
202 return hres;
203 case EXPRVAL_STACK_REF:
204 hres = stack_push(ctx, jsval_number(val->u.off));
205 if(SUCCEEDED(hres))
206 hres = stack_push(ctx, jsval_undefined());
207 return hres;
208 case EXPRVAL_INVALID:
209 hres = stack_push(ctx, jsval_undefined());
210 if(SUCCEEDED(hres))
211 hres = stack_push(ctx, jsval_number(val->u.hres));
212 return hres;
215 assert(0);
216 return E_FAIL;
219 static BOOL stack_topn_exprval(script_ctx_t *ctx, unsigned n, exprval_t *r)
221 jsval_t v = stack_topn(ctx, n+1);
223 switch(jsval_type(v)) {
224 case JSV_NUMBER: {
225 call_frame_t *frame = ctx->call_ctx;
226 unsigned off = get_number(v);
228 if(!frame->base_scope->frame && off >= frame->arguments_off) {
229 DISPID id;
230 BSTR name;
231 HRESULT hres;
233 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
235 assert(off < frame->variables_off + frame->function->var_cnt);
236 name = off >= frame->variables_off
237 ? frame->function->variables[off - frame->variables_off].name
238 : frame->function->params[off - frame->arguments_off];
239 hres = jsdisp_get_id(ctx->call_ctx->base_scope->jsobj, name, 0, &id);
240 if(FAILED(hres)) {
241 r->type = EXPRVAL_INVALID;
242 r->u.hres = hres;
243 return FALSE;
246 *stack_top_ref(ctx, n+1) = jsval_obj(jsdisp_addref(frame->base_scope->jsobj));
247 *stack_top_ref(ctx, n) = jsval_number(id);
248 r->type = EXPRVAL_IDREF;
249 r->u.idref.disp = frame->base_scope->obj;
250 r->u.idref.id = id;
251 return TRUE;
254 r->type = EXPRVAL_STACK_REF;
255 r->u.off = off;
256 return TRUE;
258 case JSV_OBJECT:
259 r->type = EXPRVAL_IDREF;
260 r->u.idref.disp = get_object(v);
261 assert(is_number(stack_topn(ctx, n)));
262 r->u.idref.id = get_number(stack_topn(ctx, n));
263 return TRUE;
264 case JSV_UNDEFINED:
265 r->type = EXPRVAL_INVALID;
266 assert(is_number(stack_topn(ctx, n)));
267 r->u.hres = get_number(stack_topn(ctx, n));
268 return FALSE;
269 default:
270 assert(0);
271 return FALSE;
275 static inline BOOL stack_pop_exprval(script_ctx_t *ctx, exprval_t *r)
277 BOOL ret = stack_topn_exprval(ctx, 0, r);
278 ctx->stack_top -= 2;
279 return ret;
282 static HRESULT exprval_propput(script_ctx_t *ctx, exprval_t *ref, jsval_t v)
284 switch(ref->type) {
285 case EXPRVAL_STACK_REF: {
286 jsval_t *r = ctx->stack + ref->u.off;
287 jsval_release(*r);
288 return jsval_copy(v, r);
290 case EXPRVAL_IDREF:
291 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v);
292 default:
293 assert(0);
294 return E_FAIL;
298 static HRESULT exprval_propget(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
300 switch(ref->type) {
301 case EXPRVAL_STACK_REF:
302 return jsval_copy(ctx->stack[ref->u.off], r);
303 case EXPRVAL_IDREF:
304 return disp_propget(ctx, ref->u.idref.disp, ref->u.idref.id, r);
305 default:
306 assert(0);
307 return E_FAIL;
311 static HRESULT exprval_call(script_ctx_t *ctx, exprval_t *ref, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
313 switch(ref->type) {
314 case EXPRVAL_STACK_REF: {
315 jsval_t v = ctx->stack[ref->u.off];
317 if(!is_object_instance(v)) {
318 FIXME("invoke %s\n", debugstr_jsval(v));
319 return E_FAIL;
322 return disp_call_value(ctx, get_object(v), NULL, flags, argc, argv, r);
324 case EXPRVAL_IDREF:
325 return disp_call(ctx, ref->u.idref.disp, ref->u.idref.id, flags, argc, argv, r);
326 default:
327 assert(0);
328 return E_FAIL;
332 /* ECMA-262 3rd Edition 8.7.1 */
333 /* Steals input reference. */
334 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
336 HRESULT hres;
338 if(ref->type == EXPRVAL_JSVAL) {
339 *r = ref->u.val;
340 return S_OK;
343 hres = exprval_propget(ctx, ref, r);
345 if(ref->type == EXPRVAL_IDREF)
346 IDispatch_Release(ref->u.idref.disp);
347 return hres;
350 static void exprval_release(exprval_t *val)
352 switch(val->type) {
353 case EXPRVAL_JSVAL:
354 jsval_release(val->u.val);
355 return;
356 case EXPRVAL_IDREF:
357 if(val->u.idref.disp)
358 IDispatch_Release(val->u.idref.disp);
359 return;
360 case EXPRVAL_STACK_REF:
361 case EXPRVAL_INVALID:
362 return;
366 static inline void exprval_set_exception(exprval_t *val, HRESULT hres)
368 val->type = EXPRVAL_INVALID;
369 val->u.hres = hres;
372 static inline void exprval_set_disp_ref(exprval_t *ref, IDispatch *obj, DISPID id)
374 ref->type = EXPRVAL_IDREF;
375 IDispatch_AddRef(ref->u.idref.disp = obj);
376 ref->u.idref.id = id;
379 static inline jsval_t steal_ret(call_frame_t *frame)
381 jsval_t r = frame->ret;
382 frame->ret = jsval_undefined();
383 return r;
386 static inline void clear_acc(script_ctx_t *ctx)
388 jsval_release(ctx->acc);
389 ctx->acc = jsval_undefined();
392 static HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
394 scope_chain_t *new_scope;
396 new_scope = heap_alloc(sizeof(scope_chain_t));
397 if(!new_scope)
398 return E_OUTOFMEMORY;
400 new_scope->ref = 1;
402 IDispatch_AddRef(obj);
403 new_scope->jsobj = jsobj;
404 new_scope->obj = obj;
405 new_scope->frame = NULL;
406 new_scope->next = scope ? scope_addref(scope) : NULL;
408 *ret = new_scope;
409 return S_OK;
412 static void scope_pop(scope_chain_t **scope)
414 scope_chain_t *tmp;
416 tmp = *scope;
417 *scope = tmp->next;
418 scope_release(tmp);
421 void scope_release(scope_chain_t *scope)
423 if(--scope->ref)
424 return;
426 if(scope->next)
427 scope_release(scope->next);
429 IDispatch_Release(scope->obj);
430 heap_free(scope);
433 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, const WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
435 IDispatchEx *dispex;
436 jsdisp_t *jsdisp;
437 BSTR bstr;
438 HRESULT hres;
440 jsdisp = iface_to_jsdisp(disp);
441 if(jsdisp) {
442 hres = jsdisp_get_id(jsdisp, name, flags, id);
443 jsdisp_release(jsdisp);
444 return hres;
447 if(name_bstr) {
448 bstr = name_bstr;
449 }else {
450 bstr = SysAllocString(name);
451 if(!bstr)
452 return E_OUTOFMEMORY;
455 *id = 0;
456 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
457 if(SUCCEEDED(hres)) {
458 hres = IDispatchEx_GetDispID(dispex, bstr, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
459 IDispatchEx_Release(dispex);
460 }else {
461 TRACE("using IDispatch\n");
462 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &bstr, 1, 0, id);
465 if(name_bstr != bstr)
466 SysFreeString(bstr);
467 return hres;
470 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
472 IObjectIdentity *identity;
473 IUnknown *unk1, *unk2;
474 HRESULT hres;
476 if(disp1 == disp2) {
477 *ret = TRUE;
478 return S_OK;
481 if(!disp1 || !disp2) {
482 *ret = FALSE;
483 return S_OK;
486 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
487 if(FAILED(hres))
488 return hres;
490 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
491 if(FAILED(hres)) {
492 IUnknown_Release(unk1);
493 return hres;
496 if(unk1 == unk2) {
497 *ret = TRUE;
498 }else {
499 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
500 if(SUCCEEDED(hres)) {
501 hres = IObjectIdentity_IsEqualObject(identity, unk2);
502 IObjectIdentity_Release(identity);
503 *ret = hres == S_OK;
504 }else {
505 *ret = FALSE;
509 IUnknown_Release(unk1);
510 IUnknown_Release(unk2);
511 return S_OK;
514 /* ECMA-262 3rd Edition 11.9.6 */
515 HRESULT jsval_strict_equal(jsval_t lval, jsval_t rval, BOOL *ret)
517 jsval_type_t type = jsval_type(lval);
519 TRACE("\n");
521 if(type != jsval_type(rval)) {
522 if(is_null_instance(lval))
523 *ret = is_null_instance(rval);
524 else
525 *ret = FALSE;
526 return S_OK;
529 switch(type) {
530 case JSV_UNDEFINED:
531 case JSV_NULL:
532 *ret = TRUE;
533 break;
534 case JSV_OBJECT:
535 return disp_cmp(get_object(lval), get_object(rval), ret);
536 case JSV_STRING:
537 *ret = jsstr_eq(get_string(lval), get_string(rval));
538 break;
539 case JSV_NUMBER:
540 *ret = get_number(lval) == get_number(rval);
541 break;
542 case JSV_BOOL:
543 *ret = !get_bool(lval) == !get_bool(rval);
544 break;
545 case JSV_VARIANT:
546 FIXME("VARIANT not implemented\n");
547 return E_NOTIMPL;
550 return S_OK;
554 * Transfers local variables from stack to variable object.
555 * It's slow, so we want to avoid it as much as possible.
557 static HRESULT detach_variable_object(script_ctx_t *ctx, call_frame_t *frame, BOOL from_release)
559 unsigned i;
560 HRESULT hres;
562 if(!frame->base_scope || !frame->base_scope->frame)
563 return S_OK;
565 TRACE("detaching %p\n", frame);
567 assert(frame == frame->base_scope->frame);
568 assert(frame->variable_obj == frame->base_scope->jsobj);
570 if(!from_release && !frame->arguments_obj) {
571 hres = setup_arguments_object(ctx, frame);
572 if(FAILED(hres))
573 return hres;
576 frame->base_scope->frame = NULL;
578 for(i = 0; i < frame->function->locals_cnt; i++) {
579 hres = jsdisp_propput_name(frame->variable_obj, frame->function->locals[i].name,
580 ctx->stack[local_off(frame, frame->function->locals[i].ref)]);
581 if(FAILED(hres))
582 return hres;
585 return S_OK;
588 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
590 named_item_t *item;
591 DISPID id;
592 HRESULT hres;
594 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
595 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
596 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
597 if(SUCCEEDED(hres)) {
598 if(ret)
599 exprval_set_disp_ref(ret, item->disp, id);
600 return TRUE;
605 return FALSE;
608 IDispatch *lookup_global_host(script_ctx_t *ctx)
610 IDispatch *disp = NULL;
611 named_item_t *item;
613 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
614 if(!(item->flags & SCRIPTITEM_GLOBALMEMBERS)) continue;
615 disp = item->disp;
616 break;
618 if(!disp) disp = to_disp(ctx->global);
620 return disp;
623 static int __cdecl local_ref_cmp(const void *key, const void *ref)
625 return wcscmp((const WCHAR*)key, ((const local_ref_t*)ref)->name);
628 local_ref_t *lookup_local(const function_code_t *function, const WCHAR *identifier)
630 return bsearch(identifier, function->locals, function->locals_cnt, sizeof(*function->locals), local_ref_cmp);
633 /* ECMA-262 3rd Edition 10.1.4 */
634 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
636 scope_chain_t *scope;
637 named_item_t *item;
638 DISPID id = 0;
639 HRESULT hres;
641 TRACE("%s\n", debugstr_w(identifier));
643 if(ctx->call_ctx) {
644 for(scope = ctx->call_ctx->scope; scope; scope = scope->next) {
645 if(scope->frame) {
646 function_code_t *func = scope->frame->function;
647 local_ref_t *ref = lookup_local(func, identifier);
648 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
650 if(ref) {
651 ret->type = EXPRVAL_STACK_REF;
652 ret->u.off = local_off(scope->frame, ref->ref);
653 TRACE("returning ref %d for %d\n", ret->u.off, ref->ref);
654 return S_OK;
657 if(!wcscmp(identifier, argumentsW)) {
658 hres = detach_variable_object(ctx, scope->frame, FALSE);
659 if(FAILED(hres))
660 return hres;
663 if(scope->jsobj)
664 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
665 else
666 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
667 if(SUCCEEDED(hres)) {
668 exprval_set_disp_ref(ret, scope->obj, id);
669 return S_OK;
674 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
675 if(SUCCEEDED(hres)) {
676 exprval_set_disp_ref(ret, to_disp(ctx->global), id);
677 return S_OK;
680 item = lookup_named_item(ctx, identifier, SCRIPTITEM_ISVISIBLE);
681 if(item) {
682 IDispatch_AddRef(item->disp);
683 ret->type = EXPRVAL_JSVAL;
684 ret->u.val = jsval_disp(item->disp);
685 return S_OK;
688 if(lookup_global_members(ctx, identifier, ret))
689 return S_OK;
691 exprval_set_exception(ret, JS_E_UNDEFINED_VARIABLE);
692 return S_OK;
695 static inline BSTR get_op_bstr(script_ctx_t *ctx, int i)
697 call_frame_t *frame = ctx->call_ctx;
698 return frame->bytecode->instrs[frame->ip].u.arg[i].bstr;
701 static inline unsigned get_op_uint(script_ctx_t *ctx, int i)
703 call_frame_t *frame = ctx->call_ctx;
704 return frame->bytecode->instrs[frame->ip].u.arg[i].uint;
707 static inline unsigned get_op_int(script_ctx_t *ctx, int i)
709 call_frame_t *frame = ctx->call_ctx;
710 return frame->bytecode->instrs[frame->ip].u.arg[i].lng;
713 static inline jsstr_t *get_op_str(script_ctx_t *ctx, int i)
715 call_frame_t *frame = ctx->call_ctx;
716 return frame->bytecode->instrs[frame->ip].u.arg[i].str;
719 static inline double get_op_double(script_ctx_t *ctx)
721 call_frame_t *frame = ctx->call_ctx;
722 return frame->bytecode->instrs[frame->ip].u.dbl;
725 static inline void jmp_next(script_ctx_t *ctx)
727 ctx->call_ctx->ip++;
730 static inline void jmp_abs(script_ctx_t *ctx, unsigned dst)
732 ctx->call_ctx->ip = dst;
735 /* ECMA-262 3rd Edition 12.6.4 */
736 static HRESULT interp_forin(script_ctx_t *ctx)
738 const HRESULT arg = get_op_uint(ctx, 0);
739 IDispatch *obj = NULL;
740 IDispatchEx *dispex;
741 exprval_t prop_ref;
742 DISPID id;
743 BSTR name = NULL;
744 HRESULT hres;
746 TRACE("\n");
748 assert(is_number(stack_top(ctx)));
749 id = get_number(stack_top(ctx));
751 if(!stack_topn_exprval(ctx, 1, &prop_ref)) {
752 FIXME("invalid ref: %08x\n", prop_ref.u.hres);
753 return E_FAIL;
756 if(is_object_instance(stack_topn(ctx, 3)))
757 obj = get_object(stack_topn(ctx, 3));
759 if(obj) {
760 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
761 if(SUCCEEDED(hres)) {
762 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
763 if(hres == S_OK)
764 hres = IDispatchEx_GetMemberName(dispex, id, &name);
765 IDispatchEx_Release(dispex);
766 if(FAILED(hres))
767 return hres;
768 }else {
769 TRACE("No IDispatchEx\n");
773 if(name) {
774 jsstr_t *str;
776 str = jsstr_alloc_len(name, SysStringLen(name));
777 SysFreeString(name);
778 if(!str)
779 return E_OUTOFMEMORY;
781 stack_pop(ctx);
782 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
784 hres = exprval_propput(ctx, &prop_ref, jsval_string(str));
785 jsstr_release(str);
786 if(FAILED(hres))
787 return hres;
789 jmp_next(ctx);
790 }else {
791 stack_popn(ctx, 4);
792 jmp_abs(ctx, arg);
794 return S_OK;
797 /* ECMA-262 3rd Edition 12.10 */
798 static HRESULT interp_push_scope(script_ctx_t *ctx)
800 IDispatch *disp;
801 jsval_t v;
802 HRESULT hres;
804 TRACE("\n");
806 v = stack_pop(ctx);
807 hres = to_object(ctx, v, &disp);
808 jsval_release(v);
809 if(FAILED(hres))
810 return hres;
812 hres = scope_push(ctx->call_ctx->scope, to_jsdisp(disp), disp, &ctx->call_ctx->scope);
813 IDispatch_Release(disp);
814 return hres;
817 /* ECMA-262 3rd Edition 12.10 */
818 static HRESULT interp_pop_scope(script_ctx_t *ctx)
820 TRACE("\n");
822 scope_pop(&ctx->call_ctx->scope);
823 return S_OK;
826 /* ECMA-262 3rd Edition 12.13 */
827 static HRESULT interp_case(script_ctx_t *ctx)
829 const unsigned arg = get_op_uint(ctx, 0);
830 jsval_t v;
831 BOOL b;
832 HRESULT hres;
834 TRACE("\n");
836 v = stack_pop(ctx);
837 hres = jsval_strict_equal(stack_top(ctx), v, &b);
838 jsval_release(v);
839 if(FAILED(hres))
840 return hres;
842 if(b) {
843 stack_popn(ctx, 1);
844 jmp_abs(ctx, arg);
845 }else {
846 jmp_next(ctx);
848 return S_OK;
851 static void set_error_value(script_ctx_t *ctx, jsval_t value)
853 jsexcept_t *ei = ctx->ei;
854 jsdisp_t *obj;
856 reset_ei(ei);
857 ei->error = JS_E_EXCEPTION_THROWN;
858 ei->valid_value = TRUE;
859 ei->value = value;
861 if(is_object_instance(value) && get_object(value) && (obj = to_jsdisp(get_object(value)))) {
862 UINT32 number;
863 jsstr_t *str;
864 jsval_t v;
865 HRESULT hres;
867 /* FIXME: We should check if object is an error instance */
869 hres = jsdisp_propget_name(obj, L"number", &v);
870 if(SUCCEEDED(hres)) {
871 hres = to_uint32(ctx, v, &number);
872 if(SUCCEEDED(hres))
873 ei->error = FAILED(number) ? number : E_FAIL;
874 jsval_release(v);
877 hres = jsdisp_propget_name(obj, L"description", &v);
878 if(SUCCEEDED(hres)) {
879 hres = to_string(ctx, v, &str);
880 if(SUCCEEDED(hres))
881 ei->message = str;
882 jsval_release(v);
887 /* ECMA-262 3rd Edition 12.13 */
888 static HRESULT interp_throw(script_ctx_t *ctx)
890 TRACE("\n");
892 set_error_value(ctx, stack_pop(ctx));
893 return DISP_E_EXCEPTION;
896 static HRESULT interp_throw_ref(script_ctx_t *ctx)
898 const HRESULT arg = get_op_uint(ctx, 0);
900 TRACE("%08x\n", arg);
902 return arg;
905 static HRESULT interp_throw_type(script_ctx_t *ctx)
907 const HRESULT hres = get_op_uint(ctx, 0);
908 jsstr_t *str = get_op_str(ctx, 1);
909 const WCHAR *ptr;
911 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
913 ptr = jsstr_flatten(str);
914 return ptr ? throw_error(ctx, hres, ptr) : E_OUTOFMEMORY;
917 /* ECMA-262 3rd Edition 12.14 */
918 static HRESULT interp_push_except(script_ctx_t *ctx)
920 const unsigned catch_off = get_op_uint(ctx, 0);
921 const unsigned finally_off = get_op_uint(ctx, 1);
922 call_frame_t *frame = ctx->call_ctx;
923 except_frame_t *except;
925 TRACE("\n");
927 except = heap_alloc(sizeof(*except));
928 if(!except)
929 return E_OUTOFMEMORY;
931 except->stack_top = ctx->stack_top;
932 except->scope = frame->scope;
933 except->catch_off = catch_off;
934 except->finally_off = finally_off;
935 except->next = frame->except_frame;
936 frame->except_frame = except;
937 return S_OK;
940 /* ECMA-262 3rd Edition 12.14 */
941 static HRESULT interp_pop_except(script_ctx_t *ctx)
943 const unsigned ret_off = get_op_uint(ctx, 0);
944 call_frame_t *frame = ctx->call_ctx;
945 except_frame_t *except;
946 unsigned finally_off;
948 TRACE("%u\n", ret_off);
950 except = frame->except_frame;
951 assert(except != NULL);
953 finally_off = except->finally_off;
954 frame->except_frame = except->next;
955 heap_free(except);
957 if(finally_off) {
958 HRESULT hres;
960 hres = stack_push(ctx, jsval_number(ret_off));
961 if(FAILED(hres))
962 return hres;
963 hres = stack_push(ctx, jsval_bool(TRUE));
964 if(FAILED(hres))
965 return hres;
966 frame->ip = finally_off;
967 }else {
968 frame->ip = ret_off;
971 return S_OK;
974 /* ECMA-262 3rd Edition 12.14 */
975 static HRESULT interp_end_finally(script_ctx_t *ctx)
977 call_frame_t *frame = ctx->call_ctx;
978 jsval_t v;
980 TRACE("\n");
982 v = stack_pop(ctx);
983 assert(is_bool(v));
985 if(!get_bool(v)) {
986 TRACE("passing exception\n");
988 set_error_value(ctx, stack_pop(ctx));
989 return DISP_E_EXCEPTION;
992 v = stack_pop(ctx);
993 assert(is_number(v));
994 frame->ip = get_number(v);
995 return S_OK;
998 static HRESULT interp_enter_catch(script_ctx_t *ctx)
1000 const BSTR ident = get_op_bstr(ctx, 0);
1001 jsdisp_t *scope_obj;
1002 jsval_t v;
1003 HRESULT hres;
1005 hres = create_dispex(ctx, NULL, NULL, &scope_obj);
1006 if(FAILED(hres))
1007 return hres;
1009 v = stack_pop(ctx);
1010 hres = jsdisp_propput_name(scope_obj, ident, v);
1011 jsval_release(v);
1012 if(SUCCEEDED(hres))
1013 hres = scope_push(ctx->call_ctx->scope, scope_obj, to_disp(scope_obj), &ctx->call_ctx->scope);
1014 jsdisp_release(scope_obj);
1015 return hres;
1018 /* ECMA-262 3rd Edition 13 */
1019 static HRESULT interp_func(script_ctx_t *ctx)
1021 unsigned func_idx = get_op_uint(ctx, 0);
1022 call_frame_t *frame = ctx->call_ctx;
1023 jsdisp_t *dispex;
1024 HRESULT hres;
1026 TRACE("%d\n", func_idx);
1028 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+func_idx,
1029 frame->scope, &dispex);
1030 if(FAILED(hres))
1031 return hres;
1033 return stack_push(ctx, jsval_obj(dispex));
1036 /* ECMA-262 3rd Edition 11.2.1 */
1037 static HRESULT interp_array(script_ctx_t *ctx)
1039 jsstr_t *name_str;
1040 const WCHAR *name;
1041 jsval_t v, namev;
1042 IDispatch *obj;
1043 DISPID id;
1044 HRESULT hres;
1046 TRACE("\n");
1048 namev = stack_pop(ctx);
1050 hres = stack_pop_object(ctx, &obj);
1051 if(FAILED(hres)) {
1052 jsval_release(namev);
1053 return hres;
1056 hres = to_flat_string(ctx, namev, &name_str, &name);
1057 jsval_release(namev);
1058 if(FAILED(hres)) {
1059 IDispatch_Release(obj);
1060 return hres;
1063 hres = disp_get_id(ctx, obj, name, NULL, 0, &id);
1064 jsstr_release(name_str);
1065 if(SUCCEEDED(hres)) {
1066 hres = disp_propget(ctx, obj, id, &v);
1067 }else if(hres == DISP_E_UNKNOWNNAME) {
1068 v = jsval_undefined();
1069 hres = S_OK;
1071 IDispatch_Release(obj);
1072 if(FAILED(hres))
1073 return hres;
1075 return stack_push(ctx, v);
1078 /* ECMA-262 3rd Edition 11.2.1 */
1079 static HRESULT interp_member(script_ctx_t *ctx)
1081 const BSTR arg = get_op_bstr(ctx, 0);
1082 IDispatch *obj;
1083 jsval_t v;
1084 DISPID id;
1085 HRESULT hres;
1087 TRACE("\n");
1089 hres = stack_pop_object(ctx, &obj);
1090 if(FAILED(hres))
1091 return hres;
1093 hres = disp_get_id(ctx, obj, arg, arg, 0, &id);
1094 if(SUCCEEDED(hres)) {
1095 hres = disp_propget(ctx, obj, id, &v);
1096 }else if(hres == DISP_E_UNKNOWNNAME) {
1097 v = jsval_undefined();
1098 hres = S_OK;
1100 IDispatch_Release(obj);
1101 if(FAILED(hres))
1102 return hres;
1104 return stack_push(ctx, v);
1107 /* ECMA-262 3rd Edition 11.2.1 */
1108 static HRESULT interp_memberid(script_ctx_t *ctx)
1110 const unsigned arg = get_op_uint(ctx, 0);
1111 jsval_t objv, namev;
1112 const WCHAR *name;
1113 jsstr_t *name_str;
1114 IDispatch *obj;
1115 exprval_t ref;
1116 DISPID id;
1117 HRESULT hres;
1119 TRACE("%x\n", arg);
1121 namev = stack_pop(ctx);
1122 objv = stack_pop(ctx);
1124 hres = to_object(ctx, objv, &obj);
1125 jsval_release(objv);
1126 if(SUCCEEDED(hres)) {
1127 hres = to_flat_string(ctx, namev, &name_str, &name);
1128 if(FAILED(hres))
1129 IDispatch_Release(obj);
1131 jsval_release(namev);
1132 if(FAILED(hres))
1133 return hres;
1135 hres = disp_get_id(ctx, obj, name, NULL, arg, &id);
1136 jsstr_release(name_str);
1137 if(SUCCEEDED(hres)) {
1138 ref.type = EXPRVAL_IDREF;
1139 ref.u.idref.disp = obj;
1140 ref.u.idref.id = id;
1141 }else {
1142 IDispatch_Release(obj);
1143 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1144 exprval_set_exception(&ref, JS_E_INVALID_PROPERTY);
1145 hres = S_OK;
1146 }else {
1147 ERR("failed %08x\n", hres);
1148 return hres;
1152 return stack_push_exprval(ctx, &ref);
1155 /* ECMA-262 3rd Edition 11.2.1 */
1156 static HRESULT interp_refval(script_ctx_t *ctx)
1158 exprval_t ref;
1159 jsval_t v;
1160 HRESULT hres;
1162 TRACE("\n");
1164 if(!stack_topn_exprval(ctx, 0, &ref))
1165 return JS_E_ILLEGAL_ASSIGN;
1167 hres = exprval_propget(ctx, &ref, &v);
1168 if(FAILED(hres))
1169 return hres;
1171 return stack_push(ctx, v);
1174 /* ECMA-262 3rd Edition 11.2.2 */
1175 static HRESULT interp_new(script_ctx_t *ctx)
1177 const unsigned argc = get_op_uint(ctx, 0);
1178 jsval_t constr;
1180 TRACE("%d\n", argc);
1182 constr = stack_topn(ctx, argc);
1184 /* NOTE: Should use to_object here */
1186 if(is_null(constr))
1187 return JS_E_OBJECT_EXPECTED;
1188 else if(!is_object_instance(constr))
1189 return JS_E_INVALID_ACTION;
1190 else if(!get_object(constr))
1191 return JS_E_INVALID_PROPERTY;
1193 clear_acc(ctx);
1194 return disp_call_value(ctx, get_object(constr), NULL, DISPATCH_CONSTRUCT | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1195 argc, stack_args(ctx, argc), &ctx->acc);
1198 /* ECMA-262 3rd Edition 11.2.3 */
1199 static HRESULT interp_call(script_ctx_t *ctx)
1201 const unsigned argn = get_op_uint(ctx, 0);
1202 const int do_ret = get_op_int(ctx, 1);
1203 jsval_t obj;
1205 TRACE("%d %d\n", argn, do_ret);
1207 obj = stack_topn(ctx, argn);
1208 if(!is_object_instance(obj))
1209 return JS_E_INVALID_PROPERTY;
1211 clear_acc(ctx);
1212 return disp_call_value(ctx, get_object(obj), NULL, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1213 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1216 /* ECMA-262 3rd Edition 11.2.3 */
1217 static HRESULT interp_call_member(script_ctx_t *ctx)
1219 const unsigned argn = get_op_uint(ctx, 0);
1220 const int do_ret = get_op_int(ctx, 1);
1221 exprval_t ref;
1223 TRACE("%d %d\n", argn, do_ret);
1225 if(!stack_topn_exprval(ctx, argn, &ref))
1226 return ref.u.hres;
1228 clear_acc(ctx);
1229 return exprval_call(ctx, &ref, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1230 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1233 /* ECMA-262 3rd Edition 11.1.1 */
1234 static HRESULT interp_this(script_ctx_t *ctx)
1236 IDispatch *this_obj = ctx->call_ctx->this_obj;
1238 TRACE("\n");
1240 if(!this_obj)
1241 this_obj = lookup_global_host(ctx);
1243 IDispatch_AddRef(this_obj);
1244 return stack_push(ctx, jsval_disp(this_obj));
1247 static HRESULT interp_identifier_ref(script_ctx_t *ctx, BSTR identifier, unsigned flags)
1249 exprval_t exprval;
1250 HRESULT hres;
1252 hres = identifier_eval(ctx, identifier, &exprval);
1253 if(FAILED(hres))
1254 return hres;
1256 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1257 DISPID id;
1259 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
1260 if(FAILED(hres))
1261 return hres;
1263 exprval_set_disp_ref(&exprval, to_disp(ctx->global), id);
1266 if(exprval.type == EXPRVAL_JSVAL || exprval.type == EXPRVAL_INVALID) {
1267 WARN("invalid ref\n");
1268 exprval_release(&exprval);
1269 exprval_set_exception(&exprval, JS_E_OBJECT_EXPECTED);
1272 return stack_push_exprval(ctx, &exprval);
1275 static HRESULT identifier_value(script_ctx_t *ctx, BSTR identifier)
1277 exprval_t exprval;
1278 jsval_t v;
1279 HRESULT hres;
1281 hres = identifier_eval(ctx, identifier, &exprval);
1282 if(FAILED(hres))
1283 return hres;
1285 if(exprval.type == EXPRVAL_INVALID)
1286 return throw_error(ctx, exprval.u.hres, identifier);
1288 hres = exprval_to_value(ctx, &exprval, &v);
1289 if(FAILED(hres))
1290 return hres;
1292 return stack_push(ctx, v);
1295 static HRESULT interp_local_ref(script_ctx_t *ctx)
1297 const int arg = get_op_int(ctx, 0);
1298 const unsigned flags = get_op_uint(ctx, 1);
1299 call_frame_t *frame = ctx->call_ctx;
1300 exprval_t ref;
1302 TRACE("%d\n", arg);
1304 if(!frame->base_scope || !frame->base_scope->frame)
1305 return interp_identifier_ref(ctx, local_name(frame, arg), flags);
1307 ref.type = EXPRVAL_STACK_REF;
1308 ref.u.off = local_off(frame, arg);
1309 return stack_push_exprval(ctx, &ref);
1312 static HRESULT interp_local(script_ctx_t *ctx)
1314 const int arg = get_op_int(ctx, 0);
1315 call_frame_t *frame = ctx->call_ctx;
1316 jsval_t copy;
1317 HRESULT hres;
1319 TRACE("%d: %s\n", arg, debugstr_w(local_name(frame, arg)));
1321 if(!frame->base_scope || !frame->base_scope->frame)
1322 return identifier_value(ctx, local_name(frame, arg));
1324 hres = jsval_copy(ctx->stack[local_off(frame, arg)], &copy);
1325 if(FAILED(hres))
1326 return hres;
1328 return stack_push(ctx, copy);
1331 /* ECMA-262 3rd Edition 10.1.4 */
1332 static HRESULT interp_ident(script_ctx_t *ctx)
1334 const BSTR arg = get_op_bstr(ctx, 0);
1336 TRACE("%s\n", debugstr_w(arg));
1338 return identifier_value(ctx, arg);
1341 /* ECMA-262 3rd Edition 10.1.4 */
1342 static HRESULT interp_identid(script_ctx_t *ctx)
1344 const BSTR arg = get_op_bstr(ctx, 0);
1345 const unsigned flags = get_op_uint(ctx, 1);
1347 TRACE("%s %x\n", debugstr_w(arg), flags);
1349 return interp_identifier_ref(ctx, arg, flags);
1352 /* ECMA-262 3rd Edition 7.8.1 */
1353 static HRESULT interp_null(script_ctx_t *ctx)
1355 TRACE("\n");
1357 return stack_push(ctx, jsval_null());
1360 /* ECMA-262 3rd Edition 7.8.2 */
1361 static HRESULT interp_bool(script_ctx_t *ctx)
1363 const int arg = get_op_int(ctx, 0);
1365 TRACE("%s\n", arg ? "true" : "false");
1367 return stack_push(ctx, jsval_bool(arg));
1370 /* ECMA-262 3rd Edition 7.8.3 */
1371 static HRESULT interp_int(script_ctx_t *ctx)
1373 const int arg = get_op_int(ctx, 0);
1375 TRACE("%d\n", arg);
1377 return stack_push(ctx, jsval_number(arg));
1380 /* ECMA-262 3rd Edition 7.8.3 */
1381 static HRESULT interp_double(script_ctx_t *ctx)
1383 const double arg = get_op_double(ctx);
1385 TRACE("%lf\n", arg);
1387 return stack_push(ctx, jsval_number(arg));
1390 /* ECMA-262 3rd Edition 7.8.4 */
1391 static HRESULT interp_str(script_ctx_t *ctx)
1393 jsstr_t *str = get_op_str(ctx, 0);
1395 TRACE("%s\n", debugstr_jsstr(str));
1397 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1400 /* ECMA-262 3rd Edition 7.8 */
1401 static HRESULT interp_regexp(script_ctx_t *ctx)
1403 jsstr_t *source = get_op_str(ctx, 0);
1404 const unsigned flags = get_op_uint(ctx, 1);
1405 jsdisp_t *regexp;
1406 HRESULT hres;
1408 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1410 hres = create_regexp(ctx, source, flags, &regexp);
1411 if(FAILED(hres))
1412 return hres;
1414 return stack_push(ctx, jsval_obj(regexp));
1417 /* ECMA-262 3rd Edition 11.1.4 */
1418 static HRESULT interp_carray(script_ctx_t *ctx)
1420 const unsigned arg = get_op_uint(ctx, 0);
1421 jsdisp_t *array;
1422 HRESULT hres;
1424 TRACE("%u\n", arg);
1426 hres = create_array(ctx, arg, &array);
1427 if(FAILED(hres))
1428 return hres;
1430 return stack_push(ctx, jsval_obj(array));
1433 static HRESULT interp_carray_set(script_ctx_t *ctx)
1435 const unsigned index = get_op_uint(ctx, 0);
1436 jsval_t value, array;
1437 HRESULT hres;
1439 value = stack_pop(ctx);
1441 TRACE("[%u] = %s\n", index, debugstr_jsval(value));
1443 array = stack_top(ctx);
1444 assert(is_object_instance(array));
1446 hres = jsdisp_propput_idx(iface_to_jsdisp(get_object(array)), index, value);
1447 jsval_release(value);
1448 return hres;
1451 /* ECMA-262 3rd Edition 11.1.5 */
1452 static HRESULT interp_new_obj(script_ctx_t *ctx)
1454 jsdisp_t *obj;
1455 HRESULT hres;
1457 TRACE("\n");
1459 hres = create_object(ctx, NULL, &obj);
1460 if(FAILED(hres))
1461 return hres;
1463 return stack_push(ctx, jsval_obj(obj));
1466 /* ECMA-262 3rd Edition 11.1.5 */
1467 static HRESULT interp_obj_prop(script_ctx_t *ctx)
1469 jsstr_t *name_arg = get_op_str(ctx, 0);
1470 unsigned type = get_op_uint(ctx, 1);
1471 const WCHAR *name;
1472 jsdisp_t *obj;
1473 jsval_t val;
1474 HRESULT hres;
1476 TRACE("%s\n", debugstr_jsstr(name_arg));
1478 val = stack_pop(ctx);
1480 /* FIXME: we should pass it as jsstr_t */
1481 name = jsstr_flatten(name_arg);
1483 assert(is_object_instance(stack_top(ctx)));
1484 obj = as_jsdisp(get_object(stack_top(ctx)));
1486 if(type == PROPERTY_DEFINITION_VALUE) {
1487 hres = jsdisp_propput_name(obj, name, val);
1488 }else {
1489 property_desc_t desc = {PROPF_ENUMERABLE | PROPF_CONFIGURABLE};
1490 jsdisp_t *func;
1492 assert(is_object_instance(val));
1493 func = iface_to_jsdisp(get_object(val));
1495 desc.mask = desc.flags;
1496 if(type == PROPERTY_DEFINITION_GETTER) {
1497 desc.explicit_getter = TRUE;
1498 desc.getter = func;
1499 }else {
1500 desc.explicit_setter = TRUE;
1501 desc.setter = func;
1504 hres = jsdisp_define_property(obj, name, &desc);
1505 jsdisp_release(func);
1508 jsval_release(val);
1509 return hres;
1512 /* ECMA-262 3rd Edition 11.11 */
1513 static HRESULT interp_cnd_nz(script_ctx_t *ctx)
1515 const unsigned arg = get_op_uint(ctx, 0);
1516 BOOL b;
1517 HRESULT hres;
1519 TRACE("\n");
1521 hres = to_boolean(stack_top(ctx), &b);
1522 if(FAILED(hres))
1523 return hres;
1525 if(b) {
1526 jmp_abs(ctx, arg);
1527 }else {
1528 stack_popn(ctx, 1);
1529 jmp_next(ctx);
1531 return S_OK;
1534 /* ECMA-262 3rd Edition 11.11 */
1535 static HRESULT interp_cnd_z(script_ctx_t *ctx)
1537 const unsigned arg = get_op_uint(ctx, 0);
1538 BOOL b;
1539 HRESULT hres;
1541 TRACE("\n");
1543 hres = to_boolean(stack_top(ctx), &b);
1544 if(FAILED(hres))
1545 return hres;
1547 if(b) {
1548 stack_popn(ctx, 1);
1549 jmp_next(ctx);
1550 }else {
1551 jmp_abs(ctx, arg);
1553 return S_OK;
1556 /* ECMA-262 3rd Edition 11.10 */
1557 static HRESULT interp_or(script_ctx_t *ctx)
1559 INT l, r;
1560 HRESULT hres;
1562 TRACE("\n");
1564 hres = stack_pop_int(ctx, &r);
1565 if(FAILED(hres))
1566 return hres;
1568 hres = stack_pop_int(ctx, &l);
1569 if(FAILED(hres))
1570 return hres;
1572 return stack_push(ctx, jsval_number(l|r));
1575 /* ECMA-262 3rd Edition 11.10 */
1576 static HRESULT interp_xor(script_ctx_t *ctx)
1578 INT l, r;
1579 HRESULT hres;
1581 TRACE("\n");
1583 hres = stack_pop_int(ctx, &r);
1584 if(FAILED(hres))
1585 return hres;
1587 hres = stack_pop_int(ctx, &l);
1588 if(FAILED(hres))
1589 return hres;
1591 return stack_push(ctx, jsval_number(l^r));
1594 /* ECMA-262 3rd Edition 11.10 */
1595 static HRESULT interp_and(script_ctx_t *ctx)
1597 INT l, r;
1598 HRESULT hres;
1600 TRACE("\n");
1602 hres = stack_pop_int(ctx, &r);
1603 if(FAILED(hres))
1604 return hres;
1606 hres = stack_pop_int(ctx, &l);
1607 if(FAILED(hres))
1608 return hres;
1610 return stack_push(ctx, jsval_number(l&r));
1613 /* ECMA-262 3rd Edition 11.8.6 */
1614 static HRESULT interp_instanceof(script_ctx_t *ctx)
1616 jsdisp_t *obj, *iter, *tmp = NULL;
1617 jsval_t prot, v;
1618 BOOL ret = FALSE;
1619 HRESULT hres;
1621 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1623 v = stack_pop(ctx);
1624 if(!is_object_instance(v) || !get_object(v)) {
1625 jsval_release(v);
1626 return JS_E_FUNCTION_EXPECTED;
1629 obj = iface_to_jsdisp(get_object(v));
1630 IDispatch_Release(get_object(v));
1631 if(!obj) {
1632 FIXME("non-jsdisp objects not supported\n");
1633 return E_FAIL;
1636 if(is_class(obj, JSCLASS_FUNCTION)) {
1637 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1638 }else {
1639 hres = JS_E_FUNCTION_EXPECTED;
1641 jsdisp_release(obj);
1642 if(FAILED(hres))
1643 return hres;
1645 v = stack_pop(ctx);
1647 if(is_object_instance(prot)) {
1648 if(is_object_instance(v))
1649 tmp = iface_to_jsdisp(get_object(v));
1650 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1651 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1652 if(FAILED(hres))
1653 break;
1656 if(tmp)
1657 jsdisp_release(tmp);
1658 }else {
1659 FIXME("prototype is not an object\n");
1660 hres = E_FAIL;
1663 jsval_release(prot);
1664 jsval_release(v);
1665 if(FAILED(hres))
1666 return hres;
1668 return stack_push(ctx, jsval_bool(ret));
1671 /* ECMA-262 3rd Edition 11.8.7 */
1672 static HRESULT interp_in(script_ctx_t *ctx)
1674 const WCHAR *str;
1675 jsstr_t *jsstr;
1676 jsval_t obj, v;
1677 DISPID id = 0;
1678 BOOL ret;
1679 HRESULT hres;
1681 TRACE("\n");
1683 obj = stack_pop(ctx);
1684 if(!is_object_instance(obj) || !get_object(obj)) {
1685 jsval_release(obj);
1686 return JS_E_OBJECT_EXPECTED;
1689 v = stack_pop(ctx);
1690 hres = to_flat_string(ctx, v, &jsstr, &str);
1691 jsval_release(v);
1692 if(FAILED(hres)) {
1693 IDispatch_Release(get_object(obj));
1694 return hres;
1697 hres = disp_get_id(ctx, get_object(obj), str, NULL, 0, &id);
1698 IDispatch_Release(get_object(obj));
1699 jsstr_release(jsstr);
1700 if(SUCCEEDED(hres))
1701 ret = TRUE;
1702 else if(hres == DISP_E_UNKNOWNNAME)
1703 ret = FALSE;
1704 else
1705 return hres;
1707 return stack_push(ctx, jsval_bool(ret));
1710 /* ECMA-262 3rd Edition 11.6.1 */
1711 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1713 jsval_t r, l;
1714 HRESULT hres;
1716 hres = to_primitive(ctx, lval, &l, NO_HINT);
1717 if(FAILED(hres))
1718 return hres;
1720 hres = to_primitive(ctx, rval, &r, NO_HINT);
1721 if(FAILED(hres)) {
1722 jsval_release(l);
1723 return hres;
1726 if(is_string(l) || is_string(r)) {
1727 jsstr_t *lstr, *rstr = NULL;
1729 hres = to_string(ctx, l, &lstr);
1730 if(SUCCEEDED(hres))
1731 hres = to_string(ctx, r, &rstr);
1733 if(SUCCEEDED(hres)) {
1734 jsstr_t *ret_str;
1736 ret_str = jsstr_concat(lstr, rstr);
1737 if(ret_str)
1738 *ret = jsval_string(ret_str);
1739 else
1740 hres = E_OUTOFMEMORY;
1743 jsstr_release(lstr);
1744 if(rstr)
1745 jsstr_release(rstr);
1746 }else {
1747 double nl, nr;
1749 hres = to_number(ctx, l, &nl);
1750 if(SUCCEEDED(hres)) {
1751 hres = to_number(ctx, r, &nr);
1752 if(SUCCEEDED(hres))
1753 *ret = jsval_number(nl+nr);
1757 jsval_release(r);
1758 jsval_release(l);
1759 return hres;
1762 /* ECMA-262 3rd Edition 11.6.1 */
1763 static HRESULT interp_add(script_ctx_t *ctx)
1765 jsval_t l, r, ret;
1766 HRESULT hres;
1768 r = stack_pop(ctx);
1769 l = stack_pop(ctx);
1771 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1773 hres = add_eval(ctx, l, r, &ret);
1774 jsval_release(l);
1775 jsval_release(r);
1776 if(FAILED(hres))
1777 return hres;
1779 return stack_push(ctx, ret);
1782 /* ECMA-262 3rd Edition 11.6.2 */
1783 static HRESULT interp_sub(script_ctx_t *ctx)
1785 double l, r;
1786 HRESULT hres;
1788 TRACE("\n");
1790 hres = stack_pop_number(ctx, &r);
1791 if(FAILED(hres))
1792 return hres;
1794 hres = stack_pop_number(ctx, &l);
1795 if(FAILED(hres))
1796 return hres;
1798 return stack_push(ctx, jsval_number(l-r));
1801 /* ECMA-262 3rd Edition 11.5.1 */
1802 static HRESULT interp_mul(script_ctx_t *ctx)
1804 double l, r;
1805 HRESULT hres;
1807 TRACE("\n");
1809 hres = stack_pop_number(ctx, &r);
1810 if(FAILED(hres))
1811 return hres;
1813 hres = stack_pop_number(ctx, &l);
1814 if(FAILED(hres))
1815 return hres;
1817 return stack_push(ctx, jsval_number(l*r));
1820 /* ECMA-262 3rd Edition 11.5.2 */
1821 static HRESULT interp_div(script_ctx_t *ctx)
1823 double l, r;
1824 HRESULT hres;
1826 TRACE("\n");
1828 hres = stack_pop_number(ctx, &r);
1829 if(FAILED(hres))
1830 return hres;
1832 hres = stack_pop_number(ctx, &l);
1833 if(FAILED(hres))
1834 return hres;
1836 return stack_push(ctx, jsval_number(l/r));
1839 /* ECMA-262 3rd Edition 11.5.3 */
1840 static HRESULT interp_mod(script_ctx_t *ctx)
1842 double l, r;
1843 HRESULT hres;
1845 TRACE("\n");
1847 hres = stack_pop_number(ctx, &r);
1848 if(FAILED(hres))
1849 return hres;
1851 hres = stack_pop_number(ctx, &l);
1852 if(FAILED(hres))
1853 return hres;
1855 return stack_push(ctx, jsval_number(fmod(l, r)));
1858 /* ECMA-262 3rd Edition 11.4.2 */
1859 static HRESULT interp_delete(script_ctx_t *ctx)
1861 jsval_t objv, namev;
1862 IDispatch *obj;
1863 jsstr_t *name;
1864 BOOL ret;
1865 HRESULT hres;
1867 TRACE("\n");
1869 namev = stack_pop(ctx);
1870 objv = stack_pop(ctx);
1872 hres = to_object(ctx, objv, &obj);
1873 jsval_release(objv);
1874 if(FAILED(hres)) {
1875 jsval_release(namev);
1876 return hres;
1879 hres = to_string(ctx, namev, &name);
1880 jsval_release(namev);
1881 if(FAILED(hres)) {
1882 IDispatch_Release(obj);
1883 return hres;
1886 hres = disp_delete_name(ctx, obj, name, &ret);
1887 IDispatch_Release(obj);
1888 jsstr_release(name);
1889 if(FAILED(hres))
1890 return hres;
1892 return stack_push(ctx, jsval_bool(ret));
1895 /* ECMA-262 3rd Edition 11.4.2 */
1896 static HRESULT interp_delete_ident(script_ctx_t *ctx)
1898 const BSTR arg = get_op_bstr(ctx, 0);
1899 exprval_t exprval;
1900 BOOL ret;
1901 HRESULT hres;
1903 TRACE("%s\n", debugstr_w(arg));
1905 hres = identifier_eval(ctx, arg, &exprval);
1906 if(FAILED(hres))
1907 return hres;
1909 switch(exprval.type) {
1910 case EXPRVAL_STACK_REF:
1911 ret = FALSE;
1912 break;
1913 case EXPRVAL_IDREF:
1914 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1915 IDispatch_Release(exprval.u.idref.disp);
1916 if(FAILED(hres))
1917 return hres;
1918 break;
1919 case EXPRVAL_INVALID:
1920 ret = TRUE;
1921 break;
1922 default:
1923 FIXME("Unsupported exprval\n");
1924 exprval_release(&exprval);
1925 return E_NOTIMPL;
1929 return stack_push(ctx, jsval_bool(ret));
1932 /* ECMA-262 3rd Edition 11.4.2 */
1933 static HRESULT interp_void(script_ctx_t *ctx)
1935 TRACE("\n");
1937 stack_popn(ctx, 1);
1938 return stack_push(ctx, jsval_undefined());
1941 /* ECMA-262 3rd Edition 11.4.3 */
1942 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1944 switch(jsval_type(v)) {
1945 case JSV_UNDEFINED:
1946 *ret = undefinedW;
1947 break;
1948 case JSV_NULL:
1949 *ret = objectW;
1950 break;
1951 case JSV_OBJECT: {
1952 jsdisp_t *dispex;
1954 if(get_object(v) && (dispex = iface_to_jsdisp(get_object(v)))) {
1955 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1956 jsdisp_release(dispex);
1957 }else {
1958 *ret = objectW;
1960 break;
1962 case JSV_STRING:
1963 *ret = stringW;
1964 break;
1965 case JSV_NUMBER:
1966 *ret = numberW;
1967 break;
1968 case JSV_BOOL:
1969 *ret = booleanW;
1970 break;
1971 case JSV_VARIANT:
1972 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1973 return E_NOTIMPL;
1976 return S_OK;
1979 /* ECMA-262 3rd Edition 11.4.3 */
1980 static HRESULT interp_typeofid(script_ctx_t *ctx)
1982 const WCHAR *ret;
1983 exprval_t ref;
1984 jsval_t v;
1985 HRESULT hres;
1987 TRACE("\n");
1989 if(!stack_pop_exprval(ctx, &ref))
1990 return stack_push(ctx, jsval_string(jsstr_undefined()));
1992 hres = exprval_propget(ctx, &ref, &v);
1993 exprval_release(&ref);
1994 if(FAILED(hres))
1995 return stack_push_string(ctx, unknownW);
1997 hres = typeof_string(v, &ret);
1998 jsval_release(v);
1999 if(FAILED(hres))
2000 return hres;
2002 return stack_push_string(ctx, ret);
2005 /* ECMA-262 3rd Edition 11.4.3 */
2006 static HRESULT interp_typeofident(script_ctx_t *ctx)
2008 const BSTR arg = get_op_bstr(ctx, 0);
2009 exprval_t exprval;
2010 const WCHAR *ret;
2011 jsval_t v;
2012 HRESULT hres;
2014 TRACE("%s\n", debugstr_w(arg));
2016 hres = identifier_eval(ctx, arg, &exprval);
2017 if(FAILED(hres))
2018 return hres;
2020 if(exprval.type == EXPRVAL_INVALID)
2021 return stack_push(ctx, jsval_string(jsstr_undefined()));
2023 hres = exprval_to_value(ctx, &exprval, &v);
2024 if(FAILED(hres))
2025 return hres;
2027 hres = typeof_string(v, &ret);
2028 jsval_release(v);
2029 if(FAILED(hres))
2030 return hres;
2032 return stack_push_string(ctx, ret);
2035 /* ECMA-262 3rd Edition 11.4.3 */
2036 static HRESULT interp_typeof(script_ctx_t *ctx)
2038 const WCHAR *ret;
2039 jsval_t v;
2040 HRESULT hres;
2042 TRACE("\n");
2044 v = stack_pop(ctx);
2045 hres = typeof_string(v, &ret);
2046 jsval_release(v);
2047 if(FAILED(hres))
2048 return hres;
2050 return stack_push_string(ctx, ret);
2053 /* ECMA-262 3rd Edition 11.4.7 */
2054 static HRESULT interp_minus(script_ctx_t *ctx)
2056 double n;
2057 HRESULT hres;
2059 TRACE("\n");
2061 hres = stack_pop_number(ctx, &n);
2062 if(FAILED(hres))
2063 return hres;
2065 return stack_push(ctx, jsval_number(-n));
2068 /* ECMA-262 3rd Edition 11.4.6 */
2069 static HRESULT interp_tonum(script_ctx_t *ctx)
2071 jsval_t v;
2072 double n;
2073 HRESULT hres;
2075 TRACE("\n");
2077 v = stack_pop(ctx);
2078 hres = to_number(ctx, v, &n);
2079 jsval_release(v);
2080 if(FAILED(hres))
2081 return hres;
2083 return stack_push(ctx, jsval_number(n));
2086 /* ECMA-262 3rd Edition 11.3.1 */
2087 static HRESULT interp_postinc(script_ctx_t *ctx)
2089 const int arg = get_op_int(ctx, 0);
2090 exprval_t ref;
2091 jsval_t v;
2092 HRESULT hres;
2094 TRACE("%d\n", arg);
2096 if(!stack_pop_exprval(ctx, &ref))
2097 return JS_E_OBJECT_EXPECTED;
2099 hres = exprval_propget(ctx, &ref, &v);
2100 if(SUCCEEDED(hres)) {
2101 double n;
2103 hres = to_number(ctx, v, &n);
2104 if(SUCCEEDED(hres))
2105 hres = exprval_propput(ctx, &ref, jsval_number(n+(double)arg));
2106 if(FAILED(hres))
2107 jsval_release(v);
2109 exprval_release(&ref);
2110 if(FAILED(hres))
2111 return hres;
2113 return stack_push(ctx, v);
2116 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2117 static HRESULT interp_preinc(script_ctx_t *ctx)
2119 const int arg = get_op_int(ctx, 0);
2120 exprval_t ref;
2121 double ret;
2122 jsval_t v;
2123 HRESULT hres;
2125 TRACE("%d\n", arg);
2127 if(!stack_pop_exprval(ctx, &ref))
2128 return JS_E_OBJECT_EXPECTED;
2130 hres = exprval_propget(ctx, &ref, &v);
2131 if(SUCCEEDED(hres)) {
2132 double n;
2134 hres = to_number(ctx, v, &n);
2135 jsval_release(v);
2136 if(SUCCEEDED(hres)) {
2137 ret = n+(double)arg;
2138 hres = exprval_propput(ctx, &ref, jsval_number(ret));
2141 exprval_release(&ref);
2142 if(FAILED(hres))
2143 return hres;
2145 return stack_push(ctx, jsval_number(ret));
2148 /* ECMA-262 3rd Edition 11.9.3 */
2149 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
2151 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
2152 return jsval_strict_equal(lval, rval, ret);
2154 /* FIXME: NULL disps should be handled in more general way */
2155 if(is_object_instance(lval) && !get_object(lval))
2156 return equal_values(ctx, jsval_null(), rval, ret);
2157 if(is_object_instance(rval) && !get_object(rval))
2158 return equal_values(ctx, lval, jsval_null(), ret);
2160 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
2161 *ret = TRUE;
2162 return S_OK;
2165 if(is_string(lval) && is_number(rval)) {
2166 double n;
2167 HRESULT hres;
2169 hres = to_number(ctx, lval, &n);
2170 if(FAILED(hres))
2171 return hres;
2173 /* FIXME: optimize */
2174 return equal_values(ctx, jsval_number(n), rval, ret);
2177 if(is_string(rval) && is_number(lval)) {
2178 double n;
2179 HRESULT hres;
2181 hres = to_number(ctx, rval, &n);
2182 if(FAILED(hres))
2183 return hres;
2185 /* FIXME: optimize */
2186 return equal_values(ctx, lval, jsval_number(n), ret);
2189 if(is_bool(rval))
2190 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
2192 if(is_bool(lval))
2193 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
2196 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
2197 jsval_t prim;
2198 HRESULT hres;
2200 hres = to_primitive(ctx, rval, &prim, NO_HINT);
2201 if(FAILED(hres))
2202 return hres;
2204 hres = equal_values(ctx, lval, prim, ret);
2205 jsval_release(prim);
2206 return hres;
2210 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
2211 jsval_t prim;
2212 HRESULT hres;
2214 hres = to_primitive(ctx, lval, &prim, NO_HINT);
2215 if(FAILED(hres))
2216 return hres;
2218 hres = equal_values(ctx, prim, rval, ret);
2219 jsval_release(prim);
2220 return hres;
2224 *ret = FALSE;
2225 return S_OK;
2228 /* ECMA-262 3rd Edition 11.9.1 */
2229 static HRESULT interp_eq(script_ctx_t *ctx)
2231 jsval_t l, r;
2232 BOOL b;
2233 HRESULT hres;
2235 r = stack_pop(ctx);
2236 l = stack_pop(ctx);
2238 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
2240 hres = equal_values(ctx, l, r, &b);
2241 jsval_release(l);
2242 jsval_release(r);
2243 if(FAILED(hres))
2244 return hres;
2246 return stack_push(ctx, jsval_bool(b));
2249 /* ECMA-262 3rd Edition 11.9.2 */
2250 static HRESULT interp_neq(script_ctx_t *ctx)
2252 jsval_t l, r;
2253 BOOL b;
2254 HRESULT hres;
2256 r = stack_pop(ctx);
2257 l = stack_pop(ctx);
2259 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2261 hres = equal_values(ctx, l, r, &b);
2262 jsval_release(l);
2263 jsval_release(r);
2264 if(FAILED(hres))
2265 return hres;
2267 return stack_push(ctx, jsval_bool(!b));
2270 /* ECMA-262 3rd Edition 11.9.4 */
2271 static HRESULT interp_eq2(script_ctx_t *ctx)
2273 jsval_t l, r;
2274 BOOL b;
2275 HRESULT hres;
2277 r = stack_pop(ctx);
2278 l = stack_pop(ctx);
2280 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2282 hres = jsval_strict_equal(r, l, &b);
2283 jsval_release(l);
2284 jsval_release(r);
2285 if(FAILED(hres))
2286 return hres;
2288 return stack_push(ctx, jsval_bool(b));
2291 /* ECMA-262 3rd Edition 11.9.5 */
2292 static HRESULT interp_neq2(script_ctx_t *ctx)
2294 jsval_t l, r;
2295 BOOL b;
2296 HRESULT hres;
2298 TRACE("\n");
2300 r = stack_pop(ctx);
2301 l = stack_pop(ctx);
2303 hres = jsval_strict_equal(r, l, &b);
2304 jsval_release(l);
2305 jsval_release(r);
2306 if(FAILED(hres))
2307 return hres;
2309 return stack_push(ctx, jsval_bool(!b));
2312 /* ECMA-262 3rd Edition 11.8.5 */
2313 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2315 double ln, rn;
2316 jsval_t l, r;
2317 HRESULT hres;
2319 hres = to_primitive(ctx, lval, &l, NO_HINT);
2320 if(FAILED(hres))
2321 return hres;
2323 hres = to_primitive(ctx, rval, &r, NO_HINT);
2324 if(FAILED(hres)) {
2325 jsval_release(l);
2326 return hres;
2329 if(is_string(l) && is_string(r)) {
2330 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2331 jsstr_release(get_string(l));
2332 jsstr_release(get_string(r));
2333 return S_OK;
2336 hres = to_number(ctx, l, &ln);
2337 jsval_release(l);
2338 if(SUCCEEDED(hres))
2339 hres = to_number(ctx, r, &rn);
2340 jsval_release(r);
2341 if(FAILED(hres))
2342 return hres;
2344 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2345 return S_OK;
2348 /* ECMA-262 3rd Edition 11.8.1 */
2349 static HRESULT interp_lt(script_ctx_t *ctx)
2351 jsval_t l, r;
2352 BOOL b;
2353 HRESULT hres;
2355 r = stack_pop(ctx);
2356 l = stack_pop(ctx);
2358 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2360 hres = less_eval(ctx, l, r, FALSE, &b);
2361 jsval_release(l);
2362 jsval_release(r);
2363 if(FAILED(hres))
2364 return hres;
2366 return stack_push(ctx, jsval_bool(b));
2369 /* ECMA-262 3rd Edition 11.8.1 */
2370 static HRESULT interp_lteq(script_ctx_t *ctx)
2372 jsval_t l, r;
2373 BOOL b;
2374 HRESULT hres;
2376 r = stack_pop(ctx);
2377 l = stack_pop(ctx);
2379 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2381 hres = less_eval(ctx, r, l, TRUE, &b);
2382 jsval_release(l);
2383 jsval_release(r);
2384 if(FAILED(hres))
2385 return hres;
2387 return stack_push(ctx, jsval_bool(b));
2390 /* ECMA-262 3rd Edition 11.8.2 */
2391 static HRESULT interp_gt(script_ctx_t *ctx)
2393 jsval_t l, r;
2394 BOOL b;
2395 HRESULT hres;
2397 r = stack_pop(ctx);
2398 l = stack_pop(ctx);
2400 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2402 hres = less_eval(ctx, r, l, FALSE, &b);
2403 jsval_release(l);
2404 jsval_release(r);
2405 if(FAILED(hres))
2406 return hres;
2408 return stack_push(ctx, jsval_bool(b));
2411 /* ECMA-262 3rd Edition 11.8.4 */
2412 static HRESULT interp_gteq(script_ctx_t *ctx)
2414 jsval_t l, r;
2415 BOOL b;
2416 HRESULT hres;
2418 r = stack_pop(ctx);
2419 l = stack_pop(ctx);
2421 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2423 hres = less_eval(ctx, l, r, TRUE, &b);
2424 jsval_release(l);
2425 jsval_release(r);
2426 if(FAILED(hres))
2427 return hres;
2429 return stack_push(ctx, jsval_bool(b));
2432 /* ECMA-262 3rd Edition 11.4.8 */
2433 static HRESULT interp_bneg(script_ctx_t *ctx)
2435 jsval_t v;
2436 INT i;
2437 HRESULT hres;
2439 TRACE("\n");
2441 v = stack_pop(ctx);
2442 hres = to_int32(ctx, v, &i);
2443 jsval_release(v);
2444 if(FAILED(hres))
2445 return hres;
2447 return stack_push(ctx, jsval_number(~i));
2450 /* ECMA-262 3rd Edition 11.4.9 */
2451 static HRESULT interp_neg(script_ctx_t *ctx)
2453 jsval_t v;
2454 BOOL b;
2455 HRESULT hres;
2457 TRACE("\n");
2459 v = stack_pop(ctx);
2460 hres = to_boolean(v, &b);
2461 jsval_release(v);
2462 if(FAILED(hres))
2463 return hres;
2465 return stack_push(ctx, jsval_bool(!b));
2468 /* ECMA-262 3rd Edition 11.7.1 */
2469 static HRESULT interp_lshift(script_ctx_t *ctx)
2471 DWORD r;
2472 INT l;
2473 HRESULT hres;
2475 hres = stack_pop_uint(ctx, &r);
2476 if(FAILED(hres))
2477 return hres;
2479 hres = stack_pop_int(ctx, &l);
2480 if(FAILED(hres))
2481 return hres;
2483 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2486 /* ECMA-262 3rd Edition 11.7.2 */
2487 static HRESULT interp_rshift(script_ctx_t *ctx)
2489 DWORD r;
2490 INT l;
2491 HRESULT hres;
2493 hres = stack_pop_uint(ctx, &r);
2494 if(FAILED(hres))
2495 return hres;
2497 hres = stack_pop_int(ctx, &l);
2498 if(FAILED(hres))
2499 return hres;
2501 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2504 /* ECMA-262 3rd Edition 11.7.3 */
2505 static HRESULT interp_rshift2(script_ctx_t *ctx)
2507 DWORD r, l;
2508 HRESULT hres;
2510 hres = stack_pop_uint(ctx, &r);
2511 if(FAILED(hres))
2512 return hres;
2514 hres = stack_pop_uint(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.13.1 */
2522 static HRESULT interp_assign(script_ctx_t *ctx)
2524 exprval_t ref;
2525 jsval_t v;
2526 HRESULT hres;
2528 TRACE("\n");
2530 v = stack_pop(ctx);
2532 if(!stack_pop_exprval(ctx, &ref)) {
2533 jsval_release(v);
2534 return JS_E_ILLEGAL_ASSIGN;
2537 hres = exprval_propput(ctx, &ref, v);
2538 exprval_release(&ref);
2539 if(FAILED(hres)) {
2540 jsval_release(v);
2541 return hres;
2544 return stack_push(ctx, v);
2547 /* JScript extension */
2548 static HRESULT interp_assign_call(script_ctx_t *ctx)
2550 const unsigned argc = get_op_uint(ctx, 0);
2551 exprval_t ref;
2552 jsval_t v;
2553 HRESULT hres;
2555 TRACE("%u\n", argc);
2557 if(!stack_topn_exprval(ctx, argc+1, &ref))
2558 return JS_E_ILLEGAL_ASSIGN;
2560 hres = exprval_call(ctx, &ref, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2561 if(FAILED(hres))
2562 return hres;
2564 v = stack_pop(ctx);
2565 stack_popn(ctx, argc+2);
2566 return stack_push(ctx, v);
2569 static HRESULT interp_undefined(script_ctx_t *ctx)
2571 TRACE("\n");
2573 return stack_push(ctx, jsval_undefined());
2576 static HRESULT interp_jmp(script_ctx_t *ctx)
2578 const unsigned arg = get_op_uint(ctx, 0);
2580 TRACE("%u\n", arg);
2582 jmp_abs(ctx, arg);
2583 return S_OK;
2586 static HRESULT interp_jmp_z(script_ctx_t *ctx)
2588 const unsigned arg = get_op_uint(ctx, 0);
2589 BOOL b;
2590 jsval_t v;
2591 HRESULT hres;
2593 TRACE("\n");
2595 v = stack_pop(ctx);
2596 hres = to_boolean(v, &b);
2597 jsval_release(v);
2598 if(FAILED(hres))
2599 return hres;
2601 if(b)
2602 jmp_next(ctx);
2603 else
2604 jmp_abs(ctx, arg);
2605 return S_OK;
2608 static HRESULT interp_pop(script_ctx_t *ctx)
2610 const unsigned arg = get_op_uint(ctx, 0);
2612 TRACE("%u\n", arg);
2614 stack_popn(ctx, arg);
2615 return S_OK;
2618 static HRESULT interp_ret(script_ctx_t *ctx)
2620 const unsigned clear_ret = get_op_uint(ctx, 0);
2621 call_frame_t *frame = ctx->call_ctx;
2623 TRACE("\n");
2625 if(clear_ret)
2626 jsval_release(steal_ret(frame));
2628 if((frame->flags & EXEC_CONSTRUCTOR) && !is_object_instance(frame->ret)) {
2629 jsval_release(frame->ret);
2630 IDispatch_AddRef(frame->this_obj);
2631 frame->ret = jsval_disp(frame->this_obj);
2634 jmp_abs(ctx, -1);
2635 return S_OK;
2638 static HRESULT interp_setret(script_ctx_t *ctx)
2640 call_frame_t *frame = ctx->call_ctx;
2642 TRACE("\n");
2644 jsval_release(frame->ret);
2645 frame->ret = stack_pop(ctx);
2646 return S_OK;
2649 static HRESULT interp_push_acc(script_ctx_t *ctx)
2651 HRESULT hres;
2653 TRACE("\n");
2655 hres = stack_push(ctx, ctx->acc);
2656 if(SUCCEEDED(hres))
2657 ctx->acc = jsval_undefined();
2658 return hres;
2661 typedef HRESULT (*op_func_t)(script_ctx_t*);
2663 static const op_func_t op_funcs[] = {
2664 #define X(x,a,b,c) interp_##x,
2665 OP_LIST
2666 #undef X
2669 static const unsigned op_move[] = {
2670 #define X(a,x,b,c) x,
2671 OP_LIST
2672 #undef X
2675 static void pop_call_frame(script_ctx_t *ctx)
2677 call_frame_t *frame = ctx->call_ctx;
2679 frame->stack_base -= frame->pop_locals + frame->pop_variables;
2681 assert(frame->scope == frame->base_scope);
2683 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2684 if(frame->scope && frame->scope->ref > 1) {
2685 HRESULT hres = detach_variable_object(ctx, frame, TRUE);
2686 if(FAILED(hres))
2687 ERR("Failed to detach variable object: %08x\n", hres);
2690 if(frame->arguments_obj)
2691 detach_arguments_object(frame->arguments_obj);
2692 if(frame->scope)
2693 scope_release(frame->scope);
2695 if(frame->pop_variables)
2696 stack_popn(ctx, frame->pop_variables);
2697 stack_popn(ctx, frame->pop_locals);
2699 ctx->call_ctx = frame->prev_frame;
2701 if(frame->function_instance)
2702 jsdisp_release(frame->function_instance);
2703 if(frame->variable_obj)
2704 jsdisp_release(frame->variable_obj);
2705 if(frame->this_obj)
2706 IDispatch_Release(frame->this_obj);
2707 jsval_release(frame->ret);
2708 release_bytecode(frame->bytecode);
2709 heap_free(frame);
2712 static void print_backtrace(script_ctx_t *ctx)
2714 unsigned depth = 0, i;
2715 call_frame_t *frame;
2717 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
2718 WARN("%u\t", depth);
2719 depth++;
2721 if(frame->this_obj)
2722 WARN("%p->", frame->this_obj);
2723 WARN("%s(", frame->function->name ? debugstr_w(frame->function->name) : "[unnamed]");
2724 if(frame->base_scope && frame->base_scope->frame) {
2725 for(i=0; i < frame->argc; i++) {
2726 if(i < frame->function->param_cnt)
2727 WARN("%s%s=%s", i ? ", " : "", debugstr_w(frame->function->params[i]),
2728 debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2729 else
2730 WARN("%s%s", i ? ", " : "", debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2732 }else {
2733 WARN("[detached frame]");
2735 WARN(")\n");
2737 if(!(frame->flags & EXEC_RETURN_TO_INTERP)) {
2738 WARN("%u\t[native code]\n", depth);
2739 depth++;
2744 static HRESULT unwind_exception(script_ctx_t *ctx, HRESULT exception_hres)
2746 except_frame_t *except_frame;
2747 jsexcept_t *ei = ctx->ei;
2748 call_frame_t *frame;
2749 jsval_t except_val;
2750 unsigned catch_off;
2751 HRESULT hres;
2753 if(WARN_ON(jscript)) {
2754 jsdisp_t *error_obj;
2755 jsval_t msg;
2757 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
2759 WARN("Exception %08x %s", exception_hres, debugstr_jsval(ei->valid_value ? ei->value : jsval_undefined()));
2760 if(ei->valid_value && jsval_type(ei->value) == JSV_OBJECT) {
2761 error_obj = to_jsdisp(get_object(ei->value));
2762 if(error_obj) {
2763 hres = jsdisp_propget_name(error_obj, messageW, &msg);
2764 if(SUCCEEDED(hres)) {
2765 WARN(" (message %s)", debugstr_jsval(msg));
2766 jsval_release(msg);
2770 WARN(" in:\n");
2772 print_backtrace(ctx);
2775 frame = ctx->call_ctx;
2776 if(exception_hres != DISP_E_EXCEPTION)
2777 throw_error(ctx, exception_hres, NULL);
2778 set_error_location(ei, frame->bytecode, frame->bytecode->instrs[frame->ip].loc, IDS_RUNTIME_ERROR, NULL);
2780 while(!frame->except_frame) {
2781 DWORD flags;
2783 while(frame->scope != frame->base_scope)
2784 scope_pop(&frame->scope);
2786 stack_popn(ctx, ctx->stack_top-frame->stack_base);
2788 flags = frame->flags;
2789 pop_call_frame(ctx);
2790 if(!(flags & EXEC_RETURN_TO_INTERP))
2791 return DISP_E_EXCEPTION;
2792 frame = ctx->call_ctx;
2795 except_frame = frame->except_frame;
2796 catch_off = except_frame->catch_off;
2798 assert(except_frame->stack_top <= ctx->stack_top);
2799 stack_popn(ctx, ctx->stack_top - except_frame->stack_top);
2801 while(except_frame->scope != frame->scope)
2802 scope_pop(&frame->scope);
2804 frame->ip = catch_off ? catch_off : except_frame->finally_off;
2805 assert(!catch_off || frame->bytecode->instrs[frame->ip].op == OP_enter_catch);
2807 if(ei->valid_value) {
2808 except_val = ctx->ei->value;
2809 ei->valid_value = FALSE;
2810 }else {
2811 jsdisp_t *err;
2812 if(!(err = create_builtin_error(ctx)))
2813 return E_OUTOFMEMORY;
2814 except_val = jsval_obj(err);
2817 /* keep current except_frame if we're entering catch block with finally block associated */
2818 if(catch_off && except_frame->finally_off) {
2819 except_frame->catch_off = 0;
2820 }else {
2821 frame->except_frame = except_frame->next;
2822 heap_free(except_frame);
2825 hres = stack_push(ctx, except_val);
2826 if(FAILED(hres))
2827 return hres;
2829 if(!catch_off)
2830 hres = stack_push(ctx, jsval_bool(FALSE));
2831 return hres;
2834 static HRESULT enter_bytecode(script_ctx_t *ctx, jsval_t *r)
2836 call_frame_t *frame;
2837 jsop_t op;
2838 HRESULT hres = S_OK;
2840 TRACE("\n");
2842 while(1) {
2843 frame = ctx->call_ctx;
2844 op = frame->bytecode->instrs[frame->ip].op;
2845 hres = op_funcs[op](ctx);
2846 if(FAILED(hres)) {
2847 hres = unwind_exception(ctx, hres);
2848 if(FAILED(hres))
2849 return hres;
2850 }else if(frame->ip == -1) {
2851 const DWORD return_to_interp = frame->flags & EXEC_RETURN_TO_INTERP;
2853 assert(ctx->stack_top == frame->stack_base);
2854 assert(frame->scope == frame->base_scope);
2856 if(return_to_interp) {
2857 jsval_release(ctx->acc);
2858 ctx->acc = steal_ret(frame);
2859 }else if(r) {
2860 *r = steal_ret(frame);
2862 pop_call_frame(ctx);
2863 if(!return_to_interp)
2864 break;
2865 }else {
2866 frame->ip += op_move[op];
2870 return S_OK;
2873 static HRESULT bind_event_target(script_ctx_t *ctx, function_code_t *func, jsdisp_t *func_obj)
2875 IBindEventHandler *target;
2876 exprval_t exprval;
2877 IDispatch *disp;
2878 jsval_t v;
2879 HRESULT hres;
2881 hres = identifier_eval(ctx, func->event_target, &exprval);
2882 if(FAILED(hres))
2883 return hres;
2885 hres = exprval_to_value(ctx, &exprval, &v);
2886 if(FAILED(hres))
2887 return hres;
2889 if(!is_object_instance(v)) {
2890 FIXME("Can't bind to %s\n", debugstr_jsval(v));
2891 jsval_release(v);
2894 disp = get_object(v);
2895 hres = IDispatch_QueryInterface(disp, &IID_IBindEventHandler, (void**)&target);
2896 if(SUCCEEDED(hres)) {
2897 hres = IBindEventHandler_BindHandler(target, func->name, (IDispatch*)&func_obj->IDispatchEx_iface);
2898 IBindEventHandler_Release(target);
2899 if(FAILED(hres))
2900 WARN("BindEvent failed: %08x\n", hres);
2901 }else {
2902 FIXME("No IBindEventHandler, not yet supported binding\n");
2905 IDispatch_Release(disp);
2906 return hres;
2909 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)
2911 const unsigned orig_stack = ctx->stack_top;
2912 scope_chain_t *scope;
2913 unsigned i;
2914 jsval_t v;
2915 HRESULT hres;
2917 /* If arguments are already on the stack, we may use them. */
2918 if(argv + argc == ctx->stack + ctx->stack_top) {
2919 frame->arguments_off = argv - ctx->stack;
2920 i = argc;
2921 }else {
2922 frame->arguments_off = ctx->stack_top;
2923 for(i = 0; i < argc; i++) {
2924 hres = jsval_copy(argv[i], &v);
2925 if(SUCCEEDED(hres))
2926 hres = stack_push(ctx, v);
2927 if(FAILED(hres)) {
2928 stack_popn(ctx, i);
2929 return hres;
2934 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2935 for(; i < frame->function->param_cnt; i++) {
2936 hres = stack_push(ctx, jsval_undefined());
2937 if(FAILED(hres)) {
2938 stack_popn(ctx, ctx->stack_top - orig_stack);
2939 return hres;
2943 frame->pop_locals = ctx->stack_top - orig_stack;
2945 frame->variables_off = ctx->stack_top;
2947 for(i = 0; i < frame->function->var_cnt; i++) {
2948 hres = stack_push(ctx, jsval_undefined());
2949 if(FAILED(hres)) {
2950 stack_popn(ctx, ctx->stack_top - orig_stack);
2951 return hres;
2955 frame->pop_variables = i;
2957 hres = scope_push(scope_chain, variable_object, to_disp(variable_object), &scope);
2958 if(FAILED(hres)) {
2959 stack_popn(ctx, ctx->stack_top - orig_stack);
2960 return hres;
2963 for(i = 0; i < frame->function->func_cnt; i++) {
2964 if(frame->function->funcs[i].name && !frame->function->funcs[i].event_target) {
2965 jsdisp_t *func_obj;
2966 unsigned off;
2968 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+i, scope, &func_obj);
2969 if(FAILED(hres)) {
2970 stack_popn(ctx, ctx->stack_top - orig_stack);
2971 scope_release(scope);
2972 return hres;
2975 off = local_off(frame, frame->function->funcs[i].local_ref);
2976 jsval_release(ctx->stack[off]);
2977 ctx->stack[off] = jsval_obj(func_obj);
2981 scope->frame = frame;
2982 frame->base_scope = frame->scope = scope;
2983 return S_OK;
2986 HRESULT exec_source(script_ctx_t *ctx, DWORD flags, bytecode_t *bytecode, function_code_t *function, scope_chain_t *scope,
2987 IDispatch *this_obj, jsdisp_t *function_instance, jsdisp_t *variable_obj, unsigned argc, jsval_t *argv, jsval_t *r)
2989 call_frame_t *frame;
2990 unsigned i;
2991 HRESULT hres;
2993 if(!ctx->ei->enter_notified) {
2994 ctx->ei->enter_notified = TRUE;
2995 IActiveScriptSite_OnEnterScript(ctx->site);
2998 for(i = 0; i < function->func_cnt; i++) {
2999 jsdisp_t *func_obj;
3001 if(!function->funcs[i].event_target)
3002 continue;
3004 hres = create_source_function(ctx, bytecode, function->funcs+i, scope, &func_obj);
3005 if(FAILED(hres))
3006 return hres;
3008 hres = bind_event_target(ctx, function->funcs+i, func_obj);
3009 jsdisp_release(func_obj);
3010 if(FAILED(hres))
3011 return hres;
3014 if(flags & (EXEC_GLOBAL | EXEC_EVAL)) {
3015 for(i=0; i < function->var_cnt; i++) {
3016 TRACE("[%d] %s %d\n", i, debugstr_w(function->variables[i].name), function->variables[i].func_id);
3017 if(function->variables[i].func_id != -1) {
3018 jsdisp_t *func_obj;
3020 hres = create_source_function(ctx, bytecode, function->funcs+function->variables[i].func_id, scope, &func_obj);
3021 if(FAILED(hres))
3022 return hres;
3024 hres = jsdisp_propput_name(variable_obj, function->variables[i].name, jsval_obj(func_obj));
3025 jsdisp_release(func_obj);
3026 }else if(!(flags & EXEC_GLOBAL) || !lookup_global_members(ctx, function->variables[i].name, NULL)) {
3027 DISPID id = 0;
3029 hres = jsdisp_get_id(variable_obj, function->variables[i].name, fdexNameEnsure, &id);
3030 if(FAILED(hres))
3031 return hres;
3036 /* ECMA-262 3rd Edition 11.2.3.7 */
3037 if(this_obj) {
3038 jsdisp_t *jsthis;
3040 jsthis = iface_to_jsdisp(this_obj);
3041 if(jsthis) {
3042 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
3043 this_obj = NULL;
3044 jsdisp_release(jsthis);
3048 if(ctx->call_ctx && (flags & EXEC_EVAL)) {
3049 hres = detach_variable_object(ctx, ctx->call_ctx, FALSE);
3050 if(FAILED(hres))
3051 return hres;
3054 frame = heap_alloc_zero(sizeof(*frame));
3055 if(!frame)
3056 return E_OUTOFMEMORY;
3058 frame->function = function;
3059 frame->ret = jsval_undefined();
3060 frame->argc = argc;
3061 frame->bytecode = bytecode_addref(bytecode);
3063 if(!(flags & (EXEC_GLOBAL|EXEC_EVAL))) {
3064 hres = setup_scope(ctx, frame, scope, variable_obj, argc, argv);
3065 if(FAILED(hres)) {
3066 release_bytecode(frame->bytecode);
3067 heap_free(frame);
3068 return hres;
3070 }else if(scope) {
3071 frame->base_scope = frame->scope = scope_addref(scope);
3074 frame->ip = function->instr_off;
3075 frame->stack_base = ctx->stack_top;
3076 if(this_obj) {
3077 frame->this_obj = this_obj;
3078 IDispatch_AddRef(frame->this_obj);
3081 if(function_instance)
3082 frame->function_instance = jsdisp_addref(function_instance);
3084 frame->flags = flags;
3085 frame->variable_obj = jsdisp_addref(variable_obj);
3087 frame->prev_frame = ctx->call_ctx;
3088 ctx->call_ctx = frame;
3090 if(flags & EXEC_RETURN_TO_INTERP) {
3092 * We're called directly from interpreter, so we may just setup call frame and return.
3093 * Already running interpreter will take care of execution.
3095 if(r)
3096 *r = jsval_undefined();
3097 return S_OK;
3100 return enter_bytecode(ctx, r);