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_pop(exec_ctx_t *ctx)
139 return ctx->stack + --ctx->top;
142 static void stack_popn(exec_ctx_t *ctx, unsigned n)
145 VariantClear(stack_pop(ctx));
148 static HRESULT stack_pop_number(exec_ctx_t *ctx, double *r)
154 hres = to_number(ctx->script, v, ctx->ei, r);
159 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
165 if(V_VT(v) == VT_DISPATCH) {
167 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
172 hres = to_object(ctx->script, v, r);
177 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
179 return to_int32(ctx->script, stack_pop(ctx), ctx->ei, r);
182 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
184 return to_uint32(ctx->script, stack_pop(ctx), ctx->ei, r);
187 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
189 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
191 *id = V_INT(stack_pop(ctx));
192 return V_DISPATCH(stack_pop(ctx));
195 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
197 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
199 *id = V_INT(stack_topn(ctx, n));
200 return V_DISPATCH(stack_topn(ctx, n+1));
203 static void exprval_release(exprval_t *val)
206 case EXPRVAL_VARIANT:
207 if(V_VT(&val->u.var) != VT_EMPTY)
208 VariantClear(&val->u.var);
211 if(val->u.idref.disp)
212 IDispatch_Release(val->u.idref.disp);
214 case EXPRVAL_INVALID:
219 /* ECMA-262 3rd Edition 8.7.1 */
220 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
222 V_VT(ret) = VT_EMPTY;
225 case EXPRVAL_VARIANT:
226 return VariantCopy(ret, &val->u.var);
228 if(!val->u.idref.disp) {
229 FIXME("throw ReferenceError\n");
233 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei);
234 case EXPRVAL_INVALID:
238 ERR("type %d\n", val->type);
242 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
244 if(val->type == EXPRVAL_VARIANT) {
246 V_VT(&val->u.var) = VT_EMPTY;
250 return exprval_value(ctx, val, ei, ret);
253 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
255 val->type = EXPRVAL_IDREF;
256 val->u.idref.disp = disp;
257 val->u.idref.id = id;
260 IDispatch_AddRef(disp);
263 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
265 scope_chain_t *new_scope;
267 new_scope = heap_alloc(sizeof(scope_chain_t));
269 return E_OUTOFMEMORY;
274 new_scope->obj = obj;
278 new_scope->next = scope;
280 new_scope->next = NULL;
287 static void scope_pop(scope_chain_t **scope)
296 void scope_release(scope_chain_t *scope)
302 scope_release(scope->next);
304 jsdisp_release(scope->obj);
308 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
309 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
313 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
315 return E_OUTOFMEMORY;
318 ctx->is_global = is_global;
321 ctx->this_obj = this_obj;
322 else if(script_ctx->host_global)
323 ctx->this_obj = script_ctx->host_global;
325 ctx->this_obj = to_disp(script_ctx->global);
326 IDispatch_AddRef(ctx->this_obj);
328 jsdisp_addref(var_disp);
329 ctx->var_disp = var_disp;
331 script_addref(script_ctx);
332 ctx->script = script_ctx;
336 ctx->scope_chain = scope;
343 void exec_release(exec_ctx_t *ctx)
349 scope_release(ctx->scope_chain);
351 jsdisp_release(ctx->var_disp);
353 IDispatch_Release(ctx->this_obj);
355 script_release(ctx->script);
356 heap_free(ctx->stack);
360 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
365 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
367 TRACE("using IDispatch\n");
370 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
374 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
375 IDispatchEx_Release(dispex);
379 static inline BOOL is_null(const VARIANT *v)
381 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
384 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
386 IObjectIdentity *identity;
387 IUnknown *unk1, *unk2;
395 if(!disp1 || !disp2) {
400 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
404 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
406 IUnknown_Release(unk1);
413 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
414 if(SUCCEEDED(hres)) {
415 hres = IObjectIdentity_IsEqualObject(identity, unk2);
416 IObjectIdentity_Release(identity);
423 IUnknown_Release(unk1);
424 IUnknown_Release(unk2);
428 /* ECMA-262 3rd Edition 11.9.6 */
429 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
433 if(V_VT(lval) != V_VT(rval)) {
434 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
435 *ret = num_val(lval) == num_val(rval);
436 else if(is_null(lval))
437 *ret = is_null(rval);
449 *ret = V_I4(lval) == V_I4(rval);
452 *ret = V_R8(lval) == V_R8(rval);
456 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
457 else if(!V_BSTR(rval))
458 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
460 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
463 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
465 *ret = !V_BOOL(lval) == !V_BOOL(rval);
468 FIXME("unimplemented vt %d\n", V_VT(lval));
475 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
481 for(item = ctx->named_items; item; item = item->next) {
482 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
483 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
484 if(SUCCEEDED(hres)) {
486 exprval_set_idref(ret, item->disp, id);
495 /* ECMA-262 3rd Edition 10.1.4 */
496 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
498 scope_chain_t *scope;
503 TRACE("%s\n", debugstr_w(identifier));
505 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
506 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
507 if(SUCCEEDED(hres)) {
508 exprval_set_idref(ret, to_disp(scope->obj), id);
513 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
514 if(SUCCEEDED(hres)) {
515 exprval_set_idref(ret, to_disp(ctx->global), id);
519 for(item = ctx->named_items; item; item = item->next) {
520 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
527 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
528 SCRIPTINFO_IUNKNOWN, &unk, NULL);
530 WARN("GetItemInfo failed: %08x\n", hres);
534 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
535 IUnknown_Release(unk);
537 WARN("object does not implement IDispatch\n");
542 ret->type = EXPRVAL_VARIANT;
543 V_VT(&ret->u.var) = VT_DISPATCH;
544 V_DISPATCH(&ret->u.var) = item->disp;
545 IDispatch_AddRef(item->disp);
550 if(lookup_global_members(ctx, identifier, ret))
553 ret->type = EXPRVAL_INVALID;
557 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
558 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
561 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
562 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
565 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
566 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
569 static inline const WCHAR *get_op_str(exec_ctx_t *ctx, int i){
570 return ctx->code->instrs[ctx->ip].u.arg[i].str;
573 static inline double get_op_double(exec_ctx_t *ctx){
574 return ctx->code->instrs[ctx->ip].u.dbl;
577 /* ECMA-262 3rd Edition 12.2 */
578 static HRESULT interp_var_set(exec_ctx_t *ctx)
580 const BSTR name = get_op_bstr(ctx, 0);
584 TRACE("%s\n", debugstr_w(name));
587 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei);
592 /* ECMA-262 3rd Edition 12.6.4 */
593 static HRESULT interp_forin(exec_ctx_t *ctx)
595 const HRESULT arg = get_op_uint(ctx, 0);
596 IDispatch *var_obj, *obj = NULL;
605 val = stack_pop(ctx);
607 assert(V_VT(stack_top(ctx)) == VT_I4);
608 id = V_I4(stack_top(ctx));
610 var_obj = stack_topn_objid(ctx, 1, &var_id);
612 FIXME("invalid ref\n");
617 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
618 obj = V_DISPATCH(stack_topn(ctx, 3));
621 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
622 if(SUCCEEDED(hres)) {
623 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
625 hres = IDispatchEx_GetMemberName(dispex, id, &name);
626 IDispatchEx_Release(dispex);
632 TRACE("No IDispatchEx\n");
641 V_I4(stack_top(ctx)) = id;
645 hres = disp_propput(ctx->script, var_obj, var_id, &v, ctx->ei);
654 return stack_push(ctx, val);
659 /* ECMA-262 3rd Edition 12.10 */
660 static HRESULT interp_push_scope(exec_ctx_t *ctx)
670 hres = to_object(ctx->script, v, &disp);
675 obj = to_jsdisp(disp);
677 IDispatch_Release(disp);
678 FIXME("disp is not jsdisp\n");
682 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
687 /* ECMA-262 3rd Edition 12.10 */
688 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
692 scope_pop(&ctx->scope_chain);
696 /* ECMA-262 3rd Edition 12.13 */
697 static HRESULT interp_case(exec_ctx_t *ctx)
699 const unsigned arg = get_op_uint(ctx, 0);
707 hres = equal2_values(stack_top(ctx), v, &b);
721 /* ECMA-262 3rd Edition 12.13 */
722 static HRESULT interp_throw(exec_ctx_t *ctx)
726 ctx->ei->var = *stack_pop(ctx);
727 return DISP_E_EXCEPTION;
730 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
732 const HRESULT arg = get_op_uint(ctx, 0);
734 TRACE("%08x\n", arg);
736 return throw_reference_error(ctx->script, ctx->ei, arg, NULL);
739 static HRESULT interp_throw_type(exec_ctx_t *ctx)
741 const HRESULT hres = get_op_uint(ctx, 0);
742 const WCHAR *str = get_op_str(ctx, 1);
744 TRACE("%08x %s\n", hres, debugstr_w(str));
746 return throw_type_error(ctx->script, ctx->ei, hres, str);
749 /* ECMA-262 3rd Edition 12.14 */
750 static HRESULT interp_push_except(exec_ctx_t *ctx)
752 const unsigned arg1 = get_op_uint(ctx, 0);
753 const BSTR arg2 = get_op_bstr(ctx, 1);
754 except_frame_t *except;
759 stack_top = ctx->top;
764 hres = stack_push_bool(ctx, TRUE);
767 hres = stack_push_bool(ctx, TRUE);
772 except = heap_alloc(sizeof(*except));
774 return E_OUTOFMEMORY;
776 except->stack_top = stack_top;
777 except->scope = ctx->scope_chain;
778 except->catch_off = arg1;
779 except->ident = arg2;
780 except->next = ctx->except_frame;
781 ctx->except_frame = except;
785 /* ECMA-262 3rd Edition 12.14 */
786 static HRESULT interp_pop_except(exec_ctx_t *ctx)
788 except_frame_t *except;
792 except = ctx->except_frame;
793 assert(except != NULL);
795 ctx->except_frame = except->next;
800 /* ECMA-262 3rd Edition 12.14 */
801 static HRESULT interp_end_finally(exec_ctx_t *ctx)
809 assert(V_VT(stack_top(ctx)) == VT_BOOL);
810 if(!V_BOOL(stack_top(ctx))) {
811 TRACE("passing exception\n");
815 ctx->ei->var = *stack_pop(ctx);
816 return DISP_E_EXCEPTION;
820 return stack_push(ctx, v);
823 /* ECMA-262 3rd Edition 13 */
824 static HRESULT interp_func(exec_ctx_t *ctx)
826 unsigned func_idx = get_op_uint(ctx, 0);
831 TRACE("%d\n", func_idx);
833 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
834 ctx->scope_chain, &dispex);
838 var_set_jsdisp(&v, dispex);
839 return stack_push(ctx, &v);
842 /* ECMA-262 3rd Edition 11.2.1 */
843 static HRESULT interp_array(exec_ctx_t *ctx)
853 namev = stack_pop(ctx);
855 hres = stack_pop_object(ctx, &obj);
861 hres = to_string(ctx->script, namev, ctx->ei, &name);
864 IDispatch_Release(obj);
868 hres = disp_get_id(ctx->script, obj, name, 0, &id);
870 if(SUCCEEDED(hres)) {
871 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
872 }else if(hres == DISP_E_UNKNOWNNAME) {
876 IDispatch_Release(obj);
880 return stack_push(ctx, &v);
883 /* ECMA-262 3rd Edition 11.2.1 */
884 static HRESULT interp_member(exec_ctx_t *ctx)
886 const BSTR arg = get_op_bstr(ctx, 0);
894 hres = stack_pop_object(ctx, &obj);
898 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
899 if(SUCCEEDED(hres)) {
901 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
902 }else if(hres == DISP_E_UNKNOWNNAME) {
906 IDispatch_Release(obj);
910 return stack_push(ctx, &v);
913 /* ECMA-262 3rd Edition 11.2.1 */
914 static HRESULT interp_memberid(exec_ctx_t *ctx)
916 const unsigned arg = get_op_uint(ctx, 0);
917 VARIANT *objv, *namev;
925 namev = stack_pop(ctx);
926 objv = stack_pop(ctx);
928 hres = to_object(ctx->script, objv, &obj);
930 if(SUCCEEDED(hres)) {
931 hres = to_string(ctx->script, namev, ctx->ei, &name);
933 IDispatch_Release(obj);
939 hres = disp_get_id(ctx->script, obj, name, arg, &id);
942 IDispatch_Release(obj);
943 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
945 id = JS_E_INVALID_PROPERTY;
951 return stack_push_objid(ctx, obj, id);
954 /* ECMA-262 3rd Edition 11.2.1 */
955 static HRESULT interp_refval(exec_ctx_t *ctx)
964 disp = stack_topn_objid(ctx, 0, &id);
966 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
968 hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
972 return stack_push(ctx, &v);
975 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
981 dp->rgdispidNamedArgs = NULL;
984 assert(ctx->top >= arg_cnt);
986 for(i=1; i*2 <= arg_cnt; i++) {
987 tmp = ctx->stack[ctx->top-i];
988 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
989 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
992 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
995 /* ECMA-262 3rd Edition 11.2.2 */
996 static HRESULT interp_new(exec_ctx_t *ctx)
998 const unsigned arg = get_op_uint(ctx, 0);
1005 constr = stack_topn(ctx, arg);
1007 /* NOTE: Should use to_object here */
1009 if(V_VT(constr) == VT_NULL)
1010 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1011 else if(V_VT(constr) != VT_DISPATCH)
1012 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
1013 else if(!V_DISPATCH(constr))
1014 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1016 jsstack_to_dp(ctx, arg, &dp);
1017 hres = disp_call(ctx->script, V_DISPATCH(constr), DISPID_VALUE,
1018 DISPATCH_CONSTRUCT, &dp, &v, ctx->ei);
1022 stack_popn(ctx, arg+1);
1023 return stack_push(ctx, &v);
1026 /* ECMA-262 3rd Edition 11.2.3 */
1027 static HRESULT interp_call(exec_ctx_t *ctx)
1029 const unsigned argn = get_op_uint(ctx, 0);
1030 const int do_ret = get_op_int(ctx, 1);
1035 TRACE("%d %d\n", argn, do_ret);
1037 objv = stack_topn(ctx, argn);
1038 if(V_VT(objv) != VT_DISPATCH)
1039 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1041 jsstack_to_dp(ctx, argn, &dp);
1042 hres = disp_call(ctx->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1043 do_ret ? &v : NULL, ctx->ei);
1047 stack_popn(ctx, argn+1);
1048 return do_ret ? stack_push(ctx, &v) : S_OK;
1052 /* ECMA-262 3rd Edition 11.2.3 */
1053 static HRESULT interp_call_member(exec_ctx_t *ctx)
1055 const unsigned argn = get_op_uint(ctx, 0);
1056 const int do_ret = get_op_int(ctx, 1);
1063 TRACE("%d %d\n", argn, do_ret);
1065 obj = stack_topn_objid(ctx, argn, &id);
1067 return throw_type_error(ctx->script, ctx->ei, id, NULL);
1069 jsstack_to_dp(ctx, argn, &dp);
1070 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, ctx->ei);
1074 stack_popn(ctx, argn+2);
1075 return do_ret ? stack_push(ctx, &v) : S_OK;
1079 /* ECMA-262 3rd Edition 11.1.1 */
1080 static HRESULT interp_this(exec_ctx_t *ctx)
1086 V_VT(&v) = VT_DISPATCH;
1087 V_DISPATCH(&v) = ctx->this_obj;
1088 IDispatch_AddRef(ctx->this_obj);
1089 return stack_push(ctx, &v);
1092 /* ECMA-262 3rd Edition 10.1.4 */
1093 static HRESULT interp_ident(exec_ctx_t *ctx)
1095 const BSTR arg = get_op_bstr(ctx, 0);
1100 TRACE("%s\n", debugstr_w(arg));
1102 hres = identifier_eval(ctx->script, arg, &exprval);
1106 if(exprval.type == EXPRVAL_INVALID)
1107 return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1109 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1110 exprval_release(&exprval);
1114 return stack_push(ctx, &v);
1117 /* ECMA-262 3rd Edition 10.1.4 */
1118 static HRESULT interp_identid(exec_ctx_t *ctx)
1120 const BSTR arg = get_op_bstr(ctx, 0);
1121 const unsigned flags = get_op_uint(ctx, 1);
1125 TRACE("%s %x\n", debugstr_w(arg), flags);
1127 hres = identifier_eval(ctx->script, arg, &exprval);
1131 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1134 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1138 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1141 if(exprval.type != EXPRVAL_IDREF) {
1142 WARN("invalid ref\n");
1143 exprval_release(&exprval);
1144 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1147 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1150 /* ECMA-262 3rd Edition 7.8.1 */
1151 static HRESULT interp_null(exec_ctx_t *ctx)
1158 return stack_push(ctx, &v);
1161 /* ECMA-262 3rd Edition 7.8.2 */
1162 static HRESULT interp_bool(exec_ctx_t *ctx)
1164 const int arg = get_op_int(ctx, 0);
1166 TRACE("%s\n", arg ? "true" : "false");
1168 return stack_push_bool(ctx, arg);
1171 /* ECMA-262 3rd Edition 7.8.3 */
1172 static HRESULT interp_int(exec_ctx_t *ctx)
1174 const int arg = get_op_int(ctx, 0);
1181 return stack_push(ctx, &v);
1184 /* ECMA-262 3rd Edition 7.8.3 */
1185 static HRESULT interp_double(exec_ctx_t *ctx)
1187 const double arg = get_op_double(ctx);
1189 TRACE("%lf\n", arg);
1191 return stack_push_number(ctx, arg);
1194 /* ECMA-262 3rd Edition 7.8.4 */
1195 static HRESULT interp_str(exec_ctx_t *ctx)
1197 const WCHAR *str = get_op_str(ctx, 0);
1200 TRACE("%s\n", debugstr_w(str));
1203 V_BSTR(&v) = SysAllocString(str);
1205 return E_OUTOFMEMORY;
1207 return stack_push(ctx, &v);
1210 /* ECMA-262 3rd Edition 7.8 */
1211 static HRESULT interp_regexp(exec_ctx_t *ctx)
1213 const WCHAR *source = get_op_str(ctx, 0);
1214 const unsigned flags = get_op_uint(ctx, 1);
1219 TRACE("%s %x\n", debugstr_w(source), flags);
1221 hres = create_regexp(ctx->script, source, strlenW(source), flags, ®exp);
1225 var_set_jsdisp(&v, regexp);
1226 return stack_push(ctx, &v);
1229 /* ECMA-262 3rd Edition 11.1.4 */
1230 static HRESULT interp_carray(exec_ctx_t *ctx)
1232 const unsigned arg = get_op_uint(ctx, 0);
1240 hres = create_array(ctx->script, arg, &array);
1247 hres = jsdisp_propput_idx(array, i, v, ctx->ei);
1250 jsdisp_release(array);
1255 var_set_jsdisp(&r, array);
1256 return stack_push(ctx, &r);
1259 /* ECMA-262 3rd Edition 11.1.5 */
1260 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1268 hres = create_object(ctx->script, NULL, &obj);
1272 var_set_jsdisp(&v, obj);
1273 return stack_push(ctx, &v);
1276 /* ECMA-262 3rd Edition 11.1.5 */
1277 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1279 const BSTR name = get_op_bstr(ctx, 0);
1284 TRACE("%s\n", debugstr_w(name));
1288 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1289 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1291 hres = jsdisp_propput_name(obj, name, v, ctx->ei);
1296 /* ECMA-262 3rd Edition 11.11 */
1297 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1299 const unsigned arg = get_op_uint(ctx, 0);
1305 hres = to_boolean(stack_top(ctx), &b);
1318 /* ECMA-262 3rd Edition 11.11 */
1319 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1321 const unsigned arg = get_op_uint(ctx, 0);
1327 hres = to_boolean(stack_top(ctx), &b);
1340 /* ECMA-262 3rd Edition 11.10 */
1341 static HRESULT interp_or(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_xor(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.10 */
1379 static HRESULT interp_and(exec_ctx_t *ctx)
1386 hres = stack_pop_int(ctx, &r);
1390 hres = stack_pop_int(ctx, &l);
1394 return stack_push_int(ctx, l&r);
1397 /* ECMA-262 3rd Edition 11.8.6 */
1398 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1400 jsdisp_t *obj, *iter, *tmp = NULL;
1405 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1408 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1410 return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1413 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1414 IDispatch_Release(V_DISPATCH(v));
1416 FIXME("non-jsdisp objects not supported\n");
1420 if(is_class(obj, JSCLASS_FUNCTION)) {
1421 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1423 hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1425 jsdisp_release(obj);
1431 if(V_VT(&prot) == VT_DISPATCH) {
1432 if(V_VT(v) == VT_DISPATCH)
1433 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1434 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1435 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1441 jsdisp_release(tmp);
1443 FIXME("prototype is not an object\n");
1447 VariantClear(&prot);
1452 return stack_push_bool(ctx, ret);
1455 /* ECMA-262 3rd Edition 11.8.7 */
1456 static HRESULT interp_in(exec_ctx_t *ctx)
1466 obj = stack_pop(ctx);
1469 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1472 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1475 hres = to_string(ctx->script, v, ctx->ei, &str);
1478 IDispatch_Release(V_DISPATCH(obj));
1482 hres = disp_get_id(ctx->script, V_DISPATCH(obj), str, 0, &id);
1483 IDispatch_Release(V_DISPATCH(obj));
1487 else if(hres == DISP_E_UNKNOWNNAME)
1492 return stack_push_bool(ctx, ret);
1495 /* ECMA-262 3rd Edition 11.6.1 */
1496 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1501 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1505 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1511 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1512 BSTR lstr = NULL, rstr = NULL;
1514 if(V_VT(&l) == VT_BSTR)
1517 hres = to_string(ctx, &l, ei, &lstr);
1519 if(SUCCEEDED(hres)) {
1520 if(V_VT(&r) == VT_BSTR)
1523 hres = to_string(ctx, &r, ei, &rstr);
1526 if(SUCCEEDED(hres)) {
1529 len1 = SysStringLen(lstr);
1530 len2 = SysStringLen(rstr);
1532 V_VT(retv) = VT_BSTR;
1533 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1535 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1537 memcpy(V_BSTR(retv)+len1, rstr, len2*sizeof(WCHAR));
1538 V_BSTR(retv)[len1+len2] = 0;
1541 if(V_VT(&l) != VT_BSTR)
1542 SysFreeString(lstr);
1543 if(V_VT(&r) != VT_BSTR)
1544 SysFreeString(rstr);
1548 hres = to_number(ctx, &l, ei, &nl);
1549 if(SUCCEEDED(hres)) {
1550 hres = to_number(ctx, &r, ei, &nr);
1552 num_set_val(retv, nl + nr);
1561 /* ECMA-262 3rd Edition 11.6.1 */
1562 static HRESULT interp_add(exec_ctx_t *ctx)
1564 VARIANT *l, *r, ret;
1570 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1572 hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1578 return stack_push(ctx, &ret);
1581 /* ECMA-262 3rd Edition 11.6.2 */
1582 static HRESULT interp_sub(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.1 */
1601 static HRESULT interp_mul(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.2 */
1620 static HRESULT interp_div(exec_ctx_t *ctx)
1627 hres = stack_pop_number(ctx, &r);
1631 hres = stack_pop_number(ctx, &l);
1635 return stack_push_number(ctx, l/r);
1638 /* ECMA-262 3rd Edition 11.5.3 */
1639 static HRESULT interp_mod(exec_ctx_t *ctx)
1646 hres = stack_pop_number(ctx, &r);
1650 hres = stack_pop_number(ctx, &l);
1654 return stack_push_number(ctx, fmod(l, r));
1657 /* ECMA-262 3rd Edition 11.4.2 */
1658 static HRESULT interp_delete(exec_ctx_t *ctx)
1660 VARIANT *obj_var, *name_var;
1661 IDispatchEx *dispex;
1669 name_var = stack_pop(ctx);
1670 obj_var = stack_pop(ctx);
1672 hres = to_object(ctx->script, obj_var, &obj);
1673 VariantClear(obj_var);
1675 VariantClear(name_var);
1679 hres = to_string(ctx->script, name_var, ctx->ei, &name);
1680 VariantClear(name_var);
1682 IDispatch_Release(obj);
1686 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1687 if(SUCCEEDED(hres)) {
1688 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1690 IDispatchEx_Release(dispex);
1696 IDispatch_Release(obj);
1697 SysFreeString(name);
1701 return stack_push_bool(ctx, ret);
1704 /* ECMA-262 3rd Edition 11.4.2 */
1705 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1707 const BSTR arg = get_op_bstr(ctx, 0);
1708 IDispatchEx *dispex;
1713 TRACE("%s\n", debugstr_w(arg));
1715 hres = identifier_eval(ctx->script, arg, &exprval);
1719 if(exprval.type != EXPRVAL_IDREF) {
1720 FIXME("Unsupported exprval\n");
1721 exprval_release(&exprval);
1725 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1726 IDispatch_Release(exprval.u.idref.disp);
1727 if(SUCCEEDED(hres)) {
1728 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1729 IDispatchEx_Release(dispex);
1736 return stack_push_bool(ctx, ret);
1739 /* ECMA-262 3rd Edition 11.4.2 */
1740 static HRESULT interp_void(exec_ctx_t *ctx)
1748 V_VT(&v) = VT_EMPTY;
1749 return stack_push(ctx, &v);
1752 /* ECMA-262 3rd Edition 11.4.3 */
1753 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1775 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1776 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1777 jsdisp_release(dispex);
1784 FIXME("unhandled vt %d\n", V_VT(v));
1791 /* ECMA-262 3rd Edition 11.4.3 */
1792 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1800 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1804 obj = stack_pop_objid(ctx, &id);
1806 return stack_push_string(ctx, undefinedW);
1808 V_VT(&v) = VT_EMPTY;
1809 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1810 IDispatch_Release(obj);
1812 return stack_push_string(ctx, unknownW);
1814 hres = typeof_string(&v, &ret);
1819 return stack_push_string(ctx, ret);
1822 /* ECMA-262 3rd Edition 11.4.3 */
1823 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1825 const BSTR arg = get_op_bstr(ctx, 0);
1831 TRACE("%s\n", debugstr_w(arg));
1833 hres = identifier_eval(ctx->script, arg, &exprval);
1837 if(exprval.type == EXPRVAL_INVALID) {
1838 hres = stack_push_string(ctx, undefinedW);
1839 exprval_release(&exprval);
1843 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1844 exprval_release(&exprval);
1848 hres = typeof_string(&v, &ret);
1853 return stack_push_string(ctx, ret);
1856 /* ECMA-262 3rd Edition 11.4.3 */
1857 static HRESULT interp_typeof(exec_ctx_t *ctx)
1866 hres = typeof_string(v, &ret);
1871 return stack_push_string(ctx, ret);
1874 /* ECMA-262 3rd Edition 11.4.7 */
1875 static HRESULT interp_minus(exec_ctx_t *ctx)
1882 hres = stack_pop_number(ctx, &n);
1886 return stack_push_number(ctx, -n);
1889 /* ECMA-262 3rd Edition 11.4.6 */
1890 static HRESULT interp_tonum(exec_ctx_t *ctx)
1899 hres = to_number(ctx->script, v, ctx->ei, &n);
1904 return stack_push_number(ctx, n);
1907 /* ECMA-262 3rd Edition 11.3.1 */
1908 static HRESULT interp_postinc(exec_ctx_t *ctx)
1910 const int arg = get_op_int(ctx, 0);
1918 obj = stack_pop_objid(ctx, &id);
1920 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1922 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1923 if(SUCCEEDED(hres)) {
1927 hres = to_number(ctx->script, &v, ctx->ei, &n);
1928 if(SUCCEEDED(hres)) {
1929 num_set_val(&inc, n+(double)arg);
1930 hres = disp_propput(ctx->script, obj, id, &inc, ctx->ei);
1935 IDispatch_Release(obj);
1939 return stack_push(ctx, &v);
1942 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1943 static HRESULT interp_preinc(exec_ctx_t *ctx)
1945 const int arg = get_op_int(ctx, 0);
1953 obj = stack_pop_objid(ctx, &id);
1955 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1957 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1958 if(SUCCEEDED(hres)) {
1961 hres = to_number(ctx->script, &v, ctx->ei, &n);
1963 if(SUCCEEDED(hres)) {
1964 num_set_val(&v, n+(double)arg);
1965 hres = disp_propput(ctx->script, obj, id, &v, ctx->ei);
1968 IDispatch_Release(obj);
1972 return stack_push(ctx, &v);
1975 /* ECMA-262 3rd Edition 11.9.3 */
1976 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1978 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1979 return equal2_values(lval, rval, ret);
1981 /* FIXME: NULL disps should be handled in more general way */
1982 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1985 return equal_values(ctx, &v, rval, ei, ret);
1988 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1991 return equal_values(ctx, lval, &v, ei, ret);
1994 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1995 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2000 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2005 hres = to_number(ctx, lval, ei, &n);
2009 /* FIXME: optimize */
2012 return equal_values(ctx, &v, rval, ei, ret);
2015 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2020 hres = to_number(ctx, rval, ei, &n);
2024 /* FIXME: optimize */
2027 return equal_values(ctx, lval, &v, ei, ret);
2030 if(V_VT(rval) == VT_BOOL) {
2033 num_set_int(&v, V_BOOL(rval) ? 1 : 0);
2034 return equal_values(ctx, lval, &v, ei, ret);
2037 if(V_VT(lval) == VT_BOOL) {
2040 num_set_int(&v, V_BOOL(lval) ? 1 : 0);
2041 return equal_values(ctx, &v, rval, ei, ret);
2045 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2049 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2053 hres = equal_values(ctx, lval, &v, ei, ret);
2060 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2064 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2068 hres = equal_values(ctx, &v, rval, ei, ret);
2079 /* ECMA-262 3rd Edition 11.9.1 */
2080 static HRESULT interp_eq(exec_ctx_t *ctx)
2089 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2091 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2097 return stack_push_bool(ctx, b);
2100 /* ECMA-262 3rd Edition 11.9.2 */
2101 static HRESULT interp_neq(exec_ctx_t *ctx)
2110 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2112 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2118 return stack_push_bool(ctx, !b);
2121 /* ECMA-262 3rd Edition 11.9.4 */
2122 static HRESULT interp_eq2(exec_ctx_t *ctx)
2133 hres = equal2_values(r, l, &b);
2139 return stack_push_bool(ctx, b);
2142 /* ECMA-262 3rd Edition 11.9.5 */
2143 static HRESULT interp_neq2(exec_ctx_t *ctx)
2154 hres = equal2_values(r, l, &b);
2160 return stack_push_bool(ctx, !b);
2163 /* ECMA-262 3rd Edition 11.8.5 */
2164 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2170 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2174 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2180 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2181 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2182 SysFreeString(V_BSTR(&l));
2183 SysFreeString(V_BSTR(&r));
2187 hres = to_number(ctx, &l, ei, &ln);
2190 hres = to_number(ctx, &r, ei, &rn);
2195 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2199 /* ECMA-262 3rd Edition 11.8.1 */
2200 static HRESULT interp_lt(exec_ctx_t *ctx)
2209 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2211 hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2217 return stack_push_bool(ctx, b);
2220 /* ECMA-262 3rd Edition 11.8.1 */
2221 static HRESULT interp_lteq(exec_ctx_t *ctx)
2230 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2232 hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2238 return stack_push_bool(ctx, b);
2241 /* ECMA-262 3rd Edition 11.8.2 */
2242 static HRESULT interp_gt(exec_ctx_t *ctx)
2251 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2253 hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2259 return stack_push_bool(ctx, b);
2262 /* ECMA-262 3rd Edition 11.8.4 */
2263 static HRESULT interp_gteq(exec_ctx_t *ctx)
2272 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2274 hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2280 return stack_push_bool(ctx, b);
2283 /* ECMA-262 3rd Edition 11.4.8 */
2284 static HRESULT interp_bneg(exec_ctx_t *ctx)
2293 hres = to_int32(ctx->script, v, ctx->ei, &i);
2298 return stack_push_int(ctx, ~i);
2301 /* ECMA-262 3rd Edition 11.4.9 */
2302 static HRESULT interp_neg(exec_ctx_t *ctx)
2311 hres = to_boolean(v, &b);
2316 return stack_push_bool(ctx, !b);
2319 /* ECMA-262 3rd Edition 11.7.1 */
2320 static HRESULT interp_lshift(exec_ctx_t *ctx)
2326 hres = stack_pop_uint(ctx, &r);
2330 hres = stack_pop_int(ctx, &l);
2334 return stack_push_int(ctx, l << (r&0x1f));
2337 /* ECMA-262 3rd Edition 11.7.2 */
2338 static HRESULT interp_rshift(exec_ctx_t *ctx)
2344 hres = stack_pop_uint(ctx, &r);
2348 hres = stack_pop_int(ctx, &l);
2352 return stack_push_int(ctx, l >> (r&0x1f));
2355 /* ECMA-262 3rd Edition 11.7.3 */
2356 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2361 hres = stack_pop_uint(ctx, &r);
2365 hres = stack_pop_uint(ctx, &l);
2369 return stack_push_int(ctx, l >> (r&0x1f));
2372 /* ECMA-262 3rd Edition 11.13.1 */
2373 static HRESULT interp_assign(exec_ctx_t *ctx)
2383 disp = stack_pop_objid(ctx, &id);
2386 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2388 hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2389 IDispatch_Release(disp);
2395 return stack_push(ctx, v);
2398 /* JScript extension */
2399 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2401 const unsigned arg = get_op_uint(ctx, 0);
2402 DISPID propput_dispid = DISPID_PROPERTYPUT;
2411 disp = stack_topn_objid(ctx, arg+1, &id);
2413 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2415 jsstack_to_dp(ctx, arg+1, &dp);
2417 dp.rgdispidNamedArgs = &propput_dispid;
2418 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, &dp, NULL, ctx->ei);
2423 stack_popn(ctx, arg+2);
2424 return stack_push(ctx, v);
2427 static HRESULT interp_undefined(exec_ctx_t *ctx)
2433 V_VT(&v) = VT_EMPTY;
2434 return stack_push(ctx, &v);
2437 static HRESULT interp_jmp(exec_ctx_t *ctx)
2439 const unsigned arg = get_op_uint(ctx, 0);
2447 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2449 const unsigned arg = get_op_uint(ctx, 0);
2457 hres = to_boolean(v, &b);
2469 static HRESULT interp_pop(exec_ctx_t *ctx)
2477 static HRESULT interp_ret(exec_ctx_t *ctx)
2485 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2487 static const op_func_t op_funcs[] = {
2488 #define X(x,a,b,c) interp_##x,
2493 static const unsigned op_move[] = {
2494 #define X(a,x,b,c) x,
2499 static HRESULT unwind_exception(exec_ctx_t *ctx)
2501 except_frame_t *except_frame;
2506 except_frame = ctx->except_frame;
2507 ctx->except_frame = except_frame->next;
2509 assert(except_frame->stack_top <= ctx->top);
2510 stack_popn(ctx, ctx->top - except_frame->stack_top);
2512 while(except_frame->scope != ctx->scope_chain)
2513 scope_pop(&ctx->scope_chain);
2515 ctx->ip = except_frame->catch_off;
2517 except_val = ctx->ei->var;
2518 memset(ctx->ei, 0, sizeof(*ctx->ei));
2520 ident = except_frame->ident;
2521 heap_free(except_frame);
2524 jsdisp_t *scope_obj;
2526 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2527 if(SUCCEEDED(hres)) {
2528 hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei);
2530 jsdisp_release(scope_obj);
2532 VariantClear(&except_val);
2536 hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2537 jsdisp_release(scope_obj);
2541 hres = stack_push(ctx, &except_val);
2545 hres = stack_push_bool(ctx, FALSE);
2549 V_VT(&v) = VT_EMPTY;
2550 hres = stack_push(ctx, &v);
2556 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsexcept_t *ei, VARIANT *ret)
2558 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2559 except_frame_t *prev_except_frame;
2560 function_code_t *prev_func;
2561 unsigned prev_ip, prev_top;
2562 scope_chain_t *prev_scope;
2563 bytecode_t *prev_code;
2564 jsexcept_t *prev_ei;
2566 HRESULT hres = S_OK;
2570 prev_top = exec_ctx->top;
2571 prev_scope = exec_ctx->scope_chain;
2572 prev_except_frame = exec_ctx->except_frame;
2573 prev_ip = exec_ctx->ip;
2574 prev_ei = exec_ctx->ei;
2575 prev_code = exec_ctx->code;
2576 prev_func = exec_ctx->func_code;
2577 exec_ctx->ip = func->instr_off;
2579 exec_ctx->except_frame = NULL;
2580 exec_ctx->code = code;
2581 exec_ctx->func_code = func;
2583 while(exec_ctx->ip != -1) {
2584 op = code->instrs[exec_ctx->ip].op;
2585 hres = op_funcs[op](exec_ctx);
2587 TRACE("EXCEPTION\n");
2589 if(!exec_ctx->except_frame)
2592 hres = unwind_exception(exec_ctx);
2596 exec_ctx->ip += op_move[op];
2600 exec_ctx->ip = prev_ip;
2601 exec_ctx->ei = prev_ei;
2602 exec_ctx->except_frame = prev_except_frame;
2603 exec_ctx->code = prev_code;
2604 exec_ctx->func_code = prev_func;
2607 while(exec_ctx->scope_chain != prev_scope)
2608 scope_pop(&exec_ctx->scope_chain);
2609 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2613 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2614 assert(exec_ctx->scope_chain == prev_scope);
2616 if(exec_ctx->top == prev_top)
2617 V_VT(ret) = VT_EMPTY;
2619 *ret = *stack_pop(exec_ctx);
2623 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval,
2624 jsexcept_t *ei, VARIANT *retv)
2626 exec_ctx_t *prev_ctx;
2629 HRESULT hres = S_OK;
2631 for(i = 0; i < func->func_cnt; i++) {
2635 if(!func->funcs[i].name)
2638 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2642 var_set_jsdisp(&var, func_obj);
2643 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, &var, ei);
2644 jsdisp_release(func_obj);
2649 for(i=0; i < func->var_cnt; i++) {
2650 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2653 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2659 prev_ctx = ctx->script->exec_ctx;
2660 ctx->script->exec_ctx = ctx;
2662 hres = enter_bytecode(ctx->script, code, func, ei, &val);
2663 assert(ctx->script->exec_ctx == ctx);
2664 ctx->script->exec_ctx = prev_ctx;