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