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, 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, BSTR name, DWORD flags, DISPID *id)
340 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
342 TRACE("using IDispatch\n");
345 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
349 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
350 IDispatchEx_Release(dispex);
354 static inline BOOL var_is_null(const VARIANT *v)
356 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
359 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
361 IObjectIdentity *identity;
362 IUnknown *unk1, *unk2;
370 if(!disp1 || !disp2) {
375 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
379 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
381 IUnknown_Release(unk1);
388 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
389 if(SUCCEEDED(hres)) {
390 hres = IObjectIdentity_IsEqualObject(identity, unk2);
391 IObjectIdentity_Release(identity);
398 IUnknown_Release(unk1);
399 IUnknown_Release(unk2);
403 /* ECMA-262 3rd Edition 11.9.6 */
404 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
406 jsval_type_t type = jsval_type(lval);
410 if(type != jsval_type(rval)) {
411 if(is_null_instance(lval))
412 *ret = is_null_instance(rval);
424 return disp_cmp(get_object(lval), get_object(rval), ret);
426 if(!get_string(lval))
427 *ret = !SysStringLen(get_string(rval));
428 else if(!get_string(rval))
429 *ret = !SysStringLen(get_string(lval));
431 *ret = !strcmpW(get_string(lval), get_string(rval));
434 *ret = get_number(lval) == get_number(rval);
437 *ret = !get_bool(lval) == !get_bool(rval);
440 FIXME("VARIANT not implemented\n");
447 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
453 for(item = ctx->named_items; item; item = item->next) {
454 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
455 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
456 if(SUCCEEDED(hres)) {
458 exprval_set_idref(ret, item->disp, id);
467 /* ECMA-262 3rd Edition 10.1.4 */
468 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
470 scope_chain_t *scope;
475 TRACE("%s\n", debugstr_w(identifier));
477 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
479 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
481 hres = disp_get_id(ctx, scope->obj, identifier, fdexNameImplicit, &id);
482 if(SUCCEEDED(hres)) {
483 exprval_set_idref(ret, scope->obj, id);
488 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
489 if(SUCCEEDED(hres)) {
490 exprval_set_idref(ret, to_disp(ctx->global), id);
494 for(item = ctx->named_items; item; item = item->next) {
495 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
502 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
503 SCRIPTINFO_IUNKNOWN, &unk, NULL);
505 WARN("GetItemInfo failed: %08x\n", hres);
509 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
510 IUnknown_Release(unk);
512 WARN("object does not implement IDispatch\n");
517 IDispatch_AddRef(item->disp);
518 ret->type = EXPRVAL_JSVAL;
519 ret->u.val = jsval_disp(item->disp);
524 if(lookup_global_members(ctx, identifier, ret))
527 ret->type = EXPRVAL_INVALID;
531 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
532 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
535 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
536 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
539 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
540 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
543 static inline const WCHAR *get_op_str(exec_ctx_t *ctx, int i){
544 return ctx->code->instrs[ctx->ip].u.arg[i].str;
547 static inline double get_op_double(exec_ctx_t *ctx){
548 return ctx->code->instrs[ctx->ip].u.dbl;
551 /* ECMA-262 3rd Edition 12.2 */
552 static HRESULT interp_var_set(exec_ctx_t *ctx)
554 const BSTR name = get_op_bstr(ctx, 0);
558 TRACE("%s\n", debugstr_w(name));
560 val = stack_pop(ctx);
561 hres = jsdisp_propput_name(ctx->var_disp, name, val);
566 /* ECMA-262 3rd Edition 12.6.4 */
567 static HRESULT interp_forin(exec_ctx_t *ctx)
569 const HRESULT arg = get_op_uint(ctx, 0);
570 IDispatch *var_obj, *obj = NULL;
579 val = stack_pop(ctx);
581 assert(is_number(stack_top(ctx)));
582 id = get_number(stack_top(ctx));
584 var_obj = stack_topn_objid(ctx, 1, &var_id);
586 FIXME("invalid ref\n");
591 if(is_object_instance(stack_topn(ctx, 3)))
592 obj = get_object(stack_topn(ctx, 3));
595 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
596 if(SUCCEEDED(hres)) {
597 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
599 hres = IDispatchEx_GetMemberName(dispex, id, &name);
600 IDispatchEx_Release(dispex);
606 TRACE("No IDispatchEx\n");
613 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
615 hres = disp_propput(ctx->script, var_obj, var_id, jsval_string(name));
624 return stack_push(ctx, val);
629 /* ECMA-262 3rd Edition 12.10 */
630 static HRESULT interp_push_scope(exec_ctx_t *ctx)
639 hres = to_object(ctx->script, v, &disp);
644 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
645 IDispatch_Release(disp);
649 /* ECMA-262 3rd Edition 12.10 */
650 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
654 scope_pop(&ctx->scope_chain);
658 /* ECMA-262 3rd Edition 12.13 */
659 static HRESULT interp_case(exec_ctx_t *ctx)
661 const unsigned arg = get_op_uint(ctx, 0);
669 hres = equal2_values(stack_top(ctx), v, &b);
683 /* ECMA-262 3rd Edition 12.13 */
684 static HRESULT interp_throw(exec_ctx_t *ctx)
688 jsval_release(ctx->script->ei.val);
689 ctx->script->ei.val = stack_pop(ctx);
690 return DISP_E_EXCEPTION;
693 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
695 const HRESULT arg = get_op_uint(ctx, 0);
697 TRACE("%08x\n", arg);
699 return throw_reference_error(ctx->script, arg, NULL);
702 static HRESULT interp_throw_type(exec_ctx_t *ctx)
704 const HRESULT hres = get_op_uint(ctx, 0);
705 const WCHAR *str = get_op_str(ctx, 1);
707 TRACE("%08x %s\n", hres, debugstr_w(str));
709 return throw_type_error(ctx->script, hres, str);
712 /* ECMA-262 3rd Edition 12.14 */
713 static HRESULT interp_push_except(exec_ctx_t *ctx)
715 const unsigned arg1 = get_op_uint(ctx, 0);
716 const BSTR arg2 = get_op_bstr(ctx, 1);
717 except_frame_t *except;
722 stack_top = ctx->top;
727 hres = stack_push(ctx, jsval_bool(TRUE));
730 hres = stack_push(ctx, jsval_bool(TRUE));
735 except = heap_alloc(sizeof(*except));
737 return E_OUTOFMEMORY;
739 except->stack_top = stack_top;
740 except->scope = ctx->scope_chain;
741 except->catch_off = arg1;
742 except->ident = arg2;
743 except->next = ctx->except_frame;
744 ctx->except_frame = except;
748 /* ECMA-262 3rd Edition 12.14 */
749 static HRESULT interp_pop_except(exec_ctx_t *ctx)
751 except_frame_t *except;
755 except = ctx->except_frame;
756 assert(except != NULL);
758 ctx->except_frame = except->next;
763 /* ECMA-262 3rd Edition 12.14 */
764 static HRESULT interp_end_finally(exec_ctx_t *ctx)
772 assert(is_bool(stack_top(ctx)));
773 if(!get_bool(stack_top(ctx))) {
774 TRACE("passing exception\n");
779 ctx->script->ei.val = stack_pop(ctx);
780 return DISP_E_EXCEPTION;
784 return stack_push(ctx, v);
787 /* ECMA-262 3rd Edition 13 */
788 static HRESULT interp_func(exec_ctx_t *ctx)
790 unsigned func_idx = get_op_uint(ctx, 0);
794 TRACE("%d\n", func_idx);
796 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
797 ctx->scope_chain, &dispex);
801 return stack_push(ctx, jsval_obj(dispex));
804 /* ECMA-262 3rd Edition 11.2.1 */
805 static HRESULT interp_array(exec_ctx_t *ctx)
815 namev = stack_pop(ctx);
817 hres = stack_pop_object(ctx, &obj);
819 jsval_release(namev);
823 hres = to_string(ctx->script, namev, &name);
824 jsval_release(namev);
826 IDispatch_Release(obj);
830 hres = disp_get_id(ctx->script, obj, name, 0, &id);
832 if(SUCCEEDED(hres)) {
833 hres = disp_propget(ctx->script, obj, id, &v);
834 }else if(hres == DISP_E_UNKNOWNNAME) {
835 v = jsval_undefined();
838 IDispatch_Release(obj);
842 return stack_push(ctx, v);
845 /* ECMA-262 3rd Edition 11.2.1 */
846 static HRESULT interp_member(exec_ctx_t *ctx)
848 const BSTR arg = get_op_bstr(ctx, 0);
856 hres = stack_pop_object(ctx, &obj);
860 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
861 if(SUCCEEDED(hres)) {
862 hres = disp_propget(ctx->script, obj, id, &v);
863 }else if(hres == DISP_E_UNKNOWNNAME) {
864 v = jsval_undefined();
867 IDispatch_Release(obj);
871 return stack_push(ctx, v);
874 /* ECMA-262 3rd Edition 11.2.1 */
875 static HRESULT interp_memberid(exec_ctx_t *ctx)
877 const unsigned arg = get_op_uint(ctx, 0);
886 namev = stack_pop(ctx);
887 objv = stack_pop(ctx);
889 hres = to_object(ctx->script, objv, &obj);
891 if(SUCCEEDED(hres)) {
892 hres = to_string(ctx->script, namev, &name);
894 IDispatch_Release(obj);
896 jsval_release(namev);
900 hres = disp_get_id(ctx->script, obj, name, arg, &id);
903 IDispatch_Release(obj);
904 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
906 id = JS_E_INVALID_PROPERTY;
908 ERR("failed %08x\n", hres);
913 return stack_push_objid(ctx, obj, id);
916 /* ECMA-262 3rd Edition 11.2.1 */
917 static HRESULT interp_refval(exec_ctx_t *ctx)
926 disp = stack_topn_objid(ctx, 0, &id);
928 return throw_reference_error(ctx->script, JS_E_ILLEGAL_ASSIGN, NULL);
930 hres = disp_propget(ctx->script, disp, id, &v);
934 return stack_push(ctx, v);
937 /* ECMA-262 3rd Edition 11.2.2 */
938 static HRESULT interp_new(exec_ctx_t *ctx)
940 const unsigned argc = get_op_uint(ctx, 0);
946 constr = stack_topn(ctx, argc);
948 /* NOTE: Should use to_object here */
951 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
952 else if(!is_object_instance(constr))
953 return throw_type_error(ctx->script, JS_E_INVALID_ACTION, NULL);
954 else if(!get_object(constr))
955 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
957 hres = disp_call_value(ctx->script, get_object(constr), NULL, DISPATCH_CONSTRUCT, argc, stack_args(ctx, argc), &r);
961 stack_popn(ctx, argc+1);
962 return stack_push(ctx, r);
965 /* ECMA-262 3rd Edition 11.2.3 */
966 static HRESULT interp_call(exec_ctx_t *ctx)
968 const unsigned argn = get_op_uint(ctx, 0);
969 const int do_ret = get_op_int(ctx, 1);
973 TRACE("%d %d\n", argn, do_ret);
975 obj = stack_topn(ctx, argn);
976 if(!is_object_instance(obj))
977 return throw_type_error(ctx->script, JS_E_INVALID_PROPERTY, NULL);
979 hres = disp_call_value(ctx->script, get_object(obj), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
984 stack_popn(ctx, argn+1);
985 return do_ret ? stack_push(ctx, r) : S_OK;
988 /* ECMA-262 3rd Edition 11.2.3 */
989 static HRESULT interp_call_member(exec_ctx_t *ctx)
991 const unsigned argn = get_op_uint(ctx, 0);
992 const int do_ret = get_op_int(ctx, 1);
998 TRACE("%d %d\n", argn, do_ret);
1000 obj = stack_topn_objid(ctx, argn, &id);
1002 return throw_type_error(ctx->script, id, NULL);
1004 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &r : NULL);
1008 stack_popn(ctx, argn+2);
1009 return do_ret ? stack_push(ctx, r) : S_OK;
1013 /* ECMA-262 3rd Edition 11.1.1 */
1014 static HRESULT interp_this(exec_ctx_t *ctx)
1018 IDispatch_AddRef(ctx->this_obj);
1019 return stack_push(ctx, jsval_disp(ctx->this_obj));
1022 /* ECMA-262 3rd Edition 10.1.4 */
1023 static HRESULT interp_ident(exec_ctx_t *ctx)
1025 const BSTR arg = get_op_bstr(ctx, 0);
1030 TRACE("%s\n", debugstr_w(arg));
1032 hres = identifier_eval(ctx->script, arg, &exprval);
1036 if(exprval.type == EXPRVAL_INVALID)
1037 return throw_type_error(ctx->script, JS_E_UNDEFINED_VARIABLE, arg);
1039 hres = exprval_to_value(ctx->script, &exprval, &v);
1040 exprval_release(&exprval);
1044 return stack_push(ctx, v);
1047 /* ECMA-262 3rd Edition 10.1.4 */
1048 static HRESULT interp_identid(exec_ctx_t *ctx)
1050 const BSTR arg = get_op_bstr(ctx, 0);
1051 const unsigned flags = get_op_uint(ctx, 1);
1055 TRACE("%s %x\n", debugstr_w(arg), flags);
1057 hres = identifier_eval(ctx->script, arg, &exprval);
1061 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1064 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1068 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1071 if(exprval.type != EXPRVAL_IDREF) {
1072 WARN("invalid ref\n");
1073 exprval_release(&exprval);
1074 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1077 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1080 /* ECMA-262 3rd Edition 7.8.1 */
1081 static HRESULT interp_null(exec_ctx_t *ctx)
1085 return stack_push(ctx, jsval_null());
1088 /* ECMA-262 3rd Edition 7.8.2 */
1089 static HRESULT interp_bool(exec_ctx_t *ctx)
1091 const int arg = get_op_int(ctx, 0);
1093 TRACE("%s\n", arg ? "true" : "false");
1095 return stack_push(ctx, jsval_bool(arg));
1098 /* ECMA-262 3rd Edition 7.8.3 */
1099 static HRESULT interp_int(exec_ctx_t *ctx)
1101 const int arg = get_op_int(ctx, 0);
1105 return stack_push(ctx, jsval_number(arg));
1108 /* ECMA-262 3rd Edition 7.8.3 */
1109 static HRESULT interp_double(exec_ctx_t *ctx)
1111 const double arg = get_op_double(ctx);
1113 TRACE("%lf\n", arg);
1115 return stack_push(ctx, jsval_number(arg));
1118 /* ECMA-262 3rd Edition 7.8.4 */
1119 static HRESULT interp_str(exec_ctx_t *ctx)
1121 const WCHAR *str = get_op_str(ctx, 0);
1124 TRACE("%s\n", debugstr_w(str));
1126 bstr = SysAllocString(str);
1128 return E_OUTOFMEMORY;
1130 return stack_push(ctx, jsval_string(bstr));
1133 /* ECMA-262 3rd Edition 7.8 */
1134 static HRESULT interp_regexp(exec_ctx_t *ctx)
1136 const WCHAR *source = get_op_str(ctx, 0);
1137 const unsigned flags = get_op_uint(ctx, 1);
1141 TRACE("%s %x\n", debugstr_w(source), flags);
1143 hres = create_regexp(ctx->script, source, strlenW(source), flags, ®exp);
1147 return stack_push(ctx, jsval_obj(regexp));
1150 /* ECMA-262 3rd Edition 11.1.4 */
1151 static HRESULT interp_carray(exec_ctx_t *ctx)
1153 const unsigned arg = get_op_uint(ctx, 0);
1161 hres = create_array(ctx->script, arg, &array);
1167 val = stack_pop(ctx);
1168 hres = jsdisp_propput_idx(array, i, val);
1171 jsdisp_release(array);
1176 return stack_push(ctx, jsval_obj(array));
1179 /* ECMA-262 3rd Edition 11.1.5 */
1180 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1187 hres = create_object(ctx->script, NULL, &obj);
1191 return stack_push(ctx, jsval_obj(obj));
1194 /* ECMA-262 3rd Edition 11.1.5 */
1195 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1197 const BSTR name = get_op_bstr(ctx, 0);
1202 TRACE("%s\n", debugstr_w(name));
1204 val = stack_pop(ctx);
1206 assert(is_object_instance(stack_top(ctx)));
1207 obj = as_jsdisp(get_object(stack_top(ctx)));
1209 hres = jsdisp_propput_name(obj, name, val);
1214 /* ECMA-262 3rd Edition 11.11 */
1215 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1217 const unsigned arg = get_op_uint(ctx, 0);
1223 hres = to_boolean(stack_top(ctx), &b);
1236 /* ECMA-262 3rd Edition 11.11 */
1237 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1239 const unsigned arg = get_op_uint(ctx, 0);
1245 hres = to_boolean(stack_top(ctx), &b);
1258 /* ECMA-262 3rd Edition 11.10 */
1259 static HRESULT interp_or(exec_ctx_t *ctx)
1266 hres = stack_pop_int(ctx, &r);
1270 hres = stack_pop_int(ctx, &l);
1274 return stack_push(ctx, jsval_number(l|r));
1277 /* ECMA-262 3rd Edition 11.10 */
1278 static HRESULT interp_xor(exec_ctx_t *ctx)
1285 hres = stack_pop_int(ctx, &r);
1289 hres = stack_pop_int(ctx, &l);
1293 return stack_push(ctx, jsval_number(l^r));
1296 /* ECMA-262 3rd Edition 11.10 */
1297 static HRESULT interp_and(exec_ctx_t *ctx)
1304 hres = stack_pop_int(ctx, &r);
1308 hres = stack_pop_int(ctx, &l);
1312 return stack_push(ctx, jsval_number(l&r));
1315 /* ECMA-262 3rd Edition 11.8.6 */
1316 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1318 jsdisp_t *obj, *iter, *tmp = NULL;
1323 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1326 if(!is_object_instance(v) || !get_object(v)) {
1328 return throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1331 obj = iface_to_jsdisp((IUnknown*)get_object(v));
1332 IDispatch_Release(get_object(v));
1334 FIXME("non-jsdisp objects not supported\n");
1338 if(is_class(obj, JSCLASS_FUNCTION)) {
1339 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1341 hres = throw_type_error(ctx->script, JS_E_FUNCTION_EXPECTED, NULL);
1343 jsdisp_release(obj);
1349 if(is_object_instance(prot)) {
1350 if(is_object_instance(v))
1351 tmp = iface_to_jsdisp((IUnknown*)get_object(v));
1352 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1353 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1359 jsdisp_release(tmp);
1361 FIXME("prototype is not an object\n");
1365 jsval_release(prot);
1370 return stack_push(ctx, jsval_bool(ret));
1373 /* ECMA-262 3rd Edition 11.8.7 */
1374 static HRESULT interp_in(exec_ctx_t *ctx)
1384 obj = stack_pop(ctx);
1385 if(!is_object_instance(obj) || !get_object(obj)) {
1387 return throw_type_error(ctx->script, JS_E_OBJECT_EXPECTED, NULL);
1391 hres = to_string(ctx->script, v, &str);
1394 IDispatch_Release(get_object(obj));
1398 hres = disp_get_id(ctx->script, get_object(obj), str, 0, &id);
1399 IDispatch_Release(get_object(obj));
1403 else if(hres == DISP_E_UNKNOWNNAME)
1408 return stack_push(ctx, jsval_bool(ret));
1411 /* ECMA-262 3rd Edition 11.6.1 */
1412 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1417 hres = to_primitive(ctx, lval, &l, NO_HINT);
1421 hres = to_primitive(ctx, rval, &r, NO_HINT);
1427 if(is_string(l) || is_string(r)) {
1428 BSTR lstr = NULL, rstr = NULL;
1431 lstr = get_string(l);
1433 hres = to_string(ctx, l, &lstr);
1435 if(SUCCEEDED(hres)) {
1437 rstr = get_string(r);
1439 hres = to_string(ctx, r, &rstr);
1442 if(SUCCEEDED(hres)) {
1446 len1 = SysStringLen(lstr);
1447 len2 = SysStringLen(rstr);
1449 ret_str = SysAllocStringLen(NULL, len1+len2);
1451 memcpy(ret_str, lstr, len1*sizeof(WCHAR));
1453 memcpy(ret_str+len1, rstr, len2*sizeof(WCHAR));
1454 ret_str[len1+len2] = 0;
1455 *ret = jsval_string(ret_str);
1459 SysFreeString(lstr);
1460 else if(!is_string(r))
1461 SysFreeString(rstr);
1465 hres = to_number(ctx, l, &nl);
1466 if(SUCCEEDED(hres)) {
1467 hres = to_number(ctx, r, &nr);
1469 *ret = jsval_number(nl+nr);
1478 /* ECMA-262 3rd Edition 11.6.1 */
1479 static HRESULT interp_add(exec_ctx_t *ctx)
1487 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1489 hres = add_eval(ctx->script, l, r, &ret);
1495 return stack_push(ctx, ret);
1498 /* ECMA-262 3rd Edition 11.6.2 */
1499 static HRESULT interp_sub(exec_ctx_t *ctx)
1506 hres = stack_pop_number(ctx, &r);
1510 hres = stack_pop_number(ctx, &l);
1514 return stack_push(ctx, jsval_number(l-r));
1517 /* ECMA-262 3rd Edition 11.5.1 */
1518 static HRESULT interp_mul(exec_ctx_t *ctx)
1525 hres = stack_pop_number(ctx, &r);
1529 hres = stack_pop_number(ctx, &l);
1533 return stack_push(ctx, jsval_number(l*r));
1536 /* ECMA-262 3rd Edition 11.5.2 */
1537 static HRESULT interp_div(exec_ctx_t *ctx)
1544 hres = stack_pop_number(ctx, &r);
1548 hres = stack_pop_number(ctx, &l);
1552 return stack_push(ctx, jsval_number(l/r));
1555 /* ECMA-262 3rd Edition 11.5.3 */
1556 static HRESULT interp_mod(exec_ctx_t *ctx)
1563 hres = stack_pop_number(ctx, &r);
1567 hres = stack_pop_number(ctx, &l);
1571 return stack_push(ctx, jsval_number(fmod(l, r)));
1574 /* ECMA-262 3rd Edition 11.4.2 */
1575 static HRESULT interp_delete(exec_ctx_t *ctx)
1577 jsval_t objv, namev;
1578 IDispatchEx *dispex;
1586 namev = stack_pop(ctx);
1587 objv = stack_pop(ctx);
1589 hres = to_object(ctx->script, objv, &obj);
1590 jsval_release(objv);
1592 jsval_release(namev);
1596 hres = to_string(ctx->script, namev, &name);
1597 jsval_release(namev);
1599 IDispatch_Release(obj);
1603 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1604 if(SUCCEEDED(hres)) {
1605 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1607 IDispatchEx_Release(dispex);
1613 IDispatch_Release(obj);
1614 SysFreeString(name);
1618 return stack_push(ctx, jsval_bool(ret));
1621 /* ECMA-262 3rd Edition 11.4.2 */
1622 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1624 const BSTR arg = get_op_bstr(ctx, 0);
1625 IDispatchEx *dispex;
1630 TRACE("%s\n", debugstr_w(arg));
1632 hres = identifier_eval(ctx->script, arg, &exprval);
1636 if(exprval.type != EXPRVAL_IDREF) {
1637 FIXME("Unsupported exprval\n");
1638 exprval_release(&exprval);
1642 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1643 IDispatch_Release(exprval.u.idref.disp);
1644 if(SUCCEEDED(hres)) {
1645 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1646 IDispatchEx_Release(dispex);
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 = (strcmpW(get_string(l), get_string(r)) < 0) ^ greater;
2067 SysFreeString(get_string(l));
2068 SysFreeString(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 TRACE("top %d\n", exec_ctx->top);
2460 hres = op_funcs[op](exec_ctx);
2462 TRACE("EXCEPTION\n");
2464 if(!exec_ctx->except_frame)
2467 hres = unwind_exception(exec_ctx);
2471 exec_ctx->ip += op_move[op];
2475 exec_ctx->ip = prev_ip;
2476 exec_ctx->except_frame = prev_except_frame;
2477 exec_ctx->code = prev_code;
2478 exec_ctx->func_code = prev_func;
2481 while(exec_ctx->scope_chain != prev_scope)
2482 scope_pop(&exec_ctx->scope_chain);
2483 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2487 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2488 assert(exec_ctx->scope_chain == prev_scope);
2490 if(exec_ctx->top == prev_top)
2491 *ret = jsval_undefined();
2493 *ret = stack_pop(exec_ctx);
2497 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval, jsval_t *ret)
2499 exec_ctx_t *prev_ctx;
2502 HRESULT hres = S_OK;
2504 for(i = 0; i < func->func_cnt; i++) {
2507 if(!func->funcs[i].name)
2510 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2514 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, jsval_obj(func_obj));
2515 jsdisp_release(func_obj);
2520 for(i=0; i < func->var_cnt; i++) {
2521 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2524 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2530 prev_ctx = ctx->script->exec_ctx;
2531 ctx->script->exec_ctx = ctx;
2533 hres = enter_bytecode(ctx->script, code, func, &val);
2534 assert(ctx->script->exec_ctx == ctx);
2535 ctx->script->exec_ctx = prev_ctx;