shcore: Use CRT allocation functions.
[wine.git] / dlls / mshtml / dispex.c
blobb5d8ac83346bbe0bef637cc6372719da0d5c8cd8
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 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "ole2.h"
26 #include "mscoree.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34 #define MAX_ARGS 16
36 static ExternalCycleCollectionParticipant dispex_ccp;
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
88 #define DYNPROP_HIDDEN 0x02
90 typedef struct {
91 DispatchEx dispex;
92 IUnknown IUnknown_iface;
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: %08lx\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 %lu.\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: %08lx\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: %08lx\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 free(iter->funcs[i].arg_types);
205 free(iter->funcs[i].arg_info);
206 SysFreeString(iter->funcs[i].name);
209 free(iter->funcs);
210 free(iter->name_table);
211 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: %08lx\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, const WCHAR *name_override)
275 func_info_t *info;
276 BSTR name;
277 HRESULT hres;
279 if(name_override)
280 name = SysAllocString(name_override);
281 else {
282 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &name, NULL, NULL, NULL);
283 if(FAILED(hres)) {
284 WARN("GetDocumentation failed: %08lx\n", hres);
285 return;
289 for(info = data->funcs; info < data->funcs+data->func_cnt; info++) {
290 if(info->id == desc->memid || !wcscmp(info->name, name)) {
291 if(info->tid != tid) {
292 SysFreeString(name);
293 return; /* Duplicated in other interface */
295 break;
299 TRACE("adding %s...\n", debugstr_w(name));
301 if(info == data->funcs+data->func_cnt) {
302 if(data->func_cnt == data->func_size) {
303 info = realloc(data->funcs, data->func_size * 2 * sizeof(func_info_t));
304 if(!info) {
305 SysFreeString(name);
306 return;
308 memset(info + data->func_size, 0, data->func_size * sizeof(func_info_t));
309 data->funcs = info;
310 data->func_size *= 2;
312 info = data->funcs+data->func_cnt;
314 data->func_cnt++;
316 info->id = desc->memid;
317 info->name = name;
318 info->tid = tid;
319 info->func_disp_idx = -1;
320 info->prop_vt = VT_EMPTY;
321 info->hook = hook;
322 }else {
323 SysFreeString(name);
326 if(desc->invkind & DISPATCH_METHOD) {
327 unsigned i;
329 info->func_disp_idx = data->func_disp_cnt++;
330 info->argc = desc->cParams;
332 assert(info->argc < MAX_ARGS);
333 assert(desc->funckind == FUNC_DISPATCH);
335 info->arg_info = calloc(info->argc, sizeof(*info->arg_info));
336 if(!info->arg_info)
337 return;
339 info->prop_vt = desc->elemdescFunc.tdesc.vt;
340 if(info->prop_vt != VT_VOID && info->prop_vt != VT_PTR && !is_arg_type_supported(info->prop_vt)) {
341 TRACE("%s: return type %d\n", debugstr_w(info->name), info->prop_vt);
342 return; /* Fallback to ITypeInfo::Invoke */
345 info->arg_types = malloc(sizeof(*info->arg_types) * (info->argc + (info->prop_vt == VT_VOID ? 0 : 1)));
346 if(!info->arg_types)
347 return;
349 for(i=0; i < info->argc; i++)
350 info->arg_types[i] = desc->lprgelemdescParam[i].tdesc.vt;
352 if(info->prop_vt == VT_PTR)
353 info->arg_types[info->argc] = VT_BYREF | VT_DISPATCH;
354 else if(info->prop_vt != VT_VOID)
355 info->arg_types[info->argc] = VT_BYREF | info->prop_vt;
357 if(desc->cParamsOpt) {
358 TRACE("%s: optional params\n", debugstr_w(info->name));
359 return; /* Fallback to ITypeInfo::Invoke */
362 for(i=0; i < info->argc; i++) {
363 TYPEDESC *tdesc = &desc->lprgelemdescParam[i].tdesc;
364 if(tdesc->vt == VT_PTR && tdesc->lptdesc->vt == VT_USERDEFINED) {
365 ITypeInfo *ref_type_info;
366 TYPEATTR *attr;
368 hres = ITypeInfo_GetRefTypeInfo(dti, tdesc->lptdesc->hreftype, &ref_type_info);
369 if(FAILED(hres)) {
370 ERR("Could not get referenced type info: %08lx\n", hres);
371 return;
374 hres = ITypeInfo_GetTypeAttr(ref_type_info, &attr);
375 if(SUCCEEDED(hres)) {
376 assert(attr->typekind == TKIND_DISPATCH);
377 info->arg_info[i].iid = attr->guid;
378 ITypeInfo_ReleaseTypeAttr(ref_type_info, attr);
379 }else {
380 ERR("GetTypeAttr failed: %08lx\n", hres);
382 ITypeInfo_Release(ref_type_info);
383 if(FAILED(hres))
384 return;
385 info->arg_types[i] = VT_DISPATCH;
386 }else if(!is_arg_type_supported(info->arg_types[i])) {
387 TRACE("%s: unsupported arg type %s\n", debugstr_w(info->name), debugstr_vt(info->arg_types[i]));
388 return; /* Fallback to ITypeInfo for unsupported arg types */
391 if(desc->lprgelemdescParam[i].paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
392 hres = VariantCopy(&info->arg_info[i].default_value,
393 &desc->lprgelemdescParam[i].paramdesc.pparamdescex->varDefaultValue);
394 if(FAILED(hres)) {
395 ERR("Could not copy default value: %08lx\n", hres);
396 return;
398 TRACE("%s param %d: default value %s\n", debugstr_w(info->name),
399 i, debugstr_variant(&info->arg_info[i].default_value));
400 info->default_value_cnt++;
404 assert(info->argc <= MAX_ARGS);
405 assert(desc->callconv == CC_STDCALL);
407 info->call_vtbl_off = desc->oVft/sizeof(void*);
408 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
409 VARTYPE vt = VT_EMPTY;
411 if(desc->wFuncFlags & FUNCFLAG_FHIDDEN)
412 info->func_disp_idx = -2;
414 if(desc->invkind & DISPATCH_PROPERTYGET) {
415 vt = desc->elemdescFunc.tdesc.vt;
416 info->get_vtbl_off = desc->oVft/sizeof(void*);
418 if(desc->invkind & DISPATCH_PROPERTYPUT) {
419 assert(desc->cParams == 1);
420 vt = desc->lprgelemdescParam->tdesc.vt;
421 info->put_vtbl_off = desc->oVft/sizeof(void*);
424 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
425 info->prop_vt = vt;
429 static HRESULT process_interface(dispex_data_t *data, tid_t tid, ITypeInfo *disp_typeinfo, const dispex_hook_t *hooks)
431 unsigned i = 7; /* skip IDispatch functions */
432 ITypeInfo *typeinfo;
433 FUNCDESC *funcdesc;
434 HRESULT hres;
436 hres = get_typeinfo(tid, &typeinfo);
437 if(FAILED(hres))
438 return hres;
440 while(1) {
441 const dispex_hook_t *hook = NULL;
443 hres = ITypeInfo_GetFuncDesc(typeinfo, i++, &funcdesc);
444 if(FAILED(hres))
445 break;
447 if(hooks) {
448 for(hook = hooks; hook->dispid != DISPID_UNKNOWN; hook++) {
449 if(hook->dispid == funcdesc->memid)
450 break;
452 if(hook->dispid == DISPID_UNKNOWN)
453 hook = NULL;
456 if(!hook || hook->invoke || hook->name) {
457 add_func_info(data, tid, funcdesc, disp_typeinfo ? disp_typeinfo : typeinfo,
458 hook ? hook->invoke : NULL, hook ? hook->name : NULL);
461 ITypeInfo_ReleaseFuncDesc(typeinfo, funcdesc);
464 return S_OK;
467 void dispex_info_add_interface(dispex_data_t *info, tid_t tid, const dispex_hook_t *hooks)
469 HRESULT hres;
471 hres = process_interface(info, tid, NULL, hooks);
472 if(FAILED(hres))
473 ERR("process_interface failed: %08lx\n", hres);
476 static int __cdecl dispid_cmp(const void *p1, const void *p2)
478 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
481 static int __cdecl func_name_cmp(const void *p1, const void *p2)
483 return wcsicmp((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
486 static dispex_data_t *preprocess_dispex_data(dispex_static_data_t *desc, compat_mode_t compat_mode)
488 const tid_t *tid;
489 dispex_data_t *data;
490 DWORD i;
491 ITypeInfo *dti;
492 HRESULT hres;
494 if(desc->disp_tid) {
495 hres = get_typeinfo(desc->disp_tid, &dti);
496 if(FAILED(hres)) {
497 ERR("Could not get disp type info: %08lx\n", hres);
498 return NULL;
502 data = malloc(sizeof(dispex_data_t));
503 if (!data) {
504 ERR("Out of memory\n");
505 return NULL;
507 data->desc = desc;
508 data->compat_mode = compat_mode;
509 data->func_cnt = 0;
510 data->func_disp_cnt = 0;
511 data->func_size = 16;
512 data->funcs = calloc(data->func_size, sizeof(func_info_t));
513 if (!data->funcs) {
514 free(data);
515 ERR("Out of memory\n");
516 return NULL;
518 list_add_tail(&dispex_data_list, &data->entry);
520 if(desc->init_info)
521 desc->init_info(data, compat_mode);
523 for(tid = desc->iface_tids; *tid; tid++) {
524 hres = process_interface(data, *tid, dti, NULL);
525 if(FAILED(hres))
526 break;
529 if(!data->func_cnt) {
530 free(data->funcs);
531 data->name_table = NULL;
532 data->funcs = NULL;
533 data->func_size = 0;
534 return data;
538 data->funcs = realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
539 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
541 data->name_table = malloc(data->func_cnt * sizeof(func_info_t*));
542 for(i=0; i < data->func_cnt; i++)
543 data->name_table[i] = data->funcs+i;
544 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
545 return data;
548 static int __cdecl id_cmp(const void *p1, const void *p2)
550 return *(const DISPID*)p1 - *(const DISPID*)p2;
553 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
555 unsigned i, func_cnt;
556 FUNCDESC *funcdesc;
557 ITypeInfo *ti;
558 TYPEATTR *attr;
559 DISPID *ids;
560 HRESULT hres;
562 hres = get_typeinfo(tid, &ti);
563 if(FAILED(hres))
564 return hres;
566 hres = ITypeInfo_GetTypeAttr(ti, &attr);
567 if(FAILED(hres)) {
568 ITypeInfo_Release(ti);
569 return hres;
572 func_cnt = attr->cFuncs;
573 ITypeInfo_ReleaseTypeAttr(ti, attr);
575 ids = malloc(func_cnt * sizeof(DISPID));
576 if(!ids) {
577 ITypeInfo_Release(ti);
578 return E_OUTOFMEMORY;
581 for(i=0; i < func_cnt; i++) {
582 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
583 if(FAILED(hres))
584 break;
586 ids[i] = funcdesc->memid;
587 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
590 ITypeInfo_Release(ti);
591 if(FAILED(hres)) {
592 free(ids);
593 return hres;
596 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
598 *ret_size = func_cnt;
599 *ret = ids;
600 return S_OK;
603 static inline BOOL is_custom_dispid(DISPID id)
605 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
608 static inline BOOL is_dynamic_dispid(DISPID id)
610 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
613 dispex_prop_type_t get_dispid_type(DISPID id)
615 if(is_dynamic_dispid(id))
616 return DISPEXPROP_DYNAMIC;
617 if(is_custom_dispid(id))
618 return DISPEXPROP_CUSTOM;
619 return DISPEXPROP_BUILTIN;
622 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
624 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
625 V_VT(dest) = VT_BSTR;
626 V_BSTR(dest) = NULL;
627 return S_OK;
630 return VariantCopy(dest, src);
633 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
635 if(This->dynamic_data)
636 return This->dynamic_data;
638 This->dynamic_data = calloc(1, sizeof(dispex_dynamic_data_t));
639 if(!This->dynamic_data)
640 return NULL;
642 if(This->info->desc->vtbl->populate_props)
643 This->info->desc->vtbl->populate_props(This);
645 return This->dynamic_data;
648 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
650 const BOOL alloc = flags & fdexNameEnsure;
651 dispex_dynamic_data_t *data;
652 dynamic_prop_t *prop;
654 data = get_dynamic_data(This);
655 if(!data)
656 return E_OUTOFMEMORY;
658 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
659 if(flags & fdexNameCaseInsensitive ? !wcsicmp(prop->name, name) : !wcscmp(prop->name, name)) {
660 if(prop->flags & DYNPROP_DELETED) {
661 if(!alloc)
662 return DISP_E_UNKNOWNNAME;
663 prop->flags &= ~DYNPROP_DELETED;
665 *ret = prop;
666 return S_OK;
670 if(!alloc)
671 return DISP_E_UNKNOWNNAME;
673 TRACE("creating dynamic prop %s\n", debugstr_w(name));
675 if(!data->buf_size) {
676 data->props = malloc(sizeof(dynamic_prop_t) * 4);
677 if(!data->props)
678 return E_OUTOFMEMORY;
679 data->buf_size = 4;
680 }else if(data->buf_size == data->prop_cnt) {
681 dynamic_prop_t *new_props;
683 new_props = realloc(data->props, sizeof(dynamic_prop_t) * (data->buf_size << 1));
684 if(!new_props)
685 return E_OUTOFMEMORY;
687 data->props = new_props;
688 data->buf_size <<= 1;
691 prop = data->props + data->prop_cnt;
693 prop->name = wcsdup(name);
694 if(!prop->name)
695 return E_OUTOFMEMORY;
697 VariantInit(&prop->var);
698 prop->flags = 0;
699 data->prop_cnt++;
700 *ret = prop;
701 return S_OK;
704 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
706 dynamic_prop_t *prop;
707 HRESULT hres;
709 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
710 if(FAILED(hres))
711 return hres;
713 if(alloc)
714 prop->flags |= DYNPROP_HIDDEN;
715 *ret = &prop->var;
716 return S_OK;
719 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, BOOL hidden, DISPID *id)
721 dynamic_prop_t *prop;
722 HRESULT hres;
724 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
725 if(FAILED(hres))
726 return hres;
728 if(hidden)
729 prop->flags |= DYNPROP_HIDDEN;
730 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
731 return S_OK;
734 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
735 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
737 HRESULT hres;
739 if(This->info->desc->vtbl->value)
740 return This->info->desc->vtbl->value(This, lcid, flags, params, res, ei, caller);
742 switch(flags) {
743 case DISPATCH_PROPERTYGET:
744 V_VT(res) = VT_BSTR;
745 hres = dispex_to_string(This, &V_BSTR(res));
746 if(FAILED(hres))
747 return hres;
748 break;
749 default:
750 FIXME("Unimplemented flags %x\n", flags);
751 return E_NOTIMPL;
754 return S_OK;
757 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
758 EXCEPINFO *ei)
760 DISPPARAMS params = {dp->rgvarg, NULL, dp->cArgs, 0};
761 ITypeInfo *ti;
762 IUnknown *unk;
763 UINT argerr=0;
764 HRESULT hres;
766 if(params.cArgs > func->argc) {
767 params.rgvarg += params.cArgs - func->argc;
768 params.cArgs = func->argc;
771 hres = get_typeinfo(func->tid, &ti);
772 if(FAILED(hres)) {
773 ERR("Could not get type info: %08lx\n", hres);
774 return hres;
777 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
778 if(FAILED(hres)) {
779 ERR("Could not get iface %s: %08lx\n", debugstr_mshtml_guid(tid_ids[func->tid]), hres);
780 return E_FAIL;
783 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, &params, res, ei, &argerr);
785 IUnknown_Release(unk);
786 return hres;
789 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
791 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
794 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
796 func_disp_t *This = impl_from_IUnknown(iface);
798 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
800 if(IsEqualGUID(&IID_IUnknown, riid)) {
801 *ppv = &This->IUnknown_iface;
802 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
803 return *ppv ? S_OK : E_NOINTERFACE;
804 }else {
805 *ppv = NULL;
806 return E_NOINTERFACE;
809 IUnknown_AddRef((IUnknown*)*ppv);
810 return S_OK;
813 static ULONG WINAPI Function_AddRef(IUnknown *iface)
815 func_disp_t *This = impl_from_IUnknown(iface);
816 LONG ref = dispex_ref_incr(&This->dispex);
818 TRACE("(%p) ref=%ld\n", This, ref);
820 return ref;
823 static ULONG WINAPI Function_Release(IUnknown *iface)
825 func_disp_t *This = impl_from_IUnknown(iface);
826 LONG ref = dispex_ref_decr(&This->dispex);
828 TRACE("(%p) ref=%ld\n", This, ref);
830 return ref;
833 static const IUnknownVtbl FunctionUnkVtbl = {
834 Function_QueryInterface,
835 Function_AddRef,
836 Function_Release
839 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
841 return CONTAINING_RECORD(iface, func_disp_t, dispex);
844 static void function_destructor(DispatchEx *dispex)
846 func_disp_t *This = impl_from_DispatchEx(dispex);
847 assert(!This->obj);
848 free(This);
851 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
852 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
854 func_disp_t *This = impl_from_DispatchEx(dispex);
855 HRESULT hres;
857 switch(flags) {
858 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
859 if(!res)
860 return E_INVALIDARG;
861 /* fall through */
862 case DISPATCH_METHOD:
863 if(!This->obj)
864 return E_UNEXPECTED;
865 hres = dispex_call_builtin(This->obj, This->info->id, params, res, ei, caller);
866 break;
867 case DISPATCH_PROPERTYGET: {
868 unsigned name_len;
869 WCHAR *ptr;
870 BSTR str;
872 static const WCHAR func_prefixW[] =
873 {'\n','f','u','n','c','t','i','o','n',' '};
874 static const WCHAR func_suffixW[] =
875 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
877 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
878 if(!caller)
879 return E_ACCESSDENIED;
881 name_len = SysStringLen(This->info->name);
882 ptr = str = SysAllocStringLen(NULL, name_len + ARRAY_SIZE(func_prefixW) + ARRAY_SIZE(func_suffixW));
883 if(!str)
884 return E_OUTOFMEMORY;
886 memcpy(ptr, func_prefixW, sizeof(func_prefixW));
887 ptr += ARRAY_SIZE(func_prefixW);
889 memcpy(ptr, This->info->name, name_len*sizeof(WCHAR));
890 ptr += name_len;
892 memcpy(ptr, func_suffixW, sizeof(func_suffixW));
894 V_VT(res) = VT_BSTR;
895 V_BSTR(res) = str;
896 return S_OK;
898 default:
899 FIXME("Unimplemented flags %x\n", flags);
900 hres = E_NOTIMPL;
903 return hres;
906 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
907 .destructor = function_destructor,
908 .value = function_value,
911 static const tid_t function_iface_tids[] = {0};
913 static dispex_static_data_t function_dispex = {
914 "Function",
915 &function_dispex_vtbl,
916 NULL_tid,
917 function_iface_tids
920 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
922 func_disp_t *ret;
924 ret = calloc(1, sizeof(func_disp_t));
925 if(!ret)
926 return NULL;
928 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
929 init_dispatch(&ret->dispex, &ret->IUnknown_iface, &function_dispex, dispex_compat_mode(obj));
930 ret->obj = obj;
931 ret->info = info;
933 return ret;
936 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
937 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
939 DISPID named_arg = DISPID_THIS;
940 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
941 IDispatchEx *dispex;
942 HRESULT hres;
944 if(dp->cNamedArgs) {
945 FIXME("named args not supported\n");
946 return E_NOTIMPL;
949 new_dp.rgvarg = malloc((dp->cArgs + 1) * sizeof(VARIANTARG));
950 if(!new_dp.rgvarg)
951 return E_OUTOFMEMORY;
953 new_dp.cArgs = dp->cArgs+1;
954 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
956 V_VT(new_dp.rgvarg) = VT_DISPATCH;
957 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
959 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
960 TRACE(">>>\n");
961 if(SUCCEEDED(hres)) {
962 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
963 IDispatchEx_Release(dispex);
964 }else {
965 UINT err = 0;
966 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
968 if(SUCCEEDED(hres))
969 TRACE("<<< %s\n", debugstr_variant(res));
970 else
971 WARN("<<< %08lx\n", hres);
973 free(new_dp.rgvarg);
974 return hres;
977 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
979 dispex_dynamic_data_t *dynamic_data;
980 func_obj_entry_t *entry;
982 dynamic_data = get_dynamic_data(This);
983 if(!dynamic_data)
984 return E_OUTOFMEMORY;
986 if(!dynamic_data->func_disps) {
987 dynamic_data->func_disps = calloc(This->info->func_disp_cnt, sizeof(*dynamic_data->func_disps));
988 if(!dynamic_data->func_disps)
989 return E_OUTOFMEMORY;
992 entry = dynamic_data->func_disps + func->func_disp_idx;
993 if(!entry->func_obj) {
994 entry->func_obj = create_func_disp(This, func);
995 if(!entry->func_obj)
996 return E_OUTOFMEMORY;
998 IDispatchEx_AddRef(&entry->func_obj->dispex.IDispatchEx_iface);
999 V_VT(&entry->val) = VT_DISPATCH;
1000 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
1003 *ret = entry;
1004 return S_OK;
1007 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
1009 int min, max, n;
1011 min = 0;
1012 max = data->func_cnt-1;
1014 while(min <= max) {
1015 n = (min+max)/2;
1017 if(data->funcs[n].id == id) {
1018 *ret = data->funcs+n;
1019 return S_OK;
1022 if(data->funcs[n].id < id)
1023 min = n+1;
1024 else
1025 max = n-1;
1028 WARN("invalid id %lx\n", id);
1029 return DISP_E_MEMBERNOTFOUND;
1032 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
1034 int min, max, n, c;
1036 min = 0;
1037 max = This->info->func_cnt-1;
1039 while(min <= max) {
1040 n = (min+max)/2;
1042 c = wcsicmp(This->info->name_table[n]->name, name);
1043 if(!c) {
1044 if((grfdex & fdexNameCaseSensitive) && wcscmp(This->info->name_table[n]->name, name))
1045 break;
1047 *ret = This->info->name_table[n]->id;
1048 return S_OK;
1051 if(c > 0)
1052 max = n-1;
1053 else
1054 min = n+1;
1057 if(This->info->desc->vtbl->get_dispid) {
1058 HRESULT hres;
1060 hres = This->info->desc->vtbl->get_dispid(This, name, grfdex, ret);
1061 if(hres != DISP_E_UNKNOWNNAME)
1062 return hres;
1065 return DISP_E_UNKNOWNNAME;
1068 HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
1070 V_VT(dst) = VT_EMPTY;
1072 if(caller) {
1073 IVariantChangeType *change_type = NULL;
1074 HRESULT hres;
1076 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
1077 if(SUCCEEDED(hres)) {
1078 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
1079 IVariantChangeType_Release(change_type);
1080 if(SUCCEEDED(hres))
1081 return S_OK;
1085 switch(vt) {
1086 case VT_BOOL:
1087 if(V_VT(src) == VT_BSTR) {
1088 V_VT(dst) = VT_BOOL;
1089 V_BOOL(dst) = variant_bool(V_BSTR(src) && *V_BSTR(src));
1090 return S_OK;
1092 break;
1093 case VT_UNKNOWN:
1094 case VT_DISPATCH:
1095 if(V_VT(src) == VT_EMPTY || V_VT(src) == VT_NULL) {
1096 V_VT(dst) = vt;
1097 V_DISPATCH(dst) = NULL;
1098 return S_OK;
1100 break;
1103 return VariantChangeType(dst, src, 0, vt);
1106 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
1108 IUnknown *iface;
1109 HRESULT hres;
1111 if(dp && dp->cArgs) {
1112 FIXME("cArgs %d\n", dp->cArgs);
1113 return E_NOTIMPL;
1116 assert(func->get_vtbl_off);
1118 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1119 if(SUCCEEDED(hres)) {
1120 switch(func->prop_vt) {
1121 #define CASE_VT(vt,type,access) \
1122 case vt: { \
1123 type val; \
1124 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
1125 if(SUCCEEDED(hres)) \
1126 access(res) = val; \
1128 break
1129 BUILTIN_TYPES_SWITCH;
1130 #undef CASE_VT
1131 default:
1132 FIXME("Unhandled vt %d\n", func->prop_vt);
1133 hres = E_NOTIMPL;
1135 IUnknown_Release(iface);
1138 if(FAILED(hres))
1139 return hres;
1141 if(func->prop_vt != VT_VARIANT)
1142 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
1143 return S_OK;
1146 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
1148 VARIANT *v, tmpv;
1149 IUnknown *iface;
1150 HRESULT hres;
1152 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1153 || dp->cNamedArgs > 1) {
1154 FIXME("invalid args\n");
1155 return E_INVALIDARG;
1158 if(!func->put_vtbl_off) {
1159 if(dispex_compat_mode(This) >= COMPAT_MODE_IE9) {
1160 WARN("No setter\n");
1161 return S_OK;
1163 FIXME("No setter\n");
1164 return E_FAIL;
1167 v = dp->rgvarg;
1168 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
1169 hres = change_type(&tmpv, v, func->prop_vt, caller);
1170 if(FAILED(hres))
1171 return hres;
1172 v = &tmpv;
1175 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1176 if(SUCCEEDED(hres)) {
1177 switch(func->prop_vt) {
1178 #define CASE_VT(vt,type,access) \
1179 case vt: \
1180 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1181 break
1182 BUILTIN_TYPES_SWITCH;
1183 #undef CASE_VT
1184 default:
1185 FIXME("Unimplemented vt %d\n", func->prop_vt);
1186 hres = E_NOTIMPL;
1189 IUnknown_Release(iface);
1192 if(v == &tmpv)
1193 VariantClear(v);
1194 return hres;
1197 static HRESULT invoke_builtin_function(DispatchEx *This, func_info_t *func, DISPPARAMS *dp,
1198 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1200 VARIANT arg_buf[MAX_ARGS], *arg_ptrs[MAX_ARGS], *arg, retv, ret_ref, vhres;
1201 unsigned i, nconv = 0;
1202 IUnknown *iface;
1203 HRESULT hres;
1205 if(func->hook) {
1206 hres = func->hook(This, DISPATCH_METHOD, dp, res, ei, caller);
1207 if(hres != S_FALSE)
1208 return hres;
1211 if(!func->call_vtbl_off)
1212 return typeinfo_invoke(This, func, DISPATCH_METHOD, dp, res, ei);
1214 if(dp->cArgs + func->default_value_cnt < func->argc) {
1215 FIXME("Invalid argument count (expected %u, got %u)\n", func->argc, dp->cArgs);
1216 return E_INVALIDARG;
1219 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1220 if(FAILED(hres))
1221 return hres;
1223 for(i=0; i < func->argc; i++) {
1224 BOOL own_value = FALSE;
1225 if(i >= dp->cArgs) {
1226 /* use default value */
1227 arg_ptrs[i] = &func->arg_info[i].default_value;
1228 continue;
1230 arg = dp->rgvarg+dp->cArgs-i-1;
1231 if(func->arg_types[i] == V_VT(arg)) {
1232 arg_ptrs[i] = arg;
1233 }else {
1234 hres = change_type(arg_buf+nconv, arg, func->arg_types[i], caller);
1235 if(FAILED(hres))
1236 break;
1237 arg_ptrs[i] = arg_buf + nconv++;
1238 own_value = TRUE;
1241 if(func->arg_types[i] == VT_DISPATCH && !IsEqualGUID(&func->arg_info[i].iid, &IID_NULL)
1242 && V_DISPATCH(arg_ptrs[i])) {
1243 IDispatch *iface;
1244 if(!own_value) {
1245 arg_buf[nconv] = *arg_ptrs[i];
1246 arg_ptrs[i] = arg_buf + nconv++;
1248 hres = IDispatch_QueryInterface(V_DISPATCH(arg_ptrs[i]), &func->arg_info[i].iid, (void**)&iface);
1249 if(FAILED(hres)) {
1250 WARN("Could not get %s iface: %08lx\n", debugstr_guid(&func->arg_info[i].iid), hres);
1251 break;
1253 if(own_value)
1254 IDispatch_Release(V_DISPATCH(arg_ptrs[i]));
1255 V_DISPATCH(arg_ptrs[i]) = iface;
1259 if(SUCCEEDED(hres)) {
1260 if(func->prop_vt == VT_VOID) {
1261 V_VT(&retv) = VT_EMPTY;
1262 }else {
1263 V_VT(&retv) = func->prop_vt;
1264 arg_ptrs[func->argc] = &ret_ref;
1265 V_VT(&ret_ref) = VT_BYREF|func->prop_vt;
1267 switch(func->prop_vt) {
1268 #define CASE_VT(vt,type,access) \
1269 case vt: \
1270 V_BYREF(&ret_ref) = &access(&retv); \
1271 break
1272 BUILTIN_ARG_TYPES_SWITCH;
1273 #undef CASE_VT
1274 case VT_PTR:
1275 V_VT(&retv) = VT_DISPATCH;
1276 V_VT(&ret_ref) = VT_BYREF | VT_DISPATCH;
1277 V_BYREF(&ret_ref) = &V_DISPATCH(&retv);
1278 break;
1279 default:
1280 assert(0);
1284 V_VT(&vhres) = VT_ERROR;
1285 hres = DispCallFunc(iface, func->call_vtbl_off*sizeof(void*), CC_STDCALL, VT_ERROR,
1286 func->argc + (func->prop_vt == VT_VOID ? 0 : 1), func->arg_types, arg_ptrs, &vhres);
1289 while(nconv--)
1290 VariantClear(arg_buf+nconv);
1291 IUnknown_Release(iface);
1292 if(FAILED(hres))
1293 return hres;
1294 if(FAILED(V_ERROR(&vhres)))
1295 return V_ERROR(&vhres);
1297 if(res)
1298 *res = retv;
1299 else
1300 VariantClear(&retv);
1301 return V_ERROR(&vhres);
1304 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
1305 EXCEPINFO *ei, IServiceProvider *caller)
1307 HRESULT hres;
1309 switch(flags) {
1310 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1311 if(!res)
1312 return E_INVALIDARG;
1313 /* fall through */
1314 case DISPATCH_METHOD:
1315 if(This->dynamic_data && This->dynamic_data->func_disps
1316 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1317 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
1319 if(V_VT(&entry->val) != VT_DISPATCH) {
1320 FIXME("calling %s not supported\n", debugstr_variant(&entry->val));
1321 return E_NOTIMPL;
1324 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != V_DISPATCH(&entry->val)) {
1325 if(!V_DISPATCH(&entry->val)) {
1326 FIXME("Calling null\n");
1327 return E_FAIL;
1330 hres = invoke_disp_value(This, V_DISPATCH(&entry->val), 0, flags, dp, res, ei, NULL);
1331 break;
1335 hres = invoke_builtin_function(This, func, dp, res, ei, caller);
1336 break;
1337 case DISPATCH_PROPERTYGET: {
1338 func_obj_entry_t *entry;
1340 if(func->id == DISPID_VALUE) {
1341 BSTR ret;
1343 hres = dispex_to_string(This, &ret);
1344 if(FAILED(hres))
1345 return hres;
1347 V_VT(res) = VT_BSTR;
1348 V_BSTR(res) = ret;
1349 return S_OK;
1352 hres = get_func_obj_entry(This, func, &entry);
1353 if(FAILED(hres))
1354 return hres;
1356 V_VT(res) = VT_EMPTY;
1357 return VariantCopy(res, &entry->val);
1359 case DISPATCH_PROPERTYPUT: {
1360 func_obj_entry_t *entry;
1362 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1363 || dp->cNamedArgs > 1) {
1364 FIXME("invalid args\n");
1365 return E_INVALIDARG;
1369 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1370 * function property using InvokeEx, it's possible to do that from jscript.
1371 * Native probably uses some undocumented interface in this case, but it should
1372 * be fine for us to allow IDispatchEx handle that.
1374 hres = get_func_obj_entry(This, func, &entry);
1375 if(FAILED(hres))
1376 return hres;
1378 return VariantCopy(&entry->val, dp->rgvarg);
1380 default:
1381 FIXME("Unimplemented flags %x\n", flags);
1382 hres = E_NOTIMPL;
1385 return hres;
1388 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1389 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1391 func_info_t *func;
1392 HRESULT hres;
1394 hres = get_builtin_func(This->info, id, &func);
1395 if(id == DISPID_VALUE && hres == DISP_E_MEMBERNOTFOUND)
1396 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1397 if(FAILED(hres))
1398 return hres;
1400 if(func->func_disp_idx >= 0)
1401 return function_invoke(This, func, flags, dp, res, ei, caller);
1403 if(func->hook) {
1404 hres = func->hook(This, flags, dp, res, ei, caller);
1405 if(hres != S_FALSE)
1406 return hres;
1409 switch(flags) {
1410 case DISPATCH_PROPERTYPUT:
1411 if(res)
1412 V_VT(res) = VT_EMPTY;
1413 hres = builtin_propput(This, func, dp, caller);
1414 break;
1415 case DISPATCH_PROPERTYGET:
1416 hres = builtin_propget(This, func, dp, res);
1417 break;
1418 default:
1419 if(!func->get_vtbl_off) {
1420 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1421 }else {
1422 VARIANT v;
1424 hres = builtin_propget(This, func, NULL, &v);
1425 if(FAILED(hres))
1426 return hres;
1428 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD) || dp->cArgs) {
1429 if(V_VT(&v) != VT_DISPATCH) {
1430 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v), flags);
1431 VariantClear(&v);
1432 return E_FAIL;
1435 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1436 IDispatch_Release(V_DISPATCH(&v));
1437 }else if(res) {
1438 *res = v;
1439 }else {
1440 VariantClear(&v);
1445 return hres;
1448 HRESULT dispex_call_builtin(DispatchEx *dispex, DISPID id, DISPPARAMS *dp,
1449 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1451 func_info_t *func;
1452 HRESULT hres;
1454 hres = get_builtin_func(dispex->info, id, &func);
1455 if(FAILED(hres))
1456 return hres;
1458 return invoke_builtin_function(dispex, func, dp, res, ei, caller);
1461 HRESULT remove_attribute(DispatchEx *This, DISPID id, VARIANT_BOOL *success)
1463 switch(get_dispid_type(id)) {
1464 case DISPEXPROP_CUSTOM:
1465 FIXME("DISPEXPROP_CUSTOM not supported\n");
1466 return E_NOTIMPL;
1468 case DISPEXPROP_DYNAMIC: {
1469 DWORD idx = id - DISPID_DYNPROP_0;
1470 dynamic_prop_t *prop;
1472 prop = This->dynamic_data->props+idx;
1473 VariantClear(&prop->var);
1474 prop->flags |= DYNPROP_DELETED;
1475 *success = VARIANT_TRUE;
1476 return S_OK;
1478 case DISPEXPROP_BUILTIN: {
1479 VARIANT var;
1480 DISPPARAMS dp = {&var,NULL,1,0};
1481 func_info_t *func;
1482 HRESULT hres;
1484 hres = get_builtin_func(This->info, id, &func);
1485 if(FAILED(hres))
1486 return hres;
1488 /* For builtin functions, we set their value to the original function. */
1489 if(func->func_disp_idx >= 0) {
1490 func_obj_entry_t *entry;
1492 if(!This->dynamic_data || !This->dynamic_data->func_disps
1493 || !This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1494 *success = VARIANT_FALSE;
1495 return S_OK;
1498 entry = This->dynamic_data->func_disps + func->func_disp_idx;
1499 if(V_VT(&entry->val) == VT_DISPATCH
1500 && V_DISPATCH(&entry->val) == (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface) {
1501 *success = VARIANT_FALSE;
1502 return S_OK;
1505 VariantClear(&entry->val);
1506 V_VT(&entry->val) = VT_DISPATCH;
1507 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
1508 IDispatch_AddRef(V_DISPATCH(&entry->val));
1509 *success = VARIANT_TRUE;
1510 return S_OK;
1512 *success = VARIANT_TRUE;
1514 V_VT(&var) = VT_EMPTY;
1515 hres = builtin_propput(This, func, &dp, NULL);
1516 if(FAILED(hres)) {
1517 VARIANT *ref;
1518 hres = dispex_get_dprop_ref(This, func->name, FALSE, &ref);
1519 if(FAILED(hres) || V_VT(ref) != VT_BSTR)
1520 *success = VARIANT_FALSE;
1521 else
1522 VariantClear(ref);
1524 return S_OK;
1526 default:
1527 assert(0);
1528 return E_FAIL;
1532 compat_mode_t dispex_compat_mode(DispatchEx *dispex)
1534 return dispex->info != dispex->info->desc->delayed_init_info
1535 ? dispex->info->compat_mode
1536 : dispex->info->desc->vtbl->get_compat_mode(dispex);
1539 HRESULT dispex_to_string(DispatchEx *dispex, BSTR *ret)
1541 static const WCHAR prefix[8] = L"[object ";
1542 static const WCHAR suffix[] = L"]";
1543 WCHAR buf[ARRAY_SIZE(prefix) + 28 + ARRAY_SIZE(suffix)], *p = buf;
1544 compat_mode_t compat_mode = dispex_compat_mode(dispex);
1545 const char *name = dispex->info->desc->name;
1547 if(!ret)
1548 return E_INVALIDARG;
1550 memcpy(p, prefix, sizeof(prefix));
1551 p += ARRAY_SIZE(prefix);
1552 if(compat_mode < COMPAT_MODE_IE9)
1553 p--;
1554 else {
1555 while(*name)
1556 *p++ = *name++;
1557 assert(p + ARRAY_SIZE(suffix) - buf <= ARRAY_SIZE(buf));
1559 memcpy(p, suffix, sizeof(suffix));
1561 *ret = SysAllocString(buf);
1562 return *ret ? S_OK : E_OUTOFMEMORY;
1565 static dispex_data_t *ensure_dispex_info(dispex_static_data_t *desc, compat_mode_t compat_mode)
1567 if(!desc->info_cache[compat_mode]) {
1568 EnterCriticalSection(&cs_dispex_static_data);
1569 if(!desc->info_cache[compat_mode])
1570 desc->info_cache[compat_mode] = preprocess_dispex_data(desc, compat_mode);
1571 LeaveCriticalSection(&cs_dispex_static_data);
1573 return desc->info_cache[compat_mode];
1576 static BOOL ensure_real_info(DispatchEx *dispex)
1578 if(dispex->info != dispex->info->desc->delayed_init_info)
1579 return TRUE;
1581 dispex->info = ensure_dispex_info(dispex->info->desc, dispex_compat_mode(dispex));
1582 return dispex->info != NULL;
1585 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1587 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1590 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1592 DispatchEx *This = impl_from_IDispatchEx(iface);
1594 return IUnknown_QueryInterface(This->outer, riid, ppv);
1597 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1599 DispatchEx *This = impl_from_IDispatchEx(iface);
1601 return IUnknown_AddRef(This->outer);
1604 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1606 DispatchEx *This = impl_from_IDispatchEx(iface);
1608 return IUnknown_Release(This->outer);
1611 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1613 DispatchEx *This = impl_from_IDispatchEx(iface);
1615 TRACE("(%p)->(%p)\n", This, pctinfo);
1617 *pctinfo = 1;
1618 return S_OK;
1621 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1622 LCID lcid, ITypeInfo **ppTInfo)
1624 DispatchEx *This = impl_from_IDispatchEx(iface);
1625 HRESULT hres;
1627 TRACE("(%p)->(%u %lu %p)\n", This, iTInfo, lcid, ppTInfo);
1629 hres = get_typeinfo(This->info->desc->disp_tid, ppTInfo);
1630 if(FAILED(hres))
1631 return hres;
1633 ITypeInfo_AddRef(*ppTInfo);
1634 return S_OK;
1637 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1638 LPOLESTR *rgszNames, UINT cNames,
1639 LCID lcid, DISPID *rgDispId)
1641 DispatchEx *This = impl_from_IDispatchEx(iface);
1642 HRESULT hres = S_OK;
1644 TRACE("(%p)->(%s %p %u %lu %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1645 lcid, rgDispId);
1647 /* Native ignores all cNames > 1, and doesn't even fill them */
1648 if(cNames)
1649 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[0], 0, rgDispId);
1651 return hres;
1654 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1655 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1656 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1658 DispatchEx *This = impl_from_IDispatchEx(iface);
1660 TRACE("(%p)->(%ld %s %ld %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1661 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1663 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1664 pVarResult, pExcepInfo, NULL);
1667 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1669 DispatchEx *This = impl_from_IDispatchEx(iface);
1670 dynamic_prop_t *dprop;
1671 HRESULT hres;
1673 TRACE("(%p)->(%s %lx %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1675 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1676 FIXME("Unsupported grfdex %lx\n", grfdex);
1678 if(!ensure_real_info(This))
1679 return E_OUTOFMEMORY;
1681 hres = get_builtin_id(This, bstrName, grfdex, pid);
1682 if(hres != DISP_E_UNKNOWNNAME)
1683 return hres;
1685 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1686 if(FAILED(hres))
1687 return hres;
1689 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1690 return S_OK;
1693 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1694 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1696 DispatchEx *This = impl_from_IDispatchEx(iface);
1697 HRESULT hres;
1699 TRACE("(%p)->(%lx %lx %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1701 if(!ensure_real_info(This))
1702 return E_OUTOFMEMORY;
1704 if(wFlags == (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF))
1705 wFlags = DISPATCH_PROPERTYPUT;
1707 switch(get_dispid_type(id)) {
1708 case DISPEXPROP_CUSTOM:
1709 if(!This->info->desc->vtbl->invoke)
1710 return DISP_E_MEMBERNOTFOUND;
1711 return This->info->desc->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1713 case DISPEXPROP_DYNAMIC: {
1714 DWORD idx = id - DISPID_DYNPROP_0;
1715 dynamic_prop_t *prop;
1717 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1718 return DISP_E_MEMBERNOTFOUND;
1720 prop = This->dynamic_data->props+idx;
1722 switch(wFlags) {
1723 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1724 if(!pvarRes)
1725 return E_INVALIDARG;
1726 /* fall through */
1727 case DISPATCH_METHOD:
1728 if(V_VT(&prop->var) != VT_DISPATCH) {
1729 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1730 return E_NOTIMPL;
1733 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1734 case DISPATCH_PROPERTYGET:
1735 if(prop->flags & DYNPROP_DELETED)
1736 return DISP_E_MEMBERNOTFOUND;
1737 V_VT(pvarRes) = VT_EMPTY;
1738 return variant_copy(pvarRes, &prop->var);
1739 case DISPATCH_PROPERTYPUT:
1740 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1741 || pdp->cNamedArgs > 1) {
1742 FIXME("invalid args\n");
1743 return E_INVALIDARG;
1746 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1747 VariantClear(&prop->var);
1748 hres = variant_copy(&prop->var, pdp->rgvarg);
1749 if(FAILED(hres))
1750 return hres;
1752 prop->flags &= ~DYNPROP_DELETED;
1753 return S_OK;
1754 default:
1755 FIXME("unhandled wFlags %x\n", wFlags);
1756 return E_NOTIMPL;
1759 case DISPEXPROP_BUILTIN:
1760 if(wFlags == DISPATCH_CONSTRUCT) {
1761 if(id == DISPID_VALUE) {
1762 if(This->info->desc->vtbl->value) {
1763 return This->info->desc->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1765 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1766 return E_FAIL;
1768 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1769 return E_FAIL;
1772 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1773 default:
1774 assert(0);
1775 return E_FAIL;
1779 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR name, DWORD grfdex)
1781 DispatchEx *This = impl_from_IDispatchEx(iface);
1782 DISPID id;
1783 HRESULT hres;
1785 TRACE("(%p)->(%s %lx)\n", This, debugstr_w(name), grfdex);
1787 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, name, grfdex & ~fdexNameEnsure, &id);
1788 if(FAILED(hres)) {
1789 compat_mode_t compat_mode = dispex_compat_mode(This);
1790 TRACE("property %s not found\n", debugstr_w(name));
1791 return compat_mode < COMPAT_MODE_IE8 ? E_NOTIMPL :
1792 compat_mode < COMPAT_MODE_IE9 ? hres : S_OK;
1795 return IDispatchEx_DeleteMemberByDispID(&This->IDispatchEx_iface, id);
1798 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1800 DispatchEx *This = impl_from_IDispatchEx(iface);
1802 TRACE("(%p)->(%lx)\n", This, id);
1804 if(is_custom_dispid(id) && This->info->desc->vtbl->delete)
1805 return This->info->desc->vtbl->delete(This, id);
1807 if(dispex_compat_mode(This) < COMPAT_MODE_IE8) {
1808 /* Not implemented by IE */
1809 return E_NOTIMPL;
1812 if(is_dynamic_dispid(id)) {
1813 DWORD idx = id - DISPID_DYNPROP_0;
1814 dynamic_prop_t *prop;
1816 if(!get_dynamic_data(This) || idx >= This->dynamic_data->prop_cnt)
1817 return S_OK;
1819 prop = This->dynamic_data->props + idx;
1820 VariantClear(&prop->var);
1821 prop->flags |= DYNPROP_DELETED;
1822 return S_OK;
1825 return S_OK;
1828 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1830 DispatchEx *This = impl_from_IDispatchEx(iface);
1831 FIXME("(%p)->(%lx %lx %p)\n", This, id, grfdexFetch, pgrfdex);
1832 return E_NOTIMPL;
1835 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1837 DispatchEx *This = impl_from_IDispatchEx(iface);
1838 func_info_t *func;
1839 HRESULT hres;
1841 TRACE("(%p)->(%lx %p)\n", This, id, pbstrName);
1843 if(!ensure_real_info(This))
1844 return E_OUTOFMEMORY;
1846 if(is_custom_dispid(id)) {
1847 if(This->info->desc->vtbl->get_name)
1848 return This->info->desc->vtbl->get_name(This, id, pbstrName);
1849 return DISP_E_MEMBERNOTFOUND;
1852 if(is_dynamic_dispid(id)) {
1853 DWORD idx = id - DISPID_DYNPROP_0;
1855 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1856 return DISP_E_MEMBERNOTFOUND;
1858 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1859 if(!*pbstrName)
1860 return E_OUTOFMEMORY;
1862 return S_OK;
1865 hres = get_builtin_func(This->info, id, &func);
1866 if(FAILED(hres))
1867 return hres;
1869 *pbstrName = SysAllocString(func->name);
1870 if(!*pbstrName)
1871 return E_OUTOFMEMORY;
1872 return S_OK;
1875 static HRESULT next_dynamic_id(DispatchEx *dispex, DWORD idx, DISPID *ret_id)
1877 while(idx < dispex->dynamic_data->prop_cnt &&
1878 (dispex->dynamic_data->props[idx].flags & (DYNPROP_DELETED | DYNPROP_HIDDEN)))
1879 idx++;
1881 if(idx == dispex->dynamic_data->prop_cnt) {
1882 *ret_id = DISPID_STARTENUM;
1883 return S_FALSE;
1886 *ret_id = DISPID_DYNPROP_0+idx;
1887 return S_OK;
1890 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1892 DispatchEx *This = impl_from_IDispatchEx(iface);
1893 func_info_t *func;
1894 HRESULT hres;
1896 TRACE("(%p)->(%lx %lx %p)\n", This, grfdex, id, pid);
1898 if(!ensure_real_info(This))
1899 return E_OUTOFMEMORY;
1901 if(is_dynamic_dispid(id)) {
1902 DWORD idx = id - DISPID_DYNPROP_0;
1904 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1905 return DISP_E_MEMBERNOTFOUND;
1907 return next_dynamic_id(This, idx+1, pid);
1910 if(!is_custom_dispid(id)) {
1911 if(id == DISPID_STARTENUM) {
1912 func = This->info->funcs;
1913 }else {
1914 hres = get_builtin_func(This->info, id, &func);
1915 if(FAILED(hres))
1916 return hres;
1917 func++;
1920 while(func < This->info->funcs + This->info->func_cnt) {
1921 if(func->func_disp_idx == -1) {
1922 *pid = func->id;
1923 return S_OK;
1925 func++;
1928 id = DISPID_STARTENUM;
1931 if(This->info->desc->vtbl->next_dispid) {
1932 hres = This->info->desc->vtbl->next_dispid(This, id, pid);
1933 if(hres != S_FALSE)
1934 return hres;
1937 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt)
1938 return next_dynamic_id(This, 0, pid);
1940 *pid = DISPID_STARTENUM;
1941 return S_FALSE;
1944 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1946 DispatchEx *This = impl_from_IDispatchEx(iface);
1947 FIXME("(%p)->(%p)\n", This, ppunk);
1948 return E_NOTIMPL;
1951 static IDispatchExVtbl DispatchExVtbl = {
1952 DispatchEx_QueryInterface,
1953 DispatchEx_AddRef,
1954 DispatchEx_Release,
1955 DispatchEx_GetTypeInfoCount,
1956 DispatchEx_GetTypeInfo,
1957 DispatchEx_GetIDsOfNames,
1958 DispatchEx_Invoke,
1959 DispatchEx_GetDispID,
1960 DispatchEx_InvokeEx,
1961 DispatchEx_DeleteMemberByName,
1962 DispatchEx_DeleteMemberByDispID,
1963 DispatchEx_GetMemberProperties,
1964 DispatchEx_GetMemberName,
1965 DispatchEx_GetNextDispID,
1966 DispatchEx_GetNameSpaceParent
1969 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1971 if(IsEqualGUID(&IID_IDispatch, riid))
1972 *ppv = &This->IDispatchEx_iface;
1973 else if(IsEqualGUID(&IID_IDispatchEx, riid))
1974 *ppv = &This->IDispatchEx_iface;
1975 else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant, riid)) {
1976 *ppv = &dispex_ccp;
1977 return TRUE;
1978 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports, riid)) {
1979 *ppv = &This->IDispatchEx_iface;
1980 return TRUE;
1981 }else if(IsEqualGUID(&IID_IDispatchJS, riid))
1982 *ppv = NULL;
1983 else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid))
1984 *ppv = NULL;
1985 else if(IsEqualGUID(&IID_IMarshal, riid))
1986 *ppv = NULL;
1987 else if(IsEqualGUID(&IID_IManagedObject, riid))
1988 *ppv = NULL;
1989 else
1990 return FALSE;
1992 if(*ppv)
1993 IUnknown_AddRef((IUnknown*)*ppv);
1994 return TRUE;
1997 LONG dispex_ref_decr(DispatchEx *dispex)
1999 LONG ref = ccref_decr(&dispex->ccref, (nsISupports*)&dispex->IDispatchEx_iface, &dispex_ccp);
2001 /* Gecko ccref may not free the object immediatelly when ref count reaches 0, so we need
2002 * an extra care for objects that need an immediate clean up. See
2003 * NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE_WITH_LAST_RELEASE for details. */
2004 if(!ref && dispex->info->desc->vtbl->last_release) {
2005 ccref_incr(&dispex->ccref, (nsISupports*)&dispex->IDispatchEx_iface);
2006 dispex->info->desc->vtbl->last_release(dispex);
2007 ccref_decr(&dispex->ccref, (nsISupports*)&dispex->IDispatchEx_iface, &dispex_ccp);
2010 return ref;
2013 static nsresult NSAPI dispex_traverse(void *ccp, void *p, nsCycleCollectionTraversalCallback *cb)
2015 DispatchEx *This = impl_from_IDispatchEx(p);
2016 dynamic_prop_t *prop;
2018 describe_cc_node(&This->ccref, This->info->desc->name, cb);
2020 if(This->info->desc->vtbl->traverse)
2021 This->info->desc->vtbl->traverse(This, cb);
2023 if(!This->dynamic_data)
2024 return NS_OK;
2026 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++)
2027 traverse_variant(&prop->var, "dispex_data", cb);
2029 if(This->dynamic_data->func_disps) {
2030 func_obj_entry_t *iter = This->dynamic_data->func_disps, *end = iter + This->info->func_disp_cnt;
2032 for(iter = This->dynamic_data->func_disps; iter < end; iter++) {
2033 if(!iter->func_obj)
2034 continue;
2035 note_cc_edge((nsISupports*)&iter->func_obj->dispex.IDispatchEx_iface, "func_obj", cb);
2036 traverse_variant(&iter->val, "func_val", cb);
2040 return NS_OK;
2043 void dispex_props_unlink(DispatchEx *This)
2045 dynamic_prop_t *prop;
2047 if(!This->dynamic_data)
2048 return;
2050 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
2051 prop->flags |= DYNPROP_DELETED;
2052 unlink_variant(&prop->var);
2055 if(This->dynamic_data->func_disps) {
2056 func_obj_entry_t *iter = This->dynamic_data->func_disps, *end = iter + This->info->func_disp_cnt;
2058 for(iter = This->dynamic_data->func_disps; iter < end; iter++) {
2059 if(!iter->func_obj)
2060 continue;
2061 iter->func_obj->obj = NULL;
2062 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
2063 VariantClear(&iter->val);
2066 free(This->dynamic_data->func_disps);
2067 This->dynamic_data->func_disps = NULL;
2071 static nsresult NSAPI dispex_unlink(void *p)
2073 DispatchEx *This = impl_from_IDispatchEx(p);
2075 if(This->info->desc->vtbl->unlink)
2076 This->info->desc->vtbl->unlink(This);
2078 dispex_props_unlink(This);
2079 return NS_OK;
2082 static void NSAPI dispex_delete_cycle_collectable(void *p)
2084 DispatchEx *This = impl_from_IDispatchEx(p);
2085 dynamic_prop_t *prop;
2087 if(This->info->desc->vtbl->unlink)
2088 This->info->desc->vtbl->unlink(This);
2090 if(!This->dynamic_data)
2091 goto destructor;
2093 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
2094 VariantClear(&prop->var);
2095 free(prop->name);
2098 free(This->dynamic_data->props);
2100 if(This->dynamic_data->func_disps) {
2101 func_obj_entry_t *iter;
2103 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps + This->info->func_disp_cnt; iter++) {
2104 if(iter->func_obj) {
2105 iter->func_obj->obj = NULL;
2106 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
2108 VariantClear(&iter->val);
2111 free(This->dynamic_data->func_disps);
2114 free(This->dynamic_data);
2116 destructor:
2117 This->info->desc->vtbl->destructor(This);
2120 void init_dispex_cc(void)
2122 static const CCObjCallback dispex_ccp_callback = {
2123 dispex_traverse,
2124 dispex_unlink,
2125 dispex_delete_cycle_collectable
2127 ccp_init(&dispex_ccp, &dispex_ccp_callback);
2130 const void *dispex_get_vtbl(DispatchEx *dispex)
2132 return dispex->info->desc->vtbl;
2135 void init_dispatch(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data, compat_mode_t compat_mode)
2137 assert(compat_mode < COMPAT_MODE_CNT);
2139 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
2140 dispex->outer = outer;
2141 dispex->dynamic_data = NULL;
2142 ccref_init(&dispex->ccref, 1);
2144 if(data->vtbl->get_compat_mode) {
2145 /* delayed init */
2146 if(!data->delayed_init_info) {
2147 EnterCriticalSection(&cs_dispex_static_data);
2148 if(!data->delayed_init_info) {
2149 dispex_data_t *info = calloc(1, sizeof(*data->delayed_init_info));
2150 if(info) {
2151 info->desc = data;
2152 data->delayed_init_info = info;
2155 LeaveCriticalSection(&cs_dispex_static_data);
2157 dispex->info = data->delayed_init_info;
2158 }else {
2159 dispex->info = ensure_dispex_info(data, compat_mode);