2 * Copyright 2011 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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript
);
27 #define FDEX_VERSION_MASK 0xf0000000
29 static inline BOOL
is_func_id(vbdisp_t
*This
, DISPID id
)
31 return id
< This
->desc
->func_cnt
;
34 static BOOL
get_func_id(vbdisp_t
*This
, const WCHAR
*name
, vbdisp_invoke_type_t invoke_type
, BOOL search_private
, DISPID
*id
)
38 for(i
= invoke_type
== VBDISP_ANY
? 0 : 1; i
< This
->desc
->func_cnt
; i
++) {
39 if(invoke_type
== VBDISP_ANY
) {
40 if(!search_private
&& !This
->desc
->funcs
[i
].is_public
)
42 if(!i
&& !This
->desc
->funcs
[0].name
) /* default value may not exist */
45 if(!This
->desc
->funcs
[i
].entries
[invoke_type
]
46 || (!search_private
&& !This
->desc
->funcs
[i
].entries
[invoke_type
]->is_public
))
50 if(!strcmpiW(This
->desc
->funcs
[i
].name
, name
)) {
59 HRESULT
vbdisp_get_id(vbdisp_t
*This
, BSTR name
, vbdisp_invoke_type_t invoke_type
, BOOL search_private
, DISPID
*id
)
63 if(get_func_id(This
, name
, invoke_type
, search_private
, id
))
66 for(i
=0; i
< This
->desc
->prop_cnt
; i
++) {
67 if(!search_private
&& !This
->desc
->props
[i
].is_public
)
70 if(!strcmpiW(This
->desc
->props
[i
].name
, name
)) {
71 *id
= i
+ This
->desc
->func_cnt
;
76 if(This
->desc
->typeinfo
) {
79 hres
= ITypeInfo_GetIDsOfNames(This
->desc
->typeinfo
, &name
, 1, id
);
85 return DISP_E_UNKNOWNNAME
;
88 static VARIANT
*get_propput_arg(const DISPPARAMS
*dp
)
92 for(i
=0; i
< dp
->cNamedArgs
; i
++) {
93 if(dp
->rgdispidNamedArgs
[i
] == DISPID_PROPERTYPUT
)
100 static HRESULT
invoke_variant_prop(vbdisp_t
*This
, VARIANT
*v
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
)
105 case DISPATCH_PROPERTYGET
|DISPATCH_METHOD
:
107 WARN("called with arguments\n");
108 return DISP_E_MEMBERNOTFOUND
; /* That's what tests show */
111 hres
= VariantCopy(res
, v
);
114 case DISPATCH_PROPERTYPUT
: {
117 put_val
= get_propput_arg(dp
);
119 WARN("no value to set\n");
120 return DISP_E_PARAMNOTOPTIONAL
;
124 V_VT(res
) = VT_EMPTY
;
126 hres
= VariantCopy(v
, put_val
);
131 FIXME("unimplemented flags %x\n", flags
);
138 static HRESULT
invoke_builtin(vbdisp_t
*This
, const builtin_prop_t
*prop
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*res
)
140 VARIANT
*args
, arg_buf
[8];
144 case DISPATCH_PROPERTYGET
:
145 if(!(prop
->flags
& (BP_GET
|BP_GETPUT
))) {
146 FIXME("property does not support DISPATCH_PROPERTYGET\n");
150 case DISPATCH_PROPERTYGET
|DISPATCH_METHOD
:
152 case DISPATCH_METHOD
:
153 if(prop
->flags
& (BP_GET
|BP_GETPUT
)) {
154 FIXME("Call on property\n");
158 case DISPATCH_PROPERTYPUT
:
159 if(!(prop
->flags
& (BP_GET
|BP_GETPUT
))) {
160 FIXME("property does not support DISPATCH_PROPERTYPUT\n");
167 FIXME("unsupported flags %x\n", flags
);
173 if(argn
< prop
->min_args
|| argn
> (prop
->max_args
? prop
->max_args
: prop
->min_args
)) {
174 FIXME("invalid number of arguments\n");
181 if(V_VT(dp
->rgvarg
) == (VT_BYREF
|VT_VARIANT
))
182 args
= V_VARIANTREF(dp
->rgvarg
);
186 assert(argn
< sizeof(arg_buf
)/sizeof(*arg_buf
));
188 for(i
=0; i
< argn
; i
++) {
189 if(V_VT(dp
->rgvarg
+i
) == (VT_BYREF
|VT_VARIANT
)) {
190 for(i
=0; i
< argn
; i
++) {
191 if(V_VT(dp
->rgvarg
+i
) == (VT_BYREF
|VT_VARIANT
))
192 arg_buf
[i
] = *V_VARIANTREF(dp
->rgvarg
+i
);
194 arg_buf
[i
] = dp
->rgvarg
[i
];
202 return prop
->proc(This
, args
, dp
->cArgs
, res
);
205 static BOOL
run_terminator(vbdisp_t
*This
)
209 if(This
->terminator_ran
)
211 This
->terminator_ran
= TRUE
;
213 if(!This
->desc
->class_terminate_id
)
217 exec_script(This
->desc
->ctx
, This
->desc
->funcs
[This
->desc
->class_terminate_id
].entries
[VBDISP_CALLGET
],
218 (IDispatch
*)&This
->IDispatchEx_iface
, &dp
, NULL
);
222 static void clean_props(vbdisp_t
*This
)
229 for(i
=0; i
< This
->desc
->prop_cnt
; i
++)
230 VariantClear(This
->props
+i
);
233 static inline vbdisp_t
*impl_from_IDispatchEx(IDispatchEx
*iface
)
235 return CONTAINING_RECORD(iface
, vbdisp_t
, IDispatchEx_iface
);
238 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
240 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
242 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
243 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
244 *ppv
= &This
->IDispatchEx_iface
;
245 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
246 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
247 *ppv
= &This
->IDispatchEx_iface
;
248 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
249 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
250 *ppv
= &This
->IDispatchEx_iface
;
252 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
254 return E_NOINTERFACE
;
257 IUnknown_AddRef((IUnknown
*)*ppv
);
261 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
263 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
264 LONG ref
= InterlockedIncrement(&This
->ref
);
266 TRACE("(%p) ref=%d\n", This
, ref
);
271 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
273 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
274 LONG ref
= InterlockedDecrement(&This
->ref
);
276 TRACE("(%p) ref=%d\n", This
, ref
);
278 if(!ref
&& run_terminator(This
)) {
280 list_remove(&This
->entry
);
287 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
289 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
291 TRACE("(%p)->(%p)\n", This
, pctinfo
);
297 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
, LCID lcid
,
300 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
301 FIXME("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
305 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
306 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
,
309 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
310 FIXME("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
315 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
316 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
317 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
319 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
320 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
321 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
325 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
327 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
329 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
331 grfdex
&= ~FDEX_VERSION_MASK
;
336 if(grfdex
& ~(fdexNameEnsure
|fdexNameCaseInsensitive
)) {
337 FIXME("unsupported flags %x\n", grfdex
);
341 return vbdisp_get_id(This
, bstrName
, VBDISP_ANY
, FALSE
, pid
);
344 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
345 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
347 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
349 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
355 V_VT(pvarRes
) = VT_EMPTY
;
358 return DISP_E_MEMBERNOTFOUND
;
360 if(is_func_id(This
, id
)) {
364 case DISPATCH_METHOD
:
365 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
366 func
= This
->desc
->funcs
[id
].entries
[VBDISP_CALLGET
];
368 FIXME("no invoke/getter\n");
369 return DISP_E_MEMBERNOTFOUND
;
372 return exec_script(This
->desc
->ctx
, func
, (IDispatch
*)&This
->IDispatchEx_iface
, pdp
, pvarRes
);
373 case DISPATCH_PROPERTYPUT
: {
375 DISPPARAMS dp
= {NULL
, NULL
, 1, 0};
378 FIXME("arguments not implemented\n");
382 put_val
= get_propput_arg(pdp
);
384 WARN("no value to set\n");
385 return DISP_E_PARAMNOTOPTIONAL
;
389 func
= This
->desc
->funcs
[id
].entries
[V_VT(put_val
) == VT_DISPATCH
? VBDISP_SET
: VBDISP_LET
];
391 FIXME("no letter/setter\n");
392 return DISP_E_MEMBERNOTFOUND
;
395 return exec_script(This
->desc
->ctx
, func
, (IDispatch
*)&This
->IDispatchEx_iface
, &dp
, NULL
);
398 FIXME("flags %x\n", wFlags
);
399 return DISP_E_MEMBERNOTFOUND
;
403 if(id
< This
->desc
->prop_cnt
+ This
->desc
->func_cnt
)
404 return invoke_variant_prop(This
, This
->props
+(id
-This
->desc
->func_cnt
), wFlags
, pdp
, pvarRes
);
406 if(This
->desc
->builtin_prop_cnt
) {
407 unsigned min
= 0, max
= This
->desc
->builtin_prop_cnt
-1, i
;
411 if(This
->desc
->builtin_props
[i
].id
== id
)
412 return invoke_builtin(This
, This
->desc
->builtin_props
+i
, wFlags
, pdp
, pvarRes
);
413 if(This
->desc
->builtin_props
[i
].id
< id
)
420 return DISP_E_MEMBERNOTFOUND
;
423 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
425 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
426 FIXME("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
430 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
432 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
433 FIXME("(%p)->(%x)\n", This
, id
);
437 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
439 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
440 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
444 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
446 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
447 FIXME("(%p)->(%x %p)\n", This
, id
, pbstrName
);
451 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
453 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
454 FIXME("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
458 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
460 vbdisp_t
*This
= impl_from_IDispatchEx(iface
);
461 FIXME("(%p)->(%p)\n", This
, ppunk
);
465 static IDispatchExVtbl DispatchExVtbl
= {
466 DispatchEx_QueryInterface
,
469 DispatchEx_GetTypeInfoCount
,
470 DispatchEx_GetTypeInfo
,
471 DispatchEx_GetIDsOfNames
,
473 DispatchEx_GetDispID
,
475 DispatchEx_DeleteMemberByName
,
476 DispatchEx_DeleteMemberByDispID
,
477 DispatchEx_GetMemberProperties
,
478 DispatchEx_GetMemberName
,
479 DispatchEx_GetNextDispID
,
480 DispatchEx_GetNameSpaceParent
483 static inline vbdisp_t
*unsafe_impl_from_IDispatch(IDispatch
*iface
)
485 return iface
->lpVtbl
== (IDispatchVtbl
*)&DispatchExVtbl
486 ? CONTAINING_RECORD(iface
, vbdisp_t
, IDispatchEx_iface
)
490 HRESULT
create_vbdisp(const class_desc_t
*desc
, vbdisp_t
**ret
)
494 vbdisp
= heap_alloc_zero( FIELD_OFFSET( vbdisp_t
, props
[desc
->prop_cnt
] ));
496 return E_OUTOFMEMORY
;
498 vbdisp
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
502 if(desc
->class_initialize_id
) {
506 hres
= exec_script(desc
->ctx
, desc
->funcs
[desc
->class_initialize_id
].entries
[VBDISP_CALLGET
],
507 (IDispatch
*)&vbdisp
->IDispatchEx_iface
, &dp
, NULL
);
509 IDispatchEx_Release(&vbdisp
->IDispatchEx_iface
);
514 list_add_tail(&desc
->ctx
->objects
, &vbdisp
->entry
);
519 void collect_objects(script_ctx_t
*ctx
)
521 vbdisp_t
*iter
, *iter2
;
523 LIST_FOR_EACH_ENTRY_SAFE(iter
, iter2
, &ctx
->objects
, vbdisp_t
, entry
)
524 run_terminator(iter
);
526 while(!list_empty(&ctx
->objects
)) {
527 iter
= LIST_ENTRY(list_head(&ctx
->objects
), vbdisp_t
, entry
);
529 IDispatchEx_AddRef(&iter
->IDispatchEx_iface
);
532 list_remove(&iter
->entry
);
533 list_init(&iter
->entry
);
534 IDispatchEx_Release(&iter
->IDispatchEx_iface
);
538 HRESULT
disp_get_id(IDispatch
*disp
, BSTR name
, vbdisp_invoke_type_t invoke_type
, BOOL search_private
, DISPID
*id
)
544 vbdisp
= unsafe_impl_from_IDispatch(disp
);
546 return vbdisp_get_id(vbdisp
, name
, invoke_type
, search_private
, id
);
548 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
550 TRACE("using IDispatch\n");
551 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
554 hres
= IDispatchEx_GetDispID(dispex
, name
, fdexNameCaseInsensitive
, id
);
555 IDispatchEx_Release(dispex
);
559 HRESULT
disp_call(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, DISPPARAMS
*dp
, VARIANT
*retv
)
561 const WORD flags
= DISPATCH_METHOD
|(retv
? DISPATCH_PROPERTYGET
: 0);
566 memset(&ei
, 0, sizeof(ei
));
568 V_VT(retv
) = VT_EMPTY
;
570 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
574 TRACE("using IDispatch\n");
575 return IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, dp
, retv
, &ei
, &err
);
578 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, dp
, retv
, &ei
, NULL
/* CALLER_FIXME */);
579 IDispatchEx_Release(dispex
);
583 HRESULT
disp_propput(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, DISPPARAMS
*dp
)
589 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
590 if(SUCCEEDED(hres
)) {
591 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, dp
, NULL
, &ei
, NULL
/* FIXME! */);
592 IDispatchEx_Release(dispex
);
596 TRACE("using IDispatch\n");
597 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, dp
, NULL
, &ei
, &err
);