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);
38 dynamic_var_t *dynamic_vars;
48 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)) {
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 inline VARIANT *stack_pop(exec_ctx_t *ctx)
204 return ctx->stack + --ctx->top;
207 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
209 if(ctx->stack_size == ctx->top) {
212 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
215 return E_OUTOFMEMORY;
218 ctx->stack = new_stack;
219 ctx->stack_size *= 2;
222 ctx->stack[ctx->top++] = *v;
226 static void stack_popn(exec_ctx_t *ctx, unsigned n)
229 VariantClear(stack_pop(ctx));
232 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
236 var = stack_pop(ctx);
238 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
240 var = V_VARIANTREF(var);
245 if(V_VT(var) == VT_DISPATCH) {
249 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
251 IDispatch_Release(V_DISPATCH(var));
264 static inline void release_val(variant_val_t *v)
270 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
272 VARIANT *v = stack_pop(ctx);
274 if(V_VT(v) == VT_DISPATCH) {
275 *ret = V_DISPATCH(v);
279 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
280 FIXME("not supported type: %s\n", debugstr_variant(v));
286 if(V_VT(v) != VT_DISPATCH) {
287 FIXME("not disp %s\n", debugstr_variant(v));
292 IDispatch_AddRef(V_DISPATCH(v));
293 *ret = V_DISPATCH(v);
297 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
299 ctx->instr = ctx->code->instrs + addr;
302 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
305 dp->rgdispidNamedArgs = NULL;
312 assert(ctx->top >= arg_cnt);
314 for(i=1; i*2 <= arg_cnt; i++) {
315 tmp = ctx->stack[ctx->top-i];
316 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
317 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
320 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
326 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
328 BSTR identifier = ctx->instr->arg1.bstr;
329 const unsigned arg_cnt = ctx->instr->arg2.uint;
334 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
338 vbstack_to_dp(ctx, arg_cnt, &dp);
343 FIXME("REF_VAR no res\n");
348 FIXME("arguments not implemented\n");
352 V_VT(res) = VT_BYREF|VT_VARIANT;
353 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
356 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
361 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
367 FIXME("arguments on object\n");
372 IDispatch_AddRef(ref.u.obj);
373 V_VT(res) = VT_DISPATCH;
374 V_DISPATCH(res) = ref.u.obj;
378 FIXME("%s not found\n", debugstr_w(identifier));
379 return DISP_E_UNKNOWNNAME;
382 stack_popn(ctx, arg_cnt);
386 static HRESULT interp_icall(exec_ctx_t *ctx)
393 hres = do_icall(ctx, &v);
397 return stack_push(ctx, &v);
400 static HRESULT interp_icallv(exec_ctx_t *ctx)
403 return do_icall(ctx, NULL);
406 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
408 const BSTR identifier = ctx->instr->arg1.bstr;
409 const unsigned arg_cnt = ctx->instr->arg2.uint;
415 hres = stack_pop_disp(ctx, &obj);
424 vbstack_to_dp(ctx, arg_cnt, &dp);
426 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
428 hres = disp_call(ctx->script, obj, id, &dp, res);
429 IDispatch_Release(obj);
433 stack_popn(ctx, arg_cnt);
437 static HRESULT interp_mcall(exec_ctx_t *ctx)
444 hres = do_mcall(ctx, &res);
448 return stack_push(ctx, &res);
451 static HRESULT interp_mcallv(exec_ctx_t *ctx)
455 return do_mcall(ctx, NULL);
458 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
463 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
469 VARIANT *v = ref.u.v;
471 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
479 hres = VariantCopy(v, val);
484 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
489 FIXME("functions not implemented\n");
495 dynamic_var_t *new_var;
500 if(ctx->func->code_ctx->option_explicit) {
501 FIXME("throw exception\n");
505 TRACE("creating variable %s\n", debugstr_w(name));
507 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
509 new_var = vbsheap_alloc(heap, sizeof(*new_var));
511 return E_OUTOFMEMORY;
513 size = (strlenW(name)+1)*sizeof(WCHAR);
514 str = vbsheap_alloc(heap, size);
516 return E_OUTOFMEMORY;
517 memcpy(str, name, size);
523 hres = VariantCopy(&new_var->v, val);
528 if(ctx->func->type == FUNC_GLOBAL) {
529 new_var->next = ctx->script->global_vars;
530 ctx->script->global_vars = new_var;
532 new_var->next = ctx->dynamic_vars;
533 ctx->dynamic_vars = new_var;
541 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
543 const BSTR arg = ctx->instr->arg1.bstr;
547 TRACE("%s\n", debugstr_w(arg));
549 hres = stack_pop_val(ctx, &v);
553 return assign_ident(ctx, arg, v.v, v.owned);
556 static HRESULT interp_set_ident(exec_ctx_t *ctx)
558 const BSTR arg = ctx->instr->arg1.bstr;
563 TRACE("%s\n", debugstr_w(arg));
565 hres = stack_pop_disp(ctx, &disp);
569 V_VT(&v) = VT_DISPATCH;
570 V_DISPATCH(&v) = disp;
571 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
574 static HRESULT interp_assign_member(exec_ctx_t *ctx)
576 BSTR identifier = ctx->instr->arg1.bstr;
582 TRACE("%s\n", debugstr_w(identifier));
584 hres = stack_pop_disp(ctx, &obj);
593 hres = stack_pop_val(ctx, &val);
595 IDispatch_Release(obj);
599 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
601 hres = disp_propput(ctx->script, obj, id, val.v);
604 IDispatch_Release(obj);
608 static HRESULT interp_set_member(exec_ctx_t *ctx)
610 BSTR identifier = ctx->instr->arg1.bstr;
611 IDispatch *obj, *val;
615 TRACE("%s\n", debugstr_w(identifier));
617 hres = stack_pop_disp(ctx, &obj);
626 hres = stack_pop_disp(ctx, &val);
628 IDispatch_Release(obj);
632 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
633 if(SUCCEEDED(hres)) {
636 V_VT(&v) = VT_DISPATCH;
637 V_DISPATCH(&v) = val;
638 hres = disp_propput(ctx->script, obj, id, &v);
642 IDispatch_Release(val);
643 IDispatch_Release(obj);
647 static HRESULT interp_new(exec_ctx_t *ctx)
649 const WCHAR *arg = ctx->instr->arg1.bstr;
650 class_desc_t *class_desc;
655 TRACE("%s\n", debugstr_w(arg));
657 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
658 if(!strcmpiW(class_desc->name, arg))
662 FIXME("Class %s not found\n", debugstr_w(arg));
666 hres = create_vbdisp(class_desc, &obj);
670 V_VT(&v) = VT_DISPATCH;
671 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
672 return stack_push(ctx, &v);
675 static HRESULT interp_jmp(exec_ctx_t *ctx)
677 const unsigned arg = ctx->instr->arg1.uint;
685 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
687 const unsigned arg = ctx->instr->arg1.uint;
693 hres = stack_pop_val(ctx, &val);
697 if(V_VT(val.v) != VT_BOOL) {
698 FIXME("unsupported for %s\n", debugstr_variant(val.v));
706 instr_jmp(ctx, ctx->instr->arg1.uint);
710 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
712 const unsigned arg = ctx->instr->arg1.uint;
718 hres = stack_pop_val(ctx, &val);
722 if(V_VT(val.v) != VT_BOOL) {
723 FIXME("unsupported for %s\n", debugstr_variant(val.v));
729 instr_jmp(ctx, ctx->instr->arg1.uint);
735 static HRESULT interp_ret(exec_ctx_t *ctx)
743 static HRESULT interp_stop(exec_ctx_t *ctx)
747 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
751 static HRESULT interp_me(exec_ctx_t *ctx)
757 IDispatch_AddRef(ctx->this_obj);
758 V_VT(&v) = VT_DISPATCH;
759 V_DISPATCH(&v) = ctx->this_obj;
760 return stack_push(ctx, &v);
763 static HRESULT interp_bool(exec_ctx_t *ctx)
765 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
768 TRACE("%s\n", arg ? "true" : "false");
772 return stack_push(ctx, &v);
775 static HRESULT interp_errmode(exec_ctx_t *ctx)
777 const int err_mode = ctx->instr->arg1.uint;
778 FIXME("%d\n", err_mode);
782 static HRESULT interp_string(exec_ctx_t *ctx)
789 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
791 return E_OUTOFMEMORY;
793 return stack_push(ctx, &v);
796 static HRESULT interp_long(exec_ctx_t *ctx)
798 const LONG arg = ctx->instr->arg1.lng;
805 return stack_push(ctx, &v);
808 static HRESULT interp_short(exec_ctx_t *ctx)
810 const LONG arg = ctx->instr->arg1.lng;
817 return stack_push(ctx, &v);
820 static HRESULT interp_double(exec_ctx_t *ctx)
822 const DOUBLE *arg = ctx->instr->arg1.dbl;
825 TRACE("%lf\n", *arg);
829 return stack_push(ctx, &v);
832 static HRESULT interp_empty(exec_ctx_t *ctx)
839 return stack_push(ctx, &v);
842 static HRESULT interp_null(exec_ctx_t *ctx)
849 return stack_push(ctx, &v);
852 static HRESULT interp_nothing(exec_ctx_t *ctx)
858 V_VT(&v) = VT_DISPATCH;
859 V_DISPATCH(&v) = NULL;
860 return stack_push(ctx, &v);
863 static HRESULT interp_not(exec_ctx_t *ctx)
871 hres = stack_pop_val(ctx, &val);
875 hres = VarNot(val.v, &v);
880 return stack_push(ctx, &v);
883 static HRESULT interp_and(exec_ctx_t *ctx)
891 hres = stack_pop_val(ctx, &r);
895 hres = stack_pop_val(ctx, &l);
896 if(SUCCEEDED(hres)) {
897 hres = VarAnd(l.v, r.v, &v);
904 return stack_push(ctx, &v);
907 static HRESULT interp_or(exec_ctx_t *ctx)
915 hres = stack_pop_val(ctx, &r);
919 hres = stack_pop_val(ctx, &l);
920 if(SUCCEEDED(hres)) {
921 hres = VarOr(l.v, r.v, &v);
928 return stack_push(ctx, &v);
931 static HRESULT interp_xor(exec_ctx_t *ctx)
939 hres = stack_pop_val(ctx, &r);
943 hres = stack_pop_val(ctx, &l);
944 if(SUCCEEDED(hres)) {
945 hres = VarXor(l.v, r.v, &v);
952 return stack_push(ctx, &v);
955 static HRESULT interp_eqv(exec_ctx_t *ctx)
963 hres = stack_pop_val(ctx, &r);
967 hres = stack_pop_val(ctx, &l);
968 if(SUCCEEDED(hres)) {
969 hres = VarEqv(l.v, r.v, &v);
976 return stack_push(ctx, &v);
979 static HRESULT interp_imp(exec_ctx_t *ctx)
987 hres = stack_pop_val(ctx, &r);
991 hres = stack_pop_val(ctx, &l);
992 if(SUCCEEDED(hres)) {
993 hres = VarImp(l.v, r.v, &v);
1000 return stack_push(ctx, &v);
1003 static HRESULT cmp_oper(exec_ctx_t *ctx)
1008 hres = stack_pop_val(ctx, &r);
1012 hres = stack_pop_val(ctx, &l);
1013 if(SUCCEEDED(hres)) {
1014 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1015 FIXME("comparing nulls is not implemented\n");
1018 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1027 static HRESULT interp_equal(exec_ctx_t *ctx)
1034 hres = cmp_oper(ctx);
1039 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1040 return stack_push(ctx, &v);
1043 static HRESULT interp_nequal(exec_ctx_t *ctx)
1050 hres = cmp_oper(ctx);
1055 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1056 return stack_push(ctx, &v);
1059 static HRESULT interp_gt(exec_ctx_t *ctx)
1066 hres = cmp_oper(ctx);
1071 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1072 return stack_push(ctx, &v);
1075 static HRESULT interp_gteq(exec_ctx_t *ctx)
1082 hres = cmp_oper(ctx);
1087 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1088 return stack_push(ctx, &v);
1091 static HRESULT interp_lt(exec_ctx_t *ctx)
1098 hres = cmp_oper(ctx);
1103 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1104 return stack_push(ctx, &v);
1107 static HRESULT interp_lteq(exec_ctx_t *ctx)
1114 hres = cmp_oper(ctx);
1119 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1120 return stack_push(ctx, &v);
1123 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1125 IObjectIdentity *identity;
1126 IUnknown *unk1, *unk2;
1129 if(disp1 == disp2) {
1130 *ret = VARIANT_TRUE;
1134 if(!disp1 || !disp2) {
1135 *ret = VARIANT_FALSE;
1139 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1143 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1145 IUnknown_Release(unk1);
1150 *ret = VARIANT_TRUE;
1152 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1153 if(SUCCEEDED(hres)) {
1154 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1155 IObjectIdentity_Release(identity);
1156 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1158 *ret = VARIANT_FALSE;
1162 IUnknown_Release(unk1);
1163 IUnknown_Release(unk2);
1167 static HRESULT interp_is(exec_ctx_t *ctx)
1175 hres = stack_pop_disp(ctx, &r);
1179 hres = stack_pop_disp(ctx, &l);
1180 if(SUCCEEDED(hres)) {
1182 hres = disp_cmp(l, r, &V_BOOL(&v));
1184 IDispatch_Release(l);
1187 IDispatch_Release(r);
1191 return stack_push(ctx, &v);
1194 static HRESULT interp_concat(exec_ctx_t *ctx)
1202 hres = stack_pop_val(ctx, &r);
1206 hres = stack_pop_val(ctx, &l);
1207 if(SUCCEEDED(hres)) {
1208 hres = VarCat(l.v, r.v, &v);
1215 return stack_push(ctx, &v);
1218 static HRESULT interp_add(exec_ctx_t *ctx)
1226 hres = stack_pop_val(ctx, &r);
1230 hres = stack_pop_val(ctx, &l);
1231 if(SUCCEEDED(hres)) {
1232 hres = VarAdd(l.v, r.v, &v);
1239 return stack_push(ctx, &v);
1242 static HRESULT interp_sub(exec_ctx_t *ctx)
1250 hres = stack_pop_val(ctx, &r);
1254 hres = stack_pop_val(ctx, &l);
1255 if(SUCCEEDED(hres)) {
1256 hres = VarSub(l.v, r.v, &v);
1263 return stack_push(ctx, &v);
1266 static HRESULT interp_mod(exec_ctx_t *ctx)
1274 hres = stack_pop_val(ctx, &r);
1278 hres = stack_pop_val(ctx, &l);
1279 if(SUCCEEDED(hres)) {
1280 hres = VarMod(l.v, r.v, &v);
1287 return stack_push(ctx, &v);
1290 static HRESULT interp_idiv(exec_ctx_t *ctx)
1298 hres = stack_pop_val(ctx, &r);
1302 hres = stack_pop_val(ctx, &l);
1303 if(SUCCEEDED(hres)) {
1304 hres = VarIdiv(l.v, r.v, &v);
1311 return stack_push(ctx, &v);
1314 static HRESULT interp_div(exec_ctx_t *ctx)
1322 hres = stack_pop_val(ctx, &r);
1326 hres = stack_pop_val(ctx, &l);
1327 if(SUCCEEDED(hres)) {
1328 hres = VarDiv(l.v, r.v, &v);
1335 return stack_push(ctx, &v);
1338 static HRESULT interp_mul(exec_ctx_t *ctx)
1346 hres = stack_pop_val(ctx, &r);
1350 hres = stack_pop_val(ctx, &l);
1351 if(SUCCEEDED(hres)) {
1352 hres = VarMul(l.v, r.v, &v);
1359 return stack_push(ctx, &v);
1362 static HRESULT interp_exp(exec_ctx_t *ctx)
1370 hres = stack_pop_val(ctx, &r);
1374 hres = stack_pop_val(ctx, &l);
1375 if(SUCCEEDED(hres)) {
1376 hres = VarPow(l.v, r.v, &v);
1383 return stack_push(ctx, &v);
1386 static HRESULT interp_neg(exec_ctx_t *ctx)
1392 hres = stack_pop_val(ctx, &val);
1396 hres = VarNeg(val.v, &v);
1401 return stack_push(ctx, &v);
1404 static const instr_func_t op_funcs[] = {
1405 #define X(x,n,a,b) interp_ ## x,
1410 static const unsigned op_move[] = {
1411 #define X(x,n,a,b) n,
1416 static void release_exec(exec_ctx_t *ctx)
1420 VariantClear(&ctx->ret_val);
1423 IDispatch_Release(ctx->this_obj);
1426 for(i=0; i < ctx->func->arg_cnt; i++)
1427 VariantClear(ctx->args+i);
1431 for(i=0; i < ctx->func->var_cnt; i++)
1432 VariantClear(ctx->vars+i);
1435 vbsheap_free(&ctx->heap);
1436 heap_free(ctx->args);
1437 heap_free(ctx->vars);
1438 heap_free(ctx->stack);
1441 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1443 exec_ctx_t exec = {func->code_ctx};
1445 HRESULT hres = S_OK;
1447 exec.code = func->code_ctx;
1449 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1450 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1454 vbsheap_init(&exec.heap);
1460 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1462 release_exec(&exec);
1463 return E_OUTOFMEMORY;
1466 for(i=0; i < func->arg_cnt; i++) {
1468 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1469 if(func->args[i].by_ref)
1472 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1474 hres = VariantCopy(exec.args+i, v);
1477 release_exec(&exec);
1486 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1488 release_exec(&exec);
1489 return E_OUTOFMEMORY;
1495 exec.stack_size = 16;
1497 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1499 release_exec(&exec);
1500 return E_OUTOFMEMORY;
1504 exec.this_obj = this_obj;
1505 else if (ctx->host_global)
1506 exec.this_obj = ctx->host_global;
1508 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1509 IDispatch_AddRef(exec.this_obj);
1511 exec.instr = exec.code->instrs + func->code_off;
1516 op = exec.instr->op;
1517 hres = op_funcs[op](&exec);
1519 FIXME("Failed %08x\n", hres);
1520 stack_popn(&exec, exec.top);
1524 exec.instr += op_move[op];
1528 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1529 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1531 if(SUCCEEDED(hres) && res) {
1532 *res = exec.ret_val;
1533 V_VT(&exec.ret_val) = VT_EMPTY;
1536 release_exec(&exec);