advapi32: Fix the FILE_DIR_DISALLOWED spelling.
[wine.git] / dlls / mshtml / dispex.c
blobff4e28a4046c27c5c63f0073bcc5dc638bea8b1b
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"
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 #define MAX_ARGS 16
38 static CRITICAL_SECTION cs_dispex_static_data;
39 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
41 0, 0, &cs_dispex_static_data,
42 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
43 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
45 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
48 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
50 typedef struct {
51 DISPID id;
52 BSTR name;
53 tid_t tid;
54 SHORT call_vtbl_off;
55 SHORT put_vtbl_off;
56 SHORT get_vtbl_off;
57 SHORT func_disp_idx;
58 USHORT argc;
59 VARTYPE prop_vt;
60 VARTYPE *arg_types;
61 } func_info_t;
63 struct dispex_data_t {
64 DWORD func_cnt;
65 func_info_t *funcs;
66 func_info_t **name_table;
67 DWORD func_disp_cnt;
69 struct list entry;
72 typedef struct {
73 VARIANT var;
74 LPWSTR name;
75 DWORD flags;
76 } dynamic_prop_t;
78 #define DYNPROP_DELETED 0x01
80 typedef struct {
81 DispatchEx dispex;
82 IUnknown IUnknown_iface;
83 LONG ref;
84 DispatchEx *obj;
85 func_info_t *info;
86 } func_disp_t;
88 typedef struct {
89 func_disp_t *func_obj;
90 VARIANT val;
91 } func_obj_entry_t;
93 struct dispex_dynamic_data_t {
94 DWORD buf_size;
95 DWORD prop_cnt;
96 dynamic_prop_t *props;
97 func_obj_entry_t *func_disps;
100 #define DISPID_DYNPROP_0 0x50000000
101 #define DISPID_DYNPROP_MAX 0x5fffffff
103 #define FDEX_VERSION_MASK 0xf0000000
105 static ITypeLib *typelib;
106 static ITypeInfo *typeinfos[LAST_tid];
107 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
109 static REFIID tid_ids[] = {
110 #define XIID(iface) &IID_ ## iface,
111 #define XDIID(iface) &DIID_ ## iface,
112 TID_LIST
113 #undef XIID
114 #undef XDIID
117 static HRESULT load_typelib(void)
119 HRESULT hres;
120 ITypeLib *tl;
122 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
123 if(FAILED(hres)) {
124 ERR("LoadRegTypeLib failed: %08x\n", hres);
125 return hres;
128 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
129 ITypeLib_Release(tl);
130 return hres;
133 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
135 HRESULT hres;
137 if (!typelib)
138 hres = load_typelib();
139 if (!typelib)
140 return hres;
142 if(!typeinfos[tid]) {
143 ITypeInfo *ti;
145 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
146 if(FAILED(hres)) {
147 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_mshtml_guid(tid_ids[tid]), hres);
148 return hres;
151 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
152 ITypeInfo_Release(ti);
155 *typeinfo = typeinfos[tid];
156 return S_OK;
159 void release_typelib(void)
161 dispex_data_t *iter;
162 unsigned i;
164 while(!list_empty(&dispex_data_list)) {
165 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
166 list_remove(&iter->entry);
168 for(i=0; i < iter->func_cnt; i++)
169 SysFreeString(iter->funcs[i].name);
171 heap_free(iter->funcs);
172 heap_free(iter->name_table);
173 heap_free(iter);
176 if(!typelib)
177 return;
179 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
180 if(typeinfos[i])
181 ITypeInfo_Release(typeinfos[i]);
183 ITypeLib_Release(typelib);
184 DeleteCriticalSection(&cs_dispex_static_data);
187 HRESULT get_htmldoc_classinfo(ITypeInfo **typeinfo)
189 HRESULT hres;
191 if (!typelib)
192 hres = load_typelib();
193 if (!typelib)
194 return hres;
196 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &CLSID_HTMLDocument, typeinfo);
197 if(FAILED(hres))
198 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
199 return hres;
202 /* Not all argument types are supported yet */
203 #define BUILTIN_ARG_TYPES_SWITCH \
204 CASE_VT(VT_I2, INT16, V_I2); \
205 CASE_VT(VT_I4, INT32, V_I4); \
206 CASE_VT(VT_R4, float, V_R4); \
207 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
208 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
210 /* List all types used by IDispatchEx-based properties */
211 #define BUILTIN_TYPES_SWITCH \
212 BUILTIN_ARG_TYPES_SWITCH; \
213 CASE_VT(VT_VARIANT, VARIANT, *); \
214 CASE_VT(VT_PTR, void*, V_BYREF); \
215 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
216 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH)
218 static BOOL is_arg_type_supported(VARTYPE vt)
220 switch(vt) {
221 #define CASE_VT(x,a,b) case x: return TRUE
222 BUILTIN_ARG_TYPES_SWITCH;
223 #undef CASE_VT
225 return FALSE;
228 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
230 func_info_t *info;
231 HRESULT hres;
233 for(info = data->funcs; info < data->funcs+data->func_cnt; info++) {
234 if(info->id == desc->memid) {
235 if(info->tid != tid)
236 return; /* Duplicated in other interface */
237 break;
241 if(info == data->funcs+data->func_cnt) {
242 if(data->func_cnt == *size)
243 data->funcs = heap_realloc_zero(data->funcs, (*size <<= 1)*sizeof(func_info_t));
245 info = data->funcs+data->func_cnt;
246 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &info->name, NULL, NULL, NULL);
247 if(FAILED(hres))
248 return;
250 data->func_cnt++;
252 info->id = desc->memid;
253 info->tid = tid;
254 info->func_disp_idx = -1;
255 info->prop_vt = VT_EMPTY;
258 if(desc->invkind & DISPATCH_METHOD) {
259 unsigned i;
261 info->func_disp_idx = data->func_disp_cnt++;
262 info->argc = desc->cParams;
264 assert(info->argc < MAX_ARGS);
265 assert(desc->funckind == FUNC_DISPATCH);
267 info->arg_types = heap_alloc(sizeof(*info->arg_types) * info->argc);
268 if(!info->arg_types)
269 return; /* FIXME: real error instead of fallback */
271 for(i=0; i < info->argc; i++)
272 info->arg_types[i] = desc->lprgelemdescParam[i].tdesc.vt;
274 info->prop_vt = desc->elemdescFunc.tdesc.vt;
275 if(info->prop_vt != VT_VOID && !is_arg_type_supported(info->prop_vt)) {
276 TRACE("%s: return type %d\n", debugstr_w(info->name), info->prop_vt);
277 return; /* Fallback to ITypeInfo::Invoke */
280 if(desc->cParamsOpt) {
281 TRACE("%s: optional params\n", debugstr_w(info->name));
282 return; /* Fallback to ITypeInfo::Invoke */
285 for(i=0; i < info->argc; i++) {
286 if(!is_arg_type_supported(info->arg_types[i])) {
287 return; /* Fallback to ITypeInfo for unsupported arg types */
290 if(desc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
291 TRACE("%s param %d: default value\n", debugstr_w(info->name), i);
292 return; /* Fallback to ITypeInfo::Invoke */
296 assert(info->argc <= MAX_ARGS);
297 assert(desc->callconv == CC_STDCALL);
299 info->call_vtbl_off = desc->oVft/sizeof(void*);
300 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
301 VARTYPE vt = VT_EMPTY;
303 if(desc->invkind & DISPATCH_PROPERTYGET) {
304 vt = desc->elemdescFunc.tdesc.vt;
305 info->get_vtbl_off = desc->oVft/sizeof(void*);
307 if(desc->invkind & DISPATCH_PROPERTYPUT) {
308 assert(desc->cParams == 1);
309 vt = desc->lprgelemdescParam->tdesc.vt;
310 info->put_vtbl_off = desc->oVft/sizeof(void*);
313 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
314 info->prop_vt = vt;
318 static int dispid_cmp(const void *p1, const void *p2)
320 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
323 static int func_name_cmp(const void *p1, const void *p2)
325 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
328 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
330 const tid_t *tid = This->data->iface_tids;
331 FUNCDESC *funcdesc;
332 dispex_data_t *data;
333 DWORD size = 16, i;
334 ITypeInfo *ti, *dti;
335 HRESULT hres;
337 TRACE("(%p)\n", This);
339 if(This->data->disp_tid) {
340 hres = get_typeinfo(This->data->disp_tid, &dti);
341 if(FAILED(hres)) {
342 ERR("Could not get disp type info: %08x\n", hres);
343 return NULL;
347 data = heap_alloc(sizeof(dispex_data_t));
348 if (!data) {
349 ERR("Out of memory\n");
350 return NULL;
352 data->func_cnt = 0;
353 data->func_disp_cnt = 0;
354 data->funcs = heap_alloc_zero(size*sizeof(func_info_t));
355 if (!data->funcs) {
356 heap_free (data);
357 ERR("Out of memory\n");
358 return NULL;
360 list_add_tail(&dispex_data_list, &data->entry);
362 while(*tid) {
363 hres = get_typeinfo(*tid, &ti);
364 if(FAILED(hres))
365 break;
367 i=7;
368 while(1) {
369 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
370 if(FAILED(hres))
371 break;
373 add_func_info(data, &size, *tid, funcdesc, dti);
374 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
377 tid++;
380 if(!data->func_cnt) {
381 heap_free(data->funcs);
382 data->name_table = NULL;
383 data->funcs = NULL;
384 return data;
388 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
389 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
391 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
392 for(i=0; i < data->func_cnt; i++)
393 data->name_table[i] = data->funcs+i;
394 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
396 return data;
399 static int id_cmp(const void *p1, const void *p2)
401 return *(const DISPID*)p1 - *(const DISPID*)p2;
404 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
406 unsigned i, func_cnt;
407 FUNCDESC *funcdesc;
408 ITypeInfo *ti;
409 TYPEATTR *attr;
410 DISPID *ids;
411 HRESULT hres;
413 hres = get_typeinfo(tid, &ti);
414 if(FAILED(hres))
415 return hres;
417 hres = ITypeInfo_GetTypeAttr(ti, &attr);
418 if(FAILED(hres)) {
419 ITypeInfo_Release(ti);
420 return hres;
423 func_cnt = attr->cFuncs;
424 ITypeInfo_ReleaseTypeAttr(ti, attr);
426 ids = heap_alloc(func_cnt*sizeof(DISPID));
427 if(!ids) {
428 ITypeInfo_Release(ti);
429 return E_OUTOFMEMORY;
432 for(i=0; i < func_cnt; i++) {
433 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
434 if(FAILED(hres))
435 break;
437 ids[i] = funcdesc->memid;
438 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
441 ITypeInfo_Release(ti);
442 if(FAILED(hres)) {
443 heap_free(ids);
444 return hres;
447 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
449 *ret_size = func_cnt;
450 *ret = ids;
451 return S_OK;
454 static dispex_data_t *get_dispex_data(DispatchEx *This)
456 if(This->data->data)
457 return This->data->data;
459 EnterCriticalSection(&cs_dispex_static_data);
461 if(!This->data->data)
462 This->data->data = preprocess_dispex_data(This);
464 LeaveCriticalSection(&cs_dispex_static_data);
466 return This->data->data;
469 static inline BOOL is_custom_dispid(DISPID id)
471 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
474 static inline BOOL is_dynamic_dispid(DISPID id)
476 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
479 dispex_prop_type_t get_dispid_type(DISPID id)
481 if(is_dynamic_dispid(id))
482 return DISPEXPROP_DYNAMIC;
483 if(is_custom_dispid(id))
484 return DISPEXPROP_CUSTOM;
485 return DISPEXPROP_BUILTIN;
488 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
490 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
491 V_VT(dest) = VT_BSTR;
492 V_BSTR(dest) = NULL;
493 return S_OK;
496 return VariantCopy(dest, src);
499 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
501 if(This->dynamic_data)
502 return This->dynamic_data;
504 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
505 if(!This->dynamic_data)
506 return NULL;
508 if(This->data->vtbl && This->data->vtbl->populate_props)
509 This->data->vtbl->populate_props(This);
511 return This->dynamic_data;
514 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
516 const BOOL alloc = flags & fdexNameEnsure;
517 dispex_dynamic_data_t *data;
518 dynamic_prop_t *prop;
520 data = get_dynamic_data(This);
521 if(!data)
522 return E_OUTOFMEMORY;
524 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
525 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
526 if(prop->flags & DYNPROP_DELETED) {
527 if(!alloc)
528 return DISP_E_UNKNOWNNAME;
529 prop->flags &= ~DYNPROP_DELETED;
531 *ret = prop;
532 return S_OK;
536 if(!alloc)
537 return DISP_E_UNKNOWNNAME;
539 TRACE("creating dynamic prop %s\n", debugstr_w(name));
541 if(!data->buf_size) {
542 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
543 if(!data->props)
544 return E_OUTOFMEMORY;
545 data->buf_size = 4;
546 }else if(data->buf_size == data->prop_cnt) {
547 dynamic_prop_t *new_props;
549 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
550 if(!new_props)
551 return E_OUTOFMEMORY;
553 data->props = new_props;
554 data->buf_size <<= 1;
557 prop = data->props + data->prop_cnt;
559 prop->name = heap_strdupW(name);
560 if(!prop->name)
561 return E_OUTOFMEMORY;
563 VariantInit(&prop->var);
564 prop->flags = 0;
565 data->prop_cnt++;
566 *ret = prop;
567 return S_OK;
570 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
572 dynamic_prop_t *prop;
573 HRESULT hres;
575 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
576 if(FAILED(hres))
577 return hres;
579 *ret = &prop->var;
580 return S_OK;
583 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
585 dynamic_prop_t *prop;
586 HRESULT hres;
588 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
589 if(FAILED(hres))
590 return hres;
592 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
593 return S_OK;
596 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
597 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
599 if(This->data->vtbl && This->data->vtbl->value)
600 return This->data->vtbl->value(This, lcid, flags, params, res, ei, caller);
602 switch(flags) {
603 case DISPATCH_PROPERTYGET:
604 V_VT(res) = VT_BSTR;
605 V_BSTR(res) = SysAllocString(objectW);
606 if(!V_BSTR(res))
607 return E_OUTOFMEMORY;
608 break;
609 default:
610 FIXME("Unimplemented flags %x\n", flags);
611 return E_NOTIMPL;
614 return S_OK;
617 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
618 EXCEPINFO *ei)
620 ITypeInfo *ti;
621 IUnknown *unk;
622 UINT argerr=0;
623 HRESULT hres;
625 hres = get_typeinfo(func->tid, &ti);
626 if(FAILED(hres)) {
627 ERR("Could not get type info: %08x\n", hres);
628 return hres;
631 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
632 if(FAILED(hres)) {
633 ERR("Could not get iface %s: %08x\n", debugstr_mshtml_guid(tid_ids[func->tid]), hres);
634 return E_FAIL;
637 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
639 IUnknown_Release(unk);
640 return hres;
643 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
645 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
648 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
650 func_disp_t *This = impl_from_IUnknown(iface);
652 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
654 if(IsEqualGUID(&IID_IUnknown, riid)) {
655 *ppv = &This->IUnknown_iface;
656 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
657 return *ppv ? S_OK : E_NOINTERFACE;
658 }else {
659 *ppv = NULL;
660 return E_NOINTERFACE;
663 IUnknown_AddRef((IUnknown*)*ppv);
664 return S_OK;
667 static ULONG WINAPI Function_AddRef(IUnknown *iface)
669 func_disp_t *This = impl_from_IUnknown(iface);
670 LONG ref = InterlockedIncrement(&This->ref);
672 TRACE("(%p) ref=%d\n", This, ref);
674 return ref;
677 static ULONG WINAPI Function_Release(IUnknown *iface)
679 func_disp_t *This = impl_from_IUnknown(iface);
680 LONG ref = InterlockedDecrement(&This->ref);
682 TRACE("(%p) ref=%d\n", This, ref);
684 if(!ref) {
685 assert(!This->obj);
686 release_dispex(&This->dispex);
687 heap_free(This);
690 return ref;
693 static const IUnknownVtbl FunctionUnkVtbl = {
694 Function_QueryInterface,
695 Function_AddRef,
696 Function_Release
699 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
701 return CONTAINING_RECORD(iface, func_disp_t, dispex);
704 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
705 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
707 func_disp_t *This = impl_from_DispatchEx(dispex);
708 HRESULT hres;
710 switch(flags) {
711 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
712 if(!res)
713 return E_INVALIDARG;
714 /* fall through */
715 case DISPATCH_METHOD:
716 if(!This->obj)
717 return E_UNEXPECTED;
718 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
719 break;
720 case DISPATCH_PROPERTYGET: {
721 unsigned name_len;
722 WCHAR *ptr;
723 BSTR str;
725 static const WCHAR func_prefixW[] =
726 {'\n','f','u','n','c','t','i','o','n',' '};
727 static const WCHAR func_suffixW[] =
728 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
730 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
731 if(!caller)
732 return E_ACCESSDENIED;
734 name_len = SysStringLen(This->info->name);
735 ptr = str = SysAllocStringLen(NULL, name_len + (sizeof(func_prefixW)+sizeof(func_suffixW))/sizeof(WCHAR));
736 if(!str)
737 return E_OUTOFMEMORY;
739 memcpy(ptr, func_prefixW, sizeof(func_prefixW));
740 ptr += sizeof(func_prefixW)/sizeof(WCHAR);
742 memcpy(ptr, This->info->name, name_len*sizeof(WCHAR));
743 ptr += name_len;
745 memcpy(ptr, func_suffixW, sizeof(func_suffixW));
747 V_VT(res) = VT_BSTR;
748 V_BSTR(res) = str;
749 return S_OK;
751 default:
752 FIXME("Unimplemented flags %x\n", flags);
753 hres = E_NOTIMPL;
756 return hres;
759 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
760 function_value,
761 NULL,
762 NULL,
763 NULL
766 static const tid_t function_iface_tids[] = {0};
768 static dispex_static_data_t function_dispex = {
769 &function_dispex_vtbl,
770 NULL_tid,
771 NULL,
772 function_iface_tids
775 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
777 func_disp_t *ret;
779 ret = heap_alloc_zero(sizeof(func_disp_t));
780 if(!ret)
781 return NULL;
783 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
784 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
785 ret->ref = 1;
786 ret->obj = obj;
787 ret->info = info;
789 return ret;
792 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
793 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
795 DISPID named_arg = DISPID_THIS;
796 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
797 IDispatchEx *dispex;
798 HRESULT hres;
800 if(dp->cNamedArgs) {
801 FIXME("named args not supported\n");
802 return E_NOTIMPL;
805 new_dp.rgvarg = heap_alloc((dp->cArgs+1)*sizeof(VARIANTARG));
806 if(!new_dp.rgvarg)
807 return E_OUTOFMEMORY;
809 new_dp.cArgs = dp->cArgs+1;
810 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
812 V_VT(new_dp.rgvarg) = VT_DISPATCH;
813 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
815 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
816 TRACE(">>>\n");
817 if(SUCCEEDED(hres)) {
818 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
819 IDispatchEx_Release(dispex);
820 }else {
821 ULONG err = 0;
822 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
824 if(SUCCEEDED(hres))
825 TRACE("<<< %s\n", debugstr_variant(res));
826 else
827 WARN("<<< %08x\n", hres);
829 heap_free(new_dp.rgvarg);
830 return hres;
833 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
835 dispex_dynamic_data_t *dynamic_data;
836 func_obj_entry_t *entry;
838 dynamic_data = get_dynamic_data(This);
839 if(!dynamic_data)
840 return E_OUTOFMEMORY;
842 if(!dynamic_data->func_disps) {
843 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(*dynamic_data->func_disps));
844 if(!dynamic_data->func_disps)
845 return E_OUTOFMEMORY;
848 entry = dynamic_data->func_disps + func->func_disp_idx;
849 if(!entry->func_obj) {
850 entry->func_obj = create_func_disp(This, func);
851 if(!entry->func_obj)
852 return E_OUTOFMEMORY;
854 IDispatchEx_AddRef(&entry->func_obj->dispex.IDispatchEx_iface);
855 V_VT(&entry->val) = VT_DISPATCH;
856 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
859 *ret = entry;
860 return S_OK;
863 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
865 int min, max, n;
867 min = 0;
868 max = data->func_cnt-1;
870 while(min <= max) {
871 n = (min+max)/2;
873 if(data->funcs[n].id == id) {
874 *ret = data->funcs+n;
875 return S_OK;
878 if(data->funcs[n].id < id)
879 min = n+1;
880 else
881 max = n-1;
884 WARN("invalid id %x\n", id);
885 return DISP_E_UNKNOWNNAME;
888 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
890 dispex_data_t *data;
891 int min, max, n, c;
893 data = get_dispex_data(This);
894 if(!data)
895 return E_FAIL;
897 min = 0;
898 max = data->func_cnt-1;
900 while(min <= max) {
901 n = (min+max)/2;
903 c = strcmpiW(data->name_table[n]->name, name);
904 if(!c) {
905 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
906 break;
908 *ret = data->name_table[n]->id;
909 return S_OK;
912 if(c > 0)
913 max = n-1;
914 else
915 min = n+1;
918 if(This->data->vtbl && This->data->vtbl->get_dispid) {
919 HRESULT hres;
921 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
922 if(hres != DISP_E_UNKNOWNNAME)
923 return hres;
926 return DISP_E_UNKNOWNNAME;
929 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
931 V_VT(dst) = VT_EMPTY;
933 if(caller) {
934 IVariantChangeType *change_type = NULL;
935 HRESULT hres;
937 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
938 if(SUCCEEDED(hres)) {
939 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
940 IVariantChangeType_Release(change_type);
941 return hres;
945 switch(vt) {
946 case VT_BOOL:
947 if(V_VT(src) == VT_BSTR) {
948 V_VT(dst) = VT_BOOL;
949 V_BOOL(dst) = V_BSTR(src) && *V_BSTR(src) ? VARIANT_TRUE : VARIANT_FALSE;
950 return S_OK;
952 break;
955 return VariantChangeType(dst, src, 0, vt);
958 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
960 IUnknown *iface;
961 HRESULT hres;
963 if(dp && dp->cArgs) {
964 FIXME("cArgs %d\n", dp->cArgs);
965 return E_NOTIMPL;
968 assert(func->get_vtbl_off);
970 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
971 if(SUCCEEDED(hres)) {
972 switch(func->prop_vt) {
973 #define CASE_VT(vt,type,access) \
974 case vt: { \
975 type val; \
976 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
977 if(SUCCEEDED(hres)) \
978 access(res) = val; \
980 break
981 BUILTIN_TYPES_SWITCH;
982 #undef CASE_VT
983 default:
984 FIXME("Unhandled vt %d\n", func->prop_vt);
985 hres = E_NOTIMPL;
987 IUnknown_Release(iface);
990 if(FAILED(hres))
991 return hres;
993 if(func->prop_vt != VT_VARIANT)
994 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
995 return S_OK;
998 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
1000 VARIANT *v, tmpv;
1001 IUnknown *iface;
1002 HRESULT hres;
1004 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1005 || dp->cNamedArgs > 1) {
1006 FIXME("invalid args\n");
1007 return E_INVALIDARG;
1010 if(!func->put_vtbl_off) {
1011 FIXME("No setter\n");
1012 return E_FAIL;
1015 v = dp->rgvarg;
1016 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
1017 hres = change_type(&tmpv, v, func->prop_vt, caller);
1018 if(FAILED(hres))
1019 return hres;
1020 v = &tmpv;
1023 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1024 if(SUCCEEDED(hres)) {
1025 switch(func->prop_vt) {
1026 #define CASE_VT(vt,type,access) \
1027 case vt: \
1028 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1029 break
1030 BUILTIN_TYPES_SWITCH;
1031 #undef CASE_VT
1032 default:
1033 FIXME("Unimplemented vt %d\n", func->prop_vt);
1034 hres = E_NOTIMPL;
1037 IUnknown_Release(iface);
1040 if(v == &tmpv)
1041 VariantClear(v);
1042 return hres;
1045 static HRESULT invoke_builtin_function(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res, IServiceProvider *caller)
1047 VARIANT arg_buf[MAX_ARGS], *arg_ptrs[MAX_ARGS], *arg, retv, ret_ref, vhres;
1048 unsigned i, nconv = 0;
1049 IUnknown *iface;
1050 HRESULT hres;
1052 if(dp->cNamedArgs) {
1053 FIXME("Named arguments not supported\n");
1054 return E_NOTIMPL;
1057 if(dp->cArgs != func->argc) {
1058 FIXME("Invalid argument count (expected %u, got %u)\n", func->argc, dp->cArgs);
1059 return E_INVALIDARG;
1062 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1063 if(FAILED(hres))
1064 return hres;
1066 for(i=0; i < func->argc; i++) {
1067 arg = dp->rgvarg+dp->cArgs-i-1;
1068 if(func->arg_types[i] == V_VT(arg)) {
1069 arg_ptrs[i] = arg;
1070 }else {
1071 hres = change_type(arg_buf+nconv, arg, func->arg_types[i], caller);
1072 if(FAILED(hres))
1073 break;
1074 arg_ptrs[i] = arg_buf + nconv++;
1078 if(SUCCEEDED(hres)) {
1079 if(func->prop_vt == VT_VOID) {
1080 V_VT(&retv) = VT_EMPTY;
1081 }else {
1082 V_VT(&retv) = func->prop_vt;
1083 arg_ptrs[func->argc] = &ret_ref;
1084 V_VT(&ret_ref) = VT_BYREF|func->prop_vt;
1086 switch(func->prop_vt) {
1087 #define CASE_VT(vt,type,access) \
1088 case vt: \
1089 V_BYREF(&ret_ref) = &access(&retv); \
1090 break
1091 BUILTIN_TYPES_SWITCH;
1092 #undef CASE_VT
1093 default:
1094 assert(0);
1098 V_VT(&vhres) = VT_ERROR;
1099 hres = DispCallFunc(iface, func->call_vtbl_off*sizeof(void*), CC_STDCALL, VT_ERROR,
1100 func->argc + (func->prop_vt == VT_VOID ? 0 : 1), func->arg_types, arg_ptrs, &vhres);
1103 while(nconv--)
1104 VariantClear(arg_buf+nconv);
1105 IUnknown_Release(iface);
1106 if(FAILED(hres))
1107 return hres;
1108 if(FAILED(V_ERROR(&vhres)))
1109 return V_ERROR(&vhres);
1111 if(res)
1112 *res = retv;
1113 else
1114 VariantClear(&retv);
1115 return V_ERROR(&vhres);
1118 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
1119 EXCEPINFO *ei, IServiceProvider *caller)
1121 HRESULT hres;
1123 switch(flags) {
1124 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1125 if(!res)
1126 return E_INVALIDARG;
1127 /* fall through */
1128 case DISPATCH_METHOD:
1129 if(This->dynamic_data && This->dynamic_data->func_disps
1130 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1131 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
1133 if(V_VT(&entry->val) != VT_DISPATCH) {
1134 FIXME("calling %s not supported\n", debugstr_variant(&entry->val));
1135 return E_NOTIMPL;
1138 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != V_DISPATCH(&entry->val)) {
1139 if(!V_DISPATCH(&entry->val)) {
1140 FIXME("Calling null\n");
1141 return E_FAIL;
1144 hres = invoke_disp_value(This, V_DISPATCH(&entry->val), 0, flags, dp, res, ei, NULL);
1145 break;
1149 if(func->call_vtbl_off)
1150 hres = invoke_builtin_function(This, func, dp, res, caller);
1151 else
1152 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1153 break;
1154 case DISPATCH_PROPERTYGET: {
1155 func_obj_entry_t *entry;
1157 if(func->id == DISPID_VALUE) {
1158 BSTR ret;
1160 ret = SysAllocString(objectW);
1161 if(!ret)
1162 return E_OUTOFMEMORY;
1164 V_VT(res) = VT_BSTR;
1165 V_BSTR(res) = ret;
1166 return S_OK;
1169 hres = get_func_obj_entry(This, func, &entry);
1170 if(FAILED(hres))
1171 return hres;
1173 V_VT(res) = VT_EMPTY;
1174 return VariantCopy(res, &entry->val);
1176 case DISPATCH_PROPERTYPUT: {
1177 func_obj_entry_t *entry;
1179 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1180 || dp->cNamedArgs > 1) {
1181 FIXME("invalid args\n");
1182 return E_INVALIDARG;
1186 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1187 * function property using InvokeEx, it's possible to do that from jscript.
1188 * Native probably uses some undocumented interface in this case, but it should
1189 * be fine for us to allow IDispatchEx handle that.
1191 hres = get_func_obj_entry(This, func, &entry);
1192 if(FAILED(hres))
1193 return hres;
1195 return VariantCopy(&entry->val, dp->rgvarg);
1197 default:
1198 FIXME("Unimplemented flags %x\n", flags);
1199 hres = E_NOTIMPL;
1202 return hres;
1205 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1206 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1208 dispex_data_t *data;
1209 func_info_t *func;
1210 HRESULT hres;
1212 data = get_dispex_data(This);
1213 if(!data)
1214 return E_FAIL;
1216 hres = get_builtin_func(data, id, &func);
1217 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
1218 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1219 if(FAILED(hres))
1220 return hres;
1222 if(func->func_disp_idx != -1)
1223 return function_invoke(This, func, flags, dp, res, ei, caller);
1225 switch(flags) {
1226 case DISPATCH_PROPERTYPUT:
1227 if(res)
1228 V_VT(res) = VT_EMPTY;
1229 hres = builtin_propput(This, func, dp, caller);
1230 break;
1231 case DISPATCH_PROPERTYGET:
1232 hres = builtin_propget(This, func, dp, res);
1233 break;
1234 default:
1235 if(!func->get_vtbl_off) {
1236 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1237 }else {
1238 VARIANT v;
1240 hres = builtin_propget(This, func, NULL, &v);
1241 if(FAILED(hres))
1242 return hres;
1244 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD) || dp->cArgs) {
1245 if(V_VT(&v) != VT_DISPATCH) {
1246 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v), flags);
1247 VariantClear(&v);
1248 return E_FAIL;
1251 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1252 IDispatch_Release(V_DISPATCH(&v));
1253 }else if(res) {
1254 *res = v;
1255 }else {
1256 VariantClear(&v);
1261 return hres;
1264 HRESULT remove_attribute(DispatchEx *This, DISPID id, VARIANT_BOOL *success)
1266 switch(get_dispid_type(id)) {
1267 case DISPEXPROP_CUSTOM:
1268 FIXME("DISPEXPROP_CUSTOM not supported\n");
1269 return E_NOTIMPL;
1271 case DISPEXPROP_DYNAMIC: {
1272 DWORD idx = id - DISPID_DYNPROP_0;
1273 dynamic_prop_t *prop;
1275 prop = This->dynamic_data->props+idx;
1276 VariantClear(&prop->var);
1277 prop->flags |= DYNPROP_DELETED;
1278 *success = VARIANT_TRUE;
1279 return S_OK;
1281 case DISPEXPROP_BUILTIN: {
1282 VARIANT var;
1283 DISPPARAMS dp = {&var,NULL,1,0};
1284 dispex_data_t *data;
1285 func_info_t *func;
1286 HRESULT hres;
1288 data = get_dispex_data(This);
1289 if(!data)
1290 return E_FAIL;
1292 hres = get_builtin_func(data, id, &func);
1293 if(FAILED(hres))
1294 return hres;
1296 /* For builtin functions, we set their value to the original function. */
1297 if(func->func_disp_idx != -1) {
1298 func_obj_entry_t *entry;
1300 if(!This->dynamic_data || !This->dynamic_data->func_disps
1301 || !This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1302 *success = VARIANT_FALSE;
1303 return S_OK;
1306 entry = This->dynamic_data->func_disps + func->func_disp_idx;
1307 if(V_VT(&entry->val) == VT_DISPATCH
1308 && V_DISPATCH(&entry->val) == (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface) {
1309 *success = VARIANT_FALSE;
1310 return S_OK;
1313 VariantClear(&entry->val);
1314 V_VT(&entry->val) = VT_DISPATCH;
1315 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
1316 IDispatch_AddRef(V_DISPATCH(&entry->val));
1317 *success = VARIANT_TRUE;
1318 return S_OK;
1321 V_VT(&var) = VT_EMPTY;
1322 hres = builtin_propput(This, func, &dp, NULL);
1323 if(FAILED(hres))
1324 return hres;
1326 *success = VARIANT_TRUE;
1327 return S_OK;
1329 default:
1330 assert(0);
1331 return E_FAIL;
1335 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1337 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1340 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1342 DispatchEx *This = impl_from_IDispatchEx(iface);
1344 return IUnknown_QueryInterface(This->outer, riid, ppv);
1347 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1349 DispatchEx *This = impl_from_IDispatchEx(iface);
1351 return IUnknown_AddRef(This->outer);
1354 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1356 DispatchEx *This = impl_from_IDispatchEx(iface);
1358 return IUnknown_Release(This->outer);
1361 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1363 DispatchEx *This = impl_from_IDispatchEx(iface);
1365 TRACE("(%p)->(%p)\n", This, pctinfo);
1367 *pctinfo = 1;
1368 return S_OK;
1371 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1372 LCID lcid, ITypeInfo **ppTInfo)
1374 DispatchEx *This = impl_from_IDispatchEx(iface);
1375 HRESULT hres;
1377 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1379 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
1380 if(FAILED(hres))
1381 return hres;
1383 ITypeInfo_AddRef(*ppTInfo);
1384 return S_OK;
1387 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1388 LPOLESTR *rgszNames, UINT cNames,
1389 LCID lcid, DISPID *rgDispId)
1391 DispatchEx *This = impl_from_IDispatchEx(iface);
1392 UINT i;
1393 HRESULT hres;
1395 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1396 lcid, rgDispId);
1398 for(i=0; i < cNames; i++) {
1399 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1400 if(FAILED(hres))
1401 return hres;
1404 return S_OK;
1407 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1408 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1409 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1411 DispatchEx *This = impl_from_IDispatchEx(iface);
1413 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1414 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1416 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1417 pVarResult, pExcepInfo, NULL);
1420 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1422 DispatchEx *This = impl_from_IDispatchEx(iface);
1423 dynamic_prop_t *dprop;
1424 HRESULT hres;
1426 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1428 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1429 FIXME("Unsupported grfdex %x\n", grfdex);
1431 hres = get_builtin_id(This, bstrName, grfdex, pid);
1432 if(hres != DISP_E_UNKNOWNNAME)
1433 return hres;
1435 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1436 if(FAILED(hres))
1437 return hres;
1439 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1440 return S_OK;
1443 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1444 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1446 DispatchEx *This = impl_from_IDispatchEx(iface);
1447 HRESULT hres;
1449 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1451 if(wFlags == (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF))
1452 wFlags = DISPATCH_PROPERTYPUT;
1454 switch(get_dispid_type(id)) {
1455 case DISPEXPROP_CUSTOM:
1456 if(!This->data->vtbl || !This->data->vtbl->invoke)
1457 return DISP_E_UNKNOWNNAME;
1458 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1460 case DISPEXPROP_DYNAMIC: {
1461 DWORD idx = id - DISPID_DYNPROP_0;
1462 dynamic_prop_t *prop;
1464 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1465 return DISP_E_UNKNOWNNAME;
1467 prop = This->dynamic_data->props+idx;
1469 switch(wFlags) {
1470 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1471 if(!pvarRes)
1472 return E_INVALIDARG;
1473 /* fall through */
1474 case DISPATCH_METHOD:
1475 if(V_VT(&prop->var) != VT_DISPATCH) {
1476 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1477 return E_NOTIMPL;
1480 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1481 case DISPATCH_PROPERTYGET:
1482 if(prop->flags & DYNPROP_DELETED)
1483 return DISP_E_UNKNOWNNAME;
1484 V_VT(pvarRes) = VT_EMPTY;
1485 return variant_copy(pvarRes, &prop->var);
1486 case DISPATCH_PROPERTYPUT:
1487 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1488 || pdp->cNamedArgs > 1) {
1489 FIXME("invalid args\n");
1490 return E_INVALIDARG;
1493 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1494 VariantClear(&prop->var);
1495 hres = variant_copy(&prop->var, pdp->rgvarg);
1496 if(FAILED(hres))
1497 return hres;
1499 prop->flags &= ~DYNPROP_DELETED;
1500 return S_OK;
1501 default:
1502 FIXME("unhandled wFlags %x\n", wFlags);
1503 return E_NOTIMPL;
1506 case DISPEXPROP_BUILTIN:
1507 if(wFlags == DISPATCH_CONSTRUCT) {
1508 if(id == DISPID_VALUE) {
1509 if(This->data->vtbl && This->data->vtbl->value) {
1510 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1512 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1513 return E_FAIL;
1515 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1516 return E_FAIL;
1519 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1520 default:
1521 assert(0);
1522 return E_FAIL;
1526 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1528 DispatchEx *This = impl_from_IDispatchEx(iface);
1530 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1532 /* Not implemented by IE */
1533 return E_NOTIMPL;
1536 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1538 DispatchEx *This = impl_from_IDispatchEx(iface);
1540 TRACE("(%p)->(%x)\n", This, id);
1542 /* Not implemented by IE */
1543 return E_NOTIMPL;
1546 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1548 DispatchEx *This = impl_from_IDispatchEx(iface);
1549 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1550 return E_NOTIMPL;
1553 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1555 DispatchEx *This = impl_from_IDispatchEx(iface);
1556 dispex_data_t *data;
1557 func_info_t *func;
1558 HRESULT hres;
1560 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1562 if(is_dynamic_dispid(id)) {
1563 DWORD idx = id - DISPID_DYNPROP_0;
1565 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1566 return DISP_E_UNKNOWNNAME;
1568 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1569 if(!*pbstrName)
1570 return E_OUTOFMEMORY;
1572 return S_OK;
1575 data = get_dispex_data(This);
1576 if(!data)
1577 return E_FAIL;
1579 hres = get_builtin_func(data, id, &func);
1580 if(FAILED(hres))
1581 return hres;
1583 *pbstrName = SysAllocString(func->name);
1584 if(!*pbstrName)
1585 return E_OUTOFMEMORY;
1586 return S_OK;
1589 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1591 DispatchEx *This = impl_from_IDispatchEx(iface);
1592 dispex_data_t *data;
1593 func_info_t *func;
1594 HRESULT hres;
1596 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1598 if(is_dynamic_dispid(id)) {
1599 DWORD idx = id - DISPID_DYNPROP_0;
1601 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1602 return DISP_E_UNKNOWNNAME;
1604 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1606 if(idx == This->dynamic_data->prop_cnt) {
1607 *pid = DISPID_STARTENUM;
1608 return S_FALSE;
1611 *pid = DISPID_DYNPROP_0+idx;
1612 return S_OK;
1615 data = get_dispex_data(This);
1616 if(!data)
1617 return E_FAIL;
1619 if(id == DISPID_STARTENUM) {
1620 func = data->funcs;
1621 }else {
1622 hres = get_builtin_func(data, id, &func);
1623 if(FAILED(hres))
1624 return hres;
1625 func++;
1628 while(func < data->funcs+data->func_cnt) {
1629 /* FIXME: Skip hidden properties */
1630 if(func->func_disp_idx == -1) {
1631 *pid = func->id;
1632 return S_OK;
1634 func++;
1637 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1638 *pid = DISPID_DYNPROP_0;
1639 return S_OK;
1642 *pid = DISPID_STARTENUM;
1643 return S_FALSE;
1646 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1648 DispatchEx *This = impl_from_IDispatchEx(iface);
1649 FIXME("(%p)->(%p)\n", This, ppunk);
1650 return E_NOTIMPL;
1653 static IDispatchExVtbl DispatchExVtbl = {
1654 DispatchEx_QueryInterface,
1655 DispatchEx_AddRef,
1656 DispatchEx_Release,
1657 DispatchEx_GetTypeInfoCount,
1658 DispatchEx_GetTypeInfo,
1659 DispatchEx_GetIDsOfNames,
1660 DispatchEx_Invoke,
1661 DispatchEx_GetDispID,
1662 DispatchEx_InvokeEx,
1663 DispatchEx_DeleteMemberByName,
1664 DispatchEx_DeleteMemberByDispID,
1665 DispatchEx_GetMemberProperties,
1666 DispatchEx_GetMemberName,
1667 DispatchEx_GetNextDispID,
1668 DispatchEx_GetNameSpaceParent
1671 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1673 if(IsEqualGUID(&IID_IDispatch, riid))
1674 *ppv = &This->IDispatchEx_iface;
1675 else if(IsEqualGUID(&IID_IDispatchEx, riid))
1676 *ppv = &This->IDispatchEx_iface;
1677 else if(IsEqualGUID(&IID_IDispatchJS, riid))
1678 *ppv = NULL;
1679 else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid))
1680 *ppv = NULL;
1681 else
1682 return FALSE;
1684 if(*ppv)
1685 IUnknown_AddRef((IUnknown*)*ppv);
1686 return TRUE;
1689 void dispex_traverse(DispatchEx *This, nsCycleCollectionTraversalCallback *cb)
1691 dynamic_prop_t *prop;
1693 if(!This->dynamic_data)
1694 return;
1696 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1697 if(V_VT(&prop->var) == VT_DISPATCH)
1698 note_cc_edge((nsISupports*)V_DISPATCH(&prop->var), "dispex_data", cb);
1701 /* FIXME: Traverse func_disps */
1704 void dispex_unlink(DispatchEx *This)
1706 dynamic_prop_t *prop;
1708 if(!This->dynamic_data)
1709 return;
1711 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1712 if(V_VT(&prop->var) == VT_DISPATCH) {
1713 V_VT(&prop->var) = VT_EMPTY;
1714 IDispatch_Release(V_DISPATCH(&prop->var));
1715 }else {
1716 VariantClear(&prop->var);
1721 void release_dispex(DispatchEx *This)
1723 dynamic_prop_t *prop;
1725 if(!This->dynamic_data)
1726 return;
1728 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1729 VariantClear(&prop->var);
1730 heap_free(prop->name);
1733 heap_free(This->dynamic_data->props);
1735 if(This->dynamic_data->func_disps) {
1736 func_obj_entry_t *iter;
1738 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps+This->data->data->func_disp_cnt; iter++) {
1739 if(iter->func_obj) {
1740 iter->func_obj->obj = NULL;
1741 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
1743 VariantClear(&iter->val);
1746 heap_free(This->dynamic_data->func_disps);
1749 heap_free(This->dynamic_data);
1752 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1754 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1755 dispex->outer = outer;
1756 dispex->data = data;
1757 dispex->dynamic_data = NULL;