980b13b1654096b6fa85b3d3251f2da00ac6fc7d
[reactos.git] / reactos / dll / win32 / mshtml / dispex.c
1 /*
2 * Copyright 2008-2009 Jacek Caban for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27
28 #include "wine/debug.h"
29
30 #include "mshtml_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
33
34 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
35
36 typedef struct {
37 DISPID id;
38 BSTR name;
39 tid_t tid;
40 int func_disp_idx;
41 } func_info_t;
42
43 struct dispex_data_t {
44 DWORD func_cnt;
45 func_info_t *funcs;
46 func_info_t **name_table;
47 DWORD func_disp_cnt;
48
49 struct list entry;
50 };
51
52 typedef struct {
53 VARIANT var;
54 LPWSTR name;
55 } dynamic_prop_t;
56
57 typedef struct {
58 DispatchEx dispex;
59 const IUnknownVtbl *lpIUnknownVtbl;
60 DispatchEx *obj;
61 func_info_t *info;
62 } func_disp_t;
63
64 #define FUNCUNKNOWN(x) ((IUnknown*) &(x)->lpIUnknownVtbl)
65
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;
71 };
72
73 #define DISPID_DYNPROP_0 0x50000000
74 #define DISPID_DYNPROP_MAX 0x5fffffff
75
76 #define FDEX_VERSION_MASK 0xf0000000
77
78 static ITypeLib *typelib;
79 static ITypeInfo *typeinfos[LAST_tid];
80 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
81
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_IOmNavigator
148 };
149
150 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
151 {
152 HRESULT hres;
153
154 if(!typelib) {
155 ITypeLib *tl;
156
157 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
158 if(FAILED(hres)) {
159 ERR("LoadRegTypeLib failed: %08x\n", hres);
160 return hres;
161 }
162
163 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
164 ITypeLib_Release(tl);
165 }
166
167 if(!typeinfos[tid]) {
168 ITypeInfo *typeinfo;
169
170 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
171 if(FAILED(hres)) {
172 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
173 return hres;
174 }
175
176 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
177 ITypeInfo_Release(typeinfo);
178 }
179
180 *typeinfo = typeinfos[tid];
181 return S_OK;
182 }
183
184 void release_typelib(void)
185 {
186 dispex_data_t *iter;
187 unsigned i;
188
189 while(!list_empty(&dispex_data_list)) {
190 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
191 list_remove(&iter->entry);
192
193 for(i=0; i < iter->func_cnt; i++)
194 SysFreeString(iter->funcs[i].name);
195
196 heap_free(iter->funcs);
197 heap_free(iter->name_table);
198 heap_free(iter);
199 }
200
201 if(!typelib)
202 return;
203
204 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
205 if(typeinfos[i])
206 ITypeInfo_Release(typeinfos[i]);
207
208 ITypeLib_Release(typelib);
209 }
210
211 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
212 {
213 HRESULT hres;
214
215 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
216 return;
217
218 if(data->func_cnt == *size)
219 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
220
221 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
222 if(FAILED(hres))
223 return;
224
225 data->funcs[data->func_cnt].id = desc->memid;
226 data->funcs[data->func_cnt].tid = tid;
227 data->funcs[data->func_cnt].func_disp_idx = (desc->invkind & DISPATCH_METHOD) ? data->func_disp_cnt++ : -1;
228
229 data->func_cnt++;
230 }
231
232 static int dispid_cmp(const void *p1, const void *p2)
233 {
234 return ((func_info_t*)p1)->id - ((func_info_t*)p2)->id;
235 }
236
237 static int func_name_cmp(const void *p1, const void *p2)
238 {
239 return strcmpiW((*(func_info_t**)p1)->name, (*(func_info_t**)p2)->name);
240 }
241
242 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
243 {
244 const tid_t *tid = This->data->iface_tids;
245 FUNCDESC *funcdesc;
246 dispex_data_t *data;
247 DWORD size = 16, i;
248 ITypeInfo *ti, *dti;
249 HRESULT hres;
250
251 TRACE("(%p)\n", This);
252
253 if(This->data->disp_tid) {
254 hres = get_typeinfo(This->data->disp_tid, &dti);
255 if(FAILED(hres)) {
256 ERR("Could not get disp type info: %08x\n", hres);
257 return NULL;
258 }
259 }
260
261 data = heap_alloc(sizeof(dispex_data_t));
262 data->func_cnt = 0;
263 data->func_disp_cnt = 0;
264 data->funcs = heap_alloc(size*sizeof(func_info_t));
265 list_add_tail(&dispex_data_list, &data->entry);
266
267 while(*tid) {
268 hres = get_typeinfo(*tid, &ti);
269 if(FAILED(hres))
270 break;
271
272 i=7;
273 while(1) {
274 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
275 if(FAILED(hres))
276 break;
277
278 add_func_info(data, &size, *tid, funcdesc, dti);
279 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
280 }
281
282 tid++;
283 }
284
285 if(!data->func_cnt) {
286 heap_free(data->funcs);
287 data->funcs = NULL;
288 }else if(data->func_cnt != size) {
289 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
290 }
291
292 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
293
294 if(data->funcs) {
295 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
296 for(i=0; i < data->func_cnt; i++)
297 data->name_table[i] = data->funcs+i;
298 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
299 }else {
300 data->name_table = NULL;
301 }
302
303 return data;
304 }
305
306 static CRITICAL_SECTION cs_dispex_static_data;
307 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
308 {
309 0, 0, &cs_dispex_static_data,
310 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
311 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
312 };
313 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
314
315
316 static dispex_data_t *get_dispex_data(DispatchEx *This)
317 {
318 if(This->data->data)
319 return This->data->data;
320
321 EnterCriticalSection(&cs_dispex_static_data);
322
323 if(!This->data->data)
324 This->data->data = preprocess_dispex_data(This);
325
326 LeaveCriticalSection(&cs_dispex_static_data);
327
328 return This->data->data;
329 }
330
331 HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp)
332 {
333 EXCEPINFO ei;
334 IDispatchEx *dispex;
335 VARIANT res;
336 HRESULT hres;
337
338 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
339 if(FAILED(hres)) {
340 FIXME("Could not get IDispatchEx interface: %08x\n", hres);
341 return hres;
342 }
343
344 VariantInit(&res);
345 memset(&ei, 0, sizeof(ei));
346
347 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, &res, &ei, NULL);
348
349 IDispatchEx_Release(dispex);
350 VariantClear(&res);
351 return hres;
352 }
353
354 static inline BOOL is_custom_dispid(DISPID id)
355 {
356 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
357 }
358
359 static inline BOOL is_dynamic_dispid(DISPID id)
360 {
361 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
362 }
363
364 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
365 {
366 return !alloc || This->dynamic_data
367 ? This->dynamic_data
368 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
369 }
370
371 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
372 {
373 const BOOL alloc = flags & fdexNameEnsure;
374 dispex_dynamic_data_t *data;
375 unsigned i;
376
377 data = get_dynamic_data(This, alloc);
378 if(!data) {
379 if(alloc)
380 return E_OUTOFMEMORY;
381
382 TRACE("not found %s\n", debugstr_w(name));
383 return DISP_E_UNKNOWNNAME;
384 }
385
386 for(i=0; i < data->prop_cnt; i++) {
387 if(flags & fdexNameCaseInsensitive ? !strcmpiW(data->props[i].name, name) : !strcmpW(data->props[i].name, name)) {
388 *ret = data->props+i;
389 return S_OK;
390 }
391 }
392
393 if(!alloc)
394 return DISP_E_UNKNOWNNAME;
395
396 TRACE("creating dynamic prop %s\n", debugstr_w(name));
397
398 if(!data->buf_size) {
399 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
400 if(!data->props)
401 return E_OUTOFMEMORY;
402 data->buf_size = 4;
403 }else if(data->buf_size == data->prop_cnt) {
404 dynamic_prop_t *new_props;
405
406 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
407 if(!new_props)
408 return E_OUTOFMEMORY;
409
410 data->props = new_props;
411 data->buf_size <<= 1;
412 }
413
414 data->props[data->prop_cnt].name = heap_strdupW(name);
415 VariantInit(&data->props[data->prop_cnt].var);
416 *ret = data->props + data->prop_cnt++;
417
418 return S_OK;
419 }
420
421 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
422 {
423 dynamic_prop_t *prop;
424 HRESULT hres;
425
426 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
427 if(FAILED(hres))
428 return hres;
429
430 *ret = &prop->var;
431 return S_OK;
432 }
433
434 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
435 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
436 {
437 if(This->data->vtbl && This->data->vtbl->value)
438 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
439
440 switch(flags) {
441 case DISPATCH_PROPERTYGET:
442 V_VT(res) = VT_BSTR;
443 V_BSTR(res) = SysAllocString(objectW);
444 if(!V_BSTR(res))
445 return E_OUTOFMEMORY;
446 break;
447 default:
448 FIXME("Unimplemented flags %x\n", flags);
449 return E_NOTIMPL;
450 }
451
452 return S_OK;
453 }
454
455 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
456 EXCEPINFO *ei)
457 {
458 ITypeInfo *ti;
459 IUnknown *unk;
460 UINT argerr=0;
461 HRESULT hres;
462
463 hres = get_typeinfo(func->tid, &ti);
464 if(FAILED(hres)) {
465 ERR("Could not get type info: %08x\n", hres);
466 return hres;
467 }
468
469 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
470 if(FAILED(hres)) {
471 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
472 return E_FAIL;
473 }
474
475 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
476
477 IUnknown_Release(unk);
478 return hres;
479 }
480
481 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
482
483 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
484 {
485 func_disp_t *This = FUNCTION_THIS(iface);
486
487 if(IsEqualGUID(&IID_IUnknown, riid)) {
488 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
489 *ppv = FUNCUNKNOWN(This);
490 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
491 return *ppv ? S_OK : E_NOINTERFACE;
492 }else {
493 *ppv = NULL;
494 return E_NOINTERFACE;
495 }
496
497 IUnknown_AddRef((IUnknown*)*ppv);
498 return S_OK;
499 }
500
501 static ULONG WINAPI Function_AddRef(IUnknown *iface)
502 {
503 func_disp_t *This = FUNCTION_THIS(iface);
504
505 TRACE("(%p)\n", This);
506
507 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
508 }
509
510 static ULONG WINAPI Function_Release(IUnknown *iface)
511 {
512 func_disp_t *This = FUNCTION_THIS(iface);
513
514 TRACE("(%p)\n", This);
515
516 return IDispatchEx_Release(DISPATCHEX(This->obj));
517 }
518
519 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
520 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
521 {
522 func_disp_t *This = FUNCTION_THIS(iface);
523 HRESULT hres;
524
525 switch(flags) {
526 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
527 if(!res)
528 return E_INVALIDARG;
529 case DISPATCH_METHOD:
530 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
531 break;
532 default:
533 FIXME("Unimplemented flags %x\n", flags);
534 hres = E_NOTIMPL;
535 }
536
537 return hres;
538 }
539
540 #undef FUNCTION_THIS
541
542 static const IUnknownVtbl FunctionUnkVtbl = {
543 Function_QueryInterface,
544 Function_AddRef,
545 Function_Release
546 };
547
548 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
549 function_value,
550 NULL,
551 NULL
552 };
553
554 static const tid_t function_iface_tids[] = {0};
555
556 static dispex_static_data_t function_dispex = {
557 &function_dispex_vtbl,
558 NULL_tid,
559 NULL,
560 function_iface_tids
561 };
562
563 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
564 {
565 func_disp_t *ret;
566
567 ret = heap_alloc_zero(sizeof(func_disp_t));
568 if(!ret)
569 return NULL;
570
571 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
572 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
573 ret->obj = obj;
574 ret->info = info;
575
576 return ret;
577 }
578
579 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
580 EXCEPINFO *ei)
581 {
582 HRESULT hres;
583
584 switch(flags) {
585 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
586 if(!res)
587 return E_INVALIDARG;
588 case DISPATCH_METHOD:
589 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
590 break;
591 case DISPATCH_PROPERTYGET: {
592 dispex_dynamic_data_t *dynamic_data;
593
594 if(func->id == DISPID_VALUE) {
595 BSTR ret;
596
597 ret = SysAllocString(objectW);
598 if(!ret)
599 return E_OUTOFMEMORY;
600
601 V_VT(res) = VT_BSTR;
602 V_BSTR(res) = ret;
603 return S_OK;
604 }
605
606 dynamic_data = get_dynamic_data(This, TRUE);
607 if(!dynamic_data)
608 return E_OUTOFMEMORY;
609
610 if(!dynamic_data->func_disps) {
611 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
612 if(!dynamic_data->func_disps)
613 return E_OUTOFMEMORY;
614 }
615
616 if(!dynamic_data->func_disps[func->func_disp_idx]) {
617 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
618 if(!dynamic_data->func_disps[func->func_disp_idx])
619 return E_OUTOFMEMORY;
620 }
621
622 V_VT(res) = VT_DISPATCH;
623 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
624 IDispatch_AddRef(V_DISPATCH(res));
625 hres = S_OK;
626 break;
627 }
628 default:
629 FIXME("Unimplemented flags %x\n", flags);
630 case DISPATCH_PROPERTYPUT:
631 hres = E_NOTIMPL;
632 }
633
634 return hres;
635 }
636
637 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
638 {
639 int min, max, n;
640
641 min = 0;
642 max = data->func_cnt-1;
643
644 while(min <= max) {
645 n = (min+max)/2;
646
647 if(data->funcs[n].id == id) {
648 *ret = data->funcs+n;
649 return S_OK;
650 }
651
652 if(data->funcs[n].id < id)
653 min = n+1;
654 else
655 max = n-1;
656 }
657
658 WARN("invalid id %x\n", id);
659 return DISP_E_UNKNOWNNAME;
660 }
661
662 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
663
664 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
665 {
666 DispatchEx *This = DISPATCHEX_THIS(iface);
667
668 return IUnknown_QueryInterface(This->outer, riid, ppv);
669 }
670
671 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
672 {
673 DispatchEx *This = DISPATCHEX_THIS(iface);
674
675 return IUnknown_AddRef(This->outer);
676 }
677
678 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
679 {
680 DispatchEx *This = DISPATCHEX_THIS(iface);
681
682 return IUnknown_Release(This->outer);
683 }
684
685 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
686 {
687 DispatchEx *This = DISPATCHEX_THIS(iface);
688
689 TRACE("(%p)->(%p)\n", This, pctinfo);
690
691 *pctinfo = 1;
692 return S_OK;
693 }
694
695 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
696 LCID lcid, ITypeInfo **ppTInfo)
697 {
698 DispatchEx *This = DISPATCHEX_THIS(iface);
699 HRESULT hres;
700
701 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
702
703 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
704 if(FAILED(hres))
705 return hres;
706
707 ITypeInfo_AddRef(*ppTInfo);
708 return S_OK;
709 }
710
711 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
712 LPOLESTR *rgszNames, UINT cNames,
713 LCID lcid, DISPID *rgDispId)
714 {
715 DispatchEx *This = DISPATCHEX_THIS(iface);
716 UINT i;
717 HRESULT hres;
718
719 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
720 lcid, rgDispId);
721
722 for(i=0; i < cNames; i++) {
723 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
724 if(FAILED(hres))
725 return hres;
726 }
727
728 return S_OK;
729 }
730
731 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
732 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
733 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
734 {
735 DispatchEx *This = DISPATCHEX_THIS(iface);
736
737 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
738 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
739
740 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
741 pDispParams, pVarResult, pExcepInfo, NULL);
742 }
743
744 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
745 {
746 DispatchEx *This = DISPATCHEX_THIS(iface);
747 dynamic_prop_t *dprop;
748 dispex_data_t *data;
749 int min, max, n, c;
750 HRESULT hres;
751
752 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
753
754 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
755 FIXME("Unsupported grfdex %x\n", grfdex);
756
757 data = get_dispex_data(This);
758 if(!data)
759 return E_FAIL;
760
761 min = 0;
762 max = data->func_cnt-1;
763
764 while(min <= max) {
765 n = (min+max)/2;
766
767 c = strcmpiW(data->name_table[n]->name, bstrName);
768 if(!c) {
769 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
770 break;
771
772 *pid = data->name_table[n]->id;
773 return S_OK;
774 }
775
776 if(c > 0)
777 max = n-1;
778 else
779 min = n+1;
780 }
781
782 if(This->data->vtbl && This->data->vtbl->get_dispid) {
783 HRESULT hres;
784
785 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
786 if(hres != DISP_E_UNKNOWNNAME)
787 return hres;
788 }
789
790 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
791 if(FAILED(hres))
792 return hres;
793
794 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
795 return S_OK;
796 }
797
798 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
799 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
800 {
801 DispatchEx *This = DISPATCHEX_THIS(iface);
802 dispex_data_t *data;
803 func_info_t *func;
804 HRESULT hres;
805
806 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
807
808 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
809 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
810
811 if(wFlags == DISPATCH_CONSTRUCT) {
812 if(id == DISPID_VALUE) {
813 if(This->data->vtbl && This->data->vtbl->value) {
814 return This->data->vtbl->value(This->outer, lcid, wFlags, pdp,
815 pvarRes, pei, pspCaller);
816 }
817 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
818 return E_FAIL;
819 }
820 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
821 return E_FAIL;
822 }
823
824 if(is_dynamic_dispid(id)) {
825 DWORD idx = id - DISPID_DYNPROP_0;
826 VARIANT *var;
827
828 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
829 return DISP_E_UNKNOWNNAME;
830
831 var = &This->dynamic_data->props[idx].var;
832
833 switch(wFlags) {
834 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
835 if(!pvarRes)
836 return E_INVALIDARG;
837 case DISPATCH_METHOD: {
838 DISPID named_arg = DISPID_THIS;
839 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
840 IDispatchEx *dispex;
841
842 if(V_VT(var) != VT_DISPATCH) {
843 FIXME("invoke vt %d\n", V_VT(var));
844 return E_NOTIMPL;
845 }
846
847 if(pdp->cNamedArgs) {
848 FIXME("named args not supported\n");
849 return E_NOTIMPL;
850 }
851
852 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
853 if(!dp.rgvarg)
854 return E_OUTOFMEMORY;
855
856 dp.cArgs = pdp->cArgs+1;
857 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
858
859 V_VT(dp.rgvarg) = VT_DISPATCH;
860 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
861
862 hres = IDispatch_QueryInterface(V_DISPATCH(var), &IID_IDispatchEx, (void**)&dispex);
863 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
864 if(SUCCEEDED(hres)) {
865 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
866 IDispatchEx_Release(dispex);
867 }else {
868 ULONG err = 0;
869 hres = IDispatch_Invoke(V_DISPATCH(var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
870 }
871 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
872
873 heap_free(dp.rgvarg);
874 return hres;
875 }
876 case DISPATCH_PROPERTYGET:
877 return VariantCopy(pvarRes, var);
878 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
879 case DISPATCH_PROPERTYPUT:
880 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
881 || pdp->cNamedArgs > 1) {
882 FIXME("invalid args\n");
883 return E_INVALIDARG;
884 }
885
886 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
887 VariantClear(var);
888 return VariantCopy(var, pdp->rgvarg);
889 default:
890 FIXME("unhandled wFlags %x\n", wFlags);
891 return E_NOTIMPL;
892 }
893 }
894
895 data = get_dispex_data(This);
896 if(!data)
897 return E_FAIL;
898
899 hres = get_builtin_func(data, id, &func);
900 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
901 return dispex_value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
902 if(FAILED(hres))
903 return hres;
904
905 if(func->func_disp_idx == -1)
906 hres = typeinfo_invoke(This, func, wFlags, pdp, pvarRes, pei);
907 else
908 hres = function_invoke(This, func, wFlags, pdp, pvarRes, pei);
909
910 return hres;
911 }
912
913 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
914 {
915 DispatchEx *This = DISPATCHEX_THIS(iface);
916 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
917 return E_NOTIMPL;
918 }
919
920 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
921 {
922 DispatchEx *This = DISPATCHEX_THIS(iface);
923 FIXME("(%p)->(%x)\n", This, id);
924 return E_NOTIMPL;
925 }
926
927 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
928 {
929 DispatchEx *This = DISPATCHEX_THIS(iface);
930 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
931 return E_NOTIMPL;
932 }
933
934 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
935 {
936 DispatchEx *This = DISPATCHEX_THIS(iface);
937 dispex_data_t *data;
938 func_info_t *func;
939 HRESULT hres;
940
941 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
942
943 if(is_dynamic_dispid(id)) {
944 DWORD idx = id - DISPID_DYNPROP_0;
945
946 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
947 return DISP_E_UNKNOWNNAME;
948
949 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
950 if(!*pbstrName)
951 return E_OUTOFMEMORY;
952
953 return S_OK;
954 }
955
956 data = get_dispex_data(This);
957 if(!data)
958 return E_FAIL;
959
960 hres = get_builtin_func(data, id, &func);
961 if(FAILED(hres))
962 return hres;
963
964 *pbstrName = SysAllocString(func->name);
965 if(!*pbstrName)
966 return E_OUTOFMEMORY;
967 return S_OK;
968 }
969
970 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
971 {
972 DispatchEx *This = DISPATCHEX_THIS(iface);
973 dispex_data_t *data;
974 func_info_t *func;
975 HRESULT hres;
976
977 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
978
979 if(is_dynamic_dispid(id)) {
980 DWORD idx = id - DISPID_DYNPROP_0;
981
982 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
983 return DISP_E_UNKNOWNNAME;
984
985 if(idx+1 == This->dynamic_data->prop_cnt) {
986 *pid = DISPID_STARTENUM;
987 return S_FALSE;
988 }
989
990 *pid = id+1;
991 return S_OK;
992 }
993
994 data = get_dispex_data(This);
995 if(!data)
996 return E_FAIL;
997
998 if(id == DISPID_STARTENUM) {
999 func = data->funcs;
1000 }else {
1001 hres = get_builtin_func(data, id, &func);
1002 if(FAILED(hres))
1003 return hres;
1004 func++;
1005 }
1006
1007 while(func < data->funcs+data->func_cnt) {
1008 /* FIXME: Skip hidden properties */
1009 if(func->func_disp_idx == -1) {
1010 *pid = func->id;
1011 return S_OK;
1012 }
1013 func++;
1014 }
1015
1016 if(This->dynamic_data && This->dynamic_data->prop_cnt) {
1017 *pid = DISPID_DYNPROP_0;
1018 return S_OK;
1019 }
1020
1021 *pid = DISPID_STARTENUM;
1022 return S_FALSE;
1023 }
1024
1025 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1026 {
1027 DispatchEx *This = DISPATCHEX_THIS(iface);
1028 FIXME("(%p)->(%p)\n", This, ppunk);
1029 return E_NOTIMPL;
1030 }
1031
1032 #undef DISPATCHEX_THIS
1033
1034 static IDispatchExVtbl DispatchExVtbl = {
1035 DispatchEx_QueryInterface,
1036 DispatchEx_AddRef,
1037 DispatchEx_Release,
1038 DispatchEx_GetTypeInfoCount,
1039 DispatchEx_GetTypeInfo,
1040 DispatchEx_GetIDsOfNames,
1041 DispatchEx_Invoke,
1042 DispatchEx_GetDispID,
1043 DispatchEx_InvokeEx,
1044 DispatchEx_DeleteMemberByName,
1045 DispatchEx_DeleteMemberByDispID,
1046 DispatchEx_GetMemberProperties,
1047 DispatchEx_GetMemberName,
1048 DispatchEx_GetNextDispID,
1049 DispatchEx_GetNameSpaceParent
1050 };
1051
1052 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1053 {
1054 static const IID IID_UndocumentedScriptIface =
1055 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1056 static const IID IID_IDispatchJS =
1057 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1058
1059 if(IsEqualGUID(&IID_IDispatch, riid)) {
1060 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1061 *ppv = DISPATCHEX(This);
1062 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1063 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1064 *ppv = DISPATCHEX(This);
1065 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1066 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1067 *ppv = NULL;
1068 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1069 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1070 *ppv = NULL;
1071 }else {
1072 return FALSE;
1073 }
1074
1075 if(*ppv)
1076 IUnknown_AddRef((IUnknown*)*ppv);
1077 return TRUE;
1078 }
1079
1080 void release_dispex(DispatchEx *This)
1081 {
1082 dynamic_prop_t *prop;
1083
1084 if(!This->dynamic_data)
1085 return;
1086
1087 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1088 VariantClear(&prop->var);
1089 heap_free(prop->name);
1090 }
1091
1092 heap_free(This->dynamic_data->props);
1093
1094 if(This->dynamic_data->func_disps) {
1095 unsigned i;
1096
1097 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1098 if(This->dynamic_data->func_disps[i]) {
1099 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1100 heap_free(This->dynamic_data->func_disps[i]);
1101 }
1102 }
1103
1104 heap_free(This->dynamic_data->func_disps);
1105 }
1106
1107 heap_free(This->dynamic_data);
1108 }
1109
1110 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1111 {
1112 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
1113 dispex->outer = outer;
1114 dispex->data = data;
1115 dispex->dynamic_data = NULL;
1116 }