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
22 #define NONAMELESSUNION
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
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
91 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: %08x\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 %u.\n", len
);
150 lstrcatW(module_path
, L
"\\1");
152 hres
= LoadTypeLibEx(module_path
, REGKIND_NONE
, &tl
);
154 ERR("LoadTypeLibEx failed for private typelib: %08x\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: %08x\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 heap_free(iter
->funcs
[i
].arg_types
);
205 heap_free(iter
->funcs
[i
].arg_info
);
206 SysFreeString(iter
->funcs
[i
].name
);
209 heap_free(iter
->funcs
);
210 heap_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: %08x\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
)
279 hres
= ITypeInfo_GetDocumentation(dti
, desc
->memid
, &name
, NULL
, NULL
, NULL
);
281 WARN("GetDocumentation failed: %08x\n", hres
);
285 for(info
= data
->funcs
; info
< data
->funcs
+data
->func_cnt
; info
++) {
286 if(info
->id
== desc
->memid
|| !wcscmp(info
->name
, name
)) {
287 if(info
->tid
!= tid
) {
289 return; /* Duplicated in other interface */
295 TRACE("adding %s...\n", debugstr_w(name
));
297 if(info
== data
->funcs
+data
->func_cnt
) {
298 if(data
->func_cnt
== data
->func_size
)
299 data
->funcs
= heap_realloc_zero(data
->funcs
, (data
->func_size
<<= 1)*sizeof(func_info_t
));
300 info
= data
->funcs
+data
->func_cnt
;
304 info
->id
= desc
->memid
;
307 info
->func_disp_idx
= -1;
308 info
->prop_vt
= VT_EMPTY
;
314 if(desc
->invkind
& DISPATCH_METHOD
) {
317 info
->func_disp_idx
= data
->func_disp_cnt
++;
318 info
->argc
= desc
->cParams
;
320 assert(info
->argc
< MAX_ARGS
);
321 assert(desc
->funckind
== FUNC_DISPATCH
);
323 info
->arg_info
= heap_alloc_zero(sizeof(*info
->arg_info
) * info
->argc
);
327 info
->prop_vt
= desc
->elemdescFunc
.tdesc
.vt
;
328 if(info
->prop_vt
!= VT_VOID
&& info
->prop_vt
!= VT_PTR
&& !is_arg_type_supported(info
->prop_vt
)) {
329 TRACE("%s: return type %d\n", debugstr_w(info
->name
), info
->prop_vt
);
330 return; /* Fallback to ITypeInfo::Invoke */
333 info
->arg_types
= heap_alloc(sizeof(*info
->arg_types
) * (info
->argc
+ (info
->prop_vt
== VT_VOID
? 0 : 1)));
337 for(i
=0; i
< info
->argc
; i
++)
338 info
->arg_types
[i
] = desc
->lprgelemdescParam
[i
].tdesc
.vt
;
340 if(info
->prop_vt
== VT_PTR
)
341 info
->arg_types
[info
->argc
] = VT_BYREF
| VT_DISPATCH
;
342 else if(info
->prop_vt
!= VT_VOID
)
343 info
->arg_types
[info
->argc
] = VT_BYREF
| info
->prop_vt
;
345 if(desc
->cParamsOpt
) {
346 TRACE("%s: optional params\n", debugstr_w(info
->name
));
347 return; /* Fallback to ITypeInfo::Invoke */
350 for(i
=0; i
< info
->argc
; i
++) {
351 TYPEDESC
*tdesc
= &desc
->lprgelemdescParam
[i
].tdesc
;
352 if(tdesc
->vt
== VT_PTR
&& tdesc
->u
.lptdesc
->vt
== VT_USERDEFINED
) {
353 ITypeInfo
*ref_type_info
;
356 hres
= ITypeInfo_GetRefTypeInfo(dti
, tdesc
->u
.lptdesc
->u
.hreftype
, &ref_type_info
);
358 ERR("Could not get referenced type info: %08x\n", hres
);
362 hres
= ITypeInfo_GetTypeAttr(ref_type_info
, &attr
);
363 if(SUCCEEDED(hres
)) {
364 assert(attr
->typekind
== TKIND_DISPATCH
);
365 info
->arg_info
[i
].iid
= attr
->guid
;
366 ITypeInfo_ReleaseTypeAttr(ref_type_info
, attr
);
368 ERR("GetTypeAttr failed: %08x\n", hres
);
370 ITypeInfo_Release(ref_type_info
);
373 info
->arg_types
[i
] = VT_DISPATCH
;
374 }else if(!is_arg_type_supported(info
->arg_types
[i
])) {
375 TRACE("%s: unsupported arg type %s\n", debugstr_w(info
->name
), debugstr_vt(info
->arg_types
[i
]));
376 return; /* Fallback to ITypeInfo for unsupported arg types */
379 if(desc
->lprgelemdescParam
[i
].u
.paramdesc
.wParamFlags
& PARAMFLAG_FHASDEFAULT
) {
380 hres
= VariantCopy(&info
->arg_info
[i
].default_value
,
381 &desc
->lprgelemdescParam
[i
].u
.paramdesc
.pparamdescex
->varDefaultValue
);
383 ERR("Could not copy default value: %08x\n", hres
);
386 TRACE("%s param %d: default value %s\n", debugstr_w(info
->name
),
387 i
, debugstr_variant(&info
->arg_info
[i
].default_value
));
388 info
->default_value_cnt
++;
392 assert(info
->argc
<= MAX_ARGS
);
393 assert(desc
->callconv
== CC_STDCALL
);
395 info
->call_vtbl_off
= desc
->oVft
/sizeof(void*);
396 }else if(desc
->invkind
& (DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYGET
)) {
397 VARTYPE vt
= VT_EMPTY
;
399 if(desc
->invkind
& DISPATCH_PROPERTYGET
) {
400 vt
= desc
->elemdescFunc
.tdesc
.vt
;
401 info
->get_vtbl_off
= desc
->oVft
/sizeof(void*);
403 if(desc
->invkind
& DISPATCH_PROPERTYPUT
) {
404 assert(desc
->cParams
== 1);
405 vt
= desc
->lprgelemdescParam
->tdesc
.vt
;
406 info
->put_vtbl_off
= desc
->oVft
/sizeof(void*);
409 assert(info
->prop_vt
== VT_EMPTY
|| vt
== info
->prop_vt
);
414 static HRESULT
process_interface(dispex_data_t
*data
, tid_t tid
, ITypeInfo
*disp_typeinfo
, const dispex_hook_t
*hooks
)
416 unsigned i
= 7; /* skip IDispatch functions */
421 hres
= get_typeinfo(tid
, &typeinfo
);
426 const dispex_hook_t
*hook
= NULL
;
428 hres
= ITypeInfo_GetFuncDesc(typeinfo
, i
++, &funcdesc
);
433 for(hook
= hooks
; hook
->dispid
!= DISPID_UNKNOWN
; hook
++) {
434 if(hook
->dispid
== funcdesc
->memid
)
437 if(hook
->dispid
== DISPID_UNKNOWN
)
441 if(!hook
|| hook
->invoke
) {
442 add_func_info(data
, tid
, funcdesc
, disp_typeinfo
? disp_typeinfo
: typeinfo
,
443 hook
? hook
->invoke
: NULL
);
446 ITypeInfo_ReleaseFuncDesc(typeinfo
, funcdesc
);
452 void dispex_info_add_interface(dispex_data_t
*info
, tid_t tid
, const dispex_hook_t
*hooks
)
456 hres
= process_interface(info
, tid
, NULL
, hooks
);
458 ERR("process_interface failed: %08x\n", hres
);
461 static int __cdecl
dispid_cmp(const void *p1
, const void *p2
)
463 return ((const func_info_t
*)p1
)->id
- ((const func_info_t
*)p2
)->id
;
466 static int __cdecl
func_name_cmp(const void *p1
, const void *p2
)
468 return wcsicmp((*(func_info_t
* const*)p1
)->name
, (*(func_info_t
* const*)p2
)->name
);
471 static dispex_data_t
*preprocess_dispex_data(dispex_static_data_t
*desc
, compat_mode_t compat_mode
)
480 hres
= get_typeinfo(desc
->disp_tid
, &dti
);
482 ERR("Could not get disp type info: %08x\n", hres
);
487 data
= heap_alloc(sizeof(dispex_data_t
));
489 ERR("Out of memory\n");
493 data
->compat_mode
= compat_mode
;
495 data
->func_disp_cnt
= 0;
496 data
->func_size
= 16;
497 data
->funcs
= heap_alloc_zero(data
->func_size
*sizeof(func_info_t
));
500 ERR("Out of memory\n");
503 list_add_tail(&dispex_data_list
, &data
->entry
);
506 desc
->init_info(data
, compat_mode
);
508 for(tid
= desc
->iface_tids
; *tid
; tid
++) {
509 hres
= process_interface(data
, *tid
, dti
, NULL
);
514 if(!data
->func_cnt
) {
515 heap_free(data
->funcs
);
516 data
->name_table
= NULL
;
523 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
524 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
526 data
->name_table
= heap_alloc(data
->func_cnt
* sizeof(func_info_t
*));
527 for(i
=0; i
< data
->func_cnt
; i
++)
528 data
->name_table
[i
] = data
->funcs
+i
;
529 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
533 static int __cdecl
id_cmp(const void *p1
, const void *p2
)
535 return *(const DISPID
*)p1
- *(const DISPID
*)p2
;
538 HRESULT
get_dispids(tid_t tid
, DWORD
*ret_size
, DISPID
**ret
)
540 unsigned i
, func_cnt
;
547 hres
= get_typeinfo(tid
, &ti
);
551 hres
= ITypeInfo_GetTypeAttr(ti
, &attr
);
553 ITypeInfo_Release(ti
);
557 func_cnt
= attr
->cFuncs
;
558 ITypeInfo_ReleaseTypeAttr(ti
, attr
);
560 ids
= heap_alloc(func_cnt
*sizeof(DISPID
));
562 ITypeInfo_Release(ti
);
563 return E_OUTOFMEMORY
;
566 for(i
=0; i
< func_cnt
; i
++) {
567 hres
= ITypeInfo_GetFuncDesc(ti
, i
, &funcdesc
);
571 ids
[i
] = funcdesc
->memid
;
572 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
575 ITypeInfo_Release(ti
);
581 qsort(ids
, func_cnt
, sizeof(DISPID
), id_cmp
);
583 *ret_size
= func_cnt
;
588 static inline BOOL
is_custom_dispid(DISPID id
)
590 return MSHTML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSHTML_DISPID_CUSTOM_MAX
;
593 static inline BOOL
is_dynamic_dispid(DISPID id
)
595 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
598 dispex_prop_type_t
get_dispid_type(DISPID id
)
600 if(is_dynamic_dispid(id
))
601 return DISPEXPROP_DYNAMIC
;
602 if(is_custom_dispid(id
))
603 return DISPEXPROP_CUSTOM
;
604 return DISPEXPROP_BUILTIN
;
607 static HRESULT
variant_copy(VARIANT
*dest
, VARIANT
*src
)
609 if(V_VT(src
) == VT_BSTR
&& !V_BSTR(src
)) {
610 V_VT(dest
) = VT_BSTR
;
615 return VariantCopy(dest
, src
);
618 static inline dispex_dynamic_data_t
*get_dynamic_data(DispatchEx
*This
)
620 if(This
->dynamic_data
)
621 return This
->dynamic_data
;
623 This
->dynamic_data
= heap_alloc_zero(sizeof(dispex_dynamic_data_t
));
624 if(!This
->dynamic_data
)
627 if(This
->info
->desc
->vtbl
&& This
->info
->desc
->vtbl
->populate_props
)
628 This
->info
->desc
->vtbl
->populate_props(This
);
630 return This
->dynamic_data
;
633 static HRESULT
get_dynamic_prop(DispatchEx
*This
, const WCHAR
*name
, DWORD flags
, dynamic_prop_t
**ret
)
635 const BOOL alloc
= flags
& fdexNameEnsure
;
636 dispex_dynamic_data_t
*data
;
637 dynamic_prop_t
*prop
;
639 data
= get_dynamic_data(This
);
641 return E_OUTOFMEMORY
;
643 for(prop
= data
->props
; prop
< data
->props
+data
->prop_cnt
; prop
++) {
644 if(flags
& fdexNameCaseInsensitive
? !wcsicmp(prop
->name
, name
) : !wcscmp(prop
->name
, name
)) {
645 if(prop
->flags
& DYNPROP_DELETED
) {
647 return DISP_E_UNKNOWNNAME
;
648 prop
->flags
&= ~DYNPROP_DELETED
;
656 return DISP_E_UNKNOWNNAME
;
658 TRACE("creating dynamic prop %s\n", debugstr_w(name
));
660 if(!data
->buf_size
) {
661 data
->props
= heap_alloc(sizeof(dynamic_prop_t
)*4);
663 return E_OUTOFMEMORY
;
665 }else if(data
->buf_size
== data
->prop_cnt
) {
666 dynamic_prop_t
*new_props
;
668 new_props
= heap_realloc(data
->props
, sizeof(dynamic_prop_t
)*(data
->buf_size
<<1));
670 return E_OUTOFMEMORY
;
672 data
->props
= new_props
;
673 data
->buf_size
<<= 1;
676 prop
= data
->props
+ data
->prop_cnt
;
678 prop
->name
= heap_strdupW(name
);
680 return E_OUTOFMEMORY
;
682 VariantInit(&prop
->var
);
689 HRESULT
dispex_get_dprop_ref(DispatchEx
*This
, const WCHAR
*name
, BOOL alloc
, VARIANT
**ret
)
691 dynamic_prop_t
*prop
;
694 hres
= get_dynamic_prop(This
, name
, alloc
? fdexNameEnsure
: 0, &prop
);
702 HRESULT
dispex_get_dynid(DispatchEx
*This
, const WCHAR
*name
, DISPID
*id
)
704 dynamic_prop_t
*prop
;
707 hres
= get_dynamic_prop(This
, name
, fdexNameEnsure
, &prop
);
711 *id
= DISPID_DYNPROP_0
+ (prop
- This
->dynamic_data
->props
);
715 static HRESULT
dispex_value(DispatchEx
*This
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
716 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
720 if(This
->info
->desc
->vtbl
&& This
->info
->desc
->vtbl
->value
)
721 return This
->info
->desc
->vtbl
->value(This
, lcid
, flags
, params
, res
, ei
, caller
);
724 case DISPATCH_PROPERTYGET
:
726 hres
= dispex_to_string(This
, &V_BSTR(res
));
731 FIXME("Unimplemented flags %x\n", flags
);
738 static HRESULT
typeinfo_invoke(DispatchEx
*This
, func_info_t
*func
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
,
741 DISPPARAMS params
= {dp
->rgvarg
, NULL
, dp
->cArgs
, 0};
747 if(params
.cArgs
> func
->argc
) {
748 params
.rgvarg
+= params
.cArgs
- func
->argc
;
749 params
.cArgs
= func
->argc
;
752 hres
= get_typeinfo(func
->tid
, &ti
);
754 ERR("Could not get type info: %08x\n", hres
);
758 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&unk
);
760 ERR("Could not get iface %s: %08x\n", debugstr_mshtml_guid(tid_ids
[func
->tid
]), hres
);
764 hres
= ITypeInfo_Invoke(ti
, unk
, func
->id
, flags
, ¶ms
, res
, ei
, &argerr
);
766 IUnknown_Release(unk
);
770 static inline func_disp_t
*impl_from_IUnknown(IUnknown
*iface
)
772 return CONTAINING_RECORD(iface
, func_disp_t
, IUnknown_iface
);
775 static HRESULT WINAPI
Function_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
777 func_disp_t
*This
= impl_from_IUnknown(iface
);
779 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
781 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
782 *ppv
= &This
->IUnknown_iface
;
783 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
784 return *ppv
? S_OK
: E_NOINTERFACE
;
787 return E_NOINTERFACE
;
790 IUnknown_AddRef((IUnknown
*)*ppv
);
794 static ULONG WINAPI
Function_AddRef(IUnknown
*iface
)
796 func_disp_t
*This
= impl_from_IUnknown(iface
);
797 LONG ref
= InterlockedIncrement(&This
->ref
);
799 TRACE("(%p) ref=%d\n", This
, ref
);
804 static ULONG WINAPI
Function_Release(IUnknown
*iface
)
806 func_disp_t
*This
= impl_from_IUnknown(iface
);
807 LONG ref
= InterlockedDecrement(&This
->ref
);
809 TRACE("(%p) ref=%d\n", This
, ref
);
813 release_dispex(&This
->dispex
);
820 static const IUnknownVtbl FunctionUnkVtbl
= {
821 Function_QueryInterface
,
826 static inline func_disp_t
*impl_from_DispatchEx(DispatchEx
*iface
)
828 return CONTAINING_RECORD(iface
, func_disp_t
, dispex
);
831 static HRESULT
function_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
832 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
834 func_disp_t
*This
= impl_from_DispatchEx(dispex
);
838 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
842 case DISPATCH_METHOD
:
845 hres
= dispex_call_builtin(This
->obj
, This
->info
->id
, params
, res
, ei
, caller
);
847 case DISPATCH_PROPERTYGET
: {
852 static const WCHAR func_prefixW
[] =
853 {'\n','f','u','n','c','t','i','o','n',' '};
854 static const WCHAR func_suffixW
[] =
855 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
857 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
859 return E_ACCESSDENIED
;
861 name_len
= SysStringLen(This
->info
->name
);
862 ptr
= str
= SysAllocStringLen(NULL
, name_len
+ ARRAY_SIZE(func_prefixW
) + ARRAY_SIZE(func_suffixW
));
864 return E_OUTOFMEMORY
;
866 memcpy(ptr
, func_prefixW
, sizeof(func_prefixW
));
867 ptr
+= ARRAY_SIZE(func_prefixW
);
869 memcpy(ptr
, This
->info
->name
, name_len
*sizeof(WCHAR
));
872 memcpy(ptr
, func_suffixW
, sizeof(func_suffixW
));
879 FIXME("Unimplemented flags %x\n", flags
);
886 static const dispex_static_data_vtbl_t function_dispex_vtbl
= {
893 static const tid_t function_iface_tids
[] = {0};
895 static dispex_static_data_t function_dispex
= {
897 &function_dispex_vtbl
,
902 static func_disp_t
*create_func_disp(DispatchEx
*obj
, func_info_t
*info
)
906 ret
= heap_alloc_zero(sizeof(func_disp_t
));
910 ret
->IUnknown_iface
.lpVtbl
= &FunctionUnkVtbl
;
911 init_dispatch(&ret
->dispex
, &ret
->IUnknown_iface
, &function_dispex
, dispex_compat_mode(obj
));
919 static HRESULT
invoke_disp_value(DispatchEx
*This
, IDispatch
*func_disp
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
920 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
922 DISPID named_arg
= DISPID_THIS
;
923 DISPPARAMS new_dp
= {NULL
, &named_arg
, 0, 1};
928 FIXME("named args not supported\n");
932 new_dp
.rgvarg
= heap_alloc((dp
->cArgs
+1)*sizeof(VARIANTARG
));
934 return E_OUTOFMEMORY
;
936 new_dp
.cArgs
= dp
->cArgs
+1;
937 memcpy(new_dp
.rgvarg
+1, dp
->rgvarg
, dp
->cArgs
*sizeof(VARIANTARG
));
939 V_VT(new_dp
.rgvarg
) = VT_DISPATCH
;
940 V_DISPATCH(new_dp
.rgvarg
) = (IDispatch
*)&This
->IDispatchEx_iface
;
942 hres
= IDispatch_QueryInterface(func_disp
, &IID_IDispatchEx
, (void**)&dispex
);
944 if(SUCCEEDED(hres
)) {
945 hres
= IDispatchEx_InvokeEx(dispex
, DISPID_VALUE
, lcid
, flags
, &new_dp
, res
, ei
, caller
);
946 IDispatchEx_Release(dispex
);
949 hres
= IDispatch_Invoke(func_disp
, DISPID_VALUE
, &IID_NULL
, lcid
, flags
, &new_dp
, res
, ei
, &err
);
952 TRACE("<<< %s\n", debugstr_variant(res
));
954 WARN("<<< %08x\n", hres
);
956 heap_free(new_dp
.rgvarg
);
960 static HRESULT
get_func_obj_entry(DispatchEx
*This
, func_info_t
*func
, func_obj_entry_t
**ret
)
962 dispex_dynamic_data_t
*dynamic_data
;
963 func_obj_entry_t
*entry
;
965 dynamic_data
= get_dynamic_data(This
);
967 return E_OUTOFMEMORY
;
969 if(!dynamic_data
->func_disps
) {
970 dynamic_data
->func_disps
= heap_alloc_zero(This
->info
->func_disp_cnt
* sizeof(*dynamic_data
->func_disps
));
971 if(!dynamic_data
->func_disps
)
972 return E_OUTOFMEMORY
;
975 entry
= dynamic_data
->func_disps
+ func
->func_disp_idx
;
976 if(!entry
->func_obj
) {
977 entry
->func_obj
= create_func_disp(This
, func
);
979 return E_OUTOFMEMORY
;
981 IDispatchEx_AddRef(&entry
->func_obj
->dispex
.IDispatchEx_iface
);
982 V_VT(&entry
->val
) = VT_DISPATCH
;
983 V_DISPATCH(&entry
->val
) = (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
;
990 static HRESULT
get_builtin_func(dispex_data_t
*data
, DISPID id
, func_info_t
**ret
)
995 max
= data
->func_cnt
-1;
1000 if(data
->funcs
[n
].id
== id
) {
1001 *ret
= data
->funcs
+n
;
1005 if(data
->funcs
[n
].id
< id
)
1011 WARN("invalid id %x\n", id
);
1012 return DISP_E_UNKNOWNNAME
;
1015 static HRESULT
get_builtin_id(DispatchEx
*This
, BSTR name
, DWORD grfdex
, DISPID
*ret
)
1020 max
= This
->info
->func_cnt
-1;
1025 c
= wcsicmp(This
->info
->name_table
[n
]->name
, name
);
1027 if((grfdex
& fdexNameCaseSensitive
) && wcscmp(This
->info
->name_table
[n
]->name
, name
))
1030 *ret
= This
->info
->name_table
[n
]->id
;
1040 if(This
->info
->desc
->vtbl
&& This
->info
->desc
->vtbl
->get_dispid
) {
1043 hres
= This
->info
->desc
->vtbl
->get_dispid(This
, name
, grfdex
, ret
);
1044 if(hres
!= DISP_E_UNKNOWNNAME
)
1048 return DISP_E_UNKNOWNNAME
;
1051 HRESULT
change_type(VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
, IServiceProvider
*caller
)
1053 V_VT(dst
) = VT_EMPTY
;
1056 IVariantChangeType
*change_type
= NULL
;
1059 hres
= IServiceProvider_QueryService(caller
, &SID_VariantConversion
, &IID_IVariantChangeType
, (void**)&change_type
);
1060 if(SUCCEEDED(hres
)) {
1061 hres
= IVariantChangeType_ChangeType(change_type
, dst
, src
, LOCALE_NEUTRAL
, vt
);
1062 IVariantChangeType_Release(change_type
);
1069 if(V_VT(src
) == VT_BSTR
) {
1070 V_VT(dst
) = VT_BOOL
;
1071 V_BOOL(dst
) = variant_bool(V_BSTR(src
) && *V_BSTR(src
));
1077 return VariantChangeType(dst
, src
, 0, vt
);
1080 static HRESULT
builtin_propget(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
, VARIANT
*res
)
1085 if(dp
&& dp
->cArgs
) {
1086 FIXME("cArgs %d\n", dp
->cArgs
);
1090 assert(func
->get_vtbl_off
);
1092 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&iface
);
1093 if(SUCCEEDED(hres
)) {
1094 switch(func
->prop_vt
) {
1095 #define CASE_VT(vt,type,access) \
1098 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
1099 if(SUCCEEDED(hres)) \
1100 access(res) = val; \
1103 BUILTIN_TYPES_SWITCH
;
1106 FIXME("Unhandled vt %d\n", func
->prop_vt
);
1109 IUnknown_Release(iface
);
1115 if(func
->prop_vt
!= VT_VARIANT
)
1116 V_VT(res
) = func
->prop_vt
== VT_PTR
? VT_DISPATCH
: func
->prop_vt
;
1120 static HRESULT
builtin_propput(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
, IServiceProvider
*caller
)
1126 if(dp
->cArgs
!= 1 || (dp
->cNamedArgs
== 1 && *dp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1127 || dp
->cNamedArgs
> 1) {
1128 FIXME("invalid args\n");
1129 return E_INVALIDARG
;
1132 if(!func
->put_vtbl_off
) {
1133 if(dispex_compat_mode(This
) >= COMPAT_MODE_IE9
) {
1134 WARN("No setter\n");
1137 FIXME("No setter\n");
1142 if(func
->prop_vt
!= VT_VARIANT
&& V_VT(v
) != func
->prop_vt
) {
1143 hres
= change_type(&tmpv
, v
, func
->prop_vt
, caller
);
1149 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&iface
);
1150 if(SUCCEEDED(hres
)) {
1151 switch(func
->prop_vt
) {
1152 #define CASE_VT(vt,type,access) \
1154 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1156 BUILTIN_TYPES_SWITCH
;
1159 FIXME("Unimplemented vt %d\n", func
->prop_vt
);
1163 IUnknown_Release(iface
);
1171 static HRESULT
invoke_builtin_function(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
,
1172 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1174 VARIANT arg_buf
[MAX_ARGS
], *arg_ptrs
[MAX_ARGS
], *arg
, retv
, ret_ref
, vhres
;
1175 unsigned i
, nconv
= 0;
1180 hres
= func
->hook(This
, DISPATCH_METHOD
, dp
, res
, ei
, caller
);
1185 if(!func
->call_vtbl_off
)
1186 return typeinfo_invoke(This
, func
, DISPATCH_METHOD
, dp
, res
, ei
);
1188 if(dp
->cArgs
+ func
->default_value_cnt
< func
->argc
) {
1189 FIXME("Invalid argument count (expected %u, got %u)\n", func
->argc
, dp
->cArgs
);
1190 return E_INVALIDARG
;
1193 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&iface
);
1197 for(i
=0; i
< func
->argc
; i
++) {
1198 BOOL own_value
= FALSE
;
1199 if(i
>= dp
->cArgs
) {
1200 /* use default value */
1201 arg_ptrs
[i
] = &func
->arg_info
[i
].default_value
;
1204 arg
= dp
->rgvarg
+dp
->cArgs
-i
-1;
1205 if(func
->arg_types
[i
] == V_VT(arg
)) {
1208 hres
= change_type(arg_buf
+nconv
, arg
, func
->arg_types
[i
], caller
);
1211 arg_ptrs
[i
] = arg_buf
+ nconv
++;
1215 if(func
->arg_types
[i
] == VT_DISPATCH
&& !IsEqualGUID(&func
->arg_info
[i
].iid
, &IID_NULL
)
1216 && V_DISPATCH(arg_ptrs
[i
])) {
1219 arg_buf
[nconv
] = *arg_ptrs
[i
];
1220 arg_ptrs
[i
] = arg_buf
+ nconv
++;
1222 hres
= IDispatch_QueryInterface(V_DISPATCH(arg_ptrs
[i
]), &func
->arg_info
[i
].iid
, (void**)&iface
);
1224 WARN("Could not get %s iface: %08x\n", debugstr_guid(&func
->arg_info
[i
].iid
), hres
);
1228 IDispatch_Release(V_DISPATCH(arg_ptrs
[i
]));
1229 V_DISPATCH(arg_ptrs
[i
]) = iface
;
1233 if(SUCCEEDED(hres
)) {
1234 if(func
->prop_vt
== VT_VOID
) {
1235 V_VT(&retv
) = VT_EMPTY
;
1237 V_VT(&retv
) = func
->prop_vt
;
1238 arg_ptrs
[func
->argc
] = &ret_ref
;
1239 V_VT(&ret_ref
) = VT_BYREF
|func
->prop_vt
;
1241 switch(func
->prop_vt
) {
1242 #define CASE_VT(vt,type,access) \
1244 V_BYREF(&ret_ref) = &access(&retv); \
1246 BUILTIN_ARG_TYPES_SWITCH
;
1249 V_VT(&retv
) = VT_DISPATCH
;
1250 V_VT(&ret_ref
) = VT_BYREF
| VT_DISPATCH
;
1251 V_BYREF(&ret_ref
) = &V_DISPATCH(&retv
);
1258 V_VT(&vhres
) = VT_ERROR
;
1259 hres
= DispCallFunc(iface
, func
->call_vtbl_off
*sizeof(void*), CC_STDCALL
, VT_ERROR
,
1260 func
->argc
+ (func
->prop_vt
== VT_VOID
? 0 : 1), func
->arg_types
, arg_ptrs
, &vhres
);
1264 VariantClear(arg_buf
+nconv
);
1265 IUnknown_Release(iface
);
1268 if(FAILED(V_ERROR(&vhres
)))
1269 return V_ERROR(&vhres
);
1274 VariantClear(&retv
);
1275 return V_ERROR(&vhres
);
1278 static HRESULT
function_invoke(DispatchEx
*This
, func_info_t
*func
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
,
1279 EXCEPINFO
*ei
, IServiceProvider
*caller
)
1284 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
1286 return E_INVALIDARG
;
1288 case DISPATCH_METHOD
:
1289 if(This
->dynamic_data
&& This
->dynamic_data
->func_disps
1290 && This
->dynamic_data
->func_disps
[func
->func_disp_idx
].func_obj
) {
1291 func_obj_entry_t
*entry
= This
->dynamic_data
->func_disps
+ func
->func_disp_idx
;
1293 if(V_VT(&entry
->val
) != VT_DISPATCH
) {
1294 FIXME("calling %s not supported\n", debugstr_variant(&entry
->val
));
1298 if((IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
!= V_DISPATCH(&entry
->val
)) {
1299 if(!V_DISPATCH(&entry
->val
)) {
1300 FIXME("Calling null\n");
1304 hres
= invoke_disp_value(This
, V_DISPATCH(&entry
->val
), 0, flags
, dp
, res
, ei
, NULL
);
1309 hres
= invoke_builtin_function(This
, func
, dp
, res
, ei
, caller
);
1311 case DISPATCH_PROPERTYGET
: {
1312 func_obj_entry_t
*entry
;
1314 if(func
->id
== DISPID_VALUE
) {
1317 hres
= dispex_to_string(This
, &ret
);
1321 V_VT(res
) = VT_BSTR
;
1326 hres
= get_func_obj_entry(This
, func
, &entry
);
1330 V_VT(res
) = VT_EMPTY
;
1331 return VariantCopy(res
, &entry
->val
);
1333 case DISPATCH_PROPERTYPUT
: {
1334 func_obj_entry_t
*entry
;
1336 if(dp
->cArgs
!= 1 || (dp
->cNamedArgs
== 1 && *dp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1337 || dp
->cNamedArgs
> 1) {
1338 FIXME("invalid args\n");
1339 return E_INVALIDARG
;
1343 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1344 * function property using InvokeEx, it's possible to do that from jscript.
1345 * Native probably uses some undocumented interface in this case, but it should
1346 * be fine for us to allow IDispatchEx handle that.
1348 hres
= get_func_obj_entry(This
, func
, &entry
);
1352 return VariantCopy(&entry
->val
, dp
->rgvarg
);
1355 FIXME("Unimplemented flags %x\n", flags
);
1362 static HRESULT
invoke_builtin_prop(DispatchEx
*This
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
1363 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1368 hres
= get_builtin_func(This
->info
, id
, &func
);
1369 if(id
== DISPID_VALUE
&& hres
== DISP_E_UNKNOWNNAME
)
1370 return dispex_value(This
, lcid
, flags
, dp
, res
, ei
, caller
);
1374 if(func
->func_disp_idx
!= -1)
1375 return function_invoke(This
, func
, flags
, dp
, res
, ei
, caller
);
1378 hres
= func
->hook(This
, flags
, dp
, res
, ei
, caller
);
1384 case DISPATCH_PROPERTYPUT
:
1386 V_VT(res
) = VT_EMPTY
;
1387 hres
= builtin_propput(This
, func
, dp
, caller
);
1389 case DISPATCH_PROPERTYGET
:
1390 hres
= builtin_propget(This
, func
, dp
, res
);
1393 if(!func
->get_vtbl_off
) {
1394 hres
= typeinfo_invoke(This
, func
, flags
, dp
, res
, ei
);
1398 hres
= builtin_propget(This
, func
, NULL
, &v
);
1402 if(flags
!= (DISPATCH_PROPERTYGET
|DISPATCH_METHOD
) || dp
->cArgs
) {
1403 if(V_VT(&v
) != VT_DISPATCH
) {
1404 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v
), flags
);
1409 hres
= invoke_disp_value(This
, V_DISPATCH(&v
), lcid
, flags
, dp
, res
, ei
, caller
);
1410 IDispatch_Release(V_DISPATCH(&v
));
1422 HRESULT
dispex_call_builtin(DispatchEx
*dispex
, DISPID id
, DISPPARAMS
*dp
,
1423 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1428 hres
= get_builtin_func(dispex
->info
, id
, &func
);
1432 return invoke_builtin_function(dispex
, func
, dp
, res
, ei
, caller
);
1435 HRESULT
remove_attribute(DispatchEx
*This
, DISPID id
, VARIANT_BOOL
*success
)
1437 switch(get_dispid_type(id
)) {
1438 case DISPEXPROP_CUSTOM
:
1439 FIXME("DISPEXPROP_CUSTOM not supported\n");
1442 case DISPEXPROP_DYNAMIC
: {
1443 DWORD idx
= id
- DISPID_DYNPROP_0
;
1444 dynamic_prop_t
*prop
;
1446 prop
= This
->dynamic_data
->props
+idx
;
1447 VariantClear(&prop
->var
);
1448 prop
->flags
|= DYNPROP_DELETED
;
1449 *success
= VARIANT_TRUE
;
1452 case DISPEXPROP_BUILTIN
: {
1454 DISPPARAMS dp
= {&var
,NULL
,1,0};
1458 hres
= get_builtin_func(This
->info
, id
, &func
);
1462 /* For builtin functions, we set their value to the original function. */
1463 if(func
->func_disp_idx
!= -1) {
1464 func_obj_entry_t
*entry
;
1466 if(!This
->dynamic_data
|| !This
->dynamic_data
->func_disps
1467 || !This
->dynamic_data
->func_disps
[func
->func_disp_idx
].func_obj
) {
1468 *success
= VARIANT_FALSE
;
1472 entry
= This
->dynamic_data
->func_disps
+ func
->func_disp_idx
;
1473 if(V_VT(&entry
->val
) == VT_DISPATCH
1474 && V_DISPATCH(&entry
->val
) == (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
) {
1475 *success
= VARIANT_FALSE
;
1479 VariantClear(&entry
->val
);
1480 V_VT(&entry
->val
) = VT_DISPATCH
;
1481 V_DISPATCH(&entry
->val
) = (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
;
1482 IDispatch_AddRef(V_DISPATCH(&entry
->val
));
1483 *success
= VARIANT_TRUE
;
1486 *success
= VARIANT_TRUE
;
1488 V_VT(&var
) = VT_EMPTY
;
1489 hres
= builtin_propput(This
, func
, &dp
, NULL
);
1492 hres
= dispex_get_dprop_ref(This
, func
->name
, FALSE
, &ref
);
1493 if(FAILED(hres
) || V_VT(ref
) != VT_BSTR
)
1494 *success
= VARIANT_FALSE
;
1506 compat_mode_t
dispex_compat_mode(DispatchEx
*dispex
)
1508 return dispex
->info
!= dispex
->info
->desc
->delayed_init_info
1509 ? dispex
->info
->compat_mode
1510 : dispex
->info
->desc
->vtbl
->get_compat_mode(dispex
);
1513 HRESULT
dispex_to_string(DispatchEx
*dispex
, BSTR
*ret
)
1515 static const WCHAR prefix
[8] = L
"[object ";
1516 static const WCHAR suffix
[] = L
"]";
1517 WCHAR buf
[ARRAY_SIZE(prefix
) + 28 + ARRAY_SIZE(suffix
)], *p
= buf
;
1518 compat_mode_t compat_mode
= dispex_compat_mode(dispex
);
1519 const WCHAR
*name
= dispex
->info
->desc
->name
;
1523 return E_INVALIDARG
;
1525 memcpy(p
, prefix
, sizeof(prefix
));
1526 p
+= ARRAY_SIZE(prefix
);
1527 if(compat_mode
< COMPAT_MODE_IE9
)
1532 memcpy(p
, name
, len
* sizeof(WCHAR
));
1535 memcpy(p
, suffix
, sizeof(suffix
));
1537 *ret
= SysAllocString(buf
);
1538 return *ret
? S_OK
: E_OUTOFMEMORY
;
1541 static dispex_data_t
*ensure_dispex_info(dispex_static_data_t
*desc
, compat_mode_t compat_mode
)
1543 if(!desc
->info_cache
[compat_mode
]) {
1544 EnterCriticalSection(&cs_dispex_static_data
);
1545 if(!desc
->info_cache
[compat_mode
])
1546 desc
->info_cache
[compat_mode
] = preprocess_dispex_data(desc
, compat_mode
);
1547 LeaveCriticalSection(&cs_dispex_static_data
);
1549 return desc
->info_cache
[compat_mode
];
1552 static BOOL
ensure_real_info(DispatchEx
*dispex
)
1554 if(dispex
->info
!= dispex
->info
->desc
->delayed_init_info
)
1557 dispex
->info
= ensure_dispex_info(dispex
->info
->desc
, dispex_compat_mode(dispex
));
1558 return dispex
->info
!= NULL
;
1561 static inline DispatchEx
*impl_from_IDispatchEx(IDispatchEx
*iface
)
1563 return CONTAINING_RECORD(iface
, DispatchEx
, IDispatchEx_iface
);
1566 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
1568 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1570 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
1573 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
1575 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1577 return IUnknown_AddRef(This
->outer
);
1580 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
1582 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1584 return IUnknown_Release(This
->outer
);
1587 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
1589 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1591 TRACE("(%p)->(%p)\n", This
, pctinfo
);
1597 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
1598 LCID lcid
, ITypeInfo
**ppTInfo
)
1600 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1603 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
1605 hres
= get_typeinfo(This
->info
->desc
->disp_tid
, ppTInfo
);
1609 ITypeInfo_AddRef(*ppTInfo
);
1613 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
1614 LPOLESTR
*rgszNames
, UINT cNames
,
1615 LCID lcid
, DISPID
*rgDispId
)
1617 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1621 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
1624 for(i
=0; i
< cNames
; i
++) {
1625 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[i
], 0, rgDispId
+i
);
1633 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
1634 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
1635 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1637 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1639 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
1640 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
1642 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
, pDispParams
,
1643 pVarResult
, pExcepInfo
, NULL
);
1646 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
1648 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1649 dynamic_prop_t
*dprop
;
1652 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
1654 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameCaseInsensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
1655 FIXME("Unsupported grfdex %x\n", grfdex
);
1657 if(!ensure_real_info(This
))
1658 return E_OUTOFMEMORY
;
1660 hres
= get_builtin_id(This
, bstrName
, grfdex
, pid
);
1661 if(hres
!= DISP_E_UNKNOWNNAME
)
1664 hres
= get_dynamic_prop(This
, bstrName
, grfdex
, &dprop
);
1668 *pid
= DISPID_DYNPROP_0
+ (dprop
- This
->dynamic_data
->props
);
1672 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
1673 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
1675 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1678 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1680 if(!ensure_real_info(This
))
1681 return E_OUTOFMEMORY
;
1683 if(wFlags
== (DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
))
1684 wFlags
= DISPATCH_PROPERTYPUT
;
1686 switch(get_dispid_type(id
)) {
1687 case DISPEXPROP_CUSTOM
:
1688 if(!This
->info
->desc
->vtbl
|| !This
->info
->desc
->vtbl
->invoke
)
1689 return DISP_E_UNKNOWNNAME
;
1690 return This
->info
->desc
->vtbl
->invoke(This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1692 case DISPEXPROP_DYNAMIC
: {
1693 DWORD idx
= id
- DISPID_DYNPROP_0
;
1694 dynamic_prop_t
*prop
;
1696 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1697 return DISP_E_UNKNOWNNAME
;
1699 prop
= This
->dynamic_data
->props
+idx
;
1702 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
1704 return E_INVALIDARG
;
1706 case DISPATCH_METHOD
:
1707 if(V_VT(&prop
->var
) != VT_DISPATCH
) {
1708 FIXME("invoke %s\n", debugstr_variant(&prop
->var
));
1712 return invoke_disp_value(This
, V_DISPATCH(&prop
->var
), lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1713 case DISPATCH_PROPERTYGET
:
1714 if(prop
->flags
& DYNPROP_DELETED
)
1715 return DISP_E_UNKNOWNNAME
;
1716 V_VT(pvarRes
) = VT_EMPTY
;
1717 return variant_copy(pvarRes
, &prop
->var
);
1718 case DISPATCH_PROPERTYPUT
:
1719 if(pdp
->cArgs
!= 1 || (pdp
->cNamedArgs
== 1 && *pdp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1720 || pdp
->cNamedArgs
> 1) {
1721 FIXME("invalid args\n");
1722 return E_INVALIDARG
;
1725 TRACE("put %s\n", debugstr_variant(pdp
->rgvarg
));
1726 VariantClear(&prop
->var
);
1727 hres
= variant_copy(&prop
->var
, pdp
->rgvarg
);
1731 prop
->flags
&= ~DYNPROP_DELETED
;
1734 FIXME("unhandled wFlags %x\n", wFlags
);
1738 case DISPEXPROP_BUILTIN
:
1739 if(wFlags
== DISPATCH_CONSTRUCT
) {
1740 if(id
== DISPID_VALUE
) {
1741 if(This
->info
->desc
->vtbl
&& This
->info
->desc
->vtbl
->value
) {
1742 return This
->info
->desc
->vtbl
->value(This
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1744 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1747 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1751 return invoke_builtin_prop(This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1758 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR name
, DWORD grfdex
)
1760 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1764 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(name
), grfdex
);
1766 if(dispex_compat_mode(This
) < COMPAT_MODE_IE8
) {
1767 /* Not implemented by IE */
1771 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, name
, grfdex
& ~fdexNameEnsure
, &id
);
1773 TRACE("property %s not found\n", debugstr_w(name
));
1774 return dispex_compat_mode(This
) < COMPAT_MODE_IE9
? hres
: S_OK
;
1777 return IDispatchEx_DeleteMemberByDispID(&This
->IDispatchEx_iface
, id
);
1780 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
1782 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1784 TRACE("(%p)->(%x)\n", This
, id
);
1786 if(dispex_compat_mode(This
) < COMPAT_MODE_IE8
) {
1787 /* Not implemented by IE */
1791 if(is_dynamic_dispid(id
)) {
1792 DWORD idx
= id
- DISPID_DYNPROP_0
;
1793 dynamic_prop_t
*prop
;
1795 if(!get_dynamic_data(This
) || idx
>= This
->dynamic_data
->prop_cnt
)
1798 prop
= This
->dynamic_data
->props
+ idx
;
1799 VariantClear(&prop
->var
);
1800 prop
->flags
|= DYNPROP_DELETED
;
1807 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
1809 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1810 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
1814 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
1816 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1820 TRACE("(%p)->(%x %p)\n", This
, id
, pbstrName
);
1822 if(!ensure_real_info(This
))
1823 return E_OUTOFMEMORY
;
1825 if(is_dynamic_dispid(id
)) {
1826 DWORD idx
= id
- DISPID_DYNPROP_0
;
1828 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1829 return DISP_E_UNKNOWNNAME
;
1831 *pbstrName
= SysAllocString(This
->dynamic_data
->props
[idx
].name
);
1833 return E_OUTOFMEMORY
;
1838 hres
= get_builtin_func(This
->info
, id
, &func
);
1842 *pbstrName
= SysAllocString(func
->name
);
1844 return E_OUTOFMEMORY
;
1848 static HRESULT
next_dynamic_id(DispatchEx
*dispex
, DWORD idx
, DISPID
*ret_id
)
1850 while(idx
< dispex
->dynamic_data
->prop_cnt
&& dispex
->dynamic_data
->props
[idx
].flags
& DYNPROP_DELETED
)
1853 if(idx
== dispex
->dynamic_data
->prop_cnt
) {
1854 *ret_id
= DISPID_STARTENUM
;
1858 *ret_id
= DISPID_DYNPROP_0
+idx
;
1862 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
1864 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1868 TRACE("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
1870 if(!ensure_real_info(This
))
1871 return E_OUTOFMEMORY
;
1873 if(is_dynamic_dispid(id
)) {
1874 DWORD idx
= id
- DISPID_DYNPROP_0
;
1876 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1877 return DISP_E_UNKNOWNNAME
;
1879 return next_dynamic_id(This
, idx
+1, pid
);
1882 if(id
== DISPID_STARTENUM
) {
1883 func
= This
->info
->funcs
;
1885 hres
= get_builtin_func(This
->info
, id
, &func
);
1891 while(func
< This
->info
->funcs
+ This
->info
->func_cnt
) {
1892 /* FIXME: Skip hidden properties */
1893 if(func
->func_disp_idx
== -1) {
1900 if(get_dynamic_data(This
) && This
->dynamic_data
->prop_cnt
)
1901 return next_dynamic_id(This
, 0, pid
);
1903 *pid
= DISPID_STARTENUM
;
1907 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
1909 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1910 FIXME("(%p)->(%p)\n", This
, ppunk
);
1914 static IDispatchExVtbl DispatchExVtbl
= {
1915 DispatchEx_QueryInterface
,
1918 DispatchEx_GetTypeInfoCount
,
1919 DispatchEx_GetTypeInfo
,
1920 DispatchEx_GetIDsOfNames
,
1922 DispatchEx_GetDispID
,
1923 DispatchEx_InvokeEx
,
1924 DispatchEx_DeleteMemberByName
,
1925 DispatchEx_DeleteMemberByDispID
,
1926 DispatchEx_GetMemberProperties
,
1927 DispatchEx_GetMemberName
,
1928 DispatchEx_GetNextDispID
,
1929 DispatchEx_GetNameSpaceParent
1932 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
1934 if(IsEqualGUID(&IID_IDispatch
, riid
))
1935 *ppv
= &This
->IDispatchEx_iface
;
1936 else if(IsEqualGUID(&IID_IDispatchEx
, riid
))
1937 *ppv
= &This
->IDispatchEx_iface
;
1938 else if(IsEqualGUID(&IID_IDispatchJS
, riid
))
1940 else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
))
1942 else if(IsEqualGUID(&IID_IMarshal
, riid
))
1944 else if(IsEqualGUID(&IID_IManagedObject
, riid
))
1950 IUnknown_AddRef((IUnknown
*)*ppv
);
1954 void dispex_traverse(DispatchEx
*This
, nsCycleCollectionTraversalCallback
*cb
)
1956 dynamic_prop_t
*prop
;
1958 if(!This
->dynamic_data
)
1961 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
1962 if(V_VT(&prop
->var
) == VT_DISPATCH
)
1963 note_cc_edge((nsISupports
*)V_DISPATCH(&prop
->var
), "dispex_data", cb
);
1966 /* FIXME: Traverse func_disps */
1969 void dispex_unlink(DispatchEx
*This
)
1971 dynamic_prop_t
*prop
;
1973 if(!This
->dynamic_data
)
1976 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
1977 if(V_VT(&prop
->var
) == VT_DISPATCH
) {
1978 V_VT(&prop
->var
) = VT_EMPTY
;
1979 IDispatch_Release(V_DISPATCH(&prop
->var
));
1981 VariantClear(&prop
->var
);
1986 const void *dispex_get_vtbl(DispatchEx
*dispex
)
1988 return dispex
->info
->desc
->vtbl
;
1991 void release_dispex(DispatchEx
*This
)
1993 dynamic_prop_t
*prop
;
1995 if(!This
->dynamic_data
)
1998 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
1999 VariantClear(&prop
->var
);
2000 heap_free(prop
->name
);
2003 heap_free(This
->dynamic_data
->props
);
2005 if(This
->dynamic_data
->func_disps
) {
2006 func_obj_entry_t
*iter
;
2008 for(iter
= This
->dynamic_data
->func_disps
; iter
< This
->dynamic_data
->func_disps
+ This
->info
->func_disp_cnt
; iter
++) {
2009 if(iter
->func_obj
) {
2010 iter
->func_obj
->obj
= NULL
;
2011 IDispatchEx_Release(&iter
->func_obj
->dispex
.IDispatchEx_iface
);
2013 VariantClear(&iter
->val
);
2016 heap_free(This
->dynamic_data
->func_disps
);
2019 heap_free(This
->dynamic_data
);
2022 void init_dispatch(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
, compat_mode_t compat_mode
)
2024 assert(compat_mode
< COMPAT_MODE_CNT
);
2026 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
2027 dispex
->outer
= outer
;
2028 dispex
->dynamic_data
= NULL
;
2030 if(data
->vtbl
&& data
->vtbl
->get_compat_mode
) {
2032 if(!data
->delayed_init_info
) {
2033 EnterCriticalSection(&cs_dispex_static_data
);
2034 if(!data
->delayed_init_info
) {
2035 dispex_data_t
*info
= heap_alloc_zero(sizeof(*data
->delayed_init_info
));
2038 data
->delayed_init_info
= info
;
2041 LeaveCriticalSection(&cs_dispex_static_data
);
2043 dispex
->info
= data
->delayed_init_info
;
2045 dispex
->info
= ensure_dispex_info(data
, compat_mode
);