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, WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
355 jsdisp = iface_to_jsdisp((IUnknown*)disp);
357 hres = jsdisp_get_id(jsdisp, name, flags, id);
358 jsdisp_release(jsdisp);
363 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
364 if(SUCCEEDED(hres)) {
365 BSTR str = name_bstr;
368 str = SysAllocString(name);
370 hres = IDispatchEx_GetDispID(dispex, str, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
372 hres = E_OUTOFMEMORY;
373 IDispatchEx_Release(dispex);
377 TRACE("using IDispatch\n");
379 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
382 static inline BOOL var_is_null(const VARIANT *v)
384 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
387 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
389 IObjectIdentity *identity;
390 IUnknown *unk1, *unk2;
398 if(!disp1 || !disp2) {
403 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
407 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
409 IUnknown_Release(unk1);
416 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
417 if(SUCCEEDED(hres)) {
418 hres = IObjectIdentity_IsEqualObject(identity, unk2);
419 IObjectIdentity_Release(identity);
426 IUnknown_Release(unk1);
427 IUnknown_Release(unk2);
431 /* ECMA-262 3rd Edition 11.9.6 */
432 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
434 jsval_type_t type = jsval_type(lval);
438 if(type != jsval_type(rval)) {
439 if(is_null_instance(lval))
440 *ret = is_null_instance(rval);
452 return disp_cmp(get_object(lval), get_object(rval), ret);
454 *ret = jsstr_eq(get_string(lval), get_string(rval));
457 *ret = get_number(lval) == get_number(rval);
460 *ret = !get_bool(lval) == !get_bool(rval);
463 FIXME("VARIANT not implemented\n");
470 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
476 for(item = ctx->named_items; item; item = item->next) {
477 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
478 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
479 if(SUCCEEDED(hres)) {
481 exprval_set_idref(ret, item->disp, id);
490 /* ECMA-262 3rd Edition 10.1.4 */
491 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
493 scope_chain_t *scope;
498 TRACE("%s\n", debugstr_w(identifier));
500 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
502 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
504 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
505 if(SUCCEEDED(hres)) {
506 exprval_set_idref(ret, scope->obj, id);
511 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
512 if(SUCCEEDED(hres)) {
513 exprval_set_idref(ret, to_disp(ctx->global), id);
517 for(item = ctx->named_items; item; item = item->next) {
518 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
525 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
526 SCRIPTINFO_IUNKNOWN, &unk, NULL);
528 WARN("GetItemInfo failed: %08x\n", hres);
532 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
533 IUnknown_Release(unk);
535 WARN("object does not implement IDispatch\n");
540 IDispatch_AddRef(item->disp);
541 ret->type = EXPRVAL_JSVAL;
542 ret->u.val = jsval_disp(item->disp);
547 if(lookup_global_members(ctx, identifier, ret))
550 ret->type = EXPRVAL_INVALID;
554 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
555 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
558 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
559 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
562 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
563 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
566 static inline jsstr_t *get_op_str(exec_ctx_t *ctx, int i){
567 return ctx->code->instrs[ctx->ip].u.arg[i].str;
570 static inline double get_op_double(exec_ctx_t *ctx){
571 return ctx->code->instrs[ctx->ip].u.dbl;
574 /* ECMA-262 3rd Edition 12.2 */
575 static HRESULT interp_var_set(exec_ctx_t *ctx)
577 const BSTR name = get_op_bstr(ctx, 0);
581 TRACE("%s\n", debugstr_w(name));
583 val = stack_pop(ctx);
584 hres = jsdisp_propput_name(ctx->var_disp, name, val);
589 /* ECMA-262 3rd Edition 12.6.4 */
590 static HRESULT interp_forin(exec_ctx_t *ctx)
592 const HRESULT arg = get_op_uint(ctx, 0);
593 IDispatch *var_obj, *obj = NULL;
601 assert(is_number(stack_top(ctx)));
602 id = get_number(stack_top(ctx));
604 var_obj = stack_topn_objid(ctx, 1, &var_id);
606 FIXME("invalid ref\n");
610 if(is_object_instance(stack_topn(ctx, 3)))
611 obj = get_object(stack_topn(ctx, 3));
614 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
615 if(SUCCEEDED(hres)) {
616 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
618 hres = IDispatchEx_GetMemberName(dispex, id, &name);
619 IDispatchEx_Release(dispex);
623 TRACE("No IDispatchEx\n");
630 str = jsstr_alloc_len(name, SysStringLen(name));
633 return E_OUTOFMEMORY;
636 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
638 hres = disp_propput(ctx->script, var_obj, var_id, jsval_string(str));
651 /* ECMA-262 3rd Edition 12.10 */
652 static HRESULT interp_push_scope(exec_ctx_t *ctx)
661 hres = to_object(ctx->script, v, &disp);
666 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
667 IDispatch_Release(disp);
671 /* ECMA-262 3rd Edition 12.10 */
672 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
676 scope_pop(&ctx->scope_chain);
680 /* ECMA-262 3rd Edition 12.13 */
681 static HRESULT interp_case(exec_ctx_t *ctx)
683 const unsigned arg = get_op_uint(ctx, 0);
691 hres = equal2_values(stack_top(ctx), v, &b);
705 /* ECMA-262 3rd Edition 12.13 */
706 static HRESULT interp_throw(exec_ctx_t *ctx)
710 jsval_release(ctx->script->ei.val);
711 ctx->script->ei.val = stack_pop(ctx);
712 return DISP_E_EXCEPTION;
715 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
717 const HRESULT arg = get_op_uint(ctx, 0);
719 TRACE("%08x\n", arg);
721 return throw_reference_error(ctx->script, arg, NULL);
724 static HRESULT interp_throw_type(exec_ctx_t *ctx)
726 const HRESULT hres = get_op_uint(ctx, 0);
727 jsstr_t *str = get_op_str(ctx, 1);
729 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
731 return throw_type_error(ctx->script, hres, str->str);
734 /* ECMA-262 3rd Edition 12.14 */
735 static HRESULT interp_push_except(exec_ctx_t *ctx)
737 const unsigned arg1 = get_op_uint(ctx, 0);
738 const BSTR arg2 = get_op_bstr(ctx, 1);
739 except_frame_t *except;
744 stack_top = ctx->top;
749 hres = stack_push(ctx, jsval_bool(TRUE));
752 hres = stack_push(ctx, jsval_bool(TRUE));
757 except = heap_alloc(sizeof(*except));
759 return E_OUTOFMEMORY;
761 except->stack_top = stack_top;
762 except->scope = ctx->scope_chain;
763 except->catch_off = arg1;
764 except->ident = arg2;
765 except->next = ctx->except_frame;
766 ctx->except_frame = except;
770 /* ECMA-262 3rd Edition 12.14 */
771 static HRESULT interp_pop_except(exec_ctx_t *ctx)
773 except_frame_t *except;
777 except = ctx->except_frame;
778 assert(except != NULL);
780 ctx->except_frame = except->next;
785 /* ECMA-262 3rd Edition 12.14 */
786 static HRESULT interp_end_finally(exec_ctx_t *ctx)
792 assert(is_bool(stack_top(ctx)));
793 if(!get_bool(stack_top(ctx))) {
794 TRACE("passing exception\n");
799 ctx->script->ei.val = stack_pop(ctx);
800 return DISP_E_EXCEPTION;
807 /* ECMA-262 3rd Edition 13 */
808 static HRESULT interp_func(exec_ctx_t *ctx)
810 unsigned func_idx = get_op_uint(ctx, 0);
814 TRACE("%d\n", func_idx);
816 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
817 ctx->scope_chain, &dispex);
821 return stack_push(ctx, jsval_obj(dispex));
824 /* ECMA-262 3rd Edition 11.2.1 */
825 static HRESULT interp_array(exec_ctx_t *ctx)
835 namev = stack_pop(ctx);
837 hres = stack_pop_object(ctx, &obj);
839 jsval_release(namev);
843 hres = to_string(ctx->script, namev, &name);
844 jsval_release(namev);
846 IDispatch_Release(obj);
850 hres = disp_get_id(ctx->script, obj, name->str, NULL, 0, &id);
852 if(SUCCEEDED(hres)) {
853 hres = disp_propget(ctx->script, obj, id, &v);
854 }else if(hres == DISP_E_UNKNOWNNAME) {
855 v = jsval_undefined();
858 IDispatch_Release(obj);
862 return stack_push(ctx, v);
865 /* ECMA-262 3rd Edition 11.2.1 */
866 static HRESULT interp_member(exec_ctx_t *ctx)
868 const BSTR arg = get_op_bstr(ctx, 0);
876 hres = stack_pop_object(ctx, &obj);
880 hres = disp_get_id(ctx->script, obj, arg, arg, 0, &id);
881 if(SUCCEEDED(hres)) {
882 hres = disp_propget(ctx->script, obj, id, &v);
883 }else if(hres == DISP_E_UNKNOWNNAME) {
884 v = jsval_undefined();
887 IDispatch_Release(obj);
891 return stack_push(ctx, v);
894 /* ECMA-262 3rd Edition 11.2.1 */
895 static HRESULT interp_memberid(exec_ctx_t *ctx)
897 const unsigned arg = get_op_uint(ctx, 0);
906 namev = stack_pop(ctx);
907 objv = stack_pop(ctx);
909 hres = to_object(ctx->script, objv, &obj);
911 if(SUCCEEDED(hres)) {
912 hres = to_string(ctx->script, namev, &name);
914 IDispatch_Release(obj);
916 jsval_release(namev);
920 hres = disp_get_id(ctx->script, obj, name->str, NULL, arg, &id);
923 IDispatch_Release(obj);
924 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
926 id = JS_E_INVALID_PROPERTY;
928 ERR("failed %08x\n", hres);
933 return stack_push_objid(ctx, obj, id);
936 /* ECMA-262 3rd Edition 11.2.1 */
937 static HRESULT interp_refval(exec_ctx_t *ctx)
946 disp = stack_topn_objid(ctx, 0, &id);
948 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
950 hres = disp_propget(ctx->script, disp, id, &v);
954 return stack_push(ctx, v);
957 /* ECMA-262 3rd Edition 11.2.2 */
958 static HRESULT interp_new(exec_ctx_t *ctx)
960 const unsigned argc = get_op_uint(ctx, 0);
966 constr = stack_topn(ctx, argc);
968 /* NOTE: Should use to_object here */
971 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
972 else if(!is_object_instance(constr))
973 return throw_type_error(ctx->script, JS_E_INVALID_ACTION, NULL);
974 else if(!get_object(constr))
975 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
977 hres = disp_call_value(ctx->script, get_object(constr), NULL, DISPATCH_CONSTRUCT, argc, stack_args(ctx, argc), &r);
981 stack_popn(ctx, argc+1);
982 return stack_push(ctx, r);
985 /* ECMA-262 3rd Edition 11.2.3 */
986 static HRESULT interp_call(exec_ctx_t *ctx)
988 const unsigned argn = get_op_uint(ctx, 0);
989 const int do_ret = get_op_int(ctx, 1);
993 TRACE("%d %d\n", argn, do_ret);
995 obj = stack_topn(ctx, argn);
996 if(!is_object_instance(obj))
997 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
999 hres = disp_call_value(ctx->script, get_object(obj), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
1000 do_ret ? &r : NULL);
1004 stack_popn(ctx, argn+1);
1005 return do_ret ? stack_push(ctx, r) : S_OK;
1008 /* ECMA-262 3rd Edition 11.2.3 */
1009 static HRESULT interp_call_member(exec_ctx_t *ctx)
1011 const unsigned argn = get_op_uint(ctx, 0);
1012 const int do_ret = get_op_int(ctx, 1);
1018 TRACE("%d %d\n", argn, do_ret);
1020 obj = stack_topn_objid(ctx, argn, &id);
1022 return throw_type_error(ctx->script, id, NULL);
1024 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &r : NULL);
1028 stack_popn(ctx, argn+2);
1029 return do_ret ? stack_push(ctx, r) : S_OK;
1033 /* ECMA-262 3rd Edition 11.1.1 */
1034 static HRESULT interp_this(exec_ctx_t *ctx)
1038 IDispatch_AddRef(ctx->this_obj);
1039 return stack_push(ctx, jsval_disp(ctx->this_obj));
1042 /* ECMA-262 3rd Edition 10.1.4 */
1043 static HRESULT interp_ident(exec_ctx_t *ctx)
1045 const BSTR arg = get_op_bstr(ctx, 0);
1050 TRACE("%s\n", debugstr_w(arg));
1052 hres = identifier_eval(ctx->script, arg, &exprval);
1056 if(exprval.type == EXPRVAL_INVALID)
1057 return throw_type_error(ctx->script, JS_E_UNDEFINED_VARIABLE, arg);
1059 hres = exprval_to_value(ctx->script, &exprval, &v);
1060 exprval_release(&exprval);
1064 return stack_push(ctx, v);
1067 /* ECMA-262 3rd Edition 10.1.4 */
1068 static HRESULT interp_identid(exec_ctx_t *ctx)
1070 const BSTR arg = get_op_bstr(ctx, 0);
1071 const unsigned flags = get_op_uint(ctx, 1);
1075 TRACE("%s %x\n", debugstr_w(arg), flags);
1077 hres = identifier_eval(ctx->script, arg, &exprval);
1081 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1084 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1088 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1091 if(exprval.type != EXPRVAL_IDREF) {
1092 WARN("invalid ref\n");
1093 exprval_release(&exprval);
1094 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1097 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1100 /* ECMA-262 3rd Edition 7.8.1 */
1101 static HRESULT interp_null(exec_ctx_t *ctx)
1105 return stack_push(ctx, jsval_null());
1108 /* ECMA-262 3rd Edition 7.8.2 */
1109 static HRESULT interp_bool(exec_ctx_t *ctx)
1111 const int arg = get_op_int(ctx, 0);
1113 TRACE("%s\n", arg ? "true" : "false");
1115 return stack_push(ctx, jsval_bool(arg));
1118 /* ECMA-262 3rd Edition 7.8.3 */
1119 static HRESULT interp_int(exec_ctx_t *ctx)
1121 const int arg = get_op_int(ctx, 0);
1125 return stack_push(ctx, jsval_number(arg));
1128 /* ECMA-262 3rd Edition 7.8.3 */
1129 static HRESULT interp_double(exec_ctx_t *ctx)
1131 const double arg = get_op_double(ctx);
1133 TRACE("%lf\n", arg);
1135 return stack_push(ctx, jsval_number(arg));
1138 /* ECMA-262 3rd Edition 7.8.4 */
1139 static HRESULT interp_str(exec_ctx_t *ctx)
1141 jsstr_t *str = get_op_str(ctx, 0);
1143 TRACE("%s\n", debugstr_jsstr(str));
1145 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1148 /* ECMA-262 3rd Edition 7.8 */
1149 static HRESULT interp_regexp(exec_ctx_t *ctx)
1151 jsstr_t *source = get_op_str(ctx, 0);
1152 const unsigned flags = get_op_uint(ctx, 1);
1156 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1158 hres = create_regexp(ctx->script, source, flags, ®exp);
1162 return stack_push(ctx, jsval_obj(regexp));
1165 /* ECMA-262 3rd Edition 11.1.4 */
1166 static HRESULT interp_carray(exec_ctx_t *ctx)
1168 const unsigned arg = get_op_uint(ctx, 0);
1176 hres = create_array(ctx->script, arg, &array);
1182 val = stack_pop(ctx);
1183 hres = jsdisp_propput_idx(array, i, val);
1186 jsdisp_release(array);
1191 return stack_push(ctx, jsval_obj(array));
1194 /* ECMA-262 3rd Edition 11.1.5 */
1195 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1202 hres = create_object(ctx->script, NULL, &obj);
1206 return stack_push(ctx, jsval_obj(obj));
1209 /* ECMA-262 3rd Edition 11.1.5 */
1210 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1212 const BSTR name = get_op_bstr(ctx, 0);
1217 TRACE("%s\n", debugstr_w(name));
1219 val = stack_pop(ctx);
1221 assert(is_object_instance(stack_top(ctx)));
1222 obj = as_jsdisp(get_object(stack_top(ctx)));
1224 hres = jsdisp_propput_name(obj, name, val);
1229 /* ECMA-262 3rd Edition 11.11 */
1230 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1232 const unsigned arg = get_op_uint(ctx, 0);
1238 hres = to_boolean(stack_top(ctx), &b);
1251 /* ECMA-262 3rd Edition 11.11 */
1252 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1254 const unsigned arg = get_op_uint(ctx, 0);
1260 hres = to_boolean(stack_top(ctx), &b);
1273 /* ECMA-262 3rd Edition 11.10 */
1274 static HRESULT interp_or(exec_ctx_t *ctx)
1281 hres = stack_pop_int(ctx, &r);
1285 hres = stack_pop_int(ctx, &l);
1289 return stack_push(ctx, jsval_number(l|r));
1292 /* ECMA-262 3rd Edition 11.10 */
1293 static HRESULT interp_xor(exec_ctx_t *ctx)
1300 hres = stack_pop_int(ctx, &r);
1304 hres = stack_pop_int(ctx, &l);
1308 return stack_push(ctx, jsval_number(l^r));
1311 /* ECMA-262 3rd Edition 11.10 */
1312 static HRESULT interp_and(exec_ctx_t *ctx)
1319 hres = stack_pop_int(ctx, &r);
1323 hres = stack_pop_int(ctx, &l);
1327 return stack_push(ctx, jsval_number(l&r));
1330 /* ECMA-262 3rd Edition 11.8.6 */
1331 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1333 jsdisp_t *obj, *iter, *tmp = NULL;
1338 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1341 if(!is_object_instance(v) || !get_object(v)) {
1343 return throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1346 obj = iface_to_jsdisp((IUnknown*)get_object(v));
1347 IDispatch_Release(get_object(v));
1349 FIXME("non-jsdisp objects not supported\n");
1353 if(is_class(obj, JSCLASS_FUNCTION)) {
1354 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1356 hres = throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1358 jsdisp_release(obj);
1364 if(is_object_instance(prot)) {
1365 if(is_object_instance(v))
1366 tmp = iface_to_jsdisp((IUnknown*)get_object(v));
1367 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1368 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1374 jsdisp_release(tmp);
1376 FIXME("prototype is not an object\n");
1380 jsval_release(prot);
1385 return stack_push(ctx, jsval_bool(ret));
1388 /* ECMA-262 3rd Edition 11.8.7 */
1389 static HRESULT interp_in(exec_ctx_t *ctx)
1399 obj = stack_pop(ctx);
1400 if(!is_object_instance(obj) || !get_object(obj)) {
1402 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1406 hres = to_string(ctx->script, v, &str);
1409 IDispatch_Release(get_object(obj));
1413 hres = disp_get_id(ctx->script, get_object(obj), str->str, NULL, 0, &id);
1414 IDispatch_Release(get_object(obj));
1418 else if(hres == DISP_E_UNKNOWNNAME)
1423 return stack_push(ctx, jsval_bool(ret));
1426 /* ECMA-262 3rd Edition 11.6.1 */
1427 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1432 hres = to_primitive(ctx, lval, &l, NO_HINT);
1436 hres = to_primitive(ctx, rval, &r, NO_HINT);
1442 if(is_string(l) || is_string(r)) {
1443 jsstr_t *lstr, *rstr = NULL;
1445 hres = to_string(ctx, l, &lstr);
1447 hres = to_string(ctx, r, &rstr);
1449 if(SUCCEEDED(hres)) {
1452 ret_str = jsstr_concat(lstr, rstr);
1454 *ret = jsval_string(ret_str);
1456 hres = E_OUTOFMEMORY;
1459 jsstr_release(lstr);
1461 jsstr_release(rstr);
1465 hres = to_number(ctx, l, &nl);
1466 if(SUCCEEDED(hres)) {
1467 hres = to_number(ctx, r, &nr);
1469 *ret = jsval_number(nl+nr);
1478 /* ECMA-262 3rd Edition 11.6.1 */
1479 static HRESULT interp_add(exec_ctx_t *ctx)
1487 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1489 hres = add_eval(ctx->script, l, r, &ret);
1495 return stack_push(ctx, ret);
1498 /* ECMA-262 3rd Edition 11.6.2 */
1499 static HRESULT interp_sub(exec_ctx_t *ctx)
1506 hres = stack_pop_number(ctx, &r);
1510 hres = stack_pop_number(ctx, &l);
1514 return stack_push(ctx, jsval_number(l-r));
1517 /* ECMA-262 3rd Edition 11.5.1 */
1518 static HRESULT interp_mul(exec_ctx_t *ctx)
1525 hres = stack_pop_number(ctx, &r);
1529 hres = stack_pop_number(ctx, &l);
1533 return stack_push(ctx, jsval_number(l*r));
1536 /* ECMA-262 3rd Edition 11.5.2 */
1537 static HRESULT interp_div(exec_ctx_t *ctx)
1544 hres = stack_pop_number(ctx, &r);
1548 hres = stack_pop_number(ctx, &l);
1552 return stack_push(ctx, jsval_number(l/r));
1555 /* ECMA-262 3rd Edition 11.5.3 */
1556 static HRESULT interp_mod(exec_ctx_t *ctx)
1563 hres = stack_pop_number(ctx, &r);
1567 hres = stack_pop_number(ctx, &l);
1571 return stack_push(ctx, jsval_number(fmod(l, r)));
1574 /* ECMA-262 3rd Edition 11.4.2 */
1575 static HRESULT interp_delete(exec_ctx_t *ctx)
1577 jsval_t objv, namev;
1585 namev = stack_pop(ctx);
1586 objv = stack_pop(ctx);
1588 hres = to_object(ctx->script, objv, &obj);
1589 jsval_release(objv);
1591 jsval_release(namev);
1595 hres = to_string(ctx->script, namev, &name);
1596 jsval_release(namev);
1598 IDispatch_Release(obj);
1602 hres = disp_delete_name(ctx->script, obj, name, &ret);
1603 IDispatch_Release(obj);
1604 jsstr_release(name);
1608 return stack_push(ctx, jsval_bool(ret));
1611 /* ECMA-262 3rd Edition 11.4.2 */
1612 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1614 const BSTR arg = get_op_bstr(ctx, 0);
1619 TRACE("%s\n", debugstr_w(arg));
1621 hres = identifier_eval(ctx->script, arg, &exprval);
1625 switch(exprval.type) {
1627 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1628 IDispatch_Release(exprval.u.idref.disp);
1632 case EXPRVAL_INVALID:
1636 FIXME("Unsupported exprval\n");
1637 exprval_release(&exprval);
1642 return stack_push(ctx, jsval_bool(ret));
1645 /* ECMA-262 3rd Edition 11.4.2 */
1646 static HRESULT interp_void(exec_ctx_t *ctx)
1651 return stack_push(ctx, jsval_undefined());
1654 /* ECMA-262 3rd Edition 11.4.3 */
1655 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1657 switch(jsval_type(v)) {
1667 if(get_object(v) && (dispex = iface_to_jsdisp((IUnknown*)get_object(v)))) {
1668 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1669 jsdisp_release(dispex);
1685 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1692 /* ECMA-262 3rd Edition 11.4.3 */
1693 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1701 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1705 obj = stack_pop_objid(ctx, &id);
1707 return stack_push_string(ctx, undefinedW);
1709 hres = disp_propget(ctx->script, obj, id, &v);
1710 IDispatch_Release(obj);
1712 return stack_push_string(ctx, unknownW);
1714 hres = typeof_string(v, &ret);
1719 return stack_push_string(ctx, ret);
1722 /* ECMA-262 3rd Edition 11.4.3 */
1723 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1725 const BSTR arg = get_op_bstr(ctx, 0);
1731 TRACE("%s\n", debugstr_w(arg));
1733 hres = identifier_eval(ctx->script, arg, &exprval);
1737 if(exprval.type == EXPRVAL_INVALID) {
1738 hres = stack_push_string(ctx, undefinedW);
1739 exprval_release(&exprval);
1743 hres = exprval_to_value(ctx->script, &exprval, &v);
1744 exprval_release(&exprval);
1748 hres = typeof_string(v, &ret);
1753 return stack_push_string(ctx, ret);
1756 /* ECMA-262 3rd Edition 11.4.3 */
1757 static HRESULT interp_typeof(exec_ctx_t *ctx)
1766 hres = typeof_string(v, &ret);
1771 return stack_push_string(ctx, ret);
1774 /* ECMA-262 3rd Edition 11.4.7 */
1775 static HRESULT interp_minus(exec_ctx_t *ctx)
1782 hres = stack_pop_number(ctx, &n);
1786 return stack_push(ctx, jsval_number(-n));
1789 /* ECMA-262 3rd Edition 11.4.6 */
1790 static HRESULT interp_tonum(exec_ctx_t *ctx)
1799 hres = to_number(ctx->script, v, &n);
1804 return stack_push(ctx, jsval_number(n));
1807 /* ECMA-262 3rd Edition 11.3.1 */
1808 static HRESULT interp_postinc(exec_ctx_t *ctx)
1810 const int arg = get_op_int(ctx, 0);
1818 obj = stack_pop_objid(ctx, &id);
1820 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1822 hres = disp_propget(ctx->script, obj, id, &v);
1823 if(SUCCEEDED(hres)) {
1826 hres = to_number(ctx->script, v, &n);
1828 hres = disp_propput(ctx->script, obj, id, jsval_number(n+(double)arg));
1832 IDispatch_Release(obj);
1836 return stack_push(ctx, v);
1839 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1840 static HRESULT interp_preinc(exec_ctx_t *ctx)
1842 const int arg = get_op_int(ctx, 0);
1851 obj = stack_pop_objid(ctx, &id);
1853 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1855 hres = disp_propget(ctx->script, obj, id, &v);
1856 if(SUCCEEDED(hres)) {
1859 hres = to_number(ctx->script, v, &n);
1861 if(SUCCEEDED(hres)) {
1862 ret = n+(double)arg;
1863 hres = disp_propput(ctx->script, obj, id, jsval_number(ret));
1866 IDispatch_Release(obj);
1870 return stack_push(ctx, jsval_number(ret));
1873 /* ECMA-262 3rd Edition 11.9.3 */
1874 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
1876 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
1877 return equal2_values(lval, rval, ret);
1879 /* FIXME: NULL disps should be handled in more general way */
1880 if(is_object_instance(lval) && !get_object(lval))
1881 return equal_values(ctx, jsval_null(), rval, ret);
1882 if(is_object_instance(rval) && !get_object(rval))
1883 return equal_values(ctx, lval, jsval_null(), ret);
1885 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
1890 if(is_string(lval) && is_number(rval)) {
1894 hres = to_number(ctx, lval, &n);
1898 /* FIXME: optimize */
1899 return equal_values(ctx, jsval_number(n), rval, ret);
1902 if(is_string(rval) && is_number(lval)) {
1906 hres = to_number(ctx, rval, &n);
1910 /* FIXME: optimize */
1911 return equal_values(ctx, lval, jsval_number(n), ret);
1915 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
1918 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
1921 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
1925 hres = to_primitive(ctx, rval, &prim, NO_HINT);
1929 hres = equal_values(ctx, lval, prim, ret);
1930 jsval_release(prim);
1935 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
1939 hres = to_primitive(ctx, lval, &prim, NO_HINT);
1943 hres = equal_values(ctx, prim, rval, ret);
1944 jsval_release(prim);
1953 /* ECMA-262 3rd Edition 11.9.1 */
1954 static HRESULT interp_eq(exec_ctx_t *ctx)
1963 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
1965 hres = equal_values(ctx->script, l, r, &b);
1971 return stack_push(ctx, jsval_bool(b));
1974 /* ECMA-262 3rd Edition 11.9.2 */
1975 static HRESULT interp_neq(exec_ctx_t *ctx)
1984 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
1986 hres = equal_values(ctx->script, l, r, &b);
1992 return stack_push(ctx, jsval_bool(!b));
1995 /* ECMA-262 3rd Edition 11.9.4 */
1996 static HRESULT interp_eq2(exec_ctx_t *ctx)
2007 hres = equal2_values(r, l, &b);
2013 return stack_push(ctx, jsval_bool(b));
2016 /* ECMA-262 3rd Edition 11.9.5 */
2017 static HRESULT interp_neq2(exec_ctx_t *ctx)
2028 hres = equal2_values(r, l, &b);
2034 return stack_push(ctx, jsval_bool(!b));
2037 /* ECMA-262 3rd Edition 11.8.5 */
2038 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2044 hres = to_primitive(ctx, lval, &l, NO_HINT);
2048 hres = to_primitive(ctx, rval, &r, NO_HINT);
2054 if(is_string(l) && is_string(r)) {
2055 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2056 jsstr_release(get_string(l));
2057 jsstr_release(get_string(r));
2061 hres = to_number(ctx, l, &ln);
2064 hres = to_number(ctx, r, &rn);
2069 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2073 /* ECMA-262 3rd Edition 11.8.1 */
2074 static HRESULT interp_lt(exec_ctx_t *ctx)
2083 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2085 hres = less_eval(ctx->script, l, r, FALSE, &b);
2091 return stack_push(ctx, jsval_bool(b));
2094 /* ECMA-262 3rd Edition 11.8.1 */
2095 static HRESULT interp_lteq(exec_ctx_t *ctx)
2104 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2106 hres = less_eval(ctx->script, r, l, TRUE, &b);
2112 return stack_push(ctx, jsval_bool(b));
2115 /* ECMA-262 3rd Edition 11.8.2 */
2116 static HRESULT interp_gt(exec_ctx_t *ctx)
2125 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2127 hres = less_eval(ctx->script, r, l, FALSE, &b);
2133 return stack_push(ctx, jsval_bool(b));
2136 /* ECMA-262 3rd Edition 11.8.4 */
2137 static HRESULT interp_gteq(exec_ctx_t *ctx)
2146 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2148 hres = less_eval(ctx->script, l, r, TRUE, &b);
2154 return stack_push(ctx, jsval_bool(b));
2157 /* ECMA-262 3rd Edition 11.4.8 */
2158 static HRESULT interp_bneg(exec_ctx_t *ctx)
2167 hres = to_int32(ctx->script, v, &i);
2172 return stack_push(ctx, jsval_number(~i));
2175 /* ECMA-262 3rd Edition 11.4.9 */
2176 static HRESULT interp_neg(exec_ctx_t *ctx)
2185 hres = to_boolean(v, &b);
2190 return stack_push(ctx, jsval_bool(!b));
2193 /* ECMA-262 3rd Edition 11.7.1 */
2194 static HRESULT interp_lshift(exec_ctx_t *ctx)
2200 hres = stack_pop_uint(ctx, &r);
2204 hres = stack_pop_int(ctx, &l);
2208 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2211 /* ECMA-262 3rd Edition 11.7.2 */
2212 static HRESULT interp_rshift(exec_ctx_t *ctx)
2218 hres = stack_pop_uint(ctx, &r);
2222 hres = stack_pop_int(ctx, &l);
2226 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2229 /* ECMA-262 3rd Edition 11.7.3 */
2230 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2235 hres = stack_pop_uint(ctx, &r);
2239 hres = stack_pop_uint(ctx, &l);
2243 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2246 /* ECMA-262 3rd Edition 11.13.1 */
2247 static HRESULT interp_assign(exec_ctx_t *ctx)
2258 disp = stack_pop_objid(ctx, &id);
2261 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2264 hres = disp_propput(ctx->script, disp, id, v);
2265 IDispatch_Release(disp);
2271 return stack_push(ctx, v);
2274 /* JScript extension */
2275 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2277 const unsigned argc = get_op_uint(ctx, 0);
2283 TRACE("%u\n", argc);
2285 disp = stack_topn_objid(ctx, argc+1, &id);
2287 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2289 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2294 stack_popn(ctx, argc+2);
2295 return stack_push(ctx, v);
2298 static HRESULT interp_undefined(exec_ctx_t *ctx)
2302 return stack_push(ctx, jsval_undefined());
2305 static HRESULT interp_jmp(exec_ctx_t *ctx)
2307 const unsigned arg = get_op_uint(ctx, 0);
2315 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2317 const unsigned arg = get_op_uint(ctx, 0);
2325 hres = to_boolean(v, &b);
2337 static HRESULT interp_pop(exec_ctx_t *ctx)
2339 const unsigned arg = get_op_uint(ctx, 0);
2343 stack_popn(ctx, arg);
2347 static HRESULT interp_ret(exec_ctx_t *ctx)
2355 static HRESULT interp_setret(exec_ctx_t *ctx)
2359 jsval_release(ctx->ret);
2360 ctx->ret = stack_pop(ctx);
2364 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2366 static const op_func_t op_funcs[] = {
2367 #define X(x,a,b,c) interp_##x,
2372 static const unsigned op_move[] = {
2373 #define X(a,x,b,c) x,
2378 static HRESULT unwind_exception(exec_ctx_t *ctx)
2380 except_frame_t *except_frame;
2385 except_frame = ctx->except_frame;
2386 ctx->except_frame = except_frame->next;
2388 assert(except_frame->stack_top <= ctx->top);
2389 stack_popn(ctx, ctx->top - except_frame->stack_top);
2391 while(except_frame->scope != ctx->scope_chain)
2392 scope_pop(&ctx->scope_chain);
2394 ctx->ip = except_frame->catch_off;
2396 except_val = ctx->script->ei.val;
2397 ctx->script->ei.val = jsval_undefined();
2398 clear_ei(ctx->script);
2400 ident = except_frame->ident;
2401 heap_free(except_frame);
2404 jsdisp_t *scope_obj;
2406 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2407 if(SUCCEEDED(hres)) {
2408 hres = jsdisp_propput_name(scope_obj, ident, except_val);
2410 jsdisp_release(scope_obj);
2412 jsval_release(except_val);
2416 hres = scope_push(ctx->scope_chain, scope_obj, to_disp(scope_obj), &ctx->scope_chain);
2417 jsdisp_release(scope_obj);
2419 hres = stack_push(ctx, except_val);
2423 hres = stack_push(ctx, jsval_bool(FALSE));
2429 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsval_t *ret)
2431 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2432 except_frame_t *prev_except_frame;
2433 function_code_t *prev_func;
2434 unsigned prev_ip, prev_top;
2435 scope_chain_t *prev_scope;
2436 bytecode_t *prev_code;
2438 HRESULT hres = S_OK;
2442 prev_top = exec_ctx->top;
2443 prev_scope = exec_ctx->scope_chain;
2444 prev_except_frame = exec_ctx->except_frame;
2445 prev_ip = exec_ctx->ip;
2446 prev_code = exec_ctx->code;
2447 prev_func = exec_ctx->func_code;
2448 exec_ctx->ip = func->instr_off;
2449 exec_ctx->except_frame = NULL;
2450 exec_ctx->code = code;
2451 exec_ctx->func_code = func;
2453 while(exec_ctx->ip != -1) {
2454 op = code->instrs[exec_ctx->ip].op;
2455 hres = op_funcs[op](exec_ctx);
2457 TRACE("EXCEPTION %08x\n", hres);
2459 if(!exec_ctx->except_frame)
2462 hres = unwind_exception(exec_ctx);
2466 exec_ctx->ip += op_move[op];
2470 exec_ctx->ip = prev_ip;
2471 exec_ctx->except_frame = prev_except_frame;
2472 exec_ctx->code = prev_code;
2473 exec_ctx->func_code = prev_func;
2476 while(exec_ctx->scope_chain != prev_scope)
2477 scope_pop(&exec_ctx->scope_chain);
2478 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2482 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2483 assert(exec_ctx->scope_chain == prev_scope);
2484 assert(exec_ctx->top == prev_top);
2486 *ret = exec_ctx->ret;
2487 exec_ctx->ret = jsval_undefined();
2491 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval, jsval_t *ret)
2493 exec_ctx_t *prev_ctx;
2496 HRESULT hres = S_OK;
2498 for(i = 0; i < func->func_cnt; i++) {
2501 if(!func->funcs[i].name)
2504 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2508 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, jsval_obj(func_obj));
2509 jsdisp_release(func_obj);
2514 for(i=0; i < func->var_cnt; i++) {
2515 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2518 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2524 prev_ctx = ctx->script->exec_ctx;
2525 ctx->script->exec_ctx = ctx;
2527 hres = enter_bytecode(ctx->script, code, func, &val);
2528 assert(ctx->script->exec_ctx == ctx);
2529 ctx->script->exec_ctx = prev_ctx;