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
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
36 static ExternalCycleCollectionParticipant dispex_ccp
;
38 static CRITICAL_SECTION cs_dispex_static_data
;
39 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
41 0, 0, &cs_dispex_static_data
,
42 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
43 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
45 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
49 VARIANT default_value
;
56 dispex_hook_invoke_t hook
;
62 USHORT default_value_cnt
;
65 func_arg_info_t
*arg_info
;
68 struct dispex_data_t
{
69 dispex_static_data_t
*desc
;
70 compat_mode_t compat_mode
;
75 func_info_t
**name_table
;
87 #define DYNPROP_DELETED 0x01
88 #define DYNPROP_HIDDEN 0x02
92 IUnknown IUnknown_iface
;
98 func_disp_t
*func_obj
;
102 struct dispex_dynamic_data_t
{
105 dynamic_prop_t
*props
;
106 func_obj_entry_t
*func_disps
;
109 #define DISPID_DYNPROP_0 0x50000000
110 #define DISPID_DYNPROP_MAX 0x5fffffff
112 #define FDEX_VERSION_MASK 0xf0000000
114 static ITypeLib
*typelib
, *typelib_private
;
115 static ITypeInfo
*typeinfos
[LAST_tid
];
116 static struct list dispex_data_list
= LIST_INIT(dispex_data_list
);
118 static REFIID tid_ids
[] = {
119 #define XIID(iface) &IID_ ## iface,
120 #define XDIID(iface) &DIID_ ## iface,
128 static HRESULT
load_typelib(void)
130 WCHAR module_path
[MAX_PATH
+ 3];
135 hres
= LoadRegTypeLib(&LIBID_MSHTML
, 4, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
137 ERR("LoadRegTypeLib failed: %08lx\n", hres
);
141 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
142 ITypeLib_Release(tl
);
144 len
= GetModuleFileNameW(hInst
, module_path
, MAX_PATH
+ 1);
145 if (!len
|| len
== MAX_PATH
+ 1)
147 ERR("Could not get module file name, len %lu.\n", len
);
150 lstrcatW(module_path
, L
"\\1");
152 hres
= LoadTypeLibEx(module_path
, REGKIND_NONE
, &tl
);
154 ERR("LoadTypeLibEx failed for private typelib: %08lx\n", hres
);
158 if(InterlockedCompareExchangePointer((void**)&typelib_private
, tl
, NULL
))
159 ITypeLib_Release(tl
);
164 static HRESULT
get_typeinfo(tid_t tid
, ITypeInfo
**typeinfo
)
169 hres
= load_typelib();
173 if(!typeinfos
[tid
]) {
176 hres
= ITypeLib_GetTypeInfoOfGuid(tid
> LAST_public_tid
? typelib_private
: typelib
, tid_ids
[tid
], &ti
);
178 ERR("GetTypeInfoOfGuid(%s) failed: %08lx\n", debugstr_mshtml_guid(tid_ids
[tid
]), hres
);
182 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), ti
, NULL
))
183 ITypeInfo_Release(ti
);
186 *typeinfo
= typeinfos
[tid
];
190 void release_typelib(void)
195 while(!list_empty(&dispex_data_list
)) {
196 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
197 list_remove(&iter
->entry
);
199 for(i
= 0; i
< iter
->func_cnt
; i
++) {
200 if(iter
->funcs
[i
].default_value_cnt
&& iter
->funcs
[i
].arg_info
) {
201 for(j
= 0; j
< iter
->funcs
[i
].argc
; j
++)
202 VariantClear(&iter
->funcs
[i
].arg_info
[j
].default_value
);
204 free(iter
->funcs
[i
].arg_types
);
205 free(iter
->funcs
[i
].arg_info
);
206 SysFreeString(iter
->funcs
[i
].name
);
210 free(iter
->name_table
);
217 for(i
=0; i
< ARRAY_SIZE(typeinfos
); i
++)
219 ITypeInfo_Release(typeinfos
[i
]);
221 ITypeLib_Release(typelib
);
222 ITypeLib_Release(typelib_private
);
223 DeleteCriticalSection(&cs_dispex_static_data
);
226 HRESULT
get_class_typeinfo(const CLSID
*clsid
, ITypeInfo
**typeinfo
)
231 hres
= load_typelib();
235 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, clsid
, typeinfo
);
237 hres
= ITypeLib_GetTypeInfoOfGuid(typelib_private
, clsid
, typeinfo
);
239 ERR("GetTypeInfoOfGuid failed: %08lx\n", hres
);
243 /* Not all argument types are supported yet */
244 #define BUILTIN_ARG_TYPES_SWITCH \
245 CASE_VT(VT_I2, INT16, V_I2); \
246 CASE_VT(VT_UI2, UINT16, V_UI2); \
247 CASE_VT(VT_I4, INT32, V_I4); \
248 CASE_VT(VT_UI4, UINT32, V_UI4); \
249 CASE_VT(VT_R4, float, V_R4); \
250 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
251 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH); \
252 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
254 /* List all types used by IDispatchEx-based properties */
255 #define BUILTIN_TYPES_SWITCH \
256 BUILTIN_ARG_TYPES_SWITCH; \
257 CASE_VT(VT_VARIANT, VARIANT, *); \
258 CASE_VT(VT_PTR, void*, V_BYREF); \
259 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
260 CASE_VT(VT_UI8, ULONGLONG, V_UI8)
262 static BOOL
is_arg_type_supported(VARTYPE vt
)
265 #define CASE_VT(x,a,b) case x: return TRUE
266 BUILTIN_ARG_TYPES_SWITCH
;
272 static void add_func_info(dispex_data_t
*data
, tid_t tid
, const FUNCDESC
*desc
, ITypeInfo
*dti
,
273 dispex_hook_invoke_t hook
, const WCHAR
*name_override
)
280 name
= SysAllocString(name_override
);
282 hres
= ITypeInfo_GetDocumentation(dti
, desc
->memid
, &name
, NULL
, NULL
, NULL
);
284 WARN("GetDocumentation failed: %08lx\n", hres
);
289 for(info
= data
->funcs
; info
< data
->funcs
+data
->func_cnt
; info
++) {
290 if(info
->id
== desc
->memid
|| !wcscmp(info
->name
, name
)) {
291 if(info
->tid
!= tid
) {
293 return; /* Duplicated in other interface */
299 TRACE("adding %s...\n", debugstr_w(name
));
301 if(info
== data
->funcs
+data
->func_cnt
) {
302 if(data
->func_cnt
== data
->func_size
) {
303 info
= realloc(data
->funcs
, data
->func_size
* 2 * sizeof(func_info_t
));
308 memset(info
+ data
->func_size
, 0, data
->func_size
* sizeof(func_info_t
));
310 data
->func_size
*= 2;
312 info
= data
->funcs
+data
->func_cnt
;
316 info
->id
= desc
->memid
;
319 info
->func_disp_idx
= -1;
320 info
->prop_vt
= VT_EMPTY
;
326 if(desc
->invkind
& DISPATCH_METHOD
) {
329 info
->func_disp_idx
= data
->func_disp_cnt
++;
330 info
->argc
= desc
->cParams
;
332 assert(info
->argc
< MAX_ARGS
);
333 assert(desc
->funckind
== FUNC_DISPATCH
);
335 info
->arg_info
= calloc(info
->argc
, sizeof(*info
->arg_info
));
339 info
->prop_vt
= desc
->elemdescFunc
.tdesc
.vt
;
340 if(info
->prop_vt
!= VT_VOID
&& info
->prop_vt
!= VT_PTR
&& !is_arg_type_supported(info
->prop_vt
)) {
341 TRACE("%s: return type %d\n", debugstr_w(info
->name
), info
->prop_vt
);
342 return; /* Fallback to ITypeInfo::Invoke */
345 info
->arg_types
= malloc(sizeof(*info
->arg_types
) * (info
->argc
+ (info
->prop_vt
== VT_VOID
? 0 : 1)));
349 for(i
=0; i
< info
->argc
; i
++)
350 info
->arg_types
[i
] = desc
->lprgelemdescParam
[i
].tdesc
.vt
;
352 if(info
->prop_vt
== VT_PTR
)
353 info
->arg_types
[info
->argc
] = VT_BYREF
| VT_DISPATCH
;
354 else if(info
->prop_vt
!= VT_VOID
)
355 info
->arg_types
[info
->argc
] = VT_BYREF
| info
->prop_vt
;
357 if(desc
->cParamsOpt
) {
358 TRACE("%s: optional params\n", debugstr_w(info
->name
));
359 return; /* Fallback to ITypeInfo::Invoke */
362 for(i
=0; i
< info
->argc
; i
++) {
363 TYPEDESC
*tdesc
= &desc
->lprgelemdescParam
[i
].tdesc
;
364 if(tdesc
->vt
== VT_PTR
&& tdesc
->lptdesc
->vt
== VT_USERDEFINED
) {
365 ITypeInfo
*ref_type_info
;
368 hres
= ITypeInfo_GetRefTypeInfo(dti
, tdesc
->lptdesc
->hreftype
, &ref_type_info
);
370 ERR("Could not get referenced type info: %08lx\n", hres
);
374 hres
= ITypeInfo_GetTypeAttr(ref_type_info
, &attr
);
375 if(SUCCEEDED(hres
)) {
376 assert(attr
->typekind
== TKIND_DISPATCH
);
377 info
->arg_info
[i
].iid
= attr
->guid
;
378 ITypeInfo_ReleaseTypeAttr(ref_type_info
, attr
);
380 ERR("GetTypeAttr failed: %08lx\n", hres
);
382 ITypeInfo_Release(ref_type_info
);
385 info
->arg_types
[i
] = VT_DISPATCH
;
386 }else if(!is_arg_type_supported(info
->arg_types
[i
])) {
387 TRACE("%s: unsupported arg type %s\n", debugstr_w(info
->name
), debugstr_vt(info
->arg_types
[i
]));
388 return; /* Fallback to ITypeInfo for unsupported arg types */
391 if(desc
->lprgelemdescParam
[i
].paramdesc
.wParamFlags
& PARAMFLAG_FHASDEFAULT
) {
392 hres
= VariantCopy(&info
->arg_info
[i
].default_value
,
393 &desc
->lprgelemdescParam
[i
].paramdesc
.pparamdescex
->varDefaultValue
);
395 ERR("Could not copy default value: %08lx\n", hres
);
398 TRACE("%s param %d: default value %s\n", debugstr_w(info
->name
),
399 i
, debugstr_variant(&info
->arg_info
[i
].default_value
));
400 info
->default_value_cnt
++;
404 assert(info
->argc
<= MAX_ARGS
);
405 assert(desc
->callconv
== CC_STDCALL
);
407 info
->call_vtbl_off
= desc
->oVft
/sizeof(void*);
408 }else if(desc
->invkind
& (DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYGET
)) {
409 VARTYPE vt
= VT_EMPTY
;
411 if(desc
->wFuncFlags
& FUNCFLAG_FHIDDEN
)
412 info
->func_disp_idx
= -2;
414 if(desc
->invkind
& DISPATCH_PROPERTYGET
) {
415 vt
= desc
->elemdescFunc
.tdesc
.vt
;
416 info
->get_vtbl_off
= desc
->oVft
/sizeof(void*);
418 if(desc
->invkind
& DISPATCH_PROPERTYPUT
) {
419 assert(desc
->cParams
== 1);
420 vt
= desc
->lprgelemdescParam
->tdesc
.vt
;
421 info
->put_vtbl_off
= desc
->oVft
/sizeof(void*);
424 assert(info
->prop_vt
== VT_EMPTY
|| vt
== info
->prop_vt
);
429 static HRESULT
process_interface(dispex_data_t
*data
, tid_t tid
, ITypeInfo
*disp_typeinfo
, const dispex_hook_t
*hooks
)
431 unsigned i
= 7; /* skip IDispatch functions */
436 hres
= get_typeinfo(tid
, &typeinfo
);
441 const dispex_hook_t
*hook
= NULL
;
443 hres
= ITypeInfo_GetFuncDesc(typeinfo
, i
++, &funcdesc
);
448 for(hook
= hooks
; hook
->dispid
!= DISPID_UNKNOWN
; hook
++) {
449 if(hook
->dispid
== funcdesc
->memid
)
452 if(hook
->dispid
== DISPID_UNKNOWN
)
456 if(!hook
|| hook
->invoke
|| hook
->name
) {
457 add_func_info(data
, tid
, funcdesc
, disp_typeinfo
? disp_typeinfo
: typeinfo
,
458 hook
? hook
->invoke
: NULL
, hook
? hook
->name
: NULL
);
461 ITypeInfo_ReleaseFuncDesc(typeinfo
, funcdesc
);
467 void dispex_info_add_interface(dispex_data_t
*info
, tid_t tid
, const dispex_hook_t
*hooks
)
471 hres
= process_interface(info
, tid
, NULL
, hooks
);
473 ERR("process_interface failed: %08lx\n", hres
);
476 static int __cdecl
dispid_cmp(const void *p1
, const void *p2
)
478 return ((const func_info_t
*)p1
)->id
- ((const func_info_t
*)p2
)->id
;
481 static int __cdecl
func_name_cmp(const void *p1
, const void *p2
)
483 return wcsicmp((*(func_info_t
* const*)p1
)->name
, (*(func_info_t
* const*)p2
)->name
);
486 static dispex_data_t
*preprocess_dispex_data(dispex_static_data_t
*desc
, compat_mode_t compat_mode
)
495 hres
= get_typeinfo(desc
->disp_tid
, &dti
);
497 ERR("Could not get disp type info: %08lx\n", hres
);
502 data
= malloc(sizeof(dispex_data_t
));
504 ERR("Out of memory\n");
508 data
->compat_mode
= compat_mode
;
510 data
->func_disp_cnt
= 0;
511 data
->func_size
= 16;
512 data
->funcs
= calloc(data
->func_size
, sizeof(func_info_t
));
515 ERR("Out of memory\n");
518 list_add_tail(&dispex_data_list
, &data
->entry
);
521 desc
->init_info(data
, compat_mode
);
523 for(tid
= desc
->iface_tids
; *tid
; tid
++) {
524 hres
= process_interface(data
, *tid
, dti
, NULL
);
529 if(!data
->func_cnt
) {
531 data
->name_table
= NULL
;
538 data
->funcs
= realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
539 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
541 data
->name_table
= malloc(data
->func_cnt
* sizeof(func_info_t
*));
542 for(i
=0; i
< data
->func_cnt
; i
++)
543 data
->name_table
[i
] = data
->funcs
+i
;
544 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
548 static int __cdecl
id_cmp(const void *p1
, const void *p2
)
550 return *(const DISPID
*)p1
- *(const DISPID
*)p2
;
553 HRESULT
get_dispids(tid_t tid
, DWORD
*ret_size
, DISPID
**ret
)
555 unsigned i
, func_cnt
;
562 hres
= get_typeinfo(tid
, &ti
);
566 hres
= ITypeInfo_GetTypeAttr(ti
, &attr
);
568 ITypeInfo_Release(ti
);
572 func_cnt
= attr
->cFuncs
;
573 ITypeInfo_ReleaseTypeAttr(ti
, attr
);
575 ids
= malloc(func_cnt
* sizeof(DISPID
));
577 ITypeInfo_Release(ti
);
578 return E_OUTOFMEMORY
;
581 for(i
=0; i
< func_cnt
; i
++) {
582 hres
= ITypeInfo_GetFuncDesc(ti
, i
, &funcdesc
);
586 ids
[i
] = funcdesc
->memid
;
587 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
590 ITypeInfo_Release(ti
);
596 qsort(ids
, func_cnt
, sizeof(DISPID
), id_cmp
);
598 *ret_size
= func_cnt
;
603 static inline BOOL
is_custom_dispid(DISPID id
)
605 return MSHTML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSHTML_DISPID_CUSTOM_MAX
;
608 static inline BOOL
is_dynamic_dispid(DISPID id
)
610 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
613 dispex_prop_type_t
get_dispid_type(DISPID id
)
615 if(is_dynamic_dispid(id
))
616 return DISPEXPROP_DYNAMIC
;
617 if(is_custom_dispid(id
))
618 return DISPEXPROP_CUSTOM
;
619 return DISPEXPROP_BUILTIN
;
622 static HRESULT
variant_copy(VARIANT
*dest
, VARIANT
*src
)
624 if(V_VT(src
) == VT_BSTR
&& !V_BSTR(src
)) {
625 V_VT(dest
) = VT_BSTR
;
630 return VariantCopy(dest
, src
);
633 static inline dispex_dynamic_data_t
*get_dynamic_data(DispatchEx
*This
)
635 if(This
->dynamic_data
)
636 return This
->dynamic_data
;
638 This
->dynamic_data
= calloc(1, sizeof(dispex_dynamic_data_t
));
639 if(!This
->dynamic_data
)
642 if(This
->info
->desc
->vtbl
->populate_props
)
643 This
->info
->desc
->vtbl
->populate_props(This
);
645 return This
->dynamic_data
;
648 static HRESULT
get_dynamic_prop(DispatchEx
*This
, const WCHAR
*name
, DWORD flags
, dynamic_prop_t
**ret
)
650 const BOOL alloc
= flags
& fdexNameEnsure
;
651 dispex_dynamic_data_t
*data
;
652 dynamic_prop_t
*prop
;
654 data
= get_dynamic_data(This
);
656 return E_OUTOFMEMORY
;
658 for(prop
= data
->props
; prop
< data
->props
+data
->prop_cnt
; prop
++) {
659 if(flags
& fdexNameCaseInsensitive
? !wcsicmp(prop
->name
, name
) : !wcscmp(prop
->name
, name
)) {
660 if(prop
->flags
& DYNPROP_DELETED
) {
662 return DISP_E_UNKNOWNNAME
;
663 prop
->flags
&= ~DYNPROP_DELETED
;
671 return DISP_E_UNKNOWNNAME
;
673 TRACE("creating dynamic prop %s\n", debugstr_w(name
));
675 if(!data
->buf_size
) {
676 data
->props
= malloc(sizeof(dynamic_prop_t
) * 4);
678 return E_OUTOFMEMORY
;
680 }else if(data
->buf_size
== data
->prop_cnt
) {
681 dynamic_prop_t
*new_props
;
683 new_props
= realloc(data
->props
, sizeof(dynamic_prop_t
) * (data
->buf_size
<< 1));
685 return E_OUTOFMEMORY
;
687 data
->props
= new_props
;
688 data
->buf_size
<<= 1;
691 prop
= data
->props
+ data
->prop_cnt
;
693 prop
->name
= wcsdup(name
);
695 return E_OUTOFMEMORY
;
697 VariantInit(&prop
->var
);
704 HRESULT
dispex_get_dprop_ref(DispatchEx
*This
, const WCHAR
*name
, BOOL alloc
, VARIANT
**ret
)
706 dynamic_prop_t
*prop
;
709 hres
= get_dynamic_prop(This
, name
, alloc
? fdexNameEnsure
: 0, &prop
);
714 prop
->flags
|= DYNPROP_HIDDEN
;
719 HRESULT
dispex_get_dynid(DispatchEx
*This
, const WCHAR
*name
, BOOL hidden
, DISPID
*id
)
721 dynamic_prop_t
*prop
;
724 hres
= get_dynamic_prop(This
, name
, fdexNameEnsure
, &prop
);
729 prop
->flags
|= DYNPROP_HIDDEN
;
730 *id
= DISPID_DYNPROP_0
+ (prop
- This
->dynamic_data
->props
);
734 static HRESULT
dispex_value(DispatchEx
*This
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
735 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
739 if(This
->info
->desc
->vtbl
->value
)
740 return This
->info
->desc
->vtbl
->value(This
, lcid
, flags
, params
, res
, ei
, caller
);
743 case DISPATCH_PROPERTYGET
:
745 hres
= dispex_to_string(This
, &V_BSTR(res
));
750 FIXME("Unimplemented flags %x\n", flags
);
757 static HRESULT
typeinfo_invoke(DispatchEx
*This
, func_info_t
*func
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
,
760 DISPPARAMS params
= {dp
->rgvarg
, NULL
, dp
->cArgs
, 0};
766 if(params
.cArgs
> func
->argc
) {
767 params
.rgvarg
+= params
.cArgs
- func
->argc
;
768 params
.cArgs
= func
->argc
;
771 hres
= get_typeinfo(func
->tid
, &ti
);
773 ERR("Could not get type info: %08lx\n", hres
);
777 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&unk
);
779 ERR("Could not get iface %s: %08lx\n", debugstr_mshtml_guid(tid_ids
[func
->tid
]), hres
);
783 hres
= ITypeInfo_Invoke(ti
, unk
, func
->id
, flags
, ¶ms
, res
, ei
, &argerr
);
785 IUnknown_Release(unk
);
789 static inline func_disp_t
*impl_from_IUnknown(IUnknown
*iface
)
791 return CONTAINING_RECORD(iface
, func_disp_t
, IUnknown_iface
);
794 static HRESULT WINAPI
Function_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
796 func_disp_t
*This
= impl_from_IUnknown(iface
);
798 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
800 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
801 *ppv
= &This
->IUnknown_iface
;
802 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
803 return *ppv
? S_OK
: E_NOINTERFACE
;
806 return E_NOINTERFACE
;
809 IUnknown_AddRef((IUnknown
*)*ppv
);
813 static ULONG WINAPI
Function_AddRef(IUnknown
*iface
)
815 func_disp_t
*This
= impl_from_IUnknown(iface
);
816 LONG ref
= dispex_ref_incr(&This
->dispex
);
818 TRACE("(%p) ref=%ld\n", This
, ref
);
823 static ULONG WINAPI
Function_Release(IUnknown
*iface
)
825 func_disp_t
*This
= impl_from_IUnknown(iface
);
826 LONG ref
= dispex_ref_decr(&This
->dispex
);
828 TRACE("(%p) ref=%ld\n", This
, ref
);
833 static const IUnknownVtbl FunctionUnkVtbl
= {
834 Function_QueryInterface
,
839 static inline func_disp_t
*impl_from_DispatchEx(DispatchEx
*iface
)
841 return CONTAINING_RECORD(iface
, func_disp_t
, dispex
);
844 static void function_destructor(DispatchEx
*dispex
)
846 func_disp_t
*This
= impl_from_DispatchEx(dispex
);
851 static HRESULT
function_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
852 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
854 func_disp_t
*This
= impl_from_DispatchEx(dispex
);
858 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
862 case DISPATCH_METHOD
:
865 hres
= dispex_call_builtin(This
->obj
, This
->info
->id
, params
, res
, ei
, caller
);
867 case DISPATCH_PROPERTYGET
: {
872 static const WCHAR func_prefixW
[] =
873 {'\n','f','u','n','c','t','i','o','n',' '};
874 static const WCHAR func_suffixW
[] =
875 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
877 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
879 return E_ACCESSDENIED
;
881 name_len
= SysStringLen(This
->info
->name
);
882 ptr
= str
= SysAllocStringLen(NULL
, name_len
+ ARRAY_SIZE(func_prefixW
) + ARRAY_SIZE(func_suffixW
));
884 return E_OUTOFMEMORY
;
886 memcpy(ptr
, func_prefixW
, sizeof(func_prefixW
));
887 ptr
+= ARRAY_SIZE(func_prefixW
);
889 memcpy(ptr
, This
->info
->name
, name_len
*sizeof(WCHAR
));
892 memcpy(ptr
, func_suffixW
, sizeof(func_suffixW
));
899 FIXME("Unimplemented flags %x\n", flags
);
906 static const dispex_static_data_vtbl_t function_dispex_vtbl
= {
907 .destructor
= function_destructor
,
908 .value
= function_value
,
911 static const tid_t function_iface_tids
[] = {0};
913 static dispex_static_data_t function_dispex
= {
915 &function_dispex_vtbl
,
920 static func_disp_t
*create_func_disp(DispatchEx
*obj
, func_info_t
*info
)
924 ret
= calloc(1, sizeof(func_disp_t
));
928 ret
->IUnknown_iface
.lpVtbl
= &FunctionUnkVtbl
;
929 init_dispatch(&ret
->dispex
, &ret
->IUnknown_iface
, &function_dispex
, dispex_compat_mode(obj
));
936 static HRESULT
invoke_disp_value(DispatchEx
*This
, IDispatch
*func_disp
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
937 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
939 DISPID named_arg
= DISPID_THIS
;
940 DISPPARAMS new_dp
= {NULL
, &named_arg
, 0, 1};
945 FIXME("named args not supported\n");
949 new_dp
.rgvarg
= malloc((dp
->cArgs
+ 1) * sizeof(VARIANTARG
));
951 return E_OUTOFMEMORY
;
953 new_dp
.cArgs
= dp
->cArgs
+1;
954 memcpy(new_dp
.rgvarg
+1, dp
->rgvarg
, dp
->cArgs
*sizeof(VARIANTARG
));
956 V_VT(new_dp
.rgvarg
) = VT_DISPATCH
;
957 V_DISPATCH(new_dp
.rgvarg
) = (IDispatch
*)&This
->IDispatchEx_iface
;
959 hres
= IDispatch_QueryInterface(func_disp
, &IID_IDispatchEx
, (void**)&dispex
);
961 if(SUCCEEDED(hres
)) {
962 hres
= IDispatchEx_InvokeEx(dispex
, DISPID_VALUE
, lcid
, flags
, &new_dp
, res
, ei
, caller
);
963 IDispatchEx_Release(dispex
);
966 hres
= IDispatch_Invoke(func_disp
, DISPID_VALUE
, &IID_NULL
, lcid
, flags
, &new_dp
, res
, ei
, &err
);
969 TRACE("<<< %s\n", debugstr_variant(res
));
971 WARN("<<< %08lx\n", hres
);
977 static HRESULT
get_func_obj_entry(DispatchEx
*This
, func_info_t
*func
, func_obj_entry_t
**ret
)
979 dispex_dynamic_data_t
*dynamic_data
;
980 func_obj_entry_t
*entry
;
982 dynamic_data
= get_dynamic_data(This
);
984 return E_OUTOFMEMORY
;
986 if(!dynamic_data
->func_disps
) {
987 dynamic_data
->func_disps
= calloc(This
->info
->func_disp_cnt
, sizeof(*dynamic_data
->func_disps
));
988 if(!dynamic_data
->func_disps
)
989 return E_OUTOFMEMORY
;
992 entry
= dynamic_data
->func_disps
+ func
->func_disp_idx
;
993 if(!entry
->func_obj
) {
994 entry
->func_obj
= create_func_disp(This
, func
);
996 return E_OUTOFMEMORY
;
998 IDispatchEx_AddRef(&entry
->func_obj
->dispex
.IDispatchEx_iface
);
999 V_VT(&entry
->val
) = VT_DISPATCH
;
1000 V_DISPATCH(&entry
->val
) = (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
;
1007 static HRESULT
get_builtin_func(dispex_data_t
*data
, DISPID id
, func_info_t
**ret
)
1012 max
= data
->func_cnt
-1;
1017 if(data
->funcs
[n
].id
== id
) {
1018 *ret
= data
->funcs
+n
;
1022 if(data
->funcs
[n
].id
< id
)
1028 WARN("invalid id %lx\n", id
);
1029 return DISP_E_MEMBERNOTFOUND
;
1032 static HRESULT
get_builtin_id(DispatchEx
*This
, BSTR name
, DWORD grfdex
, DISPID
*ret
)
1037 max
= This
->info
->func_cnt
-1;
1042 c
= wcsicmp(This
->info
->name_table
[n
]->name
, name
);
1044 if((grfdex
& fdexNameCaseSensitive
) && wcscmp(This
->info
->name_table
[n
]->name
, name
))
1047 *ret
= This
->info
->name_table
[n
]->id
;
1057 if(This
->info
->desc
->vtbl
->get_dispid
) {
1060 hres
= This
->info
->desc
->vtbl
->get_dispid(This
, name
, grfdex
, ret
);
1061 if(hres
!= DISP_E_UNKNOWNNAME
)
1065 return DISP_E_UNKNOWNNAME
;
1068 HRESULT
change_type(VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
, IServiceProvider
*caller
)
1070 V_VT(dst
) = VT_EMPTY
;
1073 IVariantChangeType
*change_type
= NULL
;
1076 hres
= IServiceProvider_QueryService(caller
, &SID_VariantConversion
, &IID_IVariantChangeType
, (void**)&change_type
);
1077 if(SUCCEEDED(hres
)) {
1078 hres
= IVariantChangeType_ChangeType(change_type
, dst
, src
, LOCALE_NEUTRAL
, vt
);
1079 IVariantChangeType_Release(change_type
);
1087 if(V_VT(src
) == VT_BSTR
) {
1088 V_VT(dst
) = VT_BOOL
;
1089 V_BOOL(dst
) = variant_bool(V_BSTR(src
) && *V_BSTR(src
));
1095 if(V_VT(src
) == VT_EMPTY
|| V_VT(src
) == VT_NULL
) {
1097 V_DISPATCH(dst
) = NULL
;
1103 return VariantChangeType(dst
, src
, 0, vt
);
1106 static HRESULT
builtin_propget(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
, VARIANT
*res
)
1111 if(dp
&& dp
->cArgs
) {
1112 FIXME("cArgs %d\n", dp
->cArgs
);
1116 assert(func
->get_vtbl_off
);
1118 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&iface
);
1119 if(SUCCEEDED(hres
)) {
1120 switch(func
->prop_vt
) {
1121 #define CASE_VT(vt,type,access) \
1124 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
1125 if(SUCCEEDED(hres)) \
1126 access(res) = val; \
1129 BUILTIN_TYPES_SWITCH
;
1132 FIXME("Unhandled vt %d\n", func
->prop_vt
);
1135 IUnknown_Release(iface
);
1141 if(func
->prop_vt
!= VT_VARIANT
)
1142 V_VT(res
) = func
->prop_vt
== VT_PTR
? VT_DISPATCH
: func
->prop_vt
;
1146 static HRESULT
builtin_propput(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
, IServiceProvider
*caller
)
1152 if(dp
->cArgs
!= 1 || (dp
->cNamedArgs
== 1 && *dp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1153 || dp
->cNamedArgs
> 1) {
1154 FIXME("invalid args\n");
1155 return E_INVALIDARG
;
1158 if(!func
->put_vtbl_off
) {
1159 if(dispex_compat_mode(This
) >= COMPAT_MODE_IE9
) {
1160 WARN("No setter\n");
1163 FIXME("No setter\n");
1168 if(func
->prop_vt
!= VT_VARIANT
&& V_VT(v
) != func
->prop_vt
) {
1169 hres
= change_type(&tmpv
, v
, func
->prop_vt
, caller
);
1175 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&iface
);
1176 if(SUCCEEDED(hres
)) {
1177 switch(func
->prop_vt
) {
1178 #define CASE_VT(vt,type,access) \
1180 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1182 BUILTIN_TYPES_SWITCH
;
1185 FIXME("Unimplemented vt %d\n", func
->prop_vt
);
1189 IUnknown_Release(iface
);
1197 static HRESULT
invoke_builtin_function(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
,
1198 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1200 VARIANT arg_buf
[MAX_ARGS
], *arg_ptrs
[MAX_ARGS
], *arg
, retv
, ret_ref
, vhres
;
1201 unsigned i
, nconv
= 0;
1206 hres
= func
->hook(This
, DISPATCH_METHOD
, dp
, res
, ei
, caller
);
1211 if(!func
->call_vtbl_off
)
1212 return typeinfo_invoke(This
, func
, DISPATCH_METHOD
, dp
, res
, ei
);
1214 if(dp
->cArgs
+ func
->default_value_cnt
< func
->argc
) {
1215 FIXME("Invalid argument count (expected %u, got %u)\n", func
->argc
, dp
->cArgs
);
1216 return E_INVALIDARG
;
1219 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&iface
);
1223 for(i
=0; i
< func
->argc
; i
++) {
1224 BOOL own_value
= FALSE
;
1225 if(i
>= dp
->cArgs
) {
1226 /* use default value */
1227 arg_ptrs
[i
] = &func
->arg_info
[i
].default_value
;
1230 arg
= dp
->rgvarg
+dp
->cArgs
-i
-1;
1231 if(func
->arg_types
[i
] == V_VT(arg
)) {
1234 hres
= change_type(arg_buf
+nconv
, arg
, func
->arg_types
[i
], caller
);
1237 arg_ptrs
[i
] = arg_buf
+ nconv
++;
1241 if(func
->arg_types
[i
] == VT_DISPATCH
&& !IsEqualGUID(&func
->arg_info
[i
].iid
, &IID_NULL
)
1242 && V_DISPATCH(arg_ptrs
[i
])) {
1245 arg_buf
[nconv
] = *arg_ptrs
[i
];
1246 arg_ptrs
[i
] = arg_buf
+ nconv
++;
1248 hres
= IDispatch_QueryInterface(V_DISPATCH(arg_ptrs
[i
]), &func
->arg_info
[i
].iid
, (void**)&iface
);
1250 WARN("Could not get %s iface: %08lx\n", debugstr_guid(&func
->arg_info
[i
].iid
), hres
);
1254 IDispatch_Release(V_DISPATCH(arg_ptrs
[i
]));
1255 V_DISPATCH(arg_ptrs
[i
]) = iface
;
1259 if(SUCCEEDED(hres
)) {
1260 if(func
->prop_vt
== VT_VOID
) {
1261 V_VT(&retv
) = VT_EMPTY
;
1263 V_VT(&retv
) = func
->prop_vt
;
1264 arg_ptrs
[func
->argc
] = &ret_ref
;
1265 V_VT(&ret_ref
) = VT_BYREF
|func
->prop_vt
;
1267 switch(func
->prop_vt
) {
1268 #define CASE_VT(vt,type,access) \
1270 V_BYREF(&ret_ref) = &access(&retv); \
1272 BUILTIN_ARG_TYPES_SWITCH
;
1275 V_VT(&retv
) = VT_DISPATCH
;
1276 V_VT(&ret_ref
) = VT_BYREF
| VT_DISPATCH
;
1277 V_BYREF(&ret_ref
) = &V_DISPATCH(&retv
);
1284 V_VT(&vhres
) = VT_ERROR
;
1285 hres
= DispCallFunc(iface
, func
->call_vtbl_off
*sizeof(void*), CC_STDCALL
, VT_ERROR
,
1286 func
->argc
+ (func
->prop_vt
== VT_VOID
? 0 : 1), func
->arg_types
, arg_ptrs
, &vhres
);
1290 VariantClear(arg_buf
+nconv
);
1291 IUnknown_Release(iface
);
1294 if(FAILED(V_ERROR(&vhres
)))
1295 return V_ERROR(&vhres
);
1300 VariantClear(&retv
);
1301 return V_ERROR(&vhres
);
1304 static HRESULT
function_invoke(DispatchEx
*This
, func_info_t
*func
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
,
1305 EXCEPINFO
*ei
, IServiceProvider
*caller
)
1310 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
1312 return E_INVALIDARG
;
1314 case DISPATCH_METHOD
:
1315 if(This
->dynamic_data
&& This
->dynamic_data
->func_disps
1316 && This
->dynamic_data
->func_disps
[func
->func_disp_idx
].func_obj
) {
1317 func_obj_entry_t
*entry
= This
->dynamic_data
->func_disps
+ func
->func_disp_idx
;
1319 if(V_VT(&entry
->val
) != VT_DISPATCH
) {
1320 FIXME("calling %s not supported\n", debugstr_variant(&entry
->val
));
1324 if((IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
!= V_DISPATCH(&entry
->val
)) {
1325 if(!V_DISPATCH(&entry
->val
)) {
1326 FIXME("Calling null\n");
1330 hres
= invoke_disp_value(This
, V_DISPATCH(&entry
->val
), 0, flags
, dp
, res
, ei
, NULL
);
1335 hres
= invoke_builtin_function(This
, func
, dp
, res
, ei
, caller
);
1337 case DISPATCH_PROPERTYGET
: {
1338 func_obj_entry_t
*entry
;
1340 if(func
->id
== DISPID_VALUE
) {
1343 hres
= dispex_to_string(This
, &ret
);
1347 V_VT(res
) = VT_BSTR
;
1352 hres
= get_func_obj_entry(This
, func
, &entry
);
1356 V_VT(res
) = VT_EMPTY
;
1357 return VariantCopy(res
, &entry
->val
);
1359 case DISPATCH_PROPERTYPUT
: {
1360 func_obj_entry_t
*entry
;
1362 if(dp
->cArgs
!= 1 || (dp
->cNamedArgs
== 1 && *dp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1363 || dp
->cNamedArgs
> 1) {
1364 FIXME("invalid args\n");
1365 return E_INVALIDARG
;
1369 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1370 * function property using InvokeEx, it's possible to do that from jscript.
1371 * Native probably uses some undocumented interface in this case, but it should
1372 * be fine for us to allow IDispatchEx handle that.
1374 hres
= get_func_obj_entry(This
, func
, &entry
);
1378 return VariantCopy(&entry
->val
, dp
->rgvarg
);
1381 FIXME("Unimplemented flags %x\n", flags
);
1388 static HRESULT
invoke_builtin_prop(DispatchEx
*This
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
1389 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1394 hres
= get_builtin_func(This
->info
, id
, &func
);
1395 if(id
== DISPID_VALUE
&& hres
== DISP_E_MEMBERNOTFOUND
)
1396 return dispex_value(This
, lcid
, flags
, dp
, res
, ei
, caller
);
1400 if(func
->func_disp_idx
>= 0)
1401 return function_invoke(This
, func
, flags
, dp
, res
, ei
, caller
);
1404 hres
= func
->hook(This
, flags
, dp
, res
, ei
, caller
);
1410 case DISPATCH_PROPERTYPUT
:
1412 V_VT(res
) = VT_EMPTY
;
1413 hres
= builtin_propput(This
, func
, dp
, caller
);
1415 case DISPATCH_PROPERTYGET
:
1416 hres
= builtin_propget(This
, func
, dp
, res
);
1419 if(!func
->get_vtbl_off
) {
1420 hres
= typeinfo_invoke(This
, func
, flags
, dp
, res
, ei
);
1424 hres
= builtin_propget(This
, func
, NULL
, &v
);
1428 if(flags
!= (DISPATCH_PROPERTYGET
|DISPATCH_METHOD
) || dp
->cArgs
) {
1429 if(V_VT(&v
) != VT_DISPATCH
) {
1430 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v
), flags
);
1435 hres
= invoke_disp_value(This
, V_DISPATCH(&v
), lcid
, flags
, dp
, res
, ei
, caller
);
1436 IDispatch_Release(V_DISPATCH(&v
));
1448 HRESULT
dispex_call_builtin(DispatchEx
*dispex
, DISPID id
, DISPPARAMS
*dp
,
1449 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1454 hres
= get_builtin_func(dispex
->info
, id
, &func
);
1458 return invoke_builtin_function(dispex
, func
, dp
, res
, ei
, caller
);
1461 HRESULT
remove_attribute(DispatchEx
*This
, DISPID id
, VARIANT_BOOL
*success
)
1463 switch(get_dispid_type(id
)) {
1464 case DISPEXPROP_CUSTOM
:
1465 FIXME("DISPEXPROP_CUSTOM not supported\n");
1468 case DISPEXPROP_DYNAMIC
: {
1469 DWORD idx
= id
- DISPID_DYNPROP_0
;
1470 dynamic_prop_t
*prop
;
1472 prop
= This
->dynamic_data
->props
+idx
;
1473 VariantClear(&prop
->var
);
1474 prop
->flags
|= DYNPROP_DELETED
;
1475 *success
= VARIANT_TRUE
;
1478 case DISPEXPROP_BUILTIN
: {
1480 DISPPARAMS dp
= {&var
,NULL
,1,0};
1484 hres
= get_builtin_func(This
->info
, id
, &func
);
1488 /* For builtin functions, we set their value to the original function. */
1489 if(func
->func_disp_idx
>= 0) {
1490 func_obj_entry_t
*entry
;
1492 if(!This
->dynamic_data
|| !This
->dynamic_data
->func_disps
1493 || !This
->dynamic_data
->func_disps
[func
->func_disp_idx
].func_obj
) {
1494 *success
= VARIANT_FALSE
;
1498 entry
= This
->dynamic_data
->func_disps
+ func
->func_disp_idx
;
1499 if(V_VT(&entry
->val
) == VT_DISPATCH
1500 && V_DISPATCH(&entry
->val
) == (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
) {
1501 *success
= VARIANT_FALSE
;
1505 VariantClear(&entry
->val
);
1506 V_VT(&entry
->val
) = VT_DISPATCH
;
1507 V_DISPATCH(&entry
->val
) = (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
;
1508 IDispatch_AddRef(V_DISPATCH(&entry
->val
));
1509 *success
= VARIANT_TRUE
;
1512 *success
= VARIANT_TRUE
;
1514 V_VT(&var
) = VT_EMPTY
;
1515 hres
= builtin_propput(This
, func
, &dp
, NULL
);
1518 hres
= dispex_get_dprop_ref(This
, func
->name
, FALSE
, &ref
);
1519 if(FAILED(hres
) || V_VT(ref
) != VT_BSTR
)
1520 *success
= VARIANT_FALSE
;
1532 compat_mode_t
dispex_compat_mode(DispatchEx
*dispex
)
1534 return dispex
->info
!= dispex
->info
->desc
->delayed_init_info
1535 ? dispex
->info
->compat_mode
1536 : dispex
->info
->desc
->vtbl
->get_compat_mode(dispex
);
1539 HRESULT
dispex_to_string(DispatchEx
*dispex
, BSTR
*ret
)
1541 static const WCHAR prefix
[8] = L
"[object ";
1542 static const WCHAR suffix
[] = L
"]";
1543 WCHAR buf
[ARRAY_SIZE(prefix
) + 28 + ARRAY_SIZE(suffix
)], *p
= buf
;
1544 compat_mode_t compat_mode
= dispex_compat_mode(dispex
);
1545 const char *name
= dispex
->info
->desc
->name
;
1548 return E_INVALIDARG
;
1550 memcpy(p
, prefix
, sizeof(prefix
));
1551 p
+= ARRAY_SIZE(prefix
);
1552 if(compat_mode
< COMPAT_MODE_IE9
)
1557 assert(p
+ ARRAY_SIZE(suffix
) - buf
<= ARRAY_SIZE(buf
));
1559 memcpy(p
, suffix
, sizeof(suffix
));
1561 *ret
= SysAllocString(buf
);
1562 return *ret
? S_OK
: E_OUTOFMEMORY
;
1565 static dispex_data_t
*ensure_dispex_info(dispex_static_data_t
*desc
, compat_mode_t compat_mode
)
1567 if(!desc
->info_cache
[compat_mode
]) {
1568 EnterCriticalSection(&cs_dispex_static_data
);
1569 if(!desc
->info_cache
[compat_mode
])
1570 desc
->info_cache
[compat_mode
] = preprocess_dispex_data(desc
, compat_mode
);
1571 LeaveCriticalSection(&cs_dispex_static_data
);
1573 return desc
->info_cache
[compat_mode
];
1576 static BOOL
ensure_real_info(DispatchEx
*dispex
)
1578 if(dispex
->info
!= dispex
->info
->desc
->delayed_init_info
)
1581 dispex
->info
= ensure_dispex_info(dispex
->info
->desc
, dispex_compat_mode(dispex
));
1582 return dispex
->info
!= NULL
;
1585 static inline DispatchEx
*impl_from_IDispatchEx(IDispatchEx
*iface
)
1587 return CONTAINING_RECORD(iface
, DispatchEx
, IDispatchEx_iface
);
1590 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
1592 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1594 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
1597 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
1599 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1601 return IUnknown_AddRef(This
->outer
);
1604 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
1606 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1608 return IUnknown_Release(This
->outer
);
1611 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
1613 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1615 TRACE("(%p)->(%p)\n", This
, pctinfo
);
1621 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
1622 LCID lcid
, ITypeInfo
**ppTInfo
)
1624 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1627 TRACE("(%p)->(%u %lu %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
1629 hres
= get_typeinfo(This
->info
->desc
->disp_tid
, ppTInfo
);
1633 ITypeInfo_AddRef(*ppTInfo
);
1637 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
1638 LPOLESTR
*rgszNames
, UINT cNames
,
1639 LCID lcid
, DISPID
*rgDispId
)
1641 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1642 HRESULT hres
= S_OK
;
1644 TRACE("(%p)->(%s %p %u %lu %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
1647 /* Native ignores all cNames > 1, and doesn't even fill them */
1649 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[0], 0, rgDispId
);
1654 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
1655 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
1656 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1658 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1660 TRACE("(%p)->(%ld %s %ld %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
1661 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
1663 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
, pDispParams
,
1664 pVarResult
, pExcepInfo
, NULL
);
1667 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
1669 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1670 dynamic_prop_t
*dprop
;
1673 TRACE("(%p)->(%s %lx %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
1675 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameCaseInsensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
1676 FIXME("Unsupported grfdex %lx\n", grfdex
);
1678 if(!ensure_real_info(This
))
1679 return E_OUTOFMEMORY
;
1681 hres
= get_builtin_id(This
, bstrName
, grfdex
, pid
);
1682 if(hres
!= DISP_E_UNKNOWNNAME
)
1685 hres
= get_dynamic_prop(This
, bstrName
, grfdex
, &dprop
);
1689 *pid
= DISPID_DYNPROP_0
+ (dprop
- This
->dynamic_data
->props
);
1693 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
1694 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
1696 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1699 TRACE("(%p)->(%lx %lx %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1701 if(!ensure_real_info(This
))
1702 return E_OUTOFMEMORY
;
1704 if(wFlags
== (DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
))
1705 wFlags
= DISPATCH_PROPERTYPUT
;
1707 switch(get_dispid_type(id
)) {
1708 case DISPEXPROP_CUSTOM
:
1709 if(!This
->info
->desc
->vtbl
->invoke
)
1710 return DISP_E_MEMBERNOTFOUND
;
1711 return This
->info
->desc
->vtbl
->invoke(This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1713 case DISPEXPROP_DYNAMIC
: {
1714 DWORD idx
= id
- DISPID_DYNPROP_0
;
1715 dynamic_prop_t
*prop
;
1717 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1718 return DISP_E_MEMBERNOTFOUND
;
1720 prop
= This
->dynamic_data
->props
+idx
;
1723 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
1725 return E_INVALIDARG
;
1727 case DISPATCH_METHOD
:
1728 if(V_VT(&prop
->var
) != VT_DISPATCH
) {
1729 FIXME("invoke %s\n", debugstr_variant(&prop
->var
));
1733 return invoke_disp_value(This
, V_DISPATCH(&prop
->var
), lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1734 case DISPATCH_PROPERTYGET
:
1735 if(prop
->flags
& DYNPROP_DELETED
)
1736 return DISP_E_MEMBERNOTFOUND
;
1737 V_VT(pvarRes
) = VT_EMPTY
;
1738 return variant_copy(pvarRes
, &prop
->var
);
1739 case DISPATCH_PROPERTYPUT
:
1740 if(pdp
->cArgs
!= 1 || (pdp
->cNamedArgs
== 1 && *pdp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1741 || pdp
->cNamedArgs
> 1) {
1742 FIXME("invalid args\n");
1743 return E_INVALIDARG
;
1746 TRACE("put %s\n", debugstr_variant(pdp
->rgvarg
));
1747 VariantClear(&prop
->var
);
1748 hres
= variant_copy(&prop
->var
, pdp
->rgvarg
);
1752 prop
->flags
&= ~DYNPROP_DELETED
;
1755 FIXME("unhandled wFlags %x\n", wFlags
);
1759 case DISPEXPROP_BUILTIN
:
1760 if(wFlags
== DISPATCH_CONSTRUCT
) {
1761 if(id
== DISPID_VALUE
) {
1762 if(This
->info
->desc
->vtbl
->value
) {
1763 return This
->info
->desc
->vtbl
->value(This
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1765 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1768 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1772 return invoke_builtin_prop(This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1779 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR name
, DWORD grfdex
)
1781 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1785 TRACE("(%p)->(%s %lx)\n", This
, debugstr_w(name
), grfdex
);
1787 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, name
, grfdex
& ~fdexNameEnsure
, &id
);
1789 compat_mode_t compat_mode
= dispex_compat_mode(This
);
1790 TRACE("property %s not found\n", debugstr_w(name
));
1791 return compat_mode
< COMPAT_MODE_IE8
? E_NOTIMPL
:
1792 compat_mode
< COMPAT_MODE_IE9
? hres
: S_OK
;
1795 return IDispatchEx_DeleteMemberByDispID(&This
->IDispatchEx_iface
, id
);
1798 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
1800 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1802 TRACE("(%p)->(%lx)\n", This
, id
);
1804 if(is_custom_dispid(id
) && This
->info
->desc
->vtbl
->delete)
1805 return This
->info
->desc
->vtbl
->delete(This
, id
);
1807 if(dispex_compat_mode(This
) < COMPAT_MODE_IE8
) {
1808 /* Not implemented by IE */
1812 if(is_dynamic_dispid(id
)) {
1813 DWORD idx
= id
- DISPID_DYNPROP_0
;
1814 dynamic_prop_t
*prop
;
1816 if(!get_dynamic_data(This
) || idx
>= This
->dynamic_data
->prop_cnt
)
1819 prop
= This
->dynamic_data
->props
+ idx
;
1820 VariantClear(&prop
->var
);
1821 prop
->flags
|= DYNPROP_DELETED
;
1828 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
1830 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1831 FIXME("(%p)->(%lx %lx %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
1835 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
1837 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1841 TRACE("(%p)->(%lx %p)\n", This
, id
, pbstrName
);
1843 if(!ensure_real_info(This
))
1844 return E_OUTOFMEMORY
;
1846 if(is_custom_dispid(id
)) {
1847 if(This
->info
->desc
->vtbl
->get_name
)
1848 return This
->info
->desc
->vtbl
->get_name(This
, id
, pbstrName
);
1849 return DISP_E_MEMBERNOTFOUND
;
1852 if(is_dynamic_dispid(id
)) {
1853 DWORD idx
= id
- DISPID_DYNPROP_0
;
1855 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1856 return DISP_E_MEMBERNOTFOUND
;
1858 *pbstrName
= SysAllocString(This
->dynamic_data
->props
[idx
].name
);
1860 return E_OUTOFMEMORY
;
1865 hres
= get_builtin_func(This
->info
, id
, &func
);
1869 *pbstrName
= SysAllocString(func
->name
);
1871 return E_OUTOFMEMORY
;
1875 static HRESULT
next_dynamic_id(DispatchEx
*dispex
, DWORD idx
, DISPID
*ret_id
)
1877 while(idx
< dispex
->dynamic_data
->prop_cnt
&&
1878 (dispex
->dynamic_data
->props
[idx
].flags
& (DYNPROP_DELETED
| DYNPROP_HIDDEN
)))
1881 if(idx
== dispex
->dynamic_data
->prop_cnt
) {
1882 *ret_id
= DISPID_STARTENUM
;
1886 *ret_id
= DISPID_DYNPROP_0
+idx
;
1890 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
1892 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1896 TRACE("(%p)->(%lx %lx %p)\n", This
, grfdex
, id
, pid
);
1898 if(!ensure_real_info(This
))
1899 return E_OUTOFMEMORY
;
1901 if(is_dynamic_dispid(id
)) {
1902 DWORD idx
= id
- DISPID_DYNPROP_0
;
1904 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1905 return DISP_E_MEMBERNOTFOUND
;
1907 return next_dynamic_id(This
, idx
+1, pid
);
1910 if(!is_custom_dispid(id
)) {
1911 if(id
== DISPID_STARTENUM
) {
1912 func
= This
->info
->funcs
;
1914 hres
= get_builtin_func(This
->info
, id
, &func
);
1920 while(func
< This
->info
->funcs
+ This
->info
->func_cnt
) {
1921 if(func
->func_disp_idx
== -1) {
1928 id
= DISPID_STARTENUM
;
1931 if(This
->info
->desc
->vtbl
->next_dispid
) {
1932 hres
= This
->info
->desc
->vtbl
->next_dispid(This
, id
, pid
);
1937 if(get_dynamic_data(This
) && This
->dynamic_data
->prop_cnt
)
1938 return next_dynamic_id(This
, 0, pid
);
1940 *pid
= DISPID_STARTENUM
;
1944 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
1946 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1947 FIXME("(%p)->(%p)\n", This
, ppunk
);
1951 static IDispatchExVtbl DispatchExVtbl
= {
1952 DispatchEx_QueryInterface
,
1955 DispatchEx_GetTypeInfoCount
,
1956 DispatchEx_GetTypeInfo
,
1957 DispatchEx_GetIDsOfNames
,
1959 DispatchEx_GetDispID
,
1960 DispatchEx_InvokeEx
,
1961 DispatchEx_DeleteMemberByName
,
1962 DispatchEx_DeleteMemberByDispID
,
1963 DispatchEx_GetMemberProperties
,
1964 DispatchEx_GetMemberName
,
1965 DispatchEx_GetNextDispID
,
1966 DispatchEx_GetNameSpaceParent
1969 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
1971 if(IsEqualGUID(&IID_IDispatch
, riid
))
1972 *ppv
= &This
->IDispatchEx_iface
;
1973 else if(IsEqualGUID(&IID_IDispatchEx
, riid
))
1974 *ppv
= &This
->IDispatchEx_iface
;
1975 else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant
, riid
)) {
1978 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports
, riid
)) {
1979 *ppv
= &This
->IDispatchEx_iface
;
1981 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
))
1983 else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
))
1985 else if(IsEqualGUID(&IID_IMarshal
, riid
))
1987 else if(IsEqualGUID(&IID_IManagedObject
, riid
))
1993 IUnknown_AddRef((IUnknown
*)*ppv
);
1997 LONG
dispex_ref_decr(DispatchEx
*dispex
)
1999 LONG ref
= ccref_decr(&dispex
->ccref
, (nsISupports
*)&dispex
->IDispatchEx_iface
, &dispex_ccp
);
2001 /* Gecko ccref may not free the object immediatelly when ref count reaches 0, so we need
2002 * an extra care for objects that need an immediate clean up. See
2003 * NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE_WITH_LAST_RELEASE for details. */
2004 if(!ref
&& dispex
->info
->desc
->vtbl
->last_release
) {
2005 ccref_incr(&dispex
->ccref
, (nsISupports
*)&dispex
->IDispatchEx_iface
);
2006 dispex
->info
->desc
->vtbl
->last_release(dispex
);
2007 ccref_decr(&dispex
->ccref
, (nsISupports
*)&dispex
->IDispatchEx_iface
, &dispex_ccp
);
2013 static nsresult NSAPI
dispex_traverse(void *ccp
, void *p
, nsCycleCollectionTraversalCallback
*cb
)
2015 DispatchEx
*This
= impl_from_IDispatchEx(p
);
2016 dynamic_prop_t
*prop
;
2018 describe_cc_node(&This
->ccref
, This
->info
->desc
->name
, cb
);
2020 if(This
->info
->desc
->vtbl
->traverse
)
2021 This
->info
->desc
->vtbl
->traverse(This
, cb
);
2023 if(!This
->dynamic_data
)
2026 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++)
2027 traverse_variant(&prop
->var
, "dispex_data", cb
);
2029 if(This
->dynamic_data
->func_disps
) {
2030 func_obj_entry_t
*iter
= This
->dynamic_data
->func_disps
, *end
= iter
+ This
->info
->func_disp_cnt
;
2032 for(iter
= This
->dynamic_data
->func_disps
; iter
< end
; iter
++) {
2035 note_cc_edge((nsISupports
*)&iter
->func_obj
->dispex
.IDispatchEx_iface
, "func_obj", cb
);
2036 traverse_variant(&iter
->val
, "func_val", cb
);
2043 void dispex_props_unlink(DispatchEx
*This
)
2045 dynamic_prop_t
*prop
;
2047 if(!This
->dynamic_data
)
2050 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
2051 prop
->flags
|= DYNPROP_DELETED
;
2052 unlink_variant(&prop
->var
);
2055 if(This
->dynamic_data
->func_disps
) {
2056 func_obj_entry_t
*iter
= This
->dynamic_data
->func_disps
, *end
= iter
+ This
->info
->func_disp_cnt
;
2058 for(iter
= This
->dynamic_data
->func_disps
; iter
< end
; iter
++) {
2061 iter
->func_obj
->obj
= NULL
;
2062 IDispatchEx_Release(&iter
->func_obj
->dispex
.IDispatchEx_iface
);
2063 VariantClear(&iter
->val
);
2066 free(This
->dynamic_data
->func_disps
);
2067 This
->dynamic_data
->func_disps
= NULL
;
2071 static nsresult NSAPI
dispex_unlink(void *p
)
2073 DispatchEx
*This
= impl_from_IDispatchEx(p
);
2075 if(This
->info
->desc
->vtbl
->unlink
)
2076 This
->info
->desc
->vtbl
->unlink(This
);
2078 dispex_props_unlink(This
);
2082 static void NSAPI
dispex_delete_cycle_collectable(void *p
)
2084 DispatchEx
*This
= impl_from_IDispatchEx(p
);
2085 dynamic_prop_t
*prop
;
2087 if(This
->info
->desc
->vtbl
->unlink
)
2088 This
->info
->desc
->vtbl
->unlink(This
);
2090 if(!This
->dynamic_data
)
2093 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
2094 VariantClear(&prop
->var
);
2098 free(This
->dynamic_data
->props
);
2100 if(This
->dynamic_data
->func_disps
) {
2101 func_obj_entry_t
*iter
;
2103 for(iter
= This
->dynamic_data
->func_disps
; iter
< This
->dynamic_data
->func_disps
+ This
->info
->func_disp_cnt
; iter
++) {
2104 if(iter
->func_obj
) {
2105 iter
->func_obj
->obj
= NULL
;
2106 IDispatchEx_Release(&iter
->func_obj
->dispex
.IDispatchEx_iface
);
2108 VariantClear(&iter
->val
);
2111 free(This
->dynamic_data
->func_disps
);
2114 free(This
->dynamic_data
);
2117 This
->info
->desc
->vtbl
->destructor(This
);
2120 void init_dispex_cc(void)
2122 static const CCObjCallback dispex_ccp_callback
= {
2125 dispex_delete_cycle_collectable
2127 ccp_init(&dispex_ccp
, &dispex_ccp_callback
);
2130 const void *dispex_get_vtbl(DispatchEx
*dispex
)
2132 return dispex
->info
->desc
->vtbl
;
2135 void init_dispatch(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
, compat_mode_t compat_mode
)
2137 assert(compat_mode
< COMPAT_MODE_CNT
);
2139 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
2140 dispex
->outer
= outer
;
2141 dispex
->dynamic_data
= NULL
;
2142 ccref_init(&dispex
->ccref
, 1);
2144 if(data
->vtbl
->get_compat_mode
) {
2146 if(!data
->delayed_init_info
) {
2147 EnterCriticalSection(&cs_dispex_static_data
);
2148 if(!data
->delayed_init_info
) {
2149 dispex_data_t
*info
= calloc(1, sizeof(*data
->delayed_init_info
));
2152 data
->delayed_init_info
= info
;
2155 LeaveCriticalSection(&cs_dispex_static_data
);
2157 dispex
->info
= data
->delayed_init_info
;
2159 dispex
->info
= ensure_dispex_info(data
, compat_mode
);