update dev300-m58
[ooovba.git] / scripting / source / basprov / basscript.cxx
blobe1ea656bfc011c1297c20cc5bd46bf7247456256
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: basscript.cxx,v $
10 * $Revision: 1.16.6.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_scripting.hxx"
33 #include "basscript.hxx"
34 #include <vos/mutex.hxx>
35 #include <vcl/svapp.hxx>
36 #include <basic/sbx.hxx>
37 #include <basic/sbstar.hxx>
38 #include <basic/sbmod.hxx>
39 #include <basic/sbmeth.hxx>
40 #include <basic/basmgr.hxx>
41 #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
42 #include "bcholder.hxx"
43 #include <comphelper/proparrhlp.hxx>
44 #include <comphelper/propertycontainer.hxx>
45 #include <com/sun/star/beans/PropertyAttribute.hpp>
46 #include <map>
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::script;
53 using namespace ::com::sun::star::document;
54 using namespace ::com::sun::star::beans;
56 extern ::com::sun::star::uno::Any sbxToUnoValue( SbxVariable* pVar );
57 extern void unoToSbxValue( SbxVariable* pVar, const ::com::sun::star::uno::Any& aValue );
60 //.........................................................................
61 namespace basprov
63 //.........................................................................
64 #define BASSCRIPT_PROPERTY_ID_CALLER 1
65 #define BASSCRIPT_PROPERTY_CALLER ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Caller" ) )
67 #define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
69 typedef ::std::map< sal_Int16, Any, ::std::less< sal_Int16 > > OutParamMap;
71 // =============================================================================
72 // BasicScriptImpl
73 // =============================================================================
75 // -----------------------------------------------------------------------------
77 BasicScriptImpl::BasicScriptImpl( const ::rtl::OUString& funcName, SbMethodRef xMethod )
78 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
79 ,OPropertyContainer( GetBroadcastHelper() )
80 ,m_xMethod( xMethod )
81 ,m_funcName( funcName )
82 ,m_documentBasicManager( NULL )
83 ,m_xDocumentScriptContext()
85 registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, ::getCppuType( &m_caller ) );
88 // -----------------------------------------------------------------------------
90 BasicScriptImpl::BasicScriptImpl( const ::rtl::OUString& funcName, SbMethodRef xMethod,
91 BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
92 ,OPropertyContainer( GetBroadcastHelper() )
93 ,m_xMethod( xMethod )
94 ,m_funcName( funcName )
95 ,m_documentBasicManager( &documentBasicManager )
96 ,m_xDocumentScriptContext( documentScriptContext )
98 //
99 registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, ::getCppuType( &m_caller ) );
102 // -----------------------------------------------------------------------------
103 BasicScriptImpl::~BasicScriptImpl()
107 // -----------------------------------------------------------------------------
108 // XInterface
109 // -----------------------------------------------------------------------------
111 IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )
113 // -----------------------------------------------------------------------------
114 // XTypeProvider
115 // -----------------------------------------------------------------------------
117 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )
119 // -----------------------------------------------------------------------------
120 // OPropertySetHelper
121 // -----------------------------------------------------------------------------
123 ::cppu::IPropertyArrayHelper& BasicScriptImpl::getInfoHelper( )
125 return *getArrayHelper();
128 // -----------------------------------------------------------------------------
129 // OPropertyArrayUsageHelper
130 // -----------------------------------------------------------------------------
132 ::cppu::IPropertyArrayHelper* BasicScriptImpl::createArrayHelper( ) const
134 Sequence< Property > aProps;
135 describeProperties( aProps );
136 return new ::cppu::OPropertyArrayHelper( aProps );
139 // -----------------------------------------------------------------------------
140 // XPropertySet
141 // -----------------------------------------------------------------------------
143 Reference< XPropertySetInfo > BasicScriptImpl::getPropertySetInfo( ) throw (RuntimeException)
145 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
146 return xInfo;
149 // -----------------------------------------------------------------------------
150 // XScript
151 // -----------------------------------------------------------------------------
153 Any BasicScriptImpl::invoke( const Sequence< Any >& aParams, Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
154 throw ( provider::ScriptFrameworkErrorException, reflection::InvocationTargetException, uno::RuntimeException)
156 // TODO: throw CannotConvertException
157 // TODO: check length of aOutParamIndex, aOutParam
159 ::vos::OGuard aGuard( Application::GetSolarMutex() );
161 Any aReturn;
163 if ( m_xMethod )
165 // check if compiled
166 SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() );
167 if ( pModule && !pModule->IsCompiled() )
168 pModule->Compile();
170 // check number of parameters
171 sal_Int32 nParamsCount = aParams.getLength();
172 SbxInfo* pInfo = m_xMethod->GetInfo();
173 if ( pInfo )
175 sal_Int32 nSbxOptional = 0;
176 USHORT n = 1;
177 for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) )
179 if ( ( pParamInfo->nFlags & SBX_OPTIONAL ) != 0 )
180 ++nSbxOptional;
181 else
182 nSbxOptional = 0;
184 sal_Int32 nSbxCount = n - 1;
185 if ( nParamsCount < nSbxCount - nSbxOptional )
187 throw provider::ScriptFrameworkErrorException(
188 ::rtl::OUString(
189 RTL_CONSTASCII_USTRINGPARAM(
190 "wrong number of parameters!" ) ),
191 Reference< XInterface >(),
192 m_funcName,
193 ::rtl::OUString(
194 RTL_CONSTASCII_USTRINGPARAM( "Basic" ) ),
195 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
199 // set parameters
200 SbxArrayRef xSbxParams;
201 if ( nParamsCount > 0 )
203 xSbxParams = new SbxArray;
204 const Any* pParams = aParams.getConstArray();
205 for ( sal_Int32 i = 0; i < nParamsCount; ++i )
207 SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
208 unoToSbxValue( static_cast< SbxVariable* >( xSbxVar ), pParams[i] );
209 xSbxParams->Put( xSbxVar, static_cast< USHORT >( i ) + 1 );
211 // Enable passing by ref
212 if ( xSbxVar->GetType() != SbxVARIANT )
213 xSbxVar->SetFlag( SBX_FIXED );
216 if ( xSbxParams.Is() )
217 m_xMethod->SetParameters( xSbxParams );
219 // call method
220 SbxVariableRef xReturn = new SbxVariable;
221 ErrCode nErr = SbxERR_OK;
223 // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
224 Any aOldThisComponent;
225 if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
226 aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) );
228 if ( m_caller.getLength() && m_caller[ 0 ].hasValue() )
230 SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
231 unoToSbxValue( static_cast< SbxVariable* >( xCallerVar ), m_caller[ 0 ] );
232 nErr = m_xMethod->Call( xReturn, xCallerVar );
234 else
235 nErr = m_xMethod->Call( xReturn );
236 if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
237 m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );
239 if ( nErr != SbxERR_OK )
241 // TODO: throw InvocationTargetException ?
244 // get output parameters
245 if ( xSbxParams.Is() )
247 SbxInfo* pInfo_ = m_xMethod->GetInfo();
248 if ( pInfo_ )
250 OutParamMap aOutParamMap;
251 for ( USHORT 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 );
257 if ( pVar )
259 SbxVariableRef xVar = pVar;
260 aOutParamMap.insert( OutParamMap::value_type( n - 1, sbxToUnoValue( xVar ) ) );
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 ( OutParamMap::iterator aIt = aOutParamMap.begin(); aIt != aOutParamMap.end(); ++aIt, ++pOutParamIndex, ++pOutParam )
271 *pOutParamIndex = aIt->first;
272 *pOutParam = aIt->second;
277 // get return value
278 aReturn = sbxToUnoValue( xReturn );
280 // reset parameters
281 m_xMethod->SetParameters( NULL );
284 return aReturn;
287 // -----------------------------------------------------------------------------
289 //.........................................................................
290 } // namespace basprov
291 //.........................................................................