jscript: Add a missing terminator to a string constant.
[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_t;
89
90 jsdisp_t *iface_to_jsdisp(IUnknown*);
91
92 typedef struct {
93     union {
94         IDispatch *disp;
95         IDispatchEx *dispex;
96         jsdisp_t *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, jsdisp_t *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     jsdisp_t *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 jsdisp_t *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)(jsdisp_t*);
170     void (*on_put)(jsdisp_t*,const WCHAR*);
171 } builtin_info_t;
172
173 struct jsdisp_t {
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     jsdisp_t *prototype;
184
185     const builtin_info_t *builtin_info;
186 };
187
188 #define _IDispatchEx_(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
189
190 static inline IDispatch *to_disp(jsdisp_t *jsdisp)
191 {
192     return (IDispatch*)&jsdisp->lpIDispatchExVtbl;
193 }
194
195 static inline void jsdisp_addref(jsdisp_t *jsdisp)
196 {
197     IDispatchEx_AddRef(_IDispatchEx_(jsdisp));
198 }
199
200 static inline void jsdisp_release(jsdisp_t *jsdisp)
201 {
202     IDispatchEx_Release(_IDispatchEx_(jsdisp));
203 }
204
205 HRESULT create_dispex(script_ctx_t*,const builtin_info_t*,jsdisp_t*,jsdisp_t**);
206 HRESULT init_dispex(jsdisp_t*,script_ctx_t*,const builtin_info_t*,jsdisp_t*);
207 HRESULT init_dispex_from_constr(jsdisp_t*,script_ctx_t*,const builtin_info_t*,jsdisp_t*);
208
209 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
210 HRESULT jsdisp_call_value(jsdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
211 HRESULT jsdisp_call(jsdisp_t*,DISPID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
212 HRESULT jsdisp_call_name(jsdisp_t*,const WCHAR*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
213 HRESULT disp_propget(script_ctx_t*,IDispatch*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
214 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
215 HRESULT jsdisp_propget(jsdisp_t*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
216 HRESULT jsdisp_propput_name(jsdisp_t*,const WCHAR*,VARIANT*,jsexcept_t*,IServiceProvider*);
217 HRESULT jsdisp_propput_const(jsdisp_t*,const WCHAR*,VARIANT*);
218 HRESULT jsdisp_propput_idx(jsdisp_t*,DWORD,VARIANT*,jsexcept_t*,IServiceProvider*);
219 HRESULT jsdisp_propget_name(jsdisp_t*,LPCWSTR,VARIANT*,jsexcept_t*,IServiceProvider*);
220 HRESULT jsdisp_get_idx(jsdisp_t*,DWORD,VARIANT*,jsexcept_t*,IServiceProvider*);
221 HRESULT jsdisp_get_id(jsdisp_t*,const WCHAR*,DWORD,DISPID*);
222 HRESULT jsdisp_delete_idx(jsdisp_t*,DWORD);
223
224 HRESULT create_builtin_function(script_ctx_t*,builtin_invoke_t,const WCHAR*,const builtin_info_t*,DWORD,
225         jsdisp_t*,jsdisp_t**);
226 HRESULT Function_value(script_ctx_t*,vdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
227
228 HRESULT throw_eval_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
229 HRESULT throw_generic_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
230 HRESULT throw_range_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
231 HRESULT throw_reference_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
232 HRESULT throw_regexp_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
233 HRESULT throw_syntax_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
234 HRESULT throw_type_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
235 HRESULT throw_uri_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
236
237 HRESULT create_object(script_ctx_t*,jsdisp_t*,jsdisp_t**);
238 HRESULT create_math(script_ctx_t*,jsdisp_t**);
239 HRESULT create_array(script_ctx_t*,DWORD,jsdisp_t**);
240 HRESULT create_regexp(script_ctx_t*,const WCHAR *,int,DWORD,jsdisp_t**);
241 HRESULT create_regexp_var(script_ctx_t*,VARIANT*,VARIANT*,jsdisp_t**);
242 HRESULT create_string(script_ctx_t*,const WCHAR*,DWORD,jsdisp_t**);
243 HRESULT create_bool(script_ctx_t*,VARIANT_BOOL,jsdisp_t**);
244 HRESULT create_number(script_ctx_t*,VARIANT*,jsdisp_t**);
245
246 typedef enum {
247     NO_HINT,
248     HINT_STRING,
249     HINT_NUMBER
250 } hint_t;
251
252 HRESULT to_primitive(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*, hint_t);
253 HRESULT to_boolean(VARIANT*,VARIANT_BOOL*);
254 HRESULT to_number(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
255 HRESULT to_integer(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
256 HRESULT to_int32(script_ctx_t*,VARIANT*,jsexcept_t*,INT*);
257 HRESULT to_uint32(script_ctx_t*,VARIANT*,jsexcept_t*,DWORD*);
258 HRESULT to_string(script_ctx_t*,VARIANT*,jsexcept_t*,BSTR*);
259 HRESULT to_object(script_ctx_t*,VARIANT*,IDispatch**);
260
261 typedef struct named_item_t {
262     IDispatch *disp;
263     DWORD flags;
264     LPWSTR name;
265
266     struct named_item_t *next;
267 } named_item_t;
268
269 struct _script_ctx_t {
270     LONG ref;
271
272     SCRIPTSTATE state;
273     exec_ctx_t *exec_ctx;
274     named_item_t *named_items;
275     IActiveScriptSite *site;
276     IInternetHostSecurityManager *secmgr;
277     DWORD safeopt;
278     DWORD version;
279     LCID lcid;
280
281     jsheap_t tmp_heap;
282
283     IDispatch *host_global;
284
285     BSTR last_match;
286     DWORD last_match_index;
287     DWORD last_match_length;
288
289     jsdisp_t *global;
290     jsdisp_t *function_constr;
291     jsdisp_t *activex_constr;
292     jsdisp_t *array_constr;
293     jsdisp_t *bool_constr;
294     jsdisp_t *date_constr;
295     jsdisp_t *error_constr;
296     jsdisp_t *eval_error_constr;
297     jsdisp_t *range_error_constr;
298     jsdisp_t *reference_error_constr;
299     jsdisp_t *regexp_error_constr;
300     jsdisp_t *syntax_error_constr;
301     jsdisp_t *type_error_constr;
302     jsdisp_t *uri_error_constr;
303     jsdisp_t *number_constr;
304     jsdisp_t *object_constr;
305     jsdisp_t *regexp_constr;
306     jsdisp_t *string_constr;
307 };
308
309 void script_release(script_ctx_t*);
310
311 static inline void script_addref(script_ctx_t *ctx)
312 {
313     ctx->ref++;
314 }
315
316 HRESULT init_global(script_ctx_t*);
317 HRESULT init_function_constr(script_ctx_t*,jsdisp_t*);
318 HRESULT create_object_prototype(script_ctx_t*,jsdisp_t**);
319
320 HRESULT create_activex_constr(script_ctx_t*,jsdisp_t**);
321 HRESULT create_array_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
322 HRESULT create_bool_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
323 HRESULT create_date_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
324 HRESULT init_error_constr(script_ctx_t*,jsdisp_t*);
325 HRESULT create_number_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
326 HRESULT create_object_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
327 HRESULT create_regexp_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
328 HRESULT create_string_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
329
330 IUnknown *create_ax_site(script_ctx_t*);
331
332 typedef struct {
333     const WCHAR *str;
334     DWORD len;
335 } match_result_t;
336
337 #define REM_CHECK_GLOBAL   0x0001
338 #define REM_RESET_INDEX    0x0002
339 #define REM_NO_CTX_UPDATE  0x0004
340 HRESULT regexp_match_next(script_ctx_t*,jsdisp_t*,DWORD,const WCHAR*,DWORD,const WCHAR**,match_result_t**,
341         DWORD*,DWORD*,match_result_t*);
342 HRESULT regexp_match(script_ctx_t*,jsdisp_t*,const WCHAR*,DWORD,BOOL,match_result_t**,DWORD*);
343 HRESULT parse_regexp_flags(const WCHAR*,DWORD,DWORD*);
344 HRESULT regexp_string_match(script_ctx_t*,jsdisp_t*,BSTR,VARIANT*,jsexcept_t*);
345
346 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
347 {
348     return dp->rgvarg + dp->cArgs-i-1;
349 }
350
351 static inline DWORD arg_cnt(const DISPPARAMS *dp)
352 {
353     return dp->cArgs - dp->cNamedArgs;
354 }
355
356 static inline BOOL is_class(jsdisp_t *jsdisp, jsclass_t class)
357 {
358     return jsdisp->builtin_info->class == class;
359 }
360
361 static inline BOOL is_vclass(vdisp_t *vdisp, jsclass_t class)
362 {
363     return is_jsdisp(vdisp) && is_class(vdisp->u.jsdisp, class);
364 }
365
366 static inline BOOL is_num_vt(enum VARENUM vt)
367 {
368     return vt == VT_I4 || vt == VT_R8;
369 }
370
371 static inline DOUBLE num_val(const VARIANT *v)
372 {
373     return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
374 }
375
376 static inline void num_set_val(VARIANT *v, DOUBLE d)
377 {
378     if(d == (DOUBLE)(INT)d) {
379         V_VT(v) = VT_I4;
380         V_I4(v) = d;
381     }else {
382         V_VT(v) = VT_R8;
383         V_R8(v) = d;
384     }
385 }
386
387 static inline void num_set_nan(VARIANT *v)
388 {
389     V_VT(v) = VT_R8;
390 #ifdef NAN
391     V_R8(v) = NAN;
392 #else
393     V_UI8(v) = (ULONGLONG)0x7ff80000<<32;
394 #endif
395 }
396
397 static inline DOUBLE ret_nan(void)
398 {
399     VARIANT v;
400     num_set_nan(&v);
401     return V_R8(&v);
402 }
403
404 static inline void num_set_inf(VARIANT *v, BOOL positive)
405 {
406     V_VT(v) = VT_R8;
407 #ifdef INFINITY
408     V_R8(v) = positive ? INFINITY : -INFINITY;
409 #else
410     V_UI8(v) = (ULONGLONG)0x7ff00000<<32;
411     if(!positive)
412         V_R8(v) = -V_R8(v);
413 #endif
414 }
415
416 static inline void var_set_jsdisp(VARIANT *v, jsdisp_t *jsdisp)
417 {
418     V_VT(v) = VT_DISPATCH;
419     V_DISPATCH(v) = to_disp(jsdisp);
420 }
421
422 static inline DWORD make_grfdex(script_ctx_t *ctx, DWORD flags)
423 {
424     return (ctx->version << 28) | flags;
425 }
426
427 const char *debugstr_variant(const VARIANT*);
428
429 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
430
431 extern LONG module_ref;
432
433 static inline void lock_module(void)
434 {
435     InterlockedIncrement(&module_ref);
436 }
437
438 static inline void unlock_module(void)
439 {
440     InterlockedDecrement(&module_ref);
441 }
442
443 static inline void *heap_alloc(size_t len)
444 {
445     return HeapAlloc(GetProcessHeap(), 0, len);
446 }
447
448 static inline void *heap_alloc_zero(size_t len)
449 {
450     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
451 }
452
453 static inline void *heap_realloc(void *mem, size_t len)
454 {
455     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
456 }
457
458 static inline BOOL heap_free(void *mem)
459 {
460     return HeapFree(GetProcessHeap(), 0, mem);
461 }
462
463 static inline LPWSTR heap_strdupW(LPCWSTR str)
464 {
465     LPWSTR ret = NULL;
466
467     if(str) {
468         DWORD size;
469
470         size = (strlenW(str)+1)*sizeof(WCHAR);
471         ret = heap_alloc(size);
472         memcpy(ret, str, size);
473     }
474
475     return ret;
476 }
477
478 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))