user32: Avoid TRUE:FALSE conditional expressions.
[wine/multimedia.git] / dlls / vbscript / vbdisp.c
blob5f9d70a64f4b7936618e7dfcffb341659a978443
1 /*
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
19 #include <assert.h>
21 #include "vbscript.h"
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)
36 unsigned i;
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)
41 continue;
42 if(!i && !This->desc->funcs[0].name) /* default value may not exist */
43 continue;
44 }else {
45 if(!This->desc->funcs[i].entries[invoke_type]
46 || (!search_private && !This->desc->funcs[i].entries[invoke_type]->is_public))
47 continue;
50 if(!strcmpiW(This->desc->funcs[i].name, name)) {
51 *id = i;
52 return TRUE;
56 return FALSE;
59 HRESULT vbdisp_get_id(vbdisp_t *This, BSTR name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
61 unsigned i;
63 if(get_func_id(This, name, invoke_type, search_private, id))
64 return S_OK;
66 for(i=0; i < This->desc->prop_cnt; i++) {
67 if(!search_private && !This->desc->props[i].is_public)
68 continue;
70 if(!strcmpiW(This->desc->props[i].name, name)) {
71 *id = i + This->desc->func_cnt;
72 return S_OK;
76 if(This->desc->typeinfo) {
77 HRESULT hres;
79 hres = ITypeInfo_GetIDsOfNames(This->desc->typeinfo, &name, 1, id);
80 if(SUCCEEDED(hres))
81 return S_OK;
84 *id = -1;
85 return DISP_E_UNKNOWNNAME;
88 static VARIANT *get_propput_arg(const DISPPARAMS *dp)
90 unsigned i;
92 for(i=0; i < dp->cNamedArgs; i++) {
93 if(dp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
94 return dp->rgvarg+i;
97 return NULL;
100 static HRESULT invoke_variant_prop(vbdisp_t *This, VARIANT *v, WORD flags, DISPPARAMS *dp, VARIANT *res)
102 HRESULT hres;
104 switch(flags) {
105 case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
106 if(dp->cArgs) {
107 WARN("called with arguments\n");
108 return DISP_E_MEMBERNOTFOUND; /* That's what tests show */
111 hres = VariantCopy(res, v);
112 break;
114 case DISPATCH_PROPERTYPUT: {
115 VARIANT *put_val;
117 put_val = get_propput_arg(dp);
118 if(!put_val) {
119 WARN("no value to set\n");
120 return DISP_E_PARAMNOTOPTIONAL;
123 if(res)
124 V_VT(res) = VT_EMPTY;
126 hres = VariantCopy(v, put_val);
127 break;
130 default:
131 FIXME("unimplemented flags %x\n", flags);
132 return E_NOTIMPL;
135 return hres;
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];
141 unsigned argn;
143 switch(flags) {
144 case DISPATCH_PROPERTYGET:
145 if(!(prop->flags & (BP_GET|BP_GETPUT))) {
146 FIXME("property does not support DISPATCH_PROPERTYGET\n");
147 return E_FAIL;
149 break;
150 case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
151 break;
152 case DISPATCH_METHOD:
153 if(prop->flags & (BP_GET|BP_GETPUT)) {
154 FIXME("Call on property\n");
155 return E_FAIL;
157 break;
158 case DISPATCH_PROPERTYPUT:
159 if(!(prop->flags & (BP_GET|BP_GETPUT))) {
160 FIXME("property does not support DISPATCH_PROPERTYPUT\n");
161 return E_FAIL;
164 FIXME("call put\n");
165 return E_NOTIMPL;
166 default:
167 FIXME("unsupported flags %x\n", flags);
168 return E_NOTIMPL;
171 argn = arg_cnt(dp);
173 if(argn < prop->min_args || argn > (prop->max_args ? prop->max_args : prop->min_args)) {
174 FIXME("invalid number of arguments\n");
175 return E_FAIL;
178 args = dp->rgvarg;
180 if(argn == 1) {
181 if(V_VT(dp->rgvarg) == (VT_BYREF|VT_VARIANT))
182 args = V_VARIANTREF(dp->rgvarg);
183 }else {
184 unsigned i;
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);
193 else
194 arg_buf[i] = dp->rgvarg[i];
196 args = arg_buf;
197 break;
202 return prop->proc(This, args, dp->cArgs, res);
205 static BOOL run_terminator(vbdisp_t *This)
207 DISPPARAMS dp = {0};
209 if(This->terminator_ran)
210 return TRUE;
211 This->terminator_ran = TRUE;
213 if(!This->desc->class_terminate_id)
214 return TRUE;
216 This->ref++;
217 exec_script(This->desc->ctx, This->desc->funcs[This->desc->class_terminate_id].entries[VBDISP_CALLGET],
218 (IDispatch*)&This->IDispatchEx_iface, &dp, NULL);
219 return !--This->ref;
222 static void clean_props(vbdisp_t *This)
224 unsigned i;
226 if(!This->desc)
227 return;
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;
251 }else {
252 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
253 *ppv = NULL;
254 return E_NOINTERFACE;
257 IUnknown_AddRef((IUnknown*)*ppv);
258 return S_OK;
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);
268 return 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)) {
279 clean_props(This);
280 list_remove(&This->entry);
281 heap_free(This);
284 return ref;
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);
293 *pctinfo = 1;
294 return S_OK;
297 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
298 ITypeInfo **ppTInfo)
300 vbdisp_t *This = impl_from_IDispatchEx(iface);
301 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
302 return E_NOTIMPL;
305 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
306 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
307 DISPID *rgDispId)
309 vbdisp_t *This = impl_from_IDispatchEx(iface);
310 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
311 lcid, rgDispId);
312 return E_NOTIMPL;
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);
322 return E_NOTIMPL;
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;
333 if(!This->desc)
334 return E_UNEXPECTED;
336 if(grfdex & ~(fdexNameEnsure|fdexNameCaseInsensitive)) {
337 FIXME("unsupported flags %x\n", grfdex);
338 return E_NOTIMPL;
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);
351 if(!This->desc)
352 return E_UNEXPECTED;
354 if(pvarRes)
355 V_VT(pvarRes) = VT_EMPTY;
357 if(id < 0)
358 return DISP_E_MEMBERNOTFOUND;
360 if(is_func_id(This, id)) {
361 function_t *func;
363 switch(wFlags) {
364 case DISPATCH_METHOD:
365 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
366 func = This->desc->funcs[id].entries[VBDISP_CALLGET];
367 if(!func) {
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: {
374 VARIANT *put_val;
375 DISPPARAMS dp = {NULL, NULL, 1, 0};
377 if(arg_cnt(pdp)) {
378 FIXME("arguments not implemented\n");
379 return E_NOTIMPL;
382 put_val = get_propput_arg(pdp);
383 if(!put_val) {
384 WARN("no value to set\n");
385 return DISP_E_PARAMNOTOPTIONAL;
388 dp.rgvarg = put_val;
389 func = This->desc->funcs[id].entries[V_VT(put_val) == VT_DISPATCH ? VBDISP_SET : VBDISP_LET];
390 if(!func) {
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);
397 default:
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;
409 while(min <= max) {
410 i = (min+max)/2;
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)
414 min = i+1;
415 else
416 max = i-1;
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);
427 return E_NOTIMPL;
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);
434 return E_NOTIMPL;
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);
441 return E_NOTIMPL;
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);
448 return E_NOTIMPL;
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);
455 return E_NOTIMPL;
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);
462 return E_NOTIMPL;
465 static IDispatchExVtbl DispatchExVtbl = {
466 DispatchEx_QueryInterface,
467 DispatchEx_AddRef,
468 DispatchEx_Release,
469 DispatchEx_GetTypeInfoCount,
470 DispatchEx_GetTypeInfo,
471 DispatchEx_GetIDsOfNames,
472 DispatchEx_Invoke,
473 DispatchEx_GetDispID,
474 DispatchEx_InvokeEx,
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)
487 : NULL;
490 HRESULT create_vbdisp(const class_desc_t *desc, vbdisp_t **ret)
492 vbdisp_t *vbdisp;
494 vbdisp = heap_alloc_zero( FIELD_OFFSET( vbdisp_t, props[desc->prop_cnt] ));
495 if(!vbdisp)
496 return E_OUTOFMEMORY;
498 vbdisp->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
499 vbdisp->ref = 1;
500 vbdisp->desc = desc;
502 if(desc->class_initialize_id) {
503 DISPPARAMS dp = {0};
504 HRESULT hres;
506 hres = exec_script(desc->ctx, desc->funcs[desc->class_initialize_id].entries[VBDISP_CALLGET],
507 (IDispatch*)&vbdisp->IDispatchEx_iface, &dp, NULL);
508 if(FAILED(hres)) {
509 IDispatchEx_Release(&vbdisp->IDispatchEx_iface);
510 return hres;
514 list_add_tail(&desc->ctx->objects, &vbdisp->entry);
515 *ret = vbdisp;
516 return S_OK;
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);
530 clean_props(iter);
531 iter->desc = NULL;
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)
540 IDispatchEx *dispex;
541 vbdisp_t *vbdisp;
542 HRESULT hres;
544 vbdisp = unsafe_impl_from_IDispatch(disp);
545 if(vbdisp)
546 return vbdisp_get_id(vbdisp, name, invoke_type, search_private, id);
548 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
549 if(FAILED(hres)) {
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);
556 return hres;
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);
562 IDispatchEx *dispex;
563 EXCEPINFO ei;
564 HRESULT hres;
566 memset(&ei, 0, sizeof(ei));
567 if(retv)
568 V_VT(retv) = VT_EMPTY;
570 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
571 if(FAILED(hres)) {
572 UINT err = 0;
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);
580 return hres;
583 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, DISPPARAMS *dp)
585 IDispatchEx *dispex;
586 EXCEPINFO ei = {0};
587 HRESULT hres;
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);
593 }else {
594 ULONG err = 0;
596 TRACE("using IDispatch\n");
597 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, dp, NULL, &ei, &err);
600 return hres;