Files
libreoffice/cppuhelper/source/exc_thrower.cxx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

266 lines
7.1 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 14:29:57 +00:00
#include <rtl/instance.hxx>
#include <osl/diagnose.h>
#include <uno/dispatcher.hxx>
#include <uno/lbnames.h>
#include <uno/mapping.hxx>
#include <cppuhelper/detail/XExceptionThrower.hpp>
#include <com/sun/star/uno/RuntimeException.hpp>
2000-09-18 14:29:57 +00:00
#include <cppuhelper/exc_hlp.hxx>
Re-think cppu::throwException() and the C++/UNO bridge on iOS It seems that on iOS, where we don't have any Java, Python, BASIC, or other scripting, the only thing that would use the C++/UNO bridge functionality that invokes codeSnippet() was cppu::throwException(). codeSnippet() is part of what corresponds to the code that uses run-time-generated machine code on other platforms. We can't generate code at run-time on iOS, that has been known forever. Instead we have used some manually written assembler to handle it instead. We used to have a Perl script to generate a set of code snippets for different cases, different numbers of parameters of the called function and whatnot, but that went away at some stage some year ago. (It is unclear whether that broke the C++/UNO bridge on iOS, or whether the stuff continued to work even after that.) Anyway, this handwritten assembly, or the manual construction of internal data structures for exceptions, or something else, seemed to have bit-rotten. Exceptions thrown with cppu::throwException() were not catchable properly any longer. Instead of digging in and trying to understand what is wrong, I chose another solution. It turns out that the number of types of exception objects thrown by cppu::throwException() is fairly small. During startup of the LibreOffice code, and loading of an .odt document, only one kind of exception is thrown this way... (The lovely css::ucb:InteractiveAugmentedIOException.) So we can simply have code that checks what the type of object being thrown is, and explicitgly throws such an object then with a normal C++ throw statement. Seems to work. Sadly the cppu::getCaughtException() API still needs some inline assembly in the C++/UNO brige. That seems to work though, knock on wood. This commit also adds a small "unit test" for iOS, copied from cppuhelperm to ImplSVMain(). Ideally we should not copy code around of course, but have a separate unit test app for iOS that would somehow include relevant unit tests from source files all over the place. Later. Change-Id: Ib6d9d5b6fb8cc684ec15c97a312ca2f720e87069 Reviewed-on: https://gerrit.libreoffice.org/60506 Tested-by: Jenkins Reviewed-by: Tor Lillqvist <tml@collabora.com>
2018-09-12 22:06:18 +03:00
#ifdef IOS
#include <ios/ios.hxx>
#endif
using namespace ::osl;
using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
2000-09-18 14:29:57 +00:00
namespace
2000-09-18 14:29:57 +00:00
{
using cppuhelper::detail::XExceptionThrower;
struct ExceptionThrower : public uno_Interface, XExceptionThrower
2000-09-18 14:29:57 +00:00
{
ExceptionThrower();
2000-09-18 14:29:57 +00:00
virtual ~ExceptionThrower() {}
static Type const & getCppuType()
2000-09-18 14:29:57 +00:00
{
return cppu::UnoType<XExceptionThrower>::get();
2000-09-18 14:29:57 +00:00
}
// XInterface
virtual Any SAL_CALL queryInterface( Type const & type ) override;
virtual void SAL_CALL acquire() throw () override;
virtual void SAL_CALL release() throw () override;
2000-09-18 14:29:57 +00:00
// XExceptionThrower
virtual void SAL_CALL throwException( Any const & exc ) override;
virtual void SAL_CALL rethrowException() override;
2000-09-18 14:29:57 +00:00
};
extern "C"
{
void ExceptionThrower_acquire_release_nop(
SAL_UNUSED_PARAMETER uno_Interface * )
{}
void ExceptionThrower_dispatch(
uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
void * pReturn, void * pArgs [], uno_Any ** ppException )
2000-09-18 14:29:57 +00:00
{
OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );
2000-09-18 14:29:57 +00:00
switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
const_cast< typelib_TypeDescription * >( pMemberType ) )->
nPosition)
2000-09-18 14:29:57 +00:00
{
case 0: // queryInterface()
2000-09-18 14:29:57 +00:00
{
Type const & rType_demanded =
*static_cast< Type const * >( pArgs[ 0 ] );
if (rType_demanded.equals( cppu::UnoType<XInterface>::get() ) ||
rType_demanded.equals( ExceptionThrower::getCppuType() ))
2000-09-18 14:29:57 +00:00
{
typelib_TypeDescription * pTD = nullptr;
TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
uno_any_construct(
static_cast< uno_Any * >( pReturn ), &pUnoI, pTD, nullptr );
2000-09-18 14:29:57 +00:00
TYPELIB_DANGER_RELEASE( pTD );
}
else
{
uno_any_construct(
static_cast< uno_Any * >( pReturn ), nullptr, nullptr, nullptr );
2000-09-18 14:29:57 +00:00
}
*ppException = nullptr;
2000-09-18 14:29:57 +00:00
break;
}
case 1: // acquire()
case 2: // release()
*ppException = nullptr;
2000-09-18 14:29:57 +00:00
break;
case 3: // throwException()
{
uno_Any * pAny = static_cast< uno_Any * >( pArgs[ 0 ] );
OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
uno_type_any_construct( *ppException, pAny->pData, pAny->pType, nullptr );
2000-09-18 14:29:57 +00:00
break;
}
default:
{
OSL_ASSERT( false );
RuntimeException exc( "not implemented!" );
uno_type_any_construct(
*ppException, &exc, cppu::UnoType<decltype(exc)>::get().getTypeLibType(), nullptr );
break;
}
2000-09-18 14:29:57 +00:00
}
}
} // extern "C"
Any ExceptionThrower::queryInterface( Type const & type )
{
if (type.equals( cppu::UnoType<XInterface>::get() ) ||
type.equals( ExceptionThrower::getCppuType() ))
{
XExceptionThrower * that = this;
return Any( &that, type );
}
return Any();
}
void ExceptionThrower::acquire() throw ()
{
}
void ExceptionThrower::release() throw ()
2000-09-18 14:29:57 +00:00
{
}
void ExceptionThrower::throwException( Any const & exc )
{
OSL_FAIL( "unexpected!" );
cppu::throwException( exc );
}
void ExceptionThrower::rethrowException()
{
throw;
}
ExceptionThrower::ExceptionThrower()
{
uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
uno_Interface::release = ExceptionThrower_acquire_release_nop;
2000-09-18 14:29:57 +00:00
uno_Interface::pDispatcher = ExceptionThrower_dispatch;
}
class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};
} // anonymous namespace
namespace cppu
{
void SAL_CALL throwException( Any const & exc )
{
if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
{
throw RuntimeException(
"no UNO exception given "
"(must be derived from com::sun::star::uno::Exception)!" );
2000-09-18 14:29:57 +00:00
}
Re-think cppu::throwException() and the C++/UNO bridge on iOS It seems that on iOS, where we don't have any Java, Python, BASIC, or other scripting, the only thing that would use the C++/UNO bridge functionality that invokes codeSnippet() was cppu::throwException(). codeSnippet() is part of what corresponds to the code that uses run-time-generated machine code on other platforms. We can't generate code at run-time on iOS, that has been known forever. Instead we have used some manually written assembler to handle it instead. We used to have a Perl script to generate a set of code snippets for different cases, different numbers of parameters of the called function and whatnot, but that went away at some stage some year ago. (It is unclear whether that broke the C++/UNO bridge on iOS, or whether the stuff continued to work even after that.) Anyway, this handwritten assembly, or the manual construction of internal data structures for exceptions, or something else, seemed to have bit-rotten. Exceptions thrown with cppu::throwException() were not catchable properly any longer. Instead of digging in and trying to understand what is wrong, I chose another solution. It turns out that the number of types of exception objects thrown by cppu::throwException() is fairly small. During startup of the LibreOffice code, and loading of an .odt document, only one kind of exception is thrown this way... (The lovely css::ucb:InteractiveAugmentedIOException.) So we can simply have code that checks what the type of object being thrown is, and explicitgly throws such an object then with a normal C++ throw statement. Seems to work. Sadly the cppu::getCaughtException() API still needs some inline assembly in the C++/UNO brige. That seems to work though, knock on wood. This commit also adds a small "unit test" for iOS, copied from cppuhelperm to ImplSVMain(). Ideally we should not copy code around of course, but have a separate unit test app for iOS that would somehow include relevant unit tests from source files all over the place. Later. Change-Id: Ib6d9d5b6fb8cc684ec15c97a312ca2f720e87069 Reviewed-on: https://gerrit.libreoffice.org/60506 Tested-by: Jenkins Reviewed-by: Tor Lillqvist <tml@collabora.com>
2018-09-12 22:06:18 +03:00
#ifdef IOS
lo_ios_throwException(exc);
#else
Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
if (! uno2cpp.is())
{
throw RuntimeException(
"cannot get binary UNO to C++ mapping!" );
}
Reference< XExceptionThrower > xThrower;
uno2cpp.mapInterface(
reinterpret_cast< void ** >( &xThrower ),
static_cast< uno_Interface * >( &theExceptionThrower::get() ),
ExceptionThrower::getCppuType() );
OSL_ASSERT( xThrower.is() );
xThrower->throwException( exc );
Re-think cppu::throwException() and the C++/UNO bridge on iOS It seems that on iOS, where we don't have any Java, Python, BASIC, or other scripting, the only thing that would use the C++/UNO bridge functionality that invokes codeSnippet() was cppu::throwException(). codeSnippet() is part of what corresponds to the code that uses run-time-generated machine code on other platforms. We can't generate code at run-time on iOS, that has been known forever. Instead we have used some manually written assembler to handle it instead. We used to have a Perl script to generate a set of code snippets for different cases, different numbers of parameters of the called function and whatnot, but that went away at some stage some year ago. (It is unclear whether that broke the C++/UNO bridge on iOS, or whether the stuff continued to work even after that.) Anyway, this handwritten assembly, or the manual construction of internal data structures for exceptions, or something else, seemed to have bit-rotten. Exceptions thrown with cppu::throwException() were not catchable properly any longer. Instead of digging in and trying to understand what is wrong, I chose another solution. It turns out that the number of types of exception objects thrown by cppu::throwException() is fairly small. During startup of the LibreOffice code, and loading of an .odt document, only one kind of exception is thrown this way... (The lovely css::ucb:InteractiveAugmentedIOException.) So we can simply have code that checks what the type of object being thrown is, and explicitgly throws such an object then with a normal C++ throw statement. Seems to work. Sadly the cppu::getCaughtException() API still needs some inline assembly in the C++/UNO brige. That seems to work though, knock on wood. This commit also adds a small "unit test" for iOS, copied from cppuhelperm to ImplSVMain(). Ideally we should not copy code around of course, but have a separate unit test app for iOS that would somehow include relevant unit tests from source files all over the place. Later. Change-Id: Ib6d9d5b6fb8cc684ec15c97a312ca2f720e87069 Reviewed-on: https://gerrit.libreoffice.org/60506 Tested-by: Jenkins Reviewed-by: Tor Lillqvist <tml@collabora.com>
2018-09-12 22:06:18 +03:00
#endif
}
Any SAL_CALL getCaughtException()
{
Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
if (! cpp2uno.is())
{
throw RuntimeException(
"cannot get C++ to binary UNO mapping!" );
}
Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
if (! uno2cpp.is())
{
throw RuntimeException(
"cannot get binary UNO to C++ mapping!" );
}
typelib_TypeDescription * pTD = nullptr;
TYPELIB_DANGER_GET(
&pTD, ExceptionThrower::getCppuType().getTypeLibType() );
UnoInterfaceReference unoI;
cpp2uno.mapInterface(
reinterpret_cast< void ** >( &unoI.m_pUnoI ),
static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
OSL_ASSERT( unoI.is() );
typelib_TypeDescription * pMemberTD = nullptr;
TYPELIB_DANGER_GET(
&pMemberTD,
reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
ppMembers[ 1 ] /* rethrowException() */ );
uno_Any exc_mem;
uno_Any * exc = &exc_mem;
unoI.dispatch( pMemberTD, nullptr, nullptr, &exc );
TYPELIB_DANGER_RELEASE( pMemberTD );
TYPELIB_DANGER_RELEASE( pTD );
if (exc == nullptr)
{
throw RuntimeException( "rethrowing C++ exception failed!" );
}
Any ret;
uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
uno_type_any_constructAndConvert(
&ret, exc->pData, exc->pType, uno2cpp.get() );
uno_any_destruct( exc, nullptr );
return ret;
2000-09-18 14:29:57 +00:00
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */