msvcrt: _Gettnames() should respect user overrides.
[wine.git] / dlls / mshtml / dispex.c
bloba46a0cdb06e601db72aa047b990b96e8d8542011
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;
68 DWORD func_cnt;
69 DWORD func_size;
70 func_info_t *funcs;
71 func_info_t **name_table;
72 DWORD func_disp_cnt;
74 struct list entry;
77 typedef struct {
78 VARIANT var;
79 LPWSTR name;
80 DWORD flags;
81 } dynamic_prop_t;
83 #define DYNPROP_DELETED 0x01
85 typedef struct {
86 DispatchEx dispex;
87 IUnknown IUnknown_iface;
88 LONG ref;
89 DispatchEx *obj;
90 func_info_t *info;
91 } func_disp_t;
93 typedef struct {
94 func_disp_t *func_obj;
95 VARIANT val;
96 } func_obj_entry_t;
98 struct dispex_dynamic_data_t {
99 DWORD buf_size;
100 DWORD prop_cnt;
101 dynamic_prop_t *props;
102 func_obj_entry_t *func_disps;
105 #define DISPID_DYNPROP_0 0x50000000
106 #define DISPID_DYNPROP_MAX 0x5fffffff
108 #define FDEX_VERSION_MASK 0xf0000000
110 static ITypeLib *typelib;
111 static ITypeInfo *typeinfos[LAST_tid];
112 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
114 static REFIID tid_ids[] = {
115 #define XIID(iface) &IID_ ## iface,
116 #define XDIID(iface) &DIID_ ## iface,
117 TID_LIST
118 #undef XIID
119 #undef XDIID
122 static HRESULT load_typelib(void)
124 HRESULT hres;
125 ITypeLib *tl;
127 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
128 if(FAILED(hres)) {
129 ERR("LoadRegTypeLib failed: %08x\n", hres);
130 return hres;
133 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
134 ITypeLib_Release(tl);
135 return hres;
138 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
140 HRESULT hres;
142 if (!typelib)
143 hres = load_typelib();
144 if (!typelib)
145 return hres;
147 if(!typeinfos[tid]) {
148 ITypeInfo *ti;
150 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
151 if(FAILED(hres)) {
152 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_mshtml_guid(tid_ids[tid]), hres);
153 return hres;
156 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
157 ITypeInfo_Release(ti);
160 *typeinfo = typeinfos[tid];
161 return S_OK;
164 void release_typelib(void)
166 dispex_data_t *iter;
167 unsigned i;
169 while(!list_empty(&dispex_data_list)) {
170 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
171 list_remove(&iter->entry);
173 for(i=0; i < iter->func_cnt; i++)
174 SysFreeString(iter->funcs[i].name);
176 heap_free(iter->funcs);
177 heap_free(iter->name_table);
178 heap_free(iter);
181 if(!typelib)
182 return;
184 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
185 if(typeinfos[i])
186 ITypeInfo_Release(typeinfos[i]);
188 ITypeLib_Release(typelib);
189 DeleteCriticalSection(&cs_dispex_static_data);
192 HRESULT get_class_typeinfo(const CLSID *clsid, ITypeInfo **typeinfo)
194 HRESULT hres;
196 if (!typelib)
197 hres = load_typelib();
198 if (!typelib)
199 return hres;
201 hres = ITypeLib_GetTypeInfoOfGuid(typelib, clsid, typeinfo);
202 if(FAILED(hres))
203 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
204 return hres;
207 /* Not all argument types are supported yet */
208 #define BUILTIN_ARG_TYPES_SWITCH \
209 CASE_VT(VT_I2, INT16, V_I2); \
210 CASE_VT(VT_UI2, UINT16, V_UI2); \
211 CASE_VT(VT_I4, INT32, V_I4); \
212 CASE_VT(VT_R4, float, V_R4); \
213 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
214 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
216 /* List all types used by IDispatchEx-based properties */
217 #define BUILTIN_TYPES_SWITCH \
218 BUILTIN_ARG_TYPES_SWITCH; \
219 CASE_VT(VT_VARIANT, VARIANT, *); \
220 CASE_VT(VT_PTR, void*, V_BYREF); \
221 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
222 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH); \
223 CASE_VT(VT_UI8, ULONGLONG, V_UI8)
225 static BOOL is_arg_type_supported(VARTYPE vt)
227 switch(vt) {
228 #define CASE_VT(x,a,b) case x: return TRUE
229 BUILTIN_ARG_TYPES_SWITCH;
230 #undef CASE_VT
232 return FALSE;
235 static void add_func_info(dispex_data_t *data, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti,
236 dispex_hook_invoke_t hook)
238 func_info_t *info;
239 BSTR name;
240 HRESULT hres;
242 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &name, NULL, NULL, NULL);
243 if(FAILED(hres))
244 return;
246 for(info = data->funcs; info < data->funcs+data->func_cnt; info++) {
247 if(info->id == desc->memid || !strcmpW(info->name, name)) {
248 if(info->tid != tid) {
249 SysFreeString(name);
250 return; /* Duplicated in other interface */
252 break;
256 if(info == data->funcs+data->func_cnt) {
257 if(data->func_cnt == data->func_size)
258 data->funcs = heap_realloc_zero(data->funcs, (data->func_size <<= 1)*sizeof(func_info_t));
259 info = data->funcs+data->func_cnt;
261 data->func_cnt++;
263 info->id = desc->memid;
264 info->name = name;
265 info->tid = tid;
266 info->func_disp_idx = -1;
267 info->prop_vt = VT_EMPTY;
268 info->hook = hook;
269 }else {
270 SysFreeString(name);
273 if(desc->invkind & DISPATCH_METHOD) {
274 unsigned i;
276 info->func_disp_idx = data->func_disp_cnt++;
277 info->argc = desc->cParams;
279 assert(info->argc < MAX_ARGS);
280 assert(desc->funckind == FUNC_DISPATCH);
282 info->arg_types = heap_alloc(sizeof(*info->arg_types) * info->argc);
283 if(!info->arg_types)
284 return; /* FIXME: real error instead of fallback */
286 for(i=0; i < info->argc; i++)
287 info->arg_types[i] = desc->lprgelemdescParam[i].tdesc.vt;
289 info->prop_vt = desc->elemdescFunc.tdesc.vt;
290 if(info->prop_vt != VT_VOID && !is_arg_type_supported(info->prop_vt)) {
291 TRACE("%s: return type %d\n", debugstr_w(info->name), info->prop_vt);
292 return; /* Fallback to ITypeInfo::Invoke */
295 if(desc->cParamsOpt) {
296 TRACE("%s: optional params\n", debugstr_w(info->name));
297 return; /* Fallback to ITypeInfo::Invoke */
300 for(i=0; i < info->argc; i++) {
301 if(!is_arg_type_supported(info->arg_types[i])) {
302 return; /* Fallback to ITypeInfo for unsupported arg types */
305 if(desc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
306 TRACE("%s param %d: default value\n", debugstr_w(info->name), i);
307 return; /* Fallback to ITypeInfo::Invoke */
311 assert(info->argc <= MAX_ARGS);
312 assert(desc->callconv == CC_STDCALL);
314 info->call_vtbl_off = desc->oVft/sizeof(void*);
315 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
316 VARTYPE vt = VT_EMPTY;
318 if(desc->invkind & DISPATCH_PROPERTYGET) {
319 vt = desc->elemdescFunc.tdesc.vt;
320 info->get_vtbl_off = desc->oVft/sizeof(void*);
322 if(desc->invkind & DISPATCH_PROPERTYPUT) {
323 assert(desc->cParams == 1);
324 vt = desc->lprgelemdescParam->tdesc.vt;
325 info->put_vtbl_off = desc->oVft/sizeof(void*);
328 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
329 info->prop_vt = vt;
333 static HRESULT process_interface(dispex_data_t *data, tid_t tid, ITypeInfo *disp_typeinfo, const dispex_hook_t *hooks)
335 unsigned i = 7; /* skip IDispatch functions */
336 ITypeInfo *typeinfo;
337 FUNCDESC *funcdesc;
338 HRESULT hres;
340 hres = get_typeinfo(tid, &typeinfo);
341 if(FAILED(hres))
342 return hres;
344 while(1) {
345 const dispex_hook_t *hook = NULL;
347 hres = ITypeInfo_GetFuncDesc(typeinfo, i++, &funcdesc);
348 if(FAILED(hres))
349 break;
351 if(hooks) {
352 for(hook = hooks; hook->dispid != DISPID_UNKNOWN; hook++) {
353 if(hook->dispid == funcdesc->memid)
354 break;
356 if(hook->dispid == DISPID_UNKNOWN)
357 hook = NULL;
360 if(!hook || hook->invoke) {
361 TRACE("adding...\n");
362 add_func_info(data, tid, funcdesc, disp_typeinfo ? disp_typeinfo : typeinfo,
363 hook ? hook->invoke : NULL);
366 ITypeInfo_ReleaseFuncDesc(typeinfo, funcdesc);
369 return S_OK;
372 void dispex_info_add_interface(dispex_data_t *info, tid_t tid, const dispex_hook_t *hooks)
374 HRESULT hres;
376 hres = process_interface(info, tid, NULL, hooks);
377 if(FAILED(hres))
378 ERR("process_interface failed: %08x\n", hres);
381 static int dispid_cmp(const void *p1, const void *p2)
383 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
386 static int func_name_cmp(const void *p1, const void *p2)
388 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
391 static dispex_data_t *preprocess_dispex_data(dispex_static_data_t *desc, compat_mode_t compat_mode)
393 const tid_t *tid;
394 dispex_data_t *data;
395 DWORD i;
396 ITypeInfo *dti;
397 HRESULT hres;
399 if(desc->disp_tid) {
400 hres = get_typeinfo(desc->disp_tid, &dti);
401 if(FAILED(hres)) {
402 ERR("Could not get disp type info: %08x\n", hres);
403 return NULL;
407 data = heap_alloc(sizeof(dispex_data_t));
408 if (!data) {
409 ERR("Out of memory\n");
410 return NULL;
412 data->desc = desc;
413 data->func_cnt = 0;
414 data->func_disp_cnt = 0;
415 data->func_size = 16;
416 data->funcs = heap_alloc_zero(data->func_size*sizeof(func_info_t));
417 if (!data->funcs) {
418 heap_free (data);
419 ERR("Out of memory\n");
420 return NULL;
422 list_add_tail(&dispex_data_list, &data->entry);
424 if(desc->init_info)
425 desc->init_info(data, compat_mode);
427 for(tid = desc->iface_tids; *tid; tid++) {
428 hres = process_interface(data, *tid, dti, NULL);
429 if(FAILED(hres))
430 break;
433 if(!data->func_cnt) {
434 heap_free(data->funcs);
435 data->name_table = NULL;
436 data->funcs = NULL;
437 data->func_size = 0;
438 return data;
442 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
443 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
445 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
446 for(i=0; i < data->func_cnt; i++)
447 data->name_table[i] = data->funcs+i;
448 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
449 return data;
452 static int id_cmp(const void *p1, const void *p2)
454 return *(const DISPID*)p1 - *(const DISPID*)p2;
457 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
459 unsigned i, func_cnt;
460 FUNCDESC *funcdesc;
461 ITypeInfo *ti;
462 TYPEATTR *attr;
463 DISPID *ids;
464 HRESULT hres;
466 hres = get_typeinfo(tid, &ti);
467 if(FAILED(hres))
468 return hres;
470 hres = ITypeInfo_GetTypeAttr(ti, &attr);
471 if(FAILED(hres)) {
472 ITypeInfo_Release(ti);
473 return hres;
476 func_cnt = attr->cFuncs;
477 ITypeInfo_ReleaseTypeAttr(ti, attr);
479 ids = heap_alloc(func_cnt*sizeof(DISPID));
480 if(!ids) {
481 ITypeInfo_Release(ti);
482 return E_OUTOFMEMORY;
485 for(i=0; i < func_cnt; i++) {
486 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
487 if(FAILED(hres))
488 break;
490 ids[i] = funcdesc->memid;
491 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
494 ITypeInfo_Release(ti);
495 if(FAILED(hres)) {
496 heap_free(ids);
497 return hres;
500 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
502 *ret_size = func_cnt;
503 *ret = ids;
504 return S_OK;
507 static inline BOOL is_custom_dispid(DISPID id)
509 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
512 static inline BOOL is_dynamic_dispid(DISPID id)
514 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
517 dispex_prop_type_t get_dispid_type(DISPID id)
519 if(is_dynamic_dispid(id))
520 return DISPEXPROP_DYNAMIC;
521 if(is_custom_dispid(id))
522 return DISPEXPROP_CUSTOM;
523 return DISPEXPROP_BUILTIN;
526 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
528 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
529 V_VT(dest) = VT_BSTR;
530 V_BSTR(dest) = NULL;
531 return S_OK;
534 return VariantCopy(dest, src);
537 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
539 if(This->dynamic_data)
540 return This->dynamic_data;
542 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
543 if(!This->dynamic_data)
544 return NULL;
546 if(This->info->desc->vtbl && This->info->desc->vtbl->populate_props)
547 This->info->desc->vtbl->populate_props(This);
549 return This->dynamic_data;
552 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
554 const BOOL alloc = flags & fdexNameEnsure;
555 dispex_dynamic_data_t *data;
556 dynamic_prop_t *prop;
558 data = get_dynamic_data(This);
559 if(!data)
560 return E_OUTOFMEMORY;
562 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
563 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
564 if(prop->flags & DYNPROP_DELETED) {
565 if(!alloc)
566 return DISP_E_UNKNOWNNAME;
567 prop->flags &= ~DYNPROP_DELETED;
569 *ret = prop;
570 return S_OK;
574 if(!alloc)
575 return DISP_E_UNKNOWNNAME;
577 TRACE("creating dynamic prop %s\n", debugstr_w(name));
579 if(!data->buf_size) {
580 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
581 if(!data->props)
582 return E_OUTOFMEMORY;
583 data->buf_size = 4;
584 }else if(data->buf_size == data->prop_cnt) {
585 dynamic_prop_t *new_props;
587 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
588 if(!new_props)
589 return E_OUTOFMEMORY;
591 data->props = new_props;
592 data->buf_size <<= 1;
595 prop = data->props + data->prop_cnt;
597 prop->name = heap_strdupW(name);
598 if(!prop->name)
599 return E_OUTOFMEMORY;
601 VariantInit(&prop->var);
602 prop->flags = 0;
603 data->prop_cnt++;
604 *ret = prop;
605 return S_OK;
608 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
610 dynamic_prop_t *prop;
611 HRESULT hres;
613 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
614 if(FAILED(hres))
615 return hres;
617 *ret = &prop->var;
618 return S_OK;
621 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
623 dynamic_prop_t *prop;
624 HRESULT hres;
626 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
627 if(FAILED(hres))
628 return hres;
630 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
631 return S_OK;
634 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
635 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
637 if(This->info->desc->vtbl && This->info->desc->vtbl->value)
638 return This->info->desc->vtbl->value(This, lcid, flags, params, res, ei, caller);
640 switch(flags) {
641 case DISPATCH_PROPERTYGET:
642 V_VT(res) = VT_BSTR;
643 V_BSTR(res) = SysAllocString(objectW);
644 if(!V_BSTR(res))
645 return E_OUTOFMEMORY;
646 break;
647 default:
648 FIXME("Unimplemented flags %x\n", flags);
649 return E_NOTIMPL;
652 return S_OK;
655 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
656 EXCEPINFO *ei)
658 DISPPARAMS params = {dp->rgvarg+dp->cNamedArgs, NULL, dp->cArgs-dp->cNamedArgs, 0};
659 ITypeInfo *ti;
660 IUnknown *unk;
661 UINT argerr=0;
662 HRESULT hres;
664 hres = get_typeinfo(func->tid, &ti);
665 if(FAILED(hres)) {
666 ERR("Could not get type info: %08x\n", hres);
667 return hres;
670 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
671 if(FAILED(hres)) {
672 ERR("Could not get iface %s: %08x\n", debugstr_mshtml_guid(tid_ids[func->tid]), hres);
673 return E_FAIL;
676 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, &params, res, ei, &argerr);
678 IUnknown_Release(unk);
679 return hres;
682 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
684 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
687 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
689 func_disp_t *This = impl_from_IUnknown(iface);
691 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
693 if(IsEqualGUID(&IID_IUnknown, riid)) {
694 *ppv = &This->IUnknown_iface;
695 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
696 return *ppv ? S_OK : E_NOINTERFACE;
697 }else {
698 *ppv = NULL;
699 return E_NOINTERFACE;
702 IUnknown_AddRef((IUnknown*)*ppv);
703 return S_OK;
706 static ULONG WINAPI Function_AddRef(IUnknown *iface)
708 func_disp_t *This = impl_from_IUnknown(iface);
709 LONG ref = InterlockedIncrement(&This->ref);
711 TRACE("(%p) ref=%d\n", This, ref);
713 return ref;
716 static ULONG WINAPI Function_Release(IUnknown *iface)
718 func_disp_t *This = impl_from_IUnknown(iface);
719 LONG ref = InterlockedDecrement(&This->ref);
721 TRACE("(%p) ref=%d\n", This, ref);
723 if(!ref) {
724 assert(!This->obj);
725 release_dispex(&This->dispex);
726 heap_free(This);
729 return ref;
732 static const IUnknownVtbl FunctionUnkVtbl = {
733 Function_QueryInterface,
734 Function_AddRef,
735 Function_Release
738 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
740 return CONTAINING_RECORD(iface, func_disp_t, dispex);
743 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
744 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
746 func_disp_t *This = impl_from_DispatchEx(dispex);
747 HRESULT hres;
749 switch(flags) {
750 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
751 if(!res)
752 return E_INVALIDARG;
753 /* fall through */
754 case DISPATCH_METHOD:
755 if(!This->obj)
756 return E_UNEXPECTED;
757 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
758 break;
759 case DISPATCH_PROPERTYGET: {
760 unsigned name_len;
761 WCHAR *ptr;
762 BSTR str;
764 static const WCHAR func_prefixW[] =
765 {'\n','f','u','n','c','t','i','o','n',' '};
766 static const WCHAR func_suffixW[] =
767 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
769 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
770 if(!caller)
771 return E_ACCESSDENIED;
773 name_len = SysStringLen(This->info->name);
774 ptr = str = SysAllocStringLen(NULL, name_len + (sizeof(func_prefixW)+sizeof(func_suffixW))/sizeof(WCHAR));
775 if(!str)
776 return E_OUTOFMEMORY;
778 memcpy(ptr, func_prefixW, sizeof(func_prefixW));
779 ptr += sizeof(func_prefixW)/sizeof(WCHAR);
781 memcpy(ptr, This->info->name, name_len*sizeof(WCHAR));
782 ptr += name_len;
784 memcpy(ptr, func_suffixW, sizeof(func_suffixW));
786 V_VT(res) = VT_BSTR;
787 V_BSTR(res) = str;
788 return S_OK;
790 default:
791 FIXME("Unimplemented flags %x\n", flags);
792 hres = E_NOTIMPL;
795 return hres;
798 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
799 function_value,
800 NULL,
801 NULL,
802 NULL
805 static const tid_t function_iface_tids[] = {0};
807 static dispex_static_data_t function_dispex = {
808 &function_dispex_vtbl,
809 NULL_tid,
810 function_iface_tids
813 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
815 func_disp_t *ret;
817 ret = heap_alloc_zero(sizeof(func_disp_t));
818 if(!ret)
819 return NULL;
821 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
822 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
823 ret->ref = 1;
824 ret->obj = obj;
825 ret->info = info;
827 return ret;
830 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
831 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
833 DISPID named_arg = DISPID_THIS;
834 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
835 IDispatchEx *dispex;
836 HRESULT hres;
838 if(dp->cNamedArgs) {
839 FIXME("named args not supported\n");
840 return E_NOTIMPL;
843 new_dp.rgvarg = heap_alloc((dp->cArgs+1)*sizeof(VARIANTARG));
844 if(!new_dp.rgvarg)
845 return E_OUTOFMEMORY;
847 new_dp.cArgs = dp->cArgs+1;
848 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
850 V_VT(new_dp.rgvarg) = VT_DISPATCH;
851 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
853 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
854 TRACE(">>>\n");
855 if(SUCCEEDED(hres)) {
856 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
857 IDispatchEx_Release(dispex);
858 }else {
859 ULONG err = 0;
860 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
862 if(SUCCEEDED(hres))
863 TRACE("<<< %s\n", debugstr_variant(res));
864 else
865 WARN("<<< %08x\n", hres);
867 heap_free(new_dp.rgvarg);
868 return hres;
871 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
873 dispex_dynamic_data_t *dynamic_data;
874 func_obj_entry_t *entry;
876 dynamic_data = get_dynamic_data(This);
877 if(!dynamic_data)
878 return E_OUTOFMEMORY;
880 if(!dynamic_data->func_disps) {
881 dynamic_data->func_disps = heap_alloc_zero(This->info->func_disp_cnt * sizeof(*dynamic_data->func_disps));
882 if(!dynamic_data->func_disps)
883 return E_OUTOFMEMORY;
886 entry = dynamic_data->func_disps + func->func_disp_idx;
887 if(!entry->func_obj) {
888 entry->func_obj = create_func_disp(This, func);
889 if(!entry->func_obj)
890 return E_OUTOFMEMORY;
892 IDispatchEx_AddRef(&entry->func_obj->dispex.IDispatchEx_iface);
893 V_VT(&entry->val) = VT_DISPATCH;
894 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
897 *ret = entry;
898 return S_OK;
901 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
903 int min, max, n;
905 min = 0;
906 max = data->func_cnt-1;
908 while(min <= max) {
909 n = (min+max)/2;
911 if(data->funcs[n].id == id) {
912 *ret = data->funcs+n;
913 return S_OK;
916 if(data->funcs[n].id < id)
917 min = n+1;
918 else
919 max = n-1;
922 WARN("invalid id %x\n", id);
923 return DISP_E_UNKNOWNNAME;
926 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
928 int min, max, n, c;
930 min = 0;
931 max = This->info->func_cnt-1;
933 while(min <= max) {
934 n = (min+max)/2;
936 c = strcmpiW(This->info->name_table[n]->name, name);
937 if(!c) {
938 if((grfdex & fdexNameCaseSensitive) && strcmpW(This->info->name_table[n]->name, name))
939 break;
941 *ret = This->info->name_table[n]->id;
942 return S_OK;
945 if(c > 0)
946 max = n-1;
947 else
948 min = n+1;
951 if(This->info->desc->vtbl && This->info->desc->vtbl->get_dispid) {
952 HRESULT hres;
954 hres = This->info->desc->vtbl->get_dispid(This, name, grfdex, ret);
955 if(hres != DISP_E_UNKNOWNNAME)
956 return hres;
959 return DISP_E_UNKNOWNNAME;
962 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
964 V_VT(dst) = VT_EMPTY;
966 if(caller) {
967 IVariantChangeType *change_type = NULL;
968 HRESULT hres;
970 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
971 if(SUCCEEDED(hres)) {
972 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
973 IVariantChangeType_Release(change_type);
974 return hres;
978 switch(vt) {
979 case VT_BOOL:
980 if(V_VT(src) == VT_BSTR) {
981 V_VT(dst) = VT_BOOL;
982 V_BOOL(dst) = variant_bool(V_BSTR(src) && *V_BSTR(src));
983 return S_OK;
985 break;
988 return VariantChangeType(dst, src, 0, vt);
991 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
993 IUnknown *iface;
994 HRESULT hres;
996 if(dp && dp->cArgs) {
997 FIXME("cArgs %d\n", dp->cArgs);
998 return E_NOTIMPL;
1001 assert(func->get_vtbl_off);
1003 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1004 if(SUCCEEDED(hres)) {
1005 switch(func->prop_vt) {
1006 #define CASE_VT(vt,type,access) \
1007 case vt: { \
1008 type val; \
1009 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
1010 if(SUCCEEDED(hres)) \
1011 access(res) = val; \
1013 break
1014 BUILTIN_TYPES_SWITCH;
1015 #undef CASE_VT
1016 default:
1017 FIXME("Unhandled vt %d\n", func->prop_vt);
1018 hres = E_NOTIMPL;
1020 IUnknown_Release(iface);
1023 if(FAILED(hres))
1024 return hres;
1026 if(func->prop_vt != VT_VARIANT)
1027 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
1028 return S_OK;
1031 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
1033 VARIANT *v, tmpv;
1034 IUnknown *iface;
1035 HRESULT hres;
1037 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1038 || dp->cNamedArgs > 1) {
1039 FIXME("invalid args\n");
1040 return E_INVALIDARG;
1043 if(!func->put_vtbl_off) {
1044 FIXME("No setter\n");
1045 return E_FAIL;
1048 v = dp->rgvarg;
1049 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
1050 hres = change_type(&tmpv, v, func->prop_vt, caller);
1051 if(FAILED(hres))
1052 return hres;
1053 v = &tmpv;
1056 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1057 if(SUCCEEDED(hres)) {
1058 switch(func->prop_vt) {
1059 #define CASE_VT(vt,type,access) \
1060 case vt: \
1061 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1062 break
1063 BUILTIN_TYPES_SWITCH;
1064 #undef CASE_VT
1065 default:
1066 FIXME("Unimplemented vt %d\n", func->prop_vt);
1067 hres = E_NOTIMPL;
1070 IUnknown_Release(iface);
1073 if(v == &tmpv)
1074 VariantClear(v);
1075 return hres;
1078 static HRESULT invoke_builtin_function(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res, IServiceProvider *caller)
1080 VARIANT arg_buf[MAX_ARGS], *arg_ptrs[MAX_ARGS], *arg, retv, ret_ref, vhres;
1081 unsigned i, nconv = 0;
1082 IUnknown *iface;
1083 HRESULT hres;
1085 if(dp->cNamedArgs) {
1086 FIXME("Named arguments not supported\n");
1087 return E_NOTIMPL;
1090 if(dp->cArgs != func->argc) {
1091 FIXME("Invalid argument count (expected %u, got %u)\n", func->argc, dp->cArgs);
1092 return E_INVALIDARG;
1095 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1096 if(FAILED(hres))
1097 return hres;
1099 for(i=0; i < func->argc; i++) {
1100 arg = dp->rgvarg+dp->cArgs-i-1;
1101 if(func->arg_types[i] == V_VT(arg)) {
1102 arg_ptrs[i] = arg;
1103 }else {
1104 hres = change_type(arg_buf+nconv, arg, func->arg_types[i], caller);
1105 if(FAILED(hres))
1106 break;
1107 arg_ptrs[i] = arg_buf + nconv++;
1111 if(SUCCEEDED(hres)) {
1112 if(func->prop_vt == VT_VOID) {
1113 V_VT(&retv) = VT_EMPTY;
1114 }else {
1115 V_VT(&retv) = func->prop_vt;
1116 arg_ptrs[func->argc] = &ret_ref;
1117 V_VT(&ret_ref) = VT_BYREF|func->prop_vt;
1119 switch(func->prop_vt) {
1120 #define CASE_VT(vt,type,access) \
1121 case vt: \
1122 V_BYREF(&ret_ref) = &access(&retv); \
1123 break
1124 BUILTIN_TYPES_SWITCH;
1125 #undef CASE_VT
1126 default:
1127 assert(0);
1131 V_VT(&vhres) = VT_ERROR;
1132 hres = DispCallFunc(iface, func->call_vtbl_off*sizeof(void*), CC_STDCALL, VT_ERROR,
1133 func->argc + (func->prop_vt == VT_VOID ? 0 : 1), func->arg_types, arg_ptrs, &vhres);
1136 while(nconv--)
1137 VariantClear(arg_buf+nconv);
1138 IUnknown_Release(iface);
1139 if(FAILED(hres))
1140 return hres;
1141 if(FAILED(V_ERROR(&vhres)))
1142 return V_ERROR(&vhres);
1144 if(res)
1145 *res = retv;
1146 else
1147 VariantClear(&retv);
1148 return V_ERROR(&vhres);
1151 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
1152 EXCEPINFO *ei, IServiceProvider *caller)
1154 HRESULT hres;
1156 switch(flags) {
1157 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1158 if(!res)
1159 return E_INVALIDARG;
1160 /* fall through */
1161 case DISPATCH_METHOD:
1162 if(This->dynamic_data && This->dynamic_data->func_disps
1163 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1164 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
1166 if(V_VT(&entry->val) != VT_DISPATCH) {
1167 FIXME("calling %s not supported\n", debugstr_variant(&entry->val));
1168 return E_NOTIMPL;
1171 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != V_DISPATCH(&entry->val)) {
1172 if(!V_DISPATCH(&entry->val)) {
1173 FIXME("Calling null\n");
1174 return E_FAIL;
1177 hres = invoke_disp_value(This, V_DISPATCH(&entry->val), 0, flags, dp, res, ei, NULL);
1178 break;
1182 if(func->call_vtbl_off)
1183 hres = invoke_builtin_function(This, func, dp, res, caller);
1184 else
1185 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1186 break;
1187 case DISPATCH_PROPERTYGET: {
1188 func_obj_entry_t *entry;
1190 if(func->id == DISPID_VALUE) {
1191 BSTR ret;
1193 ret = SysAllocString(objectW);
1194 if(!ret)
1195 return E_OUTOFMEMORY;
1197 V_VT(res) = VT_BSTR;
1198 V_BSTR(res) = ret;
1199 return S_OK;
1202 hres = get_func_obj_entry(This, func, &entry);
1203 if(FAILED(hres))
1204 return hres;
1206 V_VT(res) = VT_EMPTY;
1207 return VariantCopy(res, &entry->val);
1209 case DISPATCH_PROPERTYPUT: {
1210 func_obj_entry_t *entry;
1212 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1213 || dp->cNamedArgs > 1) {
1214 FIXME("invalid args\n");
1215 return E_INVALIDARG;
1219 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1220 * function property using InvokeEx, it's possible to do that from jscript.
1221 * Native probably uses some undocumented interface in this case, but it should
1222 * be fine for us to allow IDispatchEx handle that.
1224 hres = get_func_obj_entry(This, func, &entry);
1225 if(FAILED(hres))
1226 return hres;
1228 return VariantCopy(&entry->val, dp->rgvarg);
1230 default:
1231 FIXME("Unimplemented flags %x\n", flags);
1232 hres = E_NOTIMPL;
1235 return hres;
1238 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1239 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1241 func_info_t *func;
1242 HRESULT hres;
1244 hres = get_builtin_func(This->info, id, &func);
1245 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
1246 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1247 if(FAILED(hres))
1248 return hres;
1250 if(func->hook) {
1251 hres = func->hook(This, lcid, flags, dp, res, ei, caller);
1252 if(hres != S_FALSE)
1253 return hres;
1256 if(func->func_disp_idx != -1)
1257 return function_invoke(This, func, flags, dp, res, ei, caller);
1259 switch(flags) {
1260 case DISPATCH_PROPERTYPUT:
1261 if(res)
1262 V_VT(res) = VT_EMPTY;
1263 hres = builtin_propput(This, func, dp, caller);
1264 break;
1265 case DISPATCH_PROPERTYGET:
1266 hres = builtin_propget(This, func, dp, res);
1267 break;
1268 default:
1269 if(!func->get_vtbl_off) {
1270 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1271 }else {
1272 VARIANT v;
1274 hres = builtin_propget(This, func, NULL, &v);
1275 if(FAILED(hres))
1276 return hres;
1278 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD) || dp->cArgs) {
1279 if(V_VT(&v) != VT_DISPATCH) {
1280 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v), flags);
1281 VariantClear(&v);
1282 return E_FAIL;
1285 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1286 IDispatch_Release(V_DISPATCH(&v));
1287 }else if(res) {
1288 *res = v;
1289 }else {
1290 VariantClear(&v);
1295 return hres;
1298 HRESULT remove_attribute(DispatchEx *This, DISPID id, VARIANT_BOOL *success)
1300 switch(get_dispid_type(id)) {
1301 case DISPEXPROP_CUSTOM:
1302 FIXME("DISPEXPROP_CUSTOM not supported\n");
1303 return E_NOTIMPL;
1305 case DISPEXPROP_DYNAMIC: {
1306 DWORD idx = id - DISPID_DYNPROP_0;
1307 dynamic_prop_t *prop;
1309 prop = This->dynamic_data->props+idx;
1310 VariantClear(&prop->var);
1311 prop->flags |= DYNPROP_DELETED;
1312 *success = VARIANT_TRUE;
1313 return S_OK;
1315 case DISPEXPROP_BUILTIN: {
1316 VARIANT var;
1317 DISPPARAMS dp = {&var,NULL,1,0};
1318 func_info_t *func;
1319 HRESULT hres;
1321 hres = get_builtin_func(This->info, id, &func);
1322 if(FAILED(hres))
1323 return hres;
1325 /* For builtin functions, we set their value to the original function. */
1326 if(func->func_disp_idx != -1) {
1327 func_obj_entry_t *entry;
1329 if(!This->dynamic_data || !This->dynamic_data->func_disps
1330 || !This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1331 *success = VARIANT_FALSE;
1332 return S_OK;
1335 entry = This->dynamic_data->func_disps + func->func_disp_idx;
1336 if(V_VT(&entry->val) == VT_DISPATCH
1337 && V_DISPATCH(&entry->val) == (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface) {
1338 *success = VARIANT_FALSE;
1339 return S_OK;
1342 VariantClear(&entry->val);
1343 V_VT(&entry->val) = VT_DISPATCH;
1344 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
1345 IDispatch_AddRef(V_DISPATCH(&entry->val));
1346 *success = VARIANT_TRUE;
1347 return S_OK;
1350 V_VT(&var) = VT_EMPTY;
1351 hres = builtin_propput(This, func, &dp, NULL);
1352 if(FAILED(hres))
1353 return hres;
1355 *success = VARIANT_TRUE;
1356 return S_OK;
1358 default:
1359 assert(0);
1360 return E_FAIL;
1364 compat_mode_t dispex_compat_mode(DispatchEx *dispex)
1366 return dispex->info->desc->vtbl->get_compat_mode(dispex);
1369 static dispex_data_t *ensure_dispex_info(dispex_static_data_t *desc, compat_mode_t compat_mode)
1371 if(!desc->info_cache[compat_mode]) {
1372 EnterCriticalSection(&cs_dispex_static_data);
1373 if(!desc->info_cache[compat_mode])
1374 desc->info_cache[compat_mode] = preprocess_dispex_data(desc, compat_mode);
1375 LeaveCriticalSection(&cs_dispex_static_data);
1377 return desc->info_cache[compat_mode];
1380 static BOOL ensure_real_info(DispatchEx *dispex)
1382 if(dispex->info != dispex->info->desc->delayed_init_info)
1383 return TRUE;
1385 dispex->info = ensure_dispex_info(dispex->info->desc, dispex_compat_mode(dispex));
1386 return dispex->info != NULL;
1389 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1391 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1394 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1396 DispatchEx *This = impl_from_IDispatchEx(iface);
1398 return IUnknown_QueryInterface(This->outer, riid, ppv);
1401 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1403 DispatchEx *This = impl_from_IDispatchEx(iface);
1405 return IUnknown_AddRef(This->outer);
1408 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1410 DispatchEx *This = impl_from_IDispatchEx(iface);
1412 return IUnknown_Release(This->outer);
1415 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1417 DispatchEx *This = impl_from_IDispatchEx(iface);
1419 TRACE("(%p)->(%p)\n", This, pctinfo);
1421 *pctinfo = 1;
1422 return S_OK;
1425 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1426 LCID lcid, ITypeInfo **ppTInfo)
1428 DispatchEx *This = impl_from_IDispatchEx(iface);
1429 HRESULT hres;
1431 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1433 hres = get_typeinfo(This->info->desc->disp_tid, ppTInfo);
1434 if(FAILED(hres))
1435 return hres;
1437 ITypeInfo_AddRef(*ppTInfo);
1438 return S_OK;
1441 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1442 LPOLESTR *rgszNames, UINT cNames,
1443 LCID lcid, DISPID *rgDispId)
1445 DispatchEx *This = impl_from_IDispatchEx(iface);
1446 UINT i;
1447 HRESULT hres;
1449 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1450 lcid, rgDispId);
1452 for(i=0; i < cNames; i++) {
1453 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1454 if(FAILED(hres))
1455 return hres;
1458 return S_OK;
1461 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1462 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1463 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1465 DispatchEx *This = impl_from_IDispatchEx(iface);
1467 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1468 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1470 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1471 pVarResult, pExcepInfo, NULL);
1474 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1476 DispatchEx *This = impl_from_IDispatchEx(iface);
1477 dynamic_prop_t *dprop;
1478 HRESULT hres;
1480 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1482 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1483 FIXME("Unsupported grfdex %x\n", grfdex);
1485 if(!ensure_real_info(This))
1486 return E_OUTOFMEMORY;
1488 hres = get_builtin_id(This, bstrName, grfdex, pid);
1489 if(hres != DISP_E_UNKNOWNNAME)
1490 return hres;
1492 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1493 if(FAILED(hres))
1494 return hres;
1496 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1497 return S_OK;
1500 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1501 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1503 DispatchEx *This = impl_from_IDispatchEx(iface);
1504 HRESULT hres;
1506 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1508 if(!ensure_real_info(This))
1509 return E_OUTOFMEMORY;
1511 if(wFlags == (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF))
1512 wFlags = DISPATCH_PROPERTYPUT;
1514 switch(get_dispid_type(id)) {
1515 case DISPEXPROP_CUSTOM:
1516 if(!This->info->desc->vtbl || !This->info->desc->vtbl->invoke)
1517 return DISP_E_UNKNOWNNAME;
1518 return This->info->desc->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1520 case DISPEXPROP_DYNAMIC: {
1521 DWORD idx = id - DISPID_DYNPROP_0;
1522 dynamic_prop_t *prop;
1524 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1525 return DISP_E_UNKNOWNNAME;
1527 prop = This->dynamic_data->props+idx;
1529 switch(wFlags) {
1530 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1531 if(!pvarRes)
1532 return E_INVALIDARG;
1533 /* fall through */
1534 case DISPATCH_METHOD:
1535 if(V_VT(&prop->var) != VT_DISPATCH) {
1536 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1537 return E_NOTIMPL;
1540 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1541 case DISPATCH_PROPERTYGET:
1542 if(prop->flags & DYNPROP_DELETED)
1543 return DISP_E_UNKNOWNNAME;
1544 V_VT(pvarRes) = VT_EMPTY;
1545 return variant_copy(pvarRes, &prop->var);
1546 case DISPATCH_PROPERTYPUT:
1547 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1548 || pdp->cNamedArgs > 1) {
1549 FIXME("invalid args\n");
1550 return E_INVALIDARG;
1553 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1554 VariantClear(&prop->var);
1555 hres = variant_copy(&prop->var, pdp->rgvarg);
1556 if(FAILED(hres))
1557 return hres;
1559 prop->flags &= ~DYNPROP_DELETED;
1560 return S_OK;
1561 default:
1562 FIXME("unhandled wFlags %x\n", wFlags);
1563 return E_NOTIMPL;
1566 case DISPEXPROP_BUILTIN:
1567 if(wFlags == DISPATCH_CONSTRUCT) {
1568 if(id == DISPID_VALUE) {
1569 if(This->info->desc->vtbl && This->info->desc->vtbl->value) {
1570 return This->info->desc->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1572 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1573 return E_FAIL;
1575 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1576 return E_FAIL;
1579 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1580 default:
1581 assert(0);
1582 return E_FAIL;
1586 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1588 DispatchEx *This = impl_from_IDispatchEx(iface);
1590 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1592 /* Not implemented by IE */
1593 return E_NOTIMPL;
1596 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1598 DispatchEx *This = impl_from_IDispatchEx(iface);
1600 TRACE("(%p)->(%x)\n", This, id);
1602 /* Not implemented by IE */
1603 return E_NOTIMPL;
1606 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1608 DispatchEx *This = impl_from_IDispatchEx(iface);
1609 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1610 return E_NOTIMPL;
1613 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1615 DispatchEx *This = impl_from_IDispatchEx(iface);
1616 func_info_t *func;
1617 HRESULT hres;
1619 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1621 if(!ensure_real_info(This))
1622 return E_OUTOFMEMORY;
1624 if(is_dynamic_dispid(id)) {
1625 DWORD idx = id - DISPID_DYNPROP_0;
1627 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1628 return DISP_E_UNKNOWNNAME;
1630 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1631 if(!*pbstrName)
1632 return E_OUTOFMEMORY;
1634 return S_OK;
1637 hres = get_builtin_func(This->info, id, &func);
1638 if(FAILED(hres))
1639 return hres;
1641 *pbstrName = SysAllocString(func->name);
1642 if(!*pbstrName)
1643 return E_OUTOFMEMORY;
1644 return S_OK;
1647 static HRESULT next_dynamic_id(DispatchEx *dispex, DWORD idx, DISPID *ret_id)
1649 while(idx < dispex->dynamic_data->prop_cnt && dispex->dynamic_data->props[idx].flags & DYNPROP_DELETED)
1650 idx++;
1652 if(idx == dispex->dynamic_data->prop_cnt) {
1653 *ret_id = DISPID_STARTENUM;
1654 return S_FALSE;
1657 *ret_id = DISPID_DYNPROP_0+idx;
1658 return S_OK;
1661 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1663 DispatchEx *This = impl_from_IDispatchEx(iface);
1664 func_info_t *func;
1665 HRESULT hres;
1667 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1669 if(!ensure_real_info(This))
1670 return E_OUTOFMEMORY;
1672 if(is_dynamic_dispid(id)) {
1673 DWORD idx = id - DISPID_DYNPROP_0;
1675 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1676 return DISP_E_UNKNOWNNAME;
1678 return next_dynamic_id(This, idx+1, pid);
1681 if(id == DISPID_STARTENUM) {
1682 func = This->info->funcs;
1683 }else {
1684 hres = get_builtin_func(This->info, id, &func);
1685 if(FAILED(hres))
1686 return hres;
1687 func++;
1690 while(func < This->info->funcs + This->info->func_cnt) {
1691 /* FIXME: Skip hidden properties */
1692 if(func->func_disp_idx == -1) {
1693 *pid = func->id;
1694 return S_OK;
1696 func++;
1699 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt)
1700 return next_dynamic_id(This, 0, pid);
1702 *pid = DISPID_STARTENUM;
1703 return S_FALSE;
1706 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1708 DispatchEx *This = impl_from_IDispatchEx(iface);
1709 FIXME("(%p)->(%p)\n", This, ppunk);
1710 return E_NOTIMPL;
1713 static IDispatchExVtbl DispatchExVtbl = {
1714 DispatchEx_QueryInterface,
1715 DispatchEx_AddRef,
1716 DispatchEx_Release,
1717 DispatchEx_GetTypeInfoCount,
1718 DispatchEx_GetTypeInfo,
1719 DispatchEx_GetIDsOfNames,
1720 DispatchEx_Invoke,
1721 DispatchEx_GetDispID,
1722 DispatchEx_InvokeEx,
1723 DispatchEx_DeleteMemberByName,
1724 DispatchEx_DeleteMemberByDispID,
1725 DispatchEx_GetMemberProperties,
1726 DispatchEx_GetMemberName,
1727 DispatchEx_GetNextDispID,
1728 DispatchEx_GetNameSpaceParent
1731 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1733 if(IsEqualGUID(&IID_IDispatch, riid))
1734 *ppv = &This->IDispatchEx_iface;
1735 else if(IsEqualGUID(&IID_IDispatchEx, riid))
1736 *ppv = &This->IDispatchEx_iface;
1737 else if(IsEqualGUID(&IID_IDispatchJS, riid))
1738 *ppv = NULL;
1739 else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid))
1740 *ppv = NULL;
1741 else if(IsEqualGUID(&IID_IMarshal, riid))
1742 *ppv = NULL;
1743 else if(IsEqualGUID(&IID_IManagedObject, riid))
1744 *ppv = NULL;
1745 else
1746 return FALSE;
1748 if(*ppv)
1749 IUnknown_AddRef((IUnknown*)*ppv);
1750 return TRUE;
1753 void dispex_traverse(DispatchEx *This, nsCycleCollectionTraversalCallback *cb)
1755 dynamic_prop_t *prop;
1757 if(!This->dynamic_data)
1758 return;
1760 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1761 if(V_VT(&prop->var) == VT_DISPATCH)
1762 note_cc_edge((nsISupports*)V_DISPATCH(&prop->var), "dispex_data", cb);
1765 /* FIXME: Traverse func_disps */
1768 void dispex_unlink(DispatchEx *This)
1770 dynamic_prop_t *prop;
1772 if(!This->dynamic_data)
1773 return;
1775 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1776 if(V_VT(&prop->var) == VT_DISPATCH) {
1777 V_VT(&prop->var) = VT_EMPTY;
1778 IDispatch_Release(V_DISPATCH(&prop->var));
1779 }else {
1780 VariantClear(&prop->var);
1785 const void *dispex_get_vtbl(DispatchEx *dispex)
1787 return dispex->info->desc->vtbl;
1790 void release_dispex(DispatchEx *This)
1792 dynamic_prop_t *prop;
1794 if(!This->dynamic_data)
1795 return;
1797 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1798 VariantClear(&prop->var);
1799 heap_free(prop->name);
1802 heap_free(This->dynamic_data->props);
1804 if(This->dynamic_data->func_disps) {
1805 func_obj_entry_t *iter;
1807 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps + This->info->func_disp_cnt; iter++) {
1808 if(iter->func_obj) {
1809 iter->func_obj->obj = NULL;
1810 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
1812 VariantClear(&iter->val);
1815 heap_free(This->dynamic_data->func_disps);
1818 heap_free(This->dynamic_data);
1821 void init_dispex_with_compat_mode(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data, compat_mode_t compat_mode)
1823 assert(compat_mode < COMPAT_MODE_CNT);
1825 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1826 dispex->outer = outer;
1827 dispex->dynamic_data = NULL;
1829 if(data->vtbl && data->vtbl->get_compat_mode) {
1830 /* delayed init */
1831 if(!data->delayed_init_info) {
1832 EnterCriticalSection(&cs_dispex_static_data);
1833 if(!data->delayed_init_info) {
1834 dispex_data_t *info = heap_alloc_zero(sizeof(*data->delayed_init_info));
1835 if(info) {
1836 info->desc = data;
1837 data->delayed_init_info = info;
1840 LeaveCriticalSection(&cs_dispex_static_data);
1842 dispex->info = data->delayed_init_info;
1843 }else {
1844 dispex->info = ensure_dispex_info(data, compat_mode);