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