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)
77 v = SysAllocString(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, ctx->ei, 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, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
151 hres = to_object_jsval(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), ctx->ei, r);
161 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
163 return to_uint32_jsval(ctx->script, stack_pop(ctx), ctx->ei, 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, jsexcept_t *ei, 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, ei);
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 scope_release(scope_chain_t *scope)
270 scope_release(scope->next);
272 IDispatch_Release(scope->obj);
276 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
277 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
281 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
283 return E_OUTOFMEMORY;
286 ctx->is_global = is_global;
289 ctx->this_obj = this_obj;
290 else if(script_ctx->host_global)
291 ctx->this_obj = script_ctx->host_global;
293 ctx->this_obj = to_disp(script_ctx->global);
294 IDispatch_AddRef(ctx->this_obj);
296 jsdisp_addref(var_disp);
297 ctx->var_disp = var_disp;
299 script_addref(script_ctx);
300 ctx->script = script_ctx;
304 ctx->scope_chain = scope;
311 void exec_release(exec_ctx_t *ctx)
317 scope_release(ctx->scope_chain);
319 jsdisp_release(ctx->var_disp);
321 IDispatch_Release(ctx->this_obj);
323 script_release(ctx->script);
324 heap_free(ctx->stack);
328 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
333 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
335 TRACE("using IDispatch\n");
338 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
342 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
343 IDispatchEx_Release(dispex);
347 static inline BOOL var_is_null(const VARIANT *v)
349 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
352 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
354 IObjectIdentity *identity;
355 IUnknown *unk1, *unk2;
363 if(!disp1 || !disp2) {
368 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
372 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
374 IUnknown_Release(unk1);
381 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
382 if(SUCCEEDED(hres)) {
383 hres = IObjectIdentity_IsEqualObject(identity, unk2);
384 IObjectIdentity_Release(identity);
391 IUnknown_Release(unk1);
392 IUnknown_Release(unk2);
396 /* ECMA-262 3rd Edition 11.9.6 */
397 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
401 if(lval.type != rval.type) {
402 if(is_null_instance(lval))
403 *ret = is_null_instance(rval);
415 return disp_cmp(get_object(lval), get_object(rval), ret);
417 if(!get_string(lval))
418 *ret = !SysStringLen(get_string(rval));
419 else if(!get_string(rval))
420 *ret = !SysStringLen(get_string(lval));
422 *ret = !strcmpW(get_string(lval), get_string(rval));
425 *ret = get_number(lval) == get_number(rval);
428 *ret = !get_bool(lval) == !get_bool(rval);
431 FIXME("VARIANT not implemented\n");
438 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
444 for(item = ctx->named_items; item; item = item->next) {
445 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
446 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
447 if(SUCCEEDED(hres)) {
449 exprval_set_idref(ret, item->disp, id);
458 /* ECMA-262 3rd Edition 10.1.4 */
459 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
461 scope_chain_t *scope;
466 TRACE("%s\n", debugstr_w(identifier));
468 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
470 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
472 hres = disp_get_id(ctx, scope->obj, identifier, fdexNameImplicit, &id);
473 if(SUCCEEDED(hres)) {
474 exprval_set_idref(ret, scope->obj, id);
479 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
480 if(SUCCEEDED(hres)) {
481 exprval_set_idref(ret, to_disp(ctx->global), id);
485 for(item = ctx->named_items; item; item = item->next) {
486 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
493 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
494 SCRIPTINFO_IUNKNOWN, &unk, NULL);
496 WARN("GetItemInfo failed: %08x\n", hres);
500 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
501 IUnknown_Release(unk);
503 WARN("object does not implement IDispatch\n");
508 IDispatch_AddRef(item->disp);
509 ret->type = EXPRVAL_JSVAL;
510 ret->u.val = jsval_disp(item->disp);
515 if(lookup_global_members(ctx, identifier, ret))
518 ret->type = EXPRVAL_INVALID;
522 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
523 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
526 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
527 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
530 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
531 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
534 static inline const WCHAR *get_op_str(exec_ctx_t *ctx, int i){
535 return ctx->code->instrs[ctx->ip].u.arg[i].str;
538 static inline double get_op_double(exec_ctx_t *ctx){
539 return ctx->code->instrs[ctx->ip].u.dbl;
542 /* ECMA-262 3rd Edition 12.2 */
543 static HRESULT interp_var_set(exec_ctx_t *ctx)
545 const BSTR name = get_op_bstr(ctx, 0);
549 TRACE("%s\n", debugstr_w(name));
551 val = stack_pop(ctx);
552 hres = jsdisp_propput_name(ctx->var_disp, name, val, ctx->ei);
557 /* ECMA-262 3rd Edition 12.6.4 */
558 static HRESULT interp_forin(exec_ctx_t *ctx)
560 const HRESULT arg = get_op_uint(ctx, 0);
561 IDispatch *var_obj, *obj = NULL;
570 val = stack_pop(ctx);
572 assert(is_number(stack_top(ctx)));
573 id = get_number(stack_top(ctx));
575 var_obj = stack_topn_objid(ctx, 1, &var_id);
577 FIXME("invalid ref\n");
582 if(is_object_instance(stack_topn(ctx, 3)))
583 obj = get_object(stack_topn(ctx, 3));
586 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
587 if(SUCCEEDED(hres)) {
588 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
590 hres = IDispatchEx_GetMemberName(dispex, id, &name);
591 IDispatchEx_Release(dispex);
597 TRACE("No IDispatchEx\n");
604 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
606 hres = disp_propput(ctx->script, var_obj, var_id, jsval_string(name), ctx->ei);
615 return stack_push(ctx, val);
620 /* ECMA-262 3rd Edition 12.10 */
621 static HRESULT interp_push_scope(exec_ctx_t *ctx)
630 hres = to_object_jsval(ctx->script, v, &disp);
635 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
636 IDispatch_Release(disp);
640 /* ECMA-262 3rd Edition 12.10 */
641 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
645 scope_pop(&ctx->scope_chain);
649 /* ECMA-262 3rd Edition 12.13 */
650 static HRESULT interp_case(exec_ctx_t *ctx)
652 const unsigned arg = get_op_uint(ctx, 0);
660 hres = equal2_values(stack_top(ctx), v, &b);
674 /* ECMA-262 3rd Edition 12.13 */
675 static HRESULT interp_throw(exec_ctx_t *ctx)
679 ctx->ei->val = stack_pop(ctx);
680 return DISP_E_EXCEPTION;
683 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
685 const HRESULT arg = get_op_uint(ctx, 0);
687 TRACE("%08x\n", arg);
689 return throw_reference_error(ctx->script, ctx->ei, arg, NULL);
692 static HRESULT interp_throw_type(exec_ctx_t *ctx)
694 const HRESULT hres = get_op_uint(ctx, 0);
695 const WCHAR *str = get_op_str(ctx, 1);
697 TRACE("%08x %s\n", hres, debugstr_w(str));
699 return throw_type_error(ctx->script, ctx->ei, hres, str);
702 /* ECMA-262 3rd Edition 12.14 */
703 static HRESULT interp_push_except(exec_ctx_t *ctx)
705 const unsigned arg1 = get_op_uint(ctx, 0);
706 const BSTR arg2 = get_op_bstr(ctx, 1);
707 except_frame_t *except;
712 stack_top = ctx->top;
717 hres = stack_push(ctx, jsval_bool(TRUE));
720 hres = stack_push(ctx, jsval_bool(TRUE));
725 except = heap_alloc(sizeof(*except));
727 return E_OUTOFMEMORY;
729 except->stack_top = stack_top;
730 except->scope = ctx->scope_chain;
731 except->catch_off = arg1;
732 except->ident = arg2;
733 except->next = ctx->except_frame;
734 ctx->except_frame = except;
738 /* ECMA-262 3rd Edition 12.14 */
739 static HRESULT interp_pop_except(exec_ctx_t *ctx)
741 except_frame_t *except;
745 except = ctx->except_frame;
746 assert(except != NULL);
748 ctx->except_frame = except->next;
753 /* ECMA-262 3rd Edition 12.14 */
754 static HRESULT interp_end_finally(exec_ctx_t *ctx)
762 assert(is_bool(stack_top(ctx)));
763 if(!get_bool(stack_top(ctx))) {
766 TRACE("passing exception\n");
771 ctx->ei->val = stack_pop(ctx);
772 return SUCCEEDED(hres) ? DISP_E_EXCEPTION : hres;
776 return stack_push(ctx, v);
779 /* ECMA-262 3rd Edition 13 */
780 static HRESULT interp_func(exec_ctx_t *ctx)
782 unsigned func_idx = get_op_uint(ctx, 0);
786 TRACE("%d\n", func_idx);
788 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
789 ctx->scope_chain, &dispex);
793 return stack_push(ctx, jsval_obj(dispex));
796 /* ECMA-262 3rd Edition 11.2.1 */
797 static HRESULT interp_array(exec_ctx_t *ctx)
807 namev = stack_pop(ctx);
809 hres = stack_pop_object(ctx, &obj);
811 jsval_release(namev);
815 hres = to_string_jsval(ctx->script, namev, ctx->ei, &name);
816 jsval_release(namev);
818 IDispatch_Release(obj);
822 hres = disp_get_id(ctx->script, obj, name, 0, &id);
824 if(SUCCEEDED(hres)) {
825 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
826 }else if(hres == DISP_E_UNKNOWNNAME) {
827 v = jsval_undefined();
830 IDispatch_Release(obj);
834 return stack_push(ctx, v);
837 /* ECMA-262 3rd Edition 11.2.1 */
838 static HRESULT interp_member(exec_ctx_t *ctx)
840 const BSTR arg = get_op_bstr(ctx, 0);
848 hres = stack_pop_object(ctx, &obj);
852 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
853 if(SUCCEEDED(hres)) {
854 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
855 }else if(hres == DISP_E_UNKNOWNNAME) {
856 v = jsval_undefined();
859 IDispatch_Release(obj);
863 return stack_push(ctx, v);
866 /* ECMA-262 3rd Edition 11.2.1 */
867 static HRESULT interp_memberid(exec_ctx_t *ctx)
869 const unsigned arg = get_op_uint(ctx, 0);
878 namev = stack_pop(ctx);
879 objv = stack_pop(ctx);
881 hres = to_object_jsval(ctx->script, objv, &obj);
883 if(SUCCEEDED(hres)) {
884 hres = to_string_jsval(ctx->script, namev, ctx->ei, &name);
886 IDispatch_Release(obj);
888 jsval_release(namev);
892 hres = disp_get_id(ctx->script, obj, name, arg, &id);
895 IDispatch_Release(obj);
896 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
898 id = JS_E_INVALID_PROPERTY;
900 ERR("failed %08x\n", hres);
905 return stack_push_objid(ctx, obj, id);
908 /* ECMA-262 3rd Edition 11.2.1 */
909 static HRESULT interp_refval(exec_ctx_t *ctx)
918 disp = stack_topn_objid(ctx, 0, &id);
920 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
922 hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
926 return stack_push(ctx, v);
929 /* ECMA-262 3rd Edition 11.2.2 */
930 static HRESULT interp_new(exec_ctx_t *ctx)
932 const unsigned argc = get_op_uint(ctx, 0);
938 constr = stack_topn(ctx, argc);
940 /* NOTE: Should use to_object here */
943 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
944 else if(!is_object_instance(constr))
945 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
946 else if(!get_object(constr))
947 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
949 hres = disp_call_value(ctx->script, get_object(constr), NULL, DISPATCH_CONSTRUCT, argc, stack_args(ctx, argc), &r, ctx->ei);
953 stack_popn(ctx, argc+1);
954 return stack_push(ctx, r);
957 /* ECMA-262 3rd Edition 11.2.3 */
958 static HRESULT interp_call(exec_ctx_t *ctx)
960 const unsigned argn = get_op_uint(ctx, 0);
961 const int do_ret = get_op_int(ctx, 1);
965 TRACE("%d %d\n", argn, do_ret);
967 obj = stack_topn(ctx, argn);
968 if(!is_object_instance(obj))
969 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
971 hres = disp_call_value(ctx->script, get_object(obj), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
972 do_ret ? &r : NULL, ctx->ei);
976 stack_popn(ctx, argn+1);
977 return do_ret ? stack_push(ctx, r) : S_OK;
980 /* ECMA-262 3rd Edition 11.2.3 */
981 static HRESULT interp_call_member(exec_ctx_t *ctx)
983 const unsigned argn = get_op_uint(ctx, 0);
984 const int do_ret = get_op_int(ctx, 1);
990 TRACE("%d %d\n", argn, do_ret);
992 obj = stack_topn_objid(ctx, argn, &id);
994 return throw_type_error(ctx->script, ctx->ei, id, NULL);
996 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &r : NULL, ctx->ei);
1000 stack_popn(ctx, argn+2);
1001 return do_ret ? stack_push(ctx, r) : S_OK;
1005 /* ECMA-262 3rd Edition 11.1.1 */
1006 static HRESULT interp_this(exec_ctx_t *ctx)
1010 IDispatch_AddRef(ctx->this_obj);
1011 return stack_push(ctx, jsval_disp(ctx->this_obj));
1014 /* ECMA-262 3rd Edition 10.1.4 */
1015 static HRESULT interp_ident(exec_ctx_t *ctx)
1017 const BSTR arg = get_op_bstr(ctx, 0);
1022 TRACE("%s\n", debugstr_w(arg));
1024 hres = identifier_eval(ctx->script, arg, &exprval);
1028 if(exprval.type == EXPRVAL_INVALID)
1029 return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1031 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1032 exprval_release(&exprval);
1036 return stack_push(ctx, v);
1039 /* ECMA-262 3rd Edition 10.1.4 */
1040 static HRESULT interp_identid(exec_ctx_t *ctx)
1042 const BSTR arg = get_op_bstr(ctx, 0);
1043 const unsigned flags = get_op_uint(ctx, 1);
1047 TRACE("%s %x\n", debugstr_w(arg), flags);
1049 hres = identifier_eval(ctx->script, arg, &exprval);
1053 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1056 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1060 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1063 if(exprval.type != EXPRVAL_IDREF) {
1064 WARN("invalid ref\n");
1065 exprval_release(&exprval);
1066 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1069 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1072 /* ECMA-262 3rd Edition 7.8.1 */
1073 static HRESULT interp_null(exec_ctx_t *ctx)
1077 return stack_push(ctx, jsval_null());
1080 /* ECMA-262 3rd Edition 7.8.2 */
1081 static HRESULT interp_bool(exec_ctx_t *ctx)
1083 const int arg = get_op_int(ctx, 0);
1085 TRACE("%s\n", arg ? "true" : "false");
1087 return stack_push(ctx, jsval_bool(arg));
1090 /* ECMA-262 3rd Edition 7.8.3 */
1091 static HRESULT interp_int(exec_ctx_t *ctx)
1093 const int arg = get_op_int(ctx, 0);
1097 return stack_push(ctx, jsval_number(arg));
1100 /* ECMA-262 3rd Edition 7.8.3 */
1101 static HRESULT interp_double(exec_ctx_t *ctx)
1103 const double arg = get_op_double(ctx);
1105 TRACE("%lf\n", arg);
1107 return stack_push(ctx, jsval_number(arg));
1110 /* ECMA-262 3rd Edition 7.8.4 */
1111 static HRESULT interp_str(exec_ctx_t *ctx)
1113 const WCHAR *str = get_op_str(ctx, 0);
1116 TRACE("%s\n", debugstr_w(str));
1118 bstr = SysAllocString(str);
1120 return E_OUTOFMEMORY;
1122 return stack_push(ctx, jsval_string(bstr));
1125 /* ECMA-262 3rd Edition 7.8 */
1126 static HRESULT interp_regexp(exec_ctx_t *ctx)
1128 const WCHAR *source = get_op_str(ctx, 0);
1129 const unsigned flags = get_op_uint(ctx, 1);
1133 TRACE("%s %x\n", debugstr_w(source), flags);
1135 hres = create_regexp(ctx->script, source, strlenW(source), flags, ®exp);
1139 return stack_push(ctx, jsval_obj(regexp));
1142 /* ECMA-262 3rd Edition 11.1.4 */
1143 static HRESULT interp_carray(exec_ctx_t *ctx)
1145 const unsigned arg = get_op_uint(ctx, 0);
1153 hres = create_array(ctx->script, arg, &array);
1159 val = stack_pop(ctx);
1160 hres = jsdisp_propput_idx(array, i, val, ctx->ei);
1163 jsdisp_release(array);
1168 return stack_push(ctx, jsval_obj(array));
1171 /* ECMA-262 3rd Edition 11.1.5 */
1172 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1179 hres = create_object(ctx->script, NULL, &obj);
1183 return stack_push(ctx, jsval_obj(obj));
1186 /* ECMA-262 3rd Edition 11.1.5 */
1187 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1189 const BSTR name = get_op_bstr(ctx, 0);
1194 TRACE("%s\n", debugstr_w(name));
1196 val = stack_pop(ctx);
1198 assert(is_object_instance(stack_top(ctx)));
1199 obj = as_jsdisp(get_object(stack_top(ctx)));
1201 hres = jsdisp_propput_name(obj, name, val, ctx->ei);
1206 /* ECMA-262 3rd Edition 11.11 */
1207 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1209 const unsigned arg = get_op_uint(ctx, 0);
1215 hres = to_boolean(stack_top(ctx), &b);
1228 /* ECMA-262 3rd Edition 11.11 */
1229 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1231 const unsigned arg = get_op_uint(ctx, 0);
1237 hres = to_boolean(stack_top(ctx), &b);
1250 /* ECMA-262 3rd Edition 11.10 */
1251 static HRESULT interp_or(exec_ctx_t *ctx)
1258 hres = stack_pop_int(ctx, &r);
1262 hres = stack_pop_int(ctx, &l);
1266 return stack_push(ctx, jsval_number(l|r));
1269 /* ECMA-262 3rd Edition 11.10 */
1270 static HRESULT interp_xor(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_and(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.8.6 */
1308 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1310 jsdisp_t *obj, *iter, *tmp = NULL;
1315 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1318 if(!is_object_instance(v) || !get_object(v)) {
1320 return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1323 obj = iface_to_jsdisp((IUnknown*)get_object(v));
1324 IDispatch_Release(get_object(v));
1326 FIXME("non-jsdisp objects not supported\n");
1330 if(is_class(obj, JSCLASS_FUNCTION)) {
1331 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1333 hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1335 jsdisp_release(obj);
1341 if(is_object_instance(prot)) {
1342 if(is_object_instance(v))
1343 tmp = iface_to_jsdisp((IUnknown*)get_object(v));
1344 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1345 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1351 jsdisp_release(tmp);
1353 FIXME("prototype is not an object\n");
1357 jsval_release(prot);
1362 return stack_push(ctx, jsval_bool(ret));
1365 /* ECMA-262 3rd Edition 11.8.7 */
1366 static HRESULT interp_in(exec_ctx_t *ctx)
1376 obj = stack_pop(ctx);
1377 if(!is_object_instance(obj) || !get_object(obj)) {
1379 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1383 hres = to_string_jsval(ctx->script, v, ctx->ei, &str);
1386 IDispatch_Release(get_object(obj));
1390 hres = disp_get_id(ctx->script, get_object(obj), str, 0, &id);
1391 IDispatch_Release(get_object(obj));
1395 else if(hres == DISP_E_UNKNOWNNAME)
1400 return stack_push(ctx, jsval_bool(ret));
1403 /* ECMA-262 3rd Edition 11.6.1 */
1404 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsexcept_t *ei, jsval_t *ret)
1409 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1413 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1419 if(is_string(l) || is_string(r)) {
1420 BSTR lstr = NULL, rstr = NULL;
1423 lstr = get_string(l);
1425 hres = to_string_jsval(ctx, l, ei, &lstr);
1427 if(SUCCEEDED(hres)) {
1429 rstr = get_string(r);
1431 hres = to_string_jsval(ctx, r, ei, &rstr);
1434 if(SUCCEEDED(hres)) {
1438 len1 = SysStringLen(lstr);
1439 len2 = SysStringLen(rstr);
1441 ret_str = SysAllocStringLen(NULL, len1+len2);
1443 memcpy(ret_str, lstr, len1*sizeof(WCHAR));
1445 memcpy(ret_str+len1, rstr, len2*sizeof(WCHAR));
1446 ret_str[len1+len2] = 0;
1447 *ret = jsval_string(ret_str);
1451 SysFreeString(lstr);
1452 else if(!is_string(r))
1453 SysFreeString(rstr);
1457 hres = to_number(ctx, l, ei, &nl);
1458 if(SUCCEEDED(hres)) {
1459 hres = to_number(ctx, r, ei, &nr);
1461 *ret = jsval_number(nl+nr);
1470 /* ECMA-262 3rd Edition 11.6.1 */
1471 static HRESULT interp_add(exec_ctx_t *ctx)
1479 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1481 hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1487 return stack_push(ctx, ret);
1490 /* ECMA-262 3rd Edition 11.6.2 */
1491 static HRESULT interp_sub(exec_ctx_t *ctx)
1498 hres = stack_pop_number(ctx, &r);
1502 hres = stack_pop_number(ctx, &l);
1506 return stack_push(ctx, jsval_number(l-r));
1509 /* ECMA-262 3rd Edition 11.5.1 */
1510 static HRESULT interp_mul(exec_ctx_t *ctx)
1517 hres = stack_pop_number(ctx, &r);
1521 hres = stack_pop_number(ctx, &l);
1525 return stack_push(ctx, jsval_number(l*r));
1528 /* ECMA-262 3rd Edition 11.5.2 */
1529 static HRESULT interp_div(exec_ctx_t *ctx)
1536 hres = stack_pop_number(ctx, &r);
1540 hres = stack_pop_number(ctx, &l);
1544 return stack_push(ctx, jsval_number(l/r));
1547 /* ECMA-262 3rd Edition 11.5.3 */
1548 static HRESULT interp_mod(exec_ctx_t *ctx)
1555 hres = stack_pop_number(ctx, &r);
1559 hres = stack_pop_number(ctx, &l);
1563 return stack_push(ctx, jsval_number(fmod(l, r)));
1566 /* ECMA-262 3rd Edition 11.4.2 */
1567 static HRESULT interp_delete(exec_ctx_t *ctx)
1569 jsval_t objv, namev;
1570 IDispatchEx *dispex;
1578 namev = stack_pop(ctx);
1579 objv = stack_pop(ctx);
1581 hres = to_object_jsval(ctx->script, objv, &obj);
1582 jsval_release(objv);
1584 jsval_release(namev);
1588 hres = to_string_jsval(ctx->script, namev, ctx->ei, &name);
1589 jsval_release(namev);
1591 IDispatch_Release(obj);
1595 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1596 if(SUCCEEDED(hres)) {
1597 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1599 IDispatchEx_Release(dispex);
1605 IDispatch_Release(obj);
1606 SysFreeString(name);
1610 return stack_push(ctx, jsval_bool(ret));
1613 /* ECMA-262 3rd Edition 11.4.2 */
1614 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1616 const BSTR arg = get_op_bstr(ctx, 0);
1617 IDispatchEx *dispex;
1622 TRACE("%s\n", debugstr_w(arg));
1624 hres = identifier_eval(ctx->script, arg, &exprval);
1628 if(exprval.type != EXPRVAL_IDREF) {
1629 FIXME("Unsupported exprval\n");
1630 exprval_release(&exprval);
1634 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1635 IDispatch_Release(exprval.u.idref.disp);
1636 if(SUCCEEDED(hres)) {
1637 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1638 IDispatchEx_Release(dispex);
1645 return stack_push(ctx, jsval_bool(ret));
1648 /* ECMA-262 3rd Edition 11.4.2 */
1649 static HRESULT interp_void(exec_ctx_t *ctx)
1654 return stack_push(ctx, jsval_undefined());
1657 /* ECMA-262 3rd Edition 11.4.3 */
1658 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1670 if(get_object(v) && (dispex = iface_to_jsdisp((IUnknown*)get_object(v)))) {
1671 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1672 jsdisp_release(dispex);
1688 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1695 /* ECMA-262 3rd Edition 11.4.3 */
1696 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1704 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1708 obj = stack_pop_objid(ctx, &id);
1710 return stack_push_string(ctx, undefinedW);
1712 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1713 IDispatch_Release(obj);
1715 return stack_push_string(ctx, unknownW);
1717 hres = typeof_string(v, &ret);
1722 return stack_push_string(ctx, ret);
1725 /* ECMA-262 3rd Edition 11.4.3 */
1726 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1728 const BSTR arg = get_op_bstr(ctx, 0);
1734 TRACE("%s\n", debugstr_w(arg));
1736 hres = identifier_eval(ctx->script, arg, &exprval);
1740 if(exprval.type == EXPRVAL_INVALID) {
1741 hres = stack_push_string(ctx, undefinedW);
1742 exprval_release(&exprval);
1746 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1747 exprval_release(&exprval);
1751 hres = typeof_string(v, &ret);
1756 return stack_push_string(ctx, ret);
1759 /* ECMA-262 3rd Edition 11.4.3 */
1760 static HRESULT interp_typeof(exec_ctx_t *ctx)
1769 hres = typeof_string(v, &ret);
1774 return stack_push_string(ctx, ret);
1777 /* ECMA-262 3rd Edition 11.4.7 */
1778 static HRESULT interp_minus(exec_ctx_t *ctx)
1785 hres = stack_pop_number(ctx, &n);
1789 return stack_push(ctx, jsval_number(-n));
1792 /* ECMA-262 3rd Edition 11.4.6 */
1793 static HRESULT interp_tonum(exec_ctx_t *ctx)
1802 hres = to_number(ctx->script, v, ctx->ei, &n);
1807 return stack_push(ctx, jsval_number(n));
1810 /* ECMA-262 3rd Edition 11.3.1 */
1811 static HRESULT interp_postinc(exec_ctx_t *ctx)
1813 const int arg = get_op_int(ctx, 0);
1821 obj = stack_pop_objid(ctx, &id);
1823 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1825 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1826 if(SUCCEEDED(hres)) {
1829 hres = to_number(ctx->script, v, ctx->ei, &n);
1831 hres = disp_propput(ctx->script, obj, id, jsval_number(n+(double)arg), ctx->ei);
1835 IDispatch_Release(obj);
1839 return stack_push(ctx, v);
1842 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1843 static HRESULT interp_preinc(exec_ctx_t *ctx)
1845 const int arg = get_op_int(ctx, 0);
1854 obj = stack_pop_objid(ctx, &id);
1856 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1858 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1859 if(SUCCEEDED(hres)) {
1862 hres = to_number(ctx->script, v, ctx->ei, &n);
1864 if(SUCCEEDED(hres)) {
1865 ret = n+(double)arg;
1866 hres = disp_propput(ctx->script, obj, id, jsval_number(ret), ctx->ei);
1869 IDispatch_Release(obj);
1873 return stack_push(ctx, jsval_number(ret));
1876 /* ECMA-262 3rd Edition 11.9.3 */
1877 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsexcept_t *ei, BOOL *ret)
1879 if(lval.type == rval.type || (is_number(lval) && is_number(rval)))
1880 return equal2_values(lval, rval, ret);
1882 /* FIXME: NULL disps should be handled in more general way */
1883 if(is_object_instance(lval) && !get_object(lval))
1884 return equal_values(ctx, jsval_null(), rval, ei, ret);
1885 if(is_object_instance(rval) && !get_object(rval))
1886 return equal_values(ctx, lval, jsval_null(), ei, ret);
1888 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
1893 if(is_string(lval) && is_number(rval)) {
1897 hres = to_number(ctx, lval, ei, &n);
1901 /* FIXME: optimize */
1902 return equal_values(ctx, jsval_number(n), rval, ei, ret);
1905 if(is_string(rval) && is_number(lval)) {
1909 hres = to_number(ctx, rval, ei, &n);
1913 /* FIXME: optimize */
1914 return equal_values(ctx, lval, jsval_number(n), ei, ret);
1918 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ei, ret);
1921 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ei, ret);
1924 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
1928 hres = to_primitive(ctx, rval, ei, &prim, NO_HINT);
1932 hres = equal_values(ctx, lval, prim, ei, ret);
1933 jsval_release(prim);
1938 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
1942 hres = to_primitive(ctx, lval, ei, &prim, NO_HINT);
1946 hres = equal_values(ctx, prim, rval, ei, ret);
1947 jsval_release(prim);
1956 /* ECMA-262 3rd Edition 11.9.1 */
1957 static HRESULT interp_eq(exec_ctx_t *ctx)
1966 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
1968 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
1974 return stack_push(ctx, jsval_bool(b));
1977 /* ECMA-262 3rd Edition 11.9.2 */
1978 static HRESULT interp_neq(exec_ctx_t *ctx)
1987 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
1989 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
1995 return stack_push(ctx, jsval_bool(!b));
1998 /* ECMA-262 3rd Edition 11.9.4 */
1999 static HRESULT interp_eq2(exec_ctx_t *ctx)
2010 hres = equal2_values(r, l, &b);
2016 return stack_push(ctx, jsval_bool(b));
2019 /* ECMA-262 3rd Edition 11.9.5 */
2020 static HRESULT interp_neq2(exec_ctx_t *ctx)
2031 hres = equal2_values(r, l, &b);
2037 return stack_push(ctx, jsval_bool(!b));
2040 /* ECMA-262 3rd Edition 11.8.5 */
2041 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2047 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2051 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2057 if(is_string(l) && is_string(r)) {
2058 *ret = (strcmpW(get_string(l), get_string(r)) < 0) ^ greater;
2059 SysFreeString(get_string(l));
2060 SysFreeString(get_string(r));
2064 hres = to_number(ctx, l, ei, &ln);
2067 hres = to_number(ctx, r, ei, &rn);
2072 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2076 /* ECMA-262 3rd Edition 11.8.1 */
2077 static HRESULT interp_lt(exec_ctx_t *ctx)
2086 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2088 hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2094 return stack_push(ctx, jsval_bool(b));
2097 /* ECMA-262 3rd Edition 11.8.1 */
2098 static HRESULT interp_lteq(exec_ctx_t *ctx)
2107 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2109 hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2115 return stack_push(ctx, jsval_bool(b));
2118 /* ECMA-262 3rd Edition 11.8.2 */
2119 static HRESULT interp_gt(exec_ctx_t *ctx)
2128 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2130 hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2136 return stack_push(ctx, jsval_bool(b));
2139 /* ECMA-262 3rd Edition 11.8.4 */
2140 static HRESULT interp_gteq(exec_ctx_t *ctx)
2149 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2151 hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2157 return stack_push(ctx, jsval_bool(b));
2160 /* ECMA-262 3rd Edition 11.4.8 */
2161 static HRESULT interp_bneg(exec_ctx_t *ctx)
2170 hres = to_int32(ctx->script, v, ctx->ei, &i);
2175 return stack_push(ctx, jsval_number(~i));
2178 /* ECMA-262 3rd Edition 11.4.9 */
2179 static HRESULT interp_neg(exec_ctx_t *ctx)
2188 hres = to_boolean(v, &b);
2193 return stack_push(ctx, jsval_bool(!b));
2196 /* ECMA-262 3rd Edition 11.7.1 */
2197 static HRESULT interp_lshift(exec_ctx_t *ctx)
2203 hres = stack_pop_uint(ctx, &r);
2207 hres = stack_pop_int(ctx, &l);
2211 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2214 /* ECMA-262 3rd Edition 11.7.2 */
2215 static HRESULT interp_rshift(exec_ctx_t *ctx)
2221 hres = stack_pop_uint(ctx, &r);
2225 hres = stack_pop_int(ctx, &l);
2229 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2232 /* ECMA-262 3rd Edition 11.7.3 */
2233 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2238 hres = stack_pop_uint(ctx, &r);
2242 hres = stack_pop_uint(ctx, &l);
2246 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2249 /* ECMA-262 3rd Edition 11.13.1 */
2250 static HRESULT interp_assign(exec_ctx_t *ctx)
2261 disp = stack_pop_objid(ctx, &id);
2264 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2267 hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2268 IDispatch_Release(disp);
2274 return stack_push(ctx, v);
2277 /* JScript extension */
2278 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2280 const unsigned argc = get_op_uint(ctx, 0);
2286 TRACE("%u\n", argc);
2288 disp = stack_topn_objid(ctx, argc+1, &id);
2290 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2292 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL, ctx->ei);
2297 stack_popn(ctx, argc+2);
2298 return stack_push(ctx, v);
2301 static HRESULT interp_undefined(exec_ctx_t *ctx)
2305 return stack_push(ctx, jsval_undefined());
2308 static HRESULT interp_jmp(exec_ctx_t *ctx)
2310 const unsigned arg = get_op_uint(ctx, 0);
2318 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2320 const unsigned arg = get_op_uint(ctx, 0);
2328 hres = to_boolean(v, &b);
2340 static HRESULT interp_pop(exec_ctx_t *ctx)
2348 static HRESULT interp_ret(exec_ctx_t *ctx)
2356 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2358 static const op_func_t op_funcs[] = {
2359 #define X(x,a,b,c) interp_##x,
2364 static const unsigned op_move[] = {
2365 #define X(a,x,b,c) x,
2370 static HRESULT unwind_exception(exec_ctx_t *ctx)
2372 except_frame_t *except_frame;
2377 except_frame = ctx->except_frame;
2378 ctx->except_frame = except_frame->next;
2380 assert(except_frame->stack_top <= ctx->top);
2381 stack_popn(ctx, ctx->top - except_frame->stack_top);
2383 while(except_frame->scope != ctx->scope_chain)
2384 scope_pop(&ctx->scope_chain);
2386 ctx->ip = except_frame->catch_off;
2388 except_val = ctx->ei->val;
2389 memset(ctx->ei, 0, sizeof(*ctx->ei));
2391 ident = except_frame->ident;
2392 heap_free(except_frame);
2395 jsdisp_t *scope_obj;
2397 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2398 if(SUCCEEDED(hres)) {
2399 hres = jsdisp_propput_name(scope_obj, ident, except_val, ctx->ei);
2401 jsdisp_release(scope_obj);
2403 jsval_release(except_val);
2407 hres = scope_push(ctx->scope_chain, scope_obj, to_disp(scope_obj), &ctx->scope_chain);
2408 jsdisp_release(scope_obj);
2410 hres = stack_push(ctx, except_val);
2414 hres = stack_push(ctx, jsval_bool(FALSE));
2418 hres = stack_push(ctx, jsval_undefined());
2424 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsexcept_t *ei, jsval_t *ret)
2426 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2427 except_frame_t *prev_except_frame;
2428 function_code_t *prev_func;
2429 unsigned prev_ip, prev_top;
2430 scope_chain_t *prev_scope;
2431 bytecode_t *prev_code;
2432 jsexcept_t *prev_ei;
2434 HRESULT hres = S_OK;
2438 prev_top = exec_ctx->top;
2439 prev_scope = exec_ctx->scope_chain;
2440 prev_except_frame = exec_ctx->except_frame;
2441 prev_ip = exec_ctx->ip;
2442 prev_ei = exec_ctx->ei;
2443 prev_code = exec_ctx->code;
2444 prev_func = exec_ctx->func_code;
2445 exec_ctx->ip = func->instr_off;
2447 exec_ctx->except_frame = NULL;
2448 exec_ctx->code = code;
2449 exec_ctx->func_code = func;
2451 while(exec_ctx->ip != -1) {
2452 op = code->instrs[exec_ctx->ip].op;
2453 TRACE("top %d\n", exec_ctx->top);
2454 hres = op_funcs[op](exec_ctx);
2456 TRACE("EXCEPTION\n");
2458 if(!exec_ctx->except_frame)
2461 hres = unwind_exception(exec_ctx);
2465 exec_ctx->ip += op_move[op];
2469 exec_ctx->ip = prev_ip;
2470 exec_ctx->ei = prev_ei;
2471 exec_ctx->except_frame = prev_except_frame;
2472 exec_ctx->code = prev_code;
2473 exec_ctx->func_code = prev_func;
2476 while(exec_ctx->scope_chain != prev_scope)
2477 scope_pop(&exec_ctx->scope_chain);
2478 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2482 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2483 assert(exec_ctx->scope_chain == prev_scope);
2485 if(exec_ctx->top == prev_top)
2486 *ret = jsval_undefined();
2488 *ret = stack_pop(exec_ctx);
2492 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval,
2493 jsexcept_t *ei, 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), ei);
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, ei, &val);
2530 assert(ctx->script->exec_ctx == ctx);
2531 ctx->script->exec_ctx = prev_ctx;