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)
154 hres = to_number(ctx->parser->script, v, ctx->ei, r);
159 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
165 if(V_VT(v) == VT_DISPATCH) {
167 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
172 hres = to_object(ctx->parser->script, v, r);
177 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
179 return to_int32(ctx->parser->script, stack_pop(ctx), ctx->ei, r);
182 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
184 return to_uint32(ctx->parser->script, stack_pop(ctx), ctx->ei, r);
187 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
189 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
191 *id = V_INT(stack_pop(ctx));
192 return V_DISPATCH(stack_pop(ctx));
195 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
197 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
199 *id = V_INT(stack_topn(ctx, n));
200 return V_DISPATCH(stack_topn(ctx, n+1));
203 static void exprval_release(exprval_t *val)
206 case EXPRVAL_VARIANT:
207 if(V_VT(&val->u.var) != VT_EMPTY)
208 VariantClear(&val->u.var);
211 if(val->u.idref.disp)
212 IDispatch_Release(val->u.idref.disp);
214 case EXPRVAL_INVALID:
219 /* ECMA-262 3rd Edition 8.7.1 */
220 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
222 V_VT(ret) = VT_EMPTY;
225 case EXPRVAL_VARIANT:
226 return VariantCopy(ret, &val->u.var);
228 if(!val->u.idref.disp) {
229 FIXME("throw ReferenceError\n");
233 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
234 case EXPRVAL_INVALID:
238 ERR("type %d\n", val->type);
242 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
244 if(val->type == EXPRVAL_VARIANT) {
246 V_VT(&val->u.var) = VT_EMPTY;
250 return exprval_value(ctx, val, ei, ret);
253 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
255 val->type = EXPRVAL_IDREF;
256 val->u.idref.disp = disp;
257 val->u.idref.id = id;
260 IDispatch_AddRef(disp);
263 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
265 scope_chain_t *new_scope;
267 new_scope = heap_alloc(sizeof(scope_chain_t));
269 return E_OUTOFMEMORY;
274 new_scope->obj = obj;
278 new_scope->next = scope;
280 new_scope->next = NULL;
287 static void scope_pop(scope_chain_t **scope)
296 void scope_release(scope_chain_t *scope)
302 scope_release(scope->next);
304 jsdisp_release(scope->obj);
308 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
309 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
313 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
315 return E_OUTOFMEMORY;
318 ctx->is_global = is_global;
321 ctx->this_obj = this_obj;
322 else if(script_ctx->host_global)
323 ctx->this_obj = script_ctx->host_global;
325 ctx->this_obj = to_disp(script_ctx->global);
326 IDispatch_AddRef(ctx->this_obj);
328 jsdisp_addref(var_disp);
329 ctx->var_disp = var_disp;
333 ctx->scope_chain = scope;
340 void exec_release(exec_ctx_t *ctx)
346 scope_release(ctx->scope_chain);
348 jsdisp_release(ctx->var_disp);
350 IDispatch_Release(ctx->this_obj);
351 heap_free(ctx->stack);
355 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
360 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
362 TRACE("unsing IDispatch\n");
365 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
369 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
370 IDispatchEx_Release(dispex);
374 static inline BOOL is_null(const VARIANT *v)
376 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
379 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
381 IObjectIdentity *identity;
382 IUnknown *unk1, *unk2;
390 if(!disp1 || !disp2) {
395 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
399 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
401 IUnknown_Release(unk1);
408 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
409 if(SUCCEEDED(hres)) {
410 hres = IObjectIdentity_IsEqualObject(identity, unk2);
411 IObjectIdentity_Release(identity);
418 IUnknown_Release(unk1);
419 IUnknown_Release(unk2);
423 /* ECMA-262 3rd Edition 11.9.6 */
424 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
428 if(V_VT(lval) != V_VT(rval)) {
429 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
430 *ret = num_val(lval) == num_val(rval);
431 else if(is_null(lval))
432 *ret = is_null(rval);
444 *ret = V_I4(lval) == V_I4(rval);
447 *ret = V_R8(lval) == V_R8(rval);
451 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
452 else if(!V_BSTR(rval))
453 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
455 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
458 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
460 *ret = !V_BOOL(lval) == !V_BOOL(rval);
463 FIXME("unimplemented vt %d\n", V_VT(lval));
470 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
476 for(item = ctx->named_items; item; item = item->next) {
477 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
478 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
479 if(SUCCEEDED(hres)) {
481 exprval_set_idref(ret, item->disp, id);
490 /* ECMA-262 3rd Edition 10.1.4 */
491 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
493 scope_chain_t *scope;
498 TRACE("%s\n", debugstr_w(identifier));
500 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
501 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
502 if(SUCCEEDED(hres)) {
503 exprval_set_idref(ret, to_disp(scope->obj), id);
508 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
509 if(SUCCEEDED(hres)) {
510 exprval_set_idref(ret, to_disp(ctx->global), id);
514 for(item = ctx->named_items; item; item = item->next) {
515 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
522 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
523 SCRIPTINFO_IUNKNOWN, &unk, NULL);
525 WARN("GetItemInfo failed: %08x\n", hres);
529 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
530 IUnknown_Release(unk);
532 WARN("object does not implement IDispatch\n");
537 ret->type = EXPRVAL_VARIANT;
538 V_VT(&ret->u.var) = VT_DISPATCH;
539 V_DISPATCH(&ret->u.var) = item->disp;
540 IDispatch_AddRef(item->disp);
545 if(lookup_global_members(ctx, identifier, ret))
548 if(flags & fdexNameEnsure) {
549 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
553 exprval_set_idref(ret, to_disp(ctx->global), id);
557 ret->type = EXPRVAL_INVALID;
561 /* ECMA-262 3rd Edition 12.2 */
562 static HRESULT interp_var_set(exec_ctx_t *ctx)
564 const BSTR name = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
568 TRACE("%s\n", debugstr_w(name));
571 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei, NULL/*FIXME*/);
576 /* ECMA-262 3rd Edition 12.6.4 */
577 static HRESULT interp_forin(exec_ctx_t *ctx)
579 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
580 IDispatch *var_obj, *obj = NULL;
589 val = stack_pop(ctx);
591 assert(V_VT(stack_top(ctx)) == VT_I4);
592 id = V_I4(stack_top(ctx));
594 var_obj = stack_topn_objid(ctx, 1, &var_id);
596 FIXME("invalid ref\n");
601 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
602 obj = V_DISPATCH(stack_topn(ctx, 3));
605 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
606 if(SUCCEEDED(hres)) {
607 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
609 hres = IDispatchEx_GetMemberName(dispex, id, &name);
610 IDispatchEx_Release(dispex);
616 TRACE("No IDispatchEx\n");
625 V_I4(stack_top(ctx)) = id;
629 hres = disp_propput(ctx->parser->script, var_obj, var_id, &v, ctx->ei, NULL/*FIXME*/);
638 return stack_push(ctx, val);
643 /* ECMA-262 3rd Edition 12.10 */
644 static HRESULT interp_push_scope(exec_ctx_t *ctx)
654 hres = to_object(ctx->parser->script, v, &disp);
659 obj = to_jsdisp(disp);
661 IDispatch_Release(disp);
662 FIXME("disp is not jsdisp\n");
666 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
671 /* ECMA-262 3rd Edition 12.10 */
672 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
676 scope_pop(&ctx->scope_chain);
680 /* ECMA-262 3rd Edition 12.13 */
681 static HRESULT interp_case(exec_ctx_t *ctx)
683 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
691 hres = equal2_values(stack_top(ctx), v, &b);
705 /* ECMA-262 3rd Edition 12.13 */
706 static HRESULT interp_throw(exec_ctx_t *ctx)
710 ctx->ei->var = *stack_pop(ctx);
711 return DISP_E_EXCEPTION;
714 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
716 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
718 TRACE("%08x\n", arg);
720 return throw_reference_error(ctx->parser->script, ctx->ei, arg, NULL);
723 static HRESULT interp_throw_type(exec_ctx_t *ctx)
725 const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
726 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
728 TRACE("%08x %s\n", hres, debugstr_w(str));
730 return throw_type_error(ctx->parser->script, ctx->ei, hres, str);
733 /* ECMA-262 3rd Edition 12.14 */
734 static HRESULT interp_push_except(exec_ctx_t *ctx)
736 const unsigned arg1 = ctx->parser->code->instrs[ctx->ip].arg1.uint;
737 const BSTR arg2 = ctx->parser->code->instrs[ctx->ip].arg2.bstr;
738 except_frame_t *except;
743 stack_top = ctx->top;
748 hres = stack_push_bool(ctx, TRUE);
751 hres = stack_push_bool(ctx, TRUE);
756 except = heap_alloc(sizeof(*except));
758 return E_OUTOFMEMORY;
760 except->stack_top = stack_top;
761 except->scope = ctx->scope_chain;
762 except->catch_off = arg1;
763 except->ident = arg2;
764 except->next = ctx->except_frame;
765 ctx->except_frame = except;
769 /* ECMA-262 3rd Edition 12.14 */
770 static HRESULT interp_pop_except(exec_ctx_t *ctx)
772 except_frame_t *except;
776 except = ctx->except_frame;
777 assert(except != NULL);
779 ctx->except_frame = except->next;
784 /* ECMA-262 3rd Edition 12.14 */
785 static HRESULT interp_end_finally(exec_ctx_t *ctx)
793 assert(V_VT(stack_top(ctx)) == VT_BOOL);
794 if(!V_BOOL(stack_top(ctx))) {
795 TRACE("passing exception\n");
799 ctx->ei->var = *stack_pop(ctx);
800 return DISP_E_EXCEPTION;
804 return stack_push(ctx, v);
807 /* ECMA-262 3rd Edition 13 */
808 static HRESULT interp_func(exec_ctx_t *ctx)
810 function_expression_t *expr = ctx->parser->code->instrs[ctx->ip].arg1.func;
817 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
818 expr->src_str, expr->src_len, &dispex);
822 var_set_jsdisp(&v, dispex);
823 return stack_push(ctx, &v);
826 /* ECMA-262 3rd Edition 11.2.1 */
827 static HRESULT interp_array(exec_ctx_t *ctx)
837 namev = stack_pop(ctx);
839 hres = stack_pop_object(ctx, &obj);
845 hres = to_string(ctx->parser->script, namev, ctx->ei, &name);
848 IDispatch_Release(obj);
852 hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
854 if(SUCCEEDED(hres)) {
855 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
856 }else if(hres == DISP_E_UNKNOWNNAME) {
860 IDispatch_Release(obj);
864 return stack_push(ctx, &v);
867 /* ECMA-262 3rd Edition 11.2.1 */
868 static HRESULT interp_member(exec_ctx_t *ctx)
870 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
878 hres = stack_pop_object(ctx, &obj);
882 hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
883 if(SUCCEEDED(hres)) {
885 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
886 }else if(hres == DISP_E_UNKNOWNNAME) {
890 IDispatch_Release(obj);
894 return stack_push(ctx, &v);
897 /* ECMA-262 3rd Edition 11.2.1 */
898 static HRESULT interp_memberid(exec_ctx_t *ctx)
900 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
901 VARIANT *objv, *namev;
909 namev = stack_pop(ctx);
910 objv = stack_pop(ctx);
912 hres = to_object(ctx->parser->script, objv, &obj);
914 if(SUCCEEDED(hres)) {
915 hres = to_string(ctx->parser->script, namev, ctx->ei, &name);
917 IDispatch_Release(obj);
923 hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
926 IDispatch_Release(obj);
927 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
929 id = JS_E_INVALID_PROPERTY;
935 return stack_push_objid(ctx, obj, id);
938 /* ECMA-262 3rd Edition 11.2.1 */
939 static HRESULT interp_refval(exec_ctx_t *ctx)
948 disp = stack_topn_objid(ctx, 0, &id);
950 return throw_reference_error(ctx->parser->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
952 hres = disp_propget(ctx->parser->script, disp, id, &v, ctx->ei, NULL/*FIXME*/);
956 return stack_push(ctx, &v);
959 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
965 dp->rgdispidNamedArgs = NULL;
968 assert(ctx->top >= arg_cnt);
970 for(i=1; i*2 <= arg_cnt; i++) {
971 tmp = ctx->stack[ctx->top-i];
972 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
973 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
976 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
979 /* ECMA-262 3rd Edition 11.2.2 */
980 static HRESULT interp_new(exec_ctx_t *ctx)
982 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
989 constr = stack_topn(ctx, arg);
991 /* NOTE: Should use to_object here */
993 if(V_VT(constr) == VT_NULL)
994 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
995 else if(V_VT(constr) != VT_DISPATCH)
996 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
997 else if(!V_DISPATCH(constr))
998 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1000 jsstack_to_dp(ctx, arg, &dp);
1001 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1002 DISPATCH_CONSTRUCT, &dp, &v, ctx->ei, NULL/*FIXME*/);
1006 stack_popn(ctx, arg+1);
1007 return stack_push(ctx, &v);
1010 /* ECMA-262 3rd Edition 11.2.3 */
1011 static HRESULT interp_call(exec_ctx_t *ctx)
1013 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1014 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1019 TRACE("%d %d\n", argn, do_ret);
1021 objv = stack_topn(ctx, argn);
1022 if(V_VT(objv) != VT_DISPATCH)
1023 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1025 jsstack_to_dp(ctx, argn, &dp);
1026 hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1027 do_ret ? &v : NULL, ctx->ei, NULL/*FIXME*/);
1031 stack_popn(ctx, argn+1);
1032 return do_ret ? stack_push(ctx, &v) : S_OK;
1036 /* ECMA-262 3rd Edition 11.2.3 */
1037 static HRESULT interp_call_member(exec_ctx_t *ctx)
1039 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1040 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1047 TRACE("%d %d\n", argn, do_ret);
1049 obj = stack_topn_objid(ctx, argn, &id);
1051 return throw_type_error(ctx->parser->script, ctx->ei, id, NULL);
1053 jsstack_to_dp(ctx, argn, &dp);
1054 hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, ctx->ei, NULL/*FIXME*/);
1058 stack_popn(ctx, argn+2);
1059 return do_ret ? stack_push(ctx, &v) : S_OK;
1063 /* ECMA-262 3rd Edition 11.1.1 */
1064 static HRESULT interp_this(exec_ctx_t *ctx)
1070 V_VT(&v) = VT_DISPATCH;
1071 V_DISPATCH(&v) = ctx->this_obj;
1072 IDispatch_AddRef(ctx->this_obj);
1073 return stack_push(ctx, &v);
1076 /* ECMA-262 3rd Edition 10.1.4 */
1077 static HRESULT interp_ident(exec_ctx_t *ctx)
1079 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1084 TRACE("%s\n", debugstr_w(arg));
1086 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
1090 if(exprval.type == EXPRVAL_INVALID)
1091 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1093 hres = exprval_to_value(ctx->parser->script, &exprval, ctx->ei, &v);
1094 exprval_release(&exprval);
1098 return stack_push(ctx, &v);
1101 /* ECMA-262 3rd Edition 10.1.4 */
1102 static HRESULT interp_identid(exec_ctx_t *ctx)
1104 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1105 const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1109 TRACE("%s %x\n", debugstr_w(arg), flags);
1111 hres = identifier_eval(ctx->parser->script, arg, flags, ctx->ei, &exprval);
1115 if(exprval.type != EXPRVAL_IDREF) {
1116 WARN("invalid ref\n");
1117 exprval_release(&exprval);
1118 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1121 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1124 /* ECMA-262 3rd Edition 7.8.1 */
1125 static HRESULT interp_null(exec_ctx_t *ctx)
1132 return stack_push(ctx, &v);
1135 /* ECMA-262 3rd Edition 7.8.2 */
1136 static HRESULT interp_bool(exec_ctx_t *ctx)
1138 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1140 TRACE("%s\n", arg ? "true" : "false");
1142 return stack_push_bool(ctx, arg);
1145 /* ECMA-262 3rd Edition 7.8.3 */
1146 static HRESULT interp_int(exec_ctx_t *ctx)
1148 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1155 return stack_push(ctx, &v);
1158 /* ECMA-262 3rd Edition 7.8.3 */
1159 static HRESULT interp_double(exec_ctx_t *ctx)
1161 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1164 TRACE("%lf\n", arg);
1168 return stack_push(ctx, &v);
1171 /* ECMA-262 3rd Edition 7.8.4 */
1172 static HRESULT interp_str(exec_ctx_t *ctx)
1174 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1177 TRACE("%s\n", debugstr_w(str));
1180 V_BSTR(&v) = SysAllocString(str);
1182 return E_OUTOFMEMORY;
1184 return stack_push(ctx, &v);
1187 /* ECMA-262 3rd Edition 7.8 */
1188 static HRESULT interp_regexp(exec_ctx_t *ctx)
1190 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1191 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1196 TRACE("%s %x\n", debugstr_w(source), flags);
1198 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, ®exp);
1202 var_set_jsdisp(&v, regexp);
1203 return stack_push(ctx, &v);
1206 /* ECMA-262 3rd Edition 11.1.4 */
1207 static HRESULT interp_carray(exec_ctx_t *ctx)
1209 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1217 hres = create_array(ctx->parser->script, arg, &array);
1224 hres = jsdisp_propput_idx(array, i, v, ctx->ei, NULL/*FIXME*/);
1227 jsdisp_release(array);
1232 var_set_jsdisp(&r, array);
1233 return stack_push(ctx, &r);
1236 /* ECMA-262 3rd Edition 11.1.5 */
1237 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1245 hres = create_object(ctx->parser->script, NULL, &obj);
1249 var_set_jsdisp(&v, obj);
1250 return stack_push(ctx, &v);
1253 /* ECMA-262 3rd Edition 11.1.5 */
1254 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1256 const BSTR name = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1261 TRACE("%s\n", debugstr_w(name));
1265 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1266 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1268 hres = jsdisp_propput_name(obj, name, v, ctx->ei, NULL/*FIXME*/);
1273 /* ECMA-262 3rd Edition 11.11 */
1274 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1276 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1282 hres = to_boolean(stack_top(ctx), &b);
1295 /* ECMA-262 3rd Edition 11.11 */
1296 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1298 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1304 hres = to_boolean(stack_top(ctx), &b);
1317 /* ECMA-262 3rd Edition 11.10 */
1318 static HRESULT interp_or(exec_ctx_t *ctx)
1325 hres = stack_pop_int(ctx, &r);
1329 hres = stack_pop_int(ctx, &l);
1333 return stack_push_int(ctx, l|r);
1336 /* ECMA-262 3rd Edition 11.10 */
1337 static HRESULT interp_xor(exec_ctx_t *ctx)
1344 hres = stack_pop_int(ctx, &r);
1348 hres = stack_pop_int(ctx, &l);
1352 return stack_push_int(ctx, l^r);
1355 /* ECMA-262 3rd Edition 11.10 */
1356 static HRESULT interp_and(exec_ctx_t *ctx)
1363 hres = stack_pop_int(ctx, &r);
1367 hres = stack_pop_int(ctx, &l);
1371 return stack_push_int(ctx, l&r);
1374 /* ECMA-262 3rd Edition 11.8.6 */
1375 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1377 jsdisp_t *obj, *iter, *tmp = NULL;
1382 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1385 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1387 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1390 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1391 IDispatch_Release(V_DISPATCH(v));
1393 FIXME("non-jsdisp objects not supported\n");
1397 if(is_class(obj, JSCLASS_FUNCTION)) {
1398 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei, NULL/*FIXME*/);
1400 hres = throw_type_error(ctx->parser->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1402 jsdisp_release(obj);
1408 if(V_VT(&prot) == VT_DISPATCH) {
1409 if(V_VT(v) == VT_DISPATCH)
1410 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1411 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1412 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1418 jsdisp_release(tmp);
1420 FIXME("prototype is not an object\n");
1424 VariantClear(&prot);
1429 return stack_push_bool(ctx, ret);
1432 /* ECMA-262 3rd Edition 11.8.7 */
1433 static HRESULT interp_in(exec_ctx_t *ctx)
1443 obj = stack_pop(ctx);
1446 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1449 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1452 hres = to_string(ctx->parser->script, v, ctx->ei, &str);
1455 IDispatch_Release(V_DISPATCH(obj));
1459 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
1460 IDispatch_Release(V_DISPATCH(obj));
1464 else if(hres == DISP_E_UNKNOWNNAME)
1469 return stack_push_bool(ctx, ret);
1472 /* ECMA-262 3rd Edition 11.6.1 */
1473 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1478 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1482 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1488 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1489 BSTR lstr = NULL, rstr = NULL;
1491 if(V_VT(&l) == VT_BSTR)
1494 hres = to_string(ctx, &l, ei, &lstr);
1496 if(SUCCEEDED(hres)) {
1497 if(V_VT(&r) == VT_BSTR)
1500 hres = to_string(ctx, &r, ei, &rstr);
1503 if(SUCCEEDED(hres)) {
1506 len1 = SysStringLen(lstr);
1507 len2 = SysStringLen(rstr);
1509 V_VT(retv) = VT_BSTR;
1510 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1511 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1512 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1515 if(V_VT(&l) != VT_BSTR)
1516 SysFreeString(lstr);
1517 if(V_VT(&r) != VT_BSTR)
1518 SysFreeString(rstr);
1522 hres = to_number(ctx, &l, ei, &nl);
1523 if(SUCCEEDED(hres)) {
1524 hres = to_number(ctx, &r, ei, &nr);
1526 num_set_val(retv, num_val(&nl) + num_val(&nr));
1535 /* ECMA-262 3rd Edition 11.6.1 */
1536 static HRESULT interp_add(exec_ctx_t *ctx)
1538 VARIANT *l, *r, ret;
1544 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1546 hres = add_eval(ctx->parser->script, l, r, ctx->ei, &ret);
1552 return stack_push(ctx, &ret);
1555 /* ECMA-262 3rd Edition 11.6.2 */
1556 static HRESULT interp_sub(exec_ctx_t *ctx)
1563 hres = stack_pop_number(ctx, &r);
1567 hres = stack_pop_number(ctx, &l);
1571 return stack_push_number(ctx, num_val(&l)-num_val(&r));
1574 /* ECMA-262 3rd Edition 11.5.1 */
1575 static HRESULT interp_mul(exec_ctx_t *ctx)
1582 hres = stack_pop_number(ctx, &r);
1586 hres = stack_pop_number(ctx, &l);
1590 return stack_push_number(ctx, num_val(&l)*num_val(&r));
1593 /* ECMA-262 3rd Edition 11.5.2 */
1594 static HRESULT interp_div(exec_ctx_t *ctx)
1601 hres = stack_pop_number(ctx, &r);
1605 hres = stack_pop_number(ctx, &l);
1609 return stack_push_number(ctx, num_val(&l)/num_val(&r));
1612 /* ECMA-262 3rd Edition 11.5.3 */
1613 static HRESULT interp_mod(exec_ctx_t *ctx)
1620 hres = stack_pop_number(ctx, &r);
1624 hres = stack_pop_number(ctx, &l);
1628 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
1631 /* ECMA-262 3rd Edition 11.4.2 */
1632 static HRESULT interp_delete(exec_ctx_t *ctx)
1634 VARIANT *obj_var, *name_var;
1635 IDispatchEx *dispex;
1643 name_var = stack_pop(ctx);
1644 obj_var = stack_pop(ctx);
1646 hres = to_object(ctx->parser->script, obj_var, &obj);
1647 VariantClear(obj_var);
1649 VariantClear(name_var);
1653 hres = to_string(ctx->parser->script, name_var, ctx->ei, &name);
1654 VariantClear(name_var);
1656 IDispatch_Release(obj);
1660 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1661 if(SUCCEEDED(hres)) {
1662 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
1664 IDispatchEx_Release(dispex);
1670 IDispatch_Release(obj);
1671 SysFreeString(name);
1675 return stack_push_bool(ctx, ret);
1678 /* ECMA-262 3rd Edition 11.4.2 */
1679 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1681 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1682 IDispatchEx *dispex;
1687 TRACE("%s\n", debugstr_w(arg));
1689 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
1693 if(exprval.type != EXPRVAL_IDREF) {
1694 FIXME("Unsupported exprval\n");
1695 exprval_release(&exprval);
1699 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1700 IDispatch_Release(exprval.u.idref.disp);
1701 if(SUCCEEDED(hres)) {
1702 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1703 IDispatchEx_Release(dispex);
1710 return stack_push_bool(ctx, ret);
1713 /* ECMA-262 3rd Edition 11.4.2 */
1714 static HRESULT interp_void(exec_ctx_t *ctx)
1722 V_VT(&v) = VT_EMPTY;
1723 return stack_push(ctx, &v);
1726 /* ECMA-262 3rd Edition 11.4.3 */
1727 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1749 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1750 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1751 jsdisp_release(dispex);
1758 FIXME("unhandled vt %d\n", V_VT(v));
1765 /* ECMA-262 3rd Edition 11.4.3 */
1766 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1774 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1778 obj = stack_pop_objid(ctx, &id);
1780 return stack_push_string(ctx, undefinedW);
1782 V_VT(&v) = VT_EMPTY;
1783 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1784 IDispatch_Release(obj);
1786 return stack_push_string(ctx, unknownW);
1788 hres = typeof_string(&v, &ret);
1793 return stack_push_string(ctx, ret);
1796 /* ECMA-262 3rd Edition 11.4.3 */
1797 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1799 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1805 TRACE("%s\n", debugstr_w(arg));
1807 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
1811 if(exprval.type == EXPRVAL_INVALID) {
1812 hres = stack_push_string(ctx, undefinedW);
1813 exprval_release(&exprval);
1817 hres = exprval_to_value(ctx->parser->script, &exprval, ctx->ei, &v);
1818 exprval_release(&exprval);
1822 hres = typeof_string(&v, &ret);
1827 return stack_push_string(ctx, ret);
1830 /* ECMA-262 3rd Edition 11.4.3 */
1831 static HRESULT interp_typeof(exec_ctx_t *ctx)
1840 hres = typeof_string(v, &ret);
1845 return stack_push_string(ctx, ret);
1848 /* ECMA-262 3rd Edition 11.4.7 */
1849 static HRESULT interp_minus(exec_ctx_t *ctx)
1856 hres = stack_pop_number(ctx, &n);
1860 return stack_push_number(ctx, -num_val(&n));
1863 /* ECMA-262 3rd Edition 11.4.6 */
1864 static HRESULT interp_tonum(exec_ctx_t *ctx)
1872 hres = to_number(ctx->parser->script, v, ctx->ei, &num);
1877 return stack_push(ctx, &num);
1880 /* ECMA-262 3rd Edition 11.3.1 */
1881 static HRESULT interp_postinc(exec_ctx_t *ctx)
1883 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1891 obj = stack_pop_objid(ctx, &id);
1893 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1895 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1896 if(SUCCEEDED(hres)) {
1899 hres = to_number(ctx->parser->script, &v, ctx->ei, &n);
1900 if(SUCCEEDED(hres)) {
1901 num_set_val(&inc, num_val(&n)+(double)arg);
1902 hres = disp_propput(ctx->parser->script, obj, id, &inc, ctx->ei, NULL/*FIXME*/);
1907 IDispatch_Release(obj);
1911 return stack_push(ctx, &v);
1914 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1915 static HRESULT interp_preinc(exec_ctx_t *ctx)
1917 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1925 obj = stack_pop_objid(ctx, &id);
1927 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1929 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1930 if(SUCCEEDED(hres)) {
1933 hres = to_number(ctx->parser->script, &v, ctx->ei, &n);
1935 if(SUCCEEDED(hres)) {
1936 num_set_val(&v, num_val(&n)+(double)arg);
1937 hres = disp_propput(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1940 IDispatch_Release(obj);
1944 return stack_push(ctx, &v);
1947 /* ECMA-262 3rd Edition 11.9.3 */
1948 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1950 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1951 return equal2_values(lval, rval, ret);
1953 /* FIXME: NULL disps should be handled in more general way */
1954 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1957 return equal_values(ctx, &v, rval, ei, ret);
1960 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1963 return equal_values(ctx, lval, &v, ei, ret);
1966 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1967 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1972 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1976 hres = to_number(ctx, lval, ei, &v);
1980 return equal_values(ctx, &v, rval, ei, ret);
1983 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
1987 hres = to_number(ctx, rval, ei, &v);
1991 return equal_values(ctx, lval, &v, ei, ret);
1994 if(V_VT(rval) == VT_BOOL) {
1998 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
1999 return equal_values(ctx, lval, &v, ei, ret);
2002 if(V_VT(lval) == VT_BOOL) {
2006 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2007 return equal_values(ctx, &v, rval, ei, ret);
2011 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2015 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2019 hres = equal_values(ctx, lval, &v, ei, ret);
2026 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2030 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2034 hres = equal_values(ctx, &v, rval, ei, ret);
2045 /* ECMA-262 3rd Edition 11.9.1 */
2046 static HRESULT interp_eq(exec_ctx_t *ctx)
2055 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2057 hres = equal_values(ctx->parser->script, l, r, ctx->ei, &b);
2063 return stack_push_bool(ctx, b);
2066 /* ECMA-262 3rd Edition 11.9.2 */
2067 static HRESULT interp_neq(exec_ctx_t *ctx)
2076 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2078 hres = equal_values(ctx->parser->script, l, r, ctx->ei, &b);
2084 return stack_push_bool(ctx, !b);
2087 /* ECMA-262 3rd Edition 11.9.4 */
2088 static HRESULT interp_eq2(exec_ctx_t *ctx)
2099 hres = equal2_values(r, l, &b);
2105 return stack_push_bool(ctx, b);
2108 /* ECMA-262 3rd Edition 11.9.5 */
2109 static HRESULT interp_neq2(exec_ctx_t *ctx)
2120 hres = equal2_values(r, l, &b);
2126 return stack_push_bool(ctx, !b);
2129 /* ECMA-262 3rd Edition 11.8.5 */
2130 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2132 VARIANT l, r, ln, rn;
2135 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2139 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2145 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2146 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2147 SysFreeString(V_BSTR(&l));
2148 SysFreeString(V_BSTR(&r));
2152 hres = to_number(ctx, &l, ei, &ln);
2155 hres = to_number(ctx, &r, ei, &rn);
2160 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2161 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2163 DOUBLE ld = num_val(&ln);
2164 DOUBLE rd = num_val(&rn);
2166 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2172 /* ECMA-262 3rd Edition 11.8.1 */
2173 static HRESULT interp_lt(exec_ctx_t *ctx)
2182 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2184 hres = less_eval(ctx->parser->script, l, r, FALSE, ctx->ei, &b);
2190 return stack_push_bool(ctx, b);
2193 /* ECMA-262 3rd Edition 11.8.1 */
2194 static HRESULT interp_lteq(exec_ctx_t *ctx)
2203 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2205 hres = less_eval(ctx->parser->script, r, l, TRUE, ctx->ei, &b);
2211 return stack_push_bool(ctx, b);
2214 /* ECMA-262 3rd Edition 11.8.2 */
2215 static HRESULT interp_gt(exec_ctx_t *ctx)
2224 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2226 hres = less_eval(ctx->parser->script, r, l, FALSE, ctx->ei, &b);
2232 return stack_push_bool(ctx, b);
2235 /* ECMA-262 3rd Edition 11.8.4 */
2236 static HRESULT interp_gteq(exec_ctx_t *ctx)
2245 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2247 hres = less_eval(ctx->parser->script, l, r, TRUE, ctx->ei, &b);
2253 return stack_push_bool(ctx, b);
2256 /* ECMA-262 3rd Edition 11.4.8 */
2257 static HRESULT interp_bneg(exec_ctx_t *ctx)
2266 hres = to_int32(ctx->parser->script, v, ctx->ei, &i);
2273 return stack_push(ctx, &r);
2276 /* ECMA-262 3rd Edition 11.4.9 */
2277 static HRESULT interp_neg(exec_ctx_t *ctx)
2286 hres = to_boolean(v, &b);
2291 return stack_push_bool(ctx, !b);
2294 /* ECMA-262 3rd Edition 11.7.1 */
2295 static HRESULT interp_lshift(exec_ctx_t *ctx)
2301 hres = stack_pop_uint(ctx, &r);
2305 hres = stack_pop_int(ctx, &l);
2309 return stack_push_int(ctx, l << (r&0x1f));
2312 /* ECMA-262 3rd Edition 11.7.2 */
2313 static HRESULT interp_rshift(exec_ctx_t *ctx)
2319 hres = stack_pop_uint(ctx, &r);
2323 hres = stack_pop_int(ctx, &l);
2327 return stack_push_int(ctx, l >> (r&0x1f));
2330 /* ECMA-262 3rd Edition 11.7.3 */
2331 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2336 hres = stack_pop_uint(ctx, &r);
2340 hres = stack_pop_uint(ctx, &l);
2344 return stack_push_int(ctx, l >> (r&0x1f));
2347 /* ECMA-262 3rd Edition 11.13.1 */
2348 static HRESULT interp_assign(exec_ctx_t *ctx)
2358 disp = stack_pop_objid(ctx, &id);
2361 return throw_reference_error(ctx->parser->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2363 hres = disp_propput(ctx->parser->script, disp, id, v, ctx->ei, NULL/*FIXME*/);
2364 IDispatch_Release(disp);
2370 return stack_push(ctx, v);
2373 static HRESULT interp_undefined(exec_ctx_t *ctx)
2379 V_VT(&v) = VT_EMPTY;
2380 return stack_push(ctx, &v);
2383 static HRESULT interp_jmp(exec_ctx_t *ctx)
2385 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2393 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2395 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2403 hres = to_boolean(v, &b);
2415 static HRESULT interp_pop(exec_ctx_t *ctx)
2423 static HRESULT interp_ret(exec_ctx_t *ctx)
2431 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2433 static const op_func_t op_funcs[] = {
2434 #define X(x,a,b,c) interp_##x,
2439 static const unsigned op_move[] = {
2440 #define X(a,x,b,c) x,
2445 static HRESULT unwind_exception(exec_ctx_t *ctx)
2447 except_frame_t *except_frame;
2452 except_frame = ctx->except_frame;
2453 ctx->except_frame = except_frame->next;
2455 assert(except_frame->stack_top <= ctx->top);
2456 stack_popn(ctx, ctx->top - except_frame->stack_top);
2458 while(except_frame->scope != ctx->scope_chain)
2459 scope_pop(&ctx->scope_chain);
2461 ctx->ip = except_frame->catch_off;
2463 except_val = ctx->ei->var;
2464 memset(ctx->ei, 0, sizeof(*ctx->ei));
2466 ident = except_frame->ident;
2467 heap_free(except_frame);
2470 jsdisp_t *scope_obj;
2472 hres = create_dispex(ctx->parser->script, NULL, NULL, &scope_obj);
2473 if(SUCCEEDED(hres)) {
2474 hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei, NULL/*FIXME*/);
2476 jsdisp_release(scope_obj);
2478 VariantClear(&except_val);
2482 hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2483 jsdisp_release(scope_obj);
2487 hres = stack_push(ctx, &except_val);
2491 hres = stack_push_bool(ctx, FALSE);
2495 V_VT(&v) = VT_EMPTY;
2496 hres = stack_push(ctx, &v);
2502 static HRESULT enter_bytecode(script_ctx_t *ctx, unsigned ip, jsexcept_t *ei, VARIANT *ret)
2504 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2505 except_frame_t *prev_except_frame;
2506 unsigned prev_ip, prev_top;
2507 scope_chain_t *prev_scope;
2508 jsexcept_t *prev_ei;
2510 HRESULT hres = S_OK;
2514 prev_top = exec_ctx->top;
2515 prev_scope = exec_ctx->scope_chain;
2516 prev_except_frame = exec_ctx->except_frame;
2517 prev_ip = exec_ctx->ip;
2518 prev_ei = exec_ctx->ei;
2521 exec_ctx->except_frame = NULL;
2523 while(exec_ctx->ip != -1) {
2524 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
2525 hres = op_funcs[op](exec_ctx);
2527 TRACE("EXCEPTION\n");
2529 if(!exec_ctx->except_frame)
2532 hres = unwind_exception(exec_ctx);
2536 exec_ctx->ip += op_move[op];
2540 exec_ctx->ip = prev_ip;
2541 exec_ctx->ei = prev_ei;
2542 exec_ctx->except_frame = prev_except_frame;
2545 while(exec_ctx->scope_chain != prev_scope)
2546 scope_pop(&exec_ctx->scope_chain);
2547 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2551 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2552 assert(exec_ctx->scope_chain == prev_scope);
2554 if(exec_ctx->top == prev_top)
2555 V_VT(ret) = VT_EMPTY;
2557 *ret = *stack_pop(exec_ctx);
2561 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
2562 jsexcept_t *ei, VARIANT *retv)
2564 script_ctx_t *script = parser->script;
2565 function_declaration_t *func;
2566 parser_ctx_t *prev_parser;
2569 exec_ctx_t *prev_ctx;
2570 HRESULT hres = S_OK;
2572 for(func = source->functions; func; func = func->next) {
2576 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
2577 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
2581 var_set_jsdisp(&var, func_obj);
2582 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
2583 jsdisp_release(func_obj);
2588 for(var = source->variables; var; var = var->next) {
2592 name = SysAllocString(var->identifier);
2594 return E_OUTOFMEMORY;
2596 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
2597 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
2598 SysFreeString(name);
2603 prev_ctx = script->exec_ctx;
2604 script->exec_ctx = ctx;
2606 prev_parser = ctx->parser;
2607 ctx->parser = parser;
2609 if(source->statement) {
2610 if(!source->instr_off) {
2611 hres = compile_subscript_stat(ctx->parser, source->statement, from_eval, &source->instr_off);
2612 if(FAILED(hres) && is_jscript_error(hres))
2613 hres = throw_syntax_error(script, ei, hres, NULL);
2616 hres = enter_bytecode(script, source->instr_off, ei, &val);
2618 V_VT(&val) = VT_EMPTY;
2621 script->exec_ctx = prev_ctx;
2622 ctx->parser = prev_parser;