vbscript: Added support for arguments in interp_icallv.
[wine] / dlls / vbscript / interp.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <assert.h>
20
21 #include "vbscript.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
26
27
28 typedef struct {
29     vbscode_t *code;
30     instr_t *instr;
31     script_ctx_t *script;
32
33     unsigned stack_size;
34     unsigned top;
35     VARIANT *stack;
36 } exec_ctx_t;
37
38 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
39
40 typedef enum {
41     REF_NONE,
42     REF_DISP
43 } ref_type_t;
44
45 typedef struct {
46     ref_type_t type;
47     union {
48         struct {
49             IDispatch *disp;
50             DISPID id;
51         } d;
52     } u;
53 } ref_t;
54
55 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
56 {
57     named_item_t *item;
58     DISPID id;
59     HRESULT hres;
60
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);
64             if(SUCCEEDED(hres)) {
65                 ref->type = REF_DISP;
66                 ref->u.d.disp = item->disp;
67                 ref->u.d.id = id;
68                 return S_OK;
69             }
70         }
71     }
72
73     FIXME("create if no option explicit\n");
74
75     ref->type = REF_NONE;
76     return S_OK;
77 }
78
79 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
80 {
81     assert(ctx->top);
82     return ctx->stack + --ctx->top;
83 }
84
85 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
86 {
87     if(ctx->stack_size == ctx->top) {
88         VARIANT *new_stack;
89
90         new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
91         if(!new_stack) {
92             VariantClear(v);
93             return E_OUTOFMEMORY;
94         }
95
96         ctx->stack = new_stack;
97         ctx->stack_size *= 2;
98     }
99
100     ctx->stack[ctx->top++] = *v;
101     return S_OK;
102 }
103
104 static void stack_popn(exec_ctx_t *ctx, unsigned n)
105 {
106     while(n--)
107         VariantClear(stack_pop(ctx));
108 }
109
110 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
111 {
112     dp->cArgs = arg_cnt;
113     dp->rgdispidNamedArgs = NULL;
114     dp->cNamedArgs = 0;
115
116     if(arg_cnt) {
117         VARIANT tmp;
118         unsigned i;
119
120         assert(ctx->top >= arg_cnt);
121
122         for(i=1; i*2 <= arg_cnt; i++) {
123             tmp = ctx->stack[ctx->top-i];
124             ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
125             ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
126         }
127
128         dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
129     }else {
130         dp->rgvarg = NULL;
131     }
132 }
133
134 static HRESULT interp_icallv(exec_ctx_t *ctx)
135 {
136     BSTR identifier = ctx->instr->arg1.bstr;
137     const unsigned arg_cnt = ctx->instr->arg2.uint;
138     ref_t ref = {0};
139     DISPPARAMS dp;
140     HRESULT hres;
141
142     TRACE("\n");
143
144     hres = lookup_identifier(ctx, identifier, &ref);
145     if(FAILED(hres))
146         return hres;
147
148     vbstack_to_dp(ctx, arg_cnt, &dp);
149
150     switch(ref.type) {
151     case REF_DISP:
152         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, NULL);
153         if(FAILED(hres))
154             return hres;
155         break;
156     default:
157         FIXME("%s not found\n", debugstr_w(identifier));
158         return DISP_E_UNKNOWNNAME;
159     }
160
161     stack_popn(ctx, arg_cnt);
162     return S_OK;
163 }
164
165 static HRESULT interp_ret(exec_ctx_t *ctx)
166 {
167     TRACE("\n");
168
169     ctx->instr = NULL;
170     return S_OK;
171 }
172
173 static HRESULT interp_bool(exec_ctx_t *ctx)
174 {
175     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
176     VARIANT v;
177
178     TRACE("%s\n", arg ? "true" : "false");
179
180     V_VT(&v) = VT_BOOL;
181     V_BOOL(&v) = arg;
182     return stack_push(ctx, &v);
183 }
184
185 static HRESULT interp_string(exec_ctx_t *ctx)
186 {
187     VARIANT v;
188
189     TRACE("\n");
190
191     V_VT(&v) = VT_BSTR;
192     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
193     if(!V_BSTR(&v))
194         return E_OUTOFMEMORY;
195
196     return stack_push(ctx, &v);
197 }
198
199 static const instr_func_t op_funcs[] = {
200 #define X(x,n,a,b) interp_ ## x,
201 OP_LIST
202 #undef X
203 };
204
205 static const unsigned op_move[] = {
206 #define X(x,n,a,b) n,
207 OP_LIST
208 #undef X
209 };
210
211 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
212 {
213     exec_ctx_t exec;
214     vbsop_t op;
215     HRESULT hres = S_OK;
216
217     exec.stack_size = 16;
218     exec.top = 0;
219     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
220     if(!exec.stack)
221         return E_OUTOFMEMORY;
222
223     exec.code = func->code_ctx;
224     exec.instr = exec.code->instrs + func->code_off;
225     exec.script = ctx;
226
227     while(exec.instr) {
228         op = exec.instr->op;
229         hres = op_funcs[op](&exec);
230         if(FAILED(hres)) {
231             FIXME("Failed %08x\n", hres);
232             stack_popn(&exec, exec.top);
233             break;
234         }
235
236         exec.instr += op_move[op];
237     }
238
239     assert(!exec.top);
240     heap_free(exec.stack);
241
242     return hres;
243 }