vbscript: Added interp_bool implementation.
[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 HRESULT interp_icallv(exec_ctx_t *ctx)
111 {
112     BSTR identifier = ctx->instr->arg1.bstr;
113     const unsigned arg_cnt = ctx->instr->arg2.uint;
114     DISPPARAMS dp = {0};
115     ref_t ref;
116     HRESULT hres;
117
118     TRACE("\n");
119
120     if(arg_cnt) {
121         FIXME("arguments not implemented\n");
122         return E_NOTIMPL;
123     }
124
125     hres = lookup_identifier(ctx, identifier, &ref);
126     if(FAILED(hres))
127         return hres;
128
129     switch(ref.type) {
130     case REF_DISP:
131         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, NULL);
132         if(FAILED(hres))
133             return hres;
134         break;
135     default:
136         FIXME("%s not found\n", debugstr_w(identifier));
137         return DISP_E_UNKNOWNNAME;
138     }
139
140     return S_OK;
141 }
142
143 static HRESULT interp_ret(exec_ctx_t *ctx)
144 {
145     TRACE("\n");
146
147     ctx->instr = NULL;
148     return S_OK;
149 }
150
151 static HRESULT interp_bool(exec_ctx_t *ctx)
152 {
153     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
154     VARIANT v;
155
156     TRACE("%s\n", arg ? "true" : "false");
157
158     V_VT(&v) = VT_BOOL;
159     V_BOOL(&v) = arg;
160     return stack_push(ctx, &v);
161 }
162
163 static HRESULT interp_string(exec_ctx_t *ctx)
164 {
165     FIXME("\n");
166     return E_NOTIMPL;
167 }
168
169 static const instr_func_t op_funcs[] = {
170 #define X(x,n,a,b) interp_ ## x,
171 OP_LIST
172 #undef X
173 };
174
175 static const unsigned op_move[] = {
176 #define X(x,n,a,b) n,
177 OP_LIST
178 #undef X
179 };
180
181 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
182 {
183     exec_ctx_t exec;
184     vbsop_t op;
185     HRESULT hres = S_OK;
186
187     exec.stack_size = 16;
188     exec.top = 0;
189     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
190     if(!exec.stack)
191         return E_OUTOFMEMORY;
192
193     exec.code = func->code_ctx;
194     exec.instr = exec.code->instrs + func->code_off;
195     exec.script = ctx;
196
197     while(exec.instr) {
198         op = exec.instr->op;
199         hres = op_funcs[op](&exec);
200         if(FAILED(hres)) {
201             FIXME("Failed %08x\n", hres);
202             stack_popn(&exec, exec.top);
203             break;
204         }
205
206         exec.instr += op_move[op];
207     }
208
209     assert(!exec.top);
210     heap_free(exec.stack);
211
212     return hres;
213 }