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 assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
335 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
341 VARIANT *v = ref.u.v;
343 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
351 hres = VariantCopy(v, val);
356 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
361 FIXME("functions not implemented\n");
364 FIXME("%s not found\n", debugstr_w(name));
367 return DISP_E_UNKNOWNNAME;
373 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
375 const BSTR arg = ctx->instr->arg1.bstr;
379 TRACE("%s\n", debugstr_w(arg));
381 hres = stack_pop_val(ctx, &v);
385 return assign_ident(ctx, arg, v.v, v.owned);
388 static HRESULT interp_assign_member(exec_ctx_t *ctx)
390 BSTR identifier = ctx->instr->arg1.bstr;
396 TRACE("%s\n", debugstr_w(identifier));
398 hres = stack_pop_disp(ctx, &obj);
407 hres = stack_pop_val(ctx, &val);
409 IDispatch_Release(obj);
413 hres = disp_get_id(obj, identifier, &id);
415 hres = disp_propput(ctx->script, obj, id, val.v);
418 IDispatch_Release(obj);
422 static HRESULT interp_jmp(exec_ctx_t *ctx)
424 const unsigned arg = ctx->instr->arg1.uint;
432 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
434 const unsigned arg = ctx->instr->arg1.uint;
440 hres = stack_pop_val(ctx, &val);
444 if(V_VT(val.v) != VT_BOOL) {
445 FIXME("unsupported for %s\n", debugstr_variant(val.v));
453 instr_jmp(ctx, ctx->instr->arg1.uint);
457 static HRESULT interp_ret(exec_ctx_t *ctx)
465 static HRESULT interp_bool(exec_ctx_t *ctx)
467 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
470 TRACE("%s\n", arg ? "true" : "false");
474 return stack_push(ctx, &v);
477 static HRESULT interp_string(exec_ctx_t *ctx)
484 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
486 return E_OUTOFMEMORY;
488 return stack_push(ctx, &v);
491 static HRESULT interp_long(exec_ctx_t *ctx)
493 const LONG arg = ctx->instr->arg1.lng;
500 return stack_push(ctx, &v);
503 static HRESULT interp_short(exec_ctx_t *ctx)
505 const LONG arg = ctx->instr->arg1.lng;
512 return stack_push(ctx, &v);
515 static HRESULT interp_double(exec_ctx_t *ctx)
517 const DOUBLE *arg = ctx->instr->arg1.dbl;
520 TRACE("%lf\n", *arg);
524 return stack_push(ctx, &v);
527 static HRESULT interp_empty(exec_ctx_t *ctx)
534 return stack_push(ctx, &v);
537 static HRESULT interp_null(exec_ctx_t *ctx)
544 return stack_push(ctx, &v);
547 static HRESULT interp_not(exec_ctx_t *ctx)
555 hres = stack_pop_val(ctx, &val);
559 hres = VarNot(val.v, &v);
564 return stack_push(ctx, &v);
567 static HRESULT cmp_oper(exec_ctx_t *ctx)
572 hres = stack_pop_val(ctx, &r);
576 hres = stack_pop_val(ctx, &l);
577 if(SUCCEEDED(hres)) {
578 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
579 FIXME("comparing nulls is not implemented\n");
582 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
591 static HRESULT interp_equal(exec_ctx_t *ctx)
598 hres = cmp_oper(ctx);
603 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
604 return stack_push(ctx, &v);
607 static HRESULT interp_nequal(exec_ctx_t *ctx)
614 hres = cmp_oper(ctx);
619 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
620 return stack_push(ctx, &v);
623 static HRESULT interp_concat(exec_ctx_t *ctx)
631 hres = stack_pop_val(ctx, &r);
635 hres = stack_pop_val(ctx, &l);
636 if(SUCCEEDED(hres)) {
637 hres = VarCat(l.v, r.v, &v);
644 return stack_push(ctx, &v);
647 static HRESULT interp_add(exec_ctx_t *ctx)
655 hres = stack_pop_val(ctx, &r);
659 hres = stack_pop_val(ctx, &l);
660 if(SUCCEEDED(hres)) {
661 hres = VarAdd(l.v, r.v, &v);
668 return stack_push(ctx, &v);
671 static HRESULT interp_sub(exec_ctx_t *ctx)
679 hres = stack_pop_val(ctx, &r);
683 hres = stack_pop_val(ctx, &l);
684 if(SUCCEEDED(hres)) {
685 hres = VarSub(l.v, r.v, &v);
692 return stack_push(ctx, &v);
695 static HRESULT interp_mod(exec_ctx_t *ctx)
703 hres = stack_pop_val(ctx, &r);
707 hres = stack_pop_val(ctx, &l);
708 if(SUCCEEDED(hres)) {
709 hres = VarMod(l.v, r.v, &v);
716 return stack_push(ctx, &v);
719 static HRESULT interp_idiv(exec_ctx_t *ctx)
727 hres = stack_pop_val(ctx, &r);
731 hres = stack_pop_val(ctx, &l);
732 if(SUCCEEDED(hres)) {
733 hres = VarIdiv(l.v, r.v, &v);
740 return stack_push(ctx, &v);
743 static HRESULT interp_div(exec_ctx_t *ctx)
751 hres = stack_pop_val(ctx, &r);
755 hres = stack_pop_val(ctx, &l);
756 if(SUCCEEDED(hres)) {
757 hres = VarDiv(l.v, r.v, &v);
764 return stack_push(ctx, &v);
767 static HRESULT interp_mul(exec_ctx_t *ctx)
775 hres = stack_pop_val(ctx, &r);
779 hres = stack_pop_val(ctx, &l);
780 if(SUCCEEDED(hres)) {
781 hres = VarMul(l.v, r.v, &v);
788 return stack_push(ctx, &v);
791 static HRESULT interp_exp(exec_ctx_t *ctx)
799 hres = stack_pop_val(ctx, &r);
803 hres = stack_pop_val(ctx, &l);
804 if(SUCCEEDED(hres)) {
805 hres = VarPow(l.v, r.v, &v);
812 return stack_push(ctx, &v);
815 static HRESULT interp_neg(exec_ctx_t *ctx)
821 hres = stack_pop_val(ctx, &val);
825 hres = VarNeg(val.v, &v);
830 return stack_push(ctx, &v);
833 static const instr_func_t op_funcs[] = {
834 #define X(x,n,a,b) interp_ ## x,
839 static const unsigned op_move[] = {
840 #define X(x,n,a,b) n,
845 static void release_exec(exec_ctx_t *ctx)
849 VariantClear(&ctx->ret_val);
852 for(i=0; i < ctx->func->arg_cnt; i++)
853 VariantClear(ctx->args+i);
857 for(i=0; i < ctx->func->var_cnt; i++)
858 VariantClear(ctx->vars+i);
861 heap_free(ctx->args);
862 heap_free(ctx->vars);
863 heap_free(ctx->stack);
866 HRESULT exec_script(script_ctx_t *ctx, function_t *func, DISPPARAMS *dp, VARIANT *res)
868 exec_ctx_t exec = {func->code_ctx};
872 exec.code = func->code_ctx;
874 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
875 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
883 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
886 return E_OUTOFMEMORY;
889 for(i=0; i < func->arg_cnt; i++) {
891 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
892 if(func->args[i].by_ref)
895 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
897 hres = VariantCopy(exec.args+i, v);
909 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
912 return E_OUTOFMEMORY;
918 exec.stack_size = 16;
920 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
923 return E_OUTOFMEMORY;
926 exec.instr = exec.code->instrs + func->code_off;
932 hres = op_funcs[op](&exec);
934 FIXME("Failed %08x\n", hres);
935 stack_popn(&exec, exec.top);
939 exec.instr += op_move[op];
943 if(func->type != FUNC_FUNCTION)
944 assert(V_VT(&exec.ret_val) == VT_EMPTY);
946 if(SUCCEEDED(hres) && res) {
948 V_VT(&exec.ret_val) = VT_EMPTY;