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);
45 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
72 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
75 if(!strcmpiW(var->name, name)) {
87 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
95 if(invoke_type == VBDISP_LET && ctx->func->type == FUNC_FUNCTION && !strcmpiW(name, ctx->func->name)) {
97 ref->u.v = &ctx->ret_val;
101 for(i=0; i < ctx->func->var_cnt; i++) {
102 if(!strcmpiW(ctx->func->vars[i].name, name)) {
104 ref->u.v = ctx->vars+i;
109 for(i=0; i < ctx->func->arg_cnt; i++) {
110 if(!strcmpiW(ctx->func->args[i].name, name)) {
112 ref->u.v = ctx->args+i;
117 hres = disp_get_id(ctx->this_obj, name, TRUE, &id);
118 if(SUCCEEDED(hres)) {
119 ref->type = REF_DISP;
120 ref->u.d.disp = ctx->this_obj;
125 if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
128 for(func = ctx->script->global_funcs; func; func = func->next) {
129 if(!strcmpiW(func->name, name)) {
130 ref->type = REF_FUNC;
136 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
137 if((item->flags & SCRIPTITEM_GLOBALMEMBERS) && item->disp != ctx->this_obj) {
138 hres = disp_get_id(item->disp, name, FALSE, &id);
139 if(SUCCEEDED(hres)) {
140 ref->type = REF_DISP;
141 ref->u.d.disp = item->disp;
148 if(!ctx->func->code_ctx->option_explicit)
149 FIXME("create an attempt to set\n");
151 ref->type = REF_NONE;
155 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
158 return ctx->stack + --ctx->top;
161 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
163 if(ctx->stack_size == ctx->top) {
166 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
169 return E_OUTOFMEMORY;
172 ctx->stack = new_stack;
173 ctx->stack_size *= 2;
176 ctx->stack[ctx->top++] = *v;
180 static void stack_popn(exec_ctx_t *ctx, unsigned n)
183 VariantClear(stack_pop(ctx));
186 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
190 var = stack_pop(ctx);
192 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
194 var = V_VARIANTREF(var);
199 if(V_VT(var) == VT_DISPATCH) {
200 FIXME("got dispatch - get its default value\n");
209 static inline void release_val(variant_val_t *v)
215 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
217 VARIANT *v = stack_pop(ctx);
219 if(V_VT(v) == VT_DISPATCH) {
220 *ret = V_DISPATCH(v);
224 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
225 FIXME("not supported type: %s\n", debugstr_variant(v));
231 if(V_VT(v) != VT_DISPATCH) {
232 FIXME("not disp %s\n", debugstr_variant(v));
237 IDispatch_AddRef(V_DISPATCH(v));
238 *ret = V_DISPATCH(v);
242 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
244 ctx->instr = ctx->code->instrs + addr;
247 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
250 dp->rgdispidNamedArgs = NULL;
257 assert(ctx->top >= arg_cnt);
259 for(i=1; i*2 <= arg_cnt; i++) {
260 tmp = ctx->stack[ctx->top-i];
261 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
262 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
265 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
271 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
273 BSTR identifier = ctx->instr->arg1.bstr;
274 const unsigned arg_cnt = ctx->instr->arg2.uint;
279 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
283 vbstack_to_dp(ctx, arg_cnt, &dp);
288 FIXME("REF_VAR no res\n");
293 FIXME("arguments not implemented\n");
297 V_VT(res) = VT_BYREF|VT_VARIANT;
298 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
301 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
306 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
311 FIXME("%s not found\n", debugstr_w(identifier));
312 return DISP_E_UNKNOWNNAME;
315 stack_popn(ctx, arg_cnt);
319 static HRESULT interp_icall(exec_ctx_t *ctx)
326 hres = do_icall(ctx, &v);
330 return stack_push(ctx, &v);
333 static HRESULT interp_icallv(exec_ctx_t *ctx)
336 return do_icall(ctx, NULL);
339 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
341 const BSTR identifier = ctx->instr->arg1.bstr;
342 const unsigned arg_cnt = ctx->instr->arg2.uint;
348 hres = stack_pop_disp(ctx, &obj);
357 vbstack_to_dp(ctx, arg_cnt, &dp);
359 hres = disp_get_id(obj, identifier, FALSE, &id);
361 hres = disp_call(ctx->script, obj, id, &dp, res);
362 IDispatch_Release(obj);
366 stack_popn(ctx, arg_cnt);
370 static HRESULT interp_mcall(exec_ctx_t *ctx)
377 hres = do_mcall(ctx, &res);
381 return stack_push(ctx, &res);
384 static HRESULT interp_mcallv(exec_ctx_t *ctx)
388 return do_mcall(ctx, NULL);
391 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
396 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
402 VARIANT *v = ref.u.v;
404 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
412 hres = VariantCopy(v, val);
417 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
422 FIXME("functions not implemented\n");
425 FIXME("%s not found\n", debugstr_w(name));
428 return DISP_E_UNKNOWNNAME;
434 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
436 const BSTR arg = ctx->instr->arg1.bstr;
440 TRACE("%s\n", debugstr_w(arg));
442 hres = stack_pop_val(ctx, &v);
446 return assign_ident(ctx, arg, v.v, v.owned);
449 static HRESULT interp_set_ident(exec_ctx_t *ctx)
451 const BSTR arg = ctx->instr->arg1.bstr;
456 TRACE("%s\n", debugstr_w(arg));
458 hres = stack_pop_disp(ctx, &disp);
462 V_VT(&v) = VT_DISPATCH;
463 V_DISPATCH(&v) = disp;
464 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
467 static HRESULT interp_assign_member(exec_ctx_t *ctx)
469 BSTR identifier = ctx->instr->arg1.bstr;
475 TRACE("%s\n", debugstr_w(identifier));
477 hres = stack_pop_disp(ctx, &obj);
486 hres = stack_pop_val(ctx, &val);
488 IDispatch_Release(obj);
492 hres = disp_get_id(obj, identifier, FALSE, &id);
494 hres = disp_propput(ctx->script, obj, id, val.v);
497 IDispatch_Release(obj);
501 static HRESULT interp_set_member(exec_ctx_t *ctx)
503 BSTR identifier = ctx->instr->arg1.bstr;
504 FIXME("%s\n", debugstr_w(identifier));
508 static HRESULT interp_new(exec_ctx_t *ctx)
510 const WCHAR *arg = ctx->instr->arg1.bstr;
511 class_desc_t *class_desc;
516 TRACE("%s\n", debugstr_w(arg));
518 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
519 if(!strcmpiW(class_desc->name, arg))
523 FIXME("Class %s not found\n", debugstr_w(arg));
527 hres = create_vbdisp(class_desc, &obj);
531 V_VT(&v) = VT_DISPATCH;
532 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
533 return stack_push(ctx, &v);
536 static HRESULT interp_jmp(exec_ctx_t *ctx)
538 const unsigned arg = ctx->instr->arg1.uint;
546 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
548 const unsigned arg = ctx->instr->arg1.uint;
554 hres = stack_pop_val(ctx, &val);
558 if(V_VT(val.v) != VT_BOOL) {
559 FIXME("unsupported for %s\n", debugstr_variant(val.v));
567 instr_jmp(ctx, ctx->instr->arg1.uint);
571 static HRESULT interp_ret(exec_ctx_t *ctx)
579 static HRESULT interp_stop(exec_ctx_t *ctx)
583 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
587 static HRESULT interp_bool(exec_ctx_t *ctx)
589 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
592 TRACE("%s\n", arg ? "true" : "false");
596 return stack_push(ctx, &v);
599 static HRESULT interp_string(exec_ctx_t *ctx)
606 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
608 return E_OUTOFMEMORY;
610 return stack_push(ctx, &v);
613 static HRESULT interp_long(exec_ctx_t *ctx)
615 const LONG arg = ctx->instr->arg1.lng;
622 return stack_push(ctx, &v);
625 static HRESULT interp_short(exec_ctx_t *ctx)
627 const LONG arg = ctx->instr->arg1.lng;
634 return stack_push(ctx, &v);
637 static HRESULT interp_double(exec_ctx_t *ctx)
639 const DOUBLE *arg = ctx->instr->arg1.dbl;
642 TRACE("%lf\n", *arg);
646 return stack_push(ctx, &v);
649 static HRESULT interp_empty(exec_ctx_t *ctx)
656 return stack_push(ctx, &v);
659 static HRESULT interp_null(exec_ctx_t *ctx)
666 return stack_push(ctx, &v);
669 static HRESULT interp_nothing(exec_ctx_t *ctx)
675 V_VT(&v) = VT_DISPATCH;
676 V_DISPATCH(&v) = NULL;
677 return stack_push(ctx, &v);
680 static HRESULT interp_not(exec_ctx_t *ctx)
688 hres = stack_pop_val(ctx, &val);
692 hres = VarNot(val.v, &v);
697 return stack_push(ctx, &v);
700 static HRESULT interp_and(exec_ctx_t *ctx)
708 hres = stack_pop_val(ctx, &r);
712 hres = stack_pop_val(ctx, &l);
713 if(SUCCEEDED(hres)) {
714 hres = VarAnd(l.v, r.v, &v);
721 return stack_push(ctx, &v);
724 static HRESULT interp_or(exec_ctx_t *ctx)
732 hres = stack_pop_val(ctx, &r);
736 hres = stack_pop_val(ctx, &l);
737 if(SUCCEEDED(hres)) {
738 hres = VarOr(l.v, r.v, &v);
745 return stack_push(ctx, &v);
748 static HRESULT interp_xor(exec_ctx_t *ctx)
756 hres = stack_pop_val(ctx, &r);
760 hres = stack_pop_val(ctx, &l);
761 if(SUCCEEDED(hres)) {
762 hres = VarXor(l.v, r.v, &v);
769 return stack_push(ctx, &v);
772 static HRESULT interp_eqv(exec_ctx_t *ctx)
780 hres = stack_pop_val(ctx, &r);
784 hres = stack_pop_val(ctx, &l);
785 if(SUCCEEDED(hres)) {
786 hres = VarEqv(l.v, r.v, &v);
793 return stack_push(ctx, &v);
796 static HRESULT interp_imp(exec_ctx_t *ctx)
804 hres = stack_pop_val(ctx, &r);
808 hres = stack_pop_val(ctx, &l);
809 if(SUCCEEDED(hres)) {
810 hres = VarImp(l.v, r.v, &v);
817 return stack_push(ctx, &v);
820 static HRESULT cmp_oper(exec_ctx_t *ctx)
825 hres = stack_pop_val(ctx, &r);
829 hres = stack_pop_val(ctx, &l);
830 if(SUCCEEDED(hres)) {
831 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
832 FIXME("comparing nulls is not implemented\n");
835 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
844 static HRESULT interp_equal(exec_ctx_t *ctx)
851 hres = cmp_oper(ctx);
856 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
857 return stack_push(ctx, &v);
860 static HRESULT interp_nequal(exec_ctx_t *ctx)
867 hres = cmp_oper(ctx);
872 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
873 return stack_push(ctx, &v);
876 static HRESULT interp_concat(exec_ctx_t *ctx)
884 hres = stack_pop_val(ctx, &r);
888 hres = stack_pop_val(ctx, &l);
889 if(SUCCEEDED(hres)) {
890 hres = VarCat(l.v, r.v, &v);
897 return stack_push(ctx, &v);
900 static HRESULT interp_add(exec_ctx_t *ctx)
908 hres = stack_pop_val(ctx, &r);
912 hres = stack_pop_val(ctx, &l);
913 if(SUCCEEDED(hres)) {
914 hres = VarAdd(l.v, r.v, &v);
921 return stack_push(ctx, &v);
924 static HRESULT interp_sub(exec_ctx_t *ctx)
932 hres = stack_pop_val(ctx, &r);
936 hres = stack_pop_val(ctx, &l);
937 if(SUCCEEDED(hres)) {
938 hres = VarSub(l.v, r.v, &v);
945 return stack_push(ctx, &v);
948 static HRESULT interp_mod(exec_ctx_t *ctx)
956 hres = stack_pop_val(ctx, &r);
960 hres = stack_pop_val(ctx, &l);
961 if(SUCCEEDED(hres)) {
962 hres = VarMod(l.v, r.v, &v);
969 return stack_push(ctx, &v);
972 static HRESULT interp_idiv(exec_ctx_t *ctx)
980 hres = stack_pop_val(ctx, &r);
984 hres = stack_pop_val(ctx, &l);
985 if(SUCCEEDED(hres)) {
986 hres = VarIdiv(l.v, r.v, &v);
993 return stack_push(ctx, &v);
996 static HRESULT interp_div(exec_ctx_t *ctx)
1004 hres = stack_pop_val(ctx, &r);
1008 hres = stack_pop_val(ctx, &l);
1009 if(SUCCEEDED(hres)) {
1010 hres = VarDiv(l.v, r.v, &v);
1017 return stack_push(ctx, &v);
1020 static HRESULT interp_mul(exec_ctx_t *ctx)
1028 hres = stack_pop_val(ctx, &r);
1032 hres = stack_pop_val(ctx, &l);
1033 if(SUCCEEDED(hres)) {
1034 hres = VarMul(l.v, r.v, &v);
1041 return stack_push(ctx, &v);
1044 static HRESULT interp_exp(exec_ctx_t *ctx)
1052 hres = stack_pop_val(ctx, &r);
1056 hres = stack_pop_val(ctx, &l);
1057 if(SUCCEEDED(hres)) {
1058 hres = VarPow(l.v, r.v, &v);
1065 return stack_push(ctx, &v);
1068 static HRESULT interp_neg(exec_ctx_t *ctx)
1074 hres = stack_pop_val(ctx, &val);
1078 hres = VarNeg(val.v, &v);
1083 return stack_push(ctx, &v);
1086 static const instr_func_t op_funcs[] = {
1087 #define X(x,n,a,b) interp_ ## x,
1092 static const unsigned op_move[] = {
1093 #define X(x,n,a,b) n,
1098 static void release_exec(exec_ctx_t *ctx)
1102 VariantClear(&ctx->ret_val);
1105 IDispatch_Release(ctx->this_obj);
1108 for(i=0; i < ctx->func->arg_cnt; i++)
1109 VariantClear(ctx->args+i);
1113 for(i=0; i < ctx->func->var_cnt; i++)
1114 VariantClear(ctx->vars+i);
1117 heap_free(ctx->args);
1118 heap_free(ctx->vars);
1119 heap_free(ctx->stack);
1122 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1124 exec_ctx_t exec = {func->code_ctx};
1126 HRESULT hres = S_OK;
1128 exec.code = func->code_ctx;
1130 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1131 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1139 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1141 release_exec(&exec);
1142 return E_OUTOFMEMORY;
1145 for(i=0; i < func->arg_cnt; i++) {
1147 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1148 if(func->args[i].by_ref)
1151 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1153 hres = VariantCopy(exec.args+i, v);
1156 release_exec(&exec);
1165 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1167 release_exec(&exec);
1168 return E_OUTOFMEMORY;
1174 exec.stack_size = 16;
1176 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1178 release_exec(&exec);
1179 return E_OUTOFMEMORY;
1183 exec.this_obj = this_obj;
1184 else if (ctx->host_global)
1185 exec.this_obj = ctx->host_global;
1187 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1188 IDispatch_AddRef(exec.this_obj);
1190 exec.instr = exec.code->instrs + func->code_off;
1195 op = exec.instr->op;
1196 hres = op_funcs[op](&exec);
1198 FIXME("Failed %08x\n", hres);
1199 stack_popn(&exec, exec.top);
1203 exec.instr += op_move[op];
1207 if(func->type != FUNC_FUNCTION)
1208 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1210 if(SUCCEEDED(hres) && res) {
1211 *res = exec.ret_val;
1212 V_VT(&exec.ret_val) = VT_EMPTY;
1215 release_exec(&exec);