[VBSCRIPT] Sync with Wine Staging 2.9. CORE-13362
[reactos.git] / reactos / dll / win32 / 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 #ifndef _VBSCRIPT_H
20 #define _VBSCRIPT_H
21
22 #include <assert.h>
23 #include <stdarg.h>
24
25 #define WIN32_NO_STATUS
26 #define _INC_WINDOWS
27 #define COM_NO_WINDOWS_H
28
29 #define COBJMACROS
30
31 #include <windef.h>
32 #include <winbase.h>
33 #include <objbase.h>
34 #include <oleauto.h>
35 #include <objsafe.h>
36 #include <dispex.h>
37 #include <activscp.h>
38
39 #include <wine/debug.h>
40 #include <wine/list.h>
41 #include <wine/unicode.h>
42
43 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
44
45 typedef struct {
46 void **blocks;
47 DWORD block_cnt;
48 DWORD last_block;
49 DWORD offset;
50 BOOL mark;
51 struct list custom_blocks;
52 } heap_pool_t;
53
54 void heap_pool_init(heap_pool_t*) DECLSPEC_HIDDEN;
55 void *heap_pool_alloc(heap_pool_t*,size_t) __WINE_ALLOC_SIZE(2) DECLSPEC_HIDDEN;
56 void *heap_pool_grow(heap_pool_t*,void*,DWORD,DWORD) DECLSPEC_HIDDEN;
57 void heap_pool_clear(heap_pool_t*) DECLSPEC_HIDDEN;
58 void heap_pool_free(heap_pool_t*) DECLSPEC_HIDDEN;
59 heap_pool_t *heap_pool_mark(heap_pool_t*) DECLSPEC_HIDDEN;
60
61 typedef struct _function_t function_t;
62 typedef struct _vbscode_t vbscode_t;
63 typedef struct _script_ctx_t script_ctx_t;
64 typedef struct _vbdisp_t vbdisp_t;
65
66 typedef struct named_item_t {
67 IDispatch *disp;
68 DWORD flags;
69 LPWSTR name;
70
71 struct list entry;
72 } named_item_t;
73
74 typedef enum {
75 VBDISP_CALLGET,
76 VBDISP_LET,
77 VBDISP_SET,
78 VBDISP_ANY
79 } vbdisp_invoke_type_t;
80
81 typedef struct {
82 unsigned dim_cnt;
83 SAFEARRAYBOUND *bounds;
84 } array_desc_t;
85
86 typedef struct {
87 BOOL is_public;
88 BOOL is_array;
89 const WCHAR *name;
90 } vbdisp_prop_desc_t;
91
92 typedef struct {
93 const WCHAR *name;
94 BOOL is_public;
95 BOOL is_array;
96 function_t *entries[VBDISP_ANY];
97 } vbdisp_funcprop_desc_t;
98
99 #define BP_GET 1
100 #define BP_GETPUT 2
101
102 typedef struct {
103 DISPID id;
104 HRESULT (*proc)(vbdisp_t*,VARIANT*,unsigned,VARIANT*);
105 DWORD flags;
106 unsigned min_args;
107 UINT_PTR max_args;
108 } builtin_prop_t;
109
110 typedef struct _class_desc_t {
111 const WCHAR *name;
112 script_ctx_t *ctx;
113
114 unsigned class_initialize_id;
115 unsigned class_terminate_id;
116 unsigned func_cnt;
117 vbdisp_funcprop_desc_t *funcs;
118
119 unsigned prop_cnt;
120 vbdisp_prop_desc_t *props;
121
122 unsigned array_cnt;
123 array_desc_t *array_descs;
124
125 unsigned builtin_prop_cnt;
126 const builtin_prop_t *builtin_props;
127 ITypeInfo *typeinfo;
128 function_t *value_func;
129
130 struct _class_desc_t *next;
131 } class_desc_t;
132
133 struct _vbdisp_t {
134 IDispatchEx IDispatchEx_iface;
135
136 LONG ref;
137 BOOL terminator_ran;
138 struct list entry;
139
140 const class_desc_t *desc;
141 SAFEARRAY **arrays;
142 VARIANT props[1];
143 };
144
145 typedef struct _ident_map_t ident_map_t;
146
147 typedef struct {
148 IDispatchEx IDispatchEx_iface;
149 LONG ref;
150
151 ident_map_t *ident_map;
152 unsigned ident_map_cnt;
153 unsigned ident_map_size;
154
155 script_ctx_t *ctx;
156 } ScriptDisp;
157
158 HRESULT create_vbdisp(const class_desc_t*,vbdisp_t**) DECLSPEC_HIDDEN;
159 HRESULT disp_get_id(IDispatch*,BSTR,vbdisp_invoke_type_t,BOOL,DISPID*) DECLSPEC_HIDDEN;
160 HRESULT vbdisp_get_id(vbdisp_t*,BSTR,vbdisp_invoke_type_t,BOOL,DISPID*) DECLSPEC_HIDDEN;
161 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
162 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,WORD,DISPPARAMS*) DECLSPEC_HIDDEN;
163 HRESULT get_disp_value(script_ctx_t*,IDispatch*,VARIANT*) DECLSPEC_HIDDEN;
164 void collect_objects(script_ctx_t*) DECLSPEC_HIDDEN;
165 HRESULT create_procedure_disp(script_ctx_t*,vbscode_t*,IDispatch**) DECLSPEC_HIDDEN;
166 HRESULT create_script_disp(script_ctx_t*,ScriptDisp**) DECLSPEC_HIDDEN;
167
168 HRESULT to_int(VARIANT*,int*) DECLSPEC_HIDDEN;
169
170 static inline unsigned arg_cnt(const DISPPARAMS *dp)
171 {
172 return dp->cArgs - dp->cNamedArgs;
173 }
174
175 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
176 {
177 return dp->rgvarg + dp->cArgs-i-1;
178 }
179
180 typedef struct _dynamic_var_t {
181 struct _dynamic_var_t *next;
182 VARIANT v;
183 const WCHAR *name;
184 BOOL is_const;
185 } dynamic_var_t;
186
187 struct _script_ctx_t {
188 IActiveScriptSite *site;
189 LCID lcid;
190
191 IInternetHostSecurityManager *secmgr;
192 DWORD safeopt;
193
194 IDispatch *host_global;
195
196 ScriptDisp *script_obj;
197
198 class_desc_t global_desc;
199 vbdisp_t *global_obj;
200
201 class_desc_t err_desc;
202 vbdisp_t *err_obj;
203
204 HRESULT err_number;
205
206 dynamic_var_t *global_vars;
207 function_t *global_funcs;
208 class_desc_t *classes;
209 class_desc_t *procs;
210
211 heap_pool_t heap;
212
213 struct list objects;
214 struct list code_list;
215 struct list named_items;
216 };
217
218 HRESULT init_global(script_ctx_t*) DECLSPEC_HIDDEN;
219 HRESULT init_err(script_ctx_t*) DECLSPEC_HIDDEN;
220
221 IUnknown *create_ax_site(script_ctx_t*) DECLSPEC_HIDDEN;
222
223 typedef enum {
224 ARG_NONE = 0,
225 ARG_STR,
226 ARG_BSTR,
227 ARG_INT,
228 ARG_UINT,
229 ARG_ADDR,
230 ARG_DOUBLE
231 } instr_arg_type_t;
232
233 #define OP_LIST \
234 X(add, 1, 0, 0) \
235 X(and, 1, 0, 0) \
236 X(assign_ident, 1, ARG_BSTR, ARG_UINT) \
237 X(assign_member, 1, ARG_BSTR, ARG_UINT) \
238 X(bool, 1, ARG_INT, 0) \
239 X(catch, 1, ARG_ADDR, ARG_UINT) \
240 X(case, 0, ARG_ADDR, 0) \
241 X(concat, 1, 0, 0) \
242 X(const, 1, ARG_BSTR, 0) \
243 X(dim, 1, ARG_BSTR, ARG_UINT) \
244 X(div, 1, 0, 0) \
245 X(double, 1, ARG_DOUBLE, 0) \
246 X(empty, 1, 0, 0) \
247 X(enumnext, 0, ARG_ADDR, ARG_BSTR) \
248 X(equal, 1, 0, 0) \
249 X(hres, 1, ARG_UINT, 0) \
250 X(errmode, 1, ARG_INT, 0) \
251 X(eqv, 1, 0, 0) \
252 X(exp, 1, 0, 0) \
253 X(gt, 1, 0, 0) \
254 X(gteq, 1, 0, 0) \
255 X(icall, 1, ARG_BSTR, ARG_UINT) \
256 X(icallv, 1, ARG_BSTR, ARG_UINT) \
257 X(idiv, 1, 0, 0) \
258 X(imp, 1, 0, 0) \
259 X(incc, 1, ARG_BSTR, 0) \
260 X(is, 1, 0, 0) \
261 X(jmp, 0, ARG_ADDR, 0) \
262 X(jmp_false, 0, ARG_ADDR, 0) \
263 X(jmp_true, 0, ARG_ADDR, 0) \
264 X(long, 1, ARG_INT, 0) \
265 X(lt, 1, 0, 0) \
266 X(lteq, 1, 0, 0) \
267 X(mcall, 1, ARG_BSTR, ARG_UINT) \
268 X(mcallv, 1, ARG_BSTR, ARG_UINT) \
269 X(me, 1, 0, 0) \
270 X(mod, 1, 0, 0) \
271 X(mul, 1, 0, 0) \
272 X(neg, 1, 0, 0) \
273 X(nequal, 1, 0, 0) \
274 X(new, 1, ARG_STR, 0) \
275 X(newenum, 1, 0, 0) \
276 X(not, 1, 0, 0) \
277 X(nothing, 1, 0, 0) \
278 X(null, 1, 0, 0) \
279 X(or, 1, 0, 0) \
280 X(pop, 1, ARG_UINT, 0) \
281 X(ret, 0, 0, 0) \
282 X(set_ident, 1, ARG_BSTR, ARG_UINT) \
283 X(set_member, 1, ARG_BSTR, ARG_UINT) \
284 X(short, 1, ARG_INT, 0) \
285 X(step, 0, ARG_ADDR, ARG_BSTR) \
286 X(stop, 1, 0, 0) \
287 X(string, 1, ARG_STR, 0) \
288 X(sub, 1, 0, 0) \
289 X(val, 1, 0, 0) \
290 X(xor, 1, 0, 0)
291
292 typedef enum {
293 #define X(x,n,a,b) OP_##x,
294 OP_LIST
295 #undef X
296 OP_LAST
297 } vbsop_t;
298
299 typedef union {
300 const WCHAR *str;
301 BSTR bstr;
302 unsigned uint;
303 LONG lng;
304 double *dbl;
305 } instr_arg_t;
306
307 typedef struct {
308 vbsop_t op;
309 instr_arg_t arg1;
310 instr_arg_t arg2;
311 } instr_t;
312
313 typedef struct {
314 const WCHAR *name;
315 BOOL by_ref;
316 } arg_desc_t;
317
318 typedef enum {
319 FUNC_GLOBAL,
320 FUNC_FUNCTION,
321 FUNC_SUB,
322 FUNC_PROPGET,
323 FUNC_PROPLET,
324 FUNC_PROPSET,
325 FUNC_DEFGET
326 } function_type_t;
327
328 typedef struct {
329 const WCHAR *name;
330 } var_desc_t;
331
332 struct _function_t {
333 function_type_t type;
334 const WCHAR *name;
335 BOOL is_public;
336 arg_desc_t *args;
337 unsigned arg_cnt;
338 var_desc_t *vars;
339 unsigned var_cnt;
340 array_desc_t *array_descs;
341 unsigned array_cnt;
342 unsigned code_off;
343 vbscode_t *code_ctx;
344 function_t *next;
345 };
346
347 struct _vbscode_t {
348 instr_t *instrs;
349 WCHAR *source;
350
351 BOOL option_explicit;
352
353 BOOL pending_exec;
354 function_t main_code;
355
356 BSTR *bstr_pool;
357 unsigned bstr_pool_size;
358 unsigned bstr_cnt;
359 heap_pool_t heap;
360
361 struct list entry;
362 };
363
364 void release_vbscode(vbscode_t*) DECLSPEC_HIDDEN;
365 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,vbscode_t**) DECLSPEC_HIDDEN;
366 HRESULT exec_script(script_ctx_t*,function_t*,vbdisp_t*,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
367 void release_dynamic_vars(dynamic_var_t*) DECLSPEC_HIDDEN;
368
369 typedef struct {
370 UINT16 len;
371 WCHAR buf[7];
372 } string_constant_t;
373
374 #define TID_LIST \
375 XDIID(ErrObj) \
376 XDIID(GlobalObj)
377
378 typedef enum {
379 #define XDIID(iface) iface ## _tid,
380 TID_LIST
381 #undef XDIID
382 LAST_tid
383 } tid_t;
384
385 HRESULT get_typeinfo(tid_t,ITypeInfo**) DECLSPEC_HIDDEN;
386 void release_regexp_typelib(void) DECLSPEC_HIDDEN;
387
388 #ifndef INT32_MIN
389 #define INT32_MIN (-2147483647-1)
390 #endif
391
392 #ifndef INT32_MAX
393 #define INT32_MAX (2147483647)
394 #endif
395
396 static inline BOOL is_int32(double d)
397 {
398 return INT32_MIN <= d && d <= INT32_MAX && (double)(int)d == d;
399 }
400
401 HRESULT create_regexp(IDispatch**) DECLSPEC_HIDDEN;
402
403 HRESULT map_hres(HRESULT) DECLSPEC_HIDDEN;
404
405 #define FACILITY_VBS 0xa
406 #define MAKE_VBSERROR(code) MAKE_HRESULT(SEVERITY_ERROR, FACILITY_VBS, code)
407
408 #define VBSE_ILLEGAL_FUNC_CALL 5
409 #define VBSE_OVERFLOW 6
410 #define VBSE_OUT_OF_MEMORY 7
411 #define VBSE_OUT_OF_BOUNDS 9
412 #define VBSE_ARRAY_LOCKED 10
413 #define VBSE_TYPE_MISMATCH 13
414 #define VBSE_FILE_NOT_FOUND 53
415 #define VBSE_IO_ERROR 57
416 #define VBSE_FILE_ALREADY_EXISTS 58
417 #define VBSE_DISK_FULL 61
418 #define VBSE_TOO_MANY_FILES 67
419 #define VBSE_PERMISSION_DENIED 70
420 #define VBSE_PATH_FILE_ACCESS 75
421 #define VBSE_PATH_NOT_FOUND 76
422 #define VBSE_ILLEGAL_NULL_USE 94
423 #define VBSE_OLE_NOT_SUPPORTED 430
424 #define VBSE_OLE_NO_PROP_OR_METHOD 438
425 #define VBSE_ACTION_NOT_SUPPORTED 445
426 #define VBSE_NAMED_ARGS_NOT_SUPPORTED 446
427 #define VBSE_LOCALE_SETTING_NOT_SUPPORTED 447
428 #define VBSE_NAMED_PARAM_NOT_FOUND 448
429 #define VBSE_INVALID_TYPELIB_VARIABLE 458
430 #define VBSE_FUNC_ARITY_MISMATCH 450
431 #define VBSE_PARAMETER_NOT_OPTIONAL 449
432 #define VBSE_NOT_ENUM 451
433 #define VBSE_INVALID_DLL_FUNCTION_NAME 453
434 #define VBSE_CANT_CREATE_TMP_FILE 322
435 #define VBSE_OLE_FILE_NOT_FOUND 432
436 #define VBSE_CANT_CREATE_OBJECT 429
437 #define VBSE_SERVER_NOT_FOUND 462
438
439 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**) DECLSPEC_HIDDEN;
440 HRESULT WINAPI VBScriptRegExpFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**) DECLSPEC_HIDDEN;
441
442 static inline void* __WINE_ALLOC_SIZE(1) heap_alloc(size_t size)
443 {
444 return HeapAlloc(GetProcessHeap(), 0, size);
445 }
446
447 static inline void* __WINE_ALLOC_SIZE(1) heap_alloc_zero(size_t size)
448 {
449 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
450 }
451
452 static inline void* __WINE_ALLOC_SIZE(2) heap_realloc(void *mem, size_t size)
453 {
454 return HeapReAlloc(GetProcessHeap(), 0, mem, size);
455 }
456
457 static inline BOOL heap_free(void *mem)
458 {
459 return HeapFree(GetProcessHeap(), 0, mem);
460 }
461
462 static inline LPWSTR heap_strdupW(LPCWSTR str)
463 {
464 LPWSTR ret = NULL;
465
466 if(str) {
467 DWORD size;
468
469 size = (strlenW(str)+1)*sizeof(WCHAR);
470 ret = heap_alloc(size);
471 if(ret)
472 memcpy(ret, str, size);
473 }
474
475 return ret;
476 }
477
478 #define VBSCRIPT_BUILD_VERSION 16978
479 #define VBSCRIPT_MAJOR_VERSION 5
480 #define VBSCRIPT_MINOR_VERSION 8
481
482 #include "parse.h"
483 #include "regexp.h"
484 #include "vbscript_defs.h"
485
486 #endif /* _VBSCRIPT_H */