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,
150 &IID_IHTMLScriptElement,
151 &IID_IHTMLSelectElement,
158 &IID_IHTMLTextAreaElement,
159 &IID_IHTMLTextContainer,
160 &IID_IHTMLUniqueName,
167 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
174 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
176 ERR("LoadRegTypeLib failed: %08x\n", hres);
180 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
181 ITypeLib_Release(tl);
184 if(!typeinfos[tid]) {
187 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
189 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
193 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
194 ITypeInfo_Release(typeinfo);
197 *typeinfo = typeinfos[tid];
201 void release_typelib(void)
206 while(!list_empty(&dispex_data_list)) {
207 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
208 list_remove(&iter->entry);
210 for(i=0; i < iter->func_cnt; i++)
211 SysFreeString(iter->funcs[i].name);
213 heap_free(iter->funcs);
214 heap_free(iter->name_table);
221 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
223 ITypeInfo_Release(typeinfos[i]);
225 ITypeLib_Release(typelib);
228 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
232 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
235 if(data->func_cnt == *size)
236 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
238 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
242 data->funcs[data->func_cnt].id = desc->memid;
243 data->funcs[data->func_cnt].tid = tid;
244 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
249 static int dispid_cmp(const void *p1, const void *p2)
251 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
254 static int func_name_cmp(const void *p1, const void *p2)
256 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
259 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
261 const tid_t *tid = This->data->iface_tids;
268 TRACE("(%p)\n", This);
270 if(This->data->disp_tid) {
271 hres = get_typeinfo(This->data->disp_tid, &dti);
273 ERR("Could not get disp type info: %08x\n", hres);
278 data = heap_alloc(sizeof(dispex_data_t));
280 data->func_disp_cnt = 0;
281 data->funcs = heap_alloc(size*sizeof(func_info_t));
282 list_add_tail(&dispex_data_list, &data->entry);
285 hres = get_typeinfo(*tid, &ti);
291 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
295 add_func_info(data, &size, *tid, funcdesc, dti);
296 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
302 if(!data->func_cnt) {
303 heap_free(data->funcs);
305 }else if(data->func_cnt != size) {
306 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
309 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
312 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
313 for(i=0; i < data->func_cnt; i++)
314 data->name_table[i] = data->funcs+i;
315 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
317 data->name_table = NULL;
323 static int id_cmp(const void *p1, const void *p2)
325 return *(const DISPID*)p1 - *(const DISPID*)p2;
328 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
330 unsigned i, func_cnt;
337 hres = get_typeinfo(tid, &ti);
341 hres = ITypeInfo_GetTypeAttr(ti, &attr);
343 ITypeInfo_Release(ti);
347 func_cnt = attr->cFuncs;
348 ITypeInfo_ReleaseTypeAttr(ti, attr);
350 ids = heap_alloc(func_cnt*sizeof(DISPID));
352 ITypeInfo_Release(ti);
353 return E_OUTOFMEMORY;
356 for(i=0; i < func_cnt; i++) {
357 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
361 ids[i] = funcdesc->memid;
362 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
365 ITypeInfo_Release(ti);
371 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
373 *ret_size = func_cnt;
378 static CRITICAL_SECTION cs_dispex_static_data;
379 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
381 0, 0, &cs_dispex_static_data,
382 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
383 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
385 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
388 static dispex_data_t *get_dispex_data(DispatchEx *This)
391 return This->data->data;
393 EnterCriticalSection(&cs_dispex_static_data);
395 if(!This->data->data)
396 This->data->data = preprocess_dispex_data(This);
398 LeaveCriticalSection(&cs_dispex_static_data);
400 return This->data->data;
403 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
411 memset(&ei, 0, sizeof(ei));
413 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
414 if(SUCCEEDED(hres)) {
415 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
416 IDispatchEx_Release(dispex);
418 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
419 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
420 dp, &res, &ei, NULL);
427 static inline BOOL is_custom_dispid(DISPID id)
429 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
432 static inline BOOL is_dynamic_dispid(DISPID id)
434 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
437 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
439 return !alloc || This->dynamic_data
441 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
444 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
446 const BOOL alloc = flags & fdexNameEnsure;
447 dispex_dynamic_data_t *data;
448 dynamic_prop_t *prop;
450 data = get_dynamic_data(This, alloc);
453 return E_OUTOFMEMORY;
455 TRACE("not found %s\n", debugstr_w(name));
456 return DISP_E_UNKNOWNNAME;
459 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
460 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
461 if(prop->flags & DYNPROP_DELETED) {
463 return DISP_E_UNKNOWNNAME;
464 prop->flags &= ~DYNPROP_DELETED;
472 return DISP_E_UNKNOWNNAME;
474 TRACE("creating dynamic prop %s\n", debugstr_w(name));
476 if(!data->buf_size) {
477 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
479 return E_OUTOFMEMORY;
481 }else if(data->buf_size == data->prop_cnt) {
482 dynamic_prop_t *new_props;
484 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
486 return E_OUTOFMEMORY;
488 data->props = new_props;
489 data->buf_size <<= 1;
492 prop = data->props + data->prop_cnt;
494 prop->name = heap_strdupW(name);
496 return E_OUTOFMEMORY;
498 VariantInit(&prop->var);
505 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
507 dynamic_prop_t *prop;
510 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
518 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
519 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
521 if(This->data->vtbl && This->data->vtbl->value)
522 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
525 case DISPATCH_PROPERTYGET:
527 V_BSTR(res) = SysAllocString(objectW);
529 return E_OUTOFMEMORY;
532 FIXME("Unimplemented flags %x\n", flags);
539 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
547 hres = get_typeinfo(func->tid, &ti);
549 ERR("Could not get type info: %08x\n", hres);
553 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
555 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
559 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
561 IUnknown_Release(unk);
565 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
567 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
569 func_disp_t *This = FUNCTION_THIS(iface);
571 if(IsEqualGUID(&IID_IUnknown, riid)) {
572 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
573 *ppv = FUNCUNKNOWN(This);
574 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
575 return *ppv ? S_OK : E_NOINTERFACE;
578 return E_NOINTERFACE;
581 IUnknown_AddRef((IUnknown*)*ppv);
585 static ULONG WINAPI Function_AddRef(IUnknown *iface)
587 func_disp_t *This = FUNCTION_THIS(iface);
589 TRACE("(%p)\n", This);
591 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
594 static ULONG WINAPI Function_Release(IUnknown *iface)
596 func_disp_t *This = FUNCTION_THIS(iface);
598 TRACE("(%p)\n", This);
600 return IDispatchEx_Release(DISPATCHEX(This->obj));
603 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
604 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
606 func_disp_t *This = FUNCTION_THIS(iface);
610 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
613 case DISPATCH_METHOD:
614 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
617 FIXME("Unimplemented flags %x\n", flags);
626 static const IUnknownVtbl FunctionUnkVtbl = {
627 Function_QueryInterface,
632 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
638 static const tid_t function_iface_tids[] = {0};
640 static dispex_static_data_t function_dispex = {
641 &function_dispex_vtbl,
647 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
651 ret = heap_alloc_zero(sizeof(func_disp_t));
655 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
656 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
663 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
669 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
672 case DISPATCH_METHOD:
673 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
675 case DISPATCH_PROPERTYGET: {
676 dispex_dynamic_data_t *dynamic_data;
678 if(func->id == DISPID_VALUE) {
681 ret = SysAllocString(objectW);
683 return E_OUTOFMEMORY;
690 dynamic_data = get_dynamic_data(This, TRUE);
692 return E_OUTOFMEMORY;
694 if(!dynamic_data->func_disps) {
695 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
696 if(!dynamic_data->func_disps)
697 return E_OUTOFMEMORY;
700 if(!dynamic_data->func_disps[func->func_disp_idx]) {
701 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
702 if(!dynamic_data->func_disps[func->func_disp_idx])
703 return E_OUTOFMEMORY;
706 V_VT(res) = VT_DISPATCH;
707 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
708 IDispatch_AddRef(V_DISPATCH(res));
713 FIXME("Unimplemented flags %x\n", flags);
714 case DISPATCH_PROPERTYPUT:
721 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
726 max = data->func_cnt-1;
731 if(data->funcs[n].id == id) {
732 *ret = data->funcs+n;
736 if(data->funcs[n].id < id)
742 WARN("invalid id %x\n", id);
743 return DISP_E_UNKNOWNNAME;
746 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
751 data = get_dispex_data(This);
756 max = data->func_cnt-1;
761 c = strcmpiW(data->name_table[n]->name, name);
763 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
766 *ret = data->name_table[n]->id;
776 if(This->data->vtbl && This->data->vtbl->get_dispid) {
779 hres = This->data->vtbl->get_dispid(This->outer, name, grfdex, ret);
780 if(hres != DISP_E_UNKNOWNNAME)
784 return DISP_E_UNKNOWNNAME;
787 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
788 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
794 data = get_dispex_data(This);
798 hres = get_builtin_func(data, id, &func);
799 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
800 return dispex_value(This, lcid, flags, dp, res, ei, caller);
804 if(func->func_disp_idx == -1)
805 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
807 hres = function_invoke(This, func, flags, dp, res, ei);
812 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
814 dynamic_prop_t *prop;
818 hres = get_builtin_id(This, name, 0, &id);
820 DISPID named_id = DISPID_PROPERTYPUT;
822 DISPPARAMS dp = {&var,&named_id,1,1};
825 V_VT(&var) = VT_EMPTY;
826 memset(&ei, 0, sizeof(ei));
827 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
831 *success = VARIANT_TRUE;
835 hres = get_dynamic_prop(This, name, 0, &prop);
837 if(hres != DISP_E_UNKNOWNNAME)
839 *success = VARIANT_FALSE;
843 VariantClear(&prop->var);
844 prop->flags |= DYNPROP_DELETED;
845 *success = VARIANT_TRUE;
849 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
851 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
853 DispatchEx *This = DISPATCHEX_THIS(iface);
855 return IUnknown_QueryInterface(This->outer, riid, ppv);
858 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
860 DispatchEx *This = DISPATCHEX_THIS(iface);
862 return IUnknown_AddRef(This->outer);
865 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
867 DispatchEx *This = DISPATCHEX_THIS(iface);
869 return IUnknown_Release(This->outer);
872 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
874 DispatchEx *This = DISPATCHEX_THIS(iface);
876 TRACE("(%p)->(%p)\n", This, pctinfo);
882 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
883 LCID lcid, ITypeInfo **ppTInfo)
885 DispatchEx *This = DISPATCHEX_THIS(iface);
888 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
890 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
894 ITypeInfo_AddRef(*ppTInfo);
898 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
899 LPOLESTR *rgszNames, UINT cNames,
900 LCID lcid, DISPID *rgDispId)
902 DispatchEx *This = DISPATCHEX_THIS(iface);
906 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
909 for(i=0; i < cNames; i++) {
910 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
918 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
919 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
920 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
922 DispatchEx *This = DISPATCHEX_THIS(iface);
924 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
925 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
927 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
928 pDispParams, pVarResult, pExcepInfo, NULL);
931 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
933 DispatchEx *This = DISPATCHEX_THIS(iface);
934 dynamic_prop_t *dprop;
937 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
939 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
940 FIXME("Unsupported grfdex %x\n", grfdex);
942 hres = get_builtin_id(This, bstrName, grfdex, pid);
943 if(hres != DISP_E_UNKNOWNNAME)
946 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
950 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
954 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
955 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
957 DispatchEx *This = DISPATCHEX_THIS(iface);
960 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
962 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
963 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
965 if(wFlags == DISPATCH_CONSTRUCT) {
966 if(id == DISPID_VALUE) {
967 if(This->data->vtbl && This->data->vtbl->value) {
968 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
969 pvarRes, pei, pspCaller);
971 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
974 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
978 if(is_dynamic_dispid(id)) {
979 DWORD idx = id - DISPID_DYNPROP_0;
980 dynamic_prop_t *prop;
982 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
983 return DISP_E_UNKNOWNNAME;
985 prop = This->dynamic_data->props+idx;
988 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
991 case DISPATCH_METHOD: {
992 DISPID named_arg = DISPID_THIS;
993 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
996 if(V_VT(&prop->var) != VT_DISPATCH) {
997 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1001 if(pdp->cNamedArgs) {
1002 FIXME("named args not supported\n");
1006 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
1008 return E_OUTOFMEMORY;
1010 dp.cArgs = pdp->cArgs+1;
1011 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
1013 V_VT(dp.rgvarg) = VT_DISPATCH;
1014 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
1016 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
1017 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
1018 if(SUCCEEDED(hres)) {
1019 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
1020 IDispatchEx_Release(dispex);
1023 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
1025 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
1027 heap_free(dp.rgvarg);
1030 case DISPATCH_PROPERTYGET:
1031 if(prop->flags & DYNPROP_DELETED)
1032 return DISP_E_UNKNOWNNAME;
1033 return VariantCopy(pvarRes, &prop->var);
1034 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1035 case DISPATCH_PROPERTYPUT:
1036 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1037 || pdp->cNamedArgs > 1) {
1038 FIXME("invalid args\n");
1039 return E_INVALIDARG;
1042 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1043 VariantClear(&prop->var);
1044 hres = VariantCopy(&prop->var, pdp->rgvarg);
1048 prop->flags &= ~DYNPROP_DELETED;
1051 FIXME("unhandled wFlags %x\n", wFlags);
1056 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1059 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1061 DispatchEx *This = DISPATCHEX_THIS(iface);
1063 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1065 /* Not implemented by IE */
1069 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1071 DispatchEx *This = DISPATCHEX_THIS(iface);
1072 FIXME("(%p)->(%x)\n", This, id);
1076 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1078 DispatchEx *This = DISPATCHEX_THIS(iface);
1079 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1083 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1085 DispatchEx *This = DISPATCHEX_THIS(iface);
1086 dispex_data_t *data;
1090 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1092 if(is_dynamic_dispid(id)) {
1093 DWORD idx = id - DISPID_DYNPROP_0;
1095 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1096 return DISP_E_UNKNOWNNAME;
1098 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1100 return E_OUTOFMEMORY;
1105 data = get_dispex_data(This);
1109 hres = get_builtin_func(data, id, &func);
1113 *pbstrName = SysAllocString(func->name);
1115 return E_OUTOFMEMORY;
1119 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1121 DispatchEx *This = DISPATCHEX_THIS(iface);
1122 dispex_data_t *data;
1126 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1128 if(is_dynamic_dispid(id)) {
1129 DWORD idx = id - DISPID_DYNPROP_0;
1131 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1132 return DISP_E_UNKNOWNNAME;
1134 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1136 if(idx == This->dynamic_data->prop_cnt) {
1137 *pid = DISPID_STARTENUM;
1141 *pid = DISPID_DYNPROP_0+idx;
1145 data = get_dispex_data(This);
1149 if(id == DISPID_STARTENUM) {
1152 hres = get_builtin_func(data, id, &func);
1158 while(func < data->funcs+data->func_cnt) {
1159 /* FIXME: Skip hidden properties */
1160 if(func->func_disp_idx == -1) {
1167 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1168 *pid = DISPID_DYNPROP_0;
1172 *pid = DISPID_STARTENUM;
1176 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1178 DispatchEx *This = DISPATCHEX_THIS(iface);
1179 FIXME("(%p)->(%p)\n", This, ppunk);
1183 #undef DISPATCHEX_THIS
1185 static IDispatchExVtbl DispatchExVtbl = {
1186 DispatchEx_QueryInterface,
1189 DispatchEx_GetTypeInfoCount,
1190 DispatchEx_GetTypeInfo,
1191 DispatchEx_GetIDsOfNames,
1193 DispatchEx_GetDispID,
1194 DispatchEx_InvokeEx,
1195 DispatchEx_DeleteMemberByName,
1196 DispatchEx_DeleteMemberByDispID,
1197 DispatchEx_GetMemberProperties,
1198 DispatchEx_GetMemberName,
1199 DispatchEx_GetNextDispID,
1200 DispatchEx_GetNameSpaceParent
1203 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1205 static const IID IID_UndocumentedScriptIface =
1206 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1207 static const IID IID_IDispatchJS =
1208 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1210 if(IsEqualGUID(&IID_IDispatch, riid)) {
1211 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1212 *ppv = DISPATCHEX(This);
1213 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1214 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1215 *ppv = DISPATCHEX(This);
1216 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1217 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1219 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1220 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1227 IUnknown_AddRef((IUnknown*)*ppv);
1231 void release_dispex(DispatchEx *This)
1233 dynamic_prop_t *prop;
1235 if(!This->dynamic_data)
1238 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1239 VariantClear(&prop->var);
1240 heap_free(prop->name);
1243 heap_free(This->dynamic_data->props);
1245 if(This->dynamic_data->func_disps) {
1248 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1249 if(This->dynamic_data->func_disps[i]) {
1250 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1251 heap_free(This->dynamic_data->func_disps[i]);
1255 heap_free(This->dynamic_data->func_disps);
1258 heap_free(This->dynamic_data);
1261 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1263 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1264 dispex->outer = outer;
1265 dispex->data = data;
1266 dispex->dynamic_data = NULL;