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
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
35 static CRITICAL_SECTION cs_dispex_static_data
;
36 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
38 0, 0, &cs_dispex_static_data
,
39 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
40 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
42 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
45 static const WCHAR objectW
[] = {'[','o','b','j','e','c','t',']',0};
57 struct dispex_data_t
{
60 func_info_t
**name_table
;
72 #define DYNPROP_DELETED 0x01
76 IUnknown IUnknown_iface
;
81 struct dispex_dynamic_data_t
{
84 dynamic_prop_t
*props
;
85 func_disp_t
**func_disps
;
88 #define DISPID_DYNPROP_0 0x50000000
89 #define DISPID_DYNPROP_MAX 0x5fffffff
91 #define FDEX_VERSION_MASK 0xf0000000
93 static ITypeLib
*typelib
;
94 static ITypeInfo
*typeinfos
[LAST_tid
];
95 static struct list dispex_data_list
= LIST_INIT(dispex_data_list
);
97 static REFIID tid_ids
[] = {
98 #define XIID(iface) &IID_ ## iface,
99 #define XDIID(iface) &DIID_ ## iface,
105 static HRESULT
load_typelib(void)
110 hres
= LoadRegTypeLib(&LIBID_MSHTML
, 4, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
112 ERR("LoadRegTypeLib failed: %08x\n", hres
);
116 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
117 ITypeLib_Release(tl
);
121 static HRESULT
get_typeinfo(tid_t tid
, ITypeInfo
**typeinfo
)
126 hres
= load_typelib();
130 if(!typeinfos
[tid
]) {
133 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, tid_ids
[tid
], &ti
);
135 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids
[tid
]), hres
);
139 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), ti
, NULL
))
140 ITypeInfo_Release(ti
);
143 *typeinfo
= typeinfos
[tid
];
147 void release_typelib(void)
152 while(!list_empty(&dispex_data_list
)) {
153 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
154 list_remove(&iter
->entry
);
156 for(i
=0; i
< iter
->func_cnt
; i
++)
157 SysFreeString(iter
->funcs
[i
].name
);
159 heap_free(iter
->funcs
);
160 heap_free(iter
->name_table
);
167 for(i
=0; i
< sizeof(typeinfos
)/sizeof(*typeinfos
); i
++)
169 ITypeInfo_Release(typeinfos
[i
]);
171 ITypeLib_Release(typelib
);
172 DeleteCriticalSection(&cs_dispex_static_data
);
175 HRESULT
get_htmldoc_classinfo(ITypeInfo
**typeinfo
)
180 hres
= load_typelib();
184 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &CLSID_HTMLDocument
, typeinfo
);
186 ERR("GetTypeInfoOfGuid failed: %08x\n", hres
);
190 static void add_func_info(dispex_data_t
*data
, DWORD
*size
, tid_t tid
, const FUNCDESC
*desc
, ITypeInfo
*dti
)
195 if(data
->func_cnt
&& data
->funcs
[data
->func_cnt
-1].id
== desc
->memid
) {
196 info
= data
->funcs
+data
->func_cnt
-1;
198 if(data
->func_cnt
== *size
)
199 data
->funcs
= heap_realloc(data
->funcs
, (*size
<<= 1)*sizeof(func_info_t
));
201 info
= data
->funcs
+data
->func_cnt
;
202 hres
= ITypeInfo_GetDocumentation(dti
, desc
->memid
, &info
->name
, NULL
, NULL
, NULL
);
208 info
->id
= desc
->memid
;
210 info
->func_disp_idx
= -1;
211 info
->prop_vt
= VT_EMPTY
;
212 info
->put_vtbl_off
= 0;
213 info
->get_vtbl_off
= 0;
216 if(desc
->invkind
& DISPATCH_METHOD
) {
217 info
->func_disp_idx
= data
->func_disp_cnt
++;
218 }else if(desc
->invkind
& (DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYGET
)) {
219 VARTYPE vt
= VT_EMPTY
;
221 if(desc
->invkind
& DISPATCH_PROPERTYGET
) {
222 vt
= desc
->elemdescFunc
.tdesc
.vt
;
223 info
->get_vtbl_off
= desc
->oVft
/sizeof(void*);
225 if(desc
->invkind
& DISPATCH_PROPERTYPUT
) {
226 assert(desc
->cParams
== 1);
227 vt
= desc
->lprgelemdescParam
->tdesc
.vt
;
228 info
->put_vtbl_off
= desc
->oVft
/sizeof(void*);
231 assert(info
->prop_vt
== VT_EMPTY
|| vt
== info
->prop_vt
);
236 static int dispid_cmp(const void *p1
, const void *p2
)
238 return ((const func_info_t
*)p1
)->id
- ((const func_info_t
*)p2
)->id
;
241 static int func_name_cmp(const void *p1
, const void *p2
)
243 return strcmpiW((*(func_info_t
* const*)p1
)->name
, (*(func_info_t
* const*)p2
)->name
);
246 static dispex_data_t
*preprocess_dispex_data(DispatchEx
*This
)
248 const tid_t
*tid
= This
->data
->iface_tids
;
255 TRACE("(%p)\n", This
);
257 if(This
->data
->disp_tid
) {
258 hres
= get_typeinfo(This
->data
->disp_tid
, &dti
);
260 ERR("Could not get disp type info: %08x\n", hres
);
265 data
= heap_alloc(sizeof(dispex_data_t
));
267 data
->func_disp_cnt
= 0;
268 data
->funcs
= heap_alloc(size
*sizeof(func_info_t
));
269 list_add_tail(&dispex_data_list
, &data
->entry
);
272 hres
= get_typeinfo(*tid
, &ti
);
278 hres
= ITypeInfo_GetFuncDesc(ti
, i
++, &funcdesc
);
282 add_func_info(data
, &size
, *tid
, funcdesc
, dti
);
283 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
289 if(!data
->func_cnt
) {
290 heap_free(data
->funcs
);
291 data
->name_table
= NULL
;
297 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
298 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
300 data
->name_table
= heap_alloc(data
->func_cnt
* sizeof(func_info_t
*));
301 for(i
=0; i
< data
->func_cnt
; i
++)
302 data
->name_table
[i
] = data
->funcs
+i
;
303 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
308 static int id_cmp(const void *p1
, const void *p2
)
310 return *(const DISPID
*)p1
- *(const DISPID
*)p2
;
313 HRESULT
get_dispids(tid_t tid
, DWORD
*ret_size
, DISPID
**ret
)
315 unsigned i
, func_cnt
;
322 hres
= get_typeinfo(tid
, &ti
);
326 hres
= ITypeInfo_GetTypeAttr(ti
, &attr
);
328 ITypeInfo_Release(ti
);
332 func_cnt
= attr
->cFuncs
;
333 ITypeInfo_ReleaseTypeAttr(ti
, attr
);
335 ids
= heap_alloc(func_cnt
*sizeof(DISPID
));
337 ITypeInfo_Release(ti
);
338 return E_OUTOFMEMORY
;
341 for(i
=0; i
< func_cnt
; i
++) {
342 hres
= ITypeInfo_GetFuncDesc(ti
, i
, &funcdesc
);
346 ids
[i
] = funcdesc
->memid
;
347 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
350 ITypeInfo_Release(ti
);
356 qsort(ids
, func_cnt
, sizeof(DISPID
), id_cmp
);
358 *ret_size
= func_cnt
;
363 static dispex_data_t
*get_dispex_data(DispatchEx
*This
)
366 return This
->data
->data
;
368 EnterCriticalSection(&cs_dispex_static_data
);
370 if(!This
->data
->data
)
371 This
->data
->data
= preprocess_dispex_data(This
);
373 LeaveCriticalSection(&cs_dispex_static_data
);
375 return This
->data
->data
;
378 static inline BOOL
is_custom_dispid(DISPID id
)
380 return MSHTML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSHTML_DISPID_CUSTOM_MAX
;
383 static inline BOOL
is_dynamic_dispid(DISPID id
)
385 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
388 dispex_prop_type_t
get_dispid_type(DISPID id
)
390 if(is_dynamic_dispid(id
))
391 return DISPEXPROP_DYNAMIC
;
392 if(is_custom_dispid(id
))
393 return DISPEXPROP_CUSTOM
;
394 return DISPEXPROP_BUILTIN
;
397 static HRESULT
variant_copy(VARIANT
*dest
, VARIANT
*src
)
399 if(V_VT(src
) == VT_BSTR
&& !V_BSTR(src
)) {
400 V_VT(dest
) = VT_BSTR
;
405 return VariantCopy(dest
, src
);
408 static inline dispex_dynamic_data_t
*get_dynamic_data(DispatchEx
*This
)
410 if(This
->dynamic_data
)
411 return This
->dynamic_data
;
413 This
->dynamic_data
= heap_alloc_zero(sizeof(dispex_dynamic_data_t
));
414 if(!This
->dynamic_data
)
417 if(This
->data
->vtbl
&& This
->data
->vtbl
->populate_props
)
418 This
->data
->vtbl
->populate_props(This
);
420 return This
->dynamic_data
;
423 static HRESULT
get_dynamic_prop(DispatchEx
*This
, const WCHAR
*name
, DWORD flags
, dynamic_prop_t
**ret
)
425 const BOOL alloc
= flags
& fdexNameEnsure
;
426 dispex_dynamic_data_t
*data
;
427 dynamic_prop_t
*prop
;
429 data
= get_dynamic_data(This
);
431 return E_OUTOFMEMORY
;
433 for(prop
= data
->props
; prop
< data
->props
+data
->prop_cnt
; prop
++) {
434 if(flags
& fdexNameCaseInsensitive
? !strcmpiW(prop
->name
, name
) : !strcmpW(prop
->name
, name
)) {
435 if(prop
->flags
& DYNPROP_DELETED
) {
437 return DISP_E_UNKNOWNNAME
;
438 prop
->flags
&= ~DYNPROP_DELETED
;
446 return DISP_E_UNKNOWNNAME
;
448 TRACE("creating dynamic prop %s\n", debugstr_w(name
));
450 if(!data
->buf_size
) {
451 data
->props
= heap_alloc(sizeof(dynamic_prop_t
)*4);
453 return E_OUTOFMEMORY
;
455 }else if(data
->buf_size
== data
->prop_cnt
) {
456 dynamic_prop_t
*new_props
;
458 new_props
= heap_realloc(data
->props
, sizeof(dynamic_prop_t
)*(data
->buf_size
<<1));
460 return E_OUTOFMEMORY
;
462 data
->props
= new_props
;
463 data
->buf_size
<<= 1;
466 prop
= data
->props
+ data
->prop_cnt
;
468 prop
->name
= heap_strdupW(name
);
470 return E_OUTOFMEMORY
;
472 VariantInit(&prop
->var
);
479 HRESULT
dispex_get_dprop_ref(DispatchEx
*This
, const WCHAR
*name
, BOOL alloc
, VARIANT
**ret
)
481 dynamic_prop_t
*prop
;
484 hres
= get_dynamic_prop(This
, name
, alloc
? fdexNameEnsure
: 0, &prop
);
492 HRESULT
dispex_get_dynid(DispatchEx
*This
, const WCHAR
*name
, DISPID
*id
)
494 dynamic_prop_t
*prop
;
497 hres
= get_dynamic_prop(This
, name
, fdexNameEnsure
, &prop
);
501 *id
= DISPID_DYNPROP_0
+ (prop
- This
->dynamic_data
->props
);
505 static HRESULT
dispex_value(DispatchEx
*This
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
506 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
508 if(This
->data
->vtbl
&& This
->data
->vtbl
->value
)
509 return This
->data
->vtbl
->value(This
, lcid
, flags
, params
, res
, ei
, caller
);
512 case DISPATCH_PROPERTYGET
:
514 V_BSTR(res
) = SysAllocString(objectW
);
516 return E_OUTOFMEMORY
;
519 FIXME("Unimplemented flags %x\n", flags
);
526 static HRESULT
typeinfo_invoke(DispatchEx
*This
, func_info_t
*func
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
,
534 hres
= get_typeinfo(func
->tid
, &ti
);
536 ERR("Could not get type info: %08x\n", hres
);
540 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&unk
);
542 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids
[func
->tid
]), hres
);
546 hres
= ITypeInfo_Invoke(ti
, unk
, func
->id
, flags
, dp
, res
, ei
, &argerr
);
548 IUnknown_Release(unk
);
552 static inline func_disp_t
*impl_from_IUnknown(IUnknown
*iface
)
554 return CONTAINING_RECORD(iface
, func_disp_t
, IUnknown_iface
);
557 static HRESULT WINAPI
Function_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
559 func_disp_t
*This
= impl_from_IUnknown(iface
);
561 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
562 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
563 *ppv
= &This
->IUnknown_iface
;
564 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
565 return *ppv
? S_OK
: E_NOINTERFACE
;
568 return E_NOINTERFACE
;
571 IUnknown_AddRef((IUnknown
*)*ppv
);
575 static ULONG WINAPI
Function_AddRef(IUnknown
*iface
)
577 func_disp_t
*This
= impl_from_IUnknown(iface
);
579 TRACE("(%p)\n", This
);
581 return IDispatchEx_AddRef(&This
->obj
->IDispatchEx_iface
);
584 static ULONG WINAPI
Function_Release(IUnknown
*iface
)
586 func_disp_t
*This
= impl_from_IUnknown(iface
);
588 TRACE("(%p)\n", This
);
590 return IDispatchEx_Release(&This
->obj
->IDispatchEx_iface
);
593 static const IUnknownVtbl FunctionUnkVtbl
= {
594 Function_QueryInterface
,
599 static inline func_disp_t
*impl_from_DispatchEx(DispatchEx
*iface
)
601 return CONTAINING_RECORD(iface
, func_disp_t
, dispex
);
604 static HRESULT
function_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
605 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
607 func_disp_t
*This
= impl_from_DispatchEx(dispex
);
611 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
615 case DISPATCH_METHOD
:
616 hres
= typeinfo_invoke(This
->obj
, This
->info
, flags
, params
, res
, ei
);
619 FIXME("Unimplemented flags %x\n", flags
);
626 static const dispex_static_data_vtbl_t function_dispex_vtbl
= {
633 static const tid_t function_iface_tids
[] = {0};
635 static dispex_static_data_t function_dispex
= {
636 &function_dispex_vtbl
,
642 static func_disp_t
*create_func_disp(DispatchEx
*obj
, func_info_t
*info
)
646 ret
= heap_alloc_zero(sizeof(func_disp_t
));
650 ret
->IUnknown_iface
.lpVtbl
= &FunctionUnkVtbl
;
651 init_dispex(&ret
->dispex
, &ret
->IUnknown_iface
, &function_dispex
);
658 static HRESULT
invoke_disp_value(DispatchEx
*This
, IDispatch
*func_disp
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
659 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
661 DISPID named_arg
= DISPID_THIS
;
662 DISPPARAMS new_dp
= {NULL
, &named_arg
, 0, 1};
667 FIXME("named args not supported\n");
671 new_dp
.rgvarg
= heap_alloc((dp
->cArgs
+1)*sizeof(VARIANTARG
));
673 return E_OUTOFMEMORY
;
675 new_dp
.cArgs
= dp
->cArgs
+1;
676 memcpy(new_dp
.rgvarg
+1, dp
->rgvarg
, dp
->cArgs
*sizeof(VARIANTARG
));
678 V_VT(new_dp
.rgvarg
) = VT_DISPATCH
;
679 V_DISPATCH(new_dp
.rgvarg
) = (IDispatch
*)&This
->IDispatchEx_iface
;
681 hres
= IDispatch_QueryInterface(func_disp
, &IID_IDispatchEx
, (void**)&dispex
);
683 if(SUCCEEDED(hres
)) {
684 hres
= IDispatchEx_InvokeEx(dispex
, DISPID_VALUE
, lcid
, flags
, &new_dp
, res
, ei
, caller
);
685 IDispatchEx_Release(dispex
);
688 hres
= IDispatch_Invoke(func_disp
, DISPID_VALUE
, &IID_NULL
, lcid
, flags
, &new_dp
, res
, ei
, &err
);
691 TRACE("<<< %s\n", debugstr_variant(res
));
693 WARN("<<< %08x\n", hres
);
695 heap_free(new_dp
.rgvarg
);
699 static HRESULT
function_invoke(DispatchEx
*This
, func_info_t
*func
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
,
705 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
709 case DISPATCH_METHOD
:
710 hres
= typeinfo_invoke(This
, func
, flags
, dp
, res
, ei
);
712 case DISPATCH_PROPERTYGET
: {
713 dispex_dynamic_data_t
*dynamic_data
;
715 if(func
->id
== DISPID_VALUE
) {
718 ret
= SysAllocString(objectW
);
720 return E_OUTOFMEMORY
;
727 dynamic_data
= get_dynamic_data(This
);
729 return E_OUTOFMEMORY
;
731 if(!dynamic_data
->func_disps
) {
732 dynamic_data
->func_disps
= heap_alloc_zero(This
->data
->data
->func_disp_cnt
* sizeof(func_disp_t
*));
733 if(!dynamic_data
->func_disps
)
734 return E_OUTOFMEMORY
;
737 if(!dynamic_data
->func_disps
[func
->func_disp_idx
]) {
738 dynamic_data
->func_disps
[func
->func_disp_idx
] = create_func_disp(This
, func
);
739 if(!dynamic_data
->func_disps
[func
->func_disp_idx
])
740 return E_OUTOFMEMORY
;
743 V_VT(res
) = VT_DISPATCH
;
744 V_DISPATCH(res
) = (IDispatch
*)&dynamic_data
->func_disps
[func
->func_disp_idx
]->dispex
.IDispatchEx_iface
;
745 IDispatch_AddRef(V_DISPATCH(res
));
750 FIXME("Unimplemented flags %x\n", flags
);
752 case DISPATCH_PROPERTYPUT
:
759 static HRESULT
get_builtin_func(dispex_data_t
*data
, DISPID id
, func_info_t
**ret
)
764 max
= data
->func_cnt
-1;
769 if(data
->funcs
[n
].id
== id
) {
770 *ret
= data
->funcs
+n
;
774 if(data
->funcs
[n
].id
< id
)
780 WARN("invalid id %x\n", id
);
781 return DISP_E_UNKNOWNNAME
;
784 static HRESULT
get_builtin_id(DispatchEx
*This
, BSTR name
, DWORD grfdex
, DISPID
*ret
)
789 data
= get_dispex_data(This
);
794 max
= data
->func_cnt
-1;
799 c
= strcmpiW(data
->name_table
[n
]->name
, name
);
801 if((grfdex
& fdexNameCaseSensitive
) && strcmpW(data
->name_table
[n
]->name
, name
))
804 *ret
= data
->name_table
[n
]->id
;
814 if(This
->data
->vtbl
&& This
->data
->vtbl
->get_dispid
) {
817 hres
= This
->data
->vtbl
->get_dispid(This
, name
, grfdex
, ret
);
818 if(hres
!= DISP_E_UNKNOWNNAME
)
822 return DISP_E_UNKNOWNNAME
;
825 /* List all types used by IDispatchEx-based properties */
826 #define BUILTIN_TYPES_SWITCH \
827 CASE_VT(VT_I2, INT16, V_I2); \
828 CASE_VT(VT_I4, INT32, V_I4); \
829 CASE_VT(VT_R4, float, V_R4); \
830 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
831 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL); \
832 CASE_VT(VT_VARIANT, VARIANT, *); \
833 CASE_VT(VT_PTR, void*, V_BYREF); \
834 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
835 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH)
837 static HRESULT
change_type(VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
, IServiceProvider
*caller
)
839 V_VT(dst
) = VT_EMPTY
;
842 IVariantChangeType
*change_type
= NULL
;
845 hres
= IServiceProvider_QueryService(caller
, &SID_VariantConversion
, &IID_IVariantChangeType
, (void**)&change_type
);
846 if(SUCCEEDED(hres
)) {
847 hres
= IVariantChangeType_ChangeType(change_type
, dst
, src
, LOCALE_NEUTRAL
, vt
);
848 IVariantChangeType_Release(change_type
);
853 return VariantChangeType(dst
, src
, 0, vt
);
856 static HRESULT
builtin_propget(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
, VARIANT
*res
)
861 if(dp
&& dp
->cArgs
) {
862 FIXME("cArgs %d\n", dp
->cArgs
);
866 assert(func
->get_vtbl_off
);
868 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&iface
);
869 if(SUCCEEDED(hres
)) {
870 switch(func
->prop_vt
) {
871 #define CASE_VT(vt,type,access) \
874 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
875 if(SUCCEEDED(hres)) \
879 BUILTIN_TYPES_SWITCH
;
882 FIXME("Unhandled vt %d\n", func
->prop_vt
);
885 IUnknown_Release(iface
);
891 if(func
->prop_vt
!= VT_VARIANT
)
892 V_VT(res
) = func
->prop_vt
== VT_PTR
? VT_DISPATCH
: func
->prop_vt
;
896 static HRESULT
builtin_propput(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
, IServiceProvider
*caller
)
902 if(dp
->cArgs
!= 1 || (dp
->cNamedArgs
== 1 && *dp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
903 || dp
->cNamedArgs
> 1) {
904 FIXME("invalid args\n");
908 if(!func
->put_vtbl_off
) {
909 FIXME("No setter\n");
914 if(func
->prop_vt
!= VT_VARIANT
&& V_VT(v
) != func
->prop_vt
) {
915 hres
= change_type(&tmpv
, v
, func
->prop_vt
, caller
);
921 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[func
->tid
], (void**)&iface
);
922 if(SUCCEEDED(hres
)) {
923 switch(func
->prop_vt
) {
924 #define CASE_VT(vt,type,access) \
926 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
928 BUILTIN_TYPES_SWITCH
;
931 FIXME("Unimplemented vt %d\n", func
->prop_vt
);
935 IUnknown_Release(iface
);
943 static HRESULT
invoke_builtin_prop(DispatchEx
*This
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
944 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
950 data
= get_dispex_data(This
);
954 hres
= get_builtin_func(data
, id
, &func
);
955 if(id
== DISPID_VALUE
&& hres
== DISP_E_UNKNOWNNAME
)
956 return dispex_value(This
, lcid
, flags
, dp
, res
, ei
, caller
);
960 if(func
->func_disp_idx
!= -1)
961 return function_invoke(This
, func
, flags
, dp
, res
, ei
);
964 case DISPATCH_PROPERTYPUT
:
966 V_VT(res
) = VT_EMPTY
;
967 hres
= builtin_propput(This
, func
, dp
, caller
);
969 case DISPATCH_PROPERTYGET
:
970 hres
= builtin_propget(This
, func
, dp
, res
);
973 if(!func
->get_vtbl_off
) {
974 hres
= typeinfo_invoke(This
, func
, flags
, dp
, res
, ei
);
978 hres
= builtin_propget(This
, func
, NULL
, &v
);
982 if(flags
!= (DISPATCH_PROPERTYGET
|DISPATCH_METHOD
)) {
983 if(V_VT(&v
) != VT_DISPATCH
) {
984 FIXME("Not a function %s\n", debugstr_variant(&v
));
989 hres
= invoke_disp_value(This
, V_DISPATCH(&v
), lcid
, flags
, dp
, res
, ei
, caller
);
990 IDispatch_Release(V_DISPATCH(&v
));
994 IDispatch_Release(V_DISPATCH(&v
));
1002 HRESULT
remove_prop(DispatchEx
*This
, BSTR name
, VARIANT_BOOL
*success
)
1004 dynamic_prop_t
*prop
;
1008 hres
= get_builtin_id(This
, name
, 0, &id
);
1010 DISPID named_id
= DISPID_PROPERTYPUT
;
1012 DISPPARAMS dp
= {&var
,&named_id
,1,1};
1015 V_VT(&var
) = VT_EMPTY
;
1016 memset(&ei
, 0, sizeof(ei
));
1017 hres
= invoke_builtin_prop(This
, id
, 0, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
, NULL
);
1021 *success
= VARIANT_TRUE
;
1025 hres
= get_dynamic_prop(This
, name
, 0, &prop
);
1027 if(hres
!= DISP_E_UNKNOWNNAME
)
1029 *success
= VARIANT_FALSE
;
1033 VariantClear(&prop
->var
);
1034 prop
->flags
|= DYNPROP_DELETED
;
1035 *success
= VARIANT_TRUE
;
1039 static inline DispatchEx
*impl_from_IDispatchEx(IDispatchEx
*iface
)
1041 return CONTAINING_RECORD(iface
, DispatchEx
, IDispatchEx_iface
);
1044 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
1046 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1048 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
1051 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
1053 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1055 return IUnknown_AddRef(This
->outer
);
1058 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
1060 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1062 return IUnknown_Release(This
->outer
);
1065 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
1067 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1069 TRACE("(%p)->(%p)\n", This
, pctinfo
);
1075 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
1076 LCID lcid
, ITypeInfo
**ppTInfo
)
1078 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1081 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
1083 hres
= get_typeinfo(This
->data
->disp_tid
, ppTInfo
);
1087 ITypeInfo_AddRef(*ppTInfo
);
1091 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
1092 LPOLESTR
*rgszNames
, UINT cNames
,
1093 LCID lcid
, DISPID
*rgDispId
)
1095 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1099 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
1102 for(i
=0; i
< cNames
; i
++) {
1103 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[i
], 0, rgDispId
+i
);
1111 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
1112 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
1113 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1115 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1117 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
1118 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
1120 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
, pDispParams
,
1121 pVarResult
, pExcepInfo
, NULL
);
1124 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
1126 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1127 dynamic_prop_t
*dprop
;
1130 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
1132 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameCaseInsensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
1133 FIXME("Unsupported grfdex %x\n", grfdex
);
1135 hres
= get_builtin_id(This
, bstrName
, grfdex
, pid
);
1136 if(hres
!= DISP_E_UNKNOWNNAME
)
1139 hres
= get_dynamic_prop(This
, bstrName
, grfdex
, &dprop
);
1143 *pid
= DISPID_DYNPROP_0
+ (dprop
- This
->dynamic_data
->props
);
1147 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
1148 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
1150 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1153 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1155 switch(get_dispid_type(id
)) {
1156 case DISPEXPROP_CUSTOM
:
1157 if(!This
->data
->vtbl
|| !This
->data
->vtbl
->invoke
)
1158 return DISP_E_UNKNOWNNAME
;
1159 return This
->data
->vtbl
->invoke(This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1161 case DISPEXPROP_DYNAMIC
: {
1162 DWORD idx
= id
- DISPID_DYNPROP_0
;
1163 dynamic_prop_t
*prop
;
1165 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1166 return DISP_E_UNKNOWNNAME
;
1168 prop
= This
->dynamic_data
->props
+idx
;
1171 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
1173 return E_INVALIDARG
;
1175 case DISPATCH_METHOD
:
1176 if(V_VT(&prop
->var
) != VT_DISPATCH
) {
1177 FIXME("invoke %s\n", debugstr_variant(&prop
->var
));
1181 return invoke_disp_value(This
, V_DISPATCH(&prop
->var
), lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1182 case DISPATCH_PROPERTYGET
:
1183 if(prop
->flags
& DYNPROP_DELETED
)
1184 return DISP_E_UNKNOWNNAME
;
1185 V_VT(pvarRes
) = VT_EMPTY
;
1186 return variant_copy(pvarRes
, &prop
->var
);
1187 case DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
:
1188 case DISPATCH_PROPERTYPUT
:
1189 if(pdp
->cArgs
!= 1 || (pdp
->cNamedArgs
== 1 && *pdp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1190 || pdp
->cNamedArgs
> 1) {
1191 FIXME("invalid args\n");
1192 return E_INVALIDARG
;
1195 TRACE("put %s\n", debugstr_variant(pdp
->rgvarg
));
1196 VariantClear(&prop
->var
);
1197 hres
= variant_copy(&prop
->var
, pdp
->rgvarg
);
1201 prop
->flags
&= ~DYNPROP_DELETED
;
1204 FIXME("unhandled wFlags %x\n", wFlags
);
1208 case DISPEXPROP_BUILTIN
:
1209 if(wFlags
== DISPATCH_CONSTRUCT
) {
1210 if(id
== DISPID_VALUE
) {
1211 if(This
->data
->vtbl
&& This
->data
->vtbl
->value
) {
1212 return This
->data
->vtbl
->value(This
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1214 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1217 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1221 return invoke_builtin_prop(This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1228 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
1230 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1232 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
1234 /* Not implemented by IE */
1238 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
1240 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1242 TRACE("(%p)->(%x)\n", This
, id
);
1244 /* Not implemented by IE */
1248 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
1250 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1251 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
1255 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
1257 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1258 dispex_data_t
*data
;
1262 TRACE("(%p)->(%x %p)\n", This
, id
, pbstrName
);
1264 if(is_dynamic_dispid(id
)) {
1265 DWORD idx
= id
- DISPID_DYNPROP_0
;
1267 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1268 return DISP_E_UNKNOWNNAME
;
1270 *pbstrName
= SysAllocString(This
->dynamic_data
->props
[idx
].name
);
1272 return E_OUTOFMEMORY
;
1277 data
= get_dispex_data(This
);
1281 hres
= get_builtin_func(data
, id
, &func
);
1285 *pbstrName
= SysAllocString(func
->name
);
1287 return E_OUTOFMEMORY
;
1291 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
1293 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1294 dispex_data_t
*data
;
1298 TRACE("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
1300 if(is_dynamic_dispid(id
)) {
1301 DWORD idx
= id
- DISPID_DYNPROP_0
;
1303 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1304 return DISP_E_UNKNOWNNAME
;
1306 while(++idx
< This
->dynamic_data
->prop_cnt
&& This
->dynamic_data
->props
[idx
].flags
& DYNPROP_DELETED
);
1308 if(idx
== This
->dynamic_data
->prop_cnt
) {
1309 *pid
= DISPID_STARTENUM
;
1313 *pid
= DISPID_DYNPROP_0
+idx
;
1317 data
= get_dispex_data(This
);
1321 if(id
== DISPID_STARTENUM
) {
1324 hres
= get_builtin_func(data
, id
, &func
);
1330 while(func
< data
->funcs
+data
->func_cnt
) {
1331 /* FIXME: Skip hidden properties */
1332 if(func
->func_disp_idx
== -1) {
1339 if(get_dynamic_data(This
) && This
->dynamic_data
->prop_cnt
) {
1340 *pid
= DISPID_DYNPROP_0
;
1344 *pid
= DISPID_STARTENUM
;
1348 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
1350 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1351 FIXME("(%p)->(%p)\n", This
, ppunk
);
1355 static IDispatchExVtbl DispatchExVtbl
= {
1356 DispatchEx_QueryInterface
,
1359 DispatchEx_GetTypeInfoCount
,
1360 DispatchEx_GetTypeInfo
,
1361 DispatchEx_GetIDsOfNames
,
1363 DispatchEx_GetDispID
,
1364 DispatchEx_InvokeEx
,
1365 DispatchEx_DeleteMemberByName
,
1366 DispatchEx_DeleteMemberByDispID
,
1367 DispatchEx_GetMemberProperties
,
1368 DispatchEx_GetMemberName
,
1369 DispatchEx_GetNextDispID
,
1370 DispatchEx_GetNameSpaceParent
1373 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
1375 static const IID IID_UndocumentedScriptIface
=
1376 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1377 static const IID IID_IDispatchJS
=
1378 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1380 if(IsEqualGUID(&IID_IDispatch
, riid
)) {
1381 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
1382 *ppv
= &This
->IDispatchEx_iface
;
1383 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
1384 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
1385 *ppv
= &This
->IDispatchEx_iface
;
1386 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
1387 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This
, ppv
);
1389 }else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
)) {
1390 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This
, ppv
);
1397 IUnknown_AddRef((IUnknown
*)*ppv
);
1401 void dispex_traverse(DispatchEx
*This
, nsCycleCollectionTraversalCallback
*cb
)
1403 dynamic_prop_t
*prop
;
1405 if(!This
->dynamic_data
)
1408 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
1409 if(V_VT(&prop
->var
) == VT_DISPATCH
)
1410 note_cc_edge((nsISupports
*)V_DISPATCH(&prop
->var
), "dispex_data", cb
);
1413 /* FIXME: Traverse func_disps */
1416 void dispex_unlink(DispatchEx
*This
)
1418 dynamic_prop_t
*prop
;
1420 if(!This
->dynamic_data
)
1423 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
1424 if(V_VT(&prop
->var
) == VT_DISPATCH
) {
1425 V_VT(&prop
->var
) = VT_EMPTY
;
1426 IDispatch_Release(V_DISPATCH(&prop
->var
));
1428 VariantClear(&prop
->var
);
1433 void release_dispex(DispatchEx
*This
)
1435 dynamic_prop_t
*prop
;
1437 if(!This
->dynamic_data
)
1440 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
1441 VariantClear(&prop
->var
);
1442 heap_free(prop
->name
);
1445 heap_free(This
->dynamic_data
->props
);
1447 if(This
->dynamic_data
->func_disps
) {
1450 for(i
=0; i
< This
->data
->data
->func_disp_cnt
; i
++) {
1451 if(This
->dynamic_data
->func_disps
[i
]) {
1452 release_dispex(&This
->dynamic_data
->func_disps
[i
]->dispex
);
1453 heap_free(This
->dynamic_data
->func_disps
[i
]);
1457 heap_free(This
->dynamic_data
->func_disps
);
1460 heap_free(This
->dynamic_data
);
1463 void init_dispex(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
)
1465 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
1466 dispex
->outer
= outer
;
1467 dispex
->data
= data
;
1468 dispex
->dynamic_data
= NULL
;