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;
296 ctx->this_obj = this_obj;
297 else if(script_ctx->host_global)
298 ctx->this_obj = script_ctx->host_global;
300 ctx->this_obj = to_disp(script_ctx->global);
301 IDispatch_AddRef(ctx->this_obj);
303 jsdisp_addref(var_disp);
304 ctx->var_disp = var_disp;
306 script_addref(script_ctx);
307 ctx->script = script_ctx;
311 ctx->scope_chain = scope;
318 void exec_release(exec_ctx_t *ctx)
324 scope_release(ctx->scope_chain);
326 jsdisp_release(ctx->var_disp);
328 IDispatch_Release(ctx->this_obj);
330 script_release(ctx->script);
331 heap_free(ctx->stack);
335 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
341 jsdisp = iface_to_jsdisp((IUnknown*)disp);
343 hres = jsdisp_get_id(jsdisp, name, flags, id);
344 jsdisp_release(jsdisp);
349 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
350 if(SUCCEEDED(hres)) {
351 BSTR str = name_bstr;
354 str = SysAllocString(name);
356 hres = IDispatchEx_GetDispID(dispex, str, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
358 hres = E_OUTOFMEMORY;
359 IDispatchEx_Release(dispex);
363 TRACE("using IDispatch\n");
365 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
368 static inline BOOL var_is_null(const VARIANT *v)
370 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
373 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
375 IObjectIdentity *identity;
376 IUnknown *unk1, *unk2;
384 if(!disp1 || !disp2) {
389 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
393 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
395 IUnknown_Release(unk1);
402 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
403 if(SUCCEEDED(hres)) {
404 hres = IObjectIdentity_IsEqualObject(identity, unk2);
405 IObjectIdentity_Release(identity);
412 IUnknown_Release(unk1);
413 IUnknown_Release(unk2);
417 /* ECMA-262 3rd Edition 11.9.6 */
418 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
420 jsval_type_t type = jsval_type(lval);
424 if(type != jsval_type(rval)) {
425 if(is_null_instance(lval))
426 *ret = is_null_instance(rval);
438 return disp_cmp(get_object(lval), get_object(rval), ret);
440 *ret = jsstr_eq(get_string(lval), get_string(rval));
443 *ret = get_number(lval) == get_number(rval);
446 *ret = !get_bool(lval) == !get_bool(rval);
449 FIXME("VARIANT not implemented\n");
456 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
462 for(item = ctx->named_items; item; item = item->next) {
463 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
464 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
465 if(SUCCEEDED(hres)) {
467 exprval_set_idref(ret, item->disp, id);
476 /* ECMA-262 3rd Edition 10.1.4 */
477 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
479 scope_chain_t *scope;
484 TRACE("%s\n", debugstr_w(identifier));
486 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
488 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
490 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
491 if(SUCCEEDED(hres)) {
492 exprval_set_idref(ret, scope->obj, id);
497 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
498 if(SUCCEEDED(hres)) {
499 exprval_set_idref(ret, to_disp(ctx->global), id);
503 for(item = ctx->named_items; item; item = item->next) {
504 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
511 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
512 SCRIPTINFO_IUNKNOWN, &unk, NULL);
514 WARN("GetItemInfo failed: %08x\n", hres);
518 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
519 IUnknown_Release(unk);
521 WARN("object does not implement IDispatch\n");
526 IDispatch_AddRef(item->disp);
527 ret->type = EXPRVAL_JSVAL;
528 ret->u.val = jsval_disp(item->disp);
533 if(lookup_global_members(ctx, identifier, ret))
536 ret->type = EXPRVAL_INVALID;
540 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
541 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
544 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
545 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
548 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
549 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
552 static inline jsstr_t *get_op_str(exec_ctx_t *ctx, int i){
553 return ctx->code->instrs[ctx->ip].u.arg[i].str;
556 static inline double get_op_double(exec_ctx_t *ctx){
557 return ctx->code->instrs[ctx->ip].u.dbl;
560 /* ECMA-262 3rd Edition 12.2 */
561 static HRESULT interp_var_set(exec_ctx_t *ctx)
563 const BSTR name = get_op_bstr(ctx, 0);
567 TRACE("%s\n", debugstr_w(name));
569 val = stack_pop(ctx);
570 hres = jsdisp_propput_name(ctx->var_disp, name, val);
575 /* ECMA-262 3rd Edition 12.6.4 */
576 static HRESULT interp_forin(exec_ctx_t *ctx)
578 const HRESULT arg = get_op_uint(ctx, 0);
579 IDispatch *var_obj, *obj = NULL;
588 val = stack_pop(ctx);
590 assert(is_number(stack_top(ctx)));
591 id = get_number(stack_top(ctx));
593 var_obj = stack_topn_objid(ctx, 1, &var_id);
595 FIXME("invalid ref\n");
600 if(is_object_instance(stack_topn(ctx, 3)))
601 obj = get_object(stack_topn(ctx, 3));
604 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
605 if(SUCCEEDED(hres)) {
606 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
608 hres = IDispatchEx_GetMemberName(dispex, id, &name);
609 IDispatchEx_Release(dispex);
615 TRACE("No IDispatchEx\n");
622 str = jsstr_alloc_len(name, SysStringLen(name));
625 return E_OUTOFMEMORY;
629 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
631 hres = disp_propput(ctx->script, var_obj, var_id, jsval_string(str));
640 return stack_push(ctx, val);
645 /* ECMA-262 3rd Edition 12.10 */
646 static HRESULT interp_push_scope(exec_ctx_t *ctx)
655 hres = to_object(ctx->script, v, &disp);
660 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
661 IDispatch_Release(disp);
665 /* ECMA-262 3rd Edition 12.10 */
666 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
670 scope_pop(&ctx->scope_chain);
674 /* ECMA-262 3rd Edition 12.13 */
675 static HRESULT interp_case(exec_ctx_t *ctx)
677 const unsigned arg = get_op_uint(ctx, 0);
685 hres = equal2_values(stack_top(ctx), v, &b);
699 /* ECMA-262 3rd Edition 12.13 */
700 static HRESULT interp_throw(exec_ctx_t *ctx)
704 jsval_release(ctx->script->ei.val);
705 ctx->script->ei.val = stack_pop(ctx);
706 return DISP_E_EXCEPTION;
709 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
711 const HRESULT arg = get_op_uint(ctx, 0);
713 TRACE("%08x\n", arg);
715 return throw_reference_error(ctx->script, arg, NULL);
718 static HRESULT interp_throw_type(exec_ctx_t *ctx)
720 const HRESULT hres = get_op_uint(ctx, 0);
721 jsstr_t *str = get_op_str(ctx, 1);
723 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
725 return throw_type_error(ctx->script, hres, str->str);
728 /* ECMA-262 3rd Edition 12.14 */
729 static HRESULT interp_push_except(exec_ctx_t *ctx)
731 const unsigned arg1 = get_op_uint(ctx, 0);
732 const BSTR arg2 = get_op_bstr(ctx, 1);
733 except_frame_t *except;
738 stack_top = ctx->top;
743 hres = stack_push(ctx, jsval_bool(TRUE));
746 hres = stack_push(ctx, jsval_bool(TRUE));
751 except = heap_alloc(sizeof(*except));
753 return E_OUTOFMEMORY;
755 except->stack_top = stack_top;
756 except->scope = ctx->scope_chain;
757 except->catch_off = arg1;
758 except->ident = arg2;
759 except->next = ctx->except_frame;
760 ctx->except_frame = except;
764 /* ECMA-262 3rd Edition 12.14 */
765 static HRESULT interp_pop_except(exec_ctx_t *ctx)
767 except_frame_t *except;
771 except = ctx->except_frame;
772 assert(except != NULL);
774 ctx->except_frame = except->next;
779 /* ECMA-262 3rd Edition 12.14 */
780 static HRESULT interp_end_finally(exec_ctx_t *ctx)
788 assert(is_bool(stack_top(ctx)));
789 if(!get_bool(stack_top(ctx))) {
790 TRACE("passing exception\n");
795 ctx->script->ei.val = stack_pop(ctx);
796 return DISP_E_EXCEPTION;
800 return stack_push(ctx, v);
803 /* ECMA-262 3rd Edition 13 */
804 static HRESULT interp_func(exec_ctx_t *ctx)
806 unsigned func_idx = get_op_uint(ctx, 0);
810 TRACE("%d\n", func_idx);
812 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
813 ctx->scope_chain, &dispex);
817 return stack_push(ctx, jsval_obj(dispex));
820 /* ECMA-262 3rd Edition 11.2.1 */
821 static HRESULT interp_array(exec_ctx_t *ctx)
831 namev = stack_pop(ctx);
833 hres = stack_pop_object(ctx, &obj);
835 jsval_release(namev);
839 hres = to_string(ctx->script, namev, &name);
840 jsval_release(namev);
842 IDispatch_Release(obj);
846 hres = disp_get_id(ctx->script, obj, name->str, NULL, 0, &id);
848 if(SUCCEEDED(hres)) {
849 hres = disp_propget(ctx->script, obj, id, &v);
850 }else if(hres == DISP_E_UNKNOWNNAME) {
851 v = jsval_undefined();
854 IDispatch_Release(obj);
858 return stack_push(ctx, v);
861 /* ECMA-262 3rd Edition 11.2.1 */
862 static HRESULT interp_member(exec_ctx_t *ctx)
864 const BSTR arg = get_op_bstr(ctx, 0);
872 hres = stack_pop_object(ctx, &obj);
876 hres = disp_get_id(ctx->script, obj, arg, arg, 0, &id);
877 if(SUCCEEDED(hres)) {
878 hres = disp_propget(ctx->script, obj, id, &v);
879 }else if(hres == DISP_E_UNKNOWNNAME) {
880 v = jsval_undefined();
883 IDispatch_Release(obj);
887 return stack_push(ctx, v);
890 /* ECMA-262 3rd Edition 11.2.1 */
891 static HRESULT interp_memberid(exec_ctx_t *ctx)
893 const unsigned arg = get_op_uint(ctx, 0);
902 namev = stack_pop(ctx);
903 objv = stack_pop(ctx);
905 hres = to_object(ctx->script, objv, &obj);
907 if(SUCCEEDED(hres)) {
908 hres = to_string(ctx->script, namev, &name);
910 IDispatch_Release(obj);
912 jsval_release(namev);
916 hres = disp_get_id(ctx->script, obj, name->str, NULL, arg, &id);
919 IDispatch_Release(obj);
920 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
922 id = JS_E_INVALID_PROPERTY;
924 ERR("failed %08x\n", hres);
929 return stack_push_objid(ctx, obj, id);
932 /* ECMA-262 3rd Edition 11.2.1 */
933 static HRESULT interp_refval(exec_ctx_t *ctx)
942 disp = stack_topn_objid(ctx, 0, &id);
944 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
946 hres = disp_propget(ctx->script, disp, id, &v);
950 return stack_push(ctx, v);
953 /* ECMA-262 3rd Edition 11.2.2 */
954 static HRESULT interp_new(exec_ctx_t *ctx)
956 const unsigned argc = get_op_uint(ctx, 0);
962 constr = stack_topn(ctx, argc);
964 /* NOTE: Should use to_object here */
967 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
968 else if(!is_object_instance(constr))
969 return throw_type_error(ctx->script, JS_E_INVALID_ACTION, NULL);
970 else if(!get_object(constr))
971 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
973 hres = disp_call_value(ctx->script, get_object(constr), NULL, DISPATCH_CONSTRUCT, argc, stack_args(ctx, argc), &r);
977 stack_popn(ctx, argc+1);
978 return stack_push(ctx, r);
981 /* ECMA-262 3rd Edition 11.2.3 */
982 static HRESULT interp_call(exec_ctx_t *ctx)
984 const unsigned argn = get_op_uint(ctx, 0);
985 const int do_ret = get_op_int(ctx, 1);
989 TRACE("%d %d\n", argn, do_ret);
991 obj = stack_topn(ctx, argn);
992 if(!is_object_instance(obj))
993 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
995 hres = disp_call_value(ctx->script, get_object(obj), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
1000 stack_popn(ctx, argn+1);
1001 return do_ret ? stack_push(ctx, r) : S_OK;
1004 /* ECMA-262 3rd Edition 11.2.3 */
1005 static HRESULT interp_call_member(exec_ctx_t *ctx)
1007 const unsigned argn = get_op_uint(ctx, 0);
1008 const int do_ret = get_op_int(ctx, 1);
1014 TRACE("%d %d\n", argn, do_ret);
1016 obj = stack_topn_objid(ctx, argn, &id);
1018 return throw_type_error(ctx->script, id, NULL);
1020 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &r : NULL);
1024 stack_popn(ctx, argn+2);
1025 return do_ret ? stack_push(ctx, r) : S_OK;
1029 /* ECMA-262 3rd Edition 11.1.1 */
1030 static HRESULT interp_this(exec_ctx_t *ctx)
1034 IDispatch_AddRef(ctx->this_obj);
1035 return stack_push(ctx, jsval_disp(ctx->this_obj));
1038 /* ECMA-262 3rd Edition 10.1.4 */
1039 static HRESULT interp_ident(exec_ctx_t *ctx)
1041 const BSTR arg = get_op_bstr(ctx, 0);
1046 TRACE("%s\n", debugstr_w(arg));
1048 hres = identifier_eval(ctx->script, arg, &exprval);
1052 if(exprval.type == EXPRVAL_INVALID)
1053 return throw_type_error(ctx->script, JS_E_UNDEFINED_VARIABLE, arg);
1055 hres = exprval_to_value(ctx->script, &exprval, &v);
1056 exprval_release(&exprval);
1060 return stack_push(ctx, v);
1063 /* ECMA-262 3rd Edition 10.1.4 */
1064 static HRESULT interp_identid(exec_ctx_t *ctx)
1066 const BSTR arg = get_op_bstr(ctx, 0);
1067 const unsigned flags = get_op_uint(ctx, 1);
1071 TRACE("%s %x\n", debugstr_w(arg), flags);
1073 hres = identifier_eval(ctx->script, arg, &exprval);
1077 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1080 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1084 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1087 if(exprval.type != EXPRVAL_IDREF) {
1088 WARN("invalid ref\n");
1089 exprval_release(&exprval);
1090 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1093 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1096 /* ECMA-262 3rd Edition 7.8.1 */
1097 static HRESULT interp_null(exec_ctx_t *ctx)
1101 return stack_push(ctx, jsval_null());
1104 /* ECMA-262 3rd Edition 7.8.2 */
1105 static HRESULT interp_bool(exec_ctx_t *ctx)
1107 const int arg = get_op_int(ctx, 0);
1109 TRACE("%s\n", arg ? "true" : "false");
1111 return stack_push(ctx, jsval_bool(arg));
1114 /* ECMA-262 3rd Edition 7.8.3 */
1115 static HRESULT interp_int(exec_ctx_t *ctx)
1117 const int arg = get_op_int(ctx, 0);
1121 return stack_push(ctx, jsval_number(arg));
1124 /* ECMA-262 3rd Edition 7.8.3 */
1125 static HRESULT interp_double(exec_ctx_t *ctx)
1127 const double arg = get_op_double(ctx);
1129 TRACE("%lf\n", arg);
1131 return stack_push(ctx, jsval_number(arg));
1134 /* ECMA-262 3rd Edition 7.8.4 */
1135 static HRESULT interp_str(exec_ctx_t *ctx)
1137 jsstr_t *str = get_op_str(ctx, 0);
1139 TRACE("%s\n", debugstr_jsstr(str));
1141 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1144 /* ECMA-262 3rd Edition 7.8 */
1145 static HRESULT interp_regexp(exec_ctx_t *ctx)
1147 jsstr_t *source = get_op_str(ctx, 0);
1148 const unsigned flags = get_op_uint(ctx, 1);
1152 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1154 hres = create_regexp(ctx->script, source, flags, ®exp);
1158 return stack_push(ctx, jsval_obj(regexp));
1161 /* ECMA-262 3rd Edition 11.1.4 */
1162 static HRESULT interp_carray(exec_ctx_t *ctx)
1164 const unsigned arg = get_op_uint(ctx, 0);
1172 hres = create_array(ctx->script, arg, &array);
1178 val = stack_pop(ctx);
1179 hres = jsdisp_propput_idx(array, i, val);
1182 jsdisp_release(array);
1187 return stack_push(ctx, jsval_obj(array));
1190 /* ECMA-262 3rd Edition 11.1.5 */
1191 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1198 hres = create_object(ctx->script, NULL, &obj);
1202 return stack_push(ctx, jsval_obj(obj));
1205 /* ECMA-262 3rd Edition 11.1.5 */
1206 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1208 const BSTR name = get_op_bstr(ctx, 0);
1213 TRACE("%s\n", debugstr_w(name));
1215 val = stack_pop(ctx);
1217 assert(is_object_instance(stack_top(ctx)));
1218 obj = as_jsdisp(get_object(stack_top(ctx)));
1220 hres = jsdisp_propput_name(obj, name, val);
1225 /* ECMA-262 3rd Edition 11.11 */
1226 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1228 const unsigned arg = get_op_uint(ctx, 0);
1234 hres = to_boolean(stack_top(ctx), &b);
1247 /* ECMA-262 3rd Edition 11.11 */
1248 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1250 const unsigned arg = get_op_uint(ctx, 0);
1256 hres = to_boolean(stack_top(ctx), &b);
1269 /* ECMA-262 3rd Edition 11.10 */
1270 static HRESULT interp_or(exec_ctx_t *ctx)
1277 hres = stack_pop_int(ctx, &r);
1281 hres = stack_pop_int(ctx, &l);
1285 return stack_push(ctx, jsval_number(l|r));
1288 /* ECMA-262 3rd Edition 11.10 */
1289 static HRESULT interp_xor(exec_ctx_t *ctx)
1296 hres = stack_pop_int(ctx, &r);
1300 hres = stack_pop_int(ctx, &l);
1304 return stack_push(ctx, jsval_number(l^r));
1307 /* ECMA-262 3rd Edition 11.10 */
1308 static HRESULT interp_and(exec_ctx_t *ctx)
1315 hres = stack_pop_int(ctx, &r);
1319 hres = stack_pop_int(ctx, &l);
1323 return stack_push(ctx, jsval_number(l&r));
1326 /* ECMA-262 3rd Edition 11.8.6 */
1327 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1329 jsdisp_t *obj, *iter, *tmp = NULL;
1334 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1337 if(!is_object_instance(v) || !get_object(v)) {
1339 return throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1342 obj = iface_to_jsdisp((IUnknown*)get_object(v));
1343 IDispatch_Release(get_object(v));
1345 FIXME("non-jsdisp objects not supported\n");
1349 if(is_class(obj, JSCLASS_FUNCTION)) {
1350 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1352 hres = throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1354 jsdisp_release(obj);
1360 if(is_object_instance(prot)) {
1361 if(is_object_instance(v))
1362 tmp = iface_to_jsdisp((IUnknown*)get_object(v));
1363 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1364 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1370 jsdisp_release(tmp);
1372 FIXME("prototype is not an object\n");
1376 jsval_release(prot);
1381 return stack_push(ctx, jsval_bool(ret));
1384 /* ECMA-262 3rd Edition 11.8.7 */
1385 static HRESULT interp_in(exec_ctx_t *ctx)
1395 obj = stack_pop(ctx);
1396 if(!is_object_instance(obj) || !get_object(obj)) {
1398 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1402 hres = to_string(ctx->script, v, &str);
1405 IDispatch_Release(get_object(obj));
1409 hres = disp_get_id(ctx->script, get_object(obj), str->str, NULL, 0, &id);
1410 IDispatch_Release(get_object(obj));
1414 else if(hres == DISP_E_UNKNOWNNAME)
1419 return stack_push(ctx, jsval_bool(ret));
1422 /* ECMA-262 3rd Edition 11.6.1 */
1423 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1428 hres = to_primitive(ctx, lval, &l, NO_HINT);
1432 hres = to_primitive(ctx, rval, &r, NO_HINT);
1438 if(is_string(l) || is_string(r)) {
1439 jsstr_t *lstr, *rstr = NULL;
1441 hres = to_string(ctx, l, &lstr);
1443 hres = to_string(ctx, r, &rstr);
1445 if(SUCCEEDED(hres)) {
1446 unsigned len1, len2;
1449 len1 = jsstr_length(lstr);
1450 len2 = jsstr_length(rstr);
1452 ret_str = jsstr_alloc_buf(len1+len2);
1455 memcpy(ret_str->str, lstr->str, len1*sizeof(WCHAR));
1457 memcpy(ret_str->str+len1, rstr->str, len2*sizeof(WCHAR));
1458 *ret = jsval_string(ret_str);
1460 hres = E_OUTOFMEMORY;
1464 jsstr_release(lstr);
1466 jsstr_release(rstr);
1470 hres = to_number(ctx, l, &nl);
1471 if(SUCCEEDED(hres)) {
1472 hres = to_number(ctx, r, &nr);
1474 *ret = jsval_number(nl+nr);
1483 /* ECMA-262 3rd Edition 11.6.1 */
1484 static HRESULT interp_add(exec_ctx_t *ctx)
1492 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1494 hres = add_eval(ctx->script, l, r, &ret);
1500 return stack_push(ctx, ret);
1503 /* ECMA-262 3rd Edition 11.6.2 */
1504 static HRESULT interp_sub(exec_ctx_t *ctx)
1511 hres = stack_pop_number(ctx, &r);
1515 hres = stack_pop_number(ctx, &l);
1519 return stack_push(ctx, jsval_number(l-r));
1522 /* ECMA-262 3rd Edition 11.5.1 */
1523 static HRESULT interp_mul(exec_ctx_t *ctx)
1530 hres = stack_pop_number(ctx, &r);
1534 hres = stack_pop_number(ctx, &l);
1538 return stack_push(ctx, jsval_number(l*r));
1541 /* ECMA-262 3rd Edition 11.5.2 */
1542 static HRESULT interp_div(exec_ctx_t *ctx)
1549 hres = stack_pop_number(ctx, &r);
1553 hres = stack_pop_number(ctx, &l);
1557 return stack_push(ctx, jsval_number(l/r));
1560 /* ECMA-262 3rd Edition 11.5.3 */
1561 static HRESULT interp_mod(exec_ctx_t *ctx)
1568 hres = stack_pop_number(ctx, &r);
1572 hres = stack_pop_number(ctx, &l);
1576 return stack_push(ctx, jsval_number(fmod(l, r)));
1579 /* ECMA-262 3rd Edition 11.4.2 */
1580 static HRESULT interp_delete(exec_ctx_t *ctx)
1582 jsval_t objv, namev;
1583 IDispatchEx *dispex;
1591 namev = stack_pop(ctx);
1592 objv = stack_pop(ctx);
1594 hres = to_object(ctx->script, objv, &obj);
1595 jsval_release(objv);
1597 jsval_release(namev);
1601 hres = to_string(ctx->script, namev, &name);
1602 jsval_release(namev);
1604 IDispatch_Release(obj);
1608 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1609 if(SUCCEEDED(hres)) {
1612 bstr = SysAllocStringLen(name->str, jsstr_length(name));
1614 hres = IDispatchEx_DeleteMemberByName(dispex, bstr, make_grfdex(ctx->script, fdexNameCaseSensitive));
1615 SysFreeString(bstr);
1618 hres = E_OUTOFMEMORY;
1621 IDispatchEx_Release(dispex);
1627 IDispatch_Release(obj);
1628 jsstr_release(name);
1632 return stack_push(ctx, jsval_bool(ret));
1635 /* ECMA-262 3rd Edition 11.4.2 */
1636 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1638 const BSTR arg = get_op_bstr(ctx, 0);
1639 IDispatchEx *dispex;
1644 TRACE("%s\n", debugstr_w(arg));
1646 hres = identifier_eval(ctx->script, arg, &exprval);
1650 if(exprval.type != EXPRVAL_IDREF) {
1651 FIXME("Unsupported exprval\n");
1652 exprval_release(&exprval);
1656 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1657 IDispatch_Release(exprval.u.idref.disp);
1658 if(SUCCEEDED(hres)) {
1659 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1660 IDispatchEx_Release(dispex);
1667 return stack_push(ctx, jsval_bool(ret));
1670 /* ECMA-262 3rd Edition 11.4.2 */
1671 static HRESULT interp_void(exec_ctx_t *ctx)
1676 return stack_push(ctx, jsval_undefined());
1679 /* ECMA-262 3rd Edition 11.4.3 */
1680 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1682 switch(jsval_type(v)) {
1692 if(get_object(v) && (dispex = iface_to_jsdisp((IUnknown*)get_object(v)))) {
1693 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1694 jsdisp_release(dispex);
1710 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1717 /* ECMA-262 3rd Edition 11.4.3 */
1718 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1726 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1730 obj = stack_pop_objid(ctx, &id);
1732 return stack_push_string(ctx, undefinedW);
1734 hres = disp_propget(ctx->script, obj, id, &v);
1735 IDispatch_Release(obj);
1737 return stack_push_string(ctx, unknownW);
1739 hres = typeof_string(v, &ret);
1744 return stack_push_string(ctx, ret);
1747 /* ECMA-262 3rd Edition 11.4.3 */
1748 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1750 const BSTR arg = get_op_bstr(ctx, 0);
1756 TRACE("%s\n", debugstr_w(arg));
1758 hres = identifier_eval(ctx->script, arg, &exprval);
1762 if(exprval.type == EXPRVAL_INVALID) {
1763 hres = stack_push_string(ctx, undefinedW);
1764 exprval_release(&exprval);
1768 hres = exprval_to_value(ctx->script, &exprval, &v);
1769 exprval_release(&exprval);
1773 hres = typeof_string(v, &ret);
1778 return stack_push_string(ctx, ret);
1781 /* ECMA-262 3rd Edition 11.4.3 */
1782 static HRESULT interp_typeof(exec_ctx_t *ctx)
1791 hres = typeof_string(v, &ret);
1796 return stack_push_string(ctx, ret);
1799 /* ECMA-262 3rd Edition 11.4.7 */
1800 static HRESULT interp_minus(exec_ctx_t *ctx)
1807 hres = stack_pop_number(ctx, &n);
1811 return stack_push(ctx, jsval_number(-n));
1814 /* ECMA-262 3rd Edition 11.4.6 */
1815 static HRESULT interp_tonum(exec_ctx_t *ctx)
1824 hres = to_number(ctx->script, v, &n);
1829 return stack_push(ctx, jsval_number(n));
1832 /* ECMA-262 3rd Edition 11.3.1 */
1833 static HRESULT interp_postinc(exec_ctx_t *ctx)
1835 const int arg = get_op_int(ctx, 0);
1843 obj = stack_pop_objid(ctx, &id);
1845 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1847 hres = disp_propget(ctx->script, obj, id, &v);
1848 if(SUCCEEDED(hres)) {
1851 hres = to_number(ctx->script, v, &n);
1853 hres = disp_propput(ctx->script, obj, id, jsval_number(n+(double)arg));
1857 IDispatch_Release(obj);
1861 return stack_push(ctx, v);
1864 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1865 static HRESULT interp_preinc(exec_ctx_t *ctx)
1867 const int arg = get_op_int(ctx, 0);
1876 obj = stack_pop_objid(ctx, &id);
1878 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1880 hres = disp_propget(ctx->script, obj, id, &v);
1881 if(SUCCEEDED(hres)) {
1884 hres = to_number(ctx->script, v, &n);
1886 if(SUCCEEDED(hres)) {
1887 ret = n+(double)arg;
1888 hres = disp_propput(ctx->script, obj, id, jsval_number(ret));
1891 IDispatch_Release(obj);
1895 return stack_push(ctx, jsval_number(ret));
1898 /* ECMA-262 3rd Edition 11.9.3 */
1899 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
1901 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
1902 return equal2_values(lval, rval, ret);
1904 /* FIXME: NULL disps should be handled in more general way */
1905 if(is_object_instance(lval) && !get_object(lval))
1906 return equal_values(ctx, jsval_null(), rval, ret);
1907 if(is_object_instance(rval) && !get_object(rval))
1908 return equal_values(ctx, lval, jsval_null(), ret);
1910 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
1915 if(is_string(lval) && is_number(rval)) {
1919 hres = to_number(ctx, lval, &n);
1923 /* FIXME: optimize */
1924 return equal_values(ctx, jsval_number(n), rval, ret);
1927 if(is_string(rval) && is_number(lval)) {
1931 hres = to_number(ctx, rval, &n);
1935 /* FIXME: optimize */
1936 return equal_values(ctx, lval, jsval_number(n), ret);
1940 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
1943 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
1946 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
1950 hres = to_primitive(ctx, rval, &prim, NO_HINT);
1954 hres = equal_values(ctx, lval, prim, ret);
1955 jsval_release(prim);
1960 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
1964 hres = to_primitive(ctx, lval, &prim, NO_HINT);
1968 hres = equal_values(ctx, prim, rval, ret);
1969 jsval_release(prim);
1978 /* ECMA-262 3rd Edition 11.9.1 */
1979 static HRESULT interp_eq(exec_ctx_t *ctx)
1988 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
1990 hres = equal_values(ctx->script, l, r, &b);
1996 return stack_push(ctx, jsval_bool(b));
1999 /* ECMA-262 3rd Edition 11.9.2 */
2000 static HRESULT interp_neq(exec_ctx_t *ctx)
2009 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2011 hres = equal_values(ctx->script, l, r, &b);
2017 return stack_push(ctx, jsval_bool(!b));
2020 /* ECMA-262 3rd Edition 11.9.4 */
2021 static HRESULT interp_eq2(exec_ctx_t *ctx)
2032 hres = equal2_values(r, l, &b);
2038 return stack_push(ctx, jsval_bool(b));
2041 /* ECMA-262 3rd Edition 11.9.5 */
2042 static HRESULT interp_neq2(exec_ctx_t *ctx)
2053 hres = equal2_values(r, l, &b);
2059 return stack_push(ctx, jsval_bool(!b));
2062 /* ECMA-262 3rd Edition 11.8.5 */
2063 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2069 hres = to_primitive(ctx, lval, &l, NO_HINT);
2073 hres = to_primitive(ctx, rval, &r, NO_HINT);
2079 if(is_string(l) && is_string(r)) {
2080 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2081 jsstr_release(get_string(l));
2082 jsstr_release(get_string(r));
2086 hres = to_number(ctx, l, &ln);
2089 hres = to_number(ctx, r, &rn);
2094 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2098 /* ECMA-262 3rd Edition 11.8.1 */
2099 static HRESULT interp_lt(exec_ctx_t *ctx)
2108 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2110 hres = less_eval(ctx->script, l, r, FALSE, &b);
2116 return stack_push(ctx, jsval_bool(b));
2119 /* ECMA-262 3rd Edition 11.8.1 */
2120 static HRESULT interp_lteq(exec_ctx_t *ctx)
2129 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2131 hres = less_eval(ctx->script, r, l, TRUE, &b);
2137 return stack_push(ctx, jsval_bool(b));
2140 /* ECMA-262 3rd Edition 11.8.2 */
2141 static HRESULT interp_gt(exec_ctx_t *ctx)
2150 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2152 hres = less_eval(ctx->script, r, l, FALSE, &b);
2158 return stack_push(ctx, jsval_bool(b));
2161 /* ECMA-262 3rd Edition 11.8.4 */
2162 static HRESULT interp_gteq(exec_ctx_t *ctx)
2171 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2173 hres = less_eval(ctx->script, l, r, TRUE, &b);
2179 return stack_push(ctx, jsval_bool(b));
2182 /* ECMA-262 3rd Edition 11.4.8 */
2183 static HRESULT interp_bneg(exec_ctx_t *ctx)
2192 hres = to_int32(ctx->script, v, &i);
2197 return stack_push(ctx, jsval_number(~i));
2200 /* ECMA-262 3rd Edition 11.4.9 */
2201 static HRESULT interp_neg(exec_ctx_t *ctx)
2210 hres = to_boolean(v, &b);
2215 return stack_push(ctx, jsval_bool(!b));
2218 /* ECMA-262 3rd Edition 11.7.1 */
2219 static HRESULT interp_lshift(exec_ctx_t *ctx)
2225 hres = stack_pop_uint(ctx, &r);
2229 hres = stack_pop_int(ctx, &l);
2233 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2236 /* ECMA-262 3rd Edition 11.7.2 */
2237 static HRESULT interp_rshift(exec_ctx_t *ctx)
2243 hres = stack_pop_uint(ctx, &r);
2247 hres = stack_pop_int(ctx, &l);
2251 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2254 /* ECMA-262 3rd Edition 11.7.3 */
2255 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2260 hres = stack_pop_uint(ctx, &r);
2264 hres = stack_pop_uint(ctx, &l);
2268 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2271 /* ECMA-262 3rd Edition 11.13.1 */
2272 static HRESULT interp_assign(exec_ctx_t *ctx)
2283 disp = stack_pop_objid(ctx, &id);
2286 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2289 hres = disp_propput(ctx->script, disp, id, v);
2290 IDispatch_Release(disp);
2296 return stack_push(ctx, v);
2299 /* JScript extension */
2300 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2302 const unsigned argc = get_op_uint(ctx, 0);
2308 TRACE("%u\n", argc);
2310 disp = stack_topn_objid(ctx, argc+1, &id);
2312 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
2314 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2319 stack_popn(ctx, argc+2);
2320 return stack_push(ctx, v);
2323 static HRESULT interp_undefined(exec_ctx_t *ctx)
2327 return stack_push(ctx, jsval_undefined());
2330 static HRESULT interp_jmp(exec_ctx_t *ctx)
2332 const unsigned arg = get_op_uint(ctx, 0);
2340 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2342 const unsigned arg = get_op_uint(ctx, 0);
2350 hres = to_boolean(v, &b);
2362 static HRESULT interp_pop(exec_ctx_t *ctx)
2370 static HRESULT interp_ret(exec_ctx_t *ctx)
2378 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2380 static const op_func_t op_funcs[] = {
2381 #define X(x,a,b,c) interp_##x,
2386 static const unsigned op_move[] = {
2387 #define X(a,x,b,c) x,
2392 static HRESULT unwind_exception(exec_ctx_t *ctx)
2394 except_frame_t *except_frame;
2399 except_frame = ctx->except_frame;
2400 ctx->except_frame = except_frame->next;
2402 assert(except_frame->stack_top <= ctx->top);
2403 stack_popn(ctx, ctx->top - except_frame->stack_top);
2405 while(except_frame->scope != ctx->scope_chain)
2406 scope_pop(&ctx->scope_chain);
2408 ctx->ip = except_frame->catch_off;
2410 except_val = ctx->script->ei.val;
2411 ctx->script->ei.val = jsval_undefined();
2412 clear_ei(ctx->script);
2414 ident = except_frame->ident;
2415 heap_free(except_frame);
2418 jsdisp_t *scope_obj;
2420 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2421 if(SUCCEEDED(hres)) {
2422 hres = jsdisp_propput_name(scope_obj, ident, except_val);
2424 jsdisp_release(scope_obj);
2426 jsval_release(except_val);
2430 hres = scope_push(ctx->scope_chain, scope_obj, to_disp(scope_obj), &ctx->scope_chain);
2431 jsdisp_release(scope_obj);
2433 hres = stack_push(ctx, except_val);
2437 hres = stack_push(ctx, jsval_bool(FALSE));
2441 hres = stack_push(ctx, jsval_undefined());
2447 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsval_t *ret)
2449 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2450 except_frame_t *prev_except_frame;
2451 function_code_t *prev_func;
2452 unsigned prev_ip, prev_top;
2453 scope_chain_t *prev_scope;
2454 bytecode_t *prev_code;
2456 HRESULT hres = S_OK;
2460 prev_top = exec_ctx->top;
2461 prev_scope = exec_ctx->scope_chain;
2462 prev_except_frame = exec_ctx->except_frame;
2463 prev_ip = exec_ctx->ip;
2464 prev_code = exec_ctx->code;
2465 prev_func = exec_ctx->func_code;
2466 exec_ctx->ip = func->instr_off;
2467 exec_ctx->except_frame = NULL;
2468 exec_ctx->code = code;
2469 exec_ctx->func_code = func;
2471 while(exec_ctx->ip != -1) {
2472 op = code->instrs[exec_ctx->ip].op;
2473 hres = op_funcs[op](exec_ctx);
2475 TRACE("EXCEPTION\n");
2477 if(!exec_ctx->except_frame)
2480 hres = unwind_exception(exec_ctx);
2484 exec_ctx->ip += op_move[op];
2488 exec_ctx->ip = prev_ip;
2489 exec_ctx->except_frame = prev_except_frame;
2490 exec_ctx->code = prev_code;
2491 exec_ctx->func_code = prev_func;
2494 while(exec_ctx->scope_chain != prev_scope)
2495 scope_pop(&exec_ctx->scope_chain);
2496 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2500 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2501 assert(exec_ctx->scope_chain == prev_scope);
2503 if(exec_ctx->top == prev_top)
2504 *ret = jsval_undefined();
2506 *ret = stack_pop(exec_ctx);
2510 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval, jsval_t *ret)
2512 exec_ctx_t *prev_ctx;
2515 HRESULT hres = S_OK;
2517 for(i = 0; i < func->func_cnt; i++) {
2520 if(!func->funcs[i].name)
2523 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2527 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, jsval_obj(func_obj));
2528 jsdisp_release(func_obj);
2533 for(i=0; i < func->var_cnt; i++) {
2534 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2537 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2543 prev_ctx = ctx->script->exec_ctx;
2544 ctx->script->exec_ctx = ctx;
2546 hres = enter_bytecode(ctx->script, code, func, &val);
2547 assert(ctx->script->exec_ctx == ctx);
2548 ctx->script->exec_ctx = prev_ctx;