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