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);
44 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
71 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
74 if(!strcmpiW(var->name, name)) {
86 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
94 if(invoke_type == VBDISP_LET && ctx->func->type == FUNC_FUNCTION && !strcmpiW(name, ctx->func->name)) {
96 ref->u.v = &ctx->ret_val;
100 for(i=0; i < ctx->func->var_cnt; i++) {
101 if(!strcmpiW(ctx->func->vars[i].name, name)) {
103 ref->u.v = ctx->vars+i;
108 for(i=0; i < ctx->func->arg_cnt; i++) {
109 if(!strcmpiW(ctx->func->args[i].name, name)) {
111 ref->u.v = ctx->args+i;
116 if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
119 for(func = ctx->script->global_funcs; func; func = func->next) {
120 if(!strcmpiW(func->name, name)) {
121 ref->type = REF_FUNC;
127 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
128 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
129 hres = disp_get_id(item->disp, name, &id);
130 if(SUCCEEDED(hres)) {
131 ref->type = REF_DISP;
132 ref->u.d.disp = item->disp;
139 if(!ctx->func->code_ctx->option_explicit)
140 FIXME("create an attempt to set\n");
142 ref->type = REF_NONE;
146 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
149 return ctx->stack + --ctx->top;
152 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
154 if(ctx->stack_size == ctx->top) {
157 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
160 return E_OUTOFMEMORY;
163 ctx->stack = new_stack;
164 ctx->stack_size *= 2;
167 ctx->stack[ctx->top++] = *v;
171 static void stack_popn(exec_ctx_t *ctx, unsigned n)
174 VariantClear(stack_pop(ctx));
177 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
181 var = stack_pop(ctx);
183 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
185 var = V_VARIANTREF(var);
190 if(V_VT(var) == VT_DISPATCH) {
191 FIXME("got dispatch - get its default value\n");
200 static inline void release_val(variant_val_t *v)
206 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
208 VARIANT *v = stack_pop(ctx);
210 if(V_VT(v) == VT_DISPATCH) {
211 *ret = V_DISPATCH(v);
215 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
216 FIXME("not supported type: %s\n", debugstr_variant(v));
222 if(V_VT(v) != VT_DISPATCH) {
223 FIXME("not disp %s\n", debugstr_variant(v));
228 IDispatch_AddRef(V_DISPATCH(v));
229 *ret = V_DISPATCH(v);
233 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
235 ctx->instr = ctx->code->instrs + addr;
238 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
241 dp->rgdispidNamedArgs = NULL;
248 assert(ctx->top >= arg_cnt);
250 for(i=1; i*2 <= arg_cnt; i++) {
251 tmp = ctx->stack[ctx->top-i];
252 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
253 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
256 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
262 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
264 BSTR identifier = ctx->instr->arg1.bstr;
265 const unsigned arg_cnt = ctx->instr->arg2.uint;
270 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
274 vbstack_to_dp(ctx, arg_cnt, &dp);
279 FIXME("REF_VAR no res\n");
284 FIXME("arguments not implemented\n");
288 V_VT(res) = VT_BYREF|VT_VARIANT;
289 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
292 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
297 hres = exec_script(ctx->script, ref.u.f, &dp, res);
302 FIXME("%s not found\n", debugstr_w(identifier));
303 return DISP_E_UNKNOWNNAME;
306 stack_popn(ctx, arg_cnt);
310 static HRESULT interp_icall(exec_ctx_t *ctx)
317 hres = do_icall(ctx, &v);
321 return stack_push(ctx, &v);
324 static HRESULT interp_icallv(exec_ctx_t *ctx)
327 return do_icall(ctx, NULL);
330 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
332 const BSTR identifier = ctx->instr->arg1.bstr;
333 const unsigned arg_cnt = ctx->instr->arg2.uint;
339 hres = stack_pop_disp(ctx, &obj);
348 vbstack_to_dp(ctx, arg_cnt, &dp);
350 hres = disp_get_id(obj, identifier, &id);
352 hres = disp_call(ctx->script, obj, id, &dp, res);
353 IDispatch_Release(obj);
357 stack_popn(ctx, arg_cnt);
361 static HRESULT interp_mcall(exec_ctx_t *ctx)
368 hres = do_mcall(ctx, &res);
372 return stack_push(ctx, &res);
375 static HRESULT interp_mcallv(exec_ctx_t *ctx)
379 return do_mcall(ctx, NULL);
382 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
387 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
393 VARIANT *v = ref.u.v;
395 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
403 hres = VariantCopy(v, val);
408 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
413 FIXME("functions not implemented\n");
416 FIXME("%s not found\n", debugstr_w(name));
419 return DISP_E_UNKNOWNNAME;
425 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
427 const BSTR arg = ctx->instr->arg1.bstr;
431 TRACE("%s\n", debugstr_w(arg));
433 hres = stack_pop_val(ctx, &v);
437 return assign_ident(ctx, arg, v.v, v.owned);
440 static HRESULT interp_set_ident(exec_ctx_t *ctx)
442 const BSTR arg = ctx->instr->arg1.bstr;
447 TRACE("%s\n", debugstr_w(arg));
449 hres = stack_pop_disp(ctx, &disp);
453 V_VT(&v) = VT_DISPATCH;
454 V_DISPATCH(&v) = disp;
455 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
458 static HRESULT interp_assign_member(exec_ctx_t *ctx)
460 BSTR identifier = ctx->instr->arg1.bstr;
466 TRACE("%s\n", debugstr_w(identifier));
468 hres = stack_pop_disp(ctx, &obj);
477 hres = stack_pop_val(ctx, &val);
479 IDispatch_Release(obj);
483 hres = disp_get_id(obj, identifier, &id);
485 hres = disp_propput(ctx->script, obj, id, val.v);
488 IDispatch_Release(obj);
492 static HRESULT interp_set_member(exec_ctx_t *ctx)
494 BSTR identifier = ctx->instr->arg1.bstr;
495 FIXME("%s\n", debugstr_w(identifier));
499 static HRESULT interp_new(exec_ctx_t *ctx)
501 const WCHAR *arg = ctx->instr->arg1.bstr;
502 class_desc_t *class_desc;
507 TRACE("%s\n", debugstr_w(arg));
509 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
510 if(!strcmpiW(class_desc->name, arg))
514 FIXME("Class %s not found\n", debugstr_w(arg));
518 hres = create_vbdisp(class_desc, &obj);
522 V_VT(&v) = VT_DISPATCH;
523 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
524 return stack_push(ctx, &v);
527 static HRESULT interp_jmp(exec_ctx_t *ctx)
529 const unsigned arg = ctx->instr->arg1.uint;
537 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
539 const unsigned arg = ctx->instr->arg1.uint;
545 hres = stack_pop_val(ctx, &val);
549 if(V_VT(val.v) != VT_BOOL) {
550 FIXME("unsupported for %s\n", debugstr_variant(val.v));
558 instr_jmp(ctx, ctx->instr->arg1.uint);
562 static HRESULT interp_ret(exec_ctx_t *ctx)
570 static HRESULT interp_bool(exec_ctx_t *ctx)
572 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
575 TRACE("%s\n", arg ? "true" : "false");
579 return stack_push(ctx, &v);
582 static HRESULT interp_string(exec_ctx_t *ctx)
589 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
591 return E_OUTOFMEMORY;
593 return stack_push(ctx, &v);
596 static HRESULT interp_long(exec_ctx_t *ctx)
598 const LONG arg = ctx->instr->arg1.lng;
605 return stack_push(ctx, &v);
608 static HRESULT interp_short(exec_ctx_t *ctx)
610 const LONG arg = ctx->instr->arg1.lng;
617 return stack_push(ctx, &v);
620 static HRESULT interp_double(exec_ctx_t *ctx)
622 const DOUBLE *arg = ctx->instr->arg1.dbl;
625 TRACE("%lf\n", *arg);
629 return stack_push(ctx, &v);
632 static HRESULT interp_empty(exec_ctx_t *ctx)
639 return stack_push(ctx, &v);
642 static HRESULT interp_null(exec_ctx_t *ctx)
649 return stack_push(ctx, &v);
652 static HRESULT interp_nothing(exec_ctx_t *ctx)
658 V_VT(&v) = VT_DISPATCH;
659 V_DISPATCH(&v) = NULL;
660 return stack_push(ctx, &v);
663 static HRESULT interp_not(exec_ctx_t *ctx)
671 hres = stack_pop_val(ctx, &val);
675 hres = VarNot(val.v, &v);
680 return stack_push(ctx, &v);
683 static HRESULT interp_and(exec_ctx_t *ctx)
691 hres = stack_pop_val(ctx, &r);
695 hres = stack_pop_val(ctx, &l);
696 if(SUCCEEDED(hres)) {
697 hres = VarAnd(l.v, r.v, &v);
704 return stack_push(ctx, &v);
707 static HRESULT interp_or(exec_ctx_t *ctx)
715 hres = stack_pop_val(ctx, &r);
719 hres = stack_pop_val(ctx, &l);
720 if(SUCCEEDED(hres)) {
721 hres = VarOr(l.v, r.v, &v);
728 return stack_push(ctx, &v);
731 static HRESULT interp_xor(exec_ctx_t *ctx)
739 hres = stack_pop_val(ctx, &r);
743 hres = stack_pop_val(ctx, &l);
744 if(SUCCEEDED(hres)) {
745 hres = VarXor(l.v, r.v, &v);
752 return stack_push(ctx, &v);
755 static HRESULT interp_eqv(exec_ctx_t *ctx)
763 hres = stack_pop_val(ctx, &r);
767 hres = stack_pop_val(ctx, &l);
768 if(SUCCEEDED(hres)) {
769 hres = VarEqv(l.v, r.v, &v);
776 return stack_push(ctx, &v);
779 static HRESULT interp_imp(exec_ctx_t *ctx)
787 hres = stack_pop_val(ctx, &r);
791 hres = stack_pop_val(ctx, &l);
792 if(SUCCEEDED(hres)) {
793 hres = VarImp(l.v, r.v, &v);
800 return stack_push(ctx, &v);
803 static HRESULT cmp_oper(exec_ctx_t *ctx)
808 hres = stack_pop_val(ctx, &r);
812 hres = stack_pop_val(ctx, &l);
813 if(SUCCEEDED(hres)) {
814 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
815 FIXME("comparing nulls is not implemented\n");
818 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
827 static HRESULT interp_equal(exec_ctx_t *ctx)
834 hres = cmp_oper(ctx);
839 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
840 return stack_push(ctx, &v);
843 static HRESULT interp_nequal(exec_ctx_t *ctx)
850 hres = cmp_oper(ctx);
855 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
856 return stack_push(ctx, &v);
859 static HRESULT interp_concat(exec_ctx_t *ctx)
867 hres = stack_pop_val(ctx, &r);
871 hres = stack_pop_val(ctx, &l);
872 if(SUCCEEDED(hres)) {
873 hres = VarCat(l.v, r.v, &v);
880 return stack_push(ctx, &v);
883 static HRESULT interp_add(exec_ctx_t *ctx)
891 hres = stack_pop_val(ctx, &r);
895 hres = stack_pop_val(ctx, &l);
896 if(SUCCEEDED(hres)) {
897 hres = VarAdd(l.v, r.v, &v);
904 return stack_push(ctx, &v);
907 static HRESULT interp_sub(exec_ctx_t *ctx)
915 hres = stack_pop_val(ctx, &r);
919 hres = stack_pop_val(ctx, &l);
920 if(SUCCEEDED(hres)) {
921 hres = VarSub(l.v, r.v, &v);
928 return stack_push(ctx, &v);
931 static HRESULT interp_mod(exec_ctx_t *ctx)
939 hres = stack_pop_val(ctx, &r);
943 hres = stack_pop_val(ctx, &l);
944 if(SUCCEEDED(hres)) {
945 hres = VarMod(l.v, r.v, &v);
952 return stack_push(ctx, &v);
955 static HRESULT interp_idiv(exec_ctx_t *ctx)
963 hres = stack_pop_val(ctx, &r);
967 hres = stack_pop_val(ctx, &l);
968 if(SUCCEEDED(hres)) {
969 hres = VarIdiv(l.v, r.v, &v);
976 return stack_push(ctx, &v);
979 static HRESULT interp_div(exec_ctx_t *ctx)
987 hres = stack_pop_val(ctx, &r);
991 hres = stack_pop_val(ctx, &l);
992 if(SUCCEEDED(hres)) {
993 hres = VarDiv(l.v, r.v, &v);
1000 return stack_push(ctx, &v);
1003 static HRESULT interp_mul(exec_ctx_t *ctx)
1011 hres = stack_pop_val(ctx, &r);
1015 hres = stack_pop_val(ctx, &l);
1016 if(SUCCEEDED(hres)) {
1017 hres = VarMul(l.v, r.v, &v);
1024 return stack_push(ctx, &v);
1027 static HRESULT interp_exp(exec_ctx_t *ctx)
1035 hres = stack_pop_val(ctx, &r);
1039 hres = stack_pop_val(ctx, &l);
1040 if(SUCCEEDED(hres)) {
1041 hres = VarPow(l.v, r.v, &v);
1048 return stack_push(ctx, &v);
1051 static HRESULT interp_neg(exec_ctx_t *ctx)
1057 hres = stack_pop_val(ctx, &val);
1061 hres = VarNeg(val.v, &v);
1066 return stack_push(ctx, &v);
1069 static const instr_func_t op_funcs[] = {
1070 #define X(x,n,a,b) interp_ ## x,
1075 static const unsigned op_move[] = {
1076 #define X(x,n,a,b) n,
1081 static void release_exec(exec_ctx_t *ctx)
1085 VariantClear(&ctx->ret_val);
1088 for(i=0; i < ctx->func->arg_cnt; i++)
1089 VariantClear(ctx->args+i);
1093 for(i=0; i < ctx->func->var_cnt; i++)
1094 VariantClear(ctx->vars+i);
1097 heap_free(ctx->args);
1098 heap_free(ctx->vars);
1099 heap_free(ctx->stack);
1102 HRESULT exec_script(script_ctx_t *ctx, function_t *func, DISPPARAMS *dp, VARIANT *res)
1104 exec_ctx_t exec = {func->code_ctx};
1106 HRESULT hres = S_OK;
1108 exec.code = func->code_ctx;
1110 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1111 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1119 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1121 release_exec(&exec);
1122 return E_OUTOFMEMORY;
1125 for(i=0; i < func->arg_cnt; i++) {
1127 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1128 if(func->args[i].by_ref)
1131 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1133 hres = VariantCopy(exec.args+i, v);
1136 release_exec(&exec);
1145 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1147 release_exec(&exec);
1148 return E_OUTOFMEMORY;
1154 exec.stack_size = 16;
1156 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1158 release_exec(&exec);
1159 return E_OUTOFMEMORY;
1162 exec.instr = exec.code->instrs + func->code_off;
1167 op = exec.instr->op;
1168 hres = op_funcs[op](&exec);
1170 FIXME("Failed %08x\n", hres);
1171 stack_popn(&exec, exec.top);
1175 exec.instr += op_move[op];
1179 if(func->type != FUNC_FUNCTION)
1180 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1182 if(SUCCEEDED(hres) && res) {
1183 *res = exec.ret_val;
1184 V_VT(&exec.ret_val) = VT_EMPTY;
1187 release_exec(&exec);