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