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
23 #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 };
48 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
63 struct dispex_data_t {
66 func_info_t **name_table;
78 #define DYNPROP_DELETED 0x01
82 IUnknown IUnknown_iface;
89 func_disp_t *func_obj;
93 struct dispex_dynamic_data_t {
96 dynamic_prop_t *props;
97 func_obj_entry_t *func_disps;
100 #define DISPID_DYNPROP_0 0x50000000
101 #define DISPID_DYNPROP_MAX 0x5fffffff
103 #define FDEX_VERSION_MASK 0xf0000000
105 static ITypeLib *typelib;
106 static ITypeInfo *typeinfos[LAST_tid];
107 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
109 static REFIID tid_ids[] = {
110 #define XIID(iface) &IID_ ## iface,
111 #define XDIID(iface) &DIID_ ## iface,
117 static HRESULT load_typelib(void)
122 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
124 ERR("LoadRegTypeLib failed: %08x\n", hres);
128 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
129 ITypeLib_Release(tl);
133 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
138 hres = load_typelib();
142 if(!typeinfos[tid]) {
145 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
147 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
151 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
152 ITypeInfo_Release(ti);
155 *typeinfo = typeinfos[tid];
159 void release_typelib(void)
164 while(!list_empty(&dispex_data_list)) {
165 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
166 list_remove(&iter->entry);
168 for(i=0; i < iter->func_cnt; i++)
169 SysFreeString(iter->funcs[i].name);
171 heap_free(iter->funcs);
172 heap_free(iter->name_table);
179 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
181 ITypeInfo_Release(typeinfos[i]);
183 ITypeLib_Release(typelib);
184 DeleteCriticalSection(&cs_dispex_static_data);
187 HRESULT get_htmldoc_classinfo(ITypeInfo **typeinfo)
192 hres = load_typelib();
196 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &CLSID_HTMLDocument, typeinfo);
198 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
202 /* Not all argument types are supported yet */
203 #define BUILTIN_ARG_TYPES_SWITCH \
204 CASE_VT(VT_I2, INT16, V_I2); \
205 CASE_VT(VT_I4, INT32, V_I4); \
206 CASE_VT(VT_R4, float, V_R4); \
207 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
208 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
210 /* List all types used by IDispatchEx-based properties */
211 #define BUILTIN_TYPES_SWITCH \
212 BUILTIN_ARG_TYPES_SWITCH; \
213 CASE_VT(VT_VARIANT, VARIANT, *); \
214 CASE_VT(VT_PTR, void*, V_BYREF); \
215 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
216 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH)
218 static BOOL is_arg_type_supported(VARTYPE vt)
221 #define CASE_VT(x,a,b) case x: return TRUE
222 BUILTIN_ARG_TYPES_SWITCH;
228 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
233 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid) {
234 info = data->funcs+data->func_cnt-1;
236 if(data->func_cnt == *size)
237 data->funcs = heap_realloc_zero(data->funcs, (*size <<= 1)*sizeof(func_info_t));
239 info = data->funcs+data->func_cnt;
240 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &info->name, NULL, NULL, NULL);
246 info->id = desc->memid;
248 info->func_disp_idx = -1;
249 info->prop_vt = VT_EMPTY;
252 if(desc->invkind & DISPATCH_METHOD) {
255 info->func_disp_idx = data->func_disp_cnt++;
256 info->argc = desc->cParams;
258 assert(info->argc < MAX_ARGS);
259 assert(desc->funckind == FUNC_DISPATCH);
261 info->arg_types = heap_alloc(sizeof(*info->arg_types) * info->argc);
263 return; /* FIXME: real error instead of fallback */
265 for(i=0; i < info->argc; i++)
266 info->arg_types[i] = desc->lprgelemdescParam[i].tdesc.vt;
268 info->prop_vt = desc->elemdescFunc.tdesc.vt;
269 if(info->prop_vt != VT_VOID && !is_arg_type_supported(info->prop_vt)) {
270 TRACE("%s: return type %d\n", debugstr_w(info->name), info->arg_types[i]);
271 return; /* Fallback to ITypeInfo::Invoke */
274 if(desc->cParamsOpt) {
275 TRACE("%s: optional params\n", debugstr_w(info->name));
276 return; /* Fallback to ITypeInfo::Invoke */
279 for(i=0; i < info->argc; i++) {
280 if(!is_arg_type_supported(info->arg_types[i])) {
281 return; /* Fallback to ITypeInfo for unsupported arg types */
284 if(desc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
285 TRACE("%s param %d: default value\n", debugstr_w(info->name), i);
286 return; /* Fallback to ITypeInfo::Invoke */
290 assert(info->argc <= MAX_ARGS);
291 assert(desc->callconv == CC_STDCALL);
293 info->call_vtbl_off = desc->oVft/sizeof(void*);
294 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
295 VARTYPE vt = VT_EMPTY;
297 if(desc->invkind & DISPATCH_PROPERTYGET) {
298 vt = desc->elemdescFunc.tdesc.vt;
299 info->get_vtbl_off = desc->oVft/sizeof(void*);
301 if(desc->invkind & DISPATCH_PROPERTYPUT) {
302 assert(desc->cParams == 1);
303 vt = desc->lprgelemdescParam->tdesc.vt;
304 info->put_vtbl_off = desc->oVft/sizeof(void*);
307 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
312 static int dispid_cmp(const void *p1, const void *p2)
314 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
317 static int func_name_cmp(const void *p1, const void *p2)
319 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
322 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
324 const tid_t *tid = This->data->iface_tids;
331 TRACE("(%p)\n", This);
333 if(This->data->disp_tid) {
334 hres = get_typeinfo(This->data->disp_tid, &dti);
336 ERR("Could not get disp type info: %08x\n", hres);
341 data = heap_alloc(sizeof(dispex_data_t));
343 data->func_disp_cnt = 0;
344 data->funcs = heap_alloc_zero(size*sizeof(func_info_t));
345 list_add_tail(&dispex_data_list, &data->entry);
348 hres = get_typeinfo(*tid, &ti);
354 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
358 add_func_info(data, &size, *tid, funcdesc, dti);
359 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
365 if(!data->func_cnt) {
366 heap_free(data->funcs);
367 data->name_table = NULL;
373 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
374 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
376 for(i = 1; i < data->func_cnt && data->funcs[i-1].id != data->funcs[i].id; i++);
377 if(i < data->func_cnt) {
380 /* We have at least one duplicated property. This may happen if more than one
381 * interface implements the same property. We have to remove these duplicated
384 while(j < data->func_cnt) {
385 while(j+1 < data->func_cnt && data->funcs[j+1].id == data->funcs[j].id)
387 data->funcs[i++] = data->funcs[j++];
392 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
393 for(i=0; i < data->func_cnt; i++)
394 data->name_table[i] = data->funcs+i;
395 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
400 static int id_cmp(const void *p1, const void *p2)
402 return *(const DISPID*)p1 - *(const DISPID*)p2;
405 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
407 unsigned i, func_cnt;
414 hres = get_typeinfo(tid, &ti);
418 hres = ITypeInfo_GetTypeAttr(ti, &attr);
420 ITypeInfo_Release(ti);
424 func_cnt = attr->cFuncs;
425 ITypeInfo_ReleaseTypeAttr(ti, attr);
427 ids = heap_alloc(func_cnt*sizeof(DISPID));
429 ITypeInfo_Release(ti);
430 return E_OUTOFMEMORY;
433 for(i=0; i < func_cnt; i++) {
434 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
438 ids[i] = funcdesc->memid;
439 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
442 ITypeInfo_Release(ti);
448 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
450 *ret_size = func_cnt;
455 static dispex_data_t *get_dispex_data(DispatchEx *This)
458 return This->data->data;
460 EnterCriticalSection(&cs_dispex_static_data);
462 if(!This->data->data)
463 This->data->data = preprocess_dispex_data(This);
465 LeaveCriticalSection(&cs_dispex_static_data);
467 return This->data->data;
470 static inline BOOL is_custom_dispid(DISPID id)
472 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
475 static inline BOOL is_dynamic_dispid(DISPID id)
477 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
480 dispex_prop_type_t get_dispid_type(DISPID id)
482 if(is_dynamic_dispid(id))
483 return DISPEXPROP_DYNAMIC;
484 if(is_custom_dispid(id))
485 return DISPEXPROP_CUSTOM;
486 return DISPEXPROP_BUILTIN;
489 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
491 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
492 V_VT(dest) = VT_BSTR;
497 return VariantCopy(dest, src);
500 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
502 if(This->dynamic_data)
503 return This->dynamic_data;
505 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
506 if(!This->dynamic_data)
509 if(This->data->vtbl && This->data->vtbl->populate_props)
510 This->data->vtbl->populate_props(This);
512 return This->dynamic_data;
515 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
517 const BOOL alloc = flags & fdexNameEnsure;
518 dispex_dynamic_data_t *data;
519 dynamic_prop_t *prop;
521 data = get_dynamic_data(This);
523 return E_OUTOFMEMORY;
525 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
526 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
527 if(prop->flags & DYNPROP_DELETED) {
529 return DISP_E_UNKNOWNNAME;
530 prop->flags &= ~DYNPROP_DELETED;
538 return DISP_E_UNKNOWNNAME;
540 TRACE("creating dynamic prop %s\n", debugstr_w(name));
542 if(!data->buf_size) {
543 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
545 return E_OUTOFMEMORY;
547 }else if(data->buf_size == data->prop_cnt) {
548 dynamic_prop_t *new_props;
550 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
552 return E_OUTOFMEMORY;
554 data->props = new_props;
555 data->buf_size <<= 1;
558 prop = data->props + data->prop_cnt;
560 prop->name = heap_strdupW(name);
562 return E_OUTOFMEMORY;
564 VariantInit(&prop->var);
571 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
573 dynamic_prop_t *prop;
576 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
584 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
586 dynamic_prop_t *prop;
589 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
593 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
597 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
598 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
600 if(This->data->vtbl && This->data->vtbl->value)
601 return This->data->vtbl->value(This, lcid, flags, params, res, ei, caller);
604 case DISPATCH_PROPERTYGET:
606 V_BSTR(res) = SysAllocString(objectW);
608 return E_OUTOFMEMORY;
611 FIXME("Unimplemented flags %x\n", flags);
618 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
626 hres = get_typeinfo(func->tid, &ti);
628 ERR("Could not get type info: %08x\n", hres);
632 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
634 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
638 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
640 IUnknown_Release(unk);
644 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
646 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
649 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
651 func_disp_t *This = impl_from_IUnknown(iface);
653 if(IsEqualGUID(&IID_IUnknown, riid)) {
654 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
655 *ppv = &This->IUnknown_iface;
656 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
657 return *ppv ? S_OK : E_NOINTERFACE;
660 return E_NOINTERFACE;
663 IUnknown_AddRef((IUnknown*)*ppv);
667 static ULONG WINAPI Function_AddRef(IUnknown *iface)
669 func_disp_t *This = impl_from_IUnknown(iface);
670 LONG ref = InterlockedIncrement(&This->ref);
672 TRACE("(%p) ref=%d\n", This, ref);
677 static ULONG WINAPI Function_Release(IUnknown *iface)
679 func_disp_t *This = impl_from_IUnknown(iface);
680 LONG ref = InterlockedDecrement(&This->ref);
682 TRACE("(%p) ref=%d\n", This, ref);
686 release_dispex(&This->dispex);
693 static const IUnknownVtbl FunctionUnkVtbl = {
694 Function_QueryInterface,
699 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
701 return CONTAINING_RECORD(iface, func_disp_t, dispex);
704 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
705 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
707 func_disp_t *This = impl_from_DispatchEx(dispex);
711 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
715 case DISPATCH_METHOD:
718 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
721 FIXME("Unimplemented flags %x\n", flags);
728 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
735 static const tid_t function_iface_tids[] = {0};
737 static dispex_static_data_t function_dispex = {
738 &function_dispex_vtbl,
744 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
748 ret = heap_alloc_zero(sizeof(func_disp_t));
752 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
753 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
761 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
762 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
764 DISPID named_arg = DISPID_THIS;
765 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
770 FIXME("named args not supported\n");
774 new_dp.rgvarg = heap_alloc((dp->cArgs+1)*sizeof(VARIANTARG));
776 return E_OUTOFMEMORY;
778 new_dp.cArgs = dp->cArgs+1;
779 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
781 V_VT(new_dp.rgvarg) = VT_DISPATCH;
782 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
784 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
786 if(SUCCEEDED(hres)) {
787 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
788 IDispatchEx_Release(dispex);
791 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
794 TRACE("<<< %s\n", debugstr_variant(res));
796 WARN("<<< %08x\n", hres);
798 heap_free(new_dp.rgvarg);
802 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
804 dispex_dynamic_data_t *dynamic_data;
806 dynamic_data = get_dynamic_data(This);
808 return E_OUTOFMEMORY;
810 if(!dynamic_data->func_disps) {
811 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(*dynamic_data->func_disps));
812 if(!dynamic_data->func_disps)
813 return E_OUTOFMEMORY;
816 if(!dynamic_data->func_disps[func->func_disp_idx].func_obj) {
817 func_disp_t *func_obj;
819 func_obj = create_func_disp(This, func);
821 return E_OUTOFMEMORY;
823 dynamic_data->func_disps[func->func_disp_idx].func_obj = func_obj;
825 IDispatchEx_AddRef(&func_obj->dispex.IDispatchEx_iface);
826 dynamic_data->func_disps[func->func_disp_idx].val = (IDispatch*)&func_obj->dispex.IDispatchEx_iface;
829 *ret = dynamic_data->func_disps+func->func_disp_idx;
833 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
838 max = data->func_cnt-1;
843 if(data->funcs[n].id == id) {
844 *ret = data->funcs+n;
848 if(data->funcs[n].id < id)
854 WARN("invalid id %x\n", id);
855 return DISP_E_UNKNOWNNAME;
858 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
863 data = get_dispex_data(This);
868 max = data->func_cnt-1;
873 c = strcmpiW(data->name_table[n]->name, name);
875 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
878 *ret = data->name_table[n]->id;
888 if(This->data->vtbl && This->data->vtbl->get_dispid) {
891 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
892 if(hres != DISP_E_UNKNOWNNAME)
896 return DISP_E_UNKNOWNNAME;
899 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
901 V_VT(dst) = VT_EMPTY;
904 IVariantChangeType *change_type = NULL;
907 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
908 if(SUCCEEDED(hres)) {
909 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
910 IVariantChangeType_Release(change_type);
917 if(V_VT(src) == VT_BSTR) {
919 V_BOOL(dst) = V_BSTR(src) && *V_BSTR(src) ? VARIANT_TRUE : VARIANT_FALSE;
925 return VariantChangeType(dst, src, 0, vt);
928 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
933 if(dp && dp->cArgs) {
934 FIXME("cArgs %d\n", dp->cArgs);
938 assert(func->get_vtbl_off);
940 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
941 if(SUCCEEDED(hres)) {
942 switch(func->prop_vt) {
943 #define CASE_VT(vt,type,access) \
946 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
947 if(SUCCEEDED(hres)) \
951 BUILTIN_TYPES_SWITCH;
954 FIXME("Unhandled vt %d\n", func->prop_vt);
957 IUnknown_Release(iface);
963 if(func->prop_vt != VT_VARIANT)
964 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
968 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
974 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
975 || dp->cNamedArgs > 1) {
976 FIXME("invalid args\n");
980 if(!func->put_vtbl_off) {
981 FIXME("No setter\n");
986 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
987 hres = change_type(&tmpv, v, func->prop_vt, caller);
993 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
994 if(SUCCEEDED(hres)) {
995 switch(func->prop_vt) {
996 #define CASE_VT(vt,type,access) \
998 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1000 BUILTIN_TYPES_SWITCH;
1003 FIXME("Unimplemented vt %d\n", func->prop_vt);
1007 IUnknown_Release(iface);
1015 static HRESULT invoke_builtin_function(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res, IServiceProvider *caller)
1017 VARIANT arg_buf[MAX_ARGS], *arg_ptrs[MAX_ARGS], *arg, retv, ret_ref, vhres;
1018 unsigned i, nconv = 0;
1022 if(dp->cNamedArgs) {
1023 FIXME("Named arguments not supported\n");
1027 if(dp->cArgs != func->argc) {
1028 FIXME("Invalid argument count (expected %u, got %u)\n", func->argc, dp->cArgs);
1029 return E_INVALIDARG;
1032 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1036 for(i=0; i < func->argc; i++) {
1037 arg = dp->rgvarg+dp->cArgs-i-1;
1038 if(func->arg_types[i] == V_VT(arg)) {
1041 hres = change_type(arg_buf+nconv, arg, func->arg_types[i], caller);
1044 arg_ptrs[i] = arg_buf + nconv++;
1048 if(SUCCEEDED(hres)) {
1049 if(func->prop_vt == VT_VOID) {
1050 V_VT(&retv) = VT_EMPTY;
1052 V_VT(&retv) = func->prop_vt;
1053 arg_ptrs[func->argc] = &ret_ref;
1054 V_VT(&ret_ref) = VT_BYREF|func->prop_vt;
1056 switch(func->prop_vt) {
1057 #define CASE_VT(vt,type,access) \
1059 V_BYREF(&ret_ref) = &access(&retv); \
1061 BUILTIN_TYPES_SWITCH;
1068 V_VT(&vhres) = VT_ERROR;
1069 hres = DispCallFunc(iface, func->call_vtbl_off*sizeof(void*), CC_STDCALL, VT_HRESULT,
1070 func->argc + (func->prop_vt == VT_VOID ? 0 : 1), func->arg_types, arg_ptrs, &vhres);
1074 VariantClear(arg_buf+nconv);
1075 IUnknown_Release(iface);
1078 if(FAILED(V_ERROR(&vhres)))
1079 return V_ERROR(&vhres);
1084 VariantClear(&retv);
1085 return V_ERROR(&vhres);
1088 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
1089 EXCEPINFO *ei, IServiceProvider *caller)
1094 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1096 return E_INVALIDARG;
1098 case DISPATCH_METHOD:
1099 if(This->dynamic_data && This->dynamic_data->func_disps
1100 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1101 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
1103 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != entry->val) {
1105 FIXME("Calling null\n");
1109 hres = invoke_disp_value(This, entry->val, 0, flags, dp, res, ei, NULL);
1114 if(func->call_vtbl_off)
1115 hres = invoke_builtin_function(This, func, dp, res, caller);
1117 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1119 case DISPATCH_PROPERTYGET: {
1120 func_obj_entry_t *entry;
1122 if(func->id == DISPID_VALUE) {
1125 ret = SysAllocString(objectW);
1127 return E_OUTOFMEMORY;
1129 V_VT(res) = VT_BSTR;
1134 hres = get_func_obj_entry(This, func, &entry);
1138 V_VT(res) = VT_DISPATCH;
1139 V_DISPATCH(res) = entry->val;
1141 IDispatch_AddRef(V_DISPATCH(res));
1145 case DISPATCH_PROPERTYPUT: {
1146 func_obj_entry_t *entry;
1149 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1150 || dp->cNamedArgs > 1) {
1151 FIXME("invalid args\n");
1152 return E_INVALIDARG;
1156 /* FIXME: not exactly right */
1157 if(V_VT(v) != VT_DISPATCH)
1160 hres = get_func_obj_entry(This, func, &entry);
1165 IDispatch_Release(entry->val);
1166 entry->val = V_DISPATCH(v);
1168 IDispatch_AddRef(entry->val);
1173 FIXME("Unimplemented flags %x\n", flags);
1180 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1181 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1183 dispex_data_t *data;
1187 data = get_dispex_data(This);
1191 hres = get_builtin_func(data, id, &func);
1192 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
1193 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1197 if(func->func_disp_idx != -1)
1198 return function_invoke(This, func, flags, dp, res, ei, caller);
1201 case DISPATCH_PROPERTYPUT:
1203 V_VT(res) = VT_EMPTY;
1204 hres = builtin_propput(This, func, dp, caller);
1206 case DISPATCH_PROPERTYGET:
1207 hres = builtin_propget(This, func, dp, res);
1210 if(!func->get_vtbl_off) {
1211 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1215 hres = builtin_propget(This, func, NULL, &v);
1219 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD)) {
1220 if(V_VT(&v) != VT_DISPATCH) {
1221 FIXME("Not a function %s\n", debugstr_variant(&v));
1226 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1227 IDispatch_Release(V_DISPATCH(&v));
1231 IDispatch_Release(V_DISPATCH(&v));
1239 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
1241 dynamic_prop_t *prop;
1245 hres = get_builtin_id(This, name, 0, &id);
1247 DISPID named_id = DISPID_PROPERTYPUT;
1249 DISPPARAMS dp = {&var,&named_id,1,1};
1252 V_VT(&var) = VT_EMPTY;
1253 memset(&ei, 0, sizeof(ei));
1254 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
1258 *success = VARIANT_TRUE;
1262 hres = get_dynamic_prop(This, name, 0, &prop);
1264 if(hres != DISP_E_UNKNOWNNAME)
1266 *success = VARIANT_FALSE;
1270 VariantClear(&prop->var);
1271 prop->flags |= DYNPROP_DELETED;
1272 *success = VARIANT_TRUE;
1276 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1278 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1281 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1283 DispatchEx *This = impl_from_IDispatchEx(iface);
1285 return IUnknown_QueryInterface(This->outer, riid, ppv);
1288 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1290 DispatchEx *This = impl_from_IDispatchEx(iface);
1292 return IUnknown_AddRef(This->outer);
1295 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1297 DispatchEx *This = impl_from_IDispatchEx(iface);
1299 return IUnknown_Release(This->outer);
1302 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1304 DispatchEx *This = impl_from_IDispatchEx(iface);
1306 TRACE("(%p)->(%p)\n", This, pctinfo);
1312 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1313 LCID lcid, ITypeInfo **ppTInfo)
1315 DispatchEx *This = impl_from_IDispatchEx(iface);
1318 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1320 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
1324 ITypeInfo_AddRef(*ppTInfo);
1328 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1329 LPOLESTR *rgszNames, UINT cNames,
1330 LCID lcid, DISPID *rgDispId)
1332 DispatchEx *This = impl_from_IDispatchEx(iface);
1336 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1339 for(i=0; i < cNames; i++) {
1340 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1348 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1349 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1350 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1352 DispatchEx *This = impl_from_IDispatchEx(iface);
1354 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1355 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1357 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1358 pVarResult, pExcepInfo, NULL);
1361 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1363 DispatchEx *This = impl_from_IDispatchEx(iface);
1364 dynamic_prop_t *dprop;
1367 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1369 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1370 FIXME("Unsupported grfdex %x\n", grfdex);
1372 hres = get_builtin_id(This, bstrName, grfdex, pid);
1373 if(hres != DISP_E_UNKNOWNNAME)
1376 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1380 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1384 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1385 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1387 DispatchEx *This = impl_from_IDispatchEx(iface);
1390 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1392 switch(get_dispid_type(id)) {
1393 case DISPEXPROP_CUSTOM:
1394 if(!This->data->vtbl || !This->data->vtbl->invoke)
1395 return DISP_E_UNKNOWNNAME;
1396 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1398 case DISPEXPROP_DYNAMIC: {
1399 DWORD idx = id - DISPID_DYNPROP_0;
1400 dynamic_prop_t *prop;
1402 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1403 return DISP_E_UNKNOWNNAME;
1405 prop = This->dynamic_data->props+idx;
1408 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1410 return E_INVALIDARG;
1412 case DISPATCH_METHOD:
1413 if(V_VT(&prop->var) != VT_DISPATCH) {
1414 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1418 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1419 case DISPATCH_PROPERTYGET:
1420 if(prop->flags & DYNPROP_DELETED)
1421 return DISP_E_UNKNOWNNAME;
1422 V_VT(pvarRes) = VT_EMPTY;
1423 return variant_copy(pvarRes, &prop->var);
1424 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1425 case DISPATCH_PROPERTYPUT:
1426 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1427 || pdp->cNamedArgs > 1) {
1428 FIXME("invalid args\n");
1429 return E_INVALIDARG;
1432 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1433 VariantClear(&prop->var);
1434 hres = variant_copy(&prop->var, pdp->rgvarg);
1438 prop->flags &= ~DYNPROP_DELETED;
1441 FIXME("unhandled wFlags %x\n", wFlags);
1445 case DISPEXPROP_BUILTIN:
1446 if(wFlags == DISPATCH_CONSTRUCT) {
1447 if(id == DISPID_VALUE) {
1448 if(This->data->vtbl && This->data->vtbl->value) {
1449 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1451 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1454 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1458 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1465 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1467 DispatchEx *This = impl_from_IDispatchEx(iface);
1469 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1471 /* Not implemented by IE */
1475 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1477 DispatchEx *This = impl_from_IDispatchEx(iface);
1479 TRACE("(%p)->(%x)\n", This, id);
1481 /* Not implemented by IE */
1485 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1487 DispatchEx *This = impl_from_IDispatchEx(iface);
1488 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1492 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1494 DispatchEx *This = impl_from_IDispatchEx(iface);
1495 dispex_data_t *data;
1499 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1501 if(is_dynamic_dispid(id)) {
1502 DWORD idx = id - DISPID_DYNPROP_0;
1504 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1505 return DISP_E_UNKNOWNNAME;
1507 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1509 return E_OUTOFMEMORY;
1514 data = get_dispex_data(This);
1518 hres = get_builtin_func(data, id, &func);
1522 *pbstrName = SysAllocString(func->name);
1524 return E_OUTOFMEMORY;
1528 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1530 DispatchEx *This = impl_from_IDispatchEx(iface);
1531 dispex_data_t *data;
1535 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1537 if(is_dynamic_dispid(id)) {
1538 DWORD idx = id - DISPID_DYNPROP_0;
1540 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1541 return DISP_E_UNKNOWNNAME;
1543 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1545 if(idx == This->dynamic_data->prop_cnt) {
1546 *pid = DISPID_STARTENUM;
1550 *pid = DISPID_DYNPROP_0+idx;
1554 data = get_dispex_data(This);
1558 if(id == DISPID_STARTENUM) {
1561 hres = get_builtin_func(data, id, &func);
1567 while(func < data->funcs+data->func_cnt) {
1568 /* FIXME: Skip hidden properties */
1569 if(func->func_disp_idx == -1) {
1576 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1577 *pid = DISPID_DYNPROP_0;
1581 *pid = DISPID_STARTENUM;
1585 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1587 DispatchEx *This = impl_from_IDispatchEx(iface);
1588 FIXME("(%p)->(%p)\n", This, ppunk);
1592 static IDispatchExVtbl DispatchExVtbl = {
1593 DispatchEx_QueryInterface,
1596 DispatchEx_GetTypeInfoCount,
1597 DispatchEx_GetTypeInfo,
1598 DispatchEx_GetIDsOfNames,
1600 DispatchEx_GetDispID,
1601 DispatchEx_InvokeEx,
1602 DispatchEx_DeleteMemberByName,
1603 DispatchEx_DeleteMemberByDispID,
1604 DispatchEx_GetMemberProperties,
1605 DispatchEx_GetMemberName,
1606 DispatchEx_GetNextDispID,
1607 DispatchEx_GetNameSpaceParent
1610 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1612 static const IID IID_UndocumentedScriptIface =
1613 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1614 static const IID IID_IDispatchJS =
1615 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1617 if(IsEqualGUID(&IID_IDispatch, riid)) {
1618 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1619 *ppv = &This->IDispatchEx_iface;
1620 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1621 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1622 *ppv = &This->IDispatchEx_iface;
1623 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1624 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1626 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1627 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1634 IUnknown_AddRef((IUnknown*)*ppv);
1638 void dispex_traverse(DispatchEx *This, nsCycleCollectionTraversalCallback *cb)
1640 dynamic_prop_t *prop;
1642 if(!This->dynamic_data)
1645 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1646 if(V_VT(&prop->var) == VT_DISPATCH)
1647 note_cc_edge((nsISupports*)V_DISPATCH(&prop->var), "dispex_data", cb);
1650 /* FIXME: Traverse func_disps */
1653 void dispex_unlink(DispatchEx *This)
1655 dynamic_prop_t *prop;
1657 if(!This->dynamic_data)
1660 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1661 if(V_VT(&prop->var) == VT_DISPATCH) {
1662 V_VT(&prop->var) = VT_EMPTY;
1663 IDispatch_Release(V_DISPATCH(&prop->var));
1665 VariantClear(&prop->var);
1670 void release_dispex(DispatchEx *This)
1672 dynamic_prop_t *prop;
1674 if(!This->dynamic_data)
1677 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1678 VariantClear(&prop->var);
1679 heap_free(prop->name);
1682 heap_free(This->dynamic_data->props);
1684 if(This->dynamic_data->func_disps) {
1685 func_obj_entry_t *iter;
1687 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps+This->data->data->func_disp_cnt; iter++) {
1688 if(iter->func_obj) {
1689 iter->func_obj->obj = NULL;
1690 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
1693 IDispatch_Release(iter->val);
1696 heap_free(This->dynamic_data->func_disps);
1699 heap_free(This->dynamic_data);
1702 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1704 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1705 dispex->outer = outer;
1706 dispex->data = data;
1707 dispex->dynamic_data = NULL;