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);
34 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
43 struct dispex_data_t {
46 func_info_t **name_table;
58 #define DYNPROP_DELETED 0x01
62 const IUnknownVtbl *lpIUnknownVtbl;
67 #define FUNCUNKNOWN(x) ((IUnknown*) &(x)->lpIUnknownVtbl)
69 struct dispex_dynamic_data_t {
72 dynamic_prop_t *props;
73 func_disp_t **func_disps;
76 #define DISPID_DYNPROP_0 0x50000000
77 #define DISPID_DYNPROP_MAX 0x5fffffff
79 #define FDEX_VERSION_MASK 0xf0000000
81 static ITypeLib *typelib;
82 static ITypeInfo *typeinfos[LAST_tid];
83 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
85 static REFIID tid_ids[] = {
88 &DIID_DispDOMChildrenCollection,
89 &DIID_DispHTMLAnchorElement,
91 &DIID_DispHTMLCommentElement,
92 &DIID_DispHTMLCurrentStyle,
93 &DIID_DispHTMLDocument,
94 &DIID_DispHTMLDOMTextNode,
95 &DIID_DispHTMLElementCollection,
96 &DIID_DispHTMLFormElement,
97 &DIID_DispHTMLGenericElement,
98 &DIID_DispHTMLFrameElement,
101 &DIID_DispHTMLInputElement,
102 &DIID_DispHTMLLocation,
103 &DIID_DispHTMLNavigator,
104 &DIID_DispHTMLOptionElement,
105 &DIID_DispHTMLScreen,
106 &DIID_DispHTMLScriptElement,
107 &DIID_DispHTMLSelectElement,
110 &DIID_DispHTMLTableRow,
111 &DIID_DispHTMLTextAreaElement,
112 &DIID_DispHTMLUnknownElement,
113 &DIID_DispHTMLWindow2,
114 &DIID_HTMLDocumentEvents,
115 &IID_IHTMLAnchorElement,
116 &IID_IHTMLBodyElement,
117 &IID_IHTMLBodyElement2,
118 &IID_IHTMLCommentElement,
119 &IID_IHTMLCurrentStyle,
120 &IID_IHTMLCurrentStyle2,
121 &IID_IHTMLCurrentStyle3,
122 &IID_IHTMLCurrentStyle4,
127 &IID_IHTMLDOMChildrenCollection,
130 &IID_IHTMLDOMTextNode,
135 &IID_IHTMLElementCollection,
137 &IID_IHTMLFiltersCollection,
138 &IID_IHTMLFormElement,
140 &IID_IHTMLFrameBase2,
141 &IID_IHTMLGenericElement,
142 &IID_IHTMLFrameElement3,
143 &IID_IHTMLIFrameElement,
144 &IID_IHTMLImageElementFactory,
145 &IID_IHTMLImgElement,
146 &IID_IHTMLInputElement,
148 &IID_IHTMLOptionElement,
151 &IID_IHTMLScriptElement,
152 &IID_IHTMLSelectElement,
159 &IID_IHTMLTextAreaElement,
160 &IID_IHTMLTextContainer,
161 &IID_IHTMLUniqueName,
168 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
175 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
177 ERR("LoadRegTypeLib failed: %08x\n", hres);
181 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
182 ITypeLib_Release(tl);
185 if(!typeinfos[tid]) {
188 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
190 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
194 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
195 ITypeInfo_Release(typeinfo);
198 *typeinfo = typeinfos[tid];
202 void release_typelib(void)
207 while(!list_empty(&dispex_data_list)) {
208 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
209 list_remove(&iter->entry);
211 for(i=0; i < iter->func_cnt; i++)
212 SysFreeString(iter->funcs[i].name);
214 heap_free(iter->funcs);
215 heap_free(iter->name_table);
222 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
224 ITypeInfo_Release(typeinfos[i]);
226 ITypeLib_Release(typelib);
229 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)
236 if(data->func_cnt == *size)
237 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
239 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
243 data->funcs[data->func_cnt].id = desc->memid;
244 data->funcs[data->func_cnt].tid = tid;
245 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
250 static int dispid_cmp(const void *p1, const void *p2)
252 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
255 static int func_name_cmp(const void *p1, const void *p2)
257 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
260 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
262 const tid_t *tid = This->data->iface_tids;
269 TRACE("(%p)\n", This);
271 if(This->data->disp_tid) {
272 hres = get_typeinfo(This->data->disp_tid, &dti);
274 ERR("Could not get disp type info: %08x\n", hres);
279 data = heap_alloc(sizeof(dispex_data_t));
281 data->func_disp_cnt = 0;
282 data->funcs = heap_alloc(size*sizeof(func_info_t));
283 list_add_tail(&dispex_data_list, &data->entry);
286 hres = get_typeinfo(*tid, &ti);
292 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
296 add_func_info(data, &size, *tid, funcdesc, dti);
297 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
303 if(!data->func_cnt) {
304 heap_free(data->funcs);
306 }else if(data->func_cnt != size) {
307 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
310 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
313 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
314 for(i=0; i < data->func_cnt; i++)
315 data->name_table[i] = data->funcs+i;
316 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
318 data->name_table = NULL;
324 static int id_cmp(const void *p1, const void *p2)
326 return *(const DISPID*)p1 - *(const DISPID*)p2;
329 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
331 unsigned i, func_cnt;
338 hres = get_typeinfo(tid, &ti);
342 hres = ITypeInfo_GetTypeAttr(ti, &attr);
344 ITypeInfo_Release(ti);
348 func_cnt = attr->cFuncs;
349 ITypeInfo_ReleaseTypeAttr(ti, attr);
351 ids = heap_alloc(func_cnt*sizeof(DISPID));
353 ITypeInfo_Release(ti);
354 return E_OUTOFMEMORY;
357 for(i=0; i < func_cnt; i++) {
358 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
362 ids[i] = funcdesc->memid;
363 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
366 ITypeInfo_Release(ti);
372 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
374 *ret_size = func_cnt;
379 static CRITICAL_SECTION cs_dispex_static_data;
380 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
382 0, 0, &cs_dispex_static_data,
383 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
384 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
386 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
389 static dispex_data_t *get_dispex_data(DispatchEx *This)
392 return This->data->data;
394 EnterCriticalSection(&cs_dispex_static_data);
396 if(!This->data->data)
397 This->data->data = preprocess_dispex_data(This);
399 LeaveCriticalSection(&cs_dispex_static_data);
401 return This->data->data;
404 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
412 memset(&ei, 0, sizeof(ei));
414 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
415 if(SUCCEEDED(hres)) {
416 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
417 IDispatchEx_Release(dispex);
419 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
420 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
421 dp, &res, &ei, NULL);
428 static inline BOOL is_custom_dispid(DISPID id)
430 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
433 static inline BOOL is_dynamic_dispid(DISPID id)
435 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
438 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
440 return !alloc || This->dynamic_data
442 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
445 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
447 const BOOL alloc = flags & fdexNameEnsure;
448 dispex_dynamic_data_t *data;
449 dynamic_prop_t *prop;
451 data = get_dynamic_data(This, alloc);
454 return E_OUTOFMEMORY;
456 TRACE("not found %s\n", debugstr_w(name));
457 return DISP_E_UNKNOWNNAME;
460 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
461 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
462 if(prop->flags & DYNPROP_DELETED) {
464 return DISP_E_UNKNOWNNAME;
465 prop->flags &= ~DYNPROP_DELETED;
473 return DISP_E_UNKNOWNNAME;
475 TRACE("creating dynamic prop %s\n", debugstr_w(name));
477 if(!data->buf_size) {
478 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
480 return E_OUTOFMEMORY;
482 }else if(data->buf_size == data->prop_cnt) {
483 dynamic_prop_t *new_props;
485 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
487 return E_OUTOFMEMORY;
489 data->props = new_props;
490 data->buf_size <<= 1;
493 prop = data->props + data->prop_cnt;
495 prop->name = heap_strdupW(name);
497 return E_OUTOFMEMORY;
499 VariantInit(&prop->var);
506 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
508 dynamic_prop_t *prop;
511 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
519 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
520 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
522 if(This->data->vtbl && This->data->vtbl->value)
523 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
526 case DISPATCH_PROPERTYGET:
528 V_BSTR(res) = SysAllocString(objectW);
530 return E_OUTOFMEMORY;
533 FIXME("Unimplemented flags %x\n", flags);
540 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
548 hres = get_typeinfo(func->tid, &ti);
550 ERR("Could not get type info: %08x\n", hres);
554 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
556 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
560 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
562 IUnknown_Release(unk);
566 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
568 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
570 func_disp_t *This = FUNCTION_THIS(iface);
572 if(IsEqualGUID(&IID_IUnknown, riid)) {
573 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
574 *ppv = FUNCUNKNOWN(This);
575 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
576 return *ppv ? S_OK : E_NOINTERFACE;
579 return E_NOINTERFACE;
582 IUnknown_AddRef((IUnknown*)*ppv);
586 static ULONG WINAPI Function_AddRef(IUnknown *iface)
588 func_disp_t *This = FUNCTION_THIS(iface);
590 TRACE("(%p)\n", This);
592 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
595 static ULONG WINAPI Function_Release(IUnknown *iface)
597 func_disp_t *This = FUNCTION_THIS(iface);
599 TRACE("(%p)\n", This);
601 return IDispatchEx_Release(DISPATCHEX(This->obj));
604 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
605 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
607 func_disp_t *This = FUNCTION_THIS(iface);
611 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
614 case DISPATCH_METHOD:
615 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
618 FIXME("Unimplemented flags %x\n", flags);
627 static const IUnknownVtbl FunctionUnkVtbl = {
628 Function_QueryInterface,
633 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
639 static const tid_t function_iface_tids[] = {0};
641 static dispex_static_data_t function_dispex = {
642 &function_dispex_vtbl,
648 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
652 ret = heap_alloc_zero(sizeof(func_disp_t));
656 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
657 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
664 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
670 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
673 case DISPATCH_METHOD:
674 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
676 case DISPATCH_PROPERTYGET: {
677 dispex_dynamic_data_t *dynamic_data;
679 if(func->id == DISPID_VALUE) {
682 ret = SysAllocString(objectW);
684 return E_OUTOFMEMORY;
691 dynamic_data = get_dynamic_data(This, TRUE);
693 return E_OUTOFMEMORY;
695 if(!dynamic_data->func_disps) {
696 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
697 if(!dynamic_data->func_disps)
698 return E_OUTOFMEMORY;
701 if(!dynamic_data->func_disps[func->func_disp_idx]) {
702 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
703 if(!dynamic_data->func_disps[func->func_disp_idx])
704 return E_OUTOFMEMORY;
707 V_VT(res) = VT_DISPATCH;
708 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
709 IDispatch_AddRef(V_DISPATCH(res));
714 FIXME("Unimplemented flags %x\n", flags);
715 case DISPATCH_PROPERTYPUT:
722 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
727 max = data->func_cnt-1;
732 if(data->funcs[n].id == id) {
733 *ret = data->funcs+n;
737 if(data->funcs[n].id < id)
743 WARN("invalid id %x\n", id);
744 return DISP_E_UNKNOWNNAME;
747 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
752 data = get_dispex_data(This);
757 max = data->func_cnt-1;
762 c = strcmpiW(data->name_table[n]->name, name);
764 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
767 *ret = data->name_table[n]->id;
777 if(This->data->vtbl && This->data->vtbl->get_dispid) {
780 hres = This->data->vtbl->get_dispid(This->outer, name, grfdex, ret);
781 if(hres != DISP_E_UNKNOWNNAME)
785 return DISP_E_UNKNOWNNAME;
788 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
789 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
795 data = get_dispex_data(This);
799 hres = get_builtin_func(data, id, &func);
800 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
801 return dispex_value(This, lcid, flags, dp, res, ei, caller);
805 if(func->func_disp_idx == -1)
806 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
808 hres = function_invoke(This, func, flags, dp, res, ei);
813 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
815 dynamic_prop_t *prop;
819 hres = get_builtin_id(This, name, 0, &id);
821 DISPID named_id = DISPID_PROPERTYPUT;
823 DISPPARAMS dp = {&var,&named_id,1,1};
826 V_VT(&var) = VT_EMPTY;
827 memset(&ei, 0, sizeof(ei));
828 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
832 *success = VARIANT_TRUE;
836 hres = get_dynamic_prop(This, name, 0, &prop);
838 if(hres != DISP_E_UNKNOWNNAME)
840 *success = VARIANT_FALSE;
844 VariantClear(&prop->var);
845 prop->flags |= DYNPROP_DELETED;
846 *success = VARIANT_TRUE;
850 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
852 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
854 DispatchEx *This = DISPATCHEX_THIS(iface);
856 return IUnknown_QueryInterface(This->outer, riid, ppv);
859 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
861 DispatchEx *This = DISPATCHEX_THIS(iface);
863 return IUnknown_AddRef(This->outer);
866 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
868 DispatchEx *This = DISPATCHEX_THIS(iface);
870 return IUnknown_Release(This->outer);
873 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
875 DispatchEx *This = DISPATCHEX_THIS(iface);
877 TRACE("(%p)->(%p)\n", This, pctinfo);
883 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
884 LCID lcid, ITypeInfo **ppTInfo)
886 DispatchEx *This = DISPATCHEX_THIS(iface);
889 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
891 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
895 ITypeInfo_AddRef(*ppTInfo);
899 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
900 LPOLESTR *rgszNames, UINT cNames,
901 LCID lcid, DISPID *rgDispId)
903 DispatchEx *This = DISPATCHEX_THIS(iface);
907 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
910 for(i=0; i < cNames; i++) {
911 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
919 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
920 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
921 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
923 DispatchEx *This = DISPATCHEX_THIS(iface);
925 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
926 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
928 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
929 pDispParams, pVarResult, pExcepInfo, NULL);
932 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
934 DispatchEx *This = DISPATCHEX_THIS(iface);
935 dynamic_prop_t *dprop;
938 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
940 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
941 FIXME("Unsupported grfdex %x\n", grfdex);
943 hres = get_builtin_id(This, bstrName, grfdex, pid);
944 if(hres != DISP_E_UNKNOWNNAME)
947 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
951 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
955 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
956 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
958 DispatchEx *This = DISPATCHEX_THIS(iface);
961 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
963 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
964 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
966 if(wFlags == DISPATCH_CONSTRUCT) {
967 if(id == DISPID_VALUE) {
968 if(This->data->vtbl && This->data->vtbl->value) {
969 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
970 pvarRes, pei, pspCaller);
972 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
975 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
979 if(is_dynamic_dispid(id)) {
980 DWORD idx = id - DISPID_DYNPROP_0;
981 dynamic_prop_t *prop;
983 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
984 return DISP_E_UNKNOWNNAME;
986 prop = This->dynamic_data->props+idx;
989 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
992 case DISPATCH_METHOD: {
993 DISPID named_arg = DISPID_THIS;
994 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
997 if(V_VT(&prop->var) != VT_DISPATCH) {
998 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1002 if(pdp->cNamedArgs) {
1003 FIXME("named args not supported\n");
1007 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
1009 return E_OUTOFMEMORY;
1011 dp.cArgs = pdp->cArgs+1;
1012 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
1014 V_VT(dp.rgvarg) = VT_DISPATCH;
1015 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
1017 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
1018 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
1019 if(SUCCEEDED(hres)) {
1020 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
1021 IDispatchEx_Release(dispex);
1024 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
1026 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
1028 heap_free(dp.rgvarg);
1031 case DISPATCH_PROPERTYGET:
1032 if(prop->flags & DYNPROP_DELETED)
1033 return DISP_E_UNKNOWNNAME;
1034 return VariantCopy(pvarRes, &prop->var);
1035 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1036 case DISPATCH_PROPERTYPUT:
1037 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1038 || pdp->cNamedArgs > 1) {
1039 FIXME("invalid args\n");
1040 return E_INVALIDARG;
1043 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1044 VariantClear(&prop->var);
1045 hres = VariantCopy(&prop->var, pdp->rgvarg);
1049 prop->flags &= ~DYNPROP_DELETED;
1052 FIXME("unhandled wFlags %x\n", wFlags);
1057 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1060 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1062 DispatchEx *This = DISPATCHEX_THIS(iface);
1064 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1066 /* Not implemented by IE */
1070 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1072 DispatchEx *This = DISPATCHEX_THIS(iface);
1073 FIXME("(%p)->(%x)\n", This, id);
1077 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1079 DispatchEx *This = DISPATCHEX_THIS(iface);
1080 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1084 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1086 DispatchEx *This = DISPATCHEX_THIS(iface);
1087 dispex_data_t *data;
1091 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1093 if(is_dynamic_dispid(id)) {
1094 DWORD idx = id - DISPID_DYNPROP_0;
1096 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1097 return DISP_E_UNKNOWNNAME;
1099 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1101 return E_OUTOFMEMORY;
1106 data = get_dispex_data(This);
1110 hres = get_builtin_func(data, id, &func);
1114 *pbstrName = SysAllocString(func->name);
1116 return E_OUTOFMEMORY;
1120 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1122 DispatchEx *This = DISPATCHEX_THIS(iface);
1123 dispex_data_t *data;
1127 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1129 if(is_dynamic_dispid(id)) {
1130 DWORD idx = id - DISPID_DYNPROP_0;
1132 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1133 return DISP_E_UNKNOWNNAME;
1135 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1137 if(idx == This->dynamic_data->prop_cnt) {
1138 *pid = DISPID_STARTENUM;
1142 *pid = DISPID_DYNPROP_0+idx;
1146 data = get_dispex_data(This);
1150 if(id == DISPID_STARTENUM) {
1153 hres = get_builtin_func(data, id, &func);
1159 while(func < data->funcs+data->func_cnt) {
1160 /* FIXME: Skip hidden properties */
1161 if(func->func_disp_idx == -1) {
1168 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1169 *pid = DISPID_DYNPROP_0;
1173 *pid = DISPID_STARTENUM;
1177 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1179 DispatchEx *This = DISPATCHEX_THIS(iface);
1180 FIXME("(%p)->(%p)\n", This, ppunk);
1184 #undef DISPATCHEX_THIS
1186 static IDispatchExVtbl DispatchExVtbl = {
1187 DispatchEx_QueryInterface,
1190 DispatchEx_GetTypeInfoCount,
1191 DispatchEx_GetTypeInfo,
1192 DispatchEx_GetIDsOfNames,
1194 DispatchEx_GetDispID,
1195 DispatchEx_InvokeEx,
1196 DispatchEx_DeleteMemberByName,
1197 DispatchEx_DeleteMemberByDispID,
1198 DispatchEx_GetMemberProperties,
1199 DispatchEx_GetMemberName,
1200 DispatchEx_GetNextDispID,
1201 DispatchEx_GetNameSpaceParent
1204 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1206 static const IID IID_UndocumentedScriptIface =
1207 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1208 static const IID IID_IDispatchJS =
1209 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1211 if(IsEqualGUID(&IID_IDispatch, riid)) {
1212 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1213 *ppv = DISPATCHEX(This);
1214 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1215 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1216 *ppv = DISPATCHEX(This);
1217 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1218 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1220 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1221 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1228 IUnknown_AddRef((IUnknown*)*ppv);
1232 void release_dispex(DispatchEx *This)
1234 dynamic_prop_t *prop;
1236 if(!This->dynamic_data)
1239 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1240 VariantClear(&prop->var);
1241 heap_free(prop->name);
1244 heap_free(This->dynamic_data->props);
1246 if(This->dynamic_data->func_disps) {
1249 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1250 if(This->dynamic_data->func_disps[i]) {
1251 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1252 heap_free(This->dynamic_data->func_disps[i]);
1256 heap_free(This->dynamic_data->func_disps);
1259 heap_free(This->dynamic_data);
1262 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1264 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1265 dispex->outer = outer;
1266 dispex->data = data;
1267 dispex->dynamic_data = NULL;