1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
24 #include <rtl/strbuf.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <osl/mutex.hxx>
27 #include <sal/log.hxx>
29 #include <com/sun/star/uno/genfunc.hxx>
30 #include <com/sun/star/uno/RuntimeException.hpp>
31 #include <typelib/typedescription.hxx>
32 #include <unordered_map>
35 using namespace ::std
;
36 using namespace ::osl
;
37 using namespace ::com::sun::star::uno
;
38 using namespace ::__cxxabiv1
;
41 namespace CPPU_CURRENT_NAMESPACE
44 void dummy_can_throw_anything( char const * )
48 static OUString
toUNOname( char const * p
)
50 #if OSL_DEBUG_LEVEL > 1
51 char const * start
= p
;
54 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
56 OUStringBuffer
buf( 64 );
63 long n
= (*p
++ - '0');
64 while ('0' <= *p
&& '9' >= *p
)
69 buf
.appendAscii( p
, n
);
75 #if OSL_DEBUG_LEVEL > 1
76 OUString
ret( buf
.makeStringAndClear() );
77 OString
c_ret( OUStringToOString( ret
, RTL_TEXTENCODING_ASCII_US
) );
78 fprintf( stderr
, "> toUNOname(): %s => %s\n", start
, c_ret
.getStr() );
81 return buf
.makeStringAndClear();
87 typedef std::unordered_map
< OUString
, type_info
* > t_rtti_map
;
91 t_rtti_map m_generatedRttis
;
101 type_info
* getRTTI( typelib_CompoundTypeDescription
* );
106 : m_hApp( dlopen(nullptr, RTLD_LAZY
) )
119 type_info
* RTTI::getRTTI( typelib_CompoundTypeDescription
*pTypeDescr
)
123 OUString
const & unoName
= OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
);
125 MutexGuard
guard( m_mutex
);
126 t_rtti_map::const_iterator
iRttiFind( m_rttis
.find( unoName
) );
127 if (iRttiFind
== m_rttis
.end())
130 OStringBuffer
buf( 64 );
131 buf
.append( "_ZTIN" );
135 OUString
token( unoName
.getToken( 0, '.', index
) );
136 buf
.append( token
.getLength() );
137 OString
c_token( OUStringToOString( token
, RTL_TEXTENCODING_ASCII_US
) );
138 buf
.append( c_token
);
143 OString
symName( buf
.makeStringAndClear() );
145 rtti
= static_cast<type_info
*>(dlsym( m_hApp
, symName
.getStr() ));
147 rtti
= static_cast<type_info
*>(dlsym( RTLD_DEFAULT
, symName
.getStr() ));
152 pair
< t_rtti_map::iterator
, bool > insertion(
153 m_rttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
154 SAL_WARN_IF( !insertion
.second
, "bridges", "### inserting new rtti failed?!" );
158 // try to lookup the symbol in the generated rtti map
159 t_rtti_map::const_iterator
iFind( m_generatedRttis
.find( unoName
) );
160 if (iFind
== m_generatedRttis
.end())
162 // we must generate it !
163 // symbol and rtti-name is nearly identical,
164 // the symbol is prefixed with _ZTI
165 char const * rttiName
= symName
.getStr() +4;
166 #if OSL_DEBUG_LEVEL > 1
167 fprintf( stderr
,"generated rtti for %s\n", rttiName
);
169 if (pTypeDescr
->pBaseTypeDescription
)
171 // ensure availability of base
172 type_info
* base_rtti
= getRTTI(
173 pTypeDescr
->pBaseTypeDescription
);
174 rtti
= new __si_class_type_info(
175 strdup( rttiName
), static_cast<__class_type_info
*>(base_rtti
) );
179 // this class has no base class
180 rtti
= new __class_type_info( strdup( rttiName
) );
183 pair
< t_rtti_map::iterator
, bool > insertion(
184 m_generatedRttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
185 SAL_WARN_IF( !insertion
.second
, "bridges", "### inserting new generated rtti failed?!" );
187 else // taking already generated rtti
189 rtti
= iFind
->second
;
195 rtti
= iRttiFind
->second
;
203 static void _GLIBCXX_CDTOR_CALLABI
deleteException( void * pExc
)
205 __cxa_exception
const * header
= static_cast<__cxa_exception
const *>(pExc
) - 1;
206 typelib_TypeDescription
* pTD
= nullptr;
207 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
208 ::typelib_typedescription_getByName( &pTD
, unoName
.pData
);
209 assert(pTD
&& "### unknown exception type! leaving out destruction => leaking!!!");
212 ::uno_destructData( pExc
, pTD
, cpp_release
);
213 ::typelib_typedescription_release( pTD
);
218 void raiseException( uno_Any
* pUnoExc
, uno_Mapping
* pUno2Cpp
)
220 #if OSL_DEBUG_LEVEL > 1
223 OUString::unacquired( &pUnoExc
->pType
->pTypeName
),
224 RTL_TEXTENCODING_ASCII_US
) );
225 fprintf( stderr
, "> uno exception occurred: %s\n", cstr
.getStr() );
231 // construct cpp exception object
232 typelib_TypeDescription
* pTypeDescr
= nullptr;
233 TYPELIB_DANGER_GET( &pTypeDescr
, pUnoExc
->pType
);
237 throw RuntimeException(
238 "cannot get typedescription for type " +
239 OUString::unacquired( &pUnoExc
->pType
->pTypeName
) );
242 pCppExc
= __cxxabiv1::__cxa_allocate_exception( pTypeDescr
->nSize
);
243 ::uno_copyAndConvertData( pCppExc
, pUnoExc
->pData
, pTypeDescr
, pUno2Cpp
);
245 // destruct uno exception
246 ::uno_any_destruct( pUnoExc
, nullptr );
247 // avoiding locked counts
248 static RTTI rtti_data
;
249 rtti
= rtti_data
.getRTTI(reinterpret_cast<typelib_CompoundTypeDescription
*>(pTypeDescr
));
250 TYPELIB_DANGER_RELEASE( pTypeDescr
);
251 assert(rtti
&& "### no rtti for throwing exception!");
254 throw RuntimeException(
255 "no rtti for type " +
256 OUString::unacquired( &pUnoExc
->pType
->pTypeName
) );
260 __cxxabiv1::__cxa_throw( pCppExc
, rtti
, deleteException
);
263 void fillUnoException(uno_Any
* pUnoExc
, uno_Mapping
* pCpp2Uno
)
265 __cxa_exception
* header
= reinterpret_cast<CPPU_CURRENT_NAMESPACE::__cxa_eh_globals
*>(
266 __cxxabiv1::__cxa_get_globals())->caughtExceptions
;
269 RuntimeException
aRE( "no exception header!" );
270 Type
const & rType
= cppu::UnoType
<decltype(aRE
)>::get();
271 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
272 SAL_WARN("bridges", aRE
.Message
);
276 std::type_info
*exceptionType
= __cxxabiv1::__cxa_current_exception_type();
278 typelib_TypeDescription
* pExcTypeDescr
= nullptr;
279 OUString
unoName( toUNOname( exceptionType
->name() ) );
280 #if OSL_DEBUG_LEVEL > 1
281 OString
cstr_unoName( OUStringToOString( unoName
, RTL_TEXTENCODING_ASCII_US
) );
282 fprintf( stderr
, "> c++ exception occurred: %s\n", cstr_unoName
.getStr() );
284 typelib_typedescription_getByName( &pExcTypeDescr
, unoName
.pData
);
285 if (nullptr == pExcTypeDescr
)
287 RuntimeException
aRE( "exception type not found: " + unoName
);
288 Type
const & rType
= cppu::UnoType
<decltype(aRE
)>::get();
289 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
290 SAL_WARN("bridges", aRE
.Message
);
294 // construct uno exception any
295 uno_any_constructAndConvert( pUnoExc
, header
->adjustedPtr
, pExcTypeDescr
, pCpp2Uno
);
296 typelib_typedescription_release( pExcTypeDescr
);
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */