jscript: Add Error_number handling to constructor and error throwing functions.
[wine] / dlls / jscript / jscript.h
1 /*
2  * Copyright 2008 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 typedef struct _script_ctx_t script_ctx_t;
37 typedef struct _exec_ctx_t exec_ctx_t;
38 typedef struct _dispex_prop_t dispex_prop_t;
39
40 typedef struct {
41     EXCEPINFO ei;
42     VARIANT var;
43 } jsexcept_t;
44
45 typedef struct {
46     void **blocks;
47     DWORD block_cnt;
48     DWORD last_block;
49     DWORD offset;
50     BOOL mark;
51     struct list custom_blocks;
52 } jsheap_t;
53
54 void jsheap_init(jsheap_t*);
55 void *jsheap_alloc(jsheap_t*,DWORD);
56 void *jsheap_grow(jsheap_t*,void*,DWORD,DWORD);
57 void jsheap_clear(jsheap_t*);
58 void jsheap_free(jsheap_t*);
59 jsheap_t *jsheap_mark(jsheap_t*);
60
61 typedef struct DispatchEx DispatchEx;
62
63 extern HINSTANCE jscript_hinstance;
64
65 #define PROPF_ARGMASK 0x00ff
66 #define PROPF_METHOD  0x0100
67 #define PROPF_ENUM    0x0200
68 #define PROPF_CONSTR  0x0400
69
70 typedef enum {
71     JSCLASS_NONE,
72     JSCLASS_ARRAY,
73     JSCLASS_BOOLEAN,
74     JSCLASS_DATE,
75     JSCLASS_ERROR,
76     JSCLASS_FUNCTION,
77     JSCLASS_GLOBAL,
78     JSCLASS_MATH,
79     JSCLASS_NUMBER,
80     JSCLASS_OBJECT,
81     JSCLASS_REGEXP,
82     JSCLASS_STRING
83 } jsclass_t;
84
85 typedef HRESULT (*builtin_invoke_t)(DispatchEx*,LCID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
86
87 typedef struct {
88     const WCHAR *name;
89     builtin_invoke_t invoke;
90     DWORD flags;
91 } builtin_prop_t;
92
93 typedef struct {
94     jsclass_t class;
95     builtin_prop_t value_prop;
96     DWORD props_cnt;
97     const builtin_prop_t *props;
98     void (*destructor)(DispatchEx*);
99     void (*on_put)(DispatchEx*,const WCHAR*);
100 } builtin_info_t;
101
102 struct DispatchEx {
103     const IDispatchExVtbl  *lpIDispatchExVtbl;
104
105     LONG ref;
106
107     DWORD buf_size;
108     DWORD prop_cnt;
109     dispex_prop_t *props;
110     script_ctx_t *ctx;
111
112     DispatchEx *prototype;
113
114     const builtin_info_t *builtin_info;
115 };
116
117 #define _IDispatchEx_(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
118
119 static inline void jsdisp_release(DispatchEx *jsdisp)
120 {
121     IDispatchEx_Release(_IDispatchEx_(jsdisp));
122 }
123
124 HRESULT create_dispex(script_ctx_t*,const builtin_info_t*,DispatchEx*,DispatchEx**);
125 HRESULT init_dispex(DispatchEx*,script_ctx_t*,const builtin_info_t*,DispatchEx*);
126 HRESULT init_dispex_from_constr(DispatchEx*,script_ctx_t*,const builtin_info_t*,DispatchEx*);
127 DispatchEx *iface_to_jsdisp(IUnknown*);
128
129 HRESULT disp_call(IDispatch*,DISPID,LCID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
130 HRESULT jsdisp_call_value(DispatchEx*,LCID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
131 HRESULT jsdisp_call(DispatchEx*,DISPID,LCID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
132 HRESULT disp_propget(IDispatch*,DISPID,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
133 HRESULT disp_propput(IDispatch*,DISPID,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
134 HRESULT jsdisp_propget(DispatchEx*,DISPID,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
135 HRESULT jsdisp_propput_name(DispatchEx*,const WCHAR*,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
136 HRESULT jsdisp_propput_idx(DispatchEx*,DWORD,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
137 HRESULT jsdisp_propget_name(DispatchEx*,LPCWSTR,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
138 HRESULT jsdisp_propget_idx(DispatchEx*,DWORD,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
139 HRESULT jsdisp_get_id(DispatchEx*,const WCHAR*,DWORD,DISPID*);
140 HRESULT jsdisp_delete_idx(DispatchEx*,DWORD);
141
142 HRESULT create_builtin_function(script_ctx_t*,builtin_invoke_t,const builtin_info_t*,DWORD,
143         DispatchEx*,DispatchEx**);
144 HRESULT Function_value(DispatchEx*,LCID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
145
146 HRESULT throw_eval_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
147 HRESULT throw_range_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
148 HRESULT throw_reference_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
149 HRESULT throw_syntax_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
150 HRESULT throw_type_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
151 HRESULT throw_uri_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
152
153
154 HRESULT create_object(script_ctx_t*,DispatchEx*,DispatchEx**);
155 HRESULT create_math(script_ctx_t*,DispatchEx**);
156 HRESULT create_array(script_ctx_t*,DWORD,DispatchEx**);
157 HRESULT create_regexp_str(script_ctx_t*,const WCHAR*,DWORD,const WCHAR*,DWORD,DispatchEx**);
158 HRESULT create_string(script_ctx_t*,const WCHAR*,DWORD,DispatchEx**);
159 HRESULT create_bool(script_ctx_t*,VARIANT_BOOL,DispatchEx**);
160 HRESULT create_number(script_ctx_t*,VARIANT*,DispatchEx**);
161
162 typedef enum {
163     NO_HINT,
164     HINT_STRING,
165     HINT_NUMBER
166 } hint_t;
167
168 HRESULT to_primitive(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*, hint_t);
169 HRESULT to_boolean(VARIANT*,VARIANT_BOOL*);
170 HRESULT to_number(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
171 HRESULT to_integer(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
172 HRESULT to_int32(script_ctx_t*,VARIANT*,jsexcept_t*,INT*);
173 HRESULT to_uint32(script_ctx_t*,VARIANT*,jsexcept_t*,DWORD*);
174 HRESULT to_string(script_ctx_t*,VARIANT*,jsexcept_t*,BSTR*);
175 HRESULT to_object(exec_ctx_t*,VARIANT*,IDispatch**);
176
177 typedef struct named_item_t {
178     IDispatch *disp;
179     DWORD flags;
180     LPWSTR name;
181
182     struct named_item_t *next;
183 } named_item_t;
184
185 struct _script_ctx_t {
186     LONG ref;
187
188     SCRIPTSTATE state;
189     exec_ctx_t *exec_ctx;
190     named_item_t *named_items;
191     IActiveScriptSite *site;
192     LCID lcid;
193
194     jsheap_t tmp_heap;
195
196     DispatchEx *script_disp;
197     DispatchEx *global;
198     DispatchEx *function_constr;
199     DispatchEx *array_constr;
200     DispatchEx *bool_constr;
201     DispatchEx *date_constr;
202     DispatchEx *error_constr;
203     DispatchEx *eval_error_constr;
204     DispatchEx *range_error_constr;
205     DispatchEx *reference_error_constr;
206     DispatchEx *syntax_error_constr;
207     DispatchEx *type_error_constr;
208     DispatchEx *uri_error_constr;
209     DispatchEx *number_constr;
210     DispatchEx *object_constr;
211     DispatchEx *regexp_constr;
212     DispatchEx *string_constr;
213 };
214
215 void script_release(script_ctx_t*);
216
217 static inline void script_addref(script_ctx_t *ctx)
218 {
219     ctx->ref++;
220 }
221
222 HRESULT init_global(script_ctx_t*);
223 HRESULT init_function_constr(script_ctx_t*,DispatchEx*);
224 HRESULT create_object_prototype(script_ctx_t*,DispatchEx**);
225
226 HRESULT create_array_constr(script_ctx_t*,DispatchEx**);
227 HRESULT create_bool_constr(script_ctx_t*,DispatchEx**);
228 HRESULT create_date_constr(script_ctx_t*,DispatchEx**);
229 HRESULT init_error_constr(script_ctx_t*);
230 HRESULT create_number_constr(script_ctx_t*,DispatchEx**);
231 HRESULT create_object_constr(script_ctx_t*,DispatchEx*,DispatchEx**);
232 HRESULT create_regexp_constr(script_ctx_t*,DispatchEx**);
233 HRESULT create_string_constr(script_ctx_t*,DispatchEx**);
234
235 typedef struct {
236     const WCHAR *str;
237     DWORD len;
238 } match_result_t;
239
240 HRESULT regexp_match_next(DispatchEx*,BOOL,const WCHAR*,DWORD,const WCHAR**,match_result_t**,
241         DWORD*,DWORD*,match_result_t*);
242 HRESULT regexp_match(DispatchEx*,const WCHAR*,DWORD,BOOL,match_result_t**,DWORD*);
243
244 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
245 {
246     return dp->rgvarg + dp->cArgs-i-1;
247 }
248
249 static inline DWORD arg_cnt(const DISPPARAMS *dp)
250 {
251     return dp->cArgs - dp->cNamedArgs;
252 }
253
254 static inline BOOL is_class(DispatchEx *jsdisp, jsclass_t class)
255 {
256     return jsdisp->builtin_info->class == class;
257 }
258
259 static inline BOOL is_num_vt(enum VARENUM vt)
260 {
261     return vt == VT_I4 || vt == VT_R8;
262 }
263
264 static inline DOUBLE num_val(const VARIANT *v)
265 {
266     return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
267 }
268
269 static inline void num_set_val(VARIANT *v, DOUBLE d)
270 {
271     if(d == (DOUBLE)(INT)d) {
272         V_VT(v) = VT_I4;
273         V_I4(v) = d;
274     }else {
275         V_VT(v) = VT_R8;
276         V_R8(v) = d;
277     }
278 }
279
280 static inline void num_set_nan(VARIANT *v)
281 {
282     V_VT(v) = VT_R8;
283 #ifdef NAN
284     V_R8(v) = NAN;
285 #else
286     V_UI8(v) = (ULONGLONG)0x7ff80000<<32;
287 #endif
288 }
289
290 static inline DOUBLE ret_nan()
291 {
292     VARIANT v;
293     num_set_nan(&v);
294     return V_R8(&v);
295 }
296
297 static inline void num_set_inf(VARIANT *v, BOOL positive)
298 {
299     V_VT(v) = VT_R8;
300 #ifdef INFINITY
301     V_R8(v) = positive ? INFINITY : -INFINITY;
302 #else
303     V_UI8(v) = (ULONGLONG)0x7ff00000<<32;
304     if(!positive)
305         V_R8(v) = -V_R8(v);
306 #endif
307 }
308
309 const char *debugstr_variant(const VARIANT*);
310
311 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
312
313 extern LONG module_ref;
314
315 static inline void lock_module(void)
316 {
317     InterlockedIncrement(&module_ref);
318 }
319
320 static inline void unlock_module(void)
321 {
322     InterlockedDecrement(&module_ref);
323 }
324
325 static inline void *heap_alloc(size_t len)
326 {
327     return HeapAlloc(GetProcessHeap(), 0, len);
328 }
329
330 static inline void *heap_alloc_zero(size_t len)
331 {
332     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
333 }
334
335 static inline void *heap_realloc(void *mem, size_t len)
336 {
337     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
338 }
339
340 static inline BOOL heap_free(void *mem)
341 {
342     return HeapFree(GetProcessHeap(), 0, mem);
343 }
344
345 static inline LPWSTR heap_strdupW(LPCWSTR str)
346 {
347     LPWSTR ret = NULL;
348
349     if(str) {
350         DWORD size;
351
352         size = (strlenW(str)+1)*sizeof(WCHAR);
353         ret = heap_alloc(size);
354         memcpy(ret, str, size);
355     }
356
357     return ret;
358 }
359
360 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))