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 */
90 jsdisp_t
*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
, jsdisp_t
*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 jsdisp_t
*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
)(jsdisp_t
*);
170 void (*on_put
)(jsdisp_t
*,const WCHAR
*);
174 const IDispatchExVtbl
*lpIDispatchExVtbl
;
180 dispex_prop_t
*props
;
185 const builtin_info_t
*builtin_info
;
188 #define _IDispatchEx_(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
190 static inline IDispatch
*to_disp(jsdisp_t
*jsdisp
)
192 return (IDispatch
*)&jsdisp
->lpIDispatchExVtbl
;
195 static inline void jsdisp_addref(jsdisp_t
*jsdisp
)
197 IDispatchEx_AddRef(_IDispatchEx_(jsdisp
));
200 static inline void jsdisp_release(jsdisp_t
*jsdisp
)
202 IDispatchEx_Release(_IDispatchEx_(jsdisp
));
205 HRESULT
create_dispex(script_ctx_t
*,const builtin_info_t
*,jsdisp_t
*,jsdisp_t
**);
206 HRESULT
init_dispex(jsdisp_t
*,script_ctx_t
*,const builtin_info_t
*,jsdisp_t
*);
207 HRESULT
init_dispex_from_constr(jsdisp_t
*,script_ctx_t
*,const builtin_info_t
*,jsdisp_t
*);
209 HRESULT
disp_call(script_ctx_t
*,IDispatch
*,DISPID
,WORD
,DISPPARAMS
*,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
210 HRESULT
jsdisp_call_value(jsdisp_t
*,WORD
,DISPPARAMS
*,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
211 HRESULT
jsdisp_call(jsdisp_t
*,DISPID
,WORD
,DISPPARAMS
*,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
212 HRESULT
jsdisp_call_name(jsdisp_t
*,const WCHAR
*,WORD
,DISPPARAMS
*,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
213 HRESULT
disp_propget(script_ctx_t
*,IDispatch
*,DISPID
,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
214 HRESULT
disp_propput(script_ctx_t
*,IDispatch
*,DISPID
,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
215 HRESULT
jsdisp_propget(jsdisp_t
*,DISPID
,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
216 HRESULT
jsdisp_propput_name(jsdisp_t
*,const WCHAR
*,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
217 HRESULT
jsdisp_propput_const(jsdisp_t
*,const WCHAR
*,VARIANT
*);
218 HRESULT
jsdisp_propput_idx(jsdisp_t
*,DWORD
,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
219 HRESULT
jsdisp_propget_name(jsdisp_t
*,LPCWSTR
,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
220 HRESULT
jsdisp_get_idx(jsdisp_t
*,DWORD
,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
221 HRESULT
jsdisp_get_id(jsdisp_t
*,const WCHAR
*,DWORD
,DISPID
*);
222 HRESULT
jsdisp_delete_idx(jsdisp_t
*,DWORD
);
224 HRESULT
create_builtin_function(script_ctx_t
*,builtin_invoke_t
,const WCHAR
*,const builtin_info_t
*,DWORD
,
225 jsdisp_t
*,jsdisp_t
**);
226 HRESULT
Function_value(script_ctx_t
*,vdisp_t
*,WORD
,DISPPARAMS
*,VARIANT
*,jsexcept_t
*,IServiceProvider
*);
228 HRESULT
throw_eval_error(script_ctx_t
*,jsexcept_t
*,UINT
,const WCHAR
*);
229 HRESULT
throw_generic_error(script_ctx_t
*,jsexcept_t
*,UINT
,const WCHAR
*);
230 HRESULT
throw_range_error(script_ctx_t
*,jsexcept_t
*,UINT
,const WCHAR
*);
231 HRESULT
throw_reference_error(script_ctx_t
*,jsexcept_t
*,UINT
,const WCHAR
*);
232 HRESULT
throw_regexp_error(script_ctx_t
*,jsexcept_t
*,UINT
,const WCHAR
*);
233 HRESULT
throw_syntax_error(script_ctx_t
*,jsexcept_t
*,UINT
,const WCHAR
*);
234 HRESULT
throw_type_error(script_ctx_t
*,jsexcept_t
*,UINT
,const WCHAR
*);
235 HRESULT
throw_uri_error(script_ctx_t
*,jsexcept_t
*,UINT
,const WCHAR
*);
237 HRESULT
create_object(script_ctx_t
*,jsdisp_t
*,jsdisp_t
**);
238 HRESULT
create_math(script_ctx_t
*,jsdisp_t
**);
239 HRESULT
create_array(script_ctx_t
*,DWORD
,jsdisp_t
**);
240 HRESULT
create_regexp(script_ctx_t
*,const WCHAR
*,int,DWORD
,jsdisp_t
**);
241 HRESULT
create_regexp_var(script_ctx_t
*,VARIANT
*,VARIANT
*,jsdisp_t
**);
242 HRESULT
create_string(script_ctx_t
*,const WCHAR
*,DWORD
,jsdisp_t
**);
243 HRESULT
create_bool(script_ctx_t
*,VARIANT_BOOL
,jsdisp_t
**);
244 HRESULT
create_number(script_ctx_t
*,VARIANT
*,jsdisp_t
**);
252 HRESULT
to_primitive(script_ctx_t
*,VARIANT
*,jsexcept_t
*,VARIANT
*, hint_t
);
253 HRESULT
to_boolean(VARIANT
*,VARIANT_BOOL
*);
254 HRESULT
to_number(script_ctx_t
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
255 HRESULT
to_integer(script_ctx_t
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
256 HRESULT
to_int32(script_ctx_t
*,VARIANT
*,jsexcept_t
*,INT
*);
257 HRESULT
to_uint32(script_ctx_t
*,VARIANT
*,jsexcept_t
*,DWORD
*);
258 HRESULT
to_string(script_ctx_t
*,VARIANT
*,jsexcept_t
*,BSTR
*);
259 HRESULT
to_object(script_ctx_t
*,VARIANT
*,IDispatch
**);
261 typedef struct named_item_t
{
266 struct named_item_t
*next
;
269 struct _script_ctx_t
{
273 exec_ctx_t
*exec_ctx
;
274 named_item_t
*named_items
;
275 IActiveScriptSite
*site
;
276 IInternetHostSecurityManager
*secmgr
;
283 IDispatch
*host_global
;
286 DWORD last_match_index
;
287 DWORD last_match_length
;
290 jsdisp_t
*function_constr
;
291 jsdisp_t
*activex_constr
;
292 jsdisp_t
*array_constr
;
293 jsdisp_t
*bool_constr
;
294 jsdisp_t
*date_constr
;
295 jsdisp_t
*error_constr
;
296 jsdisp_t
*eval_error_constr
;
297 jsdisp_t
*range_error_constr
;
298 jsdisp_t
*reference_error_constr
;
299 jsdisp_t
*regexp_error_constr
;
300 jsdisp_t
*syntax_error_constr
;
301 jsdisp_t
*type_error_constr
;
302 jsdisp_t
*uri_error_constr
;
303 jsdisp_t
*number_constr
;
304 jsdisp_t
*object_constr
;
305 jsdisp_t
*regexp_constr
;
306 jsdisp_t
*string_constr
;
309 void script_release(script_ctx_t
*);
311 static inline void script_addref(script_ctx_t
*ctx
)
316 HRESULT
init_global(script_ctx_t
*);
317 HRESULT
init_function_constr(script_ctx_t
*,jsdisp_t
*);
318 HRESULT
create_object_prototype(script_ctx_t
*,jsdisp_t
**);
320 HRESULT
create_activex_constr(script_ctx_t
*,jsdisp_t
**);
321 HRESULT
create_array_constr(script_ctx_t
*,jsdisp_t
*,jsdisp_t
**);
322 HRESULT
create_bool_constr(script_ctx_t
*,jsdisp_t
*,jsdisp_t
**);
323 HRESULT
create_date_constr(script_ctx_t
*,jsdisp_t
*,jsdisp_t
**);
324 HRESULT
init_error_constr(script_ctx_t
*,jsdisp_t
*);
325 HRESULT
create_number_constr(script_ctx_t
*,jsdisp_t
*,jsdisp_t
**);
326 HRESULT
create_object_constr(script_ctx_t
*,jsdisp_t
*,jsdisp_t
**);
327 HRESULT
create_regexp_constr(script_ctx_t
*,jsdisp_t
*,jsdisp_t
**);
328 HRESULT
create_string_constr(script_ctx_t
*,jsdisp_t
*,jsdisp_t
**);
330 IUnknown
*create_ax_site(script_ctx_t
*);
337 #define REM_CHECK_GLOBAL 0x0001
338 #define REM_RESET_INDEX 0x0002
339 #define REM_NO_CTX_UPDATE 0x0004
340 HRESULT
regexp_match_next(script_ctx_t
*,jsdisp_t
*,DWORD
,const WCHAR
*,DWORD
,const WCHAR
**,match_result_t
**,
341 DWORD
*,DWORD
*,match_result_t
*);
342 HRESULT
regexp_match(script_ctx_t
*,jsdisp_t
*,const WCHAR
*,DWORD
,BOOL
,match_result_t
**,DWORD
*);
343 HRESULT
parse_regexp_flags(const WCHAR
*,DWORD
,DWORD
*);
344 HRESULT
regexp_string_match(script_ctx_t
*,jsdisp_t
*,BSTR
,VARIANT
*,jsexcept_t
*);
346 static inline VARIANT
*get_arg(DISPPARAMS
*dp
, DWORD i
)
348 return dp
->rgvarg
+ dp
->cArgs
-i
-1;
351 static inline DWORD
arg_cnt(const DISPPARAMS
*dp
)
353 return dp
->cArgs
- dp
->cNamedArgs
;
356 static inline BOOL
is_class(jsdisp_t
*jsdisp
, jsclass_t
class)
358 return jsdisp
->builtin_info
->class == class;
361 static inline BOOL
is_vclass(vdisp_t
*vdisp
, jsclass_t
class)
363 return is_jsdisp(vdisp
) && is_class(vdisp
->u
.jsdisp
, class);
366 static inline BOOL
is_num_vt(enum VARENUM vt
)
368 return vt
== VT_I4
|| vt
== VT_R8
;
371 static inline DOUBLE
num_val(const VARIANT
*v
)
373 return V_VT(v
) == VT_I4
? V_I4(v
) : V_R8(v
);
376 static inline void num_set_val(VARIANT
*v
, DOUBLE d
)
378 if(d
== (DOUBLE
)(INT
)d
) {
387 static inline void num_set_nan(VARIANT
*v
)
393 V_UI8(v
) = (ULONGLONG
)0x7ff80000<<32;
397 static inline DOUBLE
ret_nan(void)
404 static inline void num_set_inf(VARIANT
*v
, BOOL positive
)
408 V_R8(v
) = positive
? INFINITY
: -INFINITY
;
410 V_UI8(v
) = (ULONGLONG
)0x7ff00000<<32;
416 static inline void var_set_jsdisp(VARIANT
*v
, jsdisp_t
*jsdisp
)
418 V_VT(v
) = VT_DISPATCH
;
419 V_DISPATCH(v
) = to_disp(jsdisp
);
422 static inline DWORD
make_grfdex(script_ctx_t
*ctx
, DWORD flags
)
424 return (ctx
->version
<< 28) | flags
;
427 const char *debugstr_variant(const VARIANT
*);
429 HRESULT WINAPI
JScriptFactory_CreateInstance(IClassFactory
*,IUnknown
*,REFIID
,void**);
431 extern LONG module_ref
;
433 static inline void lock_module(void)
435 InterlockedIncrement(&module_ref
);
438 static inline void unlock_module(void)
440 InterlockedDecrement(&module_ref
);
443 static inline void *heap_alloc(size_t len
)
445 return HeapAlloc(GetProcessHeap(), 0, len
);
448 static inline void *heap_alloc_zero(size_t len
)
450 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
453 static inline void *heap_realloc(void *mem
, size_t len
)
455 return HeapReAlloc(GetProcessHeap(), 0, mem
, len
);
458 static inline BOOL
heap_free(void *mem
)
460 return HeapFree(GetProcessHeap(), 0, mem
);
463 static inline LPWSTR
heap_strdupW(LPCWSTR str
)
470 size
= (strlenW(str
)+1)*sizeof(WCHAR
);
471 ret
= heap_alloc(size
);
472 memcpy(ret
, str
, size
);
478 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))