msvcr100: The msvcr100 debug channel is unused so remove it.
[wine/testsucceed.git] / dlls / mshtml / dispex.c
blobde0c94871f63e1aad3324ca9027e2424e34d7071
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_IHTMLScreen,
150 &IID_IHTMLScriptElement,
151 &IID_IHTMLSelectElement,
152 &IID_IHTMLStyle,
153 &IID_IHTMLStyle2,
154 &IID_IHTMLStyle3,
155 &IID_IHTMLStyle4,
156 &IID_IHTMLTable,
157 &IID_IHTMLTableRow,
158 &IID_IHTMLTextAreaElement,
159 &IID_IHTMLTextContainer,
160 &IID_IHTMLUniqueName,
161 &IID_IHTMLWindow2,
162 &IID_IHTMLWindow3,
163 &IID_IHTMLWindow4,
164 &IID_IOmNavigator
167 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
169 HRESULT hres;
171 if(!typelib) {
172 ITypeLib *tl;
174 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
175 if(FAILED(hres)) {
176 ERR("LoadRegTypeLib failed: %08x\n", hres);
177 return hres;
180 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
181 ITypeLib_Release(tl);
184 if(!typeinfos[tid]) {
185 ITypeInfo *typeinfo;
187 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
188 if(FAILED(hres)) {
189 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
190 return hres;
193 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
194 ITypeInfo_Release(typeinfo);
197 *typeinfo = typeinfos[tid];
198 return S_OK;
201 void release_typelib(void)
203 dispex_data_t *iter;
204 unsigned i;
206 while(!list_empty(&dispex_data_list)) {
207 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
208 list_remove(&iter->entry);
210 for(i=0; i < iter->func_cnt; i++)
211 SysFreeString(iter->funcs[i].name);
213 heap_free(iter->funcs);
214 heap_free(iter->name_table);
215 heap_free(iter);
218 if(!typelib)
219 return;
221 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
222 if(typeinfos[i])
223 ITypeInfo_Release(typeinfos[i]);
225 ITypeLib_Release(typelib);
228 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
230 HRESULT hres;
232 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
233 return;
235 if(data->func_cnt == *size)
236 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
238 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
239 if(FAILED(hres))
240 return;
242 data->funcs[data->func_cnt].id = desc->memid;
243 data->funcs[data->func_cnt].tid = tid;
244 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
246 data->func_cnt++;
249 static int dispid_cmp(const void *p1, const void *p2)
251 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
254 static int func_name_cmp(const void *p1, const void *p2)
256 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
259 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
261 const tid_t *tid = This->data->iface_tids;
262 FUNCDESC *funcdesc;
263 dispex_data_t *data;
264 DWORD size = 16, i;
265 ITypeInfo *ti, *dti;
266 HRESULT hres;
268 TRACE("(%p)\n", This);
270 if(This->data->disp_tid) {
271 hres = get_typeinfo(This->data->disp_tid, &dti);
272 if(FAILED(hres)) {
273 ERR("Could not get disp type info: %08x\n", hres);
274 return NULL;
278 data = heap_alloc(sizeof(dispex_data_t));
279 data->func_cnt = 0;
280 data->func_disp_cnt = 0;
281 data->funcs = heap_alloc(size*sizeof(func_info_t));
282 list_add_tail(&dispex_data_list, &data->entry);
284 while(*tid) {
285 hres = get_typeinfo(*tid, &ti);
286 if(FAILED(hres))
287 break;
289 i=7;
290 while(1) {
291 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
292 if(FAILED(hres))
293 break;
295 add_func_info(data, &size, *tid, funcdesc, dti);
296 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
299 tid++;
302 if(!data->func_cnt) {
303 heap_free(data->funcs);
304 data->funcs = NULL;
305 }else if(data->func_cnt != size) {
306 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
309 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
311 if(data->funcs) {
312 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
313 for(i=0; i < data->func_cnt; i++)
314 data->name_table[i] = data->funcs+i;
315 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
316 }else {
317 data->name_table = NULL;
320 return data;
323 static int id_cmp(const void *p1, const void *p2)
325 return *(const DISPID*)p1 - *(const DISPID*)p2;
328 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
330 unsigned i, func_cnt;
331 FUNCDESC *funcdesc;
332 ITypeInfo *ti;
333 TYPEATTR *attr;
334 DISPID *ids;
335 HRESULT hres;
337 hres = get_typeinfo(tid, &ti);
338 if(FAILED(hres))
339 return hres;
341 hres = ITypeInfo_GetTypeAttr(ti, &attr);
342 if(FAILED(hres)) {
343 ITypeInfo_Release(ti);
344 return hres;
347 func_cnt = attr->cFuncs;
348 ITypeInfo_ReleaseTypeAttr(ti, attr);
350 ids = heap_alloc(func_cnt*sizeof(DISPID));
351 if(!ids) {
352 ITypeInfo_Release(ti);
353 return E_OUTOFMEMORY;
356 for(i=0; i < func_cnt; i++) {
357 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
358 if(FAILED(hres))
359 break;
361 ids[i] = funcdesc->memid;
362 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
365 ITypeInfo_Release(ti);
366 if(FAILED(hres)) {
367 heap_free(ids);
368 return hres;
371 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
373 *ret_size = func_cnt;
374 *ret = ids;
375 return S_OK;
378 static CRITICAL_SECTION cs_dispex_static_data;
379 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
381 0, 0, &cs_dispex_static_data,
382 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
383 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
385 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
388 static dispex_data_t *get_dispex_data(DispatchEx *This)
390 if(This->data->data)
391 return This->data->data;
393 EnterCriticalSection(&cs_dispex_static_data);
395 if(!This->data->data)
396 This->data->data = preprocess_dispex_data(This);
398 LeaveCriticalSection(&cs_dispex_static_data);
400 return This->data->data;
403 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
405 EXCEPINFO ei;
406 IDispatchEx *dispex;
407 VARIANT res;
408 HRESULT hres;
410 VariantInit(&res);
411 memset(&ei, 0, sizeof(ei));
413 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
414 if(SUCCEEDED(hres)) {
415 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
416 IDispatchEx_Release(dispex);
417 }else {
418 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
419 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
420 dp, &res, &ei, NULL);
423 VariantClear(&res);
424 return hres;
427 static inline BOOL is_custom_dispid(DISPID id)
429 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
432 static inline BOOL is_dynamic_dispid(DISPID id)
434 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
437 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
439 return !alloc || This->dynamic_data
440 ? This->dynamic_data
441 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
444 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
446 const BOOL alloc = flags & fdexNameEnsure;
447 dispex_dynamic_data_t *data;
448 dynamic_prop_t *prop;
450 data = get_dynamic_data(This, alloc);
451 if(!data) {
452 if(alloc)
453 return E_OUTOFMEMORY;
455 TRACE("not found %s\n", debugstr_w(name));
456 return DISP_E_UNKNOWNNAME;
459 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
460 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
461 if(prop->flags & DYNPROP_DELETED) {
462 if(!alloc)
463 return DISP_E_UNKNOWNNAME;
464 prop->flags &= ~DYNPROP_DELETED;
466 *ret = prop;
467 return S_OK;
471 if(!alloc)
472 return DISP_E_UNKNOWNNAME;
474 TRACE("creating dynamic prop %s\n", debugstr_w(name));
476 if(!data->buf_size) {
477 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
478 if(!data->props)
479 return E_OUTOFMEMORY;
480 data->buf_size = 4;
481 }else if(data->buf_size == data->prop_cnt) {
482 dynamic_prop_t *new_props;
484 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
485 if(!new_props)
486 return E_OUTOFMEMORY;
488 data->props = new_props;
489 data->buf_size <<= 1;
492 prop = data->props + data->prop_cnt;
494 prop->name = heap_strdupW(name);
495 if(!prop->name)
496 return E_OUTOFMEMORY;
498 VariantInit(&prop->var);
499 prop->flags = 0;
500 data->prop_cnt++;
501 *ret = prop;
502 return S_OK;
505 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
507 dynamic_prop_t *prop;
508 HRESULT hres;
510 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
511 if(FAILED(hres))
512 return hres;
514 *ret = &prop->var;
515 return S_OK;
518 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
519 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
521 if(This->data->vtbl && This->data->vtbl->value)
522 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
524 switch(flags) {
525 case DISPATCH_PROPERTYGET:
526 V_VT(res) = VT_BSTR;
527 V_BSTR(res) = SysAllocString(objectW);
528 if(!V_BSTR(res))
529 return E_OUTOFMEMORY;
530 break;
531 default:
532 FIXME("Unimplemented flags %x\n", flags);
533 return E_NOTIMPL;
536 return S_OK;
539 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
540 EXCEPINFO *ei)
542 ITypeInfo *ti;
543 IUnknown *unk;
544 UINT argerr=0;
545 HRESULT hres;
547 hres = get_typeinfo(func->tid, &ti);
548 if(FAILED(hres)) {
549 ERR("Could not get type info: %08x\n", hres);
550 return hres;
553 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
554 if(FAILED(hres)) {
555 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
556 return E_FAIL;
559 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
561 IUnknown_Release(unk);
562 return hres;
565 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
567 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
569 func_disp_t *This = FUNCTION_THIS(iface);
571 if(IsEqualGUID(&IID_IUnknown, riid)) {
572 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
573 *ppv = FUNCUNKNOWN(This);
574 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
575 return *ppv ? S_OK : E_NOINTERFACE;
576 }else {
577 *ppv = NULL;
578 return E_NOINTERFACE;
581 IUnknown_AddRef((IUnknown*)*ppv);
582 return S_OK;
585 static ULONG WINAPI Function_AddRef(IUnknown *iface)
587 func_disp_t *This = FUNCTION_THIS(iface);
589 TRACE("(%p)\n", This);
591 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
594 static ULONG WINAPI Function_Release(IUnknown *iface)
596 func_disp_t *This = FUNCTION_THIS(iface);
598 TRACE("(%p)\n", This);
600 return IDispatchEx_Release(DISPATCHEX(This->obj));
603 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
604 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
606 func_disp_t *This = FUNCTION_THIS(iface);
607 HRESULT hres;
609 switch(flags) {
610 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
611 if(!res)
612 return E_INVALIDARG;
613 case DISPATCH_METHOD:
614 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
615 break;
616 default:
617 FIXME("Unimplemented flags %x\n", flags);
618 hres = E_NOTIMPL;
621 return hres;
624 #undef FUNCTION_THIS
626 static const IUnknownVtbl FunctionUnkVtbl = {
627 Function_QueryInterface,
628 Function_AddRef,
629 Function_Release
632 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
633 function_value,
634 NULL,
635 NULL
638 static const tid_t function_iface_tids[] = {0};
640 static dispex_static_data_t function_dispex = {
641 &function_dispex_vtbl,
642 NULL_tid,
643 NULL,
644 function_iface_tids
647 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
649 func_disp_t *ret;
651 ret = heap_alloc_zero(sizeof(func_disp_t));
652 if(!ret)
653 return NULL;
655 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
656 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
657 ret->obj = obj;
658 ret->info = info;
660 return ret;
663 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
664 EXCEPINFO *ei)
666 HRESULT hres;
668 switch(flags) {
669 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
670 if(!res)
671 return E_INVALIDARG;
672 case DISPATCH_METHOD:
673 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
674 break;
675 case DISPATCH_PROPERTYGET: {
676 dispex_dynamic_data_t *dynamic_data;
678 if(func->id == DISPID_VALUE) {
679 BSTR ret;
681 ret = SysAllocString(objectW);
682 if(!ret)
683 return E_OUTOFMEMORY;
685 V_VT(res) = VT_BSTR;
686 V_BSTR(res) = ret;
687 return S_OK;
690 dynamic_data = get_dynamic_data(This, TRUE);
691 if(!dynamic_data)
692 return E_OUTOFMEMORY;
694 if(!dynamic_data->func_disps) {
695 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
696 if(!dynamic_data->func_disps)
697 return E_OUTOFMEMORY;
700 if(!dynamic_data->func_disps[func->func_disp_idx]) {
701 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
702 if(!dynamic_data->func_disps[func->func_disp_idx])
703 return E_OUTOFMEMORY;
706 V_VT(res) = VT_DISPATCH;
707 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
708 IDispatch_AddRef(V_DISPATCH(res));
709 hres = S_OK;
710 break;
712 default:
713 FIXME("Unimplemented flags %x\n", flags);
714 case DISPATCH_PROPERTYPUT:
715 hres = E_NOTIMPL;
718 return hres;
721 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
723 int min, max, n;
725 min = 0;
726 max = data->func_cnt-1;
728 while(min <= max) {
729 n = (min+max)/2;
731 if(data->funcs[n].id == id) {
732 *ret = data->funcs+n;
733 return S_OK;
736 if(data->funcs[n].id < id)
737 min = n+1;
738 else
739 max = n-1;
742 WARN("invalid id %x\n", id);
743 return DISP_E_UNKNOWNNAME;
746 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
748 dispex_data_t *data;
749 int min, max, n, c;
751 data = get_dispex_data(This);
752 if(!data)
753 return E_FAIL;
755 min = 0;
756 max = data->func_cnt-1;
758 while(min <= max) {
759 n = (min+max)/2;
761 c = strcmpiW(data->name_table[n]->name, name);
762 if(!c) {
763 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
764 break;
766 *ret = data->name_table[n]->id;
767 return S_OK;
770 if(c > 0)
771 max = n-1;
772 else
773 min = n+1;
776 if(This->data->vtbl && This->data->vtbl->get_dispid) {
777 HRESULT hres;
779 hres = This->data->vtbl->get_dispid(This->outer, name, grfdex, ret);
780 if(hres != DISP_E_UNKNOWNNAME)
781 return hres;
784 return DISP_E_UNKNOWNNAME;
787 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
788 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
790 dispex_data_t *data;
791 func_info_t *func;
792 HRESULT hres;
794 data = get_dispex_data(This);
795 if(!data)
796 return E_FAIL;
798 hres = get_builtin_func(data, id, &func);
799 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
800 return dispex_value(This, lcid, flags, dp, res, ei, caller);
801 if(FAILED(hres))
802 return hres;
804 if(func->func_disp_idx == -1)
805 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
806 else
807 hres = function_invoke(This, func, flags, dp, res, ei);
809 return hres;
812 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
814 dynamic_prop_t *prop;
815 DISPID id;
816 HRESULT hres;
818 hres = get_builtin_id(This, name, 0, &id);
819 if(hres == S_OK) {
820 DISPID named_id = DISPID_PROPERTYPUT;
821 VARIANT var;
822 DISPPARAMS dp = {&var,&named_id,1,1};
823 EXCEPINFO ei;
825 V_VT(&var) = VT_EMPTY;
826 memset(&ei, 0, sizeof(ei));
827 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
828 if(FAILED(hres))
829 return hres;
831 *success = VARIANT_TRUE;
832 return S_OK;
835 hres = get_dynamic_prop(This, name, 0, &prop);
836 if(FAILED(hres)) {
837 if(hres != DISP_E_UNKNOWNNAME)
838 return hres;
839 *success = VARIANT_FALSE;
840 return S_OK;
843 VariantClear(&prop->var);
844 prop->flags |= DYNPROP_DELETED;
845 *success = VARIANT_TRUE;
846 return S_OK;
849 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
851 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
853 DispatchEx *This = DISPATCHEX_THIS(iface);
855 return IUnknown_QueryInterface(This->outer, riid, ppv);
858 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
860 DispatchEx *This = DISPATCHEX_THIS(iface);
862 return IUnknown_AddRef(This->outer);
865 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
867 DispatchEx *This = DISPATCHEX_THIS(iface);
869 return IUnknown_Release(This->outer);
872 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
874 DispatchEx *This = DISPATCHEX_THIS(iface);
876 TRACE("(%p)->(%p)\n", This, pctinfo);
878 *pctinfo = 1;
879 return S_OK;
882 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
883 LCID lcid, ITypeInfo **ppTInfo)
885 DispatchEx *This = DISPATCHEX_THIS(iface);
886 HRESULT hres;
888 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
890 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
891 if(FAILED(hres))
892 return hres;
894 ITypeInfo_AddRef(*ppTInfo);
895 return S_OK;
898 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
899 LPOLESTR *rgszNames, UINT cNames,
900 LCID lcid, DISPID *rgDispId)
902 DispatchEx *This = DISPATCHEX_THIS(iface);
903 UINT i;
904 HRESULT hres;
906 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
907 lcid, rgDispId);
909 for(i=0; i < cNames; i++) {
910 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
911 if(FAILED(hres))
912 return hres;
915 return S_OK;
918 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
919 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
920 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
922 DispatchEx *This = DISPATCHEX_THIS(iface);
924 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
925 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
927 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
928 pDispParams, pVarResult, pExcepInfo, NULL);
931 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
933 DispatchEx *This = DISPATCHEX_THIS(iface);
934 dynamic_prop_t *dprop;
935 HRESULT hres;
937 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
939 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
940 FIXME("Unsupported grfdex %x\n", grfdex);
942 hres = get_builtin_id(This, bstrName, grfdex, pid);
943 if(hres != DISP_E_UNKNOWNNAME)
944 return hres;
946 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
947 if(FAILED(hres))
948 return hres;
950 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
951 return S_OK;
954 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
955 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
957 DispatchEx *This = DISPATCHEX_THIS(iface);
958 HRESULT hres;
960 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
962 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
963 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
965 if(wFlags == DISPATCH_CONSTRUCT) {
966 if(id == DISPID_VALUE) {
967 if(This->data->vtbl && This->data->vtbl->value) {
968 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
969 pvarRes, pei, pspCaller);
971 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
972 return E_FAIL;
974 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
975 return E_FAIL;
978 if(is_dynamic_dispid(id)) {
979 DWORD idx = id - DISPID_DYNPROP_0;
980 dynamic_prop_t *prop;
982 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
983 return DISP_E_UNKNOWNNAME;
985 prop = This->dynamic_data->props+idx;
987 switch(wFlags) {
988 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
989 if(!pvarRes)
990 return E_INVALIDARG;
991 case DISPATCH_METHOD: {
992 DISPID named_arg = DISPID_THIS;
993 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
994 IDispatchEx *dispex;
996 if(V_VT(&prop->var) != VT_DISPATCH) {
997 FIXME("invoke %s\n", debugstr_variant(&prop->var));
998 return E_NOTIMPL;
1001 if(pdp->cNamedArgs) {
1002 FIXME("named args not supported\n");
1003 return E_NOTIMPL;
1006 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
1007 if(!dp.rgvarg)
1008 return E_OUTOFMEMORY;
1010 dp.cArgs = pdp->cArgs+1;
1011 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
1013 V_VT(dp.rgvarg) = VT_DISPATCH;
1014 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
1016 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
1017 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
1018 if(SUCCEEDED(hres)) {
1019 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
1020 IDispatchEx_Release(dispex);
1021 }else {
1022 ULONG err = 0;
1023 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
1025 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
1027 heap_free(dp.rgvarg);
1028 return hres;
1030 case DISPATCH_PROPERTYGET:
1031 if(prop->flags & DYNPROP_DELETED)
1032 return DISP_E_UNKNOWNNAME;
1033 return VariantCopy(pvarRes, &prop->var);
1034 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1035 case DISPATCH_PROPERTYPUT:
1036 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1037 || pdp->cNamedArgs > 1) {
1038 FIXME("invalid args\n");
1039 return E_INVALIDARG;
1042 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1043 VariantClear(&prop->var);
1044 hres = VariantCopy(&prop->var, pdp->rgvarg);
1045 if(FAILED(hres))
1046 return hres;
1048 prop->flags &= ~DYNPROP_DELETED;
1049 return S_OK;
1050 default:
1051 FIXME("unhandled wFlags %x\n", wFlags);
1052 return E_NOTIMPL;
1056 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1059 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1061 DispatchEx *This = DISPATCHEX_THIS(iface);
1063 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1065 /* Not implemented by IE */
1066 return E_NOTIMPL;
1069 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1071 DispatchEx *This = DISPATCHEX_THIS(iface);
1072 FIXME("(%p)->(%x)\n", This, id);
1073 return E_NOTIMPL;
1076 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1078 DispatchEx *This = DISPATCHEX_THIS(iface);
1079 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1080 return E_NOTIMPL;
1083 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1085 DispatchEx *This = DISPATCHEX_THIS(iface);
1086 dispex_data_t *data;
1087 func_info_t *func;
1088 HRESULT hres;
1090 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1092 if(is_dynamic_dispid(id)) {
1093 DWORD idx = id - DISPID_DYNPROP_0;
1095 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1096 return DISP_E_UNKNOWNNAME;
1098 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1099 if(!*pbstrName)
1100 return E_OUTOFMEMORY;
1102 return S_OK;
1105 data = get_dispex_data(This);
1106 if(!data)
1107 return E_FAIL;
1109 hres = get_builtin_func(data, id, &func);
1110 if(FAILED(hres))
1111 return hres;
1113 *pbstrName = SysAllocString(func->name);
1114 if(!*pbstrName)
1115 return E_OUTOFMEMORY;
1116 return S_OK;
1119 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1121 DispatchEx *This = DISPATCHEX_THIS(iface);
1122 dispex_data_t *data;
1123 func_info_t *func;
1124 HRESULT hres;
1126 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1128 if(is_dynamic_dispid(id)) {
1129 DWORD idx = id - DISPID_DYNPROP_0;
1131 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1132 return DISP_E_UNKNOWNNAME;
1134 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1136 if(idx == This->dynamic_data->prop_cnt) {
1137 *pid = DISPID_STARTENUM;
1138 return S_FALSE;
1141 *pid = DISPID_DYNPROP_0+idx;
1142 return S_OK;
1145 data = get_dispex_data(This);
1146 if(!data)
1147 return E_FAIL;
1149 if(id == DISPID_STARTENUM) {
1150 func = data->funcs;
1151 }else {
1152 hres = get_builtin_func(data, id, &func);
1153 if(FAILED(hres))
1154 return hres;
1155 func++;
1158 while(func < data->funcs+data->func_cnt) {
1159 /* FIXME: Skip hidden properties */
1160 if(func->func_disp_idx == -1) {
1161 *pid = func->id;
1162 return S_OK;
1164 func++;
1167 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1168 *pid = DISPID_DYNPROP_0;
1169 return S_OK;
1172 *pid = DISPID_STARTENUM;
1173 return S_FALSE;
1176 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1178 DispatchEx *This = DISPATCHEX_THIS(iface);
1179 FIXME("(%p)->(%p)\n", This, ppunk);
1180 return E_NOTIMPL;
1183 #undef DISPATCHEX_THIS
1185 static IDispatchExVtbl DispatchExVtbl = {
1186 DispatchEx_QueryInterface,
1187 DispatchEx_AddRef,
1188 DispatchEx_Release,
1189 DispatchEx_GetTypeInfoCount,
1190 DispatchEx_GetTypeInfo,
1191 DispatchEx_GetIDsOfNames,
1192 DispatchEx_Invoke,
1193 DispatchEx_GetDispID,
1194 DispatchEx_InvokeEx,
1195 DispatchEx_DeleteMemberByName,
1196 DispatchEx_DeleteMemberByDispID,
1197 DispatchEx_GetMemberProperties,
1198 DispatchEx_GetMemberName,
1199 DispatchEx_GetNextDispID,
1200 DispatchEx_GetNameSpaceParent
1203 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1205 static const IID IID_UndocumentedScriptIface =
1206 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1207 static const IID IID_IDispatchJS =
1208 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1210 if(IsEqualGUID(&IID_IDispatch, riid)) {
1211 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1212 *ppv = DISPATCHEX(This);
1213 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1214 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1215 *ppv = DISPATCHEX(This);
1216 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1217 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1218 *ppv = NULL;
1219 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1220 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1221 *ppv = NULL;
1222 }else {
1223 return FALSE;
1226 if(*ppv)
1227 IUnknown_AddRef((IUnknown*)*ppv);
1228 return TRUE;
1231 void release_dispex(DispatchEx *This)
1233 dynamic_prop_t *prop;
1235 if(!This->dynamic_data)
1236 return;
1238 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1239 VariantClear(&prop->var);
1240 heap_free(prop->name);
1243 heap_free(This->dynamic_data->props);
1245 if(This->dynamic_data->func_disps) {
1246 unsigned i;
1248 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1249 if(This->dynamic_data->func_disps[i]) {
1250 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1251 heap_free(This->dynamic_data->func_disps[i]);
1255 heap_free(This->dynamic_data->func_disps);
1258 heap_free(This->dynamic_data);
1261 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1263 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1264 dispex->outer = outer;
1265 dispex->data = data;
1266 dispex->dynamic_data = NULL;