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);
42 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
69 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
72 if(!strcmpiW(var->name, name)) {
84 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
92 for(i=0; i < ctx->func->var_cnt; i++) {
93 if(!strcmpiW(ctx->func->vars[i].name, name)) {
95 ref->u.v = ctx->vars+i;
100 for(i=0; i < ctx->func->arg_cnt; i++) {
101 if(!strcmpiW(ctx->func->args[i].name, name)) {
103 ref->u.v = ctx->args+i;
108 if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
111 for(func = ctx->script->global_funcs; func; func = func->next) {
112 if(!strcmpiW(func->name, name)) {
113 ref->type = REF_FUNC;
119 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
120 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
121 hres = disp_get_id(item->disp, name, &id);
122 if(SUCCEEDED(hres)) {
123 ref->type = REF_DISP;
124 ref->u.d.disp = item->disp;
131 if(!ctx->func->code_ctx->option_explicit)
132 FIXME("create an attempt to set\n");
134 ref->type = REF_NONE;
138 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
141 return ctx->stack + --ctx->top;
144 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
146 if(ctx->stack_size == ctx->top) {
149 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
152 return E_OUTOFMEMORY;
155 ctx->stack = new_stack;
156 ctx->stack_size *= 2;
159 ctx->stack[ctx->top++] = *v;
163 static void stack_popn(exec_ctx_t *ctx, unsigned n)
166 VariantClear(stack_pop(ctx));
169 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
173 var = stack_pop(ctx);
175 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
177 var = V_VARIANTREF(var);
182 if(V_VT(var) == VT_DISPATCH) {
183 FIXME("got dispatch - get its default value\n");
192 static inline void release_val(variant_val_t *v)
198 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
200 VARIANT *v = stack_pop(ctx);
202 if(V_VT(v) == VT_DISPATCH) {
203 *ret = V_DISPATCH(v);
207 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
208 FIXME("not supported type: %s\n", debugstr_variant(v));
214 if(V_VT(v) != VT_DISPATCH) {
215 FIXME("not disp %s\n", debugstr_variant(v));
220 IDispatch_AddRef(V_DISPATCH(v));
221 *ret = V_DISPATCH(v);
225 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
227 ctx->instr = ctx->code->instrs + addr;
230 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
233 dp->rgdispidNamedArgs = NULL;
240 assert(ctx->top >= arg_cnt);
242 for(i=1; i*2 <= arg_cnt; i++) {
243 tmp = ctx->stack[ctx->top-i];
244 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
245 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
248 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
254 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
256 BSTR identifier = ctx->instr->arg1.bstr;
257 const unsigned arg_cnt = ctx->instr->arg2.uint;
262 hres = lookup_identifier(ctx, identifier, &ref);
266 vbstack_to_dp(ctx, arg_cnt, &dp);
271 FIXME("REF_VAR no res\n");
276 FIXME("arguments not implemented\n");
280 V_VT(res) = VT_BYREF|VT_VARIANT;
281 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
284 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
289 hres = exec_script(ctx->script, ref.u.f, &dp, res);
294 FIXME("%s not found\n", debugstr_w(identifier));
295 return DISP_E_UNKNOWNNAME;
298 stack_popn(ctx, arg_cnt);
302 static HRESULT interp_icall(exec_ctx_t *ctx)
309 hres = do_icall(ctx, &v);
313 return stack_push(ctx, &v);
316 static HRESULT interp_icallv(exec_ctx_t *ctx)
319 return do_icall(ctx, NULL);
322 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
327 hres = lookup_identifier(ctx, name, &ref);
333 VARIANT *v = ref.u.v;
335 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
343 hres = VariantCopy(v, val);
348 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
353 FIXME("functions not implemented\n");
356 FIXME("%s not found\n", debugstr_w(name));
359 return DISP_E_UNKNOWNNAME;
365 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
367 const BSTR arg = ctx->instr->arg1.bstr;
371 TRACE("%s\n", debugstr_w(arg));
373 hres = stack_pop_val(ctx, &v);
377 return assign_ident(ctx, arg, v.v, v.owned);
380 static HRESULT interp_assign_member(exec_ctx_t *ctx)
382 BSTR identifier = ctx->instr->arg1.bstr;
388 TRACE("%s\n", debugstr_w(identifier));
390 hres = stack_pop_disp(ctx, &obj);
399 hres = stack_pop_val(ctx, &val);
401 IDispatch_Release(obj);
405 hres = disp_get_id(obj, identifier, &id);
407 hres = disp_propput(ctx->script, obj, id, val.v);
410 IDispatch_Release(obj);
414 static HRESULT interp_jmp(exec_ctx_t *ctx)
416 const unsigned arg = ctx->instr->arg1.uint;
424 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
426 const unsigned arg = ctx->instr->arg1.uint;
432 hres = stack_pop_val(ctx, &val);
436 if(V_VT(val.v) != VT_BOOL) {
437 FIXME("unsupported for %s\n", debugstr_variant(val.v));
445 instr_jmp(ctx, ctx->instr->arg1.uint);
449 static HRESULT interp_ret(exec_ctx_t *ctx)
457 static HRESULT interp_bool(exec_ctx_t *ctx)
459 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
462 TRACE("%s\n", arg ? "true" : "false");
466 return stack_push(ctx, &v);
469 static HRESULT interp_string(exec_ctx_t *ctx)
476 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
478 return E_OUTOFMEMORY;
480 return stack_push(ctx, &v);
483 static HRESULT interp_long(exec_ctx_t *ctx)
485 const LONG arg = ctx->instr->arg1.lng;
492 return stack_push(ctx, &v);
495 static HRESULT interp_short(exec_ctx_t *ctx)
497 const LONG arg = ctx->instr->arg1.lng;
504 return stack_push(ctx, &v);
507 static HRESULT interp_double(exec_ctx_t *ctx)
509 const DOUBLE *arg = ctx->instr->arg1.dbl;
512 TRACE("%lf\n", *arg);
516 return stack_push(ctx, &v);
519 static HRESULT interp_empty(exec_ctx_t *ctx)
526 return stack_push(ctx, &v);
529 static HRESULT interp_null(exec_ctx_t *ctx)
536 return stack_push(ctx, &v);
539 static HRESULT interp_not(exec_ctx_t *ctx)
547 hres = stack_pop_val(ctx, &val);
551 hres = VarNot(val.v, &v);
556 return stack_push(ctx, &v);
559 static HRESULT cmp_oper(exec_ctx_t *ctx)
564 hres = stack_pop_val(ctx, &r);
568 hres = stack_pop_val(ctx, &l);
569 if(SUCCEEDED(hres)) {
570 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
571 FIXME("comparing nulls is not implemented\n");
574 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
583 static HRESULT interp_equal(exec_ctx_t *ctx)
590 hres = cmp_oper(ctx);
595 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
596 return stack_push(ctx, &v);
599 static HRESULT interp_nequal(exec_ctx_t *ctx)
606 hres = cmp_oper(ctx);
611 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
612 return stack_push(ctx, &v);
615 static HRESULT interp_concat(exec_ctx_t *ctx)
623 hres = stack_pop_val(ctx, &r);
627 hres = stack_pop_val(ctx, &l);
628 if(SUCCEEDED(hres)) {
629 hres = VarCat(l.v, r.v, &v);
636 return stack_push(ctx, &v);
639 static HRESULT interp_add(exec_ctx_t *ctx)
647 hres = stack_pop_val(ctx, &r);
651 hres = stack_pop_val(ctx, &l);
652 if(SUCCEEDED(hres)) {
653 hres = VarAdd(l.v, r.v, &v);
660 return stack_push(ctx, &v);
663 static HRESULT interp_sub(exec_ctx_t *ctx)
671 hres = stack_pop_val(ctx, &r);
675 hres = stack_pop_val(ctx, &l);
676 if(SUCCEEDED(hres)) {
677 hres = VarSub(l.v, r.v, &v);
684 return stack_push(ctx, &v);
687 static HRESULT interp_mod(exec_ctx_t *ctx)
695 hres = stack_pop_val(ctx, &r);
699 hres = stack_pop_val(ctx, &l);
700 if(SUCCEEDED(hres)) {
701 hres = VarMod(l.v, r.v, &v);
708 return stack_push(ctx, &v);
711 static HRESULT interp_idiv(exec_ctx_t *ctx)
719 hres = stack_pop_val(ctx, &r);
723 hres = stack_pop_val(ctx, &l);
724 if(SUCCEEDED(hres)) {
725 hres = VarIdiv(l.v, r.v, &v);
732 return stack_push(ctx, &v);
735 static HRESULT interp_div(exec_ctx_t *ctx)
743 hres = stack_pop_val(ctx, &r);
747 hres = stack_pop_val(ctx, &l);
748 if(SUCCEEDED(hres)) {
749 hres = VarDiv(l.v, r.v, &v);
756 return stack_push(ctx, &v);
759 static HRESULT interp_mul(exec_ctx_t *ctx)
767 hres = stack_pop_val(ctx, &r);
771 hres = stack_pop_val(ctx, &l);
772 if(SUCCEEDED(hres)) {
773 hres = VarMul(l.v, r.v, &v);
780 return stack_push(ctx, &v);
783 static HRESULT interp_exp(exec_ctx_t *ctx)
791 hres = stack_pop_val(ctx, &r);
795 hres = stack_pop_val(ctx, &l);
796 if(SUCCEEDED(hres)) {
797 hres = VarPow(l.v, r.v, &v);
804 return stack_push(ctx, &v);
807 static HRESULT interp_neg(exec_ctx_t *ctx)
813 hres = stack_pop_val(ctx, &val);
817 hres = VarNeg(val.v, &v);
822 return stack_push(ctx, &v);
825 static const instr_func_t op_funcs[] = {
826 #define X(x,n,a,b) interp_ ## x,
831 static const unsigned op_move[] = {
832 #define X(x,n,a,b) n,
837 static void release_exec(exec_ctx_t *ctx)
842 for(i=0; i < ctx->func->arg_cnt; i++)
843 VariantClear(ctx->args+i);
847 for(i=0; i < ctx->func->var_cnt; i++)
848 VariantClear(ctx->vars+i);
851 heap_free(ctx->args);
852 heap_free(ctx->vars);
853 heap_free(ctx->stack);
856 HRESULT exec_script(script_ctx_t *ctx, function_t *func, DISPPARAMS *dp, VARIANT *res)
858 exec_ctx_t exec = {func->code_ctx};
863 FIXME("returning value is not implemented\n");
867 exec.code = func->code_ctx;
869 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
870 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
878 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
881 return E_OUTOFMEMORY;
884 for(i=0; i < func->arg_cnt; i++) {
886 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
887 if(func->args[i].by_ref)
890 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
892 hres = VariantCopy(exec.args+i, v);
904 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
907 return E_OUTOFMEMORY;
913 exec.stack_size = 16;
915 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
918 return E_OUTOFMEMORY;
921 exec.instr = exec.code->instrs + func->code_off;
927 hres = op_funcs[op](&exec);
929 FIXME("Failed %08x\n", hres);
930 stack_popn(&exec, exec.top);
934 exec.instr += op_move[op];