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