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_var(ctx->script, stack_pop(ctx), ctx->ei, r);
190 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
192 return to_uint32(ctx->script, stack_pop(ctx), ctx->ei, r);
195 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
197 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
199 *id = V_INT(stack_pop(ctx));
200 return V_DISPATCH(stack_pop(ctx));
203 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
205 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
207 *id = V_INT(stack_topn(ctx, n));
208 return V_DISPATCH(stack_topn(ctx, n+1));
211 static void exprval_release(exprval_t *val)
214 case EXPRVAL_VARIANT:
215 if(V_VT(&val->u.var) != VT_EMPTY)
216 VariantClear(&val->u.var);
219 if(val->u.idref.disp)
220 IDispatch_Release(val->u.idref.disp);
222 case EXPRVAL_INVALID:
227 /* ECMA-262 3rd Edition 8.7.1 */
228 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
230 V_VT(ret) = VT_EMPTY;
233 case EXPRVAL_VARIANT:
234 return VariantCopy(ret, &val->u.var);
236 if(!val->u.idref.disp) {
237 FIXME("throw ReferenceError\n");
241 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei);
242 case EXPRVAL_INVALID:
246 ERR("type %d\n", val->type);
250 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
252 if(val->type == EXPRVAL_VARIANT) {
254 V_VT(&val->u.var) = VT_EMPTY;
258 return exprval_value(ctx, val, ei, ret);
261 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
263 val->type = EXPRVAL_IDREF;
264 val->u.idref.disp = disp;
265 val->u.idref.id = id;
268 IDispatch_AddRef(disp);
271 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
273 scope_chain_t *new_scope;
275 new_scope = heap_alloc(sizeof(scope_chain_t));
277 return E_OUTOFMEMORY;
281 IDispatch_AddRef(obj);
282 new_scope->jsobj = jsobj;
283 new_scope->obj = obj;
287 new_scope->next = scope;
289 new_scope->next = NULL;
296 static void scope_pop(scope_chain_t **scope)
305 void scope_release(scope_chain_t *scope)
311 scope_release(scope->next);
313 IDispatch_Release(scope->obj);
317 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
318 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
322 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
324 return E_OUTOFMEMORY;
327 ctx->is_global = is_global;
330 ctx->this_obj = this_obj;
331 else if(script_ctx->host_global)
332 ctx->this_obj = script_ctx->host_global;
334 ctx->this_obj = to_disp(script_ctx->global);
335 IDispatch_AddRef(ctx->this_obj);
337 jsdisp_addref(var_disp);
338 ctx->var_disp = var_disp;
340 script_addref(script_ctx);
341 ctx->script = script_ctx;
345 ctx->scope_chain = scope;
352 void exec_release(exec_ctx_t *ctx)
358 scope_release(ctx->scope_chain);
360 jsdisp_release(ctx->var_disp);
362 IDispatch_Release(ctx->this_obj);
364 script_release(ctx->script);
365 heap_free(ctx->stack);
369 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
374 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
376 TRACE("using IDispatch\n");
379 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
383 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
384 IDispatchEx_Release(dispex);
388 static inline BOOL var_is_null(const VARIANT *v)
390 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
393 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
395 IObjectIdentity *identity;
396 IUnknown *unk1, *unk2;
404 if(!disp1 || !disp2) {
409 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
413 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
415 IUnknown_Release(unk1);
422 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
423 if(SUCCEEDED(hres)) {
424 hres = IObjectIdentity_IsEqualObject(identity, unk2);
425 IObjectIdentity_Release(identity);
432 IUnknown_Release(unk1);
433 IUnknown_Release(unk2);
437 /* ECMA-262 3rd Edition 11.9.6 */
438 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
442 if(V_VT(lval) != V_VT(rval)) {
443 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
444 *ret = num_val(lval) == num_val(rval);
445 else if(var_is_null(lval))
446 *ret = var_is_null(rval);
458 *ret = V_I4(lval) == V_I4(rval);
461 *ret = V_R8(lval) == V_R8(rval);
465 *ret = !SysStringLen(V_BSTR(rval));
466 else if(!V_BSTR(rval))
467 *ret = !SysStringLen(V_BSTR(lval));
469 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
472 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
474 *ret = !V_BOOL(lval) == !V_BOOL(rval);
477 FIXME("unimplemented vt %d\n", V_VT(lval));
484 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
490 for(item = ctx->named_items; item; item = item->next) {
491 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
492 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
493 if(SUCCEEDED(hres)) {
495 exprval_set_idref(ret, item->disp, id);
504 /* ECMA-262 3rd Edition 10.1.4 */
505 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
507 scope_chain_t *scope;
512 TRACE("%s\n", debugstr_w(identifier));
514 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
516 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
518 hres = disp_get_id(ctx, scope->obj, identifier, fdexNameImplicit, &id);
519 if(SUCCEEDED(hres)) {
520 exprval_set_idref(ret, scope->obj, id);
525 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
526 if(SUCCEEDED(hres)) {
527 exprval_set_idref(ret, to_disp(ctx->global), id);
531 for(item = ctx->named_items; item; item = item->next) {
532 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
539 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
540 SCRIPTINFO_IUNKNOWN, &unk, NULL);
542 WARN("GetItemInfo failed: %08x\n", hres);
546 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
547 IUnknown_Release(unk);
549 WARN("object does not implement IDispatch\n");
554 ret->type = EXPRVAL_VARIANT;
555 V_VT(&ret->u.var) = VT_DISPATCH;
556 V_DISPATCH(&ret->u.var) = item->disp;
557 IDispatch_AddRef(item->disp);
562 if(lookup_global_members(ctx, identifier, ret))
565 ret->type = EXPRVAL_INVALID;
569 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
570 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
573 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
574 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
577 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
578 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
581 static inline const WCHAR *get_op_str(exec_ctx_t *ctx, int i){
582 return ctx->code->instrs[ctx->ip].u.arg[i].str;
585 static inline double get_op_double(exec_ctx_t *ctx){
586 return ctx->code->instrs[ctx->ip].u.dbl;
589 /* ECMA-262 3rd Edition 12.2 */
590 static HRESULT interp_var_set(exec_ctx_t *ctx)
592 const BSTR name = get_op_bstr(ctx, 0);
597 TRACE("%s\n", debugstr_w(name));
600 hres = variant_to_jsval(v, &val);
605 hres = jsdisp_propput_name(ctx->var_disp, name, val, ctx->ei);
610 /* ECMA-262 3rd Edition 12.6.4 */
611 static HRESULT interp_forin(exec_ctx_t *ctx)
613 const HRESULT arg = get_op_uint(ctx, 0);
614 IDispatch *var_obj, *obj = NULL;
623 val = stack_pop(ctx);
625 assert(V_VT(stack_top(ctx)) == VT_I4);
626 id = V_I4(stack_top(ctx));
628 var_obj = stack_topn_objid(ctx, 1, &var_id);
630 FIXME("invalid ref\n");
635 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
636 obj = V_DISPATCH(stack_topn(ctx, 3));
639 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
640 if(SUCCEEDED(hres)) {
641 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
643 hres = IDispatchEx_GetMemberName(dispex, id, &name);
644 IDispatchEx_Release(dispex);
650 TRACE("No IDispatchEx\n");
659 V_I4(stack_top(ctx)) = id;
663 hres = disp_propput(ctx->script, var_obj, var_id, &v, ctx->ei);
672 return stack_push(ctx, val);
677 /* ECMA-262 3rd Edition 12.10 */
678 static HRESULT interp_push_scope(exec_ctx_t *ctx)
687 hres = to_object(ctx->script, v, &disp);
692 hres = scope_push(ctx->scope_chain, to_jsdisp(disp), disp, &ctx->scope_chain);
693 IDispatch_Release(disp);
697 /* ECMA-262 3rd Edition 12.10 */
698 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
702 scope_pop(&ctx->scope_chain);
706 /* ECMA-262 3rd Edition 12.13 */
707 static HRESULT interp_case(exec_ctx_t *ctx)
709 const unsigned arg = get_op_uint(ctx, 0);
717 hres = equal2_values(stack_top(ctx), v, &b);
731 /* ECMA-262 3rd Edition 12.13 */
732 static HRESULT interp_throw(exec_ctx_t *ctx)
736 ctx->ei->var = *stack_pop(ctx);
737 return DISP_E_EXCEPTION;
740 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
742 const HRESULT arg = get_op_uint(ctx, 0);
744 TRACE("%08x\n", arg);
746 return throw_reference_error(ctx->script, ctx->ei, arg, NULL);
749 static HRESULT interp_throw_type(exec_ctx_t *ctx)
751 const HRESULT hres = get_op_uint(ctx, 0);
752 const WCHAR *str = get_op_str(ctx, 1);
754 TRACE("%08x %s\n", hres, debugstr_w(str));
756 return throw_type_error(ctx->script, ctx->ei, hres, str);
759 /* ECMA-262 3rd Edition 12.14 */
760 static HRESULT interp_push_except(exec_ctx_t *ctx)
762 const unsigned arg1 = get_op_uint(ctx, 0);
763 const BSTR arg2 = get_op_bstr(ctx, 1);
764 except_frame_t *except;
769 stack_top = ctx->top;
774 hres = stack_push_bool(ctx, TRUE);
777 hres = stack_push_bool(ctx, TRUE);
782 except = heap_alloc(sizeof(*except));
784 return E_OUTOFMEMORY;
786 except->stack_top = stack_top;
787 except->scope = ctx->scope_chain;
788 except->catch_off = arg1;
789 except->ident = arg2;
790 except->next = ctx->except_frame;
791 ctx->except_frame = except;
795 /* ECMA-262 3rd Edition 12.14 */
796 static HRESULT interp_pop_except(exec_ctx_t *ctx)
798 except_frame_t *except;
802 except = ctx->except_frame;
803 assert(except != NULL);
805 ctx->except_frame = except->next;
810 /* ECMA-262 3rd Edition 12.14 */
811 static HRESULT interp_end_finally(exec_ctx_t *ctx)
819 assert(V_VT(stack_top(ctx)) == VT_BOOL);
820 if(!V_BOOL(stack_top(ctx))) {
821 TRACE("passing exception\n");
825 ctx->ei->var = *stack_pop(ctx);
826 return DISP_E_EXCEPTION;
830 return stack_push(ctx, v);
833 /* ECMA-262 3rd Edition 13 */
834 static HRESULT interp_func(exec_ctx_t *ctx)
836 unsigned func_idx = get_op_uint(ctx, 0);
841 TRACE("%d\n", func_idx);
843 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
844 ctx->scope_chain, &dispex);
848 var_set_jsdisp(&v, dispex);
849 return stack_push(ctx, &v);
852 /* ECMA-262 3rd Edition 11.2.1 */
853 static HRESULT interp_array(exec_ctx_t *ctx)
863 namev = stack_pop(ctx);
865 hres = stack_pop_object(ctx, &obj);
871 hres = to_string(ctx->script, namev, ctx->ei, &name);
874 IDispatch_Release(obj);
878 hres = disp_get_id(ctx->script, obj, name, 0, &id);
880 if(SUCCEEDED(hres)) {
881 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
882 }else if(hres == DISP_E_UNKNOWNNAME) {
886 IDispatch_Release(obj);
890 return stack_push(ctx, &v);
893 /* ECMA-262 3rd Edition 11.2.1 */
894 static HRESULT interp_member(exec_ctx_t *ctx)
896 const BSTR arg = get_op_bstr(ctx, 0);
904 hres = stack_pop_object(ctx, &obj);
908 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
909 if(SUCCEEDED(hres)) {
911 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
912 }else if(hres == DISP_E_UNKNOWNNAME) {
916 IDispatch_Release(obj);
920 return stack_push(ctx, &v);
923 /* ECMA-262 3rd Edition 11.2.1 */
924 static HRESULT interp_memberid(exec_ctx_t *ctx)
926 const unsigned arg = get_op_uint(ctx, 0);
927 VARIANT *objv, *namev;
935 namev = stack_pop(ctx);
936 objv = stack_pop(ctx);
938 hres = to_object(ctx->script, objv, &obj);
940 if(SUCCEEDED(hres)) {
941 hres = to_string(ctx->script, namev, ctx->ei, &name);
943 IDispatch_Release(obj);
949 hres = disp_get_id(ctx->script, obj, name, arg, &id);
952 IDispatch_Release(obj);
953 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
955 id = JS_E_INVALID_PROPERTY;
961 return stack_push_objid(ctx, obj, id);
964 /* ECMA-262 3rd Edition 11.2.1 */
965 static HRESULT interp_refval(exec_ctx_t *ctx)
974 disp = stack_topn_objid(ctx, 0, &id);
976 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
978 hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
982 return stack_push(ctx, &v);
985 static HRESULT convert_args(exec_ctx_t *ctx, unsigned argc, jsval_t **ret)
987 VARIANT *args = stack_args(ctx, argc);
997 argv = heap_alloc(argc * sizeof(jsval_t));
999 return E_OUTOFMEMORY;
1001 for(i=0; i<argc; i++) {
1002 hres = variant_to_jsval(args+i, argv+i);
1005 jsval_release(argv[i]);
1015 /* ECMA-262 3rd Edition 11.2.2 */
1016 static HRESULT interp_new(exec_ctx_t *ctx)
1018 const unsigned argc = get_op_uint(ctx, 0);
1024 TRACE("%d\n", argc);
1026 constr = stack_topn(ctx, argc);
1028 /* NOTE: Should use to_object here */
1030 if(V_VT(constr) == VT_NULL)
1031 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1032 else if(V_VT(constr) != VT_DISPATCH)
1033 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
1034 else if(!V_DISPATCH(constr))
1035 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1037 hres = convert_args(ctx, argc, &argv);
1041 hres = disp_call_value(ctx->script, V_DISPATCH(constr), NULL, DISPATCH_CONSTRUCT, argc, argv, &r, ctx->ei);
1042 for(i=0; i < argc; i++)
1043 jsval_release(argv[i]);
1048 hres = jsval_to_variant(r, &v);
1053 stack_popn(ctx, argc+1);
1054 return stack_push(ctx, &v);
1057 /* ECMA-262 3rd Edition 11.2.3 */
1058 static HRESULT interp_call(exec_ctx_t *ctx)
1060 const unsigned argn = get_op_uint(ctx, 0);
1061 const int do_ret = get_op_int(ctx, 1);
1067 TRACE("%d %d\n", argn, do_ret);
1069 objv = stack_topn(ctx, argn);
1070 if(V_VT(objv) != VT_DISPATCH)
1071 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1073 hres = convert_args(ctx, argn, &argv);
1077 hres = disp_call_value(ctx->script, V_DISPATCH(objv), NULL, DISPATCH_METHOD, argn, argv,
1078 do_ret ? &r : NULL, ctx->ei);
1079 for(i=0; i < argn; i++)
1080 jsval_release(argv[i]);
1085 stack_popn(ctx, argn+1);
1090 hres = jsval_to_variant(r, &v);
1095 return stack_push(ctx, &v);
1099 /* ECMA-262 3rd Edition 11.2.3 */
1100 static HRESULT interp_call_member(exec_ctx_t *ctx)
1102 const unsigned argn = get_op_uint(ctx, 0);
1103 const int do_ret = get_op_int(ctx, 1);
1111 TRACE("%d %d\n", argn, do_ret);
1113 obj = stack_topn_objid(ctx, argn, &id);
1115 return throw_type_error(ctx->script, ctx->ei, id, NULL);
1117 hres = convert_args(ctx, argn, &argv);
1121 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, argv, do_ret ? &v : NULL, ctx->ei);
1122 for(i=0; i < argn; i++)
1123 jsval_release(argv[i]);
1128 stack_popn(ctx, argn+2);
1129 return do_ret ? stack_push(ctx, &v) : S_OK;
1133 /* ECMA-262 3rd Edition 11.1.1 */
1134 static HRESULT interp_this(exec_ctx_t *ctx)
1140 V_VT(&v) = VT_DISPATCH;
1141 V_DISPATCH(&v) = ctx->this_obj;
1142 IDispatch_AddRef(ctx->this_obj);
1143 return stack_push(ctx, &v);
1146 /* ECMA-262 3rd Edition 10.1.4 */
1147 static HRESULT interp_ident(exec_ctx_t *ctx)
1149 const BSTR arg = get_op_bstr(ctx, 0);
1154 TRACE("%s\n", debugstr_w(arg));
1156 hres = identifier_eval(ctx->script, arg, &exprval);
1160 if(exprval.type == EXPRVAL_INVALID)
1161 return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1163 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1164 exprval_release(&exprval);
1168 return stack_push(ctx, &v);
1171 /* ECMA-262 3rd Edition 10.1.4 */
1172 static HRESULT interp_identid(exec_ctx_t *ctx)
1174 const BSTR arg = get_op_bstr(ctx, 0);
1175 const unsigned flags = get_op_uint(ctx, 1);
1179 TRACE("%s %x\n", debugstr_w(arg), flags);
1181 hres = identifier_eval(ctx->script, arg, &exprval);
1185 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1188 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1192 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1195 if(exprval.type != EXPRVAL_IDREF) {
1196 WARN("invalid ref\n");
1197 exprval_release(&exprval);
1198 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1201 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1204 /* ECMA-262 3rd Edition 7.8.1 */
1205 static HRESULT interp_null(exec_ctx_t *ctx)
1212 return stack_push(ctx, &v);
1215 /* ECMA-262 3rd Edition 7.8.2 */
1216 static HRESULT interp_bool(exec_ctx_t *ctx)
1218 const int arg = get_op_int(ctx, 0);
1220 TRACE("%s\n", arg ? "true" : "false");
1222 return stack_push_bool(ctx, arg);
1225 /* ECMA-262 3rd Edition 7.8.3 */
1226 static HRESULT interp_int(exec_ctx_t *ctx)
1228 const int arg = get_op_int(ctx, 0);
1235 return stack_push(ctx, &v);
1238 /* ECMA-262 3rd Edition 7.8.3 */
1239 static HRESULT interp_double(exec_ctx_t *ctx)
1241 const double arg = get_op_double(ctx);
1243 TRACE("%lf\n", arg);
1245 return stack_push_number(ctx, arg);
1248 /* ECMA-262 3rd Edition 7.8.4 */
1249 static HRESULT interp_str(exec_ctx_t *ctx)
1251 const WCHAR *str = get_op_str(ctx, 0);
1254 TRACE("%s\n", debugstr_w(str));
1257 V_BSTR(&v) = SysAllocString(str);
1259 return E_OUTOFMEMORY;
1261 return stack_push(ctx, &v);
1264 /* ECMA-262 3rd Edition 7.8 */
1265 static HRESULT interp_regexp(exec_ctx_t *ctx)
1267 const WCHAR *source = get_op_str(ctx, 0);
1268 const unsigned flags = get_op_uint(ctx, 1);
1273 TRACE("%s %x\n", debugstr_w(source), flags);
1275 hres = create_regexp(ctx->script, source, strlenW(source), flags, ®exp);
1279 var_set_jsdisp(&v, regexp);
1280 return stack_push(ctx, &v);
1283 /* ECMA-262 3rd Edition 11.1.4 */
1284 static HRESULT interp_carray(exec_ctx_t *ctx)
1286 const unsigned arg = get_op_uint(ctx, 0);
1295 hres = create_array(ctx->script, arg, &array);
1302 hres = variant_to_jsval(v, &val);
1303 if(SUCCEEDED(hres)) {
1304 hres = jsdisp_propput_idx(array, i, val, ctx->ei);
1309 jsdisp_release(array);
1314 var_set_jsdisp(&r, array);
1315 return stack_push(ctx, &r);
1318 /* ECMA-262 3rd Edition 11.1.5 */
1319 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1327 hres = create_object(ctx->script, NULL, &obj);
1331 var_set_jsdisp(&v, obj);
1332 return stack_push(ctx, &v);
1335 /* ECMA-262 3rd Edition 11.1.5 */
1336 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1338 const BSTR name = get_op_bstr(ctx, 0);
1344 TRACE("%s\n", debugstr_w(name));
1348 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1349 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1351 hres = variant_to_jsval(v, &val);
1352 if(SUCCEEDED(hres)) {
1353 hres = jsdisp_propput_name(obj, name, val, ctx->ei);
1360 /* ECMA-262 3rd Edition 11.11 */
1361 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1363 const unsigned arg = get_op_uint(ctx, 0);
1369 hres = to_boolean(stack_top(ctx), &b);
1382 /* ECMA-262 3rd Edition 11.11 */
1383 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1385 const unsigned arg = get_op_uint(ctx, 0);
1391 hres = to_boolean(stack_top(ctx), &b);
1404 /* ECMA-262 3rd Edition 11.10 */
1405 static HRESULT interp_or(exec_ctx_t *ctx)
1412 hres = stack_pop_int(ctx, &r);
1416 hres = stack_pop_int(ctx, &l);
1420 return stack_push_int(ctx, l|r);
1423 /* ECMA-262 3rd Edition 11.10 */
1424 static HRESULT interp_xor(exec_ctx_t *ctx)
1431 hres = stack_pop_int(ctx, &r);
1435 hres = stack_pop_int(ctx, &l);
1439 return stack_push_int(ctx, l^r);
1442 /* ECMA-262 3rd Edition 11.10 */
1443 static HRESULT interp_and(exec_ctx_t *ctx)
1450 hres = stack_pop_int(ctx, &r);
1454 hres = stack_pop_int(ctx, &l);
1458 return stack_push_int(ctx, l&r);
1461 /* ECMA-262 3rd Edition 11.8.6 */
1462 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1464 jsdisp_t *obj, *iter, *tmp = NULL;
1470 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1473 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1475 return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1478 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1479 IDispatch_Release(V_DISPATCH(v));
1481 FIXME("non-jsdisp objects not supported\n");
1485 if(is_class(obj, JSCLASS_FUNCTION)) {
1486 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1488 hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1490 jsdisp_release(obj);
1496 if(is_object_instance(prot)) {
1497 if(V_VT(v) == VT_DISPATCH)
1498 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1499 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1500 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1506 jsdisp_release(tmp);
1508 FIXME("prototype is not an object\n");
1512 jsval_release(prot);
1517 return stack_push_bool(ctx, ret);
1520 /* ECMA-262 3rd Edition 11.8.7 */
1521 static HRESULT interp_in(exec_ctx_t *ctx)
1531 obj = stack_pop(ctx);
1534 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1537 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1540 hres = to_string(ctx->script, v, ctx->ei, &str);
1543 IDispatch_Release(V_DISPATCH(obj));
1547 hres = disp_get_id(ctx->script, V_DISPATCH(obj), str, 0, &id);
1548 IDispatch_Release(V_DISPATCH(obj));
1552 else if(hres == DISP_E_UNKNOWNNAME)
1557 return stack_push_bool(ctx, ret);
1560 /* ECMA-262 3rd Edition 11.6.1 */
1561 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1566 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1570 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1576 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1577 BSTR lstr = NULL, rstr = NULL;
1579 if(V_VT(&l) == VT_BSTR)
1582 hres = to_string(ctx, &l, ei, &lstr);
1584 if(SUCCEEDED(hres)) {
1585 if(V_VT(&r) == VT_BSTR)
1588 hres = to_string(ctx, &r, ei, &rstr);
1591 if(SUCCEEDED(hres)) {
1594 len1 = SysStringLen(lstr);
1595 len2 = SysStringLen(rstr);
1597 V_VT(retv) = VT_BSTR;
1598 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1600 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1602 memcpy(V_BSTR(retv)+len1, rstr, len2*sizeof(WCHAR));
1603 V_BSTR(retv)[len1+len2] = 0;
1606 if(V_VT(&l) != VT_BSTR)
1607 SysFreeString(lstr);
1608 if(V_VT(&r) != VT_BSTR)
1609 SysFreeString(rstr);
1613 hres = to_number(ctx, &l, ei, &nl);
1614 if(SUCCEEDED(hres)) {
1615 hres = to_number(ctx, &r, ei, &nr);
1617 num_set_val(retv, nl + nr);
1626 /* ECMA-262 3rd Edition 11.6.1 */
1627 static HRESULT interp_add(exec_ctx_t *ctx)
1629 VARIANT *l, *r, ret;
1635 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1637 hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1643 return stack_push(ctx, &ret);
1646 /* ECMA-262 3rd Edition 11.6.2 */
1647 static HRESULT interp_sub(exec_ctx_t *ctx)
1654 hres = stack_pop_number(ctx, &r);
1658 hres = stack_pop_number(ctx, &l);
1662 return stack_push_number(ctx, l-r);
1665 /* ECMA-262 3rd Edition 11.5.1 */
1666 static HRESULT interp_mul(exec_ctx_t *ctx)
1673 hres = stack_pop_number(ctx, &r);
1677 hres = stack_pop_number(ctx, &l);
1681 return stack_push_number(ctx, l*r);
1684 /* ECMA-262 3rd Edition 11.5.2 */
1685 static HRESULT interp_div(exec_ctx_t *ctx)
1692 hres = stack_pop_number(ctx, &r);
1696 hres = stack_pop_number(ctx, &l);
1700 return stack_push_number(ctx, l/r);
1703 /* ECMA-262 3rd Edition 11.5.3 */
1704 static HRESULT interp_mod(exec_ctx_t *ctx)
1711 hres = stack_pop_number(ctx, &r);
1715 hres = stack_pop_number(ctx, &l);
1719 return stack_push_number(ctx, fmod(l, r));
1722 /* ECMA-262 3rd Edition 11.4.2 */
1723 static HRESULT interp_delete(exec_ctx_t *ctx)
1725 VARIANT *obj_var, *name_var;
1726 IDispatchEx *dispex;
1734 name_var = stack_pop(ctx);
1735 obj_var = stack_pop(ctx);
1737 hres = to_object(ctx->script, obj_var, &obj);
1738 VariantClear(obj_var);
1740 VariantClear(name_var);
1744 hres = to_string(ctx->script, name_var, ctx->ei, &name);
1745 VariantClear(name_var);
1747 IDispatch_Release(obj);
1751 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1752 if(SUCCEEDED(hres)) {
1753 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1755 IDispatchEx_Release(dispex);
1761 IDispatch_Release(obj);
1762 SysFreeString(name);
1766 return stack_push_bool(ctx, ret);
1769 /* ECMA-262 3rd Edition 11.4.2 */
1770 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1772 const BSTR arg = get_op_bstr(ctx, 0);
1773 IDispatchEx *dispex;
1778 TRACE("%s\n", debugstr_w(arg));
1780 hres = identifier_eval(ctx->script, arg, &exprval);
1784 if(exprval.type != EXPRVAL_IDREF) {
1785 FIXME("Unsupported exprval\n");
1786 exprval_release(&exprval);
1790 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1791 IDispatch_Release(exprval.u.idref.disp);
1792 if(SUCCEEDED(hres)) {
1793 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1794 IDispatchEx_Release(dispex);
1801 return stack_push_bool(ctx, ret);
1804 /* ECMA-262 3rd Edition 11.4.2 */
1805 static HRESULT interp_void(exec_ctx_t *ctx)
1813 V_VT(&v) = VT_EMPTY;
1814 return stack_push(ctx, &v);
1817 /* ECMA-262 3rd Edition 11.4.3 */
1818 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1840 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1841 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1842 jsdisp_release(dispex);
1849 FIXME("unhandled vt %d\n", V_VT(v));
1856 /* ECMA-262 3rd Edition 11.4.3 */
1857 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1865 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1869 obj = stack_pop_objid(ctx, &id);
1871 return stack_push_string(ctx, undefinedW);
1873 V_VT(&v) = VT_EMPTY;
1874 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1875 IDispatch_Release(obj);
1877 return stack_push_string(ctx, unknownW);
1879 hres = typeof_string(&v, &ret);
1884 return stack_push_string(ctx, ret);
1887 /* ECMA-262 3rd Edition 11.4.3 */
1888 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1890 const BSTR arg = get_op_bstr(ctx, 0);
1896 TRACE("%s\n", debugstr_w(arg));
1898 hres = identifier_eval(ctx->script, arg, &exprval);
1902 if(exprval.type == EXPRVAL_INVALID) {
1903 hres = stack_push_string(ctx, undefinedW);
1904 exprval_release(&exprval);
1908 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1909 exprval_release(&exprval);
1913 hres = typeof_string(&v, &ret);
1918 return stack_push_string(ctx, ret);
1921 /* ECMA-262 3rd Edition 11.4.3 */
1922 static HRESULT interp_typeof(exec_ctx_t *ctx)
1931 hres = typeof_string(v, &ret);
1936 return stack_push_string(ctx, ret);
1939 /* ECMA-262 3rd Edition 11.4.7 */
1940 static HRESULT interp_minus(exec_ctx_t *ctx)
1947 hres = stack_pop_number(ctx, &n);
1951 return stack_push_number(ctx, -n);
1954 /* ECMA-262 3rd Edition 11.4.6 */
1955 static HRESULT interp_tonum(exec_ctx_t *ctx)
1964 hres = to_number(ctx->script, v, ctx->ei, &n);
1969 return stack_push_number(ctx, n);
1972 /* ECMA-262 3rd Edition 11.3.1 */
1973 static HRESULT interp_postinc(exec_ctx_t *ctx)
1975 const int arg = get_op_int(ctx, 0);
1983 obj = stack_pop_objid(ctx, &id);
1985 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1987 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1988 if(SUCCEEDED(hres)) {
1992 hres = to_number(ctx->script, &v, ctx->ei, &n);
1993 if(SUCCEEDED(hres)) {
1994 num_set_val(&inc, n+(double)arg);
1995 hres = disp_propput(ctx->script, obj, id, &inc, ctx->ei);
2000 IDispatch_Release(obj);
2004 return stack_push(ctx, &v);
2007 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2008 static HRESULT interp_preinc(exec_ctx_t *ctx)
2010 const int arg = get_op_int(ctx, 0);
2018 obj = stack_pop_objid(ctx, &id);
2020 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2022 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
2023 if(SUCCEEDED(hres)) {
2026 hres = to_number(ctx->script, &v, ctx->ei, &n);
2028 if(SUCCEEDED(hres)) {
2029 num_set_val(&v, n+(double)arg);
2030 hres = disp_propput(ctx->script, obj, id, &v, ctx->ei);
2033 IDispatch_Release(obj);
2037 return stack_push(ctx, &v);
2040 /* ECMA-262 3rd Edition 11.9.3 */
2041 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2043 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2044 return equal2_values(lval, rval, ret);
2046 /* FIXME: NULL disps should be handled in more general way */
2047 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2050 return equal_values(ctx, &v, rval, ei, ret);
2053 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2056 return equal_values(ctx, lval, &v, ei, ret);
2059 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2060 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2065 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2070 hres = to_number(ctx, lval, ei, &n);
2074 /* FIXME: optimize */
2077 return equal_values(ctx, &v, rval, ei, ret);
2080 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2085 hres = to_number(ctx, rval, ei, &n);
2089 /* FIXME: optimize */
2092 return equal_values(ctx, lval, &v, ei, ret);
2095 if(V_VT(rval) == VT_BOOL) {
2098 num_set_int(&v, V_BOOL(rval) ? 1 : 0);
2099 return equal_values(ctx, lval, &v, ei, ret);
2102 if(V_VT(lval) == VT_BOOL) {
2105 num_set_int(&v, V_BOOL(lval) ? 1 : 0);
2106 return equal_values(ctx, &v, rval, ei, ret);
2110 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2114 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2118 hres = equal_values(ctx, lval, &v, ei, ret);
2125 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2129 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2133 hres = equal_values(ctx, &v, rval, ei, ret);
2144 /* ECMA-262 3rd Edition 11.9.1 */
2145 static HRESULT interp_eq(exec_ctx_t *ctx)
2154 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2156 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2162 return stack_push_bool(ctx, b);
2165 /* ECMA-262 3rd Edition 11.9.2 */
2166 static HRESULT interp_neq(exec_ctx_t *ctx)
2175 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2177 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2183 return stack_push_bool(ctx, !b);
2186 /* ECMA-262 3rd Edition 11.9.4 */
2187 static HRESULT interp_eq2(exec_ctx_t *ctx)
2198 hres = equal2_values(r, l, &b);
2204 return stack_push_bool(ctx, b);
2207 /* ECMA-262 3rd Edition 11.9.5 */
2208 static HRESULT interp_neq2(exec_ctx_t *ctx)
2219 hres = equal2_values(r, l, &b);
2225 return stack_push_bool(ctx, !b);
2228 /* ECMA-262 3rd Edition 11.8.5 */
2229 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2235 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2239 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2245 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2246 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2247 SysFreeString(V_BSTR(&l));
2248 SysFreeString(V_BSTR(&r));
2252 hres = to_number(ctx, &l, ei, &ln);
2255 hres = to_number(ctx, &r, ei, &rn);
2260 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2264 /* ECMA-262 3rd Edition 11.8.1 */
2265 static HRESULT interp_lt(exec_ctx_t *ctx)
2274 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2276 hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2282 return stack_push_bool(ctx, b);
2285 /* ECMA-262 3rd Edition 11.8.1 */
2286 static HRESULT interp_lteq(exec_ctx_t *ctx)
2295 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2297 hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2303 return stack_push_bool(ctx, b);
2306 /* ECMA-262 3rd Edition 11.8.2 */
2307 static HRESULT interp_gt(exec_ctx_t *ctx)
2316 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2318 hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2324 return stack_push_bool(ctx, b);
2327 /* ECMA-262 3rd Edition 11.8.4 */
2328 static HRESULT interp_gteq(exec_ctx_t *ctx)
2337 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2339 hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2345 return stack_push_bool(ctx, b);
2348 /* ECMA-262 3rd Edition 11.4.8 */
2349 static HRESULT interp_bneg(exec_ctx_t *ctx)
2358 hres = to_int32_var(ctx->script, v, ctx->ei, &i);
2363 return stack_push_int(ctx, ~i);
2366 /* ECMA-262 3rd Edition 11.4.9 */
2367 static HRESULT interp_neg(exec_ctx_t *ctx)
2376 hres = to_boolean(v, &b);
2381 return stack_push_bool(ctx, !b);
2384 /* ECMA-262 3rd Edition 11.7.1 */
2385 static HRESULT interp_lshift(exec_ctx_t *ctx)
2391 hres = stack_pop_uint(ctx, &r);
2395 hres = stack_pop_int(ctx, &l);
2399 return stack_push_int(ctx, l << (r&0x1f));
2402 /* ECMA-262 3rd Edition 11.7.2 */
2403 static HRESULT interp_rshift(exec_ctx_t *ctx)
2409 hres = stack_pop_uint(ctx, &r);
2413 hres = stack_pop_int(ctx, &l);
2417 return stack_push_int(ctx, l >> (r&0x1f));
2420 /* ECMA-262 3rd Edition 11.7.3 */
2421 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2426 hres = stack_pop_uint(ctx, &r);
2430 hres = stack_pop_uint(ctx, &l);
2434 return stack_push_int(ctx, l >> (r&0x1f));
2437 /* ECMA-262 3rd Edition 11.13.1 */
2438 static HRESULT interp_assign(exec_ctx_t *ctx)
2448 disp = stack_pop_objid(ctx, &id);
2451 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2453 hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2454 IDispatch_Release(disp);
2460 return stack_push(ctx, v);
2463 /* JScript extension */
2464 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2466 const unsigned argc = get_op_uint(ctx, 0);
2474 TRACE("%u\n", argc);
2476 disp = stack_topn_objid(ctx, argc+1, &id);
2478 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2480 hres = convert_args(ctx, argc+1, &argv);
2484 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, argc+1, argv, NULL, ctx->ei);
2485 for(i=0; i < argc; i++)
2486 jsval_release(argv[i]);
2492 stack_popn(ctx, argc+2);
2493 return stack_push(ctx, v);
2496 static HRESULT interp_undefined(exec_ctx_t *ctx)
2502 V_VT(&v) = VT_EMPTY;
2503 return stack_push(ctx, &v);
2506 static HRESULT interp_jmp(exec_ctx_t *ctx)
2508 const unsigned arg = get_op_uint(ctx, 0);
2516 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2518 const unsigned arg = get_op_uint(ctx, 0);
2526 hres = to_boolean(v, &b);
2538 static HRESULT interp_pop(exec_ctx_t *ctx)
2546 static HRESULT interp_ret(exec_ctx_t *ctx)
2554 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2556 static const op_func_t op_funcs[] = {
2557 #define X(x,a,b,c) interp_##x,
2562 static const unsigned op_move[] = {
2563 #define X(a,x,b,c) x,
2568 static HRESULT unwind_exception(exec_ctx_t *ctx)
2570 except_frame_t *except_frame;
2575 except_frame = ctx->except_frame;
2576 ctx->except_frame = except_frame->next;
2578 assert(except_frame->stack_top <= ctx->top);
2579 stack_popn(ctx, ctx->top - except_frame->stack_top);
2581 while(except_frame->scope != ctx->scope_chain)
2582 scope_pop(&ctx->scope_chain);
2584 ctx->ip = except_frame->catch_off;
2586 except_val = ctx->ei->var;
2587 memset(ctx->ei, 0, sizeof(*ctx->ei));
2589 ident = except_frame->ident;
2590 heap_free(except_frame);
2593 jsdisp_t *scope_obj;
2595 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2596 if(SUCCEEDED(hres)) {
2599 hres = variant_to_jsval(&except_val, &val);
2600 if(SUCCEEDED(hres)) {
2601 hres = jsdisp_propput_name(scope_obj, ident, val, ctx->ei);
2605 jsdisp_release(scope_obj);
2607 VariantClear(&except_val);
2611 hres = scope_push(ctx->scope_chain, scope_obj, to_disp(scope_obj), &ctx->scope_chain);
2612 jsdisp_release(scope_obj);
2616 hres = stack_push(ctx, &except_val);
2620 hres = stack_push_bool(ctx, FALSE);
2624 V_VT(&v) = VT_EMPTY;
2625 hres = stack_push(ctx, &v);
2631 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsexcept_t *ei, VARIANT *ret)
2633 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2634 except_frame_t *prev_except_frame;
2635 function_code_t *prev_func;
2636 unsigned prev_ip, prev_top;
2637 scope_chain_t *prev_scope;
2638 bytecode_t *prev_code;
2639 jsexcept_t *prev_ei;
2641 HRESULT hres = S_OK;
2645 prev_top = exec_ctx->top;
2646 prev_scope = exec_ctx->scope_chain;
2647 prev_except_frame = exec_ctx->except_frame;
2648 prev_ip = exec_ctx->ip;
2649 prev_ei = exec_ctx->ei;
2650 prev_code = exec_ctx->code;
2651 prev_func = exec_ctx->func_code;
2652 exec_ctx->ip = func->instr_off;
2654 exec_ctx->except_frame = NULL;
2655 exec_ctx->code = code;
2656 exec_ctx->func_code = func;
2658 while(exec_ctx->ip != -1) {
2659 op = code->instrs[exec_ctx->ip].op;
2660 TRACE("top %d\n", exec_ctx->top);
2661 hres = op_funcs[op](exec_ctx);
2663 TRACE("EXCEPTION\n");
2665 if(!exec_ctx->except_frame)
2668 hres = unwind_exception(exec_ctx);
2672 exec_ctx->ip += op_move[op];
2676 exec_ctx->ip = prev_ip;
2677 exec_ctx->ei = prev_ei;
2678 exec_ctx->except_frame = prev_except_frame;
2679 exec_ctx->code = prev_code;
2680 exec_ctx->func_code = prev_func;
2683 while(exec_ctx->scope_chain != prev_scope)
2684 scope_pop(&exec_ctx->scope_chain);
2685 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2689 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2690 assert(exec_ctx->scope_chain == prev_scope);
2692 if(exec_ctx->top == prev_top)
2693 V_VT(ret) = VT_EMPTY;
2695 *ret = *stack_pop(exec_ctx);
2699 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval,
2700 jsexcept_t *ei, VARIANT *retv)
2702 exec_ctx_t *prev_ctx;
2705 HRESULT hres = S_OK;
2707 for(i = 0; i < func->func_cnt; i++) {
2710 if(!func->funcs[i].name)
2713 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2717 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, jsval_obj(func_obj), ei);
2718 jsdisp_release(func_obj);
2723 for(i=0; i < func->var_cnt; i++) {
2724 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2727 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2733 prev_ctx = ctx->script->exec_ctx;
2734 ctx->script->exec_ctx = ctx;
2736 hres = enter_bytecode(ctx->script, code, func, ei, &val);
2737 assert(ctx->script->exec_ctx == ctx);
2738 ctx->script->exec_ctx = prev_ctx;