dsound: Added missing LeaveCriticalSection (Coverity).
[wine/multimedia.git] / dlls / mshtml / dispex.c
blob75b3f8795fac5ff3cfa5be3ad6ff18afb26dad28
1 /*
2 * Copyright 2008-2009 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
36 typedef struct {
37 DISPID id;
38 BSTR name;
39 tid_t tid;
40 int func_disp_idx;
41 } func_info_t;
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;
49 struct list entry;
52 typedef struct {
53 VARIANT var;
54 LPWSTR name;
55 DWORD flags;
56 } dynamic_prop_t;
58 #define DYNPROP_DELETED 0x01
60 typedef struct {
61 DispatchEx dispex;
62 IUnknown IUnknown_iface;
63 DispatchEx *obj;
64 func_info_t *info;
65 } func_disp_t;
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;
74 #define DISPID_DYNPROP_0 0x50000000
75 #define DISPID_DYNPROP_MAX 0x5fffffff
77 #define FDEX_VERSION_MASK 0xf0000000
79 static ITypeLib *typelib;
80 static ITypeInfo *typeinfos[LAST_tid];
81 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
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
91 static HRESULT load_typelib(void)
93 HRESULT hres;
94 ITypeLib *tl;
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;
102 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
103 ITypeLib_Release(tl);
104 return hres;
107 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
109 HRESULT hres;
111 if (!typelib)
112 hres = load_typelib();
113 if (!typelib)
114 return hres;
116 if(!typeinfos[tid]) {
117 ITypeInfo *ti;
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;
125 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
126 ITypeInfo_Release(ti);
129 *typeinfo = typeinfos[tid];
130 return S_OK;
133 void release_typelib(void)
135 dispex_data_t *iter;
136 unsigned i;
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);
142 for(i=0; i < iter->func_cnt; i++)
143 SysFreeString(iter->funcs[i].name);
145 heap_free(iter->funcs);
146 heap_free(iter->name_table);
147 heap_free(iter);
150 if(!typelib)
151 return;
153 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
154 if(typeinfos[i])
155 ITypeInfo_Release(typeinfos[i]);
157 ITypeLib_Release(typelib);
160 HRESULT get_htmldoc_classinfo(ITypeInfo **typeinfo)
162 HRESULT hres;
164 if (!typelib)
165 hres = load_typelib();
166 if (!typelib)
167 return hres;
169 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &CLSID_HTMLDocument, typeinfo);
170 if(FAILED(hres))
171 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
172 return hres;
175 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
177 HRESULT hres;
179 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
180 return;
182 if(data->func_cnt == *size)
183 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
185 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
186 if(FAILED(hres))
187 return;
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;
193 data->func_cnt++;
196 static int dispid_cmp(const void *p1, const void *p2)
198 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
201 static int func_name_cmp(const void *p1, const void *p2)
203 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
206 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
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;
215 TRACE("(%p)\n", This);
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;
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);
231 while(*tid) {
232 hres = get_typeinfo(*tid, &ti);
233 if(FAILED(hres))
234 break;
236 i=7;
237 while(1) {
238 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
239 if(FAILED(hres))
240 break;
242 add_func_info(data, &size, *tid, funcdesc, dti);
243 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
246 tid++;
249 if(!data->func_cnt) {
250 heap_free(data->funcs);
251 data->name_table = NULL;
252 data->funcs = NULL;
253 return data;
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);
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);
265 return data;
268 static int id_cmp(const void *p1, const void *p2)
270 return *(const DISPID*)p1 - *(const DISPID*)p2;
273 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
275 unsigned i, func_cnt;
276 FUNCDESC *funcdesc;
277 ITypeInfo *ti;
278 TYPEATTR *attr;
279 DISPID *ids;
280 HRESULT hres;
282 hres = get_typeinfo(tid, &ti);
283 if(FAILED(hres))
284 return hres;
286 hres = ITypeInfo_GetTypeAttr(ti, &attr);
287 if(FAILED(hres)) {
288 ITypeInfo_Release(ti);
289 return hres;
292 func_cnt = attr->cFuncs;
293 ITypeInfo_ReleaseTypeAttr(ti, attr);
295 ids = heap_alloc(func_cnt*sizeof(DISPID));
296 if(!ids) {
297 ITypeInfo_Release(ti);
298 return E_OUTOFMEMORY;
301 for(i=0; i < func_cnt; i++) {
302 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
303 if(FAILED(hres))
304 break;
306 ids[i] = funcdesc->memid;
307 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
310 ITypeInfo_Release(ti);
311 if(FAILED(hres)) {
312 heap_free(ids);
313 return hres;
316 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
318 *ret_size = func_cnt;
319 *ret = ids;
320 return S_OK;
323 static CRITICAL_SECTION cs_dispex_static_data;
324 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
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") }
330 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
333 static dispex_data_t *get_dispex_data(DispatchEx *This)
335 if(This->data->data)
336 return This->data->data;
338 EnterCriticalSection(&cs_dispex_static_data);
340 if(!This->data->data)
341 This->data->data = preprocess_dispex_data(This);
343 LeaveCriticalSection(&cs_dispex_static_data);
345 return This->data->data;
348 static inline BOOL is_custom_dispid(DISPID id)
350 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
353 static inline BOOL is_dynamic_dispid(DISPID id)
355 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
358 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
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;
366 return VariantCopy(dest, src);
369 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
371 if(This->dynamic_data)
372 return This->dynamic_data;
374 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
375 if(!This->dynamic_data)
376 return NULL;
378 if(This->data->vtbl && This->data->vtbl->populate_props)
379 This->data->vtbl->populate_props(This);
381 return This->dynamic_data;
384 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
386 const BOOL alloc = flags & fdexNameEnsure;
387 dispex_dynamic_data_t *data;
388 dynamic_prop_t *prop;
390 data = get_dynamic_data(This);
391 if(!data)
392 return E_OUTOFMEMORY;
394 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
395 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
396 if(prop->flags & DYNPROP_DELETED) {
397 if(!alloc)
398 return DISP_E_UNKNOWNNAME;
399 prop->flags &= ~DYNPROP_DELETED;
401 *ret = prop;
402 return S_OK;
406 if(!alloc)
407 return DISP_E_UNKNOWNNAME;
409 TRACE("creating dynamic prop %s\n", debugstr_w(name));
411 if(!data->buf_size) {
412 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
413 if(!data->props)
414 return E_OUTOFMEMORY;
415 data->buf_size = 4;
416 }else if(data->buf_size == data->prop_cnt) {
417 dynamic_prop_t *new_props;
419 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
420 if(!new_props)
421 return E_OUTOFMEMORY;
423 data->props = new_props;
424 data->buf_size <<= 1;
427 prop = data->props + data->prop_cnt;
429 prop->name = heap_strdupW(name);
430 if(!prop->name)
431 return E_OUTOFMEMORY;
433 VariantInit(&prop->var);
434 prop->flags = 0;
435 data->prop_cnt++;
436 *ret = prop;
437 return S_OK;
440 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
442 dynamic_prop_t *prop;
443 HRESULT hres;
445 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
446 if(FAILED(hres))
447 return hres;
449 *ret = &prop->var;
450 return S_OK;
453 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
454 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
456 if(This->data->vtbl && This->data->vtbl->value)
457 return This->data->vtbl->value(This, lcid, flags, params, res, ei, caller);
459 switch(flags) {
460 case DISPATCH_PROPERTYGET:
461 V_VT(res) = VT_BSTR;
462 V_BSTR(res) = SysAllocString(objectW);
463 if(!V_BSTR(res))
464 return E_OUTOFMEMORY;
465 break;
466 default:
467 FIXME("Unimplemented flags %x\n", flags);
468 return E_NOTIMPL;
471 return S_OK;
474 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
475 EXCEPINFO *ei)
477 ITypeInfo *ti;
478 IUnknown *unk;
479 UINT argerr=0;
480 HRESULT hres;
482 hres = get_typeinfo(func->tid, &ti);
483 if(FAILED(hres)) {
484 ERR("Could not get type info: %08x\n", hres);
485 return hres;
488 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
489 if(FAILED(hres)) {
490 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
491 return E_FAIL;
494 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
496 IUnknown_Release(unk);
497 return hres;
500 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
502 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
505 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
507 func_disp_t *This = impl_from_IUnknown(iface);
509 if(IsEqualGUID(&IID_IUnknown, riid)) {
510 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
511 *ppv = &This->IUnknown_iface;
512 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
513 return *ppv ? S_OK : E_NOINTERFACE;
514 }else {
515 *ppv = NULL;
516 return E_NOINTERFACE;
519 IUnknown_AddRef((IUnknown*)*ppv);
520 return S_OK;
523 static ULONG WINAPI Function_AddRef(IUnknown *iface)
525 func_disp_t *This = impl_from_IUnknown(iface);
527 TRACE("(%p)\n", This);
529 return IDispatchEx_AddRef(&This->obj->IDispatchEx_iface);
532 static ULONG WINAPI Function_Release(IUnknown *iface)
534 func_disp_t *This = impl_from_IUnknown(iface);
536 TRACE("(%p)\n", This);
538 return IDispatchEx_Release(&This->obj->IDispatchEx_iface);
541 static const IUnknownVtbl FunctionUnkVtbl = {
542 Function_QueryInterface,
543 Function_AddRef,
544 Function_Release
547 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
549 return CONTAINING_RECORD(iface, func_disp_t, dispex);
552 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
553 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
555 func_disp_t *This = impl_from_DispatchEx(dispex);
556 HRESULT hres;
558 switch(flags) {
559 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
560 if(!res)
561 return E_INVALIDARG;
562 /* fall through */
563 case DISPATCH_METHOD:
564 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
565 break;
566 default:
567 FIXME("Unimplemented flags %x\n", flags);
568 hres = E_NOTIMPL;
571 return hres;
574 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
575 function_value,
576 NULL,
577 NULL,
578 NULL
581 static const tid_t function_iface_tids[] = {0};
583 static dispex_static_data_t function_dispex = {
584 &function_dispex_vtbl,
585 NULL_tid,
586 NULL,
587 function_iface_tids
590 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
592 func_disp_t *ret;
594 ret = heap_alloc_zero(sizeof(func_disp_t));
595 if(!ret)
596 return NULL;
598 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
599 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
600 ret->obj = obj;
601 ret->info = info;
603 return ret;
606 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
607 EXCEPINFO *ei)
609 HRESULT hres;
611 switch(flags) {
612 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
613 if(!res)
614 return E_INVALIDARG;
615 /* fall through */
616 case DISPATCH_METHOD:
617 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
618 break;
619 case DISPATCH_PROPERTYGET: {
620 dispex_dynamic_data_t *dynamic_data;
622 if(func->id == DISPID_VALUE) {
623 BSTR ret;
625 ret = SysAllocString(objectW);
626 if(!ret)
627 return E_OUTOFMEMORY;
629 V_VT(res) = VT_BSTR;
630 V_BSTR(res) = ret;
631 return S_OK;
634 dynamic_data = get_dynamic_data(This);
635 if(!dynamic_data)
636 return E_OUTOFMEMORY;
638 if(!dynamic_data->func_disps) {
639 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
640 if(!dynamic_data->func_disps)
641 return E_OUTOFMEMORY;
644 if(!dynamic_data->func_disps[func->func_disp_idx]) {
645 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
646 if(!dynamic_data->func_disps[func->func_disp_idx])
647 return E_OUTOFMEMORY;
650 V_VT(res) = VT_DISPATCH;
651 V_DISPATCH(res) = (IDispatch*)&dynamic_data->func_disps[func->func_disp_idx]->dispex.IDispatchEx_iface;
652 IDispatch_AddRef(V_DISPATCH(res));
653 hres = S_OK;
654 break;
656 default:
657 FIXME("Unimplemented flags %x\n", flags);
658 /* fall through */
659 case DISPATCH_PROPERTYPUT:
660 hres = E_NOTIMPL;
663 return hres;
666 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
668 int min, max, n;
670 min = 0;
671 max = data->func_cnt-1;
673 while(min <= max) {
674 n = (min+max)/2;
676 if(data->funcs[n].id == id) {
677 *ret = data->funcs+n;
678 return S_OK;
681 if(data->funcs[n].id < id)
682 min = n+1;
683 else
684 max = n-1;
687 WARN("invalid id %x\n", id);
688 return DISP_E_UNKNOWNNAME;
691 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
693 dispex_data_t *data;
694 int min, max, n, c;
696 data = get_dispex_data(This);
697 if(!data)
698 return E_FAIL;
700 min = 0;
701 max = data->func_cnt-1;
703 while(min <= max) {
704 n = (min+max)/2;
706 c = strcmpiW(data->name_table[n]->name, name);
707 if(!c) {
708 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
709 break;
711 *ret = data->name_table[n]->id;
712 return S_OK;
715 if(c > 0)
716 max = n-1;
717 else
718 min = n+1;
721 if(This->data->vtbl && This->data->vtbl->get_dispid) {
722 HRESULT hres;
724 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
725 if(hres != DISP_E_UNKNOWNNAME)
726 return hres;
729 return DISP_E_UNKNOWNNAME;
732 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
733 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
735 dispex_data_t *data;
736 func_info_t *func;
737 HRESULT hres;
739 data = get_dispex_data(This);
740 if(!data)
741 return E_FAIL;
743 hres = get_builtin_func(data, id, &func);
744 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
745 return dispex_value(This, lcid, flags, dp, res, ei, caller);
746 if(FAILED(hres))
747 return hres;
749 if(func->func_disp_idx == -1)
750 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
751 else
752 hres = function_invoke(This, func, flags, dp, res, ei);
754 return hres;
757 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
759 dynamic_prop_t *prop;
760 DISPID id;
761 HRESULT hres;
763 hres = get_builtin_id(This, name, 0, &id);
764 if(hres == S_OK) {
765 DISPID named_id = DISPID_PROPERTYPUT;
766 VARIANT var;
767 DISPPARAMS dp = {&var,&named_id,1,1};
768 EXCEPINFO ei;
770 V_VT(&var) = VT_EMPTY;
771 memset(&ei, 0, sizeof(ei));
772 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
773 if(FAILED(hres))
774 return hres;
776 *success = VARIANT_TRUE;
777 return S_OK;
780 hres = get_dynamic_prop(This, name, 0, &prop);
781 if(FAILED(hres)) {
782 if(hres != DISP_E_UNKNOWNNAME)
783 return hres;
784 *success = VARIANT_FALSE;
785 return S_OK;
788 VariantClear(&prop->var);
789 prop->flags |= DYNPROP_DELETED;
790 *success = VARIANT_TRUE;
791 return S_OK;
794 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
796 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
799 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
801 DispatchEx *This = impl_from_IDispatchEx(iface);
803 return IUnknown_QueryInterface(This->outer, riid, ppv);
806 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
808 DispatchEx *This = impl_from_IDispatchEx(iface);
810 return IUnknown_AddRef(This->outer);
813 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
815 DispatchEx *This = impl_from_IDispatchEx(iface);
817 return IUnknown_Release(This->outer);
820 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
822 DispatchEx *This = impl_from_IDispatchEx(iface);
824 TRACE("(%p)->(%p)\n", This, pctinfo);
826 *pctinfo = 1;
827 return S_OK;
830 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
831 LCID lcid, ITypeInfo **ppTInfo)
833 DispatchEx *This = impl_from_IDispatchEx(iface);
834 HRESULT hres;
836 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
838 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
839 if(FAILED(hres))
840 return hres;
842 ITypeInfo_AddRef(*ppTInfo);
843 return S_OK;
846 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
847 LPOLESTR *rgszNames, UINT cNames,
848 LCID lcid, DISPID *rgDispId)
850 DispatchEx *This = impl_from_IDispatchEx(iface);
851 UINT i;
852 HRESULT hres;
854 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
855 lcid, rgDispId);
857 for(i=0; i < cNames; i++) {
858 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
859 if(FAILED(hres))
860 return hres;
863 return S_OK;
866 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
867 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
868 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
870 DispatchEx *This = impl_from_IDispatchEx(iface);
872 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
873 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
875 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
876 pVarResult, pExcepInfo, NULL);
879 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
881 DispatchEx *This = impl_from_IDispatchEx(iface);
882 dynamic_prop_t *dprop;
883 HRESULT hres;
885 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
887 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
888 FIXME("Unsupported grfdex %x\n", grfdex);
890 hres = get_builtin_id(This, bstrName, grfdex, pid);
891 if(hres != DISP_E_UNKNOWNNAME)
892 return hres;
894 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
895 if(FAILED(hres))
896 return hres;
898 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
899 return S_OK;
902 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
903 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
905 DispatchEx *This = impl_from_IDispatchEx(iface);
906 HRESULT hres;
908 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
910 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
911 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
913 if(wFlags == DISPATCH_CONSTRUCT) {
914 if(id == DISPID_VALUE) {
915 if(This->data->vtbl && This->data->vtbl->value) {
916 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
918 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
919 return E_FAIL;
921 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
922 return E_FAIL;
925 if(is_dynamic_dispid(id)) {
926 DWORD idx = id - DISPID_DYNPROP_0;
927 dynamic_prop_t *prop;
929 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
930 return DISP_E_UNKNOWNNAME;
932 prop = This->dynamic_data->props+idx;
934 switch(wFlags) {
935 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
936 if(!pvarRes)
937 return E_INVALIDARG;
938 /* fall through */
939 case DISPATCH_METHOD: {
940 DISPID named_arg = DISPID_THIS;
941 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
942 IDispatchEx *dispex;
944 if(V_VT(&prop->var) != VT_DISPATCH) {
945 FIXME("invoke %s\n", debugstr_variant(&prop->var));
946 return E_NOTIMPL;
949 if(pdp->cNamedArgs) {
950 FIXME("named args not supported\n");
951 return E_NOTIMPL;
954 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
955 if(!dp.rgvarg)
956 return E_OUTOFMEMORY;
958 dp.cArgs = pdp->cArgs+1;
959 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
961 V_VT(dp.rgvarg) = VT_DISPATCH;
962 V_DISPATCH(dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
964 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
965 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
966 if(SUCCEEDED(hres)) {
967 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
968 IDispatchEx_Release(dispex);
969 }else {
970 ULONG err = 0;
971 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
973 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
975 heap_free(dp.rgvarg);
976 return hres;
978 case DISPATCH_PROPERTYGET:
979 if(prop->flags & DYNPROP_DELETED)
980 return DISP_E_UNKNOWNNAME;
981 return variant_copy(pvarRes, &prop->var);
982 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
983 case DISPATCH_PROPERTYPUT:
984 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
985 || pdp->cNamedArgs > 1) {
986 FIXME("invalid args\n");
987 return E_INVALIDARG;
990 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
991 VariantClear(&prop->var);
992 hres = variant_copy(&prop->var, pdp->rgvarg);
993 if(FAILED(hres))
994 return hres;
996 prop->flags &= ~DYNPROP_DELETED;
997 return S_OK;
998 default:
999 FIXME("unhandled wFlags %x\n", wFlags);
1000 return E_NOTIMPL;
1004 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1007 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1009 DispatchEx *This = impl_from_IDispatchEx(iface);
1011 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1013 /* Not implemented by IE */
1014 return E_NOTIMPL;
1017 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1019 DispatchEx *This = impl_from_IDispatchEx(iface);
1021 TRACE("(%p)->(%x)\n", This, id);
1023 /* Not implemented by IE */
1024 return E_NOTIMPL;
1027 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1029 DispatchEx *This = impl_from_IDispatchEx(iface);
1030 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1031 return E_NOTIMPL;
1034 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1036 DispatchEx *This = impl_from_IDispatchEx(iface);
1037 dispex_data_t *data;
1038 func_info_t *func;
1039 HRESULT hres;
1041 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1043 if(is_dynamic_dispid(id)) {
1044 DWORD idx = id - DISPID_DYNPROP_0;
1046 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1047 return DISP_E_UNKNOWNNAME;
1049 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1050 if(!*pbstrName)
1051 return E_OUTOFMEMORY;
1053 return S_OK;
1056 data = get_dispex_data(This);
1057 if(!data)
1058 return E_FAIL;
1060 hres = get_builtin_func(data, id, &func);
1061 if(FAILED(hres))
1062 return hres;
1064 *pbstrName = SysAllocString(func->name);
1065 if(!*pbstrName)
1066 return E_OUTOFMEMORY;
1067 return S_OK;
1070 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1072 DispatchEx *This = impl_from_IDispatchEx(iface);
1073 dispex_data_t *data;
1074 func_info_t *func;
1075 HRESULT hres;
1077 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1079 if(is_dynamic_dispid(id)) {
1080 DWORD idx = id - DISPID_DYNPROP_0;
1082 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1083 return DISP_E_UNKNOWNNAME;
1085 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1087 if(idx == This->dynamic_data->prop_cnt) {
1088 *pid = DISPID_STARTENUM;
1089 return S_FALSE;
1092 *pid = DISPID_DYNPROP_0+idx;
1093 return S_OK;
1096 data = get_dispex_data(This);
1097 if(!data)
1098 return E_FAIL;
1100 if(id == DISPID_STARTENUM) {
1101 func = data->funcs;
1102 }else {
1103 hres = get_builtin_func(data, id, &func);
1104 if(FAILED(hres))
1105 return hres;
1106 func++;
1109 while(func < data->funcs+data->func_cnt) {
1110 /* FIXME: Skip hidden properties */
1111 if(func->func_disp_idx == -1) {
1112 *pid = func->id;
1113 return S_OK;
1115 func++;
1118 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1119 *pid = DISPID_DYNPROP_0;
1120 return S_OK;
1123 *pid = DISPID_STARTENUM;
1124 return S_FALSE;
1127 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1129 DispatchEx *This = impl_from_IDispatchEx(iface);
1130 FIXME("(%p)->(%p)\n", This, ppunk);
1131 return E_NOTIMPL;
1134 static IDispatchExVtbl DispatchExVtbl = {
1135 DispatchEx_QueryInterface,
1136 DispatchEx_AddRef,
1137 DispatchEx_Release,
1138 DispatchEx_GetTypeInfoCount,
1139 DispatchEx_GetTypeInfo,
1140 DispatchEx_GetIDsOfNames,
1141 DispatchEx_Invoke,
1142 DispatchEx_GetDispID,
1143 DispatchEx_InvokeEx,
1144 DispatchEx_DeleteMemberByName,
1145 DispatchEx_DeleteMemberByDispID,
1146 DispatchEx_GetMemberProperties,
1147 DispatchEx_GetMemberName,
1148 DispatchEx_GetNextDispID,
1149 DispatchEx_GetNameSpaceParent
1152 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1154 static const IID IID_UndocumentedScriptIface =
1155 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1156 static const IID IID_IDispatchJS =
1157 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1159 if(IsEqualGUID(&IID_IDispatch, riid)) {
1160 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1161 *ppv = &This->IDispatchEx_iface;
1162 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1163 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1164 *ppv = &This->IDispatchEx_iface;
1165 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1166 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1167 *ppv = NULL;
1168 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1169 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1170 *ppv = NULL;
1171 }else {
1172 return FALSE;
1175 if(*ppv)
1176 IUnknown_AddRef((IUnknown*)*ppv);
1177 return TRUE;
1180 void release_dispex(DispatchEx *This)
1182 dynamic_prop_t *prop;
1184 if(!This->dynamic_data)
1185 return;
1187 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1188 VariantClear(&prop->var);
1189 heap_free(prop->name);
1192 heap_free(This->dynamic_data->props);
1194 if(This->dynamic_data->func_disps) {
1195 unsigned i;
1197 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1198 if(This->dynamic_data->func_disps[i]) {
1199 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1200 heap_free(This->dynamic_data->func_disps[i]);
1204 heap_free(This->dynamic_data->func_disps);
1207 heap_free(This->dynamic_data);
1210 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1212 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1213 dispex->outer = outer;
1214 dispex->data = data;
1215 dispex->dynamic_data = NULL;