2 * Copyright 2008-2009 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 static CRITICAL_SECTION cs_dispex_static_data;
39 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
41 0, 0, &cs_dispex_static_data,
42 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
43 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
45 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
48 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
63 struct dispex_data_t {
66 func_info_t **name_table;
78 #define DYNPROP_DELETED 0x01
82 IUnknown IUnknown_iface;
89 func_disp_t *func_obj;
93 struct dispex_dynamic_data_t {
96 dynamic_prop_t *props;
97 func_obj_entry_t *func_disps;
100 #define DISPID_DYNPROP_0 0x50000000
101 #define DISPID_DYNPROP_MAX 0x5fffffff
103 #define FDEX_VERSION_MASK 0xf0000000
105 static ITypeLib *typelib;
106 static ITypeInfo *typeinfos[LAST_tid];
107 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
109 static REFIID tid_ids[] = {
110 #define XIID(iface) &IID_ ## iface,
111 #define XDIID(iface) &DIID_ ## iface,
117 static HRESULT load_typelib(void)
122 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
124 ERR("LoadRegTypeLib failed: %08x\n", hres);
128 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
129 ITypeLib_Release(tl);
133 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
138 hres = load_typelib();
142 if(!typeinfos[tid]) {
145 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
147 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
151 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
152 ITypeInfo_Release(ti);
155 *typeinfo = typeinfos[tid];
159 void release_typelib(void)
164 while(!list_empty(&dispex_data_list)) {
165 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
166 list_remove(&iter->entry);
168 for(i=0; i < iter->func_cnt; i++)
169 SysFreeString(iter->funcs[i].name);
171 heap_free(iter->funcs);
172 heap_free(iter->name_table);
179 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
181 ITypeInfo_Release(typeinfos[i]);
183 ITypeLib_Release(typelib);
184 DeleteCriticalSection(&cs_dispex_static_data);
187 HRESULT get_htmldoc_classinfo(ITypeInfo **typeinfo)
192 hres = load_typelib();
196 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &CLSID_HTMLDocument, typeinfo);
198 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
202 /* Not all argument types are supported yet */
203 #define BUILTIN_ARG_TYPES_SWITCH \
204 CASE_VT(VT_I2, INT16, V_I2); \
205 CASE_VT(VT_I4, INT32, V_I4); \
206 CASE_VT(VT_R4, float, V_R4); \
207 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
208 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
210 /* List all types used by IDispatchEx-based properties */
211 #define BUILTIN_TYPES_SWITCH \
212 BUILTIN_ARG_TYPES_SWITCH; \
213 CASE_VT(VT_VARIANT, VARIANT, *); \
214 CASE_VT(VT_PTR, void*, V_BYREF); \
215 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
216 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH)
218 static BOOL is_arg_type_supported(VARTYPE vt)
221 #define CASE_VT(x,a,b) case x: return TRUE
222 BUILTIN_ARG_TYPES_SWITCH;
228 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
233 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid) {
234 info = data->funcs+data->func_cnt-1;
236 if(data->func_cnt == *size)
237 data->funcs = heap_realloc_zero(data->funcs, (*size <<= 1)*sizeof(func_info_t));
239 info = data->funcs+data->func_cnt;
240 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &info->name, NULL, NULL, NULL);
246 info->id = desc->memid;
248 info->func_disp_idx = -1;
249 info->prop_vt = VT_EMPTY;
252 if(desc->invkind & DISPATCH_METHOD) {
255 info->func_disp_idx = data->func_disp_cnt++;
256 info->argc = desc->cParams;
258 assert(info->argc < MAX_ARGS);
259 assert(desc->funckind == FUNC_DISPATCH);
261 info->arg_types = heap_alloc(sizeof(*info->arg_types) * info->argc);
263 return; /* FIXME: real error instead of fallback */
265 for(i=0; i < info->argc; i++)
266 info->arg_types[i] = desc->lprgelemdescParam[i].tdesc.vt;
268 info->prop_vt = desc->elemdescFunc.tdesc.vt;
269 if(info->prop_vt != VT_VOID && !is_arg_type_supported(info->prop_vt)) {
270 TRACE("%s: return type %d\n", debugstr_w(info->name), info->arg_types[i]);
271 return; /* Fallback to ITypeInfo::Invoke */
274 if(desc->cParamsOpt) {
275 TRACE("%s: optional params\n", debugstr_w(info->name));
276 return; /* Fallback to ITypeInfo::Invoke */
279 for(i=0; i < info->argc; i++) {
280 if(!is_arg_type_supported(info->arg_types[i])) {
281 return; /* Fallback to ITypeInfo for unsupported arg types */
284 if(desc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
285 TRACE("%s param %d: default value\n", debugstr_w(info->name), i);
286 return; /* Fallback to ITypeInfo::Invoke */
290 assert(info->argc <= MAX_ARGS);
291 assert(desc->callconv == CC_STDCALL);
293 info->call_vtbl_off = desc->oVft/sizeof(void*);
294 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
295 VARTYPE vt = VT_EMPTY;
297 if(desc->invkind & DISPATCH_PROPERTYGET) {
298 vt = desc->elemdescFunc.tdesc.vt;
299 info->get_vtbl_off = desc->oVft/sizeof(void*);
301 if(desc->invkind & DISPATCH_PROPERTYPUT) {
302 assert(desc->cParams == 1);
303 vt = desc->lprgelemdescParam->tdesc.vt;
304 info->put_vtbl_off = desc->oVft/sizeof(void*);
307 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
312 static int dispid_cmp(const void *p1, const void *p2)
314 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
317 static int func_name_cmp(const void *p1, const void *p2)
319 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
322 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
324 const tid_t *tid = This->data->iface_tids;
331 TRACE("(%p)\n", This);
333 if(This->data->disp_tid) {
334 hres = get_typeinfo(This->data->disp_tid, &dti);
336 ERR("Could not get disp type info: %08x\n", hres);
341 data = heap_alloc(sizeof(dispex_data_t));
343 data->func_disp_cnt = 0;
344 data->funcs = heap_alloc_zero(size*sizeof(func_info_t));
345 list_add_tail(&dispex_data_list, &data->entry);
348 hres = get_typeinfo(*tid, &ti);
354 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
358 add_func_info(data, &size, *tid, funcdesc, dti);
359 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
365 if(!data->func_cnt) {
366 heap_free(data->funcs);
367 data->name_table = NULL;
373 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
374 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
376 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
377 for(i=0; i < data->func_cnt; i++)
378 data->name_table[i] = data->funcs+i;
379 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
384 static int id_cmp(const void *p1, const void *p2)
386 return *(const DISPID*)p1 - *(const DISPID*)p2;
389 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
391 unsigned i, func_cnt;
398 hres = get_typeinfo(tid, &ti);
402 hres = ITypeInfo_GetTypeAttr(ti, &attr);
404 ITypeInfo_Release(ti);
408 func_cnt = attr->cFuncs;
409 ITypeInfo_ReleaseTypeAttr(ti, attr);
411 ids = heap_alloc(func_cnt*sizeof(DISPID));
413 ITypeInfo_Release(ti);
414 return E_OUTOFMEMORY;
417 for(i=0; i < func_cnt; i++) {
418 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
422 ids[i] = funcdesc->memid;
423 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
426 ITypeInfo_Release(ti);
432 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
434 *ret_size = func_cnt;
439 static dispex_data_t *get_dispex_data(DispatchEx *This)
442 return This->data->data;
444 EnterCriticalSection(&cs_dispex_static_data);
446 if(!This->data->data)
447 This->data->data = preprocess_dispex_data(This);
449 LeaveCriticalSection(&cs_dispex_static_data);
451 return This->data->data;
454 static inline BOOL is_custom_dispid(DISPID id)
456 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
459 static inline BOOL is_dynamic_dispid(DISPID id)
461 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
464 dispex_prop_type_t get_dispid_type(DISPID id)
466 if(is_dynamic_dispid(id))
467 return DISPEXPROP_DYNAMIC;
468 if(is_custom_dispid(id))
469 return DISPEXPROP_CUSTOM;
470 return DISPEXPROP_BUILTIN;
473 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
475 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
476 V_VT(dest) = VT_BSTR;
481 return VariantCopy(dest, src);
484 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
486 if(This->dynamic_data)
487 return This->dynamic_data;
489 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
490 if(!This->dynamic_data)
493 if(This->data->vtbl && This->data->vtbl->populate_props)
494 This->data->vtbl->populate_props(This);
496 return This->dynamic_data;
499 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
501 const BOOL alloc = flags & fdexNameEnsure;
502 dispex_dynamic_data_t *data;
503 dynamic_prop_t *prop;
505 data = get_dynamic_data(This);
507 return E_OUTOFMEMORY;
509 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
510 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
511 if(prop->flags & DYNPROP_DELETED) {
513 return DISP_E_UNKNOWNNAME;
514 prop->flags &= ~DYNPROP_DELETED;
522 return DISP_E_UNKNOWNNAME;
524 TRACE("creating dynamic prop %s\n", debugstr_w(name));
526 if(!data->buf_size) {
527 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
529 return E_OUTOFMEMORY;
531 }else if(data->buf_size == data->prop_cnt) {
532 dynamic_prop_t *new_props;
534 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
536 return E_OUTOFMEMORY;
538 data->props = new_props;
539 data->buf_size <<= 1;
542 prop = data->props + data->prop_cnt;
544 prop->name = heap_strdupW(name);
546 return E_OUTOFMEMORY;
548 VariantInit(&prop->var);
555 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
557 dynamic_prop_t *prop;
560 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
568 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
570 dynamic_prop_t *prop;
573 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
577 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
581 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
582 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
584 if(This->data->vtbl && This->data->vtbl->value)
585 return This->data->vtbl->value(This, lcid, flags, params, res, ei, caller);
588 case DISPATCH_PROPERTYGET:
590 V_BSTR(res) = SysAllocString(objectW);
592 return E_OUTOFMEMORY;
595 FIXME("Unimplemented flags %x\n", flags);
602 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
610 hres = get_typeinfo(func->tid, &ti);
612 ERR("Could not get type info: %08x\n", hres);
616 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
618 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
622 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
624 IUnknown_Release(unk);
628 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
630 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
633 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
635 func_disp_t *This = impl_from_IUnknown(iface);
637 if(IsEqualGUID(&IID_IUnknown, riid)) {
638 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
639 *ppv = &This->IUnknown_iface;
640 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
641 return *ppv ? S_OK : E_NOINTERFACE;
644 return E_NOINTERFACE;
647 IUnknown_AddRef((IUnknown*)*ppv);
651 static ULONG WINAPI Function_AddRef(IUnknown *iface)
653 func_disp_t *This = impl_from_IUnknown(iface);
654 LONG ref = InterlockedIncrement(&This->ref);
656 TRACE("(%p) ref=%d\n", This, ref);
661 static ULONG WINAPI Function_Release(IUnknown *iface)
663 func_disp_t *This = impl_from_IUnknown(iface);
664 LONG ref = InterlockedDecrement(&This->ref);
666 TRACE("(%p) ref=%d\n", This, ref);
670 release_dispex(&This->dispex);
677 static const IUnknownVtbl FunctionUnkVtbl = {
678 Function_QueryInterface,
683 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
685 return CONTAINING_RECORD(iface, func_disp_t, dispex);
688 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
689 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
691 func_disp_t *This = impl_from_DispatchEx(dispex);
695 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
699 case DISPATCH_METHOD:
702 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
705 FIXME("Unimplemented flags %x\n", flags);
712 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
719 static const tid_t function_iface_tids[] = {0};
721 static dispex_static_data_t function_dispex = {
722 &function_dispex_vtbl,
728 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
732 ret = heap_alloc_zero(sizeof(func_disp_t));
736 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
737 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
745 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
746 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
748 DISPID named_arg = DISPID_THIS;
749 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
754 FIXME("named args not supported\n");
758 new_dp.rgvarg = heap_alloc((dp->cArgs+1)*sizeof(VARIANTARG));
760 return E_OUTOFMEMORY;
762 new_dp.cArgs = dp->cArgs+1;
763 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
765 V_VT(new_dp.rgvarg) = VT_DISPATCH;
766 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
768 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
770 if(SUCCEEDED(hres)) {
771 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
772 IDispatchEx_Release(dispex);
775 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
778 TRACE("<<< %s\n", debugstr_variant(res));
780 WARN("<<< %08x\n", hres);
782 heap_free(new_dp.rgvarg);
786 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
788 dispex_dynamic_data_t *dynamic_data;
790 dynamic_data = get_dynamic_data(This);
792 return E_OUTOFMEMORY;
794 if(!dynamic_data->func_disps) {
795 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(*dynamic_data->func_disps));
796 if(!dynamic_data->func_disps)
797 return E_OUTOFMEMORY;
800 if(!dynamic_data->func_disps[func->func_disp_idx].func_obj) {
801 func_disp_t *func_obj;
803 func_obj = create_func_disp(This, func);
805 return E_OUTOFMEMORY;
807 dynamic_data->func_disps[func->func_disp_idx].func_obj = func_obj;
809 IDispatchEx_AddRef(&func_obj->dispex.IDispatchEx_iface);
810 dynamic_data->func_disps[func->func_disp_idx].val = (IDispatch*)&func_obj->dispex.IDispatchEx_iface;
813 *ret = dynamic_data->func_disps+func->func_disp_idx;
817 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
822 max = data->func_cnt-1;
827 if(data->funcs[n].id == id) {
828 *ret = data->funcs+n;
832 if(data->funcs[n].id < id)
838 WARN("invalid id %x\n", id);
839 return DISP_E_UNKNOWNNAME;
842 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
847 data = get_dispex_data(This);
852 max = data->func_cnt-1;
857 c = strcmpiW(data->name_table[n]->name, name);
859 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
862 *ret = data->name_table[n]->id;
872 if(This->data->vtbl && This->data->vtbl->get_dispid) {
875 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
876 if(hres != DISP_E_UNKNOWNNAME)
880 return DISP_E_UNKNOWNNAME;
883 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
885 V_VT(dst) = VT_EMPTY;
888 IVariantChangeType *change_type = NULL;
891 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
892 if(SUCCEEDED(hres)) {
893 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
894 IVariantChangeType_Release(change_type);
901 if(V_VT(src) == VT_BSTR) {
903 V_BOOL(dst) = V_BSTR(src) && *V_BSTR(src) ? VARIANT_TRUE : VARIANT_FALSE;
909 return VariantChangeType(dst, src, 0, vt);
912 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
917 if(dp && dp->cArgs) {
918 FIXME("cArgs %d\n", dp->cArgs);
922 assert(func->get_vtbl_off);
924 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
925 if(SUCCEEDED(hres)) {
926 switch(func->prop_vt) {
927 #define CASE_VT(vt,type,access) \
930 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
931 if(SUCCEEDED(hres)) \
935 BUILTIN_TYPES_SWITCH;
938 FIXME("Unhandled vt %d\n", func->prop_vt);
941 IUnknown_Release(iface);
947 if(func->prop_vt != VT_VARIANT)
948 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
952 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
958 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
959 || dp->cNamedArgs > 1) {
960 FIXME("invalid args\n");
964 if(!func->put_vtbl_off) {
965 FIXME("No setter\n");
970 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
971 hres = change_type(&tmpv, v, func->prop_vt, caller);
977 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
978 if(SUCCEEDED(hres)) {
979 switch(func->prop_vt) {
980 #define CASE_VT(vt,type,access) \
982 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
984 BUILTIN_TYPES_SWITCH;
987 FIXME("Unimplemented vt %d\n", func->prop_vt);
991 IUnknown_Release(iface);
999 static HRESULT invoke_builtin_function(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res, IServiceProvider *caller)
1001 VARIANT arg_buf[MAX_ARGS], *arg_ptrs[MAX_ARGS], *arg, retv, ret_ref, vhres;
1002 unsigned i, nconv = 0;
1006 if(dp->cNamedArgs) {
1007 FIXME("Named arguments not supported\n");
1011 if(dp->cArgs != func->argc) {
1012 FIXME("Invalid argument count (expected %u, got %u)\n", func->argc, dp->cArgs);
1013 return E_INVALIDARG;
1016 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1020 for(i=0; i < func->argc; i++) {
1021 arg = dp->rgvarg+dp->cArgs-i-1;
1022 if(func->arg_types[i] == V_VT(arg)) {
1025 hres = change_type(arg_buf+nconv, arg, func->arg_types[i], caller);
1028 arg_ptrs[i] = arg_buf + nconv++;
1032 if(SUCCEEDED(hres)) {
1033 if(func->prop_vt == VT_VOID) {
1034 V_VT(&retv) = VT_EMPTY;
1036 V_VT(&retv) = func->prop_vt;
1037 arg_ptrs[func->argc] = &ret_ref;
1038 V_VT(&ret_ref) = VT_BYREF|func->prop_vt;
1040 switch(func->prop_vt) {
1041 #define CASE_VT(vt,type,access) \
1043 V_BYREF(&ret_ref) = &access(&retv); \
1045 BUILTIN_TYPES_SWITCH;
1052 V_VT(&vhres) = VT_ERROR;
1053 hres = DispCallFunc(iface, func->call_vtbl_off*sizeof(void*), CC_STDCALL, VT_HRESULT,
1054 func->argc + (func->prop_vt == VT_VOID ? 0 : 1), func->arg_types, arg_ptrs, &vhres);
1058 VariantClear(arg_buf+nconv);
1059 IUnknown_Release(iface);
1062 if(FAILED(V_ERROR(&vhres)))
1063 return V_ERROR(&vhres);
1068 VariantClear(&retv);
1069 return V_ERROR(&vhres);
1072 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
1073 EXCEPINFO *ei, IServiceProvider *caller)
1078 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1080 return E_INVALIDARG;
1082 case DISPATCH_METHOD:
1083 if(This->dynamic_data && This->dynamic_data->func_disps
1084 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1085 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
1087 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != entry->val) {
1089 FIXME("Calling null\n");
1093 hres = invoke_disp_value(This, entry->val, 0, flags, dp, res, ei, NULL);
1098 if(func->call_vtbl_off)
1099 hres = invoke_builtin_function(This, func, dp, res, caller);
1101 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1103 case DISPATCH_PROPERTYGET: {
1104 func_obj_entry_t *entry;
1106 if(func->id == DISPID_VALUE) {
1109 ret = SysAllocString(objectW);
1111 return E_OUTOFMEMORY;
1113 V_VT(res) = VT_BSTR;
1118 hres = get_func_obj_entry(This, func, &entry);
1122 V_VT(res) = VT_DISPATCH;
1123 V_DISPATCH(res) = entry->val;
1125 IDispatch_AddRef(V_DISPATCH(res));
1129 case DISPATCH_PROPERTYPUT: {
1130 func_obj_entry_t *entry;
1133 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1134 || dp->cNamedArgs > 1) {
1135 FIXME("invalid args\n");
1136 return E_INVALIDARG;
1140 /* FIXME: not exactly right */
1141 if(V_VT(v) != VT_DISPATCH)
1144 hres = get_func_obj_entry(This, func, &entry);
1149 IDispatch_Release(entry->val);
1150 entry->val = V_DISPATCH(v);
1152 IDispatch_AddRef(entry->val);
1157 FIXME("Unimplemented flags %x\n", flags);
1164 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1165 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1167 dispex_data_t *data;
1171 data = get_dispex_data(This);
1175 hres = get_builtin_func(data, id, &func);
1176 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
1177 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1181 if(func->func_disp_idx != -1)
1182 return function_invoke(This, func, flags, dp, res, ei, caller);
1185 case DISPATCH_PROPERTYPUT:
1187 V_VT(res) = VT_EMPTY;
1188 hres = builtin_propput(This, func, dp, caller);
1190 case DISPATCH_PROPERTYGET:
1191 hres = builtin_propget(This, func, dp, res);
1194 if(!func->get_vtbl_off) {
1195 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1199 hres = builtin_propget(This, func, NULL, &v);
1203 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD)) {
1204 if(V_VT(&v) != VT_DISPATCH) {
1205 FIXME("Not a function %s\n", debugstr_variant(&v));
1210 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1211 IDispatch_Release(V_DISPATCH(&v));
1215 IDispatch_Release(V_DISPATCH(&v));
1223 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
1225 dynamic_prop_t *prop;
1229 hres = get_builtin_id(This, name, 0, &id);
1231 DISPID named_id = DISPID_PROPERTYPUT;
1233 DISPPARAMS dp = {&var,&named_id,1,1};
1236 V_VT(&var) = VT_EMPTY;
1237 memset(&ei, 0, sizeof(ei));
1238 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
1242 *success = VARIANT_TRUE;
1246 hres = get_dynamic_prop(This, name, 0, &prop);
1248 if(hres != DISP_E_UNKNOWNNAME)
1250 *success = VARIANT_FALSE;
1254 VariantClear(&prop->var);
1255 prop->flags |= DYNPROP_DELETED;
1256 *success = VARIANT_TRUE;
1260 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1262 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1265 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1267 DispatchEx *This = impl_from_IDispatchEx(iface);
1269 return IUnknown_QueryInterface(This->outer, riid, ppv);
1272 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1274 DispatchEx *This = impl_from_IDispatchEx(iface);
1276 return IUnknown_AddRef(This->outer);
1279 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1281 DispatchEx *This = impl_from_IDispatchEx(iface);
1283 return IUnknown_Release(This->outer);
1286 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1288 DispatchEx *This = impl_from_IDispatchEx(iface);
1290 TRACE("(%p)->(%p)\n", This, pctinfo);
1296 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1297 LCID lcid, ITypeInfo **ppTInfo)
1299 DispatchEx *This = impl_from_IDispatchEx(iface);
1302 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1304 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
1308 ITypeInfo_AddRef(*ppTInfo);
1312 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1313 LPOLESTR *rgszNames, UINT cNames,
1314 LCID lcid, DISPID *rgDispId)
1316 DispatchEx *This = impl_from_IDispatchEx(iface);
1320 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1323 for(i=0; i < cNames; i++) {
1324 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1332 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1333 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1334 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1336 DispatchEx *This = impl_from_IDispatchEx(iface);
1338 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1339 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1341 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1342 pVarResult, pExcepInfo, NULL);
1345 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1347 DispatchEx *This = impl_from_IDispatchEx(iface);
1348 dynamic_prop_t *dprop;
1351 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1353 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1354 FIXME("Unsupported grfdex %x\n", grfdex);
1356 hres = get_builtin_id(This, bstrName, grfdex, pid);
1357 if(hres != DISP_E_UNKNOWNNAME)
1360 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1364 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1368 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1369 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1371 DispatchEx *This = impl_from_IDispatchEx(iface);
1374 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1376 switch(get_dispid_type(id)) {
1377 case DISPEXPROP_CUSTOM:
1378 if(!This->data->vtbl || !This->data->vtbl->invoke)
1379 return DISP_E_UNKNOWNNAME;
1380 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1382 case DISPEXPROP_DYNAMIC: {
1383 DWORD idx = id - DISPID_DYNPROP_0;
1384 dynamic_prop_t *prop;
1386 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1387 return DISP_E_UNKNOWNNAME;
1389 prop = This->dynamic_data->props+idx;
1392 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1394 return E_INVALIDARG;
1396 case DISPATCH_METHOD:
1397 if(V_VT(&prop->var) != VT_DISPATCH) {
1398 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1402 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1403 case DISPATCH_PROPERTYGET:
1404 if(prop->flags & DYNPROP_DELETED)
1405 return DISP_E_UNKNOWNNAME;
1406 V_VT(pvarRes) = VT_EMPTY;
1407 return variant_copy(pvarRes, &prop->var);
1408 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1409 case DISPATCH_PROPERTYPUT:
1410 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1411 || pdp->cNamedArgs > 1) {
1412 FIXME("invalid args\n");
1413 return E_INVALIDARG;
1416 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1417 VariantClear(&prop->var);
1418 hres = variant_copy(&prop->var, pdp->rgvarg);
1422 prop->flags &= ~DYNPROP_DELETED;
1425 FIXME("unhandled wFlags %x\n", wFlags);
1429 case DISPEXPROP_BUILTIN:
1430 if(wFlags == DISPATCH_CONSTRUCT) {
1431 if(id == DISPID_VALUE) {
1432 if(This->data->vtbl && This->data->vtbl->value) {
1433 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1435 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1438 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1442 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1449 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1451 DispatchEx *This = impl_from_IDispatchEx(iface);
1453 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1455 /* Not implemented by IE */
1459 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1461 DispatchEx *This = impl_from_IDispatchEx(iface);
1463 TRACE("(%p)->(%x)\n", This, id);
1465 /* Not implemented by IE */
1469 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1471 DispatchEx *This = impl_from_IDispatchEx(iface);
1472 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1476 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1478 DispatchEx *This = impl_from_IDispatchEx(iface);
1479 dispex_data_t *data;
1483 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1485 if(is_dynamic_dispid(id)) {
1486 DWORD idx = id - DISPID_DYNPROP_0;
1488 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1489 return DISP_E_UNKNOWNNAME;
1491 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1493 return E_OUTOFMEMORY;
1498 data = get_dispex_data(This);
1502 hres = get_builtin_func(data, id, &func);
1506 *pbstrName = SysAllocString(func->name);
1508 return E_OUTOFMEMORY;
1512 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1514 DispatchEx *This = impl_from_IDispatchEx(iface);
1515 dispex_data_t *data;
1519 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1521 if(is_dynamic_dispid(id)) {
1522 DWORD idx = id - DISPID_DYNPROP_0;
1524 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1525 return DISP_E_UNKNOWNNAME;
1527 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1529 if(idx == This->dynamic_data->prop_cnt) {
1530 *pid = DISPID_STARTENUM;
1534 *pid = DISPID_DYNPROP_0+idx;
1538 data = get_dispex_data(This);
1542 if(id == DISPID_STARTENUM) {
1545 hres = get_builtin_func(data, id, &func);
1551 while(func < data->funcs+data->func_cnt) {
1552 /* FIXME: Skip hidden properties */
1553 if(func->func_disp_idx == -1) {
1560 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1561 *pid = DISPID_DYNPROP_0;
1565 *pid = DISPID_STARTENUM;
1569 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1571 DispatchEx *This = impl_from_IDispatchEx(iface);
1572 FIXME("(%p)->(%p)\n", This, ppunk);
1576 static IDispatchExVtbl DispatchExVtbl = {
1577 DispatchEx_QueryInterface,
1580 DispatchEx_GetTypeInfoCount,
1581 DispatchEx_GetTypeInfo,
1582 DispatchEx_GetIDsOfNames,
1584 DispatchEx_GetDispID,
1585 DispatchEx_InvokeEx,
1586 DispatchEx_DeleteMemberByName,
1587 DispatchEx_DeleteMemberByDispID,
1588 DispatchEx_GetMemberProperties,
1589 DispatchEx_GetMemberName,
1590 DispatchEx_GetNextDispID,
1591 DispatchEx_GetNameSpaceParent
1594 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1596 static const IID IID_UndocumentedScriptIface =
1597 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1598 static const IID IID_IDispatchJS =
1599 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1601 if(IsEqualGUID(&IID_IDispatch, riid)) {
1602 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1603 *ppv = &This->IDispatchEx_iface;
1604 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1605 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1606 *ppv = &This->IDispatchEx_iface;
1607 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1608 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1610 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1611 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1618 IUnknown_AddRef((IUnknown*)*ppv);
1622 void dispex_traverse(DispatchEx *This, nsCycleCollectionTraversalCallback *cb)
1624 dynamic_prop_t *prop;
1626 if(!This->dynamic_data)
1629 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1630 if(V_VT(&prop->var) == VT_DISPATCH)
1631 note_cc_edge((nsISupports*)V_DISPATCH(&prop->var), "dispex_data", cb);
1634 /* FIXME: Traverse func_disps */
1637 void dispex_unlink(DispatchEx *This)
1639 dynamic_prop_t *prop;
1641 if(!This->dynamic_data)
1644 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1645 if(V_VT(&prop->var) == VT_DISPATCH) {
1646 V_VT(&prop->var) = VT_EMPTY;
1647 IDispatch_Release(V_DISPATCH(&prop->var));
1649 VariantClear(&prop->var);
1654 void release_dispex(DispatchEx *This)
1656 dynamic_prop_t *prop;
1658 if(!This->dynamic_data)
1661 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1662 VariantClear(&prop->var);
1663 heap_free(prop->name);
1666 heap_free(This->dynamic_data->props);
1668 if(This->dynamic_data->func_disps) {
1669 func_obj_entry_t *iter;
1671 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps+This->data->data->func_disp_cnt; iter++) {
1672 if(iter->func_obj) {
1673 iter->func_obj->obj = NULL;
1674 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
1677 IDispatch_Release(iter->val);
1680 heap_free(This->dynamic_data->func_disps);
1683 heap_free(This->dynamic_data);
1686 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1688 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1689 dispex->outer = outer;
1690 dispex->data = data;
1691 dispex->dynamic_data = NULL;