vbscript: Added getters/setters parser 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 func_cnt;
80     vbdisp_funcprop_desc_t *funcs;
81     unsigned prop_cnt;
82     vbdisp_prop_desc_t *props;
83     struct _class_desc_t *next;
84 } class_desc_t;
85
86 typedef struct {
87     IDispatchEx IDispatchEx_iface;
88
89     LONG ref;
90
91     const class_desc_t *desc;
92     VARIANT props[1];
93 } vbdisp_t;
94
95 HRESULT create_vbdisp(const class_desc_t*,vbdisp_t**);
96 HRESULT disp_get_id(IDispatch*,BSTR,BOOL,DISPID*);
97 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,DISPPARAMS*,VARIANT*);
98 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,VARIANT*);
99
100 static inline unsigned arg_cnt(const DISPPARAMS *dp)
101 {
102     return dp->cArgs - dp->cNamedArgs;
103 }
104
105 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
106 {
107     return dp->rgvarg + dp->cArgs-i-1;
108 }
109
110 typedef struct _dynamic_var_t {
111     struct _dynamic_var_t *next;
112     VARIANT v;
113     const WCHAR *name;
114 } dynamic_var_t;
115
116 struct _script_ctx_t {
117     IActiveScriptSite *site;
118     LCID lcid;
119
120     IDispatch *host_global;
121
122     class_desc_t script_desc;
123     vbdisp_t *script_obj;
124
125     dynamic_var_t *global_vars;
126     function_t *global_funcs;
127     class_desc_t *classes;
128
129     struct list code_list;
130     struct list named_items;
131 };
132
133 HRESULT init_global(script_ctx_t*);
134
135 typedef enum {
136     ARG_NONE = 0,
137     ARG_STR,
138     ARG_BSTR,
139     ARG_INT,
140     ARG_UINT,
141     ARG_ADDR,
142     ARG_DOUBLE
143 } instr_arg_type_t;
144
145 #define OP_LIST                                   \
146     X(add,            1, 0,           0)          \
147     X(and,            1, 0,           0)          \
148     X(assign_ident,   1, ARG_BSTR,    0)          \
149     X(assign_member,  1, ARG_BSTR,    0)          \
150     X(bool,           1, ARG_INT,     0)          \
151     X(concat,         1, 0,           0)          \
152     X(div,            1, 0,           0)          \
153     X(double,         1, ARG_DOUBLE,  0)          \
154     X(empty,          1, 0,           0)          \
155     X(equal,          1, 0,           0)          \
156     X(eqv,            1, 0,           0)          \
157     X(exp,            1, 0,           0)          \
158     X(icall,          1, ARG_BSTR,    ARG_UINT)   \
159     X(icallv,         1, ARG_BSTR,    ARG_UINT)   \
160     X(idiv,           1, 0,           0)          \
161     X(imp,            1, 0,           0)          \
162     X(jmp,            0, ARG_ADDR,    0)          \
163     X(jmp_false,      0, ARG_ADDR,    0)          \
164     X(long,           1, ARG_INT,     0)          \
165     X(mcall,          1, ARG_BSTR,    ARG_UINT)   \
166     X(mcallv,         1, ARG_BSTR,    ARG_UINT)   \
167     X(mod,            1, 0,           0)          \
168     X(mul,            1, 0,           0)          \
169     X(neg,            1, 0,           0)          \
170     X(nequal,         1, 0,           0)          \
171     X(new,            1, ARG_STR,     0)          \
172     X(not,            1, 0,           0)          \
173     X(nothing,        1, 0,           0)          \
174     X(null,           1, 0,           0)          \
175     X(or,             1, 0,           0)          \
176     X(ret,            0, 0,           0)          \
177     X(set_ident,      1, ARG_BSTR,    0)          \
178     X(set_member,     1, ARG_BSTR,    0)          \
179     X(short,          1, ARG_INT,     0)          \
180     X(stop,           1, 0,           0)          \
181     X(string,         1, ARG_STR,     0)          \
182     X(sub,            1, 0,           0)          \
183     X(xor,            1, 0,           0)
184
185 typedef enum {
186 #define X(x,n,a,b) OP_##x,
187 OP_LIST
188 #undef X
189     OP_LAST
190 } vbsop_t;
191
192 typedef union {
193     const WCHAR *str;
194     BSTR bstr;
195     unsigned uint;
196     LONG lng;
197     double *dbl;
198 } instr_arg_t;
199
200 typedef struct {
201     vbsop_t op;
202     instr_arg_t arg1;
203     instr_arg_t arg2;
204 } instr_t;
205
206 typedef struct {
207     const WCHAR *name;
208     BOOL by_ref;
209 } arg_desc_t;
210
211 typedef enum {
212     FUNC_GLOBAL,
213     FUNC_FUNCTION,
214     FUNC_SUB,
215     FUNC_PROPGET,
216     FUNC_PROPLET,
217     FUNC_PROPSET
218 } function_type_t;
219
220 typedef struct {
221     const WCHAR *name;
222 } var_desc_t;
223
224 struct _function_t {
225     function_type_t type;
226     const WCHAR *name;
227     BOOL is_public;
228     arg_desc_t *args;
229     unsigned arg_cnt;
230     var_desc_t *vars;
231     unsigned var_cnt;
232     unsigned code_off;
233     vbscode_t *code_ctx;
234     function_t *next;
235 };
236
237 struct _vbscode_t {
238     instr_t *instrs;
239     WCHAR *source;
240
241     BOOL option_explicit;
242
243     BOOL global_executed;
244     function_t global_code;
245
246     BSTR *bstr_pool;
247     unsigned bstr_pool_size;
248     unsigned bstr_cnt;
249     vbsheap_t heap;
250
251     struct list entry;
252 };
253
254 void release_vbscode(vbscode_t*) DECLSPEC_HIDDEN;
255 HRESULT compile_script(script_ctx_t*,const WCHAR*,vbscode_t**) DECLSPEC_HIDDEN;
256 HRESULT exec_script(script_ctx_t*,function_t*,IDispatch*,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
257
258 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
259
260 const char *debugstr_variant(const VARIANT*) DECLSPEC_HIDDEN;
261
262 static inline void *heap_alloc(size_t len)
263 {
264     return HeapAlloc(GetProcessHeap(), 0, len);
265 }
266
267 static inline void *heap_alloc_zero(size_t len)
268 {
269     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
270 }
271
272 static inline void *heap_realloc(void *mem, size_t len)
273 {
274     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
275 }
276
277 static inline BOOL heap_free(void *mem)
278 {
279     return HeapFree(GetProcessHeap(), 0, mem);
280 }
281
282 static inline LPWSTR heap_strdupW(LPCWSTR str)
283 {
284     LPWSTR ret = NULL;
285
286     if(str) {
287         DWORD size;
288
289         size = (strlenW(str)+1)*sizeof(WCHAR);
290         ret = heap_alloc(size);
291         if(ret)
292             memcpy(ret, str, size);
293     }
294
295     return ret;
296 }