vbscript: Added do until..loop statement implementation.
[wine] / dlls / vbscript / vbscript.h
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 <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "ole2.h"
26 #include "dispex.h"
27 #include "activscp.h"
28
29 #include "vbscript_classes.h"
30
31 #include "wine/list.h"
32 #include "wine/unicode.h"
33
34 typedef struct {
35     void **blocks;
36     DWORD block_cnt;
37     DWORD last_block;
38     DWORD offset;
39     struct list custom_blocks;
40 } vbsheap_t;
41
42 void vbsheap_init(vbsheap_t*) DECLSPEC_HIDDEN;
43 void *vbsheap_alloc(vbsheap_t*,size_t) __WINE_ALLOC_SIZE(2) DECLSPEC_HIDDEN;
44 void vbsheap_free(vbsheap_t*) DECLSPEC_HIDDEN;
45
46 typedef struct _function_t function_t;
47 typedef struct _vbscode_t vbscode_t;
48 typedef struct _script_ctx_t script_ctx_t;
49
50 typedef struct named_item_t {
51     IDispatch *disp;
52     DWORD flags;
53     LPWSTR name;
54
55     struct list entry;
56 } named_item_t;
57
58 typedef enum {
59     VBDISP_CALLGET,
60     VBDISP_LET,
61     VBDISP_SET,
62     VBDISP_ANY
63 } vbdisp_invoke_type_t;
64
65 typedef struct {
66     BOOL is_public;
67     const WCHAR *name;
68 } vbdisp_prop_desc_t;
69
70 typedef struct {
71     const WCHAR *name;
72     BOOL is_public;
73     function_t *entries[VBDISP_ANY];
74 } vbdisp_funcprop_desc_t;
75
76 typedef struct _class_desc_t {
77     const WCHAR *name;
78     script_ctx_t *ctx;
79     unsigned class_initialize_id;
80     unsigned class_terminate_id;
81     unsigned func_cnt;
82     vbdisp_funcprop_desc_t *funcs;
83     unsigned prop_cnt;
84     vbdisp_prop_desc_t *props;
85     struct _class_desc_t *next;
86 } class_desc_t;
87
88 typedef struct {
89     IDispatchEx IDispatchEx_iface;
90
91     LONG ref;
92     BOOL terminator_ran;
93     struct list entry;
94
95     const class_desc_t *desc;
96     VARIANT props[1];
97 } vbdisp_t;
98
99 HRESULT create_vbdisp(const class_desc_t*,vbdisp_t**);
100 HRESULT disp_get_id(IDispatch*,BSTR,vbdisp_invoke_type_t,BOOL,DISPID*);
101 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,DISPPARAMS*,VARIANT*);
102 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,VARIANT*);
103 void collect_objects(script_ctx_t*);
104
105 static inline unsigned arg_cnt(const DISPPARAMS *dp)
106 {
107     return dp->cArgs - dp->cNamedArgs;
108 }
109
110 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
111 {
112     return dp->rgvarg + dp->cArgs-i-1;
113 }
114
115 typedef struct _dynamic_var_t {
116     struct _dynamic_var_t *next;
117     VARIANT v;
118     const WCHAR *name;
119 } dynamic_var_t;
120
121 struct _script_ctx_t {
122     IActiveScriptSite *site;
123     LCID lcid;
124
125     IDispatch *host_global;
126
127     class_desc_t script_desc;
128     vbdisp_t *script_obj;
129
130     dynamic_var_t *global_vars;
131     function_t *global_funcs;
132     class_desc_t *classes;
133
134     struct list objects;
135     struct list code_list;
136     struct list named_items;
137 };
138
139 HRESULT init_global(script_ctx_t*);
140
141 typedef enum {
142     ARG_NONE = 0,
143     ARG_STR,
144     ARG_BSTR,
145     ARG_INT,
146     ARG_UINT,
147     ARG_ADDR,
148     ARG_DOUBLE
149 } instr_arg_type_t;
150
151 #define OP_LIST                                   \
152     X(add,            1, 0,           0)          \
153     X(and,            1, 0,           0)          \
154     X(assign_ident,   1, ARG_BSTR,    0)          \
155     X(assign_member,  1, ARG_BSTR,    0)          \
156     X(bool,           1, ARG_INT,     0)          \
157     X(concat,         1, 0,           0)          \
158     X(div,            1, 0,           0)          \
159     X(double,         1, ARG_DOUBLE,  0)          \
160     X(empty,          1, 0,           0)          \
161     X(equal,          1, 0,           0)          \
162     X(eqv,            1, 0,           0)          \
163     X(exp,            1, 0,           0)          \
164     X(icall,          1, ARG_BSTR,    ARG_UINT)   \
165     X(icallv,         1, ARG_BSTR,    ARG_UINT)   \
166     X(idiv,           1, 0,           0)          \
167     X(imp,            1, 0,           0)          \
168     X(jmp,            0, ARG_ADDR,    0)          \
169     X(jmp_false,      0, ARG_ADDR,    0)          \
170     X(jmp_true,       0, ARG_ADDR,    0)          \
171     X(long,           1, ARG_INT,     0)          \
172     X(mcall,          1, ARG_BSTR,    ARG_UINT)   \
173     X(mcallv,         1, ARG_BSTR,    ARG_UINT)   \
174     X(mod,            1, 0,           0)          \
175     X(mul,            1, 0,           0)          \
176     X(neg,            1, 0,           0)          \
177     X(nequal,         1, 0,           0)          \
178     X(new,            1, ARG_STR,     0)          \
179     X(not,            1, 0,           0)          \
180     X(nothing,        1, 0,           0)          \
181     X(null,           1, 0,           0)          \
182     X(or,             1, 0,           0)          \
183     X(ret,            0, 0,           0)          \
184     X(set_ident,      1, ARG_BSTR,    0)          \
185     X(set_member,     1, ARG_BSTR,    0)          \
186     X(short,          1, ARG_INT,     0)          \
187     X(stop,           1, 0,           0)          \
188     X(string,         1, ARG_STR,     0)          \
189     X(sub,            1, 0,           0)          \
190     X(xor,            1, 0,           0)
191
192 typedef enum {
193 #define X(x,n,a,b) OP_##x,
194 OP_LIST
195 #undef X
196     OP_LAST
197 } vbsop_t;
198
199 typedef union {
200     const WCHAR *str;
201     BSTR bstr;
202     unsigned uint;
203     LONG lng;
204     double *dbl;
205 } instr_arg_t;
206
207 typedef struct {
208     vbsop_t op;
209     instr_arg_t arg1;
210     instr_arg_t arg2;
211 } instr_t;
212
213 typedef struct {
214     const WCHAR *name;
215     BOOL by_ref;
216 } arg_desc_t;
217
218 typedef enum {
219     FUNC_GLOBAL,
220     FUNC_FUNCTION,
221     FUNC_SUB,
222     FUNC_PROPGET,
223     FUNC_PROPLET,
224     FUNC_PROPSET,
225     FUNC_DEFGET
226 } function_type_t;
227
228 typedef struct {
229     const WCHAR *name;
230 } var_desc_t;
231
232 struct _function_t {
233     function_type_t type;
234     const WCHAR *name;
235     BOOL is_public;
236     arg_desc_t *args;
237     unsigned arg_cnt;
238     var_desc_t *vars;
239     unsigned var_cnt;
240     unsigned code_off;
241     vbscode_t *code_ctx;
242     function_t *next;
243 };
244
245 struct _vbscode_t {
246     instr_t *instrs;
247     WCHAR *source;
248
249     BOOL option_explicit;
250
251     BOOL global_executed;
252     function_t global_code;
253
254     BSTR *bstr_pool;
255     unsigned bstr_pool_size;
256     unsigned bstr_cnt;
257     vbsheap_t heap;
258
259     struct list entry;
260 };
261
262 void release_vbscode(vbscode_t*) DECLSPEC_HIDDEN;
263 HRESULT compile_script(script_ctx_t*,const WCHAR*,vbscode_t**) DECLSPEC_HIDDEN;
264 HRESULT exec_script(script_ctx_t*,function_t*,IDispatch*,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
265
266 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
267
268 const char *debugstr_variant(const VARIANT*) DECLSPEC_HIDDEN;
269
270 static inline void *heap_alloc(size_t len)
271 {
272     return HeapAlloc(GetProcessHeap(), 0, len);
273 }
274
275 static inline void *heap_alloc_zero(size_t len)
276 {
277     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
278 }
279
280 static inline void *heap_realloc(void *mem, size_t len)
281 {
282     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
283 }
284
285 static inline BOOL heap_free(void *mem)
286 {
287     return HeapFree(GetProcessHeap(), 0, mem);
288 }
289
290 static inline LPWSTR heap_strdupW(LPCWSTR str)
291 {
292     LPWSTR ret = NULL;
293
294     if(str) {
295         DWORD size;
296
297         size = (strlenW(str)+1)*sizeof(WCHAR);
298         ret = heap_alloc(size);
299         if(ret)
300             memcpy(ret, str, size);
301     }
302
303     return ret;
304 }