jscript: Added String default value implementation.
[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_FUNCTION,
71     JSCLASS_GLOBAL,
72     JSCLASS_MATH,
73     JSCLASS_NUMBER,
74     JSCLASS_OBJECT,
75     JSCLASS_REGEXP,
76     JSCLASS_STRING
77 } jsclass_t;
78
79 typedef HRESULT (*builtin_invoke_t)(DispatchEx*,LCID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
80
81 typedef struct {
82     const WCHAR *name;
83     builtin_invoke_t invoke;
84     DWORD flags;
85 } builtin_prop_t;
86
87 typedef struct {
88     jsclass_t class;
89     builtin_prop_t value_prop;
90     DWORD props_cnt;
91     const builtin_prop_t *props;
92     void (*destructor)(DispatchEx*);
93     void (*on_put)(DispatchEx*,const WCHAR*);
94 } builtin_info_t;
95
96 struct DispatchEx {
97     const IDispatchExVtbl  *lpIDispatchExVtbl;
98
99     LONG ref;
100
101     DWORD buf_size;
102     DWORD prop_cnt;
103     dispex_prop_t *props;
104     script_ctx_t *ctx;
105
106     DispatchEx *prototype;
107
108     const builtin_info_t *builtin_info;
109 };
110
111 #define _IDispatchEx_(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
112
113 static inline void jsdisp_release(DispatchEx *jsdisp)
114 {
115     IDispatchEx_Release(_IDispatchEx_(jsdisp));
116 }
117
118 HRESULT create_dispex(script_ctx_t*,const builtin_info_t*,DispatchEx*,DispatchEx**);
119 HRESULT init_dispex(DispatchEx*,script_ctx_t*,const builtin_info_t*,DispatchEx*);
120 HRESULT init_dispex_from_constr(DispatchEx*,script_ctx_t*,const builtin_info_t*,DispatchEx*);
121 DispatchEx *iface_to_jsdisp(IUnknown*);
122
123 HRESULT disp_call(IDispatch*,DISPID,LCID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
124 HRESULT jsdisp_call_value(DispatchEx*,LCID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
125 HRESULT disp_propget(IDispatch*,DISPID,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
126 HRESULT disp_propput(IDispatch*,DISPID,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
127 HRESULT jsdisp_propput_name(DispatchEx*,const WCHAR*,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
128 HRESULT jsdisp_propput_idx(DispatchEx*,DWORD,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
129 HRESULT jsdisp_propget_idx(DispatchEx*,DWORD,LCID,VARIANT*,jsexcept_t*,IServiceProvider*);
130
131 HRESULT create_builtin_function(script_ctx_t*,builtin_invoke_t,DWORD,DispatchEx*,DispatchEx**);
132
133 HRESULT create_object(script_ctx_t*,DispatchEx*,DispatchEx**);
134 HRESULT create_math(script_ctx_t*,DispatchEx**);
135 HRESULT create_array(script_ctx_t*,DWORD,DispatchEx**);
136 HRESULT create_regexp_str(script_ctx_t*,const WCHAR*,DWORD,const WCHAR*,DWORD,DispatchEx**);
137 HRESULT create_string(script_ctx_t*,const WCHAR*,DWORD,DispatchEx**);
138 HRESULT create_bool(script_ctx_t*,VARIANT_BOOL,DispatchEx**);
139 HRESULT create_number(script_ctx_t*,VARIANT*,DispatchEx**);
140
141 HRESULT to_primitive(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
142 HRESULT to_boolean(VARIANT*,VARIANT_BOOL*);
143 HRESULT to_number(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
144 HRESULT to_integer(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
145 HRESULT to_int32(script_ctx_t*,VARIANT*,jsexcept_t*,INT*);
146 HRESULT to_uint32(script_ctx_t*,VARIANT*,jsexcept_t*,DWORD*);
147 HRESULT to_string(script_ctx_t*,VARIANT*,jsexcept_t*,BSTR*);
148 HRESULT to_object(exec_ctx_t*,VARIANT*,IDispatch**);
149
150 typedef struct named_item_t {
151     IDispatch *disp;
152     DWORD flags;
153
154     struct named_item_t *next;
155 } named_item_t;
156
157 struct _script_ctx_t {
158     LONG ref;
159
160     SCRIPTSTATE state;
161     exec_ctx_t *exec_ctx;
162     named_item_t *named_items;
163     LCID lcid;
164
165     jsheap_t tmp_heap;
166
167     DispatchEx *script_disp;
168     DispatchEx *global;
169     DispatchEx *function_constr;
170     DispatchEx *array_constr;
171     DispatchEx *bool_constr;
172     DispatchEx *number_constr;
173     DispatchEx *object_constr;
174     DispatchEx *regexp_constr;
175     DispatchEx *string_constr;
176 };
177
178 void script_release(script_ctx_t*);
179
180 static inline void script_addref(script_ctx_t *ctx)
181 {
182     ctx->ref++;
183 }
184
185 HRESULT init_global(script_ctx_t*);
186 HRESULT init_function_constr(script_ctx_t*);
187
188 HRESULT create_array_constr(script_ctx_t*,DispatchEx**);
189 HRESULT create_bool_constr(script_ctx_t*,DispatchEx**);
190 HRESULT create_number_constr(script_ctx_t*,DispatchEx**);
191 HRESULT create_object_constr(script_ctx_t*,DispatchEx**);
192 HRESULT create_regexp_constr(script_ctx_t*,DispatchEx**);
193 HRESULT create_string_constr(script_ctx_t*,DispatchEx**);
194
195 typedef struct {
196     const WCHAR *str;
197     DWORD len;
198 } match_result_t;
199
200 HRESULT regexp_match(DispatchEx*,const WCHAR*,DWORD,BOOL,match_result_t**,DWORD*);
201
202 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
203 {
204     return dp->rgvarg + dp->cArgs-i-1;
205 }
206
207 static inline DWORD arg_cnt(const DISPPARAMS *dp)
208 {
209     return dp->cArgs - dp->cNamedArgs;
210 }
211
212 static inline BOOL is_class(DispatchEx *jsdisp, jsclass_t class)
213 {
214     return jsdisp->builtin_info->class == class;
215 }
216
217 static inline BOOL is_num_vt(enum VARENUM vt)
218 {
219     return vt == VT_I4 || vt == VT_R8;
220 }
221
222 static inline DOUBLE num_val(const VARIANT *v)
223 {
224     return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
225 }
226
227 static inline void num_set_val(VARIANT *v, DOUBLE d)
228 {
229     if(d == (DOUBLE)(INT)d) {
230         V_VT(v) = VT_I4;
231         V_I4(v) = d;
232     }else {
233         V_VT(v) = VT_R8;
234         V_R8(v) = d;
235     }
236 }
237
238 const char *debugstr_variant(const VARIANT*);
239
240 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
241
242 extern LONG module_ref;
243
244 static inline void lock_module(void)
245 {
246     InterlockedIncrement(&module_ref);
247 }
248
249 static inline void unlock_module(void)
250 {
251     InterlockedDecrement(&module_ref);
252 }
253
254 static inline void *heap_alloc(size_t len)
255 {
256     return HeapAlloc(GetProcessHeap(), 0, len);
257 }
258
259 static inline void *heap_alloc_zero(size_t len)
260 {
261     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
262 }
263
264 static inline void *heap_realloc(void *mem, size_t len)
265 {
266     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
267 }
268
269 static inline BOOL heap_free(void *mem)
270 {
271     return HeapFree(GetProcessHeap(), 0, mem);
272 }
273
274 static inline LPWSTR heap_strdupW(LPCWSTR str)
275 {
276     LPWSTR ret = NULL;
277
278     if(str) {
279         DWORD size;
280
281         size = (strlenW(str)+1)*sizeof(WCHAR);
282         ret = heap_alloc(size);
283         memcpy(ret, str, size);
284     }
285
286     return ret;
287 }
288
289 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))