jscript: Store builtin constructor's length in instance object.
[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 DispatchEx DispatchEx;
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_t;
89
90 DispatchEx *iface_to_jsdisp(IUnknown*);
91
92 typedef struct {
93     union {
94         IDispatch *disp;
95         IDispatchEx *dispex;
96         DispatchEx *jsdisp;
97     } u;
98     DWORD flags;
99 } vdisp_t;
100
101 #define VDISP_DISPEX  0x0001
102 #define VDISP_JSDISP  0x0002
103
104 static inline void vdisp_release(vdisp_t *vdisp)
105 {
106     IDispatch_Release(vdisp->u.disp);
107 }
108
109 static inline BOOL is_jsdisp(vdisp_t *vdisp)
110 {
111     return (vdisp->flags & VDISP_JSDISP) != 0;
112 }
113
114 static inline BOOL is_dispex(vdisp_t *vdisp)
115 {
116     return (vdisp->flags & VDISP_DISPEX) != 0;
117 }
118
119 static inline void set_jsdisp(vdisp_t *vdisp, DispatchEx *jsdisp)
120 {
121     vdisp->u.jsdisp = jsdisp;
122     vdisp->flags = VDISP_JSDISP | VDISP_DISPEX;
123     IDispatch_AddRef(vdisp->u.disp);
124 }
125
126 static inline void set_disp(vdisp_t *vdisp, IDispatch *disp)
127 {
128     IDispatchEx *dispex;
129     DispatchEx *jsdisp;
130     HRESULT hres;
131
132     jsdisp = iface_to_jsdisp((IUnknown*)disp);
133     if(jsdisp) {
134         vdisp->u.jsdisp = jsdisp;
135         vdisp->flags = VDISP_JSDISP | VDISP_DISPEX;
136         return;
137     }
138
139     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
140     if(SUCCEEDED(hres)) {
141         vdisp->u.dispex = dispex;
142         vdisp->flags = VDISP_DISPEX;
143         return;
144     }
145
146     IDispatch_AddRef(disp);
147     vdisp->u.disp = disp;
148     vdisp->flags = 0;
149 }
150
151 static inline DispatchEx *get_jsdisp(vdisp_t *vdisp)
152 {
153     return is_jsdisp(vdisp) ? vdisp->u.jsdisp : NULL;
154 }
155
156 typedef HRESULT (*builtin_invoke_t)(script_ctx_t*,vdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
157
158 typedef struct {
159     const WCHAR *name;
160     builtin_invoke_t invoke;
161     DWORD flags;
162 } builtin_prop_t;
163
164 typedef struct {
165     jsclass_t class;
166     builtin_prop_t value_prop;
167     DWORD props_cnt;
168     const builtin_prop_t *props;
169     void (*destructor)(DispatchEx*);
170     void (*on_put)(DispatchEx*,const WCHAR*);
171 } builtin_info_t;
172
173 struct DispatchEx {
174     const IDispatchExVtbl  *lpIDispatchExVtbl;
175
176     LONG ref;
177
178     DWORD buf_size;
179     DWORD prop_cnt;
180     dispex_prop_t *props;
181     script_ctx_t *ctx;
182
183     DispatchEx *prototype;
184
185     const builtin_info_t *builtin_info;
186 };
187
188 #define _IDispatchEx_(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
189
190 static inline void jsdisp_release(DispatchEx *jsdisp)
191 {
192     IDispatchEx_Release(_IDispatchEx_(jsdisp));
193 }
194
195 HRESULT create_dispex(script_ctx_t*,const builtin_info_t*,DispatchEx*,DispatchEx**);
196 HRESULT init_dispex(DispatchEx*,script_ctx_t*,const builtin_info_t*,DispatchEx*);
197 HRESULT init_dispex_from_constr(DispatchEx*,script_ctx_t*,const builtin_info_t*,DispatchEx*);
198
199 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
200 HRESULT jsdisp_call_value(DispatchEx*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
201 HRESULT jsdisp_call(DispatchEx*,DISPID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
202 HRESULT jsdisp_call_name(DispatchEx*,const WCHAR*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
203 HRESULT disp_propget(script_ctx_t*,IDispatch*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
204 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
205 HRESULT jsdisp_propget(DispatchEx*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
206 HRESULT jsdisp_propput_name(DispatchEx*,const WCHAR*,VARIANT*,jsexcept_t*,IServiceProvider*);
207 HRESULT jsdisp_propput_const(DispatchEx*,const WCHAR*,VARIANT*);
208 HRESULT jsdisp_propput_idx(DispatchEx*,DWORD,VARIANT*,jsexcept_t*,IServiceProvider*);
209 HRESULT jsdisp_propget_name(DispatchEx*,LPCWSTR,VARIANT*,jsexcept_t*,IServiceProvider*);
210 HRESULT jsdisp_get_idx(DispatchEx*,DWORD,VARIANT*,jsexcept_t*,IServiceProvider*);
211 HRESULT jsdisp_get_id(DispatchEx*,const WCHAR*,DWORD,DISPID*);
212 HRESULT jsdisp_delete_idx(DispatchEx*,DWORD);
213
214 HRESULT create_builtin_function(script_ctx_t*,builtin_invoke_t,const WCHAR*,const builtin_info_t*,DWORD,
215         DispatchEx*,DispatchEx**);
216 HRESULT Function_value(script_ctx_t*,vdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
217
218 HRESULT throw_eval_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
219 HRESULT throw_generic_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
220 HRESULT throw_range_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
221 HRESULT throw_reference_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
222 HRESULT throw_regexp_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
223 HRESULT throw_syntax_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
224 HRESULT throw_type_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
225 HRESULT throw_uri_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
226
227 HRESULT create_object(script_ctx_t*,DispatchEx*,DispatchEx**);
228 HRESULT create_math(script_ctx_t*,DispatchEx**);
229 HRESULT create_array(script_ctx_t*,DWORD,DispatchEx**);
230 HRESULT create_regexp(script_ctx_t*,const WCHAR *,int,DWORD,DispatchEx**);
231 HRESULT create_regexp_var(script_ctx_t*,VARIANT*,VARIANT*,DispatchEx**);
232 HRESULT create_string(script_ctx_t*,const WCHAR*,DWORD,DispatchEx**);
233 HRESULT create_bool(script_ctx_t*,VARIANT_BOOL,DispatchEx**);
234 HRESULT create_number(script_ctx_t*,VARIANT*,DispatchEx**);
235
236 typedef enum {
237     NO_HINT,
238     HINT_STRING,
239     HINT_NUMBER
240 } hint_t;
241
242 HRESULT to_primitive(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*, hint_t);
243 HRESULT to_boolean(VARIANT*,VARIANT_BOOL*);
244 HRESULT to_number(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
245 HRESULT to_integer(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
246 HRESULT to_int32(script_ctx_t*,VARIANT*,jsexcept_t*,INT*);
247 HRESULT to_uint32(script_ctx_t*,VARIANT*,jsexcept_t*,DWORD*);
248 HRESULT to_string(script_ctx_t*,VARIANT*,jsexcept_t*,BSTR*);
249 HRESULT to_object(script_ctx_t*,VARIANT*,IDispatch**);
250
251 typedef struct named_item_t {
252     IDispatch *disp;
253     DWORD flags;
254     LPWSTR name;
255
256     struct named_item_t *next;
257 } named_item_t;
258
259 struct _script_ctx_t {
260     LONG ref;
261
262     SCRIPTSTATE state;
263     exec_ctx_t *exec_ctx;
264     named_item_t *named_items;
265     IActiveScriptSite *site;
266     IInternetHostSecurityManager *secmgr;
267     DWORD safeopt;
268     DWORD version;
269     LCID lcid;
270
271     jsheap_t tmp_heap;
272
273     IDispatch *host_global;
274
275     DispatchEx *global;
276     DispatchEx *function_constr;
277     DispatchEx *activex_constr;
278     DispatchEx *array_constr;
279     DispatchEx *bool_constr;
280     DispatchEx *date_constr;
281     DispatchEx *error_constr;
282     DispatchEx *eval_error_constr;
283     DispatchEx *range_error_constr;
284     DispatchEx *reference_error_constr;
285     DispatchEx *regexp_error_constr;
286     DispatchEx *syntax_error_constr;
287     DispatchEx *type_error_constr;
288     DispatchEx *uri_error_constr;
289     DispatchEx *number_constr;
290     DispatchEx *object_constr;
291     DispatchEx *regexp_constr;
292     DispatchEx *string_constr;
293 };
294
295 void script_release(script_ctx_t*);
296
297 static inline void script_addref(script_ctx_t *ctx)
298 {
299     ctx->ref++;
300 }
301
302 HRESULT init_global(script_ctx_t*);
303 HRESULT init_function_constr(script_ctx_t*,DispatchEx*);
304 HRESULT create_object_prototype(script_ctx_t*,DispatchEx**);
305
306 HRESULT create_activex_constr(script_ctx_t*,DispatchEx**);
307 HRESULT create_array_constr(script_ctx_t*,DispatchEx*,DispatchEx**);
308 HRESULT create_bool_constr(script_ctx_t*,DispatchEx*,DispatchEx**);
309 HRESULT create_date_constr(script_ctx_t*,DispatchEx*,DispatchEx**);
310 HRESULT init_error_constr(script_ctx_t*,DispatchEx*);
311 HRESULT create_number_constr(script_ctx_t*,DispatchEx*,DispatchEx**);
312 HRESULT create_object_constr(script_ctx_t*,DispatchEx*,DispatchEx**);
313 HRESULT create_regexp_constr(script_ctx_t*,DispatchEx*,DispatchEx**);
314 HRESULT create_string_constr(script_ctx_t*,DispatchEx*,DispatchEx**);
315
316 IUnknown *create_ax_site(script_ctx_t*);
317
318 typedef struct {
319     const WCHAR *str;
320     DWORD len;
321 } match_result_t;
322
323 #define REM_CHECK_GLOBAL   0x0001
324 #define REM_RESET_INDEX    0x0002
325 #define REM_NO_CTX_UPDATE  0x0004
326 HRESULT regexp_match_next(script_ctx_t*,DispatchEx*,DWORD,const WCHAR*,DWORD,const WCHAR**,match_result_t**,
327         DWORD*,DWORD*,match_result_t*);
328 HRESULT regexp_match(script_ctx_t*,DispatchEx*,const WCHAR*,DWORD,BOOL,match_result_t**,DWORD*);
329 HRESULT parse_regexp_flags(const WCHAR*,DWORD,DWORD*);
330 HRESULT regexp_string_match(script_ctx_t*,DispatchEx*,BSTR,VARIANT*,jsexcept_t*);
331
332 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
333 {
334     return dp->rgvarg + dp->cArgs-i-1;
335 }
336
337 static inline DWORD arg_cnt(const DISPPARAMS *dp)
338 {
339     return dp->cArgs - dp->cNamedArgs;
340 }
341
342 static inline BOOL is_class(DispatchEx *jsdisp, jsclass_t class)
343 {
344     return jsdisp->builtin_info->class == class;
345 }
346
347 static inline BOOL is_vclass(vdisp_t *vdisp, jsclass_t class)
348 {
349     return is_jsdisp(vdisp) && is_class(vdisp->u.jsdisp, class);
350 }
351
352 static inline BOOL is_num_vt(enum VARENUM vt)
353 {
354     return vt == VT_I4 || vt == VT_R8;
355 }
356
357 static inline DOUBLE num_val(const VARIANT *v)
358 {
359     return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
360 }
361
362 static inline void num_set_val(VARIANT *v, DOUBLE d)
363 {
364     if(d == (DOUBLE)(INT)d) {
365         V_VT(v) = VT_I4;
366         V_I4(v) = d;
367     }else {
368         V_VT(v) = VT_R8;
369         V_R8(v) = d;
370     }
371 }
372
373 static inline void num_set_nan(VARIANT *v)
374 {
375     V_VT(v) = VT_R8;
376 #ifdef NAN
377     V_R8(v) = NAN;
378 #else
379     V_UI8(v) = (ULONGLONG)0x7ff80000<<32;
380 #endif
381 }
382
383 static inline DOUBLE ret_nan(void)
384 {
385     VARIANT v;
386     num_set_nan(&v);
387     return V_R8(&v);
388 }
389
390 static inline void num_set_inf(VARIANT *v, BOOL positive)
391 {
392     V_VT(v) = VT_R8;
393 #ifdef INFINITY
394     V_R8(v) = positive ? INFINITY : -INFINITY;
395 #else
396     V_UI8(v) = (ULONGLONG)0x7ff00000<<32;
397     if(!positive)
398         V_R8(v) = -V_R8(v);
399 #endif
400 }
401
402 static inline DWORD make_grfdex(script_ctx_t *ctx, DWORD flags)
403 {
404     return (ctx->version << 28) | flags;
405 }
406
407 const char *debugstr_variant(const VARIANT*);
408
409 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
410
411 extern LONG module_ref;
412
413 static inline void lock_module(void)
414 {
415     InterlockedIncrement(&module_ref);
416 }
417
418 static inline void unlock_module(void)
419 {
420     InterlockedDecrement(&module_ref);
421 }
422
423 static inline void *heap_alloc(size_t len)
424 {
425     return HeapAlloc(GetProcessHeap(), 0, len);
426 }
427
428 static inline void *heap_alloc_zero(size_t len)
429 {
430     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
431 }
432
433 static inline void *heap_realloc(void *mem, size_t len)
434 {
435     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
436 }
437
438 static inline BOOL heap_free(void *mem)
439 {
440     return HeapFree(GetProcessHeap(), 0, mem);
441 }
442
443 static inline LPWSTR heap_strdupW(LPCWSTR str)
444 {
445     LPWSTR ret = NULL;
446
447     if(str) {
448         DWORD size;
449
450         size = (strlenW(str)+1)*sizeof(WCHAR);
451         ret = heap_alloc(size);
452         memcpy(ret, str, size);
453     }
454
455     return ret;
456 }
457
458 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))