2 * Copyright 2008-2009 Jacek Caban for CodeWeavers
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.
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.
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
33 #include "wine/unicode.h"
34 #include "wine/list.h"
36 #define JSCRIPT_ERROR 0x800A0000
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;
53 struct list custom_blocks;
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*);
63 typedef struct DispatchEx DispatchEx;
65 extern HINSTANCE jscript_hinstance;
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
73 /* NOTE: Keep in sync with names in Object.toString implementation */
90 DispatchEx *iface_to_jsdisp(IUnknown*);
101 #define VDISP_DISPEX 0x0001
102 #define VDISP_JSDISP 0x0002
104 static inline void vdisp_release(vdisp_t *vdisp)
106 IDispatch_Release(vdisp->u.disp);
109 static inline BOOL is_jsdisp(vdisp_t *vdisp)
111 return (vdisp->flags & VDISP_JSDISP) != 0;
114 static inline BOOL is_dispex(vdisp_t *vdisp)
116 return (vdisp->flags & VDISP_DISPEX) != 0;
119 static inline void set_jsdisp(vdisp_t *vdisp, DispatchEx *jsdisp)
121 vdisp->u.jsdisp = jsdisp;
122 vdisp->flags = VDISP_JSDISP | VDISP_DISPEX;
123 IDispatch_AddRef(vdisp->u.disp);
126 static inline void set_disp(vdisp_t *vdisp, IDispatch *disp)
132 jsdisp = iface_to_jsdisp((IUnknown*)disp);
134 vdisp->u.jsdisp = jsdisp;
135 vdisp->flags = VDISP_JSDISP | VDISP_DISPEX;
139 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
140 if(SUCCEEDED(hres)) {
141 vdisp->u.dispex = dispex;
142 vdisp->flags = VDISP_DISPEX;
146 IDispatch_AddRef(disp);
147 vdisp->u.disp = disp;
151 static inline DispatchEx *get_jsdisp(vdisp_t *vdisp)
153 return is_jsdisp(vdisp) ? vdisp->u.jsdisp : NULL;
156 typedef HRESULT (*builtin_invoke_t)(script_ctx_t*,vdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
160 builtin_invoke_t invoke;
166 builtin_prop_t value_prop;
168 const builtin_prop_t *props;
169 void (*destructor)(DispatchEx*);
170 void (*on_put)(DispatchEx*,const WCHAR*);
174 const IDispatchExVtbl *lpIDispatchExVtbl;
180 dispex_prop_t *props;
183 DispatchEx *prototype;
185 const builtin_info_t *builtin_info;
188 #define _IDispatchEx_(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
190 static inline void jsdisp_release(DispatchEx *jsdisp)
192 IDispatchEx_Release(_IDispatchEx_(jsdisp));
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*);
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);
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*);
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*);
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**);
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**);
251 typedef struct named_item_t {
256 struct named_item_t *next;
259 struct _script_ctx_t {
263 exec_ctx_t *exec_ctx;
264 named_item_t *named_items;
265 IActiveScriptSite *site;
266 IInternetHostSecurityManager *secmgr;
273 IDispatch *host_global;
276 DWORD last_match_index;
277 DWORD last_match_length;
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;
299 void script_release(script_ctx_t*);
301 static inline void script_addref(script_ctx_t *ctx)
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**);
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**);
320 IUnknown *create_ax_site(script_ctx_t*);
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*);
336 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
338 return dp->rgvarg + dp->cArgs-i-1;
341 static inline DWORD arg_cnt(const DISPPARAMS *dp)
343 return dp->cArgs - dp->cNamedArgs;
346 static inline BOOL is_class(DispatchEx *jsdisp, jsclass_t class)
348 return jsdisp->builtin_info->class == class;
351 static inline BOOL is_vclass(vdisp_t *vdisp, jsclass_t class)
353 return is_jsdisp(vdisp) && is_class(vdisp->u.jsdisp, class);
356 static inline BOOL is_num_vt(enum VARENUM vt)
358 return vt == VT_I4 || vt == VT_R8;
361 static inline DOUBLE num_val(const VARIANT *v)
363 return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
366 static inline void num_set_val(VARIANT *v, DOUBLE d)
368 if(d == (DOUBLE)(INT)d) {
377 static inline void num_set_nan(VARIANT *v)
383 V_UI8(v) = (ULONGLONG)0x7ff80000<<32;
387 static inline DOUBLE ret_nan(void)
394 static inline void num_set_inf(VARIANT *v, BOOL positive)
398 V_R8(v) = positive ? INFINITY : -INFINITY;
400 V_UI8(v) = (ULONGLONG)0x7ff00000<<32;
406 static inline DWORD make_grfdex(script_ctx_t *ctx, DWORD flags)
408 return (ctx->version << 28) | flags;
411 const char *debugstr_variant(const VARIANT*);
413 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
415 extern LONG module_ref;
417 static inline void lock_module(void)
419 InterlockedIncrement(&module_ref);
422 static inline void unlock_module(void)
424 InterlockedDecrement(&module_ref);
427 static inline void *heap_alloc(size_t len)
429 return HeapAlloc(GetProcessHeap(), 0, len);
432 static inline void *heap_alloc_zero(size_t len)
434 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
437 static inline void *heap_realloc(void *mem, size_t len)
439 return HeapReAlloc(GetProcessHeap(), 0, mem, len);
442 static inline BOOL heap_free(void *mem)
444 return HeapFree(GetProcessHeap(), 0, mem);
447 static inline LPWSTR heap_strdupW(LPCWSTR str)
454 size = (strlenW(str)+1)*sizeof(WCHAR);
455 ret = heap_alloc(size);
456 memcpy(ret, str, size);
462 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))