vbscript: Added interp_lt implementation.
[wine] / dlls / 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 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "ole2.h"
26 #include "dispex.h"
27 #include "activscp.h"
28
29 #include "vbscript_classes.h"
30
31 #include "wine/list.h"
32 #include "wine/unicode.h"
33
34 typedef struct {
35     void **blocks;
36     DWORD block_cnt;
37     DWORD last_block;
38     DWORD offset;
39     struct list custom_blocks;
40 } vbsheap_t;
41
42 void vbsheap_init(vbsheap_t*) DECLSPEC_HIDDEN;
43 void *vbsheap_alloc(vbsheap_t*,size_t) __WINE_ALLOC_SIZE(2) DECLSPEC_HIDDEN;
44 void vbsheap_free(vbsheap_t*) DECLSPEC_HIDDEN;
45
46 typedef struct _function_t function_t;
47 typedef struct _vbscode_t vbscode_t;
48 typedef struct _script_ctx_t script_ctx_t;
49
50 typedef struct named_item_t {
51     IDispatch *disp;
52     DWORD flags;
53     LPWSTR name;
54
55     struct list entry;
56 } named_item_t;
57
58 typedef enum {
59     VBDISP_CALLGET,
60     VBDISP_LET,
61     VBDISP_SET,
62     VBDISP_ANY
63 } vbdisp_invoke_type_t;
64
65 typedef struct {
66     BOOL is_public;
67     const WCHAR *name;
68 } vbdisp_prop_desc_t;
69
70 typedef struct {
71     const WCHAR *name;
72     BOOL is_public;
73     function_t *entries[VBDISP_ANY];
74 } vbdisp_funcprop_desc_t;
75
76 typedef struct _class_desc_t {
77     const WCHAR *name;
78     script_ctx_t *ctx;
79     unsigned class_initialize_id;
80     unsigned class_terminate_id;
81     unsigned func_cnt;
82     vbdisp_funcprop_desc_t *funcs;
83     unsigned prop_cnt;
84     vbdisp_prop_desc_t *props;
85     struct _class_desc_t *next;
86 } class_desc_t;
87
88 typedef struct {
89     IDispatchEx IDispatchEx_iface;
90
91     LONG ref;
92     BOOL terminator_ran;
93     struct list entry;
94
95     const class_desc_t *desc;
96     VARIANT props[1];
97 } vbdisp_t;
98
99 HRESULT create_vbdisp(const class_desc_t*,vbdisp_t**);
100 HRESULT disp_get_id(IDispatch*,BSTR,vbdisp_invoke_type_t,BOOL,DISPID*);
101 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,DISPPARAMS*,VARIANT*);
102 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,VARIANT*);
103 void collect_objects(script_ctx_t*);
104
105 static inline unsigned arg_cnt(const DISPPARAMS *dp)
106 {
107     return dp->cArgs - dp->cNamedArgs;
108 }
109
110 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
111 {
112     return dp->rgvarg + dp->cArgs-i-1;
113 }
114
115 typedef struct _dynamic_var_t {
116     struct _dynamic_var_t *next;
117     VARIANT v;
118     const WCHAR *name;
119 } dynamic_var_t;
120
121 struct _script_ctx_t {
122     IActiveScriptSite *site;
123     LCID lcid;
124
125     IDispatch *host_global;
126
127     class_desc_t script_desc;
128     vbdisp_t *script_obj;
129
130     dynamic_var_t *global_vars;
131     function_t *global_funcs;
132     class_desc_t *classes;
133
134     struct list objects;
135     struct list code_list;
136     struct list named_items;
137 };
138
139 HRESULT init_global(script_ctx_t*);
140
141 typedef enum {
142     ARG_NONE = 0,
143     ARG_STR,
144     ARG_BSTR,
145     ARG_INT,
146     ARG_UINT,
147     ARG_ADDR,
148     ARG_DOUBLE
149 } instr_arg_type_t;
150
151 #define OP_LIST                                   \
152     X(add,            1, 0,           0)          \
153     X(and,            1, 0,           0)          \
154     X(assign_ident,   1, ARG_BSTR,    0)          \
155     X(assign_member,  1, ARG_BSTR,    0)          \
156     X(bool,           1, ARG_INT,     0)          \
157     X(concat,         1, 0,           0)          \
158     X(div,            1, 0,           0)          \
159     X(double,         1, ARG_DOUBLE,  0)          \
160     X(empty,          1, 0,           0)          \
161     X(equal,          1, 0,           0)          \
162     X(eqv,            1, 0,           0)          \
163     X(exp,            1, 0,           0)          \
164     X(gt,             1, 0,           0)          \
165     X(gteq,           1, 0,           0)          \
166     X(icall,          1, ARG_BSTR,    ARG_UINT)   \
167     X(icallv,         1, ARG_BSTR,    ARG_UINT)   \
168     X(idiv,           1, 0,           0)          \
169     X(imp,            1, 0,           0)          \
170     X(jmp,            0, ARG_ADDR,    0)          \
171     X(jmp_false,      0, ARG_ADDR,    0)          \
172     X(jmp_true,       0, ARG_ADDR,    0)          \
173     X(long,           1, ARG_INT,     0)          \
174     X(lt,             1, 0,           0)          \
175     X(lteq,           1, 0,           0)          \
176     X(mcall,          1, ARG_BSTR,    ARG_UINT)   \
177     X(mcallv,         1, ARG_BSTR,    ARG_UINT)   \
178     X(mod,            1, 0,           0)          \
179     X(mul,            1, 0,           0)          \
180     X(neg,            1, 0,           0)          \
181     X(nequal,         1, 0,           0)          \
182     X(new,            1, ARG_STR,     0)          \
183     X(not,            1, 0,           0)          \
184     X(nothing,        1, 0,           0)          \
185     X(null,           1, 0,           0)          \
186     X(or,             1, 0,           0)          \
187     X(ret,            0, 0,           0)          \
188     X(set_ident,      1, ARG_BSTR,    0)          \
189     X(set_member,     1, ARG_BSTR,    0)          \
190     X(short,          1, ARG_INT,     0)          \
191     X(stop,           1, 0,           0)          \
192     X(string,         1, ARG_STR,     0)          \
193     X(sub,            1, 0,           0)          \
194     X(xor,            1, 0,           0)
195
196 typedef enum {
197 #define X(x,n,a,b) OP_##x,
198 OP_LIST
199 #undef X
200     OP_LAST
201 } vbsop_t;
202
203 typedef union {
204     const WCHAR *str;
205     BSTR bstr;
206     unsigned uint;
207     LONG lng;
208     double *dbl;
209 } instr_arg_t;
210
211 typedef struct {
212     vbsop_t op;
213     instr_arg_t arg1;
214     instr_arg_t arg2;
215 } instr_t;
216
217 typedef struct {
218     const WCHAR *name;
219     BOOL by_ref;
220 } arg_desc_t;
221
222 typedef enum {
223     FUNC_GLOBAL,
224     FUNC_FUNCTION,
225     FUNC_SUB,
226     FUNC_PROPGET,
227     FUNC_PROPLET,
228     FUNC_PROPSET,
229     FUNC_DEFGET
230 } function_type_t;
231
232 typedef struct {
233     const WCHAR *name;
234 } var_desc_t;
235
236 struct _function_t {
237     function_type_t type;
238     const WCHAR *name;
239     BOOL is_public;
240     arg_desc_t *args;
241     unsigned arg_cnt;
242     var_desc_t *vars;
243     unsigned var_cnt;
244     unsigned code_off;
245     vbscode_t *code_ctx;
246     function_t *next;
247 };
248
249 struct _vbscode_t {
250     instr_t *instrs;
251     WCHAR *source;
252
253     BOOL option_explicit;
254
255     BOOL global_executed;
256     function_t global_code;
257
258     BSTR *bstr_pool;
259     unsigned bstr_pool_size;
260     unsigned bstr_cnt;
261     vbsheap_t heap;
262
263     struct list entry;
264 };
265
266 void release_vbscode(vbscode_t*) DECLSPEC_HIDDEN;
267 HRESULT compile_script(script_ctx_t*,const WCHAR*,vbscode_t**) DECLSPEC_HIDDEN;
268 HRESULT exec_script(script_ctx_t*,function_t*,IDispatch*,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
269
270 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
271
272 const char *debugstr_variant(const VARIANT*) DECLSPEC_HIDDEN;
273
274 static inline void *heap_alloc(size_t len)
275 {
276     return HeapAlloc(GetProcessHeap(), 0, len);
277 }
278
279 static inline void *heap_alloc_zero(size_t len)
280 {
281     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
282 }
283
284 static inline void *heap_realloc(void *mem, size_t len)
285 {
286     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
287 }
288
289 static inline BOOL heap_free(void *mem)
290 {
291     return HeapFree(GetProcessHeap(), 0, mem);
292 }
293
294 static inline LPWSTR heap_strdupW(LPCWSTR str)
295 {
296     LPWSTR ret = NULL;
297
298     if(str) {
299         DWORD size;
300
301         size = (strlenW(str)+1)*sizeof(WCHAR);
302         ret = heap_alloc(size);
303         if(ret)
304             memcpy(ret, str, size);
305     }
306
307     return ret;
308 }