kernelbase: Ignore URL_PARTFLAG_KEEPSCHEME when used with URL_PART_SCHEME or URL_PART...
[wine.git] / dlls / mshtml / dispex.c
blob450bc8efe512f280fea898391ad148c7b725cfc8
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
22 #define NONAMELESSUNION
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28 #include "mscoree.h"
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 #define MAX_ARGS 16
38 static CRITICAL_SECTION cs_dispex_static_data;
39 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
41 0, 0, &cs_dispex_static_data,
42 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
43 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
45 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
47 typedef struct {
48 IID iid;
49 VARIANT default_value;
50 } func_arg_info_t;
52 typedef struct {
53 DISPID id;
54 BSTR name;
55 tid_t tid;
56 dispex_hook_invoke_t hook;
57 SHORT call_vtbl_off;
58 SHORT put_vtbl_off;
59 SHORT get_vtbl_off;
60 SHORT func_disp_idx;
61 USHORT argc;
62 USHORT default_value_cnt;
63 VARTYPE prop_vt;
64 VARTYPE *arg_types;
65 func_arg_info_t *arg_info;
66 } func_info_t;
68 struct dispex_data_t {
69 dispex_static_data_t *desc;
70 compat_mode_t compat_mode;
72 DWORD func_cnt;
73 DWORD func_size;
74 func_info_t *funcs;
75 func_info_t **name_table;
76 DWORD func_disp_cnt;
78 struct list entry;
81 typedef struct {
82 VARIANT var;
83 LPWSTR name;
84 DWORD flags;
85 } dynamic_prop_t;
87 #define DYNPROP_DELETED 0x01
89 typedef struct {
90 DispatchEx dispex;
91 IUnknown IUnknown_iface;
92 LONG ref;
93 DispatchEx *obj;
94 func_info_t *info;
95 } func_disp_t;
97 typedef struct {
98 func_disp_t *func_obj;
99 VARIANT val;
100 } func_obj_entry_t;
102 struct dispex_dynamic_data_t {
103 DWORD buf_size;
104 DWORD prop_cnt;
105 dynamic_prop_t *props;
106 func_obj_entry_t *func_disps;
109 #define DISPID_DYNPROP_0 0x50000000
110 #define DISPID_DYNPROP_MAX 0x5fffffff
112 #define FDEX_VERSION_MASK 0xf0000000
114 static ITypeLib *typelib, *typelib_private;
115 static ITypeInfo *typeinfos[LAST_tid];
116 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
118 static REFIID tid_ids[] = {
119 #define XIID(iface) &IID_ ## iface,
120 #define XDIID(iface) &DIID_ ## iface,
121 TID_LIST
122 NULL,
123 PRIVATE_TID_LIST
124 #undef XIID
125 #undef XDIID
128 static HRESULT load_typelib(void)
130 WCHAR module_path[MAX_PATH + 3];
131 HRESULT hres;
132 ITypeLib *tl;
133 DWORD len;
135 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
136 if(FAILED(hres)) {
137 ERR("LoadRegTypeLib failed: %08x\n", hres);
138 return hres;
141 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
142 ITypeLib_Release(tl);
144 len = GetModuleFileNameW(hInst, module_path, MAX_PATH + 1);
145 if (!len || len == MAX_PATH + 1)
147 ERR("Could not get module file name, len %u.\n", len);
148 return E_FAIL;
150 lstrcatW(module_path, L"\\1");
152 hres = LoadTypeLibEx(module_path, REGKIND_NONE, &tl);
153 if(FAILED(hres)) {
154 ERR("LoadTypeLibEx failed for private typelib: %08x\n", hres);
155 return hres;
158 if(InterlockedCompareExchangePointer((void**)&typelib_private, tl, NULL))
159 ITypeLib_Release(tl);
161 return S_OK;
164 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
166 HRESULT hres;
168 if (!typelib)
169 hres = load_typelib();
170 if (!typelib)
171 return hres;
173 if(!typeinfos[tid]) {
174 ITypeInfo *ti;
176 hres = ITypeLib_GetTypeInfoOfGuid(tid > LAST_public_tid ? typelib_private : typelib, tid_ids[tid], &ti);
177 if(FAILED(hres)) {
178 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_mshtml_guid(tid_ids[tid]), hres);
179 return hres;
182 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
183 ITypeInfo_Release(ti);
186 *typeinfo = typeinfos[tid];
187 return S_OK;
190 void release_typelib(void)
192 dispex_data_t *iter;
193 unsigned i, j;
195 while(!list_empty(&dispex_data_list)) {
196 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
197 list_remove(&iter->entry);
199 for(i = 0; i < iter->func_cnt; i++) {
200 if(iter->funcs[i].default_value_cnt && iter->funcs[i].arg_info) {
201 for(j = 0; j < iter->funcs[i].argc; j++)
202 VariantClear(&iter->funcs[i].arg_info[j].default_value);
204 heap_free(iter->funcs[i].arg_types);
205 heap_free(iter->funcs[i].arg_info);
206 SysFreeString(iter->funcs[i].name);
209 heap_free(iter->funcs);
210 heap_free(iter->name_table);
211 heap_free(iter);
214 if(!typelib)
215 return;
217 for(i=0; i < ARRAY_SIZE(typeinfos); i++)
218 if(typeinfos[i])
219 ITypeInfo_Release(typeinfos[i]);
221 ITypeLib_Release(typelib);
222 ITypeLib_Release(typelib_private);
223 DeleteCriticalSection(&cs_dispex_static_data);
226 HRESULT get_class_typeinfo(const CLSID *clsid, ITypeInfo **typeinfo)
228 HRESULT hres;
230 if (!typelib)
231 hres = load_typelib();
232 if (!typelib)
233 return hres;
235 hres = ITypeLib_GetTypeInfoOfGuid(typelib, clsid, typeinfo);
236 if (FAILED(hres))
237 hres = ITypeLib_GetTypeInfoOfGuid(typelib_private, clsid, typeinfo);
238 if(FAILED(hres))
239 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
240 return hres;
243 /* Not all argument types are supported yet */
244 #define BUILTIN_ARG_TYPES_SWITCH \
245 CASE_VT(VT_I2, INT16, V_I2); \
246 CASE_VT(VT_UI2, UINT16, V_UI2); \
247 CASE_VT(VT_I4, INT32, V_I4); \
248 CASE_VT(VT_UI4, UINT32, V_UI4); \
249 CASE_VT(VT_R4, float, V_R4); \
250 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
251 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH); \
252 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
254 /* List all types used by IDispatchEx-based properties */
255 #define BUILTIN_TYPES_SWITCH \
256 BUILTIN_ARG_TYPES_SWITCH; \
257 CASE_VT(VT_VARIANT, VARIANT, *); \
258 CASE_VT(VT_PTR, void*, V_BYREF); \
259 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
260 CASE_VT(VT_UI8, ULONGLONG, V_UI8)
262 static BOOL is_arg_type_supported(VARTYPE vt)
264 switch(vt) {
265 #define CASE_VT(x,a,b) case x: return TRUE
266 BUILTIN_ARG_TYPES_SWITCH;
267 #undef CASE_VT
269 return FALSE;
272 static void add_func_info(dispex_data_t *data, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti,
273 dispex_hook_invoke_t hook)
275 func_info_t *info;
276 BSTR name;
277 HRESULT hres;
279 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &name, NULL, NULL, NULL);
280 if(FAILED(hres)) {
281 WARN("GetDocumentation failed: %08x\n", hres);
282 return;
285 for(info = data->funcs; info < data->funcs+data->func_cnt; info++) {
286 if(info->id == desc->memid || !wcscmp(info->name, name)) {
287 if(info->tid != tid) {
288 SysFreeString(name);
289 return; /* Duplicated in other interface */
291 break;
295 TRACE("adding %s...\n", debugstr_w(name));
297 if(info == data->funcs+data->func_cnt) {
298 if(data->func_cnt == data->func_size)
299 data->funcs = heap_realloc_zero(data->funcs, (data->func_size <<= 1)*sizeof(func_info_t));
300 info = data->funcs+data->func_cnt;
302 data->func_cnt++;
304 info->id = desc->memid;
305 info->name = name;
306 info->tid = tid;
307 info->func_disp_idx = -1;
308 info->prop_vt = VT_EMPTY;
309 info->hook = hook;
310 }else {
311 SysFreeString(name);
314 if(desc->invkind & DISPATCH_METHOD) {
315 unsigned i;
317 info->func_disp_idx = data->func_disp_cnt++;
318 info->argc = desc->cParams;
320 assert(info->argc < MAX_ARGS);
321 assert(desc->funckind == FUNC_DISPATCH);
323 info->arg_info = heap_alloc_zero(sizeof(*info->arg_info) * info->argc);
324 if(!info->arg_info)
325 return;
327 info->prop_vt = desc->elemdescFunc.tdesc.vt;
328 if(info->prop_vt != VT_VOID && info->prop_vt != VT_PTR && !is_arg_type_supported(info->prop_vt)) {
329 TRACE("%s: return type %d\n", debugstr_w(info->name), info->prop_vt);
330 return; /* Fallback to ITypeInfo::Invoke */
333 info->arg_types = heap_alloc(sizeof(*info->arg_types) * (info->argc + (info->prop_vt == VT_VOID ? 0 : 1)));
334 if(!info->arg_types)
335 return;
337 for(i=0; i < info->argc; i++)
338 info->arg_types[i] = desc->lprgelemdescParam[i].tdesc.vt;
340 if(info->prop_vt == VT_PTR)
341 info->arg_types[info->argc] = VT_BYREF | VT_DISPATCH;
342 else if(info->prop_vt != VT_VOID)
343 info->arg_types[info->argc] = VT_BYREF | info->prop_vt;
345 if(desc->cParamsOpt) {
346 TRACE("%s: optional params\n", debugstr_w(info->name));
347 return; /* Fallback to ITypeInfo::Invoke */
350 for(i=0; i < info->argc; i++) {
351 TYPEDESC *tdesc = &desc->lprgelemdescParam[i].tdesc;
352 if(tdesc->vt == VT_PTR && tdesc->u.lptdesc->vt == VT_USERDEFINED) {
353 ITypeInfo *ref_type_info;
354 TYPEATTR *attr;
356 hres = ITypeInfo_GetRefTypeInfo(dti, tdesc->u.lptdesc->u.hreftype, &ref_type_info);
357 if(FAILED(hres)) {
358 ERR("Could not get referenced type info: %08x\n", hres);
359 return;
362 hres = ITypeInfo_GetTypeAttr(ref_type_info, &attr);
363 if(SUCCEEDED(hres)) {
364 assert(attr->typekind == TKIND_DISPATCH);
365 info->arg_info[i].iid = attr->guid;
366 ITypeInfo_ReleaseTypeAttr(ref_type_info, attr);
367 }else {
368 ERR("GetTypeAttr failed: %08x\n", hres);
370 ITypeInfo_Release(ref_type_info);
371 if(FAILED(hres))
372 return;
373 info->arg_types[i] = VT_DISPATCH;
374 }else if(!is_arg_type_supported(info->arg_types[i])) {
375 TRACE("%s: unsupported arg type %s\n", debugstr_w(info->name), debugstr_vt(info->arg_types[i]));
376 return; /* Fallback to ITypeInfo for unsupported arg types */
379 if(desc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
380 hres = VariantCopy(&info->arg_info[i].default_value,
381 &desc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
382 if(FAILED(hres)) {
383 ERR("Could not copy default value: %08x\n", hres);
384 return;
386 TRACE("%s param %d: default value %s\n", debugstr_w(info->name),
387 i, debugstr_variant(&info->arg_info[i].default_value));
388 info->default_value_cnt++;
392 assert(info->argc <= MAX_ARGS);
393 assert(desc->callconv == CC_STDCALL);
395 info->call_vtbl_off = desc->oVft/sizeof(void*);
396 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
397 VARTYPE vt = VT_EMPTY;
399 if(desc->invkind & DISPATCH_PROPERTYGET) {
400 vt = desc->elemdescFunc.tdesc.vt;
401 info->get_vtbl_off = desc->oVft/sizeof(void*);
403 if(desc->invkind & DISPATCH_PROPERTYPUT) {
404 assert(desc->cParams == 1);
405 vt = desc->lprgelemdescParam->tdesc.vt;
406 info->put_vtbl_off = desc->oVft/sizeof(void*);
409 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
410 info->prop_vt = vt;
414 static HRESULT process_interface(dispex_data_t *data, tid_t tid, ITypeInfo *disp_typeinfo, const dispex_hook_t *hooks)
416 unsigned i = 7; /* skip IDispatch functions */
417 ITypeInfo *typeinfo;
418 FUNCDESC *funcdesc;
419 HRESULT hres;
421 hres = get_typeinfo(tid, &typeinfo);
422 if(FAILED(hres))
423 return hres;
425 while(1) {
426 const dispex_hook_t *hook = NULL;
428 hres = ITypeInfo_GetFuncDesc(typeinfo, i++, &funcdesc);
429 if(FAILED(hres))
430 break;
432 if(hooks) {
433 for(hook = hooks; hook->dispid != DISPID_UNKNOWN; hook++) {
434 if(hook->dispid == funcdesc->memid)
435 break;
437 if(hook->dispid == DISPID_UNKNOWN)
438 hook = NULL;
441 if(!hook || hook->invoke) {
442 add_func_info(data, tid, funcdesc, disp_typeinfo ? disp_typeinfo : typeinfo,
443 hook ? hook->invoke : NULL);
446 ITypeInfo_ReleaseFuncDesc(typeinfo, funcdesc);
449 return S_OK;
452 void dispex_info_add_interface(dispex_data_t *info, tid_t tid, const dispex_hook_t *hooks)
454 HRESULT hres;
456 hres = process_interface(info, tid, NULL, hooks);
457 if(FAILED(hres))
458 ERR("process_interface failed: %08x\n", hres);
461 static int __cdecl dispid_cmp(const void *p1, const void *p2)
463 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
466 static int __cdecl func_name_cmp(const void *p1, const void *p2)
468 return wcsicmp((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
471 static dispex_data_t *preprocess_dispex_data(dispex_static_data_t *desc, compat_mode_t compat_mode)
473 const tid_t *tid;
474 dispex_data_t *data;
475 DWORD i;
476 ITypeInfo *dti;
477 HRESULT hres;
479 if(desc->disp_tid) {
480 hres = get_typeinfo(desc->disp_tid, &dti);
481 if(FAILED(hres)) {
482 ERR("Could not get disp type info: %08x\n", hres);
483 return NULL;
487 data = heap_alloc(sizeof(dispex_data_t));
488 if (!data) {
489 ERR("Out of memory\n");
490 return NULL;
492 data->desc = desc;
493 data->compat_mode = compat_mode;
494 data->func_cnt = 0;
495 data->func_disp_cnt = 0;
496 data->func_size = 16;
497 data->funcs = heap_alloc_zero(data->func_size*sizeof(func_info_t));
498 if (!data->funcs) {
499 heap_free (data);
500 ERR("Out of memory\n");
501 return NULL;
503 list_add_tail(&dispex_data_list, &data->entry);
505 if(desc->init_info)
506 desc->init_info(data, compat_mode);
508 for(tid = desc->iface_tids; *tid; tid++) {
509 hres = process_interface(data, *tid, dti, NULL);
510 if(FAILED(hres))
511 break;
514 if(!data->func_cnt) {
515 heap_free(data->funcs);
516 data->name_table = NULL;
517 data->funcs = NULL;
518 data->func_size = 0;
519 return data;
523 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
524 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
526 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
527 for(i=0; i < data->func_cnt; i++)
528 data->name_table[i] = data->funcs+i;
529 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
530 return data;
533 static int __cdecl id_cmp(const void *p1, const void *p2)
535 return *(const DISPID*)p1 - *(const DISPID*)p2;
538 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
540 unsigned i, func_cnt;
541 FUNCDESC *funcdesc;
542 ITypeInfo *ti;
543 TYPEATTR *attr;
544 DISPID *ids;
545 HRESULT hres;
547 hres = get_typeinfo(tid, &ti);
548 if(FAILED(hres))
549 return hres;
551 hres = ITypeInfo_GetTypeAttr(ti, &attr);
552 if(FAILED(hres)) {
553 ITypeInfo_Release(ti);
554 return hres;
557 func_cnt = attr->cFuncs;
558 ITypeInfo_ReleaseTypeAttr(ti, attr);
560 ids = heap_alloc(func_cnt*sizeof(DISPID));
561 if(!ids) {
562 ITypeInfo_Release(ti);
563 return E_OUTOFMEMORY;
566 for(i=0; i < func_cnt; i++) {
567 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
568 if(FAILED(hres))
569 break;
571 ids[i] = funcdesc->memid;
572 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
575 ITypeInfo_Release(ti);
576 if(FAILED(hres)) {
577 heap_free(ids);
578 return hres;
581 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
583 *ret_size = func_cnt;
584 *ret = ids;
585 return S_OK;
588 static inline BOOL is_custom_dispid(DISPID id)
590 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
593 static inline BOOL is_dynamic_dispid(DISPID id)
595 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
598 dispex_prop_type_t get_dispid_type(DISPID id)
600 if(is_dynamic_dispid(id))
601 return DISPEXPROP_DYNAMIC;
602 if(is_custom_dispid(id))
603 return DISPEXPROP_CUSTOM;
604 return DISPEXPROP_BUILTIN;
607 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
609 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
610 V_VT(dest) = VT_BSTR;
611 V_BSTR(dest) = NULL;
612 return S_OK;
615 return VariantCopy(dest, src);
618 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
620 if(This->dynamic_data)
621 return This->dynamic_data;
623 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
624 if(!This->dynamic_data)
625 return NULL;
627 if(This->info->desc->vtbl && This->info->desc->vtbl->populate_props)
628 This->info->desc->vtbl->populate_props(This);
630 return This->dynamic_data;
633 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
635 const BOOL alloc = flags & fdexNameEnsure;
636 dispex_dynamic_data_t *data;
637 dynamic_prop_t *prop;
639 data = get_dynamic_data(This);
640 if(!data)
641 return E_OUTOFMEMORY;
643 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
644 if(flags & fdexNameCaseInsensitive ? !wcsicmp(prop->name, name) : !wcscmp(prop->name, name)) {
645 if(prop->flags & DYNPROP_DELETED) {
646 if(!alloc)
647 return DISP_E_UNKNOWNNAME;
648 prop->flags &= ~DYNPROP_DELETED;
650 *ret = prop;
651 return S_OK;
655 if(!alloc)
656 return DISP_E_UNKNOWNNAME;
658 TRACE("creating dynamic prop %s\n", debugstr_w(name));
660 if(!data->buf_size) {
661 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
662 if(!data->props)
663 return E_OUTOFMEMORY;
664 data->buf_size = 4;
665 }else if(data->buf_size == data->prop_cnt) {
666 dynamic_prop_t *new_props;
668 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
669 if(!new_props)
670 return E_OUTOFMEMORY;
672 data->props = new_props;
673 data->buf_size <<= 1;
676 prop = data->props + data->prop_cnt;
678 prop->name = heap_strdupW(name);
679 if(!prop->name)
680 return E_OUTOFMEMORY;
682 VariantInit(&prop->var);
683 prop->flags = 0;
684 data->prop_cnt++;
685 *ret = prop;
686 return S_OK;
689 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
691 dynamic_prop_t *prop;
692 HRESULT hres;
694 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
695 if(FAILED(hres))
696 return hres;
698 *ret = &prop->var;
699 return S_OK;
702 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
704 dynamic_prop_t *prop;
705 HRESULT hres;
707 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
708 if(FAILED(hres))
709 return hres;
711 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
712 return S_OK;
715 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
716 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
718 HRESULT hres;
720 if(This->info->desc->vtbl && This->info->desc->vtbl->value)
721 return This->info->desc->vtbl->value(This, lcid, flags, params, res, ei, caller);
723 switch(flags) {
724 case DISPATCH_PROPERTYGET:
725 V_VT(res) = VT_BSTR;
726 hres = dispex_to_string(This, &V_BSTR(res));
727 if(FAILED(hres))
728 return hres;
729 break;
730 default:
731 FIXME("Unimplemented flags %x\n", flags);
732 return E_NOTIMPL;
735 return S_OK;
738 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
739 EXCEPINFO *ei)
741 DISPPARAMS params = {dp->rgvarg, NULL, dp->cArgs, 0};
742 ITypeInfo *ti;
743 IUnknown *unk;
744 UINT argerr=0;
745 HRESULT hres;
747 if(params.cArgs > func->argc) {
748 params.rgvarg += params.cArgs - func->argc;
749 params.cArgs = func->argc;
752 hres = get_typeinfo(func->tid, &ti);
753 if(FAILED(hres)) {
754 ERR("Could not get type info: %08x\n", hres);
755 return hres;
758 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
759 if(FAILED(hres)) {
760 ERR("Could not get iface %s: %08x\n", debugstr_mshtml_guid(tid_ids[func->tid]), hres);
761 return E_FAIL;
764 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, &params, res, ei, &argerr);
766 IUnknown_Release(unk);
767 return hres;
770 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
772 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
775 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
777 func_disp_t *This = impl_from_IUnknown(iface);
779 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
781 if(IsEqualGUID(&IID_IUnknown, riid)) {
782 *ppv = &This->IUnknown_iface;
783 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
784 return *ppv ? S_OK : E_NOINTERFACE;
785 }else {
786 *ppv = NULL;
787 return E_NOINTERFACE;
790 IUnknown_AddRef((IUnknown*)*ppv);
791 return S_OK;
794 static ULONG WINAPI Function_AddRef(IUnknown *iface)
796 func_disp_t *This = impl_from_IUnknown(iface);
797 LONG ref = InterlockedIncrement(&This->ref);
799 TRACE("(%p) ref=%d\n", This, ref);
801 return ref;
804 static ULONG WINAPI Function_Release(IUnknown *iface)
806 func_disp_t *This = impl_from_IUnknown(iface);
807 LONG ref = InterlockedDecrement(&This->ref);
809 TRACE("(%p) ref=%d\n", This, ref);
811 if(!ref) {
812 assert(!This->obj);
813 release_dispex(&This->dispex);
814 heap_free(This);
817 return ref;
820 static const IUnknownVtbl FunctionUnkVtbl = {
821 Function_QueryInterface,
822 Function_AddRef,
823 Function_Release
826 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
828 return CONTAINING_RECORD(iface, func_disp_t, dispex);
831 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
832 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
834 func_disp_t *This = impl_from_DispatchEx(dispex);
835 HRESULT hres;
837 switch(flags) {
838 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
839 if(!res)
840 return E_INVALIDARG;
841 /* fall through */
842 case DISPATCH_METHOD:
843 if(!This->obj)
844 return E_UNEXPECTED;
845 hres = dispex_call_builtin(This->obj, This->info->id, params, res, ei, caller);
846 break;
847 case DISPATCH_PROPERTYGET: {
848 unsigned name_len;
849 WCHAR *ptr;
850 BSTR str;
852 static const WCHAR func_prefixW[] =
853 {'\n','f','u','n','c','t','i','o','n',' '};
854 static const WCHAR func_suffixW[] =
855 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
857 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
858 if(!caller)
859 return E_ACCESSDENIED;
861 name_len = SysStringLen(This->info->name);
862 ptr = str = SysAllocStringLen(NULL, name_len + ARRAY_SIZE(func_prefixW) + ARRAY_SIZE(func_suffixW));
863 if(!str)
864 return E_OUTOFMEMORY;
866 memcpy(ptr, func_prefixW, sizeof(func_prefixW));
867 ptr += ARRAY_SIZE(func_prefixW);
869 memcpy(ptr, This->info->name, name_len*sizeof(WCHAR));
870 ptr += name_len;
872 memcpy(ptr, func_suffixW, sizeof(func_suffixW));
874 V_VT(res) = VT_BSTR;
875 V_BSTR(res) = str;
876 return S_OK;
878 default:
879 FIXME("Unimplemented flags %x\n", flags);
880 hres = E_NOTIMPL;
883 return hres;
886 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
887 function_value,
888 NULL,
889 NULL,
890 NULL
893 static const tid_t function_iface_tids[] = {0};
895 static dispex_static_data_t function_dispex = {
896 L"Function",
897 &function_dispex_vtbl,
898 NULL_tid,
899 function_iface_tids
902 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
904 func_disp_t *ret;
906 ret = heap_alloc_zero(sizeof(func_disp_t));
907 if(!ret)
908 return NULL;
910 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
911 init_dispatch(&ret->dispex, &ret->IUnknown_iface, &function_dispex, dispex_compat_mode(obj));
912 ret->ref = 1;
913 ret->obj = obj;
914 ret->info = info;
916 return ret;
919 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
920 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
922 DISPID named_arg = DISPID_THIS;
923 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
924 IDispatchEx *dispex;
925 HRESULT hres;
927 if(dp->cNamedArgs) {
928 FIXME("named args not supported\n");
929 return E_NOTIMPL;
932 new_dp.rgvarg = heap_alloc((dp->cArgs+1)*sizeof(VARIANTARG));
933 if(!new_dp.rgvarg)
934 return E_OUTOFMEMORY;
936 new_dp.cArgs = dp->cArgs+1;
937 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
939 V_VT(new_dp.rgvarg) = VT_DISPATCH;
940 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
942 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
943 TRACE(">>>\n");
944 if(SUCCEEDED(hres)) {
945 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
946 IDispatchEx_Release(dispex);
947 }else {
948 UINT err = 0;
949 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
951 if(SUCCEEDED(hres))
952 TRACE("<<< %s\n", debugstr_variant(res));
953 else
954 WARN("<<< %08x\n", hres);
956 heap_free(new_dp.rgvarg);
957 return hres;
960 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
962 dispex_dynamic_data_t *dynamic_data;
963 func_obj_entry_t *entry;
965 dynamic_data = get_dynamic_data(This);
966 if(!dynamic_data)
967 return E_OUTOFMEMORY;
969 if(!dynamic_data->func_disps) {
970 dynamic_data->func_disps = heap_alloc_zero(This->info->func_disp_cnt * sizeof(*dynamic_data->func_disps));
971 if(!dynamic_data->func_disps)
972 return E_OUTOFMEMORY;
975 entry = dynamic_data->func_disps + func->func_disp_idx;
976 if(!entry->func_obj) {
977 entry->func_obj = create_func_disp(This, func);
978 if(!entry->func_obj)
979 return E_OUTOFMEMORY;
981 IDispatchEx_AddRef(&entry->func_obj->dispex.IDispatchEx_iface);
982 V_VT(&entry->val) = VT_DISPATCH;
983 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
986 *ret = entry;
987 return S_OK;
990 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
992 int min, max, n;
994 min = 0;
995 max = data->func_cnt-1;
997 while(min <= max) {
998 n = (min+max)/2;
1000 if(data->funcs[n].id == id) {
1001 *ret = data->funcs+n;
1002 return S_OK;
1005 if(data->funcs[n].id < id)
1006 min = n+1;
1007 else
1008 max = n-1;
1011 WARN("invalid id %x\n", id);
1012 return DISP_E_UNKNOWNNAME;
1015 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
1017 int min, max, n, c;
1019 min = 0;
1020 max = This->info->func_cnt-1;
1022 while(min <= max) {
1023 n = (min+max)/2;
1025 c = wcsicmp(This->info->name_table[n]->name, name);
1026 if(!c) {
1027 if((grfdex & fdexNameCaseSensitive) && wcscmp(This->info->name_table[n]->name, name))
1028 break;
1030 *ret = This->info->name_table[n]->id;
1031 return S_OK;
1034 if(c > 0)
1035 max = n-1;
1036 else
1037 min = n+1;
1040 if(This->info->desc->vtbl && This->info->desc->vtbl->get_dispid) {
1041 HRESULT hres;
1043 hres = This->info->desc->vtbl->get_dispid(This, name, grfdex, ret);
1044 if(hres != DISP_E_UNKNOWNNAME)
1045 return hres;
1048 return DISP_E_UNKNOWNNAME;
1051 HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
1053 V_VT(dst) = VT_EMPTY;
1055 if(caller) {
1056 IVariantChangeType *change_type = NULL;
1057 HRESULT hres;
1059 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
1060 if(SUCCEEDED(hres)) {
1061 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
1062 IVariantChangeType_Release(change_type);
1063 return hres;
1067 switch(vt) {
1068 case VT_BOOL:
1069 if(V_VT(src) == VT_BSTR) {
1070 V_VT(dst) = VT_BOOL;
1071 V_BOOL(dst) = variant_bool(V_BSTR(src) && *V_BSTR(src));
1072 return S_OK;
1074 break;
1077 return VariantChangeType(dst, src, 0, vt);
1080 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
1082 IUnknown *iface;
1083 HRESULT hres;
1085 if(dp && dp->cArgs) {
1086 FIXME("cArgs %d\n", dp->cArgs);
1087 return E_NOTIMPL;
1090 assert(func->get_vtbl_off);
1092 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1093 if(SUCCEEDED(hres)) {
1094 switch(func->prop_vt) {
1095 #define CASE_VT(vt,type,access) \
1096 case vt: { \
1097 type val; \
1098 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
1099 if(SUCCEEDED(hres)) \
1100 access(res) = val; \
1102 break
1103 BUILTIN_TYPES_SWITCH;
1104 #undef CASE_VT
1105 default:
1106 FIXME("Unhandled vt %d\n", func->prop_vt);
1107 hres = E_NOTIMPL;
1109 IUnknown_Release(iface);
1112 if(FAILED(hres))
1113 return hres;
1115 if(func->prop_vt != VT_VARIANT)
1116 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
1117 return S_OK;
1120 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
1122 VARIANT *v, tmpv;
1123 IUnknown *iface;
1124 HRESULT hres;
1126 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1127 || dp->cNamedArgs > 1) {
1128 FIXME("invalid args\n");
1129 return E_INVALIDARG;
1132 if(!func->put_vtbl_off) {
1133 if(dispex_compat_mode(This) >= COMPAT_MODE_IE9) {
1134 WARN("No setter\n");
1135 return S_OK;
1137 FIXME("No setter\n");
1138 return E_FAIL;
1141 v = dp->rgvarg;
1142 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
1143 hres = change_type(&tmpv, v, func->prop_vt, caller);
1144 if(FAILED(hres))
1145 return hres;
1146 v = &tmpv;
1149 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1150 if(SUCCEEDED(hres)) {
1151 switch(func->prop_vt) {
1152 #define CASE_VT(vt,type,access) \
1153 case vt: \
1154 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1155 break
1156 BUILTIN_TYPES_SWITCH;
1157 #undef CASE_VT
1158 default:
1159 FIXME("Unimplemented vt %d\n", func->prop_vt);
1160 hres = E_NOTIMPL;
1163 IUnknown_Release(iface);
1166 if(v == &tmpv)
1167 VariantClear(v);
1168 return hres;
1171 static HRESULT invoke_builtin_function(DispatchEx *This, func_info_t *func, DISPPARAMS *dp,
1172 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1174 VARIANT arg_buf[MAX_ARGS], *arg_ptrs[MAX_ARGS], *arg, retv, ret_ref, vhres;
1175 unsigned i, nconv = 0;
1176 IUnknown *iface;
1177 HRESULT hres;
1179 if(func->hook) {
1180 hres = func->hook(This, DISPATCH_METHOD, dp, res, ei, caller);
1181 if(hres != S_FALSE)
1182 return hres;
1185 if(!func->call_vtbl_off)
1186 return typeinfo_invoke(This, func, DISPATCH_METHOD, dp, res, ei);
1188 if(dp->cArgs + func->default_value_cnt < func->argc) {
1189 FIXME("Invalid argument count (expected %u, got %u)\n", func->argc, dp->cArgs);
1190 return E_INVALIDARG;
1193 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1194 if(FAILED(hres))
1195 return hres;
1197 for(i=0; i < func->argc; i++) {
1198 BOOL own_value = FALSE;
1199 if(i >= dp->cArgs) {
1200 /* use default value */
1201 arg_ptrs[i] = &func->arg_info[i].default_value;
1202 continue;
1204 arg = dp->rgvarg+dp->cArgs-i-1;
1205 if(func->arg_types[i] == V_VT(arg)) {
1206 arg_ptrs[i] = arg;
1207 }else {
1208 hres = change_type(arg_buf+nconv, arg, func->arg_types[i], caller);
1209 if(FAILED(hres))
1210 break;
1211 arg_ptrs[i] = arg_buf + nconv++;
1212 own_value = TRUE;
1215 if(func->arg_types[i] == VT_DISPATCH && !IsEqualGUID(&func->arg_info[i].iid, &IID_NULL)
1216 && V_DISPATCH(arg_ptrs[i])) {
1217 IDispatch *iface;
1218 if(!own_value) {
1219 arg_buf[nconv] = *arg_ptrs[i];
1220 arg_ptrs[i] = arg_buf + nconv++;
1222 hres = IDispatch_QueryInterface(V_DISPATCH(arg_ptrs[i]), &func->arg_info[i].iid, (void**)&iface);
1223 if(FAILED(hres)) {
1224 WARN("Could not get %s iface: %08x\n", debugstr_guid(&func->arg_info[i].iid), hres);
1225 break;
1227 if(own_value)
1228 IDispatch_Release(V_DISPATCH(arg_ptrs[i]));
1229 V_DISPATCH(arg_ptrs[i]) = iface;
1233 if(SUCCEEDED(hres)) {
1234 if(func->prop_vt == VT_VOID) {
1235 V_VT(&retv) = VT_EMPTY;
1236 }else {
1237 V_VT(&retv) = func->prop_vt;
1238 arg_ptrs[func->argc] = &ret_ref;
1239 V_VT(&ret_ref) = VT_BYREF|func->prop_vt;
1241 switch(func->prop_vt) {
1242 #define CASE_VT(vt,type,access) \
1243 case vt: \
1244 V_BYREF(&ret_ref) = &access(&retv); \
1245 break
1246 BUILTIN_ARG_TYPES_SWITCH;
1247 #undef CASE_VT
1248 case VT_PTR:
1249 V_VT(&retv) = VT_DISPATCH;
1250 V_VT(&ret_ref) = VT_BYREF | VT_DISPATCH;
1251 V_BYREF(&ret_ref) = &V_DISPATCH(&retv);
1252 break;
1253 default:
1254 assert(0);
1258 V_VT(&vhres) = VT_ERROR;
1259 hres = DispCallFunc(iface, func->call_vtbl_off*sizeof(void*), CC_STDCALL, VT_ERROR,
1260 func->argc + (func->prop_vt == VT_VOID ? 0 : 1), func->arg_types, arg_ptrs, &vhres);
1263 while(nconv--)
1264 VariantClear(arg_buf+nconv);
1265 IUnknown_Release(iface);
1266 if(FAILED(hres))
1267 return hres;
1268 if(FAILED(V_ERROR(&vhres)))
1269 return V_ERROR(&vhres);
1271 if(res)
1272 *res = retv;
1273 else
1274 VariantClear(&retv);
1275 return V_ERROR(&vhres);
1278 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
1279 EXCEPINFO *ei, IServiceProvider *caller)
1281 HRESULT hres;
1283 switch(flags) {
1284 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1285 if(!res)
1286 return E_INVALIDARG;
1287 /* fall through */
1288 case DISPATCH_METHOD:
1289 if(This->dynamic_data && This->dynamic_data->func_disps
1290 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1291 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
1293 if(V_VT(&entry->val) != VT_DISPATCH) {
1294 FIXME("calling %s not supported\n", debugstr_variant(&entry->val));
1295 return E_NOTIMPL;
1298 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != V_DISPATCH(&entry->val)) {
1299 if(!V_DISPATCH(&entry->val)) {
1300 FIXME("Calling null\n");
1301 return E_FAIL;
1304 hres = invoke_disp_value(This, V_DISPATCH(&entry->val), 0, flags, dp, res, ei, NULL);
1305 break;
1309 hres = invoke_builtin_function(This, func, dp, res, ei, caller);
1310 break;
1311 case DISPATCH_PROPERTYGET: {
1312 func_obj_entry_t *entry;
1314 if(func->id == DISPID_VALUE) {
1315 BSTR ret;
1317 hres = dispex_to_string(This, &ret);
1318 if(FAILED(hres))
1319 return hres;
1321 V_VT(res) = VT_BSTR;
1322 V_BSTR(res) = ret;
1323 return S_OK;
1326 hres = get_func_obj_entry(This, func, &entry);
1327 if(FAILED(hres))
1328 return hres;
1330 V_VT(res) = VT_EMPTY;
1331 return VariantCopy(res, &entry->val);
1333 case DISPATCH_PROPERTYPUT: {
1334 func_obj_entry_t *entry;
1336 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1337 || dp->cNamedArgs > 1) {
1338 FIXME("invalid args\n");
1339 return E_INVALIDARG;
1343 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1344 * function property using InvokeEx, it's possible to do that from jscript.
1345 * Native probably uses some undocumented interface in this case, but it should
1346 * be fine for us to allow IDispatchEx handle that.
1348 hres = get_func_obj_entry(This, func, &entry);
1349 if(FAILED(hres))
1350 return hres;
1352 return VariantCopy(&entry->val, dp->rgvarg);
1354 default:
1355 FIXME("Unimplemented flags %x\n", flags);
1356 hres = E_NOTIMPL;
1359 return hres;
1362 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1363 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1365 func_info_t *func;
1366 HRESULT hres;
1368 hres = get_builtin_func(This->info, id, &func);
1369 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
1370 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1371 if(FAILED(hres))
1372 return hres;
1374 if(func->func_disp_idx != -1)
1375 return function_invoke(This, func, flags, dp, res, ei, caller);
1377 if(func->hook) {
1378 hres = func->hook(This, flags, dp, res, ei, caller);
1379 if(hres != S_FALSE)
1380 return hres;
1383 switch(flags) {
1384 case DISPATCH_PROPERTYPUT:
1385 if(res)
1386 V_VT(res) = VT_EMPTY;
1387 hres = builtin_propput(This, func, dp, caller);
1388 break;
1389 case DISPATCH_PROPERTYGET:
1390 hres = builtin_propget(This, func, dp, res);
1391 break;
1392 default:
1393 if(!func->get_vtbl_off) {
1394 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1395 }else {
1396 VARIANT v;
1398 hres = builtin_propget(This, func, NULL, &v);
1399 if(FAILED(hres))
1400 return hres;
1402 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD) || dp->cArgs) {
1403 if(V_VT(&v) != VT_DISPATCH) {
1404 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v), flags);
1405 VariantClear(&v);
1406 return E_FAIL;
1409 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1410 IDispatch_Release(V_DISPATCH(&v));
1411 }else if(res) {
1412 *res = v;
1413 }else {
1414 VariantClear(&v);
1419 return hres;
1422 HRESULT dispex_call_builtin(DispatchEx *dispex, DISPID id, DISPPARAMS *dp,
1423 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1425 func_info_t *func;
1426 HRESULT hres;
1428 hres = get_builtin_func(dispex->info, id, &func);
1429 if(FAILED(hres))
1430 return hres;
1432 return invoke_builtin_function(dispex, func, dp, res, ei, caller);
1435 HRESULT remove_attribute(DispatchEx *This, DISPID id, VARIANT_BOOL *success)
1437 switch(get_dispid_type(id)) {
1438 case DISPEXPROP_CUSTOM:
1439 FIXME("DISPEXPROP_CUSTOM not supported\n");
1440 return E_NOTIMPL;
1442 case DISPEXPROP_DYNAMIC: {
1443 DWORD idx = id - DISPID_DYNPROP_0;
1444 dynamic_prop_t *prop;
1446 prop = This->dynamic_data->props+idx;
1447 VariantClear(&prop->var);
1448 prop->flags |= DYNPROP_DELETED;
1449 *success = VARIANT_TRUE;
1450 return S_OK;
1452 case DISPEXPROP_BUILTIN: {
1453 VARIANT var;
1454 DISPPARAMS dp = {&var,NULL,1,0};
1455 func_info_t *func;
1456 HRESULT hres;
1458 hres = get_builtin_func(This->info, id, &func);
1459 if(FAILED(hres))
1460 return hres;
1462 /* For builtin functions, we set their value to the original function. */
1463 if(func->func_disp_idx != -1) {
1464 func_obj_entry_t *entry;
1466 if(!This->dynamic_data || !This->dynamic_data->func_disps
1467 || !This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1468 *success = VARIANT_FALSE;
1469 return S_OK;
1472 entry = This->dynamic_data->func_disps + func->func_disp_idx;
1473 if(V_VT(&entry->val) == VT_DISPATCH
1474 && V_DISPATCH(&entry->val) == (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface) {
1475 *success = VARIANT_FALSE;
1476 return S_OK;
1479 VariantClear(&entry->val);
1480 V_VT(&entry->val) = VT_DISPATCH;
1481 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
1482 IDispatch_AddRef(V_DISPATCH(&entry->val));
1483 *success = VARIANT_TRUE;
1484 return S_OK;
1486 *success = VARIANT_TRUE;
1488 V_VT(&var) = VT_EMPTY;
1489 hres = builtin_propput(This, func, &dp, NULL);
1490 if(FAILED(hres)) {
1491 VARIANT *ref;
1492 hres = dispex_get_dprop_ref(This, func->name, FALSE, &ref);
1493 if(FAILED(hres) || V_VT(ref) != VT_BSTR)
1494 *success = VARIANT_FALSE;
1495 else
1496 VariantClear(ref);
1498 return S_OK;
1500 default:
1501 assert(0);
1502 return E_FAIL;
1506 compat_mode_t dispex_compat_mode(DispatchEx *dispex)
1508 return dispex->info != dispex->info->desc->delayed_init_info
1509 ? dispex->info->compat_mode
1510 : dispex->info->desc->vtbl->get_compat_mode(dispex);
1513 HRESULT dispex_to_string(DispatchEx *dispex, BSTR *ret)
1515 static const WCHAR prefix[8] = L"[object ";
1516 static const WCHAR suffix[] = L"]";
1517 WCHAR buf[ARRAY_SIZE(prefix) + 28 + ARRAY_SIZE(suffix)], *p = buf;
1518 compat_mode_t compat_mode = dispex_compat_mode(dispex);
1519 const WCHAR *name = dispex->info->desc->name;
1520 unsigned len;
1522 if(!ret)
1523 return E_INVALIDARG;
1525 memcpy(p, prefix, sizeof(prefix));
1526 p += ARRAY_SIZE(prefix);
1527 if(compat_mode < COMPAT_MODE_IE9)
1528 p--;
1529 else {
1530 len = wcslen(name);
1531 assert(len <= 28);
1532 memcpy(p, name, len * sizeof(WCHAR));
1533 p += len;
1535 memcpy(p, suffix, sizeof(suffix));
1537 *ret = SysAllocString(buf);
1538 return *ret ? S_OK : E_OUTOFMEMORY;
1541 static dispex_data_t *ensure_dispex_info(dispex_static_data_t *desc, compat_mode_t compat_mode)
1543 if(!desc->info_cache[compat_mode]) {
1544 EnterCriticalSection(&cs_dispex_static_data);
1545 if(!desc->info_cache[compat_mode])
1546 desc->info_cache[compat_mode] = preprocess_dispex_data(desc, compat_mode);
1547 LeaveCriticalSection(&cs_dispex_static_data);
1549 return desc->info_cache[compat_mode];
1552 static BOOL ensure_real_info(DispatchEx *dispex)
1554 if(dispex->info != dispex->info->desc->delayed_init_info)
1555 return TRUE;
1557 dispex->info = ensure_dispex_info(dispex->info->desc, dispex_compat_mode(dispex));
1558 return dispex->info != NULL;
1561 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1563 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1566 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1568 DispatchEx *This = impl_from_IDispatchEx(iface);
1570 return IUnknown_QueryInterface(This->outer, riid, ppv);
1573 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1575 DispatchEx *This = impl_from_IDispatchEx(iface);
1577 return IUnknown_AddRef(This->outer);
1580 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1582 DispatchEx *This = impl_from_IDispatchEx(iface);
1584 return IUnknown_Release(This->outer);
1587 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1589 DispatchEx *This = impl_from_IDispatchEx(iface);
1591 TRACE("(%p)->(%p)\n", This, pctinfo);
1593 *pctinfo = 1;
1594 return S_OK;
1597 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1598 LCID lcid, ITypeInfo **ppTInfo)
1600 DispatchEx *This = impl_from_IDispatchEx(iface);
1601 HRESULT hres;
1603 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1605 hres = get_typeinfo(This->info->desc->disp_tid, ppTInfo);
1606 if(FAILED(hres))
1607 return hres;
1609 ITypeInfo_AddRef(*ppTInfo);
1610 return S_OK;
1613 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1614 LPOLESTR *rgszNames, UINT cNames,
1615 LCID lcid, DISPID *rgDispId)
1617 DispatchEx *This = impl_from_IDispatchEx(iface);
1618 UINT i;
1619 HRESULT hres;
1621 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1622 lcid, rgDispId);
1624 for(i=0; i < cNames; i++) {
1625 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1626 if(FAILED(hres))
1627 return hres;
1630 return S_OK;
1633 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1634 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1635 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1637 DispatchEx *This = impl_from_IDispatchEx(iface);
1639 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1640 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1642 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1643 pVarResult, pExcepInfo, NULL);
1646 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1648 DispatchEx *This = impl_from_IDispatchEx(iface);
1649 dynamic_prop_t *dprop;
1650 HRESULT hres;
1652 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1654 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1655 FIXME("Unsupported grfdex %x\n", grfdex);
1657 if(!ensure_real_info(This))
1658 return E_OUTOFMEMORY;
1660 hres = get_builtin_id(This, bstrName, grfdex, pid);
1661 if(hres != DISP_E_UNKNOWNNAME)
1662 return hres;
1664 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1665 if(FAILED(hres))
1666 return hres;
1668 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1669 return S_OK;
1672 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1673 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1675 DispatchEx *This = impl_from_IDispatchEx(iface);
1676 HRESULT hres;
1678 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1680 if(!ensure_real_info(This))
1681 return E_OUTOFMEMORY;
1683 if(wFlags == (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF))
1684 wFlags = DISPATCH_PROPERTYPUT;
1686 switch(get_dispid_type(id)) {
1687 case DISPEXPROP_CUSTOM:
1688 if(!This->info->desc->vtbl || !This->info->desc->vtbl->invoke)
1689 return DISP_E_UNKNOWNNAME;
1690 return This->info->desc->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1692 case DISPEXPROP_DYNAMIC: {
1693 DWORD idx = id - DISPID_DYNPROP_0;
1694 dynamic_prop_t *prop;
1696 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1697 return DISP_E_UNKNOWNNAME;
1699 prop = This->dynamic_data->props+idx;
1701 switch(wFlags) {
1702 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1703 if(!pvarRes)
1704 return E_INVALIDARG;
1705 /* fall through */
1706 case DISPATCH_METHOD:
1707 if(V_VT(&prop->var) != VT_DISPATCH) {
1708 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1709 return E_NOTIMPL;
1712 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1713 case DISPATCH_PROPERTYGET:
1714 if(prop->flags & DYNPROP_DELETED)
1715 return DISP_E_UNKNOWNNAME;
1716 V_VT(pvarRes) = VT_EMPTY;
1717 return variant_copy(pvarRes, &prop->var);
1718 case DISPATCH_PROPERTYPUT:
1719 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1720 || pdp->cNamedArgs > 1) {
1721 FIXME("invalid args\n");
1722 return E_INVALIDARG;
1725 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1726 VariantClear(&prop->var);
1727 hres = variant_copy(&prop->var, pdp->rgvarg);
1728 if(FAILED(hres))
1729 return hres;
1731 prop->flags &= ~DYNPROP_DELETED;
1732 return S_OK;
1733 default:
1734 FIXME("unhandled wFlags %x\n", wFlags);
1735 return E_NOTIMPL;
1738 case DISPEXPROP_BUILTIN:
1739 if(wFlags == DISPATCH_CONSTRUCT) {
1740 if(id == DISPID_VALUE) {
1741 if(This->info->desc->vtbl && This->info->desc->vtbl->value) {
1742 return This->info->desc->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1744 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1745 return E_FAIL;
1747 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1748 return E_FAIL;
1751 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1752 default:
1753 assert(0);
1754 return E_FAIL;
1758 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR name, DWORD grfdex)
1760 DispatchEx *This = impl_from_IDispatchEx(iface);
1761 DISPID id;
1762 HRESULT hres;
1764 TRACE("(%p)->(%s %x)\n", This, debugstr_w(name), grfdex);
1766 if(dispex_compat_mode(This) < COMPAT_MODE_IE8) {
1767 /* Not implemented by IE */
1768 return E_NOTIMPL;
1771 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, name, grfdex & ~fdexNameEnsure, &id);
1772 if(FAILED(hres)) {
1773 TRACE("property %s not found\n", debugstr_w(name));
1774 return dispex_compat_mode(This) < COMPAT_MODE_IE9 ? hres : S_OK;
1777 return IDispatchEx_DeleteMemberByDispID(&This->IDispatchEx_iface, id);
1780 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1782 DispatchEx *This = impl_from_IDispatchEx(iface);
1784 TRACE("(%p)->(%x)\n", This, id);
1786 if(dispex_compat_mode(This) < COMPAT_MODE_IE8) {
1787 /* Not implemented by IE */
1788 return E_NOTIMPL;
1791 if(is_dynamic_dispid(id)) {
1792 DWORD idx = id - DISPID_DYNPROP_0;
1793 dynamic_prop_t *prop;
1795 if(!get_dynamic_data(This) || idx >= This->dynamic_data->prop_cnt)
1796 return S_OK;
1798 prop = This->dynamic_data->props + idx;
1799 VariantClear(&prop->var);
1800 prop->flags |= DYNPROP_DELETED;
1801 return S_OK;
1804 return S_OK;
1807 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1809 DispatchEx *This = impl_from_IDispatchEx(iface);
1810 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1811 return E_NOTIMPL;
1814 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1816 DispatchEx *This = impl_from_IDispatchEx(iface);
1817 func_info_t *func;
1818 HRESULT hres;
1820 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1822 if(!ensure_real_info(This))
1823 return E_OUTOFMEMORY;
1825 if(is_dynamic_dispid(id)) {
1826 DWORD idx = id - DISPID_DYNPROP_0;
1828 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1829 return DISP_E_UNKNOWNNAME;
1831 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1832 if(!*pbstrName)
1833 return E_OUTOFMEMORY;
1835 return S_OK;
1838 hres = get_builtin_func(This->info, id, &func);
1839 if(FAILED(hres))
1840 return hres;
1842 *pbstrName = SysAllocString(func->name);
1843 if(!*pbstrName)
1844 return E_OUTOFMEMORY;
1845 return S_OK;
1848 static HRESULT next_dynamic_id(DispatchEx *dispex, DWORD idx, DISPID *ret_id)
1850 while(idx < dispex->dynamic_data->prop_cnt && dispex->dynamic_data->props[idx].flags & DYNPROP_DELETED)
1851 idx++;
1853 if(idx == dispex->dynamic_data->prop_cnt) {
1854 *ret_id = DISPID_STARTENUM;
1855 return S_FALSE;
1858 *ret_id = DISPID_DYNPROP_0+idx;
1859 return S_OK;
1862 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1864 DispatchEx *This = impl_from_IDispatchEx(iface);
1865 func_info_t *func;
1866 HRESULT hres;
1868 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1870 if(!ensure_real_info(This))
1871 return E_OUTOFMEMORY;
1873 if(is_dynamic_dispid(id)) {
1874 DWORD idx = id - DISPID_DYNPROP_0;
1876 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1877 return DISP_E_UNKNOWNNAME;
1879 return next_dynamic_id(This, idx+1, pid);
1882 if(id == DISPID_STARTENUM) {
1883 func = This->info->funcs;
1884 }else {
1885 hres = get_builtin_func(This->info, id, &func);
1886 if(FAILED(hres))
1887 return hres;
1888 func++;
1891 while(func < This->info->funcs + This->info->func_cnt) {
1892 /* FIXME: Skip hidden properties */
1893 if(func->func_disp_idx == -1) {
1894 *pid = func->id;
1895 return S_OK;
1897 func++;
1900 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt)
1901 return next_dynamic_id(This, 0, pid);
1903 *pid = DISPID_STARTENUM;
1904 return S_FALSE;
1907 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1909 DispatchEx *This = impl_from_IDispatchEx(iface);
1910 FIXME("(%p)->(%p)\n", This, ppunk);
1911 return E_NOTIMPL;
1914 static IDispatchExVtbl DispatchExVtbl = {
1915 DispatchEx_QueryInterface,
1916 DispatchEx_AddRef,
1917 DispatchEx_Release,
1918 DispatchEx_GetTypeInfoCount,
1919 DispatchEx_GetTypeInfo,
1920 DispatchEx_GetIDsOfNames,
1921 DispatchEx_Invoke,
1922 DispatchEx_GetDispID,
1923 DispatchEx_InvokeEx,
1924 DispatchEx_DeleteMemberByName,
1925 DispatchEx_DeleteMemberByDispID,
1926 DispatchEx_GetMemberProperties,
1927 DispatchEx_GetMemberName,
1928 DispatchEx_GetNextDispID,
1929 DispatchEx_GetNameSpaceParent
1932 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1934 if(IsEqualGUID(&IID_IDispatch, riid))
1935 *ppv = &This->IDispatchEx_iface;
1936 else if(IsEqualGUID(&IID_IDispatchEx, riid))
1937 *ppv = &This->IDispatchEx_iface;
1938 else if(IsEqualGUID(&IID_IDispatchJS, riid))
1939 *ppv = NULL;
1940 else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid))
1941 *ppv = NULL;
1942 else if(IsEqualGUID(&IID_IMarshal, riid))
1943 *ppv = NULL;
1944 else if(IsEqualGUID(&IID_IManagedObject, riid))
1945 *ppv = NULL;
1946 else
1947 return FALSE;
1949 if(*ppv)
1950 IUnknown_AddRef((IUnknown*)*ppv);
1951 return TRUE;
1954 void dispex_traverse(DispatchEx *This, nsCycleCollectionTraversalCallback *cb)
1956 dynamic_prop_t *prop;
1958 if(!This->dynamic_data)
1959 return;
1961 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1962 if(V_VT(&prop->var) == VT_DISPATCH)
1963 note_cc_edge((nsISupports*)V_DISPATCH(&prop->var), "dispex_data", cb);
1966 /* FIXME: Traverse func_disps */
1969 void dispex_unlink(DispatchEx *This)
1971 dynamic_prop_t *prop;
1973 if(!This->dynamic_data)
1974 return;
1976 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1977 if(V_VT(&prop->var) == VT_DISPATCH) {
1978 V_VT(&prop->var) = VT_EMPTY;
1979 IDispatch_Release(V_DISPATCH(&prop->var));
1980 }else {
1981 VariantClear(&prop->var);
1986 const void *dispex_get_vtbl(DispatchEx *dispex)
1988 return dispex->info->desc->vtbl;
1991 void release_dispex(DispatchEx *This)
1993 dynamic_prop_t *prop;
1995 if(!This->dynamic_data)
1996 return;
1998 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1999 VariantClear(&prop->var);
2000 heap_free(prop->name);
2003 heap_free(This->dynamic_data->props);
2005 if(This->dynamic_data->func_disps) {
2006 func_obj_entry_t *iter;
2008 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps + This->info->func_disp_cnt; iter++) {
2009 if(iter->func_obj) {
2010 iter->func_obj->obj = NULL;
2011 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
2013 VariantClear(&iter->val);
2016 heap_free(This->dynamic_data->func_disps);
2019 heap_free(This->dynamic_data);
2022 void init_dispatch(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data, compat_mode_t compat_mode)
2024 assert(compat_mode < COMPAT_MODE_CNT);
2026 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
2027 dispex->outer = outer;
2028 dispex->dynamic_data = NULL;
2030 if(data->vtbl && data->vtbl->get_compat_mode) {
2031 /* delayed init */
2032 if(!data->delayed_init_info) {
2033 EnterCriticalSection(&cs_dispex_static_data);
2034 if(!data->delayed_init_info) {
2035 dispex_data_t *info = heap_alloc_zero(sizeof(*data->delayed_init_info));
2036 if(info) {
2037 info->desc = data;
2038 data->delayed_init_info = info;
2041 LeaveCriticalSection(&cs_dispex_static_data);
2043 dispex->info = data->delayed_init_info;
2044 }else {
2045 dispex->info = ensure_dispex_info(data, compat_mode);