Debian Codegraph @

basic/source/classes/sbunoobj.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <sal/config.h>
21
22 #include <osl/diagnose.h>
23 #include <o3tl/any.hxx>
24 #include <o3tl/safeint.hxx>
25 #include <utility>
26 #include <vcl/svapp.hxx>
27 #include <comphelper/errcode.hxx>
28 #include <svl/hint.hxx>
29
30 #include <cppuhelper/implbase.hxx>
31 #include <cppuhelper/exc_hlp.hxx>
32 #include <comphelper/interfacecontainer4.hxx>
33 #include <comphelper/extract.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <cppuhelper/weakref.hxx>
36
37 #include <rtl/math.hxx>
38 #include <rtl/ustrbuf.hxx>
39
40 #include <com/sun/star/script/ArrayWrapper.hpp>
41 #include <com/sun/star/script/CannotConvertException.hpp>
42 #include <com/sun/star/script/NativeObjectWrapper.hpp>
43 #include <com/sun/star/sheet/XSheetCellCursor.hpp>
44
45 #include <com/sun/star/uno/XComponentContext.hpp>
46 #include <com/sun/star/uno/DeploymentException.hpp>
47 #include <com/sun/star/lang/XTypeProvider.hpp>
48 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 #include <com/sun/star/lang/XServiceInfo.hpp>
51 #include <com/sun/star/beans/PropertyAttribute.hpp>
52 #include <com/sun/star/beans/PropertyConcept.hpp>
53 #include <com/sun/star/beans/MethodConcept.hpp>
54 #include <com/sun/star/beans/XPropertySet.hpp>
55 #include <com/sun/star/beans/theIntrospection.hpp>
56 #include <com/sun/star/script/BasicErrorException.hpp>
57 #include <com/sun/star/script/InvocationAdapterFactory.hpp>
58 #include <com/sun/star/script/XAllListener.hpp>
59 #include <com/sun/star/script/Converter.hpp>
60 #include <com/sun/star/script/XDefaultProperty.hpp>
61 #include <com/sun/star/script/XDirectInvocation.hpp>
62 #include <com/sun/star/container/XNameAccess.hpp>
63 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
64 #include <com/sun/star/reflection/XIdlArray.hpp>
65 #include <com/sun/star/reflection/XIdlReflection.hpp>
66 #include <com/sun/star/reflection/XServiceConstructorDescription.hpp>
67 #include <com/sun/star/reflection/XSingletonTypeDescription.hpp>
68 #include <com/sun/star/reflection/theCoreReflection.hpp>
69 #include <com/sun/star/bridge/oleautomation/NamedArgument.hpp>
70 #include <com/sun/star/bridge/oleautomation/Date.hpp>
71 #include <com/sun/star/bridge/oleautomation/Decimal.hpp>
72 #include <com/sun/star/bridge/oleautomation/Currency.hpp>
73 #include <com/sun/star/bridge/oleautomation/XAutomationObject.hpp>
74 #include <com/sun/star/script/XAutomationInvocation.hpp>
75
76 #include <rtlproto.hxx>
77
78 #include <basic/sbstar.hxx>
79 #include <basic/sbuno.hxx>
80 #include <basic/sberrors.hxx>
81 #include <sbunoobj.hxx>
82 #include <sbintern.hxx>
83 #include <runtime.hxx>
84
85 #include <algorithm>
86 #include <math.h>
87 #include <memory>
88 #include <string_view>
89 #include <unordered_map>
90 #include <com/sun/star/reflection/XTypeDescriptionEnumerationAccess.hpp>
91 #include <com/sun/star/reflection/XConstantsTypeDescription.hpp>
92
93 using com::sun::star::uno::Reference;
94 using namespace com::sun::star::uno;
95 using namespace com::sun::star::lang;
96 using namespace com::sun::star::reflection;
97 using namespace com::sun::star::beans;
98 using namespace com::sun::star::script;
99 using namespace com::sun::star::container;
100 using namespace com::sun::star::bridge;
101 using namespace cppu;
102
103
104 // Identifiers for creating the strings for dbg_Properties
105 constexpr OUString ID_DBG_SUPPORTEDINTERFACES = u"Dbg_SupportedInterfaces"_ustr;
106 constexpr OUString ID_DBG_PROPERTIES = u"Dbg_Properties"_ustr;
107 constexpr OUString ID_DBG_METHODS = u"Dbg_Methods"_ustr;
108
109 char const aSeqLevelStr[] = "[]";
110
111 // Gets the default property for a uno object. Note: There is some
112 // redirection built in. The property name specifies the name
113 // of the default property.
114
115 bool SbUnoObject::getDefaultPropName( SbUnoObject const * pUnoObj, OUString& sDfltProp )
116 {
117 bool bResult = false;
118 Reference< XDefaultProperty> xDefaultProp( pUnoObj->maTmpUnoObj, UNO_QUERY );
119 if ( xDefaultProp.is() )
120 {
121 sDfltProp = xDefaultProp->getDefaultPropertyName();
122 if ( !sDfltProp.isEmpty() )
123 bResult = true;
124 }
125 return bResult;
126 }
127
128 SbxVariable* getDefaultProp( SbxVariable* pRef )
129 {
130 SbxVariable* pDefaultProp = nullptr;
131 if ( pRef->GetType() == SbxOBJECT )
132 {
133 SbxObject* pObj = dynamic_cast<SbxObject*>(pRef);
134 if (!pObj)
135 {
136 SbxBase* pObjVarObj = pRef->GetObject();
137 pObj = dynamic_cast<SbxObject*>( pObjVarObj );
138 }
139 if (SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>(pObj))
140 {
141 pDefaultProp = pUnoObj->GetDfltProperty();
142 }
143 }
144 return pDefaultProp;
145 }
146
147 void SetSbUnoObjectDfltPropName( SbxObject* pObj )
148 {
149 SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>( pObj );
150 if ( pUnoObj )
151 {
152 OUString sDfltPropName;
153
154 if ( SbUnoObject::getDefaultPropName( pUnoObj, sDfltPropName ) )
155 {
156 pUnoObj->SetDfltProperty( sDfltPropName );
157 }
158 }
159 }
160
161 // save CoreReflection statically
162 static Reference< XIdlReflection > getCoreReflection_Impl()
163 {
164 return css::reflection::theCoreReflection::get(
165 comphelper::getProcessComponentContext());
166 }
167
168 // save CoreReflection statically
169 static Reference< XHierarchicalNameAccess > const & getCoreReflection_HierarchicalNameAccess_Impl()
170 {
171 static Reference< XHierarchicalNameAccess > xCoreReflection_HierarchicalNameAccess;
172
173 if( !xCoreReflection_HierarchicalNameAccess.is() )
174 {
175 Reference< XIdlReflection > xCoreReflection = getCoreReflection_Impl();
176 if( xCoreReflection.is() )
177 {
178 xCoreReflection_HierarchicalNameAccess =
179 Reference< XHierarchicalNameAccess >( xCoreReflection, UNO_QUERY );
180 }
181 }
182 return xCoreReflection_HierarchicalNameAccess;
183 }
184
185 // Hold TypeProvider statically
186 static Reference< XHierarchicalNameAccess > const & getTypeProvider_Impl()
187 {
188 static Reference< XHierarchicalNameAccess > xAccess;
189
190 // Do we have already CoreReflection; if not obtain it
191 if( !xAccess.is() )
192 {
193 const Reference< XComponentContext >& xContext(
194 comphelper::getProcessComponentContext() );
195 if( xContext.is() )
196 {
197 xContext->getValueByName(
198 u"/singletons/com.sun.star.reflection.theTypeDescriptionManager"_ustr )
199 >>= xAccess;
200 OSL_ENSURE( xAccess.is(), "### TypeDescriptionManager singleton not accessible!?" );
201 }
202 if( !xAccess.is() )
203 {
204 throw DeploymentException(
205 u"/singletons/com.sun.star.reflection.theTypeDescriptionManager singleton not accessible"_ustr );
206 }
207 }
208 return xAccess;
209 }
210
211 // Hold TypeConverter statically
212 static Reference< XTypeConverter > const & getTypeConverter_Impl()
213 {
214 static Reference< XTypeConverter > xTypeConverter;
215
216 // Do we have already CoreReflection; if not obtain it
217 if( !xTypeConverter.is() )
218 {
219 const Reference< XComponentContext >& xContext(
220 comphelper::getProcessComponentContext() );
221 if( xContext.is() )
222 {
223 xTypeConverter = Converter::create(xContext);
224 }
225 if( !xTypeConverter.is() )
226 {
227 throw DeploymentException(
228 u"com.sun.star.script.Converter service not accessible"_ustr );
229 }
230 }
231 return xTypeConverter;
232 }
233
234
235 // #111851 factory function to create an OLE object
236 SbUnoObject* createOLEObject_Impl( const OUString& aType )
237 {
238 static const Reference<XMultiServiceFactory> xOLEFactory = [] {
239 Reference<XMultiServiceFactory> xFactory;
240 const Reference< XComponentContext >& xContext( comphelper::getProcessComponentContext() );
241 if( xContext.is() )
242 {
243 Reference<XMultiComponentFactory> xSMgr = xContext->getServiceManager();
244 xFactory.set(
245 xSMgr->createInstanceWithContext( u"com.sun.star.bridge.OleObjectFactory"_ustr, xContext ),
246 UNO_QUERY );
247 }
248 return xFactory;
249 }();
250
251 SbUnoObject* pUnoObj = nullptr;
252 if( xOLEFactory.is() )
253 {
254 // some type names available in VBA can not be directly used in COM
255 OUString aOLEType = aType;
256 if ( aOLEType == "SAXXMLReader30" )
257 {
258 aOLEType = "Msxml2.SAXXMLReader.3.0";
259 }
260 Reference< XInterface > xOLEObject = xOLEFactory->createInstance( aOLEType );
261 if( xOLEObject.is() )
262 {
263 pUnoObj = new SbUnoObject( aType, Any(xOLEObject) );
264 OUString sDfltPropName;
265
266 if ( SbUnoObject::getDefaultPropName( pUnoObj, sDfltPropName ) )
267 pUnoObj->SetDfltProperty( sDfltPropName );
268 }
269 }
270 return pUnoObj;
271 }
272
273
274 namespace
275 {
276 void lcl_indent( OUStringBuffer& _inout_rBuffer, sal_Int32 _nLevel )
277 {
278 while ( _nLevel-- > 0 )
279 {
280 _inout_rBuffer.append( " " );
281 }
282 }
283 }
284
285 static void implAppendExceptionMsg( OUStringBuffer& _inout_rBuffer, const Exception& _e, std::u16string_view _rExceptionType, sal_Int32 _nLevel )
286 {
287 _inout_rBuffer.append( "\n" );
288 lcl_indent( _inout_rBuffer, _nLevel );
289 _inout_rBuffer.append( "Type: " );
290
291 if ( _rExceptionType.empty() )
292 _inout_rBuffer.append( "Unknown" );
293 else
294 _inout_rBuffer.append( _rExceptionType );
295
296 _inout_rBuffer.append( "\n" );
297 lcl_indent( _inout_rBuffer, _nLevel );
298 _inout_rBuffer.append( "Message: " );
299 _inout_rBuffer.append( _e.Message );
300
301 }
302
303 // construct an error message for the exception
304 static OUString implGetExceptionMsg( const Exception& e, std::u16string_view aExceptionType_ )
305 {
306 OUStringBuffer aMessageBuf;
307 implAppendExceptionMsg( aMessageBuf, e, aExceptionType_, 0 );
308 return aMessageBuf.makeStringAndClear();
309 }
310
311 static OUString implGetExceptionMsg( const Any& _rCaughtException )
312 {
313 auto e = o3tl::tryAccess<Exception>(_rCaughtException);
314 OSL_PRECOND( e, "implGetExceptionMsg: illegal argument!" );
315 if ( !e )
316 {
317 return OUString();
318 }
319 return implGetExceptionMsg( *e, _rCaughtException.getValueTypeName() );
320 }
321
322 static Any convertAny( const Any& rVal, const Type& aDestType )
323 {
324 Any aConvertedVal;
325 const Reference< XTypeConverter >& xConverter = getTypeConverter_Impl();
326 try
327 {
328 aConvertedVal = xConverter->convertTo( rVal, aDestType );
329 }
330 catch( const IllegalArgumentException& )
331 {
332 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION,
333 implGetExceptionMsg( ::cppu::getCaughtException() ) );
334 return aConvertedVal;
335 }
336 catch( const CannotConvertException& e2 )
337 {
338 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION,
339 implGetExceptionMsg( e2, u"com.sun.star.lang.IllegalArgumentException" ) );
340 return aConvertedVal;
341 }
342 return aConvertedVal;
343 }
344
345
346 // #105565 Special Object to wrap a strongly typed Uno Any
347
348
349 // TODO: source out later
350 static Reference<XIdlClass> TypeToIdlClass( const Type& rType )
351 {
352 return getCoreReflection_Impl()->forName(rType.getTypeName());
353 }
354
355 // Exception type unknown
356 template< class EXCEPTION >
357 static OUString implGetExceptionMsg( const EXCEPTION& e )
358 {
359 return implGetExceptionMsg( e, cppu::UnoType<decltype(e)>::get().getTypeName() );
360 }
361
362 static void implHandleBasicErrorException( BasicErrorException const & e )
363 {
364 ErrCode nError = StarBASIC::GetSfxFromVBError( static_cast<sal_uInt16>(e.ErrorCode) );
365 StarBASIC::Error( nError, e.ErrorMessageArgument );
366 }
367
368 static void implHandleWrappedTargetException( const Any& _rWrappedTargetException )
369 {
370 Any aExamine( _rWrappedTargetException );
371
372 // completely strip the first InvocationTargetException, its error message isn't of any
373 // interest to the user, it just says something like "invoking the UNO method went wrong.".
374 InvocationTargetException aInvocationError;
375 if ( aExamine >>= aInvocationError )
376 aExamine = aInvocationError.TargetException;
377
378 BasicErrorException aBasicError;
379
380 ErrCode nError( ERRCODE_BASIC_EXCEPTION );
381 OUStringBuffer aMessageBuf;
382
383 // strip any other WrappedTargetException instances, but this time preserve the error messages.
384 WrappedTargetException aWrapped;
385 sal_Int32 nLevel = 0;
386 while ( aExamine >>= aWrapped )
387 {
388 // special handling for BasicErrorException errors
389 if ( aWrapped.TargetException >>= aBasicError )
390 {
391 nError = StarBASIC::GetSfxFromVBError( static_cast<sal_uInt16>(aBasicError.ErrorCode) );
392 aMessageBuf.append( aBasicError.ErrorMessageArgument );
393 aExamine.clear();
394 break;
395 }
396
397 // append this round's message
398 implAppendExceptionMsg( aMessageBuf, aWrapped, aExamine.getValueTypeName(), nLevel );
399 if ( aWrapped.TargetException.getValueTypeClass() == TypeClass_EXCEPTION )
400 // there is a next chain element
401 aMessageBuf.append( "\nTargetException:" );
402
403 // next round
404 aExamine = aWrapped.TargetException;
405 ++nLevel;
406 }
407
408 if ( auto e = o3tl::tryAccess<Exception>(aExamine) )
409 {
410 // the last element in the chain is still an exception, but no WrappedTargetException
411 implAppendExceptionMsg( aMessageBuf, *e, aExamine.getValueTypeName(), nLevel );
412 }
413
414 StarBASIC::Error( nError, aMessageBuf.makeStringAndClear() );
415 }
416
417 static void implHandleAnyException( const Any& _rCaughtException )
418 {
419 BasicErrorException aBasicError;
420 WrappedTargetException aWrappedError;
421
422 if ( _rCaughtException >>= aBasicError )
423 {
424 implHandleBasicErrorException( aBasicError );
425 }
426 else if ( _rCaughtException >>= aWrappedError )
427 {
428 implHandleWrappedTargetException( _rCaughtException );
429 }
430 else
431 {
432 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION, implGetExceptionMsg( _rCaughtException ) );
433 }
434 }
435
436 namespace {
437
438 // NativeObjectWrapper handling
439 struct ObjectItem
440 {
441 SbxObjectRef m_xNativeObj;
442
443 explicit ObjectItem( SbxObject* pNativeObj )
444 : m_xNativeObj( pNativeObj )
445 {}
446 };
447
448 }
449
450 typedef std::vector< ObjectItem > NativeObjectWrapperVector;
451
452 namespace {
453
454 NativeObjectWrapperVector gaNativeObjectWrapperVector;
455
456 }
457
458 void clearNativeObjectWrapperVector()
459 {
460 gaNativeObjectWrapperVector.clear();
461 }
462
463 static sal_uInt32 lcl_registerNativeObjectWrapper( SbxObject* pNativeObj )
464 {
465 sal_uInt32 nIndex = gaNativeObjectWrapperVector.size();
466 gaNativeObjectWrapperVector.emplace_back( pNativeObj );
467 return nIndex;
468 }
469
470 static SbxObject* lcl_getNativeObject( sal_uInt32 nIndex )
471 {
472 SbxObjectRef xRetObj;
473 if( nIndex < gaNativeObjectWrapperVector.size() )
474 {
475 ObjectItem& rItem = gaNativeObjectWrapperVector[ nIndex ];
476 xRetObj = rItem.m_xNativeObj;
477 }
478 return xRetObj.get();
479 }
480
481 // convert from Uno to Sbx
482 static SbxDataType unoToSbxType( TypeClass eType )
483 {
484 SbxDataType eRetType = SbxVOID;
485
486 switch( eType )
487 {
488 case TypeClass_INTERFACE:
489 case TypeClass_TYPE:
490 case TypeClass_STRUCT:
491 case TypeClass_EXCEPTION: eRetType = SbxOBJECT; break;
492
493 case TypeClass_ENUM: eRetType = SbxLONG; break;
494 case TypeClass_SEQUENCE:
495 eRetType = SbxDataType( SbxOBJECT | SbxARRAY );
496 break;
497
498
499 case TypeClass_ANY: eRetType = SbxVARIANT; break;
500 case TypeClass_BOOLEAN: eRetType = SbxBOOL; break;
501 case TypeClass_CHAR: eRetType = SbxCHAR; break;
502 case TypeClass_STRING: eRetType = SbxSTRING; break;
503 case TypeClass_FLOAT: eRetType = SbxSINGLE; break;
504 case TypeClass_DOUBLE: eRetType = SbxDOUBLE; break;
505 case TypeClass_BYTE: eRetType = SbxINTEGER; break;
506 case TypeClass_SHORT: eRetType = SbxINTEGER; break;
507 case TypeClass_LONG: eRetType = SbxLONG; break;
508 case TypeClass_HYPER: eRetType = SbxSALINT64; break;
509 case TypeClass_UNSIGNED_SHORT: eRetType = SbxUSHORT; break;
510 case TypeClass_UNSIGNED_LONG: eRetType = SbxULONG; break;
511 case TypeClass_UNSIGNED_HYPER: eRetType = SbxSALUINT64;break;
512 default: break;
513 }
514 return eRetType;
515 }
516
517 static SbxDataType unoToSbxType( const Reference< XIdlClass >& xIdlClass )
518 {
519 SbxDataType eRetType = SbxVOID;
520 if( xIdlClass.is() )
521 {
522 TypeClass eType = xIdlClass->getTypeClass();
523 eRetType = unoToSbxType( eType );
524 }
525 return eRetType;
526 }
527
528 static void implSequenceToMultiDimArray( SbxDimArray*& pArray, Sequence< sal_Int32 >& indices, Sequence< sal_Int32 >& sizes, const Any& aValue, sal_Int32 dimension, bool bIsZeroIndex, Type const * pType )
529 {
530 const Type& aType = aValue.getValueType();
531 TypeClass eTypeClass = aType.getTypeClass();
532
533 sal_Int32 dimCopy = dimension;
534
535 if ( eTypeClass == TypeClass_SEQUENCE )
536 {
537 Reference< XIdlClass > xIdlTargetClass = TypeToIdlClass( aType );
538 Reference< XIdlArray > xIdlArray = xIdlTargetClass->getArray();
539 typelib_TypeDescription * pTD = nullptr;
540 aType.getDescription( &pTD );
541 Type aElementType( reinterpret_cast<typelib_IndirectTypeDescription *>(pTD)->pType );
542 ::typelib_typedescription_release( pTD );
543
544 sal_Int32 nLen = xIdlArray->getLen( aValue );
545 for ( sal_Int32 index = 0; index < nLen; ++index )
546 {
547 auto pindices = indices.getArray();
548 Any aElementAny = xIdlArray->get( aValue, static_cast<sal_uInt32>(index) );
549 // This detects the dimension were currently processing
550 if ( dimCopy == dimension )
551 {
552 ++dimCopy;
553 if ( sizes.getLength() < dimCopy )
554 {
555 sizes.realloc( sizes.getLength() + 1 );
556 sizes.getArray()[ sizes.getLength() - 1 ] = nLen;
557 indices.realloc( indices.getLength() + 1 );
558 pindices = indices.getArray();
559 }
560 }
561
562 if ( bIsZeroIndex )
563 pindices[ dimCopy - 1 ] = index;
564 else
565 pindices[ dimCopy - 1] = index + 1;
566
567 implSequenceToMultiDimArray( pArray, indices, sizes, aElementAny, dimCopy, bIsZeroIndex, &aElementType );
568 }
569
570 }
571 else
572 {
573 if ( !indices.hasElements() )
574 {
575 // Should never ever get here ( indices.getLength()
576 // should equal number of dimensions in the array )
577 // And that should at least be 1 !
578 // #QUESTION is there a better error?
579 StarBASIC::Error( ERRCODE_BASIC_INVALID_OBJECT );
580 return;
581 }
582
583 SbxDataType eSbxElementType = unoToSbxType( pType ? pType->getTypeClass() : aValue.getValueTypeClass() );
584 if ( !pArray )
585 {
586 pArray = new SbxDimArray( eSbxElementType );
587 sal_Int32 nIndexLen = indices.getLength();
588
589 // Dimension the array
590 for ( sal_Int32 index = 0; index < nIndexLen; ++index )
591 {
592 if ( bIsZeroIndex )
593 pArray->unoAddDim(0, sizes[index] - 1);
594 else
595 pArray->unoAddDim(1, sizes[index]);
596
597 }
598 }
599
600 if ( pArray )
601 {
602 auto xVar = tools::make_ref<SbxVariable>( eSbxElementType );
603 unoToSbxValue( xVar.get(), aValue );
604
605 sal_Int32* pIndices = indices.getArray();
606 pArray->Put(xVar.get(), pIndices);
607
608 }
609 }
610 }
611
612 void unoToSbxValue( SbxVariable* pVar, const Any& aValue )
613 {
614 const Type& aType = aValue.getValueType();
615 TypeClass eTypeClass = aType.getTypeClass();
616 switch( eTypeClass )
617 {
618 case TypeClass_TYPE:
619 {
620 // Map Type to IdlClass
621 Type aType_;
622 aValue >>= aType_;
623 Reference<XIdlClass> xClass = TypeToIdlClass( aType_ );
624 Any aClassAny;
625 aClassAny <<= xClass;
626
627 // instantiate SbUnoObject
628 SbUnoObject* pSbUnoObject = new SbUnoObject( OUString(), aClassAny );
629 SbxObjectRef xWrapper = static_cast<SbxObject*>(pSbUnoObject);
630
631 // If the object is invalid deliver null
632 if( !pSbUnoObject->getUnoAny().hasValue() )
633 {
634 pVar->PutObject( nullptr );
635 }
636 else
637 {
638 pVar->PutObject( xWrapper.get() );
639 }
640 }
641 break;
642 // Interfaces and Structs must be wrapped in a SbUnoObject
643 case TypeClass_INTERFACE:
644 case TypeClass_STRUCT:
645 case TypeClass_EXCEPTION:
646 {
647 if( eTypeClass == TypeClass_STRUCT )
648 {
649 ArrayWrapper aWrap;
650 NativeObjectWrapper aNativeObjectWrapper;
651 if ( aValue >>= aWrap )
652 {
653 SbxDimArray* pArray = nullptr;
654 Sequence< sal_Int32 > indices;
655 Sequence< sal_Int32 > sizes;
656 implSequenceToMultiDimArray( pArray, indices, sizes, aWrap.Array, /*dimension*/0, aWrap.IsZeroIndex, nullptr );
657 if ( pArray )
658 {
659 SbxDimArrayRef xArray = pArray;
660 SbxFlagBits nFlags = pVar->GetFlags();
661 pVar->ResetFlag( SbxFlagBits::Fixed );
662 pVar->PutObject( xArray.get() );
663 pVar->SetFlags( nFlags );
664 }
665 else
666 pVar->PutEmpty();
667 break;
668 }
669 else if ( aValue >>= aNativeObjectWrapper )
670 {
671 sal_uInt32 nIndex = 0;
672 if( aNativeObjectWrapper.ObjectId >>= nIndex )
673 {
674 SbxObject* pObj = lcl_getNativeObject( nIndex );
675 pVar->PutObject( pObj );
676 }
677 else
678 pVar->PutEmpty();
679 break;
680 }
681 else
682 {
683 SbiInstance* pInst = GetSbData()->pInst;
684 if( pInst && pInst->IsCompatibility() )
685 {
686 oleautomation::Date aDate;
687 if( aValue >>= aDate )
688 {
689 pVar->PutDate( aDate.Value );
690 break;
691 }
692 else
693 {
694 oleautomation::Decimal aDecimal;
695 if( aValue >>= aDecimal )
696 {
697 pVar->PutDecimal( aDecimal );
698 break;
699 }
700 else
701 {
702 oleautomation::Currency aCurrency;
703 if( aValue >>= aCurrency )
704 {
705 pVar->PutCurrency( aCurrency.Value );
706 break;
707 }
708 }
709 }
710 }
711 }
712 }
713 // instantiate a SbUnoObject
714 SbUnoObject* pSbUnoObject = new SbUnoObject( OUString(), aValue );
715 //If this is called externally e.g. from the scripting
716 //framework then there is no 'active' runtime the default property will not be set up
717 //only a vba object will have XDefaultProp set anyway so... this
718 //test seems a bit of overkill
719 //if ( SbiRuntime::isVBAEnabled() )
720 {
721 OUString sDfltPropName;
722
723 if ( SbUnoObject::getDefaultPropName( pSbUnoObject, sDfltPropName ) )
724 {
725 pSbUnoObject->SetDfltProperty( sDfltPropName );
726 }
727 }
728 SbxObjectRef xWrapper = static_cast<SbxObject*>(pSbUnoObject);
729
730 // If the object is invalid deliver null
731 if( !pSbUnoObject->getUnoAny().hasValue() )
732 {
733 pVar->PutObject( nullptr );
734 }
735 else
736 {
737 pVar->PutObject( xWrapper.get() );
738 }
739 }
740 break;
741
742
743 case TypeClass_ENUM:
744 {
745 sal_Int32 nEnum = 0;
746 enum2int( nEnum, aValue );
747 pVar->PutLong( nEnum );
748 }
749 break;
750
751 case TypeClass_SEQUENCE:
752 {
753 Reference< XIdlClass > xIdlTargetClass = TypeToIdlClass( aType );
754 Reference< XIdlArray > xIdlArray = xIdlTargetClass->getArray();
755 sal_Int32 i, nLen = xIdlArray->getLen( aValue );
756
757 typelib_TypeDescription * pTD = nullptr;
758 aType.getDescription( &pTD );
759 assert( pTD && pTD->eTypeClass == typelib_TypeClass_SEQUENCE );
760 Type aElementType( reinterpret_cast<typelib_IndirectTypeDescription *>(pTD)->pType );
761 ::typelib_typedescription_release( pTD );
762
763 // build an Array in Basic
764 SbxDimArrayRef xArray;
765 SbxDataType eSbxElementType = unoToSbxType( aElementType.getTypeClass() );
766 xArray = new SbxDimArray( eSbxElementType );
767 if( nLen > 0 )
768 {
769 xArray->unoAddDim(0, nLen - 1);
770
771 // register the elements as variables
772 for( i = 0 ; i < nLen ; i++ )
773 {
774 // convert elements
775 Any aElementAny = xIdlArray->get( aValue, static_cast<sal_uInt32>(i) );
776 auto xVar = tools::make_ref<SbxVariable>( eSbxElementType );
777 unoToSbxValue( xVar.get(), aElementAny );
778
779 // put into the Array
780 xArray->Put(xVar.get(), &i);
781 }
782 }
783 else
784 {
785 xArray->unoAddDim(0, -1);
786 }
787
788 // return the Array
789 SbxFlagBits nFlags = pVar->GetFlags();
790 pVar->ResetFlag( SbxFlagBits::Fixed );
791 pVar->PutObject( xArray.get() );
792 pVar->SetFlags( nFlags );
793
794 }
795 break;
796
797
798 case TypeClass_BOOLEAN: pVar->PutBool( *o3tl::forceAccess<bool>(aValue) ); break;
799 case TypeClass_CHAR:
800 {
801 pVar->PutChar( *o3tl::forceAccess<sal_Unicode>(aValue) );
802 break;
803 }
804 case TypeClass_STRING: { OUString val; aValue >>= val; pVar->PutString( val ); } break;
805 case TypeClass_FLOAT: { float val = 0; aValue >>= val; pVar->PutSingle( val ); } break;
806 case TypeClass_DOUBLE: { double val = 0; aValue >>= val; pVar->PutDouble( val ); } break;
807 case TypeClass_BYTE: { sal_Int8 val = 0; aValue >>= val; pVar->PutInteger( val ); } break;
808 case TypeClass_SHORT: { sal_Int16 val = 0; aValue >>= val; pVar->PutInteger( val ); } break;
809 case TypeClass_LONG: { sal_Int32 val = 0; aValue >>= val; pVar->PutLong( val ); } break;
810 case TypeClass_HYPER: { sal_Int64 val = 0; aValue >>= val; pVar->PutInt64( val ); } break;
811 case TypeClass_UNSIGNED_SHORT: { sal_uInt16 val = 0; aValue >>= val; pVar->PutUShort( val ); } break;
812 case TypeClass_UNSIGNED_LONG: { sal_uInt32 val = 0; aValue >>= val; pVar->PutULong( val ); } break;
813 case TypeClass_UNSIGNED_HYPER: { sal_uInt64 val = 0; aValue >>= val; pVar->PutUInt64( val ); } break;
814 default: pVar->PutEmpty(); break;
815 }
816 }
817
818 // Deliver the reflection for Sbx types
819 static Type getUnoTypeForSbxBaseType( SbxDataType eType )
820 {
821 Type aRetType = cppu::UnoType<void>::get();
822 switch( eType )
823 {
824 case SbxNULL: aRetType = cppu::UnoType<XInterface>::get(); break;
825 case SbxINTEGER: aRetType = cppu::UnoType<sal_Int16>::get(); break;
826 case SbxLONG: aRetType = cppu::UnoType<sal_Int32>::get(); break;
827 case SbxSINGLE: aRetType = cppu::UnoType<float>::get(); break;
828 case SbxDOUBLE: aRetType = cppu::UnoType<double>::get(); break;
829 case SbxCURRENCY: aRetType = cppu::UnoType<oleautomation::Currency>::get(); break;
830 case SbxDECIMAL: aRetType = cppu::UnoType<oleautomation::Decimal>::get(); break;
831 case SbxDATE: {
832 SbiInstance* pInst = GetSbData()->pInst;
833 if( pInst && pInst->IsCompatibility() )
834 aRetType = cppu::UnoType<oleautomation::Date>::get();
835 else
836 aRetType = cppu::UnoType<double>::get();
837 }
838 break;
839 case SbxSTRING: aRetType = cppu::UnoType<OUString>::get(); break;
840 case SbxBOOL: aRetType = cppu::UnoType<sal_Bool>::get(); break;
841 case SbxVARIANT: aRetType = cppu::UnoType<Any>::get(); break;
842 case SbxCHAR: aRetType = cppu::UnoType<cppu::UnoCharType>::get(); break;
843 case SbxBYTE: aRetType = cppu::UnoType<sal_Int8>::get(); break;
844 case SbxUSHORT: aRetType = cppu::UnoType<cppu::UnoUnsignedShortType>::get(); break;
845 case SbxULONG: aRetType = ::cppu::UnoType<sal_uInt32>::get(); break;
846 // map machine-dependent ones to long for consistency
847 case SbxINT: aRetType = ::cppu::UnoType<sal_Int32>::get(); break;
848 case SbxUINT: aRetType = ::cppu::UnoType<sal_uInt32>::get(); break;
849 default: break;
850 }
851 return aRetType;
852 }
853
854 // Converting of Sbx to Uno without a know target class for TypeClass_ANY
855 static Type getUnoTypeForSbxValue( const SbxValue* pVal )
856 {
857 Type aRetType = cppu::UnoType<void>::get();
858 if( !pVal )
859 return aRetType;
860
861 // convert SbxType to Uno
862 SbxDataType eBaseType = pVal->SbxValue::GetType();
863 if( eBaseType == SbxOBJECT )
864 {
865 SbxBaseRef xObj = pVal->GetObject();
866 if( !xObj.is() )
867 {
868 aRetType = cppu::UnoType<XInterface>::get();
869 return aRetType;
870 }
871
872 if( auto pArray = dynamic_cast<SbxDimArray*>( xObj.get() ) )
873 {
874 sal_Int32 nDims = pArray->GetDims();
875 Type aElementType = getUnoTypeForSbxBaseType( static_cast<SbxDataType>(pArray->GetType() & 0xfff) );
876 TypeClass eElementTypeClass = aElementType.getTypeClass();
877
878 // Normal case: One dimensional array
879 sal_Int32 nLower, nUpper;
880 if (nDims == 1 && pArray->GetDim(1, nLower, nUpper))
881 {
882 if( eElementTypeClass == TypeClass_VOID || eElementTypeClass == TypeClass_ANY )
883 {
884 // If all elements of the arrays are from the same type, take
885 // this one - otherwise the whole will be considered as Any-Sequence
886 bool bNeedsInit = true;
887
888 for (sal_Int32 aIdx[1] = { nLower }; aIdx[0] <= nUpper; ++aIdx[0])
889 {
890 SbxVariableRef xVar = pArray->Get(aIdx);
891 Type aType = getUnoTypeForSbxValue( xVar.get() );
892 if( bNeedsInit )
893 {
894 if( aType.getTypeClass() == TypeClass_VOID )
895 {
896 // if only first element is void: different types -> []any
897 // if all elements are void: []void is not allowed -> []any
898 aElementType = cppu::UnoType<Any>::get();
899 break;
900 }
901 aElementType = std::move(aType);
902 bNeedsInit = false;
903 }
904 else if( aElementType != aType )
905 {
906 // different types -> AnySequence
907 aElementType = cppu::UnoType<Any>::get();
908 break;
909 }
910 }
911 }
912
913 OUString aSeqTypeName = aSeqLevelStr + aElementType.getTypeName();
914 aRetType = Type( TypeClass_SEQUENCE, aSeqTypeName );
915 }
916 // #i33795 Map also multi dimensional arrays to corresponding sequences
917 else if( nDims > 1 )
918 {
919 if( eElementTypeClass == TypeClass_VOID || eElementTypeClass == TypeClass_ANY )
920 {
921 // For this check the array's dim structure does not matter
922 sal_uInt32 nFlatArraySize = pArray->Count();
923
924 bool bNeedsInit = true;
925 for( sal_uInt32 i = 0 ; i < nFlatArraySize ; i++ )
926 {
927 SbxVariableRef xVar = pArray->SbxArray::Get(i);
928 Type aType = getUnoTypeForSbxValue( xVar.get() );
929 if( bNeedsInit )
930 {
931 if( aType.getTypeClass() == TypeClass_VOID )
932 {
933 // if only first element is void: different types -> []any
934 // if all elements are void: []void is not allowed -> []any
935 aElementType = cppu::UnoType<Any>::get();
936 break;
937 }
938 aElementType = std::move(aType);
939 bNeedsInit = false;
940 }
941 else if( aElementType != aType )
942 {
943 // different types -> AnySequence
944 aElementType = cppu::UnoType<Any>::get();
945 break;
946 }
947 }
948 }
949
950 OUStringBuffer aSeqTypeName;
951 for(sal_Int32 iDim = 0 ; iDim < nDims ; iDim++ )
952 {
953 aSeqTypeName.append(aSeqLevelStr);
954 }
955 aSeqTypeName.append(aElementType.getTypeName());
956 aRetType = Type( TypeClass_SEQUENCE, aSeqTypeName.makeStringAndClear() );
957 }
958 }
959 // No array, but ...
960 else if( auto obj = dynamic_cast<SbUnoObject*>( xObj.get() ) )
961 {
962 aRetType = obj->getUnoAny().getValueType();
963 }
964 // SbUnoAnyObject?
965 else if( auto any = dynamic_cast<SbUnoAnyObject*>( xObj.get() ) )
966 {
967 aRetType = any->getValue().getValueType();
968 }
969 // Otherwise it is a No-Uno-Basic-Object -> default==deliver void
970 }
971 // No object, convert basic type
972 else
973 {
974 if (eBaseType == SbxBYTE && pVal->GetByte() > 127)
975 {
976 // Basic Byte type is unsigned; cppu::UnoType<sal_uInt8> corresponds to UNO boolean,
977 // so values 128-255 are only representable starting with UNO short types
978 eBaseType = SbxUSHORT;
979 }
980 aRetType = getUnoTypeForSbxBaseType( eBaseType );
981 }
982 return aRetType;
983 }
984
985 // converting of Sbx to Uno without known target class for TypeClass_ANY
986 static Any sbxToUnoValueImpl( const SbxValue* pVar, bool bBlockConversionToSmallestType = false )
987 {
988 SbxDataType eBaseType = pVar->SbxValue::GetType();
989 if( eBaseType == SbxOBJECT )
990 {
991 SbxBaseRef xObj = pVar->GetObject();
992 if( xObj.is() )
993 {
994 if( auto obj = dynamic_cast<SbUnoAnyObject*>( xObj.get() ) )
995 return obj->getValue();
996 if( auto pClassModuleObj = dynamic_cast<SbClassModuleObject*>( xObj.get() ) )
997 {
998 Any aRetAny;
999 SbModule& rClassModule = pClassModuleObj->getClassModule();
1000 if (rClassModule.createCOMWrapperForIface(aRetAny, pClassModuleObj))
1001 return aRetAny;
1002 }
1003 if( dynamic_cast<const SbUnoObject*>( xObj.get() ) == nullptr )
1004 {
1005 // Create NativeObjectWrapper to identify object in case of callbacks
1006 SbxObject* pObj = dynamic_cast<SbxObject*>( pVar->GetObject() );
1007 if( pObj != nullptr )
1008 {
1009 NativeObjectWrapper aNativeObjectWrapper;
1010 sal_uInt32 nIndex = lcl_registerNativeObjectWrapper( pObj );
1011 aNativeObjectWrapper.ObjectId <<= nIndex;
1012 Any aRetAny;
1013 aRetAny <<= aNativeObjectWrapper;
1014 return aRetAny;
1015 }
1016 }
1017 }
1018 }
1019
1020 Type aType = getUnoTypeForSbxValue( pVar );
1021 TypeClass eType = aType.getTypeClass();
1022
1023 if( !bBlockConversionToSmallestType )
1024 {
1025 // #79615 Choose "smallest" representation for int values
1026 // because up cast is allowed, downcast not
1027 switch( eType )
1028 {
1029 case TypeClass_FLOAT:
1030 case TypeClass_DOUBLE:
1031 {
1032 double d = pVar->GetDouble();
1033 if( rtl::math::approxEqual(d, floor( d )) )
1034 {
1035 if( d >= -128 && d <= 127 )
1036 aType = ::cppu::UnoType<sal_Int8>::get();
1037 else if( d >= SbxMININT && d <= SbxMAXINT )
1038 aType = ::cppu::UnoType<sal_Int16>::get();
1039 else if( d >= -SbxMAXLNG && d <= SbxMAXLNG )
1040 aType = ::cppu::UnoType<sal_Int32>::get();
1041 }
1042 break;
1043 }
1044 case TypeClass_SHORT:
1045 {
1046 sal_Int16 n = pVar->GetInteger();
1047 if( n >= -128 && n <= 127 )
1048 aType = ::cppu::UnoType<sal_Int8>::get();
1049 break;
1050 }
1051 case TypeClass_LONG:
1052 {
1053 sal_Int32 n = pVar->GetLong();
1054 if( n >= -128 && n <= 127 )
1055 aType = ::cppu::UnoType<sal_Int8>::get();
1056 else if( n >= SbxMININT && n <= SbxMAXINT )
1057 aType = ::cppu::UnoType<sal_Int16>::get();
1058 break;
1059 }
1060 case TypeClass_UNSIGNED_LONG:
1061 {
1062 sal_uInt32 n = pVar->GetLong();
1063 if( n <= SbxMAXUINT )
1064 aType = cppu::UnoType<cppu::UnoUnsignedShortType>::get();
1065 break;
1066 }
1067 // TODO: need to add hyper types ?
1068 default: break;
1069 }
1070 }
1071
1072 return sbxToUnoValue( pVar, aType );
1073 }
1074
1075
1076 // Helper function for StepREDIMP
1077 static Any implRekMultiDimArrayToSequence( SbxDimArray* pArray,
1078 const Type& aElemType, sal_Int32 nMaxDimIndex, sal_Int32 nActualDim,
1079 sal_Int32* pActualIndices, sal_Int32* pLowerBounds, sal_Int32* pUpperBounds )
1080 {
1081 sal_Int32 nSeqLevel = nMaxDimIndex - nActualDim + 1;
1082 OUStringBuffer aSeqTypeName;
1083 sal_Int32 i;
1084 for( i = 0 ; i < nSeqLevel ; i++ )
1085 {
1086 aSeqTypeName.append(aSeqLevelStr);
1087 }
1088 aSeqTypeName.append(aElemType.getTypeName());
1089 Type aSeqType( TypeClass_SEQUENCE, aSeqTypeName.makeStringAndClear() );
1090
1091 // Create Sequence instance
1092 Any aRetVal;
1093 Reference< XIdlClass > xIdlTargetClass = TypeToIdlClass( aSeqType );
1094 xIdlTargetClass->createObject( aRetVal );
1095
1096 // Alloc sequence according to array bounds
1097 sal_Int32 nUpper = pUpperBounds[nActualDim];
1098 sal_Int32 nLower = pLowerBounds[nActualDim];
1099 sal_Int32 nSeqSize = nUpper - nLower + 1;
1100 Reference< XIdlArray > xArray = xIdlTargetClass->getArray();
1101 xArray->realloc( aRetVal, nSeqSize );
1102
1103 sal_Int32& ri = pActualIndices[nActualDim];
1104
1105 for( ri = nLower,i = 0 ; ri <= nUpper ; ri++,i++ )
1106 {
1107 Any aElementVal;
1108
1109 if( nActualDim < nMaxDimIndex )
1110 {
1111 aElementVal = implRekMultiDimArrayToSequence( pArray, aElemType,
1112 nMaxDimIndex, nActualDim + 1, pActualIndices, pLowerBounds, pUpperBounds );
1113 }
1114 else
1115 {
1116 SbxVariable* pSource = pArray->Get(pActualIndices);
1117 aElementVal = sbxToUnoValue( pSource, aElemType );
1118 }
1119
1120 try
1121 {
1122 // transfer to the sequence
1123 xArray->set( aRetVal, i, aElementVal );
1124 }
1125 catch( const IllegalArgumentException& )
1126 {
1127 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION,
1128 implGetExceptionMsg( ::cppu::getCaughtException() ) );
1129 }
1130 catch (const IndexOutOfBoundsException&)
1131 {
1132 StarBASIC::Error( ERRCODE_BASIC_OUT_OF_RANGE );
1133 }
1134 }
1135 return aRetVal;
1136 }
1137
1138 // Map old interface
1139 Any sbxToUnoValue( const SbxValue* pVar )
1140 {
1141 return sbxToUnoValueImpl( pVar );
1142 }
1143
1144 // function to find a global identifier in
1145 // the UnoScope and to wrap it for Sbx
1146 static bool implGetTypeByName( const OUString& rName, Type& rRetType )
1147 {
1148 bool bSuccess = false;
1149
1150 const Reference< XHierarchicalNameAccess >& xTypeAccess = getTypeProvider_Impl();
1151 if( xTypeAccess->hasByHierarchicalName( rName ) )
1152 {
1153 Any aRet = xTypeAccess->getByHierarchicalName( rName );
1154 Reference< XTypeDescription > xTypeDesc;
1155 aRet >>= xTypeDesc;
1156
1157 if( xTypeDesc.is() )
1158 {
1159 rRetType = Type( xTypeDesc->getTypeClass(), xTypeDesc->getName() );
1160 bSuccess = true;
1161 }
1162 }
1163 return bSuccess;
1164 }
1165
1166
1167 // converting of Sbx to Uno with known target class
1168 Any sbxToUnoValue( const SbxValue* pVar, const Type& rType, Property const * pUnoProperty )
1169 {
1170 Any aRetVal;
1171
1172 // #94560 No conversion of empty/void for MAYBE_VOID properties
1173 if( pUnoProperty && pUnoProperty->Attributes & PropertyAttribute::MAYBEVOID )
1174 {
1175 if( pVar->IsEmpty() )
1176 return aRetVal;
1177 }
1178
1179 SbxDataType eBaseType = pVar->SbxValue::GetType();
1180 if( eBaseType == SbxOBJECT )
1181 {
1182 SbxBaseRef xObj = pVar->GetObject();
1183 if ( auto obj = dynamic_cast<SbUnoAnyObject*>( xObj.get() ) )
1184 {
1185 return obj->getValue();
1186 }
1187 }
1188
1189 TypeClass eType = rType.getTypeClass();
1190
1191 // tdf#162431 - check for missing parameters
1192 if (eType != TypeClass_ANY && eType != TypeClass_VOID && pVar->GetType() == SbxERROR)
1193 {
1194 SbxVariable* paSbxVariable = dynamic_cast<SbxVariable*>(const_cast<SbxValue*>(pVar));
1195 if (paSbxVariable && SbiRuntime::IsMissing(paSbxVariable, 1))
1196 StarBASIC::Error(ERRCODE_BASIC_NOT_OPTIONAL);
1197 }
1198
1199 switch( eType )
1200 {
1201 case TypeClass_INTERFACE:
1202 case TypeClass_STRUCT:
1203 case TypeClass_EXCEPTION:
1204 {
1205 Reference< XIdlClass > xIdlTargetClass = TypeToIdlClass( rType );
1206
1207 // null reference?
1208 if( pVar->IsNull() && eType == TypeClass_INTERFACE )
1209 {
1210 Reference< XInterface > xRef;
1211 OUString aClassName = xIdlTargetClass->getName();
1212 Type aClassType( xIdlTargetClass->getTypeClass(), aClassName );
1213 aRetVal.setValue( &xRef, aClassType );
1214 }
1215 else
1216 {
1217 // #112368 Special conversion for Decimal, Currency and Date
1218 if( eType == TypeClass_STRUCT )
1219 {
1220 SbiInstance* pInst = GetSbData()->pInst;
1221 if( pInst && pInst->IsCompatibility() )
1222 {
1223 if( rType == cppu::UnoType<oleautomation::Decimal>::get())
1224 {
1225 oleautomation::Decimal aDecimal;
1226 pVar->fillAutomationDecimal( aDecimal );
1227 aRetVal <<= aDecimal;
1228 break;
1229 }
1230 else if( rType == cppu::UnoType<oleautomation::Currency>::get())
1231 {
1232 // assumes per previous code that ole Currency is Int64
1233 aRetVal <<= pVar->GetInt64();
1234 break;
1235 }
1236 else if( rType == cppu::UnoType<oleautomation::Date>::get())
1237 {
1238 oleautomation::Date aDate;
1239 aDate.Value = pVar->GetDate();
1240 aRetVal <<= aDate;
1241 break;
1242 }
1243 }
1244 }
1245
1246 SbxBaseRef pObj = pVar->GetObject();
1247 if( auto obj = dynamic_cast<SbUnoObject*>( pObj.get() ) )
1248 {
1249 aRetVal = obj->getUnoAny();
1250 }
1251 else if( auto structRef = dynamic_cast<SbUnoStructRefObject*>( pObj.get() ) )
1252 {
1253 aRetVal = structRef->getUnoAny();
1254 }
1255 else
1256 {
1257 // null object -> null XInterface
1258 Reference<XInterface> xInt;
1259 aRetVal <<= xInt;
1260 }
1261 }
1262 }
1263 break;
1264
1265 case TypeClass_TYPE:
1266 {
1267 if( eBaseType == SbxOBJECT )
1268 {
1269 // XIdlClass?
1270 Reference< XIdlClass > xIdlClass;
1271
1272 SbxBaseRef pObj = pVar->GetObject();
1273 if( auto obj = dynamic_cast<SbUnoObject*>( pObj.get() ) )
1274 {
1275 Any aUnoAny = obj->getUnoAny();
1276 aUnoAny >>= xIdlClass;
1277 }
1278
1279 if( xIdlClass.is() )
1280 {
1281 OUString aClassName = xIdlClass->getName();
1282 Type aType( xIdlClass->getTypeClass(), aClassName );
1283 aRetVal <<= aType;
1284 }
1285 }
1286 else if( eBaseType == SbxSTRING )
1287 {
1288 OUString aTypeName = pVar->GetOUString();
1289 Type aType;
1290 bool bSuccess = implGetTypeByName( aTypeName, aType );
1291 if( bSuccess )
1292 {
1293 aRetVal <<= aType;
1294 }
1295 }
1296 }
1297 break;
1298
1299
1300 case TypeClass_ENUM:
1301 {
1302 aRetVal = int2enum( pVar->GetLong(), rType );
1303 }
1304 break;
1305
1306 case TypeClass_SEQUENCE:
1307 {
1308 SbxBaseRef xObj = pVar->GetObject();
1309 if( auto pArray = dynamic_cast<SbxDimArray*>( xObj.get() ) )
1310 {
1311 sal_Int32 nDims = pArray->GetDims();
1312
1313 // Normal case: One dimensional array
1314 sal_Int32 nLower, nUpper;
1315 if (nDims == 1 && pArray->GetDim(1, nLower, nUpper))
1316 {
1317 sal_Int32 nSeqSize = nUpper - nLower + 1;
1318
1319 // create the instance of the required sequence
1320 Reference< XIdlClass > xIdlTargetClass = TypeToIdlClass( rType );
1321 xIdlTargetClass->createObject( aRetVal );
1322 Reference< XIdlArray > xArray = xIdlTargetClass->getArray();
1323 xArray->realloc( aRetVal, nSeqSize );
1324
1325 // Element-Type
1326 OUString aClassName = xIdlTargetClass->getName();
1327 typelib_TypeDescription * pSeqTD = nullptr;
1328 typelib_typedescription_getByName( &pSeqTD, aClassName.pData );
1329 assert( pSeqTD );
1330 Type aElemType( reinterpret_cast<typelib_IndirectTypeDescription *>(pSeqTD)->pType );
1331
1332 // convert all array member and register them
1333 sal_Int32 aIdx[1];
1334 aIdx[0] = nLower;
1335 for (sal_Int32 i = 0 ; i < nSeqSize; ++i, ++aIdx[0])
1336 {
1337 SbxVariableRef xVar = pArray->Get(aIdx);
1338
1339 // Convert the value of Sbx to Uno
1340 Any aAnyValue = sbxToUnoValue( xVar.get(), aElemType );
1341
1342 try
1343 {
1344 // insert in the sequence
1345 xArray->set( aRetVal, i, aAnyValue );
1346 }
1347 catch( const IllegalArgumentException& )
1348 {
1349 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION,
1350 implGetExceptionMsg( ::cppu::getCaughtException() ) );
1351 }
1352 catch (const IndexOutOfBoundsException&)
1353 {
1354 StarBASIC::Error( ERRCODE_BASIC_OUT_OF_RANGE );
1355 }
1356 }
1357 }
1358 // #i33795 Map also multi dimensional arrays to corresponding sequences
1359 else if( nDims > 1 )
1360 {
1361 // Element-Type
1362 typelib_TypeDescription * pSeqTD = nullptr;
1363 Type aCurType( rType );
1364 sal_Int32 nSeqLevel = 0;
1365 Type aElemType;
1366 do
1367 {
1368 OUString aTypeName = aCurType.getTypeName();
1369 typelib_typedescription_getByName( &pSeqTD, aTypeName.pData );
1370 assert( pSeqTD );
1371 if( pSeqTD->eTypeClass == typelib_TypeClass_SEQUENCE )
1372 {
1373 aCurType = Type( reinterpret_cast<typelib_IndirectTypeDescription *>(pSeqTD)->pType );
1374 nSeqLevel++;
1375 }
1376 else
1377 {
1378 aElemType = aCurType;
1379 break;
1380 }
1381 }
1382 while( true );
1383
1384 if( nSeqLevel == nDims )
1385 {
1386 std::unique_ptr<sal_Int32[]> pLowerBounds(new sal_Int32[nDims]);
1387 std::unique_ptr<sal_Int32[]> pUpperBounds(new sal_Int32[nDims]);
1388 std::unique_ptr<sal_Int32[]> pActualIndices(new sal_Int32[nDims]);
1389 for(sal_Int32 i = 1 ; i <= nDims ; i++ )
1390 {
1391 sal_Int32 lBound, uBound;
1392 pArray->GetDim(i, lBound, uBound);
1393
1394 sal_Int32 j = i - 1;
1395 pActualIndices[j] = pLowerBounds[j] = lBound;
1396 pUpperBounds[j] = uBound;
1397 }
1398
1399 aRetVal = implRekMultiDimArrayToSequence( pArray, aElemType,
1400 nDims - 1, 0, pActualIndices.get(), pLowerBounds.get(), pUpperBounds.get() );
1401 }
1402 }
1403 }
1404 }
1405 break;
1406
1407
1408 // for Any use the class independent converting routine
1409 case TypeClass_ANY:
1410 {
1411 aRetVal = sbxToUnoValueImpl( pVar );
1412 }
1413 break;
1414
1415 case TypeClass_BOOLEAN:
1416 {
1417 aRetVal <<= pVar->GetBool();
1418 break;
1419 }
1420 case TypeClass_CHAR:
1421 {
1422 aRetVal <<= pVar->GetChar();
1423 break;
1424 }
1425 case TypeClass_STRING: aRetVal <<= pVar->GetOUString(); break;
1426 case TypeClass_FLOAT: aRetVal <<= pVar->GetSingle(); break;
1427 case TypeClass_DOUBLE: aRetVal <<= pVar->GetDouble(); break;
1428
1429 case TypeClass_BYTE:
1430 {
1431 sal_Int16 nVal = pVar->GetInteger();
1432 bool bOverflow = false;
1433 if( nVal < -128 )
1434 {
1435 bOverflow = true;
1436 nVal = -128;
1437 }
1438 else if( nVal > 255 ) // 128..255 map to -128..-1
1439 {
1440 bOverflow = true;
1441 nVal = 127;
1442 }
1443 if( bOverflow )
1444 StarBASIC::Error( ERRCODE_BASIC_MATH_OVERFLOW );
1445
1446 sal_Int8 nByteVal = static_cast<sal_Int8>(nVal);
1447 aRetVal <<= nByteVal;
1448 break;
1449 }
1450 case TypeClass_SHORT: aRetVal <<= pVar->GetInteger(); break;
1451 case TypeClass_LONG: aRetVal <<= pVar->GetLong(); break;
1452 case TypeClass_HYPER: aRetVal <<= pVar->GetInt64(); break;
1453 case TypeClass_UNSIGNED_SHORT: aRetVal <<= pVar->GetUShort(); break;
1454 case TypeClass_UNSIGNED_LONG: aRetVal <<= pVar->GetULong(); break;
1455 case TypeClass_UNSIGNED_HYPER: aRetVal <<= pVar->GetUInt64(); break;
1456 default: break;
1457 }
1458
1459 return aRetVal;
1460 }
1461
1462 static void processAutomationParams( SbxArray* pParams, Sequence< Any >& args, sal_uInt32 nParamCount )
1463 {
1464 AutomationNamedArgsSbxArray* pArgNamesArray = dynamic_cast<AutomationNamedArgsSbxArray*>( pParams );
1465
1466 args.realloc( nParamCount );
1467 Any* pAnyArgs = args.getArray();
1468 bool bBlockConversionToSmallestType = GetSbData()->pInst->IsCompatibility();
1469 sal_uInt32 i = 0;
1470 if( pArgNamesArray )
1471 {
1472 Sequence< OUString >& rNameSeq = pArgNamesArray->getNames();
1473 OUString* pNames = rNameSeq.getArray();
1474 Any aValAny;
1475 for( i = 0 ; i < nParamCount ; i++ )
1476 {
1477 sal_uInt32 iSbx = i + 1;
1478
1479 aValAny = sbxToUnoValueImpl(pParams->Get(iSbx),
1480 bBlockConversionToSmallestType );
1481
1482 OUString aParamName = pNames[iSbx];
1483 if( !aParamName.isEmpty() )
1484 {
1485 oleautomation::NamedArgument aNamedArgument;
1486 aNamedArgument.Name = aParamName;
1487 aNamedArgument.Value = aValAny;
1488 pAnyArgs[i] <<= aNamedArgument;
1489 }
1490 else
1491 {
1492 pAnyArgs[i] = aValAny;
1493 }
1494 }
1495 }
1496 else
1497 {
1498 for( i = 0 ; i < nParamCount ; i++ )
1499 {
1500 pAnyArgs[i] = sbxToUnoValueImpl(pParams->Get(i + 1),
1501 bBlockConversionToSmallestType );
1502 }
1503 }
1504
1505 }
1506
1507 namespace {
1508
1509 enum class INVOKETYPE
1510 {
1511 GetProp = 0,
1512 Func
1513 };
1514
1515 }
1516
1517 static Any invokeAutomationMethod( const OUString& Name, Sequence< Any > const & args, SbxArray* pParams, sal_uInt32 nParamCount, Reference< XInvocation > const & rxInvocation, INVOKETYPE invokeType )
1518 {
1519 Sequence< sal_Int16 > OutParamIndex;
1520 Sequence< Any > OutParam;
1521
1522 Any aRetAny;
1523 switch( invokeType )
1524 {
1525 case INVOKETYPE::Func:
1526 aRetAny = rxInvocation->invoke( Name, args, OutParamIndex, OutParam );
1527 break;
1528 case INVOKETYPE::GetProp:
1529 {
1530 Reference< XAutomationInvocation > xAutoInv( rxInvocation, UNO_QUERY );
1531 aRetAny = xAutoInv->invokeGetProperty( Name, args, OutParamIndex, OutParam );
1532 break;
1533 }
1534 default:
1535 assert(false); break;
1536
1537 }
1538 const sal_Int16* pIndices = OutParamIndex.getConstArray();
1539 sal_uInt32 nLen = OutParamIndex.getLength();
1540 if( nLen )
1541 {
1542 const Any* pNewValues = OutParam.getConstArray();
1543 for( sal_uInt32 j = 0 ; j < nLen ; j++ )
1544 {
1545 sal_Int16 iTarget = pIndices[ j ];
1546 if( o3tl::make_unsigned(iTarget) >= nParamCount )
1547 break;
1548 unoToSbxValue(pParams->Get(j + 1), pNewValues[j]);
1549 }
1550 }
1551 return aRetAny;
1552 }
1553
1554 // Debugging help method to readout the implemented interfaces of an object
1555 static OUString Impl_GetInterfaceInfo( const Reference< XInterface >& x, const Reference< XIdlClass >& xClass, sal_uInt16 nRekLevel )
1556 {
1557 Type aIfaceType = cppu::UnoType<XInterface>::get();
1558 static Reference< XIdlClass > xIfaceClass = TypeToIdlClass( aIfaceType );
1559
1560 OUStringBuffer aRetStr;
1561 for( sal_uInt16 i = 0 ; i < nRekLevel ; i++ )
1562 aRetStr.append( " " );
1563 aRetStr.append( xClass->getName() );
1564 OUString aClassName = xClass->getName();
1565 Type aClassType( xClass->getTypeClass(), aClassName );
1566
1567 // checking if the interface is really supported
1568 if( !x->queryInterface( aClassType ).hasValue() )
1569 {
1570 aRetStr.append( " (ERROR: Not really supported!)\n" );
1571 }
1572 // Are there super interfaces?
1573 else
1574 {
1575 aRetStr.append( "\n" );
1576
1577 // get the super interfaces
1578 Sequence< Reference< XIdlClass > > aSuperClassSeq = xClass->getSuperclasses();
1579 const Reference< XIdlClass >* pClasses = aSuperClassSeq.getConstArray();
1580 sal_uInt32 nSuperIfaceCount = aSuperClassSeq.getLength();
1581 for( sal_uInt32 j = 0 ; j < nSuperIfaceCount ; j++ )
1582 {
1583 const Reference< XIdlClass >& rxIfaceClass = pClasses[j];
1584 if( !rxIfaceClass->equals( xIfaceClass ) )
1585 aRetStr.append( Impl_GetInterfaceInfo( x, rxIfaceClass, nRekLevel + 1 ) );
1586 }
1587 }
1588 return aRetStr.makeStringAndClear();
1589 }
1590
1591 static OUString getDbgObjectNameImpl(SbUnoObject& rUnoObj)
1592 {
1593 OUString aName = rUnoObj.GetClassName();
1594 if( aName.isEmpty() )
1595 {
1596 Any aToInspectObj = rUnoObj.getUnoAny();
1597 Reference< XInterface > xObj(aToInspectObj, css::uno::UNO_QUERY);
1598 if( xObj.is() )
1599 {
1600 Reference< XServiceInfo > xServiceInfo( xObj, UNO_QUERY );
1601 if( xServiceInfo.is() )
1602 aName = xServiceInfo->getImplementationName();
1603 }
1604 }
1605 return aName;
1606 }
1607
1608 static OUString getDbgObjectName(SbUnoObject& rUnoObj)
1609 {
1610 OUString aName = getDbgObjectNameImpl(rUnoObj);
1611 if( aName.isEmpty() )
1612 aName += "Unknown";
1613
1614 OUStringBuffer aRet;
1615 if( aName.getLength() > 20 )
1616 {
1617 aRet.append( "\n" );
1618 }
1619 aRet.append( "\"" + aName + "\":" );
1620 return aRet.makeStringAndClear();
1621 }
1622
1623 OUString getBasicObjectTypeName( SbxObject* pObj )
1624 {
1625 if (pObj)
1626 {
1627 if (SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>(pObj))
1628 {
1629 return getDbgObjectNameImpl(*pUnoObj);
1630 }
1631 else if (SbUnoStructRefObject* pUnoStructObj = dynamic_cast<SbUnoStructRefObject*>(pObj))
1632 {
1633 return pUnoStructObj->GetClassName();
1634 }
1635 }
1636 return OUString();
1637 }
1638
1639 namespace {
1640
1641 bool matchesBasicTypeName(
1642 css::uno::Reference<css::reflection::XIdlClass> const & unoType, OUString const & basicTypeName)
1643 {
1644 if (unoType->getName().endsWithIgnoreAsciiCase(basicTypeName)) {
1645 return true;
1646 }
1647 auto const sups = unoType->getSuperclasses();
1648 return std::any_of(
1649 sups.begin(), sups.end(),
1650 [&basicTypeName](auto const & t) { return matchesBasicTypeName(t, basicTypeName); });
1651 }
1652
1653 }
1654
1655 bool checkUnoObjectType(SbUnoObject& rUnoObj, const OUString& rClass)
1656 {
1657 Any aToInspectObj = rUnoObj.getUnoAny();
1658
1659 // Return true for XInvocation based objects as interface type names don't count then
1660 Reference< XInvocation > xInvocation( aToInspectObj, UNO_QUERY );
1661 if( xInvocation.is() )
1662 {
1663 return true;
1664 }
1665 bool bResult = false;
1666 Reference< XTypeProvider > xTypeProvider( aToInspectObj, UNO_QUERY );
1667 if( xTypeProvider.is() )
1668 {
1669 /* Although interfaces in the ooo.vba namespace obey the IDL rules and
1670 have a leading 'X', in Basic we want to be able to do something
1671 like 'Dim wb As Workbooks' or 'Dim lb As MSForms.Label'. Here we
1672 add a leading 'X' to the class name and a leading dot to the entire
1673 type name. This results e.g. in '.XWorkbooks' or '.MSForms.XLabel'
1674 which matches the interface names 'ooo.vba.excel.XWorkbooks' or
1675 'ooo.vba.msforms.XLabel'.
1676 */
1677 OUString aClassName;
1678 if ( SbiRuntime::isVBAEnabled() )
1679 {
1680 aClassName = ".";
1681 sal_Int32 nClassNameDot = rClass.lastIndexOf( '.' );
1682 if( nClassNameDot >= 0 )
1683 {
1684 aClassName += OUString::Concat(rClass.subView( 0, nClassNameDot + 1 )) + "X" + rClass.subView( nClassNameDot + 1 );
1685 }
1686 else
1687 {
1688 aClassName += "X" + rClass;
1689 }
1690 }
1691 else // assume extended type declaration support for basic ( can't get here
1692 // otherwise.
1693 aClassName = rClass;
1694
1695 Sequence< Type > aTypeSeq = xTypeProvider->getTypes();
1696 const Type* pTypeArray = aTypeSeq.getConstArray();
1697 sal_uInt32 nIfaceCount = aTypeSeq.getLength();
1698 for( sal_uInt32 j = 0 ; j < nIfaceCount ; j++ )
1699 {
1700 const Type& rType = pTypeArray[j];
1701
1702 Reference<XIdlClass> xClass = TypeToIdlClass( rType );
1703 if( !xClass.is() )
1704 {
1705 OSL_FAIL("failed to get XIdlClass for type");
1706 break;
1707 }
1708 OUString aInterfaceName = xClass->getName();
1709 if ( aInterfaceName == "com.sun.star.bridge.oleautomation.XAutomationObject" )
1710 {
1711 // there is a hack in the extensions/source/ole/oleobj.cxx
1712 // to return the typename of the automation object, let's
1713 // check if it matches
1714 Reference< XInvocation > xInv( aToInspectObj, UNO_QUERY );
1715 if ( xInv.is() )
1716 {
1717 OUString sTypeName;
1718 xInv->getValue( u"$GetTypeName"_ustr ) >>= sTypeName;
1719 if ( sTypeName.isEmpty() || sTypeName == "IDispatch" )
1720 {
1721 // can't check type, leave it pass
1722 bResult = true;
1723 }
1724 else
1725 {
1726 bResult = sTypeName == rClass;
1727 }
1728 }
1729 break; // finished checking automation object
1730 }
1731
1732 if ( matchesBasicTypeName(xClass, aClassName) )
1733 {
1734 bResult = true;
1735 break;
1736 }
1737 }
1738 }
1739 return bResult;
1740 }
1741
1742 // Debugging help method to readout the implemented interfaces of an object
1743 static OUString Impl_GetSupportedInterfaces(SbUnoObject& rUnoObj)
1744 {
1745 Any aToInspectObj = rUnoObj.getUnoAny();
1746
1747 // allow only TypeClass interface
1748 OUStringBuffer aRet;
1749 auto x = o3tl::tryAccess<Reference<XInterface>>(aToInspectObj);
1750 if( !x )
1751 {
1752 aRet.append( ID_DBG_SUPPORTEDINTERFACES
1753 + " not available.\n(TypeClass is not TypeClass_INTERFACE)\n" );
1754 }
1755 else
1756 {
1757 Reference< XTypeProvider > xTypeProvider( *x, UNO_QUERY );
1758
1759 aRet.append( "Supported interfaces by object "
1760 + getDbgObjectName(rUnoObj)
1761 + "\n" );
1762 if( xTypeProvider.is() )
1763 {
1764 // get the interfaces of the implementation
1765 Sequence< Type > aTypeSeq = xTypeProvider->getTypes();
1766 const Type* pTypeArray = aTypeSeq.getConstArray();
1767 sal_uInt32 nIfaceCount = aTypeSeq.getLength();
1768 for( sal_uInt32 j = 0 ; j < nIfaceCount ; j++ )
1769 {
1770 const Type& rType = pTypeArray[j];
1771
1772 Reference<XIdlClass> xClass = TypeToIdlClass( rType );
1773 if( xClass.is() )
1774 {
1775 aRet.append( Impl_GetInterfaceInfo( *x, xClass, 1 ) );
1776 }
1777 else
1778 {
1779 typelib_TypeDescription * pTD = nullptr;
1780 rType.getDescription( &pTD );
1781
1782 aRet.append( OUString::Concat("*** ERROR: No IdlClass for type \"")
1783 + OUString::unacquired(&pTD->pTypeName)
1784 + "\"\n*** Please check type library\n" );
1785 }
1786 }
1787 }
1788 }
1789 return aRet.makeStringAndClear();
1790 }
1791
1792
1793 // Debugging help method SbxDataType -> String
1794 static OUString Dbg_SbxDataType2String( SbxDataType eType )
1795 {
1796 OUStringBuffer aRet;
1797 switch( +eType )
1798 {
1799 case SbxEMPTY: aRet.append("SbxEMPTY"); break;
1800 case SbxNULL: aRet.append("SbxNULL"); break;
1801 case SbxINTEGER: aRet.append("SbxINTEGER"); break;
1802 case SbxLONG: aRet.append("SbxLONG"); break;
1803 case SbxSINGLE: aRet.append("SbxSINGLE"); break;
1804 case SbxDOUBLE: aRet.append("SbxDOUBLE"); break;
1805 case SbxCURRENCY: aRet.append("SbxCURRENCY"); break;
1806 case SbxDECIMAL: aRet.append("SbxDECIMAL"); break;
1807 case SbxDATE: aRet.append("SbxDATE"); break;
1808 case SbxSTRING: aRet.append("SbxSTRING"); break;
1809 case SbxOBJECT: aRet.append("SbxOBJECT"); break;
1810 case SbxERROR: aRet.append("SbxERROR"); break;
1811 case SbxBOOL: aRet.append("SbxBOOL"); break;
1812 case SbxVARIANT: aRet.append("SbxVARIANT"); break;
1813 case SbxDATAOBJECT: aRet.append("SbxDATAOBJECT"); break;
1814 case SbxCHAR: aRet.append("SbxCHAR"); break;
1815 case SbxBYTE: aRet.append("SbxBYTE"); break;
1816 case SbxUSHORT: aRet.append("SbxUSHORT"); break;
1817 case SbxULONG: aRet.append("SbxULONG"); break;
1818 case SbxSALINT64: aRet.append("SbxINT64"); break;
1819 case SbxSALUINT64: aRet.append("SbxUINT64"); break;
1820 case SbxINT: aRet.append("SbxINT"); break;
1821 case SbxUINT: aRet.append("SbxUINT"); break;
1822 case SbxVOID: aRet.append("SbxVOID"); break;
1823 case SbxHRESULT: aRet.append("SbxHRESULT"); break;
1824 case SbxPOINTER: aRet.append("SbxPOINTER"); break;
1825 case SbxDIMARRAY: aRet.append("SbxDIMARRAY"); break;
1826 case SbxCARRAY: aRet.append("SbxCARRAY"); break;
1827 case SbxUSERDEF: aRet.append("SbxUSERDEF"); break;
1828 case SbxLPSTR: aRet.append("SbxLPSTR"); break;
1829 case SbxLPWSTR: aRet.append("SbxLPWSTR"); break;
1830 case SbxCoreSTRING: aRet.append("SbxCoreSTRING"); break;
1831 case SbxOBJECT | SbxARRAY: aRet.append("SbxARRAY"); break;
1832 default: aRet.append("Unknown Sbx-Type!");break;
1833 }
1834 return aRet.makeStringAndClear();
1835 }
1836
1837 // Debugging help method to display the properties of a SbUnoObjects
1838 static OUString Impl_DumpProperties(SbUnoObject& rUnoObj)
1839 {
1840 OUStringBuffer aRet("Properties of object " + getDbgObjectName(rUnoObj));
1841
1842 // analyse the Uno-Infos to recognise the arrays
1843 Reference< XIntrospectionAccess > xAccess = rUnoObj.getIntrospectionAccess();
1844 if( !xAccess.is() )
1845 {
1846 const Reference< XInvocation >& xInvok = rUnoObj.getInvocation();
1847 if( xInvok.is() )
1848 xAccess = xInvok->getIntrospection();
1849 }
1850 if( !xAccess.is() )
1851 {
1852 aRet.append( "\nUnknown, no introspection available\n" );
1853 return aRet.makeStringAndClear();
1854 }
1855
1856 Sequence<Property> props = xAccess->getProperties( PropertyConcept::ALL - PropertyConcept::DANGEROUS );
1857 sal_uInt32 nUnoPropCount = props.getLength();
1858 const Property* pUnoProps = props.getConstArray();
1859
1860 SbxArray* pProps = rUnoObj.GetProperties();
1861 sal_uInt32 nPropCount = pProps->Count();
1862 sal_uInt32 nPropsPerLine = 1 + nPropCount / 30;
1863 for( sal_uInt32 i = 0; i < nPropCount; i++ )
1864 {
1865 SbxVariable* pVar = pProps->Get(i);
1866 if( pVar )
1867 {
1868 OUStringBuffer aPropStr;
1869 if( (i % nPropsPerLine) == 0 )
1870 aPropStr.append( "\n" );
1871
1872 // output the type and name
1873 // Is it in Uno a sequence?
1874 SbxDataType eType = pVar->GetFullType();
1875
1876 bool bMaybeVoid = false;
1877 if( i < nUnoPropCount )
1878 {
1879 const Property& rProp = pUnoProps[ i ];
1880
1881 // For MAYBEVOID freshly convert the type from Uno,
1882 // so not just SbxEMPTY is returned.
1883 if( rProp.Attributes & PropertyAttribute::MAYBEVOID )
1884 {
1885 eType = unoToSbxType( rProp.Type.getTypeClass() );
1886 bMaybeVoid = true;
1887 }
1888 if( eType == SbxOBJECT )
1889 {
1890 Type aType = rProp.Type;
1891 if( aType.getTypeClass() == TypeClass_SEQUENCE )
1892 eType = SbxDataType( SbxOBJECT | SbxARRAY );
1893 }
1894 }
1895 aPropStr.append( Dbg_SbxDataType2String( eType ) );
1896 if( bMaybeVoid )
1897 aPropStr.append( "/void" );
1898 aPropStr.append( " " + pVar->GetName() );
1899
1900 if( i == nPropCount - 1 )
1901 aPropStr.append( "\n" );
1902 else
1903 aPropStr.append( "; " );
1904
1905 aRet.append( aPropStr );
1906 }
1907 }
1908 return aRet.makeStringAndClear();
1909 }
1910
1911 // Debugging help method to display the methods of an SbUnoObjects
1912 static OUString Impl_DumpMethods(SbUnoObject& rUnoObj)
1913 {
1914 OUStringBuffer aRet("Methods of object " + getDbgObjectName(rUnoObj));
1915
1916 // XIntrospectionAccess, so that the types of the parameter could be outputted
1917 Reference< XIntrospectionAccess > xAccess = rUnoObj.getIntrospectionAccess();
1918 if( !xAccess.is() )
1919 {
1920 const Reference< XInvocation >& xInvok = rUnoObj.getInvocation();
1921 if( xInvok.is() )
1922 xAccess = xInvok->getIntrospection();
1923 }
1924 if( !xAccess.is() )
1925 {
1926 aRet.append( "\nUnknown, no introspection available\n" );
1927 return aRet.makeStringAndClear();
1928 }
1929 Sequence< Reference< XIdlMethod > > methods = xAccess->getMethods
1930 ( MethodConcept::ALL - MethodConcept::DANGEROUS );
1931 const Reference< XIdlMethod >* pUnoMethods = methods.getConstArray();
1932
1933 SbxArray* pMethods = rUnoObj.GetMethods();
1934 sal_uInt32 nMethodCount = pMethods->Count();
1935 if( !nMethodCount )
1936 {
1937 aRet.append( "\nNo methods found\n" );
1938 return aRet.makeStringAndClear();
1939 }
1940 sal_uInt32 nPropsPerLine = 1 + nMethodCount / 30;
1941 for( sal_uInt32 i = 0; i < nMethodCount; i++ )
1942 {
1943 SbxVariable* pVar = pMethods->Get(i);
1944 if( pVar )
1945 {
1946 if( (i % nPropsPerLine) == 0 )
1947 aRet.append( "\n" );
1948
1949 // address the method
1950 const Reference< XIdlMethod >& rxMethod = pUnoMethods[i];
1951
1952 // Is it in Uno a sequence?
1953 SbxDataType eType = pVar->GetFullType();
1954 if( eType == SbxOBJECT )
1955 {
1956 Reference< XIdlClass > xClass = rxMethod->getReturnType();
1957 if( xClass.is() && xClass->getTypeClass() == TypeClass_SEQUENCE )
1958 eType = SbxDataType( SbxOBJECT | SbxARRAY );
1959 }
1960 // output the name and the type
1961 aRet.append( Dbg_SbxDataType2String( eType )
1962 + " " + pVar->GetName() + " ( " );
1963
1964 // the get-method mustn't have a parameter
1965 Sequence< Reference< XIdlClass > > aParamsSeq = rxMethod->getParameterTypes();
1966 sal_uInt32 nParamCount = aParamsSeq.getLength();
1967 const Reference< XIdlClass >* pParams = aParamsSeq.getConstArray();
1968
1969 if( nParamCount > 0 )
1970 {
1971 for( sal_uInt32 j = 0; j < nParamCount; j++ )
1972 {
1973 aRet.append ( Dbg_SbxDataType2String( unoToSbxType( pParams[ j ] ) ) );
1974 if( j < nParamCount - 1 )
1975 aRet.append( ", " );
1976 }
1977 }
1978 else
1979 aRet.append( "void" );
1980
1981 aRet.append( " ) " );
1982
1983 if( i == nMethodCount - 1 )
1984 aRet.append( "\n" );
1985 else
1986 aRet.append( "; " );
1987 }
1988 }
1989 return aRet.makeStringAndClear();
1990 }
1991
1992
1993 // Implementation SbUnoObject
1994 void SbUnoObject::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
1995 {
1996 if( bNeedIntrospection )
1997 doIntrospection();
1998
1999 const SbxHint* pHint = dynamic_cast<const SbxHint*>(&rHint);
2000 if( !pHint )
2001 return;
2002
2003 SbxVariable* pVar = pHint->GetVar();
2004 SbxArray* pParams = pVar->GetParameters();
2005 SbUnoProperty* pProp = dynamic_cast<SbUnoProperty*>( pVar );
2006 SbUnoMethod* pMeth = dynamic_cast<SbUnoMethod*>( pVar );
2007 if( pProp )
2008 {
2009 bool bInvocation = pProp->isInvocationBased();
2010 if( pHint->GetId() == SfxHintId::BasicDataWanted )
2011 {
2012 // Test-Properties
2013 sal_Int32 nId = pProp->nId;
2014 if( nId < 0 )
2015 {
2016 // Id == -1: Display implemented interfaces according the ClassProvider
2017 if( nId == -1 ) // Property ID_DBG_SUPPORTEDINTERFACES"
2018 {
2019 OUString aRetStr = Impl_GetSupportedInterfaces(*this);
2020 pVar->PutString( aRetStr );
2021 }
2022 // Id == -2: output properties
2023 else if( nId == -2 ) // Property ID_DBG_PROPERTIES
2024 {
2025 // now all properties must be created
2026 implCreateAll();
2027 OUString aRetStr = Impl_DumpProperties(*this);
2028 pVar->PutString( aRetStr );
2029 }
2030 // Id == -3: output the methods
2031 else if( nId == -3 ) // Property ID_DBG_METHODS
2032 {
2033 // now all properties must be created
2034 implCreateAll();
2035 OUString aRetStr = Impl_DumpMethods(*this);
2036 pVar->PutString( aRetStr );
2037 }
2038 return;
2039 }
2040
2041 if( !bInvocation && mxUnoAccess.is() )
2042 {
2043 try
2044 {
2045 if ( maStructInfo )
2046 {
2047 StructRefInfo aMember = maStructInfo->getStructMember( pProp->GetName() );
2048 if ( aMember.isEmpty() )
2049 {
2050 StarBASIC::Error( ERRCODE_BASIC_PROPERTY_NOT_FOUND );
2051 }
2052 else
2053 {
2054 if ( pProp->isUnoStruct() )
2055 {
2056 SbUnoStructRefObject* pSbUnoObject = new SbUnoStructRefObject( pProp->GetName(), std::move(aMember) );
2057 SbxObjectRef xWrapper = static_cast<SbxObject*>(pSbUnoObject);
2058 pVar->PutObject( xWrapper.get() );
2059 }
2060 else
2061 {
2062 Any aRetAny = aMember.getValue();
2063 // take over the value from Uno to Sbx
2064 unoToSbxValue( pVar, aRetAny );
2065 }
2066 return;
2067 }
2068 }
2069 // get the value
2070 Reference< XPropertySet > xPropSet( mxUnoAccess->queryAdapter( cppu::UnoType<XPropertySet>::get()), UNO_QUERY );
2071 Any aRetAny = xPropSet->getPropertyValue( pProp->GetName() );
2072 // The use of getPropertyValue (instead of using the index) is
2073 // suboptimal, but the refactoring to XInvocation is already pending
2074 // Otherwise it is possible to use FastPropertySet
2075
2076 // take over the value from Uno to Sbx
2077 unoToSbxValue( pVar, aRetAny );
2078 }
2079 catch( const Exception& )
2080 {
2081 implHandleAnyException( ::cppu::getCaughtException() );
2082 }
2083 }
2084 else if( bInvocation && mxInvocation.is() )
2085 {
2086 try
2087 {
2088 sal_uInt32 nParamCount = pParams ? (pParams->Count() - 1) : 0;
2089 bool bCanBeConsideredAMethod = mxInvocation->hasMethod( pProp->GetName() );
2090 Any aRetAny;
2091 if ( bCanBeConsideredAMethod && nParamCount )
2092 {
2093 // Automation properties have methods, so... we need to invoke this through
2094 // XInvocation
2095 Sequence<Any> args;
2096 processAutomationParams( pParams, args, nParamCount );
2097 aRetAny = invokeAutomationMethod( pProp->GetName(), args, pParams, nParamCount, mxInvocation, INVOKETYPE::GetProp );
2098 }
2099 else
2100 aRetAny = mxInvocation->getValue( pProp->GetName() );
2101 // take over the value from Uno to Sbx
2102 unoToSbxValue( pVar, aRetAny );
2103 }
2104 catch( const Exception& )
2105 {
2106 implHandleAnyException( ::cppu::getCaughtException() );
2107 }
2108 }
2109 }
2110 else if( pHint->GetId() == SfxHintId::BasicDataChanged )
2111 {
2112 if( !bInvocation && mxUnoAccess.is() )
2113 {
2114 if( pProp->aUnoProp.Attributes & PropertyAttribute::READONLY )
2115 {
2116 StarBASIC::Error( ERRCODE_BASIC_PROP_READONLY );
2117 return;
2118 }
2119 if ( maStructInfo )
2120 {
2121 StructRefInfo aMember = maStructInfo->getStructMember( pProp->GetName() );
2122 if ( aMember.isEmpty() )
2123 {
2124 StarBASIC::Error( ERRCODE_BASIC_PROPERTY_NOT_FOUND );
2125 }
2126 else
2127 {
2128 Any aAnyValue = sbxToUnoValue( pVar, pProp->aUnoProp.Type, &pProp->aUnoProp );
2129 aMember.setValue( aAnyValue );
2130 }
2131 return;
2132 }
2133 // take over the value from Uno to Sbx
2134 Any aAnyValue = sbxToUnoValue( pVar, pProp->aUnoProp.Type, &pProp->aUnoProp );
2135 try
2136 {
2137 // set the value
2138 Reference< XPropertySet > xPropSet( mxUnoAccess->queryAdapter( cppu::UnoType<XPropertySet>::get()), UNO_QUERY );
2139 xPropSet->setPropertyValue( pProp->GetName(), aAnyValue );
2140 // The use of getPropertyValue (instead of using the index) is
2141 // suboptimal, but the refactoring to XInvocation is already pending
2142 // Otherwise it is possible to use FastPropertySet
2143 }
2144 catch( const Exception& )
2145 {
2146 implHandleAnyException( ::cppu::getCaughtException() );
2147 }
2148 }
2149 else if( bInvocation && mxInvocation.is() )
2150 {
2151 // take over the value from Uno to Sbx
2152 Any aAnyValue = sbxToUnoValueImpl( pVar );
2153 try
2154 {
2155 // set the value
2156 mxInvocation->setValue( pProp->GetName(), aAnyValue );
2157 }
2158 catch( const Exception& )
2159 {
2160 implHandleAnyException( ::cppu::getCaughtException() );
2161 }
2162 }
2163 }
2164 }
2165 else if( pMeth )
2166 {
2167 bool bInvocation = pMeth->isInvocationBased();
2168 if( pHint->GetId() == SfxHintId::BasicDataWanted )
2169 {
2170 // number of Parameter -1 because of Param0 == this
2171 sal_uInt32 nParamCount = pParams ? (pParams->Count() - 1) : 0;
2172 Sequence<Any> args;
2173 bool bOutParams = false;
2174
2175 if( !bInvocation && mxUnoAccess.is() )
2176 {
2177 // get info
2178 const Sequence<ParamInfo>& rInfoSeq = pMeth->getParamInfos();
2179 const ParamInfo* pParamInfos = rInfoSeq.getConstArray();
2180 sal_uInt32 nUnoParamCount = rInfoSeq.getLength();
2181 sal_uInt32 nAllocParamCount = nParamCount;
2182
2183 // ignore surplus parameter; alternative: throw an error
2184 if( nParamCount > nUnoParamCount )
2185 {
2186 nParamCount = nUnoParamCount;
2187 nAllocParamCount = nParamCount;
2188 }
2189 else if( nParamCount < nUnoParamCount )
2190 {
2191 SbiInstance* pInst = GetSbData()->pInst;
2192 if( pInst && pInst->IsCompatibility() )
2193 {
2194 // Check types
2195 bool bError = false;
2196 for( sal_uInt32 i = nParamCount ; i < nUnoParamCount ; i++ )
2197 {
2198 const ParamInfo& rInfo = pParamInfos[i];
2199 const Reference< XIdlClass >& rxClass = rInfo.aType;
2200 if( rxClass->getTypeClass() != TypeClass_ANY )
2201 {
2202 bError = true;
2203 StarBASIC::Error( ERRCODE_BASIC_NOT_OPTIONAL );
2204 }
2205 }
2206 if( !bError )
2207 nAllocParamCount = nUnoParamCount;
2208 }
2209 }
2210
2211 if( nAllocParamCount > 0 )
2212 {
2213 args.realloc( nAllocParamCount );
2214 Any* pAnyArgs = args.getArray();
2215 for( sal_uInt32 i = 0 ; i < nParamCount ; i++ )
2216 {
2217 const ParamInfo& rInfo = pParamInfos[i];
2218 const Reference< XIdlClass >& rxClass = rInfo.aType;
2219
2220 css::uno::Type aType( rxClass->getTypeClass(), rxClass->getName() );
2221
2222 // ATTENTION: Don't forget for Sbx-Parameter the offset!
2223 pAnyArgs[i] = sbxToUnoValue(pParams->Get(i + 1), aType);
2224
2225 // If it is not certain check whether the out-parameter are available.
2226 if( !bOutParams )
2227 {
2228 ParamMode aParamMode = rInfo.aMode;
2229 if( aParamMode != ParamMode_IN )
2230 bOutParams = true;
2231 }
2232 }
2233 }
2234 }
2235 else if( bInvocation && pParams && mxInvocation.is() )
2236 {
2237 processAutomationParams( pParams, args, nParamCount );
2238 }
2239
2240 // call the method
2241 GetSbData()->bBlockCompilerError = true; // #106433 Block compiler errors for API calls
2242 try
2243 {
2244 if( !bInvocation && mxUnoAccess.is() )
2245 {
2246 Any aRetAny = pMeth->m_xUnoMethod->invoke( getUnoAny(), args );
2247
2248 // take over the value from Uno to Sbx
2249 unoToSbxValue( pVar, aRetAny );
2250
2251 // Did we to copy back the Out-Parameter?
2252 if( bOutParams )
2253 {
2254 const Any* pAnyArgs = args.getConstArray();
2255
2256 // get info
2257 const Sequence<ParamInfo>& rInfoSeq = pMeth->getParamInfos();
2258 const ParamInfo* pParamInfos = rInfoSeq.getConstArray();
2259
2260 sal_uInt32 j;
2261 for( j = 0 ; j < nParamCount ; j++ )
2262 {
2263 const ParamInfo& rInfo = pParamInfos[j];
2264 ParamMode aParamMode = rInfo.aMode;
2265 if( aParamMode != ParamMode_IN )
2266 unoToSbxValue(pParams->Get(j + 1), pAnyArgs[j]);
2267 }
2268 }
2269 }
2270 else if( bInvocation && mxInvocation.is() )
2271 {
2272 Any aRetAny = invokeAutomationMethod( pMeth->GetName(), args, pParams, nParamCount, mxInvocation, INVOKETYPE::Func );
2273 unoToSbxValue( pVar, aRetAny );
2274 }
2275
2276 // remove parameter here, because this was not done anymore in unoToSbxValue()
2277 // for arrays
2278 if( pParams )
2279 pVar->SetParameters( nullptr );
2280 }
2281 catch( const Exception& )
2282 {
2283 implHandleAnyException( ::cppu::getCaughtException() );
2284 }
2285 GetSbData()->bBlockCompilerError = false; // #106433 Unblock compiler errors
2286 }
2287 }
2288 else
2289 SbxObject::Notify( rBC, rHint );
2290 }
2291
2292
2293 SbUnoObject::SbUnoObject( const OUString& aName_, const Any& aUnoObj_ )
2294 : SbxObject( aName_ )
2295 , bNeedIntrospection( true )
2296 , bNativeCOMObject( false )
2297 {
2298 // beat out again the default properties of Sbx
2299 Remove( u"Name"_ustr, SbxClassType::DontCare );
2300 Remove( u"Parent"_ustr, SbxClassType::DontCare );
2301
2302 // check the type of the objects
2303 TypeClass eType = aUnoObj_.getValueTypeClass();
2304 Reference< XInterface > x;
2305 if( eType == TypeClass_INTERFACE )
2306 {
2307 // get the interface from the Any
2308 aUnoObj_ >>= x;
2309 if( !x.is() )
2310 return;
2311 }
2312
2313 // Did the object have an invocation itself?
2314 mxInvocation.set( x, UNO_QUERY );
2315
2316 if( mxInvocation.is() )
2317 {
2318
2319 // get the ExactName
2320 mxExactNameInvocation.set( mxInvocation, UNO_QUERY );
2321
2322 // The remainder refers only to the introspection
2323 Reference< XTypeProvider > xTypeProvider( x, UNO_QUERY );
2324 if( !xTypeProvider.is() )
2325 {
2326 bNeedIntrospection = false;
2327 return;
2328 }
2329
2330 // Ignore introspection based members for COM objects to avoid
2331 // hiding of equally named COM symbols, e.g. XInvocation::getValue
2332 Reference< oleautomation::XAutomationObject > xAutomationObject( aUnoObj_, UNO_QUERY );
2333 if( xAutomationObject.is() )
2334 bNativeCOMObject = true;
2335 }
2336
2337 maTmpUnoObj = aUnoObj_;
2338
2339
2340 //*** Define the name ***
2341 bool bFatalError = true;
2342
2343 // Is it an interface or a struct?
2344 bool bSetClassName = false;
2345 OUString aClassName_;
2346 if( eType == TypeClass_STRUCT || eType == TypeClass_EXCEPTION )
2347 {
2348 // Struct is Ok
2349 bFatalError = false;
2350
2351 // insert the real name of the class
2352 if( aName_.isEmpty() )
2353 {
2354 aClassName_ = aUnoObj_.getValueTypeName();
2355 bSetClassName = true;
2356 }
2357 StructRefInfo aThisStruct( maTmpUnoObj, maTmpUnoObj.getValueType(), 0 );
2358 maStructInfo = std::make_shared<SbUnoStructRefObject>( GetName(), aThisStruct );
2359 }
2360 else if( eType == TypeClass_INTERFACE )
2361 {
2362 // Interface works always through the type in the Any
2363 bFatalError = false;
2364 }
2365 if( bSetClassName )
2366 SetClassName( aClassName_ );
2367
2368 // Neither interface nor Struct -> FatalError
2369 if( bFatalError )
2370 {
2371 StarBASIC::FatalError( ERRCODE_BASIC_EXCEPTION );
2372 return;
2373 }
2374
2375 // pass the introspection primal on demand
2376 }
2377
2378 SbUnoObject::~SbUnoObject()
2379 {
2380 }
2381
2382
2383 // pass the introspection on Demand
2384 void SbUnoObject::doIntrospection()
2385 {
2386 if( !bNeedIntrospection )
2387 return;
2388
2389 const Reference<XComponentContext>& xContext = comphelper::getProcessComponentContext();
2390
2391 if (!xContext.is())
2392 return;
2393
2394
2395 // get the introspection service
2396 Reference<XIntrospection> xIntrospection;
2397
2398 try
2399 {
2400 xIntrospection = theIntrospection::get(xContext);
2401 }
2402 catch ( const css::uno::DeploymentException& )
2403 {
2404 }
2405
2406 if (!xIntrospection.is())
2407 return;
2408
2409 bNeedIntrospection = false;
2410
2411 // pass the introspection
2412 try
2413 {
2414 mxUnoAccess = xIntrospection->inspect( maTmpUnoObj );
2415 }
2416 catch( const RuntimeException& e )
2417 {
2418 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION, implGetExceptionMsg( e ) );
2419 }
2420
2421 if( !mxUnoAccess.is() )
2422 {
2423 // #51475 mark to indicate an invalid object (no mxMaterialHolder)
2424 return;
2425 }
2426
2427 // get MaterialHolder from access
2428 mxMaterialHolder.set( mxUnoAccess, UNO_QUERY );
2429
2430 // get ExactName from access
2431 mxExactName.set( mxUnoAccess, UNO_QUERY );
2432 }
2433
2434
2435 // Start of a list of all SbUnoMethod-Instances
2436 static SbUnoMethod* s_pFirst = nullptr;
2437
2438 void clearUnoMethodsForBasic( StarBASIC const * pBasic )
2439 {
2440 SbUnoMethod* pMeth = s_pFirst;
2441 while( pMeth )
2442 {
2443 SbxObject* pObject = pMeth->GetParent();
2444 if ( pObject )
2445 {
2446 StarBASIC* pModBasic = dynamic_cast< StarBASIC* >( pObject->GetParent() );
2447 if ( pModBasic == pBasic )
2448 {
2449 // for now the solution is to remove the method from the list and to clear it,
2450 // but in case the element should be correctly transferred to another StarBASIC,
2451 // we should either set module parent to NULL without clearing it, or even
2452 // set the new StarBASIC as the parent of the module
2453 // pObject->SetParent( NULL );
2454
2455 if( pMeth == s_pFirst )
2456 s_pFirst = pMeth->pNext;
2457 else if( pMeth->pPrev )
2458 pMeth->pPrev->pNext = pMeth->pNext;
2459 if( pMeth->pNext )
2460 pMeth->pNext->pPrev = pMeth->pPrev;
2461
2462 pMeth->pPrev = nullptr;
2463 pMeth->pNext = nullptr;
2464
2465 pMeth->SbxValue::Clear();
2466 pObject->SbxValue::Clear();
2467
2468 // start from the beginning after object clearing, the cycle will end since the method is removed each time
2469 pMeth = s_pFirst;
2470 }
2471 else
2472 pMeth = pMeth->pNext;
2473 }
2474 else
2475 pMeth = pMeth->pNext;
2476 }
2477 }
2478
2479 void clearUnoMethods()
2480 {
2481 SbUnoMethod* pMeth = s_pFirst;
2482 while( pMeth )
2483 {
2484 pMeth->SbxValue::Clear();
2485 pMeth = pMeth->pNext;
2486 }
2487 }
2488
2489
2490 SbUnoMethod::SbUnoMethod
2491 (
2492 const OUString& aName_,
2493 SbxDataType eSbxType,
2494 Reference< XIdlMethod > const & xUnoMethod_,
2495 bool bInvocation
2496 )
2497 : SbxMethod( aName_, eSbxType )
2498 , mbInvocation( bInvocation )
2499 {
2500 m_xUnoMethod = xUnoMethod_;
2501 pParamInfoSeq = nullptr;
2502
2503 // enregister the method in a list
2504 pNext = s_pFirst;
2505 pPrev = nullptr;
2506 s_pFirst = this;
2507 if( pNext )
2508 pNext->pPrev = this;
2509 }
2510
2511 SbUnoMethod::~SbUnoMethod()
2512 {
2513 pParamInfoSeq.reset();
2514
2515 if( this == s_pFirst )
2516 s_pFirst = pNext;
2517 else if( pPrev )
2518 pPrev->pNext = pNext;
2519 if( pNext )
2520 pNext->pPrev = pPrev;
2521 }
2522
2523 SbxInfo* SbUnoMethod::GetInfo()
2524 {
2525 if( !pInfo.is() && m_xUnoMethod.is() )
2526 {
2527 SbiInstance* pInst = GetSbData()->pInst;
2528 if( pInst && pInst->IsCompatibility() )
2529 {
2530 pInfo = new SbxInfo();
2531
2532 const Sequence<ParamInfo>& rInfoSeq = getParamInfos();
2533 const ParamInfo* pParamInfos = rInfoSeq.getConstArray();
2534 sal_uInt32 nParamCount = rInfoSeq.getLength();
2535
2536 for( sal_uInt32 i = 0 ; i < nParamCount ; i++ )
2537 {
2538 const ParamInfo& rInfo = pParamInfos[i];
2539 OUString aParamName = rInfo.aName;
2540
2541 pInfo->AddParam( aParamName, SbxVARIANT, SbxFlagBits::Read );
2542 }
2543 }
2544 }
2545 return pInfo.get();
2546 }
2547
2548 const Sequence<ParamInfo>& SbUnoMethod::getParamInfos()
2549 {
2550 if (!pParamInfoSeq)
2551 {
2552 Sequence<ParamInfo> aTmp;
2553 if (m_xUnoMethod.is())
2554 aTmp = m_xUnoMethod->getParameterInfos();
2555 pParamInfoSeq.reset( new Sequence<ParamInfo>(aTmp) );
2556 }
2557 return *pParamInfoSeq;
2558 }
2559
2560 SbUnoProperty::SbUnoProperty
2561 (
2562 const OUString& aName_,
2563 SbxDataType eSbxType,
2564 SbxDataType eRealSbxType,
2565 Property aUnoProp_,
2566 sal_Int32 nId_,
2567 bool bInvocation,
2568 bool bUnoStruct
2569 )
2570 : SbxProperty( aName_, eSbxType )
2571 , aUnoProp(std::move( aUnoProp_ ))
2572 , nId( nId_ )
2573 , mbInvocation( bInvocation )
2574 , mRealType( eRealSbxType )
2575 , mbUnoStruct( bUnoStruct )
2576 {
2577 // as needed establish a dummy array so that SbiRuntime::CheckArray() works
2578 static SbxArrayRef xDummyArray = new SbxArray( SbxVARIANT );
2579 if( eSbxType & SbxARRAY )
2580 PutObject( xDummyArray.get() );
2581 }
2582
2583 SbUnoProperty::~SbUnoProperty()
2584 {}
2585
2586 bool isVeryLargeUnoProperty(SbxVariable const * pVar)
2587 {
2588 auto pUnoVar = dynamic_cast<const SbUnoProperty*>(pVar);
2589 if (!pUnoVar)
2590 return false;
2591 // The ScCellRangeObj methods will attempt to generate massive strings,
2592 // which will use up massive amounts of RAM and also lock of the program
2593 // for some time.
2594 const OUString & aUnoName = pUnoVar->getUnoName();
2595 if (aUnoName == "DataArray" || aUnoName == "FormulaArray")
2596 {
2597 auto pParent = dynamic_cast<const SbUnoObject*>(pUnoVar->GetParent());
2598 if (!pParent)
2599 return false;
2600 css::uno::Any aAny = const_cast<SbUnoObject*>(pParent)->getUnoAny();
2601 css::uno::Reference<css::sheet::XSheetCellCursor> xCursor = aAny.query<css::sheet::XSheetCellCursor>();
2602 if (xCursor)
2603 return true;
2604 }
2605 return false;
2606 }
2607
2608 SbxVariable* SbUnoObject::Find( const OUString& rName, SbxClassType t )
2609 {
2610 static Reference< XIdlMethod > xDummyMethod;
2611 static Property aDummyProp;
2612
2613 SbxVariable* pRes = SbxObject::Find( rName, t );
2614
2615 if( bNeedIntrospection )
2616 doIntrospection();
2617
2618 // New 1999-03-04: Create properties on demand. Therefore search now via
2619 // IntrospectionAccess if a property or a method of the required name exist
2620 if( !pRes )
2621 {
2622 OUString aUName( rName );
2623 if( mxUnoAccess.is() && !bNativeCOMObject )
2624 {
2625 if( mxExactName.is() )
2626 {
2627 OUString aUExactName = mxExactName->getExactName( aUName );
2628 if( !aUExactName.isEmpty() )
2629 {
2630 aUName = aUExactName;
2631 }
2632 }
2633 if( mxUnoAccess->hasProperty( aUName, PropertyConcept::ALL - PropertyConcept::DANGEROUS ) )
2634 {
2635 const Property aProp = mxUnoAccess->
2636 getProperty( aUName, PropertyConcept::ALL - PropertyConcept::DANGEROUS );
2637
2638 // If the property could be void the type had to be set to Variant
2639 SbxDataType eSbxType;
2640 if( aProp.Attributes & PropertyAttribute::MAYBEVOID )
2641 eSbxType = SbxVARIANT;
2642 else
2643 eSbxType = unoToSbxType( aProp.Type.getTypeClass() );
2644
2645 SbxDataType eRealSbxType = ( ( aProp.Attributes & PropertyAttribute::MAYBEVOID ) ? unoToSbxType( aProp.Type.getTypeClass() ) : eSbxType );
2646 // create the property and superimpose it
2647 auto pProp = tools::make_ref<SbUnoProperty>( aProp.Name, eSbxType, eRealSbxType, aProp, 0, false, ( aProp.Type.getTypeClass() == css::uno::TypeClass_STRUCT ) );
2648 QuickInsert( pProp.get() );
2649 pRes = pProp.get();
2650 }
2651 else if( mxUnoAccess->hasMethod( aUName,
2652 MethodConcept::ALL - MethodConcept::DANGEROUS ) )
2653 {
2654 // address the method
2655 const Reference< XIdlMethod > xMethod = mxUnoAccess->
2656 getMethod( aUName, MethodConcept::ALL - MethodConcept::DANGEROUS );
2657
2658 // create SbUnoMethod and superimpose it
2659 auto xMethRef = tools::make_ref<SbUnoMethod>( xMethod->getName(),
2660 unoToSbxType( xMethod->getReturnType() ), xMethod, false );
2661 QuickInsert( xMethRef.get() );
2662 pRes = xMethRef.get();
2663 }
2664
2665 // If nothing was found check via XNameAccess
2666 if( !pRes )
2667 {
2668 try
2669 {
2670 Reference< XNameAccess > xNameAccess( mxUnoAccess->queryAdapter( cppu::UnoType<XPropertySet>::get()), UNO_QUERY );
2671
2672 if( xNameAccess.is() && xNameAccess->hasByName( rName ) )
2673 {
2674 Any aAny = xNameAccess->getByName( rName );
2675
2676 // ATTENTION: Because of XNameAccess, the variable generated here
2677 // may not be included as a fixed property in the object and therefore
2678 // won't be stored anywhere.
2679 // If this leads to problems, it has to be created
2680 // synthetically or a class SbUnoNameAccessProperty,
2681 // which checks the existence on access and which
2682 // is disposed if the name is not found anymore.
2683 pRes = new SbxVariable( SbxVARIANT );
2684 unoToSbxValue( pRes, aAny );
2685 }
2686 }
2687 catch( const NoSuchElementException& e )
2688 {
2689 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION, implGetExceptionMsg( e ) );
2690 }
2691 catch( const Exception& )
2692 {
2693 // Establish so that the exception error will not be overwritten
2694 if( !pRes )
2695 pRes = new SbxVariable( SbxVARIANT );
2696
2697 implHandleAnyException( ::cppu::getCaughtException() );
2698 }
2699 }
2700 }
2701 if( !pRes && mxInvocation.is() )
2702 {
2703 if( mxExactNameInvocation.is() )
2704 {
2705 OUString aUExactName = mxExactNameInvocation->getExactName( aUName );
2706 if( !aUExactName.isEmpty() )
2707 {
2708 aUName = aUExactName;
2709 }
2710 }
2711
2712 try
2713 {
2714 if( mxInvocation->hasProperty( aUName ) )
2715 {
2716 // create a property and superimpose it
2717 auto xVarRef = tools::make_ref<SbUnoProperty>( aUName, SbxVARIANT, SbxVARIANT, aDummyProp, 0, true, false );
2718 QuickInsert( xVarRef.get() );
2719 pRes = xVarRef.get();
2720 }
2721 else if( mxInvocation->hasMethod( aUName ) )
2722 {
2723 // create SbUnoMethode and superimpose it
2724 auto xMethRef = tools::make_ref<SbUnoMethod>( aUName, SbxVARIANT, xDummyMethod, true );
2725 QuickInsert( xMethRef.get() );
2726 pRes = xMethRef.get();
2727 }
2728 else
2729 {
2730 Reference< XDirectInvocation > xDirectInvoke( mxInvocation, UNO_QUERY );
2731 if ( xDirectInvoke.is() && xDirectInvoke->hasMember( aUName ) )
2732 {
2733 auto xMethRef = tools::make_ref<SbUnoMethod>( aUName, SbxVARIANT, xDummyMethod, true );
2734 QuickInsert( xMethRef.get() );
2735 pRes = xMethRef.get();
2736 }
2737
2738 }
2739 }
2740 catch( const RuntimeException& e )
2741 {
2742 // Establish so that the exception error will not be overwritten
2743 if( !pRes )
2744 pRes = new SbxVariable( SbxVARIANT );
2745
2746 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION, implGetExceptionMsg( e ) );
2747 }
2748 }
2749 }
2750
2751 // At the very end checking if the Dbg_-Properties are meant
2752
2753 if( !pRes )
2754 {
2755 if( rName.equalsIgnoreAsciiCase(ID_DBG_SUPPORTEDINTERFACES) ||
2756 rName.equalsIgnoreAsciiCase(ID_DBG_PROPERTIES) ||
2757 rName.equalsIgnoreAsciiCase(ID_DBG_METHODS) )
2758 {
2759 // Create
2760 implCreateDbgProperties();
2761
2762 // Now they have to be found regular
2763 pRes = SbxObject::Find( rName, SbxClassType::DontCare );
2764 }
2765 }
2766 return pRes;
2767 }
2768
2769
2770 // help method to create the dbg_-Properties
2771 void SbUnoObject::implCreateDbgProperties()
2772 {
2773 Property aProp;
2774
2775 // Id == -1: display the implemented interfaces corresponding the ClassProvider
2776 auto xVarRef = tools::make_ref<SbUnoProperty>( ID_DBG_SUPPORTEDINTERFACES, SbxSTRING, SbxSTRING, aProp, -1, false, false );
2777 QuickInsert( xVarRef.get() );
2778
2779 // Id == -2: output the properties
2780 xVarRef = tools::make_ref<SbUnoProperty>( ID_DBG_PROPERTIES, SbxSTRING, SbxSTRING, aProp, -2, false, false );
2781 QuickInsert( xVarRef.get() );
2782
2783 // Id == -3: output the Methods
2784 xVarRef = tools::make_ref<SbUnoProperty>( ID_DBG_METHODS, SbxSTRING, SbxSTRING, aProp, -3, false, false );
2785 QuickInsert( xVarRef.get() );
2786 }
2787
2788 void SbUnoObject::implCreateAll()
2789 {
2790 // throw away all existing methods and properties
2791 pMethods = tools::make_ref<SbxArray>();
2792 pProps = tools::make_ref<SbxArray>();
2793
2794 if( bNeedIntrospection ) doIntrospection();
2795
2796 // get introspection
2797 Reference< XIntrospectionAccess > xAccess = mxUnoAccess;
2798 if( !xAccess.is() || bNativeCOMObject )
2799 {
2800 if( mxInvocation.is() )
2801 xAccess = mxInvocation->getIntrospection();
2802 else if( bNativeCOMObject )
2803 return;
2804 }
2805 if( !xAccess.is() )
2806 return;
2807
2808 // Establish properties
2809 Sequence<Property> props = xAccess->getProperties( PropertyConcept::ALL - PropertyConcept::DANGEROUS );
2810 sal_uInt32 nPropCount = props.getLength();
2811 const Property* pProps_ = props.getConstArray();
2812
2813 sal_uInt32 i;
2814 for( i = 0 ; i < nPropCount ; i++ )
2815 {
2816 const Property& rProp = pProps_[ i ];
2817
2818 // If the property could be void the type had to be set to Variant
2819 SbxDataType eSbxType;
2820 if( rProp.Attributes & PropertyAttribute::MAYBEVOID )
2821 eSbxType = SbxVARIANT;
2822 else
2823 eSbxType = unoToSbxType( rProp.Type.getTypeClass() );
2824
2825 SbxDataType eRealSbxType = ( ( rProp.Attributes & PropertyAttribute::MAYBEVOID ) ? unoToSbxType( rProp.Type.getTypeClass() ) : eSbxType );
2826 // Create property and superimpose it
2827 auto xVarRef = tools::make_ref<SbUnoProperty>( rProp.Name, eSbxType, eRealSbxType, rProp, i, false, ( rProp.Type.getTypeClass() == css::uno::TypeClass_STRUCT ) );
2828 QuickInsert( xVarRef.get() );
2829 }
2830
2831 // Create Dbg_-Properties
2832 implCreateDbgProperties();
2833
2834 // Create methods
2835 Sequence< Reference< XIdlMethod > > aMethodSeq = xAccess->getMethods
2836 ( MethodConcept::ALL - MethodConcept::DANGEROUS );
2837 sal_uInt32 nMethCount = aMethodSeq.getLength();
2838 const Reference< XIdlMethod >* pMethods_ = aMethodSeq.getConstArray();
2839 for( i = 0 ; i < nMethCount ; i++ )
2840 {
2841 // address method
2842 const Reference< XIdlMethod >& rxMethod = pMethods_[i];
2843
2844 // Create SbUnoMethod and superimpose it
2845 auto xMethRef = tools::make_ref<SbUnoMethod>
2846 ( rxMethod->getName(), unoToSbxType( rxMethod->getReturnType() ), rxMethod, false );
2847 QuickInsert( xMethRef.get() );
2848 }
2849 }
2850
2851
2852 // output the value
2853 Any SbUnoObject::getUnoAny()
2854 {
2855 Any aRetAny;
2856 if( bNeedIntrospection ) doIntrospection();
2857 if ( maStructInfo )
2858 aRetAny = maTmpUnoObj;
2859 else if( mxMaterialHolder.is() )
2860 aRetAny = mxMaterialHolder->getMaterial();
2861 else if( mxInvocation.is() )
2862 aRetAny <<= mxInvocation;
2863 return aRetAny;
2864 }
2865
2866 double SbUnoObject::GetDate() const
2867 {
2868 // Handle structure info here. Without this, oleautomation data may be unable to convert
2869 // to the wanted data type in the base method.
2870 if (maStructInfo)
2871 {
2872 if (css::bridge::oleautomation::Date date; maTmpUnoObj >>= date)
2873 return date.Value;
2874 }
2875
2876 return SbxObject::GetDate();
2877 }
2878
2879 // help method to create a Uno-Struct per CoreReflection
2880 static SbUnoObjectRef Impl_CreateUnoStruct( const OUString& aClassName )
2881 {
2882 // get CoreReflection
2883 Reference< XIdlReflection > xCoreReflection = getCoreReflection_Impl();
2884 if( !xCoreReflection.is() )
2885 return nullptr;
2886
2887 // search for the class
2888 Reference< XIdlClass > xClass;
2889 const Reference< XHierarchicalNameAccess >& xHarryName =
2890 getCoreReflection_HierarchicalNameAccess_Impl();
2891 if( xHarryName.is() && xHarryName->hasByHierarchicalName( aClassName ) )
2892 xClass = xCoreReflection->forName( aClassName );
2893 if( !xClass.is() )
2894 return nullptr;
2895
2896 // Is it really a struct?
2897 TypeClass eType = xClass->getTypeClass();
2898 if ( ( eType != TypeClass_STRUCT ) && ( eType != TypeClass_EXCEPTION ) )
2899 return nullptr;
2900
2901 // create an instance
2902 Any aNewAny;
2903 xClass->createObject( aNewAny );
2904 // make a SbUnoObject out of it
2905 SbUnoObjectRef pUnoObj = new SbUnoObject( aClassName, aNewAny );
2906 return pUnoObj;
2907 }
2908
2909
2910 // Factory-Class to create Uno-Structs per DIM AS NEW
2911 SbxBaseRef SbUnoFactory::Create( sal_uInt16, sal_uInt32 )
2912 {
2913 // Via SbxId nothing works in Uno
2914 return nullptr;
2915 }
2916
2917 SbxObjectRef SbUnoFactory::CreateObject( const OUString& rClassName )
2918 {
2919 return Impl_CreateUnoStruct( rClassName ).get();
2920 }
2921
2922
2923 // Provisional interface for the UNO-Connection
2924 // Deliver a SbxObject, that wrap a Uno-Interface
2925 SbxObjectRef GetSbUnoObject( const OUString& aName, const Any& aUnoObj_ )
2926 {
2927 return new SbUnoObject( aName, aUnoObj_ );
2928 }
2929
2930 // Force creation of all properties for debugging
2931 void createAllObjectProperties( SbxObject* pObj )
2932 {
2933 if( !pObj )
2934 return;
2935
2936 SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>( pObj );
2937 SbUnoStructRefObject* pUnoStructObj = dynamic_cast<SbUnoStructRefObject*>( pObj );
2938 if( pUnoObj )
2939 {
2940 pUnoObj->createAllProperties();
2941 }
2942 else if ( pUnoStructObj )
2943 {
2944 pUnoStructObj->createAllProperties();
2945 }
2946 }
2947
2948
2949 void RTL_Impl_CreateUnoStruct( SbxArray& rPar )
2950 {
2951 // We need 1 parameter minimum
2952 if (rPar.Count() < 2)
2953 {
2954 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
2955 return;
2956 }
2957
2958 // get the name of the class of the struct
2959 OUString aClassName = rPar.Get(1)->GetOUString();
2960
2961 // try to create Struct with the same name
2962 SbUnoObjectRef xUnoObj = Impl_CreateUnoStruct( aClassName );
2963 if( !xUnoObj.is() )
2964 {
2965 return;
2966 }
2967 // return the object
2968 SbxVariableRef refVar = rPar.Get(0);
2969 refVar->PutObject( xUnoObj.get() );
2970 }
2971
2972 void RTL_Impl_CreateUnoService( SbxArray& rPar )
2973 {
2974 // We need 1 Parameter minimum
2975 if (rPar.Count() < 2)
2976 {
2977 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
2978 return;
2979 }
2980
2981 // get the name of the class of the struct
2982 OUString aServiceName = rPar.Get(1)->GetOUString();
2983
2984 // search for the service and instantiate it
2985 Reference< XMultiServiceFactory > xFactory( comphelper::getProcessServiceFactory() );
2986 Reference< XInterface > xInterface;
2987 try
2988 {
2989 xInterface = xFactory->createInstance( aServiceName );
2990 }
2991 catch( const Exception& )
2992 {
2993 implHandleAnyException( ::cppu::getCaughtException() );
2994 }
2995
2996 SbxVariableRef refVar = rPar.Get(0);
2997 if( xInterface.is() )
2998 {
2999 // Create a SbUnoObject out of it and return it
3000 SbUnoObjectRef xUnoObj = new SbUnoObject( aServiceName, Any(xInterface) );
3001 if( xUnoObj->getUnoAny().hasValue() )
3002 {
3003 // return the object
3004 refVar->PutObject( xUnoObj.get() );
3005 }
3006 else
3007 {
3008 refVar->PutObject( nullptr );
3009 }
3010 }
3011 else
3012 {
3013 refVar->PutObject( nullptr );
3014 }
3015 }
3016
3017 void RTL_Impl_CreateUnoServiceWithArguments( SbxArray& rPar )
3018 {
3019 // We need 2 parameter minimum
3020 if (rPar.Count() < 3)
3021 {
3022 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
3023 return;
3024 }
3025
3026 // get the name of the class of the struct
3027 OUString aServiceName = rPar.Get(1)->GetOUString();
3028 Any aArgAsAny = sbxToUnoValue(rPar.Get(2),
3029 cppu::UnoType<Sequence<Any>>::get() );
3030 Sequence< Any > aArgs;
3031 aArgAsAny >>= aArgs;
3032
3033 // search for the service and instantiate it
3034 Reference< XMultiServiceFactory > xFactory( comphelper::getProcessServiceFactory() );
3035 Reference< XInterface > xInterface;
3036 try
3037 {
3038 xInterface = xFactory->createInstanceWithArguments( aServiceName, aArgs );
3039 }
3040 catch( const Exception& )
3041 {
3042 implHandleAnyException( ::cppu::getCaughtException() );
3043 }
3044
3045 SbxVariableRef refVar = rPar.Get(0);
3046 if( xInterface.is() )
3047 {
3048 // Create a SbUnoObject out of it and return it
3049 SbUnoObjectRef xUnoObj = new SbUnoObject( aServiceName, Any(xInterface) );
3050 if( xUnoObj->getUnoAny().hasValue() )
3051 {
3052 // return the object
3053 refVar->PutObject( xUnoObj.get() );
3054 }
3055 else
3056 {
3057 refVar->PutObject( nullptr );
3058 }
3059 }
3060 else
3061 {
3062 refVar->PutObject( nullptr );
3063 }
3064 }
3065
3066 void RTL_Impl_GetProcessServiceManager( SbxArray& rPar )
3067 {
3068 SbxVariableRef refVar = rPar.Get(0);
3069
3070 // get the global service manager
3071 Reference< XMultiServiceFactory > xFactory( comphelper::getProcessServiceFactory() );
3072
3073 // Create a SbUnoObject out of it and return it
3074 SbUnoObjectRef xUnoObj = new SbUnoObject( u"ProcessServiceManager"_ustr, Any(xFactory) );
3075 refVar->PutObject( xUnoObj.get() );
3076 }
3077
3078 void RTL_Impl_HasInterfaces( SbxArray& rPar )
3079 {
3080 // We need 2 parameter minimum
3081 sal_uInt32 nParCount = rPar.Count();
3082 if( nParCount < 3 )
3083 {
3084 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
3085 return;
3086 }
3087
3088 // variable for the return value
3089 SbxVariableRef refVar = rPar.Get(0);
3090 refVar->PutBool( false );
3091
3092 // get the Uno-Object
3093 SbxBaseRef pObj = rPar.Get(1)->GetObject();
3094 auto obj = dynamic_cast<SbUnoObject*>( pObj.get() );
3095 if( obj == nullptr )
3096 {
3097 return;
3098 }
3099 Any aAny = obj->getUnoAny();
3100 auto x = o3tl::tryAccess<Reference<XInterface>>(aAny);
3101 if( !x )
3102 {
3103 return;
3104 }
3105
3106 // get CoreReflection
3107 Reference< XIdlReflection > xCoreReflection = getCoreReflection_Impl();
3108 if( !xCoreReflection.is() )
3109 {
3110 return;
3111 }
3112 for( sal_uInt32 i = 2 ; i < nParCount ; i++ )
3113 {
3114 // get the name of the interface of the struct
3115 OUString aIfaceName = rPar.Get(i)->GetOUString();
3116
3117 // search for the class
3118 Reference< XIdlClass > xClass = xCoreReflection->forName( aIfaceName );
3119 if( !xClass.is() )
3120 {
3121 return;
3122 }
3123 // check if the interface will be supported
3124 OUString aClassName = xClass->getName();
3125 Type aClassType( xClass->getTypeClass(), aClassName );
3126 if( !(*x)->queryInterface( aClassType ).hasValue() )
3127 {
3128 return;
3129 }
3130 }
3131
3132 // Everything works; then return TRUE
3133 refVar->PutBool( true );
3134 }
3135
3136 void RTL_Impl_IsUnoStruct( SbxArray& rPar )
3137 {
3138 // We need 1 parameter minimum
3139 if (rPar.Count() < 2)
3140 {
3141 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
3142 return;
3143 }
3144
3145 // variable for the return value
3146 SbxVariableRef refVar = rPar.Get(0);
3147 refVar->PutBool( false );
3148
3149 // get the Uno-Object
3150 SbxVariableRef xParam = rPar.Get(1);
3151 if( !xParam->IsObject() )
3152 {
3153 return;
3154 }
3155 SbxBaseRef pObj = xParam->GetObject();
3156 auto obj = dynamic_cast<SbUnoObject*>( pObj.get() );
3157 if( obj == nullptr )
3158 {
3159 return;
3160 }
3161 Any aAny = obj->getUnoAny();
3162 TypeClass eType = aAny.getValueTypeClass();
3163 if( eType == TypeClass_STRUCT )
3164 {
3165 refVar->PutBool( true );
3166 }
3167 }
3168
3169
3170 void RTL_Impl_EqualUnoObjects( SbxArray& rPar )
3171 {
3172 if (rPar.Count() < 3)
3173 {
3174 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
3175 return;
3176 }
3177
3178 // variable for the return value
3179 SbxVariableRef refVar = rPar.Get(0);
3180 refVar->PutBool( false );
3181
3182 // get the Uno-Objects
3183 SbxVariableRef xParam1 = rPar.Get(1);
3184 if( !xParam1->IsObject() )
3185 {
3186 return;
3187 }
3188 SbxBaseRef pObj1 = xParam1->GetObject();
3189 auto obj1 = dynamic_cast<SbUnoObject*>( pObj1.get() );
3190 if( obj1 == nullptr )
3191 {
3192 return;
3193 }
3194 Any aAny1 = obj1->getUnoAny();
3195 TypeClass eType1 = aAny1.getValueTypeClass();
3196 if( eType1 != TypeClass_INTERFACE )
3197 {
3198 return;
3199 }
3200 Reference< XInterface > x1;
3201 aAny1 >>= x1;
3202
3203 SbxVariableRef xParam2 = rPar.Get(2);
3204 if( !xParam2->IsObject() )
3205 {
3206 return;
3207 }
3208 SbxBaseRef pObj2 = xParam2->GetObject();
3209 auto obj2 = dynamic_cast<SbUnoObject*>( pObj2.get() );
3210 if( obj2 == nullptr )
3211 {
3212 return;
3213 }
3214 Any aAny2 = obj2->getUnoAny();
3215 TypeClass eType2 = aAny2.getValueTypeClass();
3216 if( eType2 != TypeClass_INTERFACE )
3217 {
3218 return;
3219 }
3220 Reference< XInterface > x2;
3221 aAny2 >>= x2;
3222
3223 if( x1 == x2 )
3224 {
3225 refVar->PutBool( true );
3226 }
3227 }
3228
3229
3230 // helper wrapper function to interact with TypeProvider and
3231 // XTypeDescriptionEnumerationAccess.
3232 // if it fails for whatever reason
3233 // returned Reference<> be null e.g. .is() will be false
3234
3235 static Reference< XTypeDescriptionEnumeration > getTypeDescriptorEnumeration( const OUString& sSearchRoot,
3236 const Sequence< TypeClass >& types,
3237 TypeDescriptionSearchDepth depth )
3238 {
3239 Reference< XTypeDescriptionEnumeration > xEnum;
3240 Reference< XTypeDescriptionEnumerationAccess> xTypeEnumAccess( getTypeProvider_Impl(), UNO_QUERY );
3241 if ( xTypeEnumAccess.is() )
3242 {
3243 try
3244 {
3245 xEnum = xTypeEnumAccess->createTypeDescriptionEnumeration(
3246 sSearchRoot, types, depth );
3247 }
3248 catch(const NoSuchTypeNameException& /*nstne*/ ) {}
3249 catch(const InvalidTypeNameException& /*nstne*/ ) {}
3250 }
3251 return xEnum;
3252 }
3253
3254 VBAConstantHelper&
3255 VBAConstantHelper::instance()
3256 {
3257 static VBAConstantHelper aHelper;
3258 return aHelper;
3259 }
3260
3261 void VBAConstantHelper::init()
3262 {
3263 if ( isInited )
3264 return;
3265
3266 Reference< XTypeDescriptionEnumeration > xEnum = getTypeDescriptorEnumeration( u"ooo.vba"_ustr, {TypeClass_CONSTANTS}, TypeDescriptionSearchDepth_INFINITE );
3267
3268 if ( !xEnum.is())
3269 {
3270 return; //NULL;
3271 }
3272 while ( xEnum->hasMoreElements() )
3273 {
3274 Reference< XConstantsTypeDescription > xConstants( xEnum->nextElement(), UNO_QUERY );
3275 if ( xConstants.is() )
3276 {
3277 // store constant group name
3278 OUString sFullName = xConstants->getName();
3279 sal_Int32 indexLastDot = sFullName.lastIndexOf('.');
3280 OUString sLeafName( sFullName );
3281 if ( indexLastDot > -1 )
3282 {
3283 sLeafName = sFullName.copy( indexLastDot + 1);
3284 }
3285 aConstCache.push_back( sLeafName ); // assume constant group names are unique
3286 const Sequence< Reference< XConstantTypeDescription > > aConsts = xConstants->getConstants();
3287 for (const auto& ctd : aConsts)
3288 {
3289 // store constant member name
3290 sFullName = ctd->getName();
3291 indexLastDot = sFullName.lastIndexOf('.');
3292 sLeafName = sFullName;
3293 if ( indexLastDot > -1 )
3294 {
3295 sLeafName = sFullName.copy( indexLastDot + 1);
3296 }
3297 aConstHash[ sLeafName.toAsciiLowerCase() ] = ctd->getConstantValue();
3298 }
3299 }
3300 }
3301 isInited = true;
3302 }
3303
3304 bool
3305 VBAConstantHelper::isVBAConstantType( std::u16string_view rName )
3306 {
3307 init();
3308 bool bConstant = false;
3309
3310 for (auto const& elem : aConstCache)
3311 {
3312 if( o3tl::equalsIgnoreAsciiCase(rName, elem) )
3313 {
3314 bConstant = true;
3315 break;
3316 }
3317 }
3318 return bConstant;
3319 }
3320
3321 SbxVariable*
3322 VBAConstantHelper::getVBAConstant( const OUString& rName )
3323 {
3324 SbxVariable* pConst = nullptr;
3325 init();
3326
3327 auto it = aConstHash.find( rName.toAsciiLowerCase() );
3328
3329 if ( it != aConstHash.end() )
3330 {
3331 pConst = new SbxVariable( SbxVARIANT );
3332 pConst->SetName( rName );
3333 unoToSbxValue( pConst, it->second );
3334 }
3335
3336 return pConst;
3337 }
3338
3339 // Function to search for a global identifier in the
3340 // UnoScope and to wrap it for Sbx
3341 SbUnoClass* findUnoClass( const OUString& rName )
3342 {
3343 // #105550 Check if module exists
3344 SbUnoClass* pUnoClass = nullptr;
3345
3346 const Reference< XHierarchicalNameAccess >& xTypeAccess = getTypeProvider_Impl();
3347 if( xTypeAccess->hasByHierarchicalName( rName ) )
3348 {
3349 Any aRet = xTypeAccess->getByHierarchicalName( rName );
3350 Reference< XTypeDescription > xTypeDesc;
3351 aRet >>= xTypeDesc;
3352
3353 if( xTypeDesc.is() )
3354 {
3355 TypeClass eTypeClass = xTypeDesc->getTypeClass();
3356 if( eTypeClass == TypeClass_MODULE || eTypeClass == TypeClass_CONSTANTS )
3357 {
3358 pUnoClass = new SbUnoClass( rName );
3359 }
3360 }
3361 }
3362 return pUnoClass;
3363 }
3364
3365 SbxVariable* SbUnoClass::Find( const OUString& rName, SbxClassType )
3366 {
3367 SbxVariable* pRes = SbxObject::Find( rName, SbxClassType::Variable );
3368
3369 // If nothing were located the submodule isn't known yet
3370 if( !pRes )
3371 {
3372 // If it is already a class, ask for the field
3373 if( m_xClass.is() )
3374 {
3375 // Is it a field(?)
3376 Reference< XIdlField > xField = m_xClass->getField( rName );
3377 if( xField.is() )
3378 {
3379 try
3380 {
3381 Any aAny = xField->get( {} ); //TODO: does this make sense?
3382
3383 // Convert to Sbx
3384 pRes = new SbxVariable( SbxVARIANT );
3385 pRes->SetName( rName );
3386 unoToSbxValue( pRes, aAny );
3387 }
3388 catch( const Exception& )
3389 {
3390 implHandleAnyException( ::cppu::getCaughtException() );
3391 }
3392 }
3393 }
3394 else
3395 {
3396 // expand fully qualified name
3397 OUString aNewName = GetName()
3398 + "."
3399 + rName;
3400
3401 // get CoreReflection
3402 Reference< XIdlReflection > xCoreReflection = getCoreReflection_Impl();
3403 if( xCoreReflection.is() )
3404 {
3405 // Is it a constant?
3406 Reference< XHierarchicalNameAccess > xHarryName( xCoreReflection, UNO_QUERY );
3407 if( xHarryName.is() )
3408 {
3409 try
3410 {
3411 Any aValue = xHarryName->getByHierarchicalName( aNewName );
3412 TypeClass eType = aValue.getValueTypeClass();
3413
3414 // Interface located? Then it is a class
3415 if( eType == TypeClass_INTERFACE )
3416 {
3417 Reference< XIdlClass > xClass( aValue, UNO_QUERY );
3418 if( xClass.is() )
3419 {
3420 pRes = new SbxVariable( SbxVARIANT );
3421 SbxObjectRef xWrapper = static_cast<SbxObject*>(new SbUnoClass( aNewName, xClass ));
3422 pRes->PutObject( xWrapper.get() );
3423 }
3424 }
3425 else
3426 {
3427 pRes = new SbxVariable( SbxVARIANT );
3428 unoToSbxValue( pRes, aValue );
3429 }
3430 }
3431 catch( const NoSuchElementException& )
3432 {
3433 }
3434 }
3435
3436 // Otherwise take it again as class
3437 if( !pRes )
3438 {
3439 SbUnoClass* pNewClass = findUnoClass( aNewName );
3440 if( pNewClass )
3441 {
3442 pRes = new SbxVariable( SbxVARIANT );
3443 SbxObjectRef xWrapper = static_cast<SbxObject*>(pNewClass);
3444 pRes->PutObject( xWrapper.get() );
3445 }
3446 }
3447
3448 // A UNO service?
3449 if( !pRes )
3450 {
3451 SbUnoService* pUnoService = findUnoService( aNewName );
3452 if( pUnoService )
3453 {
3454 pRes = new SbxVariable( SbxVARIANT );
3455 SbxObjectRef xWrapper = static_cast<SbxObject*>(pUnoService);
3456 pRes->PutObject( xWrapper.get() );
3457 }
3458 }
3459
3460 // A UNO singleton?
3461 if( !pRes )
3462 {
3463 SbUnoSingleton* pUnoSingleton = findUnoSingleton( aNewName );
3464 if( pUnoSingleton )
3465 {
3466 pRes = new SbxVariable( SbxVARIANT );
3467 SbxObjectRef xWrapper = static_cast<SbxObject*>(pUnoSingleton);
3468 pRes->PutObject( xWrapper.get() );
3469 }
3470 }
3471 }
3472 }
3473
3474 if( pRes )
3475 {
3476 pRes->SetName( rName );
3477
3478 // Insert variable, so that it could be found later
3479 QuickInsert( pRes );
3480
3481 // Take us out as listener at once,
3482 // the values are all constant
3483 if( pRes->IsBroadcaster() )
3484 EndListening( pRes->GetBroadcaster(), true );
3485 }
3486 }
3487 return pRes;
3488 }
3489
3490
3491 SbUnoService* findUnoService( const OUString& rName )
3492 {
3493 SbUnoService* pSbUnoService = nullptr;
3494
3495 const Reference< XHierarchicalNameAccess >& xTypeAccess = getTypeProvider_Impl();
3496 if( xTypeAccess->hasByHierarchicalName( rName ) )
3497 {
3498 Any aRet = xTypeAccess->getByHierarchicalName( rName );
3499 Reference< XTypeDescription > xTypeDesc;
3500 aRet >>= xTypeDesc;
3501
3502 if( xTypeDesc.is() )
3503 {
3504 TypeClass eTypeClass = xTypeDesc->getTypeClass();
3505 if( eTypeClass == TypeClass_SERVICE )
3506 {
3507 Reference< XServiceTypeDescription2 > xServiceTypeDesc( xTypeDesc, UNO_QUERY );
3508 if( xServiceTypeDesc.is() )
3509 pSbUnoService = new SbUnoService( rName, xServiceTypeDesc );
3510 }
3511 }
3512 }
3513 return pSbUnoService;
3514 }
3515
3516 SbxVariable* SbUnoService::Find( const OUString& rName, SbxClassType )
3517 {
3518 SbxVariable* pRes = SbxObject::Find( rName, SbxClassType::Method );
3519
3520 if( !pRes )
3521 {
3522 // If it is already a class ask for a field
3523 if( m_bNeedsInit && m_xServiceTypeDesc.is() )
3524 {
3525 m_bNeedsInit = false;
3526
3527 Sequence< Reference< XServiceConstructorDescription > > aSCDSeq = m_xServiceTypeDesc->getConstructors();
3528 const Reference< XServiceConstructorDescription >* pCtorSeq = aSCDSeq.getConstArray();
3529 int nCtorCount = aSCDSeq.getLength();
3530 for( int i = 0 ; i < nCtorCount ; ++i )
3531 {
3532 Reference< XServiceConstructorDescription > xCtor = pCtorSeq[i];
3533
3534 OUString aName( xCtor->getName() );
3535 if( aName.isEmpty() )
3536 {
3537 if( xCtor->isDefaultConstructor() )
3538 {
3539 aName = "create";
3540 }
3541 }
3542
3543 if( !aName.isEmpty() )
3544 {
3545 // Create and insert SbUnoServiceCtor
3546 SbxVariableRef xSbCtorRef = new SbUnoServiceCtor( aName, xCtor );
3547 QuickInsert( xSbCtorRef.get() );
3548 }
3549 }
3550 pRes = SbxObject::Find( rName, SbxClassType::Method );
3551 }
3552 }
3553
3554 return pRes;
3555 }
3556
3557 void SbUnoService::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
3558 {
3559 const SbxHint* pHint = dynamic_cast<const SbxHint*>(&rHint);
3560 if( !pHint )
3561 return;
3562
3563 SbxVariable* pVar = pHint->GetVar();
3564 SbxArray* pParams = pVar->GetParameters();
3565 SbUnoServiceCtor* pUnoCtor = dynamic_cast<SbUnoServiceCtor*>( pVar );
3566 if( pUnoCtor && pHint->GetId() == SfxHintId::BasicDataWanted )
3567 {
3568 // Parameter count -1 because of Param0 == this
3569 sal_uInt32 nParamCount = pParams ? (pParams->Count() - 1) : 0;
3570 Sequence<Any> args;
3571
3572 Reference< XServiceConstructorDescription > xCtor = pUnoCtor->getServiceCtorDesc();
3573 Sequence< Reference< XParameter > > aParameterSeq = xCtor->getParameters();
3574 const Reference< XParameter >* pParameterSeq = aParameterSeq.getConstArray();
3575 sal_uInt32 nUnoParamCount = aParameterSeq.getLength();
3576
3577 // Default: Ignore not needed parameters
3578 bool bParameterError = false;
3579
3580 // Is the last parameter a rest parameter?
3581 bool bRestParameterMode = false;
3582 if( nUnoParamCount > 0 )
3583 {
3584 Reference< XParameter > xLastParam = pParameterSeq[ nUnoParamCount - 1 ];
3585 if( xLastParam.is() )
3586 {
3587 if( xLastParam->isRestParameter() )
3588 bRestParameterMode = true;
3589 }
3590 }
3591
3592 // Too many parameters with context as first parameter?
3593 sal_uInt32 nSbxParameterOffset = 1;
3594 sal_uInt32 nParameterOffsetByContext = 0;
3595 Reference < XComponentContext > xFirstParamContext;
3596 if( nParamCount > nUnoParamCount )
3597 {
3598 // Check if first parameter is a context and use it
3599 // then in createInstanceWithArgumentsAndContext
3600 Any aArg0 = sbxToUnoValue(pParams->Get(nSbxParameterOffset));
3601 if( (aArg0 >>= xFirstParamContext) && xFirstParamContext.is() )
3602 nParameterOffsetByContext = 1;
3603 }
3604
3605 sal_uInt32 nEffectiveParamCount = nParamCount - nParameterOffsetByContext;
3606 sal_uInt32 nAllocParamCount = nEffectiveParamCount;
3607 if( nEffectiveParamCount > nUnoParamCount )
3608 {
3609 if( !bRestParameterMode )
3610 {
3611 nEffectiveParamCount = nUnoParamCount;
3612 nAllocParamCount = nUnoParamCount;
3613 }
3614 }
3615 // Not enough parameters?
3616 else if( nUnoParamCount > nEffectiveParamCount )
3617 {
3618 // RestParameterMode only helps if one (the last) parameter is missing
3619 int nDiff = nUnoParamCount - nEffectiveParamCount;
3620 if( !bRestParameterMode || nDiff > 1 )
3621 {
3622 bParameterError = true;
3623 StarBASIC::Error( ERRCODE_BASIC_NOT_OPTIONAL );
3624 }
3625 }
3626
3627 if( !bParameterError )
3628 {
3629 bool bOutParams = false;
3630 if( nAllocParamCount > 0 )
3631 {
3632 args.realloc( nAllocParamCount );
3633 Any* pAnyArgs = args.getArray();
3634 for( sal_uInt32 i = 0 ; i < nEffectiveParamCount ; i++ )
3635 {
3636 sal_uInt32 iSbx = i + nSbxParameterOffset + nParameterOffsetByContext;
3637
3638 // bRestParameterMode allows nEffectiveParamCount > nUnoParamCount
3639 Reference< XParameter > xParam;
3640 if( i < nUnoParamCount )
3641 {
3642 xParam = pParameterSeq[i];
3643 if( !xParam.is() )
3644 continue;
3645
3646 Reference< XTypeDescription > xParamTypeDesc = xParam->getType();
3647 if( !xParamTypeDesc.is() )
3648 continue;
3649 css::uno::Type aType( xParamTypeDesc->getTypeClass(), xParamTypeDesc->getName() );
3650
3651 // sbx parameter needs offset 1
3652 pAnyArgs[i] = sbxToUnoValue(pParams->Get(iSbx), aType);
3653
3654 // Check for out parameter if not already done
3655 if( !bOutParams && xParam->isOut() )
3656 bOutParams = true;
3657 }
3658 else
3659 {
3660 pAnyArgs[i] = sbxToUnoValue(pParams->Get(iSbx));
3661 }
3662 }
3663 }
3664
3665 // "Call" ctor using createInstanceWithArgumentsAndContext
3666 Reference < XComponentContext > xContext(
3667 xFirstParamContext.is()
3668 ? xFirstParamContext
3669 : comphelper::getProcessComponentContext() );
3670 Reference< XMultiComponentFactory > xServiceMgr( xContext->getServiceManager() );
3671
3672 Any aRetAny;
3673 OUString aServiceName = GetName();
3674 Reference < XInterface > xRet;
3675 try
3676 {
3677 xRet = xServiceMgr->createInstanceWithArgumentsAndContext( aServiceName, args, xContext );
3678 }
3679 catch( const Exception& )
3680 {
3681 implHandleAnyException( ::cppu::getCaughtException() );
3682 }
3683 aRetAny <<= xRet;
3684 unoToSbxValue( pVar, aRetAny );
3685
3686 // Copy back out parameters?
3687 if( bOutParams )
3688 {
3689 const Any* pAnyArgs = args.getConstArray();
3690
3691 for( sal_uInt32 j = 0 ; j < nUnoParamCount ; j++ )
3692 {
3693 Reference< XParameter > xParam = pParameterSeq[j];
3694 if( !xParam.is() )
3695 continue;
3696
3697 if( xParam->isOut() )
3698 unoToSbxValue(pParams->Get(j + 1), pAnyArgs[j]);
3699 }
3700 }
3701 }
3702 }
3703 else
3704 SbxObject::Notify( rBC, rHint );
3705 }
3706
3707
3708 SbUnoServiceCtor::SbUnoServiceCtor( const OUString& aName_, Reference< XServiceConstructorDescription > const & xServiceCtorDesc )
3709 : SbxMethod( aName_, SbxOBJECT )
3710 , m_xServiceCtorDesc( xServiceCtorDesc )
3711 {
3712 }
3713
3714 SbUnoServiceCtor::~SbUnoServiceCtor()
3715 {
3716 }
3717
3718 SbxInfo* SbUnoServiceCtor::GetInfo()
3719 {
3720 return nullptr;
3721 }
3722
3723
3724 SbUnoSingleton* findUnoSingleton( const OUString& rName )
3725 {
3726 SbUnoSingleton* pSbUnoSingleton = nullptr;
3727
3728 const Reference< XHierarchicalNameAccess >& xTypeAccess = getTypeProvider_Impl();
3729 if( xTypeAccess->hasByHierarchicalName( rName ) )
3730 {
3731 Any aRet = xTypeAccess->getByHierarchicalName( rName );
3732 Reference< XTypeDescription > xTypeDesc;
3733 aRet >>= xTypeDesc;
3734
3735 if( xTypeDesc.is() )
3736 {
3737 TypeClass eTypeClass = xTypeDesc->getTypeClass();
3738 if( eTypeClass == TypeClass_SINGLETON )
3739 {
3740 Reference< XSingletonTypeDescription > xSingletonTypeDesc( xTypeDesc, UNO_QUERY );
3741 if( xSingletonTypeDesc.is() )
3742 pSbUnoSingleton = new SbUnoSingleton( rName );
3743 }
3744 }
3745 }
3746 return pSbUnoSingleton;
3747 }
3748
3749 SbUnoSingleton::SbUnoSingleton( const OUString& aName_ )
3750 : SbxObject( aName_ )
3751 {
3752 SbxVariableRef xGetMethodRef = new SbxMethod( u"get"_ustr, SbxOBJECT );
3753 QuickInsert( xGetMethodRef.get() );
3754 }
3755
3756 void SbUnoSingleton::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
3757 {
3758 const SbxHint* pHint = dynamic_cast<const SbxHint*>(&rHint);
3759 if( pHint )
3760 {
3761 SbxVariable* pVar = pHint->GetVar();
3762 SbxArray* pParams = pVar->GetParameters();
3763 sal_uInt32 nParamCount = pParams ? (pParams->Count() - 1) : 0;
3764 sal_uInt32 nAllowedParamCount = 1;
3765
3766 Reference < XComponentContext > xContextToUse;
3767 if( nParamCount > 0 )
3768 {
3769 // Check if first parameter is a context and use it then
3770 Reference < XComponentContext > xFirstParamContext;
3771 Any aArg1 = sbxToUnoValue(pParams->Get(1));
3772 if( (aArg1 >>= xFirstParamContext) && xFirstParamContext.is() )
3773 xContextToUse = std::move(xFirstParamContext);
3774 }
3775
3776 if( !xContextToUse.is() )
3777 {
3778 xContextToUse = comphelper::getProcessComponentContext();
3779 --nAllowedParamCount;
3780 }
3781
3782 if( nParamCount > nAllowedParamCount )
3783 {
3784 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
3785 return;
3786 }
3787
3788 Any aRetAny;
3789 if( xContextToUse.is() )
3790 {
3791 OUString aSingletonName = "/singletons/"
3792 + GetName();
3793 Reference < XInterface > xRet;
3794 xContextToUse->getValueByName( aSingletonName ) >>= xRet;
3795 aRetAny <<= xRet;
3796 }
3797 unoToSbxValue( pVar, aRetAny );
3798 }
3799 else
3800 {
3801 SbxObject::Notify( rBC, rHint );
3802 }
3803 }
3804
3805 namespace {
3806
3807 // Implementation of an EventAttacher-drawn AllListener, which
3808 // solely transmits several events to a general AllListener
3809 class BasicAllListener_Impl : public WeakImplHelper< XAllListener >
3810 {
3811 void firing_impl(const AllEventObject& Event, Any* pRet);
3812
3813 public:
3814 SbxObjectRef xSbxObj;
3815 OUString aPrefixName;
3816
3817 explicit BasicAllListener_Impl( OUString aPrefixName );
3818
3819 // Methods of XAllListener
3820 virtual void SAL_CALL firing(const AllEventObject& Event) override;
3821 virtual Any SAL_CALL approveFiring(const AllEventObject& Event) override;
3822
3823 // Methods of XEventListener
3824 virtual void SAL_CALL disposing(const EventObject& Source) override;
3825 };
3826
3827 }
3828
3829 BasicAllListener_Impl::BasicAllListener_Impl(OUString aPrefixName_)
3830 : aPrefixName(std::move( aPrefixName_ ))
3831 {
3832 }
3833
3834 void BasicAllListener_Impl::firing_impl( const AllEventObject& Event, Any* pRet )
3835 {
3836 SolarMutexGuard guard;
3837
3838 if( !xSbxObj.is() )
3839 return;
3840
3841 OUString aMethodName = aPrefixName + Event.MethodName;
3842
3843 SbxVariable * pP = xSbxObj.get();
3844 while( pP->GetParent() )
3845 {
3846 pP = pP->GetParent();
3847 StarBASIC * pLib = dynamic_cast<StarBASIC*>( pP );
3848 if( pLib )
3849 {
3850 // Create in a Basic Array
3851 SbxArrayRef xSbxArray = new SbxArray( SbxVARIANT );
3852 const Any * pArgs = Event.Arguments.getConstArray();
3853 sal_Int32 nCount = Event.Arguments.getLength();
3854 for( sal_Int32 i = 0; i < nCount; i++ )
3855 {
3856 // Convert elements
3857 SbxVariableRef xVar = new SbxVariable( SbxVARIANT );
3858 unoToSbxValue( xVar.get(), pArgs[i] );
3859 xSbxArray->Put(xVar.get(), i + 1);
3860 }
3861
3862 pLib->Call( aMethodName, xSbxArray.get() );
3863
3864 // get the return value from the Param-Array, if requested
3865 if( pRet )
3866 {
3867 SbxVariable* pVar = xSbxArray->Get(0);
3868 if( pVar )
3869 {
3870 // #95792 Avoid a second call
3871 SbxFlagBits nFlags = pVar->GetFlags();
3872 pVar->SetFlag( SbxFlagBits::NoBroadcast );
3873 *pRet = sbxToUnoValueImpl( pVar );
3874 pVar->SetFlags( nFlags );
3875 }
3876 }
3877 break;
3878 }
3879 }
3880 }
3881
3882
3883 // Methods of Listener
3884 void BasicAllListener_Impl::firing( const AllEventObject& Event )
3885 {
3886 firing_impl( Event, nullptr );
3887 }
3888
3889 Any BasicAllListener_Impl::approveFiring( const AllEventObject& Event )
3890 {
3891 Any aRetAny;
3892 firing_impl( Event, &aRetAny );
3893 return aRetAny;
3894 }
3895
3896
3897 // Methods of XEventListener
3898 void BasicAllListener_Impl ::disposing(const EventObject& )
3899 {
3900 SolarMutexGuard guard;
3901
3902 xSbxObj.clear();
3903 }
3904
3905
3906 // class InvocationToAllListenerMapper
3907 // helper class to map XInvocation to XAllListener (also in project eventattacher!)
3908
3909 namespace {
3910
3911 class InvocationToAllListenerMapper : public WeakImplHelper< XInvocation >
3912 {
3913 public:
3914 InvocationToAllListenerMapper( const Reference< XIdlClass >& ListenerType,
3915 const Reference< XAllListener >& AllListener, Any Helper );
3916
3917 // XInvocation
3918 virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection() override;
3919 virtual Any SAL_CALL invoke(const OUString& FunctionName, const Sequence< Any >& Params, Sequence< sal_Int16 >& OutParamIndex, Sequence< Any >& OutParam) override;
3920 virtual void SAL_CALL setValue(const OUString& PropertyName, const Any& Value) override;
3921 virtual Any SAL_CALL getValue(const OUString& PropertyName) override;
3922 virtual sal_Bool SAL_CALL hasMethod(const OUString& Name) override;
3923 virtual sal_Bool SAL_CALL hasProperty(const OUString& Name) override;
3924
3925 private:
3926 Reference< XAllListener > m_xAllListener;
3927 Reference< XIdlClass > m_xListenerType;
3928 Any m_Helper;
3929 };
3930
3931 }
3932
3933 // Function to replace AllListenerAdapterService::createAllListerAdapter
3934 static Reference< XInterface > createAllListenerAdapter
3935 (
3936 const Reference< XInvocationAdapterFactory2 >& xInvocationAdapterFactory,
3937 const Reference< XIdlClass >& xListenerType,
3938 const Reference< XAllListener >& xListener,
3939 const Any& Helper
3940 )
3941 {
3942 Reference< XInterface > xAdapter;
3943 if( xInvocationAdapterFactory.is() && xListenerType.is() && xListener.is() )
3944 {
3945 Reference< XInvocation > xInvocationToAllListenerMapper =
3946 new InvocationToAllListenerMapper(xListenerType, xListener, Helper);
3947 Type aListenerType( xListenerType->getTypeClass(), xListenerType->getName() );
3948 xAdapter = xInvocationAdapterFactory->createAdapter( xInvocationToAllListenerMapper, {aListenerType} );
3949 }
3950 return xAdapter;
3951 }
3952
3953
3954 // InvocationToAllListenerMapper
3955 InvocationToAllListenerMapper::InvocationToAllListenerMapper
3956 ( const Reference< XIdlClass >& ListenerType, const Reference< XAllListener >& AllListener, Any Helper )
3957 : m_xAllListener( AllListener )
3958 , m_xListenerType( ListenerType )
3959 , m_Helper(std::move( Helper ))
3960 {
3961 }
3962
3963
3964 Reference< XIntrospectionAccess > SAL_CALL InvocationToAllListenerMapper::getIntrospection()
3965 {
3966 return Reference< XIntrospectionAccess >();
3967 }
3968
3969
3970 Any SAL_CALL InvocationToAllListenerMapper::invoke(const OUString& FunctionName, const Sequence< Any >& Params,
3971 Sequence< sal_Int16 >&, Sequence< Any >&)
3972 {
3973 Any aRet;
3974
3975 // Check if to firing or approveFiring has to be called
3976 Reference< XIdlMethod > xMethod = m_xListenerType->getMethod( FunctionName );
3977 bool bApproveFiring = false;
3978 if( !xMethod.is() )
3979 return aRet;
3980 Reference< XIdlClass > xReturnType = xMethod->getReturnType();
3981 Sequence< Reference< XIdlClass > > aExceptionSeq = xMethod->getExceptionTypes();
3982 if( ( xReturnType.is() && xReturnType->getTypeClass() != TypeClass_VOID ) ||
3983 aExceptionSeq.hasElements() )
3984 {
3985 bApproveFiring = true;
3986 }
3987 else
3988 {
3989 Sequence< ParamInfo > aParamSeq = xMethod->getParameterInfos();
3990 sal_uInt32 nParamCount = aParamSeq.getLength();
3991 if( nParamCount > 1 )
3992 {
3993 const ParamInfo* pInfo = aParamSeq.getConstArray();
3994 for( sal_uInt32 i = 0 ; i < nParamCount ; i++ )
3995 {
3996 if( pInfo[ i ].aMode != ParamMode_IN )
3997 {
3998 bApproveFiring = true;
3999 break;
4000 }
4001 }
4002 }
4003 }
4004
4005 AllEventObject aAllEvent;
4006 aAllEvent.Source = getXWeak();
4007 aAllEvent.Helper = m_Helper;
4008 aAllEvent.ListenerType = Type(m_xListenerType->getTypeClass(), m_xListenerType->getName() );
4009 aAllEvent.MethodName = FunctionName;
4010 aAllEvent.Arguments = Params;
4011 if( bApproveFiring )
4012 aRet = m_xAllListener->approveFiring( aAllEvent );
4013 else
4014 m_xAllListener->firing( aAllEvent );
4015 return aRet;
4016 }
4017
4018
4019 void SAL_CALL InvocationToAllListenerMapper::setValue(const OUString&, const Any&)
4020 {}
4021
4022
4023 Any SAL_CALL InvocationToAllListenerMapper::getValue(const OUString&)
4024 {
4025 return Any();
4026 }
4027
4028
4029 sal_Bool SAL_CALL InvocationToAllListenerMapper::hasMethod(const OUString& Name)
4030 {
4031 Reference< XIdlMethod > xMethod = m_xListenerType->getMethod( Name );
4032 return xMethod.is();
4033 }
4034
4035
4036 sal_Bool SAL_CALL InvocationToAllListenerMapper::hasProperty(const OUString& Name)
4037 {
4038 Reference< XIdlField > xField = m_xListenerType->getField( Name );
4039 return xField.is();
4040 }
4041
4042
4043 // create Uno-Service
4044 // 1. Parameter == Prefix-Name of the macro
4045 // 2. Parameter == fully qualified name of the listener
4046 void SbRtl_CreateUnoListener(StarBASIC * pBasic, SbxArray & rPar, bool)
4047 {
4048 // We need 2 parameters
4049 if (rPar.Count() != 3)
4050 {
4051 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
4052 return;
4053 }
4054
4055 // get the name of the class of the struct
4056 OUString aPrefixName = rPar.Get(1)->GetOUString();
4057 OUString aListenerClassName = rPar.Get(2)->GetOUString();
4058
4059 // get the CoreReflection
4060 Reference< XIdlReflection > xCoreReflection = getCoreReflection_Impl();
4061 if( !xCoreReflection.is() )
4062 return;
4063
4064 // get the AllListenerAdapterService
4065 const Reference< XComponentContext >& xContext( comphelper::getProcessComponentContext() );
4066
4067 // search the class
4068 Reference< XIdlClass > xClass = xCoreReflection->forName( aListenerClassName );
4069 if( !xClass.is() )
4070 return;
4071
4072 // From 1999-11-30: get the InvocationAdapterFactory
4073 Reference< XInvocationAdapterFactory2 > xInvocationAdapterFactory =
4074 InvocationAdapterFactory::create( xContext );
4075
4076 rtl::Reference<BasicAllListener_Impl> xAllLst = new BasicAllListener_Impl( aPrefixName );
4077 Any aTmp;
4078 Reference< XInterface > xLst = createAllListenerAdapter( xInvocationAdapterFactory, xClass, xAllLst, aTmp );
4079 if( !xLst.is() )
4080 return;
4081
4082 OUString aClassName = xClass->getName();
4083 Type aClassType( xClass->getTypeClass(), aClassName );
4084 aTmp = xLst->queryInterface( aClassType );
4085 if( !aTmp.hasValue() )
4086 return;
4087
4088 SbUnoObject* pUnoObj = new SbUnoObject( aListenerClassName, aTmp );
4089 xAllLst->xSbxObj = pUnoObj;
4090 xAllLst->xSbxObj->SetParent( pBasic );
4091
4092 // #100326 Register listener object to set Parent NULL in Dtor
4093 SbxArrayRef xBasicUnoListeners = pBasic->getUnoListeners();
4094 xBasicUnoListeners->Insert(pUnoObj, xBasicUnoListeners->Count());
4095
4096 // return the object
4097 SbxVariableRef refVar = rPar.Get(0);
4098 refVar->PutObject( xAllLst->xSbxObj.get() );
4099 }
4100
4101
4102 // Represents the DefaultContext property of the ProcessServiceManager
4103 // in the Basic runtime system.
4104 void RTL_Impl_GetDefaultContext( SbxArray& rPar )
4105 {
4106 SbxVariableRef refVar = rPar.Get(0);
4107
4108 Any aContextAny( comphelper::getProcessComponentContext() );
4109
4110 SbUnoObjectRef xUnoObj = new SbUnoObject( u"DefaultContext"_ustr, aContextAny );
4111 refVar->PutObject( xUnoObj.get() );
4112 }
4113
4114
4115 // Creates a Basic wrapper object for a strongly typed Uno value
4116 // 1. parameter: Uno type as full qualified type name, e.g. "byte[]"
4117 void RTL_Impl_CreateUnoValue( SbxArray& rPar )
4118 {
4119 // 2 parameters needed
4120 if (rPar.Count() != 3)
4121 {
4122 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
4123 return;
4124 }
4125
4126 // get the name of the class of the struct
4127 OUString aTypeName = rPar.Get(1)->GetOUString();
4128 SbxVariable* pVal = rPar.Get(2);
4129
4130 if( aTypeName == "type" )
4131 {
4132 SbxDataType eBaseType = pVal->SbxValue::GetType();
4133 OUString aValTypeName;
4134 if( eBaseType == SbxSTRING )
4135 {
4136 aValTypeName = pVal->GetOUString();
4137 }
4138 else if( eBaseType == SbxOBJECT )
4139 {
4140 // XIdlClass?
4141 Reference< XIdlClass > xIdlClass;
4142
4143 SbxBaseRef pObj = pVal->GetObject();
4144 if( auto obj = dynamic_cast<SbUnoObject*>( pObj.get() ) )
4145 {
4146 Any aUnoAny = obj->getUnoAny();
4147 aUnoAny >>= xIdlClass;
4148 }
4149
4150 if( xIdlClass.is() )
4151 {
4152 aValTypeName = xIdlClass->getName();
4153 }
4154 }
4155 Type aType;
4156 bool bSuccess = implGetTypeByName( aValTypeName, aType );
4157 if( bSuccess )
4158 {
4159 SbxVariableRef refVar = rPar.Get(0);
4160 SbxObjectRef xUnoAnyObject = new SbUnoAnyObject(Any(aType));
4161 refVar->PutObject( xUnoAnyObject.get() );
4162 }
4163 return;
4164 }
4165
4166 // Check the type
4167 const Reference< XHierarchicalNameAccess >& xTypeAccess = getTypeProvider_Impl();
4168 Any aRet;
4169 try
4170 {
4171 aRet = xTypeAccess->getByHierarchicalName( aTypeName );
4172 }
4173 catch( const NoSuchElementException& e1 )
4174 {
4175 StarBASIC::Error( ERRCODE_BASIC_EXCEPTION,
4176 implGetExceptionMsg( e1, u"com.sun.star.container.NoSuchElementException" ) );
4177 return;
4178 }
4179 Reference< XTypeDescription > xTypeDesc;
4180 aRet >>= xTypeDesc;
4181 TypeClass eTypeClass = xTypeDesc->getTypeClass();
4182 Type aDestType( eTypeClass, aTypeName );
4183
4184
4185 // Preconvert value
4186 Any aVal = sbxToUnoValueImpl( pVal );
4187 Any aConvertedVal = convertAny( aVal, aDestType );
4188
4189 SbxVariableRef refVar = rPar.Get(0);
4190 SbxObjectRef xUnoAnyObject = new SbUnoAnyObject( aConvertedVal );
4191 refVar->PutObject( xUnoAnyObject.get() );
4192 }
4193
4194 namespace {
4195
4196 class ModuleInvocationProxy : public WeakImplHelper< XInvocation, XComponent >
4197 {
4198 std::mutex m_aMutex;
4199 OUString m_aPrefix;
4200 SbxObjectRef m_xScopeObj;
4201 bool m_bProxyIsClassModuleObject;
4202
4203 ::comphelper::OInterfaceContainerHelper4<XEventListener> m_aListeners;
4204
4205 public:
4206 ModuleInvocationProxy( std::u16string_view aPrefix, SbxObjectRef const & xScopeObj );
4207
4208 // XInvocation
4209 virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection() override;
4210 virtual void SAL_CALL setValue( const OUString& rProperty, const Any& rValue ) override;
4211 virtual Any SAL_CALL getValue( const OUString& rProperty ) override;
4212 virtual sal_Bool SAL_CALL hasMethod( const OUString& rName ) override;
4213 virtual sal_Bool SAL_CALL hasProperty( const OUString& rProp ) override;
4214
4215 virtual Any SAL_CALL invoke( const OUString& rFunction,
4216 const Sequence< Any >& rParams,
4217 Sequence< sal_Int16 >& rOutParamIndex,
4218 Sequence< Any >& rOutParam ) override;
4219
4220 // XComponent
4221 virtual void SAL_CALL dispose() override;
4222 virtual void SAL_CALL addEventListener( const Reference< XEventListener >& xListener ) override;
4223 virtual void SAL_CALL removeEventListener( const Reference< XEventListener >& aListener ) override;
4224 };
4225
4226 }
4227
4228 ModuleInvocationProxy::ModuleInvocationProxy( std::u16string_view aPrefix, SbxObjectRef const & xScopeObj )
4229 : m_aPrefix( OUString::Concat(aPrefix) + "_" )
4230 , m_xScopeObj( xScopeObj )
4231 {
4232 m_bProxyIsClassModuleObject = xScopeObj.is() && dynamic_cast<const SbClassModuleObject*>( xScopeObj.get() ) != nullptr;
4233 }
4234
4235 Reference< XIntrospectionAccess > SAL_CALL ModuleInvocationProxy::getIntrospection()
4236 {
4237 return Reference< XIntrospectionAccess >();
4238 }
4239
4240 void SAL_CALL ModuleInvocationProxy::setValue(const OUString& rProperty, const Any& rValue)
4241 {
4242 if( !m_bProxyIsClassModuleObject )
4243 throw UnknownPropertyException();
4244
4245 SolarMutexGuard guard;
4246
4247 OUString aPropertyFunctionName = "Property Set "
4248 + m_aPrefix
4249 + rProperty;
4250
4251 SbxVariable* p = m_xScopeObj->Find( aPropertyFunctionName, SbxClassType::Method );
4252 SbMethod* pMeth = dynamic_cast<SbMethod*>( p );
4253 if( pMeth == nullptr )
4254 {
4255 // TODO: Check vba behavior concerning missing function
4256 //StarBASIC::Error( ERRCODE_BASIC_NO_METHOD, aFunctionName );
4257 throw UnknownPropertyException(aPropertyFunctionName);
4258 }
4259
4260 // Setup parameter
4261 SbxArrayRef xArray = new SbxArray;
4262 SbxVariableRef xVar = new SbxVariable( SbxVARIANT );
4263 unoToSbxValue( xVar.get(), rValue );
4264 xArray->Put(xVar.get(), 1);
4265
4266 // Call property method
4267 SbxVariableRef xValue = new SbxVariable;
4268 pMeth->SetParameters( xArray.get() );
4269 pMeth->Call( xValue.get() );
4270 pMeth->SetParameters( nullptr );
4271
4272 // TODO: OutParameter?
4273
4274
4275 }
4276
4277 Any SAL_CALL ModuleInvocationProxy::getValue(const OUString& rProperty)
4278 {
4279 if( !m_bProxyIsClassModuleObject )
4280 {
4281 throw UnknownPropertyException();
4282 }
4283 SolarMutexGuard guard;
4284
4285 OUString aPropertyFunctionName = "Property Get "
4286 + m_aPrefix
4287 + rProperty;
4288
4289 SbxVariable* p = m_xScopeObj->Find( aPropertyFunctionName, SbxClassType::Method );
4290 SbMethod* pMeth = dynamic_cast<SbMethod*>( p );
4291 if( pMeth == nullptr )
4292 {
4293 // TODO: Check vba behavior concerning missing function
4294 //StarBASIC::Error( ERRCODE_BASIC_NO_METHOD, aFunctionName );
4295 throw UnknownPropertyException(aPropertyFunctionName);
4296 }
4297
4298 // Call method
4299 SbxVariableRef xValue = new SbxVariable;
4300 pMeth->Call( xValue.get() );
4301 Any aRet = sbxToUnoValue( xValue.get() );
4302 return aRet;
4303 }
4304
4305 sal_Bool SAL_CALL ModuleInvocationProxy::hasMethod( const OUString& )
4306 {
4307 return false;
4308 }
4309
4310 sal_Bool SAL_CALL ModuleInvocationProxy::hasProperty( const OUString& )
4311 {
4312 return false;
4313 }
4314
4315 Any SAL_CALL ModuleInvocationProxy::invoke( const OUString& rFunction,
4316 const Sequence< Any >& rParams,
4317 Sequence< sal_Int16 >&,
4318 Sequence< Any >& )
4319 {
4320 SolarMutexGuard guard;
4321
4322 Any aRet;
4323 SbxObjectRef xScopeObj = m_xScopeObj;
4324 if( !xScopeObj.is() )
4325 {
4326 return aRet;
4327 }
4328 OUString aFunctionName = m_aPrefix
4329 + rFunction;
4330
4331 bool bOldReschedule = false;
4332 SbiInstance* pInst = GetSbData()->pInst;
4333 if( pInst && pInst->IsCompatibility() )
4334 {
4335 bOldReschedule = pInst->IsReschedule();
4336 if ( bOldReschedule )
4337 pInst->EnableReschedule( false );
4338 }
4339
4340 SbxVariable* p = xScopeObj->Find( aFunctionName, SbxClassType::Method );
4341 SbMethod* pMeth = dynamic_cast<SbMethod*>( p );
4342 if( pMeth == nullptr )
4343 {
4344 // TODO: Check vba behavior concerning missing function
4345 //StarBASIC::Error( ERRCODE_BASIC_NO_METHOD, aFunctionName );
4346 return aRet;
4347 }
4348
4349 // Setup parameters
4350 SbxArrayRef xArray;
4351 sal_Int32 nParamCount = rParams.getLength();
4352 if( nParamCount )
4353 {
4354 xArray = new SbxArray;
4355 const Any *pArgs = rParams.getConstArray();
4356 for( sal_Int32 i = 0 ; i < nParamCount ; i++ )
4357 {
4358 SbxVariableRef xVar = new SbxVariable( SbxVARIANT );
4359 unoToSbxValue( xVar.get(), pArgs[i] );
4360 xArray->Put(xVar.get(), sal::static_int_cast<sal_uInt16>(i + 1));
4361 }
4362 }
4363
4364 // Call method
4365 SbxVariableRef xValue = new SbxVariable;
4366 if( xArray.is() )
4367 pMeth->SetParameters( xArray.get() );
4368 pMeth->Call( xValue.get() );
4369 aRet = sbxToUnoValue( xValue.get() );
4370 pMeth->SetParameters( nullptr );
4371
4372 if (bOldReschedule)
4373 pInst->EnableReschedule( bOldReschedule );
4374
4375 // TODO: OutParameter?
4376
4377 return aRet;
4378 }
4379
4380 void SAL_CALL ModuleInvocationProxy::dispose()
4381 {
4382 std::unique_lock aGuard( m_aMutex );
4383
4384 EventObject aEvent( static_cast<XComponent*>(this) );
4385 m_aListeners.disposeAndClear( aGuard, aEvent );
4386
4387 m_xScopeObj = nullptr;
4388 }
4389
4390 void SAL_CALL ModuleInvocationProxy::addEventListener( const Reference< XEventListener >& xListener )
4391 {
4392 std::unique_lock aGuard( m_aMutex );
4393 m_aListeners.addInterface( aGuard, xListener );
4394 }
4395
4396 void SAL_CALL ModuleInvocationProxy::removeEventListener( const Reference< XEventListener >& xListener )
4397 {
4398 std::unique_lock aGuard( m_aMutex );
4399 m_aListeners.removeInterface( aGuard, xListener );
4400 }
4401
4402
4403 Reference< XInterface > createComListener( const Any& aControlAny, const OUString& aVBAType,
4404 std::u16string_view aPrefix,
4405 const SbxObjectRef& xScopeObj )
4406 {
4407 Reference< XInterface > xRet;
4408
4409 const Reference< XComponentContext >& xContext(
4410 comphelper::getProcessComponentContext() );
4411 Reference< XMultiComponentFactory > xServiceMgr( xContext->getServiceManager() );
4412
4413 Reference< XInvocation > xProxy = new ModuleInvocationProxy( aPrefix, xScopeObj );
4414
4415 Sequence<Any> args{ aControlAny, Any(aVBAType), Any(xProxy) };
4416
4417 try
4418 {
4419 xRet = xServiceMgr->createInstanceWithArgumentsAndContext(
4420 u"com.sun.star.custom.UnoComListener"_ustr,
4421 args, xContext );
4422 }
4423 catch( const Exception& )
4424 {
4425 implHandleAnyException( ::cppu::getCaughtException() );
4426 }
4427
4428 return xRet;
4429 }
4430
4431 typedef std::vector< WeakReference< XComponent > > ComponentRefVector;
4432
4433 namespace {
4434
4435 struct StarBasicDisposeItem
4436 {
4437 StarBASIC* m_pBasic;
4438 SbxArrayRef m_pRegisteredVariables;
4439 ComponentRefVector m_vComImplementsObjects;
4440
4441 explicit StarBasicDisposeItem( StarBASIC* pBasic )
4442 : m_pBasic( pBasic )
4443 , m_pRegisteredVariables(new SbxArray())
4444 {
4445 }
4446 };
4447
4448 }
4449
4450 typedef std::vector< StarBasicDisposeItem* > DisposeItemVector;
4451
4452 static DisposeItemVector GaDisposeItemVector;
4453
4454 static DisposeItemVector::iterator lcl_findItemForBasic( StarBASIC const * pBasic )
4455 {
4456 return std::find_if(GaDisposeItemVector.begin(), GaDisposeItemVector.end(),
4457 [&pBasic](StarBasicDisposeItem* pItem) { return pItem->m_pBasic == pBasic; });
4458 }
4459
4460 static StarBasicDisposeItem* lcl_getOrCreateItemForBasic( StarBASIC* pBasic )
4461 {
4462 DisposeItemVector::iterator it = lcl_findItemForBasic( pBasic );
4463 StarBasicDisposeItem* pItem = (it != GaDisposeItemVector.end()) ? *it : nullptr;
4464 if( pItem == nullptr )
4465 {
4466 pItem = new StarBasicDisposeItem( pBasic );
4467 GaDisposeItemVector.push_back( pItem );
4468 }
4469 return pItem;
4470 }
4471
4472 void registerComponentToBeDisposedForBasic
4473 ( const Reference< XComponent >& xComponent, StarBASIC* pBasic )
4474 {
4475 StarBasicDisposeItem* pItem = lcl_getOrCreateItemForBasic( pBasic );
4476 pItem->m_vComImplementsObjects.emplace_back(xComponent );
4477 }
4478
4479 void registerComListenerVariableForBasic( SbxVariable* pVar, StarBASIC* pBasic )
4480 {
4481 StarBasicDisposeItem* pItem = lcl_getOrCreateItemForBasic( pBasic );
4482 SbxArray* pArray = pItem->m_pRegisteredVariables.get();
4483 pArray->Put(pVar, pArray->Count());
4484 }
4485
4486 void disposeComVariablesForBasic( StarBASIC const * pBasic )
4487 {
4488 DisposeItemVector::iterator it = lcl_findItemForBasic( pBasic );
4489 if( it == GaDisposeItemVector.end() )
4490 return;
4491
4492 StarBasicDisposeItem* pItem = *it;
4493
4494 SbxArray* pArray = pItem->m_pRegisteredVariables.get();
4495 sal_uInt32 nCount = pArray->Count();
4496 for( sal_uInt32 i = 0 ; i < nCount ; ++i )
4497 {
4498 SbxVariable* pVar = pArray->Get(i);
4499 pVar->ClearComListener();
4500 }
4501
4502 ComponentRefVector& rv = pItem->m_vComImplementsObjects;
4503 for (auto const& elem : rv)
4504 {
4505 Reference< XComponent > xComponent( elem );
4506 if (xComponent.is())
4507 xComponent->dispose();
4508 }
4509
4510 delete pItem;
4511 GaDisposeItemVector.erase( it );
4512 }
4513
4514
4515 // Handle module implements mechanism for OLE types
4516 bool SbModule::createCOMWrapperForIface( Any& o_rRetAny, SbClassModuleObject* pProxyClassModuleObject )
4517 {
4518 // For now: Take first interface that allows to instantiate COM wrapper
4519 // TODO: Check if support for multiple interfaces is needed
4520
4521 const Reference< XComponentContext >& xContext(
4522 comphelper::getProcessComponentContext() );
4523 Reference< XMultiComponentFactory > xServiceMgr( xContext->getServiceManager() );
4524 Reference< XSingleServiceFactory > xComImplementsFactory
4525 (
4526 xServiceMgr->createInstanceWithContext( u"com.sun.star.custom.ComImplementsFactory"_ustr, xContext ),
4527 UNO_QUERY
4528 );
4529 if( !xComImplementsFactory.is() )
4530 return false;
4531
4532 bool bSuccess = false;
4533
4534 SbxArray* pModIfaces = pClassData->mxIfaces.get();
4535 sal_uInt32 nCount = pModIfaces->Count();
4536 for( sal_uInt32 i = 0 ; i < nCount ; ++i )
4537 {
4538 SbxVariable* pVar = pModIfaces->Get(i);
4539 const OUString& aIfaceName = pVar->GetName();
4540
4541 if( !aIfaceName.isEmpty() )
4542 {
4543 OUString aPureIfaceName = aIfaceName;
4544 sal_Int32 indexLastDot = aIfaceName.lastIndexOf('.');
4545 if ( indexLastDot > -1 )
4546 {
4547 aPureIfaceName = aIfaceName.copy( indexLastDot + 1 );
4548 }
4549 Reference< XInvocation > xProxy = new ModuleInvocationProxy( aPureIfaceName, pProxyClassModuleObject );
4550
4551 Sequence<Any> args{ Any(aIfaceName), Any(xProxy) };
4552
4553 Reference< XInterface > xRet;
4554 try
4555 {
4556 xRet = xComImplementsFactory->createInstanceWithArguments( args );
4557 bSuccess = true;
4558 }
4559 catch( const Exception& )
4560 {
4561 implHandleAnyException( ::cppu::getCaughtException() );
4562 }
4563
4564 if( bSuccess )
4565 {
4566 Reference< XComponent > xComponent( xProxy, UNO_QUERY );
4567 if( xComponent.is() )
4568 {
4569 StarBASIC* pParentBasic = nullptr;
4570 SbxObject* pCurObject = this;
4571 do
4572 {
4573 SbxObject* pObjParent = pCurObject->GetParent();
4574 pParentBasic = dynamic_cast<StarBASIC*>( pObjParent );
4575 pCurObject = pObjParent;
4576 }
4577 while( pParentBasic == nullptr && pCurObject != nullptr );
4578
4579 assert( pParentBasic != nullptr );
4580 registerComponentToBeDisposedForBasic( xComponent, pParentBasic );
4581 }
4582
4583 o_rRetAny <<= xRet;
4584 break;
4585 }
4586 }
4587 }
4588
4589 return bSuccess;
4590 }
4591
4592
4593 // Due to an incorrect behavior IE returns an object instead of a string
4594 // in some scenarios. Calling toString at the object may correct this.
4595 // Helper function used in sbxvalue.cxx
4596 bool handleToStringForCOMObjects( SbxObject* pObj, SbxValue* pVal )
4597 {
4598 bool bSuccess = false;
4599
4600 if( auto pUnoObj = dynamic_cast<SbUnoObject*>( pObj) )
4601 {
4602 // Only for native COM objects
4603 if( pUnoObj->isNativeCOMObject() )
4604 {
4605 SbxVariableRef pMeth = pObj->Find( u"toString"_ustr, SbxClassType::Method );
4606 if ( pMeth.is() )
4607 {
4608 SbxValues aRes;
4609 pMeth->Get( aRes );
4610 pVal->Put( aRes );
4611 bSuccess = true;
4612 }
4613 }
4614 }
4615 return bSuccess;
4616 }
4617
4618 Any StructRefInfo::getValue()
4619 {
4620 Any aRet;
4621 uno_any_destruct(
4622 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
4623 typelib_TypeDescription * pTD = nullptr;
4624 maType.getDescription(&pTD);
4625 uno_any_construct(
4626 &aRet, getInst(), pTD,
4627 reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
4628 typelib_typedescription_release(pTD);
4629 return aRet;
4630 }
4631
4632 void StructRefInfo::setValue( const Any& rValue )
4633 {
4634 bool bSuccess = uno_type_assignData( getInst(),
4635 maType.getTypeLibType(),
4636 const_cast<void*>(rValue.getValue()),
4637 rValue.getValueTypeRef(),
4638 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
4639 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
4640 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
4641 OSL_ENSURE(bSuccess,
4642 "StructRefInfo::setValue: ooops... the value could not be assigned!");
4643 }
4644
4645 OUString StructRefInfo::getTypeName() const
4646 {
4647 return maType.getTypeName();
4648 }
4649
4650 void* StructRefInfo::getInst()
4651 {
4652 return const_cast<char *>(static_cast<char const *>(maAny.getValue()) + mnPos);
4653 }
4654
4655 TypeClass StructRefInfo::getTypeClass() const
4656 {
4657 return maType.getTypeClass();
4658 }
4659
4660 SbUnoStructRefObject::SbUnoStructRefObject( const OUString& aName_, StructRefInfo aMemberInfo ) : SbxObject( aName_ ), maMemberInfo(std::move( aMemberInfo )), mbMemberCacheInit( false )
4661 {
4662 SetClassName( maMemberInfo.getTypeName() );
4663 }
4664
4665 SbUnoStructRefObject::~SbUnoStructRefObject()
4666 {
4667 }
4668
4669 void SbUnoStructRefObject::initMemberCache()
4670 {
4671 if ( mbMemberCacheInit )
4672 return;
4673 typelib_TypeDescription * pTD = nullptr;
4674 maMemberInfo.getType().getDescription(&pTD);
4675 for ( typelib_CompoundTypeDescription * pCompTypeDescr = reinterpret_cast<typelib_CompoundTypeDescription *>(pTD);
4676 pCompTypeDescr;
4677 pCompTypeDescr = pCompTypeDescr->pBaseTypeDescription )
4678 {
4679 typelib_TypeDescriptionReference ** ppTypeRefs = pCompTypeDescr->ppTypeRefs;
4680 rtl_uString ** ppNames = pCompTypeDescr->ppMemberNames;
4681 sal_Int32 * pMemberOffsets = pCompTypeDescr->pMemberOffsets;
4682 for ( sal_Int32 nPos = pCompTypeDescr->nMembers; nPos--; )
4683 {
4684 OUString aName( ppNames[nPos] );
4685 maFields[ aName ] = std::make_unique<StructRefInfo>( maMemberInfo.getRootAnyRef(), ppTypeRefs[nPos], maMemberInfo.getPos() + pMemberOffsets[nPos] );
4686 }
4687 }
4688 typelib_typedescription_release(pTD);
4689 mbMemberCacheInit = true;
4690 }
4691
4692 SbxVariable* SbUnoStructRefObject::Find( const OUString& rName, SbxClassType t )
4693 {
4694 SbxVariable* pRes = SbxObject::Find( rName, t );
4695 if ( !pRes )
4696 {
4697 if ( !mbMemberCacheInit )
4698 initMemberCache();
4699 StructFieldInfo::iterator it = maFields.find( rName );
4700 if ( it != maFields.end() )
4701 {
4702 SbxDataType eSbxType;
4703 eSbxType = unoToSbxType( it->second->getTypeClass() );
4704 SbxDataType eRealSbxType = eSbxType;
4705 Property aProp;
4706 aProp.Name = rName;
4707 aProp.Type = css::uno::Type( it->second->getTypeClass(), it->second->getTypeName() );
4708 const bool bIsStruct = aProp.Type.getTypeClass() == css::uno::TypeClass_STRUCT;
4709 SbUnoProperty* pProp = new SbUnoProperty( rName, eSbxType, eRealSbxType, std::move(aProp), 0, false, bIsStruct );
4710 SbxVariableRef xVarRef = pProp;
4711 QuickInsert( xVarRef.get() );
4712 pRes = xVarRef.get();
4713 }
4714 }
4715
4716 if( !pRes )
4717 {
4718 if( rName.equalsIgnoreAsciiCase(ID_DBG_SUPPORTEDINTERFACES) ||
4719 rName.equalsIgnoreAsciiCase(ID_DBG_PROPERTIES) ||
4720 rName.equalsIgnoreAsciiCase(ID_DBG_METHODS) )
4721 {
4722 // Create
4723 implCreateDbgProperties();
4724
4725 // Now they have to be found regular
4726 pRes = SbxObject::Find( rName, SbxClassType::DontCare );
4727 }
4728 }
4729
4730 return pRes;
4731 }
4732
4733 // help method to create the dbg_-Properties
4734 void SbUnoStructRefObject::implCreateDbgProperties()
4735 {
4736 Property aProp;
4737
4738 // Id == -1: display the implemented interfaces corresponding the ClassProvider
4739 SbxVariableRef xVarRef = new SbUnoProperty( ID_DBG_SUPPORTEDINTERFACES, SbxSTRING, SbxSTRING, aProp, -1, false, false );
4740 QuickInsert( xVarRef.get() );
4741
4742 // Id == -2: output the properties
4743 xVarRef = new SbUnoProperty( ID_DBG_PROPERTIES, SbxSTRING, SbxSTRING, aProp, -2, false, false );
4744 QuickInsert( xVarRef.get() );
4745
4746 // Id == -3: output the Methods
4747 xVarRef = new SbUnoProperty( ID_DBG_METHODS, SbxSTRING, SbxSTRING, std::move(aProp), -3, false, false );
4748 QuickInsert( xVarRef.get() );
4749 }
4750
4751 void SbUnoStructRefObject::implCreateAll()
4752 {
4753 // throw away all existing methods and properties
4754 pMethods = new SbxArray;
4755 pProps = new SbxArray;
4756
4757 if (!mbMemberCacheInit)
4758 initMemberCache();
4759
4760 for (auto const& field : maFields)
4761 {
4762 const OUString& rName = field.first;
4763 SbxDataType eSbxType;
4764 eSbxType = unoToSbxType( field.second->getTypeClass() );
4765 SbxDataType eRealSbxType = eSbxType;
4766 Property aProp;
4767 aProp.Name = rName;
4768 aProp.Type = css::uno::Type( field.second->getTypeClass(), field.second->getTypeName() );
4769 const bool bIsStruct = aProp.Type.getTypeClass() == css::uno::TypeClass_STRUCT;
4770 SbUnoProperty* pProp = new SbUnoProperty( rName, eSbxType, eRealSbxType, std::move(aProp), 0, false, bIsStruct );
4771 SbxVariableRef xVarRef = pProp;
4772 QuickInsert( xVarRef.get() );
4773 }
4774
4775 // Create Dbg_-Properties
4776 implCreateDbgProperties();
4777 }
4778
4779 // output the value
4780 Any SbUnoStructRefObject::getUnoAny()
4781 {
4782 return maMemberInfo.getValue();
4783 }
4784
4785 OUString SbUnoStructRefObject::Impl_DumpProperties()
4786 {
4787 OUStringBuffer aRet("Properties of object " + getDbgObjectName() );
4788
4789 sal_uInt32 nPropCount = pProps->Count();
4790 sal_uInt32 nPropsPerLine = 1 + nPropCount / 30;
4791 for( sal_uInt32 i = 0; i < nPropCount; i++ )
4792 {
4793 SbxVariable* pVar = pProps->Get(i);
4794 if( pVar )
4795 {
4796 OUStringBuffer aPropStr;
4797 if( (i % nPropsPerLine) == 0 )
4798 {
4799 aPropStr.append( "\n" );
4800 }
4801 // output the type and name
4802 // Is it in Uno a sequence?
4803 SbxDataType eType = pVar->GetFullType();
4804
4805 const OUString& aName( pVar->GetName() );
4806 StructFieldInfo::iterator it = maFields.find( aName );
4807
4808 if ( it != maFields.end() )
4809 {
4810 const StructRefInfo& rPropInfo = *it->second;
4811
4812 if( eType == SbxOBJECT )
4813 {
4814 if( rPropInfo.getTypeClass() == TypeClass_SEQUENCE )
4815 {
4816 eType = SbxDataType( SbxOBJECT | SbxARRAY );
4817 }
4818 }
4819 }
4820 aPropStr.append( Dbg_SbxDataType2String( eType )
4821 + " " + pVar->GetName() );
4822
4823 if( i == nPropCount - 1 )
4824 {
4825 aPropStr.append( "\n" );
4826 }
4827 else
4828 {
4829 aPropStr.append( "; " );
4830 }
4831 aRet.append( aPropStr );
4832 }
4833 }
4834 return aRet.makeStringAndClear();
4835 }
4836
4837 void SbUnoStructRefObject::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
4838 {
4839 if ( !mbMemberCacheInit )
4840 initMemberCache();
4841 const SbxHint* pHint = dynamic_cast<const SbxHint*>(&rHint);
4842 if( !pHint )
4843 return;
4844
4845 SbxVariable* pVar = pHint->GetVar();
4846 SbUnoProperty* pProp = dynamic_cast<SbUnoProperty*>( pVar );
4847 if( pProp )
4848 {
4849 StructFieldInfo::iterator it = maFields.find( pProp->GetName() );
4850 // handle get/set of members of struct
4851 if( pHint->GetId() == SfxHintId::BasicDataWanted )
4852 {
4853 // Test-Properties
4854 sal_Int32 nId = pProp->nId;
4855 if( nId < 0 )
4856 {
4857 // Id == -1: Display implemented interfaces according the ClassProvider
4858 if( nId == -1 ) // Property ID_DBG_SUPPORTEDINTERFACES"
4859 {
4860 OUString aRet = OUString::Concat( ID_DBG_SUPPORTEDINTERFACES )
4861 + " not available.\n(TypeClass is not TypeClass_INTERFACE)\n";
4862
4863 pVar->PutString( aRet );
4864 }
4865 // Id == -2: output properties
4866 else if( nId == -2 ) // Property ID_DBG_PROPERTIES
4867 {
4868 // by now all properties must be established
4869 implCreateAll();
4870 OUString aRetStr = Impl_DumpProperties();
4871 pVar->PutString( aRetStr );
4872 }
4873 // Id == -3: output the methods
4874 else if( nId == -3 ) // Property ID_DBG_METHODS
4875 {
4876 // by now all properties must be established
4877 implCreateAll();
4878 OUString aRet = "Methods of object "
4879 + getDbgObjectName()
4880 + "\nNo methods found\n";
4881 pVar->PutString( aRet );
4882 }
4883 return;
4884 }
4885
4886 if ( it != maFields.end() )
4887 {
4888 Any aRetAny = it->second->getValue();
4889 unoToSbxValue( pVar, aRetAny );
4890 }
4891 else
4892 StarBASIC::Error( ERRCODE_BASIC_PROPERTY_NOT_FOUND );
4893 }
4894 else if( pHint->GetId() == SfxHintId::BasicDataChanged )
4895 {
4896 if ( it != maFields.end() )
4897 {
4898 // take over the value from Uno to Sbx
4899 Any aAnyValue = sbxToUnoValue( pVar, pProp->aUnoProp.Type, &pProp->aUnoProp );
4900 it->second->setValue( aAnyValue );
4901 }
4902 else
4903 StarBASIC::Error( ERRCODE_BASIC_PROPERTY_NOT_FOUND );
4904 }
4905 }
4906 else
4907 SbxObject::Notify( rBC, rHint );
4908 }
4909
4910 StructRefInfo SbUnoStructRefObject::getStructMember( const OUString& rMemberName )
4911 {
4912 if (!mbMemberCacheInit)
4913 {
4914 initMemberCache();
4915 }
4916 StructFieldInfo::iterator it = maFields.find( rMemberName );
4917
4918 css::uno::Type aFoundType;
4919 sal_Int32 nFoundPos = -1;
4920
4921 if ( it != maFields.end() )
4922 {
4923 aFoundType = it->second->getType();
4924 nFoundPos = it->second->getPos();
4925 }
4926 StructRefInfo aRet( maMemberInfo.getRootAnyRef(), aFoundType, nFoundPos );
4927 return aRet;
4928 }
4929
4930 OUString SbUnoStructRefObject::getDbgObjectName() const
4931 {
4932 OUString aName = GetClassName();
4933 if( aName.isEmpty() )
4934 {
4935 aName += "Unknown";
4936 }
4937 OUStringBuffer aRet;
4938 if( aName.getLength() > 20 )
4939 {
4940 aRet.append( "\n" );
4941 }
4942 aRet.append( "\"" + aName + "\":" );
4943 return aRet.makeStringAndClear();
4944 }
4945
4946 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */