wined3d: Get rid of the GL_SUPPORT macro.
[wine] / dlls / mshtml / dispex.c
1 /*
2  * Copyright 2008-2009 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27
28 #include "wine/debug.h"
29
30 #include "mshtml_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
33
34 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
35
36 typedef struct {
37     DISPID id;
38     BSTR name;
39     tid_t tid;
40     int func_disp_idx;
41 } func_info_t;
42
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;
48
49     struct list entry;
50 };
51
52 typedef struct {
53     VARIANT var;
54     LPWSTR name;
55 } dynamic_prop_t;
56
57 typedef struct {
58     DispatchEx dispex;
59     const IUnknownVtbl *lpIUnknownVtbl;
60     DispatchEx *obj;
61     func_info_t *info;
62 } func_disp_t;
63
64 #define FUNCUNKNOWN(x)  ((IUnknown*) &(x)->lpIUnknownVtbl)
65
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;
71 };
72
73 #define DISPID_DYNPROP_0    0x50000000
74 #define DISPID_DYNPROP_MAX  0x5fffffff
75
76 #define FDEX_VERSION_MASK 0xf0000000
77
78 static ITypeLib *typelib;
79 static ITypeInfo *typeinfos[LAST_tid];
80 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
81
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_DispHTMLGenericElement,
94     &DIID_DispHTMLIFrame,
95     &DIID_DispHTMLImg,
96     &DIID_DispHTMLInputElement,
97     &DIID_DispHTMLLocation,
98     &DIID_DispHTMLNavigator,
99     &DIID_DispHTMLOptionElement,
100     &DIID_DispHTMLSelectElement,
101     &DIID_DispHTMLStyle,
102     &DIID_DispHTMLTable,
103     &DIID_DispHTMLTableRow,
104     &DIID_DispHTMLUnknownElement,
105     &DIID_DispHTMLWindow2,
106     &IID_IHTMLAnchorElement,
107     &IID_IHTMLBodyElement,
108     &IID_IHTMLBodyElement2,
109     &IID_IHTMLCommentElement,
110     &IID_IHTMLCurrentStyle,
111     &IID_IHTMLCurrentStyle2,
112     &IID_IHTMLCurrentStyle3,
113     &IID_IHTMLCurrentStyle4,
114     &IID_IHTMLDocument2,
115     &IID_IHTMLDocument3,
116     &IID_IHTMLDocument4,
117     &IID_IHTMLDocument5,
118     &IID_IHTMLDOMChildrenCollection,
119     &IID_IHTMLDOMNode,
120     &IID_IHTMLDOMNode2,
121     &IID_IHTMLDOMTextNode,
122     &IID_IHTMLElement,
123     &IID_IHTMLElement2,
124     &IID_IHTMLElement3,
125     &IID_IHTMLElement4,
126     &IID_IHTMLElementCollection,
127     &IID_IHTMLEventObj,
128     &IID_IHTMLFrameBase,
129     &IID_IHTMLFrameBase2,
130     &IID_IHTMLGenericElement,
131     &IID_IHTMLImageElementFactory,
132     &IID_IHTMLImgElement,
133     &IID_IHTMLInputElement,
134     &IID_IHTMLLocation,
135     &IID_IHTMLOptionElement,
136     &IID_IHTMLSelectElement,
137     &IID_IHTMLStyle,
138     &IID_IHTMLStyle2,
139     &IID_IHTMLStyle3,
140     &IID_IHTMLStyle4,
141     &IID_IHTMLTable,
142     &IID_IHTMLTableRow,
143     &IID_IHTMLTextContainer,
144     &IID_IHTMLUniqueName,
145     &IID_IHTMLWindow2,
146     &IID_IHTMLWindow3,
147     &IID_IOmNavigator
148 };
149
150 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
151 {
152     HRESULT hres;
153
154     if(!typelib) {
155         ITypeLib *tl;
156
157         hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
158         if(FAILED(hres)) {
159             ERR("LoadRegTypeLib failed: %08x\n", hres);
160             return hres;
161         }
162
163         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
164             ITypeLib_Release(tl);
165     }
166
167     if(!typeinfos[tid]) {
168         ITypeInfo *typeinfo;
169
170         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
171         if(FAILED(hres)) {
172             ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
173             return hres;
174         }
175
176         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
177             ITypeInfo_Release(typeinfo);
178     }
179
180     *typeinfo = typeinfos[tid];
181     return S_OK;
182 }
183
184 void release_typelib(void)
185 {
186     dispex_data_t *iter;
187     unsigned i;
188
189     while(!list_empty(&dispex_data_list)) {
190         iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
191         list_remove(&iter->entry);
192
193         for(i=0; i < iter->func_cnt; i++)
194             SysFreeString(iter->funcs[i].name);
195
196         heap_free(iter->funcs);
197         heap_free(iter->name_table);
198         heap_free(iter);
199     }
200
201     if(!typelib)
202         return;
203
204     for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
205         if(typeinfos[i])
206             ITypeInfo_Release(typeinfos[i]);
207
208     ITypeLib_Release(typelib);
209 }
210
211 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
212 {
213     HRESULT hres;
214
215     if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
216         return;
217
218     if(data->func_cnt == *size)
219         data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
220
221     hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
222     if(FAILED(hres))
223         return;
224
225     data->funcs[data->func_cnt].id = desc->memid;
226     data->funcs[data->func_cnt].tid = tid;
227     data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
228
229     data->func_cnt++;
230 }
231
232 static int dispid_cmp(const void *p1, const void *p2)
233 {
234     return ((func_info_t*)p1)->id - ((func_info_t*)p2)->id;
235 }
236
237 static int func_name_cmp(const void *p1, const void *p2)
238 {
239     return strcmpiW((*(func_info_t**)p1)->name, (*(func_info_t**)p2)->name);
240 }
241
242 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
243 {
244     const tid_t *tid = This->data->iface_tids;
245     FUNCDESC *funcdesc;
246     dispex_data_t *data;
247     DWORD size = 16, i;
248     ITypeInfo *ti, *dti;
249     HRESULT hres;
250
251     TRACE("(%p)\n", This);
252
253     if(This->data->disp_tid) {
254         hres = get_typeinfo(This->data->disp_tid, &dti);
255         if(FAILED(hres)) {
256             ERR("Could not get disp type info: %08x\n", hres);
257             return NULL;
258         }
259     }
260
261     data = heap_alloc(sizeof(dispex_data_t));
262     data->func_cnt = 0;
263     data->func_disp_cnt = 0;
264     data->funcs = heap_alloc(size*sizeof(func_info_t));
265     list_add_tail(&dispex_data_list, &data->entry);
266
267     while(*tid) {
268         hres = get_typeinfo(*tid, &ti);
269         if(FAILED(hres))
270             break;
271
272         i=7;
273         while(1) {
274             hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
275             if(FAILED(hres))
276                 break;
277
278             add_func_info(data, &size, *tid, funcdesc, dti);
279             ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
280         }
281
282         tid++;
283     }
284
285     if(!data->func_cnt) {
286         heap_free(data->funcs);
287         data->funcs = NULL;
288     }else if(data->func_cnt != size) {
289         data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
290     }
291
292     qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
293
294     if(data->funcs) {
295         data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
296         for(i=0; i < data->func_cnt; i++)
297             data->name_table[i] = data->funcs+i;
298         qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
299     }else {
300         data->name_table = NULL;
301     }
302
303     return data;
304 }
305
306 static CRITICAL_SECTION cs_dispex_static_data;
307 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
308 {
309     0, 0, &cs_dispex_static_data,
310     { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
311       0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
312 };
313 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
314
315
316 static dispex_data_t *get_dispex_data(DispatchEx *This)
317 {
318     if(This->data->data)
319         return This->data->data;
320
321     EnterCriticalSection(&cs_dispex_static_data);
322
323     if(!This->data->data)
324         This->data->data = preprocess_dispex_data(This);
325
326     LeaveCriticalSection(&cs_dispex_static_data);
327
328     return This->data->data;
329 }
330
331 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
332 {
333     EXCEPINFO ei;
334     IDispatchEx *dispex;
335     VARIANT res;
336     HRESULT hres;
337
338     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
339     if(FAILED(hres)) {
340         FIXME("Could not get IDispatchEx interface: %08x\n", hres);
341         return hres;
342     }
343
344     VariantInit(&res);
345     memset(&ei, 0, sizeof(ei));
346
347     hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
348
349     IDispatchEx_Release(dispex);
350     VariantClear(&res);
351     return hres;
352 }
353
354 static inline BOOL is_custom_dispid(DISPID id)
355 {
356     return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
357 }
358
359 static inline BOOL is_dynamic_dispid(DISPID id)
360 {
361     return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
362 }
363
364 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
365 {
366     return !alloc || This->dynamic_data
367         ? This->dynamic_data
368         : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
369 }
370
371 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, BOOL alloc, dynamic_prop_t **ret)
372 {
373     dispex_dynamic_data_t *data = get_dynamic_data(This, alloc);
374     unsigned i;
375
376     if(!data) {
377         if(alloc)
378             return E_OUTOFMEMORY;
379
380         TRACE("not found %s\n", debugstr_w(name));
381         return DISP_E_UNKNOWNNAME;
382     }
383
384     for(i=0; i < data->prop_cnt; i++) {
385         if(!strcmpW(data->props[i].name, name)) {
386             *ret = data->props+i;
387             return S_OK;
388         }
389     }
390
391     TRACE("creating dynamic prop %s\n", debugstr_w(name));
392
393     if(!data->buf_size) {
394         data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
395         if(!data->props)
396             return E_OUTOFMEMORY;
397         data->buf_size = 4;
398     }else if(data->buf_size == data->prop_cnt) {
399         dynamic_prop_t *new_props;
400
401         new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
402         if(!new_props)
403             return E_OUTOFMEMORY;
404
405         data->props = new_props;
406         data->buf_size <<= 1;
407     }
408
409     data->props[data->prop_cnt].name = heap_strdupW(name);
410     VariantInit(&data->props[data->prop_cnt].var);
411     *ret = data->props + data->prop_cnt++;
412
413     return S_OK;
414 }
415
416 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
417 {
418     dynamic_prop_t *prop;
419     HRESULT hres;
420
421     hres = get_dynamic_prop(This, name, alloc, &prop);
422     if(FAILED(hres))
423         return hres;
424
425     *ret = &prop->var;
426     return S_OK;
427 }
428
429 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
430         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
431 {
432     if(This->data->vtbl && This->data->vtbl->value)
433         return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
434
435     switch(flags) {
436     case DISPATCH_PROPERTYGET:
437         V_VT(res) = VT_BSTR;
438         V_BSTR(res) = SysAllocString(objectW);
439         if(!V_BSTR(res))
440             return E_OUTOFMEMORY;
441         break;
442     default:
443         FIXME("Unimplemented flags %x\n", flags);
444         return E_NOTIMPL;
445     }
446
447     return S_OK;
448 }
449
450 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
451         EXCEPINFO *ei)
452 {
453     ITypeInfo *ti;
454     IUnknown *unk;
455     UINT argerr=0;
456     HRESULT hres;
457
458     hres = get_typeinfo(func->tid, &ti);
459     if(FAILED(hres)) {
460         ERR("Could not get type info: %08x\n", hres);
461         return hres;
462     }
463
464     hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
465     if(FAILED(hres)) {
466         ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
467         return E_FAIL;
468     }
469
470     hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
471
472     IUnknown_Release(unk);
473     return hres;
474 }
475
476 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
477
478 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
479 {
480     func_disp_t *This = FUNCTION_THIS(iface);
481
482     if(IsEqualGUID(&IID_IUnknown, riid)) {
483         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
484         *ppv = FUNCUNKNOWN(This);
485     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
486         return *ppv ? S_OK : E_NOINTERFACE;
487     }else {
488         *ppv = NULL;
489         return E_NOINTERFACE;
490     }
491
492     IUnknown_AddRef((IUnknown*)*ppv);
493     return S_OK;
494 }
495
496 static ULONG WINAPI Function_AddRef(IUnknown *iface)
497 {
498     func_disp_t *This = FUNCTION_THIS(iface);
499
500     TRACE("(%p)\n", This);
501
502     return IDispatchEx_AddRef(DISPATCHEX(This->obj));
503 }
504
505 static ULONG WINAPI Function_Release(IUnknown *iface)
506 {
507     func_disp_t *This = FUNCTION_THIS(iface);
508
509     TRACE("(%p)\n", This);
510
511     return IDispatchEx_Release(DISPATCHEX(This->obj));
512 }
513
514 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
515         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
516 {
517     func_disp_t *This = FUNCTION_THIS(iface);
518     HRESULT hres;
519
520     switch(flags) {
521     case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
522         if(!res)
523             return E_INVALIDARG;
524     case DISPATCH_METHOD:
525         hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
526         break;
527     default:
528         FIXME("Unimplemented flags %x\n", flags);
529         hres = E_NOTIMPL;
530     }
531
532     return hres;
533 }
534
535 #undef FUNCTION_THIS
536
537 static const IUnknownVtbl FunctionUnkVtbl = {
538     Function_QueryInterface,
539     Function_AddRef,
540     Function_Release
541 };
542
543 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
544     function_value,
545     NULL,
546     NULL
547 };
548
549 static const tid_t function_iface_tids[] = {0};
550
551 static dispex_static_data_t function_dispex = {
552     &function_dispex_vtbl,
553     NULL_tid,
554     NULL,
555     function_iface_tids
556 };
557
558 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
559 {
560     func_disp_t *ret;
561
562     ret = heap_alloc_zero(sizeof(func_disp_t));
563     if(!ret)
564         return NULL;
565
566     ret->lpIUnknownVtbl = &FunctionUnkVtbl;
567     init_dispex(&ret->dispex, FUNCUNKNOWN(ret),  &function_dispex);
568     ret->obj = obj;
569     ret->info = info;
570
571     return ret;
572 }
573
574 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
575         EXCEPINFO *ei)
576 {
577     HRESULT hres;
578
579     switch(flags) {
580     case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
581         if(!res)
582             return E_INVALIDARG;
583     case DISPATCH_METHOD:
584         hres = typeinfo_invoke(This, func, flags, dp, res, ei);
585         break;
586     case DISPATCH_PROPERTYGET: {
587         dispex_dynamic_data_t *dynamic_data;
588
589         if(func->id == DISPID_VALUE) {
590             BSTR ret;
591
592             ret = SysAllocString(objectW);
593             if(!ret)
594                 return E_OUTOFMEMORY;
595
596             V_VT(res) = VT_BSTR;
597             V_BSTR(res) = ret;
598             return S_OK;
599         }
600
601         dynamic_data = get_dynamic_data(This, TRUE);
602         if(!dynamic_data)
603             return E_OUTOFMEMORY;
604
605         if(!dynamic_data->func_disps) {
606             dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
607             if(!dynamic_data->func_disps)
608                 return E_OUTOFMEMORY;
609         }
610
611         if(!dynamic_data->func_disps[func->func_disp_idx]) {
612             dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
613             if(!dynamic_data->func_disps[func->func_disp_idx])
614                 return E_OUTOFMEMORY;
615         }
616
617         V_VT(res) = VT_DISPATCH;
618         V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
619         IDispatch_AddRef(V_DISPATCH(res));
620         hres = S_OK;
621         break;
622     }
623     default:
624         FIXME("Unimplemented flags %x\n", flags);
625     case DISPATCH_PROPERTYPUT:
626         hres = E_NOTIMPL;
627     }
628
629     return hres;
630 }
631
632 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
633 {
634     int min, max, n;
635
636     min = 0;
637     max = data->func_cnt-1;
638
639     while(min <= max) {
640         n = (min+max)/2;
641
642         if(data->funcs[n].id == id) {
643             *ret = data->funcs+n;
644             return S_OK;
645         }
646
647         if(data->funcs[n].id < id)
648             min = n+1;
649         else
650             max = n-1;
651     }
652
653     WARN("invalid id %x\n", id);
654     return DISP_E_UNKNOWNNAME;
655 }
656
657 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
658
659 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
660 {
661     DispatchEx *This = DISPATCHEX_THIS(iface);
662
663     return IUnknown_QueryInterface(This->outer, riid, ppv);
664 }
665
666 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
667 {
668     DispatchEx *This = DISPATCHEX_THIS(iface);
669
670     return IUnknown_AddRef(This->outer);
671 }
672
673 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
674 {
675     DispatchEx *This = DISPATCHEX_THIS(iface);
676
677     return IUnknown_Release(This->outer);
678 }
679
680 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
681 {
682     DispatchEx *This = DISPATCHEX_THIS(iface);
683
684     TRACE("(%p)->(%p)\n", This, pctinfo);
685
686     *pctinfo = 1;
687     return S_OK;
688 }
689
690 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
691                                               LCID lcid, ITypeInfo **ppTInfo)
692 {
693     DispatchEx *This = DISPATCHEX_THIS(iface);
694     HRESULT hres;
695
696     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
697
698     hres = get_typeinfo(This->data->disp_tid, ppTInfo);
699     if(FAILED(hres))
700         return hres;
701
702     ITypeInfo_AddRef(*ppTInfo);
703     return S_OK;
704 }
705
706 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
707                                                 LPOLESTR *rgszNames, UINT cNames,
708                                                 LCID lcid, DISPID *rgDispId)
709 {
710     DispatchEx *This = DISPATCHEX_THIS(iface);
711     UINT i;
712     HRESULT hres;
713
714     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
715           lcid, rgDispId);
716
717     for(i=0; i < cNames; i++) {
718         hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
719         if(FAILED(hres))
720             return hres;
721     }
722
723     return S_OK;
724 }
725
726 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
727                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
728                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
729 {
730     DispatchEx *This = DISPATCHEX_THIS(iface);
731
732     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
733           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
734
735     return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
736                                 pDispParams, pVarResult, pExcepInfo, NULL);
737 }
738
739 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
740 {
741     DispatchEx *This = DISPATCHEX_THIS(iface);
742     dynamic_prop_t *dprop;
743     dispex_data_t *data;
744     int min, max, n, c;
745     HRESULT hres;
746
747     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
748
749     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
750         FIXME("Unsupported grfdex %x\n", grfdex);
751
752     data = get_dispex_data(This);
753     if(!data)
754         return E_FAIL;
755
756     min = 0;
757     max = data->func_cnt-1;
758
759     while(min <= max) {
760         n = (min+max)/2;
761
762         c = strcmpiW(data->name_table[n]->name, bstrName);
763         if(!c) {
764             if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
765                 break;
766
767             *pid = data->name_table[n]->id;
768             return S_OK;
769         }
770
771         if(c > 0)
772             max = n-1;
773         else
774             min = n+1;
775     }
776
777     if(This->data->vtbl && This->data->vtbl->get_dispid) {
778         HRESULT hres;
779
780         hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
781         if(hres != DISP_E_UNKNOWNNAME)
782             return hres;
783     }
784
785     hres = get_dynamic_prop(This, bstrName, grfdex&fdexNameEnsure, &dprop);
786     if(FAILED(hres))
787         return hres;
788
789     *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
790     return S_OK;
791 }
792
793 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
794         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
795 {
796     DispatchEx *This = DISPATCHEX_THIS(iface);
797     dispex_data_t *data;
798     func_info_t *func;
799     HRESULT hres;
800
801     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
802
803     if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
804         return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
805
806     if(wFlags == DISPATCH_CONSTRUCT) {
807         if(id == DISPID_VALUE) {
808             if(This->data->vtbl && This->data->vtbl->value) {
809                 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
810                         pvarRes, pei, pspCaller);
811             }
812             FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
813             return E_FAIL;
814         }
815         FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
816         return E_FAIL;
817     }
818
819     if(is_dynamic_dispid(id)) {
820         DWORD idx = id - DISPID_DYNPROP_0;
821         VARIANT *var;
822
823         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
824             return DISP_E_UNKNOWNNAME;
825
826         var = &This->dynamic_data->props[idx].var;
827
828         switch(wFlags) {
829         case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
830             if(!pvarRes)
831                 return E_INVALIDARG;
832         case DISPATCH_METHOD: {
833             DISPID named_arg = DISPID_THIS;
834             DISPPARAMS dp = {NULL, &named_arg, 0, 1};
835             IDispatchEx *dispex;
836
837             if(V_VT(var) != VT_DISPATCH) {
838                 FIXME("invoke vt %d\n", V_VT(var));
839                 return E_NOTIMPL;
840             }
841
842             if(pdp->cNamedArgs) {
843                 FIXME("named args not supported\n");
844                 return E_NOTIMPL;
845             }
846
847             dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
848             if(!dp.rgvarg)
849                 return E_OUTOFMEMORY;
850
851             dp.cArgs = pdp->cArgs+1;
852             memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
853
854             V_VT(dp.rgvarg) = VT_DISPATCH;
855             V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
856
857             hres = IDispatch_QueryInterface(V_DISPATCH(var), &IID_IDispatchEx, (void**)&dispex);
858             TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
859             if(SUCCEEDED(hres)) {
860                 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
861                 IDispatchEx_Release(dispex);
862             }else {
863                 ULONG err = 0;
864                 hres = IDispatch_Invoke(V_DISPATCH(var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
865             }
866             TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
867
868             heap_free(dp.rgvarg);
869             return hres;
870         }
871         case DISPATCH_PROPERTYGET:
872             return VariantCopy(pvarRes, var);
873         case DISPATCH_PROPERTYPUT:
874             VariantClear(var);
875             return VariantCopy(var, pdp->rgvarg);
876         default:
877             FIXME("unhandled wFlags %x\n", wFlags);
878             return E_NOTIMPL;
879         }
880     }
881
882     data = get_dispex_data(This);
883     if(!data)
884         return E_FAIL;
885
886     hres = get_builtin_func(data, id, &func);
887     if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
888         return dispex_value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
889     if(FAILED(hres))
890         return hres;
891
892     if(func->func_disp_idx == -1)
893         hres = typeinfo_invoke(This, func, wFlags, pdp, pvarRes, pei);
894     else
895         hres = function_invoke(This, func, wFlags, pdp, pvarRes, pei);
896
897     return hres;
898 }
899
900 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
901 {
902     DispatchEx *This = DISPATCHEX_THIS(iface);
903     FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
904     return E_NOTIMPL;
905 }
906
907 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
908 {
909     DispatchEx *This = DISPATCHEX_THIS(iface);
910     FIXME("(%p)->(%x)\n", This, id);
911     return E_NOTIMPL;
912 }
913
914 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
915 {
916     DispatchEx *This = DISPATCHEX_THIS(iface);
917     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
918     return E_NOTIMPL;
919 }
920
921 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
922 {
923     DispatchEx *This = DISPATCHEX_THIS(iface);
924     dispex_data_t *data;
925     func_info_t *func;
926     HRESULT hres;
927
928     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
929
930     if(is_dynamic_dispid(id)) {
931         DWORD idx = id - DISPID_DYNPROP_0;
932
933         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
934             return DISP_E_UNKNOWNNAME;
935
936         *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
937         if(!*pbstrName)
938             return E_OUTOFMEMORY;
939
940         return S_OK;
941     }
942
943     data = get_dispex_data(This);
944     if(!data)
945         return E_FAIL;
946
947     hres = get_builtin_func(data, id, &func);
948     if(FAILED(hres))
949         return hres;
950
951     *pbstrName = SysAllocString(func->name);
952     if(!*pbstrName)
953         return E_OUTOFMEMORY;
954     return S_OK;
955 }
956
957 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
958 {
959     DispatchEx *This = DISPATCHEX_THIS(iface);
960     dispex_data_t *data;
961     func_info_t *func;
962     HRESULT hres;
963
964     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
965
966     if(is_dynamic_dispid(id)) {
967         DWORD idx = id - DISPID_DYNPROP_0;
968
969         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
970             return DISP_E_UNKNOWNNAME;
971
972         if(idx+1 == This->dynamic_data->prop_cnt) {
973             *pid = DISPID_STARTENUM;
974             return S_FALSE;
975         }
976
977         *pid = id+1;
978         return S_OK;
979     }
980
981     data = get_dispex_data(This);
982     if(!data)
983         return E_FAIL;
984
985     if(id == DISPID_STARTENUM) {
986         func = data->funcs;
987     }else {
988         hres = get_builtin_func(data, id, &func);
989         if(FAILED(hres))
990             return hres;
991         func++;
992     }
993
994     while(func < data->funcs+data->func_cnt) {
995         /* FIXME: Skip hidden properties */
996         if(func->func_disp_idx == -1) {
997             *pid = func->id;
998             return S_OK;
999         }
1000         func++;
1001     }
1002
1003     if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1004         *pid = DISPID_DYNPROP_0;
1005         return S_OK;
1006     }
1007
1008     *pid = DISPID_STARTENUM;
1009     return S_FALSE;
1010 }
1011
1012 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1013 {
1014     DispatchEx *This = DISPATCHEX_THIS(iface);
1015     FIXME("(%p)->(%p)\n", This, ppunk);
1016     return E_NOTIMPL;
1017 }
1018
1019 #undef DISPATCHEX_THIS
1020
1021 static IDispatchExVtbl DispatchExVtbl = {
1022     DispatchEx_QueryInterface,
1023     DispatchEx_AddRef,
1024     DispatchEx_Release,
1025     DispatchEx_GetTypeInfoCount,
1026     DispatchEx_GetTypeInfo,
1027     DispatchEx_GetIDsOfNames,
1028     DispatchEx_Invoke,
1029     DispatchEx_GetDispID,
1030     DispatchEx_InvokeEx,
1031     DispatchEx_DeleteMemberByName,
1032     DispatchEx_DeleteMemberByDispID,
1033     DispatchEx_GetMemberProperties,
1034     DispatchEx_GetMemberName,
1035     DispatchEx_GetNextDispID,
1036     DispatchEx_GetNameSpaceParent
1037 };
1038
1039 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1040 {
1041     static const IID IID_UndocumentedScriptIface =
1042         {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1043     static const IID IID_IDispatchJS =
1044         {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1045
1046     if(IsEqualGUID(&IID_IDispatch, riid)) {
1047         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1048         *ppv = DISPATCHEX(This);
1049     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1050         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1051         *ppv = DISPATCHEX(This);
1052     }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1053         TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1054         *ppv = NULL;
1055     }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1056         TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1057         *ppv = NULL;
1058     }else {
1059         return FALSE;
1060     }
1061
1062     if(*ppv)
1063         IUnknown_AddRef((IUnknown*)*ppv);
1064     return TRUE;
1065 }
1066
1067 void release_dispex(DispatchEx *This)
1068 {
1069     dynamic_prop_t *prop;
1070
1071     if(!This->dynamic_data)
1072         return;
1073
1074     for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1075         VariantClear(&prop->var);
1076         heap_free(prop->name);
1077     }
1078
1079     heap_free(This->dynamic_data->props);
1080
1081     if(This->dynamic_data->func_disps) {
1082         unsigned i;
1083
1084         for(i=0; i < This->data->data->func_disp_cnt; i++) {
1085             if(This->dynamic_data->func_disps[i]) {
1086                 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1087                 heap_free(This->dynamic_data->func_disps[i]);
1088             }
1089         }
1090
1091         heap_free(This->dynamic_data->func_disps);
1092     }
1093
1094     heap_free(This->dynamic_data);
1095 }
1096
1097 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1098 {
1099     dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1100     dispex->outer = outer;
1101     dispex->data = data;
1102     dispex->dynamic_data = NULL;
1103 }