mshtml: Add stub implementation of IHTMLWindow4.
[wine/hramrach.git] / dlls / mshtml / dispex.c
blob382ab25402864de1bfab942c014af2c44f5b74bd
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 } dynamic_prop_t;
57 typedef struct {
58 DispatchEx dispex;
59 const IUnknownVtbl *lpIUnknownVtbl;
60 DispatchEx *obj;
61 func_info_t *info;
62 } func_disp_t;
64 #define FUNCUNKNOWN(x) ((IUnknown*) &(x)->lpIUnknownVtbl)
66 struct dispex_dynamic_data_t {
67 DWORD buf_size;
68 DWORD prop_cnt;
69 dynamic_prop_t *props;
70 func_disp_t **func_disps;
73 #define DISPID_DYNPROP_0 0x50000000
74 #define DISPID_DYNPROP_MAX 0x5fffffff
76 #define FDEX_VERSION_MASK 0xf0000000
78 static ITypeLib *typelib;
79 static ITypeInfo *typeinfos[LAST_tid];
80 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
82 static REFIID tid_ids[] = {
83 &IID_NULL,
84 &DIID_DispCEventObj,
85 &DIID_DispDOMChildrenCollection,
86 &DIID_DispHTMLAnchorElement,
87 &DIID_DispHTMLBody,
88 &DIID_DispHTMLCommentElement,
89 &DIID_DispHTMLCurrentStyle,
90 &DIID_DispHTMLDocument,
91 &DIID_DispHTMLDOMTextNode,
92 &DIID_DispHTMLElementCollection,
93 &DIID_DispHTMLGenericElement,
94 &DIID_DispHTMLIFrame,
95 &DIID_DispHTMLImg,
96 &DIID_DispHTMLInputElement,
97 &DIID_DispHTMLLocation,
98 &DIID_DispHTMLNavigator,
99 &DIID_DispHTMLOptionElement,
100 &DIID_DispHTMLSelectElement,
101 &DIID_DispHTMLStyle,
102 &DIID_DispHTMLTable,
103 &DIID_DispHTMLTableRow,
104 &DIID_DispHTMLUnknownElement,
105 &DIID_DispHTMLWindow2,
106 &IID_IHTMLAnchorElement,
107 &IID_IHTMLBodyElement,
108 &IID_IHTMLBodyElement2,
109 &IID_IHTMLCommentElement,
110 &IID_IHTMLCurrentStyle,
111 &IID_IHTMLCurrentStyle2,
112 &IID_IHTMLCurrentStyle3,
113 &IID_IHTMLCurrentStyle4,
114 &IID_IHTMLDocument2,
115 &IID_IHTMLDocument3,
116 &IID_IHTMLDocument4,
117 &IID_IHTMLDocument5,
118 &IID_IHTMLDOMChildrenCollection,
119 &IID_IHTMLDOMNode,
120 &IID_IHTMLDOMNode2,
121 &IID_IHTMLDOMTextNode,
122 &IID_IHTMLElement,
123 &IID_IHTMLElement2,
124 &IID_IHTMLElement3,
125 &IID_IHTMLElement4,
126 &IID_IHTMLElementCollection,
127 &IID_IHTMLEventObj,
128 &IID_IHTMLFrameBase,
129 &IID_IHTMLFrameBase2,
130 &IID_IHTMLGenericElement,
131 &IID_IHTMLImageElementFactory,
132 &IID_IHTMLImgElement,
133 &IID_IHTMLInputElement,
134 &IID_IHTMLLocation,
135 &IID_IHTMLOptionElement,
136 &IID_IHTMLSelectElement,
137 &IID_IHTMLStyle,
138 &IID_IHTMLStyle2,
139 &IID_IHTMLStyle3,
140 &IID_IHTMLStyle4,
141 &IID_IHTMLTable,
142 &IID_IHTMLTableRow,
143 &IID_IHTMLTextContainer,
144 &IID_IHTMLUniqueName,
145 &IID_IHTMLWindow2,
146 &IID_IHTMLWindow3,
147 &IID_IHTMLWindow4,
148 &IID_IOmNavigator
151 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
153 HRESULT hres;
155 if(!typelib) {
156 ITypeLib *tl;
158 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
159 if(FAILED(hres)) {
160 ERR("LoadRegTypeLib failed: %08x\n", hres);
161 return hres;
164 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
165 ITypeLib_Release(tl);
168 if(!typeinfos[tid]) {
169 ITypeInfo *typeinfo;
171 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
172 if(FAILED(hres)) {
173 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
174 return hres;
177 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
178 ITypeInfo_Release(typeinfo);
181 *typeinfo = typeinfos[tid];
182 return S_OK;
185 void release_typelib(void)
187 dispex_data_t *iter;
188 unsigned i;
190 while(!list_empty(&dispex_data_list)) {
191 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
192 list_remove(&iter->entry);
194 for(i=0; i < iter->func_cnt; i++)
195 SysFreeString(iter->funcs[i].name);
197 heap_free(iter->funcs);
198 heap_free(iter->name_table);
199 heap_free(iter);
202 if(!typelib)
203 return;
205 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
206 if(typeinfos[i])
207 ITypeInfo_Release(typeinfos[i]);
209 ITypeLib_Release(typelib);
212 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
214 HRESULT hres;
216 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
217 return;
219 if(data->func_cnt == *size)
220 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
222 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
223 if(FAILED(hres))
224 return;
226 data->funcs[data->func_cnt].id = desc->memid;
227 data->funcs[data->func_cnt].tid = tid;
228 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
230 data->func_cnt++;
233 static int dispid_cmp(const void *p1, const void *p2)
235 return ((func_info_t*)p1)->id - ((func_info_t*)p2)->id;
238 static int func_name_cmp(const void *p1, const void *p2)
240 return strcmpiW((*(func_info_t**)p1)->name, (*(func_info_t**)p2)->name);
243 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
245 const tid_t *tid = This->data->iface_tids;
246 FUNCDESC *funcdesc;
247 dispex_data_t *data;
248 DWORD size = 16, i;
249 ITypeInfo *ti, *dti;
250 HRESULT hres;
252 TRACE("(%p)\n", This);
254 if(This->data->disp_tid) {
255 hres = get_typeinfo(This->data->disp_tid, &dti);
256 if(FAILED(hres)) {
257 ERR("Could not get disp type info: %08x\n", hres);
258 return NULL;
262 data = heap_alloc(sizeof(dispex_data_t));
263 data->func_cnt = 0;
264 data->func_disp_cnt = 0;
265 data->funcs = heap_alloc(size*sizeof(func_info_t));
266 list_add_tail(&dispex_data_list, &data->entry);
268 while(*tid) {
269 hres = get_typeinfo(*tid, &ti);
270 if(FAILED(hres))
271 break;
273 i=7;
274 while(1) {
275 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
276 if(FAILED(hres))
277 break;
279 add_func_info(data, &size, *tid, funcdesc, dti);
280 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
283 tid++;
286 if(!data->func_cnt) {
287 heap_free(data->funcs);
288 data->funcs = NULL;
289 }else if(data->func_cnt != size) {
290 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
293 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
295 if(data->funcs) {
296 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
297 for(i=0; i < data->func_cnt; i++)
298 data->name_table[i] = data->funcs+i;
299 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
300 }else {
301 data->name_table = NULL;
304 return data;
307 static CRITICAL_SECTION cs_dispex_static_data;
308 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
310 0, 0, &cs_dispex_static_data,
311 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
312 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
314 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
317 static dispex_data_t *get_dispex_data(DispatchEx *This)
319 if(This->data->data)
320 return This->data->data;
322 EnterCriticalSection(&cs_dispex_static_data);
324 if(!This->data->data)
325 This->data->data = preprocess_dispex_data(This);
327 LeaveCriticalSection(&cs_dispex_static_data);
329 return This->data->data;
332 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
334 EXCEPINFO ei;
335 IDispatchEx *dispex;
336 VARIANT res;
337 HRESULT hres;
339 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
340 if(FAILED(hres)) {
341 FIXME("Could not get IDispatchEx interface: %08x\n", hres);
342 return hres;
345 VariantInit(&res);
346 memset(&ei, 0, sizeof(ei));
348 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
350 IDispatchEx_Release(dispex);
351 VariantClear(&res);
352 return hres;
355 static inline BOOL is_custom_dispid(DISPID id)
357 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
360 static inline BOOL is_dynamic_dispid(DISPID id)
362 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
365 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
367 return !alloc || This->dynamic_data
368 ? This->dynamic_data
369 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
372 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
374 const BOOL alloc = flags & fdexNameEnsure;
375 dispex_dynamic_data_t *data;
376 unsigned i;
378 data = get_dynamic_data(This, alloc);
379 if(!data) {
380 if(alloc)
381 return E_OUTOFMEMORY;
383 TRACE("not found %s\n", debugstr_w(name));
384 return DISP_E_UNKNOWNNAME;
387 for(i=0; i < data->prop_cnt; i++) {
388 if(flags & fdexNameCaseInsensitive ? !strcmpiW(data->props[i].name, name) : !strcmpW(data->props[i].name, name)) {
389 *ret = data->props+i;
390 return S_OK;
394 if(!alloc)
395 return DISP_E_UNKNOWNNAME;
397 TRACE("creating dynamic prop %s\n", debugstr_w(name));
399 if(!data->buf_size) {
400 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
401 if(!data->props)
402 return E_OUTOFMEMORY;
403 data->buf_size = 4;
404 }else if(data->buf_size == data->prop_cnt) {
405 dynamic_prop_t *new_props;
407 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
408 if(!new_props)
409 return E_OUTOFMEMORY;
411 data->props = new_props;
412 data->buf_size <<= 1;
415 data->props[data->prop_cnt].name = heap_strdupW(name);
416 VariantInit(&data->props[data->prop_cnt].var);
417 *ret = data->props + data->prop_cnt++;
419 return S_OK;
422 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
424 dynamic_prop_t *prop;
425 HRESULT hres;
427 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
428 if(FAILED(hres))
429 return hres;
431 *ret = &prop->var;
432 return S_OK;
435 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
436 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
438 if(This->data->vtbl && This->data->vtbl->value)
439 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
441 switch(flags) {
442 case DISPATCH_PROPERTYGET:
443 V_VT(res) = VT_BSTR;
444 V_BSTR(res) = SysAllocString(objectW);
445 if(!V_BSTR(res))
446 return E_OUTOFMEMORY;
447 break;
448 default:
449 FIXME("Unimplemented flags %x\n", flags);
450 return E_NOTIMPL;
453 return S_OK;
456 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
457 EXCEPINFO *ei)
459 ITypeInfo *ti;
460 IUnknown *unk;
461 UINT argerr=0;
462 HRESULT hres;
464 hres = get_typeinfo(func->tid, &ti);
465 if(FAILED(hres)) {
466 ERR("Could not get type info: %08x\n", hres);
467 return hres;
470 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
471 if(FAILED(hres)) {
472 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
473 return E_FAIL;
476 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
478 IUnknown_Release(unk);
479 return hres;
482 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
484 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
486 func_disp_t *This = FUNCTION_THIS(iface);
488 if(IsEqualGUID(&IID_IUnknown, riid)) {
489 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
490 *ppv = FUNCUNKNOWN(This);
491 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
492 return *ppv ? S_OK : E_NOINTERFACE;
493 }else {
494 *ppv = NULL;
495 return E_NOINTERFACE;
498 IUnknown_AddRef((IUnknown*)*ppv);
499 return S_OK;
502 static ULONG WINAPI Function_AddRef(IUnknown *iface)
504 func_disp_t *This = FUNCTION_THIS(iface);
506 TRACE("(%p)\n", This);
508 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
511 static ULONG WINAPI Function_Release(IUnknown *iface)
513 func_disp_t *This = FUNCTION_THIS(iface);
515 TRACE("(%p)\n", This);
517 return IDispatchEx_Release(DISPATCHEX(This->obj));
520 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
521 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
523 func_disp_t *This = FUNCTION_THIS(iface);
524 HRESULT hres;
526 switch(flags) {
527 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
528 if(!res)
529 return E_INVALIDARG;
530 case DISPATCH_METHOD:
531 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
532 break;
533 default:
534 FIXME("Unimplemented flags %x\n", flags);
535 hres = E_NOTIMPL;
538 return hres;
541 #undef FUNCTION_THIS
543 static const IUnknownVtbl FunctionUnkVtbl = {
544 Function_QueryInterface,
545 Function_AddRef,
546 Function_Release
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->lpIUnknownVtbl = &FunctionUnkVtbl;
573 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &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*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
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 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
665 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
667 DispatchEx *This = DISPATCHEX_THIS(iface);
669 return IUnknown_QueryInterface(This->outer, riid, ppv);
672 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
674 DispatchEx *This = DISPATCHEX_THIS(iface);
676 return IUnknown_AddRef(This->outer);
679 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
681 DispatchEx *This = DISPATCHEX_THIS(iface);
683 return IUnknown_Release(This->outer);
686 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
688 DispatchEx *This = DISPATCHEX_THIS(iface);
690 TRACE("(%p)->(%p)\n", This, pctinfo);
692 *pctinfo = 1;
693 return S_OK;
696 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
697 LCID lcid, ITypeInfo **ppTInfo)
699 DispatchEx *This = DISPATCHEX_THIS(iface);
700 HRESULT hres;
702 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
704 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
705 if(FAILED(hres))
706 return hres;
708 ITypeInfo_AddRef(*ppTInfo);
709 return S_OK;
712 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
713 LPOLESTR *rgszNames, UINT cNames,
714 LCID lcid, DISPID *rgDispId)
716 DispatchEx *This = DISPATCHEX_THIS(iface);
717 UINT i;
718 HRESULT hres;
720 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
721 lcid, rgDispId);
723 for(i=0; i < cNames; i++) {
724 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
725 if(FAILED(hres))
726 return hres;
729 return S_OK;
732 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
733 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
734 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
736 DispatchEx *This = DISPATCHEX_THIS(iface);
738 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
739 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
741 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
742 pDispParams, pVarResult, pExcepInfo, NULL);
745 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
747 DispatchEx *This = DISPATCHEX_THIS(iface);
748 dynamic_prop_t *dprop;
749 dispex_data_t *data;
750 int min, max, n, c;
751 HRESULT hres;
753 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
755 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
756 FIXME("Unsupported grfdex %x\n", grfdex);
758 data = get_dispex_data(This);
759 if(!data)
760 return E_FAIL;
762 min = 0;
763 max = data->func_cnt-1;
765 while(min <= max) {
766 n = (min+max)/2;
768 c = strcmpiW(data->name_table[n]->name, bstrName);
769 if(!c) {
770 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
771 break;
773 *pid = data->name_table[n]->id;
774 return S_OK;
777 if(c > 0)
778 max = n-1;
779 else
780 min = n+1;
783 if(This->data->vtbl && This->data->vtbl->get_dispid) {
784 HRESULT hres;
786 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
787 if(hres != DISP_E_UNKNOWNNAME)
788 return hres;
791 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
792 if(FAILED(hres))
793 return hres;
795 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
796 return S_OK;
799 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
800 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
802 DispatchEx *This = DISPATCHEX_THIS(iface);
803 dispex_data_t *data;
804 func_info_t *func;
805 HRESULT hres;
807 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
809 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
810 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
812 if(wFlags == DISPATCH_CONSTRUCT) {
813 if(id == DISPID_VALUE) {
814 if(This->data->vtbl && This->data->vtbl->value) {
815 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
816 pvarRes, pei, pspCaller);
818 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
819 return E_FAIL;
821 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
822 return E_FAIL;
825 if(is_dynamic_dispid(id)) {
826 DWORD idx = id - DISPID_DYNPROP_0;
827 VARIANT *var;
829 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
830 return DISP_E_UNKNOWNNAME;
832 var = &This->dynamic_data->props[idx].var;
834 switch(wFlags) {
835 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
836 if(!pvarRes)
837 return E_INVALIDARG;
838 case DISPATCH_METHOD: {
839 DISPID named_arg = DISPID_THIS;
840 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
841 IDispatchEx *dispex;
843 if(V_VT(var) != VT_DISPATCH) {
844 FIXME("invoke vt %d\n", V_VT(var));
845 return E_NOTIMPL;
848 if(pdp->cNamedArgs) {
849 FIXME("named args not supported\n");
850 return E_NOTIMPL;
853 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
854 if(!dp.rgvarg)
855 return E_OUTOFMEMORY;
857 dp.cArgs = pdp->cArgs+1;
858 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
860 V_VT(dp.rgvarg) = VT_DISPATCH;
861 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
863 hres = IDispatch_QueryInterface(V_DISPATCH(var), &IID_IDispatchEx, (void**)&dispex);
864 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
865 if(SUCCEEDED(hres)) {
866 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
867 IDispatchEx_Release(dispex);
868 }else {
869 ULONG err = 0;
870 hres = IDispatch_Invoke(V_DISPATCH(var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
872 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
874 heap_free(dp.rgvarg);
875 return hres;
877 case DISPATCH_PROPERTYGET:
878 return VariantCopy(pvarRes, var);
879 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
880 case DISPATCH_PROPERTYPUT:
881 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
882 || pdp->cNamedArgs > 1) {
883 FIXME("invalid args\n");
884 return E_INVALIDARG;
887 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
888 VariantClear(var);
889 return VariantCopy(var, pdp->rgvarg);
890 default:
891 FIXME("unhandled wFlags %x\n", wFlags);
892 return E_NOTIMPL;
896 data = get_dispex_data(This);
897 if(!data)
898 return E_FAIL;
900 hres = get_builtin_func(data, id, &func);
901 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
902 return dispex_value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
903 if(FAILED(hres))
904 return hres;
906 if(func->func_disp_idx == -1)
907 hres = typeinfo_invoke(This, func, wFlags, pdp, pvarRes, pei);
908 else
909 hres = function_invoke(This, func, wFlags, pdp, pvarRes, pei);
911 return hres;
914 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
916 DispatchEx *This = DISPATCHEX_THIS(iface);
917 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
918 return E_NOTIMPL;
921 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
923 DispatchEx *This = DISPATCHEX_THIS(iface);
924 FIXME("(%p)->(%x)\n", This, id);
925 return E_NOTIMPL;
928 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
930 DispatchEx *This = DISPATCHEX_THIS(iface);
931 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
932 return E_NOTIMPL;
935 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
937 DispatchEx *This = DISPATCHEX_THIS(iface);
938 dispex_data_t *data;
939 func_info_t *func;
940 HRESULT hres;
942 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
944 if(is_dynamic_dispid(id)) {
945 DWORD idx = id - DISPID_DYNPROP_0;
947 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
948 return DISP_E_UNKNOWNNAME;
950 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
951 if(!*pbstrName)
952 return E_OUTOFMEMORY;
954 return S_OK;
957 data = get_dispex_data(This);
958 if(!data)
959 return E_FAIL;
961 hres = get_builtin_func(data, id, &func);
962 if(FAILED(hres))
963 return hres;
965 *pbstrName = SysAllocString(func->name);
966 if(!*pbstrName)
967 return E_OUTOFMEMORY;
968 return S_OK;
971 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
973 DispatchEx *This = DISPATCHEX_THIS(iface);
974 dispex_data_t *data;
975 func_info_t *func;
976 HRESULT hres;
978 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
980 if(is_dynamic_dispid(id)) {
981 DWORD idx = id - DISPID_DYNPROP_0;
983 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
984 return DISP_E_UNKNOWNNAME;
986 if(idx+1 == This->dynamic_data->prop_cnt) {
987 *pid = DISPID_STARTENUM;
988 return S_FALSE;
991 *pid = id+1;
992 return S_OK;
995 data = get_dispex_data(This);
996 if(!data)
997 return E_FAIL;
999 if(id == DISPID_STARTENUM) {
1000 func = data->funcs;
1001 }else {
1002 hres = get_builtin_func(data, id, &func);
1003 if(FAILED(hres))
1004 return hres;
1005 func++;
1008 while(func < data->funcs+data->func_cnt) {
1009 /* FIXME: Skip hidden properties */
1010 if(func->func_disp_idx == -1) {
1011 *pid = func->id;
1012 return S_OK;
1014 func++;
1017 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1018 *pid = DISPID_DYNPROP_0;
1019 return S_OK;
1022 *pid = DISPID_STARTENUM;
1023 return S_FALSE;
1026 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1028 DispatchEx *This = DISPATCHEX_THIS(iface);
1029 FIXME("(%p)->(%p)\n", This, ppunk);
1030 return E_NOTIMPL;
1033 #undef DISPATCHEX_THIS
1035 static IDispatchExVtbl DispatchExVtbl = {
1036 DispatchEx_QueryInterface,
1037 DispatchEx_AddRef,
1038 DispatchEx_Release,
1039 DispatchEx_GetTypeInfoCount,
1040 DispatchEx_GetTypeInfo,
1041 DispatchEx_GetIDsOfNames,
1042 DispatchEx_Invoke,
1043 DispatchEx_GetDispID,
1044 DispatchEx_InvokeEx,
1045 DispatchEx_DeleteMemberByName,
1046 DispatchEx_DeleteMemberByDispID,
1047 DispatchEx_GetMemberProperties,
1048 DispatchEx_GetMemberName,
1049 DispatchEx_GetNextDispID,
1050 DispatchEx_GetNameSpaceParent
1053 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1055 static const IID IID_UndocumentedScriptIface =
1056 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1057 static const IID IID_IDispatchJS =
1058 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1060 if(IsEqualGUID(&IID_IDispatch, riid)) {
1061 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1062 *ppv = DISPATCHEX(This);
1063 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1064 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1065 *ppv = DISPATCHEX(This);
1066 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1067 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1068 *ppv = NULL;
1069 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1070 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1071 *ppv = NULL;
1072 }else {
1073 return FALSE;
1076 if(*ppv)
1077 IUnknown_AddRef((IUnknown*)*ppv);
1078 return TRUE;
1081 void release_dispex(DispatchEx *This)
1083 dynamic_prop_t *prop;
1085 if(!This->dynamic_data)
1086 return;
1088 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1089 VariantClear(&prop->var);
1090 heap_free(prop->name);
1093 heap_free(This->dynamic_data->props);
1095 if(This->dynamic_data->func_disps) {
1096 unsigned i;
1098 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1099 if(This->dynamic_data->func_disps[i]) {
1100 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1101 heap_free(This->dynamic_data->func_disps[i]);
1105 heap_free(This->dynamic_data->func_disps);
1108 heap_free(This->dynamic_data);
1111 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1113 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1114 dispex->outer = outer;
1115 dispex->data = data;
1116 dispex->dynamic_data = NULL;