mshtml: Moved builtin property invocation to separate function.
[wine/hacks.git] / dlls / mshtml / dispex.c
blobc5e6de217092c056ce299cd676f82ec5d0969da6
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_DispHTMLScriptElement,
104 &DIID_DispHTMLSelectElement,
105 &DIID_DispHTMLStyle,
106 &DIID_DispHTMLTable,
107 &DIID_DispHTMLTableRow,
108 &DIID_DispHTMLTextAreaElement,
109 &DIID_DispHTMLUnknownElement,
110 &DIID_DispHTMLWindow2,
111 &DIID_HTMLDocumentEvents,
112 &IID_IHTMLAnchorElement,
113 &IID_IHTMLBodyElement,
114 &IID_IHTMLBodyElement2,
115 &IID_IHTMLCommentElement,
116 &IID_IHTMLCurrentStyle,
117 &IID_IHTMLCurrentStyle2,
118 &IID_IHTMLCurrentStyle3,
119 &IID_IHTMLCurrentStyle4,
120 &IID_IHTMLDocument2,
121 &IID_IHTMLDocument3,
122 &IID_IHTMLDocument4,
123 &IID_IHTMLDocument5,
124 &IID_IHTMLDOMChildrenCollection,
125 &IID_IHTMLDOMNode,
126 &IID_IHTMLDOMNode2,
127 &IID_IHTMLDOMTextNode,
128 &IID_IHTMLElement,
129 &IID_IHTMLElement2,
130 &IID_IHTMLElement3,
131 &IID_IHTMLElement4,
132 &IID_IHTMLElementCollection,
133 &IID_IHTMLEventObj,
134 &IID_IHTMLFiltersCollection,
135 &IID_IHTMLFormElement,
136 &IID_IHTMLFrameBase,
137 &IID_IHTMLFrameBase2,
138 &IID_IHTMLGenericElement,
139 &IID_IHTMLFrameElement3,
140 &IID_IHTMLIFrameElement,
141 &IID_IHTMLImageElementFactory,
142 &IID_IHTMLImgElement,
143 &IID_IHTMLInputElement,
144 &IID_IHTMLLocation,
145 &IID_IHTMLOptionElement,
146 &IID_IHTMLScreen,
147 &IID_IHTMLScriptElement,
148 &IID_IHTMLSelectElement,
149 &IID_IHTMLStyle,
150 &IID_IHTMLStyle2,
151 &IID_IHTMLStyle3,
152 &IID_IHTMLStyle4,
153 &IID_IHTMLTable,
154 &IID_IHTMLTableRow,
155 &IID_IHTMLTextAreaElement,
156 &IID_IHTMLTextContainer,
157 &IID_IHTMLUniqueName,
158 &IID_IHTMLWindow2,
159 &IID_IHTMLWindow3,
160 &IID_IHTMLWindow4,
161 &IID_IOmNavigator
164 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
166 HRESULT hres;
168 if(!typelib) {
169 ITypeLib *tl;
171 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
172 if(FAILED(hres)) {
173 ERR("LoadRegTypeLib failed: %08x\n", hres);
174 return hres;
177 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
178 ITypeLib_Release(tl);
181 if(!typeinfos[tid]) {
182 ITypeInfo *typeinfo;
184 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
185 if(FAILED(hres)) {
186 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
187 return hres;
190 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
191 ITypeInfo_Release(typeinfo);
194 *typeinfo = typeinfos[tid];
195 return S_OK;
198 void release_typelib(void)
200 dispex_data_t *iter;
201 unsigned i;
203 while(!list_empty(&dispex_data_list)) {
204 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
205 list_remove(&iter->entry);
207 for(i=0; i < iter->func_cnt; i++)
208 SysFreeString(iter->funcs[i].name);
210 heap_free(iter->funcs);
211 heap_free(iter->name_table);
212 heap_free(iter);
215 if(!typelib)
216 return;
218 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
219 if(typeinfos[i])
220 ITypeInfo_Release(typeinfos[i]);
222 ITypeLib_Release(typelib);
225 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
227 HRESULT hres;
229 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
230 return;
232 if(data->func_cnt == *size)
233 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
235 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
236 if(FAILED(hres))
237 return;
239 data->funcs[data->func_cnt].id = desc->memid;
240 data->funcs[data->func_cnt].tid = tid;
241 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
243 data->func_cnt++;
246 static int dispid_cmp(const void *p1, const void *p2)
248 return ((func_info_t*)p1)->id - ((func_info_t*)p2)->id;
251 static int func_name_cmp(const void *p1, const void *p2)
253 return strcmpiW((*(func_info_t**)p1)->name, (*(func_info_t**)p2)->name);
256 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
258 const tid_t *tid = This->data->iface_tids;
259 FUNCDESC *funcdesc;
260 dispex_data_t *data;
261 DWORD size = 16, i;
262 ITypeInfo *ti, *dti;
263 HRESULT hres;
265 TRACE("(%p)\n", This);
267 if(This->data->disp_tid) {
268 hres = get_typeinfo(This->data->disp_tid, &dti);
269 if(FAILED(hres)) {
270 ERR("Could not get disp type info: %08x\n", hres);
271 return NULL;
275 data = heap_alloc(sizeof(dispex_data_t));
276 data->func_cnt = 0;
277 data->func_disp_cnt = 0;
278 data->funcs = heap_alloc(size*sizeof(func_info_t));
279 list_add_tail(&dispex_data_list, &data->entry);
281 while(*tid) {
282 hres = get_typeinfo(*tid, &ti);
283 if(FAILED(hres))
284 break;
286 i=7;
287 while(1) {
288 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
289 if(FAILED(hres))
290 break;
292 add_func_info(data, &size, *tid, funcdesc, dti);
293 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
296 tid++;
299 if(!data->func_cnt) {
300 heap_free(data->funcs);
301 data->funcs = NULL;
302 }else if(data->func_cnt != size) {
303 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
306 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
308 if(data->funcs) {
309 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
310 for(i=0; i < data->func_cnt; i++)
311 data->name_table[i] = data->funcs+i;
312 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
313 }else {
314 data->name_table = NULL;
317 return data;
320 static int id_cmp(const void *p1, const void *p2)
322 return *(DISPID*)p1 - *(DISPID*)p2;
325 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
327 unsigned i, func_cnt;
328 FUNCDESC *funcdesc;
329 ITypeInfo *ti;
330 TYPEATTR *attr;
331 DISPID *ids;
332 HRESULT hres;
334 hres = get_typeinfo(tid, &ti);
335 if(FAILED(hres))
336 return hres;
338 hres = ITypeInfo_GetTypeAttr(ti, &attr);
339 if(FAILED(hres)) {
340 ITypeInfo_Release(ti);
341 return hres;
344 func_cnt = attr->cFuncs;
345 ITypeInfo_ReleaseTypeAttr(ti, attr);
347 ids = heap_alloc(func_cnt*sizeof(DISPID));
348 if(!ids) {
349 ITypeInfo_Release(ti);
350 return E_OUTOFMEMORY;
353 for(i=0; i < func_cnt; i++) {
354 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
355 if(FAILED(hres))
356 break;
358 ids[i] = funcdesc->memid;
359 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
362 ITypeInfo_Release(ti);
363 if(FAILED(hres)) {
364 heap_free(ids);
365 return hres;
368 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
370 *ret_size = func_cnt;
371 *ret = ids;
372 return S_OK;
375 static CRITICAL_SECTION cs_dispex_static_data;
376 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
378 0, 0, &cs_dispex_static_data,
379 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
380 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
382 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
385 static dispex_data_t *get_dispex_data(DispatchEx *This)
387 if(This->data->data)
388 return This->data->data;
390 EnterCriticalSection(&cs_dispex_static_data);
392 if(!This->data->data)
393 This->data->data = preprocess_dispex_data(This);
395 LeaveCriticalSection(&cs_dispex_static_data);
397 return This->data->data;
400 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
402 EXCEPINFO ei;
403 IDispatchEx *dispex;
404 VARIANT res;
405 HRESULT hres;
407 VariantInit(&res);
408 memset(&ei, 0, sizeof(ei));
410 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
411 if(SUCCEEDED(hres)) {
412 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
413 IDispatchEx_Release(dispex);
414 }else {
415 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
416 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
417 dp, &res, &ei, NULL);
420 VariantClear(&res);
421 return hres;
424 static inline BOOL is_custom_dispid(DISPID id)
426 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
429 static inline BOOL is_dynamic_dispid(DISPID id)
431 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
434 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
436 return !alloc || This->dynamic_data
437 ? This->dynamic_data
438 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
441 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
443 const BOOL alloc = flags & fdexNameEnsure;
444 dispex_dynamic_data_t *data;
445 unsigned i;
447 data = get_dynamic_data(This, alloc);
448 if(!data) {
449 if(alloc)
450 return E_OUTOFMEMORY;
452 TRACE("not found %s\n", debugstr_w(name));
453 return DISP_E_UNKNOWNNAME;
456 for(i=0; i < data->prop_cnt; i++) {
457 if(flags & fdexNameCaseInsensitive ? !strcmpiW(data->props[i].name, name) : !strcmpW(data->props[i].name, name)) {
458 *ret = data->props+i;
459 return S_OK;
463 if(!alloc)
464 return DISP_E_UNKNOWNNAME;
466 TRACE("creating dynamic prop %s\n", debugstr_w(name));
468 if(!data->buf_size) {
469 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
470 if(!data->props)
471 return E_OUTOFMEMORY;
472 data->buf_size = 4;
473 }else if(data->buf_size == data->prop_cnt) {
474 dynamic_prop_t *new_props;
476 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
477 if(!new_props)
478 return E_OUTOFMEMORY;
480 data->props = new_props;
481 data->buf_size <<= 1;
484 data->props[data->prop_cnt].name = heap_strdupW(name);
485 VariantInit(&data->props[data->prop_cnt].var);
486 *ret = data->props + data->prop_cnt++;
488 return S_OK;
491 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
493 dynamic_prop_t *prop;
494 HRESULT hres;
496 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
497 if(FAILED(hres))
498 return hres;
500 *ret = &prop->var;
501 return S_OK;
504 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
505 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
507 if(This->data->vtbl && This->data->vtbl->value)
508 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
510 switch(flags) {
511 case DISPATCH_PROPERTYGET:
512 V_VT(res) = VT_BSTR;
513 V_BSTR(res) = SysAllocString(objectW);
514 if(!V_BSTR(res))
515 return E_OUTOFMEMORY;
516 break;
517 default:
518 FIXME("Unimplemented flags %x\n", flags);
519 return E_NOTIMPL;
522 return S_OK;
525 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
526 EXCEPINFO *ei)
528 ITypeInfo *ti;
529 IUnknown *unk;
530 UINT argerr=0;
531 HRESULT hres;
533 hres = get_typeinfo(func->tid, &ti);
534 if(FAILED(hres)) {
535 ERR("Could not get type info: %08x\n", hres);
536 return hres;
539 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
540 if(FAILED(hres)) {
541 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
542 return E_FAIL;
545 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
547 IUnknown_Release(unk);
548 return hres;
551 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
553 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
555 func_disp_t *This = FUNCTION_THIS(iface);
557 if(IsEqualGUID(&IID_IUnknown, riid)) {
558 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
559 *ppv = FUNCUNKNOWN(This);
560 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
561 return *ppv ? S_OK : E_NOINTERFACE;
562 }else {
563 *ppv = NULL;
564 return E_NOINTERFACE;
567 IUnknown_AddRef((IUnknown*)*ppv);
568 return S_OK;
571 static ULONG WINAPI Function_AddRef(IUnknown *iface)
573 func_disp_t *This = FUNCTION_THIS(iface);
575 TRACE("(%p)\n", This);
577 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
580 static ULONG WINAPI Function_Release(IUnknown *iface)
582 func_disp_t *This = FUNCTION_THIS(iface);
584 TRACE("(%p)\n", This);
586 return IDispatchEx_Release(DISPATCHEX(This->obj));
589 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
590 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
592 func_disp_t *This = FUNCTION_THIS(iface);
593 HRESULT hres;
595 switch(flags) {
596 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
597 if(!res)
598 return E_INVALIDARG;
599 case DISPATCH_METHOD:
600 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
601 break;
602 default:
603 FIXME("Unimplemented flags %x\n", flags);
604 hres = E_NOTIMPL;
607 return hres;
610 #undef FUNCTION_THIS
612 static const IUnknownVtbl FunctionUnkVtbl = {
613 Function_QueryInterface,
614 Function_AddRef,
615 Function_Release
618 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
619 function_value,
620 NULL,
621 NULL
624 static const tid_t function_iface_tids[] = {0};
626 static dispex_static_data_t function_dispex = {
627 &function_dispex_vtbl,
628 NULL_tid,
629 NULL,
630 function_iface_tids
633 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
635 func_disp_t *ret;
637 ret = heap_alloc_zero(sizeof(func_disp_t));
638 if(!ret)
639 return NULL;
641 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
642 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
643 ret->obj = obj;
644 ret->info = info;
646 return ret;
649 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
650 EXCEPINFO *ei)
652 HRESULT hres;
654 switch(flags) {
655 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
656 if(!res)
657 return E_INVALIDARG;
658 case DISPATCH_METHOD:
659 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
660 break;
661 case DISPATCH_PROPERTYGET: {
662 dispex_dynamic_data_t *dynamic_data;
664 if(func->id == DISPID_VALUE) {
665 BSTR ret;
667 ret = SysAllocString(objectW);
668 if(!ret)
669 return E_OUTOFMEMORY;
671 V_VT(res) = VT_BSTR;
672 V_BSTR(res) = ret;
673 return S_OK;
676 dynamic_data = get_dynamic_data(This, TRUE);
677 if(!dynamic_data)
678 return E_OUTOFMEMORY;
680 if(!dynamic_data->func_disps) {
681 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
682 if(!dynamic_data->func_disps)
683 return E_OUTOFMEMORY;
686 if(!dynamic_data->func_disps[func->func_disp_idx]) {
687 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
688 if(!dynamic_data->func_disps[func->func_disp_idx])
689 return E_OUTOFMEMORY;
692 V_VT(res) = VT_DISPATCH;
693 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
694 IDispatch_AddRef(V_DISPATCH(res));
695 hres = S_OK;
696 break;
698 default:
699 FIXME("Unimplemented flags %x\n", flags);
700 case DISPATCH_PROPERTYPUT:
701 hres = E_NOTIMPL;
704 return hres;
707 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
709 int min, max, n;
711 min = 0;
712 max = data->func_cnt-1;
714 while(min <= max) {
715 n = (min+max)/2;
717 if(data->funcs[n].id == id) {
718 *ret = data->funcs+n;
719 return S_OK;
722 if(data->funcs[n].id < id)
723 min = n+1;
724 else
725 max = n-1;
728 WARN("invalid id %x\n", id);
729 return DISP_E_UNKNOWNNAME;
732 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
734 dispex_data_t *data;
735 int min, max, n, c;
737 data = get_dispex_data(This);
738 if(!data)
739 return E_FAIL;
741 min = 0;
742 max = data->func_cnt-1;
744 while(min <= max) {
745 n = (min+max)/2;
747 c = strcmpiW(data->name_table[n]->name, name);
748 if(!c) {
749 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
750 break;
752 *ret = data->name_table[n]->id;
753 return S_OK;
756 if(c > 0)
757 max = n-1;
758 else
759 min = n+1;
762 if(This->data->vtbl && This->data->vtbl->get_dispid) {
763 HRESULT hres;
765 hres = This->data->vtbl->get_dispid(This->outer, name, grfdex, ret);
766 if(hres != DISP_E_UNKNOWNNAME)
767 return hres;
770 return DISP_E_UNKNOWNNAME;
773 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
774 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
776 dispex_data_t *data;
777 func_info_t *func;
778 HRESULT hres;
780 data = get_dispex_data(This);
781 if(!data)
782 return E_FAIL;
784 hres = get_builtin_func(data, id, &func);
785 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
786 return dispex_value(This, lcid, flags, dp, res, ei, caller);
787 if(FAILED(hres))
788 return hres;
790 if(func->func_disp_idx == -1)
791 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
792 else
793 hres = function_invoke(This, func, flags, dp, res, ei);
795 return hres;
798 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
800 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
802 DispatchEx *This = DISPATCHEX_THIS(iface);
804 return IUnknown_QueryInterface(This->outer, riid, ppv);
807 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
809 DispatchEx *This = DISPATCHEX_THIS(iface);
811 return IUnknown_AddRef(This->outer);
814 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
816 DispatchEx *This = DISPATCHEX_THIS(iface);
818 return IUnknown_Release(This->outer);
821 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
823 DispatchEx *This = DISPATCHEX_THIS(iface);
825 TRACE("(%p)->(%p)\n", This, pctinfo);
827 *pctinfo = 1;
828 return S_OK;
831 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
832 LCID lcid, ITypeInfo **ppTInfo)
834 DispatchEx *This = DISPATCHEX_THIS(iface);
835 HRESULT hres;
837 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
839 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
840 if(FAILED(hres))
841 return hres;
843 ITypeInfo_AddRef(*ppTInfo);
844 return S_OK;
847 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
848 LPOLESTR *rgszNames, UINT cNames,
849 LCID lcid, DISPID *rgDispId)
851 DispatchEx *This = DISPATCHEX_THIS(iface);
852 UINT i;
853 HRESULT hres;
855 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
856 lcid, rgDispId);
858 for(i=0; i < cNames; i++) {
859 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
860 if(FAILED(hres))
861 return hres;
864 return S_OK;
867 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
868 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
869 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
871 DispatchEx *This = DISPATCHEX_THIS(iface);
873 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
874 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
876 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
877 pDispParams, pVarResult, pExcepInfo, NULL);
880 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
882 DispatchEx *This = DISPATCHEX_THIS(iface);
883 dynamic_prop_t *dprop;
884 HRESULT hres;
886 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
888 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
889 FIXME("Unsupported grfdex %x\n", grfdex);
891 hres = get_builtin_id(This, bstrName, grfdex, pid);
892 if(hres != DISP_E_UNKNOWNNAME)
893 return hres;
895 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
896 if(FAILED(hres))
897 return hres;
899 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
900 return S_OK;
903 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
904 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
906 DispatchEx *This = DISPATCHEX_THIS(iface);
907 HRESULT hres;
909 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
911 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
912 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
914 if(wFlags == DISPATCH_CONSTRUCT) {
915 if(id == DISPID_VALUE) {
916 if(This->data->vtbl && This->data->vtbl->value) {
917 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
918 pvarRes, pei, pspCaller);
920 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
921 return E_FAIL;
923 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
924 return E_FAIL;
927 if(is_dynamic_dispid(id)) {
928 DWORD idx = id - DISPID_DYNPROP_0;
929 VARIANT *var;
931 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
932 return DISP_E_UNKNOWNNAME;
934 var = &This->dynamic_data->props[idx].var;
936 switch(wFlags) {
937 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
938 if(!pvarRes)
939 return E_INVALIDARG;
940 case DISPATCH_METHOD: {
941 DISPID named_arg = DISPID_THIS;
942 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
943 IDispatchEx *dispex;
945 if(V_VT(var) != VT_DISPATCH) {
946 FIXME("invoke vt %d\n", V_VT(var));
947 return E_NOTIMPL;
950 if(pdp->cNamedArgs) {
951 FIXME("named args not supported\n");
952 return E_NOTIMPL;
955 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
956 if(!dp.rgvarg)
957 return E_OUTOFMEMORY;
959 dp.cArgs = pdp->cArgs+1;
960 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
962 V_VT(dp.rgvarg) = VT_DISPATCH;
963 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
965 hres = IDispatch_QueryInterface(V_DISPATCH(var), &IID_IDispatchEx, (void**)&dispex);
966 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
967 if(SUCCEEDED(hres)) {
968 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
969 IDispatchEx_Release(dispex);
970 }else {
971 ULONG err = 0;
972 hres = IDispatch_Invoke(V_DISPATCH(var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
974 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
976 heap_free(dp.rgvarg);
977 return hres;
979 case DISPATCH_PROPERTYGET:
980 return VariantCopy(pvarRes, var);
981 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
982 case DISPATCH_PROPERTYPUT:
983 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
984 || pdp->cNamedArgs > 1) {
985 FIXME("invalid args\n");
986 return E_INVALIDARG;
989 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
990 VariantClear(var);
991 return VariantCopy(var, pdp->rgvarg);
992 default:
993 FIXME("unhandled wFlags %x\n", wFlags);
994 return E_NOTIMPL;
998 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1001 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1003 DispatchEx *This = DISPATCHEX_THIS(iface);
1005 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1007 /* Not implemented by IE */
1008 return E_NOTIMPL;
1011 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1013 DispatchEx *This = DISPATCHEX_THIS(iface);
1014 FIXME("(%p)->(%x)\n", This, id);
1015 return E_NOTIMPL;
1018 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1020 DispatchEx *This = DISPATCHEX_THIS(iface);
1021 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1022 return E_NOTIMPL;
1025 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1027 DispatchEx *This = DISPATCHEX_THIS(iface);
1028 dispex_data_t *data;
1029 func_info_t *func;
1030 HRESULT hres;
1032 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1034 if(is_dynamic_dispid(id)) {
1035 DWORD idx = id - DISPID_DYNPROP_0;
1037 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1038 return DISP_E_UNKNOWNNAME;
1040 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1041 if(!*pbstrName)
1042 return E_OUTOFMEMORY;
1044 return S_OK;
1047 data = get_dispex_data(This);
1048 if(!data)
1049 return E_FAIL;
1051 hres = get_builtin_func(data, id, &func);
1052 if(FAILED(hres))
1053 return hres;
1055 *pbstrName = SysAllocString(func->name);
1056 if(!*pbstrName)
1057 return E_OUTOFMEMORY;
1058 return S_OK;
1061 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1063 DispatchEx *This = DISPATCHEX_THIS(iface);
1064 dispex_data_t *data;
1065 func_info_t *func;
1066 HRESULT hres;
1068 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1070 if(is_dynamic_dispid(id)) {
1071 DWORD idx = id - DISPID_DYNPROP_0;
1073 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1074 return DISP_E_UNKNOWNNAME;
1076 if(idx+1 == This->dynamic_data->prop_cnt) {
1077 *pid = DISPID_STARTENUM;
1078 return S_FALSE;
1081 *pid = id+1;
1082 return S_OK;
1085 data = get_dispex_data(This);
1086 if(!data)
1087 return E_FAIL;
1089 if(id == DISPID_STARTENUM) {
1090 func = data->funcs;
1091 }else {
1092 hres = get_builtin_func(data, id, &func);
1093 if(FAILED(hres))
1094 return hres;
1095 func++;
1098 while(func < data->funcs+data->func_cnt) {
1099 /* FIXME: Skip hidden properties */
1100 if(func->func_disp_idx == -1) {
1101 *pid = func->id;
1102 return S_OK;
1104 func++;
1107 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1108 *pid = DISPID_DYNPROP_0;
1109 return S_OK;
1112 *pid = DISPID_STARTENUM;
1113 return S_FALSE;
1116 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1118 DispatchEx *This = DISPATCHEX_THIS(iface);
1119 FIXME("(%p)->(%p)\n", This, ppunk);
1120 return E_NOTIMPL;
1123 #undef DISPATCHEX_THIS
1125 static IDispatchExVtbl DispatchExVtbl = {
1126 DispatchEx_QueryInterface,
1127 DispatchEx_AddRef,
1128 DispatchEx_Release,
1129 DispatchEx_GetTypeInfoCount,
1130 DispatchEx_GetTypeInfo,
1131 DispatchEx_GetIDsOfNames,
1132 DispatchEx_Invoke,
1133 DispatchEx_GetDispID,
1134 DispatchEx_InvokeEx,
1135 DispatchEx_DeleteMemberByName,
1136 DispatchEx_DeleteMemberByDispID,
1137 DispatchEx_GetMemberProperties,
1138 DispatchEx_GetMemberName,
1139 DispatchEx_GetNextDispID,
1140 DispatchEx_GetNameSpaceParent
1143 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1145 static const IID IID_UndocumentedScriptIface =
1146 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1147 static const IID IID_IDispatchJS =
1148 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1150 if(IsEqualGUID(&IID_IDispatch, riid)) {
1151 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1152 *ppv = DISPATCHEX(This);
1153 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1154 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1155 *ppv = DISPATCHEX(This);
1156 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1157 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1158 *ppv = NULL;
1159 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1160 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1161 *ppv = NULL;
1162 }else {
1163 return FALSE;
1166 if(*ppv)
1167 IUnknown_AddRef((IUnknown*)*ppv);
1168 return TRUE;
1171 void release_dispex(DispatchEx *This)
1173 dynamic_prop_t *prop;
1175 if(!This->dynamic_data)
1176 return;
1178 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1179 VariantClear(&prop->var);
1180 heap_free(prop->name);
1183 heap_free(This->dynamic_data->props);
1185 if(This->dynamic_data->func_disps) {
1186 unsigned i;
1188 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1189 if(This->dynamic_data->func_disps[i]) {
1190 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1191 heap_free(This->dynamic_data->func_disps[i]);
1195 heap_free(This->dynamic_data->func_disps);
1198 heap_free(This->dynamic_data);
1201 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1203 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1204 dispex->outer = outer;
1205 dispex->data = data;
1206 dispex->dynamic_data = NULL;