ddraw: Use unsafe_impl_from_IDirect3DViewport for application provided interfaces.
[wine/multimedia.git] / dlls / mshtml / dispex.c
blob44d09117125023a6c547965f09fe7b6578a6129d
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 case DISPATCH_METHOD:
563 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
564 break;
565 default:
566 FIXME("Unimplemented flags %x\n", flags);
567 hres = E_NOTIMPL;
570 return hres;
573 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
574 function_value,
575 NULL,
576 NULL,
577 NULL
580 static const tid_t function_iface_tids[] = {0};
582 static dispex_static_data_t function_dispex = {
583 &function_dispex_vtbl,
584 NULL_tid,
585 NULL,
586 function_iface_tids
589 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
591 func_disp_t *ret;
593 ret = heap_alloc_zero(sizeof(func_disp_t));
594 if(!ret)
595 return NULL;
597 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
598 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
599 ret->obj = obj;
600 ret->info = info;
602 return ret;
605 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
606 EXCEPINFO *ei)
608 HRESULT hres;
610 switch(flags) {
611 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
612 if(!res)
613 return E_INVALIDARG;
614 case DISPATCH_METHOD:
615 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
616 break;
617 case DISPATCH_PROPERTYGET: {
618 dispex_dynamic_data_t *dynamic_data;
620 if(func->id == DISPID_VALUE) {
621 BSTR ret;
623 ret = SysAllocString(objectW);
624 if(!ret)
625 return E_OUTOFMEMORY;
627 V_VT(res) = VT_BSTR;
628 V_BSTR(res) = ret;
629 return S_OK;
632 dynamic_data = get_dynamic_data(This);
633 if(!dynamic_data)
634 return E_OUTOFMEMORY;
636 if(!dynamic_data->func_disps) {
637 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
638 if(!dynamic_data->func_disps)
639 return E_OUTOFMEMORY;
642 if(!dynamic_data->func_disps[func->func_disp_idx]) {
643 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
644 if(!dynamic_data->func_disps[func->func_disp_idx])
645 return E_OUTOFMEMORY;
648 V_VT(res) = VT_DISPATCH;
649 V_DISPATCH(res) = (IDispatch*)&dynamic_data->func_disps[func->func_disp_idx]->dispex.IDispatchEx_iface;
650 IDispatch_AddRef(V_DISPATCH(res));
651 hres = S_OK;
652 break;
654 default:
655 FIXME("Unimplemented flags %x\n", flags);
656 case DISPATCH_PROPERTYPUT:
657 hres = E_NOTIMPL;
660 return hres;
663 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
665 int min, max, n;
667 min = 0;
668 max = data->func_cnt-1;
670 while(min <= max) {
671 n = (min+max)/2;
673 if(data->funcs[n].id == id) {
674 *ret = data->funcs+n;
675 return S_OK;
678 if(data->funcs[n].id < id)
679 min = n+1;
680 else
681 max = n-1;
684 WARN("invalid id %x\n", id);
685 return DISP_E_UNKNOWNNAME;
688 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
690 dispex_data_t *data;
691 int min, max, n, c;
693 data = get_dispex_data(This);
694 if(!data)
695 return E_FAIL;
697 min = 0;
698 max = data->func_cnt-1;
700 while(min <= max) {
701 n = (min+max)/2;
703 c = strcmpiW(data->name_table[n]->name, name);
704 if(!c) {
705 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
706 break;
708 *ret = data->name_table[n]->id;
709 return S_OK;
712 if(c > 0)
713 max = n-1;
714 else
715 min = n+1;
718 if(This->data->vtbl && This->data->vtbl->get_dispid) {
719 HRESULT hres;
721 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
722 if(hres != DISP_E_UNKNOWNNAME)
723 return hres;
726 return DISP_E_UNKNOWNNAME;
729 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
730 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
732 dispex_data_t *data;
733 func_info_t *func;
734 HRESULT hres;
736 data = get_dispex_data(This);
737 if(!data)
738 return E_FAIL;
740 hres = get_builtin_func(data, id, &func);
741 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
742 return dispex_value(This, lcid, flags, dp, res, ei, caller);
743 if(FAILED(hres))
744 return hres;
746 if(func->func_disp_idx == -1)
747 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
748 else
749 hres = function_invoke(This, func, flags, dp, res, ei);
751 return hres;
754 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
756 dynamic_prop_t *prop;
757 DISPID id;
758 HRESULT hres;
760 hres = get_builtin_id(This, name, 0, &id);
761 if(hres == S_OK) {
762 DISPID named_id = DISPID_PROPERTYPUT;
763 VARIANT var;
764 DISPPARAMS dp = {&var,&named_id,1,1};
765 EXCEPINFO ei;
767 V_VT(&var) = VT_EMPTY;
768 memset(&ei, 0, sizeof(ei));
769 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
770 if(FAILED(hres))
771 return hres;
773 *success = VARIANT_TRUE;
774 return S_OK;
777 hres = get_dynamic_prop(This, name, 0, &prop);
778 if(FAILED(hres)) {
779 if(hres != DISP_E_UNKNOWNNAME)
780 return hres;
781 *success = VARIANT_FALSE;
782 return S_OK;
785 VariantClear(&prop->var);
786 prop->flags |= DYNPROP_DELETED;
787 *success = VARIANT_TRUE;
788 return S_OK;
791 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
793 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
796 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
798 DispatchEx *This = impl_from_IDispatchEx(iface);
800 return IUnknown_QueryInterface(This->outer, riid, ppv);
803 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
805 DispatchEx *This = impl_from_IDispatchEx(iface);
807 return IUnknown_AddRef(This->outer);
810 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
812 DispatchEx *This = impl_from_IDispatchEx(iface);
814 return IUnknown_Release(This->outer);
817 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
819 DispatchEx *This = impl_from_IDispatchEx(iface);
821 TRACE("(%p)->(%p)\n", This, pctinfo);
823 *pctinfo = 1;
824 return S_OK;
827 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
828 LCID lcid, ITypeInfo **ppTInfo)
830 DispatchEx *This = impl_from_IDispatchEx(iface);
831 HRESULT hres;
833 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
835 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
836 if(FAILED(hres))
837 return hres;
839 ITypeInfo_AddRef(*ppTInfo);
840 return S_OK;
843 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
844 LPOLESTR *rgszNames, UINT cNames,
845 LCID lcid, DISPID *rgDispId)
847 DispatchEx *This = impl_from_IDispatchEx(iface);
848 UINT i;
849 HRESULT hres;
851 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
852 lcid, rgDispId);
854 for(i=0; i < cNames; i++) {
855 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
856 if(FAILED(hres))
857 return hres;
860 return S_OK;
863 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
864 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
865 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
867 DispatchEx *This = impl_from_IDispatchEx(iface);
869 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
870 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
872 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
873 pVarResult, pExcepInfo, NULL);
876 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
878 DispatchEx *This = impl_from_IDispatchEx(iface);
879 dynamic_prop_t *dprop;
880 HRESULT hres;
882 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
884 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
885 FIXME("Unsupported grfdex %x\n", grfdex);
887 hres = get_builtin_id(This, bstrName, grfdex, pid);
888 if(hres != DISP_E_UNKNOWNNAME)
889 return hres;
891 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
892 if(FAILED(hres))
893 return hres;
895 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
896 return S_OK;
899 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
900 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
902 DispatchEx *This = impl_from_IDispatchEx(iface);
903 HRESULT hres;
905 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
907 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
908 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
910 if(wFlags == DISPATCH_CONSTRUCT) {
911 if(id == DISPID_VALUE) {
912 if(This->data->vtbl && This->data->vtbl->value) {
913 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
915 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
916 return E_FAIL;
918 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
919 return E_FAIL;
922 if(is_dynamic_dispid(id)) {
923 DWORD idx = id - DISPID_DYNPROP_0;
924 dynamic_prop_t *prop;
926 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
927 return DISP_E_UNKNOWNNAME;
929 prop = This->dynamic_data->props+idx;
931 switch(wFlags) {
932 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
933 if(!pvarRes)
934 return E_INVALIDARG;
935 case DISPATCH_METHOD: {
936 DISPID named_arg = DISPID_THIS;
937 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
938 IDispatchEx *dispex;
940 if(V_VT(&prop->var) != VT_DISPATCH) {
941 FIXME("invoke %s\n", debugstr_variant(&prop->var));
942 return E_NOTIMPL;
945 if(pdp->cNamedArgs) {
946 FIXME("named args not supported\n");
947 return E_NOTIMPL;
950 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
951 if(!dp.rgvarg)
952 return E_OUTOFMEMORY;
954 dp.cArgs = pdp->cArgs+1;
955 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
957 V_VT(dp.rgvarg) = VT_DISPATCH;
958 V_DISPATCH(dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
960 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
961 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
962 if(SUCCEEDED(hres)) {
963 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
964 IDispatchEx_Release(dispex);
965 }else {
966 ULONG err = 0;
967 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
969 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
971 heap_free(dp.rgvarg);
972 return hres;
974 case DISPATCH_PROPERTYGET:
975 if(prop->flags & DYNPROP_DELETED)
976 return DISP_E_UNKNOWNNAME;
977 return variant_copy(pvarRes, &prop->var);
978 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
979 case DISPATCH_PROPERTYPUT:
980 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
981 || pdp->cNamedArgs > 1) {
982 FIXME("invalid args\n");
983 return E_INVALIDARG;
986 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
987 VariantClear(&prop->var);
988 hres = variant_copy(&prop->var, pdp->rgvarg);
989 if(FAILED(hres))
990 return hres;
992 prop->flags &= ~DYNPROP_DELETED;
993 return S_OK;
994 default:
995 FIXME("unhandled wFlags %x\n", wFlags);
996 return E_NOTIMPL;
1000 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1003 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1005 DispatchEx *This = impl_from_IDispatchEx(iface);
1007 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1009 /* Not implemented by IE */
1010 return E_NOTIMPL;
1013 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1015 DispatchEx *This = impl_from_IDispatchEx(iface);
1017 TRACE("(%p)->(%x)\n", This, id);
1019 /* Not implemented by IE */
1020 return E_NOTIMPL;
1023 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1025 DispatchEx *This = impl_from_IDispatchEx(iface);
1026 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1027 return E_NOTIMPL;
1030 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1032 DispatchEx *This = impl_from_IDispatchEx(iface);
1033 dispex_data_t *data;
1034 func_info_t *func;
1035 HRESULT hres;
1037 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1039 if(is_dynamic_dispid(id)) {
1040 DWORD idx = id - DISPID_DYNPROP_0;
1042 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1043 return DISP_E_UNKNOWNNAME;
1045 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1046 if(!*pbstrName)
1047 return E_OUTOFMEMORY;
1049 return S_OK;
1052 data = get_dispex_data(This);
1053 if(!data)
1054 return E_FAIL;
1056 hres = get_builtin_func(data, id, &func);
1057 if(FAILED(hres))
1058 return hres;
1060 *pbstrName = SysAllocString(func->name);
1061 if(!*pbstrName)
1062 return E_OUTOFMEMORY;
1063 return S_OK;
1066 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1068 DispatchEx *This = impl_from_IDispatchEx(iface);
1069 dispex_data_t *data;
1070 func_info_t *func;
1071 HRESULT hres;
1073 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1075 if(is_dynamic_dispid(id)) {
1076 DWORD idx = id - DISPID_DYNPROP_0;
1078 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1079 return DISP_E_UNKNOWNNAME;
1081 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1083 if(idx == This->dynamic_data->prop_cnt) {
1084 *pid = DISPID_STARTENUM;
1085 return S_FALSE;
1088 *pid = DISPID_DYNPROP_0+idx;
1089 return S_OK;
1092 data = get_dispex_data(This);
1093 if(!data)
1094 return E_FAIL;
1096 if(id == DISPID_STARTENUM) {
1097 func = data->funcs;
1098 }else {
1099 hres = get_builtin_func(data, id, &func);
1100 if(FAILED(hres))
1101 return hres;
1102 func++;
1105 while(func < data->funcs+data->func_cnt) {
1106 /* FIXME: Skip hidden properties */
1107 if(func->func_disp_idx == -1) {
1108 *pid = func->id;
1109 return S_OK;
1111 func++;
1114 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1115 *pid = DISPID_DYNPROP_0;
1116 return S_OK;
1119 *pid = DISPID_STARTENUM;
1120 return S_FALSE;
1123 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1125 DispatchEx *This = impl_from_IDispatchEx(iface);
1126 FIXME("(%p)->(%p)\n", This, ppunk);
1127 return E_NOTIMPL;
1130 static IDispatchExVtbl DispatchExVtbl = {
1131 DispatchEx_QueryInterface,
1132 DispatchEx_AddRef,
1133 DispatchEx_Release,
1134 DispatchEx_GetTypeInfoCount,
1135 DispatchEx_GetTypeInfo,
1136 DispatchEx_GetIDsOfNames,
1137 DispatchEx_Invoke,
1138 DispatchEx_GetDispID,
1139 DispatchEx_InvokeEx,
1140 DispatchEx_DeleteMemberByName,
1141 DispatchEx_DeleteMemberByDispID,
1142 DispatchEx_GetMemberProperties,
1143 DispatchEx_GetMemberName,
1144 DispatchEx_GetNextDispID,
1145 DispatchEx_GetNameSpaceParent
1148 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1150 static const IID IID_UndocumentedScriptIface =
1151 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1152 static const IID IID_IDispatchJS =
1153 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1155 if(IsEqualGUID(&IID_IDispatch, riid)) {
1156 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1157 *ppv = &This->IDispatchEx_iface;
1158 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1159 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1160 *ppv = &This->IDispatchEx_iface;
1161 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1162 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1163 *ppv = NULL;
1164 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1165 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1166 *ppv = NULL;
1167 }else {
1168 return FALSE;
1171 if(*ppv)
1172 IUnknown_AddRef((IUnknown*)*ppv);
1173 return TRUE;
1176 void release_dispex(DispatchEx *This)
1178 dynamic_prop_t *prop;
1180 if(!This->dynamic_data)
1181 return;
1183 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1184 VariantClear(&prop->var);
1185 heap_free(prop->name);
1188 heap_free(This->dynamic_data->props);
1190 if(This->dynamic_data->func_disps) {
1191 unsigned i;
1193 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1194 if(This->dynamic_data->func_disps[i]) {
1195 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1196 heap_free(This->dynamic_data->func_disps[i]);
1200 heap_free(This->dynamic_data->func_disps);
1203 heap_free(This->dynamic_data);
1206 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1208 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1209 dispex->outer = outer;
1210 dispex->data = data;
1211 dispex->dynamic_data = NULL;