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