include: Specify calling convention explicitly in idl files where needed.
[wine/hramrach.git] / dlls / mshtml / dispex.c
blob64356f5fde0971d215cbe5c8d01dcb2edff2bb28
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 DWORD flags;
56 } dynamic_prop_t;
58 #define DYNPROP_DELETED 0x01
60 typedef struct {
61 DispatchEx dispex;
62 const IUnknownVtbl *lpIUnknownVtbl;
63 DispatchEx *obj;
64 func_info_t *info;
65 } func_disp_t;
67 #define FUNCUNKNOWN(x) ((IUnknown*) &(x)->lpIUnknownVtbl)
69 struct dispex_dynamic_data_t {
70 DWORD buf_size;
71 DWORD prop_cnt;
72 dynamic_prop_t *props;
73 func_disp_t **func_disps;
76 #define DISPID_DYNPROP_0 0x50000000
77 #define DISPID_DYNPROP_MAX 0x5fffffff
79 #define FDEX_VERSION_MASK 0xf0000000
81 static ITypeLib *typelib;
82 static ITypeInfo *typeinfos[LAST_tid];
83 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
85 static REFIID tid_ids[] = {
86 &IID_NULL,
87 &DIID_DispCEventObj,
88 &DIID_DispDOMChildrenCollection,
89 &DIID_DispHTMLAnchorElement,
90 &DIID_DispHTMLBody,
91 &DIID_DispHTMLCommentElement,
92 &DIID_DispHTMLCurrentStyle,
93 &DIID_DispHTMLDocument,
94 &DIID_DispHTMLDOMTextNode,
95 &DIID_DispHTMLElementCollection,
96 &DIID_DispHTMLFormElement,
97 &DIID_DispHTMLGenericElement,
98 &DIID_DispHTMLFrameElement,
99 &DIID_DispHTMLIFrame,
100 &DIID_DispHTMLImg,
101 &DIID_DispHTMLInputElement,
102 &DIID_DispHTMLLocation,
103 &DIID_DispHTMLNavigator,
104 &DIID_DispHTMLOptionElement,
105 &DIID_DispHTMLScreen,
106 &DIID_DispHTMLScriptElement,
107 &DIID_DispHTMLSelectElement,
108 &DIID_DispHTMLStyle,
109 &DIID_DispHTMLTable,
110 &DIID_DispHTMLTableRow,
111 &DIID_DispHTMLTextAreaElement,
112 &DIID_DispHTMLUnknownElement,
113 &DIID_DispHTMLWindow2,
114 &DIID_HTMLDocumentEvents,
115 &IID_IHTMLAnchorElement,
116 &IID_IHTMLBodyElement,
117 &IID_IHTMLBodyElement2,
118 &IID_IHTMLCommentElement,
119 &IID_IHTMLCurrentStyle,
120 &IID_IHTMLCurrentStyle2,
121 &IID_IHTMLCurrentStyle3,
122 &IID_IHTMLCurrentStyle4,
123 &IID_IHTMLDocument2,
124 &IID_IHTMLDocument3,
125 &IID_IHTMLDocument4,
126 &IID_IHTMLDocument5,
127 &IID_IHTMLDOMChildrenCollection,
128 &IID_IHTMLDOMNode,
129 &IID_IHTMLDOMNode2,
130 &IID_IHTMLDOMTextNode,
131 &IID_IHTMLElement,
132 &IID_IHTMLElement2,
133 &IID_IHTMLElement3,
134 &IID_IHTMLElement4,
135 &IID_IHTMLElementCollection,
136 &IID_IHTMLEventObj,
137 &IID_IHTMLFiltersCollection,
138 &IID_IHTMLFormElement,
139 &IID_IHTMLFrameBase,
140 &IID_IHTMLFrameBase2,
141 &IID_IHTMLGenericElement,
142 &IID_IHTMLFrameElement3,
143 &IID_IHTMLIFrameElement,
144 &IID_IHTMLImageElementFactory,
145 &IID_IHTMLImgElement,
146 &IID_IHTMLInputElement,
147 &IID_IHTMLLocation,
148 &IID_IHTMLOptionElement,
149 &IID_IHTMLRect,
150 &IID_IHTMLScreen,
151 &IID_IHTMLScriptElement,
152 &IID_IHTMLSelectElement,
153 &IID_IHTMLStyle,
154 &IID_IHTMLStyle2,
155 &IID_IHTMLStyle3,
156 &IID_IHTMLStyle4,
157 &IID_IHTMLTable,
158 &IID_IHTMLTableRow,
159 &IID_IHTMLTextAreaElement,
160 &IID_IHTMLTextContainer,
161 &IID_IHTMLUniqueName,
162 &IID_IHTMLWindow2,
163 &IID_IHTMLWindow3,
164 &IID_IHTMLWindow4,
165 &IID_IOmNavigator
168 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
170 HRESULT hres;
172 if(!typelib) {
173 ITypeLib *tl;
175 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
176 if(FAILED(hres)) {
177 ERR("LoadRegTypeLib failed: %08x\n", hres);
178 return hres;
181 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
182 ITypeLib_Release(tl);
185 if(!typeinfos[tid]) {
186 ITypeInfo *typeinfo;
188 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
189 if(FAILED(hres)) {
190 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
191 return hres;
194 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
195 ITypeInfo_Release(typeinfo);
198 *typeinfo = typeinfos[tid];
199 return S_OK;
202 void release_typelib(void)
204 dispex_data_t *iter;
205 unsigned i;
207 while(!list_empty(&dispex_data_list)) {
208 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
209 list_remove(&iter->entry);
211 for(i=0; i < iter->func_cnt; i++)
212 SysFreeString(iter->funcs[i].name);
214 heap_free(iter->funcs);
215 heap_free(iter->name_table);
216 heap_free(iter);
219 if(!typelib)
220 return;
222 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
223 if(typeinfos[i])
224 ITypeInfo_Release(typeinfos[i]);
226 ITypeLib_Release(typelib);
229 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
231 HRESULT hres;
233 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
234 return;
236 if(data->func_cnt == *size)
237 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
239 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
240 if(FAILED(hres))
241 return;
243 data->funcs[data->func_cnt].id = desc->memid;
244 data->funcs[data->func_cnt].tid = tid;
245 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
247 data->func_cnt++;
250 static int dispid_cmp(const void *p1, const void *p2)
252 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
255 static int func_name_cmp(const void *p1, const void *p2)
257 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
260 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
262 const tid_t *tid = This->data->iface_tids;
263 FUNCDESC *funcdesc;
264 dispex_data_t *data;
265 DWORD size = 16, i;
266 ITypeInfo *ti, *dti;
267 HRESULT hres;
269 TRACE("(%p)\n", This);
271 if(This->data->disp_tid) {
272 hres = get_typeinfo(This->data->disp_tid, &dti);
273 if(FAILED(hres)) {
274 ERR("Could not get disp type info: %08x\n", hres);
275 return NULL;
279 data = heap_alloc(sizeof(dispex_data_t));
280 data->func_cnt = 0;
281 data->func_disp_cnt = 0;
282 data->funcs = heap_alloc(size*sizeof(func_info_t));
283 list_add_tail(&dispex_data_list, &data->entry);
285 while(*tid) {
286 hres = get_typeinfo(*tid, &ti);
287 if(FAILED(hres))
288 break;
290 i=7;
291 while(1) {
292 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
293 if(FAILED(hres))
294 break;
296 add_func_info(data, &size, *tid, funcdesc, dti);
297 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
300 tid++;
303 if(!data->func_cnt) {
304 heap_free(data->funcs);
305 data->funcs = NULL;
306 }else if(data->func_cnt != size) {
307 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
310 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
312 if(data->funcs) {
313 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
314 for(i=0; i < data->func_cnt; i++)
315 data->name_table[i] = data->funcs+i;
316 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
317 }else {
318 data->name_table = NULL;
321 return data;
324 static int id_cmp(const void *p1, const void *p2)
326 return *(const DISPID*)p1 - *(const DISPID*)p2;
329 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
331 unsigned i, func_cnt;
332 FUNCDESC *funcdesc;
333 ITypeInfo *ti;
334 TYPEATTR *attr;
335 DISPID *ids;
336 HRESULT hres;
338 hres = get_typeinfo(tid, &ti);
339 if(FAILED(hres))
340 return hres;
342 hres = ITypeInfo_GetTypeAttr(ti, &attr);
343 if(FAILED(hres)) {
344 ITypeInfo_Release(ti);
345 return hres;
348 func_cnt = attr->cFuncs;
349 ITypeInfo_ReleaseTypeAttr(ti, attr);
351 ids = heap_alloc(func_cnt*sizeof(DISPID));
352 if(!ids) {
353 ITypeInfo_Release(ti);
354 return E_OUTOFMEMORY;
357 for(i=0; i < func_cnt; i++) {
358 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
359 if(FAILED(hres))
360 break;
362 ids[i] = funcdesc->memid;
363 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
366 ITypeInfo_Release(ti);
367 if(FAILED(hres)) {
368 heap_free(ids);
369 return hres;
372 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
374 *ret_size = func_cnt;
375 *ret = ids;
376 return S_OK;
379 static CRITICAL_SECTION cs_dispex_static_data;
380 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
382 0, 0, &cs_dispex_static_data,
383 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
384 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
386 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
389 static dispex_data_t *get_dispex_data(DispatchEx *This)
391 if(This->data->data)
392 return This->data->data;
394 EnterCriticalSection(&cs_dispex_static_data);
396 if(!This->data->data)
397 This->data->data = preprocess_dispex_data(This);
399 LeaveCriticalSection(&cs_dispex_static_data);
401 return This->data->data;
404 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
406 EXCEPINFO ei;
407 IDispatchEx *dispex;
408 VARIANT res;
409 HRESULT hres;
411 VariantInit(&res);
412 memset(&ei, 0, sizeof(ei));
414 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
415 if(SUCCEEDED(hres)) {
416 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
417 IDispatchEx_Release(dispex);
418 }else {
419 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
420 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
421 dp, &res, &ei, NULL);
424 VariantClear(&res);
425 return hres;
428 static inline BOOL is_custom_dispid(DISPID id)
430 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
433 static inline BOOL is_dynamic_dispid(DISPID id)
435 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
438 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
440 return !alloc || This->dynamic_data
441 ? This->dynamic_data
442 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
445 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
447 const BOOL alloc = flags & fdexNameEnsure;
448 dispex_dynamic_data_t *data;
449 dynamic_prop_t *prop;
451 data = get_dynamic_data(This, alloc);
452 if(!data) {
453 if(alloc)
454 return E_OUTOFMEMORY;
456 TRACE("not found %s\n", debugstr_w(name));
457 return DISP_E_UNKNOWNNAME;
460 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
461 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
462 if(prop->flags & DYNPROP_DELETED) {
463 if(!alloc)
464 return DISP_E_UNKNOWNNAME;
465 prop->flags &= ~DYNPROP_DELETED;
467 *ret = prop;
468 return S_OK;
472 if(!alloc)
473 return DISP_E_UNKNOWNNAME;
475 TRACE("creating dynamic prop %s\n", debugstr_w(name));
477 if(!data->buf_size) {
478 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
479 if(!data->props)
480 return E_OUTOFMEMORY;
481 data->buf_size = 4;
482 }else if(data->buf_size == data->prop_cnt) {
483 dynamic_prop_t *new_props;
485 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
486 if(!new_props)
487 return E_OUTOFMEMORY;
489 data->props = new_props;
490 data->buf_size <<= 1;
493 prop = data->props + data->prop_cnt;
495 prop->name = heap_strdupW(name);
496 if(!prop->name)
497 return E_OUTOFMEMORY;
499 VariantInit(&prop->var);
500 prop->flags = 0;
501 data->prop_cnt++;
502 *ret = prop;
503 return S_OK;
506 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
508 dynamic_prop_t *prop;
509 HRESULT hres;
511 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
512 if(FAILED(hres))
513 return hres;
515 *ret = &prop->var;
516 return S_OK;
519 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
520 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
522 if(This->data->vtbl && This->data->vtbl->value)
523 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
525 switch(flags) {
526 case DISPATCH_PROPERTYGET:
527 V_VT(res) = VT_BSTR;
528 V_BSTR(res) = SysAllocString(objectW);
529 if(!V_BSTR(res))
530 return E_OUTOFMEMORY;
531 break;
532 default:
533 FIXME("Unimplemented flags %x\n", flags);
534 return E_NOTIMPL;
537 return S_OK;
540 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
541 EXCEPINFO *ei)
543 ITypeInfo *ti;
544 IUnknown *unk;
545 UINT argerr=0;
546 HRESULT hres;
548 hres = get_typeinfo(func->tid, &ti);
549 if(FAILED(hres)) {
550 ERR("Could not get type info: %08x\n", hres);
551 return hres;
554 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
555 if(FAILED(hres)) {
556 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
557 return E_FAIL;
560 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
562 IUnknown_Release(unk);
563 return hres;
566 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
568 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
570 func_disp_t *This = FUNCTION_THIS(iface);
572 if(IsEqualGUID(&IID_IUnknown, riid)) {
573 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
574 *ppv = FUNCUNKNOWN(This);
575 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
576 return *ppv ? S_OK : E_NOINTERFACE;
577 }else {
578 *ppv = NULL;
579 return E_NOINTERFACE;
582 IUnknown_AddRef((IUnknown*)*ppv);
583 return S_OK;
586 static ULONG WINAPI Function_AddRef(IUnknown *iface)
588 func_disp_t *This = FUNCTION_THIS(iface);
590 TRACE("(%p)\n", This);
592 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
595 static ULONG WINAPI Function_Release(IUnknown *iface)
597 func_disp_t *This = FUNCTION_THIS(iface);
599 TRACE("(%p)\n", This);
601 return IDispatchEx_Release(DISPATCHEX(This->obj));
604 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
605 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
607 func_disp_t *This = FUNCTION_THIS(iface);
608 HRESULT hres;
610 switch(flags) {
611 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
612 if(!res)
613 return E_INVALIDARG;
614 case DISPATCH_METHOD:
615 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
616 break;
617 default:
618 FIXME("Unimplemented flags %x\n", flags);
619 hres = E_NOTIMPL;
622 return hres;
625 #undef FUNCTION_THIS
627 static const IUnknownVtbl FunctionUnkVtbl = {
628 Function_QueryInterface,
629 Function_AddRef,
630 Function_Release
633 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
634 function_value,
635 NULL,
636 NULL
639 static const tid_t function_iface_tids[] = {0};
641 static dispex_static_data_t function_dispex = {
642 &function_dispex_vtbl,
643 NULL_tid,
644 NULL,
645 function_iface_tids
648 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
650 func_disp_t *ret;
652 ret = heap_alloc_zero(sizeof(func_disp_t));
653 if(!ret)
654 return NULL;
656 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
657 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
658 ret->obj = obj;
659 ret->info = info;
661 return ret;
664 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
665 EXCEPINFO *ei)
667 HRESULT hres;
669 switch(flags) {
670 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
671 if(!res)
672 return E_INVALIDARG;
673 case DISPATCH_METHOD:
674 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
675 break;
676 case DISPATCH_PROPERTYGET: {
677 dispex_dynamic_data_t *dynamic_data;
679 if(func->id == DISPID_VALUE) {
680 BSTR ret;
682 ret = SysAllocString(objectW);
683 if(!ret)
684 return E_OUTOFMEMORY;
686 V_VT(res) = VT_BSTR;
687 V_BSTR(res) = ret;
688 return S_OK;
691 dynamic_data = get_dynamic_data(This, TRUE);
692 if(!dynamic_data)
693 return E_OUTOFMEMORY;
695 if(!dynamic_data->func_disps) {
696 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
697 if(!dynamic_data->func_disps)
698 return E_OUTOFMEMORY;
701 if(!dynamic_data->func_disps[func->func_disp_idx]) {
702 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
703 if(!dynamic_data->func_disps[func->func_disp_idx])
704 return E_OUTOFMEMORY;
707 V_VT(res) = VT_DISPATCH;
708 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
709 IDispatch_AddRef(V_DISPATCH(res));
710 hres = S_OK;
711 break;
713 default:
714 FIXME("Unimplemented flags %x\n", flags);
715 case DISPATCH_PROPERTYPUT:
716 hres = E_NOTIMPL;
719 return hres;
722 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
724 int min, max, n;
726 min = 0;
727 max = data->func_cnt-1;
729 while(min <= max) {
730 n = (min+max)/2;
732 if(data->funcs[n].id == id) {
733 *ret = data->funcs+n;
734 return S_OK;
737 if(data->funcs[n].id < id)
738 min = n+1;
739 else
740 max = n-1;
743 WARN("invalid id %x\n", id);
744 return DISP_E_UNKNOWNNAME;
747 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
749 dispex_data_t *data;
750 int min, max, n, c;
752 data = get_dispex_data(This);
753 if(!data)
754 return E_FAIL;
756 min = 0;
757 max = data->func_cnt-1;
759 while(min <= max) {
760 n = (min+max)/2;
762 c = strcmpiW(data->name_table[n]->name, name);
763 if(!c) {
764 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
765 break;
767 *ret = data->name_table[n]->id;
768 return S_OK;
771 if(c > 0)
772 max = n-1;
773 else
774 min = n+1;
777 if(This->data->vtbl && This->data->vtbl->get_dispid) {
778 HRESULT hres;
780 hres = This->data->vtbl->get_dispid(This->outer, name, grfdex, ret);
781 if(hres != DISP_E_UNKNOWNNAME)
782 return hres;
785 return DISP_E_UNKNOWNNAME;
788 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
789 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
791 dispex_data_t *data;
792 func_info_t *func;
793 HRESULT hres;
795 data = get_dispex_data(This);
796 if(!data)
797 return E_FAIL;
799 hres = get_builtin_func(data, id, &func);
800 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
801 return dispex_value(This, lcid, flags, dp, res, ei, caller);
802 if(FAILED(hres))
803 return hres;
805 if(func->func_disp_idx == -1)
806 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
807 else
808 hres = function_invoke(This, func, flags, dp, res, ei);
810 return hres;
813 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
815 dynamic_prop_t *prop;
816 DISPID id;
817 HRESULT hres;
819 hres = get_builtin_id(This, name, 0, &id);
820 if(hres == S_OK) {
821 DISPID named_id = DISPID_PROPERTYPUT;
822 VARIANT var;
823 DISPPARAMS dp = {&var,&named_id,1,1};
824 EXCEPINFO ei;
826 V_VT(&var) = VT_EMPTY;
827 memset(&ei, 0, sizeof(ei));
828 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
829 if(FAILED(hres))
830 return hres;
832 *success = VARIANT_TRUE;
833 return S_OK;
836 hres = get_dynamic_prop(This, name, 0, &prop);
837 if(FAILED(hres)) {
838 if(hres != DISP_E_UNKNOWNNAME)
839 return hres;
840 *success = VARIANT_FALSE;
841 return S_OK;
844 VariantClear(&prop->var);
845 prop->flags |= DYNPROP_DELETED;
846 *success = VARIANT_TRUE;
847 return S_OK;
850 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
852 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
854 DispatchEx *This = DISPATCHEX_THIS(iface);
856 return IUnknown_QueryInterface(This->outer, riid, ppv);
859 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
861 DispatchEx *This = DISPATCHEX_THIS(iface);
863 return IUnknown_AddRef(This->outer);
866 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
868 DispatchEx *This = DISPATCHEX_THIS(iface);
870 return IUnknown_Release(This->outer);
873 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
875 DispatchEx *This = DISPATCHEX_THIS(iface);
877 TRACE("(%p)->(%p)\n", This, pctinfo);
879 *pctinfo = 1;
880 return S_OK;
883 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
884 LCID lcid, ITypeInfo **ppTInfo)
886 DispatchEx *This = DISPATCHEX_THIS(iface);
887 HRESULT hres;
889 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
891 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
892 if(FAILED(hres))
893 return hres;
895 ITypeInfo_AddRef(*ppTInfo);
896 return S_OK;
899 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
900 LPOLESTR *rgszNames, UINT cNames,
901 LCID lcid, DISPID *rgDispId)
903 DispatchEx *This = DISPATCHEX_THIS(iface);
904 UINT i;
905 HRESULT hres;
907 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
908 lcid, rgDispId);
910 for(i=0; i < cNames; i++) {
911 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
912 if(FAILED(hres))
913 return hres;
916 return S_OK;
919 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
920 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
921 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
923 DispatchEx *This = DISPATCHEX_THIS(iface);
925 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
926 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
928 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
929 pDispParams, pVarResult, pExcepInfo, NULL);
932 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
934 DispatchEx *This = DISPATCHEX_THIS(iface);
935 dynamic_prop_t *dprop;
936 HRESULT hres;
938 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
940 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
941 FIXME("Unsupported grfdex %x\n", grfdex);
943 hres = get_builtin_id(This, bstrName, grfdex, pid);
944 if(hres != DISP_E_UNKNOWNNAME)
945 return hres;
947 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
948 if(FAILED(hres))
949 return hres;
951 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
952 return S_OK;
955 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
956 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
958 DispatchEx *This = DISPATCHEX_THIS(iface);
959 HRESULT hres;
961 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
963 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
964 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
966 if(wFlags == DISPATCH_CONSTRUCT) {
967 if(id == DISPID_VALUE) {
968 if(This->data->vtbl && This->data->vtbl->value) {
969 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
970 pvarRes, pei, pspCaller);
972 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
973 return E_FAIL;
975 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
976 return E_FAIL;
979 if(is_dynamic_dispid(id)) {
980 DWORD idx = id - DISPID_DYNPROP_0;
981 dynamic_prop_t *prop;
983 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
984 return DISP_E_UNKNOWNNAME;
986 prop = This->dynamic_data->props+idx;
988 switch(wFlags) {
989 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
990 if(!pvarRes)
991 return E_INVALIDARG;
992 case DISPATCH_METHOD: {
993 DISPID named_arg = DISPID_THIS;
994 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
995 IDispatchEx *dispex;
997 if(V_VT(&prop->var) != VT_DISPATCH) {
998 FIXME("invoke %s\n", debugstr_variant(&prop->var));
999 return E_NOTIMPL;
1002 if(pdp->cNamedArgs) {
1003 FIXME("named args not supported\n");
1004 return E_NOTIMPL;
1007 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
1008 if(!dp.rgvarg)
1009 return E_OUTOFMEMORY;
1011 dp.cArgs = pdp->cArgs+1;
1012 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
1014 V_VT(dp.rgvarg) = VT_DISPATCH;
1015 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
1017 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
1018 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
1019 if(SUCCEEDED(hres)) {
1020 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
1021 IDispatchEx_Release(dispex);
1022 }else {
1023 ULONG err = 0;
1024 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
1026 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
1028 heap_free(dp.rgvarg);
1029 return hres;
1031 case DISPATCH_PROPERTYGET:
1032 if(prop->flags & DYNPROP_DELETED)
1033 return DISP_E_UNKNOWNNAME;
1034 return VariantCopy(pvarRes, &prop->var);
1035 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1036 case DISPATCH_PROPERTYPUT:
1037 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1038 || pdp->cNamedArgs > 1) {
1039 FIXME("invalid args\n");
1040 return E_INVALIDARG;
1043 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1044 VariantClear(&prop->var);
1045 hres = VariantCopy(&prop->var, pdp->rgvarg);
1046 if(FAILED(hres))
1047 return hres;
1049 prop->flags &= ~DYNPROP_DELETED;
1050 return S_OK;
1051 default:
1052 FIXME("unhandled wFlags %x\n", wFlags);
1053 return E_NOTIMPL;
1057 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1060 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1062 DispatchEx *This = DISPATCHEX_THIS(iface);
1064 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1066 /* Not implemented by IE */
1067 return E_NOTIMPL;
1070 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1072 DispatchEx *This = DISPATCHEX_THIS(iface);
1073 FIXME("(%p)->(%x)\n", This, id);
1074 return E_NOTIMPL;
1077 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1079 DispatchEx *This = DISPATCHEX_THIS(iface);
1080 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1081 return E_NOTIMPL;
1084 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1086 DispatchEx *This = DISPATCHEX_THIS(iface);
1087 dispex_data_t *data;
1088 func_info_t *func;
1089 HRESULT hres;
1091 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1093 if(is_dynamic_dispid(id)) {
1094 DWORD idx = id - DISPID_DYNPROP_0;
1096 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1097 return DISP_E_UNKNOWNNAME;
1099 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1100 if(!*pbstrName)
1101 return E_OUTOFMEMORY;
1103 return S_OK;
1106 data = get_dispex_data(This);
1107 if(!data)
1108 return E_FAIL;
1110 hres = get_builtin_func(data, id, &func);
1111 if(FAILED(hres))
1112 return hres;
1114 *pbstrName = SysAllocString(func->name);
1115 if(!*pbstrName)
1116 return E_OUTOFMEMORY;
1117 return S_OK;
1120 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1122 DispatchEx *This = DISPATCHEX_THIS(iface);
1123 dispex_data_t *data;
1124 func_info_t *func;
1125 HRESULT hres;
1127 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1129 if(is_dynamic_dispid(id)) {
1130 DWORD idx = id - DISPID_DYNPROP_0;
1132 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1133 return DISP_E_UNKNOWNNAME;
1135 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1137 if(idx == This->dynamic_data->prop_cnt) {
1138 *pid = DISPID_STARTENUM;
1139 return S_FALSE;
1142 *pid = DISPID_DYNPROP_0+idx;
1143 return S_OK;
1146 data = get_dispex_data(This);
1147 if(!data)
1148 return E_FAIL;
1150 if(id == DISPID_STARTENUM) {
1151 func = data->funcs;
1152 }else {
1153 hres = get_builtin_func(data, id, &func);
1154 if(FAILED(hres))
1155 return hres;
1156 func++;
1159 while(func < data->funcs+data->func_cnt) {
1160 /* FIXME: Skip hidden properties */
1161 if(func->func_disp_idx == -1) {
1162 *pid = func->id;
1163 return S_OK;
1165 func++;
1168 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1169 *pid = DISPID_DYNPROP_0;
1170 return S_OK;
1173 *pid = DISPID_STARTENUM;
1174 return S_FALSE;
1177 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1179 DispatchEx *This = DISPATCHEX_THIS(iface);
1180 FIXME("(%p)->(%p)\n", This, ppunk);
1181 return E_NOTIMPL;
1184 #undef DISPATCHEX_THIS
1186 static IDispatchExVtbl DispatchExVtbl = {
1187 DispatchEx_QueryInterface,
1188 DispatchEx_AddRef,
1189 DispatchEx_Release,
1190 DispatchEx_GetTypeInfoCount,
1191 DispatchEx_GetTypeInfo,
1192 DispatchEx_GetIDsOfNames,
1193 DispatchEx_Invoke,
1194 DispatchEx_GetDispID,
1195 DispatchEx_InvokeEx,
1196 DispatchEx_DeleteMemberByName,
1197 DispatchEx_DeleteMemberByDispID,
1198 DispatchEx_GetMemberProperties,
1199 DispatchEx_GetMemberName,
1200 DispatchEx_GetNextDispID,
1201 DispatchEx_GetNameSpaceParent
1204 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1206 static const IID IID_UndocumentedScriptIface =
1207 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1208 static const IID IID_IDispatchJS =
1209 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1211 if(IsEqualGUID(&IID_IDispatch, riid)) {
1212 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1213 *ppv = DISPATCHEX(This);
1214 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1215 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1216 *ppv = DISPATCHEX(This);
1217 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1218 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1219 *ppv = NULL;
1220 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1221 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1222 *ppv = NULL;
1223 }else {
1224 return FALSE;
1227 if(*ppv)
1228 IUnknown_AddRef((IUnknown*)*ppv);
1229 return TRUE;
1232 void release_dispex(DispatchEx *This)
1234 dynamic_prop_t *prop;
1236 if(!This->dynamic_data)
1237 return;
1239 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1240 VariantClear(&prop->var);
1241 heap_free(prop->name);
1244 heap_free(This->dynamic_data->props);
1246 if(This->dynamic_data->func_disps) {
1247 unsigned i;
1249 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1250 if(This->dynamic_data->func_disps[i]) {
1251 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1252 heap_free(This->dynamic_data->func_disps[i]);
1256 heap_free(This->dynamic_data->func_disps);
1259 heap_free(This->dynamic_data);
1262 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1264 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1265 dispex->outer = outer;
1266 dispex->data = data;
1267 dispex->dynamic_data = NULL;