jscript: Added VBArray.dimensions() implementation.
[wine] / dlls / jscript / jscript.h
1 /*
2  * Copyright 2008-2009 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 #include <stdio.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28 #include "dispex.h"
29 #include "activscp.h"
30
31 #include "resource.h"
32
33 #include "wine/unicode.h"
34 #include "wine/list.h"
35
36 #define JSCRIPT_ERROR 0x800A0000
37
38 typedef struct _script_ctx_t script_ctx_t;
39 typedef struct _exec_ctx_t exec_ctx_t;
40 typedef struct _dispex_prop_t dispex_prop_t;
41
42 typedef struct {
43     EXCEPINFO ei;
44     VARIANT var;
45 } jsexcept_t;
46
47 typedef struct {
48     void **blocks;
49     DWORD block_cnt;
50     DWORD last_block;
51     DWORD offset;
52     BOOL mark;
53     struct list custom_blocks;
54 } jsheap_t;
55
56 void jsheap_init(jsheap_t*);
57 void *jsheap_alloc(jsheap_t*,DWORD);
58 void *jsheap_grow(jsheap_t*,void*,DWORD,DWORD);
59 void jsheap_clear(jsheap_t*);
60 void jsheap_free(jsheap_t*);
61 jsheap_t *jsheap_mark(jsheap_t*);
62
63 typedef struct jsdisp_t jsdisp_t;
64
65 extern HINSTANCE jscript_hinstance;
66
67 #define PROPF_ARGMASK 0x00ff
68 #define PROPF_METHOD  0x0100
69 #define PROPF_ENUM    0x0200
70 #define PROPF_CONSTR  0x0400
71 #define PROPF_CONST   0x0800
72
73 /* NOTE: Keep in sync with names in Object.toString implementation */
74 typedef enum {
75     JSCLASS_NONE,
76     JSCLASS_ARRAY,
77     JSCLASS_BOOLEAN,
78     JSCLASS_DATE,
79     JSCLASS_ERROR,
80     JSCLASS_FUNCTION,
81     JSCLASS_GLOBAL,
82     JSCLASS_MATH,
83     JSCLASS_NUMBER,
84     JSCLASS_OBJECT,
85     JSCLASS_REGEXP,
86     JSCLASS_STRING,
87     JSCLASS_ARGUMENTS,
88     JSCLASS_VBARRAY
89 } jsclass_t;
90
91 jsdisp_t *iface_to_jsdisp(IUnknown*);
92
93 typedef struct {
94     union {
95         IDispatch *disp;
96         IDispatchEx *dispex;
97         jsdisp_t *jsdisp;
98     } u;
99     DWORD flags;
100 } vdisp_t;
101
102 #define VDISP_DISPEX  0x0001
103 #define VDISP_JSDISP  0x0002
104
105 static inline void vdisp_release(vdisp_t *vdisp)
106 {
107     IDispatch_Release(vdisp->u.disp);
108 }
109
110 static inline BOOL is_jsdisp(vdisp_t *vdisp)
111 {
112     return (vdisp->flags & VDISP_JSDISP) != 0;
113 }
114
115 static inline BOOL is_dispex(vdisp_t *vdisp)
116 {
117     return (vdisp->flags & VDISP_DISPEX) != 0;
118 }
119
120 static inline void set_jsdisp(vdisp_t *vdisp, jsdisp_t *jsdisp)
121 {
122     vdisp->u.jsdisp = jsdisp;
123     vdisp->flags = VDISP_JSDISP | VDISP_DISPEX;
124     IDispatch_AddRef(vdisp->u.disp);
125 }
126
127 static inline void set_disp(vdisp_t *vdisp, IDispatch *disp)
128 {
129     IDispatchEx *dispex;
130     jsdisp_t *jsdisp;
131     HRESULT hres;
132
133     jsdisp = iface_to_jsdisp((IUnknown*)disp);
134     if(jsdisp) {
135         vdisp->u.jsdisp = jsdisp;
136         vdisp->flags = VDISP_JSDISP | VDISP_DISPEX;
137         return;
138     }
139
140     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
141     if(SUCCEEDED(hres)) {
142         vdisp->u.dispex = dispex;
143         vdisp->flags = VDISP_DISPEX;
144         return;
145     }
146
147     IDispatch_AddRef(disp);
148     vdisp->u.disp = disp;
149     vdisp->flags = 0;
150 }
151
152 static inline jsdisp_t *get_jsdisp(vdisp_t *vdisp)
153 {
154     return is_jsdisp(vdisp) ? vdisp->u.jsdisp : NULL;
155 }
156
157 typedef HRESULT (*builtin_invoke_t)(script_ctx_t*,vdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
158
159 typedef struct {
160     const WCHAR *name;
161     builtin_invoke_t invoke;
162     DWORD flags;
163 } builtin_prop_t;
164
165 typedef struct {
166     jsclass_t class;
167     builtin_prop_t value_prop;
168     DWORD props_cnt;
169     const builtin_prop_t *props;
170     void (*destructor)(jsdisp_t*);
171     void (*on_put)(jsdisp_t*,const WCHAR*);
172 } builtin_info_t;
173
174 struct jsdisp_t {
175     const IDispatchExVtbl  *lpIDispatchExVtbl;
176
177     LONG ref;
178
179     DWORD buf_size;
180     DWORD prop_cnt;
181     dispex_prop_t *props;
182     script_ctx_t *ctx;
183
184     jsdisp_t *prototype;
185
186     const builtin_info_t *builtin_info;
187 };
188
189 #define _IDispatchEx_(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
190
191 static inline IDispatch *to_disp(jsdisp_t *jsdisp)
192 {
193     return (IDispatch*)&jsdisp->lpIDispatchExVtbl;
194 }
195
196 static inline void jsdisp_addref(jsdisp_t *jsdisp)
197 {
198     IDispatchEx_AddRef(_IDispatchEx_(jsdisp));
199 }
200
201 static inline void jsdisp_release(jsdisp_t *jsdisp)
202 {
203     IDispatchEx_Release(_IDispatchEx_(jsdisp));
204 }
205
206 HRESULT create_dispex(script_ctx_t*,const builtin_info_t*,jsdisp_t*,jsdisp_t**);
207 HRESULT init_dispex(jsdisp_t*,script_ctx_t*,const builtin_info_t*,jsdisp_t*);
208 HRESULT init_dispex_from_constr(jsdisp_t*,script_ctx_t*,const builtin_info_t*,jsdisp_t*);
209
210 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
211 HRESULT jsdisp_call_value(jsdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
212 HRESULT jsdisp_call(jsdisp_t*,DISPID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
213 HRESULT jsdisp_call_name(jsdisp_t*,const WCHAR*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
214 HRESULT disp_propget(script_ctx_t*,IDispatch*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
215 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
216 HRESULT jsdisp_propget(jsdisp_t*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
217 HRESULT jsdisp_propput_name(jsdisp_t*,const WCHAR*,VARIANT*,jsexcept_t*,IServiceProvider*);
218 HRESULT jsdisp_propput_const(jsdisp_t*,const WCHAR*,VARIANT*);
219 HRESULT jsdisp_propput_idx(jsdisp_t*,DWORD,VARIANT*,jsexcept_t*,IServiceProvider*);
220 HRESULT jsdisp_propget_name(jsdisp_t*,LPCWSTR,VARIANT*,jsexcept_t*,IServiceProvider*);
221 HRESULT jsdisp_get_idx(jsdisp_t*,DWORD,VARIANT*,jsexcept_t*,IServiceProvider*);
222 HRESULT jsdisp_get_id(jsdisp_t*,const WCHAR*,DWORD,DISPID*);
223 HRESULT jsdisp_delete_idx(jsdisp_t*,DWORD);
224
225 HRESULT create_builtin_function(script_ctx_t*,builtin_invoke_t,const WCHAR*,const builtin_info_t*,DWORD,
226         jsdisp_t*,jsdisp_t**);
227 HRESULT Function_value(script_ctx_t*,vdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
228
229 HRESULT throw_eval_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
230 HRESULT throw_generic_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
231 HRESULT throw_range_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
232 HRESULT throw_reference_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
233 HRESULT throw_regexp_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
234 HRESULT throw_syntax_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
235 HRESULT throw_type_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
236 HRESULT throw_uri_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
237
238 HRESULT create_object(script_ctx_t*,jsdisp_t*,jsdisp_t**);
239 HRESULT create_math(script_ctx_t*,jsdisp_t**);
240 HRESULT create_array(script_ctx_t*,DWORD,jsdisp_t**);
241 HRESULT create_regexp(script_ctx_t*,const WCHAR *,int,DWORD,jsdisp_t**);
242 HRESULT create_regexp_var(script_ctx_t*,VARIANT*,VARIANT*,jsdisp_t**);
243 HRESULT create_string(script_ctx_t*,const WCHAR*,DWORD,jsdisp_t**);
244 HRESULT create_bool(script_ctx_t*,VARIANT_BOOL,jsdisp_t**);
245 HRESULT create_number(script_ctx_t*,VARIANT*,jsdisp_t**);
246
247 typedef enum {
248     NO_HINT,
249     HINT_STRING,
250     HINT_NUMBER
251 } hint_t;
252
253 HRESULT to_primitive(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*, hint_t);
254 HRESULT to_boolean(VARIANT*,VARIANT_BOOL*);
255 HRESULT to_number(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
256 HRESULT to_integer(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
257 HRESULT to_int32(script_ctx_t*,VARIANT*,jsexcept_t*,INT*);
258 HRESULT to_uint32(script_ctx_t*,VARIANT*,jsexcept_t*,DWORD*);
259 HRESULT to_string(script_ctx_t*,VARIANT*,jsexcept_t*,BSTR*);
260 HRESULT to_object(script_ctx_t*,VARIANT*,IDispatch**);
261
262 typedef struct named_item_t {
263     IDispatch *disp;
264     DWORD flags;
265     LPWSTR name;
266
267     struct named_item_t *next;
268 } named_item_t;
269
270 struct _script_ctx_t {
271     LONG ref;
272
273     SCRIPTSTATE state;
274     exec_ctx_t *exec_ctx;
275     named_item_t *named_items;
276     IActiveScriptSite *site;
277     IInternetHostSecurityManager *secmgr;
278     DWORD safeopt;
279     DWORD version;
280     LCID lcid;
281
282     jsheap_t tmp_heap;
283
284     IDispatch *host_global;
285
286     BSTR last_match;
287     DWORD last_match_index;
288     DWORD last_match_length;
289
290     jsdisp_t *global;
291     jsdisp_t *function_constr;
292     jsdisp_t *activex_constr;
293     jsdisp_t *array_constr;
294     jsdisp_t *bool_constr;
295     jsdisp_t *date_constr;
296     jsdisp_t *error_constr;
297     jsdisp_t *eval_error_constr;
298     jsdisp_t *range_error_constr;
299     jsdisp_t *reference_error_constr;
300     jsdisp_t *regexp_error_constr;
301     jsdisp_t *syntax_error_constr;
302     jsdisp_t *type_error_constr;
303     jsdisp_t *uri_error_constr;
304     jsdisp_t *number_constr;
305     jsdisp_t *object_constr;
306     jsdisp_t *regexp_constr;
307     jsdisp_t *string_constr;
308     jsdisp_t *vbarray_constr;
309 };
310
311 void script_release(script_ctx_t*);
312
313 static inline void script_addref(script_ctx_t *ctx)
314 {
315     ctx->ref++;
316 }
317
318 HRESULT init_global(script_ctx_t*);
319 HRESULT init_function_constr(script_ctx_t*,jsdisp_t*);
320 HRESULT create_object_prototype(script_ctx_t*,jsdisp_t**);
321
322 HRESULT create_activex_constr(script_ctx_t*,jsdisp_t**);
323 HRESULT create_array_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
324 HRESULT create_bool_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
325 HRESULT create_date_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
326 HRESULT init_error_constr(script_ctx_t*,jsdisp_t*);
327 HRESULT create_number_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
328 HRESULT create_object_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
329 HRESULT create_regexp_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
330 HRESULT create_string_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
331 HRESULT create_vbarray_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
332
333 IUnknown *create_ax_site(script_ctx_t*);
334
335 typedef struct {
336     const WCHAR *str;
337     DWORD len;
338 } match_result_t;
339
340 #define REM_CHECK_GLOBAL   0x0001
341 #define REM_RESET_INDEX    0x0002
342 #define REM_NO_CTX_UPDATE  0x0004
343 HRESULT regexp_match_next(script_ctx_t*,jsdisp_t*,DWORD,const WCHAR*,DWORD,const WCHAR**,match_result_t**,
344         DWORD*,DWORD*,match_result_t*);
345 HRESULT regexp_match(script_ctx_t*,jsdisp_t*,const WCHAR*,DWORD,BOOL,match_result_t**,DWORD*);
346 HRESULT parse_regexp_flags(const WCHAR*,DWORD,DWORD*);
347 HRESULT regexp_string_match(script_ctx_t*,jsdisp_t*,BSTR,VARIANT*,jsexcept_t*);
348
349 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
350 {
351     return dp->rgvarg + dp->cArgs-i-1;
352 }
353
354 static inline DWORD arg_cnt(const DISPPARAMS *dp)
355 {
356     return dp->cArgs - dp->cNamedArgs;
357 }
358
359 static inline BOOL is_class(jsdisp_t *jsdisp, jsclass_t class)
360 {
361     return jsdisp->builtin_info->class == class;
362 }
363
364 static inline BOOL is_vclass(vdisp_t *vdisp, jsclass_t class)
365 {
366     return is_jsdisp(vdisp) && is_class(vdisp->u.jsdisp, class);
367 }
368
369 static inline BOOL is_num_vt(enum VARENUM vt)
370 {
371     return vt == VT_I4 || vt == VT_R8;
372 }
373
374 static inline DOUBLE num_val(const VARIANT *v)
375 {
376     return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
377 }
378
379 static inline void num_set_val(VARIANT *v, DOUBLE d)
380 {
381     if(d == (DOUBLE)(INT)d) {
382         V_VT(v) = VT_I4;
383         V_I4(v) = d;
384     }else {
385         V_VT(v) = VT_R8;
386         V_R8(v) = d;
387     }
388 }
389
390 static inline void num_set_nan(VARIANT *v)
391 {
392     V_VT(v) = VT_R8;
393 #ifdef NAN
394     V_R8(v) = NAN;
395 #else
396     V_UI8(v) = (ULONGLONG)0x7ff80000<<32;
397 #endif
398 }
399
400 static inline DOUBLE ret_nan(void)
401 {
402     VARIANT v;
403     num_set_nan(&v);
404     return V_R8(&v);
405 }
406
407 static inline void num_set_inf(VARIANT *v, BOOL positive)
408 {
409     V_VT(v) = VT_R8;
410 #ifdef INFINITY
411     V_R8(v) = positive ? INFINITY : -INFINITY;
412 #else
413     V_UI8(v) = (ULONGLONG)0x7ff00000<<32;
414     if(!positive)
415         V_R8(v) = -V_R8(v);
416 #endif
417 }
418
419 static inline void var_set_jsdisp(VARIANT *v, jsdisp_t *jsdisp)
420 {
421     V_VT(v) = VT_DISPATCH;
422     V_DISPATCH(v) = to_disp(jsdisp);
423 }
424
425 static inline DWORD make_grfdex(script_ctx_t *ctx, DWORD flags)
426 {
427     return (ctx->version << 28) | flags;
428 }
429
430 const char *debugstr_variant(const VARIANT*);
431
432 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
433
434 extern LONG module_ref;
435
436 static inline void lock_module(void)
437 {
438     InterlockedIncrement(&module_ref);
439 }
440
441 static inline void unlock_module(void)
442 {
443     InterlockedDecrement(&module_ref);
444 }
445
446 static inline void *heap_alloc(size_t len)
447 {
448     return HeapAlloc(GetProcessHeap(), 0, len);
449 }
450
451 static inline void *heap_alloc_zero(size_t len)
452 {
453     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
454 }
455
456 static inline void *heap_realloc(void *mem, size_t len)
457 {
458     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
459 }
460
461 static inline BOOL heap_free(void *mem)
462 {
463     return HeapFree(GetProcessHeap(), 0, mem);
464 }
465
466 static inline LPWSTR heap_strdupW(LPCWSTR str)
467 {
468     LPWSTR ret = NULL;
469
470     if(str) {
471         DWORD size;
472
473         size = (strlenW(str)+1)*sizeof(WCHAR);
474         ret = heap_alloc(size);
475         memcpy(ret, str, size);
476     }
477
478     return ret;
479 }
480
481 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))