wined3d: Set the volume.c GLINFO_LOCATION to *gl_info.
[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     DWORD flags;
56 } dynamic_prop_t;
57
58 #define DYNPROP_DELETED    0x01
59
60 typedef struct {
61     DispatchEx dispex;
62     const IUnknownVtbl *lpIUnknownVtbl;
63     DispatchEx *obj;
64     func_info_t *info;
65 } func_disp_t;
66
67 #define FUNCUNKNOWN(x)  ((IUnknown*) &(x)->lpIUnknownVtbl)
68
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;
74 };
75
76 #define DISPID_DYNPROP_0    0x50000000
77 #define DISPID_DYNPROP_MAX  0x5fffffff
78
79 #define FDEX_VERSION_MASK 0xf0000000
80
81 static ITypeLib *typelib;
82 static ITypeInfo *typeinfos[LAST_tid];
83 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
84
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
165 };
166
167 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
168 {
169     HRESULT hres;
170
171     if(!typelib) {
172         ITypeLib *tl;
173
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;
178         }
179
180         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
181             ITypeLib_Release(tl);
182     }
183
184     if(!typeinfos[tid]) {
185         ITypeInfo *typeinfo;
186
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;
191         }
192
193         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
194             ITypeInfo_Release(typeinfo);
195     }
196
197     *typeinfo = typeinfos[tid];
198     return S_OK;
199 }
200
201 void release_typelib(void)
202 {
203     dispex_data_t *iter;
204     unsigned i;
205
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);
209
210         for(i=0; i < iter->func_cnt; i++)
211             SysFreeString(iter->funcs[i].name);
212
213         heap_free(iter->funcs);
214         heap_free(iter->name_table);
215         heap_free(iter);
216     }
217
218     if(!typelib)
219         return;
220
221     for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
222         if(typeinfos[i])
223             ITypeInfo_Release(typeinfos[i]);
224
225     ITypeLib_Release(typelib);
226 }
227
228 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
229 {
230     HRESULT hres;
231
232     if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
233         return;
234
235     if(data->func_cnt == *size)
236         data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
237
238     hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
239     if(FAILED(hres))
240         return;
241
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;
245
246     data->func_cnt++;
247 }
248
249 static int dispid_cmp(const void *p1, const void *p2)
250 {
251     return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
252 }
253
254 static int func_name_cmp(const void *p1, const void *p2)
255 {
256     return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
257 }
258
259 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
260 {
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;
267
268     TRACE("(%p)\n", This);
269
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;
275         }
276     }
277
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);
283
284     while(*tid) {
285         hres = get_typeinfo(*tid, &ti);
286         if(FAILED(hres))
287             break;
288
289         i=7;
290         while(1) {
291             hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
292             if(FAILED(hres))
293                 break;
294
295             add_func_info(data, &size, *tid, funcdesc, dti);
296             ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
297         }
298
299         tid++;
300     }
301
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));
307     }
308
309     qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
310
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;
318     }
319
320     return data;
321 }
322
323 static int id_cmp(const void *p1, const void *p2)
324 {
325     return *(const DISPID*)p1 - *(const DISPID*)p2;
326 }
327
328 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
329 {
330     unsigned i, func_cnt;
331     FUNCDESC *funcdesc;
332     ITypeInfo *ti;
333     TYPEATTR *attr;
334     DISPID *ids;
335     HRESULT hres;
336
337     hres = get_typeinfo(tid, &ti);
338     if(FAILED(hres))
339         return hres;
340
341     hres = ITypeInfo_GetTypeAttr(ti, &attr);
342     if(FAILED(hres)) {
343         ITypeInfo_Release(ti);
344         return hres;
345     }
346
347     func_cnt = attr->cFuncs;
348     ITypeInfo_ReleaseTypeAttr(ti, attr);
349
350     ids = heap_alloc(func_cnt*sizeof(DISPID));
351     if(!ids) {
352         ITypeInfo_Release(ti);
353         return E_OUTOFMEMORY;
354     }
355
356     for(i=0; i < func_cnt; i++) {
357         hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
358         if(FAILED(hres))
359             break;
360
361         ids[i] = funcdesc->memid;
362         ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
363     }
364
365     ITypeInfo_Release(ti);
366     if(FAILED(hres)) {
367         heap_free(ids);
368         return hres;
369     }
370
371     qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
372
373     *ret_size = func_cnt;
374     *ret = ids;
375     return S_OK;
376 }
377
378 static CRITICAL_SECTION cs_dispex_static_data;
379 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
380 {
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") }
384 };
385 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
386
387
388 static dispex_data_t *get_dispex_data(DispatchEx *This)
389 {
390     if(This->data->data)
391         return This->data->data;
392
393     EnterCriticalSection(&cs_dispex_static_data);
394
395     if(!This->data->data)
396         This->data->data = preprocess_dispex_data(This);
397
398     LeaveCriticalSection(&cs_dispex_static_data);
399
400     return This->data->data;
401 }
402
403 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
404 {
405     EXCEPINFO ei;
406     IDispatchEx *dispex;
407     VARIANT res;
408     HRESULT hres;
409
410     VariantInit(&res);
411     memset(&ei, 0, sizeof(ei));
412
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);
421     }
422
423     VariantClear(&res);
424     return hres;
425 }
426
427 static inline BOOL is_custom_dispid(DISPID id)
428 {
429     return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
430 }
431
432 static inline BOOL is_dynamic_dispid(DISPID id)
433 {
434     return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
435 }
436
437 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
438 {
439     return !alloc || This->dynamic_data
440         ? This->dynamic_data
441         : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
442 }
443
444 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
445 {
446     const BOOL alloc = flags & fdexNameEnsure;
447     dispex_dynamic_data_t *data;
448     dynamic_prop_t *prop;
449
450     data = get_dynamic_data(This, alloc);
451     if(!data) {
452         if(alloc)
453             return E_OUTOFMEMORY;
454
455         TRACE("not found %s\n", debugstr_w(name));
456         return DISP_E_UNKNOWNNAME;
457     }
458
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;
465             }
466             *ret = prop;
467             return S_OK;
468         }
469     }
470
471     if(!alloc)
472         return DISP_E_UNKNOWNNAME;
473
474     TRACE("creating dynamic prop %s\n", debugstr_w(name));
475
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;
483
484         new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
485         if(!new_props)
486             return E_OUTOFMEMORY;
487
488         data->props = new_props;
489         data->buf_size <<= 1;
490     }
491
492     prop = data->props + data->prop_cnt;
493
494     prop->name = heap_strdupW(name);
495     if(!prop->name)
496         return E_OUTOFMEMORY;
497
498     VariantInit(&prop->var);
499     prop->flags = 0;
500     data->prop_cnt++;
501     *ret = prop;
502     return S_OK;
503 }
504
505 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
506 {
507     dynamic_prop_t *prop;
508     HRESULT hres;
509
510     hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
511     if(FAILED(hres))
512         return hres;
513
514     *ret = &prop->var;
515     return S_OK;
516 }
517
518 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
519         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
520 {
521     if(This->data->vtbl && This->data->vtbl->value)
522         return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
523
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;
534     }
535
536     return S_OK;
537 }
538
539 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
540         EXCEPINFO *ei)
541 {
542     ITypeInfo *ti;
543     IUnknown *unk;
544     UINT argerr=0;
545     HRESULT hres;
546
547     hres = get_typeinfo(func->tid, &ti);
548     if(FAILED(hres)) {
549         ERR("Could not get type info: %08x\n", hres);
550         return hres;
551     }
552
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;
557     }
558
559     hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
560
561     IUnknown_Release(unk);
562     return hres;
563 }
564
565 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
566
567 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
568 {
569     func_disp_t *This = FUNCTION_THIS(iface);
570
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;
579     }
580
581     IUnknown_AddRef((IUnknown*)*ppv);
582     return S_OK;
583 }
584
585 static ULONG WINAPI Function_AddRef(IUnknown *iface)
586 {
587     func_disp_t *This = FUNCTION_THIS(iface);
588
589     TRACE("(%p)\n", This);
590
591     return IDispatchEx_AddRef(DISPATCHEX(This->obj));
592 }
593
594 static ULONG WINAPI Function_Release(IUnknown *iface)
595 {
596     func_disp_t *This = FUNCTION_THIS(iface);
597
598     TRACE("(%p)\n", This);
599
600     return IDispatchEx_Release(DISPATCHEX(This->obj));
601 }
602
603 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
604         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
605 {
606     func_disp_t *This = FUNCTION_THIS(iface);
607     HRESULT hres;
608
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;
619     }
620
621     return hres;
622 }
623
624 #undef FUNCTION_THIS
625
626 static const IUnknownVtbl FunctionUnkVtbl = {
627     Function_QueryInterface,
628     Function_AddRef,
629     Function_Release
630 };
631
632 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
633     function_value,
634     NULL,
635     NULL
636 };
637
638 static const tid_t function_iface_tids[] = {0};
639
640 static dispex_static_data_t function_dispex = {
641     &function_dispex_vtbl,
642     NULL_tid,
643     NULL,
644     function_iface_tids
645 };
646
647 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
648 {
649     func_disp_t *ret;
650
651     ret = heap_alloc_zero(sizeof(func_disp_t));
652     if(!ret)
653         return NULL;
654
655     ret->lpIUnknownVtbl = &FunctionUnkVtbl;
656     init_dispex(&ret->dispex, FUNCUNKNOWN(ret),  &function_dispex);
657     ret->obj = obj;
658     ret->info = info;
659
660     return ret;
661 }
662
663 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
664         EXCEPINFO *ei)
665 {
666     HRESULT hres;
667
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;
677
678         if(func->id == DISPID_VALUE) {
679             BSTR ret;
680
681             ret = SysAllocString(objectW);
682             if(!ret)
683                 return E_OUTOFMEMORY;
684
685             V_VT(res) = VT_BSTR;
686             V_BSTR(res) = ret;
687             return S_OK;
688         }
689
690         dynamic_data = get_dynamic_data(This, TRUE);
691         if(!dynamic_data)
692             return E_OUTOFMEMORY;
693
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;
698         }
699
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;
704         }
705
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;
711     }
712     default:
713         FIXME("Unimplemented flags %x\n", flags);
714     case DISPATCH_PROPERTYPUT:
715         hres = E_NOTIMPL;
716     }
717
718     return hres;
719 }
720
721 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
722 {
723     int min, max, n;
724
725     min = 0;
726     max = data->func_cnt-1;
727
728     while(min <= max) {
729         n = (min+max)/2;
730
731         if(data->funcs[n].id == id) {
732             *ret = data->funcs+n;
733             return S_OK;
734         }
735
736         if(data->funcs[n].id < id)
737             min = n+1;
738         else
739             max = n-1;
740     }
741
742     WARN("invalid id %x\n", id);
743     return DISP_E_UNKNOWNNAME;
744 }
745
746 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
747 {
748     dispex_data_t *data;
749     int min, max, n, c;
750
751     data = get_dispex_data(This);
752     if(!data)
753         return E_FAIL;
754
755     min = 0;
756     max = data->func_cnt-1;
757
758     while(min <= max) {
759         n = (min+max)/2;
760
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;
765
766             *ret = data->name_table[n]->id;
767             return S_OK;
768         }
769
770         if(c > 0)
771             max = n-1;
772         else
773             min = n+1;
774     }
775
776     if(This->data->vtbl && This->data->vtbl->get_dispid) {
777         HRESULT hres;
778
779         hres = This->data->vtbl->get_dispid(This->outer, name, grfdex, ret);
780         if(hres != DISP_E_UNKNOWNNAME)
781             return hres;
782     }
783
784     return DISP_E_UNKNOWNNAME;
785 }
786
787 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
788         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
789 {
790     dispex_data_t *data;
791     func_info_t *func;
792     HRESULT hres;
793
794     data = get_dispex_data(This);
795     if(!data)
796         return E_FAIL;
797
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;
803
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);
808
809     return hres;
810 }
811
812 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
813 {
814     dynamic_prop_t *prop;
815     DISPID id;
816     HRESULT hres;
817
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;
824
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;
830
831         *success = VARIANT_TRUE;
832         return S_OK;
833     }
834
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;
841     }
842
843     VariantClear(&prop->var);
844     prop->flags |= DYNPROP_DELETED;
845     *success = VARIANT_TRUE;
846     return S_OK;
847 }
848
849 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
850
851 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
852 {
853     DispatchEx *This = DISPATCHEX_THIS(iface);
854
855     return IUnknown_QueryInterface(This->outer, riid, ppv);
856 }
857
858 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
859 {
860     DispatchEx *This = DISPATCHEX_THIS(iface);
861
862     return IUnknown_AddRef(This->outer);
863 }
864
865 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
866 {
867     DispatchEx *This = DISPATCHEX_THIS(iface);
868
869     return IUnknown_Release(This->outer);
870 }
871
872 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
873 {
874     DispatchEx *This = DISPATCHEX_THIS(iface);
875
876     TRACE("(%p)->(%p)\n", This, pctinfo);
877
878     *pctinfo = 1;
879     return S_OK;
880 }
881
882 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
883                                               LCID lcid, ITypeInfo **ppTInfo)
884 {
885     DispatchEx *This = DISPATCHEX_THIS(iface);
886     HRESULT hres;
887
888     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
889
890     hres = get_typeinfo(This->data->disp_tid, ppTInfo);
891     if(FAILED(hres))
892         return hres;
893
894     ITypeInfo_AddRef(*ppTInfo);
895     return S_OK;
896 }
897
898 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
899                                                 LPOLESTR *rgszNames, UINT cNames,
900                                                 LCID lcid, DISPID *rgDispId)
901 {
902     DispatchEx *This = DISPATCHEX_THIS(iface);
903     UINT i;
904     HRESULT hres;
905
906     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
907           lcid, rgDispId);
908
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;
913     }
914
915     return S_OK;
916 }
917
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)
921 {
922     DispatchEx *This = DISPATCHEX_THIS(iface);
923
924     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
925           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
926
927     return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
928                                 pDispParams, pVarResult, pExcepInfo, NULL);
929 }
930
931 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
932 {
933     DispatchEx *This = DISPATCHEX_THIS(iface);
934     dynamic_prop_t *dprop;
935     HRESULT hres;
936
937     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
938
939     if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
940         FIXME("Unsupported grfdex %x\n", grfdex);
941
942     hres = get_builtin_id(This, bstrName, grfdex, pid);
943     if(hres != DISP_E_UNKNOWNNAME)
944         return hres;
945
946     hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
947     if(FAILED(hres))
948         return hres;
949
950     *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
951     return S_OK;
952 }
953
954 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
955         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
956 {
957     DispatchEx *This = DISPATCHEX_THIS(iface);
958     HRESULT hres;
959
960     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
961
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);
964
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);
970             }
971             FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
972             return E_FAIL;
973         }
974         FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
975         return E_FAIL;
976     }
977
978     if(is_dynamic_dispid(id)) {
979         DWORD idx = id - DISPID_DYNPROP_0;
980         dynamic_prop_t *prop;
981
982         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
983             return DISP_E_UNKNOWNNAME;
984
985         prop = This->dynamic_data->props+idx;
986
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;
995
996             if(V_VT(&prop->var) != VT_DISPATCH) {
997                 FIXME("invoke %s\n", debugstr_variant(&prop->var));
998                 return E_NOTIMPL;
999             }
1000
1001             if(pdp->cNamedArgs) {
1002                 FIXME("named args not supported\n");
1003                 return E_NOTIMPL;
1004             }
1005
1006             dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
1007             if(!dp.rgvarg)
1008                 return E_OUTOFMEMORY;
1009
1010             dp.cArgs = pdp->cArgs+1;
1011             memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
1012
1013             V_VT(dp.rgvarg) = VT_DISPATCH;
1014             V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
1015
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);
1024             }
1025             TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
1026
1027             heap_free(dp.rgvarg);
1028             return hres;
1029         }
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;
1040             }
1041
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;
1047
1048             prop->flags &= ~DYNPROP_DELETED;
1049             return S_OK;
1050         default:
1051             FIXME("unhandled wFlags %x\n", wFlags);
1052             return E_NOTIMPL;
1053         }
1054     }
1055
1056     return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1057 }
1058
1059 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1060 {
1061     DispatchEx *This = DISPATCHEX_THIS(iface);
1062
1063     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1064
1065     /* Not implemented by IE */
1066     return E_NOTIMPL;
1067 }
1068
1069 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1070 {
1071     DispatchEx *This = DISPATCHEX_THIS(iface);
1072     FIXME("(%p)->(%x)\n", This, id);
1073     return E_NOTIMPL;
1074 }
1075
1076 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1077 {
1078     DispatchEx *This = DISPATCHEX_THIS(iface);
1079     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1080     return E_NOTIMPL;
1081 }
1082
1083 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1084 {
1085     DispatchEx *This = DISPATCHEX_THIS(iface);
1086     dispex_data_t *data;
1087     func_info_t *func;
1088     HRESULT hres;
1089
1090     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1091
1092     if(is_dynamic_dispid(id)) {
1093         DWORD idx = id - DISPID_DYNPROP_0;
1094
1095         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1096             return DISP_E_UNKNOWNNAME;
1097
1098         *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1099         if(!*pbstrName)
1100             return E_OUTOFMEMORY;
1101
1102         return S_OK;
1103     }
1104
1105     data = get_dispex_data(This);
1106     if(!data)
1107         return E_FAIL;
1108
1109     hres = get_builtin_func(data, id, &func);
1110     if(FAILED(hres))
1111         return hres;
1112
1113     *pbstrName = SysAllocString(func->name);
1114     if(!*pbstrName)
1115         return E_OUTOFMEMORY;
1116     return S_OK;
1117 }
1118
1119 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1120 {
1121     DispatchEx *This = DISPATCHEX_THIS(iface);
1122     dispex_data_t *data;
1123     func_info_t *func;
1124     HRESULT hres;
1125
1126     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1127
1128     if(is_dynamic_dispid(id)) {
1129         DWORD idx = id - DISPID_DYNPROP_0;
1130
1131         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1132             return DISP_E_UNKNOWNNAME;
1133
1134         while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1135
1136         if(idx == This->dynamic_data->prop_cnt) {
1137             *pid = DISPID_STARTENUM;
1138             return S_FALSE;
1139         }
1140
1141         *pid = DISPID_DYNPROP_0+idx;
1142         return S_OK;
1143     }
1144
1145     data = get_dispex_data(This);
1146     if(!data)
1147         return E_FAIL;
1148
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++;
1156     }
1157
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;
1163         }
1164         func++;
1165     }
1166
1167     if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1168         *pid = DISPID_DYNPROP_0;
1169         return S_OK;
1170     }
1171
1172     *pid = DISPID_STARTENUM;
1173     return S_FALSE;
1174 }
1175
1176 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1177 {
1178     DispatchEx *This = DISPATCHEX_THIS(iface);
1179     FIXME("(%p)->(%p)\n", This, ppunk);
1180     return E_NOTIMPL;
1181 }
1182
1183 #undef DISPATCHEX_THIS
1184
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
1201 };
1202
1203 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1204 {
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}};
1209
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;
1224     }
1225
1226     if(*ppv)
1227         IUnknown_AddRef((IUnknown*)*ppv);
1228     return TRUE;
1229 }
1230
1231 void release_dispex(DispatchEx *This)
1232 {
1233     dynamic_prop_t *prop;
1234
1235     if(!This->dynamic_data)
1236         return;
1237
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);
1241     }
1242
1243     heap_free(This->dynamic_data->props);
1244
1245     if(This->dynamic_data->func_disps) {
1246         unsigned i;
1247
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]);
1252             }
1253         }
1254
1255         heap_free(This->dynamic_data->func_disps);
1256     }
1257
1258     heap_free(This->dynamic_data);
1259 }
1260
1261 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1262 {
1263     dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1264     dispex->outer = outer;
1265     dispex->data = data;
1266     dispex->dynamic_data = NULL;
1267 }