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, VARIANT *r)
155 hres = to_number(ctx->script, v, ctx->ei, &n);
164 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
170 if(V_VT(v) == VT_DISPATCH) {
172 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
177 hres = to_object(ctx->script, v, r);
182 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
184 return to_int32(ctx->script, stack_pop(ctx), ctx->ei, r);
187 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
189 return to_uint32(ctx->script, stack_pop(ctx), ctx->ei, r);
192 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
194 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
196 *id = V_INT(stack_pop(ctx));
197 return V_DISPATCH(stack_pop(ctx));
200 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
202 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
204 *id = V_INT(stack_topn(ctx, n));
205 return V_DISPATCH(stack_topn(ctx, n+1));
208 static void exprval_release(exprval_t *val)
211 case EXPRVAL_VARIANT:
212 if(V_VT(&val->u.var) != VT_EMPTY)
213 VariantClear(&val->u.var);
216 if(val->u.idref.disp)
217 IDispatch_Release(val->u.idref.disp);
219 case EXPRVAL_INVALID:
224 /* ECMA-262 3rd Edition 8.7.1 */
225 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
227 V_VT(ret) = VT_EMPTY;
230 case EXPRVAL_VARIANT:
231 return VariantCopy(ret, &val->u.var);
233 if(!val->u.idref.disp) {
234 FIXME("throw ReferenceError\n");
238 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei);
239 case EXPRVAL_INVALID:
243 ERR("type %d\n", val->type);
247 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
249 if(val->type == EXPRVAL_VARIANT) {
251 V_VT(&val->u.var) = VT_EMPTY;
255 return exprval_value(ctx, val, ei, ret);
258 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
260 val->type = EXPRVAL_IDREF;
261 val->u.idref.disp = disp;
262 val->u.idref.id = id;
265 IDispatch_AddRef(disp);
268 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
270 scope_chain_t *new_scope;
272 new_scope = heap_alloc(sizeof(scope_chain_t));
274 return E_OUTOFMEMORY;
279 new_scope->obj = obj;
283 new_scope->next = scope;
285 new_scope->next = NULL;
292 static void scope_pop(scope_chain_t **scope)
301 void scope_release(scope_chain_t *scope)
307 scope_release(scope->next);
309 jsdisp_release(scope->obj);
313 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
314 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
318 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
320 return E_OUTOFMEMORY;
323 ctx->is_global = is_global;
326 ctx->this_obj = this_obj;
327 else if(script_ctx->host_global)
328 ctx->this_obj = script_ctx->host_global;
330 ctx->this_obj = to_disp(script_ctx->global);
331 IDispatch_AddRef(ctx->this_obj);
333 jsdisp_addref(var_disp);
334 ctx->var_disp = var_disp;
336 script_addref(script_ctx);
337 ctx->script = script_ctx;
341 ctx->scope_chain = scope;
348 void exec_release(exec_ctx_t *ctx)
354 scope_release(ctx->scope_chain);
356 jsdisp_release(ctx->var_disp);
358 IDispatch_Release(ctx->this_obj);
360 script_release(ctx->script);
361 heap_free(ctx->stack);
365 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
370 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
372 TRACE("unsing IDispatch\n");
375 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
379 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
380 IDispatchEx_Release(dispex);
384 static inline BOOL is_null(const VARIANT *v)
386 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
389 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
391 IObjectIdentity *identity;
392 IUnknown *unk1, *unk2;
400 if(!disp1 || !disp2) {
405 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
409 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
411 IUnknown_Release(unk1);
418 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
419 if(SUCCEEDED(hres)) {
420 hres = IObjectIdentity_IsEqualObject(identity, unk2);
421 IObjectIdentity_Release(identity);
428 IUnknown_Release(unk1);
429 IUnknown_Release(unk2);
433 /* ECMA-262 3rd Edition 11.9.6 */
434 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
438 if(V_VT(lval) != V_VT(rval)) {
439 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
440 *ret = num_val(lval) == num_val(rval);
441 else if(is_null(lval))
442 *ret = is_null(rval);
454 *ret = V_I4(lval) == V_I4(rval);
457 *ret = V_R8(lval) == V_R8(rval);
461 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
462 else if(!V_BSTR(rval))
463 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
465 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
468 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
470 *ret = !V_BOOL(lval) == !V_BOOL(rval);
473 FIXME("unimplemented vt %d\n", V_VT(lval));
480 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
486 for(item = ctx->named_items; item; item = item->next) {
487 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
488 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
489 if(SUCCEEDED(hres)) {
491 exprval_set_idref(ret, item->disp, id);
500 /* ECMA-262 3rd Edition 10.1.4 */
501 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
503 scope_chain_t *scope;
508 TRACE("%s\n", debugstr_w(identifier));
510 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
511 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
512 if(SUCCEEDED(hres)) {
513 exprval_set_idref(ret, to_disp(scope->obj), id);
518 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
519 if(SUCCEEDED(hres)) {
520 exprval_set_idref(ret, to_disp(ctx->global), id);
524 for(item = ctx->named_items; item; item = item->next) {
525 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
532 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
533 SCRIPTINFO_IUNKNOWN, &unk, NULL);
535 WARN("GetItemInfo failed: %08x\n", hres);
539 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
540 IUnknown_Release(unk);
542 WARN("object does not implement IDispatch\n");
547 ret->type = EXPRVAL_VARIANT;
548 V_VT(&ret->u.var) = VT_DISPATCH;
549 V_DISPATCH(&ret->u.var) = item->disp;
550 IDispatch_AddRef(item->disp);
555 if(lookup_global_members(ctx, identifier, ret))
558 ret->type = EXPRVAL_INVALID;
562 /* ECMA-262 3rd Edition 12.2 */
563 static HRESULT interp_var_set(exec_ctx_t *ctx)
565 const BSTR name = ctx->code->instrs[ctx->ip].arg1.bstr;
569 TRACE("%s\n", debugstr_w(name));
572 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei);
577 /* ECMA-262 3rd Edition 12.6.4 */
578 static HRESULT interp_forin(exec_ctx_t *ctx)
580 const HRESULT arg = ctx->code->instrs[ctx->ip].arg1.uint;
581 IDispatch *var_obj, *obj = NULL;
590 val = stack_pop(ctx);
592 assert(V_VT(stack_top(ctx)) == VT_I4);
593 id = V_I4(stack_top(ctx));
595 var_obj = stack_topn_objid(ctx, 1, &var_id);
597 FIXME("invalid ref\n");
602 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
603 obj = V_DISPATCH(stack_topn(ctx, 3));
606 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
607 if(SUCCEEDED(hres)) {
608 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
610 hres = IDispatchEx_GetMemberName(dispex, id, &name);
611 IDispatchEx_Release(dispex);
617 TRACE("No IDispatchEx\n");
626 V_I4(stack_top(ctx)) = id;
630 hres = disp_propput(ctx->script, var_obj, var_id, &v, ctx->ei);
639 return stack_push(ctx, val);
644 /* ECMA-262 3rd Edition 12.10 */
645 static HRESULT interp_push_scope(exec_ctx_t *ctx)
655 hres = to_object(ctx->script, v, &disp);
660 obj = to_jsdisp(disp);
662 IDispatch_Release(disp);
663 FIXME("disp is not jsdisp\n");
667 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
672 /* ECMA-262 3rd Edition 12.10 */
673 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
677 scope_pop(&ctx->scope_chain);
681 /* ECMA-262 3rd Edition 12.13 */
682 static HRESULT interp_case(exec_ctx_t *ctx)
684 const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
692 hres = equal2_values(stack_top(ctx), v, &b);
706 /* ECMA-262 3rd Edition 12.13 */
707 static HRESULT interp_throw(exec_ctx_t *ctx)
711 ctx->ei->var = *stack_pop(ctx);
712 return DISP_E_EXCEPTION;
715 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
717 const HRESULT arg = ctx->code->instrs[ctx->ip].arg1.uint;
719 TRACE("%08x\n", arg);
721 return throw_reference_error(ctx->script, ctx->ei, arg, NULL);
724 static HRESULT interp_throw_type(exec_ctx_t *ctx)
726 const HRESULT hres = ctx->code->instrs[ctx->ip].arg1.uint;
727 const WCHAR *str = ctx->code->instrs[ctx->ip].arg2.str;
729 TRACE("%08x %s\n", hres, debugstr_w(str));
731 return throw_type_error(ctx->script, ctx->ei, hres, str);
734 /* ECMA-262 3rd Edition 12.14 */
735 static HRESULT interp_push_except(exec_ctx_t *ctx)
737 const unsigned arg1 = ctx->code->instrs[ctx->ip].arg1.uint;
738 const BSTR arg2 = ctx->code->instrs[ctx->ip].arg2.bstr;
739 except_frame_t *except;
744 stack_top = ctx->top;
749 hres = stack_push_bool(ctx, TRUE);
752 hres = stack_push_bool(ctx, TRUE);
757 except = heap_alloc(sizeof(*except));
759 return E_OUTOFMEMORY;
761 except->stack_top = stack_top;
762 except->scope = ctx->scope_chain;
763 except->catch_off = arg1;
764 except->ident = arg2;
765 except->next = ctx->except_frame;
766 ctx->except_frame = except;
770 /* ECMA-262 3rd Edition 12.14 */
771 static HRESULT interp_pop_except(exec_ctx_t *ctx)
773 except_frame_t *except;
777 except = ctx->except_frame;
778 assert(except != NULL);
780 ctx->except_frame = except->next;
785 /* ECMA-262 3rd Edition 12.14 */
786 static HRESULT interp_end_finally(exec_ctx_t *ctx)
794 assert(V_VT(stack_top(ctx)) == VT_BOOL);
795 if(!V_BOOL(stack_top(ctx))) {
796 TRACE("passing exception\n");
800 ctx->ei->var = *stack_pop(ctx);
801 return DISP_E_EXCEPTION;
805 return stack_push(ctx, v);
808 /* ECMA-262 3rd Edition 13 */
809 static HRESULT interp_func(exec_ctx_t *ctx)
811 function_expression_t *expr = ctx->code->instrs[ctx->ip].arg1.func;
818 hres = create_source_function(ctx->script, ctx->code, expr->parameter_list, expr->source_elements, ctx->scope_chain,
819 expr->src_str, expr->src_len, &dispex);
823 var_set_jsdisp(&v, dispex);
824 return stack_push(ctx, &v);
827 /* ECMA-262 3rd Edition 11.2.1 */
828 static HRESULT interp_array(exec_ctx_t *ctx)
838 namev = stack_pop(ctx);
840 hres = stack_pop_object(ctx, &obj);
846 hres = to_string(ctx->script, namev, ctx->ei, &name);
849 IDispatch_Release(obj);
853 hres = disp_get_id(ctx->script, obj, name, 0, &id);
855 if(SUCCEEDED(hres)) {
856 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
857 }else if(hres == DISP_E_UNKNOWNNAME) {
861 IDispatch_Release(obj);
865 return stack_push(ctx, &v);
868 /* ECMA-262 3rd Edition 11.2.1 */
869 static HRESULT interp_member(exec_ctx_t *ctx)
871 const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
879 hres = stack_pop_object(ctx, &obj);
883 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
884 if(SUCCEEDED(hres)) {
886 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
887 }else if(hres == DISP_E_UNKNOWNNAME) {
891 IDispatch_Release(obj);
895 return stack_push(ctx, &v);
898 /* ECMA-262 3rd Edition 11.2.1 */
899 static HRESULT interp_memberid(exec_ctx_t *ctx)
901 const unsigned arg = ctx->code->instrs[ctx->ip].arg1.lng;
902 VARIANT *objv, *namev;
910 namev = stack_pop(ctx);
911 objv = stack_pop(ctx);
913 hres = to_object(ctx->script, objv, &obj);
915 if(SUCCEEDED(hres)) {
916 hres = to_string(ctx->script, namev, ctx->ei, &name);
918 IDispatch_Release(obj);
924 hres = disp_get_id(ctx->script, obj, name, arg, &id);
927 IDispatch_Release(obj);
928 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
930 id = JS_E_INVALID_PROPERTY;
936 return stack_push_objid(ctx, obj, id);
939 /* ECMA-262 3rd Edition 11.2.1 */
940 static HRESULT interp_refval(exec_ctx_t *ctx)
949 disp = stack_topn_objid(ctx, 0, &id);
951 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
953 hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
957 return stack_push(ctx, &v);
960 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
966 dp->rgdispidNamedArgs = NULL;
969 assert(ctx->top >= arg_cnt);
971 for(i=1; i*2 <= arg_cnt; i++) {
972 tmp = ctx->stack[ctx->top-i];
973 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
974 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
977 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
980 /* ECMA-262 3rd Edition 11.2.2 */
981 static HRESULT interp_new(exec_ctx_t *ctx)
983 const LONG arg = ctx->code->instrs[ctx->ip].arg1.lng;
990 constr = stack_topn(ctx, arg);
992 /* NOTE: Should use to_object here */
994 if(V_VT(constr) == VT_NULL)
995 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
996 else if(V_VT(constr) != VT_DISPATCH)
997 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
998 else if(!V_DISPATCH(constr))
999 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1001 jsstack_to_dp(ctx, arg, &dp);
1002 hres = disp_call(ctx->script, V_DISPATCH(constr), DISPID_VALUE,
1003 DISPATCH_CONSTRUCT, &dp, &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 = ctx->code->instrs[ctx->ip].arg1.uint;
1015 const int do_ret = ctx->code->instrs[ctx->ip].arg2.lng;
1020 TRACE("%d %d\n", argn, do_ret);
1022 objv = stack_topn(ctx, argn);
1023 if(V_VT(objv) != VT_DISPATCH)
1024 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1026 jsstack_to_dp(ctx, argn, &dp);
1027 hres = disp_call(ctx->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1028 do_ret ? &v : NULL, ctx->ei);
1032 stack_popn(ctx, argn+1);
1033 return do_ret ? stack_push(ctx, &v) : S_OK;
1037 /* ECMA-262 3rd Edition 11.2.3 */
1038 static HRESULT interp_call_member(exec_ctx_t *ctx)
1040 const unsigned argn = ctx->code->instrs[ctx->ip].arg1.uint;
1041 const int do_ret = ctx->code->instrs[ctx->ip].arg2.lng;
1048 TRACE("%d %d\n", argn, do_ret);
1050 obj = stack_topn_objid(ctx, argn, &id);
1052 return throw_type_error(ctx->script, ctx->ei, id, NULL);
1054 jsstack_to_dp(ctx, argn, &dp);
1055 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, ctx->ei);
1059 stack_popn(ctx, argn+2);
1060 return do_ret ? stack_push(ctx, &v) : S_OK;
1064 /* ECMA-262 3rd Edition 11.1.1 */
1065 static HRESULT interp_this(exec_ctx_t *ctx)
1071 V_VT(&v) = VT_DISPATCH;
1072 V_DISPATCH(&v) = ctx->this_obj;
1073 IDispatch_AddRef(ctx->this_obj);
1074 return stack_push(ctx, &v);
1077 /* ECMA-262 3rd Edition 10.1.4 */
1078 static HRESULT interp_ident(exec_ctx_t *ctx)
1080 const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
1085 TRACE("%s\n", debugstr_w(arg));
1087 hres = identifier_eval(ctx->script, arg, &exprval);
1091 if(exprval.type == EXPRVAL_INVALID)
1092 return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1094 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1095 exprval_release(&exprval);
1099 return stack_push(ctx, &v);
1102 /* ECMA-262 3rd Edition 10.1.4 */
1103 static HRESULT interp_identid(exec_ctx_t *ctx)
1105 const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
1106 const unsigned flags = ctx->code->instrs[ctx->ip].arg2.uint;
1110 TRACE("%s %x\n", debugstr_w(arg), flags);
1112 hres = identifier_eval(ctx->script, arg, &exprval);
1116 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1119 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1123 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1126 if(exprval.type != EXPRVAL_IDREF) {
1127 WARN("invalid ref\n");
1128 exprval_release(&exprval);
1129 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1132 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1135 /* ECMA-262 3rd Edition 7.8.1 */
1136 static HRESULT interp_null(exec_ctx_t *ctx)
1143 return stack_push(ctx, &v);
1146 /* ECMA-262 3rd Edition 7.8.2 */
1147 static HRESULT interp_bool(exec_ctx_t *ctx)
1149 const LONG arg = ctx->code->instrs[ctx->ip].arg1.lng;
1151 TRACE("%s\n", arg ? "true" : "false");
1153 return stack_push_bool(ctx, arg);
1156 /* ECMA-262 3rd Edition 7.8.3 */
1157 static HRESULT interp_int(exec_ctx_t *ctx)
1159 const LONG arg = ctx->code->instrs[ctx->ip].arg1.lng;
1166 return stack_push(ctx, &v);
1169 /* ECMA-262 3rd Edition 7.8.3 */
1170 static HRESULT interp_double(exec_ctx_t *ctx)
1172 const double arg = *ctx->code->instrs[ctx->ip].arg1.dbl;
1175 TRACE("%lf\n", arg);
1179 return stack_push(ctx, &v);
1182 /* ECMA-262 3rd Edition 7.8.4 */
1183 static HRESULT interp_str(exec_ctx_t *ctx)
1185 const WCHAR *str = ctx->code->instrs[ctx->ip].arg1.str;
1188 TRACE("%s\n", debugstr_w(str));
1191 V_BSTR(&v) = SysAllocString(str);
1193 return E_OUTOFMEMORY;
1195 return stack_push(ctx, &v);
1198 /* ECMA-262 3rd Edition 7.8 */
1199 static HRESULT interp_regexp(exec_ctx_t *ctx)
1201 const WCHAR *source = ctx->code->instrs[ctx->ip].arg1.str;
1202 const LONG flags = ctx->code->instrs[ctx->ip].arg2.lng;
1207 TRACE("%s %x\n", debugstr_w(source), flags);
1209 hres = create_regexp(ctx->script, source, strlenW(source), flags, ®exp);
1213 var_set_jsdisp(&v, regexp);
1214 return stack_push(ctx, &v);
1217 /* ECMA-262 3rd Edition 11.1.4 */
1218 static HRESULT interp_carray(exec_ctx_t *ctx)
1220 const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
1228 hres = create_array(ctx->script, arg, &array);
1235 hres = jsdisp_propput_idx(array, i, v, ctx->ei);
1238 jsdisp_release(array);
1243 var_set_jsdisp(&r, array);
1244 return stack_push(ctx, &r);
1247 /* ECMA-262 3rd Edition 11.1.5 */
1248 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1256 hres = create_object(ctx->script, NULL, &obj);
1260 var_set_jsdisp(&v, obj);
1261 return stack_push(ctx, &v);
1264 /* ECMA-262 3rd Edition 11.1.5 */
1265 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1267 const BSTR name = ctx->code->instrs[ctx->ip].arg1.bstr;
1272 TRACE("%s\n", debugstr_w(name));
1276 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1277 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1279 hres = jsdisp_propput_name(obj, name, v, ctx->ei);
1284 /* ECMA-262 3rd Edition 11.11 */
1285 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1287 const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
1293 hres = to_boolean(stack_top(ctx), &b);
1306 /* ECMA-262 3rd Edition 11.11 */
1307 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1309 const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
1315 hres = to_boolean(stack_top(ctx), &b);
1328 /* ECMA-262 3rd Edition 11.10 */
1329 static HRESULT interp_or(exec_ctx_t *ctx)
1336 hres = stack_pop_int(ctx, &r);
1340 hres = stack_pop_int(ctx, &l);
1344 return stack_push_int(ctx, l|r);
1347 /* ECMA-262 3rd Edition 11.10 */
1348 static HRESULT interp_xor(exec_ctx_t *ctx)
1355 hres = stack_pop_int(ctx, &r);
1359 hres = stack_pop_int(ctx, &l);
1363 return stack_push_int(ctx, l^r);
1366 /* ECMA-262 3rd Edition 11.10 */
1367 static HRESULT interp_and(exec_ctx_t *ctx)
1374 hres = stack_pop_int(ctx, &r);
1378 hres = stack_pop_int(ctx, &l);
1382 return stack_push_int(ctx, l&r);
1385 /* ECMA-262 3rd Edition 11.8.6 */
1386 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1388 jsdisp_t *obj, *iter, *tmp = NULL;
1393 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1396 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1398 return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1401 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1402 IDispatch_Release(V_DISPATCH(v));
1404 FIXME("non-jsdisp objects not supported\n");
1408 if(is_class(obj, JSCLASS_FUNCTION)) {
1409 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1411 hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1413 jsdisp_release(obj);
1419 if(V_VT(&prot) == VT_DISPATCH) {
1420 if(V_VT(v) == VT_DISPATCH)
1421 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1422 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1423 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1429 jsdisp_release(tmp);
1431 FIXME("prototype is not an object\n");
1435 VariantClear(&prot);
1440 return stack_push_bool(ctx, ret);
1443 /* ECMA-262 3rd Edition 11.8.7 */
1444 static HRESULT interp_in(exec_ctx_t *ctx)
1454 obj = stack_pop(ctx);
1457 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1460 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1463 hres = to_string(ctx->script, v, ctx->ei, &str);
1466 IDispatch_Release(V_DISPATCH(obj));
1470 hres = disp_get_id(ctx->script, V_DISPATCH(obj), str, 0, &id);
1471 IDispatch_Release(V_DISPATCH(obj));
1475 else if(hres == DISP_E_UNKNOWNNAME)
1480 return stack_push_bool(ctx, ret);
1483 /* ECMA-262 3rd Edition 11.6.1 */
1484 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1489 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1493 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1499 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1500 BSTR lstr = NULL, rstr = NULL;
1502 if(V_VT(&l) == VT_BSTR)
1505 hres = to_string(ctx, &l, ei, &lstr);
1507 if(SUCCEEDED(hres)) {
1508 if(V_VT(&r) == VT_BSTR)
1511 hres = to_string(ctx, &r, ei, &rstr);
1514 if(SUCCEEDED(hres)) {
1517 len1 = SysStringLen(lstr);
1518 len2 = SysStringLen(rstr);
1520 V_VT(retv) = VT_BSTR;
1521 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1523 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1525 memcpy(V_BSTR(retv)+len1, rstr, len2*sizeof(WCHAR));
1526 V_BSTR(retv)[len1+len2] = 0;
1529 if(V_VT(&l) != VT_BSTR)
1530 SysFreeString(lstr);
1531 if(V_VT(&r) != VT_BSTR)
1532 SysFreeString(rstr);
1536 hres = to_number(ctx, &l, ei, &nl);
1537 if(SUCCEEDED(hres)) {
1538 hres = to_number(ctx, &r, ei, &nr);
1540 num_set_val(retv, nl + nr);
1549 /* ECMA-262 3rd Edition 11.6.1 */
1550 static HRESULT interp_add(exec_ctx_t *ctx)
1552 VARIANT *l, *r, ret;
1558 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1560 hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1566 return stack_push(ctx, &ret);
1569 /* ECMA-262 3rd Edition 11.6.2 */
1570 static HRESULT interp_sub(exec_ctx_t *ctx)
1577 hres = stack_pop_number(ctx, &r);
1581 hres = stack_pop_number(ctx, &l);
1585 return stack_push_number(ctx, num_val(&l)-num_val(&r));
1588 /* ECMA-262 3rd Edition 11.5.1 */
1589 static HRESULT interp_mul(exec_ctx_t *ctx)
1596 hres = stack_pop_number(ctx, &r);
1600 hres = stack_pop_number(ctx, &l);
1604 return stack_push_number(ctx, num_val(&l)*num_val(&r));
1607 /* ECMA-262 3rd Edition 11.5.2 */
1608 static HRESULT interp_div(exec_ctx_t *ctx)
1615 hres = stack_pop_number(ctx, &r);
1619 hres = stack_pop_number(ctx, &l);
1623 return stack_push_number(ctx, num_val(&l)/num_val(&r));
1626 /* ECMA-262 3rd Edition 11.5.3 */
1627 static HRESULT interp_mod(exec_ctx_t *ctx)
1634 hres = stack_pop_number(ctx, &r);
1638 hres = stack_pop_number(ctx, &l);
1642 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
1645 /* ECMA-262 3rd Edition 11.4.2 */
1646 static HRESULT interp_delete(exec_ctx_t *ctx)
1648 VARIANT *obj_var, *name_var;
1649 IDispatchEx *dispex;
1657 name_var = stack_pop(ctx);
1658 obj_var = stack_pop(ctx);
1660 hres = to_object(ctx->script, obj_var, &obj);
1661 VariantClear(obj_var);
1663 VariantClear(name_var);
1667 hres = to_string(ctx->script, name_var, ctx->ei, &name);
1668 VariantClear(name_var);
1670 IDispatch_Release(obj);
1674 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1675 if(SUCCEEDED(hres)) {
1676 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1678 IDispatchEx_Release(dispex);
1684 IDispatch_Release(obj);
1685 SysFreeString(name);
1689 return stack_push_bool(ctx, ret);
1692 /* ECMA-262 3rd Edition 11.4.2 */
1693 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1695 const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
1696 IDispatchEx *dispex;
1701 TRACE("%s\n", debugstr_w(arg));
1703 hres = identifier_eval(ctx->script, arg, &exprval);
1707 if(exprval.type != EXPRVAL_IDREF) {
1708 FIXME("Unsupported exprval\n");
1709 exprval_release(&exprval);
1713 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1714 IDispatch_Release(exprval.u.idref.disp);
1715 if(SUCCEEDED(hres)) {
1716 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1717 IDispatchEx_Release(dispex);
1724 return stack_push_bool(ctx, ret);
1727 /* ECMA-262 3rd Edition 11.4.2 */
1728 static HRESULT interp_void(exec_ctx_t *ctx)
1736 V_VT(&v) = VT_EMPTY;
1737 return stack_push(ctx, &v);
1740 /* ECMA-262 3rd Edition 11.4.3 */
1741 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1763 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1764 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1765 jsdisp_release(dispex);
1772 FIXME("unhandled vt %d\n", V_VT(v));
1779 /* ECMA-262 3rd Edition 11.4.3 */
1780 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1788 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1792 obj = stack_pop_objid(ctx, &id);
1794 return stack_push_string(ctx, undefinedW);
1796 V_VT(&v) = VT_EMPTY;
1797 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1798 IDispatch_Release(obj);
1800 return stack_push_string(ctx, unknownW);
1802 hres = typeof_string(&v, &ret);
1807 return stack_push_string(ctx, ret);
1810 /* ECMA-262 3rd Edition 11.4.3 */
1811 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1813 const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
1819 TRACE("%s\n", debugstr_w(arg));
1821 hres = identifier_eval(ctx->script, arg, &exprval);
1825 if(exprval.type == EXPRVAL_INVALID) {
1826 hres = stack_push_string(ctx, undefinedW);
1827 exprval_release(&exprval);
1831 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1832 exprval_release(&exprval);
1836 hres = typeof_string(&v, &ret);
1841 return stack_push_string(ctx, ret);
1844 /* ECMA-262 3rd Edition 11.4.3 */
1845 static HRESULT interp_typeof(exec_ctx_t *ctx)
1854 hres = typeof_string(v, &ret);
1859 return stack_push_string(ctx, ret);
1862 /* ECMA-262 3rd Edition 11.4.7 */
1863 static HRESULT interp_minus(exec_ctx_t *ctx)
1870 hres = stack_pop_number(ctx, &n);
1874 return stack_push_number(ctx, -num_val(&n));
1877 /* ECMA-262 3rd Edition 11.4.6 */
1878 static HRESULT interp_tonum(exec_ctx_t *ctx)
1887 hres = to_number(ctx->script, v, ctx->ei, &n);
1892 return stack_push_number(ctx, n);
1895 /* ECMA-262 3rd Edition 11.3.1 */
1896 static HRESULT interp_postinc(exec_ctx_t *ctx)
1898 const int arg = ctx->code->instrs[ctx->ip].arg1.lng;
1906 obj = stack_pop_objid(ctx, &id);
1908 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1910 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1911 if(SUCCEEDED(hres)) {
1915 hres = to_number(ctx->script, &v, ctx->ei, &n);
1916 if(SUCCEEDED(hres)) {
1917 num_set_val(&inc, n+(double)arg);
1918 hres = disp_propput(ctx->script, obj, id, &inc, ctx->ei);
1923 IDispatch_Release(obj);
1927 return stack_push(ctx, &v);
1930 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1931 static HRESULT interp_preinc(exec_ctx_t *ctx)
1933 const int arg = ctx->code->instrs[ctx->ip].arg1.lng;
1941 obj = stack_pop_objid(ctx, &id);
1943 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1945 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1946 if(SUCCEEDED(hres)) {
1949 hres = to_number(ctx->script, &v, ctx->ei, &n);
1951 if(SUCCEEDED(hres)) {
1952 num_set_val(&v, n+(double)arg);
1953 hres = disp_propput(ctx->script, obj, id, &v, ctx->ei);
1956 IDispatch_Release(obj);
1960 return stack_push(ctx, &v);
1963 /* ECMA-262 3rd Edition 11.9.3 */
1964 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1966 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1967 return equal2_values(lval, rval, ret);
1969 /* FIXME: NULL disps should be handled in more general way */
1970 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1973 return equal_values(ctx, &v, rval, ei, ret);
1976 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1979 return equal_values(ctx, lval, &v, ei, ret);
1982 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1983 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1988 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1993 hres = to_number(ctx, lval, ei, &n);
1997 /* FIXME: optimize */
2000 return equal_values(ctx, &v, rval, ei, ret);
2003 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2008 hres = to_number(ctx, rval, ei, &n);
2012 /* FIXME: optimize */
2015 return equal_values(ctx, lval, &v, ei, ret);
2018 if(V_VT(rval) == VT_BOOL) {
2022 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2023 return equal_values(ctx, lval, &v, ei, ret);
2026 if(V_VT(lval) == VT_BOOL) {
2030 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2031 return equal_values(ctx, &v, rval, ei, ret);
2035 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2039 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2043 hres = equal_values(ctx, lval, &v, ei, ret);
2050 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2054 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2058 hres = equal_values(ctx, &v, rval, ei, ret);
2069 /* ECMA-262 3rd Edition 11.9.1 */
2070 static HRESULT interp_eq(exec_ctx_t *ctx)
2079 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2081 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2087 return stack_push_bool(ctx, b);
2090 /* ECMA-262 3rd Edition 11.9.2 */
2091 static HRESULT interp_neq(exec_ctx_t *ctx)
2100 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2102 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2108 return stack_push_bool(ctx, !b);
2111 /* ECMA-262 3rd Edition 11.9.4 */
2112 static HRESULT interp_eq2(exec_ctx_t *ctx)
2123 hres = equal2_values(r, l, &b);
2129 return stack_push_bool(ctx, b);
2132 /* ECMA-262 3rd Edition 11.9.5 */
2133 static HRESULT interp_neq2(exec_ctx_t *ctx)
2144 hres = equal2_values(r, l, &b);
2150 return stack_push_bool(ctx, !b);
2153 /* ECMA-262 3rd Edition 11.8.5 */
2154 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2160 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2164 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2170 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2171 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2172 SysFreeString(V_BSTR(&l));
2173 SysFreeString(V_BSTR(&r));
2177 hres = to_number(ctx, &l, ei, &ln);
2180 hres = to_number(ctx, &r, ei, &rn);
2185 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2189 /* ECMA-262 3rd Edition 11.8.1 */
2190 static HRESULT interp_lt(exec_ctx_t *ctx)
2199 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2201 hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2207 return stack_push_bool(ctx, b);
2210 /* ECMA-262 3rd Edition 11.8.1 */
2211 static HRESULT interp_lteq(exec_ctx_t *ctx)
2220 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2222 hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2228 return stack_push_bool(ctx, b);
2231 /* ECMA-262 3rd Edition 11.8.2 */
2232 static HRESULT interp_gt(exec_ctx_t *ctx)
2241 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2243 hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2249 return stack_push_bool(ctx, b);
2252 /* ECMA-262 3rd Edition 11.8.4 */
2253 static HRESULT interp_gteq(exec_ctx_t *ctx)
2262 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2264 hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2270 return stack_push_bool(ctx, b);
2273 /* ECMA-262 3rd Edition 11.4.8 */
2274 static HRESULT interp_bneg(exec_ctx_t *ctx)
2283 hres = to_int32(ctx->script, v, ctx->ei, &i);
2290 return stack_push(ctx, &r);
2293 /* ECMA-262 3rd Edition 11.4.9 */
2294 static HRESULT interp_neg(exec_ctx_t *ctx)
2303 hres = to_boolean(v, &b);
2308 return stack_push_bool(ctx, !b);
2311 /* ECMA-262 3rd Edition 11.7.1 */
2312 static HRESULT interp_lshift(exec_ctx_t *ctx)
2318 hres = stack_pop_uint(ctx, &r);
2322 hres = stack_pop_int(ctx, &l);
2326 return stack_push_int(ctx, l << (r&0x1f));
2329 /* ECMA-262 3rd Edition 11.7.2 */
2330 static HRESULT interp_rshift(exec_ctx_t *ctx)
2336 hres = stack_pop_uint(ctx, &r);
2340 hres = stack_pop_int(ctx, &l);
2344 return stack_push_int(ctx, l >> (r&0x1f));
2347 /* ECMA-262 3rd Edition 11.7.3 */
2348 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2353 hres = stack_pop_uint(ctx, &r);
2357 hres = stack_pop_uint(ctx, &l);
2361 return stack_push_int(ctx, l >> (r&0x1f));
2364 /* ECMA-262 3rd Edition 11.13.1 */
2365 static HRESULT interp_assign(exec_ctx_t *ctx)
2375 disp = stack_pop_objid(ctx, &id);
2378 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2380 hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2381 IDispatch_Release(disp);
2387 return stack_push(ctx, v);
2390 static HRESULT interp_undefined(exec_ctx_t *ctx)
2396 V_VT(&v) = VT_EMPTY;
2397 return stack_push(ctx, &v);
2400 static HRESULT interp_jmp(exec_ctx_t *ctx)
2402 const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
2410 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2412 const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
2420 hres = to_boolean(v, &b);
2432 static HRESULT interp_pop(exec_ctx_t *ctx)
2440 static HRESULT interp_ret(exec_ctx_t *ctx)
2448 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2450 static const op_func_t op_funcs[] = {
2451 #define X(x,a,b,c) interp_##x,
2456 static const unsigned op_move[] = {
2457 #define X(a,x,b,c) x,
2462 static HRESULT unwind_exception(exec_ctx_t *ctx)
2464 except_frame_t *except_frame;
2469 except_frame = ctx->except_frame;
2470 ctx->except_frame = except_frame->next;
2472 assert(except_frame->stack_top <= ctx->top);
2473 stack_popn(ctx, ctx->top - except_frame->stack_top);
2475 while(except_frame->scope != ctx->scope_chain)
2476 scope_pop(&ctx->scope_chain);
2478 ctx->ip = except_frame->catch_off;
2480 except_val = ctx->ei->var;
2481 memset(ctx->ei, 0, sizeof(*ctx->ei));
2483 ident = except_frame->ident;
2484 heap_free(except_frame);
2487 jsdisp_t *scope_obj;
2489 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2490 if(SUCCEEDED(hres)) {
2491 hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei);
2493 jsdisp_release(scope_obj);
2495 VariantClear(&except_val);
2499 hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2500 jsdisp_release(scope_obj);
2504 hres = stack_push(ctx, &except_val);
2508 hres = stack_push_bool(ctx, FALSE);
2512 V_VT(&v) = VT_EMPTY;
2513 hres = stack_push(ctx, &v);
2519 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, unsigned ip, jsexcept_t *ei, VARIANT *ret)
2521 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2522 except_frame_t *prev_except_frame;
2523 unsigned prev_ip, prev_top;
2524 scope_chain_t *prev_scope;
2525 bytecode_t *prev_code;
2526 jsexcept_t *prev_ei;
2528 HRESULT hres = S_OK;
2532 prev_top = exec_ctx->top;
2533 prev_scope = exec_ctx->scope_chain;
2534 prev_except_frame = exec_ctx->except_frame;
2535 prev_ip = exec_ctx->ip;
2536 prev_ei = exec_ctx->ei;
2537 prev_code = exec_ctx->code;
2540 exec_ctx->except_frame = NULL;
2541 exec_ctx->code = code;
2543 while(exec_ctx->ip != -1) {
2544 op = code->instrs[exec_ctx->ip].op;
2545 hres = op_funcs[op](exec_ctx);
2547 TRACE("EXCEPTION\n");
2549 if(!exec_ctx->except_frame)
2552 hres = unwind_exception(exec_ctx);
2556 exec_ctx->ip += op_move[op];
2560 exec_ctx->ip = prev_ip;
2561 exec_ctx->ei = prev_ei;
2562 exec_ctx->except_frame = prev_except_frame;
2563 exec_ctx->code = prev_code;
2566 while(exec_ctx->scope_chain != prev_scope)
2567 scope_pop(&exec_ctx->scope_chain);
2568 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2572 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2573 assert(exec_ctx->scope_chain == prev_scope);
2575 if(exec_ctx->top == prev_top)
2576 V_VT(ret) = VT_EMPTY;
2578 *ret = *stack_pop(exec_ctx);
2582 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, source_elements_t *source, BOOL from_eval,
2583 jsexcept_t *ei, VARIANT *retv)
2585 function_declaration_t *func;
2588 exec_ctx_t *prev_ctx;
2589 HRESULT hres = S_OK;
2591 for(func = source->functions; func; func = func->next) {
2595 if(!func->expr->identifier)
2598 hres = create_source_function(ctx->script, code, func->expr->parameter_list, func->expr->source_elements,
2599 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
2603 var_set_jsdisp(&var, func_obj);
2604 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei);
2605 jsdisp_release(func_obj);
2610 for(var = source->variables; var; var = var->next) {
2614 name = SysAllocString(var->identifier);
2616 return E_OUTOFMEMORY;
2618 if(!ctx->is_global || !lookup_global_members(ctx->script, name, NULL))
2619 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
2620 SysFreeString(name);
2625 prev_ctx = ctx->script->exec_ctx;
2626 ctx->script->exec_ctx = ctx;
2628 if(source->statement) {
2629 assert(source->instr_off);
2630 hres = enter_bytecode(ctx->script, code, source->instr_off, ei, &val);
2632 V_VT(&val) = VT_EMPTY;
2635 assert(ctx->script->exec_ctx == ctx);
2636 ctx->script->exec_ctx = prev_ctx;