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