Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "basscript.hxx"
21 : : #include <osl/mutex.hxx>
22 : : #include <vcl/svapp.hxx>
23 : : #include <basic/sbx.hxx>
24 : : #include <basic/sbstar.hxx>
25 : : #include <basic/sbmod.hxx>
26 : : #include <basic/sbmeth.hxx>
27 : : #include <basic/basmgr.hxx>
28 : : #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
29 : : #include "bcholder.hxx"
30 : : #include <comphelper/proparrhlp.hxx>
31 : : #include <comphelper/propertycontainer.hxx>
32 : : #include <com/sun/star/beans/PropertyAttribute.hpp>
33 : : #include <map>
34 : :
35 : :
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;
42 : :
43 : : extern ::com::sun::star::uno::Any sbxToUnoValue( SbxVariable* pVar );
44 : : extern void unoToSbxValue( SbxVariable* pVar, const ::com::sun::star::uno::Any& aValue );
45 : :
46 : :
47 : : //.........................................................................
48 : : namespace basprov
49 : : {
50 : : //.........................................................................
51 : : #define BASSCRIPT_PROPERTY_ID_CALLER 1
52 : : #define BASSCRIPT_PROPERTY_CALLER ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Caller" ) )
53 : :
54 : : #define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
55 : :
56 : : typedef ::std::map< sal_Int16, Any, ::std::less< sal_Int16 > > OutParamMap;
57 : :
58 : : // =============================================================================
59 : : // BasicScriptImpl
60 : : // =============================================================================
61 : :
62 : : // -----------------------------------------------------------------------------
63 : :
64 : 0 : BasicScriptImpl::BasicScriptImpl( const ::rtl::OUString& funcName, SbMethodRef xMethod )
65 : : : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
66 : 0 : ,OPropertyContainer( GetBroadcastHelper() )
67 : : ,m_xMethod( xMethod )
68 : : ,m_funcName( funcName )
69 : : ,m_documentBasicManager( NULL )
70 [ # # ][ # # ]: 0 : ,m_xDocumentScriptContext()
[ # # # # ]
[ # # ][ # # ]
71 : : {
72 [ # # ][ # # ]: 0 : registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, ::getCppuType( &m_caller ) );
[ # # ]
73 : 0 : }
74 : :
75 : : // -----------------------------------------------------------------------------
76 : :
77 : 16 : BasicScriptImpl::BasicScriptImpl( const ::rtl::OUString& funcName, SbMethodRef xMethod,
78 : : BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
79 : 16 : ,OPropertyContainer( GetBroadcastHelper() )
80 : : ,m_xMethod( xMethod )
81 : : ,m_funcName( funcName )
82 : : ,m_documentBasicManager( &documentBasicManager )
83 [ + - ][ + - ]: 32 : ,m_xDocumentScriptContext( documentScriptContext )
[ + - + - ]
[ + - ][ + - ]
84 : : {
85 [ + - ]: 16 : StartListening( *m_documentBasicManager );
86 [ + - ][ + - ]: 16 : registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, ::getCppuType( &m_caller ) );
[ + - ]
87 : 16 : }
88 : :
89 : : // -----------------------------------------------------------------------------
90 [ + - ][ + - ]: 16 : BasicScriptImpl::~BasicScriptImpl()
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
91 : : {
92 [ + + ]: 16 : if ( m_documentBasicManager )
93 [ + - ]: 14 : EndListening( *m_documentBasicManager );
94 [ - + ]: 32 : }
95 : :
96 : : // -----------------------------------------------------------------------------
97 : : // SfxListener
98 : : // -----------------------------------------------------------------------------
99 : 2 : void BasicScriptImpl::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
100 : : {
101 [ - + ]: 2 : if ( &rBC != m_documentBasicManager )
102 : : {
103 : : OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
104 : : // not interested in
105 : 2 : return;
106 : : }
107 [ + - ][ + - ]: 2 : const SfxSimpleHint* pSimpleHint = PTR_CAST( SfxSimpleHint, &rHint );
108 [ + - ][ + - ]: 2 : if ( pSimpleHint && ( pSimpleHint->GetId() == SFX_HINT_DYING ) )
[ + - ]
109 : : {
110 : 2 : m_documentBasicManager = NULL;
111 : 2 : EndListening( rBC ); // prevent multiple notifications
112 : : }
113 : : }
114 : :
115 : : // -----------------------------------------------------------------------------
116 : : // XInterface
117 : : // -----------------------------------------------------------------------------
118 : :
119 [ + + ][ + - ]: 108 : IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )
120 : :
121 : : // -----------------------------------------------------------------------------
122 : : // XTypeProvider
123 : : // -----------------------------------------------------------------------------
124 : :
125 [ # # ][ # # ]: 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )
[ # # ]
126 : :
127 : : // -----------------------------------------------------------------------------
128 : : // OPropertySetHelper
129 : : // -----------------------------------------------------------------------------
130 : :
131 : 6 : ::cppu::IPropertyArrayHelper& BasicScriptImpl::getInfoHelper( )
132 : : {
133 : 6 : return *getArrayHelper();
134 : : }
135 : :
136 : : // -----------------------------------------------------------------------------
137 : : // OPropertyArrayUsageHelper
138 : : // -----------------------------------------------------------------------------
139 : :
140 : 2 : ::cppu::IPropertyArrayHelper* BasicScriptImpl::createArrayHelper( ) const
141 : : {
142 [ + - ]: 2 : Sequence< Property > aProps;
143 [ + - ]: 2 : describeProperties( aProps );
144 [ + - ][ + - ]: 2 : return new ::cppu::OPropertyArrayHelper( aProps );
145 : : }
146 : :
147 : : // -----------------------------------------------------------------------------
148 : : // XPropertySet
149 : : // -----------------------------------------------------------------------------
150 : :
151 : 0 : Reference< XPropertySetInfo > BasicScriptImpl::getPropertySetInfo( ) throw (RuntimeException)
152 : : {
153 : 0 : Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
154 : 0 : return xInfo;
155 : : }
156 : :
157 : : // -----------------------------------------------------------------------------
158 : : // XScript
159 : : // -----------------------------------------------------------------------------
160 : :
161 : 16 : Any BasicScriptImpl::invoke( const Sequence< Any >& aParams, Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
162 : : throw ( provider::ScriptFrameworkErrorException, reflection::InvocationTargetException, uno::RuntimeException)
163 : : {
164 : : // TODO: throw CannotConvertException
165 : : // TODO: check length of aOutParamIndex, aOutParam
166 : :
167 [ + - ]: 16 : SolarMutexGuard aGuard;
168 : :
169 : 16 : Any aReturn;
170 : :
171 [ + - ]: 16 : if ( m_xMethod )
172 : : {
173 : : // check if compiled
174 [ + - ]: 16 : SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() );
175 [ + - ][ + - ]: 16 : if ( pModule && !pModule->IsCompiled() )
[ + + ][ + + ]
176 [ + - ]: 8 : pModule->Compile();
177 : :
178 : : // check number of parameters
179 : 16 : sal_Int32 nParamsCount = aParams.getLength();
180 [ + - ]: 16 : SbxInfo* pInfo = m_xMethod->GetInfo();
181 [ + - ]: 16 : if ( pInfo )
182 : : {
183 : 16 : sal_Int32 nSbxOptional = 0;
184 : 16 : sal_uInt16 n = 1;
185 [ + - ][ # # ]: 16 : for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) )
[ - + ]
186 : : {
187 [ # # ]: 0 : if ( ( pParamInfo->nFlags & SBX_OPTIONAL ) != 0 )
188 : 0 : ++nSbxOptional;
189 : : else
190 : 0 : nSbxOptional = 0;
191 : : }
192 : 16 : sal_Int32 nSbxCount = n - 1;
193 [ - + ]: 16 : if ( nParamsCount < nSbxCount - nSbxOptional )
194 : : {
195 : : throw provider::ScriptFrameworkErrorException(
196 : : ::rtl::OUString(
197 : : RTL_CONSTASCII_USTRINGPARAM(
198 : : "wrong number of parameters!" ) ),
199 : : Reference< XInterface >(),
200 : : m_funcName,
201 : : ::rtl::OUString(
202 : : RTL_CONSTASCII_USTRINGPARAM( "Basic" ) ),
203 [ # # ][ # # ]: 0 : provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
[ # # ]
204 : : }
205 : : }
206 : :
207 : : // set parameters
208 : 16 : SbxArrayRef xSbxParams;
209 [ + + ]: 16 : if ( nParamsCount > 0 )
210 : : {
211 [ + - ][ + - ]: 4 : xSbxParams = new SbxArray;
[ + - ]
212 : 4 : const Any* pParams = aParams.getConstArray();
213 [ + + ]: 8 : for ( sal_Int32 i = 0; i < nParamsCount; ++i )
214 : : {
215 [ + - ][ + - ]: 4 : SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
216 [ + - ]: 4 : unoToSbxValue( static_cast< SbxVariable* >( xSbxVar ), pParams[i] );
217 [ + - ]: 4 : xSbxParams->Put( xSbxVar, static_cast< sal_uInt16 >( i ) + 1 );
218 : :
219 : : // Enable passing by ref
220 [ + - ][ + - ]: 4 : if ( xSbxVar->GetType() != SbxVARIANT )
221 : 4 : xSbxVar->SetFlag( SBX_FIXED );
222 [ + - ]: 4 : }
223 : : }
224 [ + + ]: 16 : if ( xSbxParams.Is() )
225 [ + - ]: 4 : m_xMethod->SetParameters( xSbxParams );
226 : :
227 : : // call method
228 [ + - ][ + - ]: 16 : SbxVariableRef xReturn = new SbxVariable;
229 : 16 : ErrCode nErr = SbxERR_OK;
230 : : {
231 : : // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
232 : 16 : Any aOldThisComponent;
233 [ - + ][ - + ]: 16 : if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
[ + - ]
234 [ # # ][ # # ]: 0 : aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) );
235 : :
236 [ + + ][ + - ]: 16 : if ( m_caller.getLength() && m_caller[ 0 ].hasValue() )
[ + - ][ + + ]
237 : : {
238 [ + - ][ + - ]: 2 : SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
239 [ + - ][ + - ]: 2 : unoToSbxValue( static_cast< SbxVariable* >( xCallerVar ), m_caller[ 0 ] );
240 [ + - ][ + - ]: 2 : nErr = m_xMethod->Call( xReturn, xCallerVar );
241 : : }
242 : : else
243 [ + - ]: 14 : nErr = m_xMethod->Call( xReturn );
244 [ + + ][ - + ]: 16 : if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
[ - + ]
245 [ # # ]: 16 : m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );
246 : : }
247 : : if ( nErr != SbxERR_OK )
248 : : {
249 : : // TODO: throw InvocationTargetException ?
250 : : }
251 : :
252 : : // get output parameters
253 [ + + ]: 16 : if ( xSbxParams.Is() )
254 : : {
255 [ + - ]: 4 : SbxInfo* pInfo_ = m_xMethod->GetInfo();
256 [ + - ]: 4 : if ( pInfo_ )
257 : : {
258 [ + - ]: 4 : OutParamMap aOutParamMap;
259 [ + - ][ + + ]: 8 : for ( sal_uInt16 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n )
260 : : {
261 [ + - ]: 4 : const SbxParamInfo* pParamInfo = pInfo_->GetParam( n );
262 [ - + ][ # # ]: 4 : if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 )
263 : : {
264 [ # # ]: 0 : SbxVariable* pVar = xSbxParams->Get( n );
265 [ # # ]: 0 : if ( pVar )
266 : : {
267 : 0 : SbxVariableRef xVar = pVar;
268 [ # # ][ # # ]: 0 : aOutParamMap.insert( OutParamMap::value_type( n - 1, sbxToUnoValue( xVar ) ) );
[ # # ]
269 : : }
270 : : }
271 : : }
272 : 4 : sal_Int32 nOutParamCount = aOutParamMap.size();
273 [ + - ]: 4 : aOutParamIndex.realloc( nOutParamCount );
274 [ + - ]: 4 : aOutParam.realloc( nOutParamCount );
275 [ + - ]: 4 : sal_Int16* pOutParamIndex = aOutParamIndex.getArray();
276 [ + - ]: 4 : Any* pOutParam = aOutParam.getArray();
277 [ - + ]: 4 : for ( OutParamMap::iterator aIt = aOutParamMap.begin(); aIt != aOutParamMap.end(); ++aIt, ++pOutParamIndex, ++pOutParam )
278 : : {
279 : 0 : *pOutParamIndex = aIt->first;
280 : 0 : *pOutParam = aIt->second;
281 : 4 : }
282 : : }
283 : : }
284 : :
285 : : // get return value
286 [ + - ]: 16 : aReturn = sbxToUnoValue( xReturn );
287 : :
288 : : // reset parameters
289 [ + - ][ + - ]: 16 : m_xMethod->SetParameters( NULL );
[ + - ]
290 : : }
291 : :
292 [ + - ]: 16 : return aReturn;
293 : : }
294 : :
295 : : // -----------------------------------------------------------------------------
296 : :
297 : : //.........................................................................
298 : : } // namespace basprov
299 : : //.........................................................................
300 : :
301 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|