wined3d: Use the correct source pitch and byte_count for uploading converted surfaces.
[wine/multimedia.git] / dlls / vbscript / vbdisp.c
blob5032f4acae7d80fd0fac542bcaff9f621e3a2678
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 "vbscript.h"
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
25 static inline BOOL is_func_id(vbdisp_t *This, DISPID id)
27 return id < This->desc->func_cnt;
30 static BOOL get_func_id(vbdisp_t *This, const WCHAR *name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
32 unsigned i;
34 for(i = invoke_type == VBDISP_ANY ? 0 : 1; i < This->desc->func_cnt; i++) {
35 if(invoke_type == VBDISP_ANY) {
36 if(!search_private && !This->desc->funcs[i].is_public)
37 continue;
38 if(!i && !This->desc->funcs[0].name) /* default value may not exist */
39 continue;
40 }else {
41 if(!This->desc->funcs[i].entries[invoke_type]
42 || (!search_private && !This->desc->funcs[i].entries[invoke_type]->is_public))
43 continue;
46 if(!strcmpiW(This->desc->funcs[i].name, name)) {
47 *id = i;
48 return TRUE;
52 return FALSE;
55 HRESULT vbdisp_get_id(vbdisp_t *This, BSTR name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
57 unsigned i;
59 if(get_func_id(This, name, invoke_type, search_private, id))
60 return S_OK;
62 for(i=0; i < This->desc->prop_cnt; i++) {
63 if(!search_private && !This->desc->props[i].is_public)
64 continue;
66 if(!strcmpiW(This->desc->props[i].name, name)) {
67 *id = i + This->desc->func_cnt;
68 return S_OK;
72 if(This->desc->typeinfo) {
73 HRESULT hres;
75 hres = ITypeInfo_GetIDsOfNames(This->desc->typeinfo, &name, 1, id);
76 if(SUCCEEDED(hres))
77 return S_OK;
80 *id = -1;
81 return DISP_E_UNKNOWNNAME;
84 static VARIANT *get_propput_arg(const DISPPARAMS *dp)
86 unsigned i;
88 for(i=0; i < dp->cNamedArgs; i++) {
89 if(dp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
90 return dp->rgvarg+i;
93 return NULL;
96 static HRESULT invoke_variant_prop(vbdisp_t *This, VARIANT *v, WORD flags, DISPPARAMS *dp, VARIANT *res)
98 HRESULT hres;
100 switch(flags) {
101 case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
102 if(dp->cArgs) {
103 WARN("called with arguments\n");
104 return DISP_E_MEMBERNOTFOUND; /* That's what tests show */
107 hres = VariantCopy(res, v);
108 break;
110 case DISPATCH_PROPERTYPUT: {
111 VARIANT *put_val;
113 put_val = get_propput_arg(dp);
114 if(!put_val) {
115 WARN("no value to set\n");
116 return DISP_E_PARAMNOTOPTIONAL;
119 if(res)
120 V_VT(res) = VT_EMPTY;
122 hres = VariantCopy(v, put_val);
123 break;
126 default:
127 FIXME("unimplemented flags %x\n", flags);
128 return E_NOTIMPL;
131 return hres;
134 static HRESULT invoke_builtin(vbdisp_t *This, const builtin_prop_t *prop, WORD flags, DISPPARAMS *dp, VARIANT *res)
136 VARIANT *args;
138 switch(flags) {
139 case DISPATCH_PROPERTYGET:
140 if(!(prop->flags & (BP_GET|BP_GETPUT))) {
141 FIXME("property does not support DISPATCH_PROPERTYGET\n");
142 return E_FAIL;
144 break;
145 case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
146 break;
147 case DISPATCH_METHOD:
148 if(prop->flags & (BP_GET|BP_GETPUT)) {
149 FIXME("Call on property\n");
150 return E_FAIL;
152 break;
153 case DISPATCH_PROPERTYPUT:
154 if(!(prop->flags & (BP_GET|BP_GETPUT))) {
155 FIXME("property does not support DISPATCH_PROPERTYPUT\n");
156 return E_FAIL;
159 FIXME("call put\n");
160 return E_NOTIMPL;
161 default:
162 FIXME("unsupported flags %x\n", flags);
163 return E_NOTIMPL;
166 if(arg_cnt(dp) < prop->min_args || arg_cnt(dp) > (prop->max_args ? prop->max_args : prop->min_args)) {
167 FIXME("invalid number of arguments\n");
168 return E_FAIL;
171 if(arg_cnt(dp) == 1 && V_VT(dp->rgvarg) == (VT_BYREF|VT_VARIANT))
172 args = V_VARIANTREF(dp->rgvarg);
173 else
174 args = dp->rgvarg;
176 return prop->proc(This, args, dp->cArgs, res);
179 static BOOL run_terminator(vbdisp_t *This)
181 DISPPARAMS dp = {0};
183 if(This->terminator_ran)
184 return TRUE;
185 This->terminator_ran = TRUE;
187 if(!This->desc->class_terminate_id)
188 return TRUE;
190 This->ref++;
191 exec_script(This->desc->ctx, This->desc->funcs[This->desc->class_terminate_id].entries[VBDISP_CALLGET],
192 (IDispatch*)&This->IDispatchEx_iface, &dp, NULL);
193 return !--This->ref;
196 static void clean_props(vbdisp_t *This)
198 unsigned i;
200 if(!This->desc)
201 return;
203 for(i=0; i < This->desc->prop_cnt; i++)
204 VariantClear(This->props+i);
207 static inline vbdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
209 return CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface);
212 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
214 vbdisp_t *This = impl_from_IDispatchEx(iface);
216 if(IsEqualGUID(&IID_IUnknown, riid)) {
217 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
218 *ppv = &This->IDispatchEx_iface;
219 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
220 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
221 *ppv = &This->IDispatchEx_iface;
222 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
223 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
224 *ppv = &This->IDispatchEx_iface;
225 }else {
226 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
227 *ppv = NULL;
228 return E_NOINTERFACE;
231 IUnknown_AddRef((IUnknown*)*ppv);
232 return S_OK;
235 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
237 vbdisp_t *This = impl_from_IDispatchEx(iface);
238 LONG ref = InterlockedIncrement(&This->ref);
240 TRACE("(%p) ref=%d\n", This, ref);
242 return ref;
245 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
247 vbdisp_t *This = impl_from_IDispatchEx(iface);
248 LONG ref = InterlockedDecrement(&This->ref);
250 TRACE("(%p) ref=%d\n", This, ref);
252 if(!ref && run_terminator(This)) {
253 clean_props(This);
254 list_remove(&This->entry);
255 heap_free(This);
258 return ref;
261 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
263 vbdisp_t *This = impl_from_IDispatchEx(iface);
265 TRACE("(%p)->(%p)\n", This, pctinfo);
267 *pctinfo = 1;
268 return S_OK;
271 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
272 ITypeInfo **ppTInfo)
274 vbdisp_t *This = impl_from_IDispatchEx(iface);
275 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
276 return E_NOTIMPL;
279 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
280 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
281 DISPID *rgDispId)
283 vbdisp_t *This = impl_from_IDispatchEx(iface);
284 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
285 lcid, rgDispId);
286 return E_NOTIMPL;
289 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
290 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
291 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
293 vbdisp_t *This = impl_from_IDispatchEx(iface);
294 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
295 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
296 return E_NOTIMPL;
299 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
301 vbdisp_t *This = impl_from_IDispatchEx(iface);
303 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
305 if(!This->desc)
306 return E_UNEXPECTED;
308 if(grfdex & ~(fdexNameEnsure|fdexNameCaseInsensitive)) {
309 FIXME("unsupported flags %x\n", grfdex);
310 return E_NOTIMPL;
313 return vbdisp_get_id(This, bstrName, VBDISP_ANY, FALSE, pid);
316 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
317 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
319 vbdisp_t *This = impl_from_IDispatchEx(iface);
321 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
323 if(!This->desc)
324 return E_UNEXPECTED;
326 if(pvarRes)
327 V_VT(pvarRes) = VT_EMPTY;
329 if(id < 0)
330 return DISP_E_MEMBERNOTFOUND;
332 if(is_func_id(This, id)) {
333 function_t *func;
335 switch(wFlags) {
336 case DISPATCH_METHOD:
337 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
338 func = This->desc->funcs[id].entries[VBDISP_CALLGET];
339 if(!func) {
340 FIXME("no invoke/getter\n");
341 return DISP_E_MEMBERNOTFOUND;
344 return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, pdp, pvarRes);
345 case DISPATCH_PROPERTYPUT: {
346 VARIANT *put_val;
347 DISPPARAMS dp = {NULL, NULL, 1, 0};
349 if(arg_cnt(pdp)) {
350 FIXME("arguments not implemented\n");
351 return E_NOTIMPL;
354 put_val = get_propput_arg(pdp);
355 if(!put_val) {
356 WARN("no value to set\n");
357 return DISP_E_PARAMNOTOPTIONAL;
360 dp.rgvarg = put_val;
361 func = This->desc->funcs[id].entries[V_VT(put_val) == VT_DISPATCH ? VBDISP_SET : VBDISP_LET];
362 if(!func) {
363 FIXME("no letter/setter\n");
364 return DISP_E_MEMBERNOTFOUND;
367 return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, &dp, NULL);
369 default:
370 FIXME("flags %x\n", wFlags);
371 return DISP_E_MEMBERNOTFOUND;
375 if(id < This->desc->prop_cnt + This->desc->func_cnt)
376 return invoke_variant_prop(This, This->props+(id-This->desc->func_cnt), wFlags, pdp, pvarRes);
378 if(This->desc->builtin_prop_cnt) {
379 unsigned min = 0, max = This->desc->builtin_prop_cnt-1, i;
381 while(min <= max) {
382 i = (min+max)/2;
383 if(This->desc->builtin_props[i].id == id)
384 return invoke_builtin(This, This->desc->builtin_props+i, wFlags, pdp, pvarRes);
385 if(This->desc->builtin_props[i].id < id)
386 min = i+1;
387 else
388 max = i-1;
392 return DISP_E_MEMBERNOTFOUND;
395 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
397 vbdisp_t *This = impl_from_IDispatchEx(iface);
398 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
399 return E_NOTIMPL;
402 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
404 vbdisp_t *This = impl_from_IDispatchEx(iface);
405 FIXME("(%p)->(%x)\n", This, id);
406 return E_NOTIMPL;
409 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
411 vbdisp_t *This = impl_from_IDispatchEx(iface);
412 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
413 return E_NOTIMPL;
416 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
418 vbdisp_t *This = impl_from_IDispatchEx(iface);
419 FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
420 return E_NOTIMPL;
423 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
425 vbdisp_t *This = impl_from_IDispatchEx(iface);
426 FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
427 return E_NOTIMPL;
430 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
432 vbdisp_t *This = impl_from_IDispatchEx(iface);
433 FIXME("(%p)->(%p)\n", This, ppunk);
434 return E_NOTIMPL;
437 static IDispatchExVtbl DispatchExVtbl = {
438 DispatchEx_QueryInterface,
439 DispatchEx_AddRef,
440 DispatchEx_Release,
441 DispatchEx_GetTypeInfoCount,
442 DispatchEx_GetTypeInfo,
443 DispatchEx_GetIDsOfNames,
444 DispatchEx_Invoke,
445 DispatchEx_GetDispID,
446 DispatchEx_InvokeEx,
447 DispatchEx_DeleteMemberByName,
448 DispatchEx_DeleteMemberByDispID,
449 DispatchEx_GetMemberProperties,
450 DispatchEx_GetMemberName,
451 DispatchEx_GetNextDispID,
452 DispatchEx_GetNameSpaceParent
455 static inline vbdisp_t *unsafe_impl_from_IDispatch(IDispatch *iface)
457 return iface->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl
458 ? CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface)
459 : NULL;
462 HRESULT create_vbdisp(const class_desc_t *desc, vbdisp_t **ret)
464 vbdisp_t *vbdisp;
466 vbdisp = heap_alloc_zero( FIELD_OFFSET( vbdisp_t, props[desc->prop_cnt] ));
467 if(!vbdisp)
468 return E_OUTOFMEMORY;
470 vbdisp->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
471 vbdisp->ref = 1;
472 vbdisp->desc = desc;
474 if(desc->class_initialize_id) {
475 DISPPARAMS dp = {0};
476 HRESULT hres;
478 hres = exec_script(desc->ctx, desc->funcs[desc->class_initialize_id].entries[VBDISP_CALLGET],
479 (IDispatch*)&vbdisp->IDispatchEx_iface, &dp, NULL);
480 if(FAILED(hres)) {
481 IDispatchEx_Release(&vbdisp->IDispatchEx_iface);
482 return hres;
486 list_add_tail(&desc->ctx->objects, &vbdisp->entry);
487 *ret = vbdisp;
488 return S_OK;
491 void collect_objects(script_ctx_t *ctx)
493 vbdisp_t *iter, *iter2;
495 LIST_FOR_EACH_ENTRY_SAFE(iter, iter2, &ctx->objects, vbdisp_t, entry)
496 run_terminator(iter);
498 while(!list_empty(&ctx->objects)) {
499 iter = LIST_ENTRY(list_head(&ctx->objects), vbdisp_t, entry);
501 IDispatchEx_AddRef(&iter->IDispatchEx_iface);
502 clean_props(iter);
503 iter->desc = NULL;
504 list_remove(&iter->entry);
505 list_init(&iter->entry);
506 IDispatchEx_Release(&iter->IDispatchEx_iface);
510 HRESULT disp_get_id(IDispatch *disp, BSTR name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
512 IDispatchEx *dispex;
513 vbdisp_t *vbdisp;
514 HRESULT hres;
516 vbdisp = unsafe_impl_from_IDispatch(disp);
517 if(vbdisp)
518 return vbdisp_get_id(vbdisp, name, invoke_type, search_private, id);
520 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
521 if(FAILED(hres)) {
522 TRACE("unsing IDispatch\n");
523 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
526 hres = IDispatchEx_GetDispID(dispex, name, fdexNameCaseInsensitive, id);
527 IDispatchEx_Release(dispex);
528 return hres;
531 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, DISPPARAMS *dp, VARIANT *retv)
533 const WORD flags = DISPATCH_METHOD|(retv ? DISPATCH_PROPERTYGET : 0);
534 IDispatchEx *dispex;
535 EXCEPINFO ei;
536 HRESULT hres;
538 memset(&ei, 0, sizeof(ei));
539 if(retv)
540 V_VT(retv) = VT_EMPTY;
542 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
543 if(FAILED(hres)) {
544 UINT err = 0;
546 TRACE("using IDispatch\n");
547 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei, &err);
550 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei, NULL /* CALLER_FIXME */);
551 IDispatchEx_Release(dispex);
552 return hres;
555 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val)
557 DISPID propput_dispid = DISPID_PROPERTYPUT;
558 DISPPARAMS dp = {val, &propput_dispid, 1, 1};
559 IDispatchEx *dispex;
560 EXCEPINFO ei = {0};
561 HRESULT hres;
563 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
564 if(SUCCEEDED(hres)) {
565 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL /* FIXME! */);
566 IDispatchEx_Release(dispex);
567 }else {
568 ULONG err = 0;
570 TRACE("using IDispatch\n");
571 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, &err);
574 return hres;