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_GLOBALMEMBERS)) {
168 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
169 if(SUCCEEDED(hres)) {
170 ref->type = REF_DISP;
171 ref->u.d.disp = item->disp;
177 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
181 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
183 WARN("GetItemInfo failed: %08x\n", hres);
187 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
188 IUnknown_Release(unk);
190 WARN("object does not implement IDispatch\n");
196 ref->u.obj = item->disp;
201 ref->type = REF_NONE;
205 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
207 dynamic_var_t *new_var;
213 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
215 new_var = vbsheap_alloc(heap, sizeof(*new_var));
217 return E_OUTOFMEMORY;
219 size = (strlenW(name)+1)*sizeof(WCHAR);
220 str = vbsheap_alloc(heap, size);
222 return E_OUTOFMEMORY;
223 memcpy(str, name, size);
225 new_var->is_const = is_const;
230 hres = VariantCopy(&new_var->v, val);
235 if(ctx->func->type == FUNC_GLOBAL) {
236 new_var->next = ctx->script->global_vars;
237 ctx->script->global_vars = new_var;
239 new_var->next = ctx->dynamic_vars;
240 ctx->dynamic_vars = new_var;
246 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
249 return ctx->stack + --ctx->top;
252 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
254 assert(ctx->top >= n);
255 return ctx->stack + (ctx->top-n-1);
258 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
260 if(ctx->stack_size == ctx->top) {
263 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
266 return E_OUTOFMEMORY;
269 ctx->stack = new_stack;
270 ctx->stack_size *= 2;
273 ctx->stack[ctx->top++] = *v;
277 static void stack_popn(exec_ctx_t *ctx, unsigned n)
280 VariantClear(stack_pop(ctx));
283 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
287 var = stack_pop(ctx);
289 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
291 var = V_VARIANTREF(var);
296 if(V_VT(var) == VT_DISPATCH) {
300 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
302 IDispatch_Release(V_DISPATCH(var));
315 static inline void release_val(variant_val_t *v)
321 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
326 hres = stack_pop_val(ctx, &val);
342 FIXME("unsupported for %s\n", debugstr_variant(val.v));
349 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
351 VARIANT *v = stack_pop(ctx);
353 if(V_VT(v) == VT_DISPATCH) {
354 *ret = V_DISPATCH(v);
358 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
359 FIXME("not supported type: %s\n", debugstr_variant(v));
365 if(V_VT(v) != VT_DISPATCH) {
366 FIXME("not disp %s\n", debugstr_variant(v));
371 IDispatch_AddRef(V_DISPATCH(v));
372 *ret = V_DISPATCH(v);
376 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
378 ctx->instr = ctx->code->instrs + addr;
381 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
384 dp->rgdispidNamedArgs = NULL;
391 assert(ctx->top >= arg_cnt);
393 for(i=1; i*2 <= arg_cnt; i++) {
394 tmp = ctx->stack[ctx->top-i];
395 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
396 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
399 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
405 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
407 BSTR identifier = ctx->instr->arg1.bstr;
408 const unsigned arg_cnt = ctx->instr->arg2.uint;
413 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
417 vbstack_to_dp(ctx, arg_cnt, &dp);
423 FIXME("REF_VAR no res\n");
428 FIXME("arguments not implemented\n");
432 V_VT(res) = VT_BYREF|VT_VARIANT;
433 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
436 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
441 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
447 FIXME("arguments on object\n");
452 IDispatch_AddRef(ref.u.obj);
453 V_VT(res) = VT_DISPATCH;
454 V_DISPATCH(res) = ref.u.obj;
458 FIXME("%s not found\n", debugstr_w(identifier));
459 return DISP_E_UNKNOWNNAME;
462 stack_popn(ctx, arg_cnt);
466 static HRESULT interp_icall(exec_ctx_t *ctx)
473 hres = do_icall(ctx, &v);
477 return stack_push(ctx, &v);
480 static HRESULT interp_icallv(exec_ctx_t *ctx)
483 return do_icall(ctx, NULL);
486 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
488 const BSTR identifier = ctx->instr->arg1.bstr;
489 const unsigned arg_cnt = ctx->instr->arg2.uint;
495 hres = stack_pop_disp(ctx, &obj);
504 vbstack_to_dp(ctx, arg_cnt, &dp);
506 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
508 hres = disp_call(ctx->script, obj, id, &dp, res);
509 IDispatch_Release(obj);
513 stack_popn(ctx, arg_cnt);
517 static HRESULT interp_mcall(exec_ctx_t *ctx)
524 hres = do_mcall(ctx, &res);
528 return stack_push(ctx, &res);
531 static HRESULT interp_mcallv(exec_ctx_t *ctx)
535 return do_mcall(ctx, NULL);
538 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
543 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
549 VARIANT *v = ref.u.v;
551 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
559 hres = VariantCopy(v, val);
564 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
569 FIXME("functions not implemented\n");
575 FIXME("REF_CONST\n");
578 if(ctx->func->code_ctx->option_explicit) {
579 FIXME("throw exception\n");
582 TRACE("creating variable %s\n", debugstr_w(name));
583 hres = add_dynamic_var(ctx, name, FALSE, val, own_val);
590 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
592 const BSTR arg = ctx->instr->arg1.bstr;
593 const unsigned arg_cnt = ctx->instr->arg2.uint;
597 TRACE("%s\n", debugstr_w(arg));
600 FIXME("arguments not supported\n");
604 hres = stack_pop_val(ctx, &v);
608 return assign_ident(ctx, arg, v.v, v.owned);
611 static HRESULT interp_set_ident(exec_ctx_t *ctx)
613 const BSTR arg = ctx->instr->arg1.bstr;
614 const unsigned arg_cnt = ctx->instr->arg2.uint;
619 TRACE("%s\n", debugstr_w(arg));
622 FIXME("arguments not supported\n");
626 hres = stack_pop_disp(ctx, &disp);
630 V_VT(&v) = VT_DISPATCH;
631 V_DISPATCH(&v) = disp;
632 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
635 static HRESULT interp_assign_member(exec_ctx_t *ctx)
637 BSTR identifier = ctx->instr->arg1.bstr;
638 const unsigned arg_cnt = ctx->instr->arg2.uint;
644 TRACE("%s\n", debugstr_w(identifier));
647 FIXME("arguments not supported\n");
651 hres = stack_pop_disp(ctx, &obj);
660 hres = stack_pop_val(ctx, &val);
662 IDispatch_Release(obj);
666 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
668 hres = disp_propput(ctx->script, obj, id, val.v);
671 IDispatch_Release(obj);
675 static HRESULT interp_set_member(exec_ctx_t *ctx)
677 BSTR identifier = ctx->instr->arg1.bstr;
678 const unsigned arg_cnt = ctx->instr->arg2.uint;
679 IDispatch *obj, *val;
683 TRACE("%s\n", debugstr_w(identifier));
686 FIXME("arguments not supported\n");
690 hres = stack_pop_disp(ctx, &obj);
699 hres = stack_pop_disp(ctx, &val);
701 IDispatch_Release(obj);
705 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
706 if(SUCCEEDED(hres)) {
709 V_VT(&v) = VT_DISPATCH;
710 V_DISPATCH(&v) = val;
711 hres = disp_propput(ctx->script, obj, id, &v);
715 IDispatch_Release(val);
716 IDispatch_Release(obj);
720 static HRESULT interp_const(exec_ctx_t *ctx)
722 BSTR arg = ctx->instr->arg1.bstr;
727 TRACE("%s\n", debugstr_w(arg));
729 assert(ctx->func->type == FUNC_GLOBAL);
731 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
735 if(ref.type != REF_NONE) {
736 FIXME("%s already defined\n", debugstr_w(arg));
740 hres = stack_pop_val(ctx, &val);
744 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
747 static HRESULT interp_val(exec_ctx_t *ctx)
755 hres = stack_pop_val(ctx, &val);
761 hres = VariantCopy(&v, val.v);
766 return stack_push(ctx, val.owned ? val.v : &v);
769 static HRESULT interp_pop(exec_ctx_t *ctx)
771 const unsigned n = ctx->instr->arg1.uint;
779 static HRESULT interp_new(exec_ctx_t *ctx)
781 const WCHAR *arg = ctx->instr->arg1.bstr;
782 class_desc_t *class_desc;
787 TRACE("%s\n", debugstr_w(arg));
789 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
790 if(!strcmpiW(class_desc->name, arg))
794 FIXME("Class %s not found\n", debugstr_w(arg));
798 hres = create_vbdisp(class_desc, &obj);
802 V_VT(&v) = VT_DISPATCH;
803 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
804 return stack_push(ctx, &v);
807 static HRESULT interp_step(exec_ctx_t *ctx)
809 const BSTR ident = ctx->instr->arg2.bstr;
815 TRACE("%s\n", debugstr_w(ident));
819 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
823 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
825 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
829 if(ref.type != REF_VAR) {
830 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
834 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
838 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
841 instr_jmp(ctx, ctx->instr->arg1.uint);
845 static HRESULT interp_jmp(exec_ctx_t *ctx)
847 const unsigned arg = ctx->instr->arg1.uint;
855 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
857 const unsigned arg = ctx->instr->arg1.uint;
863 hres = stack_pop_bool(ctx, &b);
870 instr_jmp(ctx, ctx->instr->arg1.uint);
874 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
876 const unsigned arg = ctx->instr->arg1.uint;
882 hres = stack_pop_bool(ctx, &b);
887 instr_jmp(ctx, ctx->instr->arg1.uint);
893 static HRESULT interp_ret(exec_ctx_t *ctx)
901 static HRESULT interp_stop(exec_ctx_t *ctx)
905 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
909 static HRESULT interp_me(exec_ctx_t *ctx)
915 IDispatch_AddRef(ctx->this_obj);
916 V_VT(&v) = VT_DISPATCH;
917 V_DISPATCH(&v) = ctx->this_obj;
918 return stack_push(ctx, &v);
921 static HRESULT interp_bool(exec_ctx_t *ctx)
923 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
926 TRACE("%s\n", arg ? "true" : "false");
930 return stack_push(ctx, &v);
933 static HRESULT interp_errmode(exec_ctx_t *ctx)
935 const int err_mode = ctx->instr->arg1.uint;
937 TRACE("%d\n", err_mode);
939 ctx->resume_next = err_mode;
943 static HRESULT interp_string(exec_ctx_t *ctx)
950 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
952 return E_OUTOFMEMORY;
954 return stack_push(ctx, &v);
957 static HRESULT interp_long(exec_ctx_t *ctx)
959 const LONG arg = ctx->instr->arg1.lng;
966 return stack_push(ctx, &v);
969 static HRESULT interp_short(exec_ctx_t *ctx)
971 const LONG arg = ctx->instr->arg1.lng;
978 return stack_push(ctx, &v);
981 static HRESULT interp_double(exec_ctx_t *ctx)
983 const DOUBLE *arg = ctx->instr->arg1.dbl;
986 TRACE("%lf\n", *arg);
990 return stack_push(ctx, &v);
993 static HRESULT interp_empty(exec_ctx_t *ctx)
1000 return stack_push(ctx, &v);
1003 static HRESULT interp_null(exec_ctx_t *ctx)
1010 return stack_push(ctx, &v);
1013 static HRESULT interp_nothing(exec_ctx_t *ctx)
1019 V_VT(&v) = VT_DISPATCH;
1020 V_DISPATCH(&v) = NULL;
1021 return stack_push(ctx, &v);
1024 static HRESULT interp_not(exec_ctx_t *ctx)
1032 hres = stack_pop_val(ctx, &val);
1036 hres = VarNot(val.v, &v);
1041 return stack_push(ctx, &v);
1044 static HRESULT interp_and(exec_ctx_t *ctx)
1052 hres = stack_pop_val(ctx, &r);
1056 hres = stack_pop_val(ctx, &l);
1057 if(SUCCEEDED(hres)) {
1058 hres = VarAnd(l.v, r.v, &v);
1065 return stack_push(ctx, &v);
1068 static HRESULT interp_or(exec_ctx_t *ctx)
1076 hres = stack_pop_val(ctx, &r);
1080 hres = stack_pop_val(ctx, &l);
1081 if(SUCCEEDED(hres)) {
1082 hres = VarOr(l.v, r.v, &v);
1089 return stack_push(ctx, &v);
1092 static HRESULT interp_xor(exec_ctx_t *ctx)
1100 hres = stack_pop_val(ctx, &r);
1104 hres = stack_pop_val(ctx, &l);
1105 if(SUCCEEDED(hres)) {
1106 hres = VarXor(l.v, r.v, &v);
1113 return stack_push(ctx, &v);
1116 static HRESULT interp_eqv(exec_ctx_t *ctx)
1124 hres = stack_pop_val(ctx, &r);
1128 hres = stack_pop_val(ctx, &l);
1129 if(SUCCEEDED(hres)) {
1130 hres = VarEqv(l.v, r.v, &v);
1137 return stack_push(ctx, &v);
1140 static HRESULT interp_imp(exec_ctx_t *ctx)
1148 hres = stack_pop_val(ctx, &r);
1152 hres = stack_pop_val(ctx, &l);
1153 if(SUCCEEDED(hres)) {
1154 hres = VarImp(l.v, r.v, &v);
1161 return stack_push(ctx, &v);
1164 static HRESULT cmp_oper(exec_ctx_t *ctx)
1169 hres = stack_pop_val(ctx, &r);
1173 hres = stack_pop_val(ctx, &l);
1174 if(SUCCEEDED(hres)) {
1175 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1176 FIXME("comparing nulls is not implemented\n");
1179 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1188 static HRESULT interp_equal(exec_ctx_t *ctx)
1195 hres = cmp_oper(ctx);
1200 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1201 return stack_push(ctx, &v);
1204 static HRESULT interp_nequal(exec_ctx_t *ctx)
1211 hres = cmp_oper(ctx);
1216 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1217 return stack_push(ctx, &v);
1220 static HRESULT interp_gt(exec_ctx_t *ctx)
1227 hres = cmp_oper(ctx);
1232 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1233 return stack_push(ctx, &v);
1236 static HRESULT interp_gteq(exec_ctx_t *ctx)
1243 hres = cmp_oper(ctx);
1248 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1249 return stack_push(ctx, &v);
1252 static HRESULT interp_lt(exec_ctx_t *ctx)
1259 hres = cmp_oper(ctx);
1264 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1265 return stack_push(ctx, &v);
1268 static HRESULT interp_lteq(exec_ctx_t *ctx)
1275 hres = cmp_oper(ctx);
1280 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1281 return stack_push(ctx, &v);
1284 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1286 IObjectIdentity *identity;
1287 IUnknown *unk1, *unk2;
1290 if(disp1 == disp2) {
1291 *ret = VARIANT_TRUE;
1295 if(!disp1 || !disp2) {
1296 *ret = VARIANT_FALSE;
1300 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1304 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1306 IUnknown_Release(unk1);
1311 *ret = VARIANT_TRUE;
1313 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1314 if(SUCCEEDED(hres)) {
1315 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1316 IObjectIdentity_Release(identity);
1317 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1319 *ret = VARIANT_FALSE;
1323 IUnknown_Release(unk1);
1324 IUnknown_Release(unk2);
1328 static HRESULT interp_is(exec_ctx_t *ctx)
1336 hres = stack_pop_disp(ctx, &r);
1340 hres = stack_pop_disp(ctx, &l);
1341 if(SUCCEEDED(hres)) {
1343 hres = disp_cmp(l, r, &V_BOOL(&v));
1345 IDispatch_Release(l);
1348 IDispatch_Release(r);
1352 return stack_push(ctx, &v);
1355 static HRESULT interp_concat(exec_ctx_t *ctx)
1363 hres = stack_pop_val(ctx, &r);
1367 hres = stack_pop_val(ctx, &l);
1368 if(SUCCEEDED(hres)) {
1369 hres = VarCat(l.v, r.v, &v);
1376 return stack_push(ctx, &v);
1379 static HRESULT interp_add(exec_ctx_t *ctx)
1387 hres = stack_pop_val(ctx, &r);
1391 hres = stack_pop_val(ctx, &l);
1392 if(SUCCEEDED(hres)) {
1393 hres = VarAdd(l.v, r.v, &v);
1400 return stack_push(ctx, &v);
1403 static HRESULT interp_sub(exec_ctx_t *ctx)
1411 hres = stack_pop_val(ctx, &r);
1415 hres = stack_pop_val(ctx, &l);
1416 if(SUCCEEDED(hres)) {
1417 hres = VarSub(l.v, r.v, &v);
1424 return stack_push(ctx, &v);
1427 static HRESULT interp_mod(exec_ctx_t *ctx)
1435 hres = stack_pop_val(ctx, &r);
1439 hres = stack_pop_val(ctx, &l);
1440 if(SUCCEEDED(hres)) {
1441 hres = VarMod(l.v, r.v, &v);
1448 return stack_push(ctx, &v);
1451 static HRESULT interp_idiv(exec_ctx_t *ctx)
1459 hres = stack_pop_val(ctx, &r);
1463 hres = stack_pop_val(ctx, &l);
1464 if(SUCCEEDED(hres)) {
1465 hres = VarIdiv(l.v, r.v, &v);
1472 return stack_push(ctx, &v);
1475 static HRESULT interp_div(exec_ctx_t *ctx)
1483 hres = stack_pop_val(ctx, &r);
1487 hres = stack_pop_val(ctx, &l);
1488 if(SUCCEEDED(hres)) {
1489 hres = VarDiv(l.v, r.v, &v);
1496 return stack_push(ctx, &v);
1499 static HRESULT interp_mul(exec_ctx_t *ctx)
1507 hres = stack_pop_val(ctx, &r);
1511 hres = stack_pop_val(ctx, &l);
1512 if(SUCCEEDED(hres)) {
1513 hres = VarMul(l.v, r.v, &v);
1520 return stack_push(ctx, &v);
1523 static HRESULT interp_exp(exec_ctx_t *ctx)
1531 hres = stack_pop_val(ctx, &r);
1535 hres = stack_pop_val(ctx, &l);
1536 if(SUCCEEDED(hres)) {
1537 hres = VarPow(l.v, r.v, &v);
1544 return stack_push(ctx, &v);
1547 static HRESULT interp_neg(exec_ctx_t *ctx)
1553 hres = stack_pop_val(ctx, &val);
1557 hres = VarNeg(val.v, &v);
1562 return stack_push(ctx, &v);
1565 static HRESULT interp_incc(exec_ctx_t *ctx)
1567 const BSTR ident = ctx->instr->arg1.bstr;
1574 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1578 if(ref.type != REF_VAR) {
1579 FIXME("ref.type is not REF_VAR\n");
1583 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1587 VariantClear(ref.u.v);
1592 static const instr_func_t op_funcs[] = {
1593 #define X(x,n,a,b) interp_ ## x,
1598 static const unsigned op_move[] = {
1599 #define X(x,n,a,b) n,
1604 void release_dynamic_vars(dynamic_var_t *var)
1607 VariantClear(&var->v);
1612 static void release_exec(exec_ctx_t *ctx)
1616 VariantClear(&ctx->ret_val);
1617 release_dynamic_vars(ctx->dynamic_vars);
1620 IDispatch_Release(ctx->this_obj);
1623 for(i=0; i < ctx->func->arg_cnt; i++)
1624 VariantClear(ctx->args+i);
1628 for(i=0; i < ctx->func->var_cnt; i++)
1629 VariantClear(ctx->vars+i);
1632 vbsheap_free(&ctx->heap);
1633 heap_free(ctx->args);
1634 heap_free(ctx->vars);
1635 heap_free(ctx->stack);
1638 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1640 exec_ctx_t exec = {func->code_ctx};
1642 HRESULT hres = S_OK;
1644 exec.code = func->code_ctx;
1646 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1647 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1651 vbsheap_init(&exec.heap);
1657 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1659 release_exec(&exec);
1660 return E_OUTOFMEMORY;
1663 for(i=0; i < func->arg_cnt; i++) {
1665 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1666 if(func->args[i].by_ref)
1669 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1671 hres = VariantCopy(exec.args+i, v);
1674 release_exec(&exec);
1683 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1685 release_exec(&exec);
1686 return E_OUTOFMEMORY;
1692 exec.stack_size = 16;
1694 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1696 release_exec(&exec);
1697 return E_OUTOFMEMORY;
1701 exec.this_obj = this_obj;
1702 else if (ctx->host_global)
1703 exec.this_obj = ctx->host_global;
1705 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1706 IDispatch_AddRef(exec.this_obj);
1708 exec.instr = exec.code->instrs + func->code_off;
1713 op = exec.instr->op;
1714 hres = op_funcs[op](&exec);
1716 if(exec.resume_next)
1717 FIXME("Failed %08x in resume next mode\n", hres);
1719 WARN("Failed %08x\n", hres);
1720 stack_popn(&exec, exec.top);
1724 exec.instr += op_move[op];
1728 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1729 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1731 if(SUCCEEDED(hres) && res) {
1732 *res = exec.ret_val;
1733 V_VT(&exec.ret_val) = VT_EMPTY;
1736 release_exec(&exec);