2 * Copyright 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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
37 dynamic_var_t *dynamic_vars;
49 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
79 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
82 if(!strcmpiW(var->name, name)) {
83 ref->type = var->is_const ? REF_CONST : REF_VAR;
94 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
102 static const WCHAR errW[] = {'e','r','r',0};
104 if(invoke_type == VBDISP_LET
105 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
106 && !strcmpiW(name, ctx->func->name)) {
108 ref->u.v = &ctx->ret_val;
112 for(i=0; i < ctx->func->var_cnt; i++) {
113 if(!strcmpiW(ctx->func->vars[i].name, name)) {
115 ref->u.v = ctx->vars+i;
120 for(i=0; i < ctx->func->arg_cnt; i++) {
121 if(!strcmpiW(ctx->func->args[i].name, name)) {
123 ref->u.v = ctx->args+i;
128 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
131 if(ctx->func->type != FUNC_GLOBAL) {
132 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
133 if(SUCCEEDED(hres)) {
134 ref->type = REF_DISP;
135 ref->u.d.disp = ctx->this_obj;
141 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
144 for(func = ctx->script->global_funcs; func; func = func->next) {
145 if(!strcmpiW(func->name, name)) {
146 ref->type = REF_FUNC;
152 if(!strcmpiW(name, errW)) {
154 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
158 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
159 if(SUCCEEDED(hres)) {
160 ref->type = REF_DISP;
161 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
166 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
167 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
171 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
173 WARN("GetItemInfo failed: %08x\n", hres);
177 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
178 IUnknown_Release(unk);
180 WARN("object does not implement IDispatch\n");
186 ref->u.obj = item->disp;
191 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
192 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
193 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
194 if(SUCCEEDED(hres)) {
195 ref->type = REF_DISP;
196 ref->u.d.disp = item->disp;
203 ref->type = REF_NONE;
207 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
209 dynamic_var_t *new_var;
215 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
217 new_var = vbsheap_alloc(heap, sizeof(*new_var));
219 return E_OUTOFMEMORY;
221 size = (strlenW(name)+1)*sizeof(WCHAR);
222 str = vbsheap_alloc(heap, size);
224 return E_OUTOFMEMORY;
225 memcpy(str, name, size);
227 new_var->is_const = is_const;
232 V_VT(&new_var->v) = VT_EMPTY;
233 hres = VariantCopy(&new_var->v, val);
238 if(ctx->func->type == FUNC_GLOBAL) {
239 new_var->next = ctx->script->global_vars;
240 ctx->script->global_vars = new_var;
242 new_var->next = ctx->dynamic_vars;
243 ctx->dynamic_vars = new_var;
249 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
252 return ctx->stack + --ctx->top;
255 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
257 assert(ctx->top >= n);
258 return ctx->stack + (ctx->top-n-1);
261 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
263 if(ctx->stack_size == ctx->top) {
266 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
269 return E_OUTOFMEMORY;
272 ctx->stack = new_stack;
273 ctx->stack_size *= 2;
276 ctx->stack[ctx->top++] = *v;
280 static void stack_popn(exec_ctx_t *ctx, unsigned n)
283 VariantClear(stack_pop(ctx));
286 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
290 var = stack_pop(ctx);
292 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
294 var = V_VARIANTREF(var);
299 if(V_VT(var) == VT_DISPATCH) {
303 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
305 IDispatch_Release(V_DISPATCH(var));
318 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
320 VARIANT *v = stack_top(ctx, n);
323 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
324 VARIANT *ref = V_VARIANTREF(v);
327 hres = VariantCopy(v, ref);
332 if(V_VT(v) == VT_DISPATCH) {
336 disp = V_DISPATCH(v);
338 hres = disp_call(ctx->script, disp, DISPID_VALUE, &dp, v);
339 IDispatch_Release(disp);
347 static inline void release_val(variant_val_t *v)
353 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
355 VARIANT *v = stack_pop(ctx);
357 if(V_VT(v) == VT_DISPATCH) {
358 *ret = V_DISPATCH(v);
362 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
363 FIXME("not supported type: %s\n", debugstr_variant(v));
369 if(V_VT(v) != VT_DISPATCH) {
370 FIXME("not disp %s\n", debugstr_variant(v));
375 IDispatch_AddRef(V_DISPATCH(v));
376 *ret = V_DISPATCH(v);
380 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
382 VARIANT *v = stack_top(ctx, n), *ref;
384 if(V_VT(v) != VT_DISPATCH) {
385 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
386 FIXME("not supported type: %s\n", debugstr_variant(v));
390 ref = V_VARIANTREF(v);
391 if(V_VT(ref) != VT_DISPATCH) {
392 FIXME("not disp %s\n", debugstr_variant(ref));
396 V_VT(v) = VT_DISPATCH;
397 V_DISPATCH(v) = V_DISPATCH(ref);
399 IDispatch_AddRef(V_DISPATCH(v));
403 *disp = V_DISPATCH(v);
407 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
409 ctx->instr = ctx->code->instrs + addr;
412 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
414 static DISPID propput_dispid = DISPID_PROPERTYPUT;
416 dp->cNamedArgs = is_propput ? 1 : 0;
417 dp->cArgs = arg_cnt + dp->cNamedArgs;
418 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
424 assert(ctx->top >= arg_cnt);
426 for(i=1; i*2 <= arg_cnt; i++) {
427 tmp = ctx->stack[ctx->top-i];
428 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
429 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
432 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
434 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
438 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
440 BSTR identifier = ctx->instr->arg1.bstr;
441 const unsigned arg_cnt = ctx->instr->arg2.uint;
446 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
450 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
456 FIXME("REF_VAR no res\n");
461 FIXME("arguments not implemented\n");
465 V_VT(res) = VT_BYREF|VT_VARIANT;
466 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
469 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
474 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
480 FIXME("arguments on object\n");
485 IDispatch_AddRef(ref.u.obj);
486 V_VT(res) = VT_DISPATCH;
487 V_DISPATCH(res) = ref.u.obj;
491 FIXME("%s not found\n", debugstr_w(identifier));
492 return DISP_E_UNKNOWNNAME;
495 stack_popn(ctx, arg_cnt);
499 static HRESULT interp_icall(exec_ctx_t *ctx)
506 hres = do_icall(ctx, &v);
510 return stack_push(ctx, &v);
513 static HRESULT interp_icallv(exec_ctx_t *ctx)
516 return do_icall(ctx, NULL);
519 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
521 const BSTR identifier = ctx->instr->arg1.bstr;
522 const unsigned arg_cnt = ctx->instr->arg2.uint;
528 hres = stack_pop_disp(ctx, &obj);
537 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
539 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
541 hres = disp_call(ctx->script, obj, id, &dp, res);
542 IDispatch_Release(obj);
546 stack_popn(ctx, arg_cnt);
550 static HRESULT interp_mcall(exec_ctx_t *ctx)
557 hres = do_mcall(ctx, &res);
561 return stack_push(ctx, &res);
564 static HRESULT interp_mcallv(exec_ctx_t *ctx)
568 return do_mcall(ctx, NULL);
571 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
576 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
582 VARIANT *v = ref.u.v;
585 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
589 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
592 hres = VariantCopy(v, dp->rgvarg);
596 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
599 FIXME("functions not implemented\n");
605 FIXME("REF_CONST\n");
608 if(ctx->func->code_ctx->option_explicit) {
609 FIXME("throw exception\n");
613 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
617 TRACE("creating variable %s\n", debugstr_w(name));
618 hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE);
625 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
627 const BSTR arg = ctx->instr->arg1.bstr;
628 const unsigned arg_cnt = ctx->instr->arg2.uint;
632 TRACE("%s\n", debugstr_w(arg));
634 hres = stack_assume_val(ctx, arg_cnt);
638 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
639 hres = assign_ident(ctx, arg, &dp);
643 stack_popn(ctx, arg_cnt+1);
647 static HRESULT interp_set_ident(exec_ctx_t *ctx)
649 const BSTR arg = ctx->instr->arg1.bstr;
650 const unsigned arg_cnt = ctx->instr->arg2.uint;
654 TRACE("%s\n", debugstr_w(arg));
657 FIXME("arguments not supported\n");
661 hres = stack_assume_disp(ctx, 0, NULL);
665 vbstack_to_dp(ctx, 0, TRUE, &dp);
666 hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
674 static HRESULT interp_assign_member(exec_ctx_t *ctx)
676 BSTR identifier = ctx->instr->arg1.bstr;
677 const unsigned arg_cnt = ctx->instr->arg2.uint;
683 TRACE("%s\n", debugstr_w(identifier));
685 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
694 hres = stack_assume_val(ctx, arg_cnt);
698 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
699 if(SUCCEEDED(hres)) {
700 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
701 hres = disp_propput(ctx->script, obj, id, &dp);
706 stack_popn(ctx, arg_cnt+2);
710 static HRESULT interp_set_member(exec_ctx_t *ctx)
712 BSTR identifier = ctx->instr->arg1.bstr;
713 const unsigned arg_cnt = ctx->instr->arg2.uint;
719 TRACE("%s\n", debugstr_w(identifier));
722 FIXME("arguments not supported\n");
726 hres = stack_assume_disp(ctx, 1, &obj);
735 hres = stack_assume_disp(ctx, 0, NULL);
739 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
740 if(SUCCEEDED(hres)) {
741 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
742 hres = disp_propput(ctx->script, obj, id, &dp);
751 static HRESULT interp_const(exec_ctx_t *ctx)
753 BSTR arg = ctx->instr->arg1.bstr;
758 TRACE("%s\n", debugstr_w(arg));
760 assert(ctx->func->type == FUNC_GLOBAL);
762 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
766 if(ref.type != REF_NONE) {
767 FIXME("%s already defined\n", debugstr_w(arg));
771 hres = stack_pop_val(ctx, &val);
775 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
778 static HRESULT interp_val(exec_ctx_t *ctx)
786 hres = stack_pop_val(ctx, &val);
792 hres = VariantCopy(&v, val.v);
797 return stack_push(ctx, val.owned ? val.v : &v);
800 static HRESULT interp_pop(exec_ctx_t *ctx)
802 const unsigned n = ctx->instr->arg1.uint;
810 static HRESULT interp_new(exec_ctx_t *ctx)
812 const WCHAR *arg = ctx->instr->arg1.bstr;
813 class_desc_t *class_desc;
818 TRACE("%s\n", debugstr_w(arg));
820 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
821 if(!strcmpiW(class_desc->name, arg))
825 FIXME("Class %s not found\n", debugstr_w(arg));
829 hres = create_vbdisp(class_desc, &obj);
833 V_VT(&v) = VT_DISPATCH;
834 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
835 return stack_push(ctx, &v);
838 static HRESULT interp_step(exec_ctx_t *ctx)
840 const BSTR ident = ctx->instr->arg2.bstr;
846 TRACE("%s\n", debugstr_w(ident));
850 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
854 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
856 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
860 if(ref.type != REF_VAR) {
861 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
865 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
869 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
872 instr_jmp(ctx, ctx->instr->arg1.uint);
876 static HRESULT interp_jmp(exec_ctx_t *ctx)
878 const unsigned arg = ctx->instr->arg1.uint;
886 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
888 const unsigned arg = ctx->instr->arg1.uint;
894 hres = stack_pop_val(ctx, &val);
898 if(V_VT(val.v) != VT_BOOL) {
899 FIXME("unsupported for %s\n", debugstr_variant(val.v));
907 instr_jmp(ctx, ctx->instr->arg1.uint);
911 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
913 const unsigned arg = ctx->instr->arg1.uint;
919 hres = stack_pop_val(ctx, &val);
923 if(V_VT(val.v) != VT_BOOL) {
924 FIXME("unsupported for %s\n", debugstr_variant(val.v));
930 instr_jmp(ctx, ctx->instr->arg1.uint);
936 static HRESULT interp_ret(exec_ctx_t *ctx)
944 static HRESULT interp_stop(exec_ctx_t *ctx)
948 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
952 static HRESULT interp_me(exec_ctx_t *ctx)
958 IDispatch_AddRef(ctx->this_obj);
959 V_VT(&v) = VT_DISPATCH;
960 V_DISPATCH(&v) = ctx->this_obj;
961 return stack_push(ctx, &v);
964 static HRESULT interp_bool(exec_ctx_t *ctx)
966 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
969 TRACE("%s\n", arg ? "true" : "false");
973 return stack_push(ctx, &v);
976 static HRESULT interp_errmode(exec_ctx_t *ctx)
978 const int err_mode = ctx->instr->arg1.uint;
980 TRACE("%d\n", err_mode);
982 ctx->resume_next = err_mode;
986 static HRESULT interp_string(exec_ctx_t *ctx)
993 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
995 return E_OUTOFMEMORY;
997 return stack_push(ctx, &v);
1000 static HRESULT interp_long(exec_ctx_t *ctx)
1002 const LONG arg = ctx->instr->arg1.lng;
1009 return stack_push(ctx, &v);
1012 static HRESULT interp_short(exec_ctx_t *ctx)
1014 const LONG arg = ctx->instr->arg1.lng;
1021 return stack_push(ctx, &v);
1024 static HRESULT interp_double(exec_ctx_t *ctx)
1026 const DOUBLE *arg = ctx->instr->arg1.dbl;
1029 TRACE("%lf\n", *arg);
1033 return stack_push(ctx, &v);
1036 static HRESULT interp_empty(exec_ctx_t *ctx)
1042 V_VT(&v) = VT_EMPTY;
1043 return stack_push(ctx, &v);
1046 static HRESULT interp_null(exec_ctx_t *ctx)
1053 return stack_push(ctx, &v);
1056 static HRESULT interp_nothing(exec_ctx_t *ctx)
1062 V_VT(&v) = VT_DISPATCH;
1063 V_DISPATCH(&v) = NULL;
1064 return stack_push(ctx, &v);
1067 static HRESULT interp_not(exec_ctx_t *ctx)
1075 hres = stack_pop_val(ctx, &val);
1079 hres = VarNot(val.v, &v);
1084 return stack_push(ctx, &v);
1087 static HRESULT interp_and(exec_ctx_t *ctx)
1095 hres = stack_pop_val(ctx, &r);
1099 hres = stack_pop_val(ctx, &l);
1100 if(SUCCEEDED(hres)) {
1101 hres = VarAnd(l.v, r.v, &v);
1108 return stack_push(ctx, &v);
1111 static HRESULT interp_or(exec_ctx_t *ctx)
1119 hres = stack_pop_val(ctx, &r);
1123 hres = stack_pop_val(ctx, &l);
1124 if(SUCCEEDED(hres)) {
1125 hres = VarOr(l.v, r.v, &v);
1132 return stack_push(ctx, &v);
1135 static HRESULT interp_xor(exec_ctx_t *ctx)
1143 hres = stack_pop_val(ctx, &r);
1147 hres = stack_pop_val(ctx, &l);
1148 if(SUCCEEDED(hres)) {
1149 hres = VarXor(l.v, r.v, &v);
1156 return stack_push(ctx, &v);
1159 static HRESULT interp_eqv(exec_ctx_t *ctx)
1167 hres = stack_pop_val(ctx, &r);
1171 hres = stack_pop_val(ctx, &l);
1172 if(SUCCEEDED(hres)) {
1173 hres = VarEqv(l.v, r.v, &v);
1180 return stack_push(ctx, &v);
1183 static HRESULT interp_imp(exec_ctx_t *ctx)
1191 hres = stack_pop_val(ctx, &r);
1195 hres = stack_pop_val(ctx, &l);
1196 if(SUCCEEDED(hres)) {
1197 hres = VarImp(l.v, r.v, &v);
1204 return stack_push(ctx, &v);
1207 static HRESULT cmp_oper(exec_ctx_t *ctx)
1212 hres = stack_pop_val(ctx, &r);
1216 hres = stack_pop_val(ctx, &l);
1217 if(SUCCEEDED(hres)) {
1218 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1219 FIXME("comparing nulls is not implemented\n");
1222 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1231 static HRESULT interp_equal(exec_ctx_t *ctx)
1238 hres = cmp_oper(ctx);
1243 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1244 return stack_push(ctx, &v);
1247 static HRESULT interp_nequal(exec_ctx_t *ctx)
1254 hres = cmp_oper(ctx);
1259 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1260 return stack_push(ctx, &v);
1263 static HRESULT interp_gt(exec_ctx_t *ctx)
1270 hres = cmp_oper(ctx);
1275 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1276 return stack_push(ctx, &v);
1279 static HRESULT interp_gteq(exec_ctx_t *ctx)
1286 hres = cmp_oper(ctx);
1291 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1292 return stack_push(ctx, &v);
1295 static HRESULT interp_lt(exec_ctx_t *ctx)
1302 hres = cmp_oper(ctx);
1307 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1308 return stack_push(ctx, &v);
1311 static HRESULT interp_lteq(exec_ctx_t *ctx)
1318 hres = cmp_oper(ctx);
1323 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1324 return stack_push(ctx, &v);
1327 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1329 IObjectIdentity *identity;
1330 IUnknown *unk1, *unk2;
1333 if(disp1 == disp2) {
1334 *ret = VARIANT_TRUE;
1338 if(!disp1 || !disp2) {
1339 *ret = VARIANT_FALSE;
1343 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1347 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1349 IUnknown_Release(unk1);
1354 *ret = VARIANT_TRUE;
1356 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1357 if(SUCCEEDED(hres)) {
1358 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1359 IObjectIdentity_Release(identity);
1360 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1362 *ret = VARIANT_FALSE;
1366 IUnknown_Release(unk1);
1367 IUnknown_Release(unk2);
1371 static HRESULT interp_is(exec_ctx_t *ctx)
1379 hres = stack_pop_disp(ctx, &r);
1383 hres = stack_pop_disp(ctx, &l);
1384 if(SUCCEEDED(hres)) {
1386 hres = disp_cmp(l, r, &V_BOOL(&v));
1388 IDispatch_Release(l);
1391 IDispatch_Release(r);
1395 return stack_push(ctx, &v);
1398 static HRESULT interp_concat(exec_ctx_t *ctx)
1406 hres = stack_pop_val(ctx, &r);
1410 hres = stack_pop_val(ctx, &l);
1411 if(SUCCEEDED(hres)) {
1412 hres = VarCat(l.v, r.v, &v);
1419 return stack_push(ctx, &v);
1422 static HRESULT interp_add(exec_ctx_t *ctx)
1430 hres = stack_pop_val(ctx, &r);
1434 hres = stack_pop_val(ctx, &l);
1435 if(SUCCEEDED(hres)) {
1436 hres = VarAdd(l.v, r.v, &v);
1443 return stack_push(ctx, &v);
1446 static HRESULT interp_sub(exec_ctx_t *ctx)
1454 hres = stack_pop_val(ctx, &r);
1458 hres = stack_pop_val(ctx, &l);
1459 if(SUCCEEDED(hres)) {
1460 hres = VarSub(l.v, r.v, &v);
1467 return stack_push(ctx, &v);
1470 static HRESULT interp_mod(exec_ctx_t *ctx)
1478 hres = stack_pop_val(ctx, &r);
1482 hres = stack_pop_val(ctx, &l);
1483 if(SUCCEEDED(hres)) {
1484 hres = VarMod(l.v, r.v, &v);
1491 return stack_push(ctx, &v);
1494 static HRESULT interp_idiv(exec_ctx_t *ctx)
1502 hres = stack_pop_val(ctx, &r);
1506 hres = stack_pop_val(ctx, &l);
1507 if(SUCCEEDED(hres)) {
1508 hres = VarIdiv(l.v, r.v, &v);
1515 return stack_push(ctx, &v);
1518 static HRESULT interp_div(exec_ctx_t *ctx)
1526 hres = stack_pop_val(ctx, &r);
1530 hres = stack_pop_val(ctx, &l);
1531 if(SUCCEEDED(hres)) {
1532 hres = VarDiv(l.v, r.v, &v);
1539 return stack_push(ctx, &v);
1542 static HRESULT interp_mul(exec_ctx_t *ctx)
1550 hres = stack_pop_val(ctx, &r);
1554 hres = stack_pop_val(ctx, &l);
1555 if(SUCCEEDED(hres)) {
1556 hres = VarMul(l.v, r.v, &v);
1563 return stack_push(ctx, &v);
1566 static HRESULT interp_exp(exec_ctx_t *ctx)
1574 hres = stack_pop_val(ctx, &r);
1578 hres = stack_pop_val(ctx, &l);
1579 if(SUCCEEDED(hres)) {
1580 hres = VarPow(l.v, r.v, &v);
1587 return stack_push(ctx, &v);
1590 static HRESULT interp_neg(exec_ctx_t *ctx)
1596 hres = stack_pop_val(ctx, &val);
1600 hres = VarNeg(val.v, &v);
1605 return stack_push(ctx, &v);
1608 static HRESULT interp_incc(exec_ctx_t *ctx)
1610 const BSTR ident = ctx->instr->arg1.bstr;
1617 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1621 if(ref.type != REF_VAR) {
1622 FIXME("ref.type is not REF_VAR\n");
1626 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1630 VariantClear(ref.u.v);
1635 static const instr_func_t op_funcs[] = {
1636 #define X(x,n,a,b) interp_ ## x,
1641 static const unsigned op_move[] = {
1642 #define X(x,n,a,b) n,
1647 void release_dynamic_vars(dynamic_var_t *var)
1650 VariantClear(&var->v);
1655 static void release_exec(exec_ctx_t *ctx)
1659 VariantClear(&ctx->ret_val);
1660 release_dynamic_vars(ctx->dynamic_vars);
1663 IDispatch_Release(ctx->this_obj);
1666 for(i=0; i < ctx->func->arg_cnt; i++)
1667 VariantClear(ctx->args+i);
1671 for(i=0; i < ctx->func->var_cnt; i++)
1672 VariantClear(ctx->vars+i);
1675 vbsheap_free(&ctx->heap);
1676 heap_free(ctx->args);
1677 heap_free(ctx->vars);
1678 heap_free(ctx->stack);
1681 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1683 exec_ctx_t exec = {func->code_ctx};
1685 HRESULT hres = S_OK;
1687 exec.code = func->code_ctx;
1689 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1690 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1694 vbsheap_init(&exec.heap);
1700 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1702 release_exec(&exec);
1703 return E_OUTOFMEMORY;
1706 for(i=0; i < func->arg_cnt; i++) {
1708 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1709 if(func->args[i].by_ref)
1712 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1714 hres = VariantCopy(exec.args+i, v);
1717 release_exec(&exec);
1726 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1728 release_exec(&exec);
1729 return E_OUTOFMEMORY;
1735 exec.stack_size = 16;
1737 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1739 release_exec(&exec);
1740 return E_OUTOFMEMORY;
1744 exec.this_obj = this_obj;
1745 else if (ctx->host_global)
1746 exec.this_obj = ctx->host_global;
1748 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1749 IDispatch_AddRef(exec.this_obj);
1751 exec.instr = exec.code->instrs + func->code_off;
1756 op = exec.instr->op;
1757 hres = op_funcs[op](&exec);
1759 if(exec.resume_next)
1760 FIXME("Failed %08x in resume next mode\n", hres);
1762 WARN("Failed %08x\n", hres);
1763 stack_popn(&exec, exec.top);
1767 exec.instr += op_move[op];
1771 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1772 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1774 if(SUCCEEDED(hres) && res) {
1775 *res = exec.ret_val;
1776 V_VT(&exec.ret_val) = VT_EMPTY;
1779 release_exec(&exec);