nss: upgrade to release 3.73
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_intel / except.cxx
blob79a37803b3c7de9de2a1e47a71547a6ae9c70d5a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <cstdio>
21 #include <cstring>
22 #include <dlfcn.h>
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>
33 #include "share.hxx"
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;
52 #endif
54 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
56 OUStringBuffer buf( 64 );
57 assert( 'N' == *p );
58 ++p; // skip N
60 while ('E' != *p)
62 // read chars count
63 long n = (*p++ - '0');
64 while ('0' <= *p && '9' >= *p)
66 n *= 10;
67 n += (*p++ - '0');
69 buf.appendAscii( p, n );
70 p += n;
71 if ('E' != *p)
72 buf.append( '.' );
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() );
79 return ret;
80 #else
81 return buf.makeStringAndClear();
82 #endif
85 class RTTI
87 typedef std::unordered_map< OUString, type_info * > t_rtti_map;
89 Mutex m_mutex;
90 t_rtti_map m_rttis;
91 t_rtti_map m_generatedRttis;
93 #if !defined ANDROID
94 void * m_hApp;
95 #endif
97 public:
98 RTTI();
99 ~RTTI();
101 type_info * getRTTI( typelib_CompoundTypeDescription * );
104 RTTI::RTTI()
105 #if !defined ANDROID
106 : m_hApp( dlopen(nullptr, RTLD_LAZY) )
107 #endif
111 RTTI::~RTTI()
113 #if !defined ANDROID
114 dlclose( m_hApp );
115 #endif
119 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
121 type_info * rtti;
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())
129 // RTTI symbol
130 OStringBuffer buf( 64 );
131 buf.append( "_ZTIN" );
132 sal_Int32 index = 0;
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 );
140 while (index >= 0);
141 buf.append( 'E' );
143 OString symName( buf.makeStringAndClear() );
144 #if !defined ANDROID
145 rtti = static_cast<type_info *>(dlsym( m_hApp, symName.getStr() ));
146 #else
147 rtti = static_cast<type_info *>(dlsym( RTLD_DEFAULT, symName.getStr() ));
148 #endif
150 if (rtti)
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?!" );
156 else
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 );
168 #endif
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) );
177 else
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;
193 else
195 rtti = iRttiFind->second;
198 return rtti;
202 extern "C" {
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!!!");
210 if (pTD)
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
221 OString cstr(
222 OUStringToOString(
223 OUString::unacquired( &pUnoExc->pType->pTypeName ),
224 RTL_TEXTENCODING_ASCII_US ) );
225 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
226 #endif
227 void * pCppExc;
228 type_info * rtti;
231 // construct cpp exception object
232 typelib_TypeDescription * pTypeDescr = nullptr;
233 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
234 assert(pTypeDescr);
235 if (! pTypeDescr)
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!");
252 if (! rtti)
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;
267 if (! header)
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);
273 return;
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() );
283 #endif
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);
292 else
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: */