msxml3: Add schema parse/validate error callbacks.
[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 HRESULT create_vbarray(script_ctx_t*,SAFEARRAY*,jsdisp_t**);
247
248 typedef enum {
249     NO_HINT,
250     HINT_STRING,
251     HINT_NUMBER
252 } hint_t;
253
254 HRESULT to_primitive(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*, hint_t);
255 HRESULT to_boolean(VARIANT*,VARIANT_BOOL*);
256 HRESULT to_number(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
257 HRESULT to_integer(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
258 HRESULT to_int32(script_ctx_t*,VARIANT*,jsexcept_t*,INT*);
259 HRESULT to_uint32(script_ctx_t*,VARIANT*,jsexcept_t*,DWORD*);
260 HRESULT to_string(script_ctx_t*,VARIANT*,jsexcept_t*,BSTR*);
261 HRESULT to_object(script_ctx_t*,VARIANT*,IDispatch**);
262
263 typedef struct named_item_t {
264     IDispatch *disp;
265     DWORD flags;
266     LPWSTR name;
267
268     struct named_item_t *next;
269 } named_item_t;
270
271 struct _script_ctx_t {
272     LONG ref;
273
274     SCRIPTSTATE state;
275     exec_ctx_t *exec_ctx;
276     named_item_t *named_items;
277     IActiveScriptSite *site;
278     IInternetHostSecurityManager *secmgr;
279     DWORD safeopt;
280     DWORD version;
281     LCID lcid;
282
283     jsheap_t tmp_heap;
284
285     IDispatch *host_global;
286
287     BSTR last_match;
288     DWORD last_match_index;
289     DWORD last_match_length;
290
291     jsdisp_t *global;
292     jsdisp_t *function_constr;
293     jsdisp_t *activex_constr;
294     jsdisp_t *array_constr;
295     jsdisp_t *bool_constr;
296     jsdisp_t *date_constr;
297     jsdisp_t *error_constr;
298     jsdisp_t *eval_error_constr;
299     jsdisp_t *range_error_constr;
300     jsdisp_t *reference_error_constr;
301     jsdisp_t *regexp_error_constr;
302     jsdisp_t *syntax_error_constr;
303     jsdisp_t *type_error_constr;
304     jsdisp_t *uri_error_constr;
305     jsdisp_t *number_constr;
306     jsdisp_t *object_constr;
307     jsdisp_t *regexp_constr;
308     jsdisp_t *string_constr;
309     jsdisp_t *vbarray_constr;
310 };
311
312 void script_release(script_ctx_t*);
313
314 static inline void script_addref(script_ctx_t *ctx)
315 {
316     ctx->ref++;
317 }
318
319 HRESULT init_global(script_ctx_t*);
320 HRESULT init_function_constr(script_ctx_t*,jsdisp_t*);
321 HRESULT create_object_prototype(script_ctx_t*,jsdisp_t**);
322
323 HRESULT create_activex_constr(script_ctx_t*,jsdisp_t**);
324 HRESULT create_array_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
325 HRESULT create_bool_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
326 HRESULT create_date_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
327 HRESULT init_error_constr(script_ctx_t*,jsdisp_t*);
328 HRESULT create_number_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
329 HRESULT create_object_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
330 HRESULT create_regexp_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
331 HRESULT create_string_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
332 HRESULT create_vbarray_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
333
334 IUnknown *create_ax_site(script_ctx_t*);
335
336 typedef struct {
337     const WCHAR *str;
338     DWORD len;
339 } match_result_t;
340
341 #define REM_CHECK_GLOBAL   0x0001
342 #define REM_RESET_INDEX    0x0002
343 #define REM_NO_CTX_UPDATE  0x0004
344 HRESULT regexp_match_next(script_ctx_t*,jsdisp_t*,DWORD,const WCHAR*,DWORD,const WCHAR**,match_result_t**,
345         DWORD*,DWORD*,match_result_t*);
346 HRESULT regexp_match(script_ctx_t*,jsdisp_t*,const WCHAR*,DWORD,BOOL,match_result_t**,DWORD*);
347 HRESULT parse_regexp_flags(const WCHAR*,DWORD,DWORD*);
348 HRESULT regexp_string_match(script_ctx_t*,jsdisp_t*,BSTR,VARIANT*,jsexcept_t*);
349
350 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
351 {
352     return dp->rgvarg + dp->cArgs-i-1;
353 }
354
355 static inline DWORD arg_cnt(const DISPPARAMS *dp)
356 {
357     return dp->cArgs - dp->cNamedArgs;
358 }
359
360 static inline BOOL is_class(jsdisp_t *jsdisp, jsclass_t class)
361 {
362     return jsdisp->builtin_info->class == class;
363 }
364
365 static inline BOOL is_vclass(vdisp_t *vdisp, jsclass_t class)
366 {
367     return is_jsdisp(vdisp) && is_class(vdisp->u.jsdisp, class);
368 }
369
370 static inline BOOL is_num_vt(enum VARENUM vt)
371 {
372     return vt == VT_I4 || vt == VT_R8;
373 }
374
375 static inline DOUBLE num_val(const VARIANT *v)
376 {
377     return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
378 }
379
380 static inline void num_set_int(VARIANT *v, INT i)
381 {
382     V_VT(v) = VT_I4;
383     V_I4(v) = i;
384 }
385
386 static inline void num_set_val(VARIANT *v, DOUBLE d)
387 {
388     if(d == (DOUBLE)(INT)d) {
389         V_VT(v) = VT_I4;
390         V_I4(v) = d;
391     }else {
392         V_VT(v) = VT_R8;
393         V_R8(v) = d;
394     }
395 }
396
397 static inline void num_set_nan(VARIANT *v)
398 {
399     V_VT(v) = VT_R8;
400 #ifdef NAN
401     V_R8(v) = NAN;
402 #else
403     V_UI8(v) = (ULONGLONG)0x7ff80000<<32;
404 #endif
405 }
406
407 static inline DOUBLE ret_nan(void)
408 {
409     VARIANT v;
410     num_set_nan(&v);
411     return V_R8(&v);
412 }
413
414 static inline void num_set_inf(VARIANT *v, BOOL positive)
415 {
416     V_VT(v) = VT_R8;
417 #ifdef INFINITY
418     V_R8(v) = positive ? INFINITY : -INFINITY;
419 #else
420     V_UI8(v) = (ULONGLONG)0x7ff00000<<32;
421     if(!positive)
422         V_R8(v) = -V_R8(v);
423 #endif
424 }
425
426 static inline void var_set_jsdisp(VARIANT *v, jsdisp_t *jsdisp)
427 {
428     V_VT(v) = VT_DISPATCH;
429     V_DISPATCH(v) = to_disp(jsdisp);
430 }
431
432 static inline DWORD make_grfdex(script_ctx_t *ctx, DWORD flags)
433 {
434     return (ctx->version << 28) | flags;
435 }
436
437 const char *debugstr_variant(const VARIANT*);
438
439 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
440
441 extern LONG module_ref;
442
443 static inline void lock_module(void)
444 {
445     InterlockedIncrement(&module_ref);
446 }
447
448 static inline void unlock_module(void)
449 {
450     InterlockedDecrement(&module_ref);
451 }
452
453 static inline void *heap_alloc(size_t len)
454 {
455     return HeapAlloc(GetProcessHeap(), 0, len);
456 }
457
458 static inline void *heap_alloc_zero(size_t len)
459 {
460     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
461 }
462
463 static inline void *heap_realloc(void *mem, size_t len)
464 {
465     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
466 }
467
468 static inline BOOL heap_free(void *mem)
469 {
470     return HeapFree(GetProcessHeap(), 0, mem);
471 }
472
473 static inline LPWSTR heap_strdupW(LPCWSTR str)
474 {
475     LPWSTR ret = NULL;
476
477     if(str) {
478         DWORD size;
479
480         size = (strlenW(str)+1)*sizeof(WCHAR);
481         ret = heap_alloc(size);
482         memcpy(ret, str, size);
483     }
484
485     return ret;
486 }
487
488 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))