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 jsdisp_t jsdisp_t;
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 */
91 jsdisp_t *iface_to_jsdisp(IUnknown*);
102 #define VDISP_DISPEX 0x0001
103 #define VDISP_JSDISP 0x0002
105 static inline void vdisp_release(vdisp_t *vdisp)
107 IDispatch_Release(vdisp->u.disp);
110 static inline BOOL is_jsdisp(vdisp_t *vdisp)
112 return (vdisp->flags & VDISP_JSDISP) != 0;
115 static inline BOOL is_dispex(vdisp_t *vdisp)
117 return (vdisp->flags & VDISP_DISPEX) != 0;
120 static inline void set_jsdisp(vdisp_t *vdisp, jsdisp_t *jsdisp)
122 vdisp->u.jsdisp = jsdisp;
123 vdisp->flags = VDISP_JSDISP | VDISP_DISPEX;
124 IDispatch_AddRef(vdisp->u.disp);
127 static inline void set_disp(vdisp_t *vdisp, IDispatch *disp)
133 jsdisp = iface_to_jsdisp((IUnknown*)disp);
135 vdisp->u.jsdisp = jsdisp;
136 vdisp->flags = VDISP_JSDISP | VDISP_DISPEX;
140 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
141 if(SUCCEEDED(hres)) {
142 vdisp->u.dispex = dispex;
143 vdisp->flags = VDISP_DISPEX;
147 IDispatch_AddRef(disp);
148 vdisp->u.disp = disp;
152 static inline jsdisp_t *get_jsdisp(vdisp_t *vdisp)
154 return is_jsdisp(vdisp) ? vdisp->u.jsdisp : NULL;
157 typedef HRESULT (*builtin_invoke_t)(script_ctx_t*,vdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
161 builtin_invoke_t invoke;
167 builtin_prop_t value_prop;
169 const builtin_prop_t *props;
170 void (*destructor)(jsdisp_t*);
171 void (*on_put)(jsdisp_t*,const WCHAR*);
175 const IDispatchExVtbl *lpIDispatchExVtbl;
181 dispex_prop_t *props;
186 const builtin_info_t *builtin_info;
189 #define _IDispatchEx_(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
191 static inline IDispatch *to_disp(jsdisp_t *jsdisp)
193 return (IDispatch*)&jsdisp->lpIDispatchExVtbl;
196 static inline void jsdisp_addref(jsdisp_t *jsdisp)
198 IDispatchEx_AddRef(_IDispatchEx_(jsdisp));
201 static inline void jsdisp_release(jsdisp_t *jsdisp)
203 IDispatchEx_Release(_IDispatchEx_(jsdisp));
206 HRESULT create_dispex(script_ctx_t*,const builtin_info_t*,jsdisp_t*,jsdisp_t**);
207 HRESULT init_dispex(jsdisp_t*,script_ctx_t*,const builtin_info_t*,jsdisp_t*);
208 HRESULT init_dispex_from_constr(jsdisp_t*,script_ctx_t*,const builtin_info_t*,jsdisp_t*);
210 HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
211 HRESULT jsdisp_call_value(jsdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
212 HRESULT jsdisp_call(jsdisp_t*,DISPID,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
213 HRESULT jsdisp_call_name(jsdisp_t*,const WCHAR*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
214 HRESULT disp_propget(script_ctx_t*,IDispatch*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
215 HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
216 HRESULT jsdisp_propget(jsdisp_t*,DISPID,VARIANT*,jsexcept_t*,IServiceProvider*);
217 HRESULT jsdisp_propput_name(jsdisp_t*,const WCHAR*,VARIANT*,jsexcept_t*,IServiceProvider*);
218 HRESULT jsdisp_propput_const(jsdisp_t*,const WCHAR*,VARIANT*);
219 HRESULT jsdisp_propput_idx(jsdisp_t*,DWORD,VARIANT*,jsexcept_t*,IServiceProvider*);
220 HRESULT jsdisp_propget_name(jsdisp_t*,LPCWSTR,VARIANT*,jsexcept_t*,IServiceProvider*);
221 HRESULT jsdisp_get_idx(jsdisp_t*,DWORD,VARIANT*,jsexcept_t*,IServiceProvider*);
222 HRESULT jsdisp_get_id(jsdisp_t*,const WCHAR*,DWORD,DISPID*);
223 HRESULT jsdisp_delete_idx(jsdisp_t*,DWORD);
225 HRESULT create_builtin_function(script_ctx_t*,builtin_invoke_t,const WCHAR*,const builtin_info_t*,DWORD,
226 jsdisp_t*,jsdisp_t**);
227 HRESULT Function_value(script_ctx_t*,vdisp_t*,WORD,DISPPARAMS*,VARIANT*,jsexcept_t*,IServiceProvider*);
229 HRESULT throw_eval_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
230 HRESULT throw_generic_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
231 HRESULT throw_range_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
232 HRESULT throw_reference_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
233 HRESULT throw_regexp_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
234 HRESULT throw_syntax_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
235 HRESULT throw_type_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
236 HRESULT throw_uri_error(script_ctx_t*,jsexcept_t*,UINT,const WCHAR*);
238 HRESULT create_object(script_ctx_t*,jsdisp_t*,jsdisp_t**);
239 HRESULT create_math(script_ctx_t*,jsdisp_t**);
240 HRESULT create_array(script_ctx_t*,DWORD,jsdisp_t**);
241 HRESULT create_regexp(script_ctx_t*,const WCHAR *,int,DWORD,jsdisp_t**);
242 HRESULT create_regexp_var(script_ctx_t*,VARIANT*,VARIANT*,jsdisp_t**);
243 HRESULT create_string(script_ctx_t*,const WCHAR*,DWORD,jsdisp_t**);
244 HRESULT create_bool(script_ctx_t*,VARIANT_BOOL,jsdisp_t**);
245 HRESULT create_number(script_ctx_t*,VARIANT*,jsdisp_t**);
246 HRESULT create_vbarray(script_ctx_t*,SAFEARRAY*,jsdisp_t**);
254 HRESULT to_primitive(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*, hint_t);
255 HRESULT to_boolean(VARIANT*,VARIANT_BOOL*);
256 HRESULT to_number(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
257 HRESULT to_integer(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
258 HRESULT to_int32(script_ctx_t*,VARIANT*,jsexcept_t*,INT*);
259 HRESULT to_uint32(script_ctx_t*,VARIANT*,jsexcept_t*,DWORD*);
260 HRESULT to_string(script_ctx_t*,VARIANT*,jsexcept_t*,BSTR*);
261 HRESULT to_object(script_ctx_t*,VARIANT*,IDispatch**);
263 typedef struct named_item_t {
268 struct named_item_t *next;
271 struct _script_ctx_t {
275 exec_ctx_t *exec_ctx;
276 named_item_t *named_items;
277 IActiveScriptSite *site;
278 IInternetHostSecurityManager *secmgr;
285 IDispatch *host_global;
288 DWORD last_match_index;
289 DWORD last_match_length;
292 jsdisp_t *function_constr;
293 jsdisp_t *activex_constr;
294 jsdisp_t *array_constr;
295 jsdisp_t *bool_constr;
296 jsdisp_t *date_constr;
297 jsdisp_t *error_constr;
298 jsdisp_t *eval_error_constr;
299 jsdisp_t *range_error_constr;
300 jsdisp_t *reference_error_constr;
301 jsdisp_t *regexp_error_constr;
302 jsdisp_t *syntax_error_constr;
303 jsdisp_t *type_error_constr;
304 jsdisp_t *uri_error_constr;
305 jsdisp_t *number_constr;
306 jsdisp_t *object_constr;
307 jsdisp_t *regexp_constr;
308 jsdisp_t *string_constr;
309 jsdisp_t *vbarray_constr;
312 void script_release(script_ctx_t*);
314 static inline void script_addref(script_ctx_t *ctx)
319 HRESULT init_global(script_ctx_t*);
320 HRESULT init_function_constr(script_ctx_t*,jsdisp_t*);
321 HRESULT create_object_prototype(script_ctx_t*,jsdisp_t**);
323 HRESULT create_activex_constr(script_ctx_t*,jsdisp_t**);
324 HRESULT create_array_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
325 HRESULT create_bool_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
326 HRESULT create_date_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
327 HRESULT init_error_constr(script_ctx_t*,jsdisp_t*);
328 HRESULT create_number_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
329 HRESULT create_object_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
330 HRESULT create_regexp_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
331 HRESULT create_string_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
332 HRESULT create_vbarray_constr(script_ctx_t*,jsdisp_t*,jsdisp_t**);
334 IUnknown *create_ax_site(script_ctx_t*);
341 #define REM_CHECK_GLOBAL 0x0001
342 #define REM_RESET_INDEX 0x0002
343 #define REM_NO_CTX_UPDATE 0x0004
344 HRESULT regexp_match_next(script_ctx_t*,jsdisp_t*,DWORD,const WCHAR*,DWORD,const WCHAR**,match_result_t**,
345 DWORD*,DWORD*,match_result_t*);
346 HRESULT regexp_match(script_ctx_t*,jsdisp_t*,const WCHAR*,DWORD,BOOL,match_result_t**,DWORD*);
347 HRESULT parse_regexp_flags(const WCHAR*,DWORD,DWORD*);
348 HRESULT regexp_string_match(script_ctx_t*,jsdisp_t*,BSTR,VARIANT*,jsexcept_t*);
350 static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
352 return dp->rgvarg + dp->cArgs-i-1;
355 static inline DWORD arg_cnt(const DISPPARAMS *dp)
357 return dp->cArgs - dp->cNamedArgs;
360 static inline BOOL is_class(jsdisp_t *jsdisp, jsclass_t class)
362 return jsdisp->builtin_info->class == class;
365 static inline BOOL is_vclass(vdisp_t *vdisp, jsclass_t class)
367 return is_jsdisp(vdisp) && is_class(vdisp->u.jsdisp, class);
370 static inline BOOL is_num_vt(enum VARENUM vt)
372 return vt == VT_I4 || vt == VT_R8;
375 static inline DOUBLE num_val(const VARIANT *v)
377 return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
380 static inline void num_set_int(VARIANT *v, INT i)
386 static inline void num_set_val(VARIANT *v, DOUBLE d)
388 if(d == (DOUBLE)(INT)d) {
397 static inline void num_set_nan(VARIANT *v)
403 V_UI8(v) = (ULONGLONG)0x7ff80000<<32;
407 static inline DOUBLE ret_nan(void)
414 static inline void num_set_inf(VARIANT *v, BOOL positive)
418 V_R8(v) = positive ? INFINITY : -INFINITY;
420 V_UI8(v) = (ULONGLONG)0x7ff00000<<32;
426 static inline void var_set_jsdisp(VARIANT *v, jsdisp_t *jsdisp)
428 V_VT(v) = VT_DISPATCH;
429 V_DISPATCH(v) = to_disp(jsdisp);
432 static inline DWORD make_grfdex(script_ctx_t *ctx, DWORD flags)
434 return (ctx->version << 28) | flags;
437 const char *debugstr_variant(const VARIANT*);
439 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
441 extern LONG module_ref;
443 static inline void lock_module(void)
445 InterlockedIncrement(&module_ref);
448 static inline void unlock_module(void)
450 InterlockedDecrement(&module_ref);
453 static inline void *heap_alloc(size_t len)
455 return HeapAlloc(GetProcessHeap(), 0, len);
458 static inline void *heap_alloc_zero(size_t len)
460 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
463 static inline void *heap_realloc(void *mem, size_t len)
465 return HeapReAlloc(GetProcessHeap(), 0, mem, len);
468 static inline BOOL heap_free(void *mem)
470 return HeapFree(GetProcessHeap(), 0, mem);
473 static inline LPWSTR heap_strdupW(LPCWSTR str)
480 size = (strlenW(str)+1)*sizeof(WCHAR);
481 ret = heap_alloc(size);
482 memcpy(ret, str, size);
488 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))