vbscript: Added is expression 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(gt,             1, 0,           0)          \
165     X(gteq,           1, 0,           0)          \
166     X(icall,          1, ARG_BSTR,    ARG_UINT)   \
167     X(icallv,         1, ARG_BSTR,    ARG_UINT)   \
168     X(idiv,           1, 0,           0)          \
169     X(imp,            1, 0,           0)          \
170     X(is,             1, 0,           0)          \
171     X(jmp,            0, ARG_ADDR,    0)          \
172     X(jmp_false,      0, ARG_ADDR,    0)          \
173     X(jmp_true,       0, ARG_ADDR,    0)          \
174     X(long,           1, ARG_INT,     0)          \
175     X(lt,             1, 0,           0)          \
176     X(lteq,           1, 0,           0)          \
177     X(mcall,          1, ARG_BSTR,    ARG_UINT)   \
178     X(mcallv,         1, ARG_BSTR,    ARG_UINT)   \
179     X(mod,            1, 0,           0)          \
180     X(mul,            1, 0,           0)          \
181     X(neg,            1, 0,           0)          \
182     X(nequal,         1, 0,           0)          \
183     X(new,            1, ARG_STR,     0)          \
184     X(not,            1, 0,           0)          \
185     X(nothing,        1, 0,           0)          \
186     X(null,           1, 0,           0)          \
187     X(or,             1, 0,           0)          \
188     X(ret,            0, 0,           0)          \
189     X(set_ident,      1, ARG_BSTR,    0)          \
190     X(set_member,     1, ARG_BSTR,    0)          \
191     X(short,          1, ARG_INT,     0)          \
192     X(stop,           1, 0,           0)          \
193     X(string,         1, ARG_STR,     0)          \
194     X(sub,            1, 0,           0)          \
195     X(xor,            1, 0,           0)
196
197 typedef enum {
198 #define X(x,n,a,b) OP_##x,
199 OP_LIST
200 #undef X
201     OP_LAST
202 } vbsop_t;
203
204 typedef union {
205     const WCHAR *str;
206     BSTR bstr;
207     unsigned uint;
208     LONG lng;
209     double *dbl;
210 } instr_arg_t;
211
212 typedef struct {
213     vbsop_t op;
214     instr_arg_t arg1;
215     instr_arg_t arg2;
216 } instr_t;
217
218 typedef struct {
219     const WCHAR *name;
220     BOOL by_ref;
221 } arg_desc_t;
222
223 typedef enum {
224     FUNC_GLOBAL,
225     FUNC_FUNCTION,
226     FUNC_SUB,
227     FUNC_PROPGET,
228     FUNC_PROPLET,
229     FUNC_PROPSET,
230     FUNC_DEFGET
231 } function_type_t;
232
233 typedef struct {
234     const WCHAR *name;
235 } var_desc_t;
236
237 struct _function_t {
238     function_type_t type;
239     const WCHAR *name;
240     BOOL is_public;
241     arg_desc_t *args;
242     unsigned arg_cnt;
243     var_desc_t *vars;
244     unsigned var_cnt;
245     unsigned code_off;
246     vbscode_t *code_ctx;
247     function_t *next;
248 };
249
250 struct _vbscode_t {
251     instr_t *instrs;
252     WCHAR *source;
253
254     BOOL option_explicit;
255
256     BOOL global_executed;
257     function_t global_code;
258
259     BSTR *bstr_pool;
260     unsigned bstr_pool_size;
261     unsigned bstr_cnt;
262     vbsheap_t heap;
263
264     struct list entry;
265 };
266
267 void release_vbscode(vbscode_t*) DECLSPEC_HIDDEN;
268 HRESULT compile_script(script_ctx_t*,const WCHAR*,vbscode_t**) DECLSPEC_HIDDEN;
269 HRESULT exec_script(script_ctx_t*,function_t*,IDispatch*,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
270
271 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
272
273 const char *debugstr_variant(const VARIANT*) DECLSPEC_HIDDEN;
274
275 static inline void *heap_alloc(size_t len)
276 {
277     return HeapAlloc(GetProcessHeap(), 0, len);
278 }
279
280 static inline void *heap_alloc_zero(size_t len)
281 {
282     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
283 }
284
285 static inline void *heap_realloc(void *mem, size_t len)
286 {
287     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
288 }
289
290 static inline BOOL heap_free(void *mem)
291 {
292     return HeapFree(GetProcessHeap(), 0, mem);
293 }
294
295 static inline LPWSTR heap_strdupW(LPCWSTR str)
296 {
297     LPWSTR ret = NULL;
298
299     if(str) {
300         DWORD size;
301
302         size = (strlenW(str)+1)*sizeof(WCHAR);
303         ret = heap_alloc(size);
304         if(ret)
305             memcpy(ret, str, size);
306     }
307
308     return ret;
309 }