jscript: Replaced OP_identid with static binding when possible.
[wine.git] / dlls / jscript / engine.c
blob2f74d8a84af0cc16320c282a4bbcfa87011351fe
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 BSTR ident;
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_ret(call_frame_t *frame)
390 jsval_release(steal_ret(frame));
393 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
395 scope_chain_t *new_scope;
397 new_scope = heap_alloc(sizeof(scope_chain_t));
398 if(!new_scope)
399 return E_OUTOFMEMORY;
401 new_scope->ref = 1;
403 IDispatch_AddRef(obj);
404 new_scope->jsobj = jsobj;
405 new_scope->obj = obj;
406 new_scope->frame = NULL;
407 new_scope->next = scope ? scope_addref(scope) : NULL;
409 *ret = new_scope;
410 return S_OK;
413 static void scope_pop(scope_chain_t **scope)
415 scope_chain_t *tmp;
417 tmp = *scope;
418 *scope = tmp->next;
419 scope_release(tmp);
422 void clear_ei(script_ctx_t *ctx)
424 memset(&ctx->ei.ei, 0, sizeof(ctx->ei.ei));
425 jsval_release(ctx->ei.val);
426 ctx->ei.val = jsval_undefined();
429 void scope_release(scope_chain_t *scope)
431 if(--scope->ref)
432 return;
434 if(scope->next)
435 scope_release(scope->next);
437 IDispatch_Release(scope->obj);
438 heap_free(scope);
441 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, const WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
443 IDispatchEx *dispex;
444 jsdisp_t *jsdisp;
445 BSTR bstr;
446 HRESULT hres;
448 jsdisp = iface_to_jsdisp(disp);
449 if(jsdisp) {
450 hres = jsdisp_get_id(jsdisp, name, flags, id);
451 jsdisp_release(jsdisp);
452 return hres;
455 if(name_bstr) {
456 bstr = name_bstr;
457 }else {
458 bstr = SysAllocString(name);
459 if(!bstr)
460 return E_OUTOFMEMORY;
463 *id = 0;
464 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
465 if(SUCCEEDED(hres)) {
466 hres = IDispatchEx_GetDispID(dispex, bstr, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
467 IDispatchEx_Release(dispex);
468 }else {
469 TRACE("using IDispatch\n");
470 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &bstr, 1, 0, id);
473 if(name_bstr != bstr)
474 SysFreeString(bstr);
475 return hres;
478 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
480 IObjectIdentity *identity;
481 IUnknown *unk1, *unk2;
482 HRESULT hres;
484 if(disp1 == disp2) {
485 *ret = TRUE;
486 return S_OK;
489 if(!disp1 || !disp2) {
490 *ret = FALSE;
491 return S_OK;
494 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
495 if(FAILED(hres))
496 return hres;
498 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
499 if(FAILED(hres)) {
500 IUnknown_Release(unk1);
501 return hres;
504 if(unk1 == unk2) {
505 *ret = TRUE;
506 }else {
507 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
508 if(SUCCEEDED(hres)) {
509 hres = IObjectIdentity_IsEqualObject(identity, unk2);
510 IObjectIdentity_Release(identity);
511 *ret = hres == S_OK;
512 }else {
513 *ret = FALSE;
517 IUnknown_Release(unk1);
518 IUnknown_Release(unk2);
519 return S_OK;
522 /* ECMA-262 3rd Edition 11.9.6 */
523 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
525 jsval_type_t type = jsval_type(lval);
527 TRACE("\n");
529 if(type != jsval_type(rval)) {
530 if(is_null_instance(lval))
531 *ret = is_null_instance(rval);
532 else
533 *ret = FALSE;
534 return S_OK;
537 switch(type) {
538 case JSV_UNDEFINED:
539 case JSV_NULL:
540 *ret = TRUE;
541 break;
542 case JSV_OBJECT:
543 return disp_cmp(get_object(lval), get_object(rval), ret);
544 case JSV_STRING:
545 *ret = jsstr_eq(get_string(lval), get_string(rval));
546 break;
547 case JSV_NUMBER:
548 *ret = get_number(lval) == get_number(rval);
549 break;
550 case JSV_BOOL:
551 *ret = !get_bool(lval) == !get_bool(rval);
552 break;
553 case JSV_VARIANT:
554 FIXME("VARIANT not implemented\n");
555 return E_NOTIMPL;
558 return S_OK;
562 * Transfers local variables from stack to variable object.
563 * It's slow, so we want to avoid it as much as possible.
565 static HRESULT detach_variable_object(script_ctx_t *ctx, call_frame_t *frame)
567 unsigned i;
568 HRESULT hres;
570 if(!frame->base_scope || !frame->base_scope->frame)
571 return S_OK;
573 TRACE("detaching %p\n", frame);
575 assert(frame == frame->base_scope->frame);
576 assert(frame->variable_obj == frame->base_scope->jsobj);
578 if(!frame->arguments_obj) {
579 hres = setup_arguments_object(ctx, frame);
580 if(FAILED(hres))
581 return hres;
584 frame->base_scope->frame = NULL;
586 for(i = 0; i < frame->function->locals_cnt; i++) {
587 hres = jsdisp_propput_name(frame->variable_obj, frame->function->locals[i].name,
588 ctx->stack[local_off(frame, frame->function->locals[i].ref)]);
589 if(FAILED(hres))
590 return hres;
593 return S_OK;
596 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
598 named_item_t *item;
599 DISPID id;
600 HRESULT hres;
602 for(item = ctx->named_items; item; item = item->next) {
603 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
604 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
605 if(SUCCEEDED(hres)) {
606 if(ret)
607 exprval_set_disp_ref(ret, item->disp, id);
608 return TRUE;
613 return FALSE;
616 static int local_ref_cmp(const void *key, const void *ref)
618 return strcmpW((const WCHAR*)key, ((const local_ref_t*)ref)->name);
621 local_ref_t *lookup_local(const function_code_t *function, const WCHAR *identifier)
623 return bsearch(identifier, function->locals, function->locals_cnt, sizeof(*function->locals), local_ref_cmp);
626 /* ECMA-262 3rd Edition 10.1.4 */
627 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
629 scope_chain_t *scope;
630 named_item_t *item;
631 DISPID id = 0;
632 HRESULT hres;
634 TRACE("%s\n", debugstr_w(identifier));
636 if(ctx->call_ctx) {
637 for(scope = ctx->call_ctx->scope; scope; scope = scope->next) {
638 if(scope->frame) {
639 function_code_t *func = scope->frame->function;
640 local_ref_t *ref = lookup_local(func, identifier);
641 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
643 if(ref) {
644 ret->type = EXPRVAL_STACK_REF;
645 ret->u.off = local_off(scope->frame, ref->ref);
646 TRACE("returning ref %d for %d\n", ret->u.off, ref->ref);
647 return S_OK;
650 if(!strcmpW(identifier, argumentsW)) {
651 hres = detach_variable_object(ctx, scope->frame);
652 if(FAILED(hres))
653 return hres;
656 if(scope->jsobj)
657 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
658 else
659 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
660 if(SUCCEEDED(hres)) {
661 exprval_set_disp_ref(ret, scope->obj, id);
662 return S_OK;
667 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
668 if(SUCCEEDED(hres)) {
669 exprval_set_disp_ref(ret, to_disp(ctx->global), id);
670 return S_OK;
673 for(item = ctx->named_items; item; item = item->next) {
674 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
675 if(!item->disp) {
676 IUnknown *unk;
678 if(!ctx->site)
679 break;
681 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
682 SCRIPTINFO_IUNKNOWN, &unk, NULL);
683 if(FAILED(hres)) {
684 WARN("GetItemInfo failed: %08x\n", hres);
685 break;
688 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
689 IUnknown_Release(unk);
690 if(FAILED(hres)) {
691 WARN("object does not implement IDispatch\n");
692 break;
696 IDispatch_AddRef(item->disp);
697 ret->type = EXPRVAL_JSVAL;
698 ret->u.val = jsval_disp(item->disp);
699 return S_OK;
703 if(lookup_global_members(ctx, identifier, ret))
704 return S_OK;
706 exprval_set_exception(ret, JS_E_UNDEFINED_VARIABLE);
707 return S_OK;
710 static inline BSTR get_op_bstr(script_ctx_t *ctx, int i)
712 call_frame_t *frame = ctx->call_ctx;
713 return frame->bytecode->instrs[frame->ip].u.arg[i].bstr;
716 static inline unsigned get_op_uint(script_ctx_t *ctx, int i)
718 call_frame_t *frame = ctx->call_ctx;
719 return frame->bytecode->instrs[frame->ip].u.arg[i].uint;
722 static inline unsigned get_op_int(script_ctx_t *ctx, int i)
724 call_frame_t *frame = ctx->call_ctx;
725 return frame->bytecode->instrs[frame->ip].u.arg[i].lng;
728 static inline jsstr_t *get_op_str(script_ctx_t *ctx, int i)
730 call_frame_t *frame = ctx->call_ctx;
731 return frame->bytecode->instrs[frame->ip].u.arg[i].str;
734 static inline double get_op_double(script_ctx_t *ctx)
736 call_frame_t *frame = ctx->call_ctx;
737 return frame->bytecode->instrs[frame->ip].u.dbl;
740 static inline void jmp_next(script_ctx_t *ctx)
742 ctx->call_ctx->ip++;
745 static inline void jmp_abs(script_ctx_t *ctx, unsigned dst)
747 ctx->call_ctx->ip = dst;
750 /* ECMA-262 3rd Edition 12.6.4 */
751 static HRESULT interp_forin(script_ctx_t *ctx)
753 const HRESULT arg = get_op_uint(ctx, 0);
754 IDispatch *obj = NULL;
755 IDispatchEx *dispex;
756 exprval_t prop_ref;
757 DISPID id;
758 BSTR name = NULL;
759 HRESULT hres;
761 TRACE("\n");
763 assert(is_number(stack_top(ctx)));
764 id = get_number(stack_top(ctx));
766 if(!stack_topn_exprval(ctx, 1, &prop_ref)) {
767 FIXME("invalid ref: %08x\n", prop_ref.u.hres);
768 return E_FAIL;
771 if(is_object_instance(stack_topn(ctx, 3)))
772 obj = get_object(stack_topn(ctx, 3));
774 if(obj) {
775 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
776 if(SUCCEEDED(hres)) {
777 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
778 if(hres == S_OK)
779 hres = IDispatchEx_GetMemberName(dispex, id, &name);
780 IDispatchEx_Release(dispex);
781 if(FAILED(hres))
782 return hres;
783 }else {
784 TRACE("No IDispatchEx\n");
788 if(name) {
789 jsstr_t *str;
791 str = jsstr_alloc_len(name, SysStringLen(name));
792 SysFreeString(name);
793 if(!str)
794 return E_OUTOFMEMORY;
796 stack_pop(ctx);
797 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
799 hres = exprval_propput(ctx, &prop_ref, jsval_string(str));
800 jsstr_release(str);
801 if(FAILED(hres))
802 return hres;
804 jmp_next(ctx);
805 }else {
806 stack_popn(ctx, 4);
807 jmp_abs(ctx, arg);
809 return S_OK;
812 /* ECMA-262 3rd Edition 12.10 */
813 static HRESULT interp_push_scope(script_ctx_t *ctx)
815 IDispatch *disp;
816 jsval_t v;
817 HRESULT hres;
819 TRACE("\n");
821 v = stack_pop(ctx);
822 hres = to_object(ctx, v, &disp);
823 jsval_release(v);
824 if(FAILED(hres))
825 return hres;
827 hres = scope_push(ctx->call_ctx->scope, to_jsdisp(disp), disp, &ctx->call_ctx->scope);
828 IDispatch_Release(disp);
829 return hres;
832 /* ECMA-262 3rd Edition 12.10 */
833 static HRESULT interp_pop_scope(script_ctx_t *ctx)
835 TRACE("\n");
837 scope_pop(&ctx->call_ctx->scope);
838 return S_OK;
841 /* ECMA-262 3rd Edition 12.13 */
842 static HRESULT interp_case(script_ctx_t *ctx)
844 const unsigned arg = get_op_uint(ctx, 0);
845 jsval_t v;
846 BOOL b;
847 HRESULT hres;
849 TRACE("\n");
851 v = stack_pop(ctx);
852 hres = equal2_values(stack_top(ctx), v, &b);
853 jsval_release(v);
854 if(FAILED(hres))
855 return hres;
857 if(b) {
858 stack_popn(ctx, 1);
859 jmp_abs(ctx, arg);
860 }else {
861 jmp_next(ctx);
863 return S_OK;
866 /* ECMA-262 3rd Edition 12.13 */
867 static HRESULT interp_throw(script_ctx_t *ctx)
869 TRACE("\n");
871 jsval_release(ctx->ei.val);
872 ctx->ei.val = stack_pop(ctx);
873 return DISP_E_EXCEPTION;
876 static HRESULT interp_throw_ref(script_ctx_t *ctx)
878 const HRESULT arg = get_op_uint(ctx, 0);
880 TRACE("%08x\n", arg);
882 return throw_reference_error(ctx, arg, NULL);
885 static HRESULT interp_throw_type(script_ctx_t *ctx)
887 const HRESULT hres = get_op_uint(ctx, 0);
888 jsstr_t *str = get_op_str(ctx, 1);
889 const WCHAR *ptr;
891 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
893 ptr = jsstr_flatten(str);
894 return ptr ? throw_type_error(ctx, hres, ptr) : E_OUTOFMEMORY;
897 /* ECMA-262 3rd Edition 12.14 */
898 static HRESULT interp_push_except(script_ctx_t *ctx)
900 const unsigned arg1 = get_op_uint(ctx, 0);
901 const BSTR arg2 = get_op_bstr(ctx, 1);
902 call_frame_t *frame = ctx->call_ctx;
903 except_frame_t *except;
904 unsigned stack_top;
906 TRACE("\n");
908 stack_top = ctx->stack_top;
910 if(!arg2) {
911 HRESULT hres;
913 hres = stack_push(ctx, jsval_bool(TRUE));
914 if(FAILED(hres))
915 return hres;
916 hres = stack_push(ctx, jsval_bool(TRUE));
917 if(FAILED(hres))
918 return hres;
921 except = heap_alloc(sizeof(*except));
922 if(!except)
923 return E_OUTOFMEMORY;
925 except->stack_top = stack_top;
926 except->scope = frame->scope;
927 except->catch_off = arg1;
928 except->ident = arg2;
929 except->next = frame->except_frame;
930 frame->except_frame = except;
931 return S_OK;
934 /* ECMA-262 3rd Edition 12.14 */
935 static HRESULT interp_pop_except(script_ctx_t *ctx)
937 call_frame_t *frame = ctx->call_ctx;
938 except_frame_t *except;
940 TRACE("\n");
942 except = frame->except_frame;
943 assert(except != NULL);
945 frame->except_frame = except->next;
946 heap_free(except);
947 return S_OK;
950 /* ECMA-262 3rd Edition 12.14 */
951 static HRESULT interp_end_finally(script_ctx_t *ctx)
953 jsval_t v;
955 TRACE("\n");
957 v = stack_pop(ctx);
958 assert(is_bool(v));
960 if(!get_bool(v)) {
961 TRACE("passing exception\n");
963 ctx->ei.val = stack_pop(ctx);
964 return DISP_E_EXCEPTION;
967 stack_pop(ctx);
968 return S_OK;
971 /* ECMA-262 3rd Edition 13 */
972 static HRESULT interp_func(script_ctx_t *ctx)
974 unsigned func_idx = get_op_uint(ctx, 0);
975 call_frame_t *frame = ctx->call_ctx;
976 jsdisp_t *dispex;
977 HRESULT hres;
979 TRACE("%d\n", func_idx);
981 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+func_idx,
982 frame->scope, &dispex);
983 if(FAILED(hres))
984 return hres;
986 return stack_push(ctx, jsval_obj(dispex));
989 /* ECMA-262 3rd Edition 11.2.1 */
990 static HRESULT interp_array(script_ctx_t *ctx)
992 jsstr_t *name_str;
993 const WCHAR *name;
994 jsval_t v, namev;
995 IDispatch *obj;
996 DISPID id;
997 HRESULT hres;
999 TRACE("\n");
1001 namev = stack_pop(ctx);
1003 hres = stack_pop_object(ctx, &obj);
1004 if(FAILED(hres)) {
1005 jsval_release(namev);
1006 return hres;
1009 hres = to_flat_string(ctx, namev, &name_str, &name);
1010 jsval_release(namev);
1011 if(FAILED(hres)) {
1012 IDispatch_Release(obj);
1013 return hres;
1016 hres = disp_get_id(ctx, obj, name, NULL, 0, &id);
1017 jsstr_release(name_str);
1018 if(SUCCEEDED(hres)) {
1019 hres = disp_propget(ctx, obj, id, &v);
1020 }else if(hres == DISP_E_UNKNOWNNAME) {
1021 v = jsval_undefined();
1022 hres = S_OK;
1024 IDispatch_Release(obj);
1025 if(FAILED(hres))
1026 return hres;
1028 return stack_push(ctx, v);
1031 /* ECMA-262 3rd Edition 11.2.1 */
1032 static HRESULT interp_member(script_ctx_t *ctx)
1034 const BSTR arg = get_op_bstr(ctx, 0);
1035 IDispatch *obj;
1036 jsval_t v;
1037 DISPID id;
1038 HRESULT hres;
1040 TRACE("\n");
1042 hres = stack_pop_object(ctx, &obj);
1043 if(FAILED(hres))
1044 return hres;
1046 hres = disp_get_id(ctx, obj, arg, arg, 0, &id);
1047 if(SUCCEEDED(hres)) {
1048 hres = disp_propget(ctx, obj, id, &v);
1049 }else if(hres == DISP_E_UNKNOWNNAME) {
1050 v = jsval_undefined();
1051 hres = S_OK;
1053 IDispatch_Release(obj);
1054 if(FAILED(hres))
1055 return hres;
1057 return stack_push(ctx, v);
1060 /* ECMA-262 3rd Edition 11.2.1 */
1061 static HRESULT interp_memberid(script_ctx_t *ctx)
1063 const unsigned arg = get_op_uint(ctx, 0);
1064 jsval_t objv, namev;
1065 const WCHAR *name;
1066 jsstr_t *name_str;
1067 IDispatch *obj;
1068 exprval_t ref;
1069 DISPID id;
1070 HRESULT hres;
1072 TRACE("%x\n", arg);
1074 namev = stack_pop(ctx);
1075 objv = stack_pop(ctx);
1077 hres = to_object(ctx, objv, &obj);
1078 jsval_release(objv);
1079 if(SUCCEEDED(hres)) {
1080 hres = to_flat_string(ctx, namev, &name_str, &name);
1081 if(FAILED(hres))
1082 IDispatch_Release(obj);
1084 jsval_release(namev);
1085 if(FAILED(hres))
1086 return hres;
1088 hres = disp_get_id(ctx, obj, name, NULL, arg, &id);
1089 jsstr_release(name_str);
1090 if(SUCCEEDED(hres)) {
1091 ref.type = EXPRVAL_IDREF;
1092 ref.u.idref.disp = obj;
1093 ref.u.idref.id = id;
1094 }else {
1095 IDispatch_Release(obj);
1096 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1097 exprval_set_exception(&ref, JS_E_INVALID_PROPERTY);
1098 hres = S_OK;
1099 }else {
1100 ERR("failed %08x\n", hres);
1101 return hres;
1105 return stack_push_exprval(ctx, &ref);
1108 /* ECMA-262 3rd Edition 11.2.1 */
1109 static HRESULT interp_refval(script_ctx_t *ctx)
1111 exprval_t ref;
1112 jsval_t v;
1113 HRESULT hres;
1115 TRACE("\n");
1117 if(!stack_topn_exprval(ctx, 0, &ref))
1118 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
1120 hres = exprval_propget(ctx, &ref, &v);
1121 if(FAILED(hres))
1122 return hres;
1124 return stack_push(ctx, v);
1127 /* ECMA-262 3rd Edition 11.2.2 */
1128 static HRESULT interp_new(script_ctx_t *ctx)
1130 const unsigned argc = get_op_uint(ctx, 0);
1131 call_frame_t *frame = ctx->call_ctx;
1132 jsval_t constr;
1134 TRACE("%d\n", argc);
1136 constr = stack_topn(ctx, argc);
1138 /* NOTE: Should use to_object here */
1140 if(is_null(constr))
1141 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
1142 else if(!is_object_instance(constr))
1143 return throw_type_error(ctx, JS_E_INVALID_ACTION, NULL);
1144 else if(!get_object(constr))
1145 return throw_type_error(ctx, JS_E_INVALID_PROPERTY, NULL);
1147 clear_ret(frame);
1148 return disp_call_value(ctx, get_object(constr), NULL, DISPATCH_CONSTRUCT | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1149 argc, stack_args(ctx, argc), &frame->ret);
1152 /* ECMA-262 3rd Edition 11.2.3 */
1153 static HRESULT interp_call(script_ctx_t *ctx)
1155 const unsigned argn = get_op_uint(ctx, 0);
1156 const int do_ret = get_op_int(ctx, 1);
1157 call_frame_t *frame = ctx->call_ctx;
1158 jsval_t obj;
1160 TRACE("%d %d\n", argn, do_ret);
1162 obj = stack_topn(ctx, argn);
1163 if(!is_object_instance(obj))
1164 return throw_type_error(ctx, JS_E_INVALID_PROPERTY, NULL);
1166 clear_ret(frame);
1167 return disp_call_value(ctx, get_object(obj), NULL, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1168 argn, stack_args(ctx, argn), do_ret ? &frame->ret : NULL);
1171 /* ECMA-262 3rd Edition 11.2.3 */
1172 static HRESULT interp_call_member(script_ctx_t *ctx)
1174 const unsigned argn = get_op_uint(ctx, 0);
1175 const int do_ret = get_op_int(ctx, 1);
1176 call_frame_t *frame = ctx->call_ctx;
1177 exprval_t ref;
1179 TRACE("%d %d\n", argn, do_ret);
1181 if(!stack_topn_exprval(ctx, argn, &ref))
1182 return throw_type_error(ctx, ref.u.hres, NULL);
1184 clear_ret(frame);
1185 return exprval_call(ctx, &ref, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1186 argn, stack_args(ctx, argn), do_ret ? &frame->ret : NULL);
1189 /* ECMA-262 3rd Edition 11.1.1 */
1190 static HRESULT interp_this(script_ctx_t *ctx)
1192 call_frame_t *frame = ctx->call_ctx;
1194 TRACE("\n");
1196 IDispatch_AddRef(frame->this_obj);
1197 return stack_push(ctx, jsval_disp(frame->this_obj));
1200 static HRESULT interp_identifier_ref(script_ctx_t *ctx, BSTR identifier, unsigned flags)
1202 exprval_t exprval;
1203 HRESULT hres;
1205 hres = identifier_eval(ctx, identifier, &exprval);
1206 if(FAILED(hres))
1207 return hres;
1209 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1210 DISPID id;
1212 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
1213 if(FAILED(hres))
1214 return hres;
1216 exprval_set_disp_ref(&exprval, to_disp(ctx->global), id);
1219 if(exprval.type == EXPRVAL_JSVAL || exprval.type == EXPRVAL_INVALID) {
1220 WARN("invalid ref\n");
1221 exprval_release(&exprval);
1222 exprval_set_exception(&exprval, JS_E_OBJECT_EXPECTED);
1225 return stack_push_exprval(ctx, &exprval);
1228 static HRESULT interp_local_ref(script_ctx_t *ctx)
1230 const int arg = get_op_int(ctx, 0);
1231 const unsigned flags = get_op_uint(ctx, 1);
1232 call_frame_t *frame = ctx->call_ctx;
1233 exprval_t ref;
1235 TRACE("%d\n", arg);
1237 if(!frame->base_scope || !frame->base_scope->frame)
1238 return interp_identifier_ref(ctx, local_name(frame, arg), flags);
1240 ref.type = EXPRVAL_STACK_REF;
1241 ref.u.off = local_off(frame, arg);
1242 return stack_push_exprval(ctx, &ref);
1245 /* ECMA-262 3rd Edition 10.1.4 */
1246 static HRESULT interp_ident(script_ctx_t *ctx)
1248 const BSTR arg = get_op_bstr(ctx, 0);
1249 exprval_t exprval;
1250 jsval_t v;
1251 HRESULT hres;
1253 TRACE("%s\n", debugstr_w(arg));
1255 hres = identifier_eval(ctx, arg, &exprval);
1256 if(FAILED(hres))
1257 return hres;
1259 if(exprval.type == EXPRVAL_INVALID)
1260 return throw_type_error(ctx, exprval.u.hres, arg);
1262 hres = exprval_to_value(ctx, &exprval, &v);
1263 if(FAILED(hres))
1264 return hres;
1266 return stack_push(ctx, v);
1269 /* ECMA-262 3rd Edition 10.1.4 */
1270 static HRESULT interp_identid(script_ctx_t *ctx)
1272 const BSTR arg = get_op_bstr(ctx, 0);
1273 const unsigned flags = get_op_uint(ctx, 1);
1275 TRACE("%s %x\n", debugstr_w(arg), flags);
1277 return interp_identifier_ref(ctx, arg, flags);
1280 /* ECMA-262 3rd Edition 7.8.1 */
1281 static HRESULT interp_null(script_ctx_t *ctx)
1283 TRACE("\n");
1285 return stack_push(ctx, jsval_null());
1288 /* ECMA-262 3rd Edition 7.8.2 */
1289 static HRESULT interp_bool(script_ctx_t *ctx)
1291 const int arg = get_op_int(ctx, 0);
1293 TRACE("%s\n", arg ? "true" : "false");
1295 return stack_push(ctx, jsval_bool(arg));
1298 /* ECMA-262 3rd Edition 7.8.3 */
1299 static HRESULT interp_int(script_ctx_t *ctx)
1301 const int arg = get_op_int(ctx, 0);
1303 TRACE("%d\n", arg);
1305 return stack_push(ctx, jsval_number(arg));
1308 /* ECMA-262 3rd Edition 7.8.3 */
1309 static HRESULT interp_double(script_ctx_t *ctx)
1311 const double arg = get_op_double(ctx);
1313 TRACE("%lf\n", arg);
1315 return stack_push(ctx, jsval_number(arg));
1318 /* ECMA-262 3rd Edition 7.8.4 */
1319 static HRESULT interp_str(script_ctx_t *ctx)
1321 jsstr_t *str = get_op_str(ctx, 0);
1323 TRACE("%s\n", debugstr_jsstr(str));
1325 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1328 /* ECMA-262 3rd Edition 7.8 */
1329 static HRESULT interp_regexp(script_ctx_t *ctx)
1331 jsstr_t *source = get_op_str(ctx, 0);
1332 const unsigned flags = get_op_uint(ctx, 1);
1333 jsdisp_t *regexp;
1334 HRESULT hres;
1336 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1338 hres = create_regexp(ctx, source, flags, &regexp);
1339 if(FAILED(hres))
1340 return hres;
1342 return stack_push(ctx, jsval_obj(regexp));
1345 /* ECMA-262 3rd Edition 11.1.4 */
1346 static HRESULT interp_carray(script_ctx_t *ctx)
1348 const unsigned arg = get_op_uint(ctx, 0);
1349 jsdisp_t *array;
1350 jsval_t val;
1351 unsigned i;
1352 HRESULT hres;
1354 TRACE("%u\n", arg);
1356 hres = create_array(ctx, arg, &array);
1357 if(FAILED(hres))
1358 return hres;
1360 i = arg;
1361 while(i--) {
1362 val = stack_pop(ctx);
1363 hres = jsdisp_propput_idx(array, i, val);
1364 jsval_release(val);
1365 if(FAILED(hres)) {
1366 jsdisp_release(array);
1367 return hres;
1371 return stack_push(ctx, jsval_obj(array));
1374 /* ECMA-262 3rd Edition 11.1.5 */
1375 static HRESULT interp_new_obj(script_ctx_t *ctx)
1377 jsdisp_t *obj;
1378 HRESULT hres;
1380 TRACE("\n");
1382 hres = create_object(ctx, NULL, &obj);
1383 if(FAILED(hres))
1384 return hres;
1386 return stack_push(ctx, jsval_obj(obj));
1389 /* ECMA-262 3rd Edition 11.1.5 */
1390 static HRESULT interp_obj_prop(script_ctx_t *ctx)
1392 const BSTR name = get_op_bstr(ctx, 0);
1393 jsdisp_t *obj;
1394 jsval_t val;
1395 HRESULT hres;
1397 TRACE("%s\n", debugstr_w(name));
1399 val = stack_pop(ctx);
1401 assert(is_object_instance(stack_top(ctx)));
1402 obj = as_jsdisp(get_object(stack_top(ctx)));
1404 hres = jsdisp_propput_name(obj, name, val);
1405 jsval_release(val);
1406 return hres;
1409 /* ECMA-262 3rd Edition 11.11 */
1410 static HRESULT interp_cnd_nz(script_ctx_t *ctx)
1412 const unsigned arg = get_op_uint(ctx, 0);
1413 BOOL b;
1414 HRESULT hres;
1416 TRACE("\n");
1418 hres = to_boolean(stack_top(ctx), &b);
1419 if(FAILED(hres))
1420 return hres;
1422 if(b) {
1423 jmp_abs(ctx, arg);
1424 }else {
1425 stack_popn(ctx, 1);
1426 jmp_next(ctx);
1428 return S_OK;
1431 /* ECMA-262 3rd Edition 11.11 */
1432 static HRESULT interp_cnd_z(script_ctx_t *ctx)
1434 const unsigned arg = get_op_uint(ctx, 0);
1435 BOOL b;
1436 HRESULT hres;
1438 TRACE("\n");
1440 hres = to_boolean(stack_top(ctx), &b);
1441 if(FAILED(hres))
1442 return hres;
1444 if(b) {
1445 stack_popn(ctx, 1);
1446 jmp_next(ctx);
1447 }else {
1448 jmp_abs(ctx, arg);
1450 return S_OK;
1453 /* ECMA-262 3rd Edition 11.10 */
1454 static HRESULT interp_or(script_ctx_t *ctx)
1456 INT l, r;
1457 HRESULT hres;
1459 TRACE("\n");
1461 hres = stack_pop_int(ctx, &r);
1462 if(FAILED(hres))
1463 return hres;
1465 hres = stack_pop_int(ctx, &l);
1466 if(FAILED(hres))
1467 return hres;
1469 return stack_push(ctx, jsval_number(l|r));
1472 /* ECMA-262 3rd Edition 11.10 */
1473 static HRESULT interp_xor(script_ctx_t *ctx)
1475 INT l, r;
1476 HRESULT hres;
1478 TRACE("\n");
1480 hres = stack_pop_int(ctx, &r);
1481 if(FAILED(hres))
1482 return hres;
1484 hres = stack_pop_int(ctx, &l);
1485 if(FAILED(hres))
1486 return hres;
1488 return stack_push(ctx, jsval_number(l^r));
1491 /* ECMA-262 3rd Edition 11.10 */
1492 static HRESULT interp_and(script_ctx_t *ctx)
1494 INT l, r;
1495 HRESULT hres;
1497 TRACE("\n");
1499 hres = stack_pop_int(ctx, &r);
1500 if(FAILED(hres))
1501 return hres;
1503 hres = stack_pop_int(ctx, &l);
1504 if(FAILED(hres))
1505 return hres;
1507 return stack_push(ctx, jsval_number(l&r));
1510 /* ECMA-262 3rd Edition 11.8.6 */
1511 static HRESULT interp_instanceof(script_ctx_t *ctx)
1513 jsdisp_t *obj, *iter, *tmp = NULL;
1514 jsval_t prot, v;
1515 BOOL ret = FALSE;
1516 HRESULT hres;
1518 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1520 v = stack_pop(ctx);
1521 if(!is_object_instance(v) || !get_object(v)) {
1522 jsval_release(v);
1523 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1526 obj = iface_to_jsdisp(get_object(v));
1527 IDispatch_Release(get_object(v));
1528 if(!obj) {
1529 FIXME("non-jsdisp objects not supported\n");
1530 return E_FAIL;
1533 if(is_class(obj, JSCLASS_FUNCTION)) {
1534 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1535 }else {
1536 hres = throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1538 jsdisp_release(obj);
1539 if(FAILED(hres))
1540 return hres;
1542 v = stack_pop(ctx);
1544 if(is_object_instance(prot)) {
1545 if(is_object_instance(v))
1546 tmp = iface_to_jsdisp(get_object(v));
1547 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1548 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1549 if(FAILED(hres))
1550 break;
1553 if(tmp)
1554 jsdisp_release(tmp);
1555 }else {
1556 FIXME("prototype is not an object\n");
1557 hres = E_FAIL;
1560 jsval_release(prot);
1561 jsval_release(v);
1562 if(FAILED(hres))
1563 return hres;
1565 return stack_push(ctx, jsval_bool(ret));
1568 /* ECMA-262 3rd Edition 11.8.7 */
1569 static HRESULT interp_in(script_ctx_t *ctx)
1571 const WCHAR *str;
1572 jsstr_t *jsstr;
1573 jsval_t obj, v;
1574 DISPID id = 0;
1575 BOOL ret;
1576 HRESULT hres;
1578 TRACE("\n");
1580 obj = stack_pop(ctx);
1581 if(!is_object_instance(obj) || !get_object(obj)) {
1582 jsval_release(obj);
1583 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
1586 v = stack_pop(ctx);
1587 hres = to_flat_string(ctx, v, &jsstr, &str);
1588 jsval_release(v);
1589 if(FAILED(hres)) {
1590 IDispatch_Release(get_object(obj));
1591 return hres;
1594 hres = disp_get_id(ctx, get_object(obj), str, NULL, 0, &id);
1595 IDispatch_Release(get_object(obj));
1596 jsstr_release(jsstr);
1597 if(SUCCEEDED(hres))
1598 ret = TRUE;
1599 else if(hres == DISP_E_UNKNOWNNAME)
1600 ret = FALSE;
1601 else
1602 return hres;
1604 return stack_push(ctx, jsval_bool(ret));
1607 /* ECMA-262 3rd Edition 11.6.1 */
1608 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1610 jsval_t r, l;
1611 HRESULT hres;
1613 hres = to_primitive(ctx, lval, &l, NO_HINT);
1614 if(FAILED(hres))
1615 return hres;
1617 hres = to_primitive(ctx, rval, &r, NO_HINT);
1618 if(FAILED(hres)) {
1619 jsval_release(l);
1620 return hres;
1623 if(is_string(l) || is_string(r)) {
1624 jsstr_t *lstr, *rstr = NULL;
1626 hres = to_string(ctx, l, &lstr);
1627 if(SUCCEEDED(hres))
1628 hres = to_string(ctx, r, &rstr);
1630 if(SUCCEEDED(hres)) {
1631 jsstr_t *ret_str;
1633 ret_str = jsstr_concat(lstr, rstr);
1634 if(ret_str)
1635 *ret = jsval_string(ret_str);
1636 else
1637 hres = E_OUTOFMEMORY;
1640 jsstr_release(lstr);
1641 if(rstr)
1642 jsstr_release(rstr);
1643 }else {
1644 double nl, nr;
1646 hres = to_number(ctx, l, &nl);
1647 if(SUCCEEDED(hres)) {
1648 hres = to_number(ctx, r, &nr);
1649 if(SUCCEEDED(hres))
1650 *ret = jsval_number(nl+nr);
1654 jsval_release(r);
1655 jsval_release(l);
1656 return hres;
1659 /* ECMA-262 3rd Edition 11.6.1 */
1660 static HRESULT interp_add(script_ctx_t *ctx)
1662 jsval_t l, r, ret;
1663 HRESULT hres;
1665 r = stack_pop(ctx);
1666 l = stack_pop(ctx);
1668 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1670 hres = add_eval(ctx, l, r, &ret);
1671 jsval_release(l);
1672 jsval_release(r);
1673 if(FAILED(hres))
1674 return hres;
1676 return stack_push(ctx, ret);
1679 /* ECMA-262 3rd Edition 11.6.2 */
1680 static HRESULT interp_sub(script_ctx_t *ctx)
1682 double l, r;
1683 HRESULT hres;
1685 TRACE("\n");
1687 hres = stack_pop_number(ctx, &r);
1688 if(FAILED(hres))
1689 return hres;
1691 hres = stack_pop_number(ctx, &l);
1692 if(FAILED(hres))
1693 return hres;
1695 return stack_push(ctx, jsval_number(l-r));
1698 /* ECMA-262 3rd Edition 11.5.1 */
1699 static HRESULT interp_mul(script_ctx_t *ctx)
1701 double l, r;
1702 HRESULT hres;
1704 TRACE("\n");
1706 hres = stack_pop_number(ctx, &r);
1707 if(FAILED(hres))
1708 return hres;
1710 hres = stack_pop_number(ctx, &l);
1711 if(FAILED(hres))
1712 return hres;
1714 return stack_push(ctx, jsval_number(l*r));
1717 /* ECMA-262 3rd Edition 11.5.2 */
1718 static HRESULT interp_div(script_ctx_t *ctx)
1720 double l, r;
1721 HRESULT hres;
1723 TRACE("\n");
1725 hres = stack_pop_number(ctx, &r);
1726 if(FAILED(hres))
1727 return hres;
1729 hres = stack_pop_number(ctx, &l);
1730 if(FAILED(hres))
1731 return hres;
1733 return stack_push(ctx, jsval_number(l/r));
1736 /* ECMA-262 3rd Edition 11.5.3 */
1737 static HRESULT interp_mod(script_ctx_t *ctx)
1739 double l, r;
1740 HRESULT hres;
1742 TRACE("\n");
1744 hres = stack_pop_number(ctx, &r);
1745 if(FAILED(hres))
1746 return hres;
1748 hres = stack_pop_number(ctx, &l);
1749 if(FAILED(hres))
1750 return hres;
1752 return stack_push(ctx, jsval_number(fmod(l, r)));
1755 /* ECMA-262 3rd Edition 11.4.2 */
1756 static HRESULT interp_delete(script_ctx_t *ctx)
1758 jsval_t objv, namev;
1759 IDispatch *obj;
1760 jsstr_t *name;
1761 BOOL ret;
1762 HRESULT hres;
1764 TRACE("\n");
1766 namev = stack_pop(ctx);
1767 objv = stack_pop(ctx);
1769 hres = to_object(ctx, objv, &obj);
1770 jsval_release(objv);
1771 if(FAILED(hres)) {
1772 jsval_release(namev);
1773 return hres;
1776 hres = to_string(ctx, namev, &name);
1777 jsval_release(namev);
1778 if(FAILED(hres)) {
1779 IDispatch_Release(obj);
1780 return hres;
1783 hres = disp_delete_name(ctx, obj, name, &ret);
1784 IDispatch_Release(obj);
1785 jsstr_release(name);
1786 if(FAILED(hres))
1787 return hres;
1789 return stack_push(ctx, jsval_bool(ret));
1792 /* ECMA-262 3rd Edition 11.4.2 */
1793 static HRESULT interp_delete_ident(script_ctx_t *ctx)
1795 const BSTR arg = get_op_bstr(ctx, 0);
1796 exprval_t exprval;
1797 BOOL ret;
1798 HRESULT hres;
1800 TRACE("%s\n", debugstr_w(arg));
1802 hres = identifier_eval(ctx, arg, &exprval);
1803 if(FAILED(hres))
1804 return hres;
1806 switch(exprval.type) {
1807 case EXPRVAL_STACK_REF:
1808 ret = FALSE;
1809 break;
1810 case EXPRVAL_IDREF:
1811 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1812 IDispatch_Release(exprval.u.idref.disp);
1813 if(FAILED(hres))
1814 return hres;
1815 break;
1816 case EXPRVAL_INVALID:
1817 ret = TRUE;
1818 break;
1819 default:
1820 FIXME("Unsupported exprval\n");
1821 exprval_release(&exprval);
1822 return E_NOTIMPL;
1826 return stack_push(ctx, jsval_bool(ret));
1829 /* ECMA-262 3rd Edition 11.4.2 */
1830 static HRESULT interp_void(script_ctx_t *ctx)
1832 TRACE("\n");
1834 stack_popn(ctx, 1);
1835 return stack_push(ctx, jsval_undefined());
1838 /* ECMA-262 3rd Edition 11.4.3 */
1839 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1841 switch(jsval_type(v)) {
1842 case JSV_UNDEFINED:
1843 *ret = undefinedW;
1844 break;
1845 case JSV_NULL:
1846 *ret = objectW;
1847 break;
1848 case JSV_OBJECT: {
1849 jsdisp_t *dispex;
1851 if(get_object(v) && (dispex = iface_to_jsdisp(get_object(v)))) {
1852 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1853 jsdisp_release(dispex);
1854 }else {
1855 *ret = objectW;
1857 break;
1859 case JSV_STRING:
1860 *ret = stringW;
1861 break;
1862 case JSV_NUMBER:
1863 *ret = numberW;
1864 break;
1865 case JSV_BOOL:
1866 *ret = booleanW;
1867 break;
1868 case JSV_VARIANT:
1869 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1870 return E_NOTIMPL;
1873 return S_OK;
1876 /* ECMA-262 3rd Edition 11.4.3 */
1877 static HRESULT interp_typeofid(script_ctx_t *ctx)
1879 const WCHAR *ret;
1880 exprval_t ref;
1881 jsval_t v;
1882 HRESULT hres;
1884 TRACE("\n");
1886 if(!stack_pop_exprval(ctx, &ref))
1887 return stack_push(ctx, jsval_string(jsstr_undefined()));
1889 hres = exprval_propget(ctx, &ref, &v);
1890 exprval_release(&ref);
1891 if(FAILED(hres))
1892 return stack_push_string(ctx, unknownW);
1894 hres = typeof_string(v, &ret);
1895 jsval_release(v);
1896 if(FAILED(hres))
1897 return hres;
1899 return stack_push_string(ctx, ret);
1902 /* ECMA-262 3rd Edition 11.4.3 */
1903 static HRESULT interp_typeofident(script_ctx_t *ctx)
1905 const BSTR arg = get_op_bstr(ctx, 0);
1906 exprval_t exprval;
1907 const WCHAR *ret;
1908 jsval_t v;
1909 HRESULT hres;
1911 TRACE("%s\n", debugstr_w(arg));
1913 hres = identifier_eval(ctx, arg, &exprval);
1914 if(FAILED(hres))
1915 return hres;
1917 if(exprval.type == EXPRVAL_INVALID)
1918 return stack_push(ctx, jsval_string(jsstr_undefined()));
1920 hres = exprval_to_value(ctx, &exprval, &v);
1921 if(FAILED(hres))
1922 return hres;
1924 hres = typeof_string(v, &ret);
1925 jsval_release(v);
1926 if(FAILED(hres))
1927 return hres;
1929 return stack_push_string(ctx, ret);
1932 /* ECMA-262 3rd Edition 11.4.3 */
1933 static HRESULT interp_typeof(script_ctx_t *ctx)
1935 const WCHAR *ret;
1936 jsval_t v;
1937 HRESULT hres;
1939 TRACE("\n");
1941 v = stack_pop(ctx);
1942 hres = typeof_string(v, &ret);
1943 jsval_release(v);
1944 if(FAILED(hres))
1945 return hres;
1947 return stack_push_string(ctx, ret);
1950 /* ECMA-262 3rd Edition 11.4.7 */
1951 static HRESULT interp_minus(script_ctx_t *ctx)
1953 double n;
1954 HRESULT hres;
1956 TRACE("\n");
1958 hres = stack_pop_number(ctx, &n);
1959 if(FAILED(hres))
1960 return hres;
1962 return stack_push(ctx, jsval_number(-n));
1965 /* ECMA-262 3rd Edition 11.4.6 */
1966 static HRESULT interp_tonum(script_ctx_t *ctx)
1968 jsval_t v;
1969 double n;
1970 HRESULT hres;
1972 TRACE("\n");
1974 v = stack_pop(ctx);
1975 hres = to_number(ctx, v, &n);
1976 jsval_release(v);
1977 if(FAILED(hres))
1978 return hres;
1980 return stack_push(ctx, jsval_number(n));
1983 /* ECMA-262 3rd Edition 11.3.1 */
1984 static HRESULT interp_postinc(script_ctx_t *ctx)
1986 const int arg = get_op_int(ctx, 0);
1987 exprval_t ref;
1988 jsval_t v;
1989 HRESULT hres;
1991 TRACE("%d\n", arg);
1993 if(!stack_pop_exprval(ctx, &ref))
1994 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
1996 hres = exprval_propget(ctx, &ref, &v);
1997 if(SUCCEEDED(hres)) {
1998 double n;
2000 hres = to_number(ctx, v, &n);
2001 if(SUCCEEDED(hres))
2002 hres = exprval_propput(ctx, &ref, jsval_number(n+(double)arg));
2003 if(FAILED(hres))
2004 jsval_release(v);
2006 exprval_release(&ref);
2007 if(FAILED(hres))
2008 return hres;
2010 return stack_push(ctx, v);
2013 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2014 static HRESULT interp_preinc(script_ctx_t *ctx)
2016 const int arg = get_op_int(ctx, 0);
2017 exprval_t ref;
2018 double ret;
2019 jsval_t v;
2020 HRESULT hres;
2022 TRACE("%d\n", arg);
2024 if(!stack_pop_exprval(ctx, &ref))
2025 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
2027 hres = exprval_propget(ctx, &ref, &v);
2028 if(SUCCEEDED(hres)) {
2029 double n;
2031 hres = to_number(ctx, v, &n);
2032 jsval_release(v);
2033 if(SUCCEEDED(hres)) {
2034 ret = n+(double)arg;
2035 hres = exprval_propput(ctx, &ref, jsval_number(ret));
2038 exprval_release(&ref);
2039 if(FAILED(hres))
2040 return hres;
2042 return stack_push(ctx, jsval_number(ret));
2045 /* ECMA-262 3rd Edition 11.9.3 */
2046 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
2048 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
2049 return equal2_values(lval, rval, ret);
2051 /* FIXME: NULL disps should be handled in more general way */
2052 if(is_object_instance(lval) && !get_object(lval))
2053 return equal_values(ctx, jsval_null(), rval, ret);
2054 if(is_object_instance(rval) && !get_object(rval))
2055 return equal_values(ctx, lval, jsval_null(), ret);
2057 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
2058 *ret = TRUE;
2059 return S_OK;
2062 if(is_string(lval) && is_number(rval)) {
2063 double n;
2064 HRESULT hres;
2066 hres = to_number(ctx, lval, &n);
2067 if(FAILED(hres))
2068 return hres;
2070 /* FIXME: optimize */
2071 return equal_values(ctx, jsval_number(n), rval, ret);
2074 if(is_string(rval) && is_number(lval)) {
2075 double n;
2076 HRESULT hres;
2078 hres = to_number(ctx, rval, &n);
2079 if(FAILED(hres))
2080 return hres;
2082 /* FIXME: optimize */
2083 return equal_values(ctx, lval, jsval_number(n), ret);
2086 if(is_bool(rval))
2087 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
2089 if(is_bool(lval))
2090 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
2093 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
2094 jsval_t prim;
2095 HRESULT hres;
2097 hres = to_primitive(ctx, rval, &prim, NO_HINT);
2098 if(FAILED(hres))
2099 return hres;
2101 hres = equal_values(ctx, lval, prim, ret);
2102 jsval_release(prim);
2103 return hres;
2107 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
2108 jsval_t prim;
2109 HRESULT hres;
2111 hres = to_primitive(ctx, lval, &prim, NO_HINT);
2112 if(FAILED(hres))
2113 return hres;
2115 hres = equal_values(ctx, prim, rval, ret);
2116 jsval_release(prim);
2117 return hres;
2121 *ret = FALSE;
2122 return S_OK;
2125 /* ECMA-262 3rd Edition 11.9.1 */
2126 static HRESULT interp_eq(script_ctx_t *ctx)
2128 jsval_t l, r;
2129 BOOL b;
2130 HRESULT hres;
2132 r = stack_pop(ctx);
2133 l = stack_pop(ctx);
2135 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
2137 hres = equal_values(ctx, l, r, &b);
2138 jsval_release(l);
2139 jsval_release(r);
2140 if(FAILED(hres))
2141 return hres;
2143 return stack_push(ctx, jsval_bool(b));
2146 /* ECMA-262 3rd Edition 11.9.2 */
2147 static HRESULT interp_neq(script_ctx_t *ctx)
2149 jsval_t l, r;
2150 BOOL b;
2151 HRESULT hres;
2153 r = stack_pop(ctx);
2154 l = stack_pop(ctx);
2156 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2158 hres = equal_values(ctx, l, r, &b);
2159 jsval_release(l);
2160 jsval_release(r);
2161 if(FAILED(hres))
2162 return hres;
2164 return stack_push(ctx, jsval_bool(!b));
2167 /* ECMA-262 3rd Edition 11.9.4 */
2168 static HRESULT interp_eq2(script_ctx_t *ctx)
2170 jsval_t l, r;
2171 BOOL b;
2172 HRESULT hres;
2174 r = stack_pop(ctx);
2175 l = stack_pop(ctx);
2177 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2179 hres = equal2_values(r, l, &b);
2180 jsval_release(l);
2181 jsval_release(r);
2182 if(FAILED(hres))
2183 return hres;
2185 return stack_push(ctx, jsval_bool(b));
2188 /* ECMA-262 3rd Edition 11.9.5 */
2189 static HRESULT interp_neq2(script_ctx_t *ctx)
2191 jsval_t l, r;
2192 BOOL b;
2193 HRESULT hres;
2195 TRACE("\n");
2197 r = stack_pop(ctx);
2198 l = stack_pop(ctx);
2200 hres = equal2_values(r, l, &b);
2201 jsval_release(l);
2202 jsval_release(r);
2203 if(FAILED(hres))
2204 return hres;
2206 return stack_push(ctx, jsval_bool(!b));
2209 /* ECMA-262 3rd Edition 11.8.5 */
2210 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2212 double ln, rn;
2213 jsval_t l, r;
2214 HRESULT hres;
2216 hres = to_primitive(ctx, lval, &l, NO_HINT);
2217 if(FAILED(hres))
2218 return hres;
2220 hres = to_primitive(ctx, rval, &r, NO_HINT);
2221 if(FAILED(hres)) {
2222 jsval_release(l);
2223 return hres;
2226 if(is_string(l) && is_string(r)) {
2227 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2228 jsstr_release(get_string(l));
2229 jsstr_release(get_string(r));
2230 return S_OK;
2233 hres = to_number(ctx, l, &ln);
2234 jsval_release(l);
2235 if(SUCCEEDED(hres))
2236 hres = to_number(ctx, r, &rn);
2237 jsval_release(r);
2238 if(FAILED(hres))
2239 return hres;
2241 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2242 return S_OK;
2245 /* ECMA-262 3rd Edition 11.8.1 */
2246 static HRESULT interp_lt(script_ctx_t *ctx)
2248 jsval_t l, r;
2249 BOOL b;
2250 HRESULT hres;
2252 r = stack_pop(ctx);
2253 l = stack_pop(ctx);
2255 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2257 hres = less_eval(ctx, l, r, FALSE, &b);
2258 jsval_release(l);
2259 jsval_release(r);
2260 if(FAILED(hres))
2261 return hres;
2263 return stack_push(ctx, jsval_bool(b));
2266 /* ECMA-262 3rd Edition 11.8.1 */
2267 static HRESULT interp_lteq(script_ctx_t *ctx)
2269 jsval_t l, r;
2270 BOOL b;
2271 HRESULT hres;
2273 r = stack_pop(ctx);
2274 l = stack_pop(ctx);
2276 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2278 hres = less_eval(ctx, r, l, TRUE, &b);
2279 jsval_release(l);
2280 jsval_release(r);
2281 if(FAILED(hres))
2282 return hres;
2284 return stack_push(ctx, jsval_bool(b));
2287 /* ECMA-262 3rd Edition 11.8.2 */
2288 static HRESULT interp_gt(script_ctx_t *ctx)
2290 jsval_t l, r;
2291 BOOL b;
2292 HRESULT hres;
2294 r = stack_pop(ctx);
2295 l = stack_pop(ctx);
2297 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2299 hres = less_eval(ctx, r, l, FALSE, &b);
2300 jsval_release(l);
2301 jsval_release(r);
2302 if(FAILED(hres))
2303 return hres;
2305 return stack_push(ctx, jsval_bool(b));
2308 /* ECMA-262 3rd Edition 11.8.4 */
2309 static HRESULT interp_gteq(script_ctx_t *ctx)
2311 jsval_t l, r;
2312 BOOL b;
2313 HRESULT hres;
2315 r = stack_pop(ctx);
2316 l = stack_pop(ctx);
2318 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2320 hres = less_eval(ctx, l, r, TRUE, &b);
2321 jsval_release(l);
2322 jsval_release(r);
2323 if(FAILED(hres))
2324 return hres;
2326 return stack_push(ctx, jsval_bool(b));
2329 /* ECMA-262 3rd Edition 11.4.8 */
2330 static HRESULT interp_bneg(script_ctx_t *ctx)
2332 jsval_t v;
2333 INT i;
2334 HRESULT hres;
2336 TRACE("\n");
2338 v = stack_pop(ctx);
2339 hres = to_int32(ctx, v, &i);
2340 jsval_release(v);
2341 if(FAILED(hres))
2342 return hres;
2344 return stack_push(ctx, jsval_number(~i));
2347 /* ECMA-262 3rd Edition 11.4.9 */
2348 static HRESULT interp_neg(script_ctx_t *ctx)
2350 jsval_t v;
2351 BOOL b;
2352 HRESULT hres;
2354 TRACE("\n");
2356 v = stack_pop(ctx);
2357 hres = to_boolean(v, &b);
2358 jsval_release(v);
2359 if(FAILED(hres))
2360 return hres;
2362 return stack_push(ctx, jsval_bool(!b));
2365 /* ECMA-262 3rd Edition 11.7.1 */
2366 static HRESULT interp_lshift(script_ctx_t *ctx)
2368 DWORD r;
2369 INT l;
2370 HRESULT hres;
2372 hres = stack_pop_uint(ctx, &r);
2373 if(FAILED(hres))
2374 return hres;
2376 hres = stack_pop_int(ctx, &l);
2377 if(FAILED(hres))
2378 return hres;
2380 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2383 /* ECMA-262 3rd Edition 11.7.2 */
2384 static HRESULT interp_rshift(script_ctx_t *ctx)
2386 DWORD r;
2387 INT l;
2388 HRESULT hres;
2390 hres = stack_pop_uint(ctx, &r);
2391 if(FAILED(hres))
2392 return hres;
2394 hres = stack_pop_int(ctx, &l);
2395 if(FAILED(hres))
2396 return hres;
2398 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2401 /* ECMA-262 3rd Edition 11.7.3 */
2402 static HRESULT interp_rshift2(script_ctx_t *ctx)
2404 DWORD r, l;
2405 HRESULT hres;
2407 hres = stack_pop_uint(ctx, &r);
2408 if(FAILED(hres))
2409 return hres;
2411 hres = stack_pop_uint(ctx, &l);
2412 if(FAILED(hres))
2413 return hres;
2415 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2418 /* ECMA-262 3rd Edition 11.13.1 */
2419 static HRESULT interp_assign(script_ctx_t *ctx)
2421 exprval_t ref;
2422 jsval_t v;
2423 HRESULT hres;
2425 TRACE("\n");
2427 v = stack_pop(ctx);
2429 if(!stack_pop_exprval(ctx, &ref)) {
2430 jsval_release(v);
2431 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
2434 hres = exprval_propput(ctx, &ref, v);
2435 exprval_release(&ref);
2436 if(FAILED(hres)) {
2437 jsval_release(v);
2438 return hres;
2441 return stack_push(ctx, v);
2444 /* JScript extension */
2445 static HRESULT interp_assign_call(script_ctx_t *ctx)
2447 const unsigned argc = get_op_uint(ctx, 0);
2448 exprval_t ref;
2449 jsval_t v;
2450 HRESULT hres;
2452 TRACE("%u\n", argc);
2454 if(!stack_topn_exprval(ctx, argc+1, &ref))
2455 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
2457 hres = exprval_call(ctx, &ref, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2458 if(FAILED(hres))
2459 return hres;
2461 v = stack_pop(ctx);
2462 stack_popn(ctx, argc+2);
2463 return stack_push(ctx, v);
2466 static HRESULT interp_undefined(script_ctx_t *ctx)
2468 TRACE("\n");
2470 return stack_push(ctx, jsval_undefined());
2473 static HRESULT interp_jmp(script_ctx_t *ctx)
2475 const unsigned arg = get_op_uint(ctx, 0);
2477 TRACE("%u\n", arg);
2479 jmp_abs(ctx, arg);
2480 return S_OK;
2483 static HRESULT interp_jmp_z(script_ctx_t *ctx)
2485 const unsigned arg = get_op_uint(ctx, 0);
2486 BOOL b;
2487 jsval_t v;
2488 HRESULT hres;
2490 TRACE("\n");
2492 v = stack_pop(ctx);
2493 hres = to_boolean(v, &b);
2494 jsval_release(v);
2495 if(FAILED(hres))
2496 return hres;
2498 if(b)
2499 jmp_next(ctx);
2500 else
2501 jmp_abs(ctx, arg);
2502 return S_OK;
2505 static HRESULT interp_pop(script_ctx_t *ctx)
2507 const unsigned arg = get_op_uint(ctx, 0);
2509 TRACE("%u\n", arg);
2511 stack_popn(ctx, arg);
2512 return S_OK;
2515 static HRESULT interp_ret(script_ctx_t *ctx)
2517 const unsigned clear_ret = get_op_uint(ctx, 0);
2518 call_frame_t *frame = ctx->call_ctx;
2520 TRACE("\n");
2522 if(clear_ret)
2523 jsval_release(steal_ret(frame));
2525 if((frame->flags & EXEC_CONSTRUCTOR) && !is_object_instance(frame->ret)) {
2526 jsval_release(frame->ret);
2527 IDispatch_AddRef(frame->this_obj);
2528 frame->ret = jsval_disp(frame->this_obj);
2531 jmp_abs(ctx, -1);
2532 return S_OK;
2535 static HRESULT interp_setret(script_ctx_t *ctx)
2537 call_frame_t *frame = ctx->call_ctx;
2539 TRACE("\n");
2541 jsval_release(frame->ret);
2542 frame->ret = stack_pop(ctx);
2543 return S_OK;
2546 static HRESULT interp_push_ret(script_ctx_t *ctx)
2548 call_frame_t *frame = ctx->call_ctx;
2549 HRESULT hres;
2551 TRACE("\n");
2553 hres = stack_push(ctx, frame->ret);
2554 if(SUCCEEDED(hres))
2555 frame->ret = jsval_undefined();
2556 return hres;
2559 typedef HRESULT (*op_func_t)(script_ctx_t*);
2561 static const op_func_t op_funcs[] = {
2562 #define X(x,a,b,c) interp_##x,
2563 OP_LIST
2564 #undef X
2567 static const unsigned op_move[] = {
2568 #define X(a,x,b,c) x,
2569 OP_LIST
2570 #undef X
2573 static void pop_call_frame(script_ctx_t *ctx)
2575 call_frame_t *frame = ctx->call_ctx;
2577 frame->stack_base -= frame->pop_locals + frame->pop_variables;
2579 assert(frame->scope == frame->base_scope);
2581 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2582 if(frame->scope && frame->scope->ref > 1) {
2583 HRESULT hres = detach_variable_object(ctx, frame);
2584 if(FAILED(hres))
2585 ERR("Failed to detach variable object: %08x\n", hres);
2588 if(frame->arguments_obj)
2589 detach_arguments_object(frame->arguments_obj);
2590 if(frame->scope)
2591 scope_release(frame->scope);
2593 if(frame->pop_variables)
2594 stack_popn(ctx, frame->pop_variables);
2595 stack_popn(ctx, frame->pop_locals);
2597 ctx->call_ctx = frame->prev_frame;
2599 if(frame->function_instance)
2600 jsdisp_release(frame->function_instance);
2601 if(frame->variable_obj)
2602 jsdisp_release(frame->variable_obj);
2603 if(frame->this_obj)
2604 IDispatch_Release(frame->this_obj);
2605 jsval_release(frame->ret);
2606 release_bytecode(frame->bytecode);
2607 heap_free(frame);
2610 static HRESULT unwind_exception(script_ctx_t *ctx, HRESULT exception_hres)
2612 except_frame_t *except_frame;
2613 call_frame_t *frame;
2614 jsval_t except_val;
2615 BSTR ident;
2616 HRESULT hres;
2618 for(frame = ctx->call_ctx; !frame->except_frame; frame = ctx->call_ctx) {
2619 DWORD flags;
2621 while(frame->scope != frame->base_scope)
2622 scope_pop(&frame->scope);
2624 stack_popn(ctx, ctx->stack_top-frame->stack_base);
2626 flags = frame->flags;
2627 pop_call_frame(ctx);
2628 if(!(flags & EXEC_RETURN_TO_INTERP))
2629 return exception_hres;
2632 except_frame = frame->except_frame;
2633 frame->except_frame = except_frame->next;
2635 assert(except_frame->stack_top <= ctx->stack_top);
2636 stack_popn(ctx, ctx->stack_top - except_frame->stack_top);
2638 while(except_frame->scope != frame->scope)
2639 scope_pop(&frame->scope);
2641 frame->ip = except_frame->catch_off;
2643 except_val = ctx->ei.val;
2644 ctx->ei.val = jsval_undefined();
2645 clear_ei(ctx);
2647 ident = except_frame->ident;
2648 heap_free(except_frame);
2650 if(ident) {
2651 jsdisp_t *scope_obj;
2653 hres = create_dispex(ctx, NULL, NULL, &scope_obj);
2654 if(SUCCEEDED(hres)) {
2655 hres = jsdisp_propput_name(scope_obj, ident, except_val);
2656 if(FAILED(hres))
2657 jsdisp_release(scope_obj);
2659 jsval_release(except_val);
2660 if(FAILED(hres))
2661 return hres;
2663 hres = scope_push(frame->scope, scope_obj, to_disp(scope_obj), &frame->scope);
2664 jsdisp_release(scope_obj);
2665 }else {
2666 hres = stack_push(ctx, except_val);
2667 if(FAILED(hres))
2668 return hres;
2670 hres = stack_push(ctx, jsval_bool(FALSE));
2673 return hres;
2676 static HRESULT enter_bytecode(script_ctx_t *ctx, jsval_t *r)
2678 call_frame_t *frame;
2679 jsop_t op;
2680 HRESULT hres = S_OK;
2682 TRACE("\n");
2684 while(1) {
2685 frame = ctx->call_ctx;
2686 op = frame->bytecode->instrs[frame->ip].op;
2687 hres = op_funcs[op](ctx);
2688 if(FAILED(hres)) {
2689 TRACE("EXCEPTION %08x\n", hres);
2691 hres = unwind_exception(ctx, hres);
2692 if(FAILED(hres))
2693 return hres;
2694 }else if(frame->ip == -1) {
2695 const DWORD return_to_interp = frame->flags & EXEC_RETURN_TO_INTERP;
2697 assert(ctx->stack_top == frame->stack_base);
2698 assert(frame->scope == frame->base_scope);
2700 if(return_to_interp) {
2701 clear_ret(frame->prev_frame);
2702 frame->prev_frame->ret = steal_ret(frame);
2703 }else if(r) {
2704 *r = steal_ret(frame);
2706 pop_call_frame(ctx);
2707 if(!return_to_interp)
2708 break;
2709 }else {
2710 frame->ip += op_move[op];
2714 return S_OK;
2717 static HRESULT bind_event_target(script_ctx_t *ctx, function_code_t *func, jsdisp_t *func_obj)
2719 IBindEventHandler *target;
2720 exprval_t exprval;
2721 IDispatch *disp;
2722 jsval_t v;
2723 HRESULT hres;
2725 hres = identifier_eval(ctx, func->event_target, &exprval);
2726 if(FAILED(hres))
2727 return hres;
2729 hres = exprval_to_value(ctx, &exprval, &v);
2730 if(FAILED(hres))
2731 return hres;
2733 if(!is_object_instance(v)) {
2734 FIXME("Can't bind to %s\n", debugstr_jsval(v));
2735 jsval_release(v);
2738 disp = get_object(v);
2739 hres = IDispatch_QueryInterface(disp, &IID_IBindEventHandler, (void**)&target);
2740 if(SUCCEEDED(hres)) {
2741 hres = IBindEventHandler_BindHandler(target, func->name, (IDispatch*)&func_obj->IDispatchEx_iface);
2742 IBindEventHandler_Release(target);
2743 if(FAILED(hres))
2744 WARN("BindEvent failed: %08x\n", hres);
2745 }else {
2746 FIXME("No IBindEventHandler, not yet supported binding\n");
2749 IDispatch_Release(disp);
2750 return hres;
2753 static HRESULT setup_scope(script_ctx_t *ctx, call_frame_t *frame, unsigned argc, jsval_t *argv)
2755 const unsigned orig_stack = ctx->stack_top;
2756 unsigned i;
2757 jsval_t v;
2758 HRESULT hres;
2760 /* If arguments are already on the stack, we may use them. */
2761 if(argv + argc == ctx->stack + ctx->stack_top) {
2762 frame->arguments_off = argv - ctx->stack;
2763 i = argc;
2764 }else {
2765 frame->arguments_off = ctx->stack_top;
2766 for(i = 0; i < argc; i++) {
2767 hres = jsval_copy(argv[i], &v);
2768 if(SUCCEEDED(hres))
2769 hres = stack_push(ctx, v);
2770 if(FAILED(hres)) {
2771 stack_popn(ctx, i);
2772 return hres;
2777 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2778 for(; i < frame->function->param_cnt; i++) {
2779 hres = stack_push(ctx, jsval_undefined());
2780 if(FAILED(hres)) {
2781 stack_popn(ctx, ctx->stack_top - orig_stack);
2782 return hres;
2786 frame->pop_locals = ctx->stack_top - orig_stack;
2788 frame->variables_off = ctx->stack_top;
2790 for(i = 0; i < frame->function->var_cnt; i++) {
2791 hres = stack_push(ctx, jsval_undefined());
2792 if(FAILED(hres)) {
2793 stack_popn(ctx, ctx->stack_top - orig_stack);
2794 return hres;
2798 frame->pop_variables = i;
2800 for(i = 0; i < frame->function->func_cnt; i++) {
2801 if(frame->function->funcs[i].name && !frame->function->funcs[i].event_target) {
2802 jsdisp_t *func_obj;
2803 unsigned off;
2805 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+i, frame->base_scope, &func_obj);
2806 if(FAILED(hres)) {
2807 stack_popn(ctx, ctx->stack_top - orig_stack);
2808 return hres;
2811 off = local_off(frame, frame->function->funcs[i].local_ref);
2812 jsval_release(ctx->stack[off]);
2813 ctx->stack[off] = jsval_obj(func_obj);
2817 frame->base_scope->frame = frame;
2818 return S_OK;
2821 HRESULT exec_source(script_ctx_t *ctx, DWORD flags, bytecode_t *bytecode, function_code_t *function, scope_chain_t *scope,
2822 IDispatch *this_obj, jsdisp_t *function_instance, jsdisp_t *variable_obj, unsigned argc, jsval_t *argv, jsval_t *r)
2824 call_frame_t *frame;
2825 unsigned i;
2826 HRESULT hres;
2828 for(i = 0; i < function->func_cnt; i++) {
2829 jsdisp_t *func_obj;
2831 if(!function->funcs[i].event_target)
2832 continue;
2834 hres = create_source_function(ctx, bytecode, function->funcs+i, scope, &func_obj);
2835 if(FAILED(hres))
2836 return hres;
2838 hres = bind_event_target(ctx, function->funcs+i, func_obj);
2839 jsdisp_release(func_obj);
2840 if(FAILED(hres))
2841 return hres;
2844 if(flags & (EXEC_GLOBAL | EXEC_EVAL)) {
2845 for(i=0; i < function->var_cnt; i++) {
2846 TRACE("[%d] %s %d\n", i, debugstr_w(function->variables[i].name), function->variables[i].func_id);
2847 if(function->variables[i].func_id != -1) {
2848 jsdisp_t *func_obj;
2850 hres = create_source_function(ctx, bytecode, function->funcs+function->variables[i].func_id, scope, &func_obj);
2851 if(FAILED(hres))
2852 return hres;
2854 hres = jsdisp_propput_name(variable_obj, function->variables[i].name, jsval_obj(func_obj));
2855 jsdisp_release(func_obj);
2856 }else if(!(flags & EXEC_GLOBAL) || !lookup_global_members(ctx, function->variables[i].name, NULL)) {
2857 DISPID id = 0;
2859 hres = jsdisp_get_id(variable_obj, function->variables[i].name, fdexNameEnsure, &id);
2860 if(FAILED(hres))
2861 return hres;
2866 /* ECMA-262 3rd Edition 11.2.3.7 */
2867 if(this_obj) {
2868 jsdisp_t *jsthis;
2870 jsthis = iface_to_jsdisp(this_obj);
2871 if(jsthis) {
2872 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
2873 this_obj = NULL;
2874 jsdisp_release(jsthis);
2878 if(ctx->call_ctx && (flags & EXEC_EVAL)) {
2879 hres = detach_variable_object(ctx, ctx->call_ctx);
2880 if(FAILED(hres))
2881 return hres;
2884 frame = heap_alloc_zero(sizeof(*frame));
2885 if(!frame)
2886 return E_OUTOFMEMORY;
2888 frame->function = function;
2889 frame->ret = jsval_undefined();
2890 frame->argc = argc;
2891 frame->bytecode = bytecode_addref(bytecode);
2893 if(scope) {
2894 frame->base_scope = frame->scope = scope_addref(scope);
2896 if(!(flags & (EXEC_GLOBAL|EXEC_EVAL))) {
2897 hres = setup_scope(ctx, frame, argc, argv);
2898 if(FAILED(hres)) {
2899 release_bytecode(frame->bytecode);
2900 heap_free(frame);
2901 return hres;
2906 frame->ip = function->instr_off;
2907 frame->stack_base = ctx->stack_top;
2908 if(this_obj)
2909 frame->this_obj = this_obj;
2910 else if(ctx->host_global)
2911 frame->this_obj = ctx->host_global;
2912 else
2913 frame->this_obj = to_disp(ctx->global);
2914 IDispatch_AddRef(frame->this_obj);
2916 if(function_instance)
2917 frame->function_instance = jsdisp_addref(function_instance);
2919 frame->flags = flags;
2920 frame->variable_obj = jsdisp_addref(variable_obj);
2922 frame->prev_frame = ctx->call_ctx;
2923 ctx->call_ctx = frame;
2925 if(flags & EXEC_RETURN_TO_INTERP) {
2927 * We're called directly from interpreter, so we may just setup call frame and return.
2928 * Already running interpreter will take care of execution.
2930 if(r)
2931 *r = jsval_undefined();
2932 return S_OK;
2935 return enter_bytecode(ctx, r);