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>
33 #include <unordered_map>
37 using namespace ::std
;
38 using namespace ::osl
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::__cxxabiv1
;
42 extern sal_Int32
* pHack
;
43 extern sal_Int32 nHack
;
45 namespace CPPU_CURRENT_NAMESPACE
47 void dummy_can_throw_anything( char const * )
51 static OUString
toUNOname( char const * p
)
53 #if OSL_DEBUG_LEVEL > 1
54 char const * start
= p
;
57 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
59 OUStringBuffer
buf( 64 );
66 long n
= (*p
++ - '0');
67 while ('0' <= *p
&& '9' >= *p
)
72 buf
.appendAscii( p
, n
);
78 #if OSL_DEBUG_LEVEL > 1
79 OUString
ret( buf
.makeStringAndClear() );
80 OString
c_ret( OUStringToOString( ret
, RTL_TEXTENCODING_ASCII_US
) );
81 fprintf( stderr
, "> toUNOname(): %s => %s\n", start
, c_ret
.getStr() );
84 return buf
.makeStringAndClear();
90 typedef std::unordered_map
< OUString
, type_info
* > t_rtti_map
;
94 t_rtti_map m_generatedRttis
;
102 type_info
* getRTTI(typelib_CompoundTypeDescription
*);
106 : m_hApp( dlopen( 0, RTLD_LAZY
) )
116 type_info
* RTTI::getRTTI( typelib_CompoundTypeDescription
*pTypeDescr
)
120 OUString
const & unoName
= *(OUString
const *)&pTypeDescr
->aBase
.pTypeName
;
122 MutexGuard
guard( m_mutex
);
123 t_rtti_map::const_iterator
iRttiFind( m_rttis
.find( unoName
) );
124 if (iRttiFind
== m_rttis
.end())
127 OStringBuffer
buf( 64 );
128 buf
.append( "_ZTIN" );
132 OUString
token( unoName
.getToken( 0, '.', index
) );
133 buf
.append( token
.getLength() );
134 OString
c_token( OUStringToOString( token
, RTL_TEXTENCODING_ASCII_US
) );
135 buf
.append( c_token
);
140 OString
symName( buf
.makeStringAndClear() );
141 rtti
= (type_info
*)dlsym( m_hApp
, symName
.getStr() );
145 pair
< t_rtti_map::iterator
, bool > insertion(
146 m_rttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
147 assert(insertion
.second
&& "### inserting new rtti failed?!");
151 // try to lookup the symbol in the generated rtti map
152 t_rtti_map::const_iterator
iFind( m_generatedRttis
.find( unoName
) );
153 if (iFind
== m_generatedRttis
.end())
155 // we must generate it !
156 // symbol and rtti-name is nearly identical,
157 // the symbol is prefixed with _ZTI
158 char const * rttiName
= symName
.getStr() +4;
159 #if OSL_DEBUG_LEVEL > 1
160 fprintf( stderr
,"generated rtti for %s\n", rttiName
);
162 if (pTypeDescr
->pBaseTypeDescription
)
164 // ensure availability of base
165 type_info
* base_rtti
= getRTTI(
166 (typelib_CompoundTypeDescription
*)pTypeDescr
->pBaseTypeDescription
);
167 rtti
= new __si_class_type_info(
168 strdup( rttiName
), (__class_type_info
*)base_rtti
);
172 // this class has no base class
173 rtti
= new __class_type_info( strdup( rttiName
) );
176 pair
< t_rtti_map::iterator
, bool > insertion(
177 m_generatedRttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
178 assert(insertion
.second
&& "### inserting new generated rtti failed?!");
180 else // taking already generated rtti
182 rtti
= iFind
->second
;
188 rtti
= iRttiFind
->second
;
195 static void deleteException( void * pExc
)
197 __cxa_exception
const * header
= ((__cxa_exception
const *)pExc
- 1);
198 typelib_TypeDescription
* pTD
= 0;
199 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
200 ::typelib_typedescription_getByName( &pTD
, unoName
.pData
);
201 assert(pTD
&& "### unknown exception type! leaving out destruction => leaking!!!");
204 ::uno_destructData( pExc
, pTD
, cpp_release
);
205 ::typelib_typedescription_release( pTD
);
209 void raiseException( uno_Any
* pUnoExc
, uno_Mapping
* pUno2Cpp
)
211 #if OSL_DEBUG_LEVEL > 1
214 OUString::unacquired( &pUnoExc
->pType
->pTypeName
),
215 RTL_TEXTENCODING_ASCII_US
) );
216 fprintf( stderr
, "> uno exception occurred: %s\n", cstr
.getStr() );
222 // construct cpp exception object
223 typelib_TypeDescription
* pTypeDescr
= 0;
224 TYPELIB_DANGER_GET( &pTypeDescr
, pUnoExc
->pType
);
228 throw RuntimeException(
229 OUString("cannot get typedescription for type ") +
230 OUString::unacquired( &pUnoExc
->pType
->pTypeName
) );
233 pCppExc
= __cxa_allocate_exception( pTypeDescr
->nSize
);
234 ::uno_copyAndConvertData( pCppExc
, pUnoExc
->pData
, pTypeDescr
, pUno2Cpp
);
236 // destruct uno exception
237 ::uno_any_destruct( pUnoExc
, 0 );
238 // avoiding locked counts
239 static RTTI rtti_data
;
240 rtti
= (type_info
*)rtti_data
.getRTTI((typelib_CompoundTypeDescription
*)pTypeDescr
);
241 TYPELIB_DANGER_RELEASE( pTypeDescr
);
242 assert(rtti
&& "### no rtti for throwing exception!");
245 throw RuntimeException(
246 OUString("no rtti for type ") +
247 OUString::unacquired( &pUnoExc
->pType
->pTypeName
) );
252 __cxa_throw( pCppExc
, rtti
, deleteException
);
255 static void* getAdjustedPtr(__cxa_exception
* header
)
257 return header
->adjustedPtr
;
260 void fillUnoException(uno_Any
* pUnoExc
, uno_Mapping
* pCpp2Uno
)
262 __cxa_exception
* header
= __cxa_get_globals()->caughtExceptions
;
265 RuntimeException
aRE( "no exception header!" );
266 Type
const & rType
= cppu::UnoType
<decltype(aRE
)>::get();
267 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
268 SAL_WARN("bridges", aRE
.Message
);
272 std::type_info
*exceptionType
= __cxa_current_exception_type();
274 typelib_TypeDescription
* pExcTypeDescr
= 0;
275 OUString
unoName( toUNOname( exceptionType
->name() ) );
276 #if OSL_DEBUG_LEVEL > 1
277 OString
cstr_unoName( OUStringToOString( unoName
, RTL_TEXTENCODING_ASCII_US
) );
278 fprintf( stderr
, "> c++ exception occurred: %s\n", cstr_unoName
.getStr() );
280 typelib_typedescription_getByName( &pExcTypeDescr
, unoName
.pData
);
281 if (0 == pExcTypeDescr
)
283 RuntimeException
aRE(
284 OUString("exception type not found: ") + unoName
);
285 Type
const & rType
= cppu::UnoType
<decltype(aRE
)>::get();
286 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
287 SAL_WARN("bridges", aRE
.Message
);
291 // construct uno exception any
292 uno_any_constructAndConvert( pUnoExc
, getAdjustedPtr(header
), pExcTypeDescr
, pCpp2Uno
);
293 typelib_typedescription_release( pExcTypeDescr
);
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */