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