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 inline void release_val(variant_val_t *v)
324 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
326 VARIANT *v = stack_pop(ctx);
328 if(V_VT(v) == VT_DISPATCH) {
329 *ret = V_DISPATCH(v);
333 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
334 FIXME("not supported type: %s\n", debugstr_variant(v));
340 if(V_VT(v) != VT_DISPATCH) {
341 FIXME("not disp %s\n", debugstr_variant(v));
346 IDispatch_AddRef(V_DISPATCH(v));
347 *ret = V_DISPATCH(v);
351 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
353 ctx->instr = ctx->code->instrs + addr;
356 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
359 dp->rgdispidNamedArgs = NULL;
366 assert(ctx->top >= arg_cnt);
368 for(i=1; i*2 <= arg_cnt; i++) {
369 tmp = ctx->stack[ctx->top-i];
370 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
371 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
374 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
380 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
382 BSTR identifier = ctx->instr->arg1.bstr;
383 const unsigned arg_cnt = ctx->instr->arg2.uint;
388 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
392 vbstack_to_dp(ctx, arg_cnt, &dp);
398 FIXME("REF_VAR no res\n");
403 FIXME("arguments not implemented\n");
407 V_VT(res) = VT_BYREF|VT_VARIANT;
408 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
411 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
416 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
422 FIXME("arguments on object\n");
427 IDispatch_AddRef(ref.u.obj);
428 V_VT(res) = VT_DISPATCH;
429 V_DISPATCH(res) = ref.u.obj;
433 FIXME("%s not found\n", debugstr_w(identifier));
434 return DISP_E_UNKNOWNNAME;
437 stack_popn(ctx, arg_cnt);
441 static HRESULT interp_icall(exec_ctx_t *ctx)
448 hres = do_icall(ctx, &v);
452 return stack_push(ctx, &v);
455 static HRESULT interp_icallv(exec_ctx_t *ctx)
458 return do_icall(ctx, NULL);
461 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
463 const BSTR identifier = ctx->instr->arg1.bstr;
464 const unsigned arg_cnt = ctx->instr->arg2.uint;
470 hres = stack_pop_disp(ctx, &obj);
479 vbstack_to_dp(ctx, arg_cnt, &dp);
481 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
483 hres = disp_call(ctx->script, obj, id, &dp, res);
484 IDispatch_Release(obj);
488 stack_popn(ctx, arg_cnt);
492 static HRESULT interp_mcall(exec_ctx_t *ctx)
499 hres = do_mcall(ctx, &res);
503 return stack_push(ctx, &res);
506 static HRESULT interp_mcallv(exec_ctx_t *ctx)
510 return do_mcall(ctx, NULL);
513 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
518 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
524 VARIANT *v = ref.u.v;
526 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
534 hres = VariantCopy(v, val);
539 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
544 FIXME("functions not implemented\n");
550 FIXME("REF_CONST\n");
553 if(ctx->func->code_ctx->option_explicit) {
554 FIXME("throw exception\n");
557 TRACE("creating variable %s\n", debugstr_w(name));
558 hres = add_dynamic_var(ctx, name, FALSE, val, own_val);
565 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
567 const BSTR arg = ctx->instr->arg1.bstr;
568 const unsigned arg_cnt = ctx->instr->arg2.uint;
572 TRACE("%s\n", debugstr_w(arg));
575 FIXME("arguments not supported\n");
579 hres = stack_pop_val(ctx, &v);
583 return assign_ident(ctx, arg, v.v, v.owned);
586 static HRESULT interp_set_ident(exec_ctx_t *ctx)
588 const BSTR arg = ctx->instr->arg1.bstr;
589 const unsigned arg_cnt = ctx->instr->arg2.uint;
594 TRACE("%s\n", debugstr_w(arg));
597 FIXME("arguments not supported\n");
601 hres = stack_pop_disp(ctx, &disp);
605 V_VT(&v) = VT_DISPATCH;
606 V_DISPATCH(&v) = disp;
607 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
610 static HRESULT interp_assign_member(exec_ctx_t *ctx)
612 BSTR identifier = ctx->instr->arg1.bstr;
613 const unsigned arg_cnt = ctx->instr->arg2.uint;
619 TRACE("%s\n", debugstr_w(identifier));
622 FIXME("arguments not supported\n");
626 hres = stack_pop_disp(ctx, &obj);
635 hres = stack_pop_val(ctx, &val);
637 IDispatch_Release(obj);
641 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
643 hres = disp_propput(ctx->script, obj, id, val.v);
646 IDispatch_Release(obj);
650 static HRESULT interp_set_member(exec_ctx_t *ctx)
652 BSTR identifier = ctx->instr->arg1.bstr;
653 const unsigned arg_cnt = ctx->instr->arg2.uint;
654 IDispatch *obj, *val;
658 TRACE("%s\n", debugstr_w(identifier));
661 FIXME("arguments not supported\n");
665 hres = stack_pop_disp(ctx, &obj);
674 hres = stack_pop_disp(ctx, &val);
676 IDispatch_Release(obj);
680 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
681 if(SUCCEEDED(hres)) {
684 V_VT(&v) = VT_DISPATCH;
685 V_DISPATCH(&v) = val;
686 hres = disp_propput(ctx->script, obj, id, &v);
690 IDispatch_Release(val);
691 IDispatch_Release(obj);
695 static HRESULT interp_const(exec_ctx_t *ctx)
697 BSTR arg = ctx->instr->arg1.bstr;
702 TRACE("%s\n", debugstr_w(arg));
704 assert(ctx->func->type == FUNC_GLOBAL);
706 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
710 if(ref.type != REF_NONE) {
711 FIXME("%s already defined\n", debugstr_w(arg));
715 hres = stack_pop_val(ctx, &val);
719 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
722 static HRESULT interp_val(exec_ctx_t *ctx)
730 hres = stack_pop_val(ctx, &val);
736 hres = VariantCopy(&v, val.v);
741 return stack_push(ctx, val.owned ? val.v : &v);
744 static HRESULT interp_pop(exec_ctx_t *ctx)
746 const unsigned n = ctx->instr->arg1.uint;
754 static HRESULT interp_new(exec_ctx_t *ctx)
756 const WCHAR *arg = ctx->instr->arg1.bstr;
757 class_desc_t *class_desc;
762 TRACE("%s\n", debugstr_w(arg));
764 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
765 if(!strcmpiW(class_desc->name, arg))
769 FIXME("Class %s not found\n", debugstr_w(arg));
773 hres = create_vbdisp(class_desc, &obj);
777 V_VT(&v) = VT_DISPATCH;
778 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
779 return stack_push(ctx, &v);
782 static HRESULT interp_step(exec_ctx_t *ctx)
784 const BSTR ident = ctx->instr->arg2.bstr;
790 TRACE("%s\n", debugstr_w(ident));
794 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
798 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
800 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
804 if(ref.type != REF_VAR) {
805 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
809 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
813 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
816 instr_jmp(ctx, ctx->instr->arg1.uint);
820 static HRESULT interp_jmp(exec_ctx_t *ctx)
822 const unsigned arg = ctx->instr->arg1.uint;
830 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
832 const unsigned arg = ctx->instr->arg1.uint;
838 hres = stack_pop_val(ctx, &val);
842 if(V_VT(val.v) != VT_BOOL) {
843 FIXME("unsupported for %s\n", debugstr_variant(val.v));
851 instr_jmp(ctx, ctx->instr->arg1.uint);
855 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
857 const unsigned arg = ctx->instr->arg1.uint;
863 hres = stack_pop_val(ctx, &val);
867 if(V_VT(val.v) != VT_BOOL) {
868 FIXME("unsupported for %s\n", debugstr_variant(val.v));
874 instr_jmp(ctx, ctx->instr->arg1.uint);
880 static HRESULT interp_ret(exec_ctx_t *ctx)
888 static HRESULT interp_stop(exec_ctx_t *ctx)
892 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
896 static HRESULT interp_me(exec_ctx_t *ctx)
902 IDispatch_AddRef(ctx->this_obj);
903 V_VT(&v) = VT_DISPATCH;
904 V_DISPATCH(&v) = ctx->this_obj;
905 return stack_push(ctx, &v);
908 static HRESULT interp_bool(exec_ctx_t *ctx)
910 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
913 TRACE("%s\n", arg ? "true" : "false");
917 return stack_push(ctx, &v);
920 static HRESULT interp_errmode(exec_ctx_t *ctx)
922 const int err_mode = ctx->instr->arg1.uint;
924 TRACE("%d\n", err_mode);
926 ctx->resume_next = err_mode;
930 static HRESULT interp_string(exec_ctx_t *ctx)
937 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
939 return E_OUTOFMEMORY;
941 return stack_push(ctx, &v);
944 static HRESULT interp_long(exec_ctx_t *ctx)
946 const LONG arg = ctx->instr->arg1.lng;
953 return stack_push(ctx, &v);
956 static HRESULT interp_short(exec_ctx_t *ctx)
958 const LONG arg = ctx->instr->arg1.lng;
965 return stack_push(ctx, &v);
968 static HRESULT interp_double(exec_ctx_t *ctx)
970 const DOUBLE *arg = ctx->instr->arg1.dbl;
973 TRACE("%lf\n", *arg);
977 return stack_push(ctx, &v);
980 static HRESULT interp_empty(exec_ctx_t *ctx)
987 return stack_push(ctx, &v);
990 static HRESULT interp_null(exec_ctx_t *ctx)
997 return stack_push(ctx, &v);
1000 static HRESULT interp_nothing(exec_ctx_t *ctx)
1006 V_VT(&v) = VT_DISPATCH;
1007 V_DISPATCH(&v) = NULL;
1008 return stack_push(ctx, &v);
1011 static HRESULT interp_not(exec_ctx_t *ctx)
1019 hres = stack_pop_val(ctx, &val);
1023 hres = VarNot(val.v, &v);
1028 return stack_push(ctx, &v);
1031 static HRESULT interp_and(exec_ctx_t *ctx)
1039 hres = stack_pop_val(ctx, &r);
1043 hres = stack_pop_val(ctx, &l);
1044 if(SUCCEEDED(hres)) {
1045 hres = VarAnd(l.v, r.v, &v);
1052 return stack_push(ctx, &v);
1055 static HRESULT interp_or(exec_ctx_t *ctx)
1063 hres = stack_pop_val(ctx, &r);
1067 hres = stack_pop_val(ctx, &l);
1068 if(SUCCEEDED(hres)) {
1069 hres = VarOr(l.v, r.v, &v);
1076 return stack_push(ctx, &v);
1079 static HRESULT interp_xor(exec_ctx_t *ctx)
1087 hres = stack_pop_val(ctx, &r);
1091 hres = stack_pop_val(ctx, &l);
1092 if(SUCCEEDED(hres)) {
1093 hres = VarXor(l.v, r.v, &v);
1100 return stack_push(ctx, &v);
1103 static HRESULT interp_eqv(exec_ctx_t *ctx)
1111 hres = stack_pop_val(ctx, &r);
1115 hres = stack_pop_val(ctx, &l);
1116 if(SUCCEEDED(hres)) {
1117 hres = VarEqv(l.v, r.v, &v);
1124 return stack_push(ctx, &v);
1127 static HRESULT interp_imp(exec_ctx_t *ctx)
1135 hres = stack_pop_val(ctx, &r);
1139 hres = stack_pop_val(ctx, &l);
1140 if(SUCCEEDED(hres)) {
1141 hres = VarImp(l.v, r.v, &v);
1148 return stack_push(ctx, &v);
1151 static HRESULT cmp_oper(exec_ctx_t *ctx)
1156 hres = stack_pop_val(ctx, &r);
1160 hres = stack_pop_val(ctx, &l);
1161 if(SUCCEEDED(hres)) {
1162 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1163 FIXME("comparing nulls is not implemented\n");
1166 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1175 static HRESULT interp_equal(exec_ctx_t *ctx)
1182 hres = cmp_oper(ctx);
1187 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1188 return stack_push(ctx, &v);
1191 static HRESULT interp_nequal(exec_ctx_t *ctx)
1198 hres = cmp_oper(ctx);
1203 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1204 return stack_push(ctx, &v);
1207 static HRESULT interp_gt(exec_ctx_t *ctx)
1214 hres = cmp_oper(ctx);
1219 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1220 return stack_push(ctx, &v);
1223 static HRESULT interp_gteq(exec_ctx_t *ctx)
1230 hres = cmp_oper(ctx);
1235 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1236 return stack_push(ctx, &v);
1239 static HRESULT interp_lt(exec_ctx_t *ctx)
1246 hres = cmp_oper(ctx);
1251 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1252 return stack_push(ctx, &v);
1255 static HRESULT interp_lteq(exec_ctx_t *ctx)
1262 hres = cmp_oper(ctx);
1267 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1268 return stack_push(ctx, &v);
1271 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1273 IObjectIdentity *identity;
1274 IUnknown *unk1, *unk2;
1277 if(disp1 == disp2) {
1278 *ret = VARIANT_TRUE;
1282 if(!disp1 || !disp2) {
1283 *ret = VARIANT_FALSE;
1287 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1291 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1293 IUnknown_Release(unk1);
1298 *ret = VARIANT_TRUE;
1300 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1301 if(SUCCEEDED(hres)) {
1302 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1303 IObjectIdentity_Release(identity);
1304 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1306 *ret = VARIANT_FALSE;
1310 IUnknown_Release(unk1);
1311 IUnknown_Release(unk2);
1315 static HRESULT interp_is(exec_ctx_t *ctx)
1323 hres = stack_pop_disp(ctx, &r);
1327 hres = stack_pop_disp(ctx, &l);
1328 if(SUCCEEDED(hres)) {
1330 hres = disp_cmp(l, r, &V_BOOL(&v));
1332 IDispatch_Release(l);
1335 IDispatch_Release(r);
1339 return stack_push(ctx, &v);
1342 static HRESULT interp_concat(exec_ctx_t *ctx)
1350 hres = stack_pop_val(ctx, &r);
1354 hres = stack_pop_val(ctx, &l);
1355 if(SUCCEEDED(hres)) {
1356 hres = VarCat(l.v, r.v, &v);
1363 return stack_push(ctx, &v);
1366 static HRESULT interp_add(exec_ctx_t *ctx)
1374 hres = stack_pop_val(ctx, &r);
1378 hres = stack_pop_val(ctx, &l);
1379 if(SUCCEEDED(hres)) {
1380 hres = VarAdd(l.v, r.v, &v);
1387 return stack_push(ctx, &v);
1390 static HRESULT interp_sub(exec_ctx_t *ctx)
1398 hres = stack_pop_val(ctx, &r);
1402 hres = stack_pop_val(ctx, &l);
1403 if(SUCCEEDED(hres)) {
1404 hres = VarSub(l.v, r.v, &v);
1411 return stack_push(ctx, &v);
1414 static HRESULT interp_mod(exec_ctx_t *ctx)
1422 hres = stack_pop_val(ctx, &r);
1426 hres = stack_pop_val(ctx, &l);
1427 if(SUCCEEDED(hres)) {
1428 hres = VarMod(l.v, r.v, &v);
1435 return stack_push(ctx, &v);
1438 static HRESULT interp_idiv(exec_ctx_t *ctx)
1446 hres = stack_pop_val(ctx, &r);
1450 hres = stack_pop_val(ctx, &l);
1451 if(SUCCEEDED(hres)) {
1452 hres = VarIdiv(l.v, r.v, &v);
1459 return stack_push(ctx, &v);
1462 static HRESULT interp_div(exec_ctx_t *ctx)
1470 hres = stack_pop_val(ctx, &r);
1474 hres = stack_pop_val(ctx, &l);
1475 if(SUCCEEDED(hres)) {
1476 hres = VarDiv(l.v, r.v, &v);
1483 return stack_push(ctx, &v);
1486 static HRESULT interp_mul(exec_ctx_t *ctx)
1494 hres = stack_pop_val(ctx, &r);
1498 hres = stack_pop_val(ctx, &l);
1499 if(SUCCEEDED(hres)) {
1500 hres = VarMul(l.v, r.v, &v);
1507 return stack_push(ctx, &v);
1510 static HRESULT interp_exp(exec_ctx_t *ctx)
1518 hres = stack_pop_val(ctx, &r);
1522 hres = stack_pop_val(ctx, &l);
1523 if(SUCCEEDED(hres)) {
1524 hres = VarPow(l.v, r.v, &v);
1531 return stack_push(ctx, &v);
1534 static HRESULT interp_neg(exec_ctx_t *ctx)
1540 hres = stack_pop_val(ctx, &val);
1544 hres = VarNeg(val.v, &v);
1549 return stack_push(ctx, &v);
1552 static HRESULT interp_incc(exec_ctx_t *ctx)
1554 const BSTR ident = ctx->instr->arg1.bstr;
1561 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1565 if(ref.type != REF_VAR) {
1566 FIXME("ref.type is not REF_VAR\n");
1570 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1574 VariantClear(ref.u.v);
1579 static const instr_func_t op_funcs[] = {
1580 #define X(x,n,a,b) interp_ ## x,
1585 static const unsigned op_move[] = {
1586 #define X(x,n,a,b) n,
1591 void release_dynamic_vars(dynamic_var_t *var)
1594 VariantClear(&var->v);
1599 static void release_exec(exec_ctx_t *ctx)
1603 VariantClear(&ctx->ret_val);
1604 release_dynamic_vars(ctx->dynamic_vars);
1607 IDispatch_Release(ctx->this_obj);
1610 for(i=0; i < ctx->func->arg_cnt; i++)
1611 VariantClear(ctx->args+i);
1615 for(i=0; i < ctx->func->var_cnt; i++)
1616 VariantClear(ctx->vars+i);
1619 vbsheap_free(&ctx->heap);
1620 heap_free(ctx->args);
1621 heap_free(ctx->vars);
1622 heap_free(ctx->stack);
1625 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1627 exec_ctx_t exec = {func->code_ctx};
1629 HRESULT hres = S_OK;
1631 exec.code = func->code_ctx;
1633 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1634 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1638 vbsheap_init(&exec.heap);
1644 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1646 release_exec(&exec);
1647 return E_OUTOFMEMORY;
1650 for(i=0; i < func->arg_cnt; i++) {
1652 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1653 if(func->args[i].by_ref)
1656 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1658 hres = VariantCopy(exec.args+i, v);
1661 release_exec(&exec);
1670 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1672 release_exec(&exec);
1673 return E_OUTOFMEMORY;
1679 exec.stack_size = 16;
1681 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1683 release_exec(&exec);
1684 return E_OUTOFMEMORY;
1688 exec.this_obj = this_obj;
1689 else if (ctx->host_global)
1690 exec.this_obj = ctx->host_global;
1692 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1693 IDispatch_AddRef(exec.this_obj);
1695 exec.instr = exec.code->instrs + func->code_off;
1700 op = exec.instr->op;
1701 hres = op_funcs[op](&exec);
1703 if(exec.resume_next)
1704 FIXME("Failed %08x in resume next mode\n", hres);
1706 WARN("Failed %08x\n", hres);
1707 stack_popn(&exec, exec.top);
1711 exec.instr += op_move[op];
1715 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1716 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1718 if(SUCCEEDED(hres) && res) {
1719 *res = exec.ret_val;
1720 V_VT(&exec.ret_val) = VT_EMPTY;
1723 release_exec(&exec);