gdi32: Fix copy-paste error (coverity).
[wine/multimedia.git] / dlls / jscript / engine.c
blob44c8333844ef542bd2806b4aeea77be0d9cc0809
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 static HRESULT stack_push(exec_ctx_t *ctx, jsval_t v)
51 if(!ctx->stack_size) {
52 ctx->stack = heap_alloc(16*sizeof(*ctx->stack));
53 if(!ctx->stack)
54 return E_OUTOFMEMORY;
55 ctx->stack_size = 16;
56 }else if(ctx->stack_size == ctx->top) {
57 jsval_t *new_stack;
59 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*new_stack));
60 if(!new_stack) {
61 jsval_release(v);
62 return E_OUTOFMEMORY;
65 ctx->stack = new_stack;
66 ctx->stack_size *= 2;
69 ctx->stack[ctx->top++] = v;
70 return S_OK;
73 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
75 jsstr_t *v;
77 v = jsstr_alloc(str);
78 if(!v)
79 return E_OUTOFMEMORY;
81 return stack_push(ctx, jsval_string(v));
84 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
86 HRESULT hres;
88 hres = stack_push(ctx, jsval_disp(disp));
89 if(FAILED(hres))
90 return hres;
92 return stack_push(ctx, jsval_number(id));
95 static inline jsval_t stack_top(exec_ctx_t *ctx)
97 assert(ctx->top);
98 return ctx->stack[ctx->top-1];
101 static inline jsval_t stack_topn(exec_ctx_t *ctx, unsigned n)
103 assert(ctx->top > n);
104 return ctx->stack[ctx->top-1-n];
107 static inline jsval_t *stack_args(exec_ctx_t *ctx, unsigned n)
109 if(!n)
110 return NULL;
111 assert(ctx->top > n-1);
112 return ctx->stack + ctx->top-n;
115 static inline jsval_t stack_pop(exec_ctx_t *ctx)
117 assert(ctx->top);
118 return ctx->stack[--ctx->top];
121 static void stack_popn(exec_ctx_t *ctx, unsigned n)
123 while(n--)
124 jsval_release(stack_pop(ctx));
127 static HRESULT stack_pop_number(exec_ctx_t *ctx, double *r)
129 jsval_t v;
130 HRESULT hres;
132 v = stack_pop(ctx);
133 hres = to_number(ctx->script, v, r);
134 jsval_release(v);
135 return hres;
138 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
140 jsval_t v;
141 HRESULT hres;
143 v = stack_pop(ctx);
144 if(is_object_instance(v)) {
145 if(!get_object(v))
146 return throw_type_error(ctx->script, JS_E_OBJECT_REQUIRED, NULL);
147 *r = get_object(v);
148 return S_OK;
151 hres = to_object(ctx->script, v, r);
152 jsval_release(v);
153 return hres;
156 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
158 return to_int32(ctx->script, stack_pop(ctx), r);
161 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
163 return to_uint32(ctx->script, stack_pop(ctx), r);
166 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
168 assert(is_number(stack_top(ctx)) && is_object_instance(stack_topn(ctx, 1)));
170 *id = get_number(stack_pop(ctx));
171 return get_object(stack_pop(ctx));
174 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
176 assert(is_number(stack_topn(ctx, n)) && is_object_instance(stack_topn(ctx, n+1)));
178 *id = get_number(stack_topn(ctx, n));
179 return get_object(stack_topn(ctx, n+1));
182 static void exprval_release(exprval_t *val)
184 switch(val->type) {
185 case EXPRVAL_JSVAL:
186 jsval_release(val->u.val);
187 return;
188 case EXPRVAL_IDREF:
189 if(val->u.idref.disp)
190 IDispatch_Release(val->u.idref.disp);
191 return;
192 case EXPRVAL_INVALID:
193 return;
197 /* ECMA-262 3rd Edition 8.7.1 */
198 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsval_t *ret)
200 switch(val->type) {
201 case EXPRVAL_JSVAL:
202 *ret = val->u.val;
203 val->u.val = jsval_undefined();
204 return S_OK;
205 case EXPRVAL_IDREF:
206 if(!val->u.idref.disp) {
207 FIXME("throw ReferenceError\n");
208 return E_FAIL;
211 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret);
212 case EXPRVAL_INVALID:
213 assert(0);
216 ERR("type %d\n", val->type);
217 return E_FAIL;
220 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
222 val->type = EXPRVAL_IDREF;
223 val->u.idref.disp = disp;
224 val->u.idref.id = id;
226 if(disp)
227 IDispatch_AddRef(disp);
230 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
232 scope_chain_t *new_scope;
234 new_scope = heap_alloc(sizeof(scope_chain_t));
235 if(!new_scope)
236 return E_OUTOFMEMORY;
238 new_scope->ref = 1;
240 IDispatch_AddRef(obj);
241 new_scope->jsobj = jsobj;
242 new_scope->obj = obj;
244 if(scope) {
245 scope_addref(scope);
246 new_scope->next = scope;
247 }else {
248 new_scope->next = NULL;
251 *ret = new_scope;
252 return S_OK;
255 static void scope_pop(scope_chain_t **scope)
257 scope_chain_t *tmp;
259 tmp = *scope;
260 *scope = tmp->next;
261 scope_release(tmp);
264 void clear_ei(script_ctx_t *ctx)
266 memset(&ctx->ei.ei, 0, sizeof(ctx->ei.ei));
267 jsval_release(ctx->ei.val);
268 ctx->ei.val = jsval_undefined();
271 void scope_release(scope_chain_t *scope)
273 if(--scope->ref)
274 return;
276 if(scope->next)
277 scope_release(scope->next);
279 IDispatch_Release(scope->obj);
280 heap_free(scope);
283 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
284 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
286 exec_ctx_t *ctx;
288 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
289 if(!ctx)
290 return E_OUTOFMEMORY;
292 ctx->ref = 1;
293 ctx->is_global = is_global;
295 if(this_obj)
296 ctx->this_obj = this_obj;
297 else if(script_ctx->host_global)
298 ctx->this_obj = script_ctx->host_global;
299 else
300 ctx->this_obj = to_disp(script_ctx->global);
301 IDispatch_AddRef(ctx->this_obj);
303 jsdisp_addref(var_disp);
304 ctx->var_disp = var_disp;
306 script_addref(script_ctx);
307 ctx->script = script_ctx;
309 if(scope) {
310 scope_addref(scope);
311 ctx->scope_chain = scope;
314 *ret = ctx;
315 return S_OK;
318 void exec_release(exec_ctx_t *ctx)
320 if(--ctx->ref)
321 return;
323 if(ctx->scope_chain)
324 scope_release(ctx->scope_chain);
325 if(ctx->var_disp)
326 jsdisp_release(ctx->var_disp);
327 if(ctx->this_obj)
328 IDispatch_Release(ctx->this_obj);
329 if(ctx->script)
330 script_release(ctx->script);
331 heap_free(ctx->stack);
332 heap_free(ctx);
335 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
337 IDispatchEx *dispex;
338 jsdisp_t *jsdisp;
339 HRESULT hres;
341 jsdisp = iface_to_jsdisp((IUnknown*)disp);
342 if(jsdisp) {
343 hres = jsdisp_get_id(jsdisp, name, flags, id);
344 jsdisp_release(jsdisp);
345 return hres;
348 *id = 0;
349 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
350 if(SUCCEEDED(hres)) {
351 BSTR str = name_bstr;
353 if(!str)
354 str = SysAllocString(name);
355 if(str)
356 hres = IDispatchEx_GetDispID(dispex, str, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
357 else
358 hres = E_OUTOFMEMORY;
359 IDispatchEx_Release(dispex);
360 return hres;
363 TRACE("using IDispatch\n");
365 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
368 static inline BOOL var_is_null(const VARIANT *v)
370 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
373 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
375 IObjectIdentity *identity;
376 IUnknown *unk1, *unk2;
377 HRESULT hres;
379 if(disp1 == disp2) {
380 *ret = TRUE;
381 return S_OK;
384 if(!disp1 || !disp2) {
385 *ret = FALSE;
386 return S_OK;
389 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
390 if(FAILED(hres))
391 return hres;
393 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
394 if(FAILED(hres)) {
395 IUnknown_Release(unk1);
396 return hres;
399 if(unk1 == unk2) {
400 *ret = TRUE;
401 }else {
402 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
403 if(SUCCEEDED(hres)) {
404 hres = IObjectIdentity_IsEqualObject(identity, unk2);
405 IObjectIdentity_Release(identity);
406 *ret = hres == S_OK;
407 }else {
408 *ret = FALSE;
412 IUnknown_Release(unk1);
413 IUnknown_Release(unk2);
414 return S_OK;
417 /* ECMA-262 3rd Edition 11.9.6 */
418 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
420 jsval_type_t type = jsval_type(lval);
422 TRACE("\n");
424 if(type != jsval_type(rval)) {
425 if(is_null_instance(lval))
426 *ret = is_null_instance(rval);
427 else
428 *ret = FALSE;
429 return S_OK;
432 switch(type) {
433 case JSV_UNDEFINED:
434 case JSV_NULL:
435 *ret = TRUE;
436 break;
437 case JSV_OBJECT:
438 return disp_cmp(get_object(lval), get_object(rval), ret);
439 case JSV_STRING:
440 *ret = jsstr_eq(get_string(lval), get_string(rval));
441 break;
442 case JSV_NUMBER:
443 *ret = get_number(lval) == get_number(rval);
444 break;
445 case JSV_BOOL:
446 *ret = !get_bool(lval) == !get_bool(rval);
447 break;
448 case JSV_VARIANT:
449 FIXME("VARIANT not implemented\n");
450 return E_NOTIMPL;
453 return S_OK;
456 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
458 named_item_t *item;
459 DISPID id;
460 HRESULT hres;
462 for(item = ctx->named_items; item; item = item->next) {
463 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
464 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
465 if(SUCCEEDED(hres)) {
466 if(ret)
467 exprval_set_idref(ret, item->disp, id);
468 return TRUE;
473 return FALSE;
476 /* ECMA-262 3rd Edition 10.1.4 */
477 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
479 scope_chain_t *scope;
480 named_item_t *item;
481 DISPID id = 0;
482 HRESULT hres;
484 TRACE("%s\n", debugstr_w(identifier));
486 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
487 if(scope->jsobj)
488 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
489 else
490 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
491 if(SUCCEEDED(hres)) {
492 exprval_set_idref(ret, scope->obj, id);
493 return S_OK;
497 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
498 if(SUCCEEDED(hres)) {
499 exprval_set_idref(ret, to_disp(ctx->global), id);
500 return S_OK;
503 for(item = ctx->named_items; item; item = item->next) {
504 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
505 if(!item->disp) {
506 IUnknown *unk;
508 if(!ctx->site)
509 break;
511 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
512 SCRIPTINFO_IUNKNOWN, &unk, NULL);
513 if(FAILED(hres)) {
514 WARN("GetItemInfo failed: %08x\n", hres);
515 break;
518 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
519 IUnknown_Release(unk);
520 if(FAILED(hres)) {
521 WARN("object does not implement IDispatch\n");
522 break;
526 IDispatch_AddRef(item->disp);
527 ret->type = EXPRVAL_JSVAL;
528 ret->u.val = jsval_disp(item->disp);
529 return S_OK;
533 if(lookup_global_members(ctx, identifier, ret))
534 return S_OK;
536 ret->type = EXPRVAL_INVALID;
537 return S_OK;
540 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
541 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
544 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
545 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
548 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
549 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
552 static inline jsstr_t *get_op_str(exec_ctx_t *ctx, int i){
553 return ctx->code->instrs[ctx->ip].u.arg[i].str;
556 static inline double get_op_double(exec_ctx_t *ctx){
557 return ctx->code->instrs[ctx->ip].u.dbl;
560 /* ECMA-262 3rd Edition 12.2 */
561 static HRESULT interp_var_set(exec_ctx_t *ctx)
563 const BSTR name = get_op_bstr(ctx, 0);
564 jsval_t val;
565 HRESULT hres;
567 TRACE("%s\n", debugstr_w(name));
569 val = stack_pop(ctx);
570 hres = jsdisp_propput_name(ctx->var_disp, name, val);
571 jsval_release(val);
572 return hres;
575 /* ECMA-262 3rd Edition 12.6.4 */
576 static HRESULT interp_forin(exec_ctx_t *ctx)
578 const HRESULT arg = get_op_uint(ctx, 0);
579 IDispatch *var_obj, *obj = NULL;
580 IDispatchEx *dispex;
581 DISPID id, var_id;
582 BSTR name = NULL;
583 jsval_t val;
584 HRESULT hres;
586 TRACE("\n");
588 val = stack_pop(ctx);
590 assert(is_number(stack_top(ctx)));
591 id = get_number(stack_top(ctx));
593 var_obj = stack_topn_objid(ctx, 1, &var_id);
594 if(!var_obj) {
595 FIXME("invalid ref\n");
596 jsval_release(val);
597 return E_FAIL;
600 if(is_object_instance(stack_topn(ctx, 3)))
601 obj = get_object(stack_topn(ctx, 3));
603 if(obj) {
604 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
605 if(SUCCEEDED(hres)) {
606 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
607 if(hres == S_OK)
608 hres = IDispatchEx_GetMemberName(dispex, id, &name);
609 IDispatchEx_Release(dispex);
610 if(FAILED(hres)) {
611 jsval_release(val);
612 return hres;
614 }else {
615 TRACE("No IDispatchEx\n");
619 if(name) {
620 jsstr_t *str;
622 str = jsstr_alloc_len(name, SysStringLen(name));
623 SysFreeString(name);
624 if(!str)
625 return E_OUTOFMEMORY;
627 jsval_release(val);
628 stack_pop(ctx);
629 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
631 hres = disp_propput(ctx->script, var_obj, var_id, jsval_string(str));
632 jsstr_release(str);
633 if(FAILED(hres))
634 return hres;
636 ctx->ip++;
637 }else {
638 stack_popn(ctx, 4);
639 ctx->ip = arg;
640 return stack_push(ctx, val);
642 return S_OK;
645 /* ECMA-262 3rd Edition 12.10 */
646 static HRESULT interp_push_scope(exec_ctx_t *ctx)
648 IDispatch *disp;
649 jsval_t v;
650 HRESULT hres;
652 TRACE("\n");
654 v = stack_pop(ctx);
655 hres = to_object(ctx->script, v, &disp);
656 jsval_release(v);
657 if(FAILED(hres))
658 return hres;
660 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
661 IDispatch_Release(disp);
662 return hres;
665 /* ECMA-262 3rd Edition 12.10 */
666 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
668 TRACE("\n");
670 scope_pop(&ctx->scope_chain);
671 return S_OK;
674 /* ECMA-262 3rd Edition 12.13 */
675 static HRESULT interp_case(exec_ctx_t *ctx)
677 const unsigned arg = get_op_uint(ctx, 0);
678 jsval_t v;
679 BOOL b;
680 HRESULT hres;
682 TRACE("\n");
684 v = stack_pop(ctx);
685 hres = equal2_values(stack_top(ctx), v, &b);
686 jsval_release(v);
687 if(FAILED(hres))
688 return hres;
690 if(b) {
691 stack_popn(ctx, 1);
692 ctx->ip = arg;
693 }else {
694 ctx->ip++;
696 return S_OK;
699 /* ECMA-262 3rd Edition 12.13 */
700 static HRESULT interp_throw(exec_ctx_t *ctx)
702 TRACE("\n");
704 jsval_release(ctx->script->ei.val);
705 ctx->script->ei.val = stack_pop(ctx);
706 return DISP_E_EXCEPTION;
709 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
711 const HRESULT arg = get_op_uint(ctx, 0);
713 TRACE("%08x\n", arg);
715 return throw_reference_error(ctx->script, arg, NULL);
718 static HRESULT interp_throw_type(exec_ctx_t *ctx)
720 const HRESULT hres = get_op_uint(ctx, 0);
721 jsstr_t *str = get_op_str(ctx, 1);
723 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
725 return throw_type_error(ctx->script, hres, str->str);
728 /* ECMA-262 3rd Edition 12.14 */
729 static HRESULT interp_push_except(exec_ctx_t *ctx)
731 const unsigned arg1 = get_op_uint(ctx, 0);
732 const BSTR arg2 = get_op_bstr(ctx, 1);
733 except_frame_t *except;
734 unsigned stack_top;
736 TRACE("\n");
738 stack_top = ctx->top;
740 if(!arg2) {
741 HRESULT hres;
743 hres = stack_push(ctx, jsval_bool(TRUE));
744 if(FAILED(hres))
745 return hres;
746 hres = stack_push(ctx, jsval_bool(TRUE));
747 if(FAILED(hres))
748 return hres;
751 except = heap_alloc(sizeof(*except));
752 if(!except)
753 return E_OUTOFMEMORY;
755 except->stack_top = stack_top;
756 except->scope = ctx->scope_chain;
757 except->catch_off = arg1;
758 except->ident = arg2;
759 except->next = ctx->except_frame;
760 ctx->except_frame = except;
761 return S_OK;
764 /* ECMA-262 3rd Edition 12.14 */
765 static HRESULT interp_pop_except(exec_ctx_t *ctx)
767 except_frame_t *except;
769 TRACE("\n");
771 except = ctx->except_frame;
772 assert(except != NULL);
774 ctx->except_frame = except->next;
775 heap_free(except);
776 return S_OK;
779 /* ECMA-262 3rd Edition 12.14 */
780 static HRESULT interp_end_finally(exec_ctx_t *ctx)
782 jsval_t v;
784 TRACE("\n");
786 v = stack_pop(ctx);
788 assert(is_bool(stack_top(ctx)));
789 if(!get_bool(stack_top(ctx))) {
790 TRACE("passing exception\n");
792 jsval_release(v);
793 stack_popn(ctx, 1);
795 ctx->script->ei.val = stack_pop(ctx);
796 return DISP_E_EXCEPTION;
799 stack_popn(ctx, 2);
800 return stack_push(ctx, v);
803 /* ECMA-262 3rd Edition 13 */
804 static HRESULT interp_func(exec_ctx_t *ctx)
806 unsigned func_idx = get_op_uint(ctx, 0);
807 jsdisp_t *dispex;
808 HRESULT hres;
810 TRACE("%d\n", func_idx);
812 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
813 ctx->scope_chain, &dispex);
814 if(FAILED(hres))
815 return hres;
817 return stack_push(ctx, jsval_obj(dispex));
820 /* ECMA-262 3rd Edition 11.2.1 */
821 static HRESULT interp_array(exec_ctx_t *ctx)
823 jsval_t v, namev;
824 IDispatch *obj;
825 jsstr_t *name;
826 DISPID id;
827 HRESULT hres;
829 TRACE("\n");
831 namev = stack_pop(ctx);
833 hres = stack_pop_object(ctx, &obj);
834 if(FAILED(hres)) {
835 jsval_release(namev);
836 return hres;
839 hres = to_string(ctx->script, namev, &name);
840 jsval_release(namev);
841 if(FAILED(hres)) {
842 IDispatch_Release(obj);
843 return hres;
846 hres = disp_get_id(ctx->script, obj, name->str, NULL, 0, &id);
847 jsstr_release(name);
848 if(SUCCEEDED(hres)) {
849 hres = disp_propget(ctx->script, obj, id, &v);
850 }else if(hres == DISP_E_UNKNOWNNAME) {
851 v = jsval_undefined();
852 hres = S_OK;
854 IDispatch_Release(obj);
855 if(FAILED(hres))
856 return hres;
858 return stack_push(ctx, v);
861 /* ECMA-262 3rd Edition 11.2.1 */
862 static HRESULT interp_member(exec_ctx_t *ctx)
864 const BSTR arg = get_op_bstr(ctx, 0);
865 IDispatch *obj;
866 jsval_t v;
867 DISPID id;
868 HRESULT hres;
870 TRACE("\n");
872 hres = stack_pop_object(ctx, &obj);
873 if(FAILED(hres))
874 return hres;
876 hres = disp_get_id(ctx->script, obj, arg, arg, 0, &id);
877 if(SUCCEEDED(hres)) {
878 hres = disp_propget(ctx->script, obj, id, &v);
879 }else if(hres == DISP_E_UNKNOWNNAME) {
880 v = jsval_undefined();
881 hres = S_OK;
883 IDispatch_Release(obj);
884 if(FAILED(hres))
885 return hres;
887 return stack_push(ctx, v);
890 /* ECMA-262 3rd Edition 11.2.1 */
891 static HRESULT interp_memberid(exec_ctx_t *ctx)
893 const unsigned arg = get_op_uint(ctx, 0);
894 jsval_t objv, namev;
895 IDispatch *obj;
896 jsstr_t *name;
897 DISPID id;
898 HRESULT hres;
900 TRACE("%x\n", arg);
902 namev = stack_pop(ctx);
903 objv = stack_pop(ctx);
905 hres = to_object(ctx->script, objv, &obj);
906 jsval_release(objv);
907 if(SUCCEEDED(hres)) {
908 hres = to_string(ctx->script, namev, &name);
909 if(FAILED(hres))
910 IDispatch_Release(obj);
912 jsval_release(namev);
913 if(FAILED(hres))
914 return hres;
916 hres = disp_get_id(ctx->script, obj, name->str, NULL, arg, &id);
917 jsstr_release(name);
918 if(FAILED(hres)) {
919 IDispatch_Release(obj);
920 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
921 obj = NULL;
922 id = JS_E_INVALID_PROPERTY;
923 }else {
924 ERR("failed %08x\n", hres);
925 return hres;
929 return stack_push_objid(ctx, obj, id);
932 /* ECMA-262 3rd Edition 11.2.1 */
933 static HRESULT interp_refval(exec_ctx_t *ctx)
935 IDispatch *disp;
936 jsval_t v;
937 DISPID id;
938 HRESULT hres;
940 TRACE("\n");
942 disp = stack_topn_objid(ctx, 0, &id);
943 if(!disp)
944 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
946 hres = disp_propget(ctx->script, disp, id, &v);
947 if(FAILED(hres))
948 return hres;
950 return stack_push(ctx, v);
953 /* ECMA-262 3rd Edition 11.2.2 */
954 static HRESULT interp_new(exec_ctx_t *ctx)
956 const unsigned argc = get_op_uint(ctx, 0);
957 jsval_t r, constr;
958 HRESULT hres;
960 TRACE("%d\n", argc);
962 constr = stack_topn(ctx, argc);
964 /* NOTE: Should use to_object here */
966 if(is_null(constr))
967 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
968 else if(!is_object_instance(constr))
969 return throw_type_error(ctx->script, JS_E_INVALID_ACTION, NULL);
970 else if(!get_object(constr))
971 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
973 hres = disp_call_value(ctx->script, get_object(constr), NULL, DISPATCH_CONSTRUCT, argc, stack_args(ctx, argc), &r);
974 if(FAILED(hres))
975 return hres;
977 stack_popn(ctx, argc+1);
978 return stack_push(ctx, r);
981 /* ECMA-262 3rd Edition 11.2.3 */
982 static HRESULT interp_call(exec_ctx_t *ctx)
984 const unsigned argn = get_op_uint(ctx, 0);
985 const int do_ret = get_op_int(ctx, 1);
986 jsval_t r, obj;
987 HRESULT hres;
989 TRACE("%d %d\n", argn, do_ret);
991 obj = stack_topn(ctx, argn);
992 if(!is_object_instance(obj))
993 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
995 hres = disp_call_value(ctx->script, get_object(obj), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
996 do_ret ? &r : NULL);
997 if(FAILED(hres))
998 return hres;
1000 stack_popn(ctx, argn+1);
1001 return do_ret ? stack_push(ctx, r) : S_OK;
1004 /* ECMA-262 3rd Edition 11.2.3 */
1005 static HRESULT interp_call_member(exec_ctx_t *ctx)
1007 const unsigned argn = get_op_uint(ctx, 0);
1008 const int do_ret = get_op_int(ctx, 1);
1009 IDispatch *obj;
1010 jsval_t r;
1011 DISPID id;
1012 HRESULT hres;
1014 TRACE("%d %d\n", argn, do_ret);
1016 obj = stack_topn_objid(ctx, argn, &id);
1017 if(!obj)
1018 return throw_type_error(ctx->script, id, NULL);
1020 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &r : NULL);
1021 if(FAILED(hres))
1022 return hres;
1024 stack_popn(ctx, argn+2);
1025 return do_ret ? stack_push(ctx, r) : S_OK;
1029 /* ECMA-262 3rd Edition 11.1.1 */
1030 static HRESULT interp_this(exec_ctx_t *ctx)
1032 TRACE("\n");
1034 IDispatch_AddRef(ctx->this_obj);
1035 return stack_push(ctx, jsval_disp(ctx->this_obj));
1038 /* ECMA-262 3rd Edition 10.1.4 */
1039 static HRESULT interp_ident(exec_ctx_t *ctx)
1041 const BSTR arg = get_op_bstr(ctx, 0);
1042 exprval_t exprval;
1043 jsval_t v;
1044 HRESULT hres;
1046 TRACE("%s\n", debugstr_w(arg));
1048 hres = identifier_eval(ctx->script, arg, &exprval);
1049 if(FAILED(hres))
1050 return hres;
1052 if(exprval.type == EXPRVAL_INVALID)
1053 return throw_type_error(ctx->script, JS_E_UNDEFINED_VARIABLE, arg);
1055 hres = exprval_to_value(ctx->script, &exprval, &v);
1056 exprval_release(&exprval);
1057 if(FAILED(hres))
1058 return hres;
1060 return stack_push(ctx, v);
1063 /* ECMA-262 3rd Edition 10.1.4 */
1064 static HRESULT interp_identid(exec_ctx_t *ctx)
1066 const BSTR arg = get_op_bstr(ctx, 0);
1067 const unsigned flags = get_op_uint(ctx, 1);
1068 exprval_t exprval;
1069 HRESULT hres;
1071 TRACE("%s %x\n", debugstr_w(arg), flags);
1073 hres = identifier_eval(ctx->script, arg, &exprval);
1074 if(FAILED(hres))
1075 return hres;
1077 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1078 DISPID id;
1080 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1081 if(FAILED(hres))
1082 return hres;
1084 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1087 if(exprval.type != EXPRVAL_IDREF) {
1088 WARN("invalid ref\n");
1089 exprval_release(&exprval);
1090 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1093 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1096 /* ECMA-262 3rd Edition 7.8.1 */
1097 static HRESULT interp_null(exec_ctx_t *ctx)
1099 TRACE("\n");
1101 return stack_push(ctx, jsval_null());
1104 /* ECMA-262 3rd Edition 7.8.2 */
1105 static HRESULT interp_bool(exec_ctx_t *ctx)
1107 const int arg = get_op_int(ctx, 0);
1109 TRACE("%s\n", arg ? "true" : "false");
1111 return stack_push(ctx, jsval_bool(arg));
1114 /* ECMA-262 3rd Edition 7.8.3 */
1115 static HRESULT interp_int(exec_ctx_t *ctx)
1117 const int arg = get_op_int(ctx, 0);
1119 TRACE("%d\n", arg);
1121 return stack_push(ctx, jsval_number(arg));
1124 /* ECMA-262 3rd Edition 7.8.3 */
1125 static HRESULT interp_double(exec_ctx_t *ctx)
1127 const double arg = get_op_double(ctx);
1129 TRACE("%lf\n", arg);
1131 return stack_push(ctx, jsval_number(arg));
1134 /* ECMA-262 3rd Edition 7.8.4 */
1135 static HRESULT interp_str(exec_ctx_t *ctx)
1137 jsstr_t *str = get_op_str(ctx, 0);
1139 TRACE("%s\n", debugstr_jsstr(str));
1141 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1144 /* ECMA-262 3rd Edition 7.8 */
1145 static HRESULT interp_regexp(exec_ctx_t *ctx)
1147 jsstr_t *source = get_op_str(ctx, 0);
1148 const unsigned flags = get_op_uint(ctx, 1);
1149 jsdisp_t *regexp;
1150 HRESULT hres;
1152 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1154 hres = create_regexp(ctx->script, source, flags, &regexp);
1155 if(FAILED(hres))
1156 return hres;
1158 return stack_push(ctx, jsval_obj(regexp));
1161 /* ECMA-262 3rd Edition 11.1.4 */
1162 static HRESULT interp_carray(exec_ctx_t *ctx)
1164 const unsigned arg = get_op_uint(ctx, 0);
1165 jsdisp_t *array;
1166 jsval_t val;
1167 unsigned i;
1168 HRESULT hres;
1170 TRACE("%u\n", arg);
1172 hres = create_array(ctx->script, arg, &array);
1173 if(FAILED(hres))
1174 return hres;
1176 i = arg;
1177 while(i--) {
1178 val = stack_pop(ctx);
1179 hres = jsdisp_propput_idx(array, i, val);
1180 jsval_release(val);
1181 if(FAILED(hres)) {
1182 jsdisp_release(array);
1183 return hres;
1187 return stack_push(ctx, jsval_obj(array));
1190 /* ECMA-262 3rd Edition 11.1.5 */
1191 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1193 jsdisp_t *obj;
1194 HRESULT hres;
1196 TRACE("\n");
1198 hres = create_object(ctx->script, NULL, &obj);
1199 if(FAILED(hres))
1200 return hres;
1202 return stack_push(ctx, jsval_obj(obj));
1205 /* ECMA-262 3rd Edition 11.1.5 */
1206 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1208 const BSTR name = get_op_bstr(ctx, 0);
1209 jsdisp_t *obj;
1210 jsval_t val;
1211 HRESULT hres;
1213 TRACE("%s\n", debugstr_w(name));
1215 val = stack_pop(ctx);
1217 assert(is_object_instance(stack_top(ctx)));
1218 obj = as_jsdisp(get_object(stack_top(ctx)));
1220 hres = jsdisp_propput_name(obj, name, val);
1221 jsval_release(val);
1222 return hres;
1225 /* ECMA-262 3rd Edition 11.11 */
1226 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1228 const unsigned arg = get_op_uint(ctx, 0);
1229 BOOL b;
1230 HRESULT hres;
1232 TRACE("\n");
1234 hres = to_boolean(stack_top(ctx), &b);
1235 if(FAILED(hres))
1236 return hres;
1238 if(b) {
1239 ctx->ip = arg;
1240 }else {
1241 stack_popn(ctx, 1);
1242 ctx->ip++;
1244 return S_OK;
1247 /* ECMA-262 3rd Edition 11.11 */
1248 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1250 const unsigned arg = get_op_uint(ctx, 0);
1251 BOOL b;
1252 HRESULT hres;
1254 TRACE("\n");
1256 hres = to_boolean(stack_top(ctx), &b);
1257 if(FAILED(hres))
1258 return hres;
1260 if(b) {
1261 stack_popn(ctx, 1);
1262 ctx->ip++;
1263 }else {
1264 ctx->ip = arg;
1266 return S_OK;
1269 /* ECMA-262 3rd Edition 11.10 */
1270 static HRESULT interp_or(exec_ctx_t *ctx)
1272 INT l, r;
1273 HRESULT hres;
1275 TRACE("\n");
1277 hres = stack_pop_int(ctx, &r);
1278 if(FAILED(hres))
1279 return hres;
1281 hres = stack_pop_int(ctx, &l);
1282 if(FAILED(hres))
1283 return hres;
1285 return stack_push(ctx, jsval_number(l|r));
1288 /* ECMA-262 3rd Edition 11.10 */
1289 static HRESULT interp_xor(exec_ctx_t *ctx)
1291 INT l, r;
1292 HRESULT hres;
1294 TRACE("\n");
1296 hres = stack_pop_int(ctx, &r);
1297 if(FAILED(hres))
1298 return hres;
1300 hres = stack_pop_int(ctx, &l);
1301 if(FAILED(hres))
1302 return hres;
1304 return stack_push(ctx, jsval_number(l^r));
1307 /* ECMA-262 3rd Edition 11.10 */
1308 static HRESULT interp_and(exec_ctx_t *ctx)
1310 INT l, r;
1311 HRESULT hres;
1313 TRACE("\n");
1315 hres = stack_pop_int(ctx, &r);
1316 if(FAILED(hres))
1317 return hres;
1319 hres = stack_pop_int(ctx, &l);
1320 if(FAILED(hres))
1321 return hres;
1323 return stack_push(ctx, jsval_number(l&r));
1326 /* ECMA-262 3rd Edition 11.8.6 */
1327 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1329 jsdisp_t *obj, *iter, *tmp = NULL;
1330 jsval_t prot, v;
1331 BOOL ret = FALSE;
1332 HRESULT hres;
1334 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1336 v = stack_pop(ctx);
1337 if(!is_object_instance(v) || !get_object(v)) {
1338 jsval_release(v);
1339 return throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1342 obj = iface_to_jsdisp((IUnknown*)get_object(v));
1343 IDispatch_Release(get_object(v));
1344 if(!obj) {
1345 FIXME("non-jsdisp objects not supported\n");
1346 return E_FAIL;
1349 if(is_class(obj, JSCLASS_FUNCTION)) {
1350 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1351 }else {
1352 hres = throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1354 jsdisp_release(obj);
1355 if(FAILED(hres))
1356 return hres;
1358 v = stack_pop(ctx);
1360 if(is_object_instance(prot)) {
1361 if(is_object_instance(v))
1362 tmp = iface_to_jsdisp((IUnknown*)get_object(v));
1363 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1364 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1365 if(FAILED(hres))
1366 break;
1369 if(tmp)
1370 jsdisp_release(tmp);
1371 }else {
1372 FIXME("prototype is not an object\n");
1373 hres = E_FAIL;
1376 jsval_release(prot);
1377 jsval_release(v);
1378 if(FAILED(hres))
1379 return hres;
1381 return stack_push(ctx, jsval_bool(ret));
1384 /* ECMA-262 3rd Edition 11.8.7 */
1385 static HRESULT interp_in(exec_ctx_t *ctx)
1387 jsval_t obj, v;
1388 DISPID id = 0;
1389 BOOL ret;
1390 jsstr_t *str;
1391 HRESULT hres;
1393 TRACE("\n");
1395 obj = stack_pop(ctx);
1396 if(!is_object_instance(obj) || !get_object(obj)) {
1397 jsval_release(obj);
1398 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1401 v = stack_pop(ctx);
1402 hres = to_string(ctx->script, v, &str);
1403 jsval_release(v);
1404 if(FAILED(hres)) {
1405 IDispatch_Release(get_object(obj));
1406 return hres;
1409 hres = disp_get_id(ctx->script, get_object(obj), str->str, NULL, 0, &id);
1410 IDispatch_Release(get_object(obj));
1411 jsstr_release(str);
1412 if(SUCCEEDED(hres))
1413 ret = TRUE;
1414 else if(hres == DISP_E_UNKNOWNNAME)
1415 ret = FALSE;
1416 else
1417 return hres;
1419 return stack_push(ctx, jsval_bool(ret));
1422 /* ECMA-262 3rd Edition 11.6.1 */
1423 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1425 jsval_t r, l;
1426 HRESULT hres;
1428 hres = to_primitive(ctx, lval, &l, NO_HINT);
1429 if(FAILED(hres))
1430 return hres;
1432 hres = to_primitive(ctx, rval, &r, NO_HINT);
1433 if(FAILED(hres)) {
1434 jsval_release(l);
1435 return hres;
1438 if(is_string(l) || is_string(r)) {
1439 jsstr_t *lstr, *rstr = NULL;
1441 hres = to_string(ctx, l, &lstr);
1442 if(SUCCEEDED(hres))
1443 hres = to_string(ctx, r, &rstr);
1445 if(SUCCEEDED(hres)) {
1446 unsigned len1, len2;
1447 jsstr_t *ret_str;
1449 len1 = jsstr_length(lstr);
1450 len2 = jsstr_length(rstr);
1452 ret_str = jsstr_alloc_buf(len1+len2);
1453 if(ret_str) {
1454 if(len1)
1455 memcpy(ret_str->str, lstr->str, len1*sizeof(WCHAR));
1456 if(len2)
1457 memcpy(ret_str->str+len1, rstr->str, len2*sizeof(WCHAR));
1458 *ret = jsval_string(ret_str);
1459 }else {
1460 hres = E_OUTOFMEMORY;
1464 jsstr_release(lstr);
1465 if(rstr)
1466 jsstr_release(rstr);
1467 }else {
1468 double nl, nr;
1470 hres = to_number(ctx, l, &nl);
1471 if(SUCCEEDED(hres)) {
1472 hres = to_number(ctx, r, &nr);
1473 if(SUCCEEDED(hres))
1474 *ret = jsval_number(nl+nr);
1478 jsval_release(r);
1479 jsval_release(l);
1480 return hres;
1483 /* ECMA-262 3rd Edition 11.6.1 */
1484 static HRESULT interp_add(exec_ctx_t *ctx)
1486 jsval_t l, r, ret;
1487 HRESULT hres;
1489 r = stack_pop(ctx);
1490 l = stack_pop(ctx);
1492 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1494 hres = add_eval(ctx->script, l, r, &ret);
1495 jsval_release(l);
1496 jsval_release(r);
1497 if(FAILED(hres))
1498 return hres;
1500 return stack_push(ctx, ret);
1503 /* ECMA-262 3rd Edition 11.6.2 */
1504 static HRESULT interp_sub(exec_ctx_t *ctx)
1506 double l, r;
1507 HRESULT hres;
1509 TRACE("\n");
1511 hres = stack_pop_number(ctx, &r);
1512 if(FAILED(hres))
1513 return hres;
1515 hres = stack_pop_number(ctx, &l);
1516 if(FAILED(hres))
1517 return hres;
1519 return stack_push(ctx, jsval_number(l-r));
1522 /* ECMA-262 3rd Edition 11.5.1 */
1523 static HRESULT interp_mul(exec_ctx_t *ctx)
1525 double l, r;
1526 HRESULT hres;
1528 TRACE("\n");
1530 hres = stack_pop_number(ctx, &r);
1531 if(FAILED(hres))
1532 return hres;
1534 hres = stack_pop_number(ctx, &l);
1535 if(FAILED(hres))
1536 return hres;
1538 return stack_push(ctx, jsval_number(l*r));
1541 /* ECMA-262 3rd Edition 11.5.2 */
1542 static HRESULT interp_div(exec_ctx_t *ctx)
1544 double l, r;
1545 HRESULT hres;
1547 TRACE("\n");
1549 hres = stack_pop_number(ctx, &r);
1550 if(FAILED(hres))
1551 return hres;
1553 hres = stack_pop_number(ctx, &l);
1554 if(FAILED(hres))
1555 return hres;
1557 return stack_push(ctx, jsval_number(l/r));
1560 /* ECMA-262 3rd Edition 11.5.3 */
1561 static HRESULT interp_mod(exec_ctx_t *ctx)
1563 double l, r;
1564 HRESULT hres;
1566 TRACE("\n");
1568 hres = stack_pop_number(ctx, &r);
1569 if(FAILED(hres))
1570 return hres;
1572 hres = stack_pop_number(ctx, &l);
1573 if(FAILED(hres))
1574 return hres;
1576 return stack_push(ctx, jsval_number(fmod(l, r)));
1579 /* ECMA-262 3rd Edition 11.4.2 */
1580 static HRESULT interp_delete(exec_ctx_t *ctx)
1582 jsval_t objv, namev;
1583 IDispatchEx *dispex;
1584 IDispatch *obj;
1585 jsstr_t *name;
1586 BOOL ret;
1587 HRESULT hres;
1589 TRACE("\n");
1591 namev = stack_pop(ctx);
1592 objv = stack_pop(ctx);
1594 hres = to_object(ctx->script, objv, &obj);
1595 jsval_release(objv);
1596 if(FAILED(hres)) {
1597 jsval_release(namev);
1598 return hres;
1601 hres = to_string(ctx->script, namev, &name);
1602 jsval_release(namev);
1603 if(FAILED(hres)) {
1604 IDispatch_Release(obj);
1605 return hres;
1608 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1609 if(SUCCEEDED(hres)) {
1610 BSTR bstr;
1612 bstr = SysAllocStringLen(name->str, jsstr_length(name));
1613 if(bstr) {
1614 hres = IDispatchEx_DeleteMemberByName(dispex, bstr, make_grfdex(ctx->script, fdexNameCaseSensitive));
1615 SysFreeString(bstr);
1616 ret = TRUE;
1617 }else {
1618 hres = E_OUTOFMEMORY;
1621 IDispatchEx_Release(dispex);
1622 }else {
1623 hres = S_OK;
1624 ret = FALSE;
1627 IDispatch_Release(obj);
1628 jsstr_release(name);
1629 if(FAILED(hres))
1630 return hres;
1632 return stack_push(ctx, jsval_bool(ret));
1635 /* ECMA-262 3rd Edition 11.4.2 */
1636 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1638 const BSTR arg = get_op_bstr(ctx, 0);
1639 IDispatchEx *dispex;
1640 exprval_t exprval;
1641 BOOL ret = FALSE;
1642 HRESULT hres;
1644 TRACE("%s\n", debugstr_w(arg));
1646 hres = identifier_eval(ctx->script, arg, &exprval);
1647 if(FAILED(hres))
1648 return hres;
1650 if(exprval.type != EXPRVAL_IDREF) {
1651 FIXME("Unsupported exprval\n");
1652 exprval_release(&exprval);
1653 return E_NOTIMPL;
1656 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1657 IDispatch_Release(exprval.u.idref.disp);
1658 if(SUCCEEDED(hres)) {
1659 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1660 IDispatchEx_Release(dispex);
1661 if(FAILED(hres))
1662 return hres;
1664 ret = TRUE;
1667 return stack_push(ctx, jsval_bool(ret));
1670 /* ECMA-262 3rd Edition 11.4.2 */
1671 static HRESULT interp_void(exec_ctx_t *ctx)
1673 TRACE("\n");
1675 stack_popn(ctx, 1);
1676 return stack_push(ctx, jsval_undefined());
1679 /* ECMA-262 3rd Edition 11.4.3 */
1680 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1682 switch(jsval_type(v)) {
1683 case JSV_UNDEFINED:
1684 *ret = undefinedW;
1685 break;
1686 case JSV_NULL:
1687 *ret = objectW;
1688 break;
1689 case JSV_OBJECT: {
1690 jsdisp_t *dispex;
1692 if(get_object(v) && (dispex = iface_to_jsdisp((IUnknown*)get_object(v)))) {
1693 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1694 jsdisp_release(dispex);
1695 }else {
1696 *ret = objectW;
1698 break;
1700 case JSV_STRING:
1701 *ret = stringW;
1702 break;
1703 case JSV_NUMBER:
1704 *ret = numberW;
1705 break;
1706 case JSV_BOOL:
1707 *ret = booleanW;
1708 break;
1709 case JSV_VARIANT:
1710 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1711 return E_NOTIMPL;
1714 return S_OK;
1717 /* ECMA-262 3rd Edition 11.4.3 */
1718 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1720 const WCHAR *ret;
1721 IDispatch *obj;
1722 jsval_t v;
1723 DISPID id;
1724 HRESULT hres;
1726 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1728 TRACE("\n");
1730 obj = stack_pop_objid(ctx, &id);
1731 if(!obj)
1732 return stack_push_string(ctx, undefinedW);
1734 hres = disp_propget(ctx->script, obj, id, &v);
1735 IDispatch_Release(obj);
1736 if(FAILED(hres))
1737 return stack_push_string(ctx, unknownW);
1739 hres = typeof_string(v, &ret);
1740 jsval_release(v);
1741 if(FAILED(hres))
1742 return hres;
1744 return stack_push_string(ctx, ret);
1747 /* ECMA-262 3rd Edition 11.4.3 */
1748 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1750 const BSTR arg = get_op_bstr(ctx, 0);
1751 exprval_t exprval;
1752 const WCHAR *ret;
1753 jsval_t v;
1754 HRESULT hres;
1756 TRACE("%s\n", debugstr_w(arg));
1758 hres = identifier_eval(ctx->script, arg, &exprval);
1759 if(FAILED(hres))
1760 return hres;
1762 if(exprval.type == EXPRVAL_INVALID) {
1763 hres = stack_push_string(ctx, undefinedW);
1764 exprval_release(&exprval);
1765 return hres;
1768 hres = exprval_to_value(ctx->script, &exprval, &v);
1769 exprval_release(&exprval);
1770 if(FAILED(hres))
1771 return hres;
1773 hres = typeof_string(v, &ret);
1774 jsval_release(v);
1775 if(FAILED(hres))
1776 return hres;
1778 return stack_push_string(ctx, ret);
1781 /* ECMA-262 3rd Edition 11.4.3 */
1782 static HRESULT interp_typeof(exec_ctx_t *ctx)
1784 const WCHAR *ret;
1785 jsval_t v;
1786 HRESULT hres;
1788 TRACE("\n");
1790 v = stack_pop(ctx);
1791 hres = typeof_string(v, &ret);
1792 jsval_release(v);
1793 if(FAILED(hres))
1794 return hres;
1796 return stack_push_string(ctx, ret);
1799 /* ECMA-262 3rd Edition 11.4.7 */
1800 static HRESULT interp_minus(exec_ctx_t *ctx)
1802 double n;
1803 HRESULT hres;
1805 TRACE("\n");
1807 hres = stack_pop_number(ctx, &n);
1808 if(FAILED(hres))
1809 return hres;
1811 return stack_push(ctx, jsval_number(-n));
1814 /* ECMA-262 3rd Edition 11.4.6 */
1815 static HRESULT interp_tonum(exec_ctx_t *ctx)
1817 jsval_t v;
1818 double n;
1819 HRESULT hres;
1821 TRACE("\n");
1823 v = stack_pop(ctx);
1824 hres = to_number(ctx->script, v, &n);
1825 jsval_release(v);
1826 if(FAILED(hres))
1827 return hres;
1829 return stack_push(ctx, jsval_number(n));
1832 /* ECMA-262 3rd Edition 11.3.1 */
1833 static HRESULT interp_postinc(exec_ctx_t *ctx)
1835 const int arg = get_op_int(ctx, 0);
1836 IDispatch *obj;
1837 DISPID id;
1838 jsval_t v;
1839 HRESULT hres;
1841 TRACE("%d\n", arg);
1843 obj = stack_pop_objid(ctx, &id);
1844 if(!obj)
1845 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1847 hres = disp_propget(ctx->script, obj, id, &v);
1848 if(SUCCEEDED(hres)) {
1849 double n;
1851 hres = to_number(ctx->script, v, &n);
1852 if(SUCCEEDED(hres))
1853 hres = disp_propput(ctx->script, obj, id, jsval_number(n+(double)arg));
1854 if(FAILED(hres))
1855 jsval_release(v);
1857 IDispatch_Release(obj);
1858 if(FAILED(hres))
1859 return hres;
1861 return stack_push(ctx, v);
1864 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1865 static HRESULT interp_preinc(exec_ctx_t *ctx)
1867 const int arg = get_op_int(ctx, 0);
1868 IDispatch *obj;
1869 double ret;
1870 DISPID id;
1871 jsval_t v;
1872 HRESULT hres;
1874 TRACE("%d\n", arg);
1876 obj = stack_pop_objid(ctx, &id);
1877 if(!obj)
1878 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1880 hres = disp_propget(ctx->script, obj, id, &v);
1881 if(SUCCEEDED(hres)) {
1882 double n;
1884 hres = to_number(ctx->script, v, &n);
1885 jsval_release(v);
1886 if(SUCCEEDED(hres)) {
1887 ret = n+(double)arg;
1888 hres = disp_propput(ctx->script, obj, id, jsval_number(ret));
1891 IDispatch_Release(obj);
1892 if(FAILED(hres))
1893 return hres;
1895 return stack_push(ctx, jsval_number(ret));
1898 /* ECMA-262 3rd Edition 11.9.3 */
1899 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
1901 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
1902 return equal2_values(lval, rval, ret);
1904 /* FIXME: NULL disps should be handled in more general way */
1905 if(is_object_instance(lval) && !get_object(lval))
1906 return equal_values(ctx, jsval_null(), rval, ret);
1907 if(is_object_instance(rval) && !get_object(rval))
1908 return equal_values(ctx, lval, jsval_null(), ret);
1910 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
1911 *ret = TRUE;
1912 return S_OK;
1915 if(is_string(lval) && is_number(rval)) {
1916 double n;
1917 HRESULT hres;
1919 hres = to_number(ctx, lval, &n);
1920 if(FAILED(hres))
1921 return hres;
1923 /* FIXME: optimize */
1924 return equal_values(ctx, jsval_number(n), rval, ret);
1927 if(is_string(rval) && is_number(lval)) {
1928 double n;
1929 HRESULT hres;
1931 hres = to_number(ctx, rval, &n);
1932 if(FAILED(hres))
1933 return hres;
1935 /* FIXME: optimize */
1936 return equal_values(ctx, lval, jsval_number(n), ret);
1939 if(is_bool(rval))
1940 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
1942 if(is_bool(lval))
1943 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
1946 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
1947 jsval_t prim;
1948 HRESULT hres;
1950 hres = to_primitive(ctx, rval, &prim, NO_HINT);
1951 if(FAILED(hres))
1952 return hres;
1954 hres = equal_values(ctx, lval, prim, ret);
1955 jsval_release(prim);
1956 return hres;
1960 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
1961 jsval_t prim;
1962 HRESULT hres;
1964 hres = to_primitive(ctx, lval, &prim, NO_HINT);
1965 if(FAILED(hres))
1966 return hres;
1968 hres = equal_values(ctx, prim, rval, ret);
1969 jsval_release(prim);
1970 return hres;
1974 *ret = FALSE;
1975 return S_OK;
1978 /* ECMA-262 3rd Edition 11.9.1 */
1979 static HRESULT interp_eq(exec_ctx_t *ctx)
1981 jsval_t l, r;
1982 BOOL b;
1983 HRESULT hres;
1985 r = stack_pop(ctx);
1986 l = stack_pop(ctx);
1988 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
1990 hres = equal_values(ctx->script, l, r, &b);
1991 jsval_release(l);
1992 jsval_release(r);
1993 if(FAILED(hres))
1994 return hres;
1996 return stack_push(ctx, jsval_bool(b));
1999 /* ECMA-262 3rd Edition 11.9.2 */
2000 static HRESULT interp_neq(exec_ctx_t *ctx)
2002 jsval_t l, r;
2003 BOOL b;
2004 HRESULT hres;
2006 r = stack_pop(ctx);
2007 l = stack_pop(ctx);
2009 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2011 hres = equal_values(ctx->script, l, r, &b);
2012 jsval_release(l);
2013 jsval_release(r);
2014 if(FAILED(hres))
2015 return hres;
2017 return stack_push(ctx, jsval_bool(!b));
2020 /* ECMA-262 3rd Edition 11.9.4 */
2021 static HRESULT interp_eq2(exec_ctx_t *ctx)
2023 jsval_t l, r;
2024 BOOL b;
2025 HRESULT hres;
2027 TRACE("\n");
2029 r = stack_pop(ctx);
2030 l = stack_pop(ctx);
2032 hres = equal2_values(r, l, &b);
2033 jsval_release(l);
2034 jsval_release(r);
2035 if(FAILED(hres))
2036 return hres;
2038 return stack_push(ctx, jsval_bool(b));
2041 /* ECMA-262 3rd Edition 11.9.5 */
2042 static HRESULT interp_neq2(exec_ctx_t *ctx)
2044 jsval_t l, r;
2045 BOOL b;
2046 HRESULT hres;
2048 TRACE("\n");
2050 r = stack_pop(ctx);
2051 l = stack_pop(ctx);
2053 hres = equal2_values(r, l, &b);
2054 jsval_release(l);
2055 jsval_release(r);
2056 if(FAILED(hres))
2057 return hres;
2059 return stack_push(ctx, jsval_bool(!b));
2062 /* ECMA-262 3rd Edition 11.8.5 */
2063 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2065 double ln, rn;
2066 jsval_t l, r;
2067 HRESULT hres;
2069 hres = to_primitive(ctx, lval, &l, NO_HINT);
2070 if(FAILED(hres))
2071 return hres;
2073 hres = to_primitive(ctx, rval, &r, NO_HINT);
2074 if(FAILED(hres)) {
2075 jsval_release(l);
2076 return hres;
2079 if(is_string(l) && is_string(r)) {
2080 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2081 jsstr_release(get_string(l));
2082 jsstr_release(get_string(r));
2083 return S_OK;
2086 hres = to_number(ctx, l, &ln);
2087 jsval_release(l);
2088 if(SUCCEEDED(hres))
2089 hres = to_number(ctx, r, &rn);
2090 jsval_release(r);
2091 if(FAILED(hres))
2092 return hres;
2094 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2095 return S_OK;
2098 /* ECMA-262 3rd Edition 11.8.1 */
2099 static HRESULT interp_lt(exec_ctx_t *ctx)
2101 jsval_t l, r;
2102 BOOL b;
2103 HRESULT hres;
2105 r = stack_pop(ctx);
2106 l = stack_pop(ctx);
2108 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2110 hres = less_eval(ctx->script, l, r, FALSE, &b);
2111 jsval_release(l);
2112 jsval_release(r);
2113 if(FAILED(hres))
2114 return hres;
2116 return stack_push(ctx, jsval_bool(b));
2119 /* ECMA-262 3rd Edition 11.8.1 */
2120 static HRESULT interp_lteq(exec_ctx_t *ctx)
2122 jsval_t l, r;
2123 BOOL b;
2124 HRESULT hres;
2126 r = stack_pop(ctx);
2127 l = stack_pop(ctx);
2129 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2131 hres = less_eval(ctx->script, r, l, TRUE, &b);
2132 jsval_release(l);
2133 jsval_release(r);
2134 if(FAILED(hres))
2135 return hres;
2137 return stack_push(ctx, jsval_bool(b));
2140 /* ECMA-262 3rd Edition 11.8.2 */
2141 static HRESULT interp_gt(exec_ctx_t *ctx)
2143 jsval_t l, r;
2144 BOOL b;
2145 HRESULT hres;
2147 r = stack_pop(ctx);
2148 l = stack_pop(ctx);
2150 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2152 hres = less_eval(ctx->script, r, l, FALSE, &b);
2153 jsval_release(l);
2154 jsval_release(r);
2155 if(FAILED(hres))
2156 return hres;
2158 return stack_push(ctx, jsval_bool(b));
2161 /* ECMA-262 3rd Edition 11.8.4 */
2162 static HRESULT interp_gteq(exec_ctx_t *ctx)
2164 jsval_t l, r;
2165 BOOL b;
2166 HRESULT hres;
2168 r = stack_pop(ctx);
2169 l = stack_pop(ctx);
2171 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2173 hres = less_eval(ctx->script, l, r, TRUE, &b);
2174 jsval_release(l);
2175 jsval_release(r);
2176 if(FAILED(hres))
2177 return hres;
2179 return stack_push(ctx, jsval_bool(b));
2182 /* ECMA-262 3rd Edition 11.4.8 */
2183 static HRESULT interp_bneg(exec_ctx_t *ctx)
2185 jsval_t v;
2186 INT i;
2187 HRESULT hres;
2189 TRACE("\n");
2191 v = stack_pop(ctx);
2192 hres = to_int32(ctx->script, v, &i);
2193 jsval_release(v);
2194 if(FAILED(hres))
2195 return hres;
2197 return stack_push(ctx, jsval_number(~i));
2200 /* ECMA-262 3rd Edition 11.4.9 */
2201 static HRESULT interp_neg(exec_ctx_t *ctx)
2203 jsval_t v;
2204 BOOL b;
2205 HRESULT hres;
2207 TRACE("\n");
2209 v = stack_pop(ctx);
2210 hres = to_boolean(v, &b);
2211 jsval_release(v);
2212 if(FAILED(hres))
2213 return hres;
2215 return stack_push(ctx, jsval_bool(!b));
2218 /* ECMA-262 3rd Edition 11.7.1 */
2219 static HRESULT interp_lshift(exec_ctx_t *ctx)
2221 DWORD r;
2222 INT l;
2223 HRESULT hres;
2225 hres = stack_pop_uint(ctx, &r);
2226 if(FAILED(hres))
2227 return hres;
2229 hres = stack_pop_int(ctx, &l);
2230 if(FAILED(hres))
2231 return hres;
2233 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2236 /* ECMA-262 3rd Edition 11.7.2 */
2237 static HRESULT interp_rshift(exec_ctx_t *ctx)
2239 DWORD r;
2240 INT l;
2241 HRESULT hres;
2243 hres = stack_pop_uint(ctx, &r);
2244 if(FAILED(hres))
2245 return hres;
2247 hres = stack_pop_int(ctx, &l);
2248 if(FAILED(hres))
2249 return hres;
2251 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2254 /* ECMA-262 3rd Edition 11.7.3 */
2255 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2257 DWORD r, l;
2258 HRESULT hres;
2260 hres = stack_pop_uint(ctx, &r);
2261 if(FAILED(hres))
2262 return hres;
2264 hres = stack_pop_uint(ctx, &l);
2265 if(FAILED(hres))
2266 return hres;
2268 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2271 /* ECMA-262 3rd Edition 11.13.1 */
2272 static HRESULT interp_assign(exec_ctx_t *ctx)
2274 IDispatch *disp;
2275 DISPID id;
2276 jsval_t v;
2277 HRESULT hres;
2279 TRACE("\n");
2281 v = stack_pop(ctx);
2283 disp = stack_pop_objid(ctx, &id);
2284 if(!disp) {
2285 jsval_release(v);
2286 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2289 hres = disp_propput(ctx->script, disp, id, v);
2290 IDispatch_Release(disp);
2291 if(FAILED(hres)) {
2292 jsval_release(v);
2293 return hres;
2296 return stack_push(ctx, v);
2299 /* JScript extension */
2300 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2302 const unsigned argc = get_op_uint(ctx, 0);
2303 IDispatch *disp;
2304 jsval_t v;
2305 DISPID id;
2306 HRESULT hres;
2308 TRACE("%u\n", argc);
2310 disp = stack_topn_objid(ctx, argc+1, &id);
2311 if(!disp)
2312 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2314 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2315 if(FAILED(hres))
2316 return hres;
2318 v = stack_pop(ctx);
2319 stack_popn(ctx, argc+2);
2320 return stack_push(ctx, v);
2323 static HRESULT interp_undefined(exec_ctx_t *ctx)
2325 TRACE("\n");
2327 return stack_push(ctx, jsval_undefined());
2330 static HRESULT interp_jmp(exec_ctx_t *ctx)
2332 const unsigned arg = get_op_uint(ctx, 0);
2334 TRACE("%u\n", arg);
2336 ctx->ip = arg;
2337 return S_OK;
2340 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2342 const unsigned arg = get_op_uint(ctx, 0);
2343 BOOL b;
2344 jsval_t v;
2345 HRESULT hres;
2347 TRACE("\n");
2349 v = stack_pop(ctx);
2350 hres = to_boolean(v, &b);
2351 jsval_release(v);
2352 if(FAILED(hres))
2353 return hres;
2355 if(b)
2356 ctx->ip++;
2357 else
2358 ctx->ip = arg;
2359 return S_OK;
2362 static HRESULT interp_pop(exec_ctx_t *ctx)
2364 TRACE("\n");
2366 stack_popn(ctx, 1);
2367 return S_OK;
2370 static HRESULT interp_ret(exec_ctx_t *ctx)
2372 TRACE("\n");
2374 ctx->ip = -1;
2375 return S_OK;
2378 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2380 static const op_func_t op_funcs[] = {
2381 #define X(x,a,b,c) interp_##x,
2382 OP_LIST
2383 #undef X
2386 static const unsigned op_move[] = {
2387 #define X(a,x,b,c) x,
2388 OP_LIST
2389 #undef X
2392 static HRESULT unwind_exception(exec_ctx_t *ctx)
2394 except_frame_t *except_frame;
2395 jsval_t except_val;
2396 BSTR ident;
2397 HRESULT hres;
2399 except_frame = ctx->except_frame;
2400 ctx->except_frame = except_frame->next;
2402 assert(except_frame->stack_top <= ctx->top);
2403 stack_popn(ctx, ctx->top - except_frame->stack_top);
2405 while(except_frame->scope != ctx->scope_chain)
2406 scope_pop(&ctx->scope_chain);
2408 ctx->ip = except_frame->catch_off;
2410 except_val = ctx->script->ei.val;
2411 ctx->script->ei.val = jsval_undefined();
2412 clear_ei(ctx->script);
2414 ident = except_frame->ident;
2415 heap_free(except_frame);
2417 if(ident) {
2418 jsdisp_t *scope_obj;
2420 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2421 if(SUCCEEDED(hres)) {
2422 hres = jsdisp_propput_name(scope_obj, ident, except_val);
2423 if(FAILED(hres))
2424 jsdisp_release(scope_obj);
2426 jsval_release(except_val);
2427 if(FAILED(hres))
2428 return hres;
2430 hres = scope_push(ctx->scope_chain, scope_obj, to_disp(scope_obj), &ctx->scope_chain);
2431 jsdisp_release(scope_obj);
2432 }else {
2433 hres = stack_push(ctx, except_val);
2434 if(FAILED(hres))
2435 return hres;
2437 hres = stack_push(ctx, jsval_bool(FALSE));
2438 if(FAILED(hres))
2439 return hres;
2441 hres = stack_push(ctx, jsval_undefined());
2444 return hres;
2447 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsval_t *ret)
2449 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2450 except_frame_t *prev_except_frame;
2451 function_code_t *prev_func;
2452 unsigned prev_ip, prev_top;
2453 scope_chain_t *prev_scope;
2454 bytecode_t *prev_code;
2455 jsop_t op;
2456 HRESULT hres = S_OK;
2458 TRACE("\n");
2460 prev_top = exec_ctx->top;
2461 prev_scope = exec_ctx->scope_chain;
2462 prev_except_frame = exec_ctx->except_frame;
2463 prev_ip = exec_ctx->ip;
2464 prev_code = exec_ctx->code;
2465 prev_func = exec_ctx->func_code;
2466 exec_ctx->ip = func->instr_off;
2467 exec_ctx->except_frame = NULL;
2468 exec_ctx->code = code;
2469 exec_ctx->func_code = func;
2471 while(exec_ctx->ip != -1) {
2472 op = code->instrs[exec_ctx->ip].op;
2473 hres = op_funcs[op](exec_ctx);
2474 if(FAILED(hres)) {
2475 TRACE("EXCEPTION\n");
2477 if(!exec_ctx->except_frame)
2478 break;
2480 hres = unwind_exception(exec_ctx);
2481 if(FAILED(hres))
2482 break;
2483 }else {
2484 exec_ctx->ip += op_move[op];
2488 exec_ctx->ip = prev_ip;
2489 exec_ctx->except_frame = prev_except_frame;
2490 exec_ctx->code = prev_code;
2491 exec_ctx->func_code = prev_func;
2493 if(FAILED(hres)) {
2494 while(exec_ctx->scope_chain != prev_scope)
2495 scope_pop(&exec_ctx->scope_chain);
2496 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2497 return hres;
2500 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2501 assert(exec_ctx->scope_chain == prev_scope);
2503 if(exec_ctx->top == prev_top)
2504 *ret = jsval_undefined();
2505 else
2506 *ret = stack_pop(exec_ctx);
2507 return S_OK;
2510 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval, jsval_t *ret)
2512 exec_ctx_t *prev_ctx;
2513 jsval_t val;
2514 unsigned i;
2515 HRESULT hres = S_OK;
2517 for(i = 0; i < func->func_cnt; i++) {
2518 jsdisp_t *func_obj;
2520 if(!func->funcs[i].name)
2521 continue;
2523 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2524 if(FAILED(hres))
2525 return hres;
2527 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, jsval_obj(func_obj));
2528 jsdisp_release(func_obj);
2529 if(FAILED(hres))
2530 return hres;
2533 for(i=0; i < func->var_cnt; i++) {
2534 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2535 DISPID id = 0;
2537 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2538 if(FAILED(hres))
2539 return hres;
2543 prev_ctx = ctx->script->exec_ctx;
2544 ctx->script->exec_ctx = ctx;
2546 hres = enter_bytecode(ctx->script, code, func, &val);
2547 assert(ctx->script->exec_ctx == ctx);
2548 ctx->script->exec_ctx = prev_ctx;
2549 if(FAILED(hres))
2550 return hres;
2552 if(ret)
2553 *ret = val;
2554 else
2555 jsval_release(val);
2556 return S_OK;