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;
295 /* ECMA-262 3rd Edition 11.2.3.7 */
299 jsthis = iface_to_jsdisp((IUnknown*)this_obj);
301 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
303 jsdisp_release(jsthis);
308 ctx->this_obj = this_obj;
309 else if(script_ctx->host_global)
310 ctx->this_obj = script_ctx->host_global;
312 ctx->this_obj = to_disp(script_ctx->global);
313 IDispatch_AddRef(ctx->this_obj);
315 jsdisp_addref(var_disp);
316 ctx->var_disp = var_disp;
318 script_addref(script_ctx);
319 ctx->script = script_ctx;
323 ctx->scope_chain = scope;
330 void exec_release(exec_ctx_t *ctx)
336 scope_release(ctx->scope_chain);
338 jsdisp_release(ctx->var_disp);
340 IDispatch_Release(ctx->this_obj);
342 script_release(ctx->script);
343 heap_free(ctx->stack);
347 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
353 jsdisp = iface_to_jsdisp((IUnknown*)disp);
355 hres = jsdisp_get_id(jsdisp, name, flags, id);
356 jsdisp_release(jsdisp);
361 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
362 if(SUCCEEDED(hres)) {
363 BSTR str = name_bstr;
366 str = SysAllocString(name);
368 hres = IDispatchEx_GetDispID(dispex, str, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
370 hres = E_OUTOFMEMORY;
371 IDispatchEx_Release(dispex);
375 TRACE("using IDispatch\n");
377 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
380 static inline BOOL var_is_null(const VARIANT *v)
382 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
385 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
387 IObjectIdentity *identity;
388 IUnknown *unk1, *unk2;
396 if(!disp1 || !disp2) {
401 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
405 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
407 IUnknown_Release(unk1);
414 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
415 if(SUCCEEDED(hres)) {
416 hres = IObjectIdentity_IsEqualObject(identity, unk2);
417 IObjectIdentity_Release(identity);
424 IUnknown_Release(unk1);
425 IUnknown_Release(unk2);
429 /* ECMA-262 3rd Edition 11.9.6 */
430 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
432 jsval_type_t type = jsval_type(lval);
436 if(type != jsval_type(rval)) {
437 if(is_null_instance(lval))
438 *ret = is_null_instance(rval);
450 return disp_cmp(get_object(lval), get_object(rval), ret);
452 *ret = jsstr_eq(get_string(lval), get_string(rval));
455 *ret = get_number(lval) == get_number(rval);
458 *ret = !get_bool(lval) == !get_bool(rval);
461 FIXME("VARIANT not implemented\n");
468 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
474 for(item = ctx->named_items; item; item = item->next) {
475 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
476 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
477 if(SUCCEEDED(hres)) {
479 exprval_set_idref(ret, item->disp, id);
488 /* ECMA-262 3rd Edition 10.1.4 */
489 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
491 scope_chain_t *scope;
496 TRACE("%s\n", debugstr_w(identifier));
498 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
500 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
502 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
503 if(SUCCEEDED(hres)) {
504 exprval_set_idref(ret, scope->obj, id);
509 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
510 if(SUCCEEDED(hres)) {
511 exprval_set_idref(ret, to_disp(ctx->global), id);
515 for(item = ctx->named_items; item; item = item->next) {
516 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
523 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
524 SCRIPTINFO_IUNKNOWN, &unk, NULL);
526 WARN("GetItemInfo failed: %08x\n", hres);
530 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
531 IUnknown_Release(unk);
533 WARN("object does not implement IDispatch\n");
538 IDispatch_AddRef(item->disp);
539 ret->type = EXPRVAL_JSVAL;
540 ret->u.val = jsval_disp(item->disp);
545 if(lookup_global_members(ctx, identifier, ret))
548 ret->type = EXPRVAL_INVALID;
552 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
553 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
556 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
557 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
560 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
561 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
564 static inline jsstr_t *get_op_str(exec_ctx_t *ctx, int i){
565 return ctx->code->instrs[ctx->ip].u.arg[i].str;
568 static inline double get_op_double(exec_ctx_t *ctx){
569 return ctx->code->instrs[ctx->ip].u.dbl;
572 /* ECMA-262 3rd Edition 12.2 */
573 static HRESULT interp_var_set(exec_ctx_t *ctx)
575 const BSTR name = get_op_bstr(ctx, 0);
579 TRACE("%s\n", debugstr_w(name));
581 val = stack_pop(ctx);
582 hres = jsdisp_propput_name(ctx->var_disp, name, val);
587 /* ECMA-262 3rd Edition 12.6.4 */
588 static HRESULT interp_forin(exec_ctx_t *ctx)
590 const HRESULT arg = get_op_uint(ctx, 0);
591 IDispatch *var_obj, *obj = NULL;
600 val = stack_pop(ctx);
602 assert(is_number(stack_top(ctx)));
603 id = get_number(stack_top(ctx));
605 var_obj = stack_topn_objid(ctx, 1, &var_id);
607 FIXME("invalid ref\n");
612 if(is_object_instance(stack_topn(ctx, 3)))
613 obj = get_object(stack_topn(ctx, 3));
616 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
617 if(SUCCEEDED(hres)) {
618 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
620 hres = IDispatchEx_GetMemberName(dispex, id, &name);
621 IDispatchEx_Release(dispex);
627 TRACE("No IDispatchEx\n");
634 str = jsstr_alloc_len(name, SysStringLen(name));
637 return E_OUTOFMEMORY;
641 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
643 hres = disp_propput(ctx->script, var_obj, var_id, jsval_string(str));
652 return stack_push(ctx, val);
657 /* ECMA-262 3rd Edition 12.10 */
658 static HRESULT interp_push_scope(exec_ctx_t *ctx)
667 hres = to_object(ctx->script, v, &disp);
672 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
673 IDispatch_Release(disp);
677 /* ECMA-262 3rd Edition 12.10 */
678 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
682 scope_pop(&ctx->scope_chain);
686 /* ECMA-262 3rd Edition 12.13 */
687 static HRESULT interp_case(exec_ctx_t *ctx)
689 const unsigned arg = get_op_uint(ctx, 0);
697 hres = equal2_values(stack_top(ctx), v, &b);
711 /* ECMA-262 3rd Edition 12.13 */
712 static HRESULT interp_throw(exec_ctx_t *ctx)
716 jsval_release(ctx->script->ei.val);
717 ctx->script->ei.val = stack_pop(ctx);
718 return DISP_E_EXCEPTION;
721 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
723 const HRESULT arg = get_op_uint(ctx, 0);
725 TRACE("%08x\n", arg);
727 return throw_reference_error(ctx->script, arg, NULL);
730 static HRESULT interp_throw_type(exec_ctx_t *ctx)
732 const HRESULT hres = get_op_uint(ctx, 0);
733 jsstr_t *str = get_op_str(ctx, 1);
735 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
737 return throw_type_error(ctx->script, hres, str->str);
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)
800 assert(is_bool(stack_top(ctx)));
801 if(!get_bool(stack_top(ctx))) {
802 TRACE("passing exception\n");
807 ctx->script->ei.val = stack_pop(ctx);
808 return DISP_E_EXCEPTION;
812 return stack_push(ctx, v);
815 /* ECMA-262 3rd Edition 13 */
816 static HRESULT interp_func(exec_ctx_t *ctx)
818 unsigned func_idx = get_op_uint(ctx, 0);
822 TRACE("%d\n", func_idx);
824 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
825 ctx->scope_chain, &dispex);
829 return stack_push(ctx, jsval_obj(dispex));
832 /* ECMA-262 3rd Edition 11.2.1 */
833 static HRESULT interp_array(exec_ctx_t *ctx)
843 namev = stack_pop(ctx);
845 hres = stack_pop_object(ctx, &obj);
847 jsval_release(namev);
851 hres = to_string(ctx->script, namev, &name);
852 jsval_release(namev);
854 IDispatch_Release(obj);
858 hres = disp_get_id(ctx->script, obj, name->str, NULL, 0, &id);
860 if(SUCCEEDED(hres)) {
861 hres = disp_propget(ctx->script, obj, id, &v);
862 }else if(hres == DISP_E_UNKNOWNNAME) {
863 v = jsval_undefined();
866 IDispatch_Release(obj);
870 return stack_push(ctx, v);
873 /* ECMA-262 3rd Edition 11.2.1 */
874 static HRESULT interp_member(exec_ctx_t *ctx)
876 const BSTR arg = get_op_bstr(ctx, 0);
884 hres = stack_pop_object(ctx, &obj);
888 hres = disp_get_id(ctx->script, obj, arg, arg, 0, &id);
889 if(SUCCEEDED(hres)) {
890 hres = disp_propget(ctx->script, obj, id, &v);
891 }else if(hres == DISP_E_UNKNOWNNAME) {
892 v = jsval_undefined();
895 IDispatch_Release(obj);
899 return stack_push(ctx, v);
902 /* ECMA-262 3rd Edition 11.2.1 */
903 static HRESULT interp_memberid(exec_ctx_t *ctx)
905 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_string(ctx->script, namev, &name);
922 IDispatch_Release(obj);
924 jsval_release(namev);
928 hres = disp_get_id(ctx->script, obj, name->str, NULL, arg, &id);
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)
1407 obj = stack_pop(ctx);
1408 if(!is_object_instance(obj) || !get_object(obj)) {
1410 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1414 hres = to_string(ctx->script, v, &str);
1417 IDispatch_Release(get_object(obj));
1421 hres = disp_get_id(ctx->script, get_object(obj), str->str, NULL, 0, &id);
1422 IDispatch_Release(get_object(obj));
1426 else if(hres == DISP_E_UNKNOWNNAME)
1431 return stack_push(ctx, jsval_bool(ret));
1434 /* ECMA-262 3rd Edition 11.6.1 */
1435 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1440 hres = to_primitive(ctx, lval, &l, NO_HINT);
1444 hres = to_primitive(ctx, rval, &r, NO_HINT);
1450 if(is_string(l) || is_string(r)) {
1451 jsstr_t *lstr, *rstr = NULL;
1453 hres = to_string(ctx, l, &lstr);
1455 hres = to_string(ctx, r, &rstr);
1457 if(SUCCEEDED(hres)) {
1458 unsigned len1, len2;
1461 len1 = jsstr_length(lstr);
1462 len2 = jsstr_length(rstr);
1464 ret_str = jsstr_alloc_buf(len1+len2);
1467 memcpy(ret_str->str, lstr->str, len1*sizeof(WCHAR));
1469 memcpy(ret_str->str+len1, rstr->str, len2*sizeof(WCHAR));
1470 *ret = jsval_string(ret_str);
1472 hres = E_OUTOFMEMORY;
1476 jsstr_release(lstr);
1478 jsstr_release(rstr);
1482 hres = to_number(ctx, l, &nl);
1483 if(SUCCEEDED(hres)) {
1484 hres = to_number(ctx, r, &nr);
1486 *ret = jsval_number(nl+nr);
1495 /* ECMA-262 3rd Edition 11.6.1 */
1496 static HRESULT interp_add(exec_ctx_t *ctx)
1504 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1506 hres = add_eval(ctx->script, l, r, &ret);
1512 return stack_push(ctx, ret);
1515 /* ECMA-262 3rd Edition 11.6.2 */
1516 static HRESULT interp_sub(exec_ctx_t *ctx)
1523 hres = stack_pop_number(ctx, &r);
1527 hres = stack_pop_number(ctx, &l);
1531 return stack_push(ctx, jsval_number(l-r));
1534 /* ECMA-262 3rd Edition 11.5.1 */
1535 static HRESULT interp_mul(exec_ctx_t *ctx)
1542 hres = stack_pop_number(ctx, &r);
1546 hres = stack_pop_number(ctx, &l);
1550 return stack_push(ctx, jsval_number(l*r));
1553 /* ECMA-262 3rd Edition 11.5.2 */
1554 static HRESULT interp_div(exec_ctx_t *ctx)
1561 hres = stack_pop_number(ctx, &r);
1565 hres = stack_pop_number(ctx, &l);
1569 return stack_push(ctx, jsval_number(l/r));
1572 /* ECMA-262 3rd Edition 11.5.3 */
1573 static HRESULT interp_mod(exec_ctx_t *ctx)
1580 hres = stack_pop_number(ctx, &r);
1584 hres = stack_pop_number(ctx, &l);
1588 return stack_push(ctx, jsval_number(fmod(l, r)));
1591 /* ECMA-262 3rd Edition 11.4.2 */
1592 static HRESULT interp_delete(exec_ctx_t *ctx)
1594 jsval_t objv, namev;
1602 namev = stack_pop(ctx);
1603 objv = stack_pop(ctx);
1605 hres = to_object(ctx->script, objv, &obj);
1606 jsval_release(objv);
1608 jsval_release(namev);
1612 hres = to_string(ctx->script, namev, &name);
1613 jsval_release(namev);
1615 IDispatch_Release(obj);
1619 hres = disp_delete_name(ctx->script, obj, name, &ret);
1620 IDispatch_Release(obj);
1621 jsstr_release(name);
1625 return stack_push(ctx, jsval_bool(ret));
1628 /* ECMA-262 3rd Edition 11.4.2 */
1629 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1631 const BSTR arg = get_op_bstr(ctx, 0);
1636 TRACE("%s\n", debugstr_w(arg));
1638 hres = identifier_eval(ctx->script, arg, &exprval);
1642 if(exprval.type != EXPRVAL_IDREF) {
1643 FIXME("Unsupported exprval\n");
1644 exprval_release(&exprval);
1648 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1649 IDispatch_Release(exprval.u.idref.disp);
1653 return stack_push(ctx, jsval_bool(ret));
1656 /* ECMA-262 3rd Edition 11.4.2 */
1657 static HRESULT interp_void(exec_ctx_t *ctx)
1662 return stack_push(ctx, jsval_undefined());
1665 /* ECMA-262 3rd Edition 11.4.3 */
1666 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1668 switch(jsval_type(v)) {
1678 if(get_object(v) && (dispex = iface_to_jsdisp((IUnknown*)get_object(v)))) {
1679 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1680 jsdisp_release(dispex);
1696 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1703 /* ECMA-262 3rd Edition 11.4.3 */
1704 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1712 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1716 obj = stack_pop_objid(ctx, &id);
1718 return stack_push_string(ctx, undefinedW);
1720 hres = disp_propget(ctx->script, obj, id, &v);
1721 IDispatch_Release(obj);
1723 return stack_push_string(ctx, unknownW);
1725 hres = typeof_string(v, &ret);
1730 return stack_push_string(ctx, ret);
1733 /* ECMA-262 3rd Edition 11.4.3 */
1734 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1736 const BSTR arg = get_op_bstr(ctx, 0);
1742 TRACE("%s\n", debugstr_w(arg));
1744 hres = identifier_eval(ctx->script, arg, &exprval);
1748 if(exprval.type == EXPRVAL_INVALID) {
1749 hres = stack_push_string(ctx, undefinedW);
1750 exprval_release(&exprval);
1754 hres = exprval_to_value(ctx->script, &exprval, &v);
1755 exprval_release(&exprval);
1759 hres = typeof_string(v, &ret);
1764 return stack_push_string(ctx, ret);
1767 /* ECMA-262 3rd Edition 11.4.3 */
1768 static HRESULT interp_typeof(exec_ctx_t *ctx)
1777 hres = typeof_string(v, &ret);
1782 return stack_push_string(ctx, ret);
1785 /* ECMA-262 3rd Edition 11.4.7 */
1786 static HRESULT interp_minus(exec_ctx_t *ctx)
1793 hres = stack_pop_number(ctx, &n);
1797 return stack_push(ctx, jsval_number(-n));
1800 /* ECMA-262 3rd Edition 11.4.6 */
1801 static HRESULT interp_tonum(exec_ctx_t *ctx)
1810 hres = to_number(ctx->script, v, &n);
1815 return stack_push(ctx, jsval_number(n));
1818 /* ECMA-262 3rd Edition 11.3.1 */
1819 static HRESULT interp_postinc(exec_ctx_t *ctx)
1821 const int arg = get_op_int(ctx, 0);
1829 obj = stack_pop_objid(ctx, &id);
1831 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1833 hres = disp_propget(ctx->script, obj, id, &v);
1834 if(SUCCEEDED(hres)) {
1837 hres = to_number(ctx->script, v, &n);
1839 hres = disp_propput(ctx->script, obj, id, jsval_number(n+(double)arg));
1843 IDispatch_Release(obj);
1847 return stack_push(ctx, v);
1850 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1851 static HRESULT interp_preinc(exec_ctx_t *ctx)
1853 const int arg = get_op_int(ctx, 0);
1862 obj = stack_pop_objid(ctx, &id);
1864 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1866 hres = disp_propget(ctx->script, obj, id, &v);
1867 if(SUCCEEDED(hres)) {
1870 hres = to_number(ctx->script, v, &n);
1872 if(SUCCEEDED(hres)) {
1873 ret = n+(double)arg;
1874 hres = disp_propput(ctx->script, obj, id, jsval_number(ret));
1877 IDispatch_Release(obj);
1881 return stack_push(ctx, jsval_number(ret));
1884 /* ECMA-262 3rd Edition 11.9.3 */
1885 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
1887 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
1888 return equal2_values(lval, rval, ret);
1890 /* FIXME: NULL disps should be handled in more general way */
1891 if(is_object_instance(lval) && !get_object(lval))
1892 return equal_values(ctx, jsval_null(), rval, ret);
1893 if(is_object_instance(rval) && !get_object(rval))
1894 return equal_values(ctx, lval, jsval_null(), ret);
1896 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
1901 if(is_string(lval) && is_number(rval)) {
1905 hres = to_number(ctx, lval, &n);
1909 /* FIXME: optimize */
1910 return equal_values(ctx, jsval_number(n), rval, ret);
1913 if(is_string(rval) && is_number(lval)) {
1917 hres = to_number(ctx, rval, &n);
1921 /* FIXME: optimize */
1922 return equal_values(ctx, lval, jsval_number(n), ret);
1926 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
1929 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
1932 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
1936 hres = to_primitive(ctx, rval, &prim, NO_HINT);
1940 hres = equal_values(ctx, lval, prim, ret);
1941 jsval_release(prim);
1946 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
1950 hres = to_primitive(ctx, lval, &prim, NO_HINT);
1954 hres = equal_values(ctx, prim, rval, ret);
1955 jsval_release(prim);
1964 /* ECMA-262 3rd Edition 11.9.1 */
1965 static HRESULT interp_eq(exec_ctx_t *ctx)
1974 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
1976 hres = equal_values(ctx->script, l, r, &b);
1982 return stack_push(ctx, jsval_bool(b));
1985 /* ECMA-262 3rd Edition 11.9.2 */
1986 static HRESULT interp_neq(exec_ctx_t *ctx)
1995 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
1997 hres = equal_values(ctx->script, l, r, &b);
2003 return stack_push(ctx, jsval_bool(!b));
2006 /* ECMA-262 3rd Edition 11.9.4 */
2007 static HRESULT interp_eq2(exec_ctx_t *ctx)
2018 hres = equal2_values(r, l, &b);
2024 return stack_push(ctx, jsval_bool(b));
2027 /* ECMA-262 3rd Edition 11.9.5 */
2028 static HRESULT interp_neq2(exec_ctx_t *ctx)
2039 hres = equal2_values(r, l, &b);
2045 return stack_push(ctx, jsval_bool(!b));
2048 /* ECMA-262 3rd Edition 11.8.5 */
2049 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2055 hres = to_primitive(ctx, lval, &l, NO_HINT);
2059 hres = to_primitive(ctx, rval, &r, NO_HINT);
2065 if(is_string(l) && is_string(r)) {
2066 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2067 jsstr_release(get_string(l));
2068 jsstr_release(get_string(r));
2072 hres = to_number(ctx, l, &ln);
2075 hres = to_number(ctx, r, &rn);
2080 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2084 /* ECMA-262 3rd Edition 11.8.1 */
2085 static HRESULT interp_lt(exec_ctx_t *ctx)
2094 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2096 hres = less_eval(ctx->script, l, r, FALSE, &b);
2102 return stack_push(ctx, jsval_bool(b));
2105 /* ECMA-262 3rd Edition 11.8.1 */
2106 static HRESULT interp_lteq(exec_ctx_t *ctx)
2115 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2117 hres = less_eval(ctx->script, r, l, TRUE, &b);
2123 return stack_push(ctx, jsval_bool(b));
2126 /* ECMA-262 3rd Edition 11.8.2 */
2127 static HRESULT interp_gt(exec_ctx_t *ctx)
2136 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2138 hres = less_eval(ctx->script, r, l, FALSE, &b);
2144 return stack_push(ctx, jsval_bool(b));
2147 /* ECMA-262 3rd Edition 11.8.4 */
2148 static HRESULT interp_gteq(exec_ctx_t *ctx)
2157 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2159 hres = less_eval(ctx->script, l, r, TRUE, &b);
2165 return stack_push(ctx, jsval_bool(b));
2168 /* ECMA-262 3rd Edition 11.4.8 */
2169 static HRESULT interp_bneg(exec_ctx_t *ctx)
2178 hres = to_int32(ctx->script, v, &i);
2183 return stack_push(ctx, jsval_number(~i));
2186 /* ECMA-262 3rd Edition 11.4.9 */
2187 static HRESULT interp_neg(exec_ctx_t *ctx)
2196 hres = to_boolean(v, &b);
2201 return stack_push(ctx, jsval_bool(!b));
2204 /* ECMA-262 3rd Edition 11.7.1 */
2205 static HRESULT interp_lshift(exec_ctx_t *ctx)
2211 hres = stack_pop_uint(ctx, &r);
2215 hres = stack_pop_int(ctx, &l);
2219 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2222 /* ECMA-262 3rd Edition 11.7.2 */
2223 static HRESULT interp_rshift(exec_ctx_t *ctx)
2229 hres = stack_pop_uint(ctx, &r);
2233 hres = stack_pop_int(ctx, &l);
2237 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2240 /* ECMA-262 3rd Edition 11.7.3 */
2241 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2246 hres = stack_pop_uint(ctx, &r);
2250 hres = stack_pop_uint(ctx, &l);
2254 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2257 /* ECMA-262 3rd Edition 11.13.1 */
2258 static HRESULT interp_assign(exec_ctx_t *ctx)
2269 disp = stack_pop_objid(ctx, &id);
2272 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2275 hres = disp_propput(ctx->script, disp, id, v);
2276 IDispatch_Release(disp);
2282 return stack_push(ctx, v);
2285 /* JScript extension */
2286 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2288 const unsigned argc = get_op_uint(ctx, 0);
2294 TRACE("%u\n", argc);
2296 disp = stack_topn_objid(ctx, argc+1, &id);
2298 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2300 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2305 stack_popn(ctx, argc+2);
2306 return stack_push(ctx, v);
2309 static HRESULT interp_undefined(exec_ctx_t *ctx)
2313 return stack_push(ctx, jsval_undefined());
2316 static HRESULT interp_jmp(exec_ctx_t *ctx)
2318 const unsigned arg = get_op_uint(ctx, 0);
2326 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2328 const unsigned arg = get_op_uint(ctx, 0);
2336 hres = to_boolean(v, &b);
2348 static HRESULT interp_pop(exec_ctx_t *ctx)
2356 static HRESULT interp_ret(exec_ctx_t *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));
2427 hres = stack_push(ctx, jsval_undefined());
2433 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsval_t *ret)
2435 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2436 except_frame_t *prev_except_frame;
2437 function_code_t *prev_func;
2438 unsigned prev_ip, prev_top;
2439 scope_chain_t *prev_scope;
2440 bytecode_t *prev_code;
2442 HRESULT hres = S_OK;
2446 prev_top = exec_ctx->top;
2447 prev_scope = exec_ctx->scope_chain;
2448 prev_except_frame = exec_ctx->except_frame;
2449 prev_ip = exec_ctx->ip;
2450 prev_code = exec_ctx->code;
2451 prev_func = exec_ctx->func_code;
2452 exec_ctx->ip = func->instr_off;
2453 exec_ctx->except_frame = NULL;
2454 exec_ctx->code = code;
2455 exec_ctx->func_code = func;
2457 while(exec_ctx->ip != -1) {
2458 op = code->instrs[exec_ctx->ip].op;
2459 hres = op_funcs[op](exec_ctx);
2461 TRACE("EXCEPTION %08x\n", hres);
2463 if(!exec_ctx->except_frame)
2466 hres = unwind_exception(exec_ctx);
2470 exec_ctx->ip += op_move[op];
2474 exec_ctx->ip = prev_ip;
2475 exec_ctx->except_frame = prev_except_frame;
2476 exec_ctx->code = prev_code;
2477 exec_ctx->func_code = prev_func;
2480 while(exec_ctx->scope_chain != prev_scope)
2481 scope_pop(&exec_ctx->scope_chain);
2482 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2486 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2487 assert(exec_ctx->scope_chain == prev_scope);
2489 if(exec_ctx->top == prev_top)
2490 *ret = jsval_undefined();
2492 *ret = stack_pop(exec_ctx);
2496 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval, jsval_t *ret)
2498 exec_ctx_t *prev_ctx;
2501 HRESULT hres = S_OK;
2503 for(i = 0; i < func->func_cnt; i++) {
2506 if(!func->funcs[i].name)
2509 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2513 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, jsval_obj(func_obj));
2514 jsdisp_release(func_obj);
2519 for(i=0; i < func->var_cnt; i++) {
2520 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2523 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2529 prev_ctx = ctx->script->exec_ctx;
2530 ctx->script->exec_ctx = ctx;
2532 hres = enter_bytecode(ctx->script, code, func, &val);
2533 assert(ctx->script->exec_ctx == ctx);
2534 ctx->script->exec_ctx = prev_ctx;