msxml3: Basic support for startElement()/endElement().
[wine/multimedia.git] / dlls / mshtml / dispex.c
blob327b5dbbc6573d8a01f713cb73ab721ff2605f72
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 get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
93 HRESULT hres;
95 if(!typelib) {
96 ITypeLib *tl;
98 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
99 if(FAILED(hres)) {
100 ERR("LoadRegTypeLib failed: %08x\n", hres);
101 return hres;
104 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
105 ITypeLib_Release(tl);
108 if(!typeinfos[tid]) {
109 ITypeInfo *ti;
111 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
112 if(FAILED(hres)) {
113 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
114 return hres;
117 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
118 ITypeInfo_Release(ti);
121 *typeinfo = typeinfos[tid];
122 return S_OK;
125 void release_typelib(void)
127 dispex_data_t *iter;
128 unsigned i;
130 while(!list_empty(&dispex_data_list)) {
131 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
132 list_remove(&iter->entry);
134 for(i=0; i < iter->func_cnt; i++)
135 SysFreeString(iter->funcs[i].name);
137 heap_free(iter->funcs);
138 heap_free(iter->name_table);
139 heap_free(iter);
142 if(!typelib)
143 return;
145 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
146 if(typeinfos[i])
147 ITypeInfo_Release(typeinfos[i]);
149 ITypeLib_Release(typelib);
152 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
154 HRESULT hres;
156 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
157 return;
159 if(data->func_cnt == *size)
160 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
162 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
163 if(FAILED(hres))
164 return;
166 data->funcs[data->func_cnt].id = desc->memid;
167 data->funcs[data->func_cnt].tid = tid;
168 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
170 data->func_cnt++;
173 static int dispid_cmp(const void *p1, const void *p2)
175 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
178 static int func_name_cmp(const void *p1, const void *p2)
180 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
183 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
185 const tid_t *tid = This->data->iface_tids;
186 FUNCDESC *funcdesc;
187 dispex_data_t *data;
188 DWORD size = 16, i;
189 ITypeInfo *ti, *dti;
190 HRESULT hres;
192 TRACE("(%p)\n", This);
194 if(This->data->disp_tid) {
195 hres = get_typeinfo(This->data->disp_tid, &dti);
196 if(FAILED(hres)) {
197 ERR("Could not get disp type info: %08x\n", hres);
198 return NULL;
202 data = heap_alloc(sizeof(dispex_data_t));
203 data->func_cnt = 0;
204 data->func_disp_cnt = 0;
205 data->funcs = heap_alloc(size*sizeof(func_info_t));
206 list_add_tail(&dispex_data_list, &data->entry);
208 while(*tid) {
209 hres = get_typeinfo(*tid, &ti);
210 if(FAILED(hres))
211 break;
213 i=7;
214 while(1) {
215 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
216 if(FAILED(hres))
217 break;
219 add_func_info(data, &size, *tid, funcdesc, dti);
220 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
223 tid++;
226 if(!data->func_cnt) {
227 heap_free(data->funcs);
228 data->funcs = NULL;
229 }else if(data->func_cnt != size) {
230 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
233 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
235 if(data->funcs) {
236 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
237 for(i=0; i < data->func_cnt; i++)
238 data->name_table[i] = data->funcs+i;
239 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
240 }else {
241 data->name_table = NULL;
244 return data;
247 static int id_cmp(const void *p1, const void *p2)
249 return *(const DISPID*)p1 - *(const DISPID*)p2;
252 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
254 unsigned i, func_cnt;
255 FUNCDESC *funcdesc;
256 ITypeInfo *ti;
257 TYPEATTR *attr;
258 DISPID *ids;
259 HRESULT hres;
261 hres = get_typeinfo(tid, &ti);
262 if(FAILED(hres))
263 return hres;
265 hres = ITypeInfo_GetTypeAttr(ti, &attr);
266 if(FAILED(hres)) {
267 ITypeInfo_Release(ti);
268 return hres;
271 func_cnt = attr->cFuncs;
272 ITypeInfo_ReleaseTypeAttr(ti, attr);
274 ids = heap_alloc(func_cnt*sizeof(DISPID));
275 if(!ids) {
276 ITypeInfo_Release(ti);
277 return E_OUTOFMEMORY;
280 for(i=0; i < func_cnt; i++) {
281 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
282 if(FAILED(hres))
283 break;
285 ids[i] = funcdesc->memid;
286 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
289 ITypeInfo_Release(ti);
290 if(FAILED(hres)) {
291 heap_free(ids);
292 return hres;
295 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
297 *ret_size = func_cnt;
298 *ret = ids;
299 return S_OK;
302 static CRITICAL_SECTION cs_dispex_static_data;
303 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
305 0, 0, &cs_dispex_static_data,
306 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
307 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
309 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
312 static dispex_data_t *get_dispex_data(DispatchEx *This)
314 if(This->data->data)
315 return This->data->data;
317 EnterCriticalSection(&cs_dispex_static_data);
319 if(!This->data->data)
320 This->data->data = preprocess_dispex_data(This);
322 LeaveCriticalSection(&cs_dispex_static_data);
324 return This->data->data;
327 static inline BOOL is_custom_dispid(DISPID id)
329 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
332 static inline BOOL is_dynamic_dispid(DISPID id)
334 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
337 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
339 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
340 V_VT(dest) = VT_BSTR;
341 V_BSTR(dest) = NULL;
342 return S_OK;
345 return VariantCopy(dest, src);
348 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
350 return !alloc || This->dynamic_data
351 ? This->dynamic_data
352 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
355 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
357 const BOOL alloc = flags & fdexNameEnsure;
358 dispex_dynamic_data_t *data;
359 dynamic_prop_t *prop;
361 data = get_dynamic_data(This, alloc);
362 if(!data) {
363 if(alloc)
364 return E_OUTOFMEMORY;
366 TRACE("not found %s\n", debugstr_w(name));
367 return DISP_E_UNKNOWNNAME;
370 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
371 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
372 if(prop->flags & DYNPROP_DELETED) {
373 if(!alloc)
374 return DISP_E_UNKNOWNNAME;
375 prop->flags &= ~DYNPROP_DELETED;
377 *ret = prop;
378 return S_OK;
382 if(!alloc)
383 return DISP_E_UNKNOWNNAME;
385 TRACE("creating dynamic prop %s\n", debugstr_w(name));
387 if(!data->buf_size) {
388 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
389 if(!data->props)
390 return E_OUTOFMEMORY;
391 data->buf_size = 4;
392 }else if(data->buf_size == data->prop_cnt) {
393 dynamic_prop_t *new_props;
395 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
396 if(!new_props)
397 return E_OUTOFMEMORY;
399 data->props = new_props;
400 data->buf_size <<= 1;
403 prop = data->props + data->prop_cnt;
405 prop->name = heap_strdupW(name);
406 if(!prop->name)
407 return E_OUTOFMEMORY;
409 VariantInit(&prop->var);
410 prop->flags = 0;
411 data->prop_cnt++;
412 *ret = prop;
413 return S_OK;
416 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
418 dynamic_prop_t *prop;
419 HRESULT hres;
421 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
422 if(FAILED(hres))
423 return hres;
425 *ret = &prop->var;
426 return S_OK;
429 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
430 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
432 if(This->data->vtbl && This->data->vtbl->value)
433 return This->data->vtbl->value(This, lcid, flags, params, res, ei, caller);
435 switch(flags) {
436 case DISPATCH_PROPERTYGET:
437 V_VT(res) = VT_BSTR;
438 V_BSTR(res) = SysAllocString(objectW);
439 if(!V_BSTR(res))
440 return E_OUTOFMEMORY;
441 break;
442 default:
443 FIXME("Unimplemented flags %x\n", flags);
444 return E_NOTIMPL;
447 return S_OK;
450 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
451 EXCEPINFO *ei)
453 ITypeInfo *ti;
454 IUnknown *unk;
455 UINT argerr=0;
456 HRESULT hres;
458 hres = get_typeinfo(func->tid, &ti);
459 if(FAILED(hres)) {
460 ERR("Could not get type info: %08x\n", hres);
461 return hres;
464 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
465 if(FAILED(hres)) {
466 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
467 return E_FAIL;
470 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
472 IUnknown_Release(unk);
473 return hres;
476 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
478 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
481 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
483 func_disp_t *This = impl_from_IUnknown(iface);
485 if(IsEqualGUID(&IID_IUnknown, riid)) {
486 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
487 *ppv = &This->IUnknown_iface;
488 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
489 return *ppv ? S_OK : E_NOINTERFACE;
490 }else {
491 *ppv = NULL;
492 return E_NOINTERFACE;
495 IUnknown_AddRef((IUnknown*)*ppv);
496 return S_OK;
499 static ULONG WINAPI Function_AddRef(IUnknown *iface)
501 func_disp_t *This = impl_from_IUnknown(iface);
503 TRACE("(%p)\n", This);
505 return IDispatchEx_AddRef(&This->obj->IDispatchEx_iface);
508 static ULONG WINAPI Function_Release(IUnknown *iface)
510 func_disp_t *This = impl_from_IUnknown(iface);
512 TRACE("(%p)\n", This);
514 return IDispatchEx_Release(&This->obj->IDispatchEx_iface);
517 static const IUnknownVtbl FunctionUnkVtbl = {
518 Function_QueryInterface,
519 Function_AddRef,
520 Function_Release
523 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
525 return CONTAINING_RECORD(iface, func_disp_t, dispex);
528 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
529 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
531 func_disp_t *This = impl_from_DispatchEx(dispex);
532 HRESULT hres;
534 switch(flags) {
535 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
536 if(!res)
537 return E_INVALIDARG;
538 case DISPATCH_METHOD:
539 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
540 break;
541 default:
542 FIXME("Unimplemented flags %x\n", flags);
543 hres = E_NOTIMPL;
546 return hres;
549 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
550 function_value,
551 NULL,
552 NULL
555 static const tid_t function_iface_tids[] = {0};
557 static dispex_static_data_t function_dispex = {
558 &function_dispex_vtbl,
559 NULL_tid,
560 NULL,
561 function_iface_tids
564 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
566 func_disp_t *ret;
568 ret = heap_alloc_zero(sizeof(func_disp_t));
569 if(!ret)
570 return NULL;
572 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
573 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
574 ret->obj = obj;
575 ret->info = info;
577 return ret;
580 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
581 EXCEPINFO *ei)
583 HRESULT hres;
585 switch(flags) {
586 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
587 if(!res)
588 return E_INVALIDARG;
589 case DISPATCH_METHOD:
590 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
591 break;
592 case DISPATCH_PROPERTYGET: {
593 dispex_dynamic_data_t *dynamic_data;
595 if(func->id == DISPID_VALUE) {
596 BSTR ret;
598 ret = SysAllocString(objectW);
599 if(!ret)
600 return E_OUTOFMEMORY;
602 V_VT(res) = VT_BSTR;
603 V_BSTR(res) = ret;
604 return S_OK;
607 dynamic_data = get_dynamic_data(This, TRUE);
608 if(!dynamic_data)
609 return E_OUTOFMEMORY;
611 if(!dynamic_data->func_disps) {
612 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
613 if(!dynamic_data->func_disps)
614 return E_OUTOFMEMORY;
617 if(!dynamic_data->func_disps[func->func_disp_idx]) {
618 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
619 if(!dynamic_data->func_disps[func->func_disp_idx])
620 return E_OUTOFMEMORY;
623 V_VT(res) = VT_DISPATCH;
624 V_DISPATCH(res) = (IDispatch*)&dynamic_data->func_disps[func->func_disp_idx]->dispex.IDispatchEx_iface;
625 IDispatch_AddRef(V_DISPATCH(res));
626 hres = S_OK;
627 break;
629 default:
630 FIXME("Unimplemented flags %x\n", flags);
631 case DISPATCH_PROPERTYPUT:
632 hres = E_NOTIMPL;
635 return hres;
638 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
640 int min, max, n;
642 min = 0;
643 max = data->func_cnt-1;
645 while(min <= max) {
646 n = (min+max)/2;
648 if(data->funcs[n].id == id) {
649 *ret = data->funcs+n;
650 return S_OK;
653 if(data->funcs[n].id < id)
654 min = n+1;
655 else
656 max = n-1;
659 WARN("invalid id %x\n", id);
660 return DISP_E_UNKNOWNNAME;
663 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
665 dispex_data_t *data;
666 int min, max, n, c;
668 data = get_dispex_data(This);
669 if(!data)
670 return E_FAIL;
672 min = 0;
673 max = data->func_cnt-1;
675 while(min <= max) {
676 n = (min+max)/2;
678 c = strcmpiW(data->name_table[n]->name, name);
679 if(!c) {
680 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
681 break;
683 *ret = data->name_table[n]->id;
684 return S_OK;
687 if(c > 0)
688 max = n-1;
689 else
690 min = n+1;
693 if(This->data->vtbl && This->data->vtbl->get_dispid) {
694 HRESULT hres;
696 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
697 if(hres != DISP_E_UNKNOWNNAME)
698 return hres;
701 return DISP_E_UNKNOWNNAME;
704 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
705 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
707 dispex_data_t *data;
708 func_info_t *func;
709 HRESULT hres;
711 data = get_dispex_data(This);
712 if(!data)
713 return E_FAIL;
715 hres = get_builtin_func(data, id, &func);
716 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
717 return dispex_value(This, lcid, flags, dp, res, ei, caller);
718 if(FAILED(hres))
719 return hres;
721 if(func->func_disp_idx == -1)
722 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
723 else
724 hres = function_invoke(This, func, flags, dp, res, ei);
726 return hres;
729 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
731 dynamic_prop_t *prop;
732 DISPID id;
733 HRESULT hres;
735 hres = get_builtin_id(This, name, 0, &id);
736 if(hres == S_OK) {
737 DISPID named_id = DISPID_PROPERTYPUT;
738 VARIANT var;
739 DISPPARAMS dp = {&var,&named_id,1,1};
740 EXCEPINFO ei;
742 V_VT(&var) = VT_EMPTY;
743 memset(&ei, 0, sizeof(ei));
744 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
745 if(FAILED(hres))
746 return hres;
748 *success = VARIANT_TRUE;
749 return S_OK;
752 hres = get_dynamic_prop(This, name, 0, &prop);
753 if(FAILED(hres)) {
754 if(hres != DISP_E_UNKNOWNNAME)
755 return hres;
756 *success = VARIANT_FALSE;
757 return S_OK;
760 VariantClear(&prop->var);
761 prop->flags |= DYNPROP_DELETED;
762 *success = VARIANT_TRUE;
763 return S_OK;
766 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
768 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
771 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
773 DispatchEx *This = impl_from_IDispatchEx(iface);
775 return IUnknown_QueryInterface(This->outer, riid, ppv);
778 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
780 DispatchEx *This = impl_from_IDispatchEx(iface);
782 return IUnknown_AddRef(This->outer);
785 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
787 DispatchEx *This = impl_from_IDispatchEx(iface);
789 return IUnknown_Release(This->outer);
792 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
794 DispatchEx *This = impl_from_IDispatchEx(iface);
796 TRACE("(%p)->(%p)\n", This, pctinfo);
798 *pctinfo = 1;
799 return S_OK;
802 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
803 LCID lcid, ITypeInfo **ppTInfo)
805 DispatchEx *This = impl_from_IDispatchEx(iface);
806 HRESULT hres;
808 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
810 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
811 if(FAILED(hres))
812 return hres;
814 ITypeInfo_AddRef(*ppTInfo);
815 return S_OK;
818 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
819 LPOLESTR *rgszNames, UINT cNames,
820 LCID lcid, DISPID *rgDispId)
822 DispatchEx *This = impl_from_IDispatchEx(iface);
823 UINT i;
824 HRESULT hres;
826 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
827 lcid, rgDispId);
829 for(i=0; i < cNames; i++) {
830 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
831 if(FAILED(hres))
832 return hres;
835 return S_OK;
838 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
839 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
840 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
842 DispatchEx *This = impl_from_IDispatchEx(iface);
844 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
845 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
847 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
848 pVarResult, pExcepInfo, NULL);
851 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
853 DispatchEx *This = impl_from_IDispatchEx(iface);
854 dynamic_prop_t *dprop;
855 HRESULT hres;
857 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
859 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
860 FIXME("Unsupported grfdex %x\n", grfdex);
862 hres = get_builtin_id(This, bstrName, grfdex, pid);
863 if(hres != DISP_E_UNKNOWNNAME)
864 return hres;
866 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
867 if(FAILED(hres))
868 return hres;
870 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
871 return S_OK;
874 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
875 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
877 DispatchEx *This = impl_from_IDispatchEx(iface);
878 HRESULT hres;
880 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
882 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
883 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
885 if(wFlags == DISPATCH_CONSTRUCT) {
886 if(id == DISPID_VALUE) {
887 if(This->data->vtbl && This->data->vtbl->value) {
888 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
890 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
891 return E_FAIL;
893 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
894 return E_FAIL;
897 if(is_dynamic_dispid(id)) {
898 DWORD idx = id - DISPID_DYNPROP_0;
899 dynamic_prop_t *prop;
901 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
902 return DISP_E_UNKNOWNNAME;
904 prop = This->dynamic_data->props+idx;
906 switch(wFlags) {
907 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
908 if(!pvarRes)
909 return E_INVALIDARG;
910 case DISPATCH_METHOD: {
911 DISPID named_arg = DISPID_THIS;
912 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
913 IDispatchEx *dispex;
915 if(V_VT(&prop->var) != VT_DISPATCH) {
916 FIXME("invoke %s\n", debugstr_variant(&prop->var));
917 return E_NOTIMPL;
920 if(pdp->cNamedArgs) {
921 FIXME("named args not supported\n");
922 return E_NOTIMPL;
925 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
926 if(!dp.rgvarg)
927 return E_OUTOFMEMORY;
929 dp.cArgs = pdp->cArgs+1;
930 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
932 V_VT(dp.rgvarg) = VT_DISPATCH;
933 V_DISPATCH(dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
935 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
936 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
937 if(SUCCEEDED(hres)) {
938 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
939 IDispatchEx_Release(dispex);
940 }else {
941 ULONG err = 0;
942 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
944 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
946 heap_free(dp.rgvarg);
947 return hres;
949 case DISPATCH_PROPERTYGET:
950 if(prop->flags & DYNPROP_DELETED)
951 return DISP_E_UNKNOWNNAME;
952 return variant_copy(pvarRes, &prop->var);
953 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
954 case DISPATCH_PROPERTYPUT:
955 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
956 || pdp->cNamedArgs > 1) {
957 FIXME("invalid args\n");
958 return E_INVALIDARG;
961 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
962 VariantClear(&prop->var);
963 hres = variant_copy(&prop->var, pdp->rgvarg);
964 if(FAILED(hres))
965 return hres;
967 prop->flags &= ~DYNPROP_DELETED;
968 return S_OK;
969 default:
970 FIXME("unhandled wFlags %x\n", wFlags);
971 return E_NOTIMPL;
975 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
978 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
980 DispatchEx *This = impl_from_IDispatchEx(iface);
982 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
984 /* Not implemented by IE */
985 return E_NOTIMPL;
988 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
990 DispatchEx *This = impl_from_IDispatchEx(iface);
991 FIXME("(%p)->(%x)\n", This, id);
992 return E_NOTIMPL;
995 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
997 DispatchEx *This = impl_from_IDispatchEx(iface);
998 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
999 return E_NOTIMPL;
1002 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1004 DispatchEx *This = impl_from_IDispatchEx(iface);
1005 dispex_data_t *data;
1006 func_info_t *func;
1007 HRESULT hres;
1009 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1011 if(is_dynamic_dispid(id)) {
1012 DWORD idx = id - DISPID_DYNPROP_0;
1014 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1015 return DISP_E_UNKNOWNNAME;
1017 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1018 if(!*pbstrName)
1019 return E_OUTOFMEMORY;
1021 return S_OK;
1024 data = get_dispex_data(This);
1025 if(!data)
1026 return E_FAIL;
1028 hres = get_builtin_func(data, id, &func);
1029 if(FAILED(hres))
1030 return hres;
1032 *pbstrName = SysAllocString(func->name);
1033 if(!*pbstrName)
1034 return E_OUTOFMEMORY;
1035 return S_OK;
1038 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1040 DispatchEx *This = impl_from_IDispatchEx(iface);
1041 dispex_data_t *data;
1042 func_info_t *func;
1043 HRESULT hres;
1045 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1047 if(is_dynamic_dispid(id)) {
1048 DWORD idx = id - DISPID_DYNPROP_0;
1050 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
1051 return DISP_E_UNKNOWNNAME;
1053 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1055 if(idx == This->dynamic_data->prop_cnt) {
1056 *pid = DISPID_STARTENUM;
1057 return S_FALSE;
1060 *pid = DISPID_DYNPROP_0+idx;
1061 return S_OK;
1064 data = get_dispex_data(This);
1065 if(!data)
1066 return E_FAIL;
1068 if(id == DISPID_STARTENUM) {
1069 func = data->funcs;
1070 }else {
1071 hres = get_builtin_func(data, id, &func);
1072 if(FAILED(hres))
1073 return hres;
1074 func++;
1077 while(func < data->funcs+data->func_cnt) {
1078 /* FIXME: Skip hidden properties */
1079 if(func->func_disp_idx == -1) {
1080 *pid = func->id;
1081 return S_OK;
1083 func++;
1086 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1087 *pid = DISPID_DYNPROP_0;
1088 return S_OK;
1091 *pid = DISPID_STARTENUM;
1092 return S_FALSE;
1095 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1097 DispatchEx *This = impl_from_IDispatchEx(iface);
1098 FIXME("(%p)->(%p)\n", This, ppunk);
1099 return E_NOTIMPL;
1102 static IDispatchExVtbl DispatchExVtbl = {
1103 DispatchEx_QueryInterface,
1104 DispatchEx_AddRef,
1105 DispatchEx_Release,
1106 DispatchEx_GetTypeInfoCount,
1107 DispatchEx_GetTypeInfo,
1108 DispatchEx_GetIDsOfNames,
1109 DispatchEx_Invoke,
1110 DispatchEx_GetDispID,
1111 DispatchEx_InvokeEx,
1112 DispatchEx_DeleteMemberByName,
1113 DispatchEx_DeleteMemberByDispID,
1114 DispatchEx_GetMemberProperties,
1115 DispatchEx_GetMemberName,
1116 DispatchEx_GetNextDispID,
1117 DispatchEx_GetNameSpaceParent
1120 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1122 static const IID IID_UndocumentedScriptIface =
1123 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1124 static const IID IID_IDispatchJS =
1125 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1127 if(IsEqualGUID(&IID_IDispatch, riid)) {
1128 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1129 *ppv = &This->IDispatchEx_iface;
1130 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1131 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1132 *ppv = &This->IDispatchEx_iface;
1133 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1134 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1135 *ppv = NULL;
1136 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1137 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1138 *ppv = NULL;
1139 }else {
1140 return FALSE;
1143 if(*ppv)
1144 IUnknown_AddRef((IUnknown*)*ppv);
1145 return TRUE;
1148 void release_dispex(DispatchEx *This)
1150 dynamic_prop_t *prop;
1152 if(!This->dynamic_data)
1153 return;
1155 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1156 VariantClear(&prop->var);
1157 heap_free(prop->name);
1160 heap_free(This->dynamic_data->props);
1162 if(This->dynamic_data->func_disps) {
1163 unsigned i;
1165 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1166 if(This->dynamic_data->func_disps[i]) {
1167 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1168 heap_free(This->dynamic_data->func_disps[i]);
1172 heap_free(This->dynamic_data->func_disps);
1175 heap_free(This->dynamic_data);
1178 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1180 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1181 dispex->outer = outer;
1182 dispex->data = data;
1183 dispex->dynamic_data = NULL;