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;
47 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
77 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
80 if(!strcmpiW(var->name, name)) {
81 ref->type = var->is_const ? REF_CONST : REF_VAR;
92 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
100 static const WCHAR errW[] = {'e','r','r',0};
102 if(invoke_type == VBDISP_LET
103 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
104 && !strcmpiW(name, ctx->func->name)) {
106 ref->u.v = &ctx->ret_val;
110 for(i=0; i < ctx->func->var_cnt; i++) {
111 if(!strcmpiW(ctx->func->vars[i].name, name)) {
113 ref->u.v = ctx->vars+i;
118 for(i=0; i < ctx->func->arg_cnt; i++) {
119 if(!strcmpiW(ctx->func->args[i].name, name)) {
121 ref->u.v = ctx->args+i;
126 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
129 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
130 if(SUCCEEDED(hres)) {
131 ref->type = REF_DISP;
132 ref->u.d.disp = ctx->this_obj;
137 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
140 for(func = ctx->script->global_funcs; func; func = func->next) {
141 if(!strcmpiW(func->name, name)) {
142 ref->type = REF_FUNC;
148 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
149 if((item->flags & SCRIPTITEM_GLOBALMEMBERS) && item->disp != ctx->this_obj) {
150 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
151 if(SUCCEEDED(hres)) {
152 ref->type = REF_DISP;
153 ref->u.d.disp = item->disp;
159 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
163 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
165 WARN("GetItemInfo failed: %08x\n", hres);
169 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
170 IUnknown_Release(unk);
172 WARN("object does not implement IDispatch\n");
178 ref->u.obj = item->disp;
183 if(!strcmpiW(name, errW)) {
185 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
189 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
190 if(SUCCEEDED(hres)) {
191 ref->type = REF_DISP;
192 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
197 ref->type = REF_NONE;
201 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
203 dynamic_var_t *new_var;
209 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
211 new_var = vbsheap_alloc(heap, sizeof(*new_var));
213 return E_OUTOFMEMORY;
215 size = (strlenW(name)+1)*sizeof(WCHAR);
216 str = vbsheap_alloc(heap, size);
218 return E_OUTOFMEMORY;
219 memcpy(str, name, size);
221 new_var->is_const = is_const;
226 hres = VariantCopy(&new_var->v, val);
231 if(ctx->func->type == FUNC_GLOBAL) {
232 new_var->next = ctx->script->global_vars;
233 ctx->script->global_vars = new_var;
235 new_var->next = ctx->dynamic_vars;
236 ctx->dynamic_vars = new_var;
242 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
245 return ctx->stack + --ctx->top;
248 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
250 if(ctx->stack_size == ctx->top) {
253 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
256 return E_OUTOFMEMORY;
259 ctx->stack = new_stack;
260 ctx->stack_size *= 2;
263 ctx->stack[ctx->top++] = *v;
267 static void stack_popn(exec_ctx_t *ctx, unsigned n)
270 VariantClear(stack_pop(ctx));
273 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
277 var = stack_pop(ctx);
279 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
281 var = V_VARIANTREF(var);
286 if(V_VT(var) == VT_DISPATCH) {
290 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
292 IDispatch_Release(V_DISPATCH(var));
305 static inline void release_val(variant_val_t *v)
311 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
313 VARIANT *v = stack_pop(ctx);
315 if(V_VT(v) == VT_DISPATCH) {
316 *ret = V_DISPATCH(v);
320 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
321 FIXME("not supported type: %s\n", debugstr_variant(v));
327 if(V_VT(v) != VT_DISPATCH) {
328 FIXME("not disp %s\n", debugstr_variant(v));
333 IDispatch_AddRef(V_DISPATCH(v));
334 *ret = V_DISPATCH(v);
338 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
340 ctx->instr = ctx->code->instrs + addr;
343 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
346 dp->rgdispidNamedArgs = NULL;
353 assert(ctx->top >= arg_cnt);
355 for(i=1; i*2 <= arg_cnt; i++) {
356 tmp = ctx->stack[ctx->top-i];
357 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
358 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
361 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
367 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
369 BSTR identifier = ctx->instr->arg1.bstr;
370 const unsigned arg_cnt = ctx->instr->arg2.uint;
375 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
379 vbstack_to_dp(ctx, arg_cnt, &dp);
385 FIXME("REF_VAR no res\n");
390 FIXME("arguments not implemented\n");
394 V_VT(res) = VT_BYREF|VT_VARIANT;
395 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
398 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
403 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
409 FIXME("arguments on object\n");
414 IDispatch_AddRef(ref.u.obj);
415 V_VT(res) = VT_DISPATCH;
416 V_DISPATCH(res) = ref.u.obj;
420 FIXME("%s not found\n", debugstr_w(identifier));
421 return DISP_E_UNKNOWNNAME;
424 stack_popn(ctx, arg_cnt);
428 static HRESULT interp_icall(exec_ctx_t *ctx)
435 hres = do_icall(ctx, &v);
439 return stack_push(ctx, &v);
442 static HRESULT interp_icallv(exec_ctx_t *ctx)
445 return do_icall(ctx, NULL);
448 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
450 const BSTR identifier = ctx->instr->arg1.bstr;
451 const unsigned arg_cnt = ctx->instr->arg2.uint;
457 hres = stack_pop_disp(ctx, &obj);
466 vbstack_to_dp(ctx, arg_cnt, &dp);
468 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
470 hres = disp_call(ctx->script, obj, id, &dp, res);
471 IDispatch_Release(obj);
475 stack_popn(ctx, arg_cnt);
479 static HRESULT interp_mcall(exec_ctx_t *ctx)
486 hres = do_mcall(ctx, &res);
490 return stack_push(ctx, &res);
493 static HRESULT interp_mcallv(exec_ctx_t *ctx)
497 return do_mcall(ctx, NULL);
500 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
505 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
511 VARIANT *v = ref.u.v;
513 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
521 hres = VariantCopy(v, val);
526 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
531 FIXME("functions not implemented\n");
537 FIXME("REF_CONST\n");
540 if(ctx->func->code_ctx->option_explicit) {
541 FIXME("throw exception\n");
544 TRACE("creating variable %s\n", debugstr_w(name));
545 hres = add_dynamic_var(ctx, name, FALSE, val, own_val);
552 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
554 const BSTR arg = ctx->instr->arg1.bstr;
558 TRACE("%s\n", debugstr_w(arg));
560 hres = stack_pop_val(ctx, &v);
564 return assign_ident(ctx, arg, v.v, v.owned);
567 static HRESULT interp_set_ident(exec_ctx_t *ctx)
569 const BSTR arg = ctx->instr->arg1.bstr;
574 TRACE("%s\n", debugstr_w(arg));
576 hres = stack_pop_disp(ctx, &disp);
580 V_VT(&v) = VT_DISPATCH;
581 V_DISPATCH(&v) = disp;
582 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
585 static HRESULT interp_assign_member(exec_ctx_t *ctx)
587 BSTR identifier = ctx->instr->arg1.bstr;
593 TRACE("%s\n", debugstr_w(identifier));
595 hres = stack_pop_disp(ctx, &obj);
604 hres = stack_pop_val(ctx, &val);
606 IDispatch_Release(obj);
610 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
612 hres = disp_propput(ctx->script, obj, id, val.v);
615 IDispatch_Release(obj);
619 static HRESULT interp_set_member(exec_ctx_t *ctx)
621 BSTR identifier = ctx->instr->arg1.bstr;
622 IDispatch *obj, *val;
626 TRACE("%s\n", debugstr_w(identifier));
628 hres = stack_pop_disp(ctx, &obj);
637 hres = stack_pop_disp(ctx, &val);
639 IDispatch_Release(obj);
643 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
644 if(SUCCEEDED(hres)) {
647 V_VT(&v) = VT_DISPATCH;
648 V_DISPATCH(&v) = val;
649 hres = disp_propput(ctx->script, obj, id, &v);
653 IDispatch_Release(val);
654 IDispatch_Release(obj);
658 static HRESULT interp_const(exec_ctx_t *ctx)
660 BSTR arg = ctx->instr->arg1.bstr;
665 TRACE("%s\n", debugstr_w(arg));
667 assert(ctx->func->type == FUNC_GLOBAL);
669 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
673 if(ref.type != REF_NONE) {
674 FIXME("%s already defined\n", debugstr_w(arg));
678 hres = stack_pop_val(ctx, &val);
682 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
685 static HRESULT interp_new(exec_ctx_t *ctx)
687 const WCHAR *arg = ctx->instr->arg1.bstr;
688 class_desc_t *class_desc;
693 TRACE("%s\n", debugstr_w(arg));
695 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
696 if(!strcmpiW(class_desc->name, arg))
700 FIXME("Class %s not found\n", debugstr_w(arg));
704 hres = create_vbdisp(class_desc, &obj);
708 V_VT(&v) = VT_DISPATCH;
709 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
710 return stack_push(ctx, &v);
713 static HRESULT interp_jmp(exec_ctx_t *ctx)
715 const unsigned arg = ctx->instr->arg1.uint;
723 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
725 const unsigned arg = ctx->instr->arg1.uint;
731 hres = stack_pop_val(ctx, &val);
735 if(V_VT(val.v) != VT_BOOL) {
736 FIXME("unsupported for %s\n", debugstr_variant(val.v));
744 instr_jmp(ctx, ctx->instr->arg1.uint);
748 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
750 const unsigned arg = ctx->instr->arg1.uint;
756 hres = stack_pop_val(ctx, &val);
760 if(V_VT(val.v) != VT_BOOL) {
761 FIXME("unsupported for %s\n", debugstr_variant(val.v));
767 instr_jmp(ctx, ctx->instr->arg1.uint);
773 static HRESULT interp_ret(exec_ctx_t *ctx)
781 static HRESULT interp_stop(exec_ctx_t *ctx)
785 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
789 static HRESULT interp_me(exec_ctx_t *ctx)
795 IDispatch_AddRef(ctx->this_obj);
796 V_VT(&v) = VT_DISPATCH;
797 V_DISPATCH(&v) = ctx->this_obj;
798 return stack_push(ctx, &v);
801 static HRESULT interp_bool(exec_ctx_t *ctx)
803 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
806 TRACE("%s\n", arg ? "true" : "false");
810 return stack_push(ctx, &v);
813 static HRESULT interp_errmode(exec_ctx_t *ctx)
815 const int err_mode = ctx->instr->arg1.uint;
816 FIXME("%d\n", err_mode);
820 static HRESULT interp_string(exec_ctx_t *ctx)
827 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
829 return E_OUTOFMEMORY;
831 return stack_push(ctx, &v);
834 static HRESULT interp_long(exec_ctx_t *ctx)
836 const LONG arg = ctx->instr->arg1.lng;
843 return stack_push(ctx, &v);
846 static HRESULT interp_short(exec_ctx_t *ctx)
848 const LONG arg = ctx->instr->arg1.lng;
855 return stack_push(ctx, &v);
858 static HRESULT interp_double(exec_ctx_t *ctx)
860 const DOUBLE *arg = ctx->instr->arg1.dbl;
863 TRACE("%lf\n", *arg);
867 return stack_push(ctx, &v);
870 static HRESULT interp_empty(exec_ctx_t *ctx)
877 return stack_push(ctx, &v);
880 static HRESULT interp_null(exec_ctx_t *ctx)
887 return stack_push(ctx, &v);
890 static HRESULT interp_nothing(exec_ctx_t *ctx)
896 V_VT(&v) = VT_DISPATCH;
897 V_DISPATCH(&v) = NULL;
898 return stack_push(ctx, &v);
901 static HRESULT interp_not(exec_ctx_t *ctx)
909 hres = stack_pop_val(ctx, &val);
913 hres = VarNot(val.v, &v);
918 return stack_push(ctx, &v);
921 static HRESULT interp_and(exec_ctx_t *ctx)
929 hres = stack_pop_val(ctx, &r);
933 hres = stack_pop_val(ctx, &l);
934 if(SUCCEEDED(hres)) {
935 hres = VarAnd(l.v, r.v, &v);
942 return stack_push(ctx, &v);
945 static HRESULT interp_or(exec_ctx_t *ctx)
953 hres = stack_pop_val(ctx, &r);
957 hres = stack_pop_val(ctx, &l);
958 if(SUCCEEDED(hres)) {
959 hres = VarOr(l.v, r.v, &v);
966 return stack_push(ctx, &v);
969 static HRESULT interp_xor(exec_ctx_t *ctx)
977 hres = stack_pop_val(ctx, &r);
981 hres = stack_pop_val(ctx, &l);
982 if(SUCCEEDED(hres)) {
983 hres = VarXor(l.v, r.v, &v);
990 return stack_push(ctx, &v);
993 static HRESULT interp_eqv(exec_ctx_t *ctx)
1001 hres = stack_pop_val(ctx, &r);
1005 hres = stack_pop_val(ctx, &l);
1006 if(SUCCEEDED(hres)) {
1007 hres = VarEqv(l.v, r.v, &v);
1014 return stack_push(ctx, &v);
1017 static HRESULT interp_imp(exec_ctx_t *ctx)
1025 hres = stack_pop_val(ctx, &r);
1029 hres = stack_pop_val(ctx, &l);
1030 if(SUCCEEDED(hres)) {
1031 hres = VarImp(l.v, r.v, &v);
1038 return stack_push(ctx, &v);
1041 static HRESULT cmp_oper(exec_ctx_t *ctx)
1046 hres = stack_pop_val(ctx, &r);
1050 hres = stack_pop_val(ctx, &l);
1051 if(SUCCEEDED(hres)) {
1052 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1053 FIXME("comparing nulls is not implemented\n");
1056 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1065 static HRESULT interp_equal(exec_ctx_t *ctx)
1072 hres = cmp_oper(ctx);
1077 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1078 return stack_push(ctx, &v);
1081 static HRESULT interp_nequal(exec_ctx_t *ctx)
1088 hres = cmp_oper(ctx);
1093 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1094 return stack_push(ctx, &v);
1097 static HRESULT interp_gt(exec_ctx_t *ctx)
1104 hres = cmp_oper(ctx);
1109 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1110 return stack_push(ctx, &v);
1113 static HRESULT interp_gteq(exec_ctx_t *ctx)
1120 hres = cmp_oper(ctx);
1125 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1126 return stack_push(ctx, &v);
1129 static HRESULT interp_lt(exec_ctx_t *ctx)
1136 hres = cmp_oper(ctx);
1141 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1142 return stack_push(ctx, &v);
1145 static HRESULT interp_lteq(exec_ctx_t *ctx)
1152 hres = cmp_oper(ctx);
1157 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1158 return stack_push(ctx, &v);
1161 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1163 IObjectIdentity *identity;
1164 IUnknown *unk1, *unk2;
1167 if(disp1 == disp2) {
1168 *ret = VARIANT_TRUE;
1172 if(!disp1 || !disp2) {
1173 *ret = VARIANT_FALSE;
1177 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1181 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1183 IUnknown_Release(unk1);
1188 *ret = VARIANT_TRUE;
1190 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1191 if(SUCCEEDED(hres)) {
1192 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1193 IObjectIdentity_Release(identity);
1194 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1196 *ret = VARIANT_FALSE;
1200 IUnknown_Release(unk1);
1201 IUnknown_Release(unk2);
1205 static HRESULT interp_is(exec_ctx_t *ctx)
1213 hres = stack_pop_disp(ctx, &r);
1217 hres = stack_pop_disp(ctx, &l);
1218 if(SUCCEEDED(hres)) {
1220 hres = disp_cmp(l, r, &V_BOOL(&v));
1222 IDispatch_Release(l);
1225 IDispatch_Release(r);
1229 return stack_push(ctx, &v);
1232 static HRESULT interp_concat(exec_ctx_t *ctx)
1240 hres = stack_pop_val(ctx, &r);
1244 hres = stack_pop_val(ctx, &l);
1245 if(SUCCEEDED(hres)) {
1246 hres = VarCat(l.v, r.v, &v);
1253 return stack_push(ctx, &v);
1256 static HRESULT interp_add(exec_ctx_t *ctx)
1264 hres = stack_pop_val(ctx, &r);
1268 hres = stack_pop_val(ctx, &l);
1269 if(SUCCEEDED(hres)) {
1270 hres = VarAdd(l.v, r.v, &v);
1277 return stack_push(ctx, &v);
1280 static HRESULT interp_sub(exec_ctx_t *ctx)
1288 hres = stack_pop_val(ctx, &r);
1292 hres = stack_pop_val(ctx, &l);
1293 if(SUCCEEDED(hres)) {
1294 hres = VarSub(l.v, r.v, &v);
1301 return stack_push(ctx, &v);
1304 static HRESULT interp_mod(exec_ctx_t *ctx)
1312 hres = stack_pop_val(ctx, &r);
1316 hres = stack_pop_val(ctx, &l);
1317 if(SUCCEEDED(hres)) {
1318 hres = VarMod(l.v, r.v, &v);
1325 return stack_push(ctx, &v);
1328 static HRESULT interp_idiv(exec_ctx_t *ctx)
1336 hres = stack_pop_val(ctx, &r);
1340 hres = stack_pop_val(ctx, &l);
1341 if(SUCCEEDED(hres)) {
1342 hres = VarIdiv(l.v, r.v, &v);
1349 return stack_push(ctx, &v);
1352 static HRESULT interp_div(exec_ctx_t *ctx)
1360 hres = stack_pop_val(ctx, &r);
1364 hres = stack_pop_val(ctx, &l);
1365 if(SUCCEEDED(hres)) {
1366 hres = VarDiv(l.v, r.v, &v);
1373 return stack_push(ctx, &v);
1376 static HRESULT interp_mul(exec_ctx_t *ctx)
1384 hres = stack_pop_val(ctx, &r);
1388 hres = stack_pop_val(ctx, &l);
1389 if(SUCCEEDED(hres)) {
1390 hres = VarMul(l.v, r.v, &v);
1397 return stack_push(ctx, &v);
1400 static HRESULT interp_exp(exec_ctx_t *ctx)
1408 hres = stack_pop_val(ctx, &r);
1412 hres = stack_pop_val(ctx, &l);
1413 if(SUCCEEDED(hres)) {
1414 hres = VarPow(l.v, r.v, &v);
1421 return stack_push(ctx, &v);
1424 static HRESULT interp_neg(exec_ctx_t *ctx)
1430 hres = stack_pop_val(ctx, &val);
1434 hres = VarNeg(val.v, &v);
1439 return stack_push(ctx, &v);
1442 static const instr_func_t op_funcs[] = {
1443 #define X(x,n,a,b) interp_ ## x,
1448 static const unsigned op_move[] = {
1449 #define X(x,n,a,b) n,
1454 static void release_exec(exec_ctx_t *ctx)
1458 VariantClear(&ctx->ret_val);
1461 IDispatch_Release(ctx->this_obj);
1464 for(i=0; i < ctx->func->arg_cnt; i++)
1465 VariantClear(ctx->args+i);
1469 for(i=0; i < ctx->func->var_cnt; i++)
1470 VariantClear(ctx->vars+i);
1473 vbsheap_free(&ctx->heap);
1474 heap_free(ctx->args);
1475 heap_free(ctx->vars);
1476 heap_free(ctx->stack);
1479 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1481 exec_ctx_t exec = {func->code_ctx};
1483 HRESULT hres = S_OK;
1485 exec.code = func->code_ctx;
1487 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1488 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1492 vbsheap_init(&exec.heap);
1498 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1500 release_exec(&exec);
1501 return E_OUTOFMEMORY;
1504 for(i=0; i < func->arg_cnt; i++) {
1506 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1507 if(func->args[i].by_ref)
1510 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1512 hres = VariantCopy(exec.args+i, v);
1515 release_exec(&exec);
1524 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1526 release_exec(&exec);
1527 return E_OUTOFMEMORY;
1533 exec.stack_size = 16;
1535 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1537 release_exec(&exec);
1538 return E_OUTOFMEMORY;
1542 exec.this_obj = this_obj;
1543 else if (ctx->host_global)
1544 exec.this_obj = ctx->host_global;
1546 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1547 IDispatch_AddRef(exec.this_obj);
1549 exec.instr = exec.code->instrs + func->code_off;
1554 op = exec.instr->op;
1555 hres = op_funcs[op](&exec);
1557 FIXME("Failed %08x\n", hres);
1558 stack_popn(&exec, exec.top);
1562 exec.instr += op_move[op];
1566 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1567 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1569 if(SUCCEEDED(hres) && res) {
1570 *res = exec.ret_val;
1571 V_VT(&exec.ret_val) = VT_EMPTY;
1574 release_exec(&exec);