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"
21 #include <vcl/svapp.hxx>
22 #include <basic/sbx.hxx>
23 #include <basic/sbstar.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/proparrhlp.hxx>
32 #include <comphelper/propertycontainer.hxx>
33 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 using namespace ::com::sun::star
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::script
;
41 using namespace ::com::sun::star::document
;
42 using namespace ::com::sun::star::beans
;
48 #define BASSCRIPT_PROPERTY_ID_CALLER 1
49 #define BASSCRIPT_PROPERTY_CALLER "Caller"
51 #define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
53 typedef ::std::map
< sal_Int16
, Any
> OutParamMap
;
59 BasicScriptImpl::BasicScriptImpl( const OUString
& funcName
, SbMethodRef
const & xMethod
)
60 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex
)
61 ,OPropertyContainer( GetBroadcastHelper() )
63 ,m_funcName( funcName
)
64 ,m_documentBasicManager( nullptr )
65 ,m_xDocumentScriptContext()
67 registerProperty( BASSCRIPT_PROPERTY_CALLER
, BASSCRIPT_PROPERTY_ID_CALLER
, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller
, cppu::UnoType
<decltype(m_caller
)>::get() );
71 BasicScriptImpl::BasicScriptImpl( const OUString
& funcName
, SbMethodRef
const & xMethod
,
72 BasicManager
& documentBasicManager
, const Reference
< XScriptInvocationContext
>& documentScriptContext
) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex
)
73 ,OPropertyContainer( GetBroadcastHelper() )
75 ,m_funcName( funcName
)
76 ,m_documentBasicManager( &documentBasicManager
)
77 ,m_xDocumentScriptContext( documentScriptContext
)
79 StartListening( *m_documentBasicManager
);
80 registerProperty( BASSCRIPT_PROPERTY_CALLER
, BASSCRIPT_PROPERTY_ID_CALLER
, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller
, cppu::UnoType
<decltype(m_caller
)>::get() );
84 BasicScriptImpl::~BasicScriptImpl()
88 if ( m_documentBasicManager
)
89 EndListening( *m_documentBasicManager
);
95 void BasicScriptImpl::Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
)
97 if ( &rBC
!= m_documentBasicManager
)
99 OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
103 if ( rHint
.GetId() == SfxHintId::Dying
)
105 m_documentBasicManager
= nullptr;
106 EndListening( rBC
); // prevent multiple notifications
114 IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl
, BasicScriptImpl_BASE
, OPropertyContainer
)
120 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl
, BasicScriptImpl_BASE
, OPropertyContainer
)
123 // OPropertySetHelper
126 ::cppu::IPropertyArrayHelper
& BasicScriptImpl::getInfoHelper( )
128 return *getArrayHelper();
132 // OPropertyArrayUsageHelper
135 ::cppu::IPropertyArrayHelper
* BasicScriptImpl::createArrayHelper( ) const
137 Sequence
< Property
> aProps
;
138 describeProperties( aProps
);
139 return new ::cppu::OPropertyArrayHelper( aProps
);
146 Reference
< XPropertySetInfo
> BasicScriptImpl::getPropertySetInfo( )
148 Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
156 Any
BasicScriptImpl::invoke( const Sequence
< Any
>& aParams
, Sequence
< sal_Int16
>& aOutParamIndex
, Sequence
< Any
>& aOutParam
)
158 // TODO: throw CannotConvertException
159 // TODO: check length of aOutParamIndex, aOutParam
161 SolarMutexGuard aGuard
;
165 if ( m_xMethod
.is() )
168 SbModule
* pModule
= static_cast< SbModule
* >( m_xMethod
->GetParent() );
169 if ( pModule
&& !pModule
->IsCompiled() )
172 // check number of parameters
173 sal_Int32 nParamsCount
= aParams
.getLength();
174 SbxInfo
* pInfo
= m_xMethod
->GetInfo();
177 sal_Int32 nSbxOptional
= 0;
179 for ( const SbxParamInfo
* pParamInfo
= pInfo
->GetParam( n
); pParamInfo
; pParamInfo
= pInfo
->GetParam( ++n
) )
181 if ( pParamInfo
->nFlags
& SbxFlagBits::Optional
)
186 sal_Int32 nSbxCount
= n
- 1;
187 if ( nParamsCount
< nSbxCount
- nSbxOptional
)
189 throw provider::ScriptFrameworkErrorException(
190 "wrong number of parameters!",
191 Reference
< XInterface
>(),
194 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT
);
199 SbxArrayRef xSbxParams
;
200 if ( nParamsCount
> 0 )
202 xSbxParams
= new SbxArray
;
203 const Any
* pParams
= aParams
.getConstArray();
204 for ( sal_Int32 i
= 0; i
< nParamsCount
; ++i
)
206 SbxVariableRef xSbxVar
= new SbxVariable( SbxVARIANT
);
207 unoToSbxValue( xSbxVar
.get(), pParams
[i
] );
208 xSbxParams
->Put( xSbxVar
.get(), static_cast< sal_uInt16
>( i
) + 1 );
210 // Enable passing by ref
211 if ( xSbxVar
->GetType() != SbxVARIANT
)
212 xSbxVar
->SetFlag( SbxFlagBits::Fixed
);
215 if ( xSbxParams
.is() )
216 m_xMethod
->SetParameters( xSbxParams
.get() );
219 SbxVariableRef xReturn
= new SbxVariable
;
220 ErrCode nErr
= ERRCODE_NONE
;
222 // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
223 Any aOldThisComponent
;
224 if ( m_documentBasicManager
&& m_xDocumentScriptContext
.is() )
225 aOldThisComponent
= m_documentBasicManager
->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext
) );
227 if ( m_caller
.hasElements() && m_caller
[ 0 ].hasValue() )
229 SbxVariableRef xCallerVar
= new SbxVariable( SbxVARIANT
);
230 unoToSbxValue( xCallerVar
.get(), m_caller
[ 0 ] );
231 nErr
= m_xMethod
->Call( xReturn
.get(), xCallerVar
.get() );
234 nErr
= m_xMethod
->Call( xReturn
.get() );
236 if ( m_documentBasicManager
&& m_xDocumentScriptContext
.is() )
237 m_documentBasicManager
->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent
);
239 if ( nErr
!= ERRCODE_NONE
)
241 // TODO: throw InvocationTargetException ?
244 // get output parameters
245 if ( xSbxParams
.is() )
247 SbxInfo
* pInfo_
= m_xMethod
->GetInfo();
250 OutParamMap aOutParamMap
;
251 for ( sal_uInt16 n
= 1, nCount
= xSbxParams
->Count(); n
< nCount
; ++n
)
253 const SbxParamInfo
* pParamInfo
= pInfo_
->GetParam( n
);
254 if ( pParamInfo
&& ( pParamInfo
->eType
& SbxBYREF
) != 0 )
256 SbxVariable
* pVar
= xSbxParams
->Get( n
);
259 SbxVariableRef xVar
= pVar
;
260 aOutParamMap
.emplace( n
- 1, sbxToUnoValue( xVar
.get() ) );
264 sal_Int32 nOutParamCount
= aOutParamMap
.size();
265 aOutParamIndex
.realloc( nOutParamCount
);
266 aOutParam
.realloc( nOutParamCount
);
267 sal_Int16
* pOutParamIndex
= aOutParamIndex
.getArray();
268 Any
* pOutParam
= aOutParam
.getArray();
269 for ( const auto& rEntry
: aOutParamMap
)
271 *pOutParamIndex
= rEntry
.first
;
273 *pOutParam
= rEntry
.second
;
280 aReturn
= sbxToUnoValue( xReturn
.get() );
283 m_xMethod
->SetParameters( nullptr );
290 } // namespace basprov
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */