LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/scripting/source/basprov - basscript.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 82 101 81.2 %
Date: 2013-07-09 Functions: 11 16 68.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10