d2d1: Add a helper to set error state.
[wine.git] / dlls / mshtml / dispex.c
blob02ba5d41d507729a0166a50ba0745c04aabec396
1 /*
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
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "mscoree.h"
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 #define MAX_ARGS 16
39 static CRITICAL_SECTION cs_dispex_static_data;
40 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
42 0, 0, &cs_dispex_static_data,
43 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
44 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
46 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
49 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
51 typedef struct {
52 DISPID id;
53 BSTR name;
54 tid_t tid;
55 dispex_hook_invoke_t hook;
56 SHORT call_vtbl_off;
57 SHORT put_vtbl_off;
58 SHORT get_vtbl_off;
59 SHORT func_disp_idx;
60 USHORT argc;
61 VARTYPE prop_vt;
62 VARTYPE *arg_types;
63 } func_info_t;
65 struct dispex_data_t {
66 dispex_static_data_t *desc;
67 compat_mode_t compat_mode;
69 DWORD func_cnt;
70 DWORD func_size;
71 func_info_t *funcs;
72 func_info_t **name_table;
73 DWORD func_disp_cnt;
75 struct list entry;
78 typedef struct {
79 VARIANT var;
80 LPWSTR name;
81 DWORD flags;
82 } dynamic_prop_t;
84 #define DYNPROP_DELETED 0x01
86 typedef struct {
87 DispatchEx dispex;
88 IUnknown IUnknown_iface;
89 LONG ref;
90 DispatchEx *obj;
91 func_info_t *info;
92 } func_disp_t;
94 typedef struct {
95 func_disp_t *func_obj;
96 VARIANT val;
97 } func_obj_entry_t;
99 struct dispex_dynamic_data_t {
100 DWORD buf_size;
101 DWORD prop_cnt;
102 dynamic_prop_t *props;
103 func_obj_entry_t *func_disps;
106 #define DISPID_DYNPROP_0 0x50000000
107 #define DISPID_DYNPROP_MAX 0x5fffffff
109 #define FDEX_VERSION_MASK 0xf0000000
111 static ITypeLib *typelib;
112 static ITypeInfo *typeinfos[LAST_tid];
113 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
115 static REFIID tid_ids[] = {
116 #define XIID(iface) &IID_ ## iface,
117 #define XDIID(iface) &DIID_ ## iface,
118 TID_LIST
119 #undef XIID
120 #undef XDIID
123 static HRESULT load_typelib(void)
125 HRESULT hres;
126 ITypeLib *tl;
128 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
129 if(FAILED(hres)) {
130 ERR("LoadRegTypeLib failed: %08x\n", hres);
131 return hres;
134 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
135 ITypeLib_Release(tl);
136 return hres;
139 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
141 HRESULT hres;
143 if (!typelib)
144 hres = load_typelib();
145 if (!typelib)
146 return hres;
148 if(!typeinfos[tid]) {
149 ITypeInfo *ti;
151 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
152 if(FAILED(hres)) {
153 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_mshtml_guid(tid_ids[tid]), hres);
154 return hres;
157 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
158 ITypeInfo_Release(ti);
161 *typeinfo = typeinfos[tid];
162 return S_OK;
165 void release_typelib(void)
167 dispex_data_t *iter;
168 unsigned i;
170 while(!list_empty(&dispex_data_list)) {
171 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
172 list_remove(&iter->entry);
174 for(i=0; i < iter->func_cnt; i++)
175 SysFreeString(iter->funcs[i].name);
177 heap_free(iter->funcs);
178 heap_free(iter->name_table);
179 heap_free(iter);
182 if(!typelib)
183 return;
185 for(i=0; i < ARRAY_SIZE(typeinfos); i++)
186 if(typeinfos[i])
187 ITypeInfo_Release(typeinfos[i]);
189 ITypeLib_Release(typelib);
190 DeleteCriticalSection(&cs_dispex_static_data);
193 HRESULT get_class_typeinfo(const CLSID *clsid, ITypeInfo **typeinfo)
195 HRESULT hres;
197 if (!typelib)
198 hres = load_typelib();
199 if (!typelib)
200 return hres;
202 hres = ITypeLib_GetTypeInfoOfGuid(typelib, clsid, typeinfo);
203 if(FAILED(hres))
204 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
205 return hres;
208 /* Not all argument types are supported yet */
209 #define BUILTIN_ARG_TYPES_SWITCH \
210 CASE_VT(VT_I2, INT16, V_I2); \
211 CASE_VT(VT_UI2, UINT16, V_UI2); \
212 CASE_VT(VT_I4, INT32, V_I4); \
213 CASE_VT(VT_UI4, UINT32, V_UI4); \
214 CASE_VT(VT_R4, float, V_R4); \
215 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
216 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
218 /* List all types used by IDispatchEx-based properties */
219 #define BUILTIN_TYPES_SWITCH \
220 BUILTIN_ARG_TYPES_SWITCH; \
221 CASE_VT(VT_VARIANT, VARIANT, *); \
222 CASE_VT(VT_PTR, void*, V_BYREF); \
223 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
224 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH); \
225 CASE_VT(VT_UI8, ULONGLONG, V_UI8)
227 static BOOL is_arg_type_supported(VARTYPE vt)
229 switch(vt) {
230 #define CASE_VT(x,a,b) case x: return TRUE
231 BUILTIN_ARG_TYPES_SWITCH;
232 #undef CASE_VT
234 return FALSE;
237 static void add_func_info(dispex_data_t *data, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti,
238 dispex_hook_invoke_t hook)
240 func_info_t *info;
241 BSTR name;
242 HRESULT hres;
244 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &name, NULL, NULL, NULL);
245 if(FAILED(hres))
246 return;
248 for(info = data->funcs; info < data->funcs+data->func_cnt; info++) {
249 if(info->id == desc->memid || !strcmpW(info->name, name)) {
250 if(info->tid != tid) {
251 SysFreeString(name);
252 return; /* Duplicated in other interface */
254 break;
258 if(info == data->funcs+data->func_cnt) {
259 if(data->func_cnt == data->func_size)
260 data->funcs = heap_realloc_zero(data->funcs, (data->func_size <<= 1)*sizeof(func_info_t));
261 info = data->funcs+data->func_cnt;
263 data->func_cnt++;
265 info->id = desc->memid;
266 info->name = name;
267 info->tid = tid;
268 info->func_disp_idx = -1;
269 info->prop_vt = VT_EMPTY;
270 info->hook = hook;
271 }else {
272 SysFreeString(name);
275 if(desc->invkind & DISPATCH_METHOD) {
276 unsigned i;
278 info->func_disp_idx = data->func_disp_cnt++;
279 info->argc = desc->cParams;
281 assert(info->argc < MAX_ARGS);
282 assert(desc->funckind == FUNC_DISPATCH);
284 info->arg_types = heap_alloc(sizeof(*info->arg_types) * info->argc);
285 if(!info->arg_types)
286 return; /* FIXME: real error instead of fallback */
288 for(i=0; i < info->argc; i++)
289 info->arg_types[i] = desc->lprgelemdescParam[i].tdesc.vt;
291 info->prop_vt = desc->elemdescFunc.tdesc.vt;
292 if(info->prop_vt != VT_VOID && !is_arg_type_supported(info->prop_vt)) {
293 TRACE("%s: return type %d\n", debugstr_w(info->name), info->prop_vt);
294 return; /* Fallback to ITypeInfo::Invoke */
297 if(desc->cParamsOpt) {
298 TRACE("%s: optional params\n", debugstr_w(info->name));
299 return; /* Fallback to ITypeInfo::Invoke */
302 for(i=0; i < info->argc; i++) {
303 if(!is_arg_type_supported(info->arg_types[i])) {
304 return; /* Fallback to ITypeInfo for unsupported arg types */
307 if(desc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
308 TRACE("%s param %d: default value\n", debugstr_w(info->name), i);
309 return; /* Fallback to ITypeInfo::Invoke */
313 assert(info->argc <= MAX_ARGS);
314 assert(desc->callconv == CC_STDCALL);
316 info->call_vtbl_off = desc->oVft/sizeof(void*);
317 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
318 VARTYPE vt = VT_EMPTY;
320 if(desc->invkind & DISPATCH_PROPERTYGET) {
321 vt = desc->elemdescFunc.tdesc.vt;
322 info->get_vtbl_off = desc->oVft/sizeof(void*);
324 if(desc->invkind & DISPATCH_PROPERTYPUT) {
325 assert(desc->cParams == 1);
326 vt = desc->lprgelemdescParam->tdesc.vt;
327 info->put_vtbl_off = desc->oVft/sizeof(void*);
330 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
331 info->prop_vt = vt;
335 static HRESULT process_interface(dispex_data_t *data, tid_t tid, ITypeInfo *disp_typeinfo, const dispex_hook_t *hooks)
337 unsigned i = 7; /* skip IDispatch functions */
338 ITypeInfo *typeinfo;
339 FUNCDESC *funcdesc;
340 HRESULT hres;
342 hres = get_typeinfo(tid, &typeinfo);
343 if(FAILED(hres))
344 return hres;
346 while(1) {
347 const dispex_hook_t *hook = NULL;
349 hres = ITypeInfo_GetFuncDesc(typeinfo, i++, &funcdesc);
350 if(FAILED(hres))
351 break;
353 if(hooks) {
354 for(hook = hooks; hook->dispid != DISPID_UNKNOWN; hook++) {
355 if(hook->dispid == funcdesc->memid)
356 break;
358 if(hook->dispid == DISPID_UNKNOWN)
359 hook = NULL;
362 if(!hook || hook->invoke) {
363 TRACE("adding...\n");
364 add_func_info(data, tid, funcdesc, disp_typeinfo ? disp_typeinfo : typeinfo,
365 hook ? hook->invoke : NULL);
368 ITypeInfo_ReleaseFuncDesc(typeinfo, funcdesc);
371 return S_OK;
374 void dispex_info_add_interface(dispex_data_t *info, tid_t tid, const dispex_hook_t *hooks)
376 HRESULT hres;
378 hres = process_interface(info, tid, NULL, hooks);
379 if(FAILED(hres))
380 ERR("process_interface failed: %08x\n", hres);
383 static int dispid_cmp(const void *p1, const void *p2)
385 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
388 static int func_name_cmp(const void *p1, const void *p2)
390 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
393 static dispex_data_t *preprocess_dispex_data(dispex_static_data_t *desc, compat_mode_t compat_mode)
395 const tid_t *tid;
396 dispex_data_t *data;
397 DWORD i;
398 ITypeInfo *dti;
399 HRESULT hres;
401 if(desc->disp_tid) {
402 hres = get_typeinfo(desc->disp_tid, &dti);
403 if(FAILED(hres)) {
404 ERR("Could not get disp type info: %08x\n", hres);
405 return NULL;
409 data = heap_alloc(sizeof(dispex_data_t));
410 if (!data) {
411 ERR("Out of memory\n");
412 return NULL;
414 data->desc = desc;
415 data->compat_mode = compat_mode;
416 data->func_cnt = 0;
417 data->func_disp_cnt = 0;
418 data->func_size = 16;
419 data->funcs = heap_alloc_zero(data->func_size*sizeof(func_info_t));
420 if (!data->funcs) {
421 heap_free (data);
422 ERR("Out of memory\n");
423 return NULL;
425 list_add_tail(&dispex_data_list, &data->entry);
427 if(desc->init_info)
428 desc->init_info(data, compat_mode);
430 for(tid = desc->iface_tids; *tid; tid++) {
431 hres = process_interface(data, *tid, dti, NULL);
432 if(FAILED(hres))
433 break;
436 if(!data->func_cnt) {
437 heap_free(data->funcs);
438 data->name_table = NULL;
439 data->funcs = NULL;
440 data->func_size = 0;
441 return data;
445 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
446 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
448 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
449 for(i=0; i < data->func_cnt; i++)
450 data->name_table[i] = data->funcs+i;
451 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
452 return data;
455 static int id_cmp(const void *p1, const void *p2)
457 return *(const DISPID*)p1 - *(const DISPID*)p2;
460 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
462 unsigned i, func_cnt;
463 FUNCDESC *funcdesc;
464 ITypeInfo *ti;
465 TYPEATTR *attr;
466 DISPID *ids;
467 HRESULT hres;
469 hres = get_typeinfo(tid, &ti);
470 if(FAILED(hres))
471 return hres;
473 hres = ITypeInfo_GetTypeAttr(ti, &attr);
474 if(FAILED(hres)) {
475 ITypeInfo_Release(ti);
476 return hres;
479 func_cnt = attr->cFuncs;
480 ITypeInfo_ReleaseTypeAttr(ti, attr);
482 ids = heap_alloc(func_cnt*sizeof(DISPID));
483 if(!ids) {
484 ITypeInfo_Release(ti);
485 return E_OUTOFMEMORY;
488 for(i=0; i < func_cnt; i++) {
489 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
490 if(FAILED(hres))
491 break;
493 ids[i] = funcdesc->memid;
494 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
497 ITypeInfo_Release(ti);
498 if(FAILED(hres)) {
499 heap_free(ids);
500 return hres;
503 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
505 *ret_size = func_cnt;
506 *ret = ids;
507 return S_OK;
510 static inline BOOL is_custom_dispid(DISPID id)
512 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
515 static inline BOOL is_dynamic_dispid(DISPID id)
517 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
520 dispex_prop_type_t get_dispid_type(DISPID id)
522 if(is_dynamic_dispid(id))
523 return DISPEXPROP_DYNAMIC;
524 if(is_custom_dispid(id))
525 return DISPEXPROP_CUSTOM;
526 return DISPEXPROP_BUILTIN;
529 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
531 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
532 V_VT(dest) = VT_BSTR;
533 V_BSTR(dest) = NULL;
534 return S_OK;
537 return VariantCopy(dest, src);
540 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
542 if(This->dynamic_data)
543 return This->dynamic_data;
545 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
546 if(!This->dynamic_data)
547 return NULL;
549 if(This->info->desc->vtbl && This->info->desc->vtbl->populate_props)
550 This->info->desc->vtbl->populate_props(This);
552 return This->dynamic_data;
555 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
557 const BOOL alloc = flags & fdexNameEnsure;
558 dispex_dynamic_data_t *data;
559 dynamic_prop_t *prop;
561 data = get_dynamic_data(This);
562 if(!data)
563 return E_OUTOFMEMORY;
565 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
566 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
567 if(prop->flags & DYNPROP_DELETED) {
568 if(!alloc)
569 return DISP_E_UNKNOWNNAME;
570 prop->flags &= ~DYNPROP_DELETED;
572 *ret = prop;
573 return S_OK;
577 if(!alloc)
578 return DISP_E_UNKNOWNNAME;
580 TRACE("creating dynamic prop %s\n", debugstr_w(name));
582 if(!data->buf_size) {
583 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
584 if(!data->props)
585 return E_OUTOFMEMORY;
586 data->buf_size = 4;
587 }else if(data->buf_size == data->prop_cnt) {
588 dynamic_prop_t *new_props;
590 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
591 if(!new_props)
592 return E_OUTOFMEMORY;
594 data->props = new_props;
595 data->buf_size <<= 1;
598 prop = data->props + data->prop_cnt;
600 prop->name = heap_strdupW(name);
601 if(!prop->name)
602 return E_OUTOFMEMORY;
604 VariantInit(&prop->var);
605 prop->flags = 0;
606 data->prop_cnt++;
607 *ret = prop;
608 return S_OK;
611 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
613 dynamic_prop_t *prop;
614 HRESULT hres;
616 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
617 if(FAILED(hres))
618 return hres;
620 *ret = &prop->var;
621 return S_OK;
624 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
626 dynamic_prop_t *prop;
627 HRESULT hres;
629 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
630 if(FAILED(hres))
631 return hres;
633 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
634 return S_OK;
637 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
638 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
640 if(This->info->desc->vtbl && This->info->desc->vtbl->value)
641 return This->info->desc->vtbl->value(This, lcid, flags, params, res, ei, caller);
643 switch(flags) {
644 case DISPATCH_PROPERTYGET:
645 V_VT(res) = VT_BSTR;
646 V_BSTR(res) = SysAllocString(objectW);
647 if(!V_BSTR(res))
648 return E_OUTOFMEMORY;
649 break;
650 default:
651 FIXME("Unimplemented flags %x\n", flags);
652 return E_NOTIMPL;
655 return S_OK;
658 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
659 EXCEPINFO *ei)
661 DISPPARAMS params = {dp->rgvarg+dp->cNamedArgs, NULL, dp->cArgs-dp->cNamedArgs, 0};
662 ITypeInfo *ti;
663 IUnknown *unk;
664 UINT argerr=0;
665 HRESULT hres;
667 hres = get_typeinfo(func->tid, &ti);
668 if(FAILED(hres)) {
669 ERR("Could not get type info: %08x\n", hres);
670 return hres;
673 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
674 if(FAILED(hres)) {
675 ERR("Could not get iface %s: %08x\n", debugstr_mshtml_guid(tid_ids[func->tid]), hres);
676 return E_FAIL;
679 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, &params, res, ei, &argerr);
681 IUnknown_Release(unk);
682 return hres;
685 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
687 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
690 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
692 func_disp_t *This = impl_from_IUnknown(iface);
694 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
696 if(IsEqualGUID(&IID_IUnknown, riid)) {
697 *ppv = &This->IUnknown_iface;
698 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
699 return *ppv ? S_OK : E_NOINTERFACE;
700 }else {
701 *ppv = NULL;
702 return E_NOINTERFACE;
705 IUnknown_AddRef((IUnknown*)*ppv);
706 return S_OK;
709 static ULONG WINAPI Function_AddRef(IUnknown *iface)
711 func_disp_t *This = impl_from_IUnknown(iface);
712 LONG ref = InterlockedIncrement(&This->ref);
714 TRACE("(%p) ref=%d\n", This, ref);
716 return ref;
719 static ULONG WINAPI Function_Release(IUnknown *iface)
721 func_disp_t *This = impl_from_IUnknown(iface);
722 LONG ref = InterlockedDecrement(&This->ref);
724 TRACE("(%p) ref=%d\n", This, ref);
726 if(!ref) {
727 assert(!This->obj);
728 release_dispex(&This->dispex);
729 heap_free(This);
732 return ref;
735 static const IUnknownVtbl FunctionUnkVtbl = {
736 Function_QueryInterface,
737 Function_AddRef,
738 Function_Release
741 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
743 return CONTAINING_RECORD(iface, func_disp_t, dispex);
746 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
747 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
749 func_disp_t *This = impl_from_DispatchEx(dispex);
750 HRESULT hres;
752 switch(flags) {
753 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
754 if(!res)
755 return E_INVALIDARG;
756 /* fall through */
757 case DISPATCH_METHOD:
758 if(!This->obj)
759 return E_UNEXPECTED;
760 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
761 break;
762 case DISPATCH_PROPERTYGET: {
763 unsigned name_len;
764 WCHAR *ptr;
765 BSTR str;
767 static const WCHAR func_prefixW[] =
768 {'\n','f','u','n','c','t','i','o','n',' '};
769 static const WCHAR func_suffixW[] =
770 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
772 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
773 if(!caller)
774 return E_ACCESSDENIED;
776 name_len = SysStringLen(This->info->name);
777 ptr = str = SysAllocStringLen(NULL, name_len + ARRAY_SIZE(func_prefixW) + ARRAY_SIZE(func_suffixW));
778 if(!str)
779 return E_OUTOFMEMORY;
781 memcpy(ptr, func_prefixW, sizeof(func_prefixW));
782 ptr += ARRAY_SIZE(func_prefixW);
784 memcpy(ptr, This->info->name, name_len*sizeof(WCHAR));
785 ptr += name_len;
787 memcpy(ptr, func_suffixW, sizeof(func_suffixW));
789 V_VT(res) = VT_BSTR;
790 V_BSTR(res) = str;
791 return S_OK;
793 default:
794 FIXME("Unimplemented flags %x\n", flags);
795 hres = E_NOTIMPL;
798 return hres;
801 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
802 function_value,
803 NULL,
804 NULL,
805 NULL
808 static const tid_t function_iface_tids[] = {0};
810 static dispex_static_data_t function_dispex = {
811 &function_dispex_vtbl,
812 NULL_tid,
813 function_iface_tids
816 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
818 func_disp_t *ret;
820 ret = heap_alloc_zero(sizeof(func_disp_t));
821 if(!ret)
822 return NULL;
824 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
825 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
826 ret->ref = 1;
827 ret->obj = obj;
828 ret->info = info;
830 return ret;
833 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
834 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
836 DISPID named_arg = DISPID_THIS;
837 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
838 IDispatchEx *dispex;
839 HRESULT hres;
841 if(dp->cNamedArgs) {
842 FIXME("named args not supported\n");
843 return E_NOTIMPL;
846 new_dp.rgvarg = heap_alloc((dp->cArgs+1)*sizeof(VARIANTARG));
847 if(!new_dp.rgvarg)
848 return E_OUTOFMEMORY;
850 new_dp.cArgs = dp->cArgs+1;
851 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
853 V_VT(new_dp.rgvarg) = VT_DISPATCH;
854 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
856 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
857 TRACE(">>>\n");
858 if(SUCCEEDED(hres)) {
859 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
860 IDispatchEx_Release(dispex);
861 }else {
862 ULONG err = 0;
863 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
865 if(SUCCEEDED(hres))
866 TRACE("<<< %s\n", debugstr_variant(res));
867 else
868 WARN("<<< %08x\n", hres);
870 heap_free(new_dp.rgvarg);
871 return hres;
874 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
876 dispex_dynamic_data_t *dynamic_data;
877 func_obj_entry_t *entry;
879 dynamic_data = get_dynamic_data(This);
880 if(!dynamic_data)
881 return E_OUTOFMEMORY;
883 if(!dynamic_data->func_disps) {
884 dynamic_data->func_disps = heap_alloc_zero(This->info->func_disp_cnt * sizeof(*dynamic_data->func_disps));
885 if(!dynamic_data->func_disps)
886 return E_OUTOFMEMORY;
889 entry = dynamic_data->func_disps + func->func_disp_idx;
890 if(!entry->func_obj) {
891 entry->func_obj = create_func_disp(This, func);
892 if(!entry->func_obj)
893 return E_OUTOFMEMORY;
895 IDispatchEx_AddRef(&entry->func_obj->dispex.IDispatchEx_iface);
896 V_VT(&entry->val) = VT_DISPATCH;
897 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
900 *ret = entry;
901 return S_OK;
904 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
906 int min, max, n;
908 min = 0;
909 max = data->func_cnt-1;
911 while(min <= max) {
912 n = (min+max)/2;
914 if(data->funcs[n].id == id) {
915 *ret = data->funcs+n;
916 return S_OK;
919 if(data->funcs[n].id < id)
920 min = n+1;
921 else
922 max = n-1;
925 WARN("invalid id %x\n", id);
926 return DISP_E_UNKNOWNNAME;
929 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
931 int min, max, n, c;
933 min = 0;
934 max = This->info->func_cnt-1;
936 while(min <= max) {
937 n = (min+max)/2;
939 c = strcmpiW(This->info->name_table[n]->name, name);
940 if(!c) {
941 if((grfdex & fdexNameCaseSensitive) && strcmpW(This->info->name_table[n]->name, name))
942 break;
944 *ret = This->info->name_table[n]->id;
945 return S_OK;
948 if(c > 0)
949 max = n-1;
950 else
951 min = n+1;
954 if(This->info->desc->vtbl && This->info->desc->vtbl->get_dispid) {
955 HRESULT hres;
957 hres = This->info->desc->vtbl->get_dispid(This, name, grfdex, ret);
958 if(hres != DISP_E_UNKNOWNNAME)
959 return hres;
962 return DISP_E_UNKNOWNNAME;
965 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
967 V_VT(dst) = VT_EMPTY;
969 if(caller) {
970 IVariantChangeType *change_type = NULL;
971 HRESULT hres;
973 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
974 if(SUCCEEDED(hres)) {
975 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
976 IVariantChangeType_Release(change_type);
977 return hres;
981 switch(vt) {
982 case VT_BOOL:
983 if(V_VT(src) == VT_BSTR) {
984 V_VT(dst) = VT_BOOL;
985 V_BOOL(dst) = variant_bool(V_BSTR(src) && *V_BSTR(src));
986 return S_OK;
988 break;
991 return VariantChangeType(dst, src, 0, vt);
994 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
996 IUnknown *iface;
997 HRESULT hres;
999 if(dp && dp->cArgs) {
1000 FIXME("cArgs %d\n", dp->cArgs);
1001 return E_NOTIMPL;
1004 assert(func->get_vtbl_off);
1006 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1007 if(SUCCEEDED(hres)) {
1008 switch(func->prop_vt) {
1009 #define CASE_VT(vt,type,access) \
1010 case vt: { \
1011 type val; \
1012 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
1013 if(SUCCEEDED(hres)) \
1014 access(res) = val; \
1016 break
1017 BUILTIN_TYPES_SWITCH;
1018 #undef CASE_VT
1019 default:
1020 FIXME("Unhandled vt %d\n", func->prop_vt);
1021 hres = E_NOTIMPL;
1023 IUnknown_Release(iface);
1026 if(FAILED(hres))
1027 return hres;
1029 if(func->prop_vt != VT_VARIANT)
1030 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
1031 return S_OK;
1034 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
1036 VARIANT *v, tmpv;
1037 IUnknown *iface;
1038 HRESULT hres;
1040 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1041 || dp->cNamedArgs > 1) {
1042 FIXME("invalid args\n");
1043 return E_INVALIDARG;
1046 if(!func->put_vtbl_off) {
1047 FIXME("No setter\n");
1048 return E_FAIL;
1051 v = dp->rgvarg;
1052 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
1053 hres = change_type(&tmpv, v, func->prop_vt, caller);
1054 if(FAILED(hres))
1055 return hres;
1056 v = &tmpv;
1059 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1060 if(SUCCEEDED(hres)) {
1061 switch(func->prop_vt) {
1062 #define CASE_VT(vt,type,access) \
1063 case vt: \
1064 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1065 break
1066 BUILTIN_TYPES_SWITCH;
1067 #undef CASE_VT
1068 default:
1069 FIXME("Unimplemented vt %d\n", func->prop_vt);
1070 hres = E_NOTIMPL;
1073 IUnknown_Release(iface);
1076 if(v == &tmpv)
1077 VariantClear(v);
1078 return hres;
1081 static HRESULT invoke_builtin_function(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res, IServiceProvider *caller)
1083 VARIANT arg_buf[MAX_ARGS], *arg_ptrs[MAX_ARGS], *arg, retv, ret_ref, vhres;
1084 unsigned i, nconv = 0;
1085 IUnknown *iface;
1086 HRESULT hres;
1088 if(dp->cNamedArgs) {
1089 FIXME("Named arguments not supported\n");
1090 return E_NOTIMPL;
1093 if(dp->cArgs != func->argc) {
1094 FIXME("Invalid argument count (expected %u, got %u)\n", func->argc, dp->cArgs);
1095 return E_INVALIDARG;
1098 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1099 if(FAILED(hres))
1100 return hres;
1102 for(i=0; i < func->argc; i++) {
1103 arg = dp->rgvarg+dp->cArgs-i-1;
1104 if(func->arg_types[i] == V_VT(arg)) {
1105 arg_ptrs[i] = arg;
1106 }else {
1107 hres = change_type(arg_buf+nconv, arg, func->arg_types[i], caller);
1108 if(FAILED(hres))
1109 break;
1110 arg_ptrs[i] = arg_buf + nconv++;
1114 if(SUCCEEDED(hres)) {
1115 if(func->prop_vt == VT_VOID) {
1116 V_VT(&retv) = VT_EMPTY;
1117 }else {
1118 V_VT(&retv) = func->prop_vt;
1119 arg_ptrs[func->argc] = &ret_ref;
1120 V_VT(&ret_ref) = VT_BYREF|func->prop_vt;
1122 switch(func->prop_vt) {
1123 #define CASE_VT(vt,type,access) \
1124 case vt: \
1125 V_BYREF(&ret_ref) = &access(&retv); \
1126 break
1127 BUILTIN_TYPES_SWITCH;
1128 #undef CASE_VT
1129 default:
1130 assert(0);
1134 V_VT(&vhres) = VT_ERROR;
1135 hres = DispCallFunc(iface, func->call_vtbl_off*sizeof(void*), CC_STDCALL, VT_ERROR,
1136 func->argc + (func->prop_vt == VT_VOID ? 0 : 1), func->arg_types, arg_ptrs, &vhres);
1139 while(nconv--)
1140 VariantClear(arg_buf+nconv);
1141 IUnknown_Release(iface);
1142 if(FAILED(hres))
1143 return hres;
1144 if(FAILED(V_ERROR(&vhres)))
1145 return V_ERROR(&vhres);
1147 if(res)
1148 *res = retv;
1149 else
1150 VariantClear(&retv);
1151 return V_ERROR(&vhres);
1154 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
1155 EXCEPINFO *ei, IServiceProvider *caller)
1157 HRESULT hres;
1159 switch(flags) {
1160 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1161 if(!res)
1162 return E_INVALIDARG;
1163 /* fall through */
1164 case DISPATCH_METHOD:
1165 if(This->dynamic_data && This->dynamic_data->func_disps
1166 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1167 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
1169 if(V_VT(&entry->val) != VT_DISPATCH) {
1170 FIXME("calling %s not supported\n", debugstr_variant(&entry->val));
1171 return E_NOTIMPL;
1174 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != V_DISPATCH(&entry->val)) {
1175 if(!V_DISPATCH(&entry->val)) {
1176 FIXME("Calling null\n");
1177 return E_FAIL;
1180 hres = invoke_disp_value(This, V_DISPATCH(&entry->val), 0, flags, dp, res, ei, NULL);
1181 break;
1185 if(func->call_vtbl_off)
1186 hres = invoke_builtin_function(This, func, dp, res, caller);
1187 else
1188 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1189 break;
1190 case DISPATCH_PROPERTYGET: {
1191 func_obj_entry_t *entry;
1193 if(func->id == DISPID_VALUE) {
1194 BSTR ret;
1196 ret = SysAllocString(objectW);
1197 if(!ret)
1198 return E_OUTOFMEMORY;
1200 V_VT(res) = VT_BSTR;
1201 V_BSTR(res) = ret;
1202 return S_OK;
1205 hres = get_func_obj_entry(This, func, &entry);
1206 if(FAILED(hres))
1207 return hres;
1209 V_VT(res) = VT_EMPTY;
1210 return VariantCopy(res, &entry->val);
1212 case DISPATCH_PROPERTYPUT: {
1213 func_obj_entry_t *entry;
1215 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1216 || dp->cNamedArgs > 1) {
1217 FIXME("invalid args\n");
1218 return E_INVALIDARG;
1222 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1223 * function property using InvokeEx, it's possible to do that from jscript.
1224 * Native probably uses some undocumented interface in this case, but it should
1225 * be fine for us to allow IDispatchEx handle that.
1227 hres = get_func_obj_entry(This, func, &entry);
1228 if(FAILED(hres))
1229 return hres;
1231 return VariantCopy(&entry->val, dp->rgvarg);
1233 default:
1234 FIXME("Unimplemented flags %x\n", flags);
1235 hres = E_NOTIMPL;
1238 return hres;
1241 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1242 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1244 func_info_t *func;
1245 HRESULT hres;
1247 hres = get_builtin_func(This->info, id, &func);
1248 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
1249 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1250 if(FAILED(hres))
1251 return hres;
1253 if(func->hook) {
1254 hres = func->hook(This, lcid, flags, dp, res, ei, caller);
1255 if(hres != S_FALSE)
1256 return hres;
1259 if(func->func_disp_idx != -1)
1260 return function_invoke(This, func, flags, dp, res, ei, caller);
1262 switch(flags) {
1263 case DISPATCH_PROPERTYPUT:
1264 if(res)
1265 V_VT(res) = VT_EMPTY;
1266 hres = builtin_propput(This, func, dp, caller);
1267 break;
1268 case DISPATCH_PROPERTYGET:
1269 hres = builtin_propget(This, func, dp, res);
1270 break;
1271 default:
1272 if(!func->get_vtbl_off) {
1273 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1274 }else {
1275 VARIANT v;
1277 hres = builtin_propget(This, func, NULL, &v);
1278 if(FAILED(hres))
1279 return hres;
1281 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD) || dp->cArgs) {
1282 if(V_VT(&v) != VT_DISPATCH) {
1283 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v), flags);
1284 VariantClear(&v);
1285 return E_FAIL;
1288 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1289 IDispatch_Release(V_DISPATCH(&v));
1290 }else if(res) {
1291 *res = v;
1292 }else {
1293 VariantClear(&v);
1298 return hres;
1301 HRESULT remove_attribute(DispatchEx *This, DISPID id, VARIANT_BOOL *success)
1303 switch(get_dispid_type(id)) {
1304 case DISPEXPROP_CUSTOM:
1305 FIXME("DISPEXPROP_CUSTOM not supported\n");
1306 return E_NOTIMPL;
1308 case DISPEXPROP_DYNAMIC: {
1309 DWORD idx = id - DISPID_DYNPROP_0;
1310 dynamic_prop_t *prop;
1312 prop = This->dynamic_data->props+idx;
1313 VariantClear(&prop->var);
1314 prop->flags |= DYNPROP_DELETED;
1315 *success = VARIANT_TRUE;
1316 return S_OK;
1318 case DISPEXPROP_BUILTIN: {
1319 VARIANT var;
1320 DISPPARAMS dp = {&var,NULL,1,0};
1321 func_info_t *func;
1322 HRESULT hres;
1324 hres = get_builtin_func(This->info, id, &func);
1325 if(FAILED(hres))
1326 return hres;
1328 /* For builtin functions, we set their value to the original function. */
1329 if(func->func_disp_idx != -1) {
1330 func_obj_entry_t *entry;
1332 if(!This->dynamic_data || !This->dynamic_data->func_disps
1333 || !This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1334 *success = VARIANT_FALSE;
1335 return S_OK;
1338 entry = This->dynamic_data->func_disps + func->func_disp_idx;
1339 if(V_VT(&entry->val) == VT_DISPATCH
1340 && V_DISPATCH(&entry->val) == (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface) {
1341 *success = VARIANT_FALSE;
1342 return S_OK;
1345 VariantClear(&entry->val);
1346 V_VT(&entry->val) = VT_DISPATCH;
1347 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
1348 IDispatch_AddRef(V_DISPATCH(&entry->val));
1349 *success = VARIANT_TRUE;
1350 return S_OK;
1353 V_VT(&var) = VT_EMPTY;
1354 hres = builtin_propput(This, func, &dp, NULL);
1355 if(FAILED(hres))
1356 return hres;
1358 *success = VARIANT_TRUE;
1359 return S_OK;
1361 default:
1362 assert(0);
1363 return E_FAIL;
1367 compat_mode_t dispex_compat_mode(DispatchEx *dispex)
1369 return dispex->info != dispex->info->desc->delayed_init_info
1370 ? dispex->info->compat_mode
1371 : dispex->info->desc->vtbl->get_compat_mode(dispex);
1374 static dispex_data_t *ensure_dispex_info(dispex_static_data_t *desc, compat_mode_t compat_mode)
1376 if(!desc->info_cache[compat_mode]) {
1377 EnterCriticalSection(&cs_dispex_static_data);
1378 if(!desc->info_cache[compat_mode])
1379 desc->info_cache[compat_mode] = preprocess_dispex_data(desc, compat_mode);
1380 LeaveCriticalSection(&cs_dispex_static_data);
1382 return desc->info_cache[compat_mode];
1385 static BOOL ensure_real_info(DispatchEx *dispex)
1387 if(dispex->info != dispex->info->desc->delayed_init_info)
1388 return TRUE;
1390 dispex->info = ensure_dispex_info(dispex->info->desc, dispex_compat_mode(dispex));
1391 return dispex->info != NULL;
1394 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1396 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1399 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1401 DispatchEx *This = impl_from_IDispatchEx(iface);
1403 return IUnknown_QueryInterface(This->outer, riid, ppv);
1406 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1408 DispatchEx *This = impl_from_IDispatchEx(iface);
1410 return IUnknown_AddRef(This->outer);
1413 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1415 DispatchEx *This = impl_from_IDispatchEx(iface);
1417 return IUnknown_Release(This->outer);
1420 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1422 DispatchEx *This = impl_from_IDispatchEx(iface);
1424 TRACE("(%p)->(%p)\n", This, pctinfo);
1426 *pctinfo = 1;
1427 return S_OK;
1430 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1431 LCID lcid, ITypeInfo **ppTInfo)
1433 DispatchEx *This = impl_from_IDispatchEx(iface);
1434 HRESULT hres;
1436 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1438 hres = get_typeinfo(This->info->desc->disp_tid, ppTInfo);
1439 if(FAILED(hres))
1440 return hres;
1442 ITypeInfo_AddRef(*ppTInfo);
1443 return S_OK;
1446 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1447 LPOLESTR *rgszNames, UINT cNames,
1448 LCID lcid, DISPID *rgDispId)
1450 DispatchEx *This = impl_from_IDispatchEx(iface);
1451 UINT i;
1452 HRESULT hres;
1454 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1455 lcid, rgDispId);
1457 for(i=0; i < cNames; i++) {
1458 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1459 if(FAILED(hres))
1460 return hres;
1463 return S_OK;
1466 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1467 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1468 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1470 DispatchEx *This = impl_from_IDispatchEx(iface);
1472 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1473 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1475 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1476 pVarResult, pExcepInfo, NULL);
1479 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1481 DispatchEx *This = impl_from_IDispatchEx(iface);
1482 dynamic_prop_t *dprop;
1483 HRESULT hres;
1485 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1487 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1488 FIXME("Unsupported grfdex %x\n", grfdex);
1490 if(!ensure_real_info(This))
1491 return E_OUTOFMEMORY;
1493 hres = get_builtin_id(This, bstrName, grfdex, pid);
1494 if(hres != DISP_E_UNKNOWNNAME)
1495 return hres;
1497 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1498 if(FAILED(hres))
1499 return hres;
1501 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1502 return S_OK;
1505 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1506 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1508 DispatchEx *This = impl_from_IDispatchEx(iface);
1509 HRESULT hres;
1511 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1513 if(!ensure_real_info(This))
1514 return E_OUTOFMEMORY;
1516 if(wFlags == (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF))
1517 wFlags = DISPATCH_PROPERTYPUT;
1519 switch(get_dispid_type(id)) {
1520 case DISPEXPROP_CUSTOM:
1521 if(!This->info->desc->vtbl || !This->info->desc->vtbl->invoke)
1522 return DISP_E_UNKNOWNNAME;
1523 return This->info->desc->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1525 case DISPEXPROP_DYNAMIC: {
1526 DWORD idx = id - DISPID_DYNPROP_0;
1527 dynamic_prop_t *prop;
1529 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1530 return DISP_E_UNKNOWNNAME;
1532 prop = This->dynamic_data->props+idx;
1534 switch(wFlags) {
1535 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1536 if(!pvarRes)
1537 return E_INVALIDARG;
1538 /* fall through */
1539 case DISPATCH_METHOD:
1540 if(V_VT(&prop->var) != VT_DISPATCH) {
1541 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1542 return E_NOTIMPL;
1545 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1546 case DISPATCH_PROPERTYGET:
1547 if(prop->flags & DYNPROP_DELETED)
1548 return DISP_E_UNKNOWNNAME;
1549 V_VT(pvarRes) = VT_EMPTY;
1550 return variant_copy(pvarRes, &prop->var);
1551 case DISPATCH_PROPERTYPUT:
1552 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1553 || pdp->cNamedArgs > 1) {
1554 FIXME("invalid args\n");
1555 return E_INVALIDARG;
1558 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1559 VariantClear(&prop->var);
1560 hres = variant_copy(&prop->var, pdp->rgvarg);
1561 if(FAILED(hres))
1562 return hres;
1564 prop->flags &= ~DYNPROP_DELETED;
1565 return S_OK;
1566 default:
1567 FIXME("unhandled wFlags %x\n", wFlags);
1568 return E_NOTIMPL;
1571 case DISPEXPROP_BUILTIN:
1572 if(wFlags == DISPATCH_CONSTRUCT) {
1573 if(id == DISPID_VALUE) {
1574 if(This->info->desc->vtbl && This->info->desc->vtbl->value) {
1575 return This->info->desc->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1577 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1578 return E_FAIL;
1580 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1581 return E_FAIL;
1584 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1585 default:
1586 assert(0);
1587 return E_FAIL;
1591 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1593 DispatchEx *This = impl_from_IDispatchEx(iface);
1595 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1597 /* Not implemented by IE */
1598 return E_NOTIMPL;
1601 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1603 DispatchEx *This = impl_from_IDispatchEx(iface);
1605 TRACE("(%p)->(%x)\n", This, id);
1607 /* Not implemented by IE */
1608 return E_NOTIMPL;
1611 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1613 DispatchEx *This = impl_from_IDispatchEx(iface);
1614 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1615 return E_NOTIMPL;
1618 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1620 DispatchEx *This = impl_from_IDispatchEx(iface);
1621 func_info_t *func;
1622 HRESULT hres;
1624 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1626 if(!ensure_real_info(This))
1627 return E_OUTOFMEMORY;
1629 if(is_dynamic_dispid(id)) {
1630 DWORD idx = id - DISPID_DYNPROP_0;
1632 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1633 return DISP_E_UNKNOWNNAME;
1635 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1636 if(!*pbstrName)
1637 return E_OUTOFMEMORY;
1639 return S_OK;
1642 hres = get_builtin_func(This->info, id, &func);
1643 if(FAILED(hres))
1644 return hres;
1646 *pbstrName = SysAllocString(func->name);
1647 if(!*pbstrName)
1648 return E_OUTOFMEMORY;
1649 return S_OK;
1652 static HRESULT next_dynamic_id(DispatchEx *dispex, DWORD idx, DISPID *ret_id)
1654 while(idx < dispex->dynamic_data->prop_cnt && dispex->dynamic_data->props[idx].flags & DYNPROP_DELETED)
1655 idx++;
1657 if(idx == dispex->dynamic_data->prop_cnt) {
1658 *ret_id = DISPID_STARTENUM;
1659 return S_FALSE;
1662 *ret_id = DISPID_DYNPROP_0+idx;
1663 return S_OK;
1666 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1668 DispatchEx *This = impl_from_IDispatchEx(iface);
1669 func_info_t *func;
1670 HRESULT hres;
1672 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1674 if(!ensure_real_info(This))
1675 return E_OUTOFMEMORY;
1677 if(is_dynamic_dispid(id)) {
1678 DWORD idx = id - DISPID_DYNPROP_0;
1680 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1681 return DISP_E_UNKNOWNNAME;
1683 return next_dynamic_id(This, idx+1, pid);
1686 if(id == DISPID_STARTENUM) {
1687 func = This->info->funcs;
1688 }else {
1689 hres = get_builtin_func(This->info, id, &func);
1690 if(FAILED(hres))
1691 return hres;
1692 func++;
1695 while(func < This->info->funcs + This->info->func_cnt) {
1696 /* FIXME: Skip hidden properties */
1697 if(func->func_disp_idx == -1) {
1698 *pid = func->id;
1699 return S_OK;
1701 func++;
1704 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt)
1705 return next_dynamic_id(This, 0, pid);
1707 *pid = DISPID_STARTENUM;
1708 return S_FALSE;
1711 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1713 DispatchEx *This = impl_from_IDispatchEx(iface);
1714 FIXME("(%p)->(%p)\n", This, ppunk);
1715 return E_NOTIMPL;
1718 static IDispatchExVtbl DispatchExVtbl = {
1719 DispatchEx_QueryInterface,
1720 DispatchEx_AddRef,
1721 DispatchEx_Release,
1722 DispatchEx_GetTypeInfoCount,
1723 DispatchEx_GetTypeInfo,
1724 DispatchEx_GetIDsOfNames,
1725 DispatchEx_Invoke,
1726 DispatchEx_GetDispID,
1727 DispatchEx_InvokeEx,
1728 DispatchEx_DeleteMemberByName,
1729 DispatchEx_DeleteMemberByDispID,
1730 DispatchEx_GetMemberProperties,
1731 DispatchEx_GetMemberName,
1732 DispatchEx_GetNextDispID,
1733 DispatchEx_GetNameSpaceParent
1736 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1738 if(IsEqualGUID(&IID_IDispatch, riid))
1739 *ppv = &This->IDispatchEx_iface;
1740 else if(IsEqualGUID(&IID_IDispatchEx, riid))
1741 *ppv = &This->IDispatchEx_iface;
1742 else if(IsEqualGUID(&IID_IDispatchJS, riid))
1743 *ppv = NULL;
1744 else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid))
1745 *ppv = NULL;
1746 else if(IsEqualGUID(&IID_IMarshal, riid))
1747 *ppv = NULL;
1748 else if(IsEqualGUID(&IID_IManagedObject, riid))
1749 *ppv = NULL;
1750 else
1751 return FALSE;
1753 if(*ppv)
1754 IUnknown_AddRef((IUnknown*)*ppv);
1755 return TRUE;
1758 void dispex_traverse(DispatchEx *This, nsCycleCollectionTraversalCallback *cb)
1760 dynamic_prop_t *prop;
1762 if(!This->dynamic_data)
1763 return;
1765 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1766 if(V_VT(&prop->var) == VT_DISPATCH)
1767 note_cc_edge((nsISupports*)V_DISPATCH(&prop->var), "dispex_data", cb);
1770 /* FIXME: Traverse func_disps */
1773 void dispex_unlink(DispatchEx *This)
1775 dynamic_prop_t *prop;
1777 if(!This->dynamic_data)
1778 return;
1780 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1781 if(V_VT(&prop->var) == VT_DISPATCH) {
1782 V_VT(&prop->var) = VT_EMPTY;
1783 IDispatch_Release(V_DISPATCH(&prop->var));
1784 }else {
1785 VariantClear(&prop->var);
1790 const void *dispex_get_vtbl(DispatchEx *dispex)
1792 return dispex->info->desc->vtbl;
1795 void release_dispex(DispatchEx *This)
1797 dynamic_prop_t *prop;
1799 if(!This->dynamic_data)
1800 return;
1802 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1803 VariantClear(&prop->var);
1804 heap_free(prop->name);
1807 heap_free(This->dynamic_data->props);
1809 if(This->dynamic_data->func_disps) {
1810 func_obj_entry_t *iter;
1812 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps + This->info->func_disp_cnt; iter++) {
1813 if(iter->func_obj) {
1814 iter->func_obj->obj = NULL;
1815 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
1817 VariantClear(&iter->val);
1820 heap_free(This->dynamic_data->func_disps);
1823 heap_free(This->dynamic_data);
1826 void init_dispex_with_compat_mode(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data, compat_mode_t compat_mode)
1828 assert(compat_mode < COMPAT_MODE_CNT);
1830 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1831 dispex->outer = outer;
1832 dispex->dynamic_data = NULL;
1834 if(data->vtbl && data->vtbl->get_compat_mode) {
1835 /* delayed init */
1836 if(!data->delayed_init_info) {
1837 EnterCriticalSection(&cs_dispex_static_data);
1838 if(!data->delayed_init_info) {
1839 dispex_data_t *info = heap_alloc_zero(sizeof(*data->delayed_init_info));
1840 if(info) {
1841 info->desc = data;
1842 data->delayed_init_info = info;
1845 LeaveCriticalSection(&cs_dispex_static_data);
1847 dispex->info = data->delayed_init_info;
1848 }else {
1849 dispex->info = ensure_dispex_info(data, compat_mode);