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);
38 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
55 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
61 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
62 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
63 hres = disp_get_id(item->disp, name, &id);
66 ref->u.d.disp = item->disp;
73 FIXME("create if no option explicit\n");
79 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
82 return ctx->stack + --ctx->top;
85 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
87 if(ctx->stack_size == ctx->top) {
90 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
96 ctx->stack = new_stack;
100 ctx->stack[ctx->top++] = *v;
104 static void stack_popn(exec_ctx_t *ctx, unsigned n)
107 VariantClear(stack_pop(ctx));
110 static HRESULT interp_icallv(exec_ctx_t *ctx)
112 BSTR identifier = ctx->instr->arg1.bstr;
113 const unsigned arg_cnt = ctx->instr->arg2.uint;
121 FIXME("arguments not implemented\n");
125 hres = lookup_identifier(ctx, identifier, &ref);
131 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, NULL);
136 FIXME("%s not found\n", debugstr_w(identifier));
137 return DISP_E_UNKNOWNNAME;
143 static HRESULT interp_ret(exec_ctx_t *ctx)
151 static HRESULT interp_bool(exec_ctx_t *ctx)
153 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
156 TRACE("%s\n", arg ? "true" : "false");
160 return stack_push(ctx, &v);
163 static HRESULT interp_string(exec_ctx_t *ctx)
170 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
172 return E_OUTOFMEMORY;
174 return stack_push(ctx, &v);
177 static const instr_func_t op_funcs[] = {
178 #define X(x,n,a,b) interp_ ## x,
183 static const unsigned op_move[] = {
184 #define X(x,n,a,b) n,
189 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
195 exec.stack_size = 16;
197 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
199 return E_OUTOFMEMORY;
201 exec.code = func->code_ctx;
202 exec.instr = exec.code->instrs + func->code_off;
207 hres = op_funcs[op](&exec);
209 FIXME("Failed %08x\n", hres);
210 stack_popn(&exec, exec.top);
214 exec.instr += op_move[op];
218 heap_free(exec.stack);