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
97 func_disp_t
*func_obj
;
101 struct dispex_dynamic_data_t
{
104 dynamic_prop_t
*props
;
105 func_obj_entry_t
*func_disps
;
108 #define DISPID_DYNPROP_0 0x50000000
109 #define DISPID_DYNPROP_MAX 0x5fffffff
111 #define FDEX_VERSION_MASK 0xf0000000
113 static ITypeLib
*typelib
, *typelib_private
;
114 static ITypeInfo
*typeinfos
[LAST_tid
];
115 static struct list dispex_data_list
= LIST_INIT(dispex_data_list
);
117 static REFIID tid_ids
[] = {
118 #define XIID(iface) &IID_ ## iface,
119 #define XDIID(iface) &DIID_ ## iface,
127 static HRESULT
load_typelib(void)
129 WCHAR module_path
[MAX_PATH
+ 3];
134 hres
= LoadRegTypeLib(&LIBID_MSHTML
, 4, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
136 ERR("LoadRegTypeLib failed: %08lx\n", hres
);
140 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
141 ITypeLib_Release(tl
);
143 len
= GetModuleFileNameW(hInst
, module_path
, MAX_PATH
+ 1);
144 if (!len
|| len
== MAX_PATH
+ 1)
146 ERR("Could not get module file name, len %lu.\n", len
);
149 lstrcatW(module_path
, L
"\\1");
151 hres
= LoadTypeLibEx(module_path
, REGKIND_NONE
, &tl
);
153 ERR("LoadTypeLibEx failed for private typelib: %08lx\n", hres
);
157 if(InterlockedCompareExchangePointer((void**)&typelib_private
, tl
, NULL
))
158 ITypeLib_Release(tl
);
163 static HRESULT
get_typeinfo(tid_t tid
, ITypeInfo
**typeinfo
)
168 hres
= load_typelib();
172 if(!typeinfos
[tid
]) {
175 hres
= ITypeLib_GetTypeInfoOfGuid(tid
> LAST_public_tid
? typelib_private
: typelib
, tid_ids
[tid
], &ti
);
177 ERR("GetTypeInfoOfGuid(%s) failed: %08lx\n", debugstr_mshtml_guid(tid_ids
[tid
]), hres
);
181 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), ti
, NULL
))
182 ITypeInfo_Release(ti
);
185 *typeinfo
= typeinfos
[tid
];
189 void release_typelib(void)
194 while(!list_empty(&dispex_data_list
)) {
195 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
196 list_remove(&iter
->entry
);
198 for(i
= 0; i
< iter
->func_cnt
; i
++) {
199 if(iter
->funcs
[i
].default_value_cnt
&& iter
->funcs
[i
].arg_info
) {
200 for(j
= 0; j
< iter
->funcs
[i
].argc
; j
++)
201 VariantClear(&iter
->funcs
[i
].arg_info
[j
].default_value
);
203 free(iter
->funcs
[i
].arg_types
);
204 free(iter
->funcs
[i
].arg_info
);
205 SysFreeString(iter
->funcs
[i
].name
);
209 free(iter
->name_table
);
216 for(i
=0; i
< ARRAY_SIZE(typeinfos
); i
++)
218 ITypeInfo_Release(typeinfos
[i
]);
220 ITypeLib_Release(typelib
);
221 ITypeLib_Release(typelib_private
);
222 DeleteCriticalSection(&cs_dispex_static_data
);
225 HRESULT
get_class_typeinfo(const CLSID
*clsid
, ITypeInfo
**typeinfo
)
230 hres
= load_typelib();
234 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, clsid
, typeinfo
);
236 hres
= ITypeLib_GetTypeInfoOfGuid(typelib_private
, clsid
, typeinfo
);
238 ERR("GetTypeInfoOfGuid failed: %08lx\n", hres
);
242 /* Not all argument types are supported yet */
243 #define BUILTIN_ARG_TYPES_SWITCH \
244 CASE_VT(VT_I2, INT16, V_I2); \
245 CASE_VT(VT_UI2, UINT16, V_UI2); \
246 CASE_VT(VT_I4, INT32, V_I4); \
247 CASE_VT(VT_UI4, UINT32, V_UI4); \
248 CASE_VT(VT_R4, float, V_R4); \
249 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
250 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH); \
251 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
253 /* List all types used by IDispatchEx-based properties */
254 #define BUILTIN_TYPES_SWITCH \
255 BUILTIN_ARG_TYPES_SWITCH; \
256 CASE_VT(VT_VARIANT, VARIANT, *); \
257 CASE_VT(VT_PTR, void*, V_BYREF); \
258 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
259 CASE_VT(VT_UI8, ULONGLONG, V_UI8)
261 static BOOL
is_arg_type_supported(VARTYPE vt
)
264 #define CASE_VT(x,a,b) case x: return TRUE
265 BUILTIN_ARG_TYPES_SWITCH
;
271 static void add_func_info(dispex_data_t
*data
, tid_t tid
, const FUNCDESC
*desc
, ITypeInfo
*dti
,
272 dispex_hook_invoke_t hook
, const WCHAR
*name_override
)
279 name
= SysAllocString(name_override
);
281 hres
= ITypeInfo_GetDocumentation(dti
, desc
->memid
, &name
, NULL
, NULL
, NULL
);
283 WARN("GetDocumentation failed: %08lx\n", hres
);
288 for(info
= data
->funcs
; info
< data
->funcs
+data
->func_cnt
; info
++) {
289 if(info
->id
== desc
->memid
|| !wcscmp(info
->name
, name
)) {
290 if(info
->tid
!= tid
) {
292 return; /* Duplicated in other interface */
298 TRACE("adding %s...\n", debugstr_w(name
));
300 if(info
== data
->funcs
+data
->func_cnt
) {
301 if(data
->func_cnt
== data
->func_size
) {
302 info
= realloc(data
->funcs
, data
->func_size
* 2 * sizeof(func_info_t
));
307 memset(info
+ data
->func_size
, 0, data
->func_size
* sizeof(func_info_t
));
309 data
->func_size
*= 2;
311 info
= data
->funcs
+data
->func_cnt
;
315 info
->id
= desc
->memid
;
318 info
->func_disp_idx
= -1;
319 info
->prop_vt
= VT_EMPTY
;
325 if(desc
->invkind
& DISPATCH_METHOD
) {
328 info
->func_disp_idx
= data
->func_disp_cnt
++;
329 info
->argc
= desc
->cParams
;
331 assert(info
->argc
< MAX_ARGS
);
332 assert(desc
->funckind
== FUNC_DISPATCH
);
334 info
->arg_info
= calloc(info
->argc
, sizeof(*info
->arg_info
));
338 info
->prop_vt
= desc
->elemdescFunc
.tdesc
.vt
;
339 if(info
->prop_vt
!= VT_VOID
&& info
->prop_vt
!= VT_PTR
&& !is_arg_type_supported(info
->prop_vt
)) {
340 TRACE("%s: return type %d\n", debugstr_w(info
->name
), info
->prop_vt
);
341 return; /* Fallback to ITypeInfo::Invoke */
344 info
->arg_types
= malloc(sizeof(*info
->arg_types
) * (info
->argc
+ (info
->prop_vt
== VT_VOID
? 0 : 1)));
348 for(i
=0; i
< info
->argc
; i
++)
349 info
->arg_types
[i
] = desc
->lprgelemdescParam
[i
].tdesc
.vt
;
351 if(info
->prop_vt
== VT_PTR
)
352 info
->arg_types
[info
->argc
] = VT_BYREF
| VT_DISPATCH
;
353 else if(info
->prop_vt
!= VT_VOID
)
354 info
->arg_types
[info
->argc
] = VT_BYREF
| info
->prop_vt
;
356 if(desc
->cParamsOpt
) {
357 TRACE("%s: optional params\n", debugstr_w(info
->name
));
358 return; /* Fallback to ITypeInfo::Invoke */
361 for(i
=0; i
< info
->argc
; i
++) {
362 TYPEDESC
*tdesc
= &desc
->lprgelemdescParam
[i
].tdesc
;
363 if(tdesc
->vt
== VT_PTR
&& tdesc
->lptdesc
->vt
== VT_USERDEFINED
) {
364 ITypeInfo
*ref_type_info
;
367 hres
= ITypeInfo_GetRefTypeInfo(dti
, tdesc
->lptdesc
->hreftype
, &ref_type_info
);
369 ERR("Could not get referenced type info: %08lx\n", hres
);
373 hres
= ITypeInfo_GetTypeAttr(ref_type_info
, &attr
);
374 if(SUCCEEDED(hres
)) {
375 assert(attr
->typekind
== TKIND_DISPATCH
);
376 info
->arg_info
[i
].iid
= attr
->guid
;
377 ITypeInfo_ReleaseTypeAttr(ref_type_info
, attr
);
379 ERR("GetTypeAttr failed: %08lx\n", hres
);
381 ITypeInfo_Release(ref_type_info
);
384 info
->arg_types
[i
] = VT_DISPATCH
;
385 }else if(!is_arg_type_supported(info
->arg_types
[i
])) {
386 TRACE("%s: unsupported arg type %s\n", debugstr_w(info
->name
), debugstr_vt(info
->arg_types
[i
]));
387 return; /* Fallback to ITypeInfo for unsupported arg types */
390 if(desc
->lprgelemdescParam
[i
].paramdesc
.wParamFlags
& PARAMFLAG_FHASDEFAULT
) {
391 hres
= VariantCopy(&info
->arg_info
[i
].default_value
,
392 &desc
->lprgelemdescParam
[i
].paramdesc
.pparamdescex
->varDefaultValue
);
394 ERR("Could not copy default value: %08lx\n", hres
);
397 TRACE("%s param %d: default value %s\n", debugstr_w(info
->name
),
398 i
, debugstr_variant(&info
->arg_info
[i
].default_value
));
399 info
->default_value_cnt
++;
403 assert(info
->argc
<= MAX_ARGS
);
404 assert(desc
->callconv
== CC_STDCALL
);
406 info
->call_vtbl_off
= desc
->oVft
/sizeof(void*);
407 }else if(desc
->invkind
& (DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYGET
)) {
408 VARTYPE vt
= VT_EMPTY
;
410 if(desc
->wFuncFlags
& FUNCFLAG_FHIDDEN
)
411 info
->func_disp_idx
= -2;
413 if(desc
->invkind
& DISPATCH_PROPERTYGET
) {
414 vt
= desc
->elemdescFunc
.tdesc
.vt
;
415 info
->get_vtbl_off
= desc
->oVft
/sizeof(void*);
417 if(desc
->invkind
& DISPATCH_PROPERTYPUT
) {
418 assert(desc
->cParams
== 1);
419 vt
= desc
->lprgelemdescParam
->tdesc
.vt
;
420 info
->put_vtbl_off
= desc
->oVft
/sizeof(void*);
423 assert(info
->prop_vt
== VT_EMPTY
|| vt
== info
->prop_vt
);
428 static HRESULT
process_interface(dispex_data_t
*data
, tid_t tid
, ITypeInfo
*disp_typeinfo
, const dispex_hook_t
*hooks
)
430 unsigned i
= 7; /* skip IDispatch functions */
435 hres
= get_typeinfo(tid
, &typeinfo
);
440 const dispex_hook_t
*hook
= NULL
;
442 hres
= ITypeInfo_GetFuncDesc(typeinfo
, i
++, &funcdesc
);
447 for(hook
= hooks
; hook
->dispid
!= DISPID_UNKNOWN
; hook
++) {
448 if(hook
->dispid
== funcdesc
->memid
)
451 if(hook
->dispid
== DISPID_UNKNOWN
)
455 if(!hook
|| hook
->invoke
|| hook
->name
) {
456 add_func_info(data
, tid
, funcdesc
, disp_typeinfo
? disp_typeinfo
: typeinfo
,
457 hook
? hook
->invoke
: NULL
, hook
? hook
->name
: NULL
);
460 ITypeInfo_ReleaseFuncDesc(typeinfo
, funcdesc
);
466 void dispex_info_add_interface(dispex_data_t
*info
, tid_t tid
, const dispex_hook_t
*hooks
)
470 hres
= process_interface(info
, tid
, NULL
, hooks
);
472 ERR("process_interface failed: %08lx\n", hres
);
475 static int __cdecl
dispid_cmp(const void *p1
, const void *p2
)
477 return ((const func_info_t
*)p1
)->id
- ((const func_info_t
*)p2
)->id
;
480 static int __cdecl
func_name_cmp(const void *p1
, const void *p2
)
482 return wcsicmp((*(func_info_t
* const*)p1
)->name
, (*(func_info_t
* const*)p2
)->name
);
485 static dispex_data_t
*preprocess_dispex_data(dispex_static_data_t
*desc
, compat_mode_t compat_mode
)
494 hres
= get_typeinfo(desc
->disp_tid
, &dti
);
496 ERR("Could not get disp type info: %08lx\n", hres
);
501 data
= malloc(sizeof(dispex_data_t
));
503 ERR("Out of memory\n");
507 data
->compat_mode
= compat_mode
;
509 data
->func_disp_cnt
= 0;
510 data
->func_size
= 16;
511 data
->funcs
= calloc(data
->func_size
, sizeof(func_info_t
));
514 ERR("Out of memory\n");
517 list_add_tail(&dispex_data_list
, &data
->entry
);
520 desc
->init_info(data
, compat_mode
);
522 for(tid
= desc
->iface_tids
; *tid
; tid
++) {
523 hres
= process_interface(data
, *tid
, dti
, NULL
);
528 if(!data
->func_cnt
) {
530 data
->name_table
= NULL
;
537 data
->funcs
= realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
538 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
540 data
->name_table
= malloc(data
->func_cnt
* sizeof(func_info_t
*));
541 for(i
=0; i
< data
->func_cnt
; i
++)
542 data
->name_table
[i
] = data
->funcs
+i
;
543 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
547 static int __cdecl
id_cmp(const void *p1
, const void *p2
)
549 return *(const DISPID
*)p1
- *(const DISPID
*)p2
;
552 HRESULT
get_dispids(tid_t tid
, DWORD
*ret_size
, DISPID
**ret
)
554 unsigned i
, func_cnt
;
561 hres
= get_typeinfo(tid
, &ti
);
565 hres
= ITypeInfo_GetTypeAttr(ti
, &attr
);
567 ITypeInfo_Release(ti
);
571 func_cnt
= attr
->cFuncs
;
572 ITypeInfo_ReleaseTypeAttr(ti
, attr
);
574 ids
= malloc(func_cnt
* sizeof(DISPID
));
576 ITypeInfo_Release(ti
);
577 return E_OUTOFMEMORY
;
580 for(i
=0; i
< func_cnt
; i
++) {
581 hres
= ITypeInfo_GetFuncDesc(ti
, i
, &funcdesc
);
585 ids
[i
] = funcdesc
->memid
;
586 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
589 ITypeInfo_Release(ti
);
595 qsort(ids
, func_cnt
, sizeof(DISPID
), id_cmp
);
597 *ret_size
= func_cnt
;
602 static inline BOOL
is_custom_dispid(DISPID id
)
604 return MSHTML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSHTML_DISPID_CUSTOM_MAX
;
607 static inline BOOL
is_dynamic_dispid(DISPID id
)
609 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
612 dispex_prop_type_t
get_dispid_type(DISPID id
)
614 if(is_dynamic_dispid(id
))
615 return DISPEXPROP_DYNAMIC
;
616 if(is_custom_dispid(id
))
617 return DISPEXPROP_CUSTOM
;
618 return DISPEXPROP_BUILTIN
;
621 static HRESULT
variant_copy(VARIANT
*dest
, VARIANT
*src
)
623 if(V_VT(src
) == VT_BSTR
&& !V_BSTR(src
)) {
624 V_VT(dest
) = VT_BSTR
;
629 return VariantCopy(dest
, src
);
632 static inline dispex_dynamic_data_t
*get_dynamic_data(DispatchEx
*This
)
634 if(This
->dynamic_data
)
635 return This
->dynamic_data
;
637 This
->dynamic_data
= calloc(1, sizeof(dispex_dynamic_data_t
));
638 if(!This
->dynamic_data
)
641 if(This
->info
->desc
->vtbl
->populate_props
)
642 This
->info
->desc
->vtbl
->populate_props(This
);
644 return This
->dynamic_data
;
647 static HRESULT
get_dynamic_prop(DispatchEx
*This
, const WCHAR
*name
, DWORD flags
, dynamic_prop_t
**ret
)
649 const BOOL alloc
= flags
& fdexNameEnsure
;
650 dispex_dynamic_data_t
*data
;
651 dynamic_prop_t
*prop
;
653 data
= get_dynamic_data(This
);
655 return E_OUTOFMEMORY
;
657 for(prop
= data
->props
; prop
< data
->props
+data
->prop_cnt
; prop
++) {
658 if(flags
& fdexNameCaseInsensitive
? !wcsicmp(prop
->name
, name
) : !wcscmp(prop
->name
, name
)) {
659 if(prop
->flags
& DYNPROP_DELETED
) {
661 return DISP_E_UNKNOWNNAME
;
662 prop
->flags
&= ~DYNPROP_DELETED
;
670 return DISP_E_UNKNOWNNAME
;
672 TRACE("creating dynamic prop %s\n", debugstr_w(name
));
674 if(!data
->buf_size
) {
675 data
->props
= malloc(sizeof(dynamic_prop_t
) * 4);
677 return E_OUTOFMEMORY
;
679 }else if(data
->buf_size
== data
->prop_cnt
) {
680 dynamic_prop_t
*new_props
;
682 new_props
= realloc(data
->props
, sizeof(dynamic_prop_t
) * (data
->buf_size
<< 1));
684 return E_OUTOFMEMORY
;
686 data
->props
= new_props
;
687 data
->buf_size
<<= 1;
690 prop
= data
->props
+ data
->prop_cnt
;
692 prop
->name
= wcsdup(name
);
694 return E_OUTOFMEMORY
;
696 VariantInit(&prop
->var
);
703 HRESULT
dispex_get_dprop_ref(DispatchEx
*This
, const WCHAR
*name
, BOOL alloc
, VARIANT
**ret
)
705 dynamic_prop_t
*prop
;
708 hres
= get_dynamic_prop(This
, name
, alloc
? fdexNameEnsure
: 0, &prop
);
713 prop
->flags
|= DYNPROP_HIDDEN
;
718 HRESULT
dispex_get_dynid(DispatchEx
*This
, const WCHAR
*name
, BOOL hidden
, DISPID
*id
)
720 dynamic_prop_t
*prop
;
723 hres
= get_dynamic_prop(This
, name
, fdexNameEnsure
, &prop
);
728 prop
->flags
|= DYNPROP_HIDDEN
;
729 *id
= DISPID_DYNPROP_0
+ (prop
- This
->dynamic_data
->props
);
733 static HRESULT
dispex_value(DispatchEx
*This
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
734 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
738 if(This
->info
->desc
->vtbl
->value
)
739 return This
->info
->desc
->vtbl
->value(This
, lcid
, flags
, params
, res
, ei
, caller
);
742 case DISPATCH_PROPERTYGET
:
744 hres
= dispex_to_string(This
, &V_BSTR(res
));
749 FIXME("Unimplemented flags %x\n", flags
);
756 static HRESULT
typeinfo_invoke(DispatchEx
*This
, func_info_t
*func
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
,
759 DISPPARAMS params
= {dp
->rgvarg
, NULL
, dp
->cArgs
, 0};
765 if(params
.cArgs
> func
->argc
) {
766 params
.rgvarg
+= params
.cArgs
- func
->argc
;
767 params
.cArgs
= func
->argc
;
770 hres
= get_typeinfo(func
->tid
, &ti
);
772 ERR("Could not get type info: %08lx\n", hres
);
776 hres
= IDispatchEx_QueryInterface(&This
->IDispatchEx_iface
, tid_ids
[func
->tid
], (void**)&unk
);
778 ERR("Could not get iface %s: %08lx\n", debugstr_mshtml_guid(tid_ids
[func
->tid
]), hres
);
782 hres
= ITypeInfo_Invoke(ti
, unk
, func
->id
, flags
, ¶ms
, res
, ei
, &argerr
);
784 IUnknown_Release(unk
);
788 static inline func_disp_t
*impl_from_DispatchEx(DispatchEx
*iface
)
790 return CONTAINING_RECORD(iface
, func_disp_t
, dispex
);
793 static void function_destructor(DispatchEx
*dispex
)
795 func_disp_t
*This
= impl_from_DispatchEx(dispex
);
800 static HRESULT
function_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
801 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
803 func_disp_t
*This
= impl_from_DispatchEx(dispex
);
807 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
811 case DISPATCH_METHOD
:
814 hres
= dispex_call_builtin(This
->obj
, This
->info
->id
, params
, res
, ei
, caller
);
816 case DISPATCH_PROPERTYGET
: {
821 static const WCHAR func_prefixW
[] =
822 {'\n','f','u','n','c','t','i','o','n',' '};
823 static const WCHAR func_suffixW
[] =
824 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
826 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
828 return E_ACCESSDENIED
;
830 name_len
= SysStringLen(This
->info
->name
);
831 ptr
= str
= SysAllocStringLen(NULL
, name_len
+ ARRAY_SIZE(func_prefixW
) + ARRAY_SIZE(func_suffixW
));
833 return E_OUTOFMEMORY
;
835 memcpy(ptr
, func_prefixW
, sizeof(func_prefixW
));
836 ptr
+= ARRAY_SIZE(func_prefixW
);
838 memcpy(ptr
, This
->info
->name
, name_len
*sizeof(WCHAR
));
841 memcpy(ptr
, func_suffixW
, sizeof(func_suffixW
));
848 FIXME("Unimplemented flags %x\n", flags
);
855 static const dispex_static_data_vtbl_t function_dispex_vtbl
= {
856 .destructor
= function_destructor
,
857 .value
= function_value
,
860 static const tid_t function_iface_tids
[] = {0};
862 static dispex_static_data_t function_dispex
= {
864 &function_dispex_vtbl
,
869 static func_disp_t
*create_func_disp(DispatchEx
*obj
, func_info_t
*info
)
873 ret
= calloc(1, sizeof(func_disp_t
));
877 init_dispatch(&ret
->dispex
, &function_dispex
, dispex_compat_mode(obj
));
884 static HRESULT
invoke_disp_value(DispatchEx
*This
, IDispatch
*func_disp
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
885 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
887 DISPID named_arg
= DISPID_THIS
;
888 DISPPARAMS new_dp
= {NULL
, &named_arg
, 0, 1};
893 FIXME("named args not supported\n");
897 new_dp
.rgvarg
= malloc((dp
->cArgs
+ 1) * sizeof(VARIANTARG
));
899 return E_OUTOFMEMORY
;
901 new_dp
.cArgs
= dp
->cArgs
+1;
902 memcpy(new_dp
.rgvarg
+1, dp
->rgvarg
, dp
->cArgs
*sizeof(VARIANTARG
));
904 V_VT(new_dp
.rgvarg
) = VT_DISPATCH
;
905 V_DISPATCH(new_dp
.rgvarg
) = (IDispatch
*)&This
->IDispatchEx_iface
;
907 hres
= IDispatch_QueryInterface(func_disp
, &IID_IDispatchEx
, (void**)&dispex
);
909 if(SUCCEEDED(hres
)) {
910 hres
= IDispatchEx_InvokeEx(dispex
, DISPID_VALUE
, lcid
, flags
, &new_dp
, res
, ei
, caller
);
911 IDispatchEx_Release(dispex
);
914 hres
= IDispatch_Invoke(func_disp
, DISPID_VALUE
, &IID_NULL
, lcid
, flags
, &new_dp
, res
, ei
, &err
);
917 TRACE("<<< %s\n", debugstr_variant(res
));
919 WARN("<<< %08lx\n", hres
);
925 static HRESULT
get_func_obj_entry(DispatchEx
*This
, func_info_t
*func
, func_obj_entry_t
**ret
)
927 dispex_dynamic_data_t
*dynamic_data
;
928 func_obj_entry_t
*entry
;
930 dynamic_data
= get_dynamic_data(This
);
932 return E_OUTOFMEMORY
;
934 if(!dynamic_data
->func_disps
) {
935 dynamic_data
->func_disps
= calloc(This
->info
->func_disp_cnt
, sizeof(*dynamic_data
->func_disps
));
936 if(!dynamic_data
->func_disps
)
937 return E_OUTOFMEMORY
;
940 entry
= dynamic_data
->func_disps
+ func
->func_disp_idx
;
941 if(!entry
->func_obj
) {
942 entry
->func_obj
= create_func_disp(This
, func
);
944 return E_OUTOFMEMORY
;
946 IDispatchEx_AddRef(&entry
->func_obj
->dispex
.IDispatchEx_iface
);
947 V_VT(&entry
->val
) = VT_DISPATCH
;
948 V_DISPATCH(&entry
->val
) = (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
;
955 static HRESULT
get_builtin_func(dispex_data_t
*data
, DISPID id
, func_info_t
**ret
)
960 max
= data
->func_cnt
-1;
965 if(data
->funcs
[n
].id
== id
) {
966 *ret
= data
->funcs
+n
;
970 if(data
->funcs
[n
].id
< id
)
976 WARN("invalid id %lx\n", id
);
977 return DISP_E_MEMBERNOTFOUND
;
980 static HRESULT
get_builtin_id(DispatchEx
*This
, BSTR name
, DWORD grfdex
, DISPID
*ret
)
985 max
= This
->info
->func_cnt
-1;
990 c
= wcsicmp(This
->info
->name_table
[n
]->name
, name
);
992 if((grfdex
& fdexNameCaseSensitive
) && wcscmp(This
->info
->name_table
[n
]->name
, name
))
995 *ret
= This
->info
->name_table
[n
]->id
;
1005 if(This
->info
->desc
->vtbl
->get_dispid
) {
1008 hres
= This
->info
->desc
->vtbl
->get_dispid(This
, name
, grfdex
, ret
);
1009 if(hres
!= DISP_E_UNKNOWNNAME
)
1013 return DISP_E_UNKNOWNNAME
;
1016 HRESULT
change_type(VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
, IServiceProvider
*caller
)
1018 V_VT(dst
) = VT_EMPTY
;
1021 IVariantChangeType
*change_type
= NULL
;
1024 hres
= IServiceProvider_QueryService(caller
, &SID_VariantConversion
, &IID_IVariantChangeType
, (void**)&change_type
);
1025 if(SUCCEEDED(hres
)) {
1026 hres
= IVariantChangeType_ChangeType(change_type
, dst
, src
, LOCALE_NEUTRAL
, vt
);
1027 IVariantChangeType_Release(change_type
);
1035 if(V_VT(src
) == VT_BSTR
) {
1036 V_VT(dst
) = VT_BOOL
;
1037 V_BOOL(dst
) = variant_bool(V_BSTR(src
) && *V_BSTR(src
));
1043 if(V_VT(src
) == VT_EMPTY
|| V_VT(src
) == VT_NULL
) {
1045 V_DISPATCH(dst
) = NULL
;
1051 return VariantChangeType(dst
, src
, 0, vt
);
1054 static HRESULT
builtin_propget(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
, VARIANT
*res
)
1059 if(dp
&& dp
->cArgs
) {
1060 FIXME("cArgs %d\n", dp
->cArgs
);
1064 assert(func
->get_vtbl_off
);
1066 hres
= IDispatchEx_QueryInterface(&This
->IDispatchEx_iface
, tid_ids
[func
->tid
], (void**)&iface
);
1067 if(SUCCEEDED(hres
)) {
1068 switch(func
->prop_vt
) {
1069 #define CASE_VT(vt,type,access) \
1072 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
1073 if(SUCCEEDED(hres)) \
1074 access(res) = val; \
1077 BUILTIN_TYPES_SWITCH
;
1080 FIXME("Unhandled vt %d\n", func
->prop_vt
);
1083 IUnknown_Release(iface
);
1089 if(func
->prop_vt
!= VT_VARIANT
)
1090 V_VT(res
) = func
->prop_vt
== VT_PTR
? VT_DISPATCH
: func
->prop_vt
;
1094 static HRESULT
builtin_propput(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
, IServiceProvider
*caller
)
1100 if(dp
->cArgs
!= 1 || (dp
->cNamedArgs
== 1 && *dp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1101 || dp
->cNamedArgs
> 1) {
1102 FIXME("invalid args\n");
1103 return E_INVALIDARG
;
1106 if(!func
->put_vtbl_off
) {
1107 if(dispex_compat_mode(This
) >= COMPAT_MODE_IE9
) {
1108 WARN("No setter\n");
1111 FIXME("No setter\n");
1116 if(func
->prop_vt
!= VT_VARIANT
&& V_VT(v
) != func
->prop_vt
) {
1117 hres
= change_type(&tmpv
, v
, func
->prop_vt
, caller
);
1123 hres
= IDispatchEx_QueryInterface(&This
->IDispatchEx_iface
, tid_ids
[func
->tid
], (void**)&iface
);
1124 if(SUCCEEDED(hres
)) {
1125 switch(func
->prop_vt
) {
1126 #define CASE_VT(vt,type,access) \
1128 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1130 BUILTIN_TYPES_SWITCH
;
1133 FIXME("Unimplemented vt %d\n", func
->prop_vt
);
1137 IUnknown_Release(iface
);
1145 static HRESULT
invoke_builtin_function(DispatchEx
*This
, func_info_t
*func
, DISPPARAMS
*dp
,
1146 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1148 VARIANT arg_buf
[MAX_ARGS
], *arg_ptrs
[MAX_ARGS
], *arg
, retv
, ret_ref
, vhres
;
1149 unsigned i
, nconv
= 0;
1154 hres
= func
->hook(This
, DISPATCH_METHOD
, dp
, res
, ei
, caller
);
1159 if(!func
->call_vtbl_off
)
1160 return typeinfo_invoke(This
, func
, DISPATCH_METHOD
, dp
, res
, ei
);
1162 if(dp
->cArgs
+ func
->default_value_cnt
< func
->argc
) {
1163 FIXME("Invalid argument count (expected %u, got %u)\n", func
->argc
, dp
->cArgs
);
1164 return E_INVALIDARG
;
1167 hres
= IDispatchEx_QueryInterface(&This
->IDispatchEx_iface
, tid_ids
[func
->tid
], (void**)&iface
);
1171 for(i
=0; i
< func
->argc
; i
++) {
1172 BOOL own_value
= FALSE
;
1173 if(i
>= dp
->cArgs
) {
1174 /* use default value */
1175 arg_ptrs
[i
] = &func
->arg_info
[i
].default_value
;
1178 arg
= dp
->rgvarg
+dp
->cArgs
-i
-1;
1179 if(func
->arg_types
[i
] == V_VT(arg
)) {
1182 hres
= change_type(arg_buf
+nconv
, arg
, func
->arg_types
[i
], caller
);
1185 arg_ptrs
[i
] = arg_buf
+ nconv
++;
1189 if(func
->arg_types
[i
] == VT_DISPATCH
&& !IsEqualGUID(&func
->arg_info
[i
].iid
, &IID_NULL
)
1190 && V_DISPATCH(arg_ptrs
[i
])) {
1193 arg_buf
[nconv
] = *arg_ptrs
[i
];
1194 arg_ptrs
[i
] = arg_buf
+ nconv
++;
1196 hres
= IDispatch_QueryInterface(V_DISPATCH(arg_ptrs
[i
]), &func
->arg_info
[i
].iid
, (void**)&iface
);
1198 WARN("Could not get %s iface: %08lx\n", debugstr_guid(&func
->arg_info
[i
].iid
), hres
);
1202 IDispatch_Release(V_DISPATCH(arg_ptrs
[i
]));
1203 V_DISPATCH(arg_ptrs
[i
]) = iface
;
1207 if(SUCCEEDED(hres
)) {
1208 if(func
->prop_vt
== VT_VOID
) {
1209 V_VT(&retv
) = VT_EMPTY
;
1211 V_VT(&retv
) = func
->prop_vt
;
1212 arg_ptrs
[func
->argc
] = &ret_ref
;
1213 V_VT(&ret_ref
) = VT_BYREF
|func
->prop_vt
;
1215 switch(func
->prop_vt
) {
1216 #define CASE_VT(vt,type,access) \
1218 V_BYREF(&ret_ref) = &access(&retv); \
1220 BUILTIN_ARG_TYPES_SWITCH
;
1223 V_VT(&retv
) = VT_DISPATCH
;
1224 V_VT(&ret_ref
) = VT_BYREF
| VT_DISPATCH
;
1225 V_BYREF(&ret_ref
) = &V_DISPATCH(&retv
);
1232 V_VT(&vhres
) = VT_ERROR
;
1233 hres
= DispCallFunc(iface
, func
->call_vtbl_off
*sizeof(void*), CC_STDCALL
, VT_ERROR
,
1234 func
->argc
+ (func
->prop_vt
== VT_VOID
? 0 : 1), func
->arg_types
, arg_ptrs
, &vhres
);
1238 VariantClear(arg_buf
+nconv
);
1239 IUnknown_Release(iface
);
1242 if(FAILED(V_ERROR(&vhres
)))
1243 return V_ERROR(&vhres
);
1248 VariantClear(&retv
);
1249 return V_ERROR(&vhres
);
1252 static HRESULT
function_invoke(DispatchEx
*This
, func_info_t
*func
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
,
1253 EXCEPINFO
*ei
, IServiceProvider
*caller
)
1258 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
1260 return E_INVALIDARG
;
1262 case DISPATCH_METHOD
:
1263 if(This
->dynamic_data
&& This
->dynamic_data
->func_disps
1264 && This
->dynamic_data
->func_disps
[func
->func_disp_idx
].func_obj
) {
1265 func_obj_entry_t
*entry
= This
->dynamic_data
->func_disps
+ func
->func_disp_idx
;
1267 if(V_VT(&entry
->val
) != VT_DISPATCH
) {
1268 FIXME("calling %s not supported\n", debugstr_variant(&entry
->val
));
1272 if((IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
!= V_DISPATCH(&entry
->val
)) {
1273 if(!V_DISPATCH(&entry
->val
)) {
1274 FIXME("Calling null\n");
1278 hres
= invoke_disp_value(This
, V_DISPATCH(&entry
->val
), 0, flags
, dp
, res
, ei
, NULL
);
1283 hres
= invoke_builtin_function(This
, func
, dp
, res
, ei
, caller
);
1285 case DISPATCH_PROPERTYGET
: {
1286 func_obj_entry_t
*entry
;
1288 if(func
->id
== DISPID_VALUE
) {
1291 hres
= dispex_to_string(This
, &ret
);
1295 V_VT(res
) = VT_BSTR
;
1300 hres
= get_func_obj_entry(This
, func
, &entry
);
1304 V_VT(res
) = VT_EMPTY
;
1305 return VariantCopy(res
, &entry
->val
);
1307 case DISPATCH_PROPERTYPUT
: {
1308 func_obj_entry_t
*entry
;
1310 if(dp
->cArgs
!= 1 || (dp
->cNamedArgs
== 1 && *dp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1311 || dp
->cNamedArgs
> 1) {
1312 FIXME("invalid args\n");
1313 return E_INVALIDARG
;
1317 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1318 * function property using InvokeEx, it's possible to do that from jscript.
1319 * Native probably uses some undocumented interface in this case, but it should
1320 * be fine for us to allow IDispatchEx handle that.
1322 hres
= get_func_obj_entry(This
, func
, &entry
);
1326 return VariantCopy(&entry
->val
, dp
->rgvarg
);
1329 FIXME("Unimplemented flags %x\n", flags
);
1336 static HRESULT
invoke_builtin_prop(DispatchEx
*This
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
1337 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1342 hres
= get_builtin_func(This
->info
, id
, &func
);
1343 if(id
== DISPID_VALUE
&& hres
== DISP_E_MEMBERNOTFOUND
)
1344 return dispex_value(This
, lcid
, flags
, dp
, res
, ei
, caller
);
1348 if(func
->func_disp_idx
>= 0)
1349 return function_invoke(This
, func
, flags
, dp
, res
, ei
, caller
);
1352 hres
= func
->hook(This
, flags
, dp
, res
, ei
, caller
);
1358 case DISPATCH_PROPERTYPUT
:
1360 V_VT(res
) = VT_EMPTY
;
1361 hres
= builtin_propput(This
, func
, dp
, caller
);
1363 case DISPATCH_PROPERTYGET
:
1364 hres
= builtin_propget(This
, func
, dp
, res
);
1367 if(!func
->get_vtbl_off
) {
1368 hres
= typeinfo_invoke(This
, func
, flags
, dp
, res
, ei
);
1372 hres
= builtin_propget(This
, func
, NULL
, &v
);
1376 if(flags
!= (DISPATCH_PROPERTYGET
|DISPATCH_METHOD
) || dp
->cArgs
) {
1377 if(V_VT(&v
) != VT_DISPATCH
) {
1378 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v
), flags
);
1383 hres
= invoke_disp_value(This
, V_DISPATCH(&v
), lcid
, flags
, dp
, res
, ei
, caller
);
1384 IDispatch_Release(V_DISPATCH(&v
));
1396 HRESULT
dispex_call_builtin(DispatchEx
*dispex
, DISPID id
, DISPPARAMS
*dp
,
1397 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
1402 hres
= get_builtin_func(dispex
->info
, id
, &func
);
1406 return invoke_builtin_function(dispex
, func
, dp
, res
, ei
, caller
);
1409 HRESULT
remove_attribute(DispatchEx
*This
, DISPID id
, VARIANT_BOOL
*success
)
1411 switch(get_dispid_type(id
)) {
1412 case DISPEXPROP_CUSTOM
:
1413 FIXME("DISPEXPROP_CUSTOM not supported\n");
1416 case DISPEXPROP_DYNAMIC
: {
1417 DWORD idx
= id
- DISPID_DYNPROP_0
;
1418 dynamic_prop_t
*prop
;
1420 prop
= This
->dynamic_data
->props
+idx
;
1421 VariantClear(&prop
->var
);
1422 prop
->flags
|= DYNPROP_DELETED
;
1423 *success
= VARIANT_TRUE
;
1426 case DISPEXPROP_BUILTIN
: {
1428 DISPPARAMS dp
= {&var
,NULL
,1,0};
1432 hres
= get_builtin_func(This
->info
, id
, &func
);
1436 /* For builtin functions, we set their value to the original function. */
1437 if(func
->func_disp_idx
>= 0) {
1438 func_obj_entry_t
*entry
;
1440 if(!This
->dynamic_data
|| !This
->dynamic_data
->func_disps
1441 || !This
->dynamic_data
->func_disps
[func
->func_disp_idx
].func_obj
) {
1442 *success
= VARIANT_FALSE
;
1446 entry
= This
->dynamic_data
->func_disps
+ func
->func_disp_idx
;
1447 if(V_VT(&entry
->val
) == VT_DISPATCH
1448 && V_DISPATCH(&entry
->val
) == (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
) {
1449 *success
= VARIANT_FALSE
;
1453 VariantClear(&entry
->val
);
1454 V_VT(&entry
->val
) = VT_DISPATCH
;
1455 V_DISPATCH(&entry
->val
) = (IDispatch
*)&entry
->func_obj
->dispex
.IDispatchEx_iface
;
1456 IDispatch_AddRef(V_DISPATCH(&entry
->val
));
1457 *success
= VARIANT_TRUE
;
1460 *success
= VARIANT_TRUE
;
1462 V_VT(&var
) = VT_EMPTY
;
1463 hres
= builtin_propput(This
, func
, &dp
, NULL
);
1466 hres
= dispex_get_dprop_ref(This
, func
->name
, FALSE
, &ref
);
1467 if(FAILED(hres
) || V_VT(ref
) != VT_BSTR
)
1468 *success
= VARIANT_FALSE
;
1480 compat_mode_t
dispex_compat_mode(DispatchEx
*dispex
)
1482 return dispex
->info
!= dispex
->info
->desc
->delayed_init_info
1483 ? dispex
->info
->compat_mode
1484 : dispex
->info
->desc
->vtbl
->get_compat_mode(dispex
);
1487 HRESULT
dispex_to_string(DispatchEx
*dispex
, BSTR
*ret
)
1489 static const WCHAR prefix
[8] = L
"[object ";
1490 static const WCHAR suffix
[] = L
"]";
1491 WCHAR buf
[ARRAY_SIZE(prefix
) + 28 + ARRAY_SIZE(suffix
)], *p
= buf
;
1492 compat_mode_t compat_mode
= dispex_compat_mode(dispex
);
1493 const char *name
= dispex
->info
->desc
->name
;
1496 return E_INVALIDARG
;
1498 memcpy(p
, prefix
, sizeof(prefix
));
1499 p
+= ARRAY_SIZE(prefix
);
1500 if(compat_mode
< COMPAT_MODE_IE9
)
1505 assert(p
+ ARRAY_SIZE(suffix
) - buf
<= ARRAY_SIZE(buf
));
1507 memcpy(p
, suffix
, sizeof(suffix
));
1509 *ret
= SysAllocString(buf
);
1510 return *ret
? S_OK
: E_OUTOFMEMORY
;
1513 static dispex_data_t
*ensure_dispex_info(dispex_static_data_t
*desc
, compat_mode_t compat_mode
)
1515 if(!desc
->info_cache
[compat_mode
]) {
1516 EnterCriticalSection(&cs_dispex_static_data
);
1517 if(!desc
->info_cache
[compat_mode
])
1518 desc
->info_cache
[compat_mode
] = preprocess_dispex_data(desc
, compat_mode
);
1519 LeaveCriticalSection(&cs_dispex_static_data
);
1521 return desc
->info_cache
[compat_mode
];
1524 static BOOL
ensure_real_info(DispatchEx
*dispex
)
1526 if(dispex
->info
!= dispex
->info
->desc
->delayed_init_info
)
1529 dispex
->info
= ensure_dispex_info(dispex
->info
->desc
, dispex_compat_mode(dispex
));
1530 return dispex
->info
!= NULL
;
1533 static inline DispatchEx
*impl_from_IDispatchEx(IDispatchEx
*iface
)
1535 return CONTAINING_RECORD(iface
, DispatchEx
, IDispatchEx_iface
);
1538 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
1540 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1542 TRACE("%s (%p)->(%s %p)\n", This
->info
->desc
->name
, This
, debugstr_mshtml_guid(riid
), ppv
);
1544 if(This
->info
->desc
->vtbl
->query_interface
) {
1545 *ppv
= This
->info
->desc
->vtbl
->query_interface(This
, riid
);
1550 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IDispatch
, riid
) || IsEqualGUID(&IID_IDispatchEx
, riid
))
1551 *ppv
= &This
->IDispatchEx_iface
;
1552 else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant
, riid
)) {
1555 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports
, riid
)) {
1556 *ppv
= &This
->IDispatchEx_iface
;
1558 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
) ||
1559 IsEqualGUID(&IID_UndocumentedScriptIface
, riid
) ||
1560 IsEqualGUID(&IID_IMarshal
, riid
) ||
1561 IsEqualGUID(&IID_IManagedObject
, riid
)) {
1563 return E_NOINTERFACE
;
1566 WARN("%s (%p)->(%s %p)\n", This
->info
->desc
->name
, This
, debugstr_mshtml_guid(riid
), ppv
);
1567 return E_NOINTERFACE
;
1571 IDispatchEx_AddRef(&This
->IDispatchEx_iface
);
1575 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
1577 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1578 LONG ref
= ccref_incr(&This
->ccref
, (nsISupports
*)&This
->IDispatchEx_iface
);
1580 TRACE("%s (%p) ref=%ld\n", This
->info
->desc
->name
, This
, ref
);
1585 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
1587 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1588 LONG ref
= ccref_decr(&This
->ccref
, (nsISupports
*)&This
->IDispatchEx_iface
, &dispex_ccp
);
1590 TRACE("%s (%p) ref=%ld\n", This
->info
->desc
->name
, This
, ref
);
1592 /* Gecko ccref may not free the object immediately when ref count reaches 0, so we need
1593 * an extra care for objects that need an immediate clean up. See Gecko's
1594 * NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE_WITH_LAST_RELEASE for details. */
1595 if(!ref
&& This
->info
->desc
->vtbl
->last_release
) {
1596 ccref_incr(&This
->ccref
, (nsISupports
*)&This
->IDispatchEx_iface
);
1597 This
->info
->desc
->vtbl
->last_release(This
);
1598 ccref_decr(&This
->ccref
, (nsISupports
*)&This
->IDispatchEx_iface
, &dispex_ccp
);
1604 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
1606 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1608 TRACE("%s (%p)->(%p)\n", This
->info
->desc
->name
, This
, pctinfo
);
1614 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
1615 LCID lcid
, ITypeInfo
**ppTInfo
)
1617 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1620 TRACE("%s (%p)->(%u %lu %p)\n", This
->info
->desc
->name
, This
, iTInfo
, lcid
, ppTInfo
);
1622 hres
= get_typeinfo(This
->info
->desc
->disp_tid
, ppTInfo
);
1626 ITypeInfo_AddRef(*ppTInfo
);
1630 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
1631 LPOLESTR
*rgszNames
, UINT cNames
,
1632 LCID lcid
, DISPID
*rgDispId
)
1634 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1635 HRESULT hres
= S_OK
;
1637 TRACE("%s (%p)->(%s %p %u %lu %p)\n", This
->info
->desc
->name
, This
, debugstr_guid(riid
), rgszNames
,
1638 cNames
, lcid
, rgDispId
);
1640 /* Native ignores all cNames > 1, and doesn't even fill them */
1642 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[0], 0, rgDispId
);
1647 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
1648 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
1649 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1651 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1653 TRACE("%s (%p)->(%ld %s %ld %d %p %p %p %p)\n", This
->info
->desc
->name
, This
, dispIdMember
,
1654 debugstr_guid(riid
), lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
1656 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
, pDispParams
,
1657 pVarResult
, pExcepInfo
, NULL
);
1660 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
1662 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1663 dynamic_prop_t
*dprop
;
1666 TRACE("%s (%p)->(%s %lx %p)\n", This
->info
->desc
->name
, This
, debugstr_w(bstrName
), grfdex
, pid
);
1668 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameCaseInsensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
1669 FIXME("Unsupported grfdex %lx\n", grfdex
);
1671 if(!ensure_real_info(This
))
1672 return E_OUTOFMEMORY
;
1674 hres
= get_builtin_id(This
, bstrName
, grfdex
, pid
);
1675 if(hres
!= DISP_E_UNKNOWNNAME
)
1678 hres
= get_dynamic_prop(This
, bstrName
, grfdex
, &dprop
);
1682 *pid
= DISPID_DYNPROP_0
+ (dprop
- This
->dynamic_data
->props
);
1686 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
1687 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
1689 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1692 TRACE("%s (%p)->(%lx %lx %x %p %p %p %p)\n", This
->info
->desc
->name
, This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1694 if(!ensure_real_info(This
))
1695 return E_OUTOFMEMORY
;
1697 if(wFlags
== (DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
))
1698 wFlags
= DISPATCH_PROPERTYPUT
;
1700 switch(get_dispid_type(id
)) {
1701 case DISPEXPROP_CUSTOM
:
1702 if(!This
->info
->desc
->vtbl
->invoke
)
1703 return DISP_E_MEMBERNOTFOUND
;
1704 return This
->info
->desc
->vtbl
->invoke(This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1706 case DISPEXPROP_DYNAMIC
: {
1707 DWORD idx
= id
- DISPID_DYNPROP_0
;
1708 dynamic_prop_t
*prop
;
1710 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1711 return DISP_E_MEMBERNOTFOUND
;
1713 prop
= This
->dynamic_data
->props
+idx
;
1716 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
1718 return E_INVALIDARG
;
1720 case DISPATCH_METHOD
:
1721 if(V_VT(&prop
->var
) != VT_DISPATCH
) {
1722 FIXME("invoke %s\n", debugstr_variant(&prop
->var
));
1726 return invoke_disp_value(This
, V_DISPATCH(&prop
->var
), lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1727 case DISPATCH_PROPERTYGET
:
1728 if(prop
->flags
& DYNPROP_DELETED
)
1729 return DISP_E_MEMBERNOTFOUND
;
1730 V_VT(pvarRes
) = VT_EMPTY
;
1731 return variant_copy(pvarRes
, &prop
->var
);
1732 case DISPATCH_PROPERTYPUT
:
1733 if(pdp
->cArgs
!= 1 || (pdp
->cNamedArgs
== 1 && *pdp
->rgdispidNamedArgs
!= DISPID_PROPERTYPUT
)
1734 || pdp
->cNamedArgs
> 1) {
1735 FIXME("invalid args\n");
1736 return E_INVALIDARG
;
1739 TRACE("put %s\n", debugstr_variant(pdp
->rgvarg
));
1740 VariantClear(&prop
->var
);
1741 hres
= variant_copy(&prop
->var
, pdp
->rgvarg
);
1745 prop
->flags
&= ~DYNPROP_DELETED
;
1748 FIXME("unhandled wFlags %x\n", wFlags
);
1752 case DISPEXPROP_BUILTIN
:
1753 if(wFlags
== DISPATCH_CONSTRUCT
) {
1754 if(id
== DISPID_VALUE
) {
1755 if(This
->info
->desc
->vtbl
->value
) {
1756 return This
->info
->desc
->vtbl
->value(This
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1758 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1761 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1765 return invoke_builtin_prop(This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
1772 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR name
, DWORD grfdex
)
1774 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1778 TRACE("%s (%p)->(%s %lx)\n", This
->info
->desc
->name
, This
, debugstr_w(name
), grfdex
);
1780 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, name
, grfdex
& ~fdexNameEnsure
, &id
);
1782 compat_mode_t compat_mode
= dispex_compat_mode(This
);
1783 TRACE("property %s not found\n", debugstr_w(name
));
1784 return compat_mode
< COMPAT_MODE_IE8
? E_NOTIMPL
:
1785 compat_mode
< COMPAT_MODE_IE9
? hres
: S_OK
;
1788 return IDispatchEx_DeleteMemberByDispID(&This
->IDispatchEx_iface
, id
);
1791 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
1793 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1795 TRACE("%s (%p)->(%lx)\n", This
->info
->desc
->name
, This
, id
);
1797 if(is_custom_dispid(id
) && This
->info
->desc
->vtbl
->delete)
1798 return This
->info
->desc
->vtbl
->delete(This
, id
);
1800 if(dispex_compat_mode(This
) < COMPAT_MODE_IE8
) {
1801 /* Not implemented by IE */
1805 if(is_dynamic_dispid(id
)) {
1806 DWORD idx
= id
- DISPID_DYNPROP_0
;
1807 dynamic_prop_t
*prop
;
1809 if(!get_dynamic_data(This
) || idx
>= This
->dynamic_data
->prop_cnt
)
1812 prop
= This
->dynamic_data
->props
+ idx
;
1813 VariantClear(&prop
->var
);
1814 prop
->flags
|= DYNPROP_DELETED
;
1821 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
1823 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1824 FIXME("%s (%p)->(%lx %lx %p)\n", This
->info
->desc
->name
, This
, id
, grfdexFetch
, pgrfdex
);
1828 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
1830 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1834 TRACE("%s (%p)->(%lx %p)\n", This
->info
->desc
->name
, This
, id
, pbstrName
);
1836 if(!ensure_real_info(This
))
1837 return E_OUTOFMEMORY
;
1839 if(is_custom_dispid(id
)) {
1840 if(This
->info
->desc
->vtbl
->get_name
)
1841 return This
->info
->desc
->vtbl
->get_name(This
, id
, pbstrName
);
1842 return DISP_E_MEMBERNOTFOUND
;
1845 if(is_dynamic_dispid(id
)) {
1846 DWORD idx
= id
- DISPID_DYNPROP_0
;
1848 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1849 return DISP_E_MEMBERNOTFOUND
;
1851 *pbstrName
= SysAllocString(This
->dynamic_data
->props
[idx
].name
);
1853 return E_OUTOFMEMORY
;
1858 hres
= get_builtin_func(This
->info
, id
, &func
);
1862 *pbstrName
= SysAllocString(func
->name
);
1864 return E_OUTOFMEMORY
;
1868 static HRESULT
next_dynamic_id(DispatchEx
*dispex
, DWORD idx
, DISPID
*ret_id
)
1870 while(idx
< dispex
->dynamic_data
->prop_cnt
&&
1871 (dispex
->dynamic_data
->props
[idx
].flags
& (DYNPROP_DELETED
| DYNPROP_HIDDEN
)))
1874 if(idx
== dispex
->dynamic_data
->prop_cnt
) {
1875 *ret_id
= DISPID_STARTENUM
;
1879 *ret_id
= DISPID_DYNPROP_0
+idx
;
1883 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
1885 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1889 TRACE("%s (%p)->(%lx %lx %p)\n", This
->info
->desc
->name
, This
, grfdex
, id
, pid
);
1891 if(!ensure_real_info(This
))
1892 return E_OUTOFMEMORY
;
1894 if(is_dynamic_dispid(id
)) {
1895 DWORD idx
= id
- DISPID_DYNPROP_0
;
1897 if(!get_dynamic_data(This
) || This
->dynamic_data
->prop_cnt
<= idx
)
1898 return DISP_E_MEMBERNOTFOUND
;
1900 return next_dynamic_id(This
, idx
+1, pid
);
1903 if(!is_custom_dispid(id
)) {
1904 if(id
== DISPID_STARTENUM
) {
1905 func
= This
->info
->funcs
;
1907 hres
= get_builtin_func(This
->info
, id
, &func
);
1913 while(func
< This
->info
->funcs
+ This
->info
->func_cnt
) {
1914 if(func
->func_disp_idx
== -1) {
1921 id
= DISPID_STARTENUM
;
1924 if(This
->info
->desc
->vtbl
->next_dispid
) {
1925 hres
= This
->info
->desc
->vtbl
->next_dispid(This
, id
, pid
);
1930 if(get_dynamic_data(This
) && This
->dynamic_data
->prop_cnt
)
1931 return next_dynamic_id(This
, 0, pid
);
1933 *pid
= DISPID_STARTENUM
;
1937 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
1939 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
1940 FIXME("%s (%p)->(%p)\n", This
->info
->desc
->name
, This
, ppunk
);
1944 static IDispatchExVtbl DispatchExVtbl
= {
1945 DispatchEx_QueryInterface
,
1948 DispatchEx_GetTypeInfoCount
,
1949 DispatchEx_GetTypeInfo
,
1950 DispatchEx_GetIDsOfNames
,
1952 DispatchEx_GetDispID
,
1953 DispatchEx_InvokeEx
,
1954 DispatchEx_DeleteMemberByName
,
1955 DispatchEx_DeleteMemberByDispID
,
1956 DispatchEx_GetMemberProperties
,
1957 DispatchEx_GetMemberName
,
1958 DispatchEx_GetNextDispID
,
1959 DispatchEx_GetNameSpaceParent
1962 static nsresult NSAPI
dispex_traverse(void *ccp
, void *p
, nsCycleCollectionTraversalCallback
*cb
)
1964 DispatchEx
*This
= impl_from_IDispatchEx(p
);
1965 dynamic_prop_t
*prop
;
1967 describe_cc_node(&This
->ccref
, This
->info
->desc
->name
, cb
);
1969 if(This
->info
->desc
->vtbl
->traverse
)
1970 This
->info
->desc
->vtbl
->traverse(This
, cb
);
1972 if(!This
->dynamic_data
)
1975 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++)
1976 traverse_variant(&prop
->var
, "dispex_data", cb
);
1978 if(This
->dynamic_data
->func_disps
) {
1979 func_obj_entry_t
*iter
= This
->dynamic_data
->func_disps
, *end
= iter
+ This
->info
->func_disp_cnt
;
1981 for(iter
= This
->dynamic_data
->func_disps
; iter
< end
; iter
++) {
1984 note_cc_edge((nsISupports
*)&iter
->func_obj
->dispex
.IDispatchEx_iface
, "func_obj", cb
);
1985 traverse_variant(&iter
->val
, "func_val", cb
);
1992 void dispex_props_unlink(DispatchEx
*This
)
1994 dynamic_prop_t
*prop
;
1996 if(!This
->dynamic_data
)
1999 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
2000 prop
->flags
|= DYNPROP_DELETED
;
2001 unlink_variant(&prop
->var
);
2004 if(This
->dynamic_data
->func_disps
) {
2005 func_obj_entry_t
*iter
= This
->dynamic_data
->func_disps
, *end
= iter
+ This
->info
->func_disp_cnt
;
2007 for(iter
= This
->dynamic_data
->func_disps
; iter
< end
; iter
++) {
2010 iter
->func_obj
->obj
= NULL
;
2011 IDispatchEx_Release(&iter
->func_obj
->dispex
.IDispatchEx_iface
);
2012 VariantClear(&iter
->val
);
2015 free(This
->dynamic_data
->func_disps
);
2016 This
->dynamic_data
->func_disps
= NULL
;
2020 static nsresult NSAPI
dispex_unlink(void *p
)
2022 DispatchEx
*This
= impl_from_IDispatchEx(p
);
2024 if(This
->info
->desc
->vtbl
->unlink
)
2025 This
->info
->desc
->vtbl
->unlink(This
);
2027 dispex_props_unlink(This
);
2031 static void NSAPI
dispex_delete_cycle_collectable(void *p
)
2033 DispatchEx
*This
= impl_from_IDispatchEx(p
);
2034 dynamic_prop_t
*prop
;
2036 if(This
->info
->desc
->vtbl
->unlink
)
2037 This
->info
->desc
->vtbl
->unlink(This
);
2039 if(!This
->dynamic_data
)
2042 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
2043 VariantClear(&prop
->var
);
2047 free(This
->dynamic_data
->props
);
2049 if(This
->dynamic_data
->func_disps
) {
2050 func_obj_entry_t
*iter
;
2052 for(iter
= This
->dynamic_data
->func_disps
; iter
< This
->dynamic_data
->func_disps
+ This
->info
->func_disp_cnt
; iter
++) {
2053 if(iter
->func_obj
) {
2054 iter
->func_obj
->obj
= NULL
;
2055 IDispatchEx_Release(&iter
->func_obj
->dispex
.IDispatchEx_iface
);
2057 VariantClear(&iter
->val
);
2060 free(This
->dynamic_data
->func_disps
);
2063 free(This
->dynamic_data
);
2066 This
->info
->desc
->vtbl
->destructor(This
);
2069 void init_dispex_cc(void)
2071 static const CCObjCallback dispex_ccp_callback
= {
2074 dispex_delete_cycle_collectable
2076 ccp_init(&dispex_ccp
, &dispex_ccp_callback
);
2079 const void *dispex_get_vtbl(DispatchEx
*dispex
)
2081 return dispex
->info
->desc
->vtbl
;
2084 void init_dispatch(DispatchEx
*dispex
, dispex_static_data_t
*data
, compat_mode_t compat_mode
)
2086 assert(compat_mode
< COMPAT_MODE_CNT
);
2088 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
2089 dispex
->dynamic_data
= NULL
;
2090 ccref_init(&dispex
->ccref
, 1);
2092 if(data
->vtbl
->get_compat_mode
) {
2094 if(!data
->delayed_init_info
) {
2095 EnterCriticalSection(&cs_dispex_static_data
);
2096 if(!data
->delayed_init_info
) {
2097 dispex_data_t
*info
= calloc(1, sizeof(*data
->delayed_init_info
));
2100 data
->delayed_init_info
= info
;
2103 LeaveCriticalSection(&cs_dispex_static_data
);
2105 dispex
->info
= data
->delayed_init_info
;
2107 dispex
->info
= ensure_dispex_info(data
, compat_mode
);