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);
27 static DISPID propput_dispid = DISPID_PROPERTYPUT;
39 dynamic_var_t *dynamic_vars;
51 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
81 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
84 if(!strcmpiW(var->name, name)) {
85 ref->type = var->is_const ? REF_CONST : REF_VAR;
96 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
104 static const WCHAR errW[] = {'e','r','r',0};
106 if(invoke_type == VBDISP_LET
107 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
108 && !strcmpiW(name, ctx->func->name)) {
110 ref->u.v = &ctx->ret_val;
114 for(i=0; i < ctx->func->var_cnt; i++) {
115 if(!strcmpiW(ctx->func->vars[i].name, name)) {
117 ref->u.v = ctx->vars+i;
122 for(i=0; i < ctx->func->arg_cnt; i++) {
123 if(!strcmpiW(ctx->func->args[i].name, name)) {
125 ref->u.v = ctx->args+i;
130 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
133 if(ctx->func->type != FUNC_GLOBAL) {
134 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
135 if(SUCCEEDED(hres)) {
136 ref->type = REF_DISP;
137 ref->u.d.disp = ctx->this_obj;
143 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
146 for(func = ctx->script->global_funcs; func; func = func->next) {
147 if(!strcmpiW(func->name, name)) {
148 ref->type = REF_FUNC;
154 if(!strcmpiW(name, errW)) {
156 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
160 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
161 if(SUCCEEDED(hres)) {
162 ref->type = REF_DISP;
163 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
168 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
169 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
173 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
175 WARN("GetItemInfo failed: %08x\n", hres);
179 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
180 IUnknown_Release(unk);
182 WARN("object does not implement IDispatch\n");
188 ref->u.obj = item->disp;
193 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
194 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
195 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
196 if(SUCCEEDED(hres)) {
197 ref->type = REF_DISP;
198 ref->u.d.disp = item->disp;
205 ref->type = REF_NONE;
209 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
211 dynamic_var_t *new_var;
217 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
219 new_var = vbsheap_alloc(heap, sizeof(*new_var));
221 return E_OUTOFMEMORY;
223 size = (strlenW(name)+1)*sizeof(WCHAR);
224 str = vbsheap_alloc(heap, size);
226 return E_OUTOFMEMORY;
227 memcpy(str, name, size);
229 new_var->is_const = is_const;
234 V_VT(&new_var->v) = VT_EMPTY;
235 hres = VariantCopy(&new_var->v, val);
240 if(ctx->func->type == FUNC_GLOBAL) {
241 new_var->next = ctx->script->global_vars;
242 ctx->script->global_vars = new_var;
244 new_var->next = ctx->dynamic_vars;
245 ctx->dynamic_vars = new_var;
251 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
254 return ctx->stack + --ctx->top;
257 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
259 assert(ctx->top >= n);
260 return ctx->stack + (ctx->top-n-1);
263 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
265 if(ctx->stack_size == ctx->top) {
268 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
271 return E_OUTOFMEMORY;
274 ctx->stack = new_stack;
275 ctx->stack_size *= 2;
278 ctx->stack[ctx->top++] = *v;
282 static void stack_popn(exec_ctx_t *ctx, unsigned n)
285 VariantClear(stack_pop(ctx));
288 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
292 var = stack_pop(ctx);
294 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
296 var = V_VARIANTREF(var);
301 if(V_VT(var) == VT_DISPATCH) {
305 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
307 IDispatch_Release(V_DISPATCH(var));
320 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
322 VARIANT *v = stack_top(ctx, n);
325 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
326 VARIANT *ref = V_VARIANTREF(v);
329 hres = VariantCopy(v, ref);
334 if(V_VT(v) == VT_DISPATCH) {
338 disp = V_DISPATCH(v);
340 hres = disp_call(ctx->script, disp, DISPID_VALUE, &dp, v);
341 IDispatch_Release(disp);
349 static inline void release_val(variant_val_t *v)
355 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
360 hres = stack_pop_val(ctx, &val);
376 FIXME("unsupported for %s\n", debugstr_variant(val.v));
383 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
385 VARIANT *v = stack_pop(ctx);
387 if(V_VT(v) == VT_DISPATCH) {
388 *ret = V_DISPATCH(v);
392 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
393 FIXME("not supported type: %s\n", debugstr_variant(v));
399 if(V_VT(v) != VT_DISPATCH) {
400 FIXME("not disp %s\n", debugstr_variant(v));
405 IDispatch_AddRef(V_DISPATCH(v));
406 *ret = V_DISPATCH(v);
410 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
412 VARIANT *v = stack_top(ctx, n), *ref;
414 if(V_VT(v) != VT_DISPATCH) {
415 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
416 FIXME("not supported type: %s\n", debugstr_variant(v));
420 ref = V_VARIANTREF(v);
421 if(V_VT(ref) != VT_DISPATCH) {
422 FIXME("not disp %s\n", debugstr_variant(ref));
426 V_VT(v) = VT_DISPATCH;
427 V_DISPATCH(v) = V_DISPATCH(ref);
429 IDispatch_AddRef(V_DISPATCH(v));
433 *disp = V_DISPATCH(v);
437 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
439 ctx->instr = ctx->code->instrs + addr;
442 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
444 dp->cNamedArgs = is_propput ? 1 : 0;
445 dp->cArgs = arg_cnt + dp->cNamedArgs;
446 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
452 assert(ctx->top >= arg_cnt);
454 for(i=1; i*2 <= arg_cnt; i++) {
455 tmp = ctx->stack[ctx->top-i];
456 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
457 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
460 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
462 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
466 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
468 BSTR identifier = ctx->instr->arg1.bstr;
469 const unsigned arg_cnt = ctx->instr->arg2.uint;
474 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
478 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
484 FIXME("REF_VAR no res\n");
489 FIXME("arguments not implemented\n");
493 V_VT(res) = VT_BYREF|VT_VARIANT;
494 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
497 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
502 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
508 FIXME("arguments on object\n");
513 IDispatch_AddRef(ref.u.obj);
514 V_VT(res) = VT_DISPATCH;
515 V_DISPATCH(res) = ref.u.obj;
519 FIXME("%s not found\n", debugstr_w(identifier));
520 return DISP_E_UNKNOWNNAME;
523 stack_popn(ctx, arg_cnt);
527 static HRESULT interp_icall(exec_ctx_t *ctx)
534 hres = do_icall(ctx, &v);
538 return stack_push(ctx, &v);
541 static HRESULT interp_icallv(exec_ctx_t *ctx)
544 return do_icall(ctx, NULL);
547 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
549 const BSTR identifier = ctx->instr->arg1.bstr;
550 const unsigned arg_cnt = ctx->instr->arg2.uint;
556 hres = stack_pop_disp(ctx, &obj);
565 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
567 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
569 hres = disp_call(ctx->script, obj, id, &dp, res);
570 IDispatch_Release(obj);
574 stack_popn(ctx, arg_cnt);
578 static HRESULT interp_mcall(exec_ctx_t *ctx)
585 hres = do_mcall(ctx, &res);
589 return stack_push(ctx, &res);
592 static HRESULT interp_mcallv(exec_ctx_t *ctx)
596 return do_mcall(ctx, NULL);
599 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
604 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
610 VARIANT *v = ref.u.v;
613 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
617 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
620 hres = VariantCopy(v, dp->rgvarg);
624 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
627 FIXME("functions not implemented\n");
633 FIXME("REF_CONST\n");
636 if(ctx->func->code_ctx->option_explicit) {
637 FIXME("throw exception\n");
641 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
645 TRACE("creating variable %s\n", debugstr_w(name));
646 hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE);
653 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
655 const BSTR arg = ctx->instr->arg1.bstr;
656 const unsigned arg_cnt = ctx->instr->arg2.uint;
660 TRACE("%s\n", debugstr_w(arg));
662 hres = stack_assume_val(ctx, arg_cnt);
666 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
667 hres = assign_ident(ctx, arg, &dp);
671 stack_popn(ctx, arg_cnt+1);
675 static HRESULT interp_set_ident(exec_ctx_t *ctx)
677 const BSTR arg = ctx->instr->arg1.bstr;
678 const unsigned arg_cnt = ctx->instr->arg2.uint;
682 TRACE("%s\n", debugstr_w(arg));
685 FIXME("arguments not supported\n");
689 hres = stack_assume_disp(ctx, 0, NULL);
693 vbstack_to_dp(ctx, 0, TRUE, &dp);
694 hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
702 static HRESULT interp_assign_member(exec_ctx_t *ctx)
704 BSTR identifier = ctx->instr->arg1.bstr;
705 const unsigned arg_cnt = ctx->instr->arg2.uint;
711 TRACE("%s\n", debugstr_w(identifier));
713 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
722 hres = stack_assume_val(ctx, arg_cnt);
726 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
727 if(SUCCEEDED(hres)) {
728 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
729 hres = disp_propput(ctx->script, obj, id, &dp);
734 stack_popn(ctx, arg_cnt+2);
738 static HRESULT interp_set_member(exec_ctx_t *ctx)
740 BSTR identifier = ctx->instr->arg1.bstr;
741 const unsigned arg_cnt = ctx->instr->arg2.uint;
747 TRACE("%s\n", debugstr_w(identifier));
750 FIXME("arguments not supported\n");
754 hres = stack_assume_disp(ctx, 1, &obj);
763 hres = stack_assume_disp(ctx, 0, NULL);
767 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
768 if(SUCCEEDED(hres)) {
769 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
770 hres = disp_propput(ctx->script, obj, id, &dp);
779 static HRESULT interp_const(exec_ctx_t *ctx)
781 BSTR arg = ctx->instr->arg1.bstr;
786 TRACE("%s\n", debugstr_w(arg));
788 assert(ctx->func->type == FUNC_GLOBAL);
790 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
794 if(ref.type != REF_NONE) {
795 FIXME("%s already defined\n", debugstr_w(arg));
799 hres = stack_pop_val(ctx, &val);
803 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
806 static HRESULT interp_val(exec_ctx_t *ctx)
814 hres = stack_pop_val(ctx, &val);
820 hres = VariantCopy(&v, val.v);
825 return stack_push(ctx, val.owned ? val.v : &v);
828 static HRESULT interp_pop(exec_ctx_t *ctx)
830 const unsigned n = ctx->instr->arg1.uint;
838 static HRESULT interp_new(exec_ctx_t *ctx)
840 const WCHAR *arg = ctx->instr->arg1.bstr;
841 class_desc_t *class_desc;
846 TRACE("%s\n", debugstr_w(arg));
848 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
849 if(!strcmpiW(class_desc->name, arg))
853 FIXME("Class %s not found\n", debugstr_w(arg));
857 hres = create_vbdisp(class_desc, &obj);
861 V_VT(&v) = VT_DISPATCH;
862 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
863 return stack_push(ctx, &v);
866 static HRESULT interp_step(exec_ctx_t *ctx)
868 const BSTR ident = ctx->instr->arg2.bstr;
874 TRACE("%s\n", debugstr_w(ident));
878 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
882 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
884 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
888 if(ref.type != REF_VAR) {
889 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
893 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
897 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
901 instr_jmp(ctx, ctx->instr->arg1.uint);
906 static HRESULT interp_newenum(exec_ctx_t *ctx)
920 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_NEWENUM, &dp, &iterv);
925 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
926 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
927 VariantClear(&iterv);
931 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
932 IUnknown_Release(V_UNKNOWN(&iterv));
934 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
938 V_VT(&r) = VT_UNKNOWN;
939 V_UNKNOWN(&r) = (IUnknown*)iter;
943 FIXME("Unsupported for %s\n", debugstr_variant(v));
948 return stack_push(ctx, &r);
951 static HRESULT interp_enumnext(exec_ctx_t *ctx)
953 const unsigned loop_end = ctx->instr->arg1.uint;
954 const BSTR ident = ctx->instr->arg2.bstr;
956 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
963 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
964 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
967 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
971 do_continue = hres == S_OK;
972 hres = assign_ident(ctx, ident, &dp);
981 instr_jmp(ctx, loop_end);
986 static HRESULT interp_jmp(exec_ctx_t *ctx)
988 const unsigned arg = ctx->instr->arg1.uint;
996 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
998 const unsigned arg = ctx->instr->arg1.uint;
1004 hres = stack_pop_bool(ctx, &b);
1011 instr_jmp(ctx, ctx->instr->arg1.uint);
1015 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1017 const unsigned arg = ctx->instr->arg1.uint;
1023 hres = stack_pop_bool(ctx, &b);
1028 instr_jmp(ctx, ctx->instr->arg1.uint);
1034 static HRESULT interp_ret(exec_ctx_t *ctx)
1042 static HRESULT interp_stop(exec_ctx_t *ctx)
1046 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1050 static HRESULT interp_me(exec_ctx_t *ctx)
1056 IDispatch_AddRef(ctx->this_obj);
1057 V_VT(&v) = VT_DISPATCH;
1058 V_DISPATCH(&v) = ctx->this_obj;
1059 return stack_push(ctx, &v);
1062 static HRESULT interp_bool(exec_ctx_t *ctx)
1064 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1067 TRACE("%s\n", arg ? "true" : "false");
1071 return stack_push(ctx, &v);
1074 static HRESULT interp_errmode(exec_ctx_t *ctx)
1076 const int err_mode = ctx->instr->arg1.uint;
1078 TRACE("%d\n", err_mode);
1080 ctx->resume_next = err_mode;
1084 static HRESULT interp_string(exec_ctx_t *ctx)
1091 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1093 return E_OUTOFMEMORY;
1095 return stack_push(ctx, &v);
1098 static HRESULT interp_long(exec_ctx_t *ctx)
1100 const LONG arg = ctx->instr->arg1.lng;
1107 return stack_push(ctx, &v);
1110 static HRESULT interp_short(exec_ctx_t *ctx)
1112 const LONG arg = ctx->instr->arg1.lng;
1119 return stack_push(ctx, &v);
1122 static HRESULT interp_double(exec_ctx_t *ctx)
1124 const DOUBLE *arg = ctx->instr->arg1.dbl;
1127 TRACE("%lf\n", *arg);
1131 return stack_push(ctx, &v);
1134 static HRESULT interp_empty(exec_ctx_t *ctx)
1140 V_VT(&v) = VT_EMPTY;
1141 return stack_push(ctx, &v);
1144 static HRESULT interp_null(exec_ctx_t *ctx)
1151 return stack_push(ctx, &v);
1154 static HRESULT interp_nothing(exec_ctx_t *ctx)
1160 V_VT(&v) = VT_DISPATCH;
1161 V_DISPATCH(&v) = NULL;
1162 return stack_push(ctx, &v);
1165 static HRESULT interp_not(exec_ctx_t *ctx)
1173 hres = stack_pop_val(ctx, &val);
1177 hres = VarNot(val.v, &v);
1182 return stack_push(ctx, &v);
1185 static HRESULT interp_and(exec_ctx_t *ctx)
1193 hres = stack_pop_val(ctx, &r);
1197 hres = stack_pop_val(ctx, &l);
1198 if(SUCCEEDED(hres)) {
1199 hres = VarAnd(l.v, r.v, &v);
1206 return stack_push(ctx, &v);
1209 static HRESULT interp_or(exec_ctx_t *ctx)
1217 hres = stack_pop_val(ctx, &r);
1221 hres = stack_pop_val(ctx, &l);
1222 if(SUCCEEDED(hres)) {
1223 hres = VarOr(l.v, r.v, &v);
1230 return stack_push(ctx, &v);
1233 static HRESULT interp_xor(exec_ctx_t *ctx)
1241 hres = stack_pop_val(ctx, &r);
1245 hres = stack_pop_val(ctx, &l);
1246 if(SUCCEEDED(hres)) {
1247 hres = VarXor(l.v, r.v, &v);
1254 return stack_push(ctx, &v);
1257 static HRESULT interp_eqv(exec_ctx_t *ctx)
1265 hres = stack_pop_val(ctx, &r);
1269 hres = stack_pop_val(ctx, &l);
1270 if(SUCCEEDED(hres)) {
1271 hres = VarEqv(l.v, r.v, &v);
1278 return stack_push(ctx, &v);
1281 static HRESULT interp_imp(exec_ctx_t *ctx)
1289 hres = stack_pop_val(ctx, &r);
1293 hres = stack_pop_val(ctx, &l);
1294 if(SUCCEEDED(hres)) {
1295 hres = VarImp(l.v, r.v, &v);
1302 return stack_push(ctx, &v);
1305 static HRESULT cmp_oper(exec_ctx_t *ctx)
1310 hres = stack_pop_val(ctx, &r);
1314 hres = stack_pop_val(ctx, &l);
1315 if(SUCCEEDED(hres)) {
1316 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1317 FIXME("comparing nulls is not implemented\n");
1320 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1329 static HRESULT interp_equal(exec_ctx_t *ctx)
1336 hres = cmp_oper(ctx);
1341 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1342 return stack_push(ctx, &v);
1345 static HRESULT interp_nequal(exec_ctx_t *ctx)
1352 hres = cmp_oper(ctx);
1357 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1358 return stack_push(ctx, &v);
1361 static HRESULT interp_gt(exec_ctx_t *ctx)
1368 hres = cmp_oper(ctx);
1373 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1374 return stack_push(ctx, &v);
1377 static HRESULT interp_gteq(exec_ctx_t *ctx)
1384 hres = cmp_oper(ctx);
1389 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1390 return stack_push(ctx, &v);
1393 static HRESULT interp_lt(exec_ctx_t *ctx)
1400 hres = cmp_oper(ctx);
1405 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1406 return stack_push(ctx, &v);
1409 static HRESULT interp_lteq(exec_ctx_t *ctx)
1416 hres = cmp_oper(ctx);
1421 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1422 return stack_push(ctx, &v);
1425 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1427 IObjectIdentity *identity;
1428 IUnknown *unk1, *unk2;
1431 if(disp1 == disp2) {
1432 *ret = VARIANT_TRUE;
1436 if(!disp1 || !disp2) {
1437 *ret = VARIANT_FALSE;
1441 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1445 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1447 IUnknown_Release(unk1);
1452 *ret = VARIANT_TRUE;
1454 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1455 if(SUCCEEDED(hres)) {
1456 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1457 IObjectIdentity_Release(identity);
1458 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1460 *ret = VARIANT_FALSE;
1464 IUnknown_Release(unk1);
1465 IUnknown_Release(unk2);
1469 static HRESULT interp_is(exec_ctx_t *ctx)
1477 hres = stack_pop_disp(ctx, &r);
1481 hres = stack_pop_disp(ctx, &l);
1482 if(SUCCEEDED(hres)) {
1484 hres = disp_cmp(l, r, &V_BOOL(&v));
1486 IDispatch_Release(l);
1489 IDispatch_Release(r);
1493 return stack_push(ctx, &v);
1496 static HRESULT interp_concat(exec_ctx_t *ctx)
1504 hres = stack_pop_val(ctx, &r);
1508 hres = stack_pop_val(ctx, &l);
1509 if(SUCCEEDED(hres)) {
1510 hres = VarCat(l.v, r.v, &v);
1517 return stack_push(ctx, &v);
1520 static HRESULT interp_add(exec_ctx_t *ctx)
1528 hres = stack_pop_val(ctx, &r);
1532 hres = stack_pop_val(ctx, &l);
1533 if(SUCCEEDED(hres)) {
1534 hres = VarAdd(l.v, r.v, &v);
1541 return stack_push(ctx, &v);
1544 static HRESULT interp_sub(exec_ctx_t *ctx)
1552 hres = stack_pop_val(ctx, &r);
1556 hres = stack_pop_val(ctx, &l);
1557 if(SUCCEEDED(hres)) {
1558 hres = VarSub(l.v, r.v, &v);
1565 return stack_push(ctx, &v);
1568 static HRESULT interp_mod(exec_ctx_t *ctx)
1576 hres = stack_pop_val(ctx, &r);
1580 hres = stack_pop_val(ctx, &l);
1581 if(SUCCEEDED(hres)) {
1582 hres = VarMod(l.v, r.v, &v);
1589 return stack_push(ctx, &v);
1592 static HRESULT interp_idiv(exec_ctx_t *ctx)
1600 hres = stack_pop_val(ctx, &r);
1604 hres = stack_pop_val(ctx, &l);
1605 if(SUCCEEDED(hres)) {
1606 hres = VarIdiv(l.v, r.v, &v);
1613 return stack_push(ctx, &v);
1616 static HRESULT interp_div(exec_ctx_t *ctx)
1624 hres = stack_pop_val(ctx, &r);
1628 hres = stack_pop_val(ctx, &l);
1629 if(SUCCEEDED(hres)) {
1630 hres = VarDiv(l.v, r.v, &v);
1637 return stack_push(ctx, &v);
1640 static HRESULT interp_mul(exec_ctx_t *ctx)
1648 hres = stack_pop_val(ctx, &r);
1652 hres = stack_pop_val(ctx, &l);
1653 if(SUCCEEDED(hres)) {
1654 hres = VarMul(l.v, r.v, &v);
1661 return stack_push(ctx, &v);
1664 static HRESULT interp_exp(exec_ctx_t *ctx)
1672 hres = stack_pop_val(ctx, &r);
1676 hres = stack_pop_val(ctx, &l);
1677 if(SUCCEEDED(hres)) {
1678 hres = VarPow(l.v, r.v, &v);
1685 return stack_push(ctx, &v);
1688 static HRESULT interp_neg(exec_ctx_t *ctx)
1694 hres = stack_pop_val(ctx, &val);
1698 hres = VarNeg(val.v, &v);
1703 return stack_push(ctx, &v);
1706 static HRESULT interp_incc(exec_ctx_t *ctx)
1708 const BSTR ident = ctx->instr->arg1.bstr;
1715 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1719 if(ref.type != REF_VAR) {
1720 FIXME("ref.type is not REF_VAR\n");
1724 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1728 VariantClear(ref.u.v);
1733 static const instr_func_t op_funcs[] = {
1734 #define X(x,n,a,b) interp_ ## x,
1739 static const unsigned op_move[] = {
1740 #define X(x,n,a,b) n,
1745 void release_dynamic_vars(dynamic_var_t *var)
1748 VariantClear(&var->v);
1753 static void release_exec(exec_ctx_t *ctx)
1757 VariantClear(&ctx->ret_val);
1758 release_dynamic_vars(ctx->dynamic_vars);
1761 IDispatch_Release(ctx->this_obj);
1764 for(i=0; i < ctx->func->arg_cnt; i++)
1765 VariantClear(ctx->args+i);
1769 for(i=0; i < ctx->func->var_cnt; i++)
1770 VariantClear(ctx->vars+i);
1773 vbsheap_free(&ctx->heap);
1774 heap_free(ctx->args);
1775 heap_free(ctx->vars);
1776 heap_free(ctx->stack);
1779 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1781 exec_ctx_t exec = {func->code_ctx};
1783 HRESULT hres = S_OK;
1785 exec.code = func->code_ctx;
1787 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1788 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1792 vbsheap_init(&exec.heap);
1798 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1800 release_exec(&exec);
1801 return E_OUTOFMEMORY;
1804 for(i=0; i < func->arg_cnt; i++) {
1806 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1807 if(func->args[i].by_ref)
1810 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1812 hres = VariantCopy(exec.args+i, v);
1815 release_exec(&exec);
1824 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1826 release_exec(&exec);
1827 return E_OUTOFMEMORY;
1833 exec.stack_size = 16;
1835 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1837 release_exec(&exec);
1838 return E_OUTOFMEMORY;
1842 exec.this_obj = this_obj;
1843 else if (ctx->host_global)
1844 exec.this_obj = ctx->host_global;
1846 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1847 IDispatch_AddRef(exec.this_obj);
1849 exec.instr = exec.code->instrs + func->code_off;
1854 op = exec.instr->op;
1855 hres = op_funcs[op](&exec);
1857 if(exec.resume_next)
1858 FIXME("Failed %08x in resume next mode\n", hres);
1860 WARN("Failed %08x\n", hres);
1861 stack_popn(&exec, exec.top);
1865 exec.instr += op_move[op];
1869 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1870 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1872 if(SUCCEEDED(hres) && res) {
1873 *res = exec.ret_val;
1874 V_VT(&exec.ret_val) = VT_EMPTY;
1877 release_exec(&exec);