LCOV - code coverage report
Current view: top level - libreoffice/scripting/source/basprov - basscript.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 100 0.0 %
Date: 2012-12-27 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            ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "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 ::rtl::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 ::rtl::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)
     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)
     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             :                         ::rtl::OUString(
     195             :                             RTL_CONSTASCII_USTRINGPARAM(
     196             :                                 "wrong number of parameters!" ) ),
     197             :                          Reference< XInterface >(),
     198             :                          m_funcName,
     199             :                          ::rtl::OUString(
     200             :                              RTL_CONSTASCII_USTRINGPARAM( "Basic" ) ),
     201           0 :                         provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT  );
     202             :                 }
     203             :             }
     204             : 
     205             :             // set parameters
     206           0 :             SbxArrayRef xSbxParams;
     207           0 :             if ( nParamsCount > 0 )
     208             :             {
     209           0 :                 xSbxParams = new SbxArray;
     210           0 :                 const Any* pParams = aParams.getConstArray();
     211           0 :                 for ( sal_Int32 i = 0; i < nParamsCount; ++i )
     212             :                 {
     213           0 :                     SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
     214           0 :                     unoToSbxValue( static_cast< SbxVariable* >( xSbxVar ), pParams[i] );
     215           0 :                     xSbxParams->Put( xSbxVar, static_cast< sal_uInt16 >( i ) + 1 );
     216             : 
     217             :                     // Enable passing by ref
     218           0 :                     if ( xSbxVar->GetType() != SbxVARIANT )
     219           0 :                         xSbxVar->SetFlag( SBX_FIXED );
     220           0 :                  }
     221             :             }
     222           0 :             if ( xSbxParams.Is() )
     223           0 :                 m_xMethod->SetParameters( xSbxParams );
     224             : 
     225             :             // call method
     226           0 :             SbxVariableRef xReturn = new SbxVariable;
     227           0 :             ErrCode nErr = SbxERR_OK;
     228             :             {
     229             :                 // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
     230           0 :                 Any aOldThisComponent;
     231           0 :                 if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
     232           0 :                     aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) );
     233             : 
     234           0 :             if ( m_caller.getLength() && m_caller[ 0 ].hasValue()  )
     235             :             {
     236           0 :                 SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
     237           0 :                 unoToSbxValue( static_cast< SbxVariable* >( xCallerVar ), m_caller[ 0 ] );
     238           0 :                 nErr = m_xMethod->Call( xReturn, xCallerVar );
     239             :             }
     240             :             else
     241           0 :                 nErr = m_xMethod->Call( xReturn );
     242           0 :                 if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
     243           0 :                     m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );
     244             :             }
     245             :             if ( nErr != SbxERR_OK )
     246             :             {
     247             :                 // TODO: throw InvocationTargetException ?
     248             :             }
     249             : 
     250             :             // get output parameters
     251           0 :             if ( xSbxParams.Is() )
     252             :             {
     253           0 :                 SbxInfo* pInfo_ = m_xMethod->GetInfo();
     254           0 :                 if ( pInfo_ )
     255             :                 {
     256           0 :                     OutParamMap aOutParamMap;
     257           0 :                     for ( sal_uInt16 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n )
     258             :                     {
     259           0 :                         const SbxParamInfo* pParamInfo = pInfo_->GetParam( n );
     260           0 :                         if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 )
     261             :                         {
     262           0 :                             SbxVariable* pVar = xSbxParams->Get( n );
     263           0 :                             if ( pVar )
     264             :                             {
     265           0 :                                 SbxVariableRef xVar = pVar;
     266           0 :                                 aOutParamMap.insert( OutParamMap::value_type( n - 1, sbxToUnoValue( xVar ) ) );
     267             :                             }
     268             :                         }
     269             :                     }
     270           0 :                     sal_Int32 nOutParamCount = aOutParamMap.size();
     271           0 :                     aOutParamIndex.realloc( nOutParamCount );
     272           0 :                     aOutParam.realloc( nOutParamCount );
     273           0 :                     sal_Int16* pOutParamIndex = aOutParamIndex.getArray();
     274           0 :                     Any* pOutParam = aOutParam.getArray();
     275           0 :                     for ( OutParamMap::iterator aIt = aOutParamMap.begin(); aIt != aOutParamMap.end(); ++aIt, ++pOutParamIndex, ++pOutParam )
     276             :                     {
     277           0 :                         *pOutParamIndex = aIt->first;
     278           0 :                         *pOutParam = aIt->second;
     279           0 :                     }
     280             :                 }
     281             :             }
     282             : 
     283             :             // get return value
     284           0 :             aReturn = sbxToUnoValue( xReturn );
     285             : 
     286             :             // reset parameters
     287           0 :             m_xMethod->SetParameters( NULL );
     288             :         }
     289             : 
     290           0 :         return aReturn;
     291             :     }
     292             : 
     293             :     // -----------------------------------------------------------------------------
     294             : 
     295             : //.........................................................................
     296             : }   // namespace basprov
     297             : //.........................................................................
     298             : 
     299             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10