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 *obj, scope_chain_t **ret)
273 scope_chain_t *new_scope;
275 new_scope = heap_alloc(sizeof(scope_chain_t));
277 return E_OUTOFMEMORY;
282 new_scope->obj = obj;
286 new_scope->next = scope;
288 new_scope->next = NULL;
295 static void scope_pop(scope_chain_t **scope)
304 void scope_release(scope_chain_t *scope)
310 scope_release(scope->next);
312 jsdisp_release(scope->obj);
316 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
317 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
321 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
323 return E_OUTOFMEMORY;
326 ctx->is_global = is_global;
329 ctx->this_obj = this_obj;
330 else if(script_ctx->host_global)
331 ctx->this_obj = script_ctx->host_global;
333 ctx->this_obj = to_disp(script_ctx->global);
334 IDispatch_AddRef(ctx->this_obj);
336 jsdisp_addref(var_disp);
337 ctx->var_disp = var_disp;
339 script_addref(script_ctx);
340 ctx->script = script_ctx;
344 ctx->scope_chain = scope;
351 void exec_release(exec_ctx_t *ctx)
357 scope_release(ctx->scope_chain);
359 jsdisp_release(ctx->var_disp);
361 IDispatch_Release(ctx->this_obj);
363 script_release(ctx->script);
364 heap_free(ctx->stack);
368 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
373 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
375 TRACE("using IDispatch\n");
378 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
382 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
383 IDispatchEx_Release(dispex);
387 static inline BOOL is_null(const VARIANT *v)
389 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
392 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
394 IObjectIdentity *identity;
395 IUnknown *unk1, *unk2;
403 if(!disp1 || !disp2) {
408 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
412 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
414 IUnknown_Release(unk1);
421 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
422 if(SUCCEEDED(hres)) {
423 hres = IObjectIdentity_IsEqualObject(identity, unk2);
424 IObjectIdentity_Release(identity);
431 IUnknown_Release(unk1);
432 IUnknown_Release(unk2);
436 /* ECMA-262 3rd Edition 11.9.6 */
437 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
441 if(V_VT(lval) != V_VT(rval)) {
442 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
443 *ret = num_val(lval) == num_val(rval);
444 else if(is_null(lval))
445 *ret = is_null(rval);
457 *ret = V_I4(lval) == V_I4(rval);
460 *ret = V_R8(lval) == V_R8(rval);
464 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
465 else if(!V_BSTR(rval))
466 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
468 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
471 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
473 *ret = !V_BOOL(lval) == !V_BOOL(rval);
476 FIXME("unimplemented vt %d\n", V_VT(lval));
483 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
489 for(item = ctx->named_items; item; item = item->next) {
490 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
491 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
492 if(SUCCEEDED(hres)) {
494 exprval_set_idref(ret, item->disp, id);
503 /* ECMA-262 3rd Edition 10.1.4 */
504 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
506 scope_chain_t *scope;
511 TRACE("%s\n", debugstr_w(identifier));
513 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
514 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
515 if(SUCCEEDED(hres)) {
516 exprval_set_idref(ret, to_disp(scope->obj), id);
521 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
522 if(SUCCEEDED(hres)) {
523 exprval_set_idref(ret, to_disp(ctx->global), id);
527 for(item = ctx->named_items; item; item = item->next) {
528 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
535 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
536 SCRIPTINFO_IUNKNOWN, &unk, NULL);
538 WARN("GetItemInfo failed: %08x\n", hres);
542 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
543 IUnknown_Release(unk);
545 WARN("object does not implement IDispatch\n");
550 ret->type = EXPRVAL_VARIANT;
551 V_VT(&ret->u.var) = VT_DISPATCH;
552 V_DISPATCH(&ret->u.var) = item->disp;
553 IDispatch_AddRef(item->disp);
558 if(lookup_global_members(ctx, identifier, ret))
561 ret->type = EXPRVAL_INVALID;
565 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
566 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
569 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
570 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
573 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
574 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
577 static inline const WCHAR *get_op_str(exec_ctx_t *ctx, int i){
578 return ctx->code->instrs[ctx->ip].u.arg[i].str;
581 static inline double get_op_double(exec_ctx_t *ctx){
582 return ctx->code->instrs[ctx->ip].u.dbl;
585 /* ECMA-262 3rd Edition 12.2 */
586 static HRESULT interp_var_set(exec_ctx_t *ctx)
588 const BSTR name = get_op_bstr(ctx, 0);
592 TRACE("%s\n", debugstr_w(name));
595 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei);
600 /* ECMA-262 3rd Edition 12.6.4 */
601 static HRESULT interp_forin(exec_ctx_t *ctx)
603 const HRESULT arg = get_op_uint(ctx, 0);
604 IDispatch *var_obj, *obj = NULL;
613 val = stack_pop(ctx);
615 assert(V_VT(stack_top(ctx)) == VT_I4);
616 id = V_I4(stack_top(ctx));
618 var_obj = stack_topn_objid(ctx, 1, &var_id);
620 FIXME("invalid ref\n");
625 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
626 obj = V_DISPATCH(stack_topn(ctx, 3));
629 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
630 if(SUCCEEDED(hres)) {
631 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
633 hres = IDispatchEx_GetMemberName(dispex, id, &name);
634 IDispatchEx_Release(dispex);
640 TRACE("No IDispatchEx\n");
649 V_I4(stack_top(ctx)) = id;
653 hres = disp_propput(ctx->script, var_obj, var_id, &v, ctx->ei);
662 return stack_push(ctx, val);
667 /* ECMA-262 3rd Edition 12.10 */
668 static HRESULT interp_push_scope(exec_ctx_t *ctx)
678 hres = to_object(ctx->script, v, &disp);
683 obj = to_jsdisp(disp);
685 IDispatch_Release(disp);
686 FIXME("disp is not jsdisp\n");
690 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
695 /* ECMA-262 3rd Edition 12.10 */
696 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
700 scope_pop(&ctx->scope_chain);
704 /* ECMA-262 3rd Edition 12.13 */
705 static HRESULT interp_case(exec_ctx_t *ctx)
707 const unsigned arg = get_op_uint(ctx, 0);
715 hres = equal2_values(stack_top(ctx), v, &b);
729 /* ECMA-262 3rd Edition 12.13 */
730 static HRESULT interp_throw(exec_ctx_t *ctx)
734 ctx->ei->var = *stack_pop(ctx);
735 return DISP_E_EXCEPTION;
738 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
740 const HRESULT arg = get_op_uint(ctx, 0);
742 TRACE("%08x\n", arg);
744 return throw_reference_error(ctx->script, ctx->ei, arg, NULL);
747 static HRESULT interp_throw_type(exec_ctx_t *ctx)
749 const HRESULT hres = get_op_uint(ctx, 0);
750 const WCHAR *str = get_op_str(ctx, 1);
752 TRACE("%08x %s\n", hres, debugstr_w(str));
754 return throw_type_error(ctx->script, ctx->ei, hres, str);
757 /* ECMA-262 3rd Edition 12.14 */
758 static HRESULT interp_push_except(exec_ctx_t *ctx)
760 const unsigned arg1 = get_op_uint(ctx, 0);
761 const BSTR arg2 = get_op_bstr(ctx, 1);
762 except_frame_t *except;
767 stack_top = ctx->top;
772 hres = stack_push_bool(ctx, TRUE);
775 hres = stack_push_bool(ctx, TRUE);
780 except = heap_alloc(sizeof(*except));
782 return E_OUTOFMEMORY;
784 except->stack_top = stack_top;
785 except->scope = ctx->scope_chain;
786 except->catch_off = arg1;
787 except->ident = arg2;
788 except->next = ctx->except_frame;
789 ctx->except_frame = except;
793 /* ECMA-262 3rd Edition 12.14 */
794 static HRESULT interp_pop_except(exec_ctx_t *ctx)
796 except_frame_t *except;
800 except = ctx->except_frame;
801 assert(except != NULL);
803 ctx->except_frame = except->next;
808 /* ECMA-262 3rd Edition 12.14 */
809 static HRESULT interp_end_finally(exec_ctx_t *ctx)
817 assert(V_VT(stack_top(ctx)) == VT_BOOL);
818 if(!V_BOOL(stack_top(ctx))) {
819 TRACE("passing exception\n");
823 ctx->ei->var = *stack_pop(ctx);
824 return DISP_E_EXCEPTION;
828 return stack_push(ctx, v);
831 /* ECMA-262 3rd Edition 13 */
832 static HRESULT interp_func(exec_ctx_t *ctx)
834 unsigned func_idx = get_op_uint(ctx, 0);
839 TRACE("%d\n", func_idx);
841 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
842 ctx->scope_chain, &dispex);
846 var_set_jsdisp(&v, dispex);
847 return stack_push(ctx, &v);
850 /* ECMA-262 3rd Edition 11.2.1 */
851 static HRESULT interp_array(exec_ctx_t *ctx)
861 namev = stack_pop(ctx);
863 hres = stack_pop_object(ctx, &obj);
869 hres = to_string(ctx->script, namev, ctx->ei, &name);
872 IDispatch_Release(obj);
876 hres = disp_get_id(ctx->script, obj, name, 0, &id);
878 if(SUCCEEDED(hres)) {
879 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
880 }else if(hres == DISP_E_UNKNOWNNAME) {
884 IDispatch_Release(obj);
888 return stack_push(ctx, &v);
891 /* ECMA-262 3rd Edition 11.2.1 */
892 static HRESULT interp_member(exec_ctx_t *ctx)
894 const BSTR arg = get_op_bstr(ctx, 0);
902 hres = stack_pop_object(ctx, &obj);
906 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
907 if(SUCCEEDED(hres)) {
909 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
910 }else if(hres == DISP_E_UNKNOWNNAME) {
914 IDispatch_Release(obj);
918 return stack_push(ctx, &v);
921 /* ECMA-262 3rd Edition 11.2.1 */
922 static HRESULT interp_memberid(exec_ctx_t *ctx)
924 const unsigned arg = get_op_uint(ctx, 0);
925 VARIANT *objv, *namev;
933 namev = stack_pop(ctx);
934 objv = stack_pop(ctx);
936 hres = to_object(ctx->script, objv, &obj);
938 if(SUCCEEDED(hres)) {
939 hres = to_string(ctx->script, namev, ctx->ei, &name);
941 IDispatch_Release(obj);
947 hres = disp_get_id(ctx->script, obj, name, arg, &id);
950 IDispatch_Release(obj);
951 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
953 id = JS_E_INVALID_PROPERTY;
959 return stack_push_objid(ctx, obj, id);
962 /* ECMA-262 3rd Edition 11.2.1 */
963 static HRESULT interp_refval(exec_ctx_t *ctx)
972 disp = stack_topn_objid(ctx, 0, &id);
974 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
976 hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
980 return stack_push(ctx, &v);
983 /* ECMA-262 3rd Edition 11.2.2 */
984 static HRESULT interp_new(exec_ctx_t *ctx)
986 const unsigned arg = get_op_uint(ctx, 0);
992 constr = stack_topn(ctx, arg);
994 /* NOTE: Should use to_object here */
996 if(V_VT(constr) == VT_NULL)
997 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
998 else if(V_VT(constr) != VT_DISPATCH)
999 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
1000 else if(!V_DISPATCH(constr))
1001 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1003 hres = disp_call_value(ctx->script, V_DISPATCH(constr), NULL, DISPATCH_CONSTRUCT, arg, stack_args(ctx, arg), &v, ctx->ei);
1007 stack_popn(ctx, arg+1);
1008 return stack_push(ctx, &v);
1011 /* ECMA-262 3rd Edition 11.2.3 */
1012 static HRESULT interp_call(exec_ctx_t *ctx)
1014 const unsigned argn = get_op_uint(ctx, 0);
1015 const int do_ret = get_op_int(ctx, 1);
1019 TRACE("%d %d\n", argn, do_ret);
1021 objv = stack_topn(ctx, argn);
1022 if(V_VT(objv) != VT_DISPATCH)
1023 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1025 hres = disp_call_value(ctx->script, V_DISPATCH(objv), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
1026 do_ret ? &v : NULL, ctx->ei);
1030 stack_popn(ctx, argn+1);
1031 return do_ret ? stack_push(ctx, &v) : S_OK;
1035 /* ECMA-262 3rd Edition 11.2.3 */
1036 static HRESULT interp_call_member(exec_ctx_t *ctx)
1038 const unsigned argn = get_op_uint(ctx, 0);
1039 const int do_ret = get_op_int(ctx, 1);
1045 TRACE("%d %d\n", argn, do_ret);
1047 obj = stack_topn_objid(ctx, argn, &id);
1049 return throw_type_error(ctx->script, ctx->ei, id, NULL);
1051 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &v : NULL, ctx->ei);
1055 stack_popn(ctx, argn+2);
1056 return do_ret ? stack_push(ctx, &v) : S_OK;
1060 /* ECMA-262 3rd Edition 11.1.1 */
1061 static HRESULT interp_this(exec_ctx_t *ctx)
1067 V_VT(&v) = VT_DISPATCH;
1068 V_DISPATCH(&v) = ctx->this_obj;
1069 IDispatch_AddRef(ctx->this_obj);
1070 return stack_push(ctx, &v);
1073 /* ECMA-262 3rd Edition 10.1.4 */
1074 static HRESULT interp_ident(exec_ctx_t *ctx)
1076 const BSTR arg = get_op_bstr(ctx, 0);
1081 TRACE("%s\n", debugstr_w(arg));
1083 hres = identifier_eval(ctx->script, arg, &exprval);
1087 if(exprval.type == EXPRVAL_INVALID)
1088 return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1090 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1091 exprval_release(&exprval);
1095 return stack_push(ctx, &v);
1098 /* ECMA-262 3rd Edition 10.1.4 */
1099 static HRESULT interp_identid(exec_ctx_t *ctx)
1101 const BSTR arg = get_op_bstr(ctx, 0);
1102 const unsigned flags = get_op_uint(ctx, 1);
1106 TRACE("%s %x\n", debugstr_w(arg), flags);
1108 hres = identifier_eval(ctx->script, arg, &exprval);
1112 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1115 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1119 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1122 if(exprval.type != EXPRVAL_IDREF) {
1123 WARN("invalid ref\n");
1124 exprval_release(&exprval);
1125 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1128 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1131 /* ECMA-262 3rd Edition 7.8.1 */
1132 static HRESULT interp_null(exec_ctx_t *ctx)
1139 return stack_push(ctx, &v);
1142 /* ECMA-262 3rd Edition 7.8.2 */
1143 static HRESULT interp_bool(exec_ctx_t *ctx)
1145 const int arg = get_op_int(ctx, 0);
1147 TRACE("%s\n", arg ? "true" : "false");
1149 return stack_push_bool(ctx, arg);
1152 /* ECMA-262 3rd Edition 7.8.3 */
1153 static HRESULT interp_int(exec_ctx_t *ctx)
1155 const int arg = get_op_int(ctx, 0);
1162 return stack_push(ctx, &v);
1165 /* ECMA-262 3rd Edition 7.8.3 */
1166 static HRESULT interp_double(exec_ctx_t *ctx)
1168 const double arg = get_op_double(ctx);
1170 TRACE("%lf\n", arg);
1172 return stack_push_number(ctx, arg);
1175 /* ECMA-262 3rd Edition 7.8.4 */
1176 static HRESULT interp_str(exec_ctx_t *ctx)
1178 const WCHAR *str = get_op_str(ctx, 0);
1181 TRACE("%s\n", debugstr_w(str));
1184 V_BSTR(&v) = SysAllocString(str);
1186 return E_OUTOFMEMORY;
1188 return stack_push(ctx, &v);
1191 /* ECMA-262 3rd Edition 7.8 */
1192 static HRESULT interp_regexp(exec_ctx_t *ctx)
1194 const WCHAR *source = get_op_str(ctx, 0);
1195 const unsigned flags = get_op_uint(ctx, 1);
1200 TRACE("%s %x\n", debugstr_w(source), flags);
1202 hres = create_regexp(ctx->script, source, strlenW(source), flags, ®exp);
1206 var_set_jsdisp(&v, regexp);
1207 return stack_push(ctx, &v);
1210 /* ECMA-262 3rd Edition 11.1.4 */
1211 static HRESULT interp_carray(exec_ctx_t *ctx)
1213 const unsigned arg = get_op_uint(ctx, 0);
1221 hres = create_array(ctx->script, arg, &array);
1228 hres = jsdisp_propput_idx(array, i, v, ctx->ei);
1231 jsdisp_release(array);
1236 var_set_jsdisp(&r, array);
1237 return stack_push(ctx, &r);
1240 /* ECMA-262 3rd Edition 11.1.5 */
1241 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1249 hres = create_object(ctx->script, NULL, &obj);
1253 var_set_jsdisp(&v, obj);
1254 return stack_push(ctx, &v);
1257 /* ECMA-262 3rd Edition 11.1.5 */
1258 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1260 const BSTR name = get_op_bstr(ctx, 0);
1265 TRACE("%s\n", debugstr_w(name));
1269 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1270 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1272 hres = jsdisp_propput_name(obj, name, v, ctx->ei);
1277 /* ECMA-262 3rd Edition 11.11 */
1278 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1280 const unsigned arg = get_op_uint(ctx, 0);
1286 hres = to_boolean(stack_top(ctx), &b);
1299 /* ECMA-262 3rd Edition 11.11 */
1300 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1302 const unsigned arg = get_op_uint(ctx, 0);
1308 hres = to_boolean(stack_top(ctx), &b);
1321 /* ECMA-262 3rd Edition 11.10 */
1322 static HRESULT interp_or(exec_ctx_t *ctx)
1329 hres = stack_pop_int(ctx, &r);
1333 hres = stack_pop_int(ctx, &l);
1337 return stack_push_int(ctx, l|r);
1340 /* ECMA-262 3rd Edition 11.10 */
1341 static HRESULT interp_xor(exec_ctx_t *ctx)
1348 hres = stack_pop_int(ctx, &r);
1352 hres = stack_pop_int(ctx, &l);
1356 return stack_push_int(ctx, l^r);
1359 /* ECMA-262 3rd Edition 11.10 */
1360 static HRESULT interp_and(exec_ctx_t *ctx)
1367 hres = stack_pop_int(ctx, &r);
1371 hres = stack_pop_int(ctx, &l);
1375 return stack_push_int(ctx, l&r);
1378 /* ECMA-262 3rd Edition 11.8.6 */
1379 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1381 jsdisp_t *obj, *iter, *tmp = NULL;
1386 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1389 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1391 return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1394 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1395 IDispatch_Release(V_DISPATCH(v));
1397 FIXME("non-jsdisp objects not supported\n");
1401 if(is_class(obj, JSCLASS_FUNCTION)) {
1402 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1404 hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1406 jsdisp_release(obj);
1412 if(V_VT(&prot) == VT_DISPATCH) {
1413 if(V_VT(v) == VT_DISPATCH)
1414 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1415 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1416 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1422 jsdisp_release(tmp);
1424 FIXME("prototype is not an object\n");
1428 VariantClear(&prot);
1433 return stack_push_bool(ctx, ret);
1436 /* ECMA-262 3rd Edition 11.8.7 */
1437 static HRESULT interp_in(exec_ctx_t *ctx)
1447 obj = stack_pop(ctx);
1450 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1453 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1456 hres = to_string(ctx->script, v, ctx->ei, &str);
1459 IDispatch_Release(V_DISPATCH(obj));
1463 hres = disp_get_id(ctx->script, V_DISPATCH(obj), str, 0, &id);
1464 IDispatch_Release(V_DISPATCH(obj));
1468 else if(hres == DISP_E_UNKNOWNNAME)
1473 return stack_push_bool(ctx, ret);
1476 /* ECMA-262 3rd Edition 11.6.1 */
1477 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1482 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1486 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1492 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1493 BSTR lstr = NULL, rstr = NULL;
1495 if(V_VT(&l) == VT_BSTR)
1498 hres = to_string(ctx, &l, ei, &lstr);
1500 if(SUCCEEDED(hres)) {
1501 if(V_VT(&r) == VT_BSTR)
1504 hres = to_string(ctx, &r, ei, &rstr);
1507 if(SUCCEEDED(hres)) {
1510 len1 = SysStringLen(lstr);
1511 len2 = SysStringLen(rstr);
1513 V_VT(retv) = VT_BSTR;
1514 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1516 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1518 memcpy(V_BSTR(retv)+len1, rstr, len2*sizeof(WCHAR));
1519 V_BSTR(retv)[len1+len2] = 0;
1522 if(V_VT(&l) != VT_BSTR)
1523 SysFreeString(lstr);
1524 if(V_VT(&r) != VT_BSTR)
1525 SysFreeString(rstr);
1529 hres = to_number(ctx, &l, ei, &nl);
1530 if(SUCCEEDED(hres)) {
1531 hres = to_number(ctx, &r, ei, &nr);
1533 num_set_val(retv, nl + nr);
1542 /* ECMA-262 3rd Edition 11.6.1 */
1543 static HRESULT interp_add(exec_ctx_t *ctx)
1545 VARIANT *l, *r, ret;
1551 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1553 hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1559 return stack_push(ctx, &ret);
1562 /* ECMA-262 3rd Edition 11.6.2 */
1563 static HRESULT interp_sub(exec_ctx_t *ctx)
1570 hres = stack_pop_number(ctx, &r);
1574 hres = stack_pop_number(ctx, &l);
1578 return stack_push_number(ctx, l-r);
1581 /* ECMA-262 3rd Edition 11.5.1 */
1582 static HRESULT interp_mul(exec_ctx_t *ctx)
1589 hres = stack_pop_number(ctx, &r);
1593 hres = stack_pop_number(ctx, &l);
1597 return stack_push_number(ctx, l*r);
1600 /* ECMA-262 3rd Edition 11.5.2 */
1601 static HRESULT interp_div(exec_ctx_t *ctx)
1608 hres = stack_pop_number(ctx, &r);
1612 hres = stack_pop_number(ctx, &l);
1616 return stack_push_number(ctx, l/r);
1619 /* ECMA-262 3rd Edition 11.5.3 */
1620 static HRESULT interp_mod(exec_ctx_t *ctx)
1627 hres = stack_pop_number(ctx, &r);
1631 hres = stack_pop_number(ctx, &l);
1635 return stack_push_number(ctx, fmod(l, r));
1638 /* ECMA-262 3rd Edition 11.4.2 */
1639 static HRESULT interp_delete(exec_ctx_t *ctx)
1641 VARIANT *obj_var, *name_var;
1642 IDispatchEx *dispex;
1650 name_var = stack_pop(ctx);
1651 obj_var = stack_pop(ctx);
1653 hres = to_object(ctx->script, obj_var, &obj);
1654 VariantClear(obj_var);
1656 VariantClear(name_var);
1660 hres = to_string(ctx->script, name_var, ctx->ei, &name);
1661 VariantClear(name_var);
1663 IDispatch_Release(obj);
1667 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1668 if(SUCCEEDED(hres)) {
1669 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1671 IDispatchEx_Release(dispex);
1677 IDispatch_Release(obj);
1678 SysFreeString(name);
1682 return stack_push_bool(ctx, ret);
1685 /* ECMA-262 3rd Edition 11.4.2 */
1686 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1688 const BSTR arg = get_op_bstr(ctx, 0);
1689 IDispatchEx *dispex;
1694 TRACE("%s\n", debugstr_w(arg));
1696 hres = identifier_eval(ctx->script, arg, &exprval);
1700 if(exprval.type != EXPRVAL_IDREF) {
1701 FIXME("Unsupported exprval\n");
1702 exprval_release(&exprval);
1706 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1707 IDispatch_Release(exprval.u.idref.disp);
1708 if(SUCCEEDED(hres)) {
1709 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1710 IDispatchEx_Release(dispex);
1717 return stack_push_bool(ctx, ret);
1720 /* ECMA-262 3rd Edition 11.4.2 */
1721 static HRESULT interp_void(exec_ctx_t *ctx)
1729 V_VT(&v) = VT_EMPTY;
1730 return stack_push(ctx, &v);
1733 /* ECMA-262 3rd Edition 11.4.3 */
1734 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1756 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1757 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1758 jsdisp_release(dispex);
1765 FIXME("unhandled vt %d\n", V_VT(v));
1772 /* ECMA-262 3rd Edition 11.4.3 */
1773 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1781 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1785 obj = stack_pop_objid(ctx, &id);
1787 return stack_push_string(ctx, undefinedW);
1789 V_VT(&v) = VT_EMPTY;
1790 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1791 IDispatch_Release(obj);
1793 return stack_push_string(ctx, unknownW);
1795 hres = typeof_string(&v, &ret);
1800 return stack_push_string(ctx, ret);
1803 /* ECMA-262 3rd Edition 11.4.3 */
1804 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1806 const BSTR arg = get_op_bstr(ctx, 0);
1812 TRACE("%s\n", debugstr_w(arg));
1814 hres = identifier_eval(ctx->script, arg, &exprval);
1818 if(exprval.type == EXPRVAL_INVALID) {
1819 hres = stack_push_string(ctx, undefinedW);
1820 exprval_release(&exprval);
1824 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1825 exprval_release(&exprval);
1829 hres = typeof_string(&v, &ret);
1834 return stack_push_string(ctx, ret);
1837 /* ECMA-262 3rd Edition 11.4.3 */
1838 static HRESULT interp_typeof(exec_ctx_t *ctx)
1847 hres = typeof_string(v, &ret);
1852 return stack_push_string(ctx, ret);
1855 /* ECMA-262 3rd Edition 11.4.7 */
1856 static HRESULT interp_minus(exec_ctx_t *ctx)
1863 hres = stack_pop_number(ctx, &n);
1867 return stack_push_number(ctx, -n);
1870 /* ECMA-262 3rd Edition 11.4.6 */
1871 static HRESULT interp_tonum(exec_ctx_t *ctx)
1880 hres = to_number(ctx->script, v, ctx->ei, &n);
1885 return stack_push_number(ctx, n);
1888 /* ECMA-262 3rd Edition 11.3.1 */
1889 static HRESULT interp_postinc(exec_ctx_t *ctx)
1891 const int arg = get_op_int(ctx, 0);
1899 obj = stack_pop_objid(ctx, &id);
1901 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1903 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1904 if(SUCCEEDED(hres)) {
1908 hres = to_number(ctx->script, &v, ctx->ei, &n);
1909 if(SUCCEEDED(hres)) {
1910 num_set_val(&inc, n+(double)arg);
1911 hres = disp_propput(ctx->script, obj, id, &inc, ctx->ei);
1916 IDispatch_Release(obj);
1920 return stack_push(ctx, &v);
1923 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1924 static HRESULT interp_preinc(exec_ctx_t *ctx)
1926 const int arg = get_op_int(ctx, 0);
1934 obj = stack_pop_objid(ctx, &id);
1936 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1938 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1939 if(SUCCEEDED(hres)) {
1942 hres = to_number(ctx->script, &v, ctx->ei, &n);
1944 if(SUCCEEDED(hres)) {
1945 num_set_val(&v, n+(double)arg);
1946 hres = disp_propput(ctx->script, obj, id, &v, ctx->ei);
1949 IDispatch_Release(obj);
1953 return stack_push(ctx, &v);
1956 /* ECMA-262 3rd Edition 11.9.3 */
1957 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1959 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1960 return equal2_values(lval, rval, ret);
1962 /* FIXME: NULL disps should be handled in more general way */
1963 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1966 return equal_values(ctx, &v, rval, ei, ret);
1969 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1972 return equal_values(ctx, lval, &v, ei, ret);
1975 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1976 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1981 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1986 hres = to_number(ctx, lval, ei, &n);
1990 /* FIXME: optimize */
1993 return equal_values(ctx, &v, rval, ei, ret);
1996 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2001 hres = to_number(ctx, rval, ei, &n);
2005 /* FIXME: optimize */
2008 return equal_values(ctx, lval, &v, ei, ret);
2011 if(V_VT(rval) == VT_BOOL) {
2014 num_set_int(&v, V_BOOL(rval) ? 1 : 0);
2015 return equal_values(ctx, lval, &v, ei, ret);
2018 if(V_VT(lval) == VT_BOOL) {
2021 num_set_int(&v, V_BOOL(lval) ? 1 : 0);
2022 return equal_values(ctx, &v, rval, ei, ret);
2026 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2030 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2034 hres = equal_values(ctx, lval, &v, ei, ret);
2041 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2045 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2049 hres = equal_values(ctx, &v, rval, ei, ret);
2060 /* ECMA-262 3rd Edition 11.9.1 */
2061 static HRESULT interp_eq(exec_ctx_t *ctx)
2070 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2072 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2078 return stack_push_bool(ctx, b);
2081 /* ECMA-262 3rd Edition 11.9.2 */
2082 static HRESULT interp_neq(exec_ctx_t *ctx)
2091 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2093 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2099 return stack_push_bool(ctx, !b);
2102 /* ECMA-262 3rd Edition 11.9.4 */
2103 static HRESULT interp_eq2(exec_ctx_t *ctx)
2114 hres = equal2_values(r, l, &b);
2120 return stack_push_bool(ctx, b);
2123 /* ECMA-262 3rd Edition 11.9.5 */
2124 static HRESULT interp_neq2(exec_ctx_t *ctx)
2135 hres = equal2_values(r, l, &b);
2141 return stack_push_bool(ctx, !b);
2144 /* ECMA-262 3rd Edition 11.8.5 */
2145 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2151 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2155 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2161 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2162 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2163 SysFreeString(V_BSTR(&l));
2164 SysFreeString(V_BSTR(&r));
2168 hres = to_number(ctx, &l, ei, &ln);
2171 hres = to_number(ctx, &r, ei, &rn);
2176 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2180 /* ECMA-262 3rd Edition 11.8.1 */
2181 static HRESULT interp_lt(exec_ctx_t *ctx)
2190 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2192 hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2198 return stack_push_bool(ctx, b);
2201 /* ECMA-262 3rd Edition 11.8.1 */
2202 static HRESULT interp_lteq(exec_ctx_t *ctx)
2211 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2213 hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2219 return stack_push_bool(ctx, b);
2222 /* ECMA-262 3rd Edition 11.8.2 */
2223 static HRESULT interp_gt(exec_ctx_t *ctx)
2232 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2234 hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2240 return stack_push_bool(ctx, b);
2243 /* ECMA-262 3rd Edition 11.8.4 */
2244 static HRESULT interp_gteq(exec_ctx_t *ctx)
2253 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2255 hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2261 return stack_push_bool(ctx, b);
2264 /* ECMA-262 3rd Edition 11.4.8 */
2265 static HRESULT interp_bneg(exec_ctx_t *ctx)
2274 hres = to_int32(ctx->script, v, ctx->ei, &i);
2279 return stack_push_int(ctx, ~i);
2282 /* ECMA-262 3rd Edition 11.4.9 */
2283 static HRESULT interp_neg(exec_ctx_t *ctx)
2292 hres = to_boolean(v, &b);
2297 return stack_push_bool(ctx, !b);
2300 /* ECMA-262 3rd Edition 11.7.1 */
2301 static HRESULT interp_lshift(exec_ctx_t *ctx)
2307 hres = stack_pop_uint(ctx, &r);
2311 hres = stack_pop_int(ctx, &l);
2315 return stack_push_int(ctx, l << (r&0x1f));
2318 /* ECMA-262 3rd Edition 11.7.2 */
2319 static HRESULT interp_rshift(exec_ctx_t *ctx)
2325 hres = stack_pop_uint(ctx, &r);
2329 hres = stack_pop_int(ctx, &l);
2333 return stack_push_int(ctx, l >> (r&0x1f));
2336 /* ECMA-262 3rd Edition 11.7.3 */
2337 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2342 hres = stack_pop_uint(ctx, &r);
2346 hres = stack_pop_uint(ctx, &l);
2350 return stack_push_int(ctx, l >> (r&0x1f));
2353 /* ECMA-262 3rd Edition 11.13.1 */
2354 static HRESULT interp_assign(exec_ctx_t *ctx)
2364 disp = stack_pop_objid(ctx, &id);
2367 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2369 hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2370 IDispatch_Release(disp);
2376 return stack_push(ctx, v);
2379 /* JScript extension */
2380 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2382 const unsigned arg = get_op_uint(ctx, 0);
2390 disp = stack_topn_objid(ctx, arg+1, &id);
2392 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2394 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, arg+1, stack_args(ctx,arg+1), NULL, ctx->ei);
2399 stack_popn(ctx, arg+2);
2400 return stack_push(ctx, v);
2403 static HRESULT interp_undefined(exec_ctx_t *ctx)
2409 V_VT(&v) = VT_EMPTY;
2410 return stack_push(ctx, &v);
2413 static HRESULT interp_jmp(exec_ctx_t *ctx)
2415 const unsigned arg = get_op_uint(ctx, 0);
2423 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2425 const unsigned arg = get_op_uint(ctx, 0);
2433 hres = to_boolean(v, &b);
2445 static HRESULT interp_pop(exec_ctx_t *ctx)
2453 static HRESULT interp_ret(exec_ctx_t *ctx)
2461 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2463 static const op_func_t op_funcs[] = {
2464 #define X(x,a,b,c) interp_##x,
2469 static const unsigned op_move[] = {
2470 #define X(a,x,b,c) x,
2475 static HRESULT unwind_exception(exec_ctx_t *ctx)
2477 except_frame_t *except_frame;
2482 except_frame = ctx->except_frame;
2483 ctx->except_frame = except_frame->next;
2485 assert(except_frame->stack_top <= ctx->top);
2486 stack_popn(ctx, ctx->top - except_frame->stack_top);
2488 while(except_frame->scope != ctx->scope_chain)
2489 scope_pop(&ctx->scope_chain);
2491 ctx->ip = except_frame->catch_off;
2493 except_val = ctx->ei->var;
2494 memset(ctx->ei, 0, sizeof(*ctx->ei));
2496 ident = except_frame->ident;
2497 heap_free(except_frame);
2500 jsdisp_t *scope_obj;
2502 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2503 if(SUCCEEDED(hres)) {
2504 hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei);
2506 jsdisp_release(scope_obj);
2508 VariantClear(&except_val);
2512 hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2513 jsdisp_release(scope_obj);
2517 hres = stack_push(ctx, &except_val);
2521 hres = stack_push_bool(ctx, FALSE);
2525 V_VT(&v) = VT_EMPTY;
2526 hres = stack_push(ctx, &v);
2532 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsexcept_t *ei, VARIANT *ret)
2534 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2535 except_frame_t *prev_except_frame;
2536 function_code_t *prev_func;
2537 unsigned prev_ip, prev_top;
2538 scope_chain_t *prev_scope;
2539 bytecode_t *prev_code;
2540 jsexcept_t *prev_ei;
2542 HRESULT hres = S_OK;
2546 prev_top = exec_ctx->top;
2547 prev_scope = exec_ctx->scope_chain;
2548 prev_except_frame = exec_ctx->except_frame;
2549 prev_ip = exec_ctx->ip;
2550 prev_ei = exec_ctx->ei;
2551 prev_code = exec_ctx->code;
2552 prev_func = exec_ctx->func_code;
2553 exec_ctx->ip = func->instr_off;
2555 exec_ctx->except_frame = NULL;
2556 exec_ctx->code = code;
2557 exec_ctx->func_code = func;
2559 while(exec_ctx->ip != -1) {
2560 op = code->instrs[exec_ctx->ip].op;
2561 hres = op_funcs[op](exec_ctx);
2563 TRACE("EXCEPTION\n");
2565 if(!exec_ctx->except_frame)
2568 hres = unwind_exception(exec_ctx);
2572 exec_ctx->ip += op_move[op];
2576 exec_ctx->ip = prev_ip;
2577 exec_ctx->ei = prev_ei;
2578 exec_ctx->except_frame = prev_except_frame;
2579 exec_ctx->code = prev_code;
2580 exec_ctx->func_code = prev_func;
2583 while(exec_ctx->scope_chain != prev_scope)
2584 scope_pop(&exec_ctx->scope_chain);
2585 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2589 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2590 assert(exec_ctx->scope_chain == prev_scope);
2592 if(exec_ctx->top == prev_top)
2593 V_VT(ret) = VT_EMPTY;
2595 *ret = *stack_pop(exec_ctx);
2599 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval,
2600 jsexcept_t *ei, VARIANT *retv)
2602 exec_ctx_t *prev_ctx;
2605 HRESULT hres = S_OK;
2607 for(i = 0; i < func->func_cnt; i++) {
2611 if(!func->funcs[i].name)
2614 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2618 var_set_jsdisp(&var, func_obj);
2619 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, &var, ei);
2620 jsdisp_release(func_obj);
2625 for(i=0; i < func->var_cnt; i++) {
2626 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2629 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2635 prev_ctx = ctx->script->exec_ctx;
2636 ctx->script->exec_ctx = ctx;
2638 hres = enter_bytecode(ctx->script, code, func, ei, &val);
2639 assert(ctx->script->exec_ctx == ctx);
2640 ctx->script->exec_ctx = prev_ctx;