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)
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, &name, 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);
733 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
735 return throw_type_error(ctx->script, hres, str->str);
738 /* ECMA-262 3rd Edition 12.14 */
739 static HRESULT interp_push_except(exec_ctx_t *ctx)
741 const unsigned arg1 = get_op_uint(ctx, 0);
742 const BSTR arg2 = get_op_bstr(ctx, 1);
743 except_frame_t *except;
748 stack_top = ctx->top;
753 hres = stack_push(ctx, jsval_bool(TRUE));
756 hres = stack_push(ctx, jsval_bool(TRUE));
761 except = heap_alloc(sizeof(*except));
763 return E_OUTOFMEMORY;
765 except->stack_top = stack_top;
766 except->scope = ctx->scope_chain;
767 except->catch_off = arg1;
768 except->ident = arg2;
769 except->next = ctx->except_frame;
770 ctx->except_frame = except;
774 /* ECMA-262 3rd Edition 12.14 */
775 static HRESULT interp_pop_except(exec_ctx_t *ctx)
777 except_frame_t *except;
781 except = ctx->except_frame;
782 assert(except != NULL);
784 ctx->except_frame = except->next;
789 /* ECMA-262 3rd Edition 12.14 */
790 static HRESULT interp_end_finally(exec_ctx_t *ctx)
796 assert(is_bool(stack_top(ctx)));
797 if(!get_bool(stack_top(ctx))) {
798 TRACE("passing exception\n");
803 ctx->script->ei.val = stack_pop(ctx);
804 return DISP_E_EXCEPTION;
811 /* ECMA-262 3rd Edition 13 */
812 static HRESULT interp_func(exec_ctx_t *ctx)
814 unsigned func_idx = get_op_uint(ctx, 0);
818 TRACE("%d\n", func_idx);
820 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
821 ctx->scope_chain, &dispex);
825 return stack_push(ctx, jsval_obj(dispex));
828 /* ECMA-262 3rd Edition 11.2.1 */
829 static HRESULT interp_array(exec_ctx_t *ctx)
839 namev = stack_pop(ctx);
841 hres = stack_pop_object(ctx, &obj);
843 jsval_release(namev);
847 hres = to_string(ctx->script, namev, &name);
848 jsval_release(namev);
850 IDispatch_Release(obj);
854 hres = disp_get_id(ctx->script, obj, name->str, NULL, 0, &id);
856 if(SUCCEEDED(hres)) {
857 hres = disp_propget(ctx->script, obj, id, &v);
858 }else if(hres == DISP_E_UNKNOWNNAME) {
859 v = jsval_undefined();
862 IDispatch_Release(obj);
866 return stack_push(ctx, v);
869 /* ECMA-262 3rd Edition 11.2.1 */
870 static HRESULT interp_member(exec_ctx_t *ctx)
872 const BSTR arg = get_op_bstr(ctx, 0);
880 hres = stack_pop_object(ctx, &obj);
884 hres = disp_get_id(ctx->script, obj, arg, arg, 0, &id);
885 if(SUCCEEDED(hres)) {
886 hres = disp_propget(ctx->script, obj, id, &v);
887 }else if(hres == DISP_E_UNKNOWNNAME) {
888 v = jsval_undefined();
891 IDispatch_Release(obj);
895 return stack_push(ctx, v);
898 /* ECMA-262 3rd Edition 11.2.1 */
899 static HRESULT interp_memberid(exec_ctx_t *ctx)
901 const unsigned arg = get_op_uint(ctx, 0);
910 namev = stack_pop(ctx);
911 objv = stack_pop(ctx);
913 hres = to_object(ctx->script, objv, &obj);
915 if(SUCCEEDED(hres)) {
916 hres = to_string(ctx->script, namev, &name);
918 IDispatch_Release(obj);
920 jsval_release(namev);
924 hres = disp_get_id(ctx->script, obj, name->str, NULL, arg, &id);
927 IDispatch_Release(obj);
928 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
930 id = JS_E_INVALID_PROPERTY;
932 ERR("failed %08x\n", hres);
937 return stack_push_objid(ctx, obj, id);
940 /* ECMA-262 3rd Edition 11.2.1 */
941 static HRESULT interp_refval(exec_ctx_t *ctx)
950 disp = stack_topn_objid(ctx, 0, &id);
952 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
954 hres = disp_propget(ctx->script, disp, id, &v);
958 return stack_push(ctx, v);
961 /* ECMA-262 3rd Edition 11.2.2 */
962 static HRESULT interp_new(exec_ctx_t *ctx)
964 const unsigned argc = get_op_uint(ctx, 0);
970 constr = stack_topn(ctx, argc);
972 /* NOTE: Should use to_object here */
975 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
976 else if(!is_object_instance(constr))
977 return throw_type_error(ctx->script, JS_E_INVALID_ACTION, NULL);
978 else if(!get_object(constr))
979 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
981 hres = disp_call_value(ctx->script, get_object(constr), NULL, DISPATCH_CONSTRUCT, argc, stack_args(ctx, argc), &r);
985 stack_popn(ctx, argc+1);
986 return stack_push(ctx, r);
989 /* ECMA-262 3rd Edition 11.2.3 */
990 static HRESULT interp_call(exec_ctx_t *ctx)
992 const unsigned argn = get_op_uint(ctx, 0);
993 const int do_ret = get_op_int(ctx, 1);
997 TRACE("%d %d\n", argn, do_ret);
999 obj = stack_topn(ctx, argn);
1000 if(!is_object_instance(obj))
1001 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
1003 hres = disp_call_value(ctx->script, get_object(obj), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
1004 do_ret ? &r : NULL);
1008 stack_popn(ctx, argn+1);
1009 return do_ret ? stack_push(ctx, r) : S_OK;
1012 /* ECMA-262 3rd Edition 11.2.3 */
1013 static HRESULT interp_call_member(exec_ctx_t *ctx)
1015 const unsigned argn = get_op_uint(ctx, 0);
1016 const int do_ret = get_op_int(ctx, 1);
1022 TRACE("%d %d\n", argn, do_ret);
1024 obj = stack_topn_objid(ctx, argn, &id);
1026 return throw_type_error(ctx->script, id, NULL);
1028 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &r : NULL);
1032 stack_popn(ctx, argn+2);
1033 return do_ret ? stack_push(ctx, r) : S_OK;
1037 /* ECMA-262 3rd Edition 11.1.1 */
1038 static HRESULT interp_this(exec_ctx_t *ctx)
1042 IDispatch_AddRef(ctx->this_obj);
1043 return stack_push(ctx, jsval_disp(ctx->this_obj));
1046 /* ECMA-262 3rd Edition 10.1.4 */
1047 static HRESULT interp_ident(exec_ctx_t *ctx)
1049 const BSTR arg = get_op_bstr(ctx, 0);
1054 TRACE("%s\n", debugstr_w(arg));
1056 hres = identifier_eval(ctx->script, arg, &exprval);
1060 if(exprval.type == EXPRVAL_INVALID)
1061 return throw_type_error(ctx->script, JS_E_UNDEFINED_VARIABLE, arg);
1063 hres = exprval_to_value(ctx->script, &exprval, &v);
1064 exprval_release(&exprval);
1068 return stack_push(ctx, v);
1071 /* ECMA-262 3rd Edition 10.1.4 */
1072 static HRESULT interp_identid(exec_ctx_t *ctx)
1074 const BSTR arg = get_op_bstr(ctx, 0);
1075 const unsigned flags = get_op_uint(ctx, 1);
1079 TRACE("%s %x\n", debugstr_w(arg), flags);
1081 hres = identifier_eval(ctx->script, arg, &exprval);
1085 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1088 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1092 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1095 if(exprval.type != EXPRVAL_IDREF) {
1096 WARN("invalid ref\n");
1097 exprval_release(&exprval);
1098 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1101 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1104 /* ECMA-262 3rd Edition 7.8.1 */
1105 static HRESULT interp_null(exec_ctx_t *ctx)
1109 return stack_push(ctx, jsval_null());
1112 /* ECMA-262 3rd Edition 7.8.2 */
1113 static HRESULT interp_bool(exec_ctx_t *ctx)
1115 const int arg = get_op_int(ctx, 0);
1117 TRACE("%s\n", arg ? "true" : "false");
1119 return stack_push(ctx, jsval_bool(arg));
1122 /* ECMA-262 3rd Edition 7.8.3 */
1123 static HRESULT interp_int(exec_ctx_t *ctx)
1125 const int arg = get_op_int(ctx, 0);
1129 return stack_push(ctx, jsval_number(arg));
1132 /* ECMA-262 3rd Edition 7.8.3 */
1133 static HRESULT interp_double(exec_ctx_t *ctx)
1135 const double arg = get_op_double(ctx);
1137 TRACE("%lf\n", arg);
1139 return stack_push(ctx, jsval_number(arg));
1142 /* ECMA-262 3rd Edition 7.8.4 */
1143 static HRESULT interp_str(exec_ctx_t *ctx)
1145 jsstr_t *str = get_op_str(ctx, 0);
1147 TRACE("%s\n", debugstr_jsstr(str));
1149 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1152 /* ECMA-262 3rd Edition 7.8 */
1153 static HRESULT interp_regexp(exec_ctx_t *ctx)
1155 jsstr_t *source = get_op_str(ctx, 0);
1156 const unsigned flags = get_op_uint(ctx, 1);
1160 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1162 hres = create_regexp(ctx->script, source, flags, ®exp);
1166 return stack_push(ctx, jsval_obj(regexp));
1169 /* ECMA-262 3rd Edition 11.1.4 */
1170 static HRESULT interp_carray(exec_ctx_t *ctx)
1172 const unsigned arg = get_op_uint(ctx, 0);
1180 hres = create_array(ctx->script, arg, &array);
1186 val = stack_pop(ctx);
1187 hres = jsdisp_propput_idx(array, i, val);
1190 jsdisp_release(array);
1195 return stack_push(ctx, jsval_obj(array));
1198 /* ECMA-262 3rd Edition 11.1.5 */
1199 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1206 hres = create_object(ctx->script, NULL, &obj);
1210 return stack_push(ctx, jsval_obj(obj));
1213 /* ECMA-262 3rd Edition 11.1.5 */
1214 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1216 const BSTR name = get_op_bstr(ctx, 0);
1221 TRACE("%s\n", debugstr_w(name));
1223 val = stack_pop(ctx);
1225 assert(is_object_instance(stack_top(ctx)));
1226 obj = as_jsdisp(get_object(stack_top(ctx)));
1228 hres = jsdisp_propput_name(obj, name, val);
1233 /* ECMA-262 3rd Edition 11.11 */
1234 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1236 const unsigned arg = get_op_uint(ctx, 0);
1242 hres = to_boolean(stack_top(ctx), &b);
1255 /* ECMA-262 3rd Edition 11.11 */
1256 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1258 const unsigned arg = get_op_uint(ctx, 0);
1264 hres = to_boolean(stack_top(ctx), &b);
1277 /* ECMA-262 3rd Edition 11.10 */
1278 static HRESULT interp_or(exec_ctx_t *ctx)
1285 hres = stack_pop_int(ctx, &r);
1289 hres = stack_pop_int(ctx, &l);
1293 return stack_push(ctx, jsval_number(l|r));
1296 /* ECMA-262 3rd Edition 11.10 */
1297 static HRESULT interp_xor(exec_ctx_t *ctx)
1304 hres = stack_pop_int(ctx, &r);
1308 hres = stack_pop_int(ctx, &l);
1312 return stack_push(ctx, jsval_number(l^r));
1315 /* ECMA-262 3rd Edition 11.10 */
1316 static HRESULT interp_and(exec_ctx_t *ctx)
1323 hres = stack_pop_int(ctx, &r);
1327 hres = stack_pop_int(ctx, &l);
1331 return stack_push(ctx, jsval_number(l&r));
1334 /* ECMA-262 3rd Edition 11.8.6 */
1335 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1337 jsdisp_t *obj, *iter, *tmp = NULL;
1342 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1345 if(!is_object_instance(v) || !get_object(v)) {
1347 return throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1350 obj = iface_to_jsdisp((IUnknown*)get_object(v));
1351 IDispatch_Release(get_object(v));
1353 FIXME("non-jsdisp objects not supported\n");
1357 if(is_class(obj, JSCLASS_FUNCTION)) {
1358 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1360 hres = throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1362 jsdisp_release(obj);
1368 if(is_object_instance(prot)) {
1369 if(is_object_instance(v))
1370 tmp = iface_to_jsdisp((IUnknown*)get_object(v));
1371 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1372 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1378 jsdisp_release(tmp);
1380 FIXME("prototype is not an object\n");
1384 jsval_release(prot);
1389 return stack_push(ctx, jsval_bool(ret));
1392 /* ECMA-262 3rd Edition 11.8.7 */
1393 static HRESULT interp_in(exec_ctx_t *ctx)
1403 obj = stack_pop(ctx);
1404 if(!is_object_instance(obj) || !get_object(obj)) {
1406 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1410 hres = to_string(ctx->script, v, &str);
1413 IDispatch_Release(get_object(obj));
1417 hres = disp_get_id(ctx->script, get_object(obj), str->str, NULL, 0, &id);
1418 IDispatch_Release(get_object(obj));
1422 else if(hres == DISP_E_UNKNOWNNAME)
1427 return stack_push(ctx, jsval_bool(ret));
1430 /* ECMA-262 3rd Edition 11.6.1 */
1431 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1436 hres = to_primitive(ctx, lval, &l, NO_HINT);
1440 hres = to_primitive(ctx, rval, &r, NO_HINT);
1446 if(is_string(l) || is_string(r)) {
1447 jsstr_t *lstr, *rstr = NULL;
1449 hres = to_string(ctx, l, &lstr);
1451 hres = to_string(ctx, r, &rstr);
1453 if(SUCCEEDED(hres)) {
1456 ret_str = jsstr_concat(lstr, rstr);
1458 *ret = jsval_string(ret_str);
1460 hres = E_OUTOFMEMORY;
1463 jsstr_release(lstr);
1465 jsstr_release(rstr);
1469 hres = to_number(ctx, l, &nl);
1470 if(SUCCEEDED(hres)) {
1471 hres = to_number(ctx, r, &nr);
1473 *ret = jsval_number(nl+nr);
1482 /* ECMA-262 3rd Edition 11.6.1 */
1483 static HRESULT interp_add(exec_ctx_t *ctx)
1491 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1493 hres = add_eval(ctx->script, l, r, &ret);
1499 return stack_push(ctx, ret);
1502 /* ECMA-262 3rd Edition 11.6.2 */
1503 static HRESULT interp_sub(exec_ctx_t *ctx)
1510 hres = stack_pop_number(ctx, &r);
1514 hres = stack_pop_number(ctx, &l);
1518 return stack_push(ctx, jsval_number(l-r));
1521 /* ECMA-262 3rd Edition 11.5.1 */
1522 static HRESULT interp_mul(exec_ctx_t *ctx)
1529 hres = stack_pop_number(ctx, &r);
1533 hres = stack_pop_number(ctx, &l);
1537 return stack_push(ctx, jsval_number(l*r));
1540 /* ECMA-262 3rd Edition 11.5.2 */
1541 static HRESULT interp_div(exec_ctx_t *ctx)
1548 hres = stack_pop_number(ctx, &r);
1552 hres = stack_pop_number(ctx, &l);
1556 return stack_push(ctx, jsval_number(l/r));
1559 /* ECMA-262 3rd Edition 11.5.3 */
1560 static HRESULT interp_mod(exec_ctx_t *ctx)
1567 hres = stack_pop_number(ctx, &r);
1571 hres = stack_pop_number(ctx, &l);
1575 return stack_push(ctx, jsval_number(fmod(l, r)));
1578 /* ECMA-262 3rd Edition 11.4.2 */
1579 static HRESULT interp_delete(exec_ctx_t *ctx)
1581 jsval_t objv, namev;
1589 namev = stack_pop(ctx);
1590 objv = stack_pop(ctx);
1592 hres = to_object(ctx->script, objv, &obj);
1593 jsval_release(objv);
1595 jsval_release(namev);
1599 hres = to_string(ctx->script, namev, &name);
1600 jsval_release(namev);
1602 IDispatch_Release(obj);
1606 hres = disp_delete_name(ctx->script, obj, name, &ret);
1607 IDispatch_Release(obj);
1608 jsstr_release(name);
1612 return stack_push(ctx, jsval_bool(ret));
1615 /* ECMA-262 3rd Edition 11.4.2 */
1616 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1618 const BSTR arg = get_op_bstr(ctx, 0);
1623 TRACE("%s\n", debugstr_w(arg));
1625 hres = identifier_eval(ctx->script, arg, &exprval);
1629 switch(exprval.type) {
1631 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1632 IDispatch_Release(exprval.u.idref.disp);
1636 case EXPRVAL_INVALID:
1640 FIXME("Unsupported exprval\n");
1641 exprval_release(&exprval);
1646 return stack_push(ctx, jsval_bool(ret));
1649 /* ECMA-262 3rd Edition 11.4.2 */
1650 static HRESULT interp_void(exec_ctx_t *ctx)
1655 return stack_push(ctx, jsval_undefined());
1658 /* ECMA-262 3rd Edition 11.4.3 */
1659 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1661 switch(jsval_type(v)) {
1671 if(get_object(v) && (dispex = iface_to_jsdisp((IUnknown*)get_object(v)))) {
1672 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1673 jsdisp_release(dispex);
1689 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1696 /* ECMA-262 3rd Edition 11.4.3 */
1697 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1707 obj = stack_pop_objid(ctx, &id);
1709 return stack_push(ctx, jsval_string(jsstr_undefined()));
1711 hres = disp_propget(ctx->script, obj, id, &v);
1712 IDispatch_Release(obj);
1714 return stack_push_string(ctx, unknownW);
1716 hres = typeof_string(v, &ret);
1721 return stack_push_string(ctx, ret);
1724 /* ECMA-262 3rd Edition 11.4.3 */
1725 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1727 const BSTR arg = get_op_bstr(ctx, 0);
1733 TRACE("%s\n", debugstr_w(arg));
1735 hres = identifier_eval(ctx->script, arg, &exprval);
1739 if(exprval.type == EXPRVAL_INVALID) {
1740 hres = stack_push(ctx, jsval_string(jsstr_undefined()));
1741 exprval_release(&exprval);
1745 hres = exprval_to_value(ctx->script, &exprval, &v);
1746 exprval_release(&exprval);
1750 hres = typeof_string(v, &ret);
1755 return stack_push_string(ctx, ret);
1758 /* ECMA-262 3rd Edition 11.4.3 */
1759 static HRESULT interp_typeof(exec_ctx_t *ctx)
1768 hres = typeof_string(v, &ret);
1773 return stack_push_string(ctx, ret);
1776 /* ECMA-262 3rd Edition 11.4.7 */
1777 static HRESULT interp_minus(exec_ctx_t *ctx)
1784 hres = stack_pop_number(ctx, &n);
1788 return stack_push(ctx, jsval_number(-n));
1791 /* ECMA-262 3rd Edition 11.4.6 */
1792 static HRESULT interp_tonum(exec_ctx_t *ctx)
1801 hres = to_number(ctx->script, v, &n);
1806 return stack_push(ctx, jsval_number(n));
1809 /* ECMA-262 3rd Edition 11.3.1 */
1810 static HRESULT interp_postinc(exec_ctx_t *ctx)
1812 const int arg = get_op_int(ctx, 0);
1820 obj = stack_pop_objid(ctx, &id);
1822 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1824 hres = disp_propget(ctx->script, obj, id, &v);
1825 if(SUCCEEDED(hres)) {
1828 hres = to_number(ctx->script, v, &n);
1830 hres = disp_propput(ctx->script, obj, id, jsval_number(n+(double)arg));
1834 IDispatch_Release(obj);
1838 return stack_push(ctx, v);
1841 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1842 static HRESULT interp_preinc(exec_ctx_t *ctx)
1844 const int arg = get_op_int(ctx, 0);
1853 obj = stack_pop_objid(ctx, &id);
1855 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1857 hres = disp_propget(ctx->script, obj, id, &v);
1858 if(SUCCEEDED(hres)) {
1861 hres = to_number(ctx->script, v, &n);
1863 if(SUCCEEDED(hres)) {
1864 ret = n+(double)arg;
1865 hres = disp_propput(ctx->script, obj, id, jsval_number(ret));
1868 IDispatch_Release(obj);
1872 return stack_push(ctx, jsval_number(ret));
1875 /* ECMA-262 3rd Edition 11.9.3 */
1876 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
1878 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
1879 return equal2_values(lval, rval, ret);
1881 /* FIXME: NULL disps should be handled in more general way */
1882 if(is_object_instance(lval) && !get_object(lval))
1883 return equal_values(ctx, jsval_null(), rval, ret);
1884 if(is_object_instance(rval) && !get_object(rval))
1885 return equal_values(ctx, lval, jsval_null(), ret);
1887 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
1892 if(is_string(lval) && is_number(rval)) {
1896 hres = to_number(ctx, lval, &n);
1900 /* FIXME: optimize */
1901 return equal_values(ctx, jsval_number(n), rval, ret);
1904 if(is_string(rval) && is_number(lval)) {
1908 hres = to_number(ctx, rval, &n);
1912 /* FIXME: optimize */
1913 return equal_values(ctx, lval, jsval_number(n), ret);
1917 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
1920 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
1923 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
1927 hres = to_primitive(ctx, rval, &prim, NO_HINT);
1931 hres = equal_values(ctx, lval, prim, ret);
1932 jsval_release(prim);
1937 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
1941 hres = to_primitive(ctx, lval, &prim, NO_HINT);
1945 hres = equal_values(ctx, prim, rval, ret);
1946 jsval_release(prim);
1955 /* ECMA-262 3rd Edition 11.9.1 */
1956 static HRESULT interp_eq(exec_ctx_t *ctx)
1965 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
1967 hres = equal_values(ctx->script, l, r, &b);
1973 return stack_push(ctx, jsval_bool(b));
1976 /* ECMA-262 3rd Edition 11.9.2 */
1977 static HRESULT interp_neq(exec_ctx_t *ctx)
1986 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
1988 hres = equal_values(ctx->script, l, r, &b);
1994 return stack_push(ctx, jsval_bool(!b));
1997 /* ECMA-262 3rd Edition 11.9.4 */
1998 static HRESULT interp_eq2(exec_ctx_t *ctx)
2009 hres = equal2_values(r, l, &b);
2015 return stack_push(ctx, jsval_bool(b));
2018 /* ECMA-262 3rd Edition 11.9.5 */
2019 static HRESULT interp_neq2(exec_ctx_t *ctx)
2030 hres = equal2_values(r, l, &b);
2036 return stack_push(ctx, jsval_bool(!b));
2039 /* ECMA-262 3rd Edition 11.8.5 */
2040 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2046 hres = to_primitive(ctx, lval, &l, NO_HINT);
2050 hres = to_primitive(ctx, rval, &r, NO_HINT);
2056 if(is_string(l) && is_string(r)) {
2057 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2058 jsstr_release(get_string(l));
2059 jsstr_release(get_string(r));
2063 hres = to_number(ctx, l, &ln);
2066 hres = to_number(ctx, r, &rn);
2071 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2075 /* ECMA-262 3rd Edition 11.8.1 */
2076 static HRESULT interp_lt(exec_ctx_t *ctx)
2085 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2087 hres = less_eval(ctx->script, l, r, FALSE, &b);
2093 return stack_push(ctx, jsval_bool(b));
2096 /* ECMA-262 3rd Edition 11.8.1 */
2097 static HRESULT interp_lteq(exec_ctx_t *ctx)
2106 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2108 hres = less_eval(ctx->script, r, l, TRUE, &b);
2114 return stack_push(ctx, jsval_bool(b));
2117 /* ECMA-262 3rd Edition 11.8.2 */
2118 static HRESULT interp_gt(exec_ctx_t *ctx)
2127 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2129 hres = less_eval(ctx->script, r, l, FALSE, &b);
2135 return stack_push(ctx, jsval_bool(b));
2138 /* ECMA-262 3rd Edition 11.8.4 */
2139 static HRESULT interp_gteq(exec_ctx_t *ctx)
2148 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2150 hres = less_eval(ctx->script, l, r, TRUE, &b);
2156 return stack_push(ctx, jsval_bool(b));
2159 /* ECMA-262 3rd Edition 11.4.8 */
2160 static HRESULT interp_bneg(exec_ctx_t *ctx)
2169 hres = to_int32(ctx->script, v, &i);
2174 return stack_push(ctx, jsval_number(~i));
2177 /* ECMA-262 3rd Edition 11.4.9 */
2178 static HRESULT interp_neg(exec_ctx_t *ctx)
2187 hres = to_boolean(v, &b);
2192 return stack_push(ctx, jsval_bool(!b));
2195 /* ECMA-262 3rd Edition 11.7.1 */
2196 static HRESULT interp_lshift(exec_ctx_t *ctx)
2202 hres = stack_pop_uint(ctx, &r);
2206 hres = stack_pop_int(ctx, &l);
2210 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2213 /* ECMA-262 3rd Edition 11.7.2 */
2214 static HRESULT interp_rshift(exec_ctx_t *ctx)
2220 hres = stack_pop_uint(ctx, &r);
2224 hres = stack_pop_int(ctx, &l);
2228 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2231 /* ECMA-262 3rd Edition 11.7.3 */
2232 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2237 hres = stack_pop_uint(ctx, &r);
2241 hres = stack_pop_uint(ctx, &l);
2245 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2248 /* ECMA-262 3rd Edition 11.13.1 */
2249 static HRESULT interp_assign(exec_ctx_t *ctx)
2260 disp = stack_pop_objid(ctx, &id);
2263 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2266 hres = disp_propput(ctx->script, disp, id, v);
2267 IDispatch_Release(disp);
2273 return stack_push(ctx, v);
2276 /* JScript extension */
2277 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2279 const unsigned argc = get_op_uint(ctx, 0);
2285 TRACE("%u\n", argc);
2287 disp = stack_topn_objid(ctx, argc+1, &id);
2289 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2291 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2296 stack_popn(ctx, argc+2);
2297 return stack_push(ctx, v);
2300 static HRESULT interp_undefined(exec_ctx_t *ctx)
2304 return stack_push(ctx, jsval_undefined());
2307 static HRESULT interp_jmp(exec_ctx_t *ctx)
2309 const unsigned arg = get_op_uint(ctx, 0);
2317 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2319 const unsigned arg = get_op_uint(ctx, 0);
2327 hres = to_boolean(v, &b);
2339 static HRESULT interp_pop(exec_ctx_t *ctx)
2341 const unsigned arg = get_op_uint(ctx, 0);
2345 stack_popn(ctx, arg);
2349 static HRESULT interp_ret(exec_ctx_t *ctx)
2357 static HRESULT interp_setret(exec_ctx_t *ctx)
2361 jsval_release(ctx->ret);
2362 ctx->ret = stack_pop(ctx);
2366 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2368 static const op_func_t op_funcs[] = {
2369 #define X(x,a,b,c) interp_##x,
2374 static const unsigned op_move[] = {
2375 #define X(a,x,b,c) x,
2380 static HRESULT unwind_exception(exec_ctx_t *ctx)
2382 except_frame_t *except_frame;
2387 except_frame = ctx->except_frame;
2388 ctx->except_frame = except_frame->next;
2390 assert(except_frame->stack_top <= ctx->top);
2391 stack_popn(ctx, ctx->top - except_frame->stack_top);
2393 while(except_frame->scope != ctx->scope_chain)
2394 scope_pop(&ctx->scope_chain);
2396 ctx->ip = except_frame->catch_off;
2398 except_val = ctx->script->ei.val;
2399 ctx->script->ei.val = jsval_undefined();
2400 clear_ei(ctx->script);
2402 ident = except_frame->ident;
2403 heap_free(except_frame);
2406 jsdisp_t *scope_obj;
2408 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2409 if(SUCCEEDED(hres)) {
2410 hres = jsdisp_propput_name(scope_obj, ident, except_val);
2412 jsdisp_release(scope_obj);
2414 jsval_release(except_val);
2418 hres = scope_push(ctx->scope_chain, scope_obj, to_disp(scope_obj), &ctx->scope_chain);
2419 jsdisp_release(scope_obj);
2421 hres = stack_push(ctx, except_val);
2425 hres = stack_push(ctx, jsval_bool(FALSE));
2431 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsval_t *ret)
2433 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2434 except_frame_t *prev_except_frame;
2435 function_code_t *prev_func;
2436 unsigned prev_ip, prev_top;
2437 scope_chain_t *prev_scope;
2438 bytecode_t *prev_code;
2440 HRESULT hres = S_OK;
2444 prev_top = exec_ctx->top;
2445 prev_scope = exec_ctx->scope_chain;
2446 prev_except_frame = exec_ctx->except_frame;
2447 prev_ip = exec_ctx->ip;
2448 prev_code = exec_ctx->code;
2449 prev_func = exec_ctx->func_code;
2450 exec_ctx->ip = func->instr_off;
2451 exec_ctx->except_frame = NULL;
2452 exec_ctx->code = code;
2453 exec_ctx->func_code = func;
2455 while(exec_ctx->ip != -1) {
2456 op = code->instrs[exec_ctx->ip].op;
2457 hres = op_funcs[op](exec_ctx);
2459 TRACE("EXCEPTION %08x\n", hres);
2461 if(!exec_ctx->except_frame)
2464 hres = unwind_exception(exec_ctx);
2468 exec_ctx->ip += op_move[op];
2472 exec_ctx->ip = prev_ip;
2473 exec_ctx->except_frame = prev_except_frame;
2474 exec_ctx->code = prev_code;
2475 exec_ctx->func_code = prev_func;
2478 while(exec_ctx->scope_chain != prev_scope)
2479 scope_pop(&exec_ctx->scope_chain);
2480 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2484 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2485 assert(exec_ctx->scope_chain == prev_scope);
2486 assert(exec_ctx->top == prev_top);
2488 *ret = exec_ctx->ret;
2489 exec_ctx->ret = jsval_undefined();
2493 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval, jsval_t *ret)
2495 exec_ctx_t *prev_ctx;
2498 HRESULT hres = S_OK;
2500 for(i = 0; i < func->func_cnt; i++) {
2503 if(!func->funcs[i].name)
2506 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2510 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, jsval_obj(func_obj));
2511 jsdisp_release(func_obj);
2516 for(i=0; i < func->var_cnt; i++) {
2517 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2520 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2526 prev_ctx = ctx->script->exec_ctx;
2527 ctx->script->exec_ctx = ctx;
2529 hres = enter_bytecode(ctx->script, code, func, &val);
2530 assert(ctx->script->exec_ctx == ctx);
2531 ctx->script->exec_ctx = prev_ctx;