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 <bcholder.hxx>
31 #include <comphelper/propertycontainer.hxx>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
36 using namespace ::com::sun::star
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::script
;
40 using namespace ::com::sun::star::document
;
41 using namespace ::com::sun::star::beans
;
44 static void ChangeTypeKeepingValue(SbxVariable
& var
, SbxDataType to
)
48 bool bSetFlag
= var
.IsSet(SbxFlagBits::Fixed
);
49 var
.ResetFlag(SbxFlagBits::Fixed
);
52 var
.SetFlag(SbxFlagBits::Fixed
);
58 #define BASSCRIPT_PROPERTY_ID_CALLER 1
59 constexpr OUString BASSCRIPT_PROPERTY_CALLER
= u
"Caller"_ustr
;
61 #define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
63 typedef ::std::map
< sal_Int16
, Any
> OutParamMap
;
69 BasicScriptImpl::BasicScriptImpl( OUString funcName
, SbMethodRef xMethod
)
70 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex
)
71 ,OPropertyContainer( GetBroadcastHelper() )
72 ,m_xMethod(std::move( xMethod
))
73 ,m_funcName(std::move( funcName
))
74 ,m_documentBasicManager( nullptr )
75 ,m_xDocumentScriptContext()
77 registerProperty( BASSCRIPT_PROPERTY_CALLER
, BASSCRIPT_PROPERTY_ID_CALLER
, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller
, cppu::UnoType
<decltype(m_caller
)>::get() );
81 BasicScriptImpl::BasicScriptImpl( OUString funcName
, SbMethodRef xMethod
,
82 BasicManager
& documentBasicManager
, const Reference
< XScriptInvocationContext
>& documentScriptContext
) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex
)
83 ,OPropertyContainer( GetBroadcastHelper() )
84 ,m_xMethod(std::move( xMethod
))
85 ,m_funcName(std::move( funcName
))
86 ,m_documentBasicManager( &documentBasicManager
)
87 ,m_xDocumentScriptContext( documentScriptContext
)
89 StartListening( *m_documentBasicManager
);
90 registerProperty( BASSCRIPT_PROPERTY_CALLER
, BASSCRIPT_PROPERTY_ID_CALLER
, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller
, cppu::UnoType
<decltype(m_caller
)>::get() );
94 BasicScriptImpl::~BasicScriptImpl()
98 if ( m_documentBasicManager
)
99 EndListening( *m_documentBasicManager
);
105 void BasicScriptImpl::Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
)
107 if ( &rBC
!= m_documentBasicManager
)
109 OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
113 if ( rHint
.GetId() == SfxHintId::Dying
)
115 m_documentBasicManager
= nullptr;
116 EndListening( rBC
); // prevent multiple notifications
124 IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl
, BasicScriptImpl_BASE
, OPropertyContainer
)
130 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl
, BasicScriptImpl_BASE
, OPropertyContainer
)
133 // OPropertySetHelper
136 ::cppu::IPropertyArrayHelper
& BasicScriptImpl::getInfoHelper( )
138 return *getArrayHelper();
142 // OPropertyArrayUsageHelper
145 ::cppu::IPropertyArrayHelper
* BasicScriptImpl::createArrayHelper( ) const
147 Sequence
< Property
> aProps
;
148 describeProperties( aProps
);
149 return new ::cppu::OPropertyArrayHelper( aProps
);
156 Reference
< XPropertySetInfo
> BasicScriptImpl::getPropertySetInfo( )
158 Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
166 Any
BasicScriptImpl::invoke( const Sequence
< Any
>& aParams
, Sequence
< sal_Int16
>& aOutParamIndex
, Sequence
< Any
>& aOutParam
)
168 // TODO: throw CannotConvertException
169 // TODO: check length of aOutParamIndex, aOutParam
171 SolarMutexGuard aGuard
;
175 if ( m_xMethod
.is() )
178 SbModule
* pModule
= static_cast< SbModule
* >( m_xMethod
->GetParent() );
179 if ( pModule
&& !pModule
->IsCompiled() )
182 // check number of parameters
183 sal_Int32 nParamsCount
= aParams
.getLength();
184 SbxInfo
* pInfo
= m_xMethod
->GetInfo();
187 sal_Int32 nSbxOptional
= 0;
189 for ( const SbxParamInfo
* pParamInfo
= pInfo
->GetParam( n
); pParamInfo
; pParamInfo
= pInfo
->GetParam( ++n
) )
191 if ( pParamInfo
->nFlags
& SbxFlagBits::Optional
)
196 sal_Int32 nSbxCount
= n
- 1;
197 if ( nParamsCount
< nSbxCount
- nSbxOptional
)
199 throw provider::ScriptFrameworkErrorException(
200 "wrong number of parameters!",
201 Reference
< XInterface
>(),
204 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT
);
209 SbxArrayRef xSbxParams
;
210 if ( nParamsCount
> 0 )
212 xSbxParams
= new SbxArray
;
213 const Any
* pParams
= aParams
.getConstArray();
214 for ( sal_Int32 i
= 0; i
< nParamsCount
; ++i
)
216 SbxVariableRef xSbxVar
= new SbxVariable( SbxVARIANT
);
217 unoToSbxValue( xSbxVar
.get(), pParams
[i
] );
218 xSbxParams
->Put(xSbxVar
.get(), static_cast<sal_uInt32
>(i
) + 1);
222 if (auto* p
= pInfo
->GetParam(static_cast<sal_uInt16
>(i
) + 1))
224 SbxDataType t
= static_cast<SbxDataType
>(p
->eType
& 0x0FFF);
225 // tdf#133889 Revert the downcasting performed in sbxToUnoValueImpl
226 // to allow passing by reference.
227 SbxDataType a
= xSbxVar
->GetType();
228 if (t
== SbxSINGLE
&& (a
== SbxINTEGER
|| a
== SbxLONG
))
230 sal_Int32 val
= xSbxVar
->GetLong();
231 if (val
>= -16777216 && val
<= 16777215)
232 ChangeTypeKeepingValue(*xSbxVar
, t
);
234 else if (t
== SbxDOUBLE
&& (a
== SbxINTEGER
|| a
== SbxLONG
))
235 ChangeTypeKeepingValue(*xSbxVar
, t
);
236 else if (t
== SbxLONG
&& a
== SbxINTEGER
)
237 ChangeTypeKeepingValue(*xSbxVar
, t
);
238 else if (t
== SbxULONG
&& a
== SbxUSHORT
)
239 ChangeTypeKeepingValue(*xSbxVar
, t
);
240 // Enable passing by ref
242 xSbxVar
->SetFlag(SbxFlagBits::Fixed
);
247 if ( xSbxParams
.is() )
248 m_xMethod
->SetParameters( xSbxParams
.get() );
251 SbxVariableRef xReturn
= new SbxVariable
;
252 ErrCode nErr
= ERRCODE_NONE
;
254 // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
255 Any aOldThisComponent
;
256 if ( m_documentBasicManager
&& m_xDocumentScriptContext
.is() )
257 m_documentBasicManager
->SetGlobalUNOConstant( "ThisComponent", Any( m_xDocumentScriptContext
), &aOldThisComponent
);
259 if ( m_caller
.hasElements() && m_caller
[ 0 ].hasValue() )
261 SbxVariableRef xCallerVar
= new SbxVariable( SbxVARIANT
);
262 unoToSbxValue( xCallerVar
.get(), m_caller
[ 0 ] );
263 nErr
= m_xMethod
->Call( xReturn
.get(), xCallerVar
.get() );
266 nErr
= m_xMethod
->Call( xReturn
.get() );
268 if ( m_documentBasicManager
&& m_xDocumentScriptContext
.is() )
269 m_documentBasicManager
->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent
);
271 if ( nErr
!= ERRCODE_NONE
)
273 // TODO: throw InvocationTargetException ?
276 // get output parameters
277 if ( xSbxParams
.is() )
279 SbxInfo
* pInfo_
= m_xMethod
->GetInfo();
282 OutParamMap aOutParamMap
;
283 for (sal_uInt32 n
= 1, nCount
= xSbxParams
->Count(); n
< nCount
; ++n
)
285 assert(nCount
<= std::numeric_limits
<sal_uInt16
>::max());
286 const SbxParamInfo
* pParamInfo
= pInfo_
->GetParam( sal::static_int_cast
<sal_uInt16
>(n
) );
287 if ( pParamInfo
&& ( pParamInfo
->eType
& SbxBYREF
) != 0 )
289 SbxVariable
* pVar
= xSbxParams
->Get(n
);
292 SbxVariableRef xVar
= pVar
;
293 aOutParamMap
.emplace( n
- 1, sbxToUnoValue( xVar
.get() ) );
297 sal_Int32 nOutParamCount
= aOutParamMap
.size();
298 aOutParamIndex
.realloc( nOutParamCount
);
299 aOutParam
.realloc( nOutParamCount
);
300 sal_Int16
* pOutParamIndex
= aOutParamIndex
.getArray();
301 Any
* pOutParam
= aOutParam
.getArray();
302 for ( const auto& rEntry
: aOutParamMap
)
304 *pOutParamIndex
= rEntry
.first
;
306 *pOutParam
= rEntry
.second
;
313 aReturn
= sbxToUnoValue( xReturn
.get() );
316 m_xMethod
->SetParameters( nullptr );
323 } // namespace basprov
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */