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 if(ctx->func->type != FUNC_GLOBAL) {
130 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
131 if(SUCCEEDED(hres)) {
132 ref->type = REF_DISP;
133 ref->u.d.disp = ctx->this_obj;
139 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
142 for(func = ctx->script->global_funcs; func; func = func->next) {
143 if(!strcmpiW(func->name, name)) {
144 ref->type = REF_FUNC;
150 if(!strcmpiW(name, errW)) {
152 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
156 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
157 if(SUCCEEDED(hres)) {
158 ref->type = REF_DISP;
159 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
164 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
165 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
166 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
167 if(SUCCEEDED(hres)) {
168 ref->type = REF_DISP;
169 ref->u.d.disp = item->disp;
175 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
179 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
181 WARN("GetItemInfo failed: %08x\n", hres);
185 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
186 IUnknown_Release(unk);
188 WARN("object does not implement IDispatch\n");
194 ref->u.obj = item->disp;
199 ref->type = REF_NONE;
203 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
205 dynamic_var_t *new_var;
211 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
213 new_var = vbsheap_alloc(heap, sizeof(*new_var));
215 return E_OUTOFMEMORY;
217 size = (strlenW(name)+1)*sizeof(WCHAR);
218 str = vbsheap_alloc(heap, size);
220 return E_OUTOFMEMORY;
221 memcpy(str, name, size);
223 new_var->is_const = is_const;
228 hres = VariantCopy(&new_var->v, val);
233 if(ctx->func->type == FUNC_GLOBAL) {
234 new_var->next = ctx->script->global_vars;
235 ctx->script->global_vars = new_var;
237 new_var->next = ctx->dynamic_vars;
238 ctx->dynamic_vars = new_var;
244 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
247 return ctx->stack + --ctx->top;
250 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
252 assert(ctx->top >= n);
253 return ctx->stack + (ctx->top-n-1);
256 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
258 if(ctx->stack_size == ctx->top) {
261 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
264 return E_OUTOFMEMORY;
267 ctx->stack = new_stack;
268 ctx->stack_size *= 2;
271 ctx->stack[ctx->top++] = *v;
275 static void stack_popn(exec_ctx_t *ctx, unsigned n)
278 VariantClear(stack_pop(ctx));
281 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
285 var = stack_pop(ctx);
287 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
289 var = V_VARIANTREF(var);
294 if(V_VT(var) == VT_DISPATCH) {
298 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
300 IDispatch_Release(V_DISPATCH(var));
313 static inline void release_val(variant_val_t *v)
319 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
321 VARIANT *v = stack_pop(ctx);
323 if(V_VT(v) == VT_DISPATCH) {
324 *ret = V_DISPATCH(v);
328 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
329 FIXME("not supported type: %s\n", debugstr_variant(v));
335 if(V_VT(v) != VT_DISPATCH) {
336 FIXME("not disp %s\n", debugstr_variant(v));
341 IDispatch_AddRef(V_DISPATCH(v));
342 *ret = V_DISPATCH(v);
346 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
348 ctx->instr = ctx->code->instrs + addr;
351 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
354 dp->rgdispidNamedArgs = NULL;
361 assert(ctx->top >= arg_cnt);
363 for(i=1; i*2 <= arg_cnt; i++) {
364 tmp = ctx->stack[ctx->top-i];
365 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
366 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
369 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
375 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
377 BSTR identifier = ctx->instr->arg1.bstr;
378 const unsigned arg_cnt = ctx->instr->arg2.uint;
383 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
387 vbstack_to_dp(ctx, arg_cnt, &dp);
393 FIXME("REF_VAR no res\n");
398 FIXME("arguments not implemented\n");
402 V_VT(res) = VT_BYREF|VT_VARIANT;
403 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
406 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
411 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
417 FIXME("arguments on object\n");
422 IDispatch_AddRef(ref.u.obj);
423 V_VT(res) = VT_DISPATCH;
424 V_DISPATCH(res) = ref.u.obj;
428 FIXME("%s not found\n", debugstr_w(identifier));
429 return DISP_E_UNKNOWNNAME;
432 stack_popn(ctx, arg_cnt);
436 static HRESULT interp_icall(exec_ctx_t *ctx)
443 hres = do_icall(ctx, &v);
447 return stack_push(ctx, &v);
450 static HRESULT interp_icallv(exec_ctx_t *ctx)
453 return do_icall(ctx, NULL);
456 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
458 const BSTR identifier = ctx->instr->arg1.bstr;
459 const unsigned arg_cnt = ctx->instr->arg2.uint;
465 hres = stack_pop_disp(ctx, &obj);
474 vbstack_to_dp(ctx, arg_cnt, &dp);
476 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
478 hres = disp_call(ctx->script, obj, id, &dp, res);
479 IDispatch_Release(obj);
483 stack_popn(ctx, arg_cnt);
487 static HRESULT interp_mcall(exec_ctx_t *ctx)
494 hres = do_mcall(ctx, &res);
498 return stack_push(ctx, &res);
501 static HRESULT interp_mcallv(exec_ctx_t *ctx)
505 return do_mcall(ctx, NULL);
508 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
513 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
519 VARIANT *v = ref.u.v;
521 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
529 hres = VariantCopy(v, val);
534 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
539 FIXME("functions not implemented\n");
545 FIXME("REF_CONST\n");
548 if(ctx->func->code_ctx->option_explicit) {
549 FIXME("throw exception\n");
552 TRACE("creating variable %s\n", debugstr_w(name));
553 hres = add_dynamic_var(ctx, name, FALSE, val, own_val);
560 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
562 const BSTR arg = ctx->instr->arg1.bstr;
566 TRACE("%s\n", debugstr_w(arg));
568 hres = stack_pop_val(ctx, &v);
572 return assign_ident(ctx, arg, v.v, v.owned);
575 static HRESULT interp_set_ident(exec_ctx_t *ctx)
577 const BSTR arg = ctx->instr->arg1.bstr;
582 TRACE("%s\n", debugstr_w(arg));
584 hres = stack_pop_disp(ctx, &disp);
588 V_VT(&v) = VT_DISPATCH;
589 V_DISPATCH(&v) = disp;
590 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
593 static HRESULT interp_assign_member(exec_ctx_t *ctx)
595 BSTR identifier = ctx->instr->arg1.bstr;
601 TRACE("%s\n", debugstr_w(identifier));
603 hres = stack_pop_disp(ctx, &obj);
612 hres = stack_pop_val(ctx, &val);
614 IDispatch_Release(obj);
618 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
620 hres = disp_propput(ctx->script, obj, id, val.v);
623 IDispatch_Release(obj);
627 static HRESULT interp_set_member(exec_ctx_t *ctx)
629 BSTR identifier = ctx->instr->arg1.bstr;
630 IDispatch *obj, *val;
634 TRACE("%s\n", debugstr_w(identifier));
636 hres = stack_pop_disp(ctx, &obj);
645 hres = stack_pop_disp(ctx, &val);
647 IDispatch_Release(obj);
651 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
652 if(SUCCEEDED(hres)) {
655 V_VT(&v) = VT_DISPATCH;
656 V_DISPATCH(&v) = val;
657 hres = disp_propput(ctx->script, obj, id, &v);
661 IDispatch_Release(val);
662 IDispatch_Release(obj);
666 static HRESULT interp_const(exec_ctx_t *ctx)
668 BSTR arg = ctx->instr->arg1.bstr;
673 TRACE("%s\n", debugstr_w(arg));
675 assert(ctx->func->type == FUNC_GLOBAL);
677 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
681 if(ref.type != REF_NONE) {
682 FIXME("%s already defined\n", debugstr_w(arg));
686 hres = stack_pop_val(ctx, &val);
690 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
693 static HRESULT interp_val(exec_ctx_t *ctx)
701 hres = stack_pop_val(ctx, &val);
707 hres = VariantCopy(&v, val.v);
712 return stack_push(ctx, val.owned ? val.v : &v);
715 static HRESULT interp_pop(exec_ctx_t *ctx)
717 const unsigned n = ctx->instr->arg1.uint;
725 static HRESULT interp_new(exec_ctx_t *ctx)
727 const WCHAR *arg = ctx->instr->arg1.bstr;
728 class_desc_t *class_desc;
733 TRACE("%s\n", debugstr_w(arg));
735 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
736 if(!strcmpiW(class_desc->name, arg))
740 FIXME("Class %s not found\n", debugstr_w(arg));
744 hres = create_vbdisp(class_desc, &obj);
748 V_VT(&v) = VT_DISPATCH;
749 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
750 return stack_push(ctx, &v);
753 static HRESULT interp_step(exec_ctx_t *ctx)
755 const BSTR ident = ctx->instr->arg2.bstr;
761 TRACE("%s\n", debugstr_w(ident));
765 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
769 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
771 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
775 if(ref.type != REF_VAR) {
776 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
780 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
784 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
787 instr_jmp(ctx, ctx->instr->arg1.uint);
791 static HRESULT interp_jmp(exec_ctx_t *ctx)
793 const unsigned arg = ctx->instr->arg1.uint;
801 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
803 const unsigned arg = ctx->instr->arg1.uint;
809 hres = stack_pop_val(ctx, &val);
813 if(V_VT(val.v) != VT_BOOL) {
814 FIXME("unsupported for %s\n", debugstr_variant(val.v));
822 instr_jmp(ctx, ctx->instr->arg1.uint);
826 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
828 const unsigned arg = ctx->instr->arg1.uint;
834 hres = stack_pop_val(ctx, &val);
838 if(V_VT(val.v) != VT_BOOL) {
839 FIXME("unsupported for %s\n", debugstr_variant(val.v));
845 instr_jmp(ctx, ctx->instr->arg1.uint);
851 static HRESULT interp_ret(exec_ctx_t *ctx)
859 static HRESULT interp_stop(exec_ctx_t *ctx)
863 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
867 static HRESULT interp_me(exec_ctx_t *ctx)
873 IDispatch_AddRef(ctx->this_obj);
874 V_VT(&v) = VT_DISPATCH;
875 V_DISPATCH(&v) = ctx->this_obj;
876 return stack_push(ctx, &v);
879 static HRESULT interp_bool(exec_ctx_t *ctx)
881 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
884 TRACE("%s\n", arg ? "true" : "false");
888 return stack_push(ctx, &v);
891 static HRESULT interp_errmode(exec_ctx_t *ctx)
893 const int err_mode = ctx->instr->arg1.uint;
894 FIXME("%d\n", err_mode);
898 static HRESULT interp_string(exec_ctx_t *ctx)
905 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
907 return E_OUTOFMEMORY;
909 return stack_push(ctx, &v);
912 static HRESULT interp_long(exec_ctx_t *ctx)
914 const LONG arg = ctx->instr->arg1.lng;
921 return stack_push(ctx, &v);
924 static HRESULT interp_short(exec_ctx_t *ctx)
926 const LONG arg = ctx->instr->arg1.lng;
933 return stack_push(ctx, &v);
936 static HRESULT interp_double(exec_ctx_t *ctx)
938 const DOUBLE *arg = ctx->instr->arg1.dbl;
941 TRACE("%lf\n", *arg);
945 return stack_push(ctx, &v);
948 static HRESULT interp_empty(exec_ctx_t *ctx)
955 return stack_push(ctx, &v);
958 static HRESULT interp_null(exec_ctx_t *ctx)
965 return stack_push(ctx, &v);
968 static HRESULT interp_nothing(exec_ctx_t *ctx)
974 V_VT(&v) = VT_DISPATCH;
975 V_DISPATCH(&v) = NULL;
976 return stack_push(ctx, &v);
979 static HRESULT interp_not(exec_ctx_t *ctx)
987 hres = stack_pop_val(ctx, &val);
991 hres = VarNot(val.v, &v);
996 return stack_push(ctx, &v);
999 static HRESULT interp_and(exec_ctx_t *ctx)
1007 hres = stack_pop_val(ctx, &r);
1011 hres = stack_pop_val(ctx, &l);
1012 if(SUCCEEDED(hres)) {
1013 hres = VarAnd(l.v, r.v, &v);
1020 return stack_push(ctx, &v);
1023 static HRESULT interp_or(exec_ctx_t *ctx)
1031 hres = stack_pop_val(ctx, &r);
1035 hres = stack_pop_val(ctx, &l);
1036 if(SUCCEEDED(hres)) {
1037 hres = VarOr(l.v, r.v, &v);
1044 return stack_push(ctx, &v);
1047 static HRESULT interp_xor(exec_ctx_t *ctx)
1055 hres = stack_pop_val(ctx, &r);
1059 hres = stack_pop_val(ctx, &l);
1060 if(SUCCEEDED(hres)) {
1061 hres = VarXor(l.v, r.v, &v);
1068 return stack_push(ctx, &v);
1071 static HRESULT interp_eqv(exec_ctx_t *ctx)
1079 hres = stack_pop_val(ctx, &r);
1083 hres = stack_pop_val(ctx, &l);
1084 if(SUCCEEDED(hres)) {
1085 hres = VarEqv(l.v, r.v, &v);
1092 return stack_push(ctx, &v);
1095 static HRESULT interp_imp(exec_ctx_t *ctx)
1103 hres = stack_pop_val(ctx, &r);
1107 hres = stack_pop_val(ctx, &l);
1108 if(SUCCEEDED(hres)) {
1109 hres = VarImp(l.v, r.v, &v);
1116 return stack_push(ctx, &v);
1119 static HRESULT cmp_oper(exec_ctx_t *ctx)
1124 hres = stack_pop_val(ctx, &r);
1128 hres = stack_pop_val(ctx, &l);
1129 if(SUCCEEDED(hres)) {
1130 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1131 FIXME("comparing nulls is not implemented\n");
1134 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1143 static HRESULT interp_equal(exec_ctx_t *ctx)
1150 hres = cmp_oper(ctx);
1155 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1156 return stack_push(ctx, &v);
1159 static HRESULT interp_nequal(exec_ctx_t *ctx)
1166 hres = cmp_oper(ctx);
1171 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1172 return stack_push(ctx, &v);
1175 static HRESULT interp_gt(exec_ctx_t *ctx)
1182 hres = cmp_oper(ctx);
1187 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1188 return stack_push(ctx, &v);
1191 static HRESULT interp_gteq(exec_ctx_t *ctx)
1198 hres = cmp_oper(ctx);
1203 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1204 return stack_push(ctx, &v);
1207 static HRESULT interp_lt(exec_ctx_t *ctx)
1214 hres = cmp_oper(ctx);
1219 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1220 return stack_push(ctx, &v);
1223 static HRESULT interp_lteq(exec_ctx_t *ctx)
1230 hres = cmp_oper(ctx);
1235 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1236 return stack_push(ctx, &v);
1239 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1241 IObjectIdentity *identity;
1242 IUnknown *unk1, *unk2;
1245 if(disp1 == disp2) {
1246 *ret = VARIANT_TRUE;
1250 if(!disp1 || !disp2) {
1251 *ret = VARIANT_FALSE;
1255 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1259 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1261 IUnknown_Release(unk1);
1266 *ret = VARIANT_TRUE;
1268 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1269 if(SUCCEEDED(hres)) {
1270 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1271 IObjectIdentity_Release(identity);
1272 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1274 *ret = VARIANT_FALSE;
1278 IUnknown_Release(unk1);
1279 IUnknown_Release(unk2);
1283 static HRESULT interp_is(exec_ctx_t *ctx)
1291 hres = stack_pop_disp(ctx, &r);
1295 hres = stack_pop_disp(ctx, &l);
1296 if(SUCCEEDED(hres)) {
1298 hres = disp_cmp(l, r, &V_BOOL(&v));
1300 IDispatch_Release(l);
1303 IDispatch_Release(r);
1307 return stack_push(ctx, &v);
1310 static HRESULT interp_concat(exec_ctx_t *ctx)
1318 hres = stack_pop_val(ctx, &r);
1322 hres = stack_pop_val(ctx, &l);
1323 if(SUCCEEDED(hres)) {
1324 hres = VarCat(l.v, r.v, &v);
1331 return stack_push(ctx, &v);
1334 static HRESULT interp_add(exec_ctx_t *ctx)
1342 hres = stack_pop_val(ctx, &r);
1346 hres = stack_pop_val(ctx, &l);
1347 if(SUCCEEDED(hres)) {
1348 hres = VarAdd(l.v, r.v, &v);
1355 return stack_push(ctx, &v);
1358 static HRESULT interp_sub(exec_ctx_t *ctx)
1366 hres = stack_pop_val(ctx, &r);
1370 hres = stack_pop_val(ctx, &l);
1371 if(SUCCEEDED(hres)) {
1372 hres = VarSub(l.v, r.v, &v);
1379 return stack_push(ctx, &v);
1382 static HRESULT interp_mod(exec_ctx_t *ctx)
1390 hres = stack_pop_val(ctx, &r);
1394 hres = stack_pop_val(ctx, &l);
1395 if(SUCCEEDED(hres)) {
1396 hres = VarMod(l.v, r.v, &v);
1403 return stack_push(ctx, &v);
1406 static HRESULT interp_idiv(exec_ctx_t *ctx)
1414 hres = stack_pop_val(ctx, &r);
1418 hres = stack_pop_val(ctx, &l);
1419 if(SUCCEEDED(hres)) {
1420 hres = VarIdiv(l.v, r.v, &v);
1427 return stack_push(ctx, &v);
1430 static HRESULT interp_div(exec_ctx_t *ctx)
1438 hres = stack_pop_val(ctx, &r);
1442 hres = stack_pop_val(ctx, &l);
1443 if(SUCCEEDED(hres)) {
1444 hres = VarDiv(l.v, r.v, &v);
1451 return stack_push(ctx, &v);
1454 static HRESULT interp_mul(exec_ctx_t *ctx)
1462 hres = stack_pop_val(ctx, &r);
1466 hres = stack_pop_val(ctx, &l);
1467 if(SUCCEEDED(hres)) {
1468 hres = VarMul(l.v, r.v, &v);
1475 return stack_push(ctx, &v);
1478 static HRESULT interp_exp(exec_ctx_t *ctx)
1486 hres = stack_pop_val(ctx, &r);
1490 hres = stack_pop_val(ctx, &l);
1491 if(SUCCEEDED(hres)) {
1492 hres = VarPow(l.v, r.v, &v);
1499 return stack_push(ctx, &v);
1502 static HRESULT interp_neg(exec_ctx_t *ctx)
1508 hres = stack_pop_val(ctx, &val);
1512 hres = VarNeg(val.v, &v);
1517 return stack_push(ctx, &v);
1520 static HRESULT interp_incc(exec_ctx_t *ctx)
1522 const BSTR ident = ctx->instr->arg1.bstr;
1529 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1533 if(ref.type != REF_VAR) {
1534 FIXME("ref.type is not REF_VAR\n");
1538 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1542 VariantClear(ref.u.v);
1547 static const instr_func_t op_funcs[] = {
1548 #define X(x,n,a,b) interp_ ## x,
1553 static const unsigned op_move[] = {
1554 #define X(x,n,a,b) n,
1559 static void release_exec(exec_ctx_t *ctx)
1563 VariantClear(&ctx->ret_val);
1566 IDispatch_Release(ctx->this_obj);
1569 for(i=0; i < ctx->func->arg_cnt; i++)
1570 VariantClear(ctx->args+i);
1574 for(i=0; i < ctx->func->var_cnt; i++)
1575 VariantClear(ctx->vars+i);
1578 vbsheap_free(&ctx->heap);
1579 heap_free(ctx->args);
1580 heap_free(ctx->vars);
1581 heap_free(ctx->stack);
1584 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1586 exec_ctx_t exec = {func->code_ctx};
1588 HRESULT hres = S_OK;
1590 exec.code = func->code_ctx;
1592 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1593 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1597 vbsheap_init(&exec.heap);
1603 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1605 release_exec(&exec);
1606 return E_OUTOFMEMORY;
1609 for(i=0; i < func->arg_cnt; i++) {
1611 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1612 if(func->args[i].by_ref)
1615 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1617 hres = VariantCopy(exec.args+i, v);
1620 release_exec(&exec);
1629 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1631 release_exec(&exec);
1632 return E_OUTOFMEMORY;
1638 exec.stack_size = 16;
1640 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1642 release_exec(&exec);
1643 return E_OUTOFMEMORY;
1647 exec.this_obj = this_obj;
1648 else if (ctx->host_global)
1649 exec.this_obj = ctx->host_global;
1651 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1652 IDispatch_AddRef(exec.this_obj);
1654 exec.instr = exec.code->instrs + func->code_off;
1659 op = exec.instr->op;
1660 hres = op_funcs[op](&exec);
1662 FIXME("Failed %08x\n", hres);
1663 stack_popn(&exec, exec.top);
1667 exec.instr += op_move[op];
1671 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1672 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1674 if(SUCCEEDED(hres) && res) {
1675 *res = exec.ret_val;
1676 V_VT(&exec.ret_val) = VT_EMPTY;
1679 release_exec(&exec);