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