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, VARIANT *v)
51 if(!ctx->stack_size) {
52 ctx->stack = heap_alloc(16*sizeof(VARIANT));
56 }else if(ctx->stack_size == ctx->top) {
59 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
65 ctx->stack = new_stack;
69 ctx->stack[ctx->top++] = *v;
73 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
78 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
79 return stack_push(ctx, &v);
82 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
86 num_set_val(&v, number);
87 return stack_push(ctx, &v);
90 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
96 return stack_push(ctx, &v);
99 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
104 V_BSTR(&v) = SysAllocString(str);
105 return V_BSTR(&v) ? stack_push(ctx, &v) : E_OUTOFMEMORY;
108 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
113 V_VT(&v) = VT_DISPATCH;
114 V_DISPATCH(&v) = disp;
115 hres = stack_push(ctx, &v);
121 return stack_push(ctx, &v);
124 static inline VARIANT *stack_top(exec_ctx_t *ctx)
127 return ctx->stack + ctx->top-1;
130 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
132 assert(ctx->top > n);
133 return ctx->stack + ctx->top-1-n;
136 static inline VARIANT *stack_args(exec_ctx_t *ctx, unsigned n)
140 assert(ctx->top > n-1);
141 return ctx->stack + ctx->top-n;
144 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
147 return ctx->stack + --ctx->top;
150 static void stack_popn(exec_ctx_t *ctx, unsigned n)
153 VariantClear(stack_pop(ctx));
156 static HRESULT stack_pop_number(exec_ctx_t *ctx, double *r)
162 hres = to_number(ctx->script, v, ctx->ei, r);
167 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
173 if(V_VT(v) == VT_DISPATCH) {
175 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
180 hres = to_object(ctx->script, v, r);
185 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
187 return to_int32(ctx->script, stack_pop(ctx), ctx->ei, r);
190 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
192 return to_uint32(ctx->script, stack_pop(ctx), ctx->ei, r);
195 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
197 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
199 *id = V_INT(stack_pop(ctx));
200 return V_DISPATCH(stack_pop(ctx));
203 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
205 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
207 *id = V_INT(stack_topn(ctx, n));
208 return V_DISPATCH(stack_topn(ctx, n+1));
211 static void exprval_release(exprval_t *val)
214 case EXPRVAL_VARIANT:
215 if(V_VT(&val->u.var) != VT_EMPTY)
216 VariantClear(&val->u.var);
219 if(val->u.idref.disp)
220 IDispatch_Release(val->u.idref.disp);
222 case EXPRVAL_INVALID:
227 /* ECMA-262 3rd Edition 8.7.1 */
228 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
230 V_VT(ret) = VT_EMPTY;
233 case EXPRVAL_VARIANT:
234 return VariantCopy(ret, &val->u.var);
236 if(!val->u.idref.disp) {
237 FIXME("throw ReferenceError\n");
241 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei);
242 case EXPRVAL_INVALID:
246 ERR("type %d\n", val->type);
250 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
252 if(val->type == EXPRVAL_VARIANT) {
254 V_VT(&val->u.var) = VT_EMPTY;
258 return exprval_value(ctx, val, ei, ret);
261 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
263 val->type = EXPRVAL_IDREF;
264 val->u.idref.disp = disp;
265 val->u.idref.id = id;
268 IDispatch_AddRef(disp);
271 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
273 scope_chain_t *new_scope;
275 new_scope = heap_alloc(sizeof(scope_chain_t));
277 return E_OUTOFMEMORY;
281 IDispatch_AddRef(obj);
282 new_scope->jsobj = jsobj;
283 new_scope->obj = obj;
287 new_scope->next = scope;
289 new_scope->next = NULL;
296 static void scope_pop(scope_chain_t **scope)
305 void scope_release(scope_chain_t *scope)
311 scope_release(scope->next);
313 IDispatch_Release(scope->obj);
317 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
318 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
322 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
324 return E_OUTOFMEMORY;
327 ctx->is_global = is_global;
330 ctx->this_obj = this_obj;
331 else if(script_ctx->host_global)
332 ctx->this_obj = script_ctx->host_global;
334 ctx->this_obj = to_disp(script_ctx->global);
335 IDispatch_AddRef(ctx->this_obj);
337 jsdisp_addref(var_disp);
338 ctx->var_disp = var_disp;
340 script_addref(script_ctx);
341 ctx->script = script_ctx;
345 ctx->scope_chain = scope;
352 void exec_release(exec_ctx_t *ctx)
358 scope_release(ctx->scope_chain);
360 jsdisp_release(ctx->var_disp);
362 IDispatch_Release(ctx->this_obj);
364 script_release(ctx->script);
365 heap_free(ctx->stack);
369 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
374 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
376 TRACE("using IDispatch\n");
379 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
383 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
384 IDispatchEx_Release(dispex);
388 static inline BOOL is_null_var(const VARIANT *v)
390 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
393 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
395 IObjectIdentity *identity;
396 IUnknown *unk1, *unk2;
404 if(!disp1 || !disp2) {
409 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
413 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
415 IUnknown_Release(unk1);
422 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
423 if(SUCCEEDED(hres)) {
424 hres = IObjectIdentity_IsEqualObject(identity, unk2);
425 IObjectIdentity_Release(identity);
432 IUnknown_Release(unk1);
433 IUnknown_Release(unk2);
437 /* ECMA-262 3rd Edition 11.9.6 */
438 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
442 if(V_VT(lval) != V_VT(rval)) {
443 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
444 *ret = num_val(lval) == num_val(rval);
445 else if(is_null_var(lval))
446 *ret = is_null_var(rval);
458 *ret = V_I4(lval) == V_I4(rval);
461 *ret = V_R8(lval) == V_R8(rval);
465 *ret = !SysStringLen(V_BSTR(rval));
466 else if(!V_BSTR(rval))
467 *ret = !SysStringLen(V_BSTR(lval));
469 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
472 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
474 *ret = !V_BOOL(lval) == !V_BOOL(rval);
477 FIXME("unimplemented vt %d\n", V_VT(lval));
484 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
490 for(item = ctx->named_items; item; item = item->next) {
491 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
492 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
493 if(SUCCEEDED(hres)) {
495 exprval_set_idref(ret, item->disp, id);
504 /* ECMA-262 3rd Edition 10.1.4 */
505 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
507 scope_chain_t *scope;
512 TRACE("%s\n", debugstr_w(identifier));
514 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
516 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
518 hres = disp_get_id(ctx, scope->obj, identifier, fdexNameImplicit, &id);
519 if(SUCCEEDED(hres)) {
520 exprval_set_idref(ret, scope->obj, id);
525 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
526 if(SUCCEEDED(hres)) {
527 exprval_set_idref(ret, to_disp(ctx->global), id);
531 for(item = ctx->named_items; item; item = item->next) {
532 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
539 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
540 SCRIPTINFO_IUNKNOWN, &unk, NULL);
542 WARN("GetItemInfo failed: %08x\n", hres);
546 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
547 IUnknown_Release(unk);
549 WARN("object does not implement IDispatch\n");
554 ret->type = EXPRVAL_VARIANT;
555 V_VT(&ret->u.var) = VT_DISPATCH;
556 V_DISPATCH(&ret->u.var) = item->disp;
557 IDispatch_AddRef(item->disp);
562 if(lookup_global_members(ctx, identifier, ret))
565 ret->type = EXPRVAL_INVALID;
569 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
570 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
573 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
574 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
577 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
578 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
581 static inline const WCHAR *get_op_str(exec_ctx_t *ctx, int i){
582 return ctx->code->instrs[ctx->ip].u.arg[i].str;
585 static inline double get_op_double(exec_ctx_t *ctx){
586 return ctx->code->instrs[ctx->ip].u.dbl;
589 /* ECMA-262 3rd Edition 12.2 */
590 static HRESULT interp_var_set(exec_ctx_t *ctx)
592 const BSTR name = get_op_bstr(ctx, 0);
596 TRACE("%s\n", debugstr_w(name));
599 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei);
604 /* ECMA-262 3rd Edition 12.6.4 */
605 static HRESULT interp_forin(exec_ctx_t *ctx)
607 const HRESULT arg = get_op_uint(ctx, 0);
608 IDispatch *var_obj, *obj = NULL;
617 val = stack_pop(ctx);
619 assert(V_VT(stack_top(ctx)) == VT_I4);
620 id = V_I4(stack_top(ctx));
622 var_obj = stack_topn_objid(ctx, 1, &var_id);
624 FIXME("invalid ref\n");
629 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
630 obj = V_DISPATCH(stack_topn(ctx, 3));
633 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
634 if(SUCCEEDED(hres)) {
635 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
637 hres = IDispatchEx_GetMemberName(dispex, id, &name);
638 IDispatchEx_Release(dispex);
644 TRACE("No IDispatchEx\n");
653 V_I4(stack_top(ctx)) = id;
657 hres = disp_propput(ctx->script, var_obj, var_id, &v, ctx->ei);
666 return stack_push(ctx, val);
671 /* ECMA-262 3rd Edition 12.10 */
672 static HRESULT interp_push_scope(exec_ctx_t *ctx)
681 hres = to_object(ctx->script, v, &disp);
686 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
687 IDispatch_Release(disp);
691 /* ECMA-262 3rd Edition 12.10 */
692 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
696 scope_pop(&ctx->scope_chain);
700 /* ECMA-262 3rd Edition 12.13 */
701 static HRESULT interp_case(exec_ctx_t *ctx)
703 const unsigned arg = get_op_uint(ctx, 0);
711 hres = equal2_values(stack_top(ctx), v, &b);
725 /* ECMA-262 3rd Edition 12.13 */
726 static HRESULT interp_throw(exec_ctx_t *ctx)
730 ctx->ei->var = *stack_pop(ctx);
731 return DISP_E_EXCEPTION;
734 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
736 const HRESULT arg = get_op_uint(ctx, 0);
738 TRACE("%08x\n", arg);
740 return throw_reference_error(ctx->script, ctx->ei, arg, NULL);
743 static HRESULT interp_throw_type(exec_ctx_t *ctx)
745 const HRESULT hres = get_op_uint(ctx, 0);
746 const WCHAR *str = get_op_str(ctx, 1);
748 TRACE("%08x %s\n", hres, debugstr_w(str));
750 return throw_type_error(ctx->script, ctx->ei, hres, str);
753 /* ECMA-262 3rd Edition 12.14 */
754 static HRESULT interp_push_except(exec_ctx_t *ctx)
756 const unsigned arg1 = get_op_uint(ctx, 0);
757 const BSTR arg2 = get_op_bstr(ctx, 1);
758 except_frame_t *except;
763 stack_top = ctx->top;
768 hres = stack_push_bool(ctx, TRUE);
771 hres = stack_push_bool(ctx, TRUE);
776 except = heap_alloc(sizeof(*except));
778 return E_OUTOFMEMORY;
780 except->stack_top = stack_top;
781 except->scope = ctx->scope_chain;
782 except->catch_off = arg1;
783 except->ident = arg2;
784 except->next = ctx->except_frame;
785 ctx->except_frame = except;
789 /* ECMA-262 3rd Edition 12.14 */
790 static HRESULT interp_pop_except(exec_ctx_t *ctx)
792 except_frame_t *except;
796 except = ctx->except_frame;
797 assert(except != NULL);
799 ctx->except_frame = except->next;
804 /* ECMA-262 3rd Edition 12.14 */
805 static HRESULT interp_end_finally(exec_ctx_t *ctx)
813 assert(V_VT(stack_top(ctx)) == VT_BOOL);
814 if(!V_BOOL(stack_top(ctx))) {
815 TRACE("passing exception\n");
819 ctx->ei->var = *stack_pop(ctx);
820 return DISP_E_EXCEPTION;
824 return stack_push(ctx, v);
827 /* ECMA-262 3rd Edition 13 */
828 static HRESULT interp_func(exec_ctx_t *ctx)
830 unsigned func_idx = get_op_uint(ctx, 0);
835 TRACE("%d\n", func_idx);
837 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
838 ctx->scope_chain, &dispex);
842 var_set_jsdisp(&v, dispex);
843 return stack_push(ctx, &v);
846 /* ECMA-262 3rd Edition 11.2.1 */
847 static HRESULT interp_array(exec_ctx_t *ctx)
857 namev = stack_pop(ctx);
859 hres = stack_pop_object(ctx, &obj);
865 hres = to_string(ctx->script, namev, ctx->ei, &name);
868 IDispatch_Release(obj);
872 hres = disp_get_id(ctx->script, obj, name, 0, &id);
874 if(SUCCEEDED(hres)) {
875 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
876 }else if(hres == DISP_E_UNKNOWNNAME) {
880 IDispatch_Release(obj);
884 return stack_push(ctx, &v);
887 /* ECMA-262 3rd Edition 11.2.1 */
888 static HRESULT interp_member(exec_ctx_t *ctx)
890 const BSTR arg = get_op_bstr(ctx, 0);
898 hres = stack_pop_object(ctx, &obj);
902 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
903 if(SUCCEEDED(hres)) {
905 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
906 }else if(hres == DISP_E_UNKNOWNNAME) {
910 IDispatch_Release(obj);
914 return stack_push(ctx, &v);
917 /* ECMA-262 3rd Edition 11.2.1 */
918 static HRESULT interp_memberid(exec_ctx_t *ctx)
920 const unsigned arg = get_op_uint(ctx, 0);
921 VARIANT *objv, *namev;
929 namev = stack_pop(ctx);
930 objv = stack_pop(ctx);
932 hres = to_object(ctx->script, objv, &obj);
934 if(SUCCEEDED(hres)) {
935 hres = to_string(ctx->script, namev, ctx->ei, &name);
937 IDispatch_Release(obj);
943 hres = disp_get_id(ctx->script, obj, name, arg, &id);
946 IDispatch_Release(obj);
947 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
949 id = JS_E_INVALID_PROPERTY;
955 return stack_push_objid(ctx, obj, id);
958 /* ECMA-262 3rd Edition 11.2.1 */
959 static HRESULT interp_refval(exec_ctx_t *ctx)
968 disp = stack_topn_objid(ctx, 0, &id);
970 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
972 hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
976 return stack_push(ctx, &v);
979 /* ECMA-262 3rd Edition 11.2.2 */
980 static HRESULT interp_new(exec_ctx_t *ctx)
982 const unsigned arg = get_op_uint(ctx, 0);
989 constr = stack_topn(ctx, arg);
991 /* NOTE: Should use to_object here */
993 if(V_VT(constr) == VT_NULL)
994 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
995 else if(V_VT(constr) != VT_DISPATCH)
996 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
997 else if(!V_DISPATCH(constr))
998 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1000 hres = disp_call_value(ctx->script, V_DISPATCH(constr), NULL, DISPATCH_CONSTRUCT, arg, stack_args(ctx, arg), &r, ctx->ei);
1004 hres = jsval_to_variant(r, &v);
1009 stack_popn(ctx, arg+1);
1010 return stack_push(ctx, &v);
1013 /* ECMA-262 3rd Edition 11.2.3 */
1014 static HRESULT interp_call(exec_ctx_t *ctx)
1016 const unsigned argn = get_op_uint(ctx, 0);
1017 const int do_ret = get_op_int(ctx, 1);
1022 TRACE("%d %d\n", argn, do_ret);
1024 objv = stack_topn(ctx, argn);
1025 if(V_VT(objv) != VT_DISPATCH)
1026 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1028 hres = disp_call_value(ctx->script, V_DISPATCH(objv), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
1029 do_ret ? &r : NULL, ctx->ei);
1033 stack_popn(ctx, argn+1);
1038 hres = jsval_to_variant(r, &v);
1043 return stack_push(ctx, &v);
1047 /* ECMA-262 3rd Edition 11.2.3 */
1048 static HRESULT interp_call_member(exec_ctx_t *ctx)
1050 const unsigned argn = get_op_uint(ctx, 0);
1051 const int do_ret = get_op_int(ctx, 1);
1057 TRACE("%d %d\n", argn, do_ret);
1059 obj = stack_topn_objid(ctx, argn, &id);
1061 return throw_type_error(ctx->script, ctx->ei, id, NULL);
1063 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &v : NULL, ctx->ei);
1067 stack_popn(ctx, argn+2);
1068 return do_ret ? stack_push(ctx, &v) : S_OK;
1072 /* ECMA-262 3rd Edition 11.1.1 */
1073 static HRESULT interp_this(exec_ctx_t *ctx)
1079 V_VT(&v) = VT_DISPATCH;
1080 V_DISPATCH(&v) = ctx->this_obj;
1081 IDispatch_AddRef(ctx->this_obj);
1082 return stack_push(ctx, &v);
1085 /* ECMA-262 3rd Edition 10.1.4 */
1086 static HRESULT interp_ident(exec_ctx_t *ctx)
1088 const BSTR arg = get_op_bstr(ctx, 0);
1093 TRACE("%s\n", debugstr_w(arg));
1095 hres = identifier_eval(ctx->script, arg, &exprval);
1099 if(exprval.type == EXPRVAL_INVALID)
1100 return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1102 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1103 exprval_release(&exprval);
1107 return stack_push(ctx, &v);
1110 /* ECMA-262 3rd Edition 10.1.4 */
1111 static HRESULT interp_identid(exec_ctx_t *ctx)
1113 const BSTR arg = get_op_bstr(ctx, 0);
1114 const unsigned flags = get_op_uint(ctx, 1);
1118 TRACE("%s %x\n", debugstr_w(arg), flags);
1120 hres = identifier_eval(ctx->script, arg, &exprval);
1124 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1127 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1131 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1134 if(exprval.type != EXPRVAL_IDREF) {
1135 WARN("invalid ref\n");
1136 exprval_release(&exprval);
1137 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1140 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1143 /* ECMA-262 3rd Edition 7.8.1 */
1144 static HRESULT interp_null(exec_ctx_t *ctx)
1151 return stack_push(ctx, &v);
1154 /* ECMA-262 3rd Edition 7.8.2 */
1155 static HRESULT interp_bool(exec_ctx_t *ctx)
1157 const int arg = get_op_int(ctx, 0);
1159 TRACE("%s\n", arg ? "true" : "false");
1161 return stack_push_bool(ctx, arg);
1164 /* ECMA-262 3rd Edition 7.8.3 */
1165 static HRESULT interp_int(exec_ctx_t *ctx)
1167 const int arg = get_op_int(ctx, 0);
1174 return stack_push(ctx, &v);
1177 /* ECMA-262 3rd Edition 7.8.3 */
1178 static HRESULT interp_double(exec_ctx_t *ctx)
1180 const double arg = get_op_double(ctx);
1182 TRACE("%lf\n", arg);
1184 return stack_push_number(ctx, arg);
1187 /* ECMA-262 3rd Edition 7.8.4 */
1188 static HRESULT interp_str(exec_ctx_t *ctx)
1190 const WCHAR *str = get_op_str(ctx, 0);
1193 TRACE("%s\n", debugstr_w(str));
1196 V_BSTR(&v) = SysAllocString(str);
1198 return E_OUTOFMEMORY;
1200 return stack_push(ctx, &v);
1203 /* ECMA-262 3rd Edition 7.8 */
1204 static HRESULT interp_regexp(exec_ctx_t *ctx)
1206 const WCHAR *source = get_op_str(ctx, 0);
1207 const unsigned flags = get_op_uint(ctx, 1);
1212 TRACE("%s %x\n", debugstr_w(source), flags);
1214 hres = create_regexp(ctx->script, source, strlenW(source), flags, ®exp);
1218 var_set_jsdisp(&v, regexp);
1219 return stack_push(ctx, &v);
1222 /* ECMA-262 3rd Edition 11.1.4 */
1223 static HRESULT interp_carray(exec_ctx_t *ctx)
1225 const unsigned arg = get_op_uint(ctx, 0);
1233 hres = create_array(ctx->script, arg, &array);
1240 hres = jsdisp_propput_idx(array, i, v, ctx->ei);
1243 jsdisp_release(array);
1248 var_set_jsdisp(&r, array);
1249 return stack_push(ctx, &r);
1252 /* ECMA-262 3rd Edition 11.1.5 */
1253 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1261 hres = create_object(ctx->script, NULL, &obj);
1265 var_set_jsdisp(&v, obj);
1266 return stack_push(ctx, &v);
1269 /* ECMA-262 3rd Edition 11.1.5 */
1270 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1272 const BSTR name = get_op_bstr(ctx, 0);
1277 TRACE("%s\n", debugstr_w(name));
1281 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1282 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1284 hres = jsdisp_propput_name(obj, name, v, ctx->ei);
1289 /* ECMA-262 3rd Edition 11.11 */
1290 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1292 const unsigned arg = get_op_uint(ctx, 0);
1298 hres = to_boolean(stack_top(ctx), &b);
1311 /* ECMA-262 3rd Edition 11.11 */
1312 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1314 const unsigned arg = get_op_uint(ctx, 0);
1320 hres = to_boolean(stack_top(ctx), &b);
1333 /* ECMA-262 3rd Edition 11.10 */
1334 static HRESULT interp_or(exec_ctx_t *ctx)
1341 hres = stack_pop_int(ctx, &r);
1345 hres = stack_pop_int(ctx, &l);
1349 return stack_push_int(ctx, l|r);
1352 /* ECMA-262 3rd Edition 11.10 */
1353 static HRESULT interp_xor(exec_ctx_t *ctx)
1360 hres = stack_pop_int(ctx, &r);
1364 hres = stack_pop_int(ctx, &l);
1368 return stack_push_int(ctx, l^r);
1371 /* ECMA-262 3rd Edition 11.10 */
1372 static HRESULT interp_and(exec_ctx_t *ctx)
1379 hres = stack_pop_int(ctx, &r);
1383 hres = stack_pop_int(ctx, &l);
1387 return stack_push_int(ctx, l&r);
1390 /* ECMA-262 3rd Edition 11.8.6 */
1391 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1393 jsdisp_t *obj, *iter, *tmp = NULL;
1398 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1401 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1403 return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1406 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1407 IDispatch_Release(V_DISPATCH(v));
1409 FIXME("non-jsdisp objects not supported\n");
1413 if(is_class(obj, JSCLASS_FUNCTION)) {
1414 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1416 hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1418 jsdisp_release(obj);
1424 if(V_VT(&prot) == VT_DISPATCH) {
1425 if(V_VT(v) == VT_DISPATCH)
1426 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1427 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1428 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1434 jsdisp_release(tmp);
1436 FIXME("prototype is not an object\n");
1440 VariantClear(&prot);
1445 return stack_push_bool(ctx, ret);
1448 /* ECMA-262 3rd Edition 11.8.7 */
1449 static HRESULT interp_in(exec_ctx_t *ctx)
1459 obj = stack_pop(ctx);
1462 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1465 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1468 hres = to_string(ctx->script, v, ctx->ei, &str);
1471 IDispatch_Release(V_DISPATCH(obj));
1475 hres = disp_get_id(ctx->script, V_DISPATCH(obj), str, 0, &id);
1476 IDispatch_Release(V_DISPATCH(obj));
1480 else if(hres == DISP_E_UNKNOWNNAME)
1485 return stack_push_bool(ctx, ret);
1488 /* ECMA-262 3rd Edition 11.6.1 */
1489 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1494 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1498 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1504 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1505 BSTR lstr = NULL, rstr = NULL;
1507 if(V_VT(&l) == VT_BSTR)
1510 hres = to_string(ctx, &l, ei, &lstr);
1512 if(SUCCEEDED(hres)) {
1513 if(V_VT(&r) == VT_BSTR)
1516 hres = to_string(ctx, &r, ei, &rstr);
1519 if(SUCCEEDED(hres)) {
1522 len1 = SysStringLen(lstr);
1523 len2 = SysStringLen(rstr);
1525 V_VT(retv) = VT_BSTR;
1526 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1528 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1530 memcpy(V_BSTR(retv)+len1, rstr, len2*sizeof(WCHAR));
1531 V_BSTR(retv)[len1+len2] = 0;
1534 if(V_VT(&l) != VT_BSTR)
1535 SysFreeString(lstr);
1536 if(V_VT(&r) != VT_BSTR)
1537 SysFreeString(rstr);
1541 hres = to_number(ctx, &l, ei, &nl);
1542 if(SUCCEEDED(hres)) {
1543 hres = to_number(ctx, &r, ei, &nr);
1545 num_set_val(retv, nl + nr);
1554 /* ECMA-262 3rd Edition 11.6.1 */
1555 static HRESULT interp_add(exec_ctx_t *ctx)
1557 VARIANT *l, *r, ret;
1563 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1565 hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1571 return stack_push(ctx, &ret);
1574 /* ECMA-262 3rd Edition 11.6.2 */
1575 static HRESULT interp_sub(exec_ctx_t *ctx)
1582 hres = stack_pop_number(ctx, &r);
1586 hres = stack_pop_number(ctx, &l);
1590 return stack_push_number(ctx, l-r);
1593 /* ECMA-262 3rd Edition 11.5.1 */
1594 static HRESULT interp_mul(exec_ctx_t *ctx)
1601 hres = stack_pop_number(ctx, &r);
1605 hres = stack_pop_number(ctx, &l);
1609 return stack_push_number(ctx, l*r);
1612 /* ECMA-262 3rd Edition 11.5.2 */
1613 static HRESULT interp_div(exec_ctx_t *ctx)
1620 hres = stack_pop_number(ctx, &r);
1624 hres = stack_pop_number(ctx, &l);
1628 return stack_push_number(ctx, l/r);
1631 /* ECMA-262 3rd Edition 11.5.3 */
1632 static HRESULT interp_mod(exec_ctx_t *ctx)
1639 hres = stack_pop_number(ctx, &r);
1643 hres = stack_pop_number(ctx, &l);
1647 return stack_push_number(ctx, fmod(l, r));
1650 /* ECMA-262 3rd Edition 11.4.2 */
1651 static HRESULT interp_delete(exec_ctx_t *ctx)
1653 VARIANT *obj_var, *name_var;
1654 IDispatchEx *dispex;
1662 name_var = stack_pop(ctx);
1663 obj_var = stack_pop(ctx);
1665 hres = to_object(ctx->script, obj_var, &obj);
1666 VariantClear(obj_var);
1668 VariantClear(name_var);
1672 hres = to_string(ctx->script, name_var, ctx->ei, &name);
1673 VariantClear(name_var);
1675 IDispatch_Release(obj);
1679 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1680 if(SUCCEEDED(hres)) {
1681 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1683 IDispatchEx_Release(dispex);
1689 IDispatch_Release(obj);
1690 SysFreeString(name);
1694 return stack_push_bool(ctx, ret);
1697 /* ECMA-262 3rd Edition 11.4.2 */
1698 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1700 const BSTR arg = get_op_bstr(ctx, 0);
1701 IDispatchEx *dispex;
1706 TRACE("%s\n", debugstr_w(arg));
1708 hres = identifier_eval(ctx->script, arg, &exprval);
1712 if(exprval.type != EXPRVAL_IDREF) {
1713 FIXME("Unsupported exprval\n");
1714 exprval_release(&exprval);
1718 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1719 IDispatch_Release(exprval.u.idref.disp);
1720 if(SUCCEEDED(hres)) {
1721 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1722 IDispatchEx_Release(dispex);
1729 return stack_push_bool(ctx, ret);
1732 /* ECMA-262 3rd Edition 11.4.2 */
1733 static HRESULT interp_void(exec_ctx_t *ctx)
1741 V_VT(&v) = VT_EMPTY;
1742 return stack_push(ctx, &v);
1745 /* ECMA-262 3rd Edition 11.4.3 */
1746 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1768 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1769 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1770 jsdisp_release(dispex);
1777 FIXME("unhandled vt %d\n", V_VT(v));
1784 /* ECMA-262 3rd Edition 11.4.3 */
1785 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1793 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1797 obj = stack_pop_objid(ctx, &id);
1799 return stack_push_string(ctx, undefinedW);
1801 V_VT(&v) = VT_EMPTY;
1802 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1803 IDispatch_Release(obj);
1805 return stack_push_string(ctx, unknownW);
1807 hres = typeof_string(&v, &ret);
1812 return stack_push_string(ctx, ret);
1815 /* ECMA-262 3rd Edition 11.4.3 */
1816 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1818 const BSTR arg = get_op_bstr(ctx, 0);
1824 TRACE("%s\n", debugstr_w(arg));
1826 hres = identifier_eval(ctx->script, arg, &exprval);
1830 if(exprval.type == EXPRVAL_INVALID) {
1831 hres = stack_push_string(ctx, undefinedW);
1832 exprval_release(&exprval);
1836 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1837 exprval_release(&exprval);
1841 hres = typeof_string(&v, &ret);
1846 return stack_push_string(ctx, ret);
1849 /* ECMA-262 3rd Edition 11.4.3 */
1850 static HRESULT interp_typeof(exec_ctx_t *ctx)
1859 hres = typeof_string(v, &ret);
1864 return stack_push_string(ctx, ret);
1867 /* ECMA-262 3rd Edition 11.4.7 */
1868 static HRESULT interp_minus(exec_ctx_t *ctx)
1875 hres = stack_pop_number(ctx, &n);
1879 return stack_push_number(ctx, -n);
1882 /* ECMA-262 3rd Edition 11.4.6 */
1883 static HRESULT interp_tonum(exec_ctx_t *ctx)
1892 hres = to_number(ctx->script, v, ctx->ei, &n);
1897 return stack_push_number(ctx, n);
1900 /* ECMA-262 3rd Edition 11.3.1 */
1901 static HRESULT interp_postinc(exec_ctx_t *ctx)
1903 const int arg = get_op_int(ctx, 0);
1911 obj = stack_pop_objid(ctx, &id);
1913 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1915 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1916 if(SUCCEEDED(hres)) {
1920 hres = to_number(ctx->script, &v, ctx->ei, &n);
1921 if(SUCCEEDED(hres)) {
1922 num_set_val(&inc, n+(double)arg);
1923 hres = disp_propput(ctx->script, obj, id, &inc, ctx->ei);
1928 IDispatch_Release(obj);
1932 return stack_push(ctx, &v);
1935 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1936 static HRESULT interp_preinc(exec_ctx_t *ctx)
1938 const int arg = get_op_int(ctx, 0);
1946 obj = stack_pop_objid(ctx, &id);
1948 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1950 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1951 if(SUCCEEDED(hres)) {
1954 hres = to_number(ctx->script, &v, ctx->ei, &n);
1956 if(SUCCEEDED(hres)) {
1957 num_set_val(&v, n+(double)arg);
1958 hres = disp_propput(ctx->script, obj, id, &v, ctx->ei);
1961 IDispatch_Release(obj);
1965 return stack_push(ctx, &v);
1968 /* ECMA-262 3rd Edition 11.9.3 */
1969 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1971 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1972 return equal2_values(lval, rval, ret);
1974 /* FIXME: NULL disps should be handled in more general way */
1975 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1978 return equal_values(ctx, &v, rval, ei, ret);
1981 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1984 return equal_values(ctx, lval, &v, ei, ret);
1987 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1988 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1993 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1998 hres = to_number(ctx, lval, ei, &n);
2002 /* FIXME: optimize */
2005 return equal_values(ctx, &v, rval, ei, ret);
2008 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2013 hres = to_number(ctx, rval, ei, &n);
2017 /* FIXME: optimize */
2020 return equal_values(ctx, lval, &v, ei, ret);
2023 if(V_VT(rval) == VT_BOOL) {
2026 num_set_int(&v, V_BOOL(rval) ? 1 : 0);
2027 return equal_values(ctx, lval, &v, ei, ret);
2030 if(V_VT(lval) == VT_BOOL) {
2033 num_set_int(&v, V_BOOL(lval) ? 1 : 0);
2034 return equal_values(ctx, &v, rval, ei, ret);
2038 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2042 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2046 hres = equal_values(ctx, lval, &v, ei, ret);
2053 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2057 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2061 hres = equal_values(ctx, &v, rval, ei, ret);
2072 /* ECMA-262 3rd Edition 11.9.1 */
2073 static HRESULT interp_eq(exec_ctx_t *ctx)
2082 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2084 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2090 return stack_push_bool(ctx, b);
2093 /* ECMA-262 3rd Edition 11.9.2 */
2094 static HRESULT interp_neq(exec_ctx_t *ctx)
2103 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2105 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2111 return stack_push_bool(ctx, !b);
2114 /* ECMA-262 3rd Edition 11.9.4 */
2115 static HRESULT interp_eq2(exec_ctx_t *ctx)
2126 hres = equal2_values(r, l, &b);
2132 return stack_push_bool(ctx, b);
2135 /* ECMA-262 3rd Edition 11.9.5 */
2136 static HRESULT interp_neq2(exec_ctx_t *ctx)
2147 hres = equal2_values(r, l, &b);
2153 return stack_push_bool(ctx, !b);
2156 /* ECMA-262 3rd Edition 11.8.5 */
2157 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2163 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2167 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2173 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2174 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2175 SysFreeString(V_BSTR(&l));
2176 SysFreeString(V_BSTR(&r));
2180 hres = to_number(ctx, &l, ei, &ln);
2183 hres = to_number(ctx, &r, ei, &rn);
2188 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2192 /* ECMA-262 3rd Edition 11.8.1 */
2193 static HRESULT interp_lt(exec_ctx_t *ctx)
2202 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2204 hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2210 return stack_push_bool(ctx, b);
2213 /* ECMA-262 3rd Edition 11.8.1 */
2214 static HRESULT interp_lteq(exec_ctx_t *ctx)
2223 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2225 hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2231 return stack_push_bool(ctx, b);
2234 /* ECMA-262 3rd Edition 11.8.2 */
2235 static HRESULT interp_gt(exec_ctx_t *ctx)
2244 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2246 hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2252 return stack_push_bool(ctx, b);
2255 /* ECMA-262 3rd Edition 11.8.4 */
2256 static HRESULT interp_gteq(exec_ctx_t *ctx)
2265 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2267 hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2273 return stack_push_bool(ctx, b);
2276 /* ECMA-262 3rd Edition 11.4.8 */
2277 static HRESULT interp_bneg(exec_ctx_t *ctx)
2286 hres = to_int32(ctx->script, v, ctx->ei, &i);
2291 return stack_push_int(ctx, ~i);
2294 /* ECMA-262 3rd Edition 11.4.9 */
2295 static HRESULT interp_neg(exec_ctx_t *ctx)
2304 hres = to_boolean(v, &b);
2309 return stack_push_bool(ctx, !b);
2312 /* ECMA-262 3rd Edition 11.7.1 */
2313 static HRESULT interp_lshift(exec_ctx_t *ctx)
2319 hres = stack_pop_uint(ctx, &r);
2323 hres = stack_pop_int(ctx, &l);
2327 return stack_push_int(ctx, l << (r&0x1f));
2330 /* ECMA-262 3rd Edition 11.7.2 */
2331 static HRESULT interp_rshift(exec_ctx_t *ctx)
2337 hres = stack_pop_uint(ctx, &r);
2341 hres = stack_pop_int(ctx, &l);
2345 return stack_push_int(ctx, l >> (r&0x1f));
2348 /* ECMA-262 3rd Edition 11.7.3 */
2349 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2354 hres = stack_pop_uint(ctx, &r);
2358 hres = stack_pop_uint(ctx, &l);
2362 return stack_push_int(ctx, l >> (r&0x1f));
2365 /* ECMA-262 3rd Edition 11.13.1 */
2366 static HRESULT interp_assign(exec_ctx_t *ctx)
2376 disp = stack_pop_objid(ctx, &id);
2379 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2381 hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2382 IDispatch_Release(disp);
2388 return stack_push(ctx, v);
2391 /* JScript extension */
2392 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2394 const unsigned arg = get_op_uint(ctx, 0);
2402 disp = stack_topn_objid(ctx, arg+1, &id);
2404 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2406 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, arg+1, stack_args(ctx,arg+1), NULL, ctx->ei);
2411 stack_popn(ctx, arg+2);
2412 return stack_push(ctx, v);
2415 static HRESULT interp_undefined(exec_ctx_t *ctx)
2421 V_VT(&v) = VT_EMPTY;
2422 return stack_push(ctx, &v);
2425 static HRESULT interp_jmp(exec_ctx_t *ctx)
2427 const unsigned arg = get_op_uint(ctx, 0);
2435 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2437 const unsigned arg = get_op_uint(ctx, 0);
2445 hres = to_boolean(v, &b);
2457 static HRESULT interp_pop(exec_ctx_t *ctx)
2465 static HRESULT interp_ret(exec_ctx_t *ctx)
2473 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2475 static const op_func_t op_funcs[] = {
2476 #define X(x,a,b,c) interp_##x,
2481 static const unsigned op_move[] = {
2482 #define X(a,x,b,c) x,
2487 static HRESULT unwind_exception(exec_ctx_t *ctx)
2489 except_frame_t *except_frame;
2494 except_frame = ctx->except_frame;
2495 ctx->except_frame = except_frame->next;
2497 assert(except_frame->stack_top <= ctx->top);
2498 stack_popn(ctx, ctx->top - except_frame->stack_top);
2500 while(except_frame->scope != ctx->scope_chain)
2501 scope_pop(&ctx->scope_chain);
2503 ctx->ip = except_frame->catch_off;
2505 except_val = ctx->ei->var;
2506 memset(ctx->ei, 0, sizeof(*ctx->ei));
2508 ident = except_frame->ident;
2509 heap_free(except_frame);
2512 jsdisp_t *scope_obj;
2514 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2515 if(SUCCEEDED(hres)) {
2516 hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei);
2518 jsdisp_release(scope_obj);
2520 VariantClear(&except_val);
2524 hres = scope_push(ctx->scope_chain, scope_obj, to_disp(scope_obj), &ctx->scope_chain);
2525 jsdisp_release(scope_obj);
2529 hres = stack_push(ctx, &except_val);
2533 hres = stack_push_bool(ctx, FALSE);
2537 V_VT(&v) = VT_EMPTY;
2538 hres = stack_push(ctx, &v);
2544 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsexcept_t *ei, VARIANT *ret)
2546 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2547 except_frame_t *prev_except_frame;
2548 function_code_t *prev_func;
2549 unsigned prev_ip, prev_top;
2550 scope_chain_t *prev_scope;
2551 bytecode_t *prev_code;
2552 jsexcept_t *prev_ei;
2554 HRESULT hres = S_OK;
2558 prev_top = exec_ctx->top;
2559 prev_scope = exec_ctx->scope_chain;
2560 prev_except_frame = exec_ctx->except_frame;
2561 prev_ip = exec_ctx->ip;
2562 prev_ei = exec_ctx->ei;
2563 prev_code = exec_ctx->code;
2564 prev_func = exec_ctx->func_code;
2565 exec_ctx->ip = func->instr_off;
2567 exec_ctx->except_frame = NULL;
2568 exec_ctx->code = code;
2569 exec_ctx->func_code = func;
2571 while(exec_ctx->ip != -1) {
2572 op = code->instrs[exec_ctx->ip].op;
2573 hres = op_funcs[op](exec_ctx);
2575 TRACE("EXCEPTION\n");
2577 if(!exec_ctx->except_frame)
2580 hres = unwind_exception(exec_ctx);
2584 exec_ctx->ip += op_move[op];
2588 exec_ctx->ip = prev_ip;
2589 exec_ctx->ei = prev_ei;
2590 exec_ctx->except_frame = prev_except_frame;
2591 exec_ctx->code = prev_code;
2592 exec_ctx->func_code = prev_func;
2595 while(exec_ctx->scope_chain != prev_scope)
2596 scope_pop(&exec_ctx->scope_chain);
2597 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2601 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2602 assert(exec_ctx->scope_chain == prev_scope);
2604 if(exec_ctx->top == prev_top)
2605 V_VT(ret) = VT_EMPTY;
2607 *ret = *stack_pop(exec_ctx);
2611 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval,
2612 jsexcept_t *ei, VARIANT *retv)
2614 exec_ctx_t *prev_ctx;
2617 HRESULT hres = S_OK;
2619 for(i = 0; i < func->func_cnt; i++) {
2623 if(!func->funcs[i].name)
2626 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2630 var_set_jsdisp(&var, func_obj);
2631 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, &var, ei);
2632 jsdisp_release(func_obj);
2637 for(i=0; i < func->var_cnt; i++) {
2638 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2641 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2647 prev_ctx = ctx->script->exec_ctx;
2648 ctx->script->exec_ctx = ctx;
2650 hres = enter_bytecode(ctx->script, code, func, ei, &val);
2651 assert(ctx->script->exec_ctx == ctx);
2652 ctx->script->exec_ctx = prev_ctx;