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
;
47 #define BASSCRIPT_PROPERTY_ID_CALLER 1
48 constexpr OUStringLiteral BASSCRIPT_PROPERTY_CALLER
= u
"Caller";
50 #define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
52 typedef ::std::map
< sal_Int16
, Any
> OutParamMap
;
58 BasicScriptImpl::BasicScriptImpl( OUString funcName
, SbMethodRef xMethod
)
59 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex
)
60 ,OPropertyContainer( GetBroadcastHelper() )
61 ,m_xMethod(std::move( xMethod
))
62 ,m_funcName(std::move( funcName
))
63 ,m_documentBasicManager( nullptr )
64 ,m_xDocumentScriptContext()
66 registerProperty( BASSCRIPT_PROPERTY_CALLER
, BASSCRIPT_PROPERTY_ID_CALLER
, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller
, cppu::UnoType
<decltype(m_caller
)>::get() );
70 BasicScriptImpl::BasicScriptImpl( OUString funcName
, SbMethodRef xMethod
,
71 BasicManager
& documentBasicManager
, const Reference
< XScriptInvocationContext
>& documentScriptContext
) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex
)
72 ,OPropertyContainer( GetBroadcastHelper() )
73 ,m_xMethod(std::move( xMethod
))
74 ,m_funcName(std::move( funcName
))
75 ,m_documentBasicManager( &documentBasicManager
)
76 ,m_xDocumentScriptContext( documentScriptContext
)
78 StartListening( *m_documentBasicManager
);
79 registerProperty( BASSCRIPT_PROPERTY_CALLER
, BASSCRIPT_PROPERTY_ID_CALLER
, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller
, cppu::UnoType
<decltype(m_caller
)>::get() );
83 BasicScriptImpl::~BasicScriptImpl()
87 if ( m_documentBasicManager
)
88 EndListening( *m_documentBasicManager
);
94 void BasicScriptImpl::Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
)
96 if ( &rBC
!= m_documentBasicManager
)
98 OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
102 if ( rHint
.GetId() == SfxHintId::Dying
)
104 m_documentBasicManager
= nullptr;
105 EndListening( rBC
); // prevent multiple notifications
113 IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl
, BasicScriptImpl_BASE
, OPropertyContainer
)
119 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl
, BasicScriptImpl_BASE
, OPropertyContainer
)
122 // OPropertySetHelper
125 ::cppu::IPropertyArrayHelper
& BasicScriptImpl::getInfoHelper( )
127 return *getArrayHelper();
131 // OPropertyArrayUsageHelper
134 ::cppu::IPropertyArrayHelper
* BasicScriptImpl::createArrayHelper( ) const
136 Sequence
< Property
> aProps
;
137 describeProperties( aProps
);
138 return new ::cppu::OPropertyArrayHelper( aProps
);
145 Reference
< XPropertySetInfo
> BasicScriptImpl::getPropertySetInfo( )
147 Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
155 Any
BasicScriptImpl::invoke( const Sequence
< Any
>& aParams
, Sequence
< sal_Int16
>& aOutParamIndex
, Sequence
< Any
>& aOutParam
)
157 // TODO: throw CannotConvertException
158 // TODO: check length of aOutParamIndex, aOutParam
160 SolarMutexGuard aGuard
;
164 if ( m_xMethod
.is() )
167 SbModule
* pModule
= static_cast< SbModule
* >( m_xMethod
->GetParent() );
168 if ( pModule
&& !pModule
->IsCompiled() )
171 // check number of parameters
172 sal_Int32 nParamsCount
= aParams
.getLength();
173 SbxInfo
* pInfo
= m_xMethod
->GetInfo();
176 sal_Int32 nSbxOptional
= 0;
178 for ( const SbxParamInfo
* pParamInfo
= pInfo
->GetParam( n
); pParamInfo
; pParamInfo
= pInfo
->GetParam( ++n
) )
180 if ( pParamInfo
->nFlags
& SbxFlagBits::Optional
)
185 sal_Int32 nSbxCount
= n
- 1;
186 if ( nParamsCount
< nSbxCount
- nSbxOptional
)
188 throw provider::ScriptFrameworkErrorException(
189 "wrong number of parameters!",
190 Reference
< XInterface
>(),
193 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT
);
198 SbxArrayRef xSbxParams
;
199 if ( nParamsCount
> 0 )
201 xSbxParams
= new SbxArray
;
202 const Any
* pParams
= aParams
.getConstArray();
203 for ( sal_Int32 i
= 0; i
< nParamsCount
; ++i
)
205 SbxVariableRef xSbxVar
= new SbxVariable( SbxVARIANT
);
206 unoToSbxValue( xSbxVar
.get(), pParams
[i
] );
207 xSbxParams
->Put(xSbxVar
.get(), static_cast<sal_uInt32
>(i
) + 1);
211 if (auto* p
= pInfo
->GetParam(static_cast<sal_uInt16
>(i
) + 1))
213 SbxDataType t
= static_cast<SbxDataType
>(p
->eType
& 0x0FFF);
214 // tdf#133889 Revert the downcasting performed in sbxToUnoValueImpl
215 // to allow passing by reference.
216 SbxDataType a
= xSbxVar
->GetType();
217 if (t
== SbxSINGLE
&& (a
== SbxINTEGER
|| a
== SbxLONG
))
219 sal_Int32 val
= xSbxVar
->GetLong();
220 if (val
>= -16777216 && val
<= 16777215)
223 else if (t
== SbxDOUBLE
&& (a
== SbxINTEGER
|| a
== SbxLONG
))
225 else if (t
== SbxLONG
&& a
== SbxINTEGER
)
227 else if (t
== SbxULONG
&& a
== SbxUSHORT
)
229 // Enable passing by ref
231 xSbxVar
->SetFlag(SbxFlagBits::Fixed
);
236 if ( xSbxParams
.is() )
237 m_xMethod
->SetParameters( xSbxParams
.get() );
240 SbxVariableRef xReturn
= new SbxVariable
;
241 ErrCode nErr
= ERRCODE_NONE
;
243 // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
244 Any aOldThisComponent
;
245 if ( m_documentBasicManager
&& m_xDocumentScriptContext
.is() )
246 m_documentBasicManager
->SetGlobalUNOConstant( "ThisComponent", Any( m_xDocumentScriptContext
), &aOldThisComponent
);
248 if ( m_caller
.hasElements() && m_caller
[ 0 ].hasValue() )
250 SbxVariableRef xCallerVar
= new SbxVariable( SbxVARIANT
);
251 unoToSbxValue( xCallerVar
.get(), m_caller
[ 0 ] );
252 nErr
= m_xMethod
->Call( xReturn
.get(), xCallerVar
.get() );
255 nErr
= m_xMethod
->Call( xReturn
.get() );
257 if ( m_documentBasicManager
&& m_xDocumentScriptContext
.is() )
258 m_documentBasicManager
->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent
);
260 if ( nErr
!= ERRCODE_NONE
)
262 // TODO: throw InvocationTargetException ?
265 // get output parameters
266 if ( xSbxParams
.is() )
268 SbxInfo
* pInfo_
= m_xMethod
->GetInfo();
271 OutParamMap aOutParamMap
;
272 for (sal_uInt32 n
= 1, nCount
= xSbxParams
->Count(); n
< nCount
; ++n
)
274 assert(nCount
<= std::numeric_limits
<sal_uInt16
>::max());
275 const SbxParamInfo
* pParamInfo
= pInfo_
->GetParam( sal::static_int_cast
<sal_uInt16
>(n
) );
276 if ( pParamInfo
&& ( pParamInfo
->eType
& SbxBYREF
) != 0 )
278 SbxVariable
* pVar
= xSbxParams
->Get(n
);
281 SbxVariableRef xVar
= pVar
;
282 aOutParamMap
.emplace( n
- 1, sbxToUnoValue( xVar
.get() ) );
286 sal_Int32 nOutParamCount
= aOutParamMap
.size();
287 aOutParamIndex
.realloc( nOutParamCount
);
288 aOutParam
.realloc( nOutParamCount
);
289 sal_Int16
* pOutParamIndex
= aOutParamIndex
.getArray();
290 Any
* pOutParam
= aOutParam
.getArray();
291 for ( const auto& rEntry
: aOutParamMap
)
293 *pOutParamIndex
= rEntry
.first
;
295 *pOutParam
= rEntry
.second
;
302 aReturn
= sbxToUnoValue( xReturn
.get() );
305 m_xMethod
->SetParameters( nullptr );
312 } // namespace basprov
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */