Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / mshtml / dispex.c
blob4019a3c54a0bf5e7b80632dae30077bd54b9b866
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>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
36 typedef struct {
37 DISPID id;
38 BSTR name;
39 tid_t tid;
40 int func_disp_idx;
41 } func_info_t;
43 struct dispex_data_t {
44 DWORD func_cnt;
45 func_info_t *funcs;
46 func_info_t **name_table;
47 DWORD func_disp_cnt;
49 struct list entry;
52 typedef struct {
53 VARIANT var;
54 LPWSTR name;
55 } dynamic_prop_t;
57 typedef struct {
58 DispatchEx dispex;
59 const IUnknownVtbl *lpIUnknownVtbl;
60 DispatchEx *obj;
61 func_info_t *info;
62 } func_disp_t;
64 #define FUNCUNKNOWN(x) ((IUnknown*) &(x)->lpIUnknownVtbl)
66 struct dispex_dynamic_data_t {
67 DWORD buf_size;
68 DWORD prop_cnt;
69 dynamic_prop_t *props;
70 func_disp_t **func_disps;
73 #define DISPID_DYNPROP_0 0x50000000
74 #define DISPID_DYNPROP_MAX 0x5fffffff
76 #define FDEX_VERSION_MASK 0xf0000000
78 static ITypeLib *typelib;
79 static ITypeInfo *typeinfos[LAST_tid];
80 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
82 static REFIID tid_ids[] = {
83 &IID_NULL,
84 &DIID_DispCEventObj,
85 &DIID_DispDOMChildrenCollection,
86 &DIID_DispHTMLAnchorElement,
87 &DIID_DispHTMLBody,
88 &DIID_DispHTMLCommentElement,
89 &DIID_DispHTMLCurrentStyle,
90 &DIID_DispHTMLDocument,
91 &DIID_DispHTMLDOMTextNode,
92 &DIID_DispHTMLElementCollection,
93 &DIID_DispHTMLFormElement,
94 &DIID_DispHTMLGenericElement,
95 &DIID_DispHTMLFrameElement,
96 &DIID_DispHTMLIFrame,
97 &DIID_DispHTMLImg,
98 &DIID_DispHTMLInputElement,
99 &DIID_DispHTMLLocation,
100 &DIID_DispHTMLNavigator,
101 &DIID_DispHTMLOptionElement,
102 &DIID_DispHTMLScreen,
103 &DIID_DispHTMLSelectElement,
104 &DIID_DispHTMLStyle,
105 &DIID_DispHTMLTable,
106 &DIID_DispHTMLTableRow,
107 &DIID_DispHTMLUnknownElement,
108 &DIID_DispHTMLWindow2,
109 &DIID_HTMLDocumentEvents,
110 &IID_IHTMLAnchorElement,
111 &IID_IHTMLBodyElement,
112 &IID_IHTMLBodyElement2,
113 &IID_IHTMLCommentElement,
114 &IID_IHTMLCurrentStyle,
115 &IID_IHTMLCurrentStyle2,
116 &IID_IHTMLCurrentStyle3,
117 &IID_IHTMLCurrentStyle4,
118 &IID_IHTMLDocument2,
119 &IID_IHTMLDocument3,
120 &IID_IHTMLDocument4,
121 &IID_IHTMLDocument5,
122 &IID_IHTMLDOMChildrenCollection,
123 &IID_IHTMLDOMNode,
124 &IID_IHTMLDOMNode2,
125 &IID_IHTMLDOMTextNode,
126 &IID_IHTMLElement,
127 &IID_IHTMLElement2,
128 &IID_IHTMLElement3,
129 &IID_IHTMLElement4,
130 &IID_IHTMLElementCollection,
131 &IID_IHTMLEventObj,
132 &IID_IHTMLFiltersCollection,
133 &IID_IHTMLFormElement,
134 &IID_IHTMLFrameBase,
135 &IID_IHTMLFrameBase2,
136 &IID_IHTMLGenericElement,
137 &IID_IHTMLFrameElement3,
138 &IID_IHTMLIFrameElement,
139 &IID_IHTMLImageElementFactory,
140 &IID_IHTMLImgElement,
141 &IID_IHTMLInputElement,
142 &IID_IHTMLLocation,
143 &IID_IHTMLOptionElement,
144 &IID_IHTMLScreen,
145 &IID_IHTMLSelectElement,
146 &IID_IHTMLStyle,
147 &IID_IHTMLStyle2,
148 &IID_IHTMLStyle3,
149 &IID_IHTMLStyle4,
150 &IID_IHTMLTable,
151 &IID_IHTMLTableRow,
152 &IID_IHTMLTextContainer,
153 &IID_IHTMLUniqueName,
154 &IID_IHTMLWindow2,
155 &IID_IHTMLWindow3,
156 &IID_IHTMLWindow4,
157 &IID_IOmNavigator
160 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
162 HRESULT hres;
164 if(!typelib) {
165 ITypeLib *tl;
167 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
168 if(FAILED(hres)) {
169 ERR("LoadRegTypeLib failed: %08x\n", hres);
170 return hres;
173 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
174 ITypeLib_Release(tl);
177 if(!typeinfos[tid]) {
178 ITypeInfo *typeinfo;
180 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
181 if(FAILED(hres)) {
182 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
183 return hres;
186 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
187 ITypeInfo_Release(typeinfo);
190 *typeinfo = typeinfos[tid];
191 return S_OK;
194 void release_typelib(void)
196 dispex_data_t *iter;
197 unsigned i;
199 while(!list_empty(&dispex_data_list)) {
200 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
201 list_remove(&iter->entry);
203 for(i=0; i < iter->func_cnt; i++)
204 SysFreeString(iter->funcs[i].name);
206 heap_free(iter->funcs);
207 heap_free(iter->name_table);
208 heap_free(iter);
211 if(!typelib)
212 return;
214 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
215 if(typeinfos[i])
216 ITypeInfo_Release(typeinfos[i]);
218 ITypeLib_Release(typelib);
221 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
223 HRESULT hres;
225 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
226 return;
228 if(data->func_cnt == *size)
229 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
231 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
232 if(FAILED(hres))
233 return;
235 data->funcs[data->func_cnt].id = desc->memid;
236 data->funcs[data->func_cnt].tid = tid;
237 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
239 data->func_cnt++;
242 static int dispid_cmp(const void *p1, const void *p2)
244 return ((func_info_t*)p1)->id - ((func_info_t*)p2)->id;
247 static int func_name_cmp(const void *p1, const void *p2)
249 return strcmpiW((*(func_info_t**)p1)->name, (*(func_info_t**)p2)->name);
252 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
254 const tid_t *tid = This->data->iface_tids;
255 FUNCDESC *funcdesc;
256 dispex_data_t *data;
257 DWORD size = 16, i;
258 ITypeInfo *ti, *dti;
259 HRESULT hres;
261 TRACE("(%p)\n", This);
263 if(This->data->disp_tid) {
264 hres = get_typeinfo(This->data->disp_tid, &dti);
265 if(FAILED(hres)) {
266 ERR("Could not get disp type info: %08x\n", hres);
267 return NULL;
271 data = heap_alloc(sizeof(dispex_data_t));
272 data->func_cnt = 0;
273 data->func_disp_cnt = 0;
274 data->funcs = heap_alloc(size*sizeof(func_info_t));
275 list_add_tail(&dispex_data_list, &data->entry);
277 while(*tid) {
278 hres = get_typeinfo(*tid, &ti);
279 if(FAILED(hres))
280 break;
282 i=7;
283 while(1) {
284 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
285 if(FAILED(hres))
286 break;
288 add_func_info(data, &size, *tid, funcdesc, dti);
289 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
292 tid++;
295 if(!data->func_cnt) {
296 heap_free(data->funcs);
297 data->funcs = NULL;
298 }else if(data->func_cnt != size) {
299 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
302 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
304 if(data->funcs) {
305 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
306 for(i=0; i < data->func_cnt; i++)
307 data->name_table[i] = data->funcs+i;
308 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
309 }else {
310 data->name_table = NULL;
313 return data;
316 static int id_cmp(const void *p1, const void *p2)
318 return *(DISPID*)p1 - *(DISPID*)p2;
321 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
323 unsigned i, func_cnt;
324 FUNCDESC *funcdesc;
325 ITypeInfo *ti;
326 TYPEATTR *attr;
327 DISPID *ids;
328 HRESULT hres;
330 hres = get_typeinfo(tid, &ti);
331 if(FAILED(hres))
332 return hres;
334 hres = ITypeInfo_GetTypeAttr(ti, &attr);
335 if(FAILED(hres)) {
336 ITypeInfo_Release(ti);
337 return hres;
340 func_cnt = attr->cFuncs;
341 ITypeInfo_ReleaseTypeAttr(ti, attr);
343 ids = heap_alloc(func_cnt*sizeof(DISPID));
344 if(!ids) {
345 ITypeInfo_Release(ti);
346 return E_OUTOFMEMORY;
349 for(i=0; i < func_cnt; i++) {
350 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
351 if(FAILED(hres))
352 break;
354 ids[i] = funcdesc->memid;
355 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
358 ITypeInfo_Release(ti);
359 if(FAILED(hres)) {
360 heap_free(ids);
361 return hres;
364 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
366 *ret_size = func_cnt;
367 *ret = ids;
368 return S_OK;
371 static CRITICAL_SECTION cs_dispex_static_data;
372 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
374 0, 0, &cs_dispex_static_data,
375 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
376 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
378 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
381 static dispex_data_t *get_dispex_data(DispatchEx *This)
383 if(This->data->data)
384 return This->data->data;
386 EnterCriticalSection(&cs_dispex_static_data);
388 if(!This->data->data)
389 This->data->data = preprocess_dispex_data(This);
391 LeaveCriticalSection(&cs_dispex_static_data);
393 return This->data->data;
396 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
398 EXCEPINFO ei;
399 IDispatchEx *dispex;
400 VARIANT res;
401 HRESULT hres;
403 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
404 if(FAILED(hres)) {
405 FIXME("Could not get IDispatchEx interface: %08x\n", hres);
406 return hres;
409 VariantInit(&res);
410 memset(&ei, 0, sizeof(ei));
412 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
414 IDispatchEx_Release(dispex);
415 VariantClear(&res);
416 return hres;
419 static inline BOOL is_custom_dispid(DISPID id)
421 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
424 static inline BOOL is_dynamic_dispid(DISPID id)
426 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
429 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
431 return !alloc || This->dynamic_data
432 ? This->dynamic_data
433 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
436 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
438 const BOOL alloc = flags & fdexNameEnsure;
439 dispex_dynamic_data_t *data;
440 unsigned i;
442 data = get_dynamic_data(This, alloc);
443 if(!data) {
444 if(alloc)
445 return E_OUTOFMEMORY;
447 TRACE("not found %s\n", debugstr_w(name));
448 return DISP_E_UNKNOWNNAME;
451 for(i=0; i < data->prop_cnt; i++) {
452 if(flags & fdexNameCaseInsensitive ? !strcmpiW(data->props[i].name, name) : !strcmpW(data->props[i].name, name)) {
453 *ret = data->props+i;
454 return S_OK;
458 if(!alloc)
459 return DISP_E_UNKNOWNNAME;
461 TRACE("creating dynamic prop %s\n", debugstr_w(name));
463 if(!data->buf_size) {
464 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
465 if(!data->props)
466 return E_OUTOFMEMORY;
467 data->buf_size = 4;
468 }else if(data->buf_size == data->prop_cnt) {
469 dynamic_prop_t *new_props;
471 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
472 if(!new_props)
473 return E_OUTOFMEMORY;
475 data->props = new_props;
476 data->buf_size <<= 1;
479 data->props[data->prop_cnt].name = heap_strdupW(name);
480 VariantInit(&data->props[data->prop_cnt].var);
481 *ret = data->props + data->prop_cnt++;
483 return S_OK;
486 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
488 dynamic_prop_t *prop;
489 HRESULT hres;
491 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
492 if(FAILED(hres))
493 return hres;
495 *ret = &prop->var;
496 return S_OK;
499 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
500 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
502 if(This->data->vtbl && This->data->vtbl->value)
503 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
505 switch(flags) {
506 case DISPATCH_PROPERTYGET:
507 V_VT(res) = VT_BSTR;
508 V_BSTR(res) = SysAllocString(objectW);
509 if(!V_BSTR(res))
510 return E_OUTOFMEMORY;
511 break;
512 default:
513 FIXME("Unimplemented flags %x\n", flags);
514 return E_NOTIMPL;
517 return S_OK;
520 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
521 EXCEPINFO *ei)
523 ITypeInfo *ti;
524 IUnknown *unk;
525 UINT argerr=0;
526 HRESULT hres;
528 hres = get_typeinfo(func->tid, &ti);
529 if(FAILED(hres)) {
530 ERR("Could not get type info: %08x\n", hres);
531 return hres;
534 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
535 if(FAILED(hres)) {
536 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
537 return E_FAIL;
540 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
542 IUnknown_Release(unk);
543 return hres;
546 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
548 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
550 func_disp_t *This = FUNCTION_THIS(iface);
552 if(IsEqualGUID(&IID_IUnknown, riid)) {
553 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
554 *ppv = FUNCUNKNOWN(This);
555 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
556 return *ppv ? S_OK : E_NOINTERFACE;
557 }else {
558 *ppv = NULL;
559 return E_NOINTERFACE;
562 IUnknown_AddRef((IUnknown*)*ppv);
563 return S_OK;
566 static ULONG WINAPI Function_AddRef(IUnknown *iface)
568 func_disp_t *This = FUNCTION_THIS(iface);
570 TRACE("(%p)\n", This);
572 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
575 static ULONG WINAPI Function_Release(IUnknown *iface)
577 func_disp_t *This = FUNCTION_THIS(iface);
579 TRACE("(%p)\n", This);
581 return IDispatchEx_Release(DISPATCHEX(This->obj));
584 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
585 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
587 func_disp_t *This = FUNCTION_THIS(iface);
588 HRESULT hres;
590 switch(flags) {
591 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
592 if(!res)
593 return E_INVALIDARG;
594 case DISPATCH_METHOD:
595 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
596 break;
597 default:
598 FIXME("Unimplemented flags %x\n", flags);
599 hres = E_NOTIMPL;
602 return hres;
605 #undef FUNCTION_THIS
607 static const IUnknownVtbl FunctionUnkVtbl = {
608 Function_QueryInterface,
609 Function_AddRef,
610 Function_Release
613 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
614 function_value,
615 NULL,
616 NULL
619 static const tid_t function_iface_tids[] = {0};
621 static dispex_static_data_t function_dispex = {
622 &function_dispex_vtbl,
623 NULL_tid,
624 NULL,
625 function_iface_tids
628 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
630 func_disp_t *ret;
632 ret = heap_alloc_zero(sizeof(func_disp_t));
633 if(!ret)
634 return NULL;
636 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
637 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
638 ret->obj = obj;
639 ret->info = info;
641 return ret;
644 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
645 EXCEPINFO *ei)
647 HRESULT hres;
649 switch(flags) {
650 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
651 if(!res)
652 return E_INVALIDARG;
653 case DISPATCH_METHOD:
654 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
655 break;
656 case DISPATCH_PROPERTYGET: {
657 dispex_dynamic_data_t *dynamic_data;
659 if(func->id == DISPID_VALUE) {
660 BSTR ret;
662 ret = SysAllocString(objectW);
663 if(!ret)
664 return E_OUTOFMEMORY;
666 V_VT(res) = VT_BSTR;
667 V_BSTR(res) = ret;
668 return S_OK;
671 dynamic_data = get_dynamic_data(This, TRUE);
672 if(!dynamic_data)
673 return E_OUTOFMEMORY;
675 if(!dynamic_data->func_disps) {
676 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
677 if(!dynamic_data->func_disps)
678 return E_OUTOFMEMORY;
681 if(!dynamic_data->func_disps[func->func_disp_idx]) {
682 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
683 if(!dynamic_data->func_disps[func->func_disp_idx])
684 return E_OUTOFMEMORY;
687 V_VT(res) = VT_DISPATCH;
688 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
689 IDispatch_AddRef(V_DISPATCH(res));
690 hres = S_OK;
691 break;
693 default:
694 FIXME("Unimplemented flags %x\n", flags);
695 case DISPATCH_PROPERTYPUT:
696 hres = E_NOTIMPL;
699 return hres;
702 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
704 int min, max, n;
706 min = 0;
707 max = data->func_cnt-1;
709 while(min <= max) {
710 n = (min+max)/2;
712 if(data->funcs[n].id == id) {
713 *ret = data->funcs+n;
714 return S_OK;
717 if(data->funcs[n].id < id)
718 min = n+1;
719 else
720 max = n-1;
723 WARN("invalid id %x\n", id);
724 return DISP_E_UNKNOWNNAME;
727 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
729 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
731 DispatchEx *This = DISPATCHEX_THIS(iface);
733 return IUnknown_QueryInterface(This->outer, riid, ppv);
736 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
738 DispatchEx *This = DISPATCHEX_THIS(iface);
740 return IUnknown_AddRef(This->outer);
743 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
745 DispatchEx *This = DISPATCHEX_THIS(iface);
747 return IUnknown_Release(This->outer);
750 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
752 DispatchEx *This = DISPATCHEX_THIS(iface);
754 TRACE("(%p)->(%p)\n", This, pctinfo);
756 *pctinfo = 1;
757 return S_OK;
760 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
761 LCID lcid, ITypeInfo **ppTInfo)
763 DispatchEx *This = DISPATCHEX_THIS(iface);
764 HRESULT hres;
766 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
768 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
769 if(FAILED(hres))
770 return hres;
772 ITypeInfo_AddRef(*ppTInfo);
773 return S_OK;
776 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
777 LPOLESTR *rgszNames, UINT cNames,
778 LCID lcid, DISPID *rgDispId)
780 DispatchEx *This = DISPATCHEX_THIS(iface);
781 UINT i;
782 HRESULT hres;
784 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
785 lcid, rgDispId);
787 for(i=0; i < cNames; i++) {
788 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
789 if(FAILED(hres))
790 return hres;
793 return S_OK;
796 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
797 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
798 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
800 DispatchEx *This = DISPATCHEX_THIS(iface);
802 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
803 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
805 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
806 pDispParams, pVarResult, pExcepInfo, NULL);
809 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
811 DispatchEx *This = DISPATCHEX_THIS(iface);
812 dynamic_prop_t *dprop;
813 dispex_data_t *data;
814 int min, max, n, c;
815 HRESULT hres;
817 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
819 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
820 FIXME("Unsupported grfdex %x\n", grfdex);
822 data = get_dispex_data(This);
823 if(!data)
824 return E_FAIL;
826 min = 0;
827 max = data->func_cnt-1;
829 while(min <= max) {
830 n = (min+max)/2;
832 c = strcmpiW(data->name_table[n]->name, bstrName);
833 if(!c) {
834 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
835 break;
837 *pid = data->name_table[n]->id;
838 return S_OK;
841 if(c > 0)
842 max = n-1;
843 else
844 min = n+1;
847 if(This->data->vtbl && This->data->vtbl->get_dispid) {
848 HRESULT hres;
850 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
851 if(hres != DISP_E_UNKNOWNNAME)
852 return hres;
855 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
856 if(FAILED(hres))
857 return hres;
859 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
860 return S_OK;
863 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
864 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
866 DispatchEx *This = DISPATCHEX_THIS(iface);
867 dispex_data_t *data;
868 func_info_t *func;
869 HRESULT hres;
871 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
873 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
874 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
876 if(wFlags == DISPATCH_CONSTRUCT) {
877 if(id == DISPID_VALUE) {
878 if(This->data->vtbl && This->data->vtbl->value) {
879 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
880 pvarRes, pei, pspCaller);
882 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
883 return E_FAIL;
885 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
886 return E_FAIL;
889 if(is_dynamic_dispid(id)) {
890 DWORD idx = id - DISPID_DYNPROP_0;
891 VARIANT *var;
893 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
894 return DISP_E_UNKNOWNNAME;
896 var = &This->dynamic_data->props[idx].var;
898 switch(wFlags) {
899 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
900 if(!pvarRes)
901 return E_INVALIDARG;
902 case DISPATCH_METHOD: {
903 DISPID named_arg = DISPID_THIS;
904 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
905 IDispatchEx *dispex;
907 if(V_VT(var) != VT_DISPATCH) {
908 FIXME("invoke vt %d\n", V_VT(var));
909 return E_NOTIMPL;
912 if(pdp->cNamedArgs) {
913 FIXME("named args not supported\n");
914 return E_NOTIMPL;
917 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
918 if(!dp.rgvarg)
919 return E_OUTOFMEMORY;
921 dp.cArgs = pdp->cArgs+1;
922 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
924 V_VT(dp.rgvarg) = VT_DISPATCH;
925 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
927 hres = IDispatch_QueryInterface(V_DISPATCH(var), &IID_IDispatchEx, (void**)&dispex);
928 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
929 if(SUCCEEDED(hres)) {
930 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
931 IDispatchEx_Release(dispex);
932 }else {
933 ULONG err = 0;
934 hres = IDispatch_Invoke(V_DISPATCH(var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
936 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
938 heap_free(dp.rgvarg);
939 return hres;
941 case DISPATCH_PROPERTYGET:
942 return VariantCopy(pvarRes, var);
943 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
944 case DISPATCH_PROPERTYPUT:
945 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
946 || pdp->cNamedArgs > 1) {
947 FIXME("invalid args\n");
948 return E_INVALIDARG;
951 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
952 VariantClear(var);
953 return VariantCopy(var, pdp->rgvarg);
954 default:
955 FIXME("unhandled wFlags %x\n", wFlags);
956 return E_NOTIMPL;
960 data = get_dispex_data(This);
961 if(!data)
962 return E_FAIL;
964 hres = get_builtin_func(data, id, &func);
965 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
966 return dispex_value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
967 if(FAILED(hres))
968 return hres;
970 if(func->func_disp_idx == -1)
971 hres = typeinfo_invoke(This, func, wFlags, pdp, pvarRes, pei);
972 else
973 hres = function_invoke(This, func, wFlags, pdp, pvarRes, pei);
975 return hres;
978 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
980 DispatchEx *This = DISPATCHEX_THIS(iface);
981 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
982 return E_NOTIMPL;
985 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
987 DispatchEx *This = DISPATCHEX_THIS(iface);
988 FIXME("(%p)->(%x)\n", This, id);
989 return E_NOTIMPL;
992 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
994 DispatchEx *This = DISPATCHEX_THIS(iface);
995 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
996 return E_NOTIMPL;
999 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1001 DispatchEx *This = DISPATCHEX_THIS(iface);
1002 dispex_data_t *data;
1003 func_info_t *func;
1004 HRESULT hres;
1006 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1008 if(is_dynamic_dispid(id)) {
1009 DWORD idx = id - DISPID_DYNPROP_0;
1011 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1012 return DISP_E_UNKNOWNNAME;
1014 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1015 if(!*pbstrName)
1016 return E_OUTOFMEMORY;
1018 return S_OK;
1021 data = get_dispex_data(This);
1022 if(!data)
1023 return E_FAIL;
1025 hres = get_builtin_func(data, id, &func);
1026 if(FAILED(hres))
1027 return hres;
1029 *pbstrName = SysAllocString(func->name);
1030 if(!*pbstrName)
1031 return E_OUTOFMEMORY;
1032 return S_OK;
1035 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1037 DispatchEx *This = DISPATCHEX_THIS(iface);
1038 dispex_data_t *data;
1039 func_info_t *func;
1040 HRESULT hres;
1042 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1044 if(is_dynamic_dispid(id)) {
1045 DWORD idx = id - DISPID_DYNPROP_0;
1047 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1048 return DISP_E_UNKNOWNNAME;
1050 if(idx+1 == This->dynamic_data->prop_cnt) {
1051 *pid = DISPID_STARTENUM;
1052 return S_FALSE;
1055 *pid = id+1;
1056 return S_OK;
1059 data = get_dispex_data(This);
1060 if(!data)
1061 return E_FAIL;
1063 if(id == DISPID_STARTENUM) {
1064 func = data->funcs;
1065 }else {
1066 hres = get_builtin_func(data, id, &func);
1067 if(FAILED(hres))
1068 return hres;
1069 func++;
1072 while(func < data->funcs+data->func_cnt) {
1073 /* FIXME: Skip hidden properties */
1074 if(func->func_disp_idx == -1) {
1075 *pid = func->id;
1076 return S_OK;
1078 func++;
1081 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1082 *pid = DISPID_DYNPROP_0;
1083 return S_OK;
1086 *pid = DISPID_STARTENUM;
1087 return S_FALSE;
1090 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1092 DispatchEx *This = DISPATCHEX_THIS(iface);
1093 FIXME("(%p)->(%p)\n", This, ppunk);
1094 return E_NOTIMPL;
1097 #undef DISPATCHEX_THIS
1099 static IDispatchExVtbl DispatchExVtbl = {
1100 DispatchEx_QueryInterface,
1101 DispatchEx_AddRef,
1102 DispatchEx_Release,
1103 DispatchEx_GetTypeInfoCount,
1104 DispatchEx_GetTypeInfo,
1105 DispatchEx_GetIDsOfNames,
1106 DispatchEx_Invoke,
1107 DispatchEx_GetDispID,
1108 DispatchEx_InvokeEx,
1109 DispatchEx_DeleteMemberByName,
1110 DispatchEx_DeleteMemberByDispID,
1111 DispatchEx_GetMemberProperties,
1112 DispatchEx_GetMemberName,
1113 DispatchEx_GetNextDispID,
1114 DispatchEx_GetNameSpaceParent
1117 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1119 static const IID IID_UndocumentedScriptIface =
1120 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1121 static const IID IID_IDispatchJS =
1122 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1124 if(IsEqualGUID(&IID_IDispatch, riid)) {
1125 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1126 *ppv = DISPATCHEX(This);
1127 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1128 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1129 *ppv = DISPATCHEX(This);
1130 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1131 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1132 *ppv = NULL;
1133 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1134 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1135 *ppv = NULL;
1136 }else {
1137 return FALSE;
1140 if(*ppv)
1141 IUnknown_AddRef((IUnknown*)*ppv);
1142 return TRUE;
1145 void release_dispex(DispatchEx *This)
1147 dynamic_prop_t *prop;
1149 if(!This->dynamic_data)
1150 return;
1152 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1153 VariantClear(&prop->var);
1154 heap_free(prop->name);
1157 heap_free(This->dynamic_data->props);
1159 if(This->dynamic_data->func_disps) {
1160 unsigned i;
1162 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1163 if(This->dynamic_data->func_disps[i]) {
1164 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1165 heap_free(This->dynamic_data->func_disps[i]);
1169 heap_free(This->dynamic_data->func_disps);
1172 heap_free(This->dynamic_data);
1175 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1177 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1178 dispex->outer = outer;
1179 dispex->data = data;
1180 dispex->dynamic_data = NULL;