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;
83 func_disp_t *func_obj;
87 struct dispex_dynamic_data_t {
90 dynamic_prop_t *props;
91 func_obj_entry_t *func_disps;
94 #define DISPID_DYNPROP_0 0x50000000
95 #define DISPID_DYNPROP_MAX 0x5fffffff
97 #define FDEX_VERSION_MASK 0xf0000000
99 static ITypeLib *typelib;
100 static ITypeInfo *typeinfos[LAST_tid];
101 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
103 static REFIID tid_ids[] = {
104 #define XIID(iface) &IID_ ## iface,
105 #define XDIID(iface) &DIID_ ## iface,
111 static HRESULT load_typelib(void)
116 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
118 ERR("LoadRegTypeLib failed: %08x\n", hres);
122 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
123 ITypeLib_Release(tl);
127 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
132 hres = load_typelib();
136 if(!typeinfos[tid]) {
139 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
141 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
145 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
146 ITypeInfo_Release(ti);
149 *typeinfo = typeinfos[tid];
153 void release_typelib(void)
158 while(!list_empty(&dispex_data_list)) {
159 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
160 list_remove(&iter->entry);
162 for(i=0; i < iter->func_cnt; i++)
163 SysFreeString(iter->funcs[i].name);
165 heap_free(iter->funcs);
166 heap_free(iter->name_table);
173 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
175 ITypeInfo_Release(typeinfos[i]);
177 ITypeLib_Release(typelib);
178 DeleteCriticalSection(&cs_dispex_static_data);
181 HRESULT get_htmldoc_classinfo(ITypeInfo **typeinfo)
186 hres = load_typelib();
190 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &CLSID_HTMLDocument, typeinfo);
192 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
196 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
201 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid) {
202 info = data->funcs+data->func_cnt-1;
204 if(data->func_cnt == *size)
205 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
207 info = data->funcs+data->func_cnt;
208 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &info->name, NULL, NULL, NULL);
214 info->id = desc->memid;
216 info->func_disp_idx = -1;
217 info->prop_vt = VT_EMPTY;
218 info->put_vtbl_off = 0;
219 info->get_vtbl_off = 0;
222 if(desc->invkind & DISPATCH_METHOD) {
223 info->func_disp_idx = data->func_disp_cnt++;
224 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
225 VARTYPE vt = VT_EMPTY;
227 if(desc->invkind & DISPATCH_PROPERTYGET) {
228 vt = desc->elemdescFunc.tdesc.vt;
229 info->get_vtbl_off = desc->oVft/sizeof(void*);
231 if(desc->invkind & DISPATCH_PROPERTYPUT) {
232 assert(desc->cParams == 1);
233 vt = desc->lprgelemdescParam->tdesc.vt;
234 info->put_vtbl_off = desc->oVft/sizeof(void*);
237 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
242 static int dispid_cmp(const void *p1, const void *p2)
244 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
247 static int func_name_cmp(const void *p1, const void *p2)
249 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
252 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
254 const tid_t *tid = This->data->iface_tids;
261 TRACE("(%p)\n", This);
263 if(This->data->disp_tid) {
264 hres = get_typeinfo(This->data->disp_tid, &dti);
266 ERR("Could not get disp type info: %08x\n", hres);
271 data = heap_alloc(sizeof(dispex_data_t));
273 data->func_disp_cnt = 0;
274 data->funcs = heap_alloc(size*sizeof(func_info_t));
275 list_add_tail(&dispex_data_list, &data->entry);
278 hres = get_typeinfo(*tid, &ti);
284 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
288 add_func_info(data, &size, *tid, funcdesc, dti);
289 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
295 if(!data->func_cnt) {
296 heap_free(data->funcs);
297 data->name_table = NULL;
303 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
304 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
306 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
307 for(i=0; i < data->func_cnt; i++)
308 data->name_table[i] = data->funcs+i;
309 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
314 static int id_cmp(const void *p1, const void *p2)
316 return *(const DISPID*)p1 - *(const DISPID*)p2;
319 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
321 unsigned i, func_cnt;
328 hres = get_typeinfo(tid, &ti);
332 hres = ITypeInfo_GetTypeAttr(ti, &attr);
334 ITypeInfo_Release(ti);
338 func_cnt = attr->cFuncs;
339 ITypeInfo_ReleaseTypeAttr(ti, attr);
341 ids = heap_alloc(func_cnt*sizeof(DISPID));
343 ITypeInfo_Release(ti);
344 return E_OUTOFMEMORY;
347 for(i=0; i < func_cnt; i++) {
348 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
352 ids[i] = funcdesc->memid;
353 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
356 ITypeInfo_Release(ti);
362 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
364 *ret_size = func_cnt;
369 static dispex_data_t *get_dispex_data(DispatchEx *This)
372 return This->data->data;
374 EnterCriticalSection(&cs_dispex_static_data);
376 if(!This->data->data)
377 This->data->data = preprocess_dispex_data(This);
379 LeaveCriticalSection(&cs_dispex_static_data);
381 return This->data->data;
384 static inline BOOL is_custom_dispid(DISPID id)
386 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
389 static inline BOOL is_dynamic_dispid(DISPID id)
391 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
394 dispex_prop_type_t get_dispid_type(DISPID id)
396 if(is_dynamic_dispid(id))
397 return DISPEXPROP_DYNAMIC;
398 if(is_custom_dispid(id))
399 return DISPEXPROP_CUSTOM;
400 return DISPEXPROP_BUILTIN;
403 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
405 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
406 V_VT(dest) = VT_BSTR;
411 return VariantCopy(dest, src);
414 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
416 if(This->dynamic_data)
417 return This->dynamic_data;
419 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
420 if(!This->dynamic_data)
423 if(This->data->vtbl && This->data->vtbl->populate_props)
424 This->data->vtbl->populate_props(This);
426 return This->dynamic_data;
429 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
431 const BOOL alloc = flags & fdexNameEnsure;
432 dispex_dynamic_data_t *data;
433 dynamic_prop_t *prop;
435 data = get_dynamic_data(This);
437 return E_OUTOFMEMORY;
439 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
440 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
441 if(prop->flags & DYNPROP_DELETED) {
443 return DISP_E_UNKNOWNNAME;
444 prop->flags &= ~DYNPROP_DELETED;
452 return DISP_E_UNKNOWNNAME;
454 TRACE("creating dynamic prop %s\n", debugstr_w(name));
456 if(!data->buf_size) {
457 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
459 return E_OUTOFMEMORY;
461 }else if(data->buf_size == data->prop_cnt) {
462 dynamic_prop_t *new_props;
464 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
466 return E_OUTOFMEMORY;
468 data->props = new_props;
469 data->buf_size <<= 1;
472 prop = data->props + data->prop_cnt;
474 prop->name = heap_strdupW(name);
476 return E_OUTOFMEMORY;
478 VariantInit(&prop->var);
485 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
487 dynamic_prop_t *prop;
490 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
498 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
500 dynamic_prop_t *prop;
503 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
507 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
511 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
512 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
514 if(This->data->vtbl && This->data->vtbl->value)
515 return This->data->vtbl->value(This, lcid, flags, params, res, ei, caller);
518 case DISPATCH_PROPERTYGET:
520 V_BSTR(res) = SysAllocString(objectW);
522 return E_OUTOFMEMORY;
525 FIXME("Unimplemented flags %x\n", flags);
532 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
540 hres = get_typeinfo(func->tid, &ti);
542 ERR("Could not get type info: %08x\n", hres);
546 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
548 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
552 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
554 IUnknown_Release(unk);
558 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
560 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
563 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
565 func_disp_t *This = impl_from_IUnknown(iface);
567 if(IsEqualGUID(&IID_IUnknown, riid)) {
568 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
569 *ppv = &This->IUnknown_iface;
570 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
571 return *ppv ? S_OK : E_NOINTERFACE;
574 return E_NOINTERFACE;
577 IUnknown_AddRef((IUnknown*)*ppv);
581 static ULONG WINAPI Function_AddRef(IUnknown *iface)
583 func_disp_t *This = impl_from_IUnknown(iface);
584 LONG ref = InterlockedIncrement(&This->ref);
586 TRACE("(%p) ref=%d\n", This, ref);
591 static ULONG WINAPI Function_Release(IUnknown *iface)
593 func_disp_t *This = impl_from_IUnknown(iface);
594 LONG ref = InterlockedDecrement(&This->ref);
596 TRACE("(%p) ref=%d\n", This, ref);
600 release_dispex(&This->dispex);
607 static const IUnknownVtbl FunctionUnkVtbl = {
608 Function_QueryInterface,
613 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
615 return CONTAINING_RECORD(iface, func_disp_t, dispex);
618 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
619 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
621 func_disp_t *This = impl_from_DispatchEx(dispex);
625 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
629 case DISPATCH_METHOD:
632 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
635 FIXME("Unimplemented flags %x\n", flags);
642 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
649 static const tid_t function_iface_tids[] = {0};
651 static dispex_static_data_t function_dispex = {
652 &function_dispex_vtbl,
658 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
662 ret = heap_alloc_zero(sizeof(func_disp_t));
666 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
667 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
675 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
676 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
678 DISPID named_arg = DISPID_THIS;
679 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
684 FIXME("named args not supported\n");
688 new_dp.rgvarg = heap_alloc((dp->cArgs+1)*sizeof(VARIANTARG));
690 return E_OUTOFMEMORY;
692 new_dp.cArgs = dp->cArgs+1;
693 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
695 V_VT(new_dp.rgvarg) = VT_DISPATCH;
696 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
698 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
700 if(SUCCEEDED(hres)) {
701 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
702 IDispatchEx_Release(dispex);
705 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
708 TRACE("<<< %s\n", debugstr_variant(res));
710 WARN("<<< %08x\n", hres);
712 heap_free(new_dp.rgvarg);
716 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
718 dispex_dynamic_data_t *dynamic_data;
720 dynamic_data = get_dynamic_data(This);
722 return E_OUTOFMEMORY;
724 if(!dynamic_data->func_disps) {
725 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(*dynamic_data->func_disps));
726 if(!dynamic_data->func_disps)
727 return E_OUTOFMEMORY;
730 if(!dynamic_data->func_disps[func->func_disp_idx].func_obj) {
731 func_disp_t *func_obj;
733 func_obj = create_func_disp(This, func);
735 return E_OUTOFMEMORY;
737 dynamic_data->func_disps[func->func_disp_idx].func_obj = func_obj;
739 IDispatchEx_AddRef(&func_obj->dispex.IDispatchEx_iface);
740 dynamic_data->func_disps[func->func_disp_idx].val = (IDispatch*)&func_obj->dispex.IDispatchEx_iface;
743 *ret = dynamic_data->func_disps+func->func_disp_idx;
747 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
753 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
757 case DISPATCH_METHOD:
758 if(This->dynamic_data && This->dynamic_data->func_disps
759 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
760 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
762 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != entry->val) {
764 FIXME("Calling null\n");
768 hres = invoke_disp_value(This, entry->val, 0, flags, dp, res, ei, NULL);
773 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
775 case DISPATCH_PROPERTYGET: {
776 func_obj_entry_t *entry;
778 if(func->id == DISPID_VALUE) {
781 ret = SysAllocString(objectW);
783 return E_OUTOFMEMORY;
790 hres = get_func_obj_entry(This, func, &entry);
794 V_VT(res) = VT_DISPATCH;
795 V_DISPATCH(res) = entry->val;
797 IDispatch_AddRef(V_DISPATCH(res));
801 case DISPATCH_PROPERTYPUT: {
802 func_obj_entry_t *entry;
805 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
806 || dp->cNamedArgs > 1) {
807 FIXME("invalid args\n");
812 /* FIXME: not exactly right */
813 if(V_VT(v) != VT_DISPATCH)
816 hres = get_func_obj_entry(This, func, &entry);
821 IDispatch_Release(entry->val);
822 entry->val = V_DISPATCH(v);
824 IDispatch_AddRef(entry->val);
829 FIXME("Unimplemented flags %x\n", flags);
836 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
841 max = data->func_cnt-1;
846 if(data->funcs[n].id == id) {
847 *ret = data->funcs+n;
851 if(data->funcs[n].id < id)
857 WARN("invalid id %x\n", id);
858 return DISP_E_UNKNOWNNAME;
861 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
866 data = get_dispex_data(This);
871 max = data->func_cnt-1;
876 c = strcmpiW(data->name_table[n]->name, name);
878 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
881 *ret = data->name_table[n]->id;
891 if(This->data->vtbl && This->data->vtbl->get_dispid) {
894 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
895 if(hres != DISP_E_UNKNOWNNAME)
899 return DISP_E_UNKNOWNNAME;
902 /* List all types used by IDispatchEx-based properties */
903 #define BUILTIN_TYPES_SWITCH \
904 CASE_VT(VT_I2, INT16, V_I2); \
905 CASE_VT(VT_I4, INT32, V_I4); \
906 CASE_VT(VT_R4, float, V_R4); \
907 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
908 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL); \
909 CASE_VT(VT_VARIANT, VARIANT, *); \
910 CASE_VT(VT_PTR, void*, V_BYREF); \
911 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
912 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH)
914 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
916 V_VT(dst) = VT_EMPTY;
919 IVariantChangeType *change_type = NULL;
922 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
923 if(SUCCEEDED(hres)) {
924 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
925 IVariantChangeType_Release(change_type);
932 if(V_VT(src) == VT_BSTR) {
934 V_BOOL(dst) = V_BSTR(src) && *V_BSTR(src) ? VARIANT_TRUE : VARIANT_FALSE;
940 return VariantChangeType(dst, src, 0, vt);
943 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
948 if(dp && dp->cArgs) {
949 FIXME("cArgs %d\n", dp->cArgs);
953 assert(func->get_vtbl_off);
955 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
956 if(SUCCEEDED(hres)) {
957 switch(func->prop_vt) {
958 #define CASE_VT(vt,type,access) \
961 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
962 if(SUCCEEDED(hres)) \
966 BUILTIN_TYPES_SWITCH;
969 FIXME("Unhandled vt %d\n", func->prop_vt);
972 IUnknown_Release(iface);
978 if(func->prop_vt != VT_VARIANT)
979 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
983 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
989 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
990 || dp->cNamedArgs > 1) {
991 FIXME("invalid args\n");
995 if(!func->put_vtbl_off) {
996 FIXME("No setter\n");
1001 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
1002 hres = change_type(&tmpv, v, func->prop_vt, caller);
1008 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1009 if(SUCCEEDED(hres)) {
1010 switch(func->prop_vt) {
1011 #define CASE_VT(vt,type,access) \
1013 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1015 BUILTIN_TYPES_SWITCH;
1018 FIXME("Unimplemented vt %d\n", func->prop_vt);
1022 IUnknown_Release(iface);
1030 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1031 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1033 dispex_data_t *data;
1037 data = get_dispex_data(This);
1041 hres = get_builtin_func(data, id, &func);
1042 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
1043 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1047 if(func->func_disp_idx != -1)
1048 return function_invoke(This, func, flags, dp, res, ei);
1051 case DISPATCH_PROPERTYPUT:
1053 V_VT(res) = VT_EMPTY;
1054 hres = builtin_propput(This, func, dp, caller);
1056 case DISPATCH_PROPERTYGET:
1057 hres = builtin_propget(This, func, dp, res);
1060 if(!func->get_vtbl_off) {
1061 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1065 hres = builtin_propget(This, func, NULL, &v);
1069 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD)) {
1070 if(V_VT(&v) != VT_DISPATCH) {
1071 FIXME("Not a function %s\n", debugstr_variant(&v));
1076 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1077 IDispatch_Release(V_DISPATCH(&v));
1081 IDispatch_Release(V_DISPATCH(&v));
1089 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
1091 dynamic_prop_t *prop;
1095 hres = get_builtin_id(This, name, 0, &id);
1097 DISPID named_id = DISPID_PROPERTYPUT;
1099 DISPPARAMS dp = {&var,&named_id,1,1};
1102 V_VT(&var) = VT_EMPTY;
1103 memset(&ei, 0, sizeof(ei));
1104 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
1108 *success = VARIANT_TRUE;
1112 hres = get_dynamic_prop(This, name, 0, &prop);
1114 if(hres != DISP_E_UNKNOWNNAME)
1116 *success = VARIANT_FALSE;
1120 VariantClear(&prop->var);
1121 prop->flags |= DYNPROP_DELETED;
1122 *success = VARIANT_TRUE;
1126 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1128 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1131 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1133 DispatchEx *This = impl_from_IDispatchEx(iface);
1135 return IUnknown_QueryInterface(This->outer, riid, ppv);
1138 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1140 DispatchEx *This = impl_from_IDispatchEx(iface);
1142 return IUnknown_AddRef(This->outer);
1145 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1147 DispatchEx *This = impl_from_IDispatchEx(iface);
1149 return IUnknown_Release(This->outer);
1152 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1154 DispatchEx *This = impl_from_IDispatchEx(iface);
1156 TRACE("(%p)->(%p)\n", This, pctinfo);
1162 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1163 LCID lcid, ITypeInfo **ppTInfo)
1165 DispatchEx *This = impl_from_IDispatchEx(iface);
1168 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1170 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
1174 ITypeInfo_AddRef(*ppTInfo);
1178 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1179 LPOLESTR *rgszNames, UINT cNames,
1180 LCID lcid, DISPID *rgDispId)
1182 DispatchEx *This = impl_from_IDispatchEx(iface);
1186 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1189 for(i=0; i < cNames; i++) {
1190 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1198 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1199 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1200 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1202 DispatchEx *This = impl_from_IDispatchEx(iface);
1204 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1205 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1207 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1208 pVarResult, pExcepInfo, NULL);
1211 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1213 DispatchEx *This = impl_from_IDispatchEx(iface);
1214 dynamic_prop_t *dprop;
1217 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1219 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1220 FIXME("Unsupported grfdex %x\n", grfdex);
1222 hres = get_builtin_id(This, bstrName, grfdex, pid);
1223 if(hres != DISP_E_UNKNOWNNAME)
1226 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1230 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1234 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1235 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1237 DispatchEx *This = impl_from_IDispatchEx(iface);
1240 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1242 switch(get_dispid_type(id)) {
1243 case DISPEXPROP_CUSTOM:
1244 if(!This->data->vtbl || !This->data->vtbl->invoke)
1245 return DISP_E_UNKNOWNNAME;
1246 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1248 case DISPEXPROP_DYNAMIC: {
1249 DWORD idx = id - DISPID_DYNPROP_0;
1250 dynamic_prop_t *prop;
1252 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1253 return DISP_E_UNKNOWNNAME;
1255 prop = This->dynamic_data->props+idx;
1258 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1260 return E_INVALIDARG;
1262 case DISPATCH_METHOD:
1263 if(V_VT(&prop->var) != VT_DISPATCH) {
1264 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1268 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1269 case DISPATCH_PROPERTYGET:
1270 if(prop->flags & DYNPROP_DELETED)
1271 return DISP_E_UNKNOWNNAME;
1272 V_VT(pvarRes) = VT_EMPTY;
1273 return variant_copy(pvarRes, &prop->var);
1274 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1275 case DISPATCH_PROPERTYPUT:
1276 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1277 || pdp->cNamedArgs > 1) {
1278 FIXME("invalid args\n");
1279 return E_INVALIDARG;
1282 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1283 VariantClear(&prop->var);
1284 hres = variant_copy(&prop->var, pdp->rgvarg);
1288 prop->flags &= ~DYNPROP_DELETED;
1291 FIXME("unhandled wFlags %x\n", wFlags);
1295 case DISPEXPROP_BUILTIN:
1296 if(wFlags == DISPATCH_CONSTRUCT) {
1297 if(id == DISPID_VALUE) {
1298 if(This->data->vtbl && This->data->vtbl->value) {
1299 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1301 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1304 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1308 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1315 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1317 DispatchEx *This = impl_from_IDispatchEx(iface);
1319 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1321 /* Not implemented by IE */
1325 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1327 DispatchEx *This = impl_from_IDispatchEx(iface);
1329 TRACE("(%p)->(%x)\n", This, id);
1331 /* Not implemented by IE */
1335 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1337 DispatchEx *This = impl_from_IDispatchEx(iface);
1338 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1342 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1344 DispatchEx *This = impl_from_IDispatchEx(iface);
1345 dispex_data_t *data;
1349 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1351 if(is_dynamic_dispid(id)) {
1352 DWORD idx = id - DISPID_DYNPROP_0;
1354 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1355 return DISP_E_UNKNOWNNAME;
1357 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1359 return E_OUTOFMEMORY;
1364 data = get_dispex_data(This);
1368 hres = get_builtin_func(data, id, &func);
1372 *pbstrName = SysAllocString(func->name);
1374 return E_OUTOFMEMORY;
1378 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1380 DispatchEx *This = impl_from_IDispatchEx(iface);
1381 dispex_data_t *data;
1385 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1387 if(is_dynamic_dispid(id)) {
1388 DWORD idx = id - DISPID_DYNPROP_0;
1390 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1391 return DISP_E_UNKNOWNNAME;
1393 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1395 if(idx == This->dynamic_data->prop_cnt) {
1396 *pid = DISPID_STARTENUM;
1400 *pid = DISPID_DYNPROP_0+idx;
1404 data = get_dispex_data(This);
1408 if(id == DISPID_STARTENUM) {
1411 hres = get_builtin_func(data, id, &func);
1417 while(func < data->funcs+data->func_cnt) {
1418 /* FIXME: Skip hidden properties */
1419 if(func->func_disp_idx == -1) {
1426 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1427 *pid = DISPID_DYNPROP_0;
1431 *pid = DISPID_STARTENUM;
1435 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1437 DispatchEx *This = impl_from_IDispatchEx(iface);
1438 FIXME("(%p)->(%p)\n", This, ppunk);
1442 static IDispatchExVtbl DispatchExVtbl = {
1443 DispatchEx_QueryInterface,
1446 DispatchEx_GetTypeInfoCount,
1447 DispatchEx_GetTypeInfo,
1448 DispatchEx_GetIDsOfNames,
1450 DispatchEx_GetDispID,
1451 DispatchEx_InvokeEx,
1452 DispatchEx_DeleteMemberByName,
1453 DispatchEx_DeleteMemberByDispID,
1454 DispatchEx_GetMemberProperties,
1455 DispatchEx_GetMemberName,
1456 DispatchEx_GetNextDispID,
1457 DispatchEx_GetNameSpaceParent
1460 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1462 static const IID IID_UndocumentedScriptIface =
1463 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1464 static const IID IID_IDispatchJS =
1465 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1467 if(IsEqualGUID(&IID_IDispatch, riid)) {
1468 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1469 *ppv = &This->IDispatchEx_iface;
1470 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1471 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1472 *ppv = &This->IDispatchEx_iface;
1473 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1474 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1476 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1477 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1484 IUnknown_AddRef((IUnknown*)*ppv);
1488 void dispex_traverse(DispatchEx *This, nsCycleCollectionTraversalCallback *cb)
1490 dynamic_prop_t *prop;
1492 if(!This->dynamic_data)
1495 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1496 if(V_VT(&prop->var) == VT_DISPATCH)
1497 note_cc_edge((nsISupports*)V_DISPATCH(&prop->var), "dispex_data", cb);
1500 /* FIXME: Traverse func_disps */
1503 void dispex_unlink(DispatchEx *This)
1505 dynamic_prop_t *prop;
1507 if(!This->dynamic_data)
1510 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1511 if(V_VT(&prop->var) == VT_DISPATCH) {
1512 V_VT(&prop->var) = VT_EMPTY;
1513 IDispatch_Release(V_DISPATCH(&prop->var));
1515 VariantClear(&prop->var);
1520 void release_dispex(DispatchEx *This)
1522 dynamic_prop_t *prop;
1524 if(!This->dynamic_data)
1527 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1528 VariantClear(&prop->var);
1529 heap_free(prop->name);
1532 heap_free(This->dynamic_data->props);
1534 if(This->dynamic_data->func_disps) {
1535 func_obj_entry_t *iter;
1537 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps+This->data->data->func_disp_cnt; iter++) {
1538 if(iter->func_obj) {
1539 iter->func_obj->obj = NULL;
1540 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
1543 IDispatch_Release(iter->val);
1546 heap_free(This->dynamic_data->func_disps);
1549 heap_free(This->dynamic_data);
1552 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1554 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1555 dispex->outer = outer;
1556 dispex->data = data;
1557 dispex->dynamic_data = NULL;