1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "basscript.hxx"
22 #include <vcl/svapp.hxx>
23 #include <basic/sbx.hxx>
24 #include <basic/sbmod.hxx>
25 #include <basic/sbmeth.hxx>
26 #include <basic/sbuno.hxx>
27 #include <basic/basmgr.hxx>
28 #include <com/sun/star/script/provider/ScriptFrameworkErrorException.hpp>
29 #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
30 #include <comphelper/propertycontainer.hxx>
31 #include <com/sun/star/beans/PropertyAttribute.hpp>
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::script
;
39 using namespace ::com::sun::star::document
;
40 using namespace ::com::sun::star::beans
;
43 static void ChangeTypeKeepingValue(SbxVariable
& var
, SbxDataType to
)
47 bool bSetFlag
= var
.IsSet(SbxFlagBits::Fixed
);
48 var
.ResetFlag(SbxFlagBits::Fixed
);
51 var
.SetFlag(SbxFlagBits::Fixed
);
57 #define BASSCRIPT_PROPERTY_ID_CALLER 1
58 constexpr OUString BASSCRIPT_PROPERTY_CALLER
= u
"Caller"_ustr
;
60 #define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
62 typedef ::std::map
< sal_Int16
, Any
> OutParamMap
;
68 BasicScriptImpl::BasicScriptImpl( OUString funcName
, SbMethodRef xMethod
)
70 m_xMethod(std::move( xMethod
))
71 ,m_funcName(std::move( funcName
))
72 ,m_documentBasicManager( nullptr )
73 ,m_xDocumentScriptContext()
75 registerProperty( BASSCRIPT_PROPERTY_CALLER
, BASSCRIPT_PROPERTY_ID_CALLER
, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller
, cppu::UnoType
<decltype(m_caller
)>::get() );
79 BasicScriptImpl::BasicScriptImpl( OUString funcName
, SbMethodRef xMethod
,
80 BasicManager
& documentBasicManager
, const Reference
< XScriptInvocationContext
>& documentScriptContext
) :
81 m_xMethod(std::move( xMethod
))
82 ,m_funcName(std::move( funcName
))
83 ,m_documentBasicManager( &documentBasicManager
)
84 ,m_xDocumentScriptContext( documentScriptContext
)
86 StartListening( *m_documentBasicManager
);
87 registerProperty( BASSCRIPT_PROPERTY_CALLER
, BASSCRIPT_PROPERTY_ID_CALLER
, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller
, cppu::UnoType
<decltype(m_caller
)>::get() );
91 BasicScriptImpl::~BasicScriptImpl()
95 if ( m_documentBasicManager
)
96 EndListening( *m_documentBasicManager
);
102 void BasicScriptImpl::Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
)
104 if ( &rBC
!= m_documentBasicManager
)
106 OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
110 if ( rHint
.GetId() == SfxHintId::Dying
)
112 m_documentBasicManager
= nullptr;
113 EndListening( rBC
); // prevent multiple notifications
121 IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl
, BasicScriptImpl_BASE
, comphelper::OPropertyContainer2
)
127 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl
, BasicScriptImpl_BASE
, comphelper::OPropertyContainer2
)
130 // OPropertySetHelper
133 ::cppu::IPropertyArrayHelper
& BasicScriptImpl::getInfoHelper( )
135 return *getArrayHelper();
139 // OPropertyArrayUsageHelper
142 ::cppu::IPropertyArrayHelper
* BasicScriptImpl::createArrayHelper( ) const
144 Sequence
< Property
> aProps
;
145 describeProperties( aProps
);
146 return new ::cppu::OPropertyArrayHelper( aProps
);
153 Reference
< XPropertySetInfo
> BasicScriptImpl::getPropertySetInfo( )
155 Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
163 Any
BasicScriptImpl::invoke( const Sequence
< Any
>& aParams
, Sequence
< sal_Int16
>& aOutParamIndex
, Sequence
< Any
>& aOutParam
)
165 // TODO: throw CannotConvertException
166 // TODO: check length of aOutParamIndex, aOutParam
168 SolarMutexGuard aGuard
;
172 if ( m_xMethod
.is() )
175 SbModule
* pModule
= static_cast< SbModule
* >( m_xMethod
->GetParent() );
176 if ( pModule
&& !pModule
->IsCompiled() )
179 // check number of parameters
180 sal_Int32 nParamsCount
= aParams
.getLength();
181 SbxInfo
* pInfo
= m_xMethod
->GetInfo();
184 sal_Int32 nSbxOptional
= 0;
186 for ( const SbxParamInfo
* pParamInfo
= pInfo
->GetParam( n
); pParamInfo
; pParamInfo
= pInfo
->GetParam( ++n
) )
188 if ( pParamInfo
->nFlags
& SbxFlagBits::Optional
)
193 sal_Int32 nSbxCount
= n
- 1;
194 if ( nParamsCount
< nSbxCount
- nSbxOptional
)
196 throw provider::ScriptFrameworkErrorException(
197 u
"wrong number of parameters!"_ustr
,
198 Reference
< XInterface
>(),
201 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT
);
206 SbxArrayRef xSbxParams
;
207 if ( nParamsCount
> 0 )
209 xSbxParams
= new SbxArray
;
210 for ( sal_Int32 i
= 0; i
< nParamsCount
; ++i
)
212 SbxVariableRef xSbxVar
= new SbxVariable( SbxVARIANT
);
213 unoToSbxValue(xSbxVar
.get(), aParams
[i
]);
214 xSbxParams
->Put(xSbxVar
.get(), static_cast<sal_uInt32
>(i
) + 1);
218 if (auto* p
= pInfo
->GetParam(static_cast<sal_uInt16
>(i
) + 1))
220 SbxDataType t
= static_cast<SbxDataType
>(p
->eType
& 0x0FFF);
221 // tdf#133889 Revert the downcasting performed in sbxToUnoValueImpl
222 // to allow passing by reference.
223 SbxDataType a
= xSbxVar
->GetType();
224 if (t
== SbxSINGLE
&& (a
== SbxINTEGER
|| a
== SbxLONG
))
226 sal_Int32 val
= xSbxVar
->GetLong();
227 if (val
>= -16777216 && val
<= 16777215)
228 ChangeTypeKeepingValue(*xSbxVar
, t
);
230 else if (t
== SbxDOUBLE
&& (a
== SbxINTEGER
|| a
== SbxLONG
))
231 ChangeTypeKeepingValue(*xSbxVar
, t
);
232 else if (t
== SbxLONG
&& a
== SbxINTEGER
)
233 ChangeTypeKeepingValue(*xSbxVar
, t
);
234 else if (t
== SbxULONG
&& a
== SbxUSHORT
)
235 ChangeTypeKeepingValue(*xSbxVar
, t
);
236 // Enable passing by ref
238 xSbxVar
->SetFlag(SbxFlagBits::Fixed
);
243 if ( xSbxParams
.is() )
244 m_xMethod
->SetParameters( xSbxParams
.get() );
247 SbxVariableRef xReturn
= new SbxVariable
;
248 ErrCode nErr
= ERRCODE_NONE
;
250 // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
251 Any aOldThisComponent
;
252 if ( m_documentBasicManager
&& m_xDocumentScriptContext
.is() )
253 m_documentBasicManager
->SetGlobalUNOConstant( u
"ThisComponent"_ustr
, Any( m_xDocumentScriptContext
), &aOldThisComponent
);
255 if ( m_caller
.hasElements() && m_caller
[ 0 ].hasValue() )
257 SbxVariableRef xCallerVar
= new SbxVariable( SbxVARIANT
);
258 unoToSbxValue( xCallerVar
.get(), m_caller
[ 0 ] );
259 nErr
= m_xMethod
->Call( xReturn
.get(), xCallerVar
.get() );
262 nErr
= m_xMethod
->Call( xReturn
.get() );
264 if ( m_documentBasicManager
&& m_xDocumentScriptContext
.is() )
265 m_documentBasicManager
->SetGlobalUNOConstant( u
"ThisComponent"_ustr
, aOldThisComponent
);
267 if ( nErr
!= ERRCODE_NONE
)
269 // TODO: throw InvocationTargetException ?
272 // get output parameters
273 if ( xSbxParams
.is() )
275 SbxInfo
* pInfo_
= m_xMethod
->GetInfo();
278 OutParamMap aOutParamMap
;
279 for (sal_uInt32 n
= 1, nCount
= xSbxParams
->Count(); n
< nCount
; ++n
)
281 assert(nCount
<= std::numeric_limits
<sal_uInt16
>::max());
282 const SbxParamInfo
* pParamInfo
= pInfo_
->GetParam( sal::static_int_cast
<sal_uInt16
>(n
) );
283 if ( pParamInfo
&& ( pParamInfo
->eType
& SbxBYREF
) != 0 )
285 SbxVariable
* pVar
= xSbxParams
->Get(n
);
288 SbxVariableRef xVar
= pVar
;
289 aOutParamMap
.emplace( n
- 1, sbxToUnoValue( xVar
.get() ) );
293 sal_Int32 nOutParamCount
= aOutParamMap
.size();
294 aOutParamIndex
.realloc( nOutParamCount
);
295 aOutParam
.realloc( nOutParamCount
);
296 sal_Int16
* pOutParamIndex
= aOutParamIndex
.getArray();
297 Any
* pOutParam
= aOutParam
.getArray();
298 for ( const auto& rEntry
: aOutParamMap
)
300 *pOutParamIndex
= rEntry
.first
;
302 *pOutParam
= rEntry
.second
;
309 aReturn
= sbxToUnoValue( xReturn
.get() );
312 m_xMethod
->SetParameters( nullptr );
319 } // namespace basprov
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */