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 _return_type_t {
44 struct _except_frame_t {
53 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
55 if(!ctx->stack_size) {
56 ctx->stack = heap_alloc(16*sizeof(VARIANT));
60 }else if(ctx->stack_size == ctx->top) {
63 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
69 ctx->stack = new_stack;
73 ctx->stack[ctx->top++] = *v;
77 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
82 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
83 return stack_push(ctx, &v);
86 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
90 num_set_val(&v, number);
91 return stack_push(ctx, &v);
94 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
100 return stack_push(ctx, &v);
103 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
108 V_BSTR(&v) = SysAllocString(str);
109 return V_BSTR(&v) ? stack_push(ctx, &v) : E_OUTOFMEMORY;
112 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
117 V_VT(&v) = VT_DISPATCH;
118 V_DISPATCH(&v) = disp;
119 hres = stack_push(ctx, &v);
125 return stack_push(ctx, &v);
128 static inline VARIANT *stack_top(exec_ctx_t *ctx)
131 return ctx->stack + ctx->top-1;
134 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
136 assert(ctx->top > n);
137 return ctx->stack + ctx->top-1-n;
140 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
143 return ctx->stack + --ctx->top;
146 static void stack_popn(exec_ctx_t *ctx, unsigned n)
149 VariantClear(stack_pop(ctx));
152 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
158 hres = to_number(ctx->parser->script, v, ctx->ei, r);
163 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
169 if(V_VT(v) == VT_DISPATCH) {
171 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
176 hres = to_object(ctx->parser->script, v, r);
181 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
183 return to_int32(ctx->parser->script, stack_pop(ctx), ctx->ei, r);
186 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
188 return to_uint32(ctx->parser->script, stack_pop(ctx), ctx->ei, r);
191 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
193 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
195 *id = V_INT(stack_pop(ctx));
196 return V_DISPATCH(stack_pop(ctx));
199 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
201 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
203 *id = V_INT(stack_topn(ctx, n));
204 return V_DISPATCH(stack_topn(ctx, n+1));
207 static void exprval_release(exprval_t *val)
210 case EXPRVAL_VARIANT:
211 if(V_VT(&val->u.var) != VT_EMPTY)
212 VariantClear(&val->u.var);
215 if(val->u.idref.disp)
216 IDispatch_Release(val->u.idref.disp);
218 case EXPRVAL_INVALID:
223 /* ECMA-262 3rd Edition 8.7.1 */
224 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
226 V_VT(ret) = VT_EMPTY;
229 case EXPRVAL_VARIANT:
230 return VariantCopy(ret, &val->u.var);
232 if(!val->u.idref.disp) {
233 FIXME("throw ReferenceError\n");
237 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
238 case EXPRVAL_INVALID:
242 ERR("type %d\n", val->type);
246 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
248 if(val->type == EXPRVAL_VARIANT) {
250 V_VT(&val->u.var) = VT_EMPTY;
254 return exprval_value(ctx, val, ei, ret);
257 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
259 val->type = EXPRVAL_IDREF;
260 val->u.idref.disp = disp;
261 val->u.idref.id = id;
264 IDispatch_AddRef(disp);
267 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
269 scope_chain_t *new_scope;
271 new_scope = heap_alloc(sizeof(scope_chain_t));
273 return E_OUTOFMEMORY;
278 new_scope->obj = obj;
282 new_scope->next = scope;
284 new_scope->next = NULL;
291 static void scope_pop(scope_chain_t **scope)
300 void scope_release(scope_chain_t *scope)
306 scope_release(scope->next);
308 jsdisp_release(scope->obj);
312 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
313 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
317 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
319 return E_OUTOFMEMORY;
322 ctx->is_global = is_global;
325 ctx->this_obj = this_obj;
326 else if(script_ctx->host_global)
327 ctx->this_obj = script_ctx->host_global;
329 ctx->this_obj = to_disp(script_ctx->global);
330 IDispatch_AddRef(ctx->this_obj);
332 jsdisp_addref(var_disp);
333 ctx->var_disp = var_disp;
337 ctx->scope_chain = scope;
344 void exec_release(exec_ctx_t *ctx)
350 scope_release(ctx->scope_chain);
352 jsdisp_release(ctx->var_disp);
354 IDispatch_Release(ctx->this_obj);
355 heap_free(ctx->stack);
359 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
364 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
366 TRACE("unsing IDispatch\n");
369 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
373 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
374 IDispatchEx_Release(dispex);
378 static inline BOOL is_null(const VARIANT *v)
380 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
383 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
385 IObjectIdentity *identity;
386 IUnknown *unk1, *unk2;
394 if(!disp1 || !disp2) {
399 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
403 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
405 IUnknown_Release(unk1);
412 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
413 if(SUCCEEDED(hres)) {
414 hres = IObjectIdentity_IsEqualObject(identity, unk2);
415 IObjectIdentity_Release(identity);
422 IUnknown_Release(unk1);
423 IUnknown_Release(unk2);
427 /* ECMA-262 3rd Edition 11.9.6 */
428 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
432 if(V_VT(lval) != V_VT(rval)) {
433 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
434 *ret = num_val(lval) == num_val(rval);
435 else if(is_null(lval))
436 *ret = is_null(rval);
448 *ret = V_I4(lval) == V_I4(rval);
451 *ret = V_R8(lval) == V_R8(rval);
455 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
456 else if(!V_BSTR(rval))
457 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
459 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
462 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
464 *ret = !V_BOOL(lval) == !V_BOOL(rval);
467 FIXME("unimplemented vt %d\n", V_VT(lval));
474 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
480 for(item = ctx->named_items; item; item = item->next) {
481 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
482 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
483 if(SUCCEEDED(hres)) {
485 exprval_set_idref(ret, item->disp, id);
494 /* ECMA-262 3rd Edition 10.1.4 */
495 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
497 scope_chain_t *scope;
502 TRACE("%s\n", debugstr_w(identifier));
504 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
505 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
506 if(SUCCEEDED(hres)) {
507 exprval_set_idref(ret, to_disp(scope->obj), id);
512 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
513 if(SUCCEEDED(hres)) {
514 exprval_set_idref(ret, to_disp(ctx->global), id);
518 for(item = ctx->named_items; item; item = item->next) {
519 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
526 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
527 SCRIPTINFO_IUNKNOWN, &unk, NULL);
529 WARN("GetItemInfo failed: %08x\n", hres);
533 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
534 IUnknown_Release(unk);
536 WARN("object does not implement IDispatch\n");
541 ret->type = EXPRVAL_VARIANT;
542 V_VT(&ret->u.var) = VT_DISPATCH;
543 V_DISPATCH(&ret->u.var) = item->disp;
544 IDispatch_AddRef(item->disp);
549 if(lookup_global_members(ctx, identifier, ret))
552 if(flags & fdexNameEnsure) {
553 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
557 exprval_set_idref(ret, to_disp(ctx->global), id);
561 ret->type = EXPRVAL_INVALID;
565 /* ECMA-262 3rd Edition 12.2 */
566 static HRESULT interp_var_set(exec_ctx_t *ctx)
568 const BSTR name = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
572 TRACE("%s\n", debugstr_w(name));
575 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei, NULL/*FIXME*/);
580 /* ECMA-262 3rd Edition 12.6.4 */
581 static HRESULT interp_forin(exec_ctx_t *ctx)
583 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
584 IDispatch *var_obj, *obj = NULL;
593 val = stack_pop(ctx);
595 assert(V_VT(stack_top(ctx)) == VT_I4);
596 id = V_I4(stack_top(ctx));
598 var_obj = stack_topn_objid(ctx, 1, &var_id);
600 FIXME("invalid ref\n");
605 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
606 obj = V_DISPATCH(stack_topn(ctx, 3));
609 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
610 if(SUCCEEDED(hres)) {
611 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
613 hres = IDispatchEx_GetMemberName(dispex, id, &name);
614 IDispatchEx_Release(dispex);
620 TRACE("No IDispatchEx\n");
629 V_I4(stack_top(ctx)) = id;
633 hres = disp_propput(ctx->parser->script, var_obj, var_id, &v, ctx->ei, NULL/*FIXME*/);
642 return stack_push(ctx, val);
647 /* ECMA-262 3rd Edition 12.10 */
648 HRESULT interp_push_scope(exec_ctx_t *ctx)
658 hres = to_object(ctx->parser->script, v, &disp);
663 obj = to_jsdisp(disp);
665 IDispatch_Release(disp);
666 FIXME("disp is not jsdisp\n");
670 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
675 /* ECMA-262 3rd Edition 12.10 */
676 HRESULT interp_pop_scope(exec_ctx_t *ctx)
680 scope_pop(&ctx->scope_chain);
684 /* ECMA-262 3rd Edition 12.12 */
685 HRESULT interp_label(exec_ctx_t *ctx)
691 /* ECMA-262 3rd Edition 12.13 */
692 static HRESULT interp_case(exec_ctx_t *ctx)
694 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
702 hres = equal2_values(stack_top(ctx), v, &b);
716 /* ECMA-262 3rd Edition 12.13 */
717 static HRESULT interp_throw(exec_ctx_t *ctx)
721 ctx->rt->ei.var = *stack_pop(ctx);
722 return DISP_E_EXCEPTION;
725 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
727 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
729 TRACE("%08x\n", arg);
731 return throw_reference_error(ctx->parser->script, ctx->ei, arg, NULL);
734 static HRESULT interp_throw_type(exec_ctx_t *ctx)
736 const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
737 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
739 TRACE("%08x %s\n", hres, debugstr_w(str));
741 return throw_type_error(ctx->parser->script, ctx->ei, hres, str);
744 /* ECMA-262 3rd Edition 12.14 */
745 static HRESULT interp_push_except(exec_ctx_t *ctx)
747 const unsigned arg1 = ctx->parser->code->instrs[ctx->ip].arg1.uint;
748 const BSTR arg2 = ctx->parser->code->instrs[ctx->ip].arg2.bstr;
749 except_frame_t *except;
754 stack_top = ctx->top;
759 hres = stack_push_bool(ctx, TRUE);
762 hres = stack_push_bool(ctx, TRUE);
767 except = heap_alloc(sizeof(*except));
769 return E_OUTOFMEMORY;
771 except->stack_top = stack_top;
772 except->scope = ctx->scope_chain;
773 except->catch_off = arg1;
774 except->ident = arg2;
775 except->next = ctx->except_frame;
776 ctx->except_frame = except;
780 /* ECMA-262 3rd Edition 12.14 */
781 static HRESULT interp_pop_except(exec_ctx_t *ctx)
783 except_frame_t *except;
787 except = ctx->except_frame;
788 assert(except != NULL);
790 ctx->except_frame = except->next;
795 /* ECMA-262 3rd Edition 12.14 */
796 static HRESULT interp_end_finally(exec_ctx_t *ctx)
804 assert(V_VT(stack_top(ctx)) == VT_BOOL);
805 if(!V_BOOL(stack_top(ctx))) {
806 TRACE("passing exception\n");
810 ctx->rt->ei.var = *stack_pop(ctx);
811 return DISP_E_EXCEPTION;
815 return stack_push(ctx, v);
818 /* ECMA-262 3rd Edition 13 */
819 static HRESULT interp_func(exec_ctx_t *ctx)
821 function_expression_t *expr = ctx->parser->code->instrs[ctx->ip].arg1.func;
828 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
829 expr->src_str, expr->src_len, &dispex);
833 var_set_jsdisp(&v, dispex);
834 return stack_push(ctx, &v);
837 /* ECMA-262 3rd Edition 11.2.1 */
838 static HRESULT interp_array(exec_ctx_t *ctx)
848 namev = stack_pop(ctx);
850 hres = stack_pop_object(ctx, &obj);
856 hres = to_string(ctx->parser->script, namev, ctx->ei, &name);
859 IDispatch_Release(obj);
863 hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
865 if(SUCCEEDED(hres)) {
866 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
867 }else if(hres == DISP_E_UNKNOWNNAME) {
871 IDispatch_Release(obj);
875 return stack_push(ctx, &v);
878 /* ECMA-262 3rd Edition 11.2.1 */
879 static HRESULT interp_member(exec_ctx_t *ctx)
881 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
889 hres = stack_pop_object(ctx, &obj);
893 hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
894 if(SUCCEEDED(hres)) {
896 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
897 }else if(hres == DISP_E_UNKNOWNNAME) {
901 IDispatch_Release(obj);
905 return stack_push(ctx, &v);
908 /* ECMA-262 3rd Edition 11.2.1 */
909 static HRESULT interp_memberid(exec_ctx_t *ctx)
911 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
912 VARIANT *objv, *namev;
920 namev = stack_pop(ctx);
921 objv = stack_pop(ctx);
923 hres = to_object(ctx->parser->script, objv, &obj);
925 if(SUCCEEDED(hres)) {
926 hres = to_string(ctx->parser->script, namev, ctx->ei, &name);
928 IDispatch_Release(obj);
934 hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
937 IDispatch_Release(obj);
938 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
940 id = JS_E_INVALID_PROPERTY;
946 return stack_push_objid(ctx, obj, id);
949 /* ECMA-262 3rd Edition 11.2.1 */
950 static HRESULT interp_refval(exec_ctx_t *ctx)
959 disp = stack_topn_objid(ctx, 0, &id);
961 return throw_reference_error(ctx->parser->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
963 hres = disp_propget(ctx->parser->script, disp, id, &v, ctx->ei, NULL/*FIXME*/);
967 return stack_push(ctx, &v);
970 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
976 dp->rgdispidNamedArgs = NULL;
979 assert(ctx->top >= arg_cnt);
981 for(i=1; i*2 <= arg_cnt; i++) {
982 tmp = ctx->stack[ctx->top-i];
983 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
984 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
987 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
990 /* ECMA-262 3rd Edition 11.2.2 */
991 static HRESULT interp_new(exec_ctx_t *ctx)
993 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1000 constr = stack_topn(ctx, arg);
1002 /* NOTE: Should use to_object here */
1004 if(V_VT(constr) == VT_NULL)
1005 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1006 else if(V_VT(constr) != VT_DISPATCH)
1007 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
1008 else if(!V_DISPATCH(constr))
1009 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1011 jsstack_to_dp(ctx, arg, &dp);
1012 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1013 DISPATCH_CONSTRUCT, &dp, &v, ctx->ei, NULL/*FIXME*/);
1017 stack_popn(ctx, arg+1);
1018 return stack_push(ctx, &v);
1021 /* ECMA-262 3rd Edition 11.2.3 */
1022 static HRESULT interp_call(exec_ctx_t *ctx)
1024 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1025 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1030 TRACE("%d %d\n", argn, do_ret);
1032 objv = stack_topn(ctx, argn);
1033 if(V_VT(objv) != VT_DISPATCH)
1034 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1036 jsstack_to_dp(ctx, argn, &dp);
1037 hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1038 do_ret ? &v : NULL, ctx->ei, NULL/*FIXME*/);
1042 stack_popn(ctx, argn+1);
1043 return do_ret ? stack_push(ctx, &v) : S_OK;
1047 /* ECMA-262 3rd Edition 11.2.3 */
1048 static HRESULT interp_call_member(exec_ctx_t *ctx)
1050 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1051 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1058 TRACE("%d %d\n", argn, do_ret);
1060 obj = stack_topn_objid(ctx, argn, &id);
1062 return throw_type_error(ctx->parser->script, ctx->ei, id, NULL);
1064 jsstack_to_dp(ctx, argn, &dp);
1065 hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, ctx->ei, NULL/*FIXME*/);
1069 stack_popn(ctx, argn+2);
1070 return do_ret ? stack_push(ctx, &v) : S_OK;
1074 /* ECMA-262 3rd Edition 11.1.1 */
1075 static HRESULT interp_this(exec_ctx_t *ctx)
1081 V_VT(&v) = VT_DISPATCH;
1082 V_DISPATCH(&v) = ctx->this_obj;
1083 IDispatch_AddRef(ctx->this_obj);
1084 return stack_push(ctx, &v);
1087 /* ECMA-262 3rd Edition 10.1.4 */
1088 static HRESULT interp_ident(exec_ctx_t *ctx)
1090 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1095 TRACE("%s\n", debugstr_w(arg));
1097 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
1101 if(exprval.type == EXPRVAL_INVALID)
1102 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1104 hres = exprval_to_value(ctx->parser->script, &exprval, ctx->ei, &v);
1105 exprval_release(&exprval);
1109 return stack_push(ctx, &v);
1112 /* ECMA-262 3rd Edition 10.1.4 */
1113 static HRESULT interp_identid(exec_ctx_t *ctx)
1115 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1116 const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1120 TRACE("%s %x\n", debugstr_w(arg), flags);
1122 hres = identifier_eval(ctx->parser->script, arg, flags, ctx->ei, &exprval);
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->parser->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->parser->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->parser->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->parser->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->parser->code->instrs[ctx->ip].arg1.str;
1202 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1207 TRACE("%s %x\n", debugstr_w(source), flags);
1209 hres = create_regexp(ctx->parser->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->parser->code->instrs[ctx->ip].arg1.uint;
1228 hres = create_array(ctx->parser->script, arg, &array);
1235 hres = jsdisp_propput_idx(array, i, v, ctx->ei, NULL/*FIXME*/);
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 HRESULT interp_new_obj(exec_ctx_t *ctx)
1256 hres = create_object(ctx->parser->script, NULL, &obj);
1260 var_set_jsdisp(&v, obj);
1261 return stack_push(ctx, &v);
1264 /* ECMA-262 3rd Edition 11.1.5 */
1265 HRESULT interp_obj_prop(exec_ctx_t *ctx)
1267 const BSTR name = ctx->parser->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, NULL/*FIXME*/);
1284 /* ECMA-262 3rd Edition 11.11 */
1285 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1287 const unsigned arg = ctx->parser->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->parser->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->parser->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, NULL/*FIXME*/);
1411 hres = throw_type_error(ctx->parser->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->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1463 hres = to_string(ctx->parser->script, v, ctx->ei, &str);
1466 IDispatch_Release(V_DISPATCH(obj));
1470 hres = disp_get_id(ctx->parser->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);
1522 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1523 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1526 if(V_VT(&l) != VT_BSTR)
1527 SysFreeString(lstr);
1528 if(V_VT(&r) != VT_BSTR)
1529 SysFreeString(rstr);
1533 hres = to_number(ctx, &l, ei, &nl);
1534 if(SUCCEEDED(hres)) {
1535 hres = to_number(ctx, &r, ei, &nr);
1537 num_set_val(retv, num_val(&nl) + num_val(&nr));
1546 /* ECMA-262 3rd Edition 11.6.1 */
1547 static HRESULT interp_add(exec_ctx_t *ctx)
1549 VARIANT *l, *r, ret;
1555 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1557 hres = add_eval(ctx->parser->script, l, r, ctx->ei, &ret);
1563 return stack_push(ctx, &ret);
1566 /* ECMA-262 3rd Edition 11.6.2 */
1567 static HRESULT interp_sub(exec_ctx_t *ctx)
1574 hres = stack_pop_number(ctx, &r);
1578 hres = stack_pop_number(ctx, &l);
1582 return stack_push_number(ctx, num_val(&l)-num_val(&r));
1585 /* ECMA-262 3rd Edition 11.5.1 */
1586 static HRESULT interp_mul(exec_ctx_t *ctx)
1593 hres = stack_pop_number(ctx, &r);
1597 hres = stack_pop_number(ctx, &l);
1601 return stack_push_number(ctx, num_val(&l)*num_val(&r));
1604 /* ECMA-262 3rd Edition 11.5.2 */
1605 static HRESULT interp_div(exec_ctx_t *ctx)
1612 hres = stack_pop_number(ctx, &r);
1616 hres = stack_pop_number(ctx, &l);
1620 return stack_push_number(ctx, num_val(&l)/num_val(&r));
1623 /* ECMA-262 3rd Edition 11.5.3 */
1624 static HRESULT interp_mod(exec_ctx_t *ctx)
1631 hres = stack_pop_number(ctx, &r);
1635 hres = stack_pop_number(ctx, &l);
1639 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
1642 /* ECMA-262 3rd Edition 11.4.2 */
1643 static HRESULT interp_delete(exec_ctx_t *ctx)
1645 VARIANT *obj_var, *name_var;
1646 IDispatchEx *dispex;
1654 name_var = stack_pop(ctx);
1655 obj_var = stack_pop(ctx);
1657 hres = to_object(ctx->parser->script, obj_var, &obj);
1658 VariantClear(obj_var);
1660 VariantClear(name_var);
1664 hres = to_string(ctx->parser->script, name_var, ctx->ei, &name);
1665 VariantClear(name_var);
1667 IDispatch_Release(obj);
1671 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1672 if(SUCCEEDED(hres)) {
1673 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
1675 IDispatchEx_Release(dispex);
1681 IDispatch_Release(obj);
1682 SysFreeString(name);
1686 return stack_push_bool(ctx, ret);
1689 /* ECMA-262 3rd Edition 11.4.2 */
1690 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1692 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1693 IDispatchEx *dispex;
1698 TRACE("%s\n", debugstr_w(arg));
1700 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
1704 if(exprval.type != EXPRVAL_IDREF) {
1705 FIXME("Unsupported exprval\n");
1706 exprval_release(&exprval);
1710 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1711 IDispatch_Release(exprval.u.idref.disp);
1712 if(SUCCEEDED(hres)) {
1713 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1714 IDispatchEx_Release(dispex);
1721 return stack_push_bool(ctx, ret);
1724 /* ECMA-262 3rd Edition 11.4.2 */
1725 static HRESULT interp_void(exec_ctx_t *ctx)
1733 V_VT(&v) = VT_EMPTY;
1734 return stack_push(ctx, &v);
1737 /* ECMA-262 3rd Edition 11.4.3 */
1738 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1760 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1761 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1762 jsdisp_release(dispex);
1769 FIXME("unhandled vt %d\n", V_VT(v));
1776 /* ECMA-262 3rd Edition 11.4.3 */
1777 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1785 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1789 obj = stack_pop_objid(ctx, &id);
1791 return stack_push_string(ctx, undefinedW);
1793 V_VT(&v) = VT_EMPTY;
1794 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1795 IDispatch_Release(obj);
1797 return stack_push_string(ctx, unknownW);
1799 hres = typeof_string(&v, &ret);
1804 return stack_push_string(ctx, ret);
1807 /* ECMA-262 3rd Edition 11.4.3 */
1808 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1810 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1816 TRACE("%s\n", debugstr_w(arg));
1818 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
1822 if(exprval.type == EXPRVAL_INVALID) {
1823 hres = stack_push_string(ctx, undefinedW);
1824 exprval_release(&exprval);
1828 hres = exprval_to_value(ctx->parser->script, &exprval, ctx->ei, &v);
1829 exprval_release(&exprval);
1833 hres = typeof_string(&v, &ret);
1838 return stack_push_string(ctx, ret);
1841 /* ECMA-262 3rd Edition 11.4.3 */
1842 static HRESULT interp_typeof(exec_ctx_t *ctx)
1851 hres = typeof_string(v, &ret);
1856 return stack_push_string(ctx, ret);
1859 /* ECMA-262 3rd Edition 11.4.7 */
1860 static HRESULT interp_minus(exec_ctx_t *ctx)
1867 hres = stack_pop_number(ctx, &n);
1871 return stack_push_number(ctx, -num_val(&n));
1874 /* ECMA-262 3rd Edition 11.4.6 */
1875 static HRESULT interp_tonum(exec_ctx_t *ctx)
1883 hres = to_number(ctx->parser->script, v, ctx->ei, &num);
1888 return stack_push(ctx, &num);
1891 /* ECMA-262 3rd Edition 11.3.1 */
1892 static HRESULT interp_postinc(exec_ctx_t *ctx)
1894 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1902 obj = stack_pop_objid(ctx, &id);
1904 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1906 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1907 if(SUCCEEDED(hres)) {
1910 hres = to_number(ctx->parser->script, &v, ctx->ei, &n);
1911 if(SUCCEEDED(hres)) {
1912 num_set_val(&inc, num_val(&n)+(double)arg);
1913 hres = disp_propput(ctx->parser->script, obj, id, &inc, ctx->ei, NULL/*FIXME*/);
1918 IDispatch_Release(obj);
1922 return stack_push(ctx, &v);
1925 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1926 static HRESULT interp_preinc(exec_ctx_t *ctx)
1928 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1936 obj = stack_pop_objid(ctx, &id);
1938 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1940 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1941 if(SUCCEEDED(hres)) {
1944 hres = to_number(ctx->parser->script, &v, ctx->ei, &n);
1946 if(SUCCEEDED(hres)) {
1947 num_set_val(&v, num_val(&n)+(double)arg);
1948 hres = disp_propput(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1951 IDispatch_Release(obj);
1955 return stack_push(ctx, &v);
1958 /* ECMA-262 3rd Edition 11.9.3 */
1959 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1961 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1962 return equal2_values(lval, rval, ret);
1964 /* FIXME: NULL disps should be handled in more general way */
1965 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1968 return equal_values(ctx, &v, rval, ei, ret);
1971 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1974 return equal_values(ctx, lval, &v, ei, ret);
1977 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1978 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1983 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1987 hres = to_number(ctx, lval, ei, &v);
1991 return equal_values(ctx, &v, rval, ei, ret);
1994 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
1998 hres = to_number(ctx, rval, ei, &v);
2002 return equal_values(ctx, lval, &v, ei, ret);
2005 if(V_VT(rval) == VT_BOOL) {
2009 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2010 return equal_values(ctx, lval, &v, ei, ret);
2013 if(V_VT(lval) == VT_BOOL) {
2017 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2018 return equal_values(ctx, &v, rval, ei, ret);
2022 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2026 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2030 hres = equal_values(ctx, lval, &v, ei, ret);
2037 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2041 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2045 hres = equal_values(ctx, &v, rval, ei, ret);
2056 /* ECMA-262 3rd Edition 11.9.1 */
2057 static HRESULT interp_eq(exec_ctx_t *ctx)
2066 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2068 hres = equal_values(ctx->parser->script, l, r, ctx->ei, &b);
2074 return stack_push_bool(ctx, b);
2077 /* ECMA-262 3rd Edition 11.9.2 */
2078 static HRESULT interp_neq(exec_ctx_t *ctx)
2087 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2089 hres = equal_values(ctx->parser->script, l, r, ctx->ei, &b);
2095 return stack_push_bool(ctx, !b);
2098 /* ECMA-262 3rd Edition 11.9.4 */
2099 static HRESULT interp_eq2(exec_ctx_t *ctx)
2110 hres = equal2_values(r, l, &b);
2116 return stack_push_bool(ctx, b);
2119 /* ECMA-262 3rd Edition 11.9.5 */
2120 static HRESULT interp_neq2(exec_ctx_t *ctx)
2131 hres = equal2_values(r, l, &b);
2137 return stack_push_bool(ctx, !b);
2140 /* ECMA-262 3rd Edition 11.8.5 */
2141 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2143 VARIANT l, r, ln, rn;
2146 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2150 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2156 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2157 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2158 SysFreeString(V_BSTR(&l));
2159 SysFreeString(V_BSTR(&r));
2163 hres = to_number(ctx, &l, ei, &ln);
2166 hres = to_number(ctx, &r, ei, &rn);
2171 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2172 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2174 DOUBLE ld = num_val(&ln);
2175 DOUBLE rd = num_val(&rn);
2177 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2183 /* ECMA-262 3rd Edition 11.8.1 */
2184 static HRESULT interp_lt(exec_ctx_t *ctx)
2193 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2195 hres = less_eval(ctx->parser->script, l, r, FALSE, ctx->ei, &b);
2201 return stack_push_bool(ctx, b);
2204 /* ECMA-262 3rd Edition 11.8.1 */
2205 static HRESULT interp_lteq(exec_ctx_t *ctx)
2214 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2216 hres = less_eval(ctx->parser->script, r, l, TRUE, ctx->ei, &b);
2222 return stack_push_bool(ctx, b);
2225 /* ECMA-262 3rd Edition 11.8.2 */
2226 static HRESULT interp_gt(exec_ctx_t *ctx)
2235 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2237 hres = less_eval(ctx->parser->script, r, l, FALSE, ctx->ei, &b);
2243 return stack_push_bool(ctx, b);
2246 /* ECMA-262 3rd Edition 11.8.4 */
2247 static HRESULT interp_gteq(exec_ctx_t *ctx)
2256 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2258 hres = less_eval(ctx->parser->script, l, r, TRUE, ctx->ei, &b);
2264 return stack_push_bool(ctx, b);
2267 /* ECMA-262 3rd Edition 11.4.8 */
2268 static HRESULT interp_bneg(exec_ctx_t *ctx)
2277 hres = to_int32(ctx->parser->script, v, ctx->ei, &i);
2284 return stack_push(ctx, &r);
2287 /* ECMA-262 3rd Edition 11.4.9 */
2288 static HRESULT interp_neg(exec_ctx_t *ctx)
2297 hres = to_boolean(v, &b);
2302 return stack_push_bool(ctx, !b);
2305 /* ECMA-262 3rd Edition 11.7.1 */
2306 static HRESULT interp_lshift(exec_ctx_t *ctx)
2312 hres = stack_pop_uint(ctx, &r);
2316 hres = stack_pop_int(ctx, &l);
2320 return stack_push_int(ctx, l << (r&0x1f));
2323 /* ECMA-262 3rd Edition 11.7.2 */
2324 static HRESULT interp_rshift(exec_ctx_t *ctx)
2330 hres = stack_pop_uint(ctx, &r);
2334 hres = stack_pop_int(ctx, &l);
2338 return stack_push_int(ctx, l >> (r&0x1f));
2341 /* ECMA-262 3rd Edition 11.7.3 */
2342 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2347 hres = stack_pop_uint(ctx, &r);
2351 hres = stack_pop_uint(ctx, &l);
2355 return stack_push_int(ctx, l >> (r&0x1f));
2358 /* ECMA-262 3rd Edition 11.13.1 */
2359 static HRESULT interp_assign(exec_ctx_t *ctx)
2369 disp = stack_pop_objid(ctx, &id);
2372 return throw_reference_error(ctx->parser->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2374 hres = disp_propput(ctx->parser->script, disp, id, v, ctx->ei, NULL/*FIXME*/);
2375 IDispatch_Release(disp);
2381 return stack_push(ctx, v);
2384 static HRESULT interp_undefined(exec_ctx_t *ctx)
2390 V_VT(&v) = VT_EMPTY;
2391 return stack_push(ctx, &v);
2394 static HRESULT interp_jmp(exec_ctx_t *ctx)
2396 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2404 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2406 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2414 hres = to_boolean(v, &b);
2426 static HRESULT interp_pop(exec_ctx_t *ctx)
2434 static HRESULT interp_ret(exec_ctx_t *ctx)
2442 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2444 static const op_func_t op_funcs[] = {
2445 #define X(x,a,b,c) interp_##x,
2450 static const unsigned op_move[] = {
2451 #define X(a,x,b,c) x,
2456 static HRESULT unwind_exception(exec_ctx_t *ctx)
2458 except_frame_t *except_frame;
2463 except_frame = ctx->except_frame;
2464 ctx->except_frame = except_frame->next;
2466 assert(except_frame->stack_top <= ctx->top);
2467 stack_popn(ctx, ctx->top - except_frame->stack_top);
2469 while(except_frame->scope != ctx->scope_chain)
2470 scope_pop(&ctx->scope_chain);
2472 ctx->ip = except_frame->catch_off;
2474 except_val = ctx->rt->ei.var;
2475 memset(&ctx->rt->ei, 0, sizeof(ctx->rt->ei));
2477 ident = except_frame->ident;
2478 heap_free(except_frame);
2481 jsdisp_t *scope_obj;
2483 hres = create_dispex(ctx->parser->script, NULL, NULL, &scope_obj);
2484 if(SUCCEEDED(hres)) {
2485 hres = jsdisp_propput_name(scope_obj, ident, &except_val, &ctx->rt->ei, NULL/*FIXME*/);
2487 jsdisp_release(scope_obj);
2489 VariantClear(&except_val);
2493 hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2494 jsdisp_release(scope_obj);
2498 hres = stack_push(ctx, &except_val);
2502 hres = stack_push_bool(ctx, FALSE);
2506 V_VT(&v) = VT_EMPTY;
2507 hres = stack_push(ctx, &v);
2513 static HRESULT enter_bytecode(script_ctx_t *ctx, unsigned ip, return_type_t *rt, VARIANT *ret)
2515 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2516 except_frame_t *prev_except_frame;
2517 unsigned prev_ip, prev_top;
2518 scope_chain_t *prev_scope;
2519 return_type_t *prev_rt;
2520 jsexcept_t *prev_ei;
2522 HRESULT hres = S_OK;
2526 prev_rt = exec_ctx->rt;
2527 prev_top = exec_ctx->top;
2528 prev_scope = exec_ctx->scope_chain;
2529 prev_except_frame = exec_ctx->except_frame;
2530 prev_ip = exec_ctx->ip;
2531 prev_ei = exec_ctx->ei;
2534 exec_ctx->ei = &rt->ei;
2535 exec_ctx->except_frame = NULL;
2537 while(exec_ctx->ip != -1) {
2538 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
2539 hres = op_funcs[op](exec_ctx);
2541 TRACE("EXCEPTION\n");
2543 if(!exec_ctx->except_frame)
2546 hres = unwind_exception(exec_ctx);
2550 exec_ctx->ip += op_move[op];
2554 exec_ctx->rt = prev_rt;
2555 exec_ctx->ip = prev_ip;
2556 exec_ctx->ei = prev_ei;
2557 exec_ctx->except_frame = prev_except_frame;
2560 while(exec_ctx->scope_chain != prev_scope)
2561 scope_pop(&exec_ctx->scope_chain);
2562 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2566 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2567 assert(exec_ctx->scope_chain == prev_scope);
2569 if(exec_ctx->top == prev_top)
2570 V_VT(ret) = VT_EMPTY;
2572 *ret = *stack_pop(exec_ctx);
2576 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
2577 jsexcept_t *ei, VARIANT *retv)
2579 script_ctx_t *script = parser->script;
2580 function_declaration_t *func;
2581 parser_ctx_t *prev_parser;
2584 exec_ctx_t *prev_ctx;
2586 HRESULT hres = S_OK;
2588 for(func = source->functions; func; func = func->next) {
2592 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
2593 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
2597 var_set_jsdisp(&var, func_obj);
2598 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
2599 jsdisp_release(func_obj);
2604 for(var = source->variables; var; var = var->next) {
2608 name = SysAllocString(var->identifier);
2610 return E_OUTOFMEMORY;
2612 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
2613 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
2614 SysFreeString(name);
2619 prev_ctx = script->exec_ctx;
2620 script->exec_ctx = ctx;
2622 prev_parser = ctx->parser;
2623 ctx->parser = parser;
2625 V_VT(&val) = VT_EMPTY;
2626 memset(&rt, 0, sizeof(rt));
2628 if(source->statement) {
2629 if(source->instr_off == -1) {
2630 hres = compile_subscript_stat(ctx->parser, source->statement, from_eval, &source->instr_off);
2631 if(FAILED(hres) && is_jscript_error(hres))
2632 hres = throw_syntax_error(script, &rt.ei, hres, NULL);
2635 hres = enter_bytecode(script, source->instr_off, &rt, &val);
2638 script->exec_ctx = prev_ctx;
2639 ctx->parser = prev_parser;