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