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