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