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_val(exec_ctx_t *ctx)
691 static HRESULT interp_pop(exec_ctx_t *ctx)
693 const unsigned n = ctx->instr->arg1.uint;
701 static HRESULT interp_new(exec_ctx_t *ctx)
703 const WCHAR *arg = ctx->instr->arg1.bstr;
704 class_desc_t *class_desc;
709 TRACE("%s\n", debugstr_w(arg));
711 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
712 if(!strcmpiW(class_desc->name, arg))
716 FIXME("Class %s not found\n", debugstr_w(arg));
720 hres = create_vbdisp(class_desc, &obj);
724 V_VT(&v) = VT_DISPATCH;
725 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
726 return stack_push(ctx, &v);
729 static HRESULT interp_step(exec_ctx_t *ctx)
731 const BSTR ident = ctx->instr->arg2.bstr;
732 FIXME("%s\n", debugstr_w(ident));
736 static HRESULT interp_jmp(exec_ctx_t *ctx)
738 const unsigned arg = ctx->instr->arg1.uint;
746 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
748 const unsigned arg = ctx->instr->arg1.uint;
754 hres = stack_pop_val(ctx, &val);
758 if(V_VT(val.v) != VT_BOOL) {
759 FIXME("unsupported for %s\n", debugstr_variant(val.v));
767 instr_jmp(ctx, ctx->instr->arg1.uint);
771 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
773 const unsigned arg = ctx->instr->arg1.uint;
779 hres = stack_pop_val(ctx, &val);
783 if(V_VT(val.v) != VT_BOOL) {
784 FIXME("unsupported for %s\n", debugstr_variant(val.v));
790 instr_jmp(ctx, ctx->instr->arg1.uint);
796 static HRESULT interp_ret(exec_ctx_t *ctx)
804 static HRESULT interp_stop(exec_ctx_t *ctx)
808 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
812 static HRESULT interp_me(exec_ctx_t *ctx)
818 IDispatch_AddRef(ctx->this_obj);
819 V_VT(&v) = VT_DISPATCH;
820 V_DISPATCH(&v) = ctx->this_obj;
821 return stack_push(ctx, &v);
824 static HRESULT interp_bool(exec_ctx_t *ctx)
826 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
829 TRACE("%s\n", arg ? "true" : "false");
833 return stack_push(ctx, &v);
836 static HRESULT interp_errmode(exec_ctx_t *ctx)
838 const int err_mode = ctx->instr->arg1.uint;
839 FIXME("%d\n", err_mode);
843 static HRESULT interp_string(exec_ctx_t *ctx)
850 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
852 return E_OUTOFMEMORY;
854 return stack_push(ctx, &v);
857 static HRESULT interp_long(exec_ctx_t *ctx)
859 const LONG arg = ctx->instr->arg1.lng;
866 return stack_push(ctx, &v);
869 static HRESULT interp_short(exec_ctx_t *ctx)
871 const LONG arg = ctx->instr->arg1.lng;
878 return stack_push(ctx, &v);
881 static HRESULT interp_double(exec_ctx_t *ctx)
883 const DOUBLE *arg = ctx->instr->arg1.dbl;
886 TRACE("%lf\n", *arg);
890 return stack_push(ctx, &v);
893 static HRESULT interp_empty(exec_ctx_t *ctx)
900 return stack_push(ctx, &v);
903 static HRESULT interp_null(exec_ctx_t *ctx)
910 return stack_push(ctx, &v);
913 static HRESULT interp_nothing(exec_ctx_t *ctx)
919 V_VT(&v) = VT_DISPATCH;
920 V_DISPATCH(&v) = NULL;
921 return stack_push(ctx, &v);
924 static HRESULT interp_not(exec_ctx_t *ctx)
932 hres = stack_pop_val(ctx, &val);
936 hres = VarNot(val.v, &v);
941 return stack_push(ctx, &v);
944 static HRESULT interp_and(exec_ctx_t *ctx)
952 hres = stack_pop_val(ctx, &r);
956 hres = stack_pop_val(ctx, &l);
957 if(SUCCEEDED(hres)) {
958 hres = VarAnd(l.v, r.v, &v);
965 return stack_push(ctx, &v);
968 static HRESULT interp_or(exec_ctx_t *ctx)
976 hres = stack_pop_val(ctx, &r);
980 hres = stack_pop_val(ctx, &l);
981 if(SUCCEEDED(hres)) {
982 hres = VarOr(l.v, r.v, &v);
989 return stack_push(ctx, &v);
992 static HRESULT interp_xor(exec_ctx_t *ctx)
1000 hres = stack_pop_val(ctx, &r);
1004 hres = stack_pop_val(ctx, &l);
1005 if(SUCCEEDED(hres)) {
1006 hres = VarXor(l.v, r.v, &v);
1013 return stack_push(ctx, &v);
1016 static HRESULT interp_eqv(exec_ctx_t *ctx)
1024 hres = stack_pop_val(ctx, &r);
1028 hres = stack_pop_val(ctx, &l);
1029 if(SUCCEEDED(hres)) {
1030 hres = VarEqv(l.v, r.v, &v);
1037 return stack_push(ctx, &v);
1040 static HRESULT interp_imp(exec_ctx_t *ctx)
1048 hres = stack_pop_val(ctx, &r);
1052 hres = stack_pop_val(ctx, &l);
1053 if(SUCCEEDED(hres)) {
1054 hres = VarImp(l.v, r.v, &v);
1061 return stack_push(ctx, &v);
1064 static HRESULT cmp_oper(exec_ctx_t *ctx)
1069 hres = stack_pop_val(ctx, &r);
1073 hres = stack_pop_val(ctx, &l);
1074 if(SUCCEEDED(hres)) {
1075 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1076 FIXME("comparing nulls is not implemented\n");
1079 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1088 static HRESULT interp_equal(exec_ctx_t *ctx)
1095 hres = cmp_oper(ctx);
1100 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1101 return stack_push(ctx, &v);
1104 static HRESULT interp_nequal(exec_ctx_t *ctx)
1111 hres = cmp_oper(ctx);
1116 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1117 return stack_push(ctx, &v);
1120 static HRESULT interp_gt(exec_ctx_t *ctx)
1127 hres = cmp_oper(ctx);
1132 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1133 return stack_push(ctx, &v);
1136 static HRESULT interp_gteq(exec_ctx_t *ctx)
1143 hres = cmp_oper(ctx);
1148 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1149 return stack_push(ctx, &v);
1152 static HRESULT interp_lt(exec_ctx_t *ctx)
1159 hres = cmp_oper(ctx);
1164 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1165 return stack_push(ctx, &v);
1168 static HRESULT interp_lteq(exec_ctx_t *ctx)
1175 hres = cmp_oper(ctx);
1180 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1181 return stack_push(ctx, &v);
1184 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1186 IObjectIdentity *identity;
1187 IUnknown *unk1, *unk2;
1190 if(disp1 == disp2) {
1191 *ret = VARIANT_TRUE;
1195 if(!disp1 || !disp2) {
1196 *ret = VARIANT_FALSE;
1200 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1204 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1206 IUnknown_Release(unk1);
1211 *ret = VARIANT_TRUE;
1213 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1214 if(SUCCEEDED(hres)) {
1215 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1216 IObjectIdentity_Release(identity);
1217 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1219 *ret = VARIANT_FALSE;
1223 IUnknown_Release(unk1);
1224 IUnknown_Release(unk2);
1228 static HRESULT interp_is(exec_ctx_t *ctx)
1236 hres = stack_pop_disp(ctx, &r);
1240 hres = stack_pop_disp(ctx, &l);
1241 if(SUCCEEDED(hres)) {
1243 hres = disp_cmp(l, r, &V_BOOL(&v));
1245 IDispatch_Release(l);
1248 IDispatch_Release(r);
1252 return stack_push(ctx, &v);
1255 static HRESULT interp_concat(exec_ctx_t *ctx)
1263 hres = stack_pop_val(ctx, &r);
1267 hres = stack_pop_val(ctx, &l);
1268 if(SUCCEEDED(hres)) {
1269 hres = VarCat(l.v, r.v, &v);
1276 return stack_push(ctx, &v);
1279 static HRESULT interp_add(exec_ctx_t *ctx)
1287 hres = stack_pop_val(ctx, &r);
1291 hres = stack_pop_val(ctx, &l);
1292 if(SUCCEEDED(hres)) {
1293 hres = VarAdd(l.v, r.v, &v);
1300 return stack_push(ctx, &v);
1303 static HRESULT interp_sub(exec_ctx_t *ctx)
1311 hres = stack_pop_val(ctx, &r);
1315 hres = stack_pop_val(ctx, &l);
1316 if(SUCCEEDED(hres)) {
1317 hres = VarSub(l.v, r.v, &v);
1324 return stack_push(ctx, &v);
1327 static HRESULT interp_mod(exec_ctx_t *ctx)
1335 hres = stack_pop_val(ctx, &r);
1339 hres = stack_pop_val(ctx, &l);
1340 if(SUCCEEDED(hres)) {
1341 hres = VarMod(l.v, r.v, &v);
1348 return stack_push(ctx, &v);
1351 static HRESULT interp_idiv(exec_ctx_t *ctx)
1359 hres = stack_pop_val(ctx, &r);
1363 hres = stack_pop_val(ctx, &l);
1364 if(SUCCEEDED(hres)) {
1365 hres = VarIdiv(l.v, r.v, &v);
1372 return stack_push(ctx, &v);
1375 static HRESULT interp_div(exec_ctx_t *ctx)
1383 hres = stack_pop_val(ctx, &r);
1387 hres = stack_pop_val(ctx, &l);
1388 if(SUCCEEDED(hres)) {
1389 hres = VarDiv(l.v, r.v, &v);
1396 return stack_push(ctx, &v);
1399 static HRESULT interp_mul(exec_ctx_t *ctx)
1407 hres = stack_pop_val(ctx, &r);
1411 hres = stack_pop_val(ctx, &l);
1412 if(SUCCEEDED(hres)) {
1413 hres = VarMul(l.v, r.v, &v);
1420 return stack_push(ctx, &v);
1423 static HRESULT interp_exp(exec_ctx_t *ctx)
1431 hres = stack_pop_val(ctx, &r);
1435 hres = stack_pop_val(ctx, &l);
1436 if(SUCCEEDED(hres)) {
1437 hres = VarPow(l.v, r.v, &v);
1444 return stack_push(ctx, &v);
1447 static HRESULT interp_neg(exec_ctx_t *ctx)
1453 hres = stack_pop_val(ctx, &val);
1457 hres = VarNeg(val.v, &v);
1462 return stack_push(ctx, &v);
1465 static HRESULT interp_incc(exec_ctx_t *ctx)
1467 const BSTR ident = ctx->instr->arg1.bstr;
1468 FIXME("%s\n", debugstr_w(ident));
1472 static const instr_func_t op_funcs[] = {
1473 #define X(x,n,a,b) interp_ ## x,
1478 static const unsigned op_move[] = {
1479 #define X(x,n,a,b) n,
1484 static void release_exec(exec_ctx_t *ctx)
1488 VariantClear(&ctx->ret_val);
1491 IDispatch_Release(ctx->this_obj);
1494 for(i=0; i < ctx->func->arg_cnt; i++)
1495 VariantClear(ctx->args+i);
1499 for(i=0; i < ctx->func->var_cnt; i++)
1500 VariantClear(ctx->vars+i);
1503 vbsheap_free(&ctx->heap);
1504 heap_free(ctx->args);
1505 heap_free(ctx->vars);
1506 heap_free(ctx->stack);
1509 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1511 exec_ctx_t exec = {func->code_ctx};
1513 HRESULT hres = S_OK;
1515 exec.code = func->code_ctx;
1517 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1518 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1522 vbsheap_init(&exec.heap);
1528 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1530 release_exec(&exec);
1531 return E_OUTOFMEMORY;
1534 for(i=0; i < func->arg_cnt; i++) {
1536 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1537 if(func->args[i].by_ref)
1540 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1542 hres = VariantCopy(exec.args+i, v);
1545 release_exec(&exec);
1554 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1556 release_exec(&exec);
1557 return E_OUTOFMEMORY;
1563 exec.stack_size = 16;
1565 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1567 release_exec(&exec);
1568 return E_OUTOFMEMORY;
1572 exec.this_obj = this_obj;
1573 else if (ctx->host_global)
1574 exec.this_obj = ctx->host_global;
1576 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1577 IDispatch_AddRef(exec.this_obj);
1579 exec.instr = exec.code->instrs + func->code_off;
1584 op = exec.instr->op;
1585 hres = op_funcs[op](&exec);
1587 FIXME("Failed %08x\n", hres);
1588 stack_popn(&exec, exec.top);
1592 exec.instr += op_move[op];
1596 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1597 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1599 if(SUCCEEDED(hres) && res) {
1600 *res = exec.ret_val;
1601 V_VT(&exec.ret_val) = VT_EMPTY;
1604 release_exec(&exec);