d3d9: Fix Reset() with system memory buffers.
[wine.git] / dlls / jscript / engine.c
blob0f9e3a5463cf0985f21544b27fc226dd5be413cf
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
23 #include <assert.h>
25 #include "jscript.h"
26 #include "engine.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
33 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
34 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
35 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
36 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
37 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
38 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
40 struct _except_frame_t {
41 unsigned stack_top;
42 scope_chain_t *scope;
43 unsigned catch_off;
44 unsigned finally_off;
46 except_frame_t *next;
49 typedef struct {
50 enum {
51 EXPRVAL_JSVAL,
52 EXPRVAL_IDREF,
53 EXPRVAL_STACK_REF,
54 EXPRVAL_INVALID
55 } type;
56 union {
57 jsval_t val;
58 struct {
59 IDispatch *disp;
60 DISPID id;
61 } idref;
62 unsigned off;
63 HRESULT hres;
64 } u;
65 } exprval_t;
67 static HRESULT stack_push(script_ctx_t *ctx, jsval_t v)
69 if(!ctx->stack_size) {
70 ctx->stack = heap_alloc(16*sizeof(*ctx->stack));
71 if(!ctx->stack)
72 return E_OUTOFMEMORY;
73 ctx->stack_size = 16;
74 }else if(ctx->stack_size == ctx->stack_top) {
75 jsval_t *new_stack;
77 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*new_stack));
78 if(!new_stack) {
79 jsval_release(v);
80 return E_OUTOFMEMORY;
83 ctx->stack = new_stack;
84 ctx->stack_size *= 2;
87 ctx->stack[ctx->stack_top++] = v;
88 return S_OK;
91 static inline HRESULT stack_push_string(script_ctx_t *ctx, const WCHAR *str)
93 jsstr_t *v;
95 v = jsstr_alloc(str);
96 if(!v)
97 return E_OUTOFMEMORY;
99 return stack_push(ctx, jsval_string(v));
102 static inline jsval_t stack_top(script_ctx_t *ctx)
104 assert(ctx->stack_top > ctx->call_ctx->stack_base);
105 return ctx->stack[ctx->stack_top-1];
108 static inline jsval_t *stack_top_ref(script_ctx_t *ctx, unsigned n)
110 assert(ctx->stack_top > ctx->call_ctx->stack_base+n);
111 return ctx->stack+ctx->stack_top-1-n;
114 static inline jsval_t stack_topn(script_ctx_t *ctx, unsigned n)
116 return *stack_top_ref(ctx, n);
119 static inline jsval_t *stack_args(script_ctx_t *ctx, unsigned n)
121 if(!n)
122 return NULL;
123 assert(ctx->stack_top > ctx->call_ctx->stack_base+n-1);
124 return ctx->stack + ctx->stack_top-n;
127 static inline jsval_t stack_pop(script_ctx_t *ctx)
129 assert(ctx->stack_top > ctx->call_ctx->stack_base);
130 return ctx->stack[--ctx->stack_top];
133 static void stack_popn(script_ctx_t *ctx, unsigned n)
135 while(n--)
136 jsval_release(stack_pop(ctx));
139 static HRESULT stack_pop_number(script_ctx_t *ctx, double *r)
141 jsval_t v;
142 HRESULT hres;
144 v = stack_pop(ctx);
145 hres = to_number(ctx, v, r);
146 jsval_release(v);
147 return hres;
150 static HRESULT stack_pop_object(script_ctx_t *ctx, IDispatch **r)
152 jsval_t v;
153 HRESULT hres;
155 v = stack_pop(ctx);
156 if(is_object_instance(v)) {
157 if(!get_object(v))
158 return throw_type_error(ctx, JS_E_OBJECT_REQUIRED, NULL);
159 *r = get_object(v);
160 return S_OK;
163 hres = to_object(ctx, v, r);
164 jsval_release(v);
165 return hres;
168 static inline HRESULT stack_pop_int(script_ctx_t *ctx, INT *r)
170 return to_int32(ctx, stack_pop(ctx), r);
173 static inline HRESULT stack_pop_uint(script_ctx_t *ctx, DWORD *r)
175 return to_uint32(ctx, stack_pop(ctx), r);
178 static inline unsigned local_off(call_frame_t *frame, int ref)
180 return ref < 0
181 ? frame->arguments_off - ref-1
182 : frame->variables_off + ref;
185 static inline BSTR local_name(call_frame_t *frame, int ref)
187 return ref < 0 ? frame->function->params[-ref-1] : frame->function->variables[ref].name;
190 /* Steals input reference even on failure. */
191 static HRESULT stack_push_exprval(script_ctx_t *ctx, exprval_t *val)
193 HRESULT hres;
195 switch(val->type) {
196 case EXPRVAL_JSVAL:
197 assert(0);
198 case EXPRVAL_IDREF:
199 hres = stack_push(ctx, jsval_disp(val->u.idref.disp));
200 if(SUCCEEDED(hres))
201 hres = stack_push(ctx, jsval_number(val->u.idref.id));
202 else
203 IDispatch_Release(val->u.idref.disp);
204 return hres;
205 case EXPRVAL_STACK_REF:
206 hres = stack_push(ctx, jsval_number(val->u.off));
207 if(SUCCEEDED(hres))
208 hres = stack_push(ctx, jsval_undefined());
209 return hres;
210 case EXPRVAL_INVALID:
211 hres = stack_push(ctx, jsval_undefined());
212 if(SUCCEEDED(hres))
213 hres = stack_push(ctx, jsval_number(val->u.hres));
214 return hres;
217 assert(0);
218 return E_FAIL;
221 static BOOL stack_topn_exprval(script_ctx_t *ctx, unsigned n, exprval_t *r)
223 jsval_t v = stack_topn(ctx, n+1);
225 switch(jsval_type(v)) {
226 case JSV_NUMBER: {
227 call_frame_t *frame = ctx->call_ctx;
228 unsigned off = get_number(v);
230 if(!frame->base_scope->frame && off >= frame->arguments_off) {
231 DISPID id;
232 BSTR name;
233 HRESULT hres;
235 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
237 assert(off < frame->variables_off + frame->function->var_cnt);
238 name = off >= frame->variables_off
239 ? frame->function->variables[off - frame->variables_off].name
240 : frame->function->params[off - frame->arguments_off];
241 hres = jsdisp_get_id(ctx->call_ctx->base_scope->jsobj, name, 0, &id);
242 if(FAILED(hres)) {
243 r->type = EXPRVAL_INVALID;
244 r->u.hres = hres;
245 return FALSE;
248 *stack_top_ref(ctx, n+1) = jsval_obj(jsdisp_addref(frame->base_scope->jsobj));
249 *stack_top_ref(ctx, n) = jsval_number(id);
250 r->type = EXPRVAL_IDREF;
251 r->u.idref.disp = frame->base_scope->obj;
252 r->u.idref.id = id;
253 return TRUE;
256 r->type = EXPRVAL_STACK_REF;
257 r->u.off = off;
258 return TRUE;
260 case JSV_OBJECT:
261 r->type = EXPRVAL_IDREF;
262 r->u.idref.disp = get_object(v);
263 assert(is_number(stack_topn(ctx, n)));
264 r->u.idref.id = get_number(stack_topn(ctx, n));
265 return TRUE;
266 case JSV_UNDEFINED:
267 r->type = EXPRVAL_INVALID;
268 assert(is_number(stack_topn(ctx, n)));
269 r->u.hres = get_number(stack_topn(ctx, n));
270 return FALSE;
271 default:
272 assert(0);
273 return FALSE;
277 static inline BOOL stack_pop_exprval(script_ctx_t *ctx, exprval_t *r)
279 BOOL ret = stack_topn_exprval(ctx, 0, r);
280 ctx->stack_top -= 2;
281 return ret;
284 static HRESULT exprval_propput(script_ctx_t *ctx, exprval_t *ref, jsval_t v)
286 switch(ref->type) {
287 case EXPRVAL_STACK_REF: {
288 jsval_t *r = ctx->stack + ref->u.off;
289 jsval_release(*r);
290 return jsval_copy(v, r);
292 case EXPRVAL_IDREF:
293 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v);
294 default:
295 assert(0);
296 return E_FAIL;
300 static HRESULT exprval_propget(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
302 switch(ref->type) {
303 case EXPRVAL_STACK_REF:
304 return jsval_copy(ctx->stack[ref->u.off], r);
305 case EXPRVAL_IDREF:
306 return disp_propget(ctx, ref->u.idref.disp, ref->u.idref.id, r);
307 default:
308 assert(0);
309 return E_FAIL;
313 static HRESULT exprval_call(script_ctx_t *ctx, exprval_t *ref, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
315 switch(ref->type) {
316 case EXPRVAL_STACK_REF: {
317 jsval_t v = ctx->stack[ref->u.off];
319 if(!is_object_instance(v)) {
320 FIXME("invoke %s\n", debugstr_jsval(v));
321 return E_FAIL;
324 return disp_call_value(ctx, get_object(v), NULL, flags, argc, argv, r);
326 case EXPRVAL_IDREF:
327 return disp_call(ctx, ref->u.idref.disp, ref->u.idref.id, flags, argc, argv, r);
328 default:
329 assert(0);
330 return E_FAIL;
334 /* ECMA-262 3rd Edition 8.7.1 */
335 /* Steals input reference. */
336 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
338 HRESULT hres;
340 if(ref->type == EXPRVAL_JSVAL) {
341 *r = ref->u.val;
342 return S_OK;
345 hres = exprval_propget(ctx, ref, r);
347 if(ref->type == EXPRVAL_IDREF)
348 IDispatch_Release(ref->u.idref.disp);
349 return hres;
352 static void exprval_release(exprval_t *val)
354 switch(val->type) {
355 case EXPRVAL_JSVAL:
356 jsval_release(val->u.val);
357 return;
358 case EXPRVAL_IDREF:
359 if(val->u.idref.disp)
360 IDispatch_Release(val->u.idref.disp);
361 return;
362 case EXPRVAL_STACK_REF:
363 case EXPRVAL_INVALID:
364 return;
368 static inline void exprval_set_exception(exprval_t *val, HRESULT hres)
370 val->type = EXPRVAL_INVALID;
371 val->u.hres = hres;
374 static inline void exprval_set_disp_ref(exprval_t *ref, IDispatch *obj, DISPID id)
376 ref->type = EXPRVAL_IDREF;
377 IDispatch_AddRef(ref->u.idref.disp = obj);
378 ref->u.idref.id = id;
381 static inline jsval_t steal_ret(call_frame_t *frame)
383 jsval_t r = frame->ret;
384 frame->ret = jsval_undefined();
385 return r;
388 static inline void clear_acc(script_ctx_t *ctx)
390 jsval_release(ctx->acc);
391 ctx->acc = jsval_undefined();
394 static HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
396 scope_chain_t *new_scope;
398 new_scope = heap_alloc(sizeof(scope_chain_t));
399 if(!new_scope)
400 return E_OUTOFMEMORY;
402 new_scope->ref = 1;
404 IDispatch_AddRef(obj);
405 new_scope->jsobj = jsobj;
406 new_scope->obj = obj;
407 new_scope->frame = NULL;
408 new_scope->next = scope ? scope_addref(scope) : NULL;
410 *ret = new_scope;
411 return S_OK;
414 static void scope_pop(scope_chain_t **scope)
416 scope_chain_t *tmp;
418 tmp = *scope;
419 *scope = tmp->next;
420 scope_release(tmp);
423 void clear_ei(script_ctx_t *ctx)
425 memset(&ctx->ei.ei, 0, sizeof(ctx->ei.ei));
426 jsval_release(ctx->ei.val);
427 ctx->ei.val = jsval_undefined();
430 void scope_release(scope_chain_t *scope)
432 if(--scope->ref)
433 return;
435 if(scope->next)
436 scope_release(scope->next);
438 IDispatch_Release(scope->obj);
439 heap_free(scope);
442 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, const WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
444 IDispatchEx *dispex;
445 jsdisp_t *jsdisp;
446 BSTR bstr;
447 HRESULT hres;
449 jsdisp = iface_to_jsdisp(disp);
450 if(jsdisp) {
451 hres = jsdisp_get_id(jsdisp, name, flags, id);
452 jsdisp_release(jsdisp);
453 return hres;
456 if(name_bstr) {
457 bstr = name_bstr;
458 }else {
459 bstr = SysAllocString(name);
460 if(!bstr)
461 return E_OUTOFMEMORY;
464 *id = 0;
465 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
466 if(SUCCEEDED(hres)) {
467 hres = IDispatchEx_GetDispID(dispex, bstr, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
468 IDispatchEx_Release(dispex);
469 }else {
470 TRACE("using IDispatch\n");
471 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &bstr, 1, 0, id);
474 if(name_bstr != bstr)
475 SysFreeString(bstr);
476 return hres;
479 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
481 IObjectIdentity *identity;
482 IUnknown *unk1, *unk2;
483 HRESULT hres;
485 if(disp1 == disp2) {
486 *ret = TRUE;
487 return S_OK;
490 if(!disp1 || !disp2) {
491 *ret = FALSE;
492 return S_OK;
495 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
496 if(FAILED(hres))
497 return hres;
499 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
500 if(FAILED(hres)) {
501 IUnknown_Release(unk1);
502 return hres;
505 if(unk1 == unk2) {
506 *ret = TRUE;
507 }else {
508 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
509 if(SUCCEEDED(hres)) {
510 hres = IObjectIdentity_IsEqualObject(identity, unk2);
511 IObjectIdentity_Release(identity);
512 *ret = hres == S_OK;
513 }else {
514 *ret = FALSE;
518 IUnknown_Release(unk1);
519 IUnknown_Release(unk2);
520 return S_OK;
523 /* ECMA-262 3rd Edition 11.9.6 */
524 HRESULT jsval_strict_equal(jsval_t lval, jsval_t rval, BOOL *ret)
526 jsval_type_t type = jsval_type(lval);
528 TRACE("\n");
530 if(type != jsval_type(rval)) {
531 if(is_null_instance(lval))
532 *ret = is_null_instance(rval);
533 else
534 *ret = FALSE;
535 return S_OK;
538 switch(type) {
539 case JSV_UNDEFINED:
540 case JSV_NULL:
541 *ret = TRUE;
542 break;
543 case JSV_OBJECT:
544 return disp_cmp(get_object(lval), get_object(rval), ret);
545 case JSV_STRING:
546 *ret = jsstr_eq(get_string(lval), get_string(rval));
547 break;
548 case JSV_NUMBER:
549 *ret = get_number(lval) == get_number(rval);
550 break;
551 case JSV_BOOL:
552 *ret = !get_bool(lval) == !get_bool(rval);
553 break;
554 case JSV_VARIANT:
555 FIXME("VARIANT not implemented\n");
556 return E_NOTIMPL;
559 return S_OK;
563 * Transfers local variables from stack to variable object.
564 * It's slow, so we want to avoid it as much as possible.
566 static HRESULT detach_variable_object(script_ctx_t *ctx, call_frame_t *frame, BOOL from_release)
568 unsigned i;
569 HRESULT hres;
571 if(!frame->base_scope || !frame->base_scope->frame)
572 return S_OK;
574 TRACE("detaching %p\n", frame);
576 assert(frame == frame->base_scope->frame);
577 assert(frame->variable_obj == frame->base_scope->jsobj);
579 if(!from_release && !frame->arguments_obj) {
580 hres = setup_arguments_object(ctx, frame);
581 if(FAILED(hres))
582 return hres;
585 frame->base_scope->frame = NULL;
587 for(i = 0; i < frame->function->locals_cnt; i++) {
588 hres = jsdisp_propput_name(frame->variable_obj, frame->function->locals[i].name,
589 ctx->stack[local_off(frame, frame->function->locals[i].ref)]);
590 if(FAILED(hres))
591 return hres;
594 return S_OK;
597 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
599 named_item_t *item;
600 DISPID id;
601 HRESULT hres;
603 for(item = ctx->named_items; item; item = item->next) {
604 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
605 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
606 if(SUCCEEDED(hres)) {
607 if(ret)
608 exprval_set_disp_ref(ret, item->disp, id);
609 return TRUE;
614 return FALSE;
617 static int local_ref_cmp(const void *key, const void *ref)
619 return strcmpW((const WCHAR*)key, ((const local_ref_t*)ref)->name);
622 local_ref_t *lookup_local(const function_code_t *function, const WCHAR *identifier)
624 return bsearch(identifier, function->locals, function->locals_cnt, sizeof(*function->locals), local_ref_cmp);
627 /* ECMA-262 3rd Edition 10.1.4 */
628 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
630 scope_chain_t *scope;
631 named_item_t *item;
632 DISPID id = 0;
633 HRESULT hres;
635 TRACE("%s\n", debugstr_w(identifier));
637 if(ctx->call_ctx) {
638 for(scope = ctx->call_ctx->scope; scope; scope = scope->next) {
639 if(scope->frame) {
640 function_code_t *func = scope->frame->function;
641 local_ref_t *ref = lookup_local(func, identifier);
642 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
644 if(ref) {
645 ret->type = EXPRVAL_STACK_REF;
646 ret->u.off = local_off(scope->frame, ref->ref);
647 TRACE("returning ref %d for %d\n", ret->u.off, ref->ref);
648 return S_OK;
651 if(!strcmpW(identifier, argumentsW)) {
652 hres = detach_variable_object(ctx, scope->frame, FALSE);
653 if(FAILED(hres))
654 return hres;
657 if(scope->jsobj)
658 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
659 else
660 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
661 if(SUCCEEDED(hres)) {
662 exprval_set_disp_ref(ret, scope->obj, id);
663 return S_OK;
668 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
669 if(SUCCEEDED(hres)) {
670 exprval_set_disp_ref(ret, to_disp(ctx->global), id);
671 return S_OK;
674 for(item = ctx->named_items; item; item = item->next) {
675 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
676 if(!item->disp) {
677 IUnknown *unk;
679 if(!ctx->site)
680 break;
682 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
683 SCRIPTINFO_IUNKNOWN, &unk, NULL);
684 if(FAILED(hres)) {
685 WARN("GetItemInfo failed: %08x\n", hres);
686 break;
689 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
690 IUnknown_Release(unk);
691 if(FAILED(hres)) {
692 WARN("object does not implement IDispatch\n");
693 break;
697 IDispatch_AddRef(item->disp);
698 ret->type = EXPRVAL_JSVAL;
699 ret->u.val = jsval_disp(item->disp);
700 return S_OK;
704 if(lookup_global_members(ctx, identifier, ret))
705 return S_OK;
707 exprval_set_exception(ret, JS_E_UNDEFINED_VARIABLE);
708 return S_OK;
711 static inline BSTR get_op_bstr(script_ctx_t *ctx, int i)
713 call_frame_t *frame = ctx->call_ctx;
714 return frame->bytecode->instrs[frame->ip].u.arg[i].bstr;
717 static inline unsigned get_op_uint(script_ctx_t *ctx, int i)
719 call_frame_t *frame = ctx->call_ctx;
720 return frame->bytecode->instrs[frame->ip].u.arg[i].uint;
723 static inline unsigned get_op_int(script_ctx_t *ctx, int i)
725 call_frame_t *frame = ctx->call_ctx;
726 return frame->bytecode->instrs[frame->ip].u.arg[i].lng;
729 static inline jsstr_t *get_op_str(script_ctx_t *ctx, int i)
731 call_frame_t *frame = ctx->call_ctx;
732 return frame->bytecode->instrs[frame->ip].u.arg[i].str;
735 static inline double get_op_double(script_ctx_t *ctx)
737 call_frame_t *frame = ctx->call_ctx;
738 return frame->bytecode->instrs[frame->ip].u.dbl;
741 static inline void jmp_next(script_ctx_t *ctx)
743 ctx->call_ctx->ip++;
746 static inline void jmp_abs(script_ctx_t *ctx, unsigned dst)
748 ctx->call_ctx->ip = dst;
751 /* ECMA-262 3rd Edition 12.6.4 */
752 static HRESULT interp_forin(script_ctx_t *ctx)
754 const HRESULT arg = get_op_uint(ctx, 0);
755 IDispatch *obj = NULL;
756 IDispatchEx *dispex;
757 exprval_t prop_ref;
758 DISPID id;
759 BSTR name = NULL;
760 HRESULT hres;
762 TRACE("\n");
764 assert(is_number(stack_top(ctx)));
765 id = get_number(stack_top(ctx));
767 if(!stack_topn_exprval(ctx, 1, &prop_ref)) {
768 FIXME("invalid ref: %08x\n", prop_ref.u.hres);
769 return E_FAIL;
772 if(is_object_instance(stack_topn(ctx, 3)))
773 obj = get_object(stack_topn(ctx, 3));
775 if(obj) {
776 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
777 if(SUCCEEDED(hres)) {
778 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
779 if(hres == S_OK)
780 hres = IDispatchEx_GetMemberName(dispex, id, &name);
781 IDispatchEx_Release(dispex);
782 if(FAILED(hres))
783 return hres;
784 }else {
785 TRACE("No IDispatchEx\n");
789 if(name) {
790 jsstr_t *str;
792 str = jsstr_alloc_len(name, SysStringLen(name));
793 SysFreeString(name);
794 if(!str)
795 return E_OUTOFMEMORY;
797 stack_pop(ctx);
798 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
800 hres = exprval_propput(ctx, &prop_ref, jsval_string(str));
801 jsstr_release(str);
802 if(FAILED(hres))
803 return hres;
805 jmp_next(ctx);
806 }else {
807 stack_popn(ctx, 4);
808 jmp_abs(ctx, arg);
810 return S_OK;
813 /* ECMA-262 3rd Edition 12.10 */
814 static HRESULT interp_push_scope(script_ctx_t *ctx)
816 IDispatch *disp;
817 jsval_t v;
818 HRESULT hres;
820 TRACE("\n");
822 v = stack_pop(ctx);
823 hres = to_object(ctx, v, &disp);
824 jsval_release(v);
825 if(FAILED(hres))
826 return hres;
828 hres = scope_push(ctx->call_ctx->scope, to_jsdisp(disp), disp, &ctx->call_ctx->scope);
829 IDispatch_Release(disp);
830 return hres;
833 /* ECMA-262 3rd Edition 12.10 */
834 static HRESULT interp_pop_scope(script_ctx_t *ctx)
836 TRACE("\n");
838 scope_pop(&ctx->call_ctx->scope);
839 return S_OK;
842 /* ECMA-262 3rd Edition 12.13 */
843 static HRESULT interp_case(script_ctx_t *ctx)
845 const unsigned arg = get_op_uint(ctx, 0);
846 jsval_t v;
847 BOOL b;
848 HRESULT hres;
850 TRACE("\n");
852 v = stack_pop(ctx);
853 hres = jsval_strict_equal(stack_top(ctx), v, &b);
854 jsval_release(v);
855 if(FAILED(hres))
856 return hres;
858 if(b) {
859 stack_popn(ctx, 1);
860 jmp_abs(ctx, arg);
861 }else {
862 jmp_next(ctx);
864 return S_OK;
867 /* ECMA-262 3rd Edition 12.13 */
868 static HRESULT interp_throw(script_ctx_t *ctx)
870 TRACE("\n");
872 jsval_release(ctx->ei.val);
873 ctx->ei.val = stack_pop(ctx);
874 return DISP_E_EXCEPTION;
877 static HRESULT interp_throw_ref(script_ctx_t *ctx)
879 const HRESULT arg = get_op_uint(ctx, 0);
881 TRACE("%08x\n", arg);
883 return throw_reference_error(ctx, arg, NULL);
886 static HRESULT interp_throw_type(script_ctx_t *ctx)
888 const HRESULT hres = get_op_uint(ctx, 0);
889 jsstr_t *str = get_op_str(ctx, 1);
890 const WCHAR *ptr;
892 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
894 ptr = jsstr_flatten(str);
895 return ptr ? throw_type_error(ctx, hres, ptr) : E_OUTOFMEMORY;
898 /* ECMA-262 3rd Edition 12.14 */
899 static HRESULT interp_push_except(script_ctx_t *ctx)
901 const unsigned catch_off = get_op_uint(ctx, 0);
902 const unsigned finally_off = get_op_uint(ctx, 1);
903 call_frame_t *frame = ctx->call_ctx;
904 except_frame_t *except;
906 TRACE("\n");
908 except = heap_alloc(sizeof(*except));
909 if(!except)
910 return E_OUTOFMEMORY;
912 except->stack_top = ctx->stack_top;
913 except->scope = frame->scope;
914 except->catch_off = catch_off;
915 except->finally_off = finally_off;
916 except->next = frame->except_frame;
917 frame->except_frame = except;
918 return S_OK;
921 /* ECMA-262 3rd Edition 12.14 */
922 static HRESULT interp_pop_except(script_ctx_t *ctx)
924 const unsigned ret_off = get_op_uint(ctx, 0);
925 call_frame_t *frame = ctx->call_ctx;
926 except_frame_t *except;
927 unsigned finally_off;
929 TRACE("%u\n", ret_off);
931 except = frame->except_frame;
932 assert(except != NULL);
934 finally_off = except->finally_off;
935 frame->except_frame = except->next;
936 heap_free(except);
938 if(finally_off) {
939 HRESULT hres;
941 hres = stack_push(ctx, jsval_number(ret_off));
942 if(FAILED(hres))
943 return hres;
944 hres = stack_push(ctx, jsval_bool(TRUE));
945 if(FAILED(hres))
946 return hres;
947 frame->ip = finally_off;
948 }else {
949 frame->ip = ret_off;
952 return S_OK;
955 /* ECMA-262 3rd Edition 12.14 */
956 static HRESULT interp_end_finally(script_ctx_t *ctx)
958 call_frame_t *frame = ctx->call_ctx;
959 jsval_t v;
961 TRACE("\n");
963 v = stack_pop(ctx);
964 assert(is_bool(v));
966 if(!get_bool(v)) {
967 TRACE("passing exception\n");
969 ctx->ei.val = stack_pop(ctx);
970 return DISP_E_EXCEPTION;
973 v = stack_pop(ctx);
974 assert(is_number(v));
975 frame->ip = get_number(v);
976 return S_OK;
979 static HRESULT interp_enter_catch(script_ctx_t *ctx)
981 const BSTR ident = get_op_bstr(ctx, 0);
982 jsdisp_t *scope_obj;
983 jsval_t v;
984 HRESULT hres;
986 hres = create_dispex(ctx, NULL, NULL, &scope_obj);
987 if(FAILED(hres))
988 return hres;
990 v = stack_pop(ctx);
991 hres = jsdisp_propput_name(scope_obj, ident, v);
992 jsval_release(v);
993 if(SUCCEEDED(hres))
994 hres = scope_push(ctx->call_ctx->scope, scope_obj, to_disp(scope_obj), &ctx->call_ctx->scope);
995 jsdisp_release(scope_obj);
996 return hres;
999 /* ECMA-262 3rd Edition 13 */
1000 static HRESULT interp_func(script_ctx_t *ctx)
1002 unsigned func_idx = get_op_uint(ctx, 0);
1003 call_frame_t *frame = ctx->call_ctx;
1004 jsdisp_t *dispex;
1005 HRESULT hres;
1007 TRACE("%d\n", func_idx);
1009 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+func_idx,
1010 frame->scope, &dispex);
1011 if(FAILED(hres))
1012 return hres;
1014 return stack_push(ctx, jsval_obj(dispex));
1017 /* ECMA-262 3rd Edition 11.2.1 */
1018 static HRESULT interp_array(script_ctx_t *ctx)
1020 jsstr_t *name_str;
1021 const WCHAR *name;
1022 jsval_t v, namev;
1023 IDispatch *obj;
1024 DISPID id;
1025 HRESULT hres;
1027 TRACE("\n");
1029 namev = stack_pop(ctx);
1031 hres = stack_pop_object(ctx, &obj);
1032 if(FAILED(hres)) {
1033 jsval_release(namev);
1034 return hres;
1037 hres = to_flat_string(ctx, namev, &name_str, &name);
1038 jsval_release(namev);
1039 if(FAILED(hres)) {
1040 IDispatch_Release(obj);
1041 return hres;
1044 hres = disp_get_id(ctx, obj, name, NULL, 0, &id);
1045 jsstr_release(name_str);
1046 if(SUCCEEDED(hres)) {
1047 hres = disp_propget(ctx, obj, id, &v);
1048 }else if(hres == DISP_E_UNKNOWNNAME) {
1049 v = jsval_undefined();
1050 hres = S_OK;
1052 IDispatch_Release(obj);
1053 if(FAILED(hres))
1054 return hres;
1056 return stack_push(ctx, v);
1059 /* ECMA-262 3rd Edition 11.2.1 */
1060 static HRESULT interp_member(script_ctx_t *ctx)
1062 const BSTR arg = get_op_bstr(ctx, 0);
1063 IDispatch *obj;
1064 jsval_t v;
1065 DISPID id;
1066 HRESULT hres;
1068 TRACE("\n");
1070 hres = stack_pop_object(ctx, &obj);
1071 if(FAILED(hres))
1072 return hres;
1074 hres = disp_get_id(ctx, obj, arg, arg, 0, &id);
1075 if(SUCCEEDED(hres)) {
1076 hres = disp_propget(ctx, obj, id, &v);
1077 }else if(hres == DISP_E_UNKNOWNNAME) {
1078 v = jsval_undefined();
1079 hres = S_OK;
1081 IDispatch_Release(obj);
1082 if(FAILED(hres))
1083 return hres;
1085 return stack_push(ctx, v);
1088 /* ECMA-262 3rd Edition 11.2.1 */
1089 static HRESULT interp_memberid(script_ctx_t *ctx)
1091 const unsigned arg = get_op_uint(ctx, 0);
1092 jsval_t objv, namev;
1093 const WCHAR *name;
1094 jsstr_t *name_str;
1095 IDispatch *obj;
1096 exprval_t ref;
1097 DISPID id;
1098 HRESULT hres;
1100 TRACE("%x\n", arg);
1102 namev = stack_pop(ctx);
1103 objv = stack_pop(ctx);
1105 hres = to_object(ctx, objv, &obj);
1106 jsval_release(objv);
1107 if(SUCCEEDED(hres)) {
1108 hres = to_flat_string(ctx, namev, &name_str, &name);
1109 if(FAILED(hres))
1110 IDispatch_Release(obj);
1112 jsval_release(namev);
1113 if(FAILED(hres))
1114 return hres;
1116 hres = disp_get_id(ctx, obj, name, NULL, arg, &id);
1117 jsstr_release(name_str);
1118 if(SUCCEEDED(hres)) {
1119 ref.type = EXPRVAL_IDREF;
1120 ref.u.idref.disp = obj;
1121 ref.u.idref.id = id;
1122 }else {
1123 IDispatch_Release(obj);
1124 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1125 exprval_set_exception(&ref, JS_E_INVALID_PROPERTY);
1126 hres = S_OK;
1127 }else {
1128 ERR("failed %08x\n", hres);
1129 return hres;
1133 return stack_push_exprval(ctx, &ref);
1136 /* ECMA-262 3rd Edition 11.2.1 */
1137 static HRESULT interp_refval(script_ctx_t *ctx)
1139 exprval_t ref;
1140 jsval_t v;
1141 HRESULT hres;
1143 TRACE("\n");
1145 if(!stack_topn_exprval(ctx, 0, &ref))
1146 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
1148 hres = exprval_propget(ctx, &ref, &v);
1149 if(FAILED(hres))
1150 return hres;
1152 return stack_push(ctx, v);
1155 /* ECMA-262 3rd Edition 11.2.2 */
1156 static HRESULT interp_new(script_ctx_t *ctx)
1158 const unsigned argc = get_op_uint(ctx, 0);
1159 jsval_t constr;
1161 TRACE("%d\n", argc);
1163 constr = stack_topn(ctx, argc);
1165 /* NOTE: Should use to_object here */
1167 if(is_null(constr))
1168 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
1169 else if(!is_object_instance(constr))
1170 return throw_type_error(ctx, JS_E_INVALID_ACTION, NULL);
1171 else if(!get_object(constr))
1172 return throw_type_error(ctx, JS_E_INVALID_PROPERTY, NULL);
1174 clear_acc(ctx);
1175 return disp_call_value(ctx, get_object(constr), NULL, DISPATCH_CONSTRUCT | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1176 argc, stack_args(ctx, argc), &ctx->acc);
1179 /* ECMA-262 3rd Edition 11.2.3 */
1180 static HRESULT interp_call(script_ctx_t *ctx)
1182 const unsigned argn = get_op_uint(ctx, 0);
1183 const int do_ret = get_op_int(ctx, 1);
1184 jsval_t obj;
1186 TRACE("%d %d\n", argn, do_ret);
1188 obj = stack_topn(ctx, argn);
1189 if(!is_object_instance(obj))
1190 return throw_type_error(ctx, JS_E_INVALID_PROPERTY, NULL);
1192 clear_acc(ctx);
1193 return disp_call_value(ctx, get_object(obj), NULL, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1194 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1197 /* ECMA-262 3rd Edition 11.2.3 */
1198 static HRESULT interp_call_member(script_ctx_t *ctx)
1200 const unsigned argn = get_op_uint(ctx, 0);
1201 const int do_ret = get_op_int(ctx, 1);
1202 exprval_t ref;
1204 TRACE("%d %d\n", argn, do_ret);
1206 if(!stack_topn_exprval(ctx, argn, &ref))
1207 return throw_type_error(ctx, ref.u.hres, NULL);
1209 clear_acc(ctx);
1210 return exprval_call(ctx, &ref, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1211 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1214 /* ECMA-262 3rd Edition 11.1.1 */
1215 static HRESULT interp_this(script_ctx_t *ctx)
1217 call_frame_t *frame = ctx->call_ctx;
1219 TRACE("\n");
1221 IDispatch_AddRef(frame->this_obj);
1222 return stack_push(ctx, jsval_disp(frame->this_obj));
1225 static HRESULT interp_identifier_ref(script_ctx_t *ctx, BSTR identifier, unsigned flags)
1227 exprval_t exprval;
1228 HRESULT hres;
1230 hres = identifier_eval(ctx, identifier, &exprval);
1231 if(FAILED(hres))
1232 return hres;
1234 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1235 DISPID id;
1237 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
1238 if(FAILED(hres))
1239 return hres;
1241 exprval_set_disp_ref(&exprval, to_disp(ctx->global), id);
1244 if(exprval.type == EXPRVAL_JSVAL || exprval.type == EXPRVAL_INVALID) {
1245 WARN("invalid ref\n");
1246 exprval_release(&exprval);
1247 exprval_set_exception(&exprval, JS_E_OBJECT_EXPECTED);
1250 return stack_push_exprval(ctx, &exprval);
1253 static HRESULT identifier_value(script_ctx_t *ctx, BSTR identifier)
1255 exprval_t exprval;
1256 jsval_t v;
1257 HRESULT hres;
1259 hres = identifier_eval(ctx, identifier, &exprval);
1260 if(FAILED(hres))
1261 return hres;
1263 if(exprval.type == EXPRVAL_INVALID)
1264 return throw_type_error(ctx, exprval.u.hres, identifier);
1266 hres = exprval_to_value(ctx, &exprval, &v);
1267 if(FAILED(hres))
1268 return hres;
1270 return stack_push(ctx, v);
1273 static HRESULT interp_local_ref(script_ctx_t *ctx)
1275 const int arg = get_op_int(ctx, 0);
1276 const unsigned flags = get_op_uint(ctx, 1);
1277 call_frame_t *frame = ctx->call_ctx;
1278 exprval_t ref;
1280 TRACE("%d\n", arg);
1282 if(!frame->base_scope || !frame->base_scope->frame)
1283 return interp_identifier_ref(ctx, local_name(frame, arg), flags);
1285 ref.type = EXPRVAL_STACK_REF;
1286 ref.u.off = local_off(frame, arg);
1287 return stack_push_exprval(ctx, &ref);
1290 static HRESULT interp_local(script_ctx_t *ctx)
1292 const int arg = get_op_int(ctx, 0);
1293 call_frame_t *frame = ctx->call_ctx;
1294 jsval_t copy;
1295 HRESULT hres;
1297 TRACE("%d: %s\n", arg, debugstr_w(local_name(frame, arg)));
1299 if(!frame->base_scope || !frame->base_scope->frame)
1300 return identifier_value(ctx, local_name(frame, arg));
1302 hres = jsval_copy(ctx->stack[local_off(frame, arg)], &copy);
1303 if(FAILED(hres))
1304 return hres;
1306 return stack_push(ctx, copy);
1309 /* ECMA-262 3rd Edition 10.1.4 */
1310 static HRESULT interp_ident(script_ctx_t *ctx)
1312 const BSTR arg = get_op_bstr(ctx, 0);
1314 TRACE("%s\n", debugstr_w(arg));
1316 return identifier_value(ctx, arg);
1319 /* ECMA-262 3rd Edition 10.1.4 */
1320 static HRESULT interp_identid(script_ctx_t *ctx)
1322 const BSTR arg = get_op_bstr(ctx, 0);
1323 const unsigned flags = get_op_uint(ctx, 1);
1325 TRACE("%s %x\n", debugstr_w(arg), flags);
1327 return interp_identifier_ref(ctx, arg, flags);
1330 /* ECMA-262 3rd Edition 7.8.1 */
1331 static HRESULT interp_null(script_ctx_t *ctx)
1333 TRACE("\n");
1335 return stack_push(ctx, jsval_null());
1338 /* ECMA-262 3rd Edition 7.8.2 */
1339 static HRESULT interp_bool(script_ctx_t *ctx)
1341 const int arg = get_op_int(ctx, 0);
1343 TRACE("%s\n", arg ? "true" : "false");
1345 return stack_push(ctx, jsval_bool(arg));
1348 /* ECMA-262 3rd Edition 7.8.3 */
1349 static HRESULT interp_int(script_ctx_t *ctx)
1351 const int arg = get_op_int(ctx, 0);
1353 TRACE("%d\n", arg);
1355 return stack_push(ctx, jsval_number(arg));
1358 /* ECMA-262 3rd Edition 7.8.3 */
1359 static HRESULT interp_double(script_ctx_t *ctx)
1361 const double arg = get_op_double(ctx);
1363 TRACE("%lf\n", arg);
1365 return stack_push(ctx, jsval_number(arg));
1368 /* ECMA-262 3rd Edition 7.8.4 */
1369 static HRESULT interp_str(script_ctx_t *ctx)
1371 jsstr_t *str = get_op_str(ctx, 0);
1373 TRACE("%s\n", debugstr_jsstr(str));
1375 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1378 /* ECMA-262 3rd Edition 7.8 */
1379 static HRESULT interp_regexp(script_ctx_t *ctx)
1381 jsstr_t *source = get_op_str(ctx, 0);
1382 const unsigned flags = get_op_uint(ctx, 1);
1383 jsdisp_t *regexp;
1384 HRESULT hres;
1386 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1388 hres = create_regexp(ctx, source, flags, &regexp);
1389 if(FAILED(hres))
1390 return hres;
1392 return stack_push(ctx, jsval_obj(regexp));
1395 /* ECMA-262 3rd Edition 11.1.4 */
1396 static HRESULT interp_carray(script_ctx_t *ctx)
1398 const unsigned arg = get_op_uint(ctx, 0);
1399 jsdisp_t *array;
1400 HRESULT hres;
1402 TRACE("%u\n", arg);
1404 hres = create_array(ctx, arg, &array);
1405 if(FAILED(hres))
1406 return hres;
1408 return stack_push(ctx, jsval_obj(array));
1411 static HRESULT interp_carray_set(script_ctx_t *ctx)
1413 const unsigned index = get_op_uint(ctx, 0);
1414 jsval_t value, array;
1415 HRESULT hres;
1417 value = stack_pop(ctx);
1419 TRACE("[%u] = %s\n", index, debugstr_jsval(value));
1421 array = stack_top(ctx);
1422 assert(is_object_instance(array));
1424 hres = jsdisp_propput_idx(iface_to_jsdisp(get_object(array)), index, value);
1425 jsval_release(value);
1426 return hres;
1429 /* ECMA-262 3rd Edition 11.1.5 */
1430 static HRESULT interp_new_obj(script_ctx_t *ctx)
1432 jsdisp_t *obj;
1433 HRESULT hres;
1435 TRACE("\n");
1437 hres = create_object(ctx, NULL, &obj);
1438 if(FAILED(hres))
1439 return hres;
1441 return stack_push(ctx, jsval_obj(obj));
1444 /* ECMA-262 3rd Edition 11.1.5 */
1445 static HRESULT interp_obj_prop(script_ctx_t *ctx)
1447 const BSTR name = get_op_bstr(ctx, 0);
1448 unsigned type = get_op_uint(ctx, 1);
1449 jsdisp_t *obj;
1450 jsval_t val;
1451 HRESULT hres;
1453 TRACE("%s\n", debugstr_w(name));
1455 val = stack_pop(ctx);
1457 assert(is_object_instance(stack_top(ctx)));
1458 obj = as_jsdisp(get_object(stack_top(ctx)));
1460 if(type == PROPERTY_DEFINITION_VALUE) {
1461 hres = jsdisp_propput_name(obj, name, val);
1462 }else {
1463 property_desc_t desc = {PROPF_ENUMERABLE | PROPF_CONFIGURABLE};
1464 jsdisp_t *func;
1466 assert(is_object_instance(val));
1467 func = iface_to_jsdisp(get_object(val));
1469 desc.mask = desc.flags;
1470 if(type == PROPERTY_DEFINITION_GETTER) {
1471 desc.explicit_getter = TRUE;
1472 desc.getter = func;
1473 }else {
1474 desc.explicit_setter = TRUE;
1475 desc.setter = func;
1478 hres = jsdisp_define_property(obj, name, &desc);
1479 jsdisp_release(func);
1482 jsval_release(val);
1483 return hres;
1486 /* ECMA-262 3rd Edition 11.11 */
1487 static HRESULT interp_cnd_nz(script_ctx_t *ctx)
1489 const unsigned arg = get_op_uint(ctx, 0);
1490 BOOL b;
1491 HRESULT hres;
1493 TRACE("\n");
1495 hres = to_boolean(stack_top(ctx), &b);
1496 if(FAILED(hres))
1497 return hres;
1499 if(b) {
1500 jmp_abs(ctx, arg);
1501 }else {
1502 stack_popn(ctx, 1);
1503 jmp_next(ctx);
1505 return S_OK;
1508 /* ECMA-262 3rd Edition 11.11 */
1509 static HRESULT interp_cnd_z(script_ctx_t *ctx)
1511 const unsigned arg = get_op_uint(ctx, 0);
1512 BOOL b;
1513 HRESULT hres;
1515 TRACE("\n");
1517 hres = to_boolean(stack_top(ctx), &b);
1518 if(FAILED(hres))
1519 return hres;
1521 if(b) {
1522 stack_popn(ctx, 1);
1523 jmp_next(ctx);
1524 }else {
1525 jmp_abs(ctx, arg);
1527 return S_OK;
1530 /* ECMA-262 3rd Edition 11.10 */
1531 static HRESULT interp_or(script_ctx_t *ctx)
1533 INT l, r;
1534 HRESULT hres;
1536 TRACE("\n");
1538 hres = stack_pop_int(ctx, &r);
1539 if(FAILED(hres))
1540 return hres;
1542 hres = stack_pop_int(ctx, &l);
1543 if(FAILED(hres))
1544 return hres;
1546 return stack_push(ctx, jsval_number(l|r));
1549 /* ECMA-262 3rd Edition 11.10 */
1550 static HRESULT interp_xor(script_ctx_t *ctx)
1552 INT l, r;
1553 HRESULT hres;
1555 TRACE("\n");
1557 hres = stack_pop_int(ctx, &r);
1558 if(FAILED(hres))
1559 return hres;
1561 hres = stack_pop_int(ctx, &l);
1562 if(FAILED(hres))
1563 return hres;
1565 return stack_push(ctx, jsval_number(l^r));
1568 /* ECMA-262 3rd Edition 11.10 */
1569 static HRESULT interp_and(script_ctx_t *ctx)
1571 INT l, r;
1572 HRESULT hres;
1574 TRACE("\n");
1576 hres = stack_pop_int(ctx, &r);
1577 if(FAILED(hres))
1578 return hres;
1580 hres = stack_pop_int(ctx, &l);
1581 if(FAILED(hres))
1582 return hres;
1584 return stack_push(ctx, jsval_number(l&r));
1587 /* ECMA-262 3rd Edition 11.8.6 */
1588 static HRESULT interp_instanceof(script_ctx_t *ctx)
1590 jsdisp_t *obj, *iter, *tmp = NULL;
1591 jsval_t prot, v;
1592 BOOL ret = FALSE;
1593 HRESULT hres;
1595 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1597 v = stack_pop(ctx);
1598 if(!is_object_instance(v) || !get_object(v)) {
1599 jsval_release(v);
1600 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1603 obj = iface_to_jsdisp(get_object(v));
1604 IDispatch_Release(get_object(v));
1605 if(!obj) {
1606 FIXME("non-jsdisp objects not supported\n");
1607 return E_FAIL;
1610 if(is_class(obj, JSCLASS_FUNCTION)) {
1611 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1612 }else {
1613 hres = throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1615 jsdisp_release(obj);
1616 if(FAILED(hres))
1617 return hres;
1619 v = stack_pop(ctx);
1621 if(is_object_instance(prot)) {
1622 if(is_object_instance(v))
1623 tmp = iface_to_jsdisp(get_object(v));
1624 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1625 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1626 if(FAILED(hres))
1627 break;
1630 if(tmp)
1631 jsdisp_release(tmp);
1632 }else {
1633 FIXME("prototype is not an object\n");
1634 hres = E_FAIL;
1637 jsval_release(prot);
1638 jsval_release(v);
1639 if(FAILED(hres))
1640 return hres;
1642 return stack_push(ctx, jsval_bool(ret));
1645 /* ECMA-262 3rd Edition 11.8.7 */
1646 static HRESULT interp_in(script_ctx_t *ctx)
1648 const WCHAR *str;
1649 jsstr_t *jsstr;
1650 jsval_t obj, v;
1651 DISPID id = 0;
1652 BOOL ret;
1653 HRESULT hres;
1655 TRACE("\n");
1657 obj = stack_pop(ctx);
1658 if(!is_object_instance(obj) || !get_object(obj)) {
1659 jsval_release(obj);
1660 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
1663 v = stack_pop(ctx);
1664 hres = to_flat_string(ctx, v, &jsstr, &str);
1665 jsval_release(v);
1666 if(FAILED(hres)) {
1667 IDispatch_Release(get_object(obj));
1668 return hres;
1671 hres = disp_get_id(ctx, get_object(obj), str, NULL, 0, &id);
1672 IDispatch_Release(get_object(obj));
1673 jsstr_release(jsstr);
1674 if(SUCCEEDED(hres))
1675 ret = TRUE;
1676 else if(hres == DISP_E_UNKNOWNNAME)
1677 ret = FALSE;
1678 else
1679 return hres;
1681 return stack_push(ctx, jsval_bool(ret));
1684 /* ECMA-262 3rd Edition 11.6.1 */
1685 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1687 jsval_t r, l;
1688 HRESULT hres;
1690 hres = to_primitive(ctx, lval, &l, NO_HINT);
1691 if(FAILED(hres))
1692 return hres;
1694 hres = to_primitive(ctx, rval, &r, NO_HINT);
1695 if(FAILED(hres)) {
1696 jsval_release(l);
1697 return hres;
1700 if(is_string(l) || is_string(r)) {
1701 jsstr_t *lstr, *rstr = NULL;
1703 hres = to_string(ctx, l, &lstr);
1704 if(SUCCEEDED(hres))
1705 hres = to_string(ctx, r, &rstr);
1707 if(SUCCEEDED(hres)) {
1708 jsstr_t *ret_str;
1710 ret_str = jsstr_concat(lstr, rstr);
1711 if(ret_str)
1712 *ret = jsval_string(ret_str);
1713 else
1714 hres = E_OUTOFMEMORY;
1717 jsstr_release(lstr);
1718 if(rstr)
1719 jsstr_release(rstr);
1720 }else {
1721 double nl, nr;
1723 hres = to_number(ctx, l, &nl);
1724 if(SUCCEEDED(hres)) {
1725 hres = to_number(ctx, r, &nr);
1726 if(SUCCEEDED(hres))
1727 *ret = jsval_number(nl+nr);
1731 jsval_release(r);
1732 jsval_release(l);
1733 return hres;
1736 /* ECMA-262 3rd Edition 11.6.1 */
1737 static HRESULT interp_add(script_ctx_t *ctx)
1739 jsval_t l, r, ret;
1740 HRESULT hres;
1742 r = stack_pop(ctx);
1743 l = stack_pop(ctx);
1745 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1747 hres = add_eval(ctx, l, r, &ret);
1748 jsval_release(l);
1749 jsval_release(r);
1750 if(FAILED(hres))
1751 return hres;
1753 return stack_push(ctx, ret);
1756 /* ECMA-262 3rd Edition 11.6.2 */
1757 static HRESULT interp_sub(script_ctx_t *ctx)
1759 double l, r;
1760 HRESULT hres;
1762 TRACE("\n");
1764 hres = stack_pop_number(ctx, &r);
1765 if(FAILED(hres))
1766 return hres;
1768 hres = stack_pop_number(ctx, &l);
1769 if(FAILED(hres))
1770 return hres;
1772 return stack_push(ctx, jsval_number(l-r));
1775 /* ECMA-262 3rd Edition 11.5.1 */
1776 static HRESULT interp_mul(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.2 */
1795 static HRESULT interp_div(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.3 */
1814 static HRESULT interp_mod(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(fmod(l, r)));
1832 /* ECMA-262 3rd Edition 11.4.2 */
1833 static HRESULT interp_delete(script_ctx_t *ctx)
1835 jsval_t objv, namev;
1836 IDispatch *obj;
1837 jsstr_t *name;
1838 BOOL ret;
1839 HRESULT hres;
1841 TRACE("\n");
1843 namev = stack_pop(ctx);
1844 objv = stack_pop(ctx);
1846 hres = to_object(ctx, objv, &obj);
1847 jsval_release(objv);
1848 if(FAILED(hres)) {
1849 jsval_release(namev);
1850 return hres;
1853 hres = to_string(ctx, namev, &name);
1854 jsval_release(namev);
1855 if(FAILED(hres)) {
1856 IDispatch_Release(obj);
1857 return hres;
1860 hres = disp_delete_name(ctx, obj, name, &ret);
1861 IDispatch_Release(obj);
1862 jsstr_release(name);
1863 if(FAILED(hres))
1864 return hres;
1866 return stack_push(ctx, jsval_bool(ret));
1869 /* ECMA-262 3rd Edition 11.4.2 */
1870 static HRESULT interp_delete_ident(script_ctx_t *ctx)
1872 const BSTR arg = get_op_bstr(ctx, 0);
1873 exprval_t exprval;
1874 BOOL ret;
1875 HRESULT hres;
1877 TRACE("%s\n", debugstr_w(arg));
1879 hres = identifier_eval(ctx, arg, &exprval);
1880 if(FAILED(hres))
1881 return hres;
1883 switch(exprval.type) {
1884 case EXPRVAL_STACK_REF:
1885 ret = FALSE;
1886 break;
1887 case EXPRVAL_IDREF:
1888 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1889 IDispatch_Release(exprval.u.idref.disp);
1890 if(FAILED(hres))
1891 return hres;
1892 break;
1893 case EXPRVAL_INVALID:
1894 ret = TRUE;
1895 break;
1896 default:
1897 FIXME("Unsupported exprval\n");
1898 exprval_release(&exprval);
1899 return E_NOTIMPL;
1903 return stack_push(ctx, jsval_bool(ret));
1906 /* ECMA-262 3rd Edition 11.4.2 */
1907 static HRESULT interp_void(script_ctx_t *ctx)
1909 TRACE("\n");
1911 stack_popn(ctx, 1);
1912 return stack_push(ctx, jsval_undefined());
1915 /* ECMA-262 3rd Edition 11.4.3 */
1916 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1918 switch(jsval_type(v)) {
1919 case JSV_UNDEFINED:
1920 *ret = undefinedW;
1921 break;
1922 case JSV_NULL:
1923 *ret = objectW;
1924 break;
1925 case JSV_OBJECT: {
1926 jsdisp_t *dispex;
1928 if(get_object(v) && (dispex = iface_to_jsdisp(get_object(v)))) {
1929 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1930 jsdisp_release(dispex);
1931 }else {
1932 *ret = objectW;
1934 break;
1936 case JSV_STRING:
1937 *ret = stringW;
1938 break;
1939 case JSV_NUMBER:
1940 *ret = numberW;
1941 break;
1942 case JSV_BOOL:
1943 *ret = booleanW;
1944 break;
1945 case JSV_VARIANT:
1946 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1947 return E_NOTIMPL;
1950 return S_OK;
1953 /* ECMA-262 3rd Edition 11.4.3 */
1954 static HRESULT interp_typeofid(script_ctx_t *ctx)
1956 const WCHAR *ret;
1957 exprval_t ref;
1958 jsval_t v;
1959 HRESULT hres;
1961 TRACE("\n");
1963 if(!stack_pop_exprval(ctx, &ref))
1964 return stack_push(ctx, jsval_string(jsstr_undefined()));
1966 hres = exprval_propget(ctx, &ref, &v);
1967 exprval_release(&ref);
1968 if(FAILED(hres))
1969 return stack_push_string(ctx, unknownW);
1971 hres = typeof_string(v, &ret);
1972 jsval_release(v);
1973 if(FAILED(hres))
1974 return hres;
1976 return stack_push_string(ctx, ret);
1979 /* ECMA-262 3rd Edition 11.4.3 */
1980 static HRESULT interp_typeofident(script_ctx_t *ctx)
1982 const BSTR arg = get_op_bstr(ctx, 0);
1983 exprval_t exprval;
1984 const WCHAR *ret;
1985 jsval_t v;
1986 HRESULT hres;
1988 TRACE("%s\n", debugstr_w(arg));
1990 hres = identifier_eval(ctx, arg, &exprval);
1991 if(FAILED(hres))
1992 return hres;
1994 if(exprval.type == EXPRVAL_INVALID)
1995 return stack_push(ctx, jsval_string(jsstr_undefined()));
1997 hres = exprval_to_value(ctx, &exprval, &v);
1998 if(FAILED(hres))
1999 return hres;
2001 hres = typeof_string(v, &ret);
2002 jsval_release(v);
2003 if(FAILED(hres))
2004 return hres;
2006 return stack_push_string(ctx, ret);
2009 /* ECMA-262 3rd Edition 11.4.3 */
2010 static HRESULT interp_typeof(script_ctx_t *ctx)
2012 const WCHAR *ret;
2013 jsval_t v;
2014 HRESULT hres;
2016 TRACE("\n");
2018 v = stack_pop(ctx);
2019 hres = typeof_string(v, &ret);
2020 jsval_release(v);
2021 if(FAILED(hres))
2022 return hres;
2024 return stack_push_string(ctx, ret);
2027 /* ECMA-262 3rd Edition 11.4.7 */
2028 static HRESULT interp_minus(script_ctx_t *ctx)
2030 double n;
2031 HRESULT hres;
2033 TRACE("\n");
2035 hres = stack_pop_number(ctx, &n);
2036 if(FAILED(hres))
2037 return hres;
2039 return stack_push(ctx, jsval_number(-n));
2042 /* ECMA-262 3rd Edition 11.4.6 */
2043 static HRESULT interp_tonum(script_ctx_t *ctx)
2045 jsval_t v;
2046 double n;
2047 HRESULT hres;
2049 TRACE("\n");
2051 v = stack_pop(ctx);
2052 hres = to_number(ctx, v, &n);
2053 jsval_release(v);
2054 if(FAILED(hres))
2055 return hres;
2057 return stack_push(ctx, jsval_number(n));
2060 /* ECMA-262 3rd Edition 11.3.1 */
2061 static HRESULT interp_postinc(script_ctx_t *ctx)
2063 const int arg = get_op_int(ctx, 0);
2064 exprval_t ref;
2065 jsval_t v;
2066 HRESULT hres;
2068 TRACE("%d\n", arg);
2070 if(!stack_pop_exprval(ctx, &ref))
2071 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
2073 hres = exprval_propget(ctx, &ref, &v);
2074 if(SUCCEEDED(hres)) {
2075 double n;
2077 hres = to_number(ctx, v, &n);
2078 if(SUCCEEDED(hres))
2079 hres = exprval_propput(ctx, &ref, jsval_number(n+(double)arg));
2080 if(FAILED(hres))
2081 jsval_release(v);
2083 exprval_release(&ref);
2084 if(FAILED(hres))
2085 return hres;
2087 return stack_push(ctx, v);
2090 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2091 static HRESULT interp_preinc(script_ctx_t *ctx)
2093 const int arg = get_op_int(ctx, 0);
2094 exprval_t ref;
2095 double ret;
2096 jsval_t v;
2097 HRESULT hres;
2099 TRACE("%d\n", arg);
2101 if(!stack_pop_exprval(ctx, &ref))
2102 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
2104 hres = exprval_propget(ctx, &ref, &v);
2105 if(SUCCEEDED(hres)) {
2106 double n;
2108 hres = to_number(ctx, v, &n);
2109 jsval_release(v);
2110 if(SUCCEEDED(hres)) {
2111 ret = n+(double)arg;
2112 hres = exprval_propput(ctx, &ref, jsval_number(ret));
2115 exprval_release(&ref);
2116 if(FAILED(hres))
2117 return hres;
2119 return stack_push(ctx, jsval_number(ret));
2122 /* ECMA-262 3rd Edition 11.9.3 */
2123 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
2125 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
2126 return jsval_strict_equal(lval, rval, ret);
2128 /* FIXME: NULL disps should be handled in more general way */
2129 if(is_object_instance(lval) && !get_object(lval))
2130 return equal_values(ctx, jsval_null(), rval, ret);
2131 if(is_object_instance(rval) && !get_object(rval))
2132 return equal_values(ctx, lval, jsval_null(), ret);
2134 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
2135 *ret = TRUE;
2136 return S_OK;
2139 if(is_string(lval) && is_number(rval)) {
2140 double n;
2141 HRESULT hres;
2143 hres = to_number(ctx, lval, &n);
2144 if(FAILED(hres))
2145 return hres;
2147 /* FIXME: optimize */
2148 return equal_values(ctx, jsval_number(n), rval, ret);
2151 if(is_string(rval) && is_number(lval)) {
2152 double n;
2153 HRESULT hres;
2155 hres = to_number(ctx, rval, &n);
2156 if(FAILED(hres))
2157 return hres;
2159 /* FIXME: optimize */
2160 return equal_values(ctx, lval, jsval_number(n), ret);
2163 if(is_bool(rval))
2164 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
2166 if(is_bool(lval))
2167 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
2170 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
2171 jsval_t prim;
2172 HRESULT hres;
2174 hres = to_primitive(ctx, rval, &prim, NO_HINT);
2175 if(FAILED(hres))
2176 return hres;
2178 hres = equal_values(ctx, lval, prim, ret);
2179 jsval_release(prim);
2180 return hres;
2184 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
2185 jsval_t prim;
2186 HRESULT hres;
2188 hres = to_primitive(ctx, lval, &prim, NO_HINT);
2189 if(FAILED(hres))
2190 return hres;
2192 hres = equal_values(ctx, prim, rval, ret);
2193 jsval_release(prim);
2194 return hres;
2198 *ret = FALSE;
2199 return S_OK;
2202 /* ECMA-262 3rd Edition 11.9.1 */
2203 static HRESULT interp_eq(script_ctx_t *ctx)
2205 jsval_t l, r;
2206 BOOL b;
2207 HRESULT hres;
2209 r = stack_pop(ctx);
2210 l = stack_pop(ctx);
2212 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
2214 hres = equal_values(ctx, l, r, &b);
2215 jsval_release(l);
2216 jsval_release(r);
2217 if(FAILED(hres))
2218 return hres;
2220 return stack_push(ctx, jsval_bool(b));
2223 /* ECMA-262 3rd Edition 11.9.2 */
2224 static HRESULT interp_neq(script_ctx_t *ctx)
2226 jsval_t l, r;
2227 BOOL b;
2228 HRESULT hres;
2230 r = stack_pop(ctx);
2231 l = stack_pop(ctx);
2233 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2235 hres = equal_values(ctx, l, r, &b);
2236 jsval_release(l);
2237 jsval_release(r);
2238 if(FAILED(hres))
2239 return hres;
2241 return stack_push(ctx, jsval_bool(!b));
2244 /* ECMA-262 3rd Edition 11.9.4 */
2245 static HRESULT interp_eq2(script_ctx_t *ctx)
2247 jsval_t l, r;
2248 BOOL b;
2249 HRESULT hres;
2251 r = stack_pop(ctx);
2252 l = stack_pop(ctx);
2254 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2256 hres = jsval_strict_equal(r, l, &b);
2257 jsval_release(l);
2258 jsval_release(r);
2259 if(FAILED(hres))
2260 return hres;
2262 return stack_push(ctx, jsval_bool(b));
2265 /* ECMA-262 3rd Edition 11.9.5 */
2266 static HRESULT interp_neq2(script_ctx_t *ctx)
2268 jsval_t l, r;
2269 BOOL b;
2270 HRESULT hres;
2272 TRACE("\n");
2274 r = stack_pop(ctx);
2275 l = stack_pop(ctx);
2277 hres = jsval_strict_equal(r, l, &b);
2278 jsval_release(l);
2279 jsval_release(r);
2280 if(FAILED(hres))
2281 return hres;
2283 return stack_push(ctx, jsval_bool(!b));
2286 /* ECMA-262 3rd Edition 11.8.5 */
2287 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2289 double ln, rn;
2290 jsval_t l, r;
2291 HRESULT hres;
2293 hres = to_primitive(ctx, lval, &l, NO_HINT);
2294 if(FAILED(hres))
2295 return hres;
2297 hres = to_primitive(ctx, rval, &r, NO_HINT);
2298 if(FAILED(hres)) {
2299 jsval_release(l);
2300 return hres;
2303 if(is_string(l) && is_string(r)) {
2304 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2305 jsstr_release(get_string(l));
2306 jsstr_release(get_string(r));
2307 return S_OK;
2310 hres = to_number(ctx, l, &ln);
2311 jsval_release(l);
2312 if(SUCCEEDED(hres))
2313 hres = to_number(ctx, r, &rn);
2314 jsval_release(r);
2315 if(FAILED(hres))
2316 return hres;
2318 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2319 return S_OK;
2322 /* ECMA-262 3rd Edition 11.8.1 */
2323 static HRESULT interp_lt(script_ctx_t *ctx)
2325 jsval_t l, r;
2326 BOOL b;
2327 HRESULT hres;
2329 r = stack_pop(ctx);
2330 l = stack_pop(ctx);
2332 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2334 hres = less_eval(ctx, l, r, FALSE, &b);
2335 jsval_release(l);
2336 jsval_release(r);
2337 if(FAILED(hres))
2338 return hres;
2340 return stack_push(ctx, jsval_bool(b));
2343 /* ECMA-262 3rd Edition 11.8.1 */
2344 static HRESULT interp_lteq(script_ctx_t *ctx)
2346 jsval_t l, r;
2347 BOOL b;
2348 HRESULT hres;
2350 r = stack_pop(ctx);
2351 l = stack_pop(ctx);
2353 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2355 hres = less_eval(ctx, r, l, TRUE, &b);
2356 jsval_release(l);
2357 jsval_release(r);
2358 if(FAILED(hres))
2359 return hres;
2361 return stack_push(ctx, jsval_bool(b));
2364 /* ECMA-262 3rd Edition 11.8.2 */
2365 static HRESULT interp_gt(script_ctx_t *ctx)
2367 jsval_t l, r;
2368 BOOL b;
2369 HRESULT hres;
2371 r = stack_pop(ctx);
2372 l = stack_pop(ctx);
2374 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2376 hres = less_eval(ctx, r, l, FALSE, &b);
2377 jsval_release(l);
2378 jsval_release(r);
2379 if(FAILED(hres))
2380 return hres;
2382 return stack_push(ctx, jsval_bool(b));
2385 /* ECMA-262 3rd Edition 11.8.4 */
2386 static HRESULT interp_gteq(script_ctx_t *ctx)
2388 jsval_t l, r;
2389 BOOL b;
2390 HRESULT hres;
2392 r = stack_pop(ctx);
2393 l = stack_pop(ctx);
2395 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2397 hres = less_eval(ctx, l, r, TRUE, &b);
2398 jsval_release(l);
2399 jsval_release(r);
2400 if(FAILED(hres))
2401 return hres;
2403 return stack_push(ctx, jsval_bool(b));
2406 /* ECMA-262 3rd Edition 11.4.8 */
2407 static HRESULT interp_bneg(script_ctx_t *ctx)
2409 jsval_t v;
2410 INT i;
2411 HRESULT hres;
2413 TRACE("\n");
2415 v = stack_pop(ctx);
2416 hres = to_int32(ctx, v, &i);
2417 jsval_release(v);
2418 if(FAILED(hres))
2419 return hres;
2421 return stack_push(ctx, jsval_number(~i));
2424 /* ECMA-262 3rd Edition 11.4.9 */
2425 static HRESULT interp_neg(script_ctx_t *ctx)
2427 jsval_t v;
2428 BOOL b;
2429 HRESULT hres;
2431 TRACE("\n");
2433 v = stack_pop(ctx);
2434 hres = to_boolean(v, &b);
2435 jsval_release(v);
2436 if(FAILED(hres))
2437 return hres;
2439 return stack_push(ctx, jsval_bool(!b));
2442 /* ECMA-262 3rd Edition 11.7.1 */
2443 static HRESULT interp_lshift(script_ctx_t *ctx)
2445 DWORD r;
2446 INT l;
2447 HRESULT hres;
2449 hres = stack_pop_uint(ctx, &r);
2450 if(FAILED(hres))
2451 return hres;
2453 hres = stack_pop_int(ctx, &l);
2454 if(FAILED(hres))
2455 return hres;
2457 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2460 /* ECMA-262 3rd Edition 11.7.2 */
2461 static HRESULT interp_rshift(script_ctx_t *ctx)
2463 DWORD r;
2464 INT l;
2465 HRESULT hres;
2467 hres = stack_pop_uint(ctx, &r);
2468 if(FAILED(hres))
2469 return hres;
2471 hres = stack_pop_int(ctx, &l);
2472 if(FAILED(hres))
2473 return hres;
2475 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2478 /* ECMA-262 3rd Edition 11.7.3 */
2479 static HRESULT interp_rshift2(script_ctx_t *ctx)
2481 DWORD r, l;
2482 HRESULT hres;
2484 hres = stack_pop_uint(ctx, &r);
2485 if(FAILED(hres))
2486 return hres;
2488 hres = stack_pop_uint(ctx, &l);
2489 if(FAILED(hres))
2490 return hres;
2492 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2495 /* ECMA-262 3rd Edition 11.13.1 */
2496 static HRESULT interp_assign(script_ctx_t *ctx)
2498 exprval_t ref;
2499 jsval_t v;
2500 HRESULT hres;
2502 TRACE("\n");
2504 v = stack_pop(ctx);
2506 if(!stack_pop_exprval(ctx, &ref)) {
2507 jsval_release(v);
2508 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
2511 hres = exprval_propput(ctx, &ref, v);
2512 exprval_release(&ref);
2513 if(FAILED(hres)) {
2514 jsval_release(v);
2515 return hres;
2518 return stack_push(ctx, v);
2521 /* JScript extension */
2522 static HRESULT interp_assign_call(script_ctx_t *ctx)
2524 const unsigned argc = get_op_uint(ctx, 0);
2525 exprval_t ref;
2526 jsval_t v;
2527 HRESULT hres;
2529 TRACE("%u\n", argc);
2531 if(!stack_topn_exprval(ctx, argc+1, &ref))
2532 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
2534 hres = exprval_call(ctx, &ref, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2535 if(FAILED(hres))
2536 return hres;
2538 v = stack_pop(ctx);
2539 stack_popn(ctx, argc+2);
2540 return stack_push(ctx, v);
2543 static HRESULT interp_undefined(script_ctx_t *ctx)
2545 TRACE("\n");
2547 return stack_push(ctx, jsval_undefined());
2550 static HRESULT interp_jmp(script_ctx_t *ctx)
2552 const unsigned arg = get_op_uint(ctx, 0);
2554 TRACE("%u\n", arg);
2556 jmp_abs(ctx, arg);
2557 return S_OK;
2560 static HRESULT interp_jmp_z(script_ctx_t *ctx)
2562 const unsigned arg = get_op_uint(ctx, 0);
2563 BOOL b;
2564 jsval_t v;
2565 HRESULT hres;
2567 TRACE("\n");
2569 v = stack_pop(ctx);
2570 hres = to_boolean(v, &b);
2571 jsval_release(v);
2572 if(FAILED(hres))
2573 return hres;
2575 if(b)
2576 jmp_next(ctx);
2577 else
2578 jmp_abs(ctx, arg);
2579 return S_OK;
2582 static HRESULT interp_pop(script_ctx_t *ctx)
2584 const unsigned arg = get_op_uint(ctx, 0);
2586 TRACE("%u\n", arg);
2588 stack_popn(ctx, arg);
2589 return S_OK;
2592 static HRESULT interp_ret(script_ctx_t *ctx)
2594 const unsigned clear_ret = get_op_uint(ctx, 0);
2595 call_frame_t *frame = ctx->call_ctx;
2597 TRACE("\n");
2599 if(clear_ret)
2600 jsval_release(steal_ret(frame));
2602 if((frame->flags & EXEC_CONSTRUCTOR) && !is_object_instance(frame->ret)) {
2603 jsval_release(frame->ret);
2604 IDispatch_AddRef(frame->this_obj);
2605 frame->ret = jsval_disp(frame->this_obj);
2608 jmp_abs(ctx, -1);
2609 return S_OK;
2612 static HRESULT interp_setret(script_ctx_t *ctx)
2614 call_frame_t *frame = ctx->call_ctx;
2616 TRACE("\n");
2618 jsval_release(frame->ret);
2619 frame->ret = stack_pop(ctx);
2620 return S_OK;
2623 static HRESULT interp_push_acc(script_ctx_t *ctx)
2625 HRESULT hres;
2627 TRACE("\n");
2629 hres = stack_push(ctx, ctx->acc);
2630 if(SUCCEEDED(hres))
2631 ctx->acc = jsval_undefined();
2632 return hres;
2635 typedef HRESULT (*op_func_t)(script_ctx_t*);
2637 static const op_func_t op_funcs[] = {
2638 #define X(x,a,b,c) interp_##x,
2639 OP_LIST
2640 #undef X
2643 static const unsigned op_move[] = {
2644 #define X(a,x,b,c) x,
2645 OP_LIST
2646 #undef X
2649 static void pop_call_frame(script_ctx_t *ctx)
2651 call_frame_t *frame = ctx->call_ctx;
2653 frame->stack_base -= frame->pop_locals + frame->pop_variables;
2655 assert(frame->scope == frame->base_scope);
2657 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2658 if(frame->scope && frame->scope->ref > 1) {
2659 HRESULT hres = detach_variable_object(ctx, frame, TRUE);
2660 if(FAILED(hres))
2661 ERR("Failed to detach variable object: %08x\n", hres);
2664 if(frame->arguments_obj)
2665 detach_arguments_object(frame->arguments_obj);
2666 if(frame->scope)
2667 scope_release(frame->scope);
2669 if(frame->pop_variables)
2670 stack_popn(ctx, frame->pop_variables);
2671 stack_popn(ctx, frame->pop_locals);
2673 ctx->call_ctx = frame->prev_frame;
2675 if(frame->function_instance)
2676 jsdisp_release(frame->function_instance);
2677 if(frame->variable_obj)
2678 jsdisp_release(frame->variable_obj);
2679 if(frame->this_obj)
2680 IDispatch_Release(frame->this_obj);
2681 jsval_release(frame->ret);
2682 release_bytecode(frame->bytecode);
2683 heap_free(frame);
2686 static void print_backtrace(script_ctx_t *ctx)
2688 unsigned depth = 0, i;
2689 call_frame_t *frame;
2691 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
2692 WARN("%u\t", depth);
2693 depth++;
2695 if(frame->this_obj && frame->this_obj != to_disp(ctx->global) && frame->this_obj != ctx->host_global)
2696 WARN("%p->", frame->this_obj);
2697 WARN("%s(", frame->function->name ? debugstr_w(frame->function->name) : "[unnamed]");
2698 if(frame->base_scope && frame->base_scope->frame) {
2699 for(i=0; i < frame->argc; i++) {
2700 if(i < frame->function->param_cnt)
2701 WARN("%s%s=%s", i ? ", " : "", debugstr_w(frame->function->params[i]),
2702 debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2703 else
2704 WARN("%s%s", i ? ", " : "", debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2706 }else {
2707 WARN("[detached frame]");
2709 WARN(")\n");
2711 if(!(frame->flags & EXEC_RETURN_TO_INTERP)) {
2712 WARN("%u\t[native code]\n", depth);
2713 depth++;
2718 static HRESULT unwind_exception(script_ctx_t *ctx, HRESULT exception_hres)
2720 except_frame_t *except_frame;
2721 call_frame_t *frame;
2722 jsval_t except_val;
2723 unsigned catch_off;
2724 HRESULT hres;
2726 if(WARN_ON(jscript)) {
2727 jsdisp_t *error_obj;
2728 jsval_t msg;
2730 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
2732 WARN("Exception %08x %s", exception_hres, debugstr_jsval(ctx->ei.val));
2733 if(jsval_type(ctx->ei.val) == JSV_OBJECT) {
2734 error_obj = to_jsdisp(get_object(ctx->ei.val));
2735 if(error_obj) {
2736 hres = jsdisp_propget_name(error_obj, messageW, &msg);
2737 if(SUCCEEDED(hres)) {
2738 WARN(" (message %s)", debugstr_jsval(msg));
2739 jsval_release(msg);
2743 WARN(" in:\n");
2745 print_backtrace(ctx);
2748 for(frame = ctx->call_ctx; !frame->except_frame; frame = ctx->call_ctx) {
2749 DWORD flags;
2751 while(frame->scope != frame->base_scope)
2752 scope_pop(&frame->scope);
2754 stack_popn(ctx, ctx->stack_top-frame->stack_base);
2756 flags = frame->flags;
2757 pop_call_frame(ctx);
2758 if(!(flags & EXEC_RETURN_TO_INTERP))
2759 return exception_hres;
2762 except_frame = frame->except_frame;
2763 catch_off = except_frame->catch_off;
2765 assert(except_frame->stack_top <= ctx->stack_top);
2766 stack_popn(ctx, ctx->stack_top - except_frame->stack_top);
2768 while(except_frame->scope != frame->scope)
2769 scope_pop(&frame->scope);
2771 frame->ip = catch_off ? catch_off : except_frame->finally_off;
2772 if(catch_off) assert(frame->bytecode->instrs[frame->ip].op == OP_enter_catch);
2774 except_val = ctx->ei.val;
2775 ctx->ei.val = jsval_undefined();
2776 clear_ei(ctx);
2778 /* keep current except_frame if we're entering catch block with finally block associated */
2779 if(catch_off && except_frame->finally_off) {
2780 except_frame->catch_off = 0;
2781 }else {
2782 frame->except_frame = except_frame->next;
2783 heap_free(except_frame);
2786 hres = stack_push(ctx, except_val);
2787 if(FAILED(hres))
2788 return hres;
2790 if(!catch_off)
2791 hres = stack_push(ctx, jsval_bool(FALSE));
2792 return hres;
2795 static HRESULT enter_bytecode(script_ctx_t *ctx, jsval_t *r)
2797 call_frame_t *frame;
2798 jsop_t op;
2799 HRESULT hres = S_OK;
2801 TRACE("\n");
2803 while(1) {
2804 frame = ctx->call_ctx;
2805 op = frame->bytecode->instrs[frame->ip].op;
2806 hres = op_funcs[op](ctx);
2807 if(FAILED(hres)) {
2808 hres = unwind_exception(ctx, hres);
2809 if(FAILED(hres))
2810 return hres;
2811 }else if(frame->ip == -1) {
2812 const DWORD return_to_interp = frame->flags & EXEC_RETURN_TO_INTERP;
2814 assert(ctx->stack_top == frame->stack_base);
2815 assert(frame->scope == frame->base_scope);
2817 if(return_to_interp) {
2818 jsval_release(ctx->acc);
2819 ctx->acc = steal_ret(frame);
2820 }else if(r) {
2821 *r = steal_ret(frame);
2823 pop_call_frame(ctx);
2824 if(!return_to_interp)
2825 break;
2826 }else {
2827 frame->ip += op_move[op];
2831 return S_OK;
2834 static HRESULT bind_event_target(script_ctx_t *ctx, function_code_t *func, jsdisp_t *func_obj)
2836 IBindEventHandler *target;
2837 exprval_t exprval;
2838 IDispatch *disp;
2839 jsval_t v;
2840 HRESULT hres;
2842 hres = identifier_eval(ctx, func->event_target, &exprval);
2843 if(FAILED(hres))
2844 return hres;
2846 hres = exprval_to_value(ctx, &exprval, &v);
2847 if(FAILED(hres))
2848 return hres;
2850 if(!is_object_instance(v)) {
2851 FIXME("Can't bind to %s\n", debugstr_jsval(v));
2852 jsval_release(v);
2855 disp = get_object(v);
2856 hres = IDispatch_QueryInterface(disp, &IID_IBindEventHandler, (void**)&target);
2857 if(SUCCEEDED(hres)) {
2858 hres = IBindEventHandler_BindHandler(target, func->name, (IDispatch*)&func_obj->IDispatchEx_iface);
2859 IBindEventHandler_Release(target);
2860 if(FAILED(hres))
2861 WARN("BindEvent failed: %08x\n", hres);
2862 }else {
2863 FIXME("No IBindEventHandler, not yet supported binding\n");
2866 IDispatch_Release(disp);
2867 return hres;
2870 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)
2872 const unsigned orig_stack = ctx->stack_top;
2873 scope_chain_t *scope;
2874 unsigned i;
2875 jsval_t v;
2876 HRESULT hres;
2878 /* If arguments are already on the stack, we may use them. */
2879 if(argv + argc == ctx->stack + ctx->stack_top) {
2880 frame->arguments_off = argv - ctx->stack;
2881 i = argc;
2882 }else {
2883 frame->arguments_off = ctx->stack_top;
2884 for(i = 0; i < argc; i++) {
2885 hres = jsval_copy(argv[i], &v);
2886 if(SUCCEEDED(hres))
2887 hres = stack_push(ctx, v);
2888 if(FAILED(hres)) {
2889 stack_popn(ctx, i);
2890 return hres;
2895 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2896 for(; i < frame->function->param_cnt; i++) {
2897 hres = stack_push(ctx, jsval_undefined());
2898 if(FAILED(hres)) {
2899 stack_popn(ctx, ctx->stack_top - orig_stack);
2900 return hres;
2904 frame->pop_locals = ctx->stack_top - orig_stack;
2906 frame->variables_off = ctx->stack_top;
2908 for(i = 0; i < frame->function->var_cnt; i++) {
2909 hres = stack_push(ctx, jsval_undefined());
2910 if(FAILED(hres)) {
2911 stack_popn(ctx, ctx->stack_top - orig_stack);
2912 return hres;
2916 frame->pop_variables = i;
2918 hres = scope_push(scope_chain, variable_object, to_disp(variable_object), &scope);
2919 if(FAILED(hres)) {
2920 stack_popn(ctx, ctx->stack_top - orig_stack);
2921 return hres;
2924 for(i = 0; i < frame->function->func_cnt; i++) {
2925 if(frame->function->funcs[i].name && !frame->function->funcs[i].event_target) {
2926 jsdisp_t *func_obj;
2927 unsigned off;
2929 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+i, scope, &func_obj);
2930 if(FAILED(hres)) {
2931 stack_popn(ctx, ctx->stack_top - orig_stack);
2932 scope_release(scope);
2933 return hres;
2936 off = local_off(frame, frame->function->funcs[i].local_ref);
2937 jsval_release(ctx->stack[off]);
2938 ctx->stack[off] = jsval_obj(func_obj);
2942 scope->frame = frame;
2943 frame->base_scope = frame->scope = scope;
2944 return S_OK;
2947 HRESULT exec_source(script_ctx_t *ctx, DWORD flags, bytecode_t *bytecode, function_code_t *function, scope_chain_t *scope,
2948 IDispatch *this_obj, jsdisp_t *function_instance, jsdisp_t *variable_obj, unsigned argc, jsval_t *argv, jsval_t *r)
2950 call_frame_t *frame;
2951 unsigned i;
2952 HRESULT hres;
2954 for(i = 0; i < function->func_cnt; i++) {
2955 jsdisp_t *func_obj;
2957 if(!function->funcs[i].event_target)
2958 continue;
2960 hres = create_source_function(ctx, bytecode, function->funcs+i, scope, &func_obj);
2961 if(FAILED(hres))
2962 return hres;
2964 hres = bind_event_target(ctx, function->funcs+i, func_obj);
2965 jsdisp_release(func_obj);
2966 if(FAILED(hres))
2967 return hres;
2970 if(flags & (EXEC_GLOBAL | EXEC_EVAL)) {
2971 for(i=0; i < function->var_cnt; i++) {
2972 TRACE("[%d] %s %d\n", i, debugstr_w(function->variables[i].name), function->variables[i].func_id);
2973 if(function->variables[i].func_id != -1) {
2974 jsdisp_t *func_obj;
2976 hres = create_source_function(ctx, bytecode, function->funcs+function->variables[i].func_id, scope, &func_obj);
2977 if(FAILED(hres))
2978 return hres;
2980 hres = jsdisp_propput_name(variable_obj, function->variables[i].name, jsval_obj(func_obj));
2981 jsdisp_release(func_obj);
2982 }else if(!(flags & EXEC_GLOBAL) || !lookup_global_members(ctx, function->variables[i].name, NULL)) {
2983 DISPID id = 0;
2985 hres = jsdisp_get_id(variable_obj, function->variables[i].name, fdexNameEnsure, &id);
2986 if(FAILED(hres))
2987 return hres;
2992 /* ECMA-262 3rd Edition 11.2.3.7 */
2993 if(this_obj) {
2994 jsdisp_t *jsthis;
2996 jsthis = iface_to_jsdisp(this_obj);
2997 if(jsthis) {
2998 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
2999 this_obj = NULL;
3000 jsdisp_release(jsthis);
3004 if(ctx->call_ctx && (flags & EXEC_EVAL)) {
3005 hres = detach_variable_object(ctx, ctx->call_ctx, FALSE);
3006 if(FAILED(hres))
3007 return hres;
3010 frame = heap_alloc_zero(sizeof(*frame));
3011 if(!frame)
3012 return E_OUTOFMEMORY;
3014 frame->function = function;
3015 frame->ret = jsval_undefined();
3016 frame->argc = argc;
3017 frame->bytecode = bytecode_addref(bytecode);
3019 if(!(flags & (EXEC_GLOBAL|EXEC_EVAL))) {
3020 hres = setup_scope(ctx, frame, scope, variable_obj, argc, argv);
3021 if(FAILED(hres)) {
3022 release_bytecode(frame->bytecode);
3023 heap_free(frame);
3024 return hres;
3026 }else if(scope) {
3027 frame->base_scope = frame->scope = scope_addref(scope);
3030 frame->ip = function->instr_off;
3031 frame->stack_base = ctx->stack_top;
3032 if(this_obj)
3033 frame->this_obj = this_obj;
3034 else if(ctx->host_global)
3035 frame->this_obj = ctx->host_global;
3036 else
3037 frame->this_obj = to_disp(ctx->global);
3038 IDispatch_AddRef(frame->this_obj);
3040 if(function_instance)
3041 frame->function_instance = jsdisp_addref(function_instance);
3043 frame->flags = flags;
3044 frame->variable_obj = jsdisp_addref(variable_obj);
3046 frame->prev_frame = ctx->call_ctx;
3047 ctx->call_ctx = frame;
3049 if(flags & EXEC_RETURN_TO_INTERP) {
3051 * We're called directly from interpreter, so we may just setup call frame and return.
3052 * Already running interpreter will take care of execution.
3054 if(r)
3055 *r = jsval_undefined();
3056 return S_OK;
3059 return enter_bytecode(ctx, r);