wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / jscript / engine.c
blob018597829edef4dd47f810a82414eb6eb2c99070
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 assert(0);
175 case EXPRVAL_IDREF:
176 hres = stack_push(ctx, jsval_disp(val->u.idref.disp));
177 if(SUCCEEDED(hres))
178 hres = stack_push(ctx, jsval_number(val->u.idref.id));
179 else
180 IDispatch_Release(val->u.idref.disp);
181 return hres;
182 case EXPRVAL_STACK_REF:
183 hres = stack_push(ctx, jsval_number(val->u.off));
184 if(SUCCEEDED(hres))
185 hres = stack_push(ctx, jsval_undefined());
186 return hres;
187 case EXPRVAL_INVALID:
188 hres = stack_push(ctx, jsval_undefined());
189 if(SUCCEEDED(hres))
190 hres = stack_push(ctx, jsval_number(val->u.hres));
191 return hres;
194 assert(0);
195 return E_FAIL;
198 static BOOL stack_topn_exprval(script_ctx_t *ctx, unsigned n, exprval_t *r)
200 jsval_t v = stack_topn(ctx, n+1);
202 switch(jsval_type(v)) {
203 case JSV_NUMBER: {
204 call_frame_t *frame = ctx->call_ctx;
205 unsigned off = get_number(v);
207 if(!frame->base_scope->frame && off >= frame->arguments_off) {
208 DISPID id;
209 BSTR name;
210 HRESULT hres;
212 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
214 assert(off < frame->variables_off + frame->function->var_cnt);
215 name = off >= frame->variables_off
216 ? frame->function->variables[off - frame->variables_off].name
217 : frame->function->params[off - frame->arguments_off];
218 hres = jsdisp_get_id(ctx->call_ctx->base_scope->jsobj, name, 0, &id);
219 if(FAILED(hres)) {
220 r->type = EXPRVAL_INVALID;
221 r->u.hres = hres;
222 return FALSE;
225 *stack_top_ref(ctx, n+1) = jsval_obj(jsdisp_addref(frame->base_scope->jsobj));
226 *stack_top_ref(ctx, n) = jsval_number(id);
227 r->type = EXPRVAL_IDREF;
228 r->u.idref.disp = frame->base_scope->obj;
229 r->u.idref.id = id;
230 return TRUE;
233 r->type = EXPRVAL_STACK_REF;
234 r->u.off = off;
235 return TRUE;
237 case JSV_OBJECT:
238 r->type = EXPRVAL_IDREF;
239 r->u.idref.disp = get_object(v);
240 assert(is_number(stack_topn(ctx, n)));
241 r->u.idref.id = get_number(stack_topn(ctx, n));
242 return TRUE;
243 case JSV_UNDEFINED:
244 r->type = EXPRVAL_INVALID;
245 assert(is_number(stack_topn(ctx, n)));
246 r->u.hres = get_number(stack_topn(ctx, n));
247 return FALSE;
248 default:
249 assert(0);
250 return FALSE;
254 static inline BOOL stack_pop_exprval(script_ctx_t *ctx, exprval_t *r)
256 BOOL ret = stack_topn_exprval(ctx, 0, r);
257 ctx->stack_top -= 2;
258 return ret;
261 static HRESULT exprval_propput(script_ctx_t *ctx, exprval_t *ref, jsval_t v)
263 switch(ref->type) {
264 case EXPRVAL_STACK_REF: {
265 jsval_t *r = ctx->stack + ref->u.off;
266 jsval_release(*r);
267 return jsval_copy(v, r);
269 case EXPRVAL_IDREF:
270 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v);
271 default:
272 assert(0);
273 return E_FAIL;
277 static HRESULT exprval_propget(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
279 switch(ref->type) {
280 case EXPRVAL_STACK_REF:
281 return jsval_copy(ctx->stack[ref->u.off], r);
282 case EXPRVAL_IDREF:
283 return disp_propget(ctx, ref->u.idref.disp, ref->u.idref.id, r);
284 default:
285 assert(0);
286 return E_FAIL;
290 static HRESULT exprval_call(script_ctx_t *ctx, exprval_t *ref, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
292 switch(ref->type) {
293 case EXPRVAL_STACK_REF: {
294 jsval_t v = ctx->stack[ref->u.off];
296 if(!is_object_instance(v)) {
297 FIXME("invoke %s\n", debugstr_jsval(v));
298 return E_FAIL;
301 return disp_call_value(ctx, get_object(v), NULL, flags, argc, argv, r);
303 case EXPRVAL_IDREF:
304 return disp_call(ctx, ref->u.idref.disp, ref->u.idref.id, flags, argc, argv, r);
305 default:
306 assert(0);
307 return E_FAIL;
311 /* ECMA-262 3rd Edition 8.7.1 */
312 /* Steals input reference. */
313 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
315 HRESULT hres;
317 if(ref->type == EXPRVAL_JSVAL) {
318 *r = ref->u.val;
319 return S_OK;
322 hres = exprval_propget(ctx, ref, r);
324 if(ref->type == EXPRVAL_IDREF)
325 IDispatch_Release(ref->u.idref.disp);
326 return hres;
329 static void exprval_release(exprval_t *val)
331 switch(val->type) {
332 case EXPRVAL_JSVAL:
333 jsval_release(val->u.val);
334 return;
335 case EXPRVAL_IDREF:
336 if(val->u.idref.disp)
337 IDispatch_Release(val->u.idref.disp);
338 return;
339 case EXPRVAL_STACK_REF:
340 case EXPRVAL_INVALID:
341 return;
345 static inline void exprval_set_exception(exprval_t *val, HRESULT hres)
347 val->type = EXPRVAL_INVALID;
348 val->u.hres = hres;
351 static inline void exprval_set_disp_ref(exprval_t *ref, IDispatch *obj, DISPID id)
353 ref->type = EXPRVAL_IDREF;
354 IDispatch_AddRef(ref->u.idref.disp = obj);
355 ref->u.idref.id = id;
358 static inline jsval_t steal_ret(call_frame_t *frame)
360 jsval_t r = frame->ret;
361 frame->ret = jsval_undefined();
362 return r;
365 static inline void clear_acc(script_ctx_t *ctx)
367 jsval_release(ctx->acc);
368 ctx->acc = jsval_undefined();
371 static HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
373 scope_chain_t *new_scope;
375 new_scope = heap_alloc(sizeof(scope_chain_t));
376 if(!new_scope)
377 return E_OUTOFMEMORY;
379 new_scope->ref = 1;
381 IDispatch_AddRef(obj);
382 new_scope->jsobj = jsobj;
383 new_scope->obj = obj;
384 new_scope->frame = NULL;
385 new_scope->next = scope ? scope_addref(scope) : NULL;
387 *ret = new_scope;
388 return S_OK;
391 static void scope_pop(scope_chain_t **scope)
393 scope_chain_t *tmp;
395 tmp = *scope;
396 *scope = tmp->next;
397 scope_release(tmp);
400 void scope_release(scope_chain_t *scope)
402 if(--scope->ref)
403 return;
405 if(scope->next)
406 scope_release(scope->next);
408 IDispatch_Release(scope->obj);
409 heap_free(scope);
412 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, const WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
414 IDispatchEx *dispex;
415 jsdisp_t *jsdisp;
416 BSTR bstr;
417 HRESULT hres;
419 jsdisp = iface_to_jsdisp(disp);
420 if(jsdisp) {
421 hres = jsdisp_get_id(jsdisp, name, flags, id);
422 jsdisp_release(jsdisp);
423 return hres;
426 if(name_bstr) {
427 bstr = name_bstr;
428 }else {
429 bstr = SysAllocString(name);
430 if(!bstr)
431 return E_OUTOFMEMORY;
434 *id = 0;
435 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
436 if(SUCCEEDED(hres)) {
437 hres = IDispatchEx_GetDispID(dispex, bstr, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
438 IDispatchEx_Release(dispex);
439 }else {
440 TRACE("using IDispatch\n");
441 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &bstr, 1, 0, id);
444 if(name_bstr != bstr)
445 SysFreeString(bstr);
446 return hres;
449 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
451 IObjectIdentity *identity;
452 IUnknown *unk1, *unk2;
453 HRESULT hres;
455 if(disp1 == disp2) {
456 *ret = TRUE;
457 return S_OK;
460 if(!disp1 || !disp2) {
461 *ret = FALSE;
462 return S_OK;
465 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
466 if(FAILED(hres))
467 return hres;
469 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
470 if(FAILED(hres)) {
471 IUnknown_Release(unk1);
472 return hres;
475 if(unk1 == unk2) {
476 *ret = TRUE;
477 }else {
478 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
479 if(SUCCEEDED(hres)) {
480 hres = IObjectIdentity_IsEqualObject(identity, unk2);
481 IObjectIdentity_Release(identity);
482 *ret = hres == S_OK;
483 }else {
484 *ret = FALSE;
488 IUnknown_Release(unk1);
489 IUnknown_Release(unk2);
490 return S_OK;
493 /* ECMA-262 3rd Edition 11.9.6 */
494 HRESULT jsval_strict_equal(jsval_t lval, jsval_t rval, BOOL *ret)
496 jsval_type_t type = jsval_type(lval);
498 TRACE("\n");
500 if(type != jsval_type(rval)) {
501 if(is_null_instance(lval))
502 *ret = is_null_instance(rval);
503 else
504 *ret = FALSE;
505 return S_OK;
508 switch(type) {
509 case JSV_UNDEFINED:
510 case JSV_NULL:
511 *ret = TRUE;
512 break;
513 case JSV_OBJECT:
514 return disp_cmp(get_object(lval), get_object(rval), ret);
515 case JSV_STRING:
516 *ret = jsstr_eq(get_string(lval), get_string(rval));
517 break;
518 case JSV_NUMBER:
519 *ret = get_number(lval) == get_number(rval);
520 break;
521 case JSV_BOOL:
522 *ret = !get_bool(lval) == !get_bool(rval);
523 break;
524 case JSV_VARIANT:
525 WARN("VARIANT type, returning false\n");
526 *ret = FALSE;
527 return S_OK;
530 return S_OK;
534 * Transfers local variables from stack to variable object.
535 * It's slow, so we want to avoid it as much as possible.
537 static HRESULT detach_variable_object(script_ctx_t *ctx, call_frame_t *frame, BOOL from_release)
539 unsigned i;
540 HRESULT hres;
542 if(!frame->base_scope || !frame->base_scope->frame)
543 return S_OK;
545 TRACE("detaching %p\n", frame);
547 assert(frame == frame->base_scope->frame);
548 assert(frame->variable_obj == frame->base_scope->jsobj);
550 if(!from_release && !frame->arguments_obj) {
551 hres = setup_arguments_object(ctx, frame);
552 if(FAILED(hres))
553 return hres;
556 frame->base_scope->frame = NULL;
558 for(i = 0; i < frame->function->locals_cnt; i++) {
559 hres = jsdisp_propput_name(frame->variable_obj, frame->function->locals[i].name,
560 ctx->stack[local_off(frame, frame->function->locals[i].ref)]);
561 if(FAILED(hres))
562 return hres;
565 return S_OK;
568 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
570 named_item_t *item;
571 DISPID id;
572 HRESULT hres;
574 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
575 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
576 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
577 if(SUCCEEDED(hres)) {
578 if(ret)
579 exprval_set_disp_ref(ret, item->disp, id);
580 return TRUE;
585 return FALSE;
588 IDispatch *lookup_global_host(script_ctx_t *ctx)
590 IDispatch *disp = NULL;
591 named_item_t *item;
593 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
594 if(!(item->flags & SCRIPTITEM_GLOBALMEMBERS)) continue;
595 disp = item->disp;
596 break;
598 if(!disp) disp = to_disp(ctx->global);
600 return disp;
603 static int __cdecl local_ref_cmp(const void *key, const void *ref)
605 return wcscmp((const WCHAR*)key, ((const local_ref_t*)ref)->name);
608 local_ref_t *lookup_local(const function_code_t *function, const WCHAR *identifier)
610 return bsearch(identifier, function->locals, function->locals_cnt, sizeof(*function->locals), local_ref_cmp);
613 /* ECMA-262 3rd Edition 10.1.4 */
614 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
616 scope_chain_t *scope;
617 named_item_t *item;
618 DISPID id = 0;
619 HRESULT hres;
621 TRACE("%s\n", debugstr_w(identifier));
623 if(ctx->call_ctx) {
624 for(scope = ctx->call_ctx->scope; scope; scope = scope->next) {
625 if(scope->frame) {
626 function_code_t *func = scope->frame->function;
627 local_ref_t *ref = lookup_local(func, identifier);
629 if(ref) {
630 ret->type = EXPRVAL_STACK_REF;
631 ret->u.off = local_off(scope->frame, ref->ref);
632 TRACE("returning ref %d for %d\n", ret->u.off, ref->ref);
633 return S_OK;
636 if(!wcscmp(identifier, L"arguments")) {
637 hres = detach_variable_object(ctx, scope->frame, FALSE);
638 if(FAILED(hres))
639 return hres;
642 if(scope->jsobj)
643 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
644 else
645 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
646 if(SUCCEEDED(hres)) {
647 exprval_set_disp_ref(ret, scope->obj, id);
648 return S_OK;
652 item = ctx->call_ctx->bytecode->named_item;
653 if(item) {
654 hres = jsdisp_get_id(item->script_obj, identifier, 0, &id);
655 if(SUCCEEDED(hres)) {
656 exprval_set_disp_ref(ret, to_disp(item->script_obj), id);
657 return S_OK;
659 if(!(item->flags & SCRIPTITEM_CODEONLY)) {
660 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
661 if(SUCCEEDED(hres)) {
662 exprval_set_disp_ref(ret, item->disp, id);
663 return S_OK;
669 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
670 if(SUCCEEDED(hres)) {
671 exprval_set_disp_ref(ret, to_disp(ctx->global), id);
672 return S_OK;
675 item = lookup_named_item(ctx, identifier, SCRIPTITEM_ISVISIBLE);
676 if(item) {
677 IDispatch_AddRef(item->disp);
678 ret->type = EXPRVAL_JSVAL;
679 ret->u.val = jsval_disp(item->disp);
680 return S_OK;
683 if(lookup_global_members(ctx, identifier, ret))
684 return S_OK;
686 exprval_set_exception(ret, JS_E_UNDEFINED_VARIABLE);
687 return S_OK;
690 static inline BSTR get_op_bstr(script_ctx_t *ctx, int i)
692 call_frame_t *frame = ctx->call_ctx;
693 return frame->bytecode->instrs[frame->ip].u.arg[i].bstr;
696 static inline unsigned get_op_uint(script_ctx_t *ctx, int i)
698 call_frame_t *frame = ctx->call_ctx;
699 return frame->bytecode->instrs[frame->ip].u.arg[i].uint;
702 static inline unsigned get_op_int(script_ctx_t *ctx, int i)
704 call_frame_t *frame = ctx->call_ctx;
705 return frame->bytecode->instrs[frame->ip].u.arg[i].lng;
708 static inline jsstr_t *get_op_str(script_ctx_t *ctx, int i)
710 call_frame_t *frame = ctx->call_ctx;
711 return frame->bytecode->instrs[frame->ip].u.arg[i].str;
714 static inline double get_op_double(script_ctx_t *ctx)
716 call_frame_t *frame = ctx->call_ctx;
717 return frame->bytecode->instrs[frame->ip].u.dbl;
720 static inline void jmp_next(script_ctx_t *ctx)
722 ctx->call_ctx->ip++;
725 static inline void jmp_abs(script_ctx_t *ctx, unsigned dst)
727 ctx->call_ctx->ip = dst;
730 /* ECMA-262 3rd Edition 12.6.4 */
731 static HRESULT interp_forin(script_ctx_t *ctx)
733 const HRESULT arg = get_op_uint(ctx, 0);
734 IDispatch *obj = NULL;
735 IDispatchEx *dispex;
736 exprval_t prop_ref;
737 DISPID id;
738 BSTR name = NULL;
739 HRESULT hres;
741 TRACE("\n");
743 assert(is_number(stack_top(ctx)));
744 id = get_number(stack_top(ctx));
746 if(!stack_topn_exprval(ctx, 1, &prop_ref)) {
747 FIXME("invalid ref: %08x\n", prop_ref.u.hres);
748 return E_FAIL;
751 if(is_object_instance(stack_topn(ctx, 3)))
752 obj = get_object(stack_topn(ctx, 3));
754 if(obj) {
755 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
756 if(SUCCEEDED(hres)) {
757 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
758 if(hres == S_OK)
759 hres = IDispatchEx_GetMemberName(dispex, id, &name);
760 IDispatchEx_Release(dispex);
761 if(FAILED(hres))
762 return hres;
763 }else {
764 TRACE("No IDispatchEx\n");
768 if(name) {
769 jsstr_t *str;
771 str = jsstr_alloc_len(name, SysStringLen(name));
772 SysFreeString(name);
773 if(!str)
774 return E_OUTOFMEMORY;
776 stack_pop(ctx);
777 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
779 hres = exprval_propput(ctx, &prop_ref, jsval_string(str));
780 jsstr_release(str);
781 if(FAILED(hres))
782 return hres;
784 jmp_next(ctx);
785 }else {
786 stack_popn(ctx, 4);
787 jmp_abs(ctx, arg);
789 return S_OK;
792 /* ECMA-262 3rd Edition 12.10 */
793 static HRESULT interp_push_scope(script_ctx_t *ctx)
795 IDispatch *disp;
796 jsval_t v;
797 HRESULT hres;
799 TRACE("\n");
801 v = stack_pop(ctx);
802 hres = to_object(ctx, v, &disp);
803 jsval_release(v);
804 if(FAILED(hres))
805 return hres;
807 hres = scope_push(ctx->call_ctx->scope, to_jsdisp(disp), disp, &ctx->call_ctx->scope);
808 IDispatch_Release(disp);
809 return hres;
812 /* ECMA-262 3rd Edition 12.10 */
813 static HRESULT interp_pop_scope(script_ctx_t *ctx)
815 TRACE("\n");
817 scope_pop(&ctx->call_ctx->scope);
818 return S_OK;
821 /* ECMA-262 3rd Edition 12.13 */
822 static HRESULT interp_case(script_ctx_t *ctx)
824 const unsigned arg = get_op_uint(ctx, 0);
825 jsval_t v;
826 BOOL b;
827 HRESULT hres;
829 TRACE("\n");
831 v = stack_pop(ctx);
832 hres = jsval_strict_equal(stack_top(ctx), v, &b);
833 jsval_release(v);
834 if(FAILED(hres))
835 return hres;
837 if(b) {
838 stack_popn(ctx, 1);
839 jmp_abs(ctx, arg);
840 }else {
841 jmp_next(ctx);
843 return S_OK;
846 static void set_error_value(script_ctx_t *ctx, jsval_t value)
848 jsexcept_t *ei = ctx->ei;
849 jsdisp_t *obj;
851 reset_ei(ei);
852 ei->error = JS_E_EXCEPTION_THROWN;
853 ei->valid_value = TRUE;
854 ei->value = value;
856 if(is_object_instance(value) && get_object(value) && (obj = to_jsdisp(get_object(value)))) {
857 UINT32 number;
858 jsstr_t *str;
859 jsval_t v;
860 HRESULT hres;
862 /* FIXME: We should check if object is an error instance */
864 hres = jsdisp_propget_name(obj, L"number", &v);
865 if(SUCCEEDED(hres)) {
866 hres = to_uint32(ctx, v, &number);
867 if(SUCCEEDED(hres))
868 ei->error = FAILED(number) ? number : E_FAIL;
869 jsval_release(v);
872 hres = jsdisp_propget_name(obj, L"description", &v);
873 if(SUCCEEDED(hres)) {
874 hres = to_string(ctx, v, &str);
875 if(SUCCEEDED(hres))
876 ei->message = str;
877 jsval_release(v);
882 /* ECMA-262 3rd Edition 12.13 */
883 static HRESULT interp_throw(script_ctx_t *ctx)
885 TRACE("\n");
887 set_error_value(ctx, stack_pop(ctx));
888 return DISP_E_EXCEPTION;
891 static HRESULT interp_throw_ref(script_ctx_t *ctx)
893 const HRESULT arg = get_op_uint(ctx, 0);
895 TRACE("%08x\n", arg);
897 return arg;
900 static HRESULT interp_throw_type(script_ctx_t *ctx)
902 const HRESULT hres = get_op_uint(ctx, 0);
903 jsstr_t *str = get_op_str(ctx, 1);
904 const WCHAR *ptr;
906 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
908 ptr = jsstr_flatten(str);
909 return ptr ? throw_error(ctx, hres, ptr) : E_OUTOFMEMORY;
912 /* ECMA-262 3rd Edition 12.14 */
913 static HRESULT interp_push_except(script_ctx_t *ctx)
915 const unsigned catch_off = get_op_uint(ctx, 0);
916 const unsigned finally_off = get_op_uint(ctx, 1);
917 call_frame_t *frame = ctx->call_ctx;
918 except_frame_t *except;
920 TRACE("\n");
922 except = heap_alloc(sizeof(*except));
923 if(!except)
924 return E_OUTOFMEMORY;
926 except->stack_top = ctx->stack_top;
927 except->scope = frame->scope;
928 except->catch_off = catch_off;
929 except->finally_off = finally_off;
930 except->next = frame->except_frame;
931 frame->except_frame = except;
932 return S_OK;
935 /* ECMA-262 3rd Edition 12.14 */
936 static HRESULT interp_pop_except(script_ctx_t *ctx)
938 const unsigned ret_off = get_op_uint(ctx, 0);
939 call_frame_t *frame = ctx->call_ctx;
940 except_frame_t *except;
941 unsigned finally_off;
943 TRACE("%u\n", ret_off);
945 except = frame->except_frame;
946 assert(except != NULL);
948 finally_off = except->finally_off;
949 frame->except_frame = except->next;
950 heap_free(except);
952 if(finally_off) {
953 HRESULT hres;
955 hres = stack_push(ctx, jsval_number(ret_off));
956 if(FAILED(hres))
957 return hres;
958 hres = stack_push(ctx, jsval_bool(TRUE));
959 if(FAILED(hres))
960 return hres;
961 frame->ip = finally_off;
962 }else {
963 frame->ip = ret_off;
966 return S_OK;
969 /* ECMA-262 3rd Edition 12.14 */
970 static HRESULT interp_end_finally(script_ctx_t *ctx)
972 call_frame_t *frame = ctx->call_ctx;
973 jsval_t v;
975 TRACE("\n");
977 v = stack_pop(ctx);
978 assert(is_bool(v));
980 if(!get_bool(v)) {
981 TRACE("passing exception\n");
983 set_error_value(ctx, stack_pop(ctx));
984 return DISP_E_EXCEPTION;
987 v = stack_pop(ctx);
988 assert(is_number(v));
989 frame->ip = get_number(v);
990 return S_OK;
993 static HRESULT interp_enter_catch(script_ctx_t *ctx)
995 const BSTR ident = get_op_bstr(ctx, 0);
996 jsdisp_t *scope_obj;
997 jsval_t v;
998 HRESULT hres;
1000 hres = create_dispex(ctx, NULL, NULL, &scope_obj);
1001 if(FAILED(hres))
1002 return hres;
1004 v = stack_pop(ctx);
1005 hres = jsdisp_propput_name(scope_obj, ident, v);
1006 jsval_release(v);
1007 if(SUCCEEDED(hres))
1008 hres = scope_push(ctx->call_ctx->scope, scope_obj, to_disp(scope_obj), &ctx->call_ctx->scope);
1009 jsdisp_release(scope_obj);
1010 return hres;
1013 /* ECMA-262 3rd Edition 13 */
1014 static HRESULT interp_func(script_ctx_t *ctx)
1016 unsigned func_idx = get_op_uint(ctx, 0);
1017 call_frame_t *frame = ctx->call_ctx;
1018 jsdisp_t *dispex;
1019 HRESULT hres;
1021 TRACE("%d\n", func_idx);
1023 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+func_idx,
1024 frame->scope, &dispex);
1025 if(FAILED(hres))
1026 return hres;
1028 return stack_push(ctx, jsval_obj(dispex));
1031 /* ECMA-262 3rd Edition 11.2.1 */
1032 static HRESULT interp_array(script_ctx_t *ctx)
1034 jsstr_t *name_str;
1035 const WCHAR *name;
1036 jsval_t v, namev;
1037 IDispatch *obj;
1038 DISPID id;
1039 HRESULT hres;
1041 TRACE("\n");
1043 namev = stack_pop(ctx);
1045 hres = stack_pop_object(ctx, &obj);
1046 if(FAILED(hres)) {
1047 jsval_release(namev);
1048 return hres;
1051 hres = to_flat_string(ctx, namev, &name_str, &name);
1052 jsval_release(namev);
1053 if(FAILED(hres)) {
1054 IDispatch_Release(obj);
1055 return hres;
1058 hres = disp_get_id(ctx, obj, name, NULL, 0, &id);
1059 jsstr_release(name_str);
1060 if(SUCCEEDED(hres)) {
1061 hres = disp_propget(ctx, obj, id, &v);
1062 }else if(hres == DISP_E_UNKNOWNNAME) {
1063 v = jsval_undefined();
1064 hres = S_OK;
1066 IDispatch_Release(obj);
1067 if(FAILED(hres))
1068 return hres;
1070 return stack_push(ctx, v);
1073 /* ECMA-262 3rd Edition 11.2.1 */
1074 static HRESULT interp_member(script_ctx_t *ctx)
1076 const BSTR arg = get_op_bstr(ctx, 0);
1077 IDispatch *obj;
1078 jsval_t v;
1079 DISPID id;
1080 HRESULT hres;
1082 TRACE("\n");
1084 hres = stack_pop_object(ctx, &obj);
1085 if(FAILED(hres))
1086 return hres;
1088 hres = disp_get_id(ctx, obj, arg, arg, 0, &id);
1089 if(SUCCEEDED(hres)) {
1090 hres = disp_propget(ctx, obj, id, &v);
1091 }else if(hres == DISP_E_UNKNOWNNAME) {
1092 v = jsval_undefined();
1093 hres = S_OK;
1095 IDispatch_Release(obj);
1096 if(FAILED(hres))
1097 return hres;
1099 return stack_push(ctx, v);
1102 /* ECMA-262 3rd Edition 11.2.1 */
1103 static HRESULT interp_memberid(script_ctx_t *ctx)
1105 const unsigned arg = get_op_uint(ctx, 0);
1106 jsval_t objv, namev;
1107 const WCHAR *name;
1108 jsstr_t *name_str;
1109 IDispatch *obj;
1110 exprval_t ref;
1111 DISPID id;
1112 HRESULT hres;
1114 TRACE("%x\n", arg);
1116 namev = stack_pop(ctx);
1117 objv = stack_pop(ctx);
1119 hres = to_object(ctx, objv, &obj);
1120 jsval_release(objv);
1121 if(SUCCEEDED(hres)) {
1122 hres = to_flat_string(ctx, namev, &name_str, &name);
1123 if(FAILED(hres))
1124 IDispatch_Release(obj);
1126 jsval_release(namev);
1127 if(FAILED(hres))
1128 return hres;
1130 hres = disp_get_id(ctx, obj, name, NULL, arg, &id);
1131 jsstr_release(name_str);
1132 if(SUCCEEDED(hres)) {
1133 ref.type = EXPRVAL_IDREF;
1134 ref.u.idref.disp = obj;
1135 ref.u.idref.id = id;
1136 }else {
1137 IDispatch_Release(obj);
1138 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1139 exprval_set_exception(&ref, JS_E_INVALID_PROPERTY);
1140 hres = S_OK;
1141 }else {
1142 ERR("failed %08x\n", hres);
1143 return hres;
1147 return stack_push_exprval(ctx, &ref);
1150 /* ECMA-262 3rd Edition 11.2.1 */
1151 static HRESULT interp_refval(script_ctx_t *ctx)
1153 exprval_t ref;
1154 jsval_t v;
1155 HRESULT hres;
1157 TRACE("\n");
1159 if(!stack_topn_exprval(ctx, 0, &ref))
1160 return JS_E_ILLEGAL_ASSIGN;
1162 hres = exprval_propget(ctx, &ref, &v);
1163 if(FAILED(hres))
1164 return hres;
1166 return stack_push(ctx, v);
1169 /* ECMA-262 3rd Edition 11.2.2 */
1170 static HRESULT interp_new(script_ctx_t *ctx)
1172 const unsigned argc = get_op_uint(ctx, 0);
1173 jsval_t constr;
1175 TRACE("%d\n", argc);
1177 constr = stack_topn(ctx, argc);
1179 /* NOTE: Should use to_object here */
1181 if(is_null(constr))
1182 return JS_E_OBJECT_EXPECTED;
1183 else if(!is_object_instance(constr))
1184 return JS_E_INVALID_ACTION;
1185 else if(!get_object(constr))
1186 return JS_E_INVALID_PROPERTY;
1188 clear_acc(ctx);
1189 return disp_call_value(ctx, get_object(constr), NULL, DISPATCH_CONSTRUCT | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1190 argc, stack_args(ctx, argc), &ctx->acc);
1193 /* ECMA-262 3rd Edition 11.2.3 */
1194 static HRESULT interp_call(script_ctx_t *ctx)
1196 const unsigned argn = get_op_uint(ctx, 0);
1197 const int do_ret = get_op_int(ctx, 1);
1198 jsval_t obj;
1200 TRACE("%d %d\n", argn, do_ret);
1202 obj = stack_topn(ctx, argn);
1203 if(!is_object_instance(obj))
1204 return JS_E_INVALID_PROPERTY;
1206 clear_acc(ctx);
1207 return disp_call_value(ctx, get_object(obj), NULL, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1208 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1211 /* ECMA-262 3rd Edition 11.2.3 */
1212 static HRESULT interp_call_member(script_ctx_t *ctx)
1214 const unsigned argn = get_op_uint(ctx, 0);
1215 const int do_ret = get_op_int(ctx, 1);
1216 exprval_t ref;
1218 TRACE("%d %d\n", argn, do_ret);
1220 if(!stack_topn_exprval(ctx, argn, &ref))
1221 return ref.u.hres;
1223 clear_acc(ctx);
1224 return exprval_call(ctx, &ref, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1225 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1228 /* ECMA-262 3rd Edition 11.1.1 */
1229 static HRESULT interp_this(script_ctx_t *ctx)
1231 IDispatch *this_obj = ctx->call_ctx->this_obj;
1233 TRACE("\n");
1235 if(!this_obj) {
1236 named_item_t *item = ctx->call_ctx->bytecode->named_item;
1238 if(item)
1239 this_obj = (item->flags & SCRIPTITEM_CODEONLY) ? to_disp(item->script_obj) : item->disp;
1240 else
1241 this_obj = lookup_global_host(ctx);
1244 IDispatch_AddRef(this_obj);
1245 return stack_push(ctx, jsval_disp(this_obj));
1248 static HRESULT interp_identifier_ref(script_ctx_t *ctx, BSTR identifier, unsigned flags)
1250 exprval_t exprval;
1251 HRESULT hres;
1253 hres = identifier_eval(ctx, identifier, &exprval);
1254 if(FAILED(hres))
1255 return hres;
1257 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1258 jsdisp_t *script_obj = ctx->global;
1259 DISPID id;
1261 if(ctx->call_ctx->bytecode->named_item)
1262 script_obj = ctx->call_ctx->bytecode->named_item->script_obj;
1264 hres = jsdisp_get_id(script_obj, identifier, fdexNameEnsure, &id);
1265 if(FAILED(hres))
1266 return hres;
1268 exprval_set_disp_ref(&exprval, to_disp(script_obj), id);
1271 if(exprval.type == EXPRVAL_JSVAL || exprval.type == EXPRVAL_INVALID) {
1272 WARN("invalid ref\n");
1273 exprval_release(&exprval);
1274 exprval_set_exception(&exprval, JS_E_OBJECT_EXPECTED);
1277 return stack_push_exprval(ctx, &exprval);
1280 static HRESULT identifier_value(script_ctx_t *ctx, BSTR identifier)
1282 exprval_t exprval;
1283 jsval_t v;
1284 HRESULT hres;
1286 hres = identifier_eval(ctx, identifier, &exprval);
1287 if(FAILED(hres))
1288 return hres;
1290 if(exprval.type == EXPRVAL_INVALID)
1291 return throw_error(ctx, exprval.u.hres, identifier);
1293 hres = exprval_to_value(ctx, &exprval, &v);
1294 if(FAILED(hres))
1295 return hres;
1297 return stack_push(ctx, v);
1300 static HRESULT interp_local_ref(script_ctx_t *ctx)
1302 const int arg = get_op_int(ctx, 0);
1303 const unsigned flags = get_op_uint(ctx, 1);
1304 call_frame_t *frame = ctx->call_ctx;
1305 exprval_t ref;
1307 TRACE("%s\n", debugstr_w(local_name(frame, arg)));
1309 if(!frame->base_scope || !frame->base_scope->frame)
1310 return interp_identifier_ref(ctx, local_name(frame, arg), flags);
1312 ref.type = EXPRVAL_STACK_REF;
1313 ref.u.off = local_off(frame, arg);
1314 return stack_push_exprval(ctx, &ref);
1317 static HRESULT interp_local(script_ctx_t *ctx)
1319 const int arg = get_op_int(ctx, 0);
1320 call_frame_t *frame = ctx->call_ctx;
1321 jsval_t copy;
1322 HRESULT hres;
1324 if(!frame->base_scope || !frame->base_scope->frame) {
1325 TRACE("%s\n", debugstr_w(local_name(frame, arg)));
1326 return identifier_value(ctx, local_name(frame, arg));
1329 hres = jsval_copy(ctx->stack[local_off(frame, arg)], &copy);
1330 if(FAILED(hres))
1331 return hres;
1333 TRACE("%s: %s\n", debugstr_w(local_name(frame, arg)), debugstr_jsval(copy));
1334 return stack_push(ctx, copy);
1337 /* ECMA-262 3rd Edition 10.1.4 */
1338 static HRESULT interp_ident(script_ctx_t *ctx)
1340 const BSTR arg = get_op_bstr(ctx, 0);
1342 TRACE("%s\n", debugstr_w(arg));
1344 return identifier_value(ctx, arg);
1347 /* ECMA-262 3rd Edition 10.1.4 */
1348 static HRESULT interp_identid(script_ctx_t *ctx)
1350 const BSTR arg = get_op_bstr(ctx, 0);
1351 const unsigned flags = get_op_uint(ctx, 1);
1353 TRACE("%s %x\n", debugstr_w(arg), flags);
1355 return interp_identifier_ref(ctx, arg, flags);
1358 /* ECMA-262 3rd Edition 7.8.1 */
1359 static HRESULT interp_null(script_ctx_t *ctx)
1361 TRACE("\n");
1363 return stack_push(ctx, jsval_null());
1366 /* ECMA-262 3rd Edition 7.8.2 */
1367 static HRESULT interp_bool(script_ctx_t *ctx)
1369 const int arg = get_op_int(ctx, 0);
1371 TRACE("%s\n", arg ? "true" : "false");
1373 return stack_push(ctx, jsval_bool(arg));
1376 /* ECMA-262 3rd Edition 7.8.3 */
1377 static HRESULT interp_int(script_ctx_t *ctx)
1379 const int arg = get_op_int(ctx, 0);
1381 TRACE("%d\n", arg);
1383 return stack_push(ctx, jsval_number(arg));
1386 /* ECMA-262 3rd Edition 7.8.3 */
1387 static HRESULT interp_double(script_ctx_t *ctx)
1389 const double arg = get_op_double(ctx);
1391 TRACE("%lf\n", arg);
1393 return stack_push(ctx, jsval_number(arg));
1396 /* ECMA-262 3rd Edition 7.8.4 */
1397 static HRESULT interp_str(script_ctx_t *ctx)
1399 jsstr_t *str = get_op_str(ctx, 0);
1401 TRACE("%s\n", debugstr_jsstr(str));
1403 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1406 /* ECMA-262 3rd Edition 7.8 */
1407 static HRESULT interp_regexp(script_ctx_t *ctx)
1409 jsstr_t *source = get_op_str(ctx, 0);
1410 const unsigned flags = get_op_uint(ctx, 1);
1411 jsdisp_t *regexp;
1412 HRESULT hres;
1414 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1416 hres = create_regexp(ctx, source, flags, &regexp);
1417 if(FAILED(hres))
1418 return hres;
1420 return stack_push(ctx, jsval_obj(regexp));
1423 /* ECMA-262 3rd Edition 11.1.4 */
1424 static HRESULT interp_carray(script_ctx_t *ctx)
1426 const unsigned arg = get_op_uint(ctx, 0);
1427 jsdisp_t *array;
1428 HRESULT hres;
1430 TRACE("%u\n", arg);
1432 hres = create_array(ctx, arg, &array);
1433 if(FAILED(hres))
1434 return hres;
1436 return stack_push(ctx, jsval_obj(array));
1439 static HRESULT interp_carray_set(script_ctx_t *ctx)
1441 const unsigned index = get_op_uint(ctx, 0);
1442 jsval_t value, array;
1443 HRESULT hres;
1445 value = stack_pop(ctx);
1447 TRACE("[%u] = %s\n", index, debugstr_jsval(value));
1449 array = stack_top(ctx);
1450 assert(is_object_instance(array));
1452 hres = jsdisp_propput_idx(iface_to_jsdisp(get_object(array)), index, value);
1453 jsval_release(value);
1454 return hres;
1457 /* ECMA-262 3rd Edition 11.1.5 */
1458 static HRESULT interp_new_obj(script_ctx_t *ctx)
1460 jsdisp_t *obj;
1461 HRESULT hres;
1463 TRACE("\n");
1465 hres = create_object(ctx, NULL, &obj);
1466 if(FAILED(hres))
1467 return hres;
1469 return stack_push(ctx, jsval_obj(obj));
1472 /* ECMA-262 3rd Edition 11.1.5 */
1473 static HRESULT interp_obj_prop(script_ctx_t *ctx)
1475 jsstr_t *name_arg = get_op_str(ctx, 0);
1476 unsigned type = get_op_uint(ctx, 1);
1477 const WCHAR *name;
1478 jsdisp_t *obj;
1479 jsval_t val;
1480 HRESULT hres;
1482 TRACE("%s\n", debugstr_jsstr(name_arg));
1484 val = stack_pop(ctx);
1486 /* FIXME: we should pass it as jsstr_t */
1487 name = jsstr_flatten(name_arg);
1489 assert(is_object_instance(stack_top(ctx)));
1490 obj = as_jsdisp(get_object(stack_top(ctx)));
1492 if(type == PROPERTY_DEFINITION_VALUE) {
1493 hres = jsdisp_propput_name(obj, name, val);
1494 }else {
1495 property_desc_t desc = {PROPF_ENUMERABLE | PROPF_CONFIGURABLE};
1496 jsdisp_t *func;
1498 assert(is_object_instance(val));
1499 func = iface_to_jsdisp(get_object(val));
1501 desc.mask = desc.flags;
1502 if(type == PROPERTY_DEFINITION_GETTER) {
1503 desc.explicit_getter = TRUE;
1504 desc.getter = func;
1505 }else {
1506 desc.explicit_setter = TRUE;
1507 desc.setter = func;
1510 hres = jsdisp_define_property(obj, name, &desc);
1511 jsdisp_release(func);
1514 jsval_release(val);
1515 return hres;
1518 /* ECMA-262 3rd Edition 11.11 */
1519 static HRESULT interp_cnd_nz(script_ctx_t *ctx)
1521 const unsigned arg = get_op_uint(ctx, 0);
1522 BOOL b;
1523 HRESULT hres;
1525 TRACE("\n");
1527 hres = to_boolean(stack_top(ctx), &b);
1528 if(FAILED(hres))
1529 return hres;
1531 if(b) {
1532 jmp_abs(ctx, arg);
1533 }else {
1534 stack_popn(ctx, 1);
1535 jmp_next(ctx);
1537 return S_OK;
1540 /* ECMA-262 3rd Edition 11.11 */
1541 static HRESULT interp_cnd_z(script_ctx_t *ctx)
1543 const unsigned arg = get_op_uint(ctx, 0);
1544 BOOL b;
1545 HRESULT hres;
1547 TRACE("\n");
1549 hres = to_boolean(stack_top(ctx), &b);
1550 if(FAILED(hres))
1551 return hres;
1553 if(b) {
1554 stack_popn(ctx, 1);
1555 jmp_next(ctx);
1556 }else {
1557 jmp_abs(ctx, arg);
1559 return S_OK;
1562 /* ECMA-262 3rd Edition 11.10 */
1563 static HRESULT interp_or(script_ctx_t *ctx)
1565 INT l, r;
1566 HRESULT hres;
1568 TRACE("\n");
1570 hres = stack_pop_int(ctx, &r);
1571 if(FAILED(hres))
1572 return hres;
1574 hres = stack_pop_int(ctx, &l);
1575 if(FAILED(hres))
1576 return hres;
1578 return stack_push(ctx, jsval_number(l|r));
1581 /* ECMA-262 3rd Edition 11.10 */
1582 static HRESULT interp_xor(script_ctx_t *ctx)
1584 INT l, r;
1585 HRESULT hres;
1587 TRACE("\n");
1589 hres = stack_pop_int(ctx, &r);
1590 if(FAILED(hres))
1591 return hres;
1593 hres = stack_pop_int(ctx, &l);
1594 if(FAILED(hres))
1595 return hres;
1597 return stack_push(ctx, jsval_number(l^r));
1600 /* ECMA-262 3rd Edition 11.10 */
1601 static HRESULT interp_and(script_ctx_t *ctx)
1603 INT l, r;
1604 HRESULT hres;
1606 TRACE("\n");
1608 hres = stack_pop_int(ctx, &r);
1609 if(FAILED(hres))
1610 return hres;
1612 hres = stack_pop_int(ctx, &l);
1613 if(FAILED(hres))
1614 return hres;
1616 return stack_push(ctx, jsval_number(l&r));
1619 /* ECMA-262 3rd Edition 11.8.6 */
1620 static HRESULT interp_instanceof(script_ctx_t *ctx)
1622 jsdisp_t *obj, *iter, *tmp = NULL;
1623 jsval_t prot, v;
1624 BOOL ret = FALSE;
1625 HRESULT hres;
1627 v = stack_pop(ctx);
1628 if(!is_object_instance(v) || !get_object(v)) {
1629 jsval_release(v);
1630 return JS_E_FUNCTION_EXPECTED;
1633 obj = iface_to_jsdisp(get_object(v));
1634 IDispatch_Release(get_object(v));
1635 if(!obj) {
1636 FIXME("non-jsdisp objects not supported\n");
1637 return E_FAIL;
1640 if(is_class(obj, JSCLASS_FUNCTION)) {
1641 hres = jsdisp_propget_name(obj, L"prototype", &prot);
1642 }else {
1643 hres = JS_E_FUNCTION_EXPECTED;
1645 jsdisp_release(obj);
1646 if(FAILED(hres))
1647 return hres;
1649 v = stack_pop(ctx);
1651 if(is_object_instance(prot)) {
1652 if(is_object_instance(v))
1653 tmp = iface_to_jsdisp(get_object(v));
1654 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1655 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1656 if(FAILED(hres))
1657 break;
1660 if(tmp)
1661 jsdisp_release(tmp);
1662 }else {
1663 FIXME("prototype is not an object\n");
1664 hres = E_FAIL;
1667 jsval_release(prot);
1668 jsval_release(v);
1669 if(FAILED(hres))
1670 return hres;
1672 return stack_push(ctx, jsval_bool(ret));
1675 /* ECMA-262 3rd Edition 11.8.7 */
1676 static HRESULT interp_in(script_ctx_t *ctx)
1678 const WCHAR *str;
1679 jsstr_t *jsstr;
1680 jsval_t obj, v;
1681 DISPID id = 0;
1682 BOOL ret;
1683 HRESULT hres;
1685 TRACE("\n");
1687 obj = stack_pop(ctx);
1688 if(!is_object_instance(obj) || !get_object(obj)) {
1689 jsval_release(obj);
1690 return JS_E_OBJECT_EXPECTED;
1693 v = stack_pop(ctx);
1694 hres = to_flat_string(ctx, v, &jsstr, &str);
1695 jsval_release(v);
1696 if(FAILED(hres)) {
1697 IDispatch_Release(get_object(obj));
1698 return hres;
1701 hres = disp_get_id(ctx, get_object(obj), str, NULL, 0, &id);
1702 IDispatch_Release(get_object(obj));
1703 jsstr_release(jsstr);
1704 if(SUCCEEDED(hres))
1705 ret = TRUE;
1706 else if(hres == DISP_E_UNKNOWNNAME)
1707 ret = FALSE;
1708 else
1709 return hres;
1711 return stack_push(ctx, jsval_bool(ret));
1714 /* ECMA-262 3rd Edition 11.6.1 */
1715 static HRESULT interp_add(script_ctx_t *ctx)
1717 jsval_t l, r, lval, rval, ret;
1718 HRESULT hres;
1720 rval = stack_pop(ctx);
1721 lval = stack_pop(ctx);
1723 TRACE("%s + %s\n", debugstr_jsval(lval), debugstr_jsval(rval));
1725 hres = to_primitive(ctx, lval, &l, NO_HINT);
1726 if(SUCCEEDED(hres)) {
1727 hres = to_primitive(ctx, rval, &r, NO_HINT);
1728 if(FAILED(hres))
1729 jsval_release(l);
1731 jsval_release(lval);
1732 jsval_release(rval);
1733 if(FAILED(hres))
1734 return hres;
1736 if(is_string(l) || is_string(r)) {
1737 jsstr_t *lstr, *rstr = NULL;
1739 hres = to_string(ctx, l, &lstr);
1740 if(SUCCEEDED(hres))
1741 hres = to_string(ctx, r, &rstr);
1743 if(SUCCEEDED(hres)) {
1744 jsstr_t *ret_str;
1746 ret_str = jsstr_concat(lstr, rstr);
1747 if(ret_str)
1748 ret = jsval_string(ret_str);
1749 else
1750 hres = E_OUTOFMEMORY;
1753 jsstr_release(lstr);
1754 if(rstr)
1755 jsstr_release(rstr);
1756 }else {
1757 double nl, nr;
1759 hres = to_number(ctx, l, &nl);
1760 if(SUCCEEDED(hres)) {
1761 hres = to_number(ctx, r, &nr);
1762 if(SUCCEEDED(hres))
1763 ret = jsval_number(nl+nr);
1767 jsval_release(r);
1768 jsval_release(l);
1769 if(FAILED(hres))
1770 return hres;
1772 return stack_push(ctx, ret);
1775 /* ECMA-262 3rd Edition 11.6.2 */
1776 static HRESULT interp_sub(script_ctx_t *ctx)
1778 double l, r;
1779 HRESULT hres;
1781 TRACE("\n");
1783 hres = stack_pop_number(ctx, &r);
1784 if(FAILED(hres))
1785 return hres;
1787 hres = stack_pop_number(ctx, &l);
1788 if(FAILED(hres))
1789 return hres;
1791 return stack_push(ctx, jsval_number(l-r));
1794 /* ECMA-262 3rd Edition 11.5.1 */
1795 static HRESULT interp_mul(script_ctx_t *ctx)
1797 double l, r;
1798 HRESULT hres;
1800 TRACE("\n");
1802 hres = stack_pop_number(ctx, &r);
1803 if(FAILED(hres))
1804 return hres;
1806 hres = stack_pop_number(ctx, &l);
1807 if(FAILED(hres))
1808 return hres;
1810 return stack_push(ctx, jsval_number(l*r));
1813 /* ECMA-262 3rd Edition 11.5.2 */
1814 static HRESULT interp_div(script_ctx_t *ctx)
1816 double l, r;
1817 HRESULT hres;
1819 TRACE("\n");
1821 hres = stack_pop_number(ctx, &r);
1822 if(FAILED(hres))
1823 return hres;
1825 hres = stack_pop_number(ctx, &l);
1826 if(FAILED(hres))
1827 return hres;
1829 return stack_push(ctx, jsval_number(l/r));
1832 /* ECMA-262 3rd Edition 11.5.3 */
1833 static HRESULT interp_mod(script_ctx_t *ctx)
1835 double l, r;
1836 HRESULT hres;
1838 TRACE("\n");
1840 hres = stack_pop_number(ctx, &r);
1841 if(FAILED(hres))
1842 return hres;
1844 hres = stack_pop_number(ctx, &l);
1845 if(FAILED(hres))
1846 return hres;
1848 return stack_push(ctx, jsval_number(fmod(l, r)));
1851 /* ECMA-262 3rd Edition 11.4.2 */
1852 static HRESULT interp_delete(script_ctx_t *ctx)
1854 jsval_t objv, namev;
1855 IDispatch *obj;
1856 jsstr_t *name;
1857 BOOL ret;
1858 HRESULT hres;
1860 TRACE("\n");
1862 namev = stack_pop(ctx);
1863 objv = stack_pop(ctx);
1865 hres = to_object(ctx, objv, &obj);
1866 jsval_release(objv);
1867 if(FAILED(hres)) {
1868 jsval_release(namev);
1869 return hres;
1872 hres = to_string(ctx, namev, &name);
1873 jsval_release(namev);
1874 if(FAILED(hres)) {
1875 IDispatch_Release(obj);
1876 return hres;
1879 hres = disp_delete_name(ctx, obj, name, &ret);
1880 IDispatch_Release(obj);
1881 jsstr_release(name);
1882 if(FAILED(hres))
1883 return hres;
1885 return stack_push(ctx, jsval_bool(ret));
1888 /* ECMA-262 3rd Edition 11.4.2 */
1889 static HRESULT interp_delete_ident(script_ctx_t *ctx)
1891 const BSTR arg = get_op_bstr(ctx, 0);
1892 exprval_t exprval;
1893 BOOL ret;
1894 HRESULT hres;
1896 TRACE("%s\n", debugstr_w(arg));
1898 hres = identifier_eval(ctx, arg, &exprval);
1899 if(FAILED(hres))
1900 return hres;
1902 switch(exprval.type) {
1903 case EXPRVAL_STACK_REF:
1904 ret = FALSE;
1905 break;
1906 case EXPRVAL_IDREF:
1907 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1908 IDispatch_Release(exprval.u.idref.disp);
1909 if(FAILED(hres))
1910 return hres;
1911 break;
1912 case EXPRVAL_INVALID:
1913 ret = TRUE;
1914 break;
1915 default:
1916 FIXME("Unsupported exprval\n");
1917 exprval_release(&exprval);
1918 return E_NOTIMPL;
1922 return stack_push(ctx, jsval_bool(ret));
1925 /* ECMA-262 3rd Edition 11.4.2 */
1926 static HRESULT interp_void(script_ctx_t *ctx)
1928 TRACE("\n");
1930 stack_popn(ctx, 1);
1931 return stack_push(ctx, jsval_undefined());
1934 /* ECMA-262 3rd Edition 11.4.3 */
1935 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1937 switch(jsval_type(v)) {
1938 case JSV_UNDEFINED:
1939 *ret = L"undefined";
1940 break;
1941 case JSV_NULL:
1942 *ret = L"object";
1943 break;
1944 case JSV_OBJECT: {
1945 jsdisp_t *dispex;
1947 if(get_object(v) && (dispex = iface_to_jsdisp(get_object(v)))) {
1948 *ret = is_class(dispex, JSCLASS_FUNCTION) ? L"function" : L"object";
1949 jsdisp_release(dispex);
1950 }else {
1951 *ret = L"object";
1953 break;
1955 case JSV_STRING:
1956 *ret = L"string";
1957 break;
1958 case JSV_NUMBER:
1959 *ret = L"number";
1960 break;
1961 case JSV_BOOL:
1962 *ret = L"boolean";
1963 break;
1964 case JSV_VARIANT:
1965 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1966 return E_NOTIMPL;
1969 return S_OK;
1972 /* ECMA-262 3rd Edition 11.4.3 */
1973 static HRESULT interp_typeofid(script_ctx_t *ctx)
1975 const WCHAR *ret;
1976 exprval_t ref;
1977 jsval_t v;
1978 HRESULT hres;
1980 TRACE("\n");
1982 if(!stack_pop_exprval(ctx, &ref))
1983 return stack_push(ctx, jsval_string(jsstr_undefined()));
1985 hres = exprval_propget(ctx, &ref, &v);
1986 exprval_release(&ref);
1987 if(FAILED(hres))
1988 return stack_push_string(ctx, L"unknown");
1990 hres = typeof_string(v, &ret);
1991 jsval_release(v);
1992 if(FAILED(hres))
1993 return hres;
1995 return stack_push_string(ctx, ret);
1998 /* ECMA-262 3rd Edition 11.4.3 */
1999 static HRESULT interp_typeofident(script_ctx_t *ctx)
2001 const BSTR arg = get_op_bstr(ctx, 0);
2002 exprval_t exprval;
2003 const WCHAR *ret;
2004 jsval_t v;
2005 HRESULT hres;
2007 TRACE("%s\n", debugstr_w(arg));
2009 hres = identifier_eval(ctx, arg, &exprval);
2010 if(FAILED(hres))
2011 return hres;
2013 if(exprval.type == EXPRVAL_INVALID)
2014 return stack_push(ctx, jsval_string(jsstr_undefined()));
2016 hres = exprval_to_value(ctx, &exprval, &v);
2017 if(FAILED(hres))
2018 return hres;
2020 hres = typeof_string(v, &ret);
2021 jsval_release(v);
2022 if(FAILED(hres))
2023 return hres;
2025 return stack_push_string(ctx, ret);
2028 /* ECMA-262 3rd Edition 11.4.3 */
2029 static HRESULT interp_typeof(script_ctx_t *ctx)
2031 const WCHAR *ret;
2032 jsval_t v;
2033 HRESULT hres;
2035 TRACE("\n");
2037 v = stack_pop(ctx);
2038 hres = typeof_string(v, &ret);
2039 jsval_release(v);
2040 if(FAILED(hres))
2041 return hres;
2043 return stack_push_string(ctx, ret);
2046 /* ECMA-262 3rd Edition 11.4.7 */
2047 static HRESULT interp_minus(script_ctx_t *ctx)
2049 double n;
2050 HRESULT hres;
2052 TRACE("\n");
2054 hres = stack_pop_number(ctx, &n);
2055 if(FAILED(hres))
2056 return hres;
2058 return stack_push(ctx, jsval_number(-n));
2061 /* ECMA-262 3rd Edition 11.4.6 */
2062 static HRESULT interp_tonum(script_ctx_t *ctx)
2064 jsval_t v;
2065 double n;
2066 HRESULT hres;
2068 TRACE("\n");
2070 v = stack_pop(ctx);
2071 hres = to_number(ctx, v, &n);
2072 jsval_release(v);
2073 if(FAILED(hres))
2074 return hres;
2076 return stack_push(ctx, jsval_number(n));
2079 /* ECMA-262 3rd Edition 11.3.1 */
2080 static HRESULT interp_postinc(script_ctx_t *ctx)
2082 const int arg = get_op_int(ctx, 0);
2083 exprval_t ref;
2084 jsval_t v;
2085 HRESULT hres;
2087 TRACE("%d\n", arg);
2089 if(!stack_pop_exprval(ctx, &ref))
2090 return JS_E_OBJECT_EXPECTED;
2092 hres = exprval_propget(ctx, &ref, &v);
2093 if(SUCCEEDED(hres)) {
2094 double n;
2096 hres = to_number(ctx, v, &n);
2097 if(SUCCEEDED(hres))
2098 hres = exprval_propput(ctx, &ref, jsval_number(n+(double)arg));
2099 if(FAILED(hres))
2100 jsval_release(v);
2102 exprval_release(&ref);
2103 if(FAILED(hres))
2104 return hres;
2106 return stack_push(ctx, v);
2109 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2110 static HRESULT interp_preinc(script_ctx_t *ctx)
2112 const int arg = get_op_int(ctx, 0);
2113 exprval_t ref;
2114 double ret;
2115 jsval_t v;
2116 HRESULT hres;
2118 TRACE("%d\n", arg);
2120 if(!stack_pop_exprval(ctx, &ref))
2121 return JS_E_OBJECT_EXPECTED;
2123 hres = exprval_propget(ctx, &ref, &v);
2124 if(SUCCEEDED(hres)) {
2125 double n;
2127 hres = to_number(ctx, v, &n);
2128 jsval_release(v);
2129 if(SUCCEEDED(hres)) {
2130 ret = n+(double)arg;
2131 hres = exprval_propput(ctx, &ref, jsval_number(ret));
2134 exprval_release(&ref);
2135 if(FAILED(hres))
2136 return hres;
2138 return stack_push(ctx, jsval_number(ret));
2141 /* ECMA-262 3rd Edition 11.9.3 */
2142 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
2144 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
2145 return jsval_strict_equal(lval, rval, ret);
2147 /* FIXME: NULL disps should be handled in more general way */
2148 if(is_object_instance(lval) && !get_object(lval))
2149 return equal_values(ctx, jsval_null(), rval, ret);
2150 if(is_object_instance(rval) && !get_object(rval))
2151 return equal_values(ctx, lval, jsval_null(), ret);
2153 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
2154 *ret = TRUE;
2155 return S_OK;
2158 if(is_string(lval) && is_number(rval)) {
2159 double n;
2160 HRESULT hres;
2162 hres = to_number(ctx, lval, &n);
2163 if(FAILED(hres))
2164 return hres;
2166 /* FIXME: optimize */
2167 return equal_values(ctx, jsval_number(n), rval, ret);
2170 if(is_string(rval) && is_number(lval)) {
2171 double n;
2172 HRESULT hres;
2174 hres = to_number(ctx, rval, &n);
2175 if(FAILED(hres))
2176 return hres;
2178 /* FIXME: optimize */
2179 return equal_values(ctx, lval, jsval_number(n), ret);
2182 if(is_bool(rval))
2183 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
2185 if(is_bool(lval))
2186 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
2189 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
2190 jsval_t prim;
2191 HRESULT hres;
2193 hres = to_primitive(ctx, rval, &prim, NO_HINT);
2194 if(FAILED(hres))
2195 return hres;
2197 hres = equal_values(ctx, lval, prim, ret);
2198 jsval_release(prim);
2199 return hres;
2203 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
2204 jsval_t prim;
2205 HRESULT hres;
2207 hres = to_primitive(ctx, lval, &prim, NO_HINT);
2208 if(FAILED(hres))
2209 return hres;
2211 hres = equal_values(ctx, prim, rval, ret);
2212 jsval_release(prim);
2213 return hres;
2217 *ret = FALSE;
2218 return S_OK;
2221 /* ECMA-262 3rd Edition 11.9.1 */
2222 static HRESULT interp_eq(script_ctx_t *ctx)
2224 jsval_t l, r;
2225 BOOL b;
2226 HRESULT hres;
2228 r = stack_pop(ctx);
2229 l = stack_pop(ctx);
2231 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
2233 hres = equal_values(ctx, l, r, &b);
2234 jsval_release(l);
2235 jsval_release(r);
2236 if(FAILED(hres))
2237 return hres;
2239 return stack_push(ctx, jsval_bool(b));
2242 /* ECMA-262 3rd Edition 11.9.2 */
2243 static HRESULT interp_neq(script_ctx_t *ctx)
2245 jsval_t l, r;
2246 BOOL b;
2247 HRESULT hres;
2249 r = stack_pop(ctx);
2250 l = stack_pop(ctx);
2252 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2254 hres = equal_values(ctx, l, r, &b);
2255 jsval_release(l);
2256 jsval_release(r);
2257 if(FAILED(hres))
2258 return hres;
2260 return stack_push(ctx, jsval_bool(!b));
2263 /* ECMA-262 3rd Edition 11.9.4 */
2264 static HRESULT interp_eq2(script_ctx_t *ctx)
2266 jsval_t l, r;
2267 BOOL b;
2268 HRESULT hres;
2270 r = stack_pop(ctx);
2271 l = stack_pop(ctx);
2273 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2275 hres = jsval_strict_equal(r, l, &b);
2276 jsval_release(l);
2277 jsval_release(r);
2278 if(FAILED(hres))
2279 return hres;
2281 return stack_push(ctx, jsval_bool(b));
2284 /* ECMA-262 3rd Edition 11.9.5 */
2285 static HRESULT interp_neq2(script_ctx_t *ctx)
2287 jsval_t l, r;
2288 BOOL b;
2289 HRESULT hres;
2291 TRACE("\n");
2293 r = stack_pop(ctx);
2294 l = stack_pop(ctx);
2296 hres = jsval_strict_equal(r, l, &b);
2297 jsval_release(l);
2298 jsval_release(r);
2299 if(FAILED(hres))
2300 return hres;
2302 return stack_push(ctx, jsval_bool(!b));
2305 /* ECMA-262 3rd Edition 11.8.5 */
2306 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2308 double ln, rn;
2309 jsval_t l, r;
2310 HRESULT hres;
2312 hres = to_primitive(ctx, lval, &l, NO_HINT);
2313 if(FAILED(hres))
2314 return hres;
2316 hres = to_primitive(ctx, rval, &r, NO_HINT);
2317 if(FAILED(hres)) {
2318 jsval_release(l);
2319 return hres;
2322 if(is_string(l) && is_string(r)) {
2323 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2324 jsstr_release(get_string(l));
2325 jsstr_release(get_string(r));
2326 return S_OK;
2329 hres = to_number(ctx, l, &ln);
2330 jsval_release(l);
2331 if(SUCCEEDED(hres))
2332 hres = to_number(ctx, r, &rn);
2333 jsval_release(r);
2334 if(FAILED(hres))
2335 return hres;
2337 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2338 return S_OK;
2341 /* ECMA-262 3rd Edition 11.8.1 */
2342 static HRESULT interp_lt(script_ctx_t *ctx)
2344 jsval_t l, r;
2345 BOOL b;
2346 HRESULT hres;
2348 r = stack_pop(ctx);
2349 l = stack_pop(ctx);
2351 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2353 hres = less_eval(ctx, l, r, FALSE, &b);
2354 jsval_release(l);
2355 jsval_release(r);
2356 if(FAILED(hres))
2357 return hres;
2359 return stack_push(ctx, jsval_bool(b));
2362 /* ECMA-262 3rd Edition 11.8.1 */
2363 static HRESULT interp_lteq(script_ctx_t *ctx)
2365 jsval_t l, r;
2366 BOOL b;
2367 HRESULT hres;
2369 r = stack_pop(ctx);
2370 l = stack_pop(ctx);
2372 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2374 hres = less_eval(ctx, r, l, TRUE, &b);
2375 jsval_release(l);
2376 jsval_release(r);
2377 if(FAILED(hres))
2378 return hres;
2380 return stack_push(ctx, jsval_bool(b));
2383 /* ECMA-262 3rd Edition 11.8.2 */
2384 static HRESULT interp_gt(script_ctx_t *ctx)
2386 jsval_t l, r;
2387 BOOL b;
2388 HRESULT hres;
2390 r = stack_pop(ctx);
2391 l = stack_pop(ctx);
2393 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2395 hres = less_eval(ctx, r, l, FALSE, &b);
2396 jsval_release(l);
2397 jsval_release(r);
2398 if(FAILED(hres))
2399 return hres;
2401 return stack_push(ctx, jsval_bool(b));
2404 /* ECMA-262 3rd Edition 11.8.4 */
2405 static HRESULT interp_gteq(script_ctx_t *ctx)
2407 jsval_t l, r;
2408 BOOL b;
2409 HRESULT hres;
2411 r = stack_pop(ctx);
2412 l = stack_pop(ctx);
2414 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2416 hres = less_eval(ctx, l, r, TRUE, &b);
2417 jsval_release(l);
2418 jsval_release(r);
2419 if(FAILED(hres))
2420 return hres;
2422 return stack_push(ctx, jsval_bool(b));
2425 /* ECMA-262 3rd Edition 11.4.8 */
2426 static HRESULT interp_bneg(script_ctx_t *ctx)
2428 jsval_t v;
2429 INT i;
2430 HRESULT hres;
2432 TRACE("\n");
2434 v = stack_pop(ctx);
2435 hres = to_int32(ctx, v, &i);
2436 jsval_release(v);
2437 if(FAILED(hres))
2438 return hres;
2440 return stack_push(ctx, jsval_number(~i));
2443 /* ECMA-262 3rd Edition 11.4.9 */
2444 static HRESULT interp_neg(script_ctx_t *ctx)
2446 jsval_t v;
2447 BOOL b;
2448 HRESULT hres;
2450 TRACE("\n");
2452 v = stack_pop(ctx);
2453 hres = to_boolean(v, &b);
2454 jsval_release(v);
2455 if(FAILED(hres))
2456 return hres;
2458 return stack_push(ctx, jsval_bool(!b));
2461 /* ECMA-262 3rd Edition 11.7.1 */
2462 static HRESULT interp_lshift(script_ctx_t *ctx)
2464 DWORD r;
2465 INT l;
2466 HRESULT hres;
2468 hres = stack_pop_uint(ctx, &r);
2469 if(FAILED(hres))
2470 return hres;
2472 hres = stack_pop_int(ctx, &l);
2473 if(FAILED(hres))
2474 return hres;
2476 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2479 /* ECMA-262 3rd Edition 11.7.2 */
2480 static HRESULT interp_rshift(script_ctx_t *ctx)
2482 DWORD r;
2483 INT l;
2484 HRESULT hres;
2486 hres = stack_pop_uint(ctx, &r);
2487 if(FAILED(hres))
2488 return hres;
2490 hres = stack_pop_int(ctx, &l);
2491 if(FAILED(hres))
2492 return hres;
2494 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2497 /* ECMA-262 3rd Edition 11.7.3 */
2498 static HRESULT interp_rshift2(script_ctx_t *ctx)
2500 DWORD r, l;
2501 HRESULT hres;
2503 hres = stack_pop_uint(ctx, &r);
2504 if(FAILED(hres))
2505 return hres;
2507 hres = stack_pop_uint(ctx, &l);
2508 if(FAILED(hres))
2509 return hres;
2511 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2514 /* ECMA-262 3rd Edition 9.8 */
2515 static HRESULT interp_to_string(script_ctx_t *ctx)
2517 jsstr_t *str;
2518 jsval_t v;
2519 HRESULT hres;
2521 v = stack_pop(ctx);
2522 TRACE("%s\n", debugstr_jsval(v));
2523 hres = to_string(ctx, v, &str);
2524 jsval_release(v);
2525 if(FAILED(hres)) {
2526 WARN("failed %08x\n", hres);
2527 return hres;
2530 return stack_push(ctx, jsval_string(str));
2533 /* ECMA-262 3rd Edition 11.13.1 */
2534 static HRESULT interp_assign(script_ctx_t *ctx)
2536 exprval_t ref;
2537 jsval_t v;
2538 HRESULT hres;
2540 TRACE("\n");
2542 v = stack_pop(ctx);
2544 if(!stack_pop_exprval(ctx, &ref)) {
2545 jsval_release(v);
2546 return JS_E_ILLEGAL_ASSIGN;
2549 hres = exprval_propput(ctx, &ref, v);
2550 exprval_release(&ref);
2551 if(FAILED(hres)) {
2552 jsval_release(v);
2553 return hres;
2556 return stack_push(ctx, v);
2559 /* ECMA-262 3rd Edition 11.13.1 */
2560 static HRESULT interp_set_member(script_ctx_t *ctx)
2562 jsval_t objv, namev, value;
2563 const WCHAR *name;
2564 IDispatch *obj;
2565 HRESULT hres;
2567 value = stack_pop(ctx);
2568 namev = stack_pop(ctx);
2569 assert(is_string(namev));
2570 objv = stack_pop(ctx);
2572 TRACE("%s.%s = %s\n", debugstr_jsval(objv), debugstr_jsval(namev), debugstr_jsval(value));
2574 hres = to_object(ctx, objv, &obj);
2575 jsval_release(objv);
2576 if(SUCCEEDED(hres) && !(name = jsstr_flatten(get_string(namev)))) {
2577 IDispatch_Release(obj);
2578 hres = E_OUTOFMEMORY;
2580 if(SUCCEEDED(hres)) {
2581 hres = disp_propput_name(ctx, obj, name, value);
2582 IDispatch_Release(obj);
2583 jsstr_release(get_string(namev));
2585 if(FAILED(hres)) {
2586 WARN("failed %08x\n", hres);
2587 jsval_release(value);
2588 return hres;
2591 return stack_push(ctx, value);
2594 /* JScript extension */
2595 static HRESULT interp_assign_call(script_ctx_t *ctx)
2597 const unsigned argc = get_op_uint(ctx, 0);
2598 exprval_t ref;
2599 jsval_t v;
2600 HRESULT hres;
2602 TRACE("%u\n", argc);
2604 if(!stack_topn_exprval(ctx, argc+1, &ref))
2605 return JS_E_ILLEGAL_ASSIGN;
2607 hres = exprval_call(ctx, &ref, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2608 if(FAILED(hres))
2609 return hres;
2611 v = stack_pop(ctx);
2612 stack_popn(ctx, argc+2);
2613 return stack_push(ctx, v);
2616 static HRESULT interp_undefined(script_ctx_t *ctx)
2618 TRACE("\n");
2620 return stack_push(ctx, jsval_undefined());
2623 static HRESULT interp_jmp(script_ctx_t *ctx)
2625 const unsigned arg = get_op_uint(ctx, 0);
2627 TRACE("%u\n", arg);
2629 jmp_abs(ctx, arg);
2630 return S_OK;
2633 static HRESULT interp_jmp_z(script_ctx_t *ctx)
2635 const unsigned arg = get_op_uint(ctx, 0);
2636 BOOL b;
2637 jsval_t v;
2638 HRESULT hres;
2640 TRACE("\n");
2642 v = stack_pop(ctx);
2643 hres = to_boolean(v, &b);
2644 jsval_release(v);
2645 if(FAILED(hres))
2646 return hres;
2648 if(b)
2649 jmp_next(ctx);
2650 else
2651 jmp_abs(ctx, arg);
2652 return S_OK;
2655 static HRESULT interp_pop(script_ctx_t *ctx)
2657 const unsigned arg = get_op_uint(ctx, 0);
2659 TRACE("%u\n", arg);
2661 stack_popn(ctx, arg);
2662 return S_OK;
2665 static HRESULT interp_ret(script_ctx_t *ctx)
2667 const unsigned clear_ret = get_op_uint(ctx, 0);
2668 call_frame_t *frame = ctx->call_ctx;
2670 TRACE("\n");
2672 if(clear_ret)
2673 jsval_release(steal_ret(frame));
2675 if((frame->flags & EXEC_CONSTRUCTOR) && !is_object_instance(frame->ret)) {
2676 jsval_release(frame->ret);
2677 IDispatch_AddRef(frame->this_obj);
2678 frame->ret = jsval_disp(frame->this_obj);
2681 jmp_abs(ctx, -1);
2682 return S_OK;
2685 static HRESULT interp_setret(script_ctx_t *ctx)
2687 call_frame_t *frame = ctx->call_ctx;
2689 TRACE("\n");
2691 jsval_release(frame->ret);
2692 frame->ret = stack_pop(ctx);
2693 return S_OK;
2696 static HRESULT interp_push_acc(script_ctx_t *ctx)
2698 HRESULT hres;
2700 TRACE("\n");
2702 hres = stack_push(ctx, ctx->acc);
2703 if(SUCCEEDED(hres))
2704 ctx->acc = jsval_undefined();
2705 return hres;
2708 typedef HRESULT (*op_func_t)(script_ctx_t*);
2710 static const op_func_t op_funcs[] = {
2711 #define X(x,a,b,c) interp_##x,
2712 OP_LIST
2713 #undef X
2716 static const unsigned op_move[] = {
2717 #define X(a,x,b,c) x,
2718 OP_LIST
2719 #undef X
2722 static void pop_call_frame(script_ctx_t *ctx)
2724 call_frame_t *frame = ctx->call_ctx;
2726 frame->stack_base -= frame->pop_locals + frame->pop_variables;
2728 assert(frame->scope == frame->base_scope);
2730 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2731 if(frame->scope && frame->scope->ref > 1) {
2732 HRESULT hres = detach_variable_object(ctx, frame, TRUE);
2733 if(FAILED(hres))
2734 ERR("Failed to detach variable object: %08x\n", hres);
2737 if(frame->arguments_obj)
2738 detach_arguments_object(frame->arguments_obj);
2739 if(frame->scope)
2740 scope_release(frame->scope);
2742 if(frame->pop_variables)
2743 stack_popn(ctx, frame->pop_variables);
2744 stack_popn(ctx, frame->pop_locals);
2746 ctx->call_ctx = frame->prev_frame;
2748 if(frame->function_instance)
2749 jsdisp_release(frame->function_instance);
2750 if(frame->variable_obj)
2751 jsdisp_release(frame->variable_obj);
2752 if(frame->this_obj)
2753 IDispatch_Release(frame->this_obj);
2754 jsval_release(frame->ret);
2755 release_bytecode(frame->bytecode);
2756 heap_free(frame);
2759 static void print_backtrace(script_ctx_t *ctx)
2761 unsigned depth = 0, i, line, char_pos;
2762 call_frame_t *frame;
2764 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
2765 WARN("%u\t", depth);
2766 depth++;
2768 if(frame->this_obj)
2769 WARN("%p->", frame->this_obj);
2770 WARN("%s(", frame->function->name ? debugstr_w(frame->function->name) : "[unnamed]");
2771 if(frame->base_scope && frame->base_scope->frame) {
2772 for(i=0; i < frame->argc; i++) {
2773 if(i < frame->function->param_cnt)
2774 WARN("%s%s=%s", i ? ", " : "", debugstr_w(frame->function->params[i]),
2775 debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2776 else
2777 WARN("%s%s", i ? ", " : "", debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2779 }else {
2780 WARN("[detached frame]");
2782 line = get_location_line(frame->bytecode, frame->bytecode->instrs[frame->ip].loc, &char_pos);
2783 WARN(") context %s line %u char %u\n", wine_dbgstr_longlong(frame->bytecode->source_context), line, char_pos);
2785 if(!(frame->flags & EXEC_RETURN_TO_INTERP)) {
2786 WARN("%u\t[native code]\n", depth);
2787 depth++;
2792 static HRESULT unwind_exception(script_ctx_t *ctx, HRESULT exception_hres)
2794 except_frame_t *except_frame;
2795 jsexcept_t *ei = ctx->ei;
2796 call_frame_t *frame;
2797 jsval_t except_val;
2798 unsigned catch_off;
2799 HRESULT hres;
2801 if(WARN_ON(jscript)) {
2802 jsdisp_t *error_obj;
2803 jsval_t msg;
2805 WARN("Exception %08x %s", exception_hres, debugstr_jsval(ei->valid_value ? ei->value : jsval_undefined()));
2806 if(ei->valid_value && jsval_type(ei->value) == JSV_OBJECT) {
2807 error_obj = to_jsdisp(get_object(ei->value));
2808 if(error_obj) {
2809 hres = jsdisp_propget_name(error_obj, L"message", &msg);
2810 if(SUCCEEDED(hres)) {
2811 WARN(" (message %s)", debugstr_jsval(msg));
2812 jsval_release(msg);
2816 WARN(" in:\n");
2818 print_backtrace(ctx);
2821 frame = ctx->call_ctx;
2822 if(exception_hres != DISP_E_EXCEPTION)
2823 throw_error(ctx, exception_hres, NULL);
2824 set_error_location(ei, frame->bytecode, frame->bytecode->instrs[frame->ip].loc, IDS_RUNTIME_ERROR, NULL);
2826 while(!frame->except_frame) {
2827 DWORD flags;
2829 while(frame->scope != frame->base_scope)
2830 scope_pop(&frame->scope);
2832 stack_popn(ctx, ctx->stack_top-frame->stack_base);
2834 flags = frame->flags;
2835 pop_call_frame(ctx);
2836 if(!(flags & EXEC_RETURN_TO_INTERP))
2837 return DISP_E_EXCEPTION;
2838 frame = ctx->call_ctx;
2841 except_frame = frame->except_frame;
2842 catch_off = except_frame->catch_off;
2844 assert(except_frame->stack_top <= ctx->stack_top);
2845 stack_popn(ctx, ctx->stack_top - except_frame->stack_top);
2847 while(except_frame->scope != frame->scope)
2848 scope_pop(&frame->scope);
2850 frame->ip = catch_off ? catch_off : except_frame->finally_off;
2851 assert(!catch_off || frame->bytecode->instrs[frame->ip].op == OP_enter_catch);
2853 if(ei->valid_value) {
2854 except_val = ctx->ei->value;
2855 ei->valid_value = FALSE;
2856 }else {
2857 jsdisp_t *err;
2858 if(!(err = create_builtin_error(ctx)))
2859 return E_OUTOFMEMORY;
2860 except_val = jsval_obj(err);
2863 /* keep current except_frame if we're entering catch block with finally block associated */
2864 if(catch_off && except_frame->finally_off) {
2865 except_frame->catch_off = 0;
2866 }else {
2867 frame->except_frame = except_frame->next;
2868 heap_free(except_frame);
2871 hres = stack_push(ctx, except_val);
2872 if(FAILED(hres))
2873 return hres;
2875 if(!catch_off)
2876 hres = stack_push(ctx, jsval_bool(FALSE));
2877 return hres;
2880 static HRESULT enter_bytecode(script_ctx_t *ctx, jsval_t *r)
2882 call_frame_t *frame;
2883 jsop_t op;
2884 HRESULT hres = S_OK;
2886 TRACE("\n");
2888 while(1) {
2889 frame = ctx->call_ctx;
2890 op = frame->bytecode->instrs[frame->ip].op;
2891 hres = op_funcs[op](ctx);
2892 if(FAILED(hres)) {
2893 hres = unwind_exception(ctx, hres);
2894 if(FAILED(hres))
2895 return hres;
2896 }else if(frame->ip == -1) {
2897 const DWORD return_to_interp = frame->flags & EXEC_RETURN_TO_INTERP;
2899 assert(ctx->stack_top == frame->stack_base);
2900 assert(frame->scope == frame->base_scope);
2902 if(return_to_interp) {
2903 jsval_release(ctx->acc);
2904 ctx->acc = steal_ret(frame);
2905 }else if(r) {
2906 *r = steal_ret(frame);
2908 pop_call_frame(ctx);
2909 if(!return_to_interp)
2910 break;
2911 }else {
2912 frame->ip += op_move[op];
2916 return S_OK;
2919 static HRESULT bind_event_target(script_ctx_t *ctx, function_code_t *func, jsdisp_t *func_obj)
2921 IBindEventHandler *target;
2922 exprval_t exprval;
2923 IDispatch *disp;
2924 jsval_t v;
2925 HRESULT hres;
2927 hres = identifier_eval(ctx, func->event_target, &exprval);
2928 if(FAILED(hres))
2929 return hres;
2931 hres = exprval_to_value(ctx, &exprval, &v);
2932 if(FAILED(hres))
2933 return hres;
2935 if(!is_object_instance(v)) {
2936 FIXME("Can't bind to %s\n", debugstr_jsval(v));
2937 jsval_release(v);
2940 disp = get_object(v);
2941 hres = IDispatch_QueryInterface(disp, &IID_IBindEventHandler, (void**)&target);
2942 if(SUCCEEDED(hres)) {
2943 hres = IBindEventHandler_BindHandler(target, func->name, (IDispatch*)&func_obj->IDispatchEx_iface);
2944 IBindEventHandler_Release(target);
2945 if(FAILED(hres))
2946 WARN("BindEvent failed: %08x\n", hres);
2947 }else {
2948 FIXME("No IBindEventHandler, not yet supported binding\n");
2951 IDispatch_Release(disp);
2952 return hres;
2955 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)
2957 const unsigned orig_stack = ctx->stack_top;
2958 scope_chain_t *scope;
2959 unsigned i;
2960 jsval_t v;
2961 HRESULT hres;
2963 /* If arguments are already on the stack, we may use them. */
2964 if(argv + argc == ctx->stack + ctx->stack_top) {
2965 frame->arguments_off = argv - ctx->stack;
2966 i = argc;
2967 }else {
2968 frame->arguments_off = ctx->stack_top;
2969 for(i = 0; i < argc; i++) {
2970 hres = jsval_copy(argv[i], &v);
2971 if(SUCCEEDED(hres))
2972 hres = stack_push(ctx, v);
2973 if(FAILED(hres)) {
2974 stack_popn(ctx, i);
2975 return hres;
2980 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2981 for(; i < frame->function->param_cnt; i++) {
2982 hres = stack_push(ctx, jsval_undefined());
2983 if(FAILED(hres)) {
2984 stack_popn(ctx, ctx->stack_top - orig_stack);
2985 return hres;
2989 frame->pop_locals = ctx->stack_top - orig_stack;
2991 frame->variables_off = ctx->stack_top;
2993 for(i = 0; i < frame->function->var_cnt; i++) {
2994 hres = stack_push(ctx, jsval_undefined());
2995 if(FAILED(hres)) {
2996 stack_popn(ctx, ctx->stack_top - orig_stack);
2997 return hres;
3001 frame->pop_variables = i;
3003 hres = scope_push(scope_chain, variable_object, to_disp(variable_object), &scope);
3004 if(FAILED(hres)) {
3005 stack_popn(ctx, ctx->stack_top - orig_stack);
3006 return hres;
3009 for(i = 0; i < frame->function->func_cnt; i++) {
3010 if(frame->function->funcs[i].name && !frame->function->funcs[i].event_target) {
3011 jsdisp_t *func_obj;
3012 unsigned off;
3014 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+i, scope, &func_obj);
3015 if(FAILED(hres)) {
3016 stack_popn(ctx, ctx->stack_top - orig_stack);
3017 scope_release(scope);
3018 return hres;
3021 off = local_off(frame, frame->function->funcs[i].local_ref);
3022 jsval_release(ctx->stack[off]);
3023 ctx->stack[off] = jsval_obj(func_obj);
3027 scope->frame = frame;
3028 frame->base_scope = frame->scope = scope;
3029 return S_OK;
3032 HRESULT exec_source(script_ctx_t *ctx, DWORD flags, bytecode_t *bytecode, function_code_t *function, scope_chain_t *scope,
3033 IDispatch *this_obj, jsdisp_t *function_instance, unsigned argc, jsval_t *argv, jsval_t *r)
3035 jsdisp_t *variable_obj;
3036 call_frame_t *frame;
3037 unsigned i;
3038 HRESULT hres;
3040 if(!ctx->stack) {
3041 ctx->stack = heap_alloc(stack_size * sizeof(*ctx->stack));
3042 if(!ctx->stack)
3043 return E_OUTOFMEMORY;
3046 if(bytecode->named_item) {
3047 if(!bytecode->named_item->script_obj) {
3048 hres = create_named_item_script_obj(ctx, bytecode->named_item);
3049 if(FAILED(hres)) return hres;
3053 if(!ctx->ei->enter_notified) {
3054 ctx->ei->enter_notified = TRUE;
3055 IActiveScriptSite_OnEnterScript(ctx->site);
3058 for(i = 0; i < function->func_cnt; i++) {
3059 jsdisp_t *func_obj;
3061 if(!function->funcs[i].event_target)
3062 continue;
3064 hres = create_source_function(ctx, bytecode, function->funcs+i, scope, &func_obj);
3065 if(FAILED(hres))
3066 return hres;
3068 hres = bind_event_target(ctx, function->funcs+i, func_obj);
3069 jsdisp_release(func_obj);
3070 if(FAILED(hres))
3071 return hres;
3074 if((flags & EXEC_EVAL) && ctx->call_ctx) {
3075 variable_obj = jsdisp_addref(ctx->call_ctx->variable_obj);
3076 }else if(!(flags & (EXEC_GLOBAL | EXEC_EVAL))) {
3077 hres = create_dispex(ctx, NULL, NULL, &variable_obj);
3078 if(FAILED(hres)) return hres;
3079 }else if(bytecode->named_item) {
3080 variable_obj = jsdisp_addref(bytecode->named_item->script_obj);
3081 }else {
3082 variable_obj = jsdisp_addref(ctx->global);
3085 if(flags & (EXEC_GLOBAL | EXEC_EVAL)) {
3086 named_item_t *item = bytecode->named_item;
3087 DISPID id;
3089 for(i=0; i < function->var_cnt; i++) {
3090 TRACE("[%d] %s %d\n", i, debugstr_w(function->variables[i].name), function->variables[i].func_id);
3091 if(function->variables[i].func_id != -1) {
3092 jsdisp_t *func_obj;
3094 hres = create_source_function(ctx, bytecode, function->funcs+function->variables[i].func_id, scope, &func_obj);
3095 if(FAILED(hres))
3096 goto fail;
3098 hres = jsdisp_propput_name(variable_obj, function->variables[i].name, jsval_obj(func_obj));
3099 jsdisp_release(func_obj);
3100 continue;
3103 if(item && !(item->flags & SCRIPTITEM_CODEONLY)
3104 && SUCCEEDED(disp_get_id(ctx, item->disp, function->variables[i].name, function->variables[i].name, 0, &id)))
3105 continue;
3107 if(!item && (flags & EXEC_GLOBAL) && lookup_global_members(ctx, function->variables[i].name, NULL))
3108 continue;
3110 hres = jsdisp_get_id(variable_obj, function->variables[i].name, fdexNameEnsure, &id);
3111 if(FAILED(hres))
3112 goto fail;
3116 /* ECMA-262 3rd Edition 11.2.3.7 */
3117 if(this_obj) {
3118 jsdisp_t *jsthis;
3120 jsthis = iface_to_jsdisp(this_obj);
3121 if(jsthis) {
3122 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
3123 this_obj = NULL;
3124 jsdisp_release(jsthis);
3128 if(ctx->call_ctx && (flags & EXEC_EVAL)) {
3129 hres = detach_variable_object(ctx, ctx->call_ctx, FALSE);
3130 if(FAILED(hres))
3131 goto fail;
3134 frame = heap_alloc_zero(sizeof(*frame));
3135 if(!frame) {
3136 hres = E_OUTOFMEMORY;
3137 goto fail;
3140 frame->function = function;
3141 frame->ret = jsval_undefined();
3142 frame->argc = argc;
3143 frame->bytecode = bytecode_addref(bytecode);
3145 if(!(flags & (EXEC_GLOBAL|EXEC_EVAL))) {
3146 hres = setup_scope(ctx, frame, scope, variable_obj, argc, argv);
3147 if(FAILED(hres)) {
3148 release_bytecode(frame->bytecode);
3149 heap_free(frame);
3150 goto fail;
3152 }else if(scope) {
3153 frame->base_scope = frame->scope = scope_addref(scope);
3156 frame->ip = function->instr_off;
3157 frame->stack_base = ctx->stack_top;
3158 if(this_obj) {
3159 frame->this_obj = this_obj;
3160 IDispatch_AddRef(frame->this_obj);
3163 if(function_instance)
3164 frame->function_instance = jsdisp_addref(function_instance);
3166 frame->flags = flags;
3167 frame->variable_obj = variable_obj;
3169 frame->prev_frame = ctx->call_ctx;
3170 ctx->call_ctx = frame;
3172 if(flags & EXEC_RETURN_TO_INTERP) {
3174 * We're called directly from interpreter, so we may just setup call frame and return.
3175 * Already running interpreter will take care of execution.
3177 if(r)
3178 *r = jsval_undefined();
3179 return S_OK;
3182 return enter_bytecode(ctx, r);
3184 fail:
3185 jsdisp_release(variable_obj);
3186 return hres;