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