LCOV - code coverage report
Current view: top level - scripting/source/basprov - basscript.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 100 0.0 %
Date: 2014-04-14 Functions: 0 14 0.0 %
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           0 :     BasicScriptImpl::BasicScriptImpl( const OUString& funcName, SbMethodRef xMethod,
      76             :         BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
      77           0 :         ,OPropertyContainer( GetBroadcastHelper() )
      78             :         ,m_xMethod( xMethod )
      79             :         ,m_funcName( funcName )
      80             :         ,m_documentBasicManager( &documentBasicManager )
      81           0 :         ,m_xDocumentScriptContext( documentScriptContext )
      82             :     {
      83           0 :         StartListening( *m_documentBasicManager );
      84           0 :         registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, ::getCppuType( &m_caller ) );
      85           0 :     }
      86             : 
      87             : 
      88           0 :     BasicScriptImpl::~BasicScriptImpl()
      89             :     {
      90           0 :         if ( m_documentBasicManager )
      91           0 :             EndListening( *m_documentBasicManager );
      92           0 :     }
      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           0 :     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           0 :     ::cppu::IPropertyArrayHelper& BasicScriptImpl::getInfoHelper(  )
     130             :     {
     131           0 :         return *getArrayHelper();
     132             :     }
     133             : 
     134             : 
     135             :     // OPropertyArrayUsageHelper
     136             : 
     137             : 
     138           0 :     ::cppu::IPropertyArrayHelper* BasicScriptImpl::createArrayHelper(  ) const
     139             :     {
     140           0 :         Sequence< Property > aProps;
     141           0 :         describeProperties( aProps );
     142           0 :         return new ::cppu::OPropertyArrayHelper( aProps );
     143             :     }
     144             : 
     145             : 
     146             :     // XPropertySet
     147             : 
     148             : 
     149           0 :     Reference< XPropertySetInfo > BasicScriptImpl::getPropertySetInfo(  ) throw (RuntimeException, std::exception)
     150             :     {
     151           0 :         Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
     152           0 :         return xInfo;
     153             :     }
     154             : 
     155             : 
     156             :     // XScript
     157             : 
     158             : 
     159           0 :     Any BasicScriptImpl::invoke( const Sequence< Any >& aParams, Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
     160             :         throw ( provider::ScriptFrameworkErrorException, reflection::InvocationTargetException, uno::RuntimeException, std::exception)
     161             :     {
     162             :         // TODO: throw CannotConvertException
     163             :         // TODO: check length of aOutParamIndex, aOutParam
     164             : 
     165           0 :         SolarMutexGuard aGuard;
     166             : 
     167           0 :         Any aReturn;
     168             : 
     169           0 :         if ( m_xMethod )
     170             :         {
     171             :             // check if compiled
     172           0 :             SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() );
     173           0 :             if ( pModule && !pModule->IsCompiled() )
     174           0 :                 pModule->Compile();
     175             : 
     176             :             // check number of parameters
     177           0 :             sal_Int32 nParamsCount = aParams.getLength();
     178           0 :             SbxInfo* pInfo = m_xMethod->GetInfo();
     179           0 :             if ( pInfo )
     180             :             {
     181           0 :                 sal_Int32 nSbxOptional = 0;
     182           0 :                 sal_uInt16 n = 1;
     183           0 :                 for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) )
     184             :                 {
     185           0 :                     if ( ( pParamInfo->nFlags & SBX_OPTIONAL ) != 0 )
     186           0 :                         ++nSbxOptional;
     187             :                     else
     188           0 :                         nSbxOptional = 0;
     189             :                 }
     190           0 :                 sal_Int32 nSbxCount = n - 1;
     191           0 :                 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           0 :             SbxArrayRef xSbxParams;
     205           0 :             if ( nParamsCount > 0 )
     206             :             {
     207           0 :                 xSbxParams = new SbxArray;
     208           0 :                 const Any* pParams = aParams.getConstArray();
     209           0 :                 for ( sal_Int32 i = 0; i < nParamsCount; ++i )
     210             :                 {
     211           0 :                     SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
     212           0 :                     unoToSbxValue( static_cast< SbxVariable* >( xSbxVar ), pParams[i] );
     213           0 :                     xSbxParams->Put( xSbxVar, static_cast< sal_uInt16 >( i ) + 1 );
     214             : 
     215             :                     // Enable passing by ref
     216           0 :                     if ( xSbxVar->GetType() != SbxVARIANT )
     217           0 :                         xSbxVar->SetFlag( SBX_FIXED );
     218           0 :                  }
     219             :             }
     220           0 :             if ( xSbxParams.Is() )
     221           0 :                 m_xMethod->SetParameters( xSbxParams );
     222             : 
     223             :             // call method
     224           0 :             SbxVariableRef xReturn = new SbxVariable;
     225           0 :             ErrCode nErr = SbxERR_OK;
     226             : 
     227             :             // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
     228           0 :             Any aOldThisComponent;
     229           0 :             if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
     230           0 :                 aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) );
     231             : 
     232           0 :             if ( m_caller.getLength() && m_caller[ 0 ].hasValue()  )
     233             :             {
     234           0 :                 SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
     235           0 :                 unoToSbxValue( static_cast< SbxVariable* >( xCallerVar ), m_caller[ 0 ] );
     236           0 :                 nErr = m_xMethod->Call( xReturn, xCallerVar );
     237             :             }
     238             :             else
     239           0 :                 nErr = m_xMethod->Call( xReturn );
     240             : 
     241           0 :             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           0 :             if ( xSbxParams.Is() )
     251             :             {
     252           0 :                 SbxInfo* pInfo_ = m_xMethod->GetInfo();
     253           0 :                 if ( pInfo_ )
     254             :                 {
     255           0 :                     OutParamMap aOutParamMap;
     256           0 :                     for ( sal_uInt16 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n )
     257             :                     {
     258           0 :                         const SbxParamInfo* pParamInfo = pInfo_->GetParam( n );
     259           0 :                         if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 )
     260             :                         {
     261           0 :                             SbxVariable* pVar = xSbxParams->Get( n );
     262           0 :                             if ( pVar )
     263             :                             {
     264           0 :                                 SbxVariableRef xVar = pVar;
     265           0 :                                 aOutParamMap.insert( OutParamMap::value_type( n - 1, sbxToUnoValue( xVar ) ) );
     266             :                             }
     267             :                         }
     268             :                     }
     269           0 :                     sal_Int32 nOutParamCount = aOutParamMap.size();
     270           0 :                     aOutParamIndex.realloc( nOutParamCount );
     271           0 :                     aOutParam.realloc( nOutParamCount );
     272           0 :                     sal_Int16* pOutParamIndex = aOutParamIndex.getArray();
     273           0 :                     Any* pOutParam = aOutParam.getArray();
     274           0 :                     for ( OutParamMap::iterator aIt = aOutParamMap.begin(); aIt != aOutParamMap.end(); ++aIt, ++pOutParamIndex, ++pOutParam )
     275             :                     {
     276           0 :                         *pOutParamIndex = aIt->first;
     277           0 :                         *pOutParam = aIt->second;
     278           0 :                     }
     279             :                 }
     280             :             }
     281             : 
     282             :             // get return value
     283           0 :             aReturn = sbxToUnoValue( xReturn );
     284             : 
     285             :             // reset parameters
     286           0 :             m_xMethod->SetParameters( NULL );
     287             :         }
     288             : 
     289           0 :         return aReturn;
     290             :     }
     291             : 
     292             : 
     293             : 
     294             : 
     295             : }   // namespace basprov
     296             : 
     297             : 
     298             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10