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);
39 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
62 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
68 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
69 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
70 hres = disp_get_id(item->disp, name, &id);
73 ref->u.d.disp = item->disp;
80 if(!ctx->func->code_ctx->option_explicit)
81 FIXME("create an attempt to set\n");
87 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
90 return ctx->stack + --ctx->top;
93 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
95 if(ctx->stack_size == ctx->top) {
98 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
101 return E_OUTOFMEMORY;
104 ctx->stack = new_stack;
105 ctx->stack_size *= 2;
108 ctx->stack[ctx->top++] = *v;
112 static void stack_popn(exec_ctx_t *ctx, unsigned n)
115 VariantClear(stack_pop(ctx));
118 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
122 var = stack_pop(ctx);
124 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
126 var = V_VARIANTREF(var);
131 if(V_VT(var) == VT_DISPATCH) {
132 FIXME("got dispatch - get its default value\n");
141 static inline void release_val(variant_val_t *v)
147 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
150 dp->rgdispidNamedArgs = NULL;
157 assert(ctx->top >= arg_cnt);
159 for(i=1; i*2 <= arg_cnt; i++) {
160 tmp = ctx->stack[ctx->top-i];
161 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
162 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
165 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
171 static HRESULT interp_icallv(exec_ctx_t *ctx)
173 BSTR identifier = ctx->instr->arg1.bstr;
174 const unsigned arg_cnt = ctx->instr->arg2.uint;
181 hres = lookup_identifier(ctx, identifier, &ref);
185 vbstack_to_dp(ctx, arg_cnt, &dp);
189 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, NULL);
194 FIXME("%s not found\n", debugstr_w(identifier));
195 return DISP_E_UNKNOWNNAME;
198 stack_popn(ctx, arg_cnt);
202 static HRESULT interp_ret(exec_ctx_t *ctx)
210 static HRESULT interp_bool(exec_ctx_t *ctx)
212 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
215 TRACE("%s\n", arg ? "true" : "false");
219 return stack_push(ctx, &v);
222 static HRESULT interp_string(exec_ctx_t *ctx)
229 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
231 return E_OUTOFMEMORY;
233 return stack_push(ctx, &v);
236 static HRESULT interp_not(exec_ctx_t *ctx)
244 hres = stack_pop_val(ctx, &val);
248 hres = VarNot(val.v, &v);
253 return stack_push(ctx, &v);
256 static HRESULT interp_equal(exec_ctx_t *ctx)
262 static const instr_func_t op_funcs[] = {
263 #define X(x,n,a,b) interp_ ## x,
268 static const unsigned op_move[] = {
269 #define X(x,n,a,b) n,
274 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
280 exec.stack_size = 16;
282 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
284 return E_OUTOFMEMORY;
286 exec.code = func->code_ctx;
287 exec.instr = exec.code->instrs + func->code_off;
293 hres = op_funcs[op](&exec);
295 FIXME("Failed %08x\n", hres);
296 stack_popn(&exec, exec.top);
300 exec.instr += op_move[op];
304 heap_free(exec.stack);