winmm: Use proper structure where possible to fix command string parsing on 64-bit.
[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_IHTMLRect,
150     &IID_IHTMLScreen,
151     &IID_IHTMLScriptElement,
152     &IID_IHTMLSelectElement,
153     &IID_IHTMLStyle,
154     &IID_IHTMLStyle2,
155     &IID_IHTMLStyle3,
156     &IID_IHTMLStyle4,
157     &IID_IHTMLTable,
158     &IID_IHTMLTableRow,
159     &IID_IHTMLTextAreaElement,
160     &IID_IHTMLTextContainer,
161     &IID_IHTMLUniqueName,
162     &IID_IHTMLWindow2,
163     &IID_IHTMLWindow3,
164     &IID_IHTMLWindow4,
165     &IID_IOmNavigator
166 };
167
168 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
169 {
170     HRESULT hres;
171
172     if(!typelib) {
173         ITypeLib *tl;
174
175         hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
176         if(FAILED(hres)) {
177             ERR("LoadRegTypeLib failed: %08x\n", hres);
178             return hres;
179         }
180
181         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
182             ITypeLib_Release(tl);
183     }
184
185     if(!typeinfos[tid]) {
186         ITypeInfo *typeinfo;
187
188         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
189         if(FAILED(hres)) {
190             ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
191             return hres;
192         }
193
194         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
195             ITypeInfo_Release(typeinfo);
196     }
197
198     *typeinfo = typeinfos[tid];
199     return S_OK;
200 }
201
202 void release_typelib(void)
203 {
204     dispex_data_t *iter;
205     unsigned i;
206
207     while(!list_empty(&dispex_data_list)) {
208         iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
209         list_remove(&iter->entry);
210
211         for(i=0; i < iter->func_cnt; i++)
212             SysFreeString(iter->funcs[i].name);
213
214         heap_free(iter->funcs);
215         heap_free(iter->name_table);
216         heap_free(iter);
217     }
218
219     if(!typelib)
220         return;
221
222     for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
223         if(typeinfos[i])
224             ITypeInfo_Release(typeinfos[i]);
225
226     ITypeLib_Release(typelib);
227 }
228
229 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
230 {
231     HRESULT hres;
232
233     if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
234         return;
235
236     if(data->func_cnt == *size)
237         data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
238
239     hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
240     if(FAILED(hres))
241         return;
242
243     data->funcs[data->func_cnt].id = desc->memid;
244     data->funcs[data->func_cnt].tid = tid;
245     data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
246
247     data->func_cnt++;
248 }
249
250 static int dispid_cmp(const void *p1, const void *p2)
251 {
252     return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
253 }
254
255 static int func_name_cmp(const void *p1, const void *p2)
256 {
257     return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
258 }
259
260 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
261 {
262     const tid_t *tid = This->data->iface_tids;
263     FUNCDESC *funcdesc;
264     dispex_data_t *data;
265     DWORD size = 16, i;
266     ITypeInfo *ti, *dti;
267     HRESULT hres;
268
269     TRACE("(%p)\n", This);
270
271     if(This->data->disp_tid) {
272         hres = get_typeinfo(This->data->disp_tid, &dti);
273         if(FAILED(hres)) {
274             ERR("Could not get disp type info: %08x\n", hres);
275             return NULL;
276         }
277     }
278
279     data = heap_alloc(sizeof(dispex_data_t));
280     data->func_cnt = 0;
281     data->func_disp_cnt = 0;
282     data->funcs = heap_alloc(size*sizeof(func_info_t));
283     list_add_tail(&dispex_data_list, &data->entry);
284
285     while(*tid) {
286         hres = get_typeinfo(*tid, &ti);
287         if(FAILED(hres))
288             break;
289
290         i=7;
291         while(1) {
292             hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
293             if(FAILED(hres))
294                 break;
295
296             add_func_info(data, &size, *tid, funcdesc, dti);
297             ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
298         }
299
300         tid++;
301     }
302
303     if(!data->func_cnt) {
304         heap_free(data->funcs);
305         data->funcs = NULL;
306     }else if(data->func_cnt != size) {
307         data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
308     }
309
310     qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
311
312     if(data->funcs) {
313         data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
314         for(i=0; i < data->func_cnt; i++)
315             data->name_table[i] = data->funcs+i;
316         qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
317     }else {
318         data->name_table = NULL;
319     }
320
321     return data;
322 }
323
324 static int id_cmp(const void *p1, const void *p2)
325 {
326     return *(const DISPID*)p1 - *(const DISPID*)p2;
327 }
328
329 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
330 {
331     unsigned i, func_cnt;
332     FUNCDESC *funcdesc;
333     ITypeInfo *ti;
334     TYPEATTR *attr;
335     DISPID *ids;
336     HRESULT hres;
337
338     hres = get_typeinfo(tid, &ti);
339     if(FAILED(hres))
340         return hres;
341
342     hres = ITypeInfo_GetTypeAttr(ti, &attr);
343     if(FAILED(hres)) {
344         ITypeInfo_Release(ti);
345         return hres;
346     }
347
348     func_cnt = attr->cFuncs;
349     ITypeInfo_ReleaseTypeAttr(ti, attr);
350
351     ids = heap_alloc(func_cnt*sizeof(DISPID));
352     if(!ids) {
353         ITypeInfo_Release(ti);
354         return E_OUTOFMEMORY;
355     }
356
357     for(i=0; i < func_cnt; i++) {
358         hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
359         if(FAILED(hres))
360             break;
361
362         ids[i] = funcdesc->memid;
363         ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
364     }
365
366     ITypeInfo_Release(ti);
367     if(FAILED(hres)) {
368         heap_free(ids);
369         return hres;
370     }
371
372     qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
373
374     *ret_size = func_cnt;
375     *ret = ids;
376     return S_OK;
377 }
378
379 static CRITICAL_SECTION cs_dispex_static_data;
380 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
381 {
382     0, 0, &cs_dispex_static_data,
383     { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
384       0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
385 };
386 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
387
388
389 static dispex_data_t *get_dispex_data(DispatchEx *This)
390 {
391     if(This->data->data)
392         return This->data->data;
393
394     EnterCriticalSection(&cs_dispex_static_data);
395
396     if(!This->data->data)
397         This->data->data = preprocess_dispex_data(This);
398
399     LeaveCriticalSection(&cs_dispex_static_data);
400
401     return This->data->data;
402 }
403
404 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
405 {
406     EXCEPINFO ei;
407     IDispatchEx *dispex;
408     VARIANT res;
409     HRESULT hres;
410
411     VariantInit(&res);
412     memset(&ei, 0, sizeof(ei));
413
414     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
415     if(SUCCEEDED(hres)) {
416         hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
417         IDispatchEx_Release(dispex);
418     }else {
419         TRACE("Could not get IDispatchEx interface: %08x\n", hres);
420         hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
421                 dp, &res, &ei, NULL);
422     }
423
424     VariantClear(&res);
425     return hres;
426 }
427
428 static inline BOOL is_custom_dispid(DISPID id)
429 {
430     return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
431 }
432
433 static inline BOOL is_dynamic_dispid(DISPID id)
434 {
435     return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
436 }
437
438 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
439 {
440     return !alloc || This->dynamic_data
441         ? This->dynamic_data
442         : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
443 }
444
445 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
446 {
447     const BOOL alloc = flags & fdexNameEnsure;
448     dispex_dynamic_data_t *data;
449     dynamic_prop_t *prop;
450
451     data = get_dynamic_data(This, alloc);
452     if(!data) {
453         if(alloc)
454             return E_OUTOFMEMORY;
455
456         TRACE("not found %s\n", debugstr_w(name));
457         return DISP_E_UNKNOWNNAME;
458     }
459
460     for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
461         if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
462             if(prop->flags & DYNPROP_DELETED) {
463                 if(!alloc)
464                     return DISP_E_UNKNOWNNAME;
465                 prop->flags &= ~DYNPROP_DELETED;
466             }
467             *ret = prop;
468             return S_OK;
469         }
470     }
471
472     if(!alloc)
473         return DISP_E_UNKNOWNNAME;
474
475     TRACE("creating dynamic prop %s\n", debugstr_w(name));
476
477     if(!data->buf_size) {
478         data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
479         if(!data->props)
480             return E_OUTOFMEMORY;
481         data->buf_size = 4;
482     }else if(data->buf_size == data->prop_cnt) {
483         dynamic_prop_t *new_props;
484
485         new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
486         if(!new_props)
487             return E_OUTOFMEMORY;
488
489         data->props = new_props;
490         data->buf_size <<= 1;
491     }
492
493     prop = data->props + data->prop_cnt;
494
495     prop->name = heap_strdupW(name);
496     if(!prop->name)
497         return E_OUTOFMEMORY;
498
499     VariantInit(&prop->var);
500     prop->flags = 0;
501     data->prop_cnt++;
502     *ret = prop;
503     return S_OK;
504 }
505
506 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
507 {
508     dynamic_prop_t *prop;
509     HRESULT hres;
510
511     hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
512     if(FAILED(hres))
513         return hres;
514
515     *ret = &prop->var;
516     return S_OK;
517 }
518
519 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
520         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
521 {
522     if(This->data->vtbl && This->data->vtbl->value)
523         return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
524
525     switch(flags) {
526     case DISPATCH_PROPERTYGET:
527         V_VT(res) = VT_BSTR;
528         V_BSTR(res) = SysAllocString(objectW);
529         if(!V_BSTR(res))
530             return E_OUTOFMEMORY;
531         break;
532     default:
533         FIXME("Unimplemented flags %x\n", flags);
534         return E_NOTIMPL;
535     }
536
537     return S_OK;
538 }
539
540 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
541         EXCEPINFO *ei)
542 {
543     ITypeInfo *ti;
544     IUnknown *unk;
545     UINT argerr=0;
546     HRESULT hres;
547
548     hres = get_typeinfo(func->tid, &ti);
549     if(FAILED(hres)) {
550         ERR("Could not get type info: %08x\n", hres);
551         return hres;
552     }
553
554     hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
555     if(FAILED(hres)) {
556         ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
557         return E_FAIL;
558     }
559
560     hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
561
562     IUnknown_Release(unk);
563     return hres;
564 }
565
566 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
567
568 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
569 {
570     func_disp_t *This = FUNCTION_THIS(iface);
571
572     if(IsEqualGUID(&IID_IUnknown, riid)) {
573         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
574         *ppv = FUNCUNKNOWN(This);
575     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
576         return *ppv ? S_OK : E_NOINTERFACE;
577     }else {
578         *ppv = NULL;
579         return E_NOINTERFACE;
580     }
581
582     IUnknown_AddRef((IUnknown*)*ppv);
583     return S_OK;
584 }
585
586 static ULONG WINAPI Function_AddRef(IUnknown *iface)
587 {
588     func_disp_t *This = FUNCTION_THIS(iface);
589
590     TRACE("(%p)\n", This);
591
592     return IDispatchEx_AddRef(DISPATCHEX(This->obj));
593 }
594
595 static ULONG WINAPI Function_Release(IUnknown *iface)
596 {
597     func_disp_t *This = FUNCTION_THIS(iface);
598
599     TRACE("(%p)\n", This);
600
601     return IDispatchEx_Release(DISPATCHEX(This->obj));
602 }
603
604 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
605         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
606 {
607     func_disp_t *This = FUNCTION_THIS(iface);
608     HRESULT hres;
609
610     switch(flags) {
611     case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
612         if(!res)
613             return E_INVALIDARG;
614     case DISPATCH_METHOD:
615         hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
616         break;
617     default:
618         FIXME("Unimplemented flags %x\n", flags);
619         hres = E_NOTIMPL;
620     }
621
622     return hres;
623 }
624
625 #undef FUNCTION_THIS
626
627 static const IUnknownVtbl FunctionUnkVtbl = {
628     Function_QueryInterface,
629     Function_AddRef,
630     Function_Release
631 };
632
633 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
634     function_value,
635     NULL,
636     NULL
637 };
638
639 static const tid_t function_iface_tids[] = {0};
640
641 static dispex_static_data_t function_dispex = {
642     &function_dispex_vtbl,
643     NULL_tid,
644     NULL,
645     function_iface_tids
646 };
647
648 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
649 {
650     func_disp_t *ret;
651
652     ret = heap_alloc_zero(sizeof(func_disp_t));
653     if(!ret)
654         return NULL;
655
656     ret->lpIUnknownVtbl = &FunctionUnkVtbl;
657     init_dispex(&ret->dispex, FUNCUNKNOWN(ret),  &function_dispex);
658     ret->obj = obj;
659     ret->info = info;
660
661     return ret;
662 }
663
664 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
665         EXCEPINFO *ei)
666 {
667     HRESULT hres;
668
669     switch(flags) {
670     case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
671         if(!res)
672             return E_INVALIDARG;
673     case DISPATCH_METHOD:
674         hres = typeinfo_invoke(This, func, flags, dp, res, ei);
675         break;
676     case DISPATCH_PROPERTYGET: {
677         dispex_dynamic_data_t *dynamic_data;
678
679         if(func->id == DISPID_VALUE) {
680             BSTR ret;
681
682             ret = SysAllocString(objectW);
683             if(!ret)
684                 return E_OUTOFMEMORY;
685
686             V_VT(res) = VT_BSTR;
687             V_BSTR(res) = ret;
688             return S_OK;
689         }
690
691         dynamic_data = get_dynamic_data(This, TRUE);
692         if(!dynamic_data)
693             return E_OUTOFMEMORY;
694
695         if(!dynamic_data->func_disps) {
696             dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
697             if(!dynamic_data->func_disps)
698                 return E_OUTOFMEMORY;
699         }
700
701         if(!dynamic_data->func_disps[func->func_disp_idx]) {
702             dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
703             if(!dynamic_data->func_disps[func->func_disp_idx])
704                 return E_OUTOFMEMORY;
705         }
706
707         V_VT(res) = VT_DISPATCH;
708         V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
709         IDispatch_AddRef(V_DISPATCH(res));
710         hres = S_OK;
711         break;
712     }
713     default:
714         FIXME("Unimplemented flags %x\n", flags);
715     case DISPATCH_PROPERTYPUT:
716         hres = E_NOTIMPL;
717     }
718
719     return hres;
720 }
721
722 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
723 {
724     int min, max, n;
725
726     min = 0;
727     max = data->func_cnt-1;
728
729     while(min <= max) {
730         n = (min+max)/2;
731
732         if(data->funcs[n].id == id) {
733             *ret = data->funcs+n;
734             return S_OK;
735         }
736
737         if(data->funcs[n].id < id)
738             min = n+1;
739         else
740             max = n-1;
741     }
742
743     WARN("invalid id %x\n", id);
744     return DISP_E_UNKNOWNNAME;
745 }
746
747 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
748 {
749     dispex_data_t *data;
750     int min, max, n, c;
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, name);
763         if(!c) {
764             if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
765                 break;
766
767             *ret = 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, name, grfdex, ret);
781         if(hres != DISP_E_UNKNOWNNAME)
782             return hres;
783     }
784
785     return DISP_E_UNKNOWNNAME;
786 }
787
788 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
789         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
790 {
791     dispex_data_t *data;
792     func_info_t *func;
793     HRESULT hres;
794
795     data = get_dispex_data(This);
796     if(!data)
797         return E_FAIL;
798
799     hres = get_builtin_func(data, id, &func);
800     if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
801         return dispex_value(This, lcid, flags, dp, res, ei, caller);
802     if(FAILED(hres))
803         return hres;
804
805     if(func->func_disp_idx == -1)
806         hres = typeinfo_invoke(This, func, flags, dp, res, ei);
807     else
808         hres = function_invoke(This, func, flags, dp, res, ei);
809
810     return hres;
811 }
812
813 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
814 {
815     dynamic_prop_t *prop;
816     DISPID id;
817     HRESULT hres;
818
819     hres = get_builtin_id(This, name, 0, &id);
820     if(hres == S_OK) {
821         DISPID named_id = DISPID_PROPERTYPUT;
822         VARIANT var;
823         DISPPARAMS dp = {&var,&named_id,1,1};
824         EXCEPINFO ei;
825
826         V_VT(&var) = VT_EMPTY;
827         memset(&ei, 0, sizeof(ei));
828         hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
829         if(FAILED(hres))
830             return hres;
831
832         *success = VARIANT_TRUE;
833         return S_OK;
834     }
835
836     hres = get_dynamic_prop(This, name, 0, &prop);
837     if(FAILED(hres)) {
838         if(hres != DISP_E_UNKNOWNNAME)
839             return hres;
840         *success = VARIANT_FALSE;
841         return S_OK;
842     }
843
844     VariantClear(&prop->var);
845     prop->flags |= DYNPROP_DELETED;
846     *success = VARIANT_TRUE;
847     return S_OK;
848 }
849
850 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
851
852 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
853 {
854     DispatchEx *This = DISPATCHEX_THIS(iface);
855
856     return IUnknown_QueryInterface(This->outer, riid, ppv);
857 }
858
859 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
860 {
861     DispatchEx *This = DISPATCHEX_THIS(iface);
862
863     return IUnknown_AddRef(This->outer);
864 }
865
866 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
867 {
868     DispatchEx *This = DISPATCHEX_THIS(iface);
869
870     return IUnknown_Release(This->outer);
871 }
872
873 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
874 {
875     DispatchEx *This = DISPATCHEX_THIS(iface);
876
877     TRACE("(%p)->(%p)\n", This, pctinfo);
878
879     *pctinfo = 1;
880     return S_OK;
881 }
882
883 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
884                                               LCID lcid, ITypeInfo **ppTInfo)
885 {
886     DispatchEx *This = DISPATCHEX_THIS(iface);
887     HRESULT hres;
888
889     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
890
891     hres = get_typeinfo(This->data->disp_tid, ppTInfo);
892     if(FAILED(hres))
893         return hres;
894
895     ITypeInfo_AddRef(*ppTInfo);
896     return S_OK;
897 }
898
899 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
900                                                 LPOLESTR *rgszNames, UINT cNames,
901                                                 LCID lcid, DISPID *rgDispId)
902 {
903     DispatchEx *This = DISPATCHEX_THIS(iface);
904     UINT i;
905     HRESULT hres;
906
907     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
908           lcid, rgDispId);
909
910     for(i=0; i < cNames; i++) {
911         hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
912         if(FAILED(hres))
913             return hres;
914     }
915
916     return S_OK;
917 }
918
919 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
920                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
921                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
922 {
923     DispatchEx *This = DISPATCHEX_THIS(iface);
924
925     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
926           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
927
928     return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
929                                 pDispParams, pVarResult, pExcepInfo, NULL);
930 }
931
932 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
933 {
934     DispatchEx *This = DISPATCHEX_THIS(iface);
935     dynamic_prop_t *dprop;
936     HRESULT hres;
937
938     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
939
940     if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
941         FIXME("Unsupported grfdex %x\n", grfdex);
942
943     hres = get_builtin_id(This, bstrName, grfdex, pid);
944     if(hres != DISP_E_UNKNOWNNAME)
945         return hres;
946
947     hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
948     if(FAILED(hres))
949         return hres;
950
951     *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
952     return S_OK;
953 }
954
955 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
956         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
957 {
958     DispatchEx *This = DISPATCHEX_THIS(iface);
959     HRESULT hres;
960
961     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
962
963     if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
964         return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
965
966     if(wFlags == DISPATCH_CONSTRUCT) {
967         if(id == DISPID_VALUE) {
968             if(This->data->vtbl && This->data->vtbl->value) {
969                 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
970                         pvarRes, pei, pspCaller);
971             }
972             FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
973             return E_FAIL;
974         }
975         FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
976         return E_FAIL;
977     }
978
979     if(is_dynamic_dispid(id)) {
980         DWORD idx = id - DISPID_DYNPROP_0;
981         dynamic_prop_t *prop;
982
983         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
984             return DISP_E_UNKNOWNNAME;
985
986         prop = This->dynamic_data->props+idx;
987
988         switch(wFlags) {
989         case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
990             if(!pvarRes)
991                 return E_INVALIDARG;
992         case DISPATCH_METHOD: {
993             DISPID named_arg = DISPID_THIS;
994             DISPPARAMS dp = {NULL, &named_arg, 0, 1};
995             IDispatchEx *dispex;
996
997             if(V_VT(&prop->var) != VT_DISPATCH) {
998                 FIXME("invoke %s\n", debugstr_variant(&prop->var));
999                 return E_NOTIMPL;
1000             }
1001
1002             if(pdp->cNamedArgs) {
1003                 FIXME("named args not supported\n");
1004                 return E_NOTIMPL;
1005             }
1006
1007             dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
1008             if(!dp.rgvarg)
1009                 return E_OUTOFMEMORY;
1010
1011             dp.cArgs = pdp->cArgs+1;
1012             memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
1013
1014             V_VT(dp.rgvarg) = VT_DISPATCH;
1015             V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
1016
1017             hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
1018             TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
1019             if(SUCCEEDED(hres)) {
1020                 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
1021                 IDispatchEx_Release(dispex);
1022             }else {
1023                 ULONG err = 0;
1024                 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
1025             }
1026             TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
1027
1028             heap_free(dp.rgvarg);
1029             return hres;
1030         }
1031         case DISPATCH_PROPERTYGET:
1032             if(prop->flags & DYNPROP_DELETED)
1033                 return DISP_E_UNKNOWNNAME;
1034             return VariantCopy(pvarRes, &prop->var);
1035         case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1036         case DISPATCH_PROPERTYPUT:
1037             if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1038                || pdp->cNamedArgs > 1) {
1039                 FIXME("invalid args\n");
1040                 return E_INVALIDARG;
1041             }
1042
1043             TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1044             VariantClear(&prop->var);
1045             hres = VariantCopy(&prop->var, pdp->rgvarg);
1046             if(FAILED(hres))
1047                 return hres;
1048
1049             prop->flags &= ~DYNPROP_DELETED;
1050             return S_OK;
1051         default:
1052             FIXME("unhandled wFlags %x\n", wFlags);
1053             return E_NOTIMPL;
1054         }
1055     }
1056
1057     return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1058 }
1059
1060 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1061 {
1062     DispatchEx *This = DISPATCHEX_THIS(iface);
1063
1064     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1065
1066     /* Not implemented by IE */
1067     return E_NOTIMPL;
1068 }
1069
1070 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1071 {
1072     DispatchEx *This = DISPATCHEX_THIS(iface);
1073     FIXME("(%p)->(%x)\n", This, id);
1074     return E_NOTIMPL;
1075 }
1076
1077 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1078 {
1079     DispatchEx *This = DISPATCHEX_THIS(iface);
1080     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1081     return E_NOTIMPL;
1082 }
1083
1084 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1085 {
1086     DispatchEx *This = DISPATCHEX_THIS(iface);
1087     dispex_data_t *data;
1088     func_info_t *func;
1089     HRESULT hres;
1090
1091     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1092
1093     if(is_dynamic_dispid(id)) {
1094         DWORD idx = id - DISPID_DYNPROP_0;
1095
1096         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1097             return DISP_E_UNKNOWNNAME;
1098
1099         *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1100         if(!*pbstrName)
1101             return E_OUTOFMEMORY;
1102
1103         return S_OK;
1104     }
1105
1106     data = get_dispex_data(This);
1107     if(!data)
1108         return E_FAIL;
1109
1110     hres = get_builtin_func(data, id, &func);
1111     if(FAILED(hres))
1112         return hres;
1113
1114     *pbstrName = SysAllocString(func->name);
1115     if(!*pbstrName)
1116         return E_OUTOFMEMORY;
1117     return S_OK;
1118 }
1119
1120 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1121 {
1122     DispatchEx *This = DISPATCHEX_THIS(iface);
1123     dispex_data_t *data;
1124     func_info_t *func;
1125     HRESULT hres;
1126
1127     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1128
1129     if(is_dynamic_dispid(id)) {
1130         DWORD idx = id - DISPID_DYNPROP_0;
1131
1132         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1133             return DISP_E_UNKNOWNNAME;
1134
1135         while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1136
1137         if(idx == This->dynamic_data->prop_cnt) {
1138             *pid = DISPID_STARTENUM;
1139             return S_FALSE;
1140         }
1141
1142         *pid = DISPID_DYNPROP_0+idx;
1143         return S_OK;
1144     }
1145
1146     data = get_dispex_data(This);
1147     if(!data)
1148         return E_FAIL;
1149
1150     if(id == DISPID_STARTENUM) {
1151         func = data->funcs;
1152     }else {
1153         hres = get_builtin_func(data, id, &func);
1154         if(FAILED(hres))
1155             return hres;
1156         func++;
1157     }
1158
1159     while(func < data->funcs+data->func_cnt) {
1160         /* FIXME: Skip hidden properties */
1161         if(func->func_disp_idx == -1) {
1162             *pid = func->id;
1163             return S_OK;
1164         }
1165         func++;
1166     }
1167
1168     if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1169         *pid = DISPID_DYNPROP_0;
1170         return S_OK;
1171     }
1172
1173     *pid = DISPID_STARTENUM;
1174     return S_FALSE;
1175 }
1176
1177 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1178 {
1179     DispatchEx *This = DISPATCHEX_THIS(iface);
1180     FIXME("(%p)->(%p)\n", This, ppunk);
1181     return E_NOTIMPL;
1182 }
1183
1184 #undef DISPATCHEX_THIS
1185
1186 static IDispatchExVtbl DispatchExVtbl = {
1187     DispatchEx_QueryInterface,
1188     DispatchEx_AddRef,
1189     DispatchEx_Release,
1190     DispatchEx_GetTypeInfoCount,
1191     DispatchEx_GetTypeInfo,
1192     DispatchEx_GetIDsOfNames,
1193     DispatchEx_Invoke,
1194     DispatchEx_GetDispID,
1195     DispatchEx_InvokeEx,
1196     DispatchEx_DeleteMemberByName,
1197     DispatchEx_DeleteMemberByDispID,
1198     DispatchEx_GetMemberProperties,
1199     DispatchEx_GetMemberName,
1200     DispatchEx_GetNextDispID,
1201     DispatchEx_GetNameSpaceParent
1202 };
1203
1204 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1205 {
1206     static const IID IID_UndocumentedScriptIface =
1207         {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1208     static const IID IID_IDispatchJS =
1209         {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1210
1211     if(IsEqualGUID(&IID_IDispatch, riid)) {
1212         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1213         *ppv = DISPATCHEX(This);
1214     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1215         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1216         *ppv = DISPATCHEX(This);
1217     }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1218         TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1219         *ppv = NULL;
1220     }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1221         TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1222         *ppv = NULL;
1223     }else {
1224         return FALSE;
1225     }
1226
1227     if(*ppv)
1228         IUnknown_AddRef((IUnknown*)*ppv);
1229     return TRUE;
1230 }
1231
1232 void release_dispex(DispatchEx *This)
1233 {
1234     dynamic_prop_t *prop;
1235
1236     if(!This->dynamic_data)
1237         return;
1238
1239     for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1240         VariantClear(&prop->var);
1241         heap_free(prop->name);
1242     }
1243
1244     heap_free(This->dynamic_data->props);
1245
1246     if(This->dynamic_data->func_disps) {
1247         unsigned i;
1248
1249         for(i=0; i < This->data->data->func_disp_cnt; i++) {
1250             if(This->dynamic_data->func_disps[i]) {
1251                 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1252                 heap_free(This->dynamic_data->func_disps[i]);
1253             }
1254         }
1255
1256         heap_free(This->dynamic_data->func_disps);
1257     }
1258
1259     heap_free(This->dynamic_data);
1260 }
1261
1262 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1263 {
1264     dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1265     dispex->outer = outer;
1266     dispex->data = data;
1267     dispex->dynamic_data = NULL;
1268 }