2 * Copyright 2008,2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.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 {
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));
56 }else if(ctx->stack_size == ctx->top) {
59 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*new_stack));
65 ctx->stack = new_stack;
69 ctx->stack[ctx->top++] = v;
73 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
81 return stack_push(ctx, jsval_string(v));
84 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
88 hres = stack_push(ctx, jsval_disp(disp));
92 return stack_push(ctx, jsval_number(id));
95 static inline jsval_t stack_top(exec_ctx_t *ctx)
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)
111 assert(ctx->top > n-1);
112 return ctx->stack + ctx->top-n;
115 static inline jsval_t stack_pop(exec_ctx_t *ctx)
118 return ctx->stack[--ctx->top];
121 static void stack_popn(exec_ctx_t *ctx, unsigned n)
124 jsval_release(stack_pop(ctx));
127 static HRESULT stack_pop_number(exec_ctx_t *ctx, double *r)
133 hres = to_number(ctx->script, v, r);
138 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
144 if(is_object_instance(v)) {
146 return throw_type_error(ctx->script, JS_E_OBJECT_REQUIRED, NULL);
151 hres = to_object(ctx->script, v, r);
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)
186 jsval_release(val->u.val);
189 if(val->u.idref.disp)
190 IDispatch_Release(val->u.idref.disp);
192 case EXPRVAL_INVALID:
197 /* ECMA-262 3rd Edition 8.7.1 */
198 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsval_t *ret)
203 val->u.val = jsval_undefined();
206 if(!val->u.idref.disp) {
207 FIXME("throw ReferenceError\n");
211 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret);
212 case EXPRVAL_INVALID:
216 ERR("type %d\n", val->type);
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;
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));
236 return E_OUTOFMEMORY;
240 IDispatch_AddRef(obj);
241 new_scope->jsobj = jsobj;
242 new_scope->obj = obj;
246 new_scope->next = scope;
248 new_scope->next = NULL;
255 static void scope_pop(scope_chain_t **scope)
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)
277 scope_release(scope->next);
279 IDispatch_Release(scope->obj);
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)
288 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
290 return E_OUTOFMEMORY;
293 ctx->is_global = is_global;
294 ctx->ret = jsval_undefined();
296 /* ECMA-262 3rd Edition 11.2.3.7 */
300 jsthis = iface_to_jsdisp((IUnknown*)this_obj);
302 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
304 jsdisp_release(jsthis);
309 ctx->this_obj = this_obj;
310 else if(script_ctx->host_global)
311 ctx->this_obj = script_ctx->host_global;
313 ctx->this_obj = to_disp(script_ctx->global);
314 IDispatch_AddRef(ctx->this_obj);
316 jsdisp_addref(var_disp);
317 ctx->var_disp = var_disp;
319 script_addref(script_ctx);
320 ctx->script = script_ctx;
324 ctx->scope_chain = scope;
331 void exec_release(exec_ctx_t *ctx)
337 scope_release(ctx->scope_chain);
339 jsdisp_release(ctx->var_disp);
341 IDispatch_Release(ctx->this_obj);
343 script_release(ctx->script);
344 jsval_release(ctx->ret);
345 heap_free(ctx->stack);
349 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, const WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
356 jsdisp = iface_to_jsdisp((IUnknown*)disp);
358 hres = jsdisp_get_id(jsdisp, name, flags, id);
359 jsdisp_release(jsdisp);
366 bstr = SysAllocString(name);
368 return E_OUTOFMEMORY;
372 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
373 if(SUCCEEDED(hres)) {
374 hres = IDispatchEx_GetDispID(dispex, bstr, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
375 IDispatchEx_Release(dispex);
377 TRACE("using IDispatch\n");
378 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &bstr, 1, 0, id);
381 if(name_bstr != bstr)
386 static inline BOOL var_is_null(const VARIANT *v)
388 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
391 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
393 IObjectIdentity *identity;
394 IUnknown *unk1, *unk2;
402 if(!disp1 || !disp2) {
407 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
411 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
413 IUnknown_Release(unk1);
420 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
421 if(SUCCEEDED(hres)) {
422 hres = IObjectIdentity_IsEqualObject(identity, unk2);
423 IObjectIdentity_Release(identity);
430 IUnknown_Release(unk1);
431 IUnknown_Release(unk2);
435 /* ECMA-262 3rd Edition 11.9.6 */
436 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
438 jsval_type_t type = jsval_type(lval);
442 if(type != jsval_type(rval)) {
443 if(is_null_instance(lval))
444 *ret = is_null_instance(rval);
456 return disp_cmp(get_object(lval), get_object(rval), ret);
458 *ret = jsstr_eq(get_string(lval), get_string(rval));
461 *ret = get_number(lval) == get_number(rval);
464 *ret = !get_bool(lval) == !get_bool(rval);
467 FIXME("VARIANT not implemented\n");
474 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
480 for(item = ctx->named_items; item; item = item->next) {
481 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
482 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
483 if(SUCCEEDED(hres)) {
485 exprval_set_idref(ret, item->disp, id);
494 /* ECMA-262 3rd Edition 10.1.4 */
495 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
497 scope_chain_t *scope;
502 TRACE("%s\n", debugstr_w(identifier));
504 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
506 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
508 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
509 if(SUCCEEDED(hres)) {
510 exprval_set_idref(ret, scope->obj, id);
515 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
516 if(SUCCEEDED(hres)) {
517 exprval_set_idref(ret, to_disp(ctx->global), id);
521 for(item = ctx->named_items; item; item = item->next) {
522 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
529 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
530 SCRIPTINFO_IUNKNOWN, &unk, NULL);
532 WARN("GetItemInfo failed: %08x\n", hres);
536 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
537 IUnknown_Release(unk);
539 WARN("object does not implement IDispatch\n");
544 IDispatch_AddRef(item->disp);
545 ret->type = EXPRVAL_JSVAL;
546 ret->u.val = jsval_disp(item->disp);
551 if(lookup_global_members(ctx, identifier, ret))
554 ret->type = EXPRVAL_INVALID;
558 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
559 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
562 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
563 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
566 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
567 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
570 static inline jsstr_t *get_op_str(exec_ctx_t *ctx, int i){
571 return ctx->code->instrs[ctx->ip].u.arg[i].str;
574 static inline double get_op_double(exec_ctx_t *ctx){
575 return ctx->code->instrs[ctx->ip].u.dbl;
578 /* ECMA-262 3rd Edition 12.2 */
579 static HRESULT interp_var_set(exec_ctx_t *ctx)
581 const BSTR name = get_op_bstr(ctx, 0);
585 TRACE("%s\n", debugstr_w(name));
587 val = stack_pop(ctx);
588 hres = jsdisp_propput_name(ctx->var_disp, name, val);
593 /* ECMA-262 3rd Edition 12.6.4 */
594 static HRESULT interp_forin(exec_ctx_t *ctx)
596 const HRESULT arg = get_op_uint(ctx, 0);
597 IDispatch *var_obj, *obj = NULL;
605 assert(is_number(stack_top(ctx)));
606 id = get_number(stack_top(ctx));
608 var_obj = stack_topn_objid(ctx, 1, &var_id);
610 FIXME("invalid ref\n");
614 if(is_object_instance(stack_topn(ctx, 3)))
615 obj = get_object(stack_topn(ctx, 3));
618 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
619 if(SUCCEEDED(hres)) {
620 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
622 hres = IDispatchEx_GetMemberName(dispex, id, &name);
623 IDispatchEx_Release(dispex);
627 TRACE("No IDispatchEx\n");
634 str = jsstr_alloc_len(name, SysStringLen(name));
637 return E_OUTOFMEMORY;
640 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
642 hres = disp_propput(ctx->script, var_obj, var_id, jsval_string(str));
655 /* ECMA-262 3rd Edition 12.10 */
656 static HRESULT interp_push_scope(exec_ctx_t *ctx)
665 hres = to_object(ctx->script, v, &disp);
670 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
671 IDispatch_Release(disp);
675 /* ECMA-262 3rd Edition 12.10 */
676 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
680 scope_pop(&ctx->scope_chain);
684 /* ECMA-262 3rd Edition 12.13 */
685 static HRESULT interp_case(exec_ctx_t *ctx)
687 const unsigned arg = get_op_uint(ctx, 0);
695 hres = equal2_values(stack_top(ctx), v, &b);
709 /* ECMA-262 3rd Edition 12.13 */
710 static HRESULT interp_throw(exec_ctx_t *ctx)
714 jsval_release(ctx->script->ei.val);
715 ctx->script->ei.val = stack_pop(ctx);
716 return DISP_E_EXCEPTION;
719 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
721 const HRESULT arg = get_op_uint(ctx, 0);
723 TRACE("%08x\n", arg);
725 return throw_reference_error(ctx->script, arg, NULL);
728 static HRESULT interp_throw_type(exec_ctx_t *ctx)
730 const HRESULT hres = get_op_uint(ctx, 0);
731 jsstr_t *str = get_op_str(ctx, 1);
734 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
736 ptr = jsstr_flatten(str);
737 return ptr ? throw_type_error(ctx->script, hres, ptr) : E_OUTOFMEMORY;
740 /* ECMA-262 3rd Edition 12.14 */
741 static HRESULT interp_push_except(exec_ctx_t *ctx)
743 const unsigned arg1 = get_op_uint(ctx, 0);
744 const BSTR arg2 = get_op_bstr(ctx, 1);
745 except_frame_t *except;
750 stack_top = ctx->top;
755 hres = stack_push(ctx, jsval_bool(TRUE));
758 hres = stack_push(ctx, jsval_bool(TRUE));
763 except = heap_alloc(sizeof(*except));
765 return E_OUTOFMEMORY;
767 except->stack_top = stack_top;
768 except->scope = ctx->scope_chain;
769 except->catch_off = arg1;
770 except->ident = arg2;
771 except->next = ctx->except_frame;
772 ctx->except_frame = except;
776 /* ECMA-262 3rd Edition 12.14 */
777 static HRESULT interp_pop_except(exec_ctx_t *ctx)
779 except_frame_t *except;
783 except = ctx->except_frame;
784 assert(except != NULL);
786 ctx->except_frame = except->next;
791 /* ECMA-262 3rd Edition 12.14 */
792 static HRESULT interp_end_finally(exec_ctx_t *ctx)
798 assert(is_bool(stack_top(ctx)));
799 if(!get_bool(stack_top(ctx))) {
800 TRACE("passing exception\n");
805 ctx->script->ei.val = stack_pop(ctx);
806 return DISP_E_EXCEPTION;
813 /* ECMA-262 3rd Edition 13 */
814 static HRESULT interp_func(exec_ctx_t *ctx)
816 unsigned func_idx = get_op_uint(ctx, 0);
820 TRACE("%d\n", func_idx);
822 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
823 ctx->scope_chain, &dispex);
827 return stack_push(ctx, jsval_obj(dispex));
830 /* ECMA-262 3rd Edition 11.2.1 */
831 static HRESULT interp_array(exec_ctx_t *ctx)
842 namev = stack_pop(ctx);
844 hres = stack_pop_object(ctx, &obj);
846 jsval_release(namev);
850 hres = to_flat_string(ctx->script, namev, &name_str, &name);
851 jsval_release(namev);
853 IDispatch_Release(obj);
857 hres = disp_get_id(ctx->script, obj, name, NULL, 0, &id);
858 jsstr_release(name_str);
859 if(SUCCEEDED(hres)) {
860 hres = disp_propget(ctx->script, obj, id, &v);
861 }else if(hres == DISP_E_UNKNOWNNAME) {
862 v = jsval_undefined();
865 IDispatch_Release(obj);
869 return stack_push(ctx, v);
872 /* ECMA-262 3rd Edition 11.2.1 */
873 static HRESULT interp_member(exec_ctx_t *ctx)
875 const BSTR arg = get_op_bstr(ctx, 0);
883 hres = stack_pop_object(ctx, &obj);
887 hres = disp_get_id(ctx->script, obj, arg, arg, 0, &id);
888 if(SUCCEEDED(hres)) {
889 hres = disp_propget(ctx->script, obj, id, &v);
890 }else if(hres == DISP_E_UNKNOWNNAME) {
891 v = jsval_undefined();
894 IDispatch_Release(obj);
898 return stack_push(ctx, v);
901 /* ECMA-262 3rd Edition 11.2.1 */
902 static HRESULT interp_memberid(exec_ctx_t *ctx)
904 const unsigned arg = get_op_uint(ctx, 0);
914 namev = stack_pop(ctx);
915 objv = stack_pop(ctx);
917 hres = to_object(ctx->script, objv, &obj);
919 if(SUCCEEDED(hres)) {
920 hres = to_flat_string(ctx->script, namev, &name_str, &name);
922 IDispatch_Release(obj);
924 jsval_release(namev);
928 hres = disp_get_id(ctx->script, obj, name, NULL, arg, &id);
929 jsstr_release(name_str);
931 IDispatch_Release(obj);
932 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
934 id = JS_E_INVALID_PROPERTY;
936 ERR("failed %08x\n", hres);
941 return stack_push_objid(ctx, obj, id);
944 /* ECMA-262 3rd Edition 11.2.1 */
945 static HRESULT interp_refval(exec_ctx_t *ctx)
954 disp = stack_topn_objid(ctx, 0, &id);
956 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
958 hres = disp_propget(ctx->script, disp, id, &v);
962 return stack_push(ctx, v);
965 /* ECMA-262 3rd Edition 11.2.2 */
966 static HRESULT interp_new(exec_ctx_t *ctx)
968 const unsigned argc = get_op_uint(ctx, 0);
974 constr = stack_topn(ctx, argc);
976 /* NOTE: Should use to_object here */
979 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
980 else if(!is_object_instance(constr))
981 return throw_type_error(ctx->script, JS_E_INVALID_ACTION, NULL);
982 else if(!get_object(constr))
983 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
985 hres = disp_call_value(ctx->script, get_object(constr), NULL, DISPATCH_CONSTRUCT, argc, stack_args(ctx, argc), &r);
989 stack_popn(ctx, argc+1);
990 return stack_push(ctx, r);
993 /* ECMA-262 3rd Edition 11.2.3 */
994 static HRESULT interp_call(exec_ctx_t *ctx)
996 const unsigned argn = get_op_uint(ctx, 0);
997 const int do_ret = get_op_int(ctx, 1);
1001 TRACE("%d %d\n", argn, do_ret);
1003 obj = stack_topn(ctx, argn);
1004 if(!is_object_instance(obj))
1005 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
1007 hres = disp_call_value(ctx->script, get_object(obj), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
1008 do_ret ? &r : NULL);
1012 stack_popn(ctx, argn+1);
1013 return do_ret ? stack_push(ctx, r) : S_OK;
1016 /* ECMA-262 3rd Edition 11.2.3 */
1017 static HRESULT interp_call_member(exec_ctx_t *ctx)
1019 const unsigned argn = get_op_uint(ctx, 0);
1020 const int do_ret = get_op_int(ctx, 1);
1026 TRACE("%d %d\n", argn, do_ret);
1028 obj = stack_topn_objid(ctx, argn, &id);
1030 return throw_type_error(ctx->script, id, NULL);
1032 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &r : NULL);
1036 stack_popn(ctx, argn+2);
1037 return do_ret ? stack_push(ctx, r) : S_OK;
1041 /* ECMA-262 3rd Edition 11.1.1 */
1042 static HRESULT interp_this(exec_ctx_t *ctx)
1046 IDispatch_AddRef(ctx->this_obj);
1047 return stack_push(ctx, jsval_disp(ctx->this_obj));
1050 /* ECMA-262 3rd Edition 10.1.4 */
1051 static HRESULT interp_ident(exec_ctx_t *ctx)
1053 const BSTR arg = get_op_bstr(ctx, 0);
1058 TRACE("%s\n", debugstr_w(arg));
1060 hres = identifier_eval(ctx->script, arg, &exprval);
1064 if(exprval.type == EXPRVAL_INVALID)
1065 return throw_type_error(ctx->script, JS_E_UNDEFINED_VARIABLE, arg);
1067 hres = exprval_to_value(ctx->script, &exprval, &v);
1068 exprval_release(&exprval);
1072 return stack_push(ctx, v);
1075 /* ECMA-262 3rd Edition 10.1.4 */
1076 static HRESULT interp_identid(exec_ctx_t *ctx)
1078 const BSTR arg = get_op_bstr(ctx, 0);
1079 const unsigned flags = get_op_uint(ctx, 1);
1083 TRACE("%s %x\n", debugstr_w(arg), flags);
1085 hres = identifier_eval(ctx->script, arg, &exprval);
1089 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1092 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1096 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1099 if(exprval.type != EXPRVAL_IDREF) {
1100 WARN("invalid ref\n");
1101 exprval_release(&exprval);
1102 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1105 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1108 /* ECMA-262 3rd Edition 7.8.1 */
1109 static HRESULT interp_null(exec_ctx_t *ctx)
1113 return stack_push(ctx, jsval_null());
1116 /* ECMA-262 3rd Edition 7.8.2 */
1117 static HRESULT interp_bool(exec_ctx_t *ctx)
1119 const int arg = get_op_int(ctx, 0);
1121 TRACE("%s\n", arg ? "true" : "false");
1123 return stack_push(ctx, jsval_bool(arg));
1126 /* ECMA-262 3rd Edition 7.8.3 */
1127 static HRESULT interp_int(exec_ctx_t *ctx)
1129 const int arg = get_op_int(ctx, 0);
1133 return stack_push(ctx, jsval_number(arg));
1136 /* ECMA-262 3rd Edition 7.8.3 */
1137 static HRESULT interp_double(exec_ctx_t *ctx)
1139 const double arg = get_op_double(ctx);
1141 TRACE("%lf\n", arg);
1143 return stack_push(ctx, jsval_number(arg));
1146 /* ECMA-262 3rd Edition 7.8.4 */
1147 static HRESULT interp_str(exec_ctx_t *ctx)
1149 jsstr_t *str = get_op_str(ctx, 0);
1151 TRACE("%s\n", debugstr_jsstr(str));
1153 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1156 /* ECMA-262 3rd Edition 7.8 */
1157 static HRESULT interp_regexp(exec_ctx_t *ctx)
1159 jsstr_t *source = get_op_str(ctx, 0);
1160 const unsigned flags = get_op_uint(ctx, 1);
1164 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1166 hres = create_regexp(ctx->script, source, flags, ®exp);
1170 return stack_push(ctx, jsval_obj(regexp));
1173 /* ECMA-262 3rd Edition 11.1.4 */
1174 static HRESULT interp_carray(exec_ctx_t *ctx)
1176 const unsigned arg = get_op_uint(ctx, 0);
1184 hres = create_array(ctx->script, arg, &array);
1190 val = stack_pop(ctx);
1191 hres = jsdisp_propput_idx(array, i, val);
1194 jsdisp_release(array);
1199 return stack_push(ctx, jsval_obj(array));
1202 /* ECMA-262 3rd Edition 11.1.5 */
1203 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1210 hres = create_object(ctx->script, NULL, &obj);
1214 return stack_push(ctx, jsval_obj(obj));
1217 /* ECMA-262 3rd Edition 11.1.5 */
1218 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1220 const BSTR name = get_op_bstr(ctx, 0);
1225 TRACE("%s\n", debugstr_w(name));
1227 val = stack_pop(ctx);
1229 assert(is_object_instance(stack_top(ctx)));
1230 obj = as_jsdisp(get_object(stack_top(ctx)));
1232 hres = jsdisp_propput_name(obj, name, val);
1237 /* ECMA-262 3rd Edition 11.11 */
1238 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1240 const unsigned arg = get_op_uint(ctx, 0);
1246 hres = to_boolean(stack_top(ctx), &b);
1259 /* ECMA-262 3rd Edition 11.11 */
1260 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1262 const unsigned arg = get_op_uint(ctx, 0);
1268 hres = to_boolean(stack_top(ctx), &b);
1281 /* ECMA-262 3rd Edition 11.10 */
1282 static HRESULT interp_or(exec_ctx_t *ctx)
1289 hres = stack_pop_int(ctx, &r);
1293 hres = stack_pop_int(ctx, &l);
1297 return stack_push(ctx, jsval_number(l|r));
1300 /* ECMA-262 3rd Edition 11.10 */
1301 static HRESULT interp_xor(exec_ctx_t *ctx)
1308 hres = stack_pop_int(ctx, &r);
1312 hres = stack_pop_int(ctx, &l);
1316 return stack_push(ctx, jsval_number(l^r));
1319 /* ECMA-262 3rd Edition 11.10 */
1320 static HRESULT interp_and(exec_ctx_t *ctx)
1327 hres = stack_pop_int(ctx, &r);
1331 hres = stack_pop_int(ctx, &l);
1335 return stack_push(ctx, jsval_number(l&r));
1338 /* ECMA-262 3rd Edition 11.8.6 */
1339 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1341 jsdisp_t *obj, *iter, *tmp = NULL;
1346 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1349 if(!is_object_instance(v) || !get_object(v)) {
1351 return throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1354 obj = iface_to_jsdisp((IUnknown*)get_object(v));
1355 IDispatch_Release(get_object(v));
1357 FIXME("non-jsdisp objects not supported\n");
1361 if(is_class(obj, JSCLASS_FUNCTION)) {
1362 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1364 hres = throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1366 jsdisp_release(obj);
1372 if(is_object_instance(prot)) {
1373 if(is_object_instance(v))
1374 tmp = iface_to_jsdisp((IUnknown*)get_object(v));
1375 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1376 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1382 jsdisp_release(tmp);
1384 FIXME("prototype is not an object\n");
1388 jsval_release(prot);
1393 return stack_push(ctx, jsval_bool(ret));
1396 /* ECMA-262 3rd Edition 11.8.7 */
1397 static HRESULT interp_in(exec_ctx_t *ctx)
1408 obj = stack_pop(ctx);
1409 if(!is_object_instance(obj) || !get_object(obj)) {
1411 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1415 hres = to_flat_string(ctx->script, v, &jsstr, &str);
1418 IDispatch_Release(get_object(obj));
1422 hres = disp_get_id(ctx->script, get_object(obj), str, NULL, 0, &id);
1423 IDispatch_Release(get_object(obj));
1424 jsstr_release(jsstr);
1427 else if(hres == DISP_E_UNKNOWNNAME)
1432 return stack_push(ctx, jsval_bool(ret));
1435 /* ECMA-262 3rd Edition 11.6.1 */
1436 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1441 hres = to_primitive(ctx, lval, &l, NO_HINT);
1445 hres = to_primitive(ctx, rval, &r, NO_HINT);
1451 if(is_string(l) || is_string(r)) {
1452 jsstr_t *lstr, *rstr = NULL;
1454 hres = to_string(ctx, l, &lstr);
1456 hres = to_string(ctx, r, &rstr);
1458 if(SUCCEEDED(hres)) {
1461 ret_str = jsstr_concat(lstr, rstr);
1463 *ret = jsval_string(ret_str);
1465 hres = E_OUTOFMEMORY;
1468 jsstr_release(lstr);
1470 jsstr_release(rstr);
1474 hres = to_number(ctx, l, &nl);
1475 if(SUCCEEDED(hres)) {
1476 hres = to_number(ctx, r, &nr);
1478 *ret = jsval_number(nl+nr);
1487 /* ECMA-262 3rd Edition 11.6.1 */
1488 static HRESULT interp_add(exec_ctx_t *ctx)
1496 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1498 hres = add_eval(ctx->script, l, r, &ret);
1504 return stack_push(ctx, ret);
1507 /* ECMA-262 3rd Edition 11.6.2 */
1508 static HRESULT interp_sub(exec_ctx_t *ctx)
1515 hres = stack_pop_number(ctx, &r);
1519 hres = stack_pop_number(ctx, &l);
1523 return stack_push(ctx, jsval_number(l-r));
1526 /* ECMA-262 3rd Edition 11.5.1 */
1527 static HRESULT interp_mul(exec_ctx_t *ctx)
1534 hres = stack_pop_number(ctx, &r);
1538 hres = stack_pop_number(ctx, &l);
1542 return stack_push(ctx, jsval_number(l*r));
1545 /* ECMA-262 3rd Edition 11.5.2 */
1546 static HRESULT interp_div(exec_ctx_t *ctx)
1553 hres = stack_pop_number(ctx, &r);
1557 hres = stack_pop_number(ctx, &l);
1561 return stack_push(ctx, jsval_number(l/r));
1564 /* ECMA-262 3rd Edition 11.5.3 */
1565 static HRESULT interp_mod(exec_ctx_t *ctx)
1572 hres = stack_pop_number(ctx, &r);
1576 hres = stack_pop_number(ctx, &l);
1580 return stack_push(ctx, jsval_number(fmod(l, r)));
1583 /* ECMA-262 3rd Edition 11.4.2 */
1584 static HRESULT interp_delete(exec_ctx_t *ctx)
1586 jsval_t objv, namev;
1594 namev = stack_pop(ctx);
1595 objv = stack_pop(ctx);
1597 hres = to_object(ctx->script, objv, &obj);
1598 jsval_release(objv);
1600 jsval_release(namev);
1604 hres = to_string(ctx->script, namev, &name);
1605 jsval_release(namev);
1607 IDispatch_Release(obj);
1611 hres = disp_delete_name(ctx->script, obj, name, &ret);
1612 IDispatch_Release(obj);
1613 jsstr_release(name);
1617 return stack_push(ctx, jsval_bool(ret));
1620 /* ECMA-262 3rd Edition 11.4.2 */
1621 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1623 const BSTR arg = get_op_bstr(ctx, 0);
1628 TRACE("%s\n", debugstr_w(arg));
1630 hres = identifier_eval(ctx->script, arg, &exprval);
1634 switch(exprval.type) {
1636 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1637 IDispatch_Release(exprval.u.idref.disp);
1641 case EXPRVAL_INVALID:
1645 FIXME("Unsupported exprval\n");
1646 exprval_release(&exprval);
1651 return stack_push(ctx, jsval_bool(ret));
1654 /* ECMA-262 3rd Edition 11.4.2 */
1655 static HRESULT interp_void(exec_ctx_t *ctx)
1660 return stack_push(ctx, jsval_undefined());
1663 /* ECMA-262 3rd Edition 11.4.3 */
1664 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1666 switch(jsval_type(v)) {
1676 if(get_object(v) && (dispex = iface_to_jsdisp((IUnknown*)get_object(v)))) {
1677 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1678 jsdisp_release(dispex);
1694 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1701 /* ECMA-262 3rd Edition 11.4.3 */
1702 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1712 obj = stack_pop_objid(ctx, &id);
1714 return stack_push(ctx, jsval_string(jsstr_undefined()));
1716 hres = disp_propget(ctx->script, obj, id, &v);
1717 IDispatch_Release(obj);
1719 return stack_push_string(ctx, unknownW);
1721 hres = typeof_string(v, &ret);
1726 return stack_push_string(ctx, ret);
1729 /* ECMA-262 3rd Edition 11.4.3 */
1730 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1732 const BSTR arg = get_op_bstr(ctx, 0);
1738 TRACE("%s\n", debugstr_w(arg));
1740 hres = identifier_eval(ctx->script, arg, &exprval);
1744 if(exprval.type == EXPRVAL_INVALID) {
1745 hres = stack_push(ctx, jsval_string(jsstr_undefined()));
1746 exprval_release(&exprval);
1750 hres = exprval_to_value(ctx->script, &exprval, &v);
1751 exprval_release(&exprval);
1755 hres = typeof_string(v, &ret);
1760 return stack_push_string(ctx, ret);
1763 /* ECMA-262 3rd Edition 11.4.3 */
1764 static HRESULT interp_typeof(exec_ctx_t *ctx)
1773 hres = typeof_string(v, &ret);
1778 return stack_push_string(ctx, ret);
1781 /* ECMA-262 3rd Edition 11.4.7 */
1782 static HRESULT interp_minus(exec_ctx_t *ctx)
1789 hres = stack_pop_number(ctx, &n);
1793 return stack_push(ctx, jsval_number(-n));
1796 /* ECMA-262 3rd Edition 11.4.6 */
1797 static HRESULT interp_tonum(exec_ctx_t *ctx)
1806 hres = to_number(ctx->script, v, &n);
1811 return stack_push(ctx, jsval_number(n));
1814 /* ECMA-262 3rd Edition 11.3.1 */
1815 static HRESULT interp_postinc(exec_ctx_t *ctx)
1817 const int arg = get_op_int(ctx, 0);
1825 obj = stack_pop_objid(ctx, &id);
1827 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1829 hres = disp_propget(ctx->script, obj, id, &v);
1830 if(SUCCEEDED(hres)) {
1833 hres = to_number(ctx->script, v, &n);
1835 hres = disp_propput(ctx->script, obj, id, jsval_number(n+(double)arg));
1839 IDispatch_Release(obj);
1843 return stack_push(ctx, v);
1846 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1847 static HRESULT interp_preinc(exec_ctx_t *ctx)
1849 const int arg = get_op_int(ctx, 0);
1858 obj = stack_pop_objid(ctx, &id);
1860 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1862 hres = disp_propget(ctx->script, obj, id, &v);
1863 if(SUCCEEDED(hres)) {
1866 hres = to_number(ctx->script, v, &n);
1868 if(SUCCEEDED(hres)) {
1869 ret = n+(double)arg;
1870 hres = disp_propput(ctx->script, obj, id, jsval_number(ret));
1873 IDispatch_Release(obj);
1877 return stack_push(ctx, jsval_number(ret));
1880 /* ECMA-262 3rd Edition 11.9.3 */
1881 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
1883 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
1884 return equal2_values(lval, rval, ret);
1886 /* FIXME: NULL disps should be handled in more general way */
1887 if(is_object_instance(lval) && !get_object(lval))
1888 return equal_values(ctx, jsval_null(), rval, ret);
1889 if(is_object_instance(rval) && !get_object(rval))
1890 return equal_values(ctx, lval, jsval_null(), ret);
1892 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
1897 if(is_string(lval) && is_number(rval)) {
1901 hres = to_number(ctx, lval, &n);
1905 /* FIXME: optimize */
1906 return equal_values(ctx, jsval_number(n), rval, ret);
1909 if(is_string(rval) && is_number(lval)) {
1913 hres = to_number(ctx, rval, &n);
1917 /* FIXME: optimize */
1918 return equal_values(ctx, lval, jsval_number(n), ret);
1922 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
1925 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
1928 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
1932 hres = to_primitive(ctx, rval, &prim, NO_HINT);
1936 hres = equal_values(ctx, lval, prim, ret);
1937 jsval_release(prim);
1942 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
1946 hres = to_primitive(ctx, lval, &prim, NO_HINT);
1950 hres = equal_values(ctx, prim, rval, ret);
1951 jsval_release(prim);
1960 /* ECMA-262 3rd Edition 11.9.1 */
1961 static HRESULT interp_eq(exec_ctx_t *ctx)
1970 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
1972 hres = equal_values(ctx->script, l, r, &b);
1978 return stack_push(ctx, jsval_bool(b));
1981 /* ECMA-262 3rd Edition 11.9.2 */
1982 static HRESULT interp_neq(exec_ctx_t *ctx)
1991 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
1993 hres = equal_values(ctx->script, l, r, &b);
1999 return stack_push(ctx, jsval_bool(!b));
2002 /* ECMA-262 3rd Edition 11.9.4 */
2003 static HRESULT interp_eq2(exec_ctx_t *ctx)
2012 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2014 hres = equal2_values(r, l, &b);
2020 return stack_push(ctx, jsval_bool(b));
2023 /* ECMA-262 3rd Edition 11.9.5 */
2024 static HRESULT interp_neq2(exec_ctx_t *ctx)
2035 hres = equal2_values(r, l, &b);
2041 return stack_push(ctx, jsval_bool(!b));
2044 /* ECMA-262 3rd Edition 11.8.5 */
2045 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2051 hres = to_primitive(ctx, lval, &l, NO_HINT);
2055 hres = to_primitive(ctx, rval, &r, NO_HINT);
2061 if(is_string(l) && is_string(r)) {
2062 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2063 jsstr_release(get_string(l));
2064 jsstr_release(get_string(r));
2068 hres = to_number(ctx, l, &ln);
2071 hres = to_number(ctx, r, &rn);
2076 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2080 /* ECMA-262 3rd Edition 11.8.1 */
2081 static HRESULT interp_lt(exec_ctx_t *ctx)
2090 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2092 hres = less_eval(ctx->script, l, r, FALSE, &b);
2098 return stack_push(ctx, jsval_bool(b));
2101 /* ECMA-262 3rd Edition 11.8.1 */
2102 static HRESULT interp_lteq(exec_ctx_t *ctx)
2111 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2113 hres = less_eval(ctx->script, r, l, TRUE, &b);
2119 return stack_push(ctx, jsval_bool(b));
2122 /* ECMA-262 3rd Edition 11.8.2 */
2123 static HRESULT interp_gt(exec_ctx_t *ctx)
2132 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2134 hres = less_eval(ctx->script, r, l, FALSE, &b);
2140 return stack_push(ctx, jsval_bool(b));
2143 /* ECMA-262 3rd Edition 11.8.4 */
2144 static HRESULT interp_gteq(exec_ctx_t *ctx)
2153 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2155 hres = less_eval(ctx->script, l, r, TRUE, &b);
2161 return stack_push(ctx, jsval_bool(b));
2164 /* ECMA-262 3rd Edition 11.4.8 */
2165 static HRESULT interp_bneg(exec_ctx_t *ctx)
2174 hres = to_int32(ctx->script, v, &i);
2179 return stack_push(ctx, jsval_number(~i));
2182 /* ECMA-262 3rd Edition 11.4.9 */
2183 static HRESULT interp_neg(exec_ctx_t *ctx)
2192 hres = to_boolean(v, &b);
2197 return stack_push(ctx, jsval_bool(!b));
2200 /* ECMA-262 3rd Edition 11.7.1 */
2201 static HRESULT interp_lshift(exec_ctx_t *ctx)
2207 hres = stack_pop_uint(ctx, &r);
2211 hres = stack_pop_int(ctx, &l);
2215 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2218 /* ECMA-262 3rd Edition 11.7.2 */
2219 static HRESULT interp_rshift(exec_ctx_t *ctx)
2225 hres = stack_pop_uint(ctx, &r);
2229 hres = stack_pop_int(ctx, &l);
2233 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2236 /* ECMA-262 3rd Edition 11.7.3 */
2237 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2242 hres = stack_pop_uint(ctx, &r);
2246 hres = stack_pop_uint(ctx, &l);
2250 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2253 /* ECMA-262 3rd Edition 11.13.1 */
2254 static HRESULT interp_assign(exec_ctx_t *ctx)
2265 disp = stack_pop_objid(ctx, &id);
2268 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2271 hres = disp_propput(ctx->script, disp, id, v);
2272 IDispatch_Release(disp);
2278 return stack_push(ctx, v);
2281 /* JScript extension */
2282 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2284 const unsigned argc = get_op_uint(ctx, 0);
2290 TRACE("%u\n", argc);
2292 disp = stack_topn_objid(ctx, argc+1, &id);
2294 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2296 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2301 stack_popn(ctx, argc+2);
2302 return stack_push(ctx, v);
2305 static HRESULT interp_undefined(exec_ctx_t *ctx)
2309 return stack_push(ctx, jsval_undefined());
2312 static HRESULT interp_jmp(exec_ctx_t *ctx)
2314 const unsigned arg = get_op_uint(ctx, 0);
2322 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2324 const unsigned arg = get_op_uint(ctx, 0);
2332 hres = to_boolean(v, &b);
2344 static HRESULT interp_pop(exec_ctx_t *ctx)
2346 const unsigned arg = get_op_uint(ctx, 0);
2350 stack_popn(ctx, arg);
2354 static HRESULT interp_ret(exec_ctx_t *ctx)
2362 static HRESULT interp_setret(exec_ctx_t *ctx)
2366 jsval_release(ctx->ret);
2367 ctx->ret = stack_pop(ctx);
2371 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2373 static const op_func_t op_funcs[] = {
2374 #define X(x,a,b,c) interp_##x,
2379 static const unsigned op_move[] = {
2380 #define X(a,x,b,c) x,
2385 static HRESULT unwind_exception(exec_ctx_t *ctx)
2387 except_frame_t *except_frame;
2392 except_frame = ctx->except_frame;
2393 ctx->except_frame = except_frame->next;
2395 assert(except_frame->stack_top <= ctx->top);
2396 stack_popn(ctx, ctx->top - except_frame->stack_top);
2398 while(except_frame->scope != ctx->scope_chain)
2399 scope_pop(&ctx->scope_chain);
2401 ctx->ip = except_frame->catch_off;
2403 except_val = ctx->script->ei.val;
2404 ctx->script->ei.val = jsval_undefined();
2405 clear_ei(ctx->script);
2407 ident = except_frame->ident;
2408 heap_free(except_frame);
2411 jsdisp_t *scope_obj;
2413 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2414 if(SUCCEEDED(hres)) {
2415 hres = jsdisp_propput_name(scope_obj, ident, except_val);
2417 jsdisp_release(scope_obj);
2419 jsval_release(except_val);
2423 hres = scope_push(ctx->scope_chain, scope_obj, to_disp(scope_obj), &ctx->scope_chain);
2424 jsdisp_release(scope_obj);
2426 hres = stack_push(ctx, except_val);
2430 hres = stack_push(ctx, jsval_bool(FALSE));
2436 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsval_t *ret)
2438 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2439 except_frame_t *prev_except_frame;
2440 function_code_t *prev_func;
2441 unsigned prev_ip, prev_top;
2442 scope_chain_t *prev_scope;
2443 bytecode_t *prev_code;
2445 HRESULT hres = S_OK;
2449 prev_top = exec_ctx->top;
2450 prev_scope = exec_ctx->scope_chain;
2451 prev_except_frame = exec_ctx->except_frame;
2452 prev_ip = exec_ctx->ip;
2453 prev_code = exec_ctx->code;
2454 prev_func = exec_ctx->func_code;
2455 exec_ctx->ip = func->instr_off;
2456 exec_ctx->except_frame = NULL;
2457 exec_ctx->code = code;
2458 exec_ctx->func_code = func;
2460 while(exec_ctx->ip != -1) {
2461 op = code->instrs[exec_ctx->ip].op;
2462 hres = op_funcs[op](exec_ctx);
2464 TRACE("EXCEPTION %08x\n", hres);
2466 if(!exec_ctx->except_frame)
2469 hres = unwind_exception(exec_ctx);
2473 exec_ctx->ip += op_move[op];
2477 exec_ctx->ip = prev_ip;
2478 exec_ctx->except_frame = prev_except_frame;
2479 exec_ctx->code = prev_code;
2480 exec_ctx->func_code = prev_func;
2483 while(exec_ctx->scope_chain != prev_scope)
2484 scope_pop(&exec_ctx->scope_chain);
2485 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2489 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2490 assert(exec_ctx->scope_chain == prev_scope);
2491 assert(exec_ctx->top == prev_top);
2493 *ret = exec_ctx->ret;
2494 exec_ctx->ret = jsval_undefined();
2498 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval, jsval_t *ret)
2500 exec_ctx_t *prev_ctx;
2503 HRESULT hres = S_OK;
2505 for(i = 0; i < func->func_cnt; i++) {
2508 if(!func->funcs[i].name)
2511 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2515 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, jsval_obj(func_obj));
2516 jsdisp_release(func_obj);
2521 for(i=0; i < func->var_cnt; i++) {
2522 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2525 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2531 prev_ctx = ctx->script->exec_ctx;
2532 ctx->script->exec_ctx = ctx;
2534 hres = enter_bytecode(ctx->script, code, func, &val);
2535 assert(ctx->script->exec_ctx == ctx);
2536 ctx->script->exec_ctx = prev_ctx;