msvcp90: Return last index in string::find_last_not_of_cstr_substr if input is empty.
[wine.git] / dlls / vbscript / vbscript.h
bloba96d0b10be2d9d4ddb9ac4dafea0caf5f42e3ac8
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "ole2.h"
26 #include "dispex.h"
27 #include "activscp.h"
29 #include "vbscript_classes.h"
31 #include "wine/list.h"
32 #include "wine/unicode.h"
34 typedef struct {
35 void **blocks;
36 DWORD block_cnt;
37 DWORD last_block;
38 DWORD offset;
39 BOOL mark;
40 struct list custom_blocks;
41 } heap_pool_t;
43 void heap_pool_init(heap_pool_t*) DECLSPEC_HIDDEN;
44 void *heap_pool_alloc(heap_pool_t*,size_t) __WINE_ALLOC_SIZE(2) DECLSPEC_HIDDEN;
45 void *heap_pool_grow(heap_pool_t*,void*,DWORD,DWORD) DECLSPEC_HIDDEN;
46 void heap_pool_clear(heap_pool_t*) DECLSPEC_HIDDEN;
47 void heap_pool_free(heap_pool_t*) DECLSPEC_HIDDEN;
48 heap_pool_t *heap_pool_mark(heap_pool_t*) DECLSPEC_HIDDEN;
50 typedef struct _function_t function_t;
51 typedef struct _vbscode_t vbscode_t;
52 typedef struct _script_ctx_t script_ctx_t;
53 typedef struct _vbdisp_t vbdisp_t;
55 typedef struct named_item_t {
56 IDispatch *disp;
57 DWORD flags;
58 LPWSTR name;
60 struct list entry;
61 } named_item_t;
63 typedef enum {
64 VBDISP_CALLGET,
65 VBDISP_LET,
66 VBDISP_SET,
67 VBDISP_ANY
68 } vbdisp_invoke_type_t;
70 typedef struct {
71 BOOL is_public;
72 const WCHAR *name;
73 } vbdisp_prop_desc_t;
75 typedef struct {
76 const WCHAR *name;
77 BOOL is_public;
78 function_t *entries[VBDISP_ANY];
79 } vbdisp_funcprop_desc_t;
81 #define BP_GET 1
82 #define BP_GETPUT 2
84 typedef struct {
85 DISPID id;
86 HRESULT (*proc)(vbdisp_t*,VARIANT*,unsigned,VARIANT*);
87 DWORD flags;
88 unsigned min_args;
89 UINT_PTR max_args;
90 } builtin_prop_t;
92 typedef struct _class_desc_t {
93 const WCHAR *name;
94 script_ctx_t *ctx;
96 unsigned class_initialize_id;
97 unsigned class_terminate_id;
98 unsigned func_cnt;
99 vbdisp_funcprop_desc_t *funcs;
101 unsigned prop_cnt;
102 vbdisp_prop_desc_t *props;
104 unsigned builtin_prop_cnt;
105 const builtin_prop_t *builtin_props;
106 ITypeInfo *typeinfo;
107 function_t *value_func;
109 struct _class_desc_t *next;
110 } class_desc_t;
112 struct _vbdisp_t {
113 IDispatchEx IDispatchEx_iface;
115 LONG ref;
116 BOOL terminator_ran;
117 struct list entry;
119 const class_desc_t *desc;
120 VARIANT props[1];
123 typedef struct _ident_map_t ident_map_t;
125 typedef struct {
126 IDispatchEx IDispatchEx_iface;
127 LONG ref;
129 ident_map_t *ident_map;
130 unsigned ident_map_cnt;
131 unsigned ident_map_size;
133 script_ctx_t *ctx;
134 } ScriptDisp;
136 HRESULT create_vbdisp(const class_desc_t*,vbdisp_t**) DECLSPEC_HIDDEN;
137 HRESULT disp_get_id(IDispatch*,BSTR,vbdisp_invoke_type_t,BOOL,DISPID*) DECLSPEC_HIDDEN;
138 HRESULT vbdisp_get_id(vbdisp_t*,BSTR,vbdisp_invoke_type_t,BOOL,DISPID*) DECLSPEC_HIDDEN;
139 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
140 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,DISPPARAMS*) DECLSPEC_HIDDEN;
141 void collect_objects(script_ctx_t*) DECLSPEC_HIDDEN;
142 HRESULT create_procedure_disp(script_ctx_t*,vbscode_t*,IDispatch**) DECLSPEC_HIDDEN;
143 HRESULT create_script_disp(script_ctx_t*,ScriptDisp**) DECLSPEC_HIDDEN;
145 HRESULT to_int(VARIANT*,int*) DECLSPEC_HIDDEN;
147 static inline unsigned arg_cnt(const DISPPARAMS *dp)
149 return dp->cArgs - dp->cNamedArgs;
152 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
154 return dp->rgvarg + dp->cArgs-i-1;
157 typedef struct _dynamic_var_t {
158 struct _dynamic_var_t *next;
159 VARIANT v;
160 const WCHAR *name;
161 BOOL is_const;
162 } dynamic_var_t;
164 struct _script_ctx_t {
165 IActiveScriptSite *site;
166 LCID lcid;
168 IInternetHostSecurityManager *secmgr;
169 DWORD safeopt;
171 IDispatch *host_global;
173 ScriptDisp *script_obj;
175 class_desc_t global_desc;
176 vbdisp_t *global_obj;
178 class_desc_t err_desc;
179 vbdisp_t *err_obj;
181 dynamic_var_t *global_vars;
182 function_t *global_funcs;
183 class_desc_t *classes;
184 class_desc_t *procs;
186 heap_pool_t heap;
188 struct list objects;
189 struct list code_list;
190 struct list named_items;
193 HRESULT init_global(script_ctx_t*) DECLSPEC_HIDDEN;
194 HRESULT init_err(script_ctx_t*) DECLSPEC_HIDDEN;
196 IUnknown *create_ax_site(script_ctx_t*) DECLSPEC_HIDDEN;
198 typedef enum {
199 ARG_NONE = 0,
200 ARG_STR,
201 ARG_BSTR,
202 ARG_INT,
203 ARG_UINT,
204 ARG_ADDR,
205 ARG_DOUBLE
206 } instr_arg_type_t;
208 #define OP_LIST \
209 X(add, 1, 0, 0) \
210 X(and, 1, 0, 0) \
211 X(assign_ident, 1, ARG_BSTR, ARG_UINT) \
212 X(assign_member, 1, ARG_BSTR, ARG_UINT) \
213 X(bool, 1, ARG_INT, 0) \
214 X(case, 0, ARG_ADDR, 0) \
215 X(concat, 1, 0, 0) \
216 X(const, 1, ARG_BSTR, 0) \
217 X(dim, 1, ARG_BSTR, ARG_UINT) \
218 X(div, 1, 0, 0) \
219 X(double, 1, ARG_DOUBLE, 0) \
220 X(empty, 1, 0, 0) \
221 X(enumnext, 0, ARG_ADDR, ARG_BSTR) \
222 X(equal, 1, 0, 0) \
223 X(errmode, 1, ARG_INT, 0) \
224 X(eqv, 1, 0, 0) \
225 X(exp, 1, 0, 0) \
226 X(gt, 1, 0, 0) \
227 X(gteq, 1, 0, 0) \
228 X(icall, 1, ARG_BSTR, ARG_UINT) \
229 X(icallv, 1, ARG_BSTR, ARG_UINT) \
230 X(idiv, 1, 0, 0) \
231 X(imp, 1, 0, 0) \
232 X(incc, 1, ARG_BSTR, 0) \
233 X(is, 1, 0, 0) \
234 X(jmp, 0, ARG_ADDR, 0) \
235 X(jmp_false, 0, ARG_ADDR, 0) \
236 X(jmp_true, 0, ARG_ADDR, 0) \
237 X(long, 1, ARG_INT, 0) \
238 X(lt, 1, 0, 0) \
239 X(lteq, 1, 0, 0) \
240 X(mcall, 1, ARG_BSTR, ARG_UINT) \
241 X(mcallv, 1, ARG_BSTR, ARG_UINT) \
242 X(me, 1, 0, 0) \
243 X(mod, 1, 0, 0) \
244 X(mul, 1, 0, 0) \
245 X(neg, 1, 0, 0) \
246 X(nequal, 1, 0, 0) \
247 X(new, 1, ARG_STR, 0) \
248 X(newenum, 1, 0, 0) \
249 X(not, 1, 0, 0) \
250 X(nothing, 1, 0, 0) \
251 X(null, 1, 0, 0) \
252 X(or, 1, 0, 0) \
253 X(pop, 1, ARG_UINT, 0) \
254 X(ret, 0, 0, 0) \
255 X(set_ident, 1, ARG_BSTR, ARG_UINT) \
256 X(set_member, 1, ARG_BSTR, ARG_UINT) \
257 X(short, 1, ARG_INT, 0) \
258 X(step, 0, ARG_ADDR, ARG_BSTR) \
259 X(stop, 1, 0, 0) \
260 X(string, 1, ARG_STR, 0) \
261 X(sub, 1, 0, 0) \
262 X(val, 1, 0, 0) \
263 X(xor, 1, 0, 0)
265 typedef enum {
266 #define X(x,n,a,b) OP_##x,
267 OP_LIST
268 #undef X
269 OP_LAST
270 } vbsop_t;
272 typedef union {
273 const WCHAR *str;
274 BSTR bstr;
275 unsigned uint;
276 LONG lng;
277 double *dbl;
278 } instr_arg_t;
280 typedef struct {
281 vbsop_t op;
282 instr_arg_t arg1;
283 instr_arg_t arg2;
284 } instr_t;
286 typedef struct {
287 const WCHAR *name;
288 BOOL by_ref;
289 } arg_desc_t;
291 typedef enum {
292 FUNC_GLOBAL,
293 FUNC_FUNCTION,
294 FUNC_SUB,
295 FUNC_PROPGET,
296 FUNC_PROPLET,
297 FUNC_PROPSET,
298 FUNC_DEFGET
299 } function_type_t;
301 typedef struct {
302 const WCHAR *name;
303 } var_desc_t;
305 typedef struct {
306 unsigned dim_cnt;
307 SAFEARRAYBOUND *bounds;
308 } array_desc_t;
310 struct _function_t {
311 function_type_t type;
312 const WCHAR *name;
313 BOOL is_public;
314 arg_desc_t *args;
315 unsigned arg_cnt;
316 var_desc_t *vars;
317 unsigned var_cnt;
318 array_desc_t *array_descs;
319 unsigned array_cnt;
320 unsigned code_off;
321 vbscode_t *code_ctx;
322 function_t *next;
325 struct _vbscode_t {
326 instr_t *instrs;
327 WCHAR *source;
329 BOOL option_explicit;
331 BOOL pending_exec;
332 function_t main_code;
334 BSTR *bstr_pool;
335 unsigned bstr_pool_size;
336 unsigned bstr_cnt;
337 heap_pool_t heap;
339 struct list entry;
342 void release_vbscode(vbscode_t*) DECLSPEC_HIDDEN;
343 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,vbscode_t**) DECLSPEC_HIDDEN;
344 HRESULT exec_script(script_ctx_t*,function_t*,IDispatch*,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
345 void release_dynamic_vars(dynamic_var_t*) DECLSPEC_HIDDEN;
347 typedef struct {
348 UINT16 len;
349 WCHAR buf[7];
350 } string_constant_t;
352 #define TID_LIST \
353 XDIID(ErrObj) \
354 XDIID(GlobalObj)
356 typedef enum {
357 #define XDIID(iface) iface ## _tid,
358 TID_LIST
359 #undef XDIID
360 LAST_tid
361 } tid_t;
363 HRESULT get_typeinfo(tid_t,ITypeInfo**) DECLSPEC_HIDDEN;
364 void release_regexp_typelib(void) DECLSPEC_HIDDEN;
366 #ifndef INT32_MIN
367 #define INT32_MIN (-2147483647-1)
368 #endif
370 #ifndef INT32_MAX
371 #define INT32_MAX (2147483647)
372 #endif
374 static inline BOOL is_int32(double d)
376 return INT32_MIN <= d && d <= INT32_MAX && (double)(int)d == d;
379 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**) DECLSPEC_HIDDEN;
380 HRESULT WINAPI VBScriptRegExpFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**) DECLSPEC_HIDDEN;
382 const char *debugstr_variant(const VARIANT*) DECLSPEC_HIDDEN;
384 static inline void *heap_alloc(size_t len)
386 return HeapAlloc(GetProcessHeap(), 0, len);
389 static inline void *heap_alloc_zero(size_t len)
391 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
394 static inline void *heap_realloc(void *mem, size_t len)
396 return HeapReAlloc(GetProcessHeap(), 0, mem, len);
399 static inline BOOL heap_free(void *mem)
401 return HeapFree(GetProcessHeap(), 0, mem);
404 static inline LPWSTR heap_strdupW(LPCWSTR str)
406 LPWSTR ret = NULL;
408 if(str) {
409 DWORD size;
411 size = (strlenW(str)+1)*sizeof(WCHAR);
412 ret = heap_alloc(size);
413 if(ret)
414 memcpy(ret, str, size);
417 return ret;