1*61dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*61dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*61dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*61dff127SAndrew Rist  * distributed with this work for additional information
6*61dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*61dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*61dff127SAndrew Rist  * "License"); you may not use this file except in compliance
9*61dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*61dff127SAndrew Rist  *
11*61dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*61dff127SAndrew Rist  *
13*61dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*61dff127SAndrew Rist  * software distributed under the License is distributed on an
15*61dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*61dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
17*61dff127SAndrew Rist  * specific language governing permissions and limitations
18*61dff127SAndrew Rist  * under the License.
19*61dff127SAndrew Rist  *
20*61dff127SAndrew Rist  *************************************************************/
21*61dff127SAndrew Rist 
22*61dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_bridges.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <dlfcn.h>
29cdf0e10cSrcweir #include <cxxabi.h>
30cdf0e10cSrcweir #include <hash_map>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <rtl/strbuf.hxx>
33cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
34cdf0e10cSrcweir #include <osl/diagnose.h>
35cdf0e10cSrcweir #include <osl/mutex.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.hxx>
38cdf0e10cSrcweir #include <typelib/typedescription.hxx>
39cdf0e10cSrcweir #include <uno/any2.h>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include "share.hxx"
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace ::std;
45cdf0e10cSrcweir using namespace ::osl;
46cdf0e10cSrcweir using namespace ::rtl;
47cdf0e10cSrcweir using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir using namespace ::__cxxabiv1;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace CPPU_CURRENT_NAMESPACE
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 
dummy_can_throw_anything(char const *)54cdf0e10cSrcweir void dummy_can_throw_anything( char const * )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir //==================================================================================================
toUNOname(char const * p)59cdf0e10cSrcweir static OUString toUNOname( char const * p ) SAL_THROW( () )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir #ifdef DEBUG
62cdf0e10cSrcweir     char const * start = p;
63cdf0e10cSrcweir #endif
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     // example: N3com3sun4star4lang24IllegalArgumentExceptionE
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	OUStringBuffer buf( 64 );
68cdf0e10cSrcweir     OSL_ASSERT( 'N' == *p );
69cdf0e10cSrcweir     ++p; // skip N
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     while ('E' != *p)
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         // read chars count
74cdf0e10cSrcweir         long n = (*p++ - '0');
75cdf0e10cSrcweir         while ('0' <= *p && '9' >= *p)
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             n *= 10;
78cdf0e10cSrcweir             n += (*p++ - '0');
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir         buf.appendAscii( p, n );
81cdf0e10cSrcweir         p += n;
82cdf0e10cSrcweir         if ('E' != *p)
83cdf0e10cSrcweir             buf.append( (sal_Unicode)'.' );
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir #ifdef DEBUG
87cdf0e10cSrcweir     OUString ret( buf.makeStringAndClear() );
88cdf0e10cSrcweir     OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
89cdf0e10cSrcweir     fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
90cdf0e10cSrcweir     return ret;
91cdf0e10cSrcweir #else
92cdf0e10cSrcweir     return buf.makeStringAndClear();
93cdf0e10cSrcweir #endif
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir //==================================================================================================
97cdf0e10cSrcweir class RTTI
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     Mutex m_mutex;
102cdf0e10cSrcweir 	t_rtti_map m_rttis;
103cdf0e10cSrcweir     t_rtti_map m_generatedRttis;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     void * m_hApp;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir public:
108cdf0e10cSrcweir     RTTI() SAL_THROW( () );
109cdf0e10cSrcweir     ~RTTI() SAL_THROW( () );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
112cdf0e10cSrcweir };
113cdf0e10cSrcweir //__________________________________________________________________________________________________
RTTI()114cdf0e10cSrcweir RTTI::RTTI() SAL_THROW( () )
115cdf0e10cSrcweir     : m_hApp( dlopen( 0, RTLD_LAZY ) )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir }
118cdf0e10cSrcweir //__________________________________________________________________________________________________
~RTTI()119cdf0e10cSrcweir RTTI::~RTTI() SAL_THROW( () )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     dlclose( m_hApp );
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir //__________________________________________________________________________________________________
getRTTI(typelib_CompoundTypeDescription * pTypeDescr)125cdf0e10cSrcweir type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     type_info * rtti;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     MutexGuard guard( m_mutex );
132cdf0e10cSrcweir     t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
133cdf0e10cSrcweir     if (iFind == m_rttis.end())
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         // RTTI symbol
136cdf0e10cSrcweir         OStringBuffer buf( 64 );
137cdf0e10cSrcweir         buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
138cdf0e10cSrcweir         sal_Int32 index = 0;
139cdf0e10cSrcweir         do
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             OUString token( unoName.getToken( 0, '.', index ) );
142cdf0e10cSrcweir             buf.append( token.getLength() );
143cdf0e10cSrcweir             OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
144cdf0e10cSrcweir             buf.append( c_token );
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir         while (index >= 0);
147cdf0e10cSrcweir         buf.append( 'E' );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         OString symName( buf.makeStringAndClear() );
150cdf0e10cSrcweir         rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         if (rtti)
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir             pair< t_rtti_map::iterator, bool > insertion(
155cdf0e10cSrcweir                 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
156cdf0e10cSrcweir             OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         else
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             // try to lookup the symbol in the generated rtti map
161cdf0e10cSrcweir             t_rtti_map::const_iterator iiFind( m_generatedRttis.find( unoName ) );
162cdf0e10cSrcweir             if (iiFind == m_generatedRttis.end())
163cdf0e10cSrcweir             {
164cdf0e10cSrcweir                 // we must generate it !
165cdf0e10cSrcweir                 // symbol and rtti-name is nearly identical,
166cdf0e10cSrcweir                 // the symbol is prefixed with _ZTI
167cdf0e10cSrcweir                 char const * rttiName = symName.getStr() +4;
168cdf0e10cSrcweir #ifdef DEBUG
169cdf0e10cSrcweir                 fprintf( stderr,"generated rtti for %s\n", rttiName );
170cdf0e10cSrcweir #endif
171cdf0e10cSrcweir                 if (pTypeDescr->pBaseTypeDescription)
172cdf0e10cSrcweir                 {
173cdf0e10cSrcweir                     // ensure availability of base
174cdf0e10cSrcweir                     type_info * base_rtti = getRTTI(
175cdf0e10cSrcweir                         (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
176cdf0e10cSrcweir                     rtti = new __si_class_type_info(
177cdf0e10cSrcweir                         strdup( rttiName ), (__class_type_info *)base_rtti );
178cdf0e10cSrcweir                 }
179cdf0e10cSrcweir                 else
180cdf0e10cSrcweir                 {
181cdf0e10cSrcweir                     // this class has no base class
182cdf0e10cSrcweir                     rtti = new __class_type_info( strdup( rttiName ) );
183cdf0e10cSrcweir                 }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir                 pair< t_rtti_map::iterator, bool > insertion(
186cdf0e10cSrcweir                     m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
187cdf0e10cSrcweir                 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
188cdf0e10cSrcweir             }
189cdf0e10cSrcweir             else // taking already generated rtti
190cdf0e10cSrcweir             {
191cdf0e10cSrcweir                 rtti = iiFind->second;
192cdf0e10cSrcweir             }
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir     else
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         rtti = iFind->second;
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     return rtti;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
deleteException(void * pExc)204cdf0e10cSrcweir static void deleteException( void * pExc )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
207cdf0e10cSrcweir     typelib_TypeDescription * pTD = 0;
208cdf0e10cSrcweir     OUString unoName( toUNOname( header->exceptionType->name() ) );
209cdf0e10cSrcweir     ::typelib_typedescription_getByName( &pTD, unoName.pData );
210cdf0e10cSrcweir     OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
211cdf0e10cSrcweir     if (pTD)
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir 		::uno_destructData( pExc, pTD, cpp_release );
214cdf0e10cSrcweir 		::typelib_typedescription_release( pTD );
215cdf0e10cSrcweir 	}
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir //==================================================================================================
raiseException(uno_Any * pUnoExc,uno_Mapping * pUno2Cpp)219cdf0e10cSrcweir void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
220cdf0e10cSrcweir {
221cdf0e10cSrcweir     void * pCppExc;
222cdf0e10cSrcweir     type_info * rtti;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir     // construct cpp exception object
226cdf0e10cSrcweir 	typelib_TypeDescription * pTypeDescr = 0;
227cdf0e10cSrcweir 	TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
228cdf0e10cSrcweir     OSL_ASSERT( pTypeDescr );
229cdf0e10cSrcweir     if (! pTypeDescr)
230cdf0e10cSrcweir         terminate();
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 	pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
233cdf0e10cSrcweir 	::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 	// destruct uno exception
236cdf0e10cSrcweir 	::uno_any_destruct( pUnoExc, 0 );
237cdf0e10cSrcweir     // avoiding locked counts
238cdf0e10cSrcweir     static RTTI * s_rtti = 0;
239cdf0e10cSrcweir     if (! s_rtti)
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir         MutexGuard guard( Mutex::getGlobalMutex() );
242cdf0e10cSrcweir         if (! s_rtti)
243cdf0e10cSrcweir         {
244cdf0e10cSrcweir #ifdef LEAK_STATIC_DATA
245cdf0e10cSrcweir             s_rtti = new RTTI();
246cdf0e10cSrcweir #else
247cdf0e10cSrcweir             static RTTI rtti_data;
248cdf0e10cSrcweir             s_rtti = &rtti_data;
249cdf0e10cSrcweir #endif
250cdf0e10cSrcweir         }
251cdf0e10cSrcweir     }
252cdf0e10cSrcweir 	rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
253cdf0e10cSrcweir     TYPELIB_DANGER_RELEASE( pTypeDescr );
254cdf0e10cSrcweir     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
255cdf0e10cSrcweir     if (! rtti)
256cdf0e10cSrcweir         terminate();
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	__cxa_throw( pCppExc, rtti, deleteException );
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir //==================================================================================================
fillUnoException(__cxa_exception * header,uno_Any * pExc,uno_Mapping * pCpp2Uno)263cdf0e10cSrcweir void fillUnoException( __cxa_exception * header, uno_Any * pExc, uno_Mapping * pCpp2Uno )
264cdf0e10cSrcweir {
265cdf0e10cSrcweir     OSL_ENSURE( header, "### no exception header!!!" );
266cdf0e10cSrcweir     if (! header)
267cdf0e10cSrcweir         terminate();
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	typelib_TypeDescription * pExcTypeDescr = 0;
270cdf0e10cSrcweir     OUString unoName( toUNOname( header->exceptionType->name() ) );
271cdf0e10cSrcweir 	::typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
272cdf0e10cSrcweir     OSL_ENSURE( pExcTypeDescr, "### can not get type description for exception!!!" );
273cdf0e10cSrcweir     if (! pExcTypeDescr)
274cdf0e10cSrcweir         terminate();
275cdf0e10cSrcweir 
276cdf0e10cSrcweir     // construct uno exception any
277cdf0e10cSrcweir     ::uno_any_constructAndConvert( pExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
278cdf0e10cSrcweir     ::typelib_typedescription_release( pExcTypeDescr );
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283