reg/tests: Test import with non-standard registry file headers.
[wine.git] / dlls / mshtml / dispex.c
blob2013cee1e3cb8c59cf8834d7112e015b209ba716
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>
20 #include <assert.h>
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.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 };
48 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
50 typedef struct {
51 DISPID id;
52 BSTR name;
53 tid_t tid;
54 SHORT call_vtbl_off;
55 SHORT put_vtbl_off;
56 SHORT get_vtbl_off;
57 SHORT func_disp_idx;
58 USHORT argc;
59 VARTYPE prop_vt;
60 VARTYPE *arg_types;
61 } func_info_t;
63 struct dispex_data_t {
64 const dispex_static_data_t *desc;
66 DWORD func_cnt;
67 DWORD func_size;
68 func_info_t *funcs;
69 func_info_t **name_table;
70 DWORD func_disp_cnt;
72 struct list entry;
75 typedef struct {
76 VARIANT var;
77 LPWSTR name;
78 DWORD flags;
79 } dynamic_prop_t;
81 #define DYNPROP_DELETED 0x01
83 typedef struct {
84 DispatchEx dispex;
85 IUnknown IUnknown_iface;
86 LONG ref;
87 DispatchEx *obj;
88 func_info_t *info;
89 } func_disp_t;
91 typedef struct {
92 func_disp_t *func_obj;
93 VARIANT val;
94 } func_obj_entry_t;
96 struct dispex_dynamic_data_t {
97 DWORD buf_size;
98 DWORD prop_cnt;
99 dynamic_prop_t *props;
100 func_obj_entry_t *func_disps;
103 #define DISPID_DYNPROP_0 0x50000000
104 #define DISPID_DYNPROP_MAX 0x5fffffff
106 #define FDEX_VERSION_MASK 0xf0000000
108 static ITypeLib *typelib;
109 static ITypeInfo *typeinfos[LAST_tid];
110 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
112 static REFIID tid_ids[] = {
113 #define XIID(iface) &IID_ ## iface,
114 #define XDIID(iface) &DIID_ ## iface,
115 TID_LIST
116 #undef XIID
117 #undef XDIID
120 static HRESULT load_typelib(void)
122 HRESULT hres;
123 ITypeLib *tl;
125 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
126 if(FAILED(hres)) {
127 ERR("LoadRegTypeLib failed: %08x\n", hres);
128 return hres;
131 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
132 ITypeLib_Release(tl);
133 return hres;
136 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
138 HRESULT hres;
140 if (!typelib)
141 hres = load_typelib();
142 if (!typelib)
143 return hres;
145 if(!typeinfos[tid]) {
146 ITypeInfo *ti;
148 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
149 if(FAILED(hres)) {
150 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_mshtml_guid(tid_ids[tid]), hres);
151 return hres;
154 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
155 ITypeInfo_Release(ti);
158 *typeinfo = typeinfos[tid];
159 return S_OK;
162 void release_typelib(void)
164 dispex_data_t *iter;
165 unsigned i;
167 while(!list_empty(&dispex_data_list)) {
168 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
169 list_remove(&iter->entry);
171 for(i=0; i < iter->func_cnt; i++)
172 SysFreeString(iter->funcs[i].name);
174 heap_free(iter->funcs);
175 heap_free(iter->name_table);
176 heap_free(iter);
179 if(!typelib)
180 return;
182 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
183 if(typeinfos[i])
184 ITypeInfo_Release(typeinfos[i]);
186 ITypeLib_Release(typelib);
187 DeleteCriticalSection(&cs_dispex_static_data);
190 HRESULT get_htmldoc_classinfo(ITypeInfo **typeinfo)
192 HRESULT hres;
194 if (!typelib)
195 hres = load_typelib();
196 if (!typelib)
197 return hres;
199 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &CLSID_HTMLDocument, typeinfo);
200 if(FAILED(hres))
201 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
202 return hres;
205 /* Not all argument types are supported yet */
206 #define BUILTIN_ARG_TYPES_SWITCH \
207 CASE_VT(VT_I2, INT16, V_I2); \
208 CASE_VT(VT_I4, INT32, V_I4); \
209 CASE_VT(VT_R4, float, V_R4); \
210 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
211 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL)
213 /* List all types used by IDispatchEx-based properties */
214 #define BUILTIN_TYPES_SWITCH \
215 BUILTIN_ARG_TYPES_SWITCH; \
216 CASE_VT(VT_VARIANT, VARIANT, *); \
217 CASE_VT(VT_PTR, void*, V_BYREF); \
218 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
219 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH)
221 static BOOL is_arg_type_supported(VARTYPE vt)
223 switch(vt) {
224 #define CASE_VT(x,a,b) case x: return TRUE
225 BUILTIN_ARG_TYPES_SWITCH;
226 #undef CASE_VT
228 return FALSE;
231 static void add_func_info(dispex_data_t *data, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
233 func_info_t *info;
234 BSTR name;
235 HRESULT hres;
237 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &name, NULL, NULL, NULL);
238 if(FAILED(hres))
239 return;
241 for(info = data->funcs; info < data->funcs+data->func_cnt; info++) {
242 if(info->id == desc->memid || !strcmpW(info->name, name)) {
243 if(info->tid != tid) {
244 SysFreeString(name);
245 return; /* Duplicated in other interface */
247 break;
251 if(info == data->funcs+data->func_cnt) {
252 if(data->func_cnt == data->func_size)
253 data->funcs = heap_realloc_zero(data->funcs, (data->func_size <<= 1)*sizeof(func_info_t));
254 info = data->funcs+data->func_cnt;
256 data->func_cnt++;
258 info->id = desc->memid;
259 info->name = name;
260 info->tid = tid;
261 info->func_disp_idx = -1;
262 info->prop_vt = VT_EMPTY;
263 }else {
264 SysFreeString(name);
267 if(desc->invkind & DISPATCH_METHOD) {
268 unsigned i;
270 info->func_disp_idx = data->func_disp_cnt++;
271 info->argc = desc->cParams;
273 assert(info->argc < MAX_ARGS);
274 assert(desc->funckind == FUNC_DISPATCH);
276 info->arg_types = heap_alloc(sizeof(*info->arg_types) * info->argc);
277 if(!info->arg_types)
278 return; /* FIXME: real error instead of fallback */
280 for(i=0; i < info->argc; i++)
281 info->arg_types[i] = desc->lprgelemdescParam[i].tdesc.vt;
283 info->prop_vt = desc->elemdescFunc.tdesc.vt;
284 if(info->prop_vt != VT_VOID && !is_arg_type_supported(info->prop_vt)) {
285 TRACE("%s: return type %d\n", debugstr_w(info->name), info->prop_vt);
286 return; /* Fallback to ITypeInfo::Invoke */
289 if(desc->cParamsOpt) {
290 TRACE("%s: optional params\n", debugstr_w(info->name));
291 return; /* Fallback to ITypeInfo::Invoke */
294 for(i=0; i < info->argc; i++) {
295 if(!is_arg_type_supported(info->arg_types[i])) {
296 return; /* Fallback to ITypeInfo for unsupported arg types */
299 if(desc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
300 TRACE("%s param %d: default value\n", debugstr_w(info->name), i);
301 return; /* Fallback to ITypeInfo::Invoke */
305 assert(info->argc <= MAX_ARGS);
306 assert(desc->callconv == CC_STDCALL);
308 info->call_vtbl_off = desc->oVft/sizeof(void*);
309 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
310 VARTYPE vt = VT_EMPTY;
312 if(desc->invkind & DISPATCH_PROPERTYGET) {
313 vt = desc->elemdescFunc.tdesc.vt;
314 info->get_vtbl_off = desc->oVft/sizeof(void*);
316 if(desc->invkind & DISPATCH_PROPERTYPUT) {
317 assert(desc->cParams == 1);
318 vt = desc->lprgelemdescParam->tdesc.vt;
319 info->put_vtbl_off = desc->oVft/sizeof(void*);
322 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
323 info->prop_vt = vt;
327 static HRESULT process_interface(dispex_data_t *data, tid_t tid, ITypeInfo *disp_typeinfo)
329 unsigned i = 7; /* skip IDispatch functions */
330 ITypeInfo *typeinfo;
331 FUNCDESC *funcdesc;
332 HRESULT hres;
334 hres = get_typeinfo(tid, &typeinfo);
335 if(FAILED(hres))
336 return hres;
338 while(1) {
339 hres = ITypeInfo_GetFuncDesc(typeinfo, i++, &funcdesc);
340 if(FAILED(hres))
341 break;
343 TRACE("adding...\n");
345 add_func_info(data, tid, funcdesc, disp_typeinfo ? disp_typeinfo : typeinfo);
346 ITypeInfo_ReleaseFuncDesc(typeinfo, funcdesc);
349 return S_OK;
352 void dispex_info_add_interface(dispex_data_t *info, tid_t tid)
354 HRESULT hres;
356 hres = process_interface(info, tid, NULL);
357 if(FAILED(hres))
358 ERR("process_interface failed: %08x\n", hres);
361 static int dispid_cmp(const void *p1, const void *p2)
363 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
366 static int func_name_cmp(const void *p1, const void *p2)
368 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
371 static dispex_data_t *preprocess_dispex_data(const dispex_static_data_t *desc, compat_mode_t compat_mode)
373 const tid_t *tid;
374 dispex_data_t *data;
375 DWORD i;
376 ITypeInfo *dti;
377 HRESULT hres;
379 if(desc->disp_tid) {
380 hres = get_typeinfo(desc->disp_tid, &dti);
381 if(FAILED(hres)) {
382 ERR("Could not get disp type info: %08x\n", hres);
383 return NULL;
387 data = heap_alloc(sizeof(dispex_data_t));
388 if (!data) {
389 ERR("Out of memory\n");
390 return NULL;
392 data->desc = desc;
393 data->func_cnt = 0;
394 data->func_disp_cnt = 0;
395 data->func_size = 16;
396 data->funcs = heap_alloc_zero(data->func_size*sizeof(func_info_t));
397 if (!data->funcs) {
398 heap_free (data);
399 ERR("Out of memory\n");
400 return NULL;
402 list_add_tail(&dispex_data_list, &data->entry);
404 if(desc->init_info)
405 desc->init_info(data, compat_mode);
407 for(tid = desc->iface_tids; *tid; tid++) {
408 hres = process_interface(data, *tid, dti);
409 if(FAILED(hres))
410 break;
413 if(!data->func_cnt) {
414 heap_free(data->funcs);
415 data->name_table = NULL;
416 data->funcs = NULL;
417 data->func_size = 0;
418 return data;
422 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
423 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
425 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
426 for(i=0; i < data->func_cnt; i++)
427 data->name_table[i] = data->funcs+i;
428 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
429 return data;
432 static int id_cmp(const void *p1, const void *p2)
434 return *(const DISPID*)p1 - *(const DISPID*)p2;
437 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
439 unsigned i, func_cnt;
440 FUNCDESC *funcdesc;
441 ITypeInfo *ti;
442 TYPEATTR *attr;
443 DISPID *ids;
444 HRESULT hres;
446 hres = get_typeinfo(tid, &ti);
447 if(FAILED(hres))
448 return hres;
450 hres = ITypeInfo_GetTypeAttr(ti, &attr);
451 if(FAILED(hres)) {
452 ITypeInfo_Release(ti);
453 return hres;
456 func_cnt = attr->cFuncs;
457 ITypeInfo_ReleaseTypeAttr(ti, attr);
459 ids = heap_alloc(func_cnt*sizeof(DISPID));
460 if(!ids) {
461 ITypeInfo_Release(ti);
462 return E_OUTOFMEMORY;
465 for(i=0; i < func_cnt; i++) {
466 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
467 if(FAILED(hres))
468 break;
470 ids[i] = funcdesc->memid;
471 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
474 ITypeInfo_Release(ti);
475 if(FAILED(hres)) {
476 heap_free(ids);
477 return hres;
480 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
482 *ret_size = func_cnt;
483 *ret = ids;
484 return S_OK;
487 static inline BOOL is_custom_dispid(DISPID id)
489 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
492 static inline BOOL is_dynamic_dispid(DISPID id)
494 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
497 dispex_prop_type_t get_dispid_type(DISPID id)
499 if(is_dynamic_dispid(id))
500 return DISPEXPROP_DYNAMIC;
501 if(is_custom_dispid(id))
502 return DISPEXPROP_CUSTOM;
503 return DISPEXPROP_BUILTIN;
506 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
508 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
509 V_VT(dest) = VT_BSTR;
510 V_BSTR(dest) = NULL;
511 return S_OK;
514 return VariantCopy(dest, src);
517 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
519 if(This->dynamic_data)
520 return This->dynamic_data;
522 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
523 if(!This->dynamic_data)
524 return NULL;
526 if(This->info->desc->vtbl && This->info->desc->vtbl->populate_props)
527 This->info->desc->vtbl->populate_props(This);
529 return This->dynamic_data;
532 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
534 const BOOL alloc = flags & fdexNameEnsure;
535 dispex_dynamic_data_t *data;
536 dynamic_prop_t *prop;
538 data = get_dynamic_data(This);
539 if(!data)
540 return E_OUTOFMEMORY;
542 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
543 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
544 if(prop->flags & DYNPROP_DELETED) {
545 if(!alloc)
546 return DISP_E_UNKNOWNNAME;
547 prop->flags &= ~DYNPROP_DELETED;
549 *ret = prop;
550 return S_OK;
554 if(!alloc)
555 return DISP_E_UNKNOWNNAME;
557 TRACE("creating dynamic prop %s\n", debugstr_w(name));
559 if(!data->buf_size) {
560 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
561 if(!data->props)
562 return E_OUTOFMEMORY;
563 data->buf_size = 4;
564 }else if(data->buf_size == data->prop_cnt) {
565 dynamic_prop_t *new_props;
567 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
568 if(!new_props)
569 return E_OUTOFMEMORY;
571 data->props = new_props;
572 data->buf_size <<= 1;
575 prop = data->props + data->prop_cnt;
577 prop->name = heap_strdupW(name);
578 if(!prop->name)
579 return E_OUTOFMEMORY;
581 VariantInit(&prop->var);
582 prop->flags = 0;
583 data->prop_cnt++;
584 *ret = prop;
585 return S_OK;
588 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
590 dynamic_prop_t *prop;
591 HRESULT hres;
593 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
594 if(FAILED(hres))
595 return hres;
597 *ret = &prop->var;
598 return S_OK;
601 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
603 dynamic_prop_t *prop;
604 HRESULT hres;
606 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
607 if(FAILED(hres))
608 return hres;
610 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
611 return S_OK;
614 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
615 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
617 if(This->info->desc->vtbl && This->info->desc->vtbl->value)
618 return This->info->desc->vtbl->value(This, lcid, flags, params, res, ei, caller);
620 switch(flags) {
621 case DISPATCH_PROPERTYGET:
622 V_VT(res) = VT_BSTR;
623 V_BSTR(res) = SysAllocString(objectW);
624 if(!V_BSTR(res))
625 return E_OUTOFMEMORY;
626 break;
627 default:
628 FIXME("Unimplemented flags %x\n", flags);
629 return E_NOTIMPL;
632 return S_OK;
635 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
636 EXCEPINFO *ei)
638 DISPPARAMS params = {dp->rgvarg+dp->cNamedArgs, NULL, dp->cArgs-dp->cNamedArgs, 0};
639 ITypeInfo *ti;
640 IUnknown *unk;
641 UINT argerr=0;
642 HRESULT hres;
644 hres = get_typeinfo(func->tid, &ti);
645 if(FAILED(hres)) {
646 ERR("Could not get type info: %08x\n", hres);
647 return hres;
650 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
651 if(FAILED(hres)) {
652 ERR("Could not get iface %s: %08x\n", debugstr_mshtml_guid(tid_ids[func->tid]), hres);
653 return E_FAIL;
656 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, &params, res, ei, &argerr);
658 IUnknown_Release(unk);
659 return hres;
662 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
664 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
667 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
669 func_disp_t *This = impl_from_IUnknown(iface);
671 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
673 if(IsEqualGUID(&IID_IUnknown, riid)) {
674 *ppv = &This->IUnknown_iface;
675 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
676 return *ppv ? S_OK : E_NOINTERFACE;
677 }else {
678 *ppv = NULL;
679 return E_NOINTERFACE;
682 IUnknown_AddRef((IUnknown*)*ppv);
683 return S_OK;
686 static ULONG WINAPI Function_AddRef(IUnknown *iface)
688 func_disp_t *This = impl_from_IUnknown(iface);
689 LONG ref = InterlockedIncrement(&This->ref);
691 TRACE("(%p) ref=%d\n", This, ref);
693 return ref;
696 static ULONG WINAPI Function_Release(IUnknown *iface)
698 func_disp_t *This = impl_from_IUnknown(iface);
699 LONG ref = InterlockedDecrement(&This->ref);
701 TRACE("(%p) ref=%d\n", This, ref);
703 if(!ref) {
704 assert(!This->obj);
705 release_dispex(&This->dispex);
706 heap_free(This);
709 return ref;
712 static const IUnknownVtbl FunctionUnkVtbl = {
713 Function_QueryInterface,
714 Function_AddRef,
715 Function_Release
718 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
720 return CONTAINING_RECORD(iface, func_disp_t, dispex);
723 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
724 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
726 func_disp_t *This = impl_from_DispatchEx(dispex);
727 HRESULT hres;
729 switch(flags) {
730 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
731 if(!res)
732 return E_INVALIDARG;
733 /* fall through */
734 case DISPATCH_METHOD:
735 if(!This->obj)
736 return E_UNEXPECTED;
737 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
738 break;
739 case DISPATCH_PROPERTYGET: {
740 unsigned name_len;
741 WCHAR *ptr;
742 BSTR str;
744 static const WCHAR func_prefixW[] =
745 {'\n','f','u','n','c','t','i','o','n',' '};
746 static const WCHAR func_suffixW[] =
747 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
749 /* FIXME: This probably should be more generic. Also we should try to get IID_IActiveScriptSite and SID_GetCaller. */
750 if(!caller)
751 return E_ACCESSDENIED;
753 name_len = SysStringLen(This->info->name);
754 ptr = str = SysAllocStringLen(NULL, name_len + (sizeof(func_prefixW)+sizeof(func_suffixW))/sizeof(WCHAR));
755 if(!str)
756 return E_OUTOFMEMORY;
758 memcpy(ptr, func_prefixW, sizeof(func_prefixW));
759 ptr += sizeof(func_prefixW)/sizeof(WCHAR);
761 memcpy(ptr, This->info->name, name_len*sizeof(WCHAR));
762 ptr += name_len;
764 memcpy(ptr, func_suffixW, sizeof(func_suffixW));
766 V_VT(res) = VT_BSTR;
767 V_BSTR(res) = str;
768 return S_OK;
770 default:
771 FIXME("Unimplemented flags %x\n", flags);
772 hres = E_NOTIMPL;
775 return hres;
778 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
779 function_value,
780 NULL,
781 NULL,
782 NULL
785 static const tid_t function_iface_tids[] = {0};
787 static dispex_static_data_t function_dispex = {
788 &function_dispex_vtbl,
789 NULL_tid,
790 function_iface_tids
793 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
795 func_disp_t *ret;
797 ret = heap_alloc_zero(sizeof(func_disp_t));
798 if(!ret)
799 return NULL;
801 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
802 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
803 ret->ref = 1;
804 ret->obj = obj;
805 ret->info = info;
807 return ret;
810 static HRESULT invoke_disp_value(DispatchEx *This, IDispatch *func_disp, LCID lcid, WORD flags, DISPPARAMS *dp,
811 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
813 DISPID named_arg = DISPID_THIS;
814 DISPPARAMS new_dp = {NULL, &named_arg, 0, 1};
815 IDispatchEx *dispex;
816 HRESULT hres;
818 if(dp->cNamedArgs) {
819 FIXME("named args not supported\n");
820 return E_NOTIMPL;
823 new_dp.rgvarg = heap_alloc((dp->cArgs+1)*sizeof(VARIANTARG));
824 if(!new_dp.rgvarg)
825 return E_OUTOFMEMORY;
827 new_dp.cArgs = dp->cArgs+1;
828 memcpy(new_dp.rgvarg+1, dp->rgvarg, dp->cArgs*sizeof(VARIANTARG));
830 V_VT(new_dp.rgvarg) = VT_DISPATCH;
831 V_DISPATCH(new_dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
833 hres = IDispatch_QueryInterface(func_disp, &IID_IDispatchEx, (void**)&dispex);
834 TRACE(">>>\n");
835 if(SUCCEEDED(hres)) {
836 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, flags, &new_dp, res, ei, caller);
837 IDispatchEx_Release(dispex);
838 }else {
839 ULONG err = 0;
840 hres = IDispatch_Invoke(func_disp, DISPID_VALUE, &IID_NULL, lcid, flags, &new_dp, res, ei, &err);
842 if(SUCCEEDED(hres))
843 TRACE("<<< %s\n", debugstr_variant(res));
844 else
845 WARN("<<< %08x\n", hres);
847 heap_free(new_dp.rgvarg);
848 return hres;
851 static HRESULT get_func_obj_entry(DispatchEx *This, func_info_t *func, func_obj_entry_t **ret)
853 dispex_dynamic_data_t *dynamic_data;
854 func_obj_entry_t *entry;
856 dynamic_data = get_dynamic_data(This);
857 if(!dynamic_data)
858 return E_OUTOFMEMORY;
860 if(!dynamic_data->func_disps) {
861 dynamic_data->func_disps = heap_alloc_zero(This->info->func_disp_cnt * sizeof(*dynamic_data->func_disps));
862 if(!dynamic_data->func_disps)
863 return E_OUTOFMEMORY;
866 entry = dynamic_data->func_disps + func->func_disp_idx;
867 if(!entry->func_obj) {
868 entry->func_obj = create_func_disp(This, func);
869 if(!entry->func_obj)
870 return E_OUTOFMEMORY;
872 IDispatchEx_AddRef(&entry->func_obj->dispex.IDispatchEx_iface);
873 V_VT(&entry->val) = VT_DISPATCH;
874 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
877 *ret = entry;
878 return S_OK;
881 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
883 int min, max, n;
885 min = 0;
886 max = data->func_cnt-1;
888 while(min <= max) {
889 n = (min+max)/2;
891 if(data->funcs[n].id == id) {
892 *ret = data->funcs+n;
893 return S_OK;
896 if(data->funcs[n].id < id)
897 min = n+1;
898 else
899 max = n-1;
902 WARN("invalid id %x\n", id);
903 return DISP_E_UNKNOWNNAME;
906 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
908 int min, max, n, c;
910 min = 0;
911 max = This->info->func_cnt-1;
913 while(min <= max) {
914 n = (min+max)/2;
916 c = strcmpiW(This->info->name_table[n]->name, name);
917 if(!c) {
918 if((grfdex & fdexNameCaseSensitive) && strcmpW(This->info->name_table[n]->name, name))
919 break;
921 *ret = This->info->name_table[n]->id;
922 return S_OK;
925 if(c > 0)
926 max = n-1;
927 else
928 min = n+1;
931 if(This->info->desc->vtbl && This->info->desc->vtbl->get_dispid) {
932 HRESULT hres;
934 hres = This->info->desc->vtbl->get_dispid(This, name, grfdex, ret);
935 if(hres != DISP_E_UNKNOWNNAME)
936 return hres;
939 return DISP_E_UNKNOWNNAME;
942 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
944 V_VT(dst) = VT_EMPTY;
946 if(caller) {
947 IVariantChangeType *change_type = NULL;
948 HRESULT hres;
950 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
951 if(SUCCEEDED(hres)) {
952 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
953 IVariantChangeType_Release(change_type);
954 return hres;
958 switch(vt) {
959 case VT_BOOL:
960 if(V_VT(src) == VT_BSTR) {
961 V_VT(dst) = VT_BOOL;
962 V_BOOL(dst) = V_BSTR(src) && *V_BSTR(src) ? VARIANT_TRUE : VARIANT_FALSE;
963 return S_OK;
965 break;
968 return VariantChangeType(dst, src, 0, vt);
971 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
973 IUnknown *iface;
974 HRESULT hres;
976 if(dp && dp->cArgs) {
977 FIXME("cArgs %d\n", dp->cArgs);
978 return E_NOTIMPL;
981 assert(func->get_vtbl_off);
983 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
984 if(SUCCEEDED(hres)) {
985 switch(func->prop_vt) {
986 #define CASE_VT(vt,type,access) \
987 case vt: { \
988 type val; \
989 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
990 if(SUCCEEDED(hres)) \
991 access(res) = val; \
993 break
994 BUILTIN_TYPES_SWITCH;
995 #undef CASE_VT
996 default:
997 FIXME("Unhandled vt %d\n", func->prop_vt);
998 hres = E_NOTIMPL;
1000 IUnknown_Release(iface);
1003 if(FAILED(hres))
1004 return hres;
1006 if(func->prop_vt != VT_VARIANT)
1007 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
1008 return S_OK;
1011 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
1013 VARIANT *v, tmpv;
1014 IUnknown *iface;
1015 HRESULT hres;
1017 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1018 || dp->cNamedArgs > 1) {
1019 FIXME("invalid args\n");
1020 return E_INVALIDARG;
1023 if(!func->put_vtbl_off) {
1024 FIXME("No setter\n");
1025 return E_FAIL;
1028 v = dp->rgvarg;
1029 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
1030 hres = change_type(&tmpv, v, func->prop_vt, caller);
1031 if(FAILED(hres))
1032 return hres;
1033 v = &tmpv;
1036 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1037 if(SUCCEEDED(hres)) {
1038 switch(func->prop_vt) {
1039 #define CASE_VT(vt,type,access) \
1040 case vt: \
1041 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
1042 break
1043 BUILTIN_TYPES_SWITCH;
1044 #undef CASE_VT
1045 default:
1046 FIXME("Unimplemented vt %d\n", func->prop_vt);
1047 hres = E_NOTIMPL;
1050 IUnknown_Release(iface);
1053 if(v == &tmpv)
1054 VariantClear(v);
1055 return hres;
1058 static HRESULT invoke_builtin_function(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res, IServiceProvider *caller)
1060 VARIANT arg_buf[MAX_ARGS], *arg_ptrs[MAX_ARGS], *arg, retv, ret_ref, vhres;
1061 unsigned i, nconv = 0;
1062 IUnknown *iface;
1063 HRESULT hres;
1065 if(dp->cNamedArgs) {
1066 FIXME("Named arguments not supported\n");
1067 return E_NOTIMPL;
1070 if(dp->cArgs != func->argc) {
1071 FIXME("Invalid argument count (expected %u, got %u)\n", func->argc, dp->cArgs);
1072 return E_INVALIDARG;
1075 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
1076 if(FAILED(hres))
1077 return hres;
1079 for(i=0; i < func->argc; i++) {
1080 arg = dp->rgvarg+dp->cArgs-i-1;
1081 if(func->arg_types[i] == V_VT(arg)) {
1082 arg_ptrs[i] = arg;
1083 }else {
1084 hres = change_type(arg_buf+nconv, arg, func->arg_types[i], caller);
1085 if(FAILED(hres))
1086 break;
1087 arg_ptrs[i] = arg_buf + nconv++;
1091 if(SUCCEEDED(hres)) {
1092 if(func->prop_vt == VT_VOID) {
1093 V_VT(&retv) = VT_EMPTY;
1094 }else {
1095 V_VT(&retv) = func->prop_vt;
1096 arg_ptrs[func->argc] = &ret_ref;
1097 V_VT(&ret_ref) = VT_BYREF|func->prop_vt;
1099 switch(func->prop_vt) {
1100 #define CASE_VT(vt,type,access) \
1101 case vt: \
1102 V_BYREF(&ret_ref) = &access(&retv); \
1103 break
1104 BUILTIN_TYPES_SWITCH;
1105 #undef CASE_VT
1106 default:
1107 assert(0);
1111 V_VT(&vhres) = VT_ERROR;
1112 hres = DispCallFunc(iface, func->call_vtbl_off*sizeof(void*), CC_STDCALL, VT_ERROR,
1113 func->argc + (func->prop_vt == VT_VOID ? 0 : 1), func->arg_types, arg_ptrs, &vhres);
1116 while(nconv--)
1117 VariantClear(arg_buf+nconv);
1118 IUnknown_Release(iface);
1119 if(FAILED(hres))
1120 return hres;
1121 if(FAILED(V_ERROR(&vhres)))
1122 return V_ERROR(&vhres);
1124 if(res)
1125 *res = retv;
1126 else
1127 VariantClear(&retv);
1128 return V_ERROR(&vhres);
1131 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
1132 EXCEPINFO *ei, IServiceProvider *caller)
1134 HRESULT hres;
1136 switch(flags) {
1137 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1138 if(!res)
1139 return E_INVALIDARG;
1140 /* fall through */
1141 case DISPATCH_METHOD:
1142 if(This->dynamic_data && This->dynamic_data->func_disps
1143 && This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1144 func_obj_entry_t *entry = This->dynamic_data->func_disps + func->func_disp_idx;
1146 if(V_VT(&entry->val) != VT_DISPATCH) {
1147 FIXME("calling %s not supported\n", debugstr_variant(&entry->val));
1148 return E_NOTIMPL;
1151 if((IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface != V_DISPATCH(&entry->val)) {
1152 if(!V_DISPATCH(&entry->val)) {
1153 FIXME("Calling null\n");
1154 return E_FAIL;
1157 hres = invoke_disp_value(This, V_DISPATCH(&entry->val), 0, flags, dp, res, ei, NULL);
1158 break;
1162 if(func->call_vtbl_off)
1163 hres = invoke_builtin_function(This, func, dp, res, caller);
1164 else
1165 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1166 break;
1167 case DISPATCH_PROPERTYGET: {
1168 func_obj_entry_t *entry;
1170 if(func->id == DISPID_VALUE) {
1171 BSTR ret;
1173 ret = SysAllocString(objectW);
1174 if(!ret)
1175 return E_OUTOFMEMORY;
1177 V_VT(res) = VT_BSTR;
1178 V_BSTR(res) = ret;
1179 return S_OK;
1182 hres = get_func_obj_entry(This, func, &entry);
1183 if(FAILED(hres))
1184 return hres;
1186 V_VT(res) = VT_EMPTY;
1187 return VariantCopy(res, &entry->val);
1189 case DISPATCH_PROPERTYPUT: {
1190 func_obj_entry_t *entry;
1192 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1193 || dp->cNamedArgs > 1) {
1194 FIXME("invalid args\n");
1195 return E_INVALIDARG;
1199 * NOTE: Although we have IDispatchEx tests showing, that it's not allowed to set
1200 * function property using InvokeEx, it's possible to do that from jscript.
1201 * Native probably uses some undocumented interface in this case, but it should
1202 * be fine for us to allow IDispatchEx handle that.
1204 hres = get_func_obj_entry(This, func, &entry);
1205 if(FAILED(hres))
1206 return hres;
1208 return VariantCopy(&entry->val, dp->rgvarg);
1210 default:
1211 FIXME("Unimplemented flags %x\n", flags);
1212 hres = E_NOTIMPL;
1215 return hres;
1218 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
1219 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1221 func_info_t *func;
1222 HRESULT hres;
1224 hres = get_builtin_func(This->info, id, &func);
1225 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
1226 return dispex_value(This, lcid, flags, dp, res, ei, caller);
1227 if(FAILED(hres))
1228 return hres;
1230 if(func->func_disp_idx != -1)
1231 return function_invoke(This, func, flags, dp, res, ei, caller);
1233 switch(flags) {
1234 case DISPATCH_PROPERTYPUT:
1235 if(res)
1236 V_VT(res) = VT_EMPTY;
1237 hres = builtin_propput(This, func, dp, caller);
1238 break;
1239 case DISPATCH_PROPERTYGET:
1240 hres = builtin_propget(This, func, dp, res);
1241 break;
1242 default:
1243 if(!func->get_vtbl_off) {
1244 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
1245 }else {
1246 VARIANT v;
1248 hres = builtin_propget(This, func, NULL, &v);
1249 if(FAILED(hres))
1250 return hres;
1252 if(flags != (DISPATCH_PROPERTYGET|DISPATCH_METHOD) || dp->cArgs) {
1253 if(V_VT(&v) != VT_DISPATCH) {
1254 FIXME("Not a function %s flags %08x\n", debugstr_variant(&v), flags);
1255 VariantClear(&v);
1256 return E_FAIL;
1259 hres = invoke_disp_value(This, V_DISPATCH(&v), lcid, flags, dp, res, ei, caller);
1260 IDispatch_Release(V_DISPATCH(&v));
1261 }else if(res) {
1262 *res = v;
1263 }else {
1264 VariantClear(&v);
1269 return hres;
1272 HRESULT remove_attribute(DispatchEx *This, DISPID id, VARIANT_BOOL *success)
1274 switch(get_dispid_type(id)) {
1275 case DISPEXPROP_CUSTOM:
1276 FIXME("DISPEXPROP_CUSTOM not supported\n");
1277 return E_NOTIMPL;
1279 case DISPEXPROP_DYNAMIC: {
1280 DWORD idx = id - DISPID_DYNPROP_0;
1281 dynamic_prop_t *prop;
1283 prop = This->dynamic_data->props+idx;
1284 VariantClear(&prop->var);
1285 prop->flags |= DYNPROP_DELETED;
1286 *success = VARIANT_TRUE;
1287 return S_OK;
1289 case DISPEXPROP_BUILTIN: {
1290 VARIANT var;
1291 DISPPARAMS dp = {&var,NULL,1,0};
1292 func_info_t *func;
1293 HRESULT hres;
1295 hres = get_builtin_func(This->info, id, &func);
1296 if(FAILED(hres))
1297 return hres;
1299 /* For builtin functions, we set their value to the original function. */
1300 if(func->func_disp_idx != -1) {
1301 func_obj_entry_t *entry;
1303 if(!This->dynamic_data || !This->dynamic_data->func_disps
1304 || !This->dynamic_data->func_disps[func->func_disp_idx].func_obj) {
1305 *success = VARIANT_FALSE;
1306 return S_OK;
1309 entry = This->dynamic_data->func_disps + func->func_disp_idx;
1310 if(V_VT(&entry->val) == VT_DISPATCH
1311 && V_DISPATCH(&entry->val) == (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface) {
1312 *success = VARIANT_FALSE;
1313 return S_OK;
1316 VariantClear(&entry->val);
1317 V_VT(&entry->val) = VT_DISPATCH;
1318 V_DISPATCH(&entry->val) = (IDispatch*)&entry->func_obj->dispex.IDispatchEx_iface;
1319 IDispatch_AddRef(V_DISPATCH(&entry->val));
1320 *success = VARIANT_TRUE;
1321 return S_OK;
1324 V_VT(&var) = VT_EMPTY;
1325 hres = builtin_propput(This, func, &dp, NULL);
1326 if(FAILED(hres))
1327 return hres;
1329 *success = VARIANT_TRUE;
1330 return S_OK;
1332 default:
1333 assert(0);
1334 return E_FAIL;
1338 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
1340 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
1343 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1345 DispatchEx *This = impl_from_IDispatchEx(iface);
1347 return IUnknown_QueryInterface(This->outer, riid, ppv);
1350 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
1352 DispatchEx *This = impl_from_IDispatchEx(iface);
1354 return IUnknown_AddRef(This->outer);
1357 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
1359 DispatchEx *This = impl_from_IDispatchEx(iface);
1361 return IUnknown_Release(This->outer);
1364 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1366 DispatchEx *This = impl_from_IDispatchEx(iface);
1368 TRACE("(%p)->(%p)\n", This, pctinfo);
1370 *pctinfo = 1;
1371 return S_OK;
1374 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1375 LCID lcid, ITypeInfo **ppTInfo)
1377 DispatchEx *This = impl_from_IDispatchEx(iface);
1378 HRESULT hres;
1380 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1382 hres = get_typeinfo(This->info->desc->disp_tid, ppTInfo);
1383 if(FAILED(hres))
1384 return hres;
1386 ITypeInfo_AddRef(*ppTInfo);
1387 return S_OK;
1390 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1391 LPOLESTR *rgszNames, UINT cNames,
1392 LCID lcid, DISPID *rgDispId)
1394 DispatchEx *This = impl_from_IDispatchEx(iface);
1395 UINT i;
1396 HRESULT hres;
1398 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1399 lcid, rgDispId);
1401 for(i=0; i < cNames; i++) {
1402 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1403 if(FAILED(hres))
1404 return hres;
1407 return S_OK;
1410 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1411 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1412 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1414 DispatchEx *This = impl_from_IDispatchEx(iface);
1416 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1417 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1419 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1420 pVarResult, pExcepInfo, NULL);
1423 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1425 DispatchEx *This = impl_from_IDispatchEx(iface);
1426 dynamic_prop_t *dprop;
1427 HRESULT hres;
1429 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1431 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1432 FIXME("Unsupported grfdex %x\n", grfdex);
1434 hres = get_builtin_id(This, bstrName, grfdex, pid);
1435 if(hres != DISP_E_UNKNOWNNAME)
1436 return hres;
1438 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1439 if(FAILED(hres))
1440 return hres;
1442 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1443 return S_OK;
1446 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1447 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1449 DispatchEx *This = impl_from_IDispatchEx(iface);
1450 HRESULT hres;
1452 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1454 if(wFlags == (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF))
1455 wFlags = DISPATCH_PROPERTYPUT;
1457 switch(get_dispid_type(id)) {
1458 case DISPEXPROP_CUSTOM:
1459 if(!This->info->desc->vtbl || !This->info->desc->vtbl->invoke)
1460 return DISP_E_UNKNOWNNAME;
1461 return This->info->desc->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1463 case DISPEXPROP_DYNAMIC: {
1464 DWORD idx = id - DISPID_DYNPROP_0;
1465 dynamic_prop_t *prop;
1467 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1468 return DISP_E_UNKNOWNNAME;
1470 prop = This->dynamic_data->props+idx;
1472 switch(wFlags) {
1473 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1474 if(!pvarRes)
1475 return E_INVALIDARG;
1476 /* fall through */
1477 case DISPATCH_METHOD:
1478 if(V_VT(&prop->var) != VT_DISPATCH) {
1479 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1480 return E_NOTIMPL;
1483 return invoke_disp_value(This, V_DISPATCH(&prop->var), lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1484 case DISPATCH_PROPERTYGET:
1485 if(prop->flags & DYNPROP_DELETED)
1486 return DISP_E_UNKNOWNNAME;
1487 V_VT(pvarRes) = VT_EMPTY;
1488 return variant_copy(pvarRes, &prop->var);
1489 case DISPATCH_PROPERTYPUT:
1490 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1491 || pdp->cNamedArgs > 1) {
1492 FIXME("invalid args\n");
1493 return E_INVALIDARG;
1496 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1497 VariantClear(&prop->var);
1498 hres = variant_copy(&prop->var, pdp->rgvarg);
1499 if(FAILED(hres))
1500 return hres;
1502 prop->flags &= ~DYNPROP_DELETED;
1503 return S_OK;
1504 default:
1505 FIXME("unhandled wFlags %x\n", wFlags);
1506 return E_NOTIMPL;
1509 case DISPEXPROP_BUILTIN:
1510 if(wFlags == DISPATCH_CONSTRUCT) {
1511 if(id == DISPID_VALUE) {
1512 if(This->info->desc->vtbl && This->info->desc->vtbl->value) {
1513 return This->info->desc->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1515 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1516 return E_FAIL;
1518 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1519 return E_FAIL;
1522 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1523 default:
1524 assert(0);
1525 return E_FAIL;
1529 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1531 DispatchEx *This = impl_from_IDispatchEx(iface);
1533 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1535 /* Not implemented by IE */
1536 return E_NOTIMPL;
1539 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1541 DispatchEx *This = impl_from_IDispatchEx(iface);
1543 TRACE("(%p)->(%x)\n", This, id);
1545 /* Not implemented by IE */
1546 return E_NOTIMPL;
1549 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1551 DispatchEx *This = impl_from_IDispatchEx(iface);
1552 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1553 return E_NOTIMPL;
1556 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1558 DispatchEx *This = impl_from_IDispatchEx(iface);
1559 func_info_t *func;
1560 HRESULT hres;
1562 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1564 if(is_dynamic_dispid(id)) {
1565 DWORD idx = id - DISPID_DYNPROP_0;
1567 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1568 return DISP_E_UNKNOWNNAME;
1570 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1571 if(!*pbstrName)
1572 return E_OUTOFMEMORY;
1574 return S_OK;
1577 hres = get_builtin_func(This->info, id, &func);
1578 if(FAILED(hres))
1579 return hres;
1581 *pbstrName = SysAllocString(func->name);
1582 if(!*pbstrName)
1583 return E_OUTOFMEMORY;
1584 return S_OK;
1587 static HRESULT next_dynamic_id(DispatchEx *dispex, DWORD idx, DISPID *ret_id)
1589 while(idx < dispex->dynamic_data->prop_cnt && dispex->dynamic_data->props[idx].flags & DYNPROP_DELETED)
1590 idx++;
1592 if(idx == dispex->dynamic_data->prop_cnt) {
1593 *ret_id = DISPID_STARTENUM;
1594 return S_FALSE;
1597 *ret_id = DISPID_DYNPROP_0+idx;
1598 return S_OK;
1601 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1603 DispatchEx *This = impl_from_IDispatchEx(iface);
1604 func_info_t *func;
1605 HRESULT hres;
1607 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1609 if(is_dynamic_dispid(id)) {
1610 DWORD idx = id - DISPID_DYNPROP_0;
1612 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1613 return DISP_E_UNKNOWNNAME;
1615 return next_dynamic_id(This, idx+1, pid);
1618 if(id == DISPID_STARTENUM) {
1619 func = This->info->funcs;
1620 }else {
1621 hres = get_builtin_func(This->info, id, &func);
1622 if(FAILED(hres))
1623 return hres;
1624 func++;
1627 while(func < This->info->funcs + This->info->func_cnt) {
1628 /* FIXME: Skip hidden properties */
1629 if(func->func_disp_idx == -1) {
1630 *pid = func->id;
1631 return S_OK;
1633 func++;
1636 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt)
1637 return next_dynamic_id(This, 0, pid);
1639 *pid = DISPID_STARTENUM;
1640 return S_FALSE;
1643 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1645 DispatchEx *This = impl_from_IDispatchEx(iface);
1646 FIXME("(%p)->(%p)\n", This, ppunk);
1647 return E_NOTIMPL;
1650 static IDispatchExVtbl DispatchExVtbl = {
1651 DispatchEx_QueryInterface,
1652 DispatchEx_AddRef,
1653 DispatchEx_Release,
1654 DispatchEx_GetTypeInfoCount,
1655 DispatchEx_GetTypeInfo,
1656 DispatchEx_GetIDsOfNames,
1657 DispatchEx_Invoke,
1658 DispatchEx_GetDispID,
1659 DispatchEx_InvokeEx,
1660 DispatchEx_DeleteMemberByName,
1661 DispatchEx_DeleteMemberByDispID,
1662 DispatchEx_GetMemberProperties,
1663 DispatchEx_GetMemberName,
1664 DispatchEx_GetNextDispID,
1665 DispatchEx_GetNameSpaceParent
1668 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1670 if(IsEqualGUID(&IID_IDispatch, riid))
1671 *ppv = &This->IDispatchEx_iface;
1672 else if(IsEqualGUID(&IID_IDispatchEx, riid))
1673 *ppv = &This->IDispatchEx_iface;
1674 else if(IsEqualGUID(&IID_IDispatchJS, riid))
1675 *ppv = NULL;
1676 else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid))
1677 *ppv = NULL;
1678 else
1679 return FALSE;
1681 if(*ppv)
1682 IUnknown_AddRef((IUnknown*)*ppv);
1683 return TRUE;
1686 void dispex_traverse(DispatchEx *This, nsCycleCollectionTraversalCallback *cb)
1688 dynamic_prop_t *prop;
1690 if(!This->dynamic_data)
1691 return;
1693 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1694 if(V_VT(&prop->var) == VT_DISPATCH)
1695 note_cc_edge((nsISupports*)V_DISPATCH(&prop->var), "dispex_data", cb);
1698 /* FIXME: Traverse func_disps */
1701 void dispex_unlink(DispatchEx *This)
1703 dynamic_prop_t *prop;
1705 if(!This->dynamic_data)
1706 return;
1708 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1709 if(V_VT(&prop->var) == VT_DISPATCH) {
1710 V_VT(&prop->var) = VT_EMPTY;
1711 IDispatch_Release(V_DISPATCH(&prop->var));
1712 }else {
1713 VariantClear(&prop->var);
1718 const dispex_static_data_vtbl_t *dispex_get_vtbl(DispatchEx *dispex)
1720 return dispex->info->desc->vtbl;
1723 void release_dispex(DispatchEx *This)
1725 dynamic_prop_t *prop;
1727 if(!This->dynamic_data)
1728 return;
1730 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1731 VariantClear(&prop->var);
1732 heap_free(prop->name);
1735 heap_free(This->dynamic_data->props);
1737 if(This->dynamic_data->func_disps) {
1738 func_obj_entry_t *iter;
1740 for(iter = This->dynamic_data->func_disps; iter < This->dynamic_data->func_disps + This->info->func_disp_cnt; iter++) {
1741 if(iter->func_obj) {
1742 iter->func_obj->obj = NULL;
1743 IDispatchEx_Release(&iter->func_obj->dispex.IDispatchEx_iface);
1745 VariantClear(&iter->val);
1748 heap_free(This->dynamic_data->func_disps);
1751 heap_free(This->dynamic_data);
1754 void init_dispex_with_compat_mode(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data, compat_mode_t compat_mode)
1756 assert(compat_mode < COMPAT_MODE_CNT);
1758 if(!data->info_cache[compat_mode]) {
1759 EnterCriticalSection(&cs_dispex_static_data);
1760 if(!data->info_cache[compat_mode])
1761 data->info_cache[compat_mode] = preprocess_dispex_data(data, compat_mode);
1762 LeaveCriticalSection(&cs_dispex_static_data);
1765 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1766 dispex->outer = outer;
1767 dispex->info = data->info_cache[compat_mode];
1768 dispex->dynamic_data = NULL;