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