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