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[] = {
86 #define XIID(iface) &IID_ ## iface,
87 #define XDIID(iface) &DIID_ ## iface,
93 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
100 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
102 ERR("LoadRegTypeLib failed: %08x\n", hres);
106 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
107 ITypeLib_Release(tl);
110 if(!typeinfos[tid]) {
113 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
115 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
119 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
120 ITypeInfo_Release(typeinfo);
123 *typeinfo = typeinfos[tid];
127 void release_typelib(void)
132 while(!list_empty(&dispex_data_list)) {
133 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
134 list_remove(&iter->entry);
136 for(i=0; i < iter->func_cnt; i++)
137 SysFreeString(iter->funcs[i].name);
139 heap_free(iter->funcs);
140 heap_free(iter->name_table);
147 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
149 ITypeInfo_Release(typeinfos[i]);
151 ITypeLib_Release(typelib);
154 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
158 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
161 if(data->func_cnt == *size)
162 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
164 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
168 data->funcs[data->func_cnt].id = desc->memid;
169 data->funcs[data->func_cnt].tid = tid;
170 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
175 static int dispid_cmp(const void *p1, const void *p2)
177 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
180 static int func_name_cmp(const void *p1, const void *p2)
182 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
185 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
187 const tid_t *tid = This->data->iface_tids;
194 TRACE("(%p)\n", This);
196 if(This->data->disp_tid) {
197 hres = get_typeinfo(This->data->disp_tid, &dti);
199 ERR("Could not get disp type info: %08x\n", hres);
204 data = heap_alloc(sizeof(dispex_data_t));
206 data->func_disp_cnt = 0;
207 data->funcs = heap_alloc(size*sizeof(func_info_t));
208 list_add_tail(&dispex_data_list, &data->entry);
211 hres = get_typeinfo(*tid, &ti);
217 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
221 add_func_info(data, &size, *tid, funcdesc, dti);
222 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
228 if(!data->func_cnt) {
229 heap_free(data->funcs);
231 }else if(data->func_cnt != size) {
232 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
235 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
238 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
239 for(i=0; i < data->func_cnt; i++)
240 data->name_table[i] = data->funcs+i;
241 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
243 data->name_table = NULL;
249 static int id_cmp(const void *p1, const void *p2)
251 return *(const DISPID*)p1 - *(const DISPID*)p2;
254 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
256 unsigned i, func_cnt;
263 hres = get_typeinfo(tid, &ti);
267 hres = ITypeInfo_GetTypeAttr(ti, &attr);
269 ITypeInfo_Release(ti);
273 func_cnt = attr->cFuncs;
274 ITypeInfo_ReleaseTypeAttr(ti, attr);
276 ids = heap_alloc(func_cnt*sizeof(DISPID));
278 ITypeInfo_Release(ti);
279 return E_OUTOFMEMORY;
282 for(i=0; i < func_cnt; i++) {
283 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
287 ids[i] = funcdesc->memid;
288 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
291 ITypeInfo_Release(ti);
297 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
299 *ret_size = func_cnt;
304 static CRITICAL_SECTION cs_dispex_static_data;
305 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
307 0, 0, &cs_dispex_static_data,
308 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
309 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
311 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
314 static dispex_data_t *get_dispex_data(DispatchEx *This)
317 return This->data->data;
319 EnterCriticalSection(&cs_dispex_static_data);
321 if(!This->data->data)
322 This->data->data = preprocess_dispex_data(This);
324 LeaveCriticalSection(&cs_dispex_static_data);
326 return This->data->data;
329 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
337 memset(&ei, 0, sizeof(ei));
339 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
340 if(SUCCEEDED(hres)) {
341 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
342 IDispatchEx_Release(dispex);
344 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
345 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
346 dp, &res, &ei, NULL);
353 static inline BOOL is_custom_dispid(DISPID id)
355 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
358 static inline BOOL is_dynamic_dispid(DISPID id)
360 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
363 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
365 return !alloc || This->dynamic_data
367 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
370 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
372 const BOOL alloc = flags & fdexNameEnsure;
373 dispex_dynamic_data_t *data;
374 dynamic_prop_t *prop;
376 data = get_dynamic_data(This, alloc);
379 return E_OUTOFMEMORY;
381 TRACE("not found %s\n", debugstr_w(name));
382 return DISP_E_UNKNOWNNAME;
385 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
386 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
387 if(prop->flags & DYNPROP_DELETED) {
389 return DISP_E_UNKNOWNNAME;
390 prop->flags &= ~DYNPROP_DELETED;
398 return DISP_E_UNKNOWNNAME;
400 TRACE("creating dynamic prop %s\n", debugstr_w(name));
402 if(!data->buf_size) {
403 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
405 return E_OUTOFMEMORY;
407 }else if(data->buf_size == data->prop_cnt) {
408 dynamic_prop_t *new_props;
410 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
412 return E_OUTOFMEMORY;
414 data->props = new_props;
415 data->buf_size <<= 1;
418 prop = data->props + data->prop_cnt;
420 prop->name = heap_strdupW(name);
422 return E_OUTOFMEMORY;
424 VariantInit(&prop->var);
431 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
433 dynamic_prop_t *prop;
436 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
444 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
445 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
447 if(This->data->vtbl && This->data->vtbl->value)
448 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
451 case DISPATCH_PROPERTYGET:
453 V_BSTR(res) = SysAllocString(objectW);
455 return E_OUTOFMEMORY;
458 FIXME("Unimplemented flags %x\n", flags);
465 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
473 hres = get_typeinfo(func->tid, &ti);
475 ERR("Could not get type info: %08x\n", hres);
479 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
481 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
485 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
487 IUnknown_Release(unk);
491 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
493 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
495 func_disp_t *This = FUNCTION_THIS(iface);
497 if(IsEqualGUID(&IID_IUnknown, riid)) {
498 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
499 *ppv = FUNCUNKNOWN(This);
500 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
501 return *ppv ? S_OK : E_NOINTERFACE;
504 return E_NOINTERFACE;
507 IUnknown_AddRef((IUnknown*)*ppv);
511 static ULONG WINAPI Function_AddRef(IUnknown *iface)
513 func_disp_t *This = FUNCTION_THIS(iface);
515 TRACE("(%p)\n", This);
517 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
520 static ULONG WINAPI Function_Release(IUnknown *iface)
522 func_disp_t *This = FUNCTION_THIS(iface);
524 TRACE("(%p)\n", This);
526 return IDispatchEx_Release(DISPATCHEX(This->obj));
529 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
530 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
532 func_disp_t *This = FUNCTION_THIS(iface);
536 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
539 case DISPATCH_METHOD:
540 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
543 FIXME("Unimplemented flags %x\n", flags);
552 static const IUnknownVtbl FunctionUnkVtbl = {
553 Function_QueryInterface,
558 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
564 static const tid_t function_iface_tids[] = {0};
566 static dispex_static_data_t function_dispex = {
567 &function_dispex_vtbl,
573 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
577 ret = heap_alloc_zero(sizeof(func_disp_t));
581 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
582 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
589 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
595 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
598 case DISPATCH_METHOD:
599 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
601 case DISPATCH_PROPERTYGET: {
602 dispex_dynamic_data_t *dynamic_data;
604 if(func->id == DISPID_VALUE) {
607 ret = SysAllocString(objectW);
609 return E_OUTOFMEMORY;
616 dynamic_data = get_dynamic_data(This, TRUE);
618 return E_OUTOFMEMORY;
620 if(!dynamic_data->func_disps) {
621 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
622 if(!dynamic_data->func_disps)
623 return E_OUTOFMEMORY;
626 if(!dynamic_data->func_disps[func->func_disp_idx]) {
627 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
628 if(!dynamic_data->func_disps[func->func_disp_idx])
629 return E_OUTOFMEMORY;
632 V_VT(res) = VT_DISPATCH;
633 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
634 IDispatch_AddRef(V_DISPATCH(res));
639 FIXME("Unimplemented flags %x\n", flags);
640 case DISPATCH_PROPERTYPUT:
647 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
652 max = data->func_cnt-1;
657 if(data->funcs[n].id == id) {
658 *ret = data->funcs+n;
662 if(data->funcs[n].id < id)
668 WARN("invalid id %x\n", id);
669 return DISP_E_UNKNOWNNAME;
672 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
677 data = get_dispex_data(This);
682 max = data->func_cnt-1;
687 c = strcmpiW(data->name_table[n]->name, name);
689 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
692 *ret = data->name_table[n]->id;
702 if(This->data->vtbl && This->data->vtbl->get_dispid) {
705 hres = This->data->vtbl->get_dispid(This->outer, name, grfdex, ret);
706 if(hres != DISP_E_UNKNOWNNAME)
710 return DISP_E_UNKNOWNNAME;
713 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
714 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
720 data = get_dispex_data(This);
724 hres = get_builtin_func(data, id, &func);
725 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
726 return dispex_value(This, lcid, flags, dp, res, ei, caller);
730 if(func->func_disp_idx == -1)
731 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
733 hres = function_invoke(This, func, flags, dp, res, ei);
738 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
740 dynamic_prop_t *prop;
744 hres = get_builtin_id(This, name, 0, &id);
746 DISPID named_id = DISPID_PROPERTYPUT;
748 DISPPARAMS dp = {&var,&named_id,1,1};
751 V_VT(&var) = VT_EMPTY;
752 memset(&ei, 0, sizeof(ei));
753 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
757 *success = VARIANT_TRUE;
761 hres = get_dynamic_prop(This, name, 0, &prop);
763 if(hres != DISP_E_UNKNOWNNAME)
765 *success = VARIANT_FALSE;
769 VariantClear(&prop->var);
770 prop->flags |= DYNPROP_DELETED;
771 *success = VARIANT_TRUE;
775 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
777 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
779 DispatchEx *This = DISPATCHEX_THIS(iface);
781 return IUnknown_QueryInterface(This->outer, riid, ppv);
784 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
786 DispatchEx *This = DISPATCHEX_THIS(iface);
788 return IUnknown_AddRef(This->outer);
791 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
793 DispatchEx *This = DISPATCHEX_THIS(iface);
795 return IUnknown_Release(This->outer);
798 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
800 DispatchEx *This = DISPATCHEX_THIS(iface);
802 TRACE("(%p)->(%p)\n", This, pctinfo);
808 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
809 LCID lcid, ITypeInfo **ppTInfo)
811 DispatchEx *This = DISPATCHEX_THIS(iface);
814 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
816 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
820 ITypeInfo_AddRef(*ppTInfo);
824 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
825 LPOLESTR *rgszNames, UINT cNames,
826 LCID lcid, DISPID *rgDispId)
828 DispatchEx *This = DISPATCHEX_THIS(iface);
832 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
835 for(i=0; i < cNames; i++) {
836 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
844 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
845 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
846 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
848 DispatchEx *This = DISPATCHEX_THIS(iface);
850 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
851 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
853 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
854 pDispParams, pVarResult, pExcepInfo, NULL);
857 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
859 DispatchEx *This = DISPATCHEX_THIS(iface);
860 dynamic_prop_t *dprop;
863 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
865 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
866 FIXME("Unsupported grfdex %x\n", grfdex);
868 hres = get_builtin_id(This, bstrName, grfdex, pid);
869 if(hres != DISP_E_UNKNOWNNAME)
872 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
876 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
880 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
881 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
883 DispatchEx *This = DISPATCHEX_THIS(iface);
886 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
888 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
889 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
891 if(wFlags == DISPATCH_CONSTRUCT) {
892 if(id == DISPID_VALUE) {
893 if(This->data->vtbl && This->data->vtbl->value) {
894 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
895 pvarRes, pei, pspCaller);
897 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
900 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
904 if(is_dynamic_dispid(id)) {
905 DWORD idx = id - DISPID_DYNPROP_0;
906 dynamic_prop_t *prop;
908 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
909 return DISP_E_UNKNOWNNAME;
911 prop = This->dynamic_data->props+idx;
914 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
917 case DISPATCH_METHOD: {
918 DISPID named_arg = DISPID_THIS;
919 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
922 if(V_VT(&prop->var) != VT_DISPATCH) {
923 FIXME("invoke %s\n", debugstr_variant(&prop->var));
927 if(pdp->cNamedArgs) {
928 FIXME("named args not supported\n");
932 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
934 return E_OUTOFMEMORY;
936 dp.cArgs = pdp->cArgs+1;
937 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
939 V_VT(dp.rgvarg) = VT_DISPATCH;
940 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
942 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
943 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
944 if(SUCCEEDED(hres)) {
945 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
946 IDispatchEx_Release(dispex);
949 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
951 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
953 heap_free(dp.rgvarg);
956 case DISPATCH_PROPERTYGET:
957 if(prop->flags & DYNPROP_DELETED)
958 return DISP_E_UNKNOWNNAME;
959 return VariantCopy(pvarRes, &prop->var);
960 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
961 case DISPATCH_PROPERTYPUT:
962 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
963 || pdp->cNamedArgs > 1) {
964 FIXME("invalid args\n");
968 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
969 VariantClear(&prop->var);
970 hres = VariantCopy(&prop->var, pdp->rgvarg);
974 prop->flags &= ~DYNPROP_DELETED;
977 FIXME("unhandled wFlags %x\n", wFlags);
982 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
985 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
987 DispatchEx *This = DISPATCHEX_THIS(iface);
989 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
991 /* Not implemented by IE */
995 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
997 DispatchEx *This = DISPATCHEX_THIS(iface);
998 FIXME("(%p)->(%x)\n", This, id);
1002 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1004 DispatchEx *This = DISPATCHEX_THIS(iface);
1005 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1009 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1011 DispatchEx *This = DISPATCHEX_THIS(iface);
1012 dispex_data_t *data;
1016 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1018 if(is_dynamic_dispid(id)) {
1019 DWORD idx = id - DISPID_DYNPROP_0;
1021 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1022 return DISP_E_UNKNOWNNAME;
1024 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1026 return E_OUTOFMEMORY;
1031 data = get_dispex_data(This);
1035 hres = get_builtin_func(data, id, &func);
1039 *pbstrName = SysAllocString(func->name);
1041 return E_OUTOFMEMORY;
1045 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1047 DispatchEx *This = DISPATCHEX_THIS(iface);
1048 dispex_data_t *data;
1052 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1054 if(is_dynamic_dispid(id)) {
1055 DWORD idx = id - DISPID_DYNPROP_0;
1057 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1058 return DISP_E_UNKNOWNNAME;
1060 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1062 if(idx == This->dynamic_data->prop_cnt) {
1063 *pid = DISPID_STARTENUM;
1067 *pid = DISPID_DYNPROP_0+idx;
1071 data = get_dispex_data(This);
1075 if(id == DISPID_STARTENUM) {
1078 hres = get_builtin_func(data, id, &func);
1084 while(func < data->funcs+data->func_cnt) {
1085 /* FIXME: Skip hidden properties */
1086 if(func->func_disp_idx == -1) {
1093 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1094 *pid = DISPID_DYNPROP_0;
1098 *pid = DISPID_STARTENUM;
1102 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1104 DispatchEx *This = DISPATCHEX_THIS(iface);
1105 FIXME("(%p)->(%p)\n", This, ppunk);
1109 #undef DISPATCHEX_THIS
1111 static IDispatchExVtbl DispatchExVtbl = {
1112 DispatchEx_QueryInterface,
1115 DispatchEx_GetTypeInfoCount,
1116 DispatchEx_GetTypeInfo,
1117 DispatchEx_GetIDsOfNames,
1119 DispatchEx_GetDispID,
1120 DispatchEx_InvokeEx,
1121 DispatchEx_DeleteMemberByName,
1122 DispatchEx_DeleteMemberByDispID,
1123 DispatchEx_GetMemberProperties,
1124 DispatchEx_GetMemberName,
1125 DispatchEx_GetNextDispID,
1126 DispatchEx_GetNameSpaceParent
1129 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1131 static const IID IID_UndocumentedScriptIface =
1132 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1133 static const IID IID_IDispatchJS =
1134 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1136 if(IsEqualGUID(&IID_IDispatch, riid)) {
1137 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1138 *ppv = DISPATCHEX(This);
1139 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1140 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1141 *ppv = DISPATCHEX(This);
1142 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1143 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1145 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1146 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1153 IUnknown_AddRef((IUnknown*)*ppv);
1157 void release_dispex(DispatchEx *This)
1159 dynamic_prop_t *prop;
1161 if(!This->dynamic_data)
1164 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1165 VariantClear(&prop->var);
1166 heap_free(prop->name);
1169 heap_free(This->dynamic_data->props);
1171 if(This->dynamic_data->func_disps) {
1174 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1175 if(This->dynamic_data->func_disps[i]) {
1176 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1177 heap_free(This->dynamic_data->func_disps[i]);
1181 heap_free(This->dynamic_data->func_disps);
1184 heap_free(This->dynamic_data);
1187 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1189 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1190 dispex->outer = outer;
1191 dispex->data = data;
1192 dispex->dynamic_data = NULL;