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 <hash_map>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <sal/alloca.h>
30cdf0e10cSrcweir #include <rtl/alloc.h>
31cdf0e10cSrcweir #include <osl/mutex.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <uno/data.h>
34cdf0e10cSrcweir #include <typelib/typedescription.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <bridges/cpp_uno/bridge.hxx>
37cdf0e10cSrcweir #include <bridges/cpp_uno/type_misc.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "share.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::osl;
43cdf0e10cSrcweir using namespace ::rtl;
44cdf0e10cSrcweir using namespace ::com::sun::star::uno;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir namespace CPPU_CURRENT_NAMESPACE
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //==================================================================================================
50cdf0e10cSrcweir rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir //==================================================================================================
cpp2uno_call(cppu_cppInterfaceProxy * pThis,const typelib_TypeDescription * pMemberTypeDescr,typelib_TypeDescriptionReference * pReturnTypeRef,sal_Int32 nParams,typelib_MethodParameter * pParams,void ** pCallStack,sal_Int64 * pRegisterReturn)53cdf0e10cSrcweir static typelib_TypeClass cpp2uno_call(
54cdf0e10cSrcweir 	cppu_cppInterfaceProxy * pThis,
55cdf0e10cSrcweir 	const typelib_TypeDescription * pMemberTypeDescr,
56cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
57cdf0e10cSrcweir 	sal_Int32 nParams, typelib_MethodParameter * pParams,
58cdf0e10cSrcweir 	void ** pCallStack,
59cdf0e10cSrcweir 	sal_Int64 * pRegisterReturn /* space for register return */ )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 	// pCallStack: ret, [return ptr], this, params
62cdf0e10cSrcweir 	char * pCppStack = (char *)(pCallStack +1);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	// return
65cdf0e10cSrcweir 	typelib_TypeDescription * pReturnTypeDescr = 0;
66cdf0e10cSrcweir 	if (pReturnTypeRef)
67cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	void * pUnoReturn = 0;
70cdf0e10cSrcweir 	void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	if (pReturnTypeDescr)
73cdf0e10cSrcweir 	{
74cdf0e10cSrcweir 		if (cppu_isSimpleType( pReturnTypeDescr ))
75cdf0e10cSrcweir 		{
76cdf0e10cSrcweir 			pUnoReturn = pRegisterReturn; // direct way for simple types
77cdf0e10cSrcweir 		}
78cdf0e10cSrcweir 		else // complex return via ptr (pCppReturn)
79cdf0e10cSrcweir 		{
80cdf0e10cSrcweir 			pCppReturn = *(void **)pCppStack;
81cdf0e10cSrcweir 			pCppStack += sizeof(void *);
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 			pUnoReturn = (cppu_relatesToInterface( pReturnTypeDescr )
84cdf0e10cSrcweir 						  ? alloca( pReturnTypeDescr->nSize )
85cdf0e10cSrcweir 						  : pCppReturn); // direct way
86cdf0e10cSrcweir 		}
87cdf0e10cSrcweir 	}
88cdf0e10cSrcweir 	// pop this
89cdf0e10cSrcweir 	pCppStack += sizeof( void* );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	// stack space
92cdf0e10cSrcweir 	OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
93cdf0e10cSrcweir 	// parameters
94cdf0e10cSrcweir 	void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
95cdf0e10cSrcweir 	void ** pCppArgs = pUnoArgs + nParams;
96cdf0e10cSrcweir 	// indizes of values this have to be converted (interface conversion cpp<=>uno)
97cdf0e10cSrcweir 	sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
98cdf0e10cSrcweir 	// type descriptions for reconversions
99cdf0e10cSrcweir 	typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	sal_Int32 nTempIndizes   = 0;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
104cdf0e10cSrcweir 	{
105cdf0e10cSrcweir 		const typelib_MethodParameter & rParam = pParams[nPos];
106cdf0e10cSrcweir 		typelib_TypeDescription * pParamTypeDescr = 0;
107cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		if (!rParam.bOut && cppu_isSimpleType( pParamTypeDescr )) // value
110cdf0e10cSrcweir 		{
111cdf0e10cSrcweir 			pCppArgs[nPos] = pCppStack;
112cdf0e10cSrcweir 			pUnoArgs[nPos] = pCppStack;
113cdf0e10cSrcweir 			switch (pParamTypeDescr->eTypeClass)
114cdf0e10cSrcweir 			{
115cdf0e10cSrcweir 			case typelib_TypeClass_HYPER:
116cdf0e10cSrcweir 			case typelib_TypeClass_UNSIGNED_HYPER:
117cdf0e10cSrcweir 			case typelib_TypeClass_DOUBLE:
118cdf0e10cSrcweir 				pCppStack += sizeof(sal_Int32); // extra long
119cdf0e10cSrcweir 			}
120cdf0e10cSrcweir 			// no longer needed
121cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
122cdf0e10cSrcweir 		}
123cdf0e10cSrcweir 		else // ptr to complex value | ref
124cdf0e10cSrcweir 		{
125cdf0e10cSrcweir 			pCppArgs[nPos] = *(void **)pCppStack;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 			if (! rParam.bIn) // is pure out
128cdf0e10cSrcweir 			{
129cdf0e10cSrcweir 				// uno out is unconstructed mem!
130cdf0e10cSrcweir 				pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
131cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos;
132cdf0e10cSrcweir 				// will be released at reconversion
133cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
134cdf0e10cSrcweir 			}
135cdf0e10cSrcweir 			// is in/inout
136cdf0e10cSrcweir 			else if (cppu_relatesToInterface( pParamTypeDescr ))
137cdf0e10cSrcweir 			{
138cdf0e10cSrcweir 				uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
139cdf0e10cSrcweir 										*(void **)pCppStack, pParamTypeDescr,
140cdf0e10cSrcweir 										&pThis->pBridge->aCpp2Uno );
141cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
142cdf0e10cSrcweir 				// will be released at reconversion
143cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
144cdf0e10cSrcweir 			}
145cdf0e10cSrcweir 			else // direct way
146cdf0e10cSrcweir 			{
147cdf0e10cSrcweir 				pUnoArgs[nPos] = *(void **)pCppStack;
148cdf0e10cSrcweir 				// no longer needed
149cdf0e10cSrcweir 				TYPELIB_DANGER_RELEASE( pParamTypeDescr );
150cdf0e10cSrcweir 			}
151cdf0e10cSrcweir 		}
152cdf0e10cSrcweir 		pCppStack += sizeof(sal_Int32); // standard parameter length
153cdf0e10cSrcweir 	}
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	// ExceptionHolder
156cdf0e10cSrcweir 	uno_Any aUnoExc; // Any will be constructed by callee
157cdf0e10cSrcweir 	uno_Any * pUnoExc = &aUnoExc;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	// invoke uno dispatch call
160cdf0e10cSrcweir 	(*pThis->pUnoI->pDispatcher)( pThis->pUnoI, pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	// in case an exception occured...
163cdf0e10cSrcweir 	if (pUnoExc)
164cdf0e10cSrcweir 	{
165cdf0e10cSrcweir 		// destruct temporary in/inout params
166cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
167cdf0e10cSrcweir 		{
168cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 			if (pParams[nIndex].bIn) // is in/inout => was constructed
171cdf0e10cSrcweir 				uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
172cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
173cdf0e10cSrcweir 		}
174cdf0e10cSrcweir 		if (pReturnTypeDescr)
175cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 		raiseException( &aUnoExc, &pThis->pBridge->aUno2Cpp ); // has to destruct the any
178cdf0e10cSrcweir 		// is here for dummy
179cdf0e10cSrcweir 		return typelib_TypeClass_VOID;
180cdf0e10cSrcweir 	}
181cdf0e10cSrcweir 	else // else no exception occured...
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		// temporary params
184cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
185cdf0e10cSrcweir 		{
186cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
187cdf0e10cSrcweir 			typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 			if (pParams[nIndex].bOut) // inout/out
190cdf0e10cSrcweir 			{
191cdf0e10cSrcweir 				// convert and assign
192cdf0e10cSrcweir 				uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
193cdf0e10cSrcweir 				uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
194cdf0e10cSrcweir 										&pThis->pBridge->aUno2Cpp );
195cdf0e10cSrcweir 			}
196cdf0e10cSrcweir 			// destroy temp uno param
197cdf0e10cSrcweir 			uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
200cdf0e10cSrcweir 		}
201cdf0e10cSrcweir 		// return
202cdf0e10cSrcweir 		if (pCppReturn) // has complex return
203cdf0e10cSrcweir 		{
204cdf0e10cSrcweir 			if (pUnoReturn != pCppReturn) // needs reconversion
205cdf0e10cSrcweir 			{
206cdf0e10cSrcweir 				uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
207cdf0e10cSrcweir 										&pThis->pBridge->aUno2Cpp );
208cdf0e10cSrcweir 				// destroy temp uno return
209cdf0e10cSrcweir 				uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
210cdf0e10cSrcweir 			}
211cdf0e10cSrcweir 			// complex return ptr is set to eax
212cdf0e10cSrcweir 			*(void **)pRegisterReturn = pCppReturn;
213cdf0e10cSrcweir 		}
214cdf0e10cSrcweir 		if (pReturnTypeDescr)
215cdf0e10cSrcweir 		{
216cdf0e10cSrcweir 			typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
217cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
218cdf0e10cSrcweir 			return eRet;
219cdf0e10cSrcweir 		}
220cdf0e10cSrcweir 		else
221cdf0e10cSrcweir 			return typelib_TypeClass_VOID;
222cdf0e10cSrcweir 	}
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 
226cdf0e10cSrcweir //==================================================================================================
cpp_mediate(sal_Int32 nVtableCall,void ** pCallStack,sal_Int64 * pRegisterReturn)227cdf0e10cSrcweir static typelib_TypeClass cpp_mediate(
228cdf0e10cSrcweir 	sal_Int32 nVtableCall,
229cdf0e10cSrcweir 	void ** pCallStack,
230cdf0e10cSrcweir 	sal_Int64 * pRegisterReturn /* space for register return */ )
231cdf0e10cSrcweir {
232cdf0e10cSrcweir 	OSL_ENSURE( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 	// pCallStack: ret adr, [ret *], this, params
235cdf0e10cSrcweir 	// _this_ ptr is patched cppu_XInterfaceProxy object
236cdf0e10cSrcweir 	cppu_cppInterfaceProxy * pCppI = NULL;
237cdf0e10cSrcweir 	if( nVtableCall & 0x80000000 )
238cdf0e10cSrcweir 	{
239cdf0e10cSrcweir 		nVtableCall &= 0x7fffffff;
240cdf0e10cSrcweir 		pCppI = (cppu_cppInterfaceProxy *)(XInterface *)*(pCallStack +2);
241cdf0e10cSrcweir 	}
242cdf0e10cSrcweir 	else
243cdf0e10cSrcweir     {
244cdf0e10cSrcweir 		pCppI = (cppu_cppInterfaceProxy *)(XInterface *)*(pCallStack +1);
245cdf0e10cSrcweir     }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pTypeDescr = pCppI->pTypeDescr;
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 	OSL_ENSURE( nVtableCall < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
250cdf0e10cSrcweir 	if (nVtableCall >= pTypeDescr->nMapFunctionIndexToMemberIndex)
251cdf0e10cSrcweir 	{
252cdf0e10cSrcweir 		throw RuntimeException(
253cdf0e10cSrcweir             OUString::createFromAscii("illegal vtable index!"),
254cdf0e10cSrcweir             (XInterface *)pCppI );
255cdf0e10cSrcweir 	}
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	// determine called method
258cdf0e10cSrcweir 	OSL_ENSURE( nVtableCall < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
259cdf0e10cSrcweir 	sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nVtableCall];
260cdf0e10cSrcweir 	OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 	typelib_TypeClass eRet;
265cdf0e10cSrcweir 	switch (aMemberDescr.get()->eTypeClass)
266cdf0e10cSrcweir 	{
267cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_ATTRIBUTE:
268cdf0e10cSrcweir 	{
269cdf0e10cSrcweir 		if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nVtableCall)
270cdf0e10cSrcweir 		{
271cdf0e10cSrcweir 			// is GET method
272cdf0e10cSrcweir 			eRet = cpp2uno_call(
273cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
274cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
275cdf0e10cSrcweir 				0, 0, // no params
276cdf0e10cSrcweir 				pCallStack, pRegisterReturn );
277cdf0e10cSrcweir 		}
278cdf0e10cSrcweir 		else
279cdf0e10cSrcweir 		{
280cdf0e10cSrcweir 			// is SET method
281cdf0e10cSrcweir 			typelib_MethodParameter aParam;
282cdf0e10cSrcweir 			aParam.pTypeRef =
283cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
284cdf0e10cSrcweir 			aParam.bIn		= sal_True;
285cdf0e10cSrcweir 			aParam.bOut		= sal_False;
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 			eRet = cpp2uno_call(
288cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
289cdf0e10cSrcweir 				0, // indicates void return
290cdf0e10cSrcweir 				1, &aParam,
291cdf0e10cSrcweir 				pCallStack, pRegisterReturn );
292cdf0e10cSrcweir 		}
293cdf0e10cSrcweir 		break;
294cdf0e10cSrcweir 	}
295cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_METHOD:
296cdf0e10cSrcweir 	{
297cdf0e10cSrcweir 		// is METHOD
298cdf0e10cSrcweir 		switch (nVtableCall)
299cdf0e10cSrcweir 		{
300cdf0e10cSrcweir 		case 1: // acquire()
301cdf0e10cSrcweir 			pCppI->acquireProxy(); // non virtual call!
302cdf0e10cSrcweir 			eRet = typelib_TypeClass_VOID;
303cdf0e10cSrcweir 			break;
304cdf0e10cSrcweir 		case 2: // release()
305cdf0e10cSrcweir 			pCppI->releaseProxy(); // non virtual call!
306cdf0e10cSrcweir 			eRet = typelib_TypeClass_VOID;
307cdf0e10cSrcweir 			break;
308cdf0e10cSrcweir 		case 0: // queryInterface() opt
309cdf0e10cSrcweir 		{
310cdf0e10cSrcweir 			typelib_TypeDescription * pTD = 0;
311cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[3] )->getTypeLibType() );
312cdf0e10cSrcweir 			if (pTD)
313cdf0e10cSrcweir 			{
314cdf0e10cSrcweir                 XInterface * pInterface = 0;
315cdf0e10cSrcweir                 (*pCppI->pBridge->pCppEnv->getRegisteredInterface)(
316cdf0e10cSrcweir                     pCppI->pBridge->pCppEnv,
317cdf0e10cSrcweir                     (void **)&pInterface, pCppI->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
318cdf0e10cSrcweir 
319cdf0e10cSrcweir                 if (pInterface)
320cdf0e10cSrcweir                 {
321cdf0e10cSrcweir                     ::uno_any_construct(
322cdf0e10cSrcweir                         reinterpret_cast< uno_Any * >( pCallStack[1] ),
323cdf0e10cSrcweir                         &pInterface, pTD, cpp_acquire );
324cdf0e10cSrcweir                     pInterface->release();
325cdf0e10cSrcweir                     TYPELIB_DANGER_RELEASE( pTD );
326cdf0e10cSrcweir                     *(void **)pRegisterReturn = pCallStack[1];
327cdf0e10cSrcweir                     eRet = typelib_TypeClass_ANY;
328cdf0e10cSrcweir                     break;
329cdf0e10cSrcweir                 }
330cdf0e10cSrcweir                 TYPELIB_DANGER_RELEASE( pTD );
331cdf0e10cSrcweir             }
332cdf0e10cSrcweir 		} // else perform queryInterface()
333cdf0e10cSrcweir 		default:
334cdf0e10cSrcweir 			eRet = cpp2uno_call(
335cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
336cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
337cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
338cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
339cdf0e10cSrcweir 				pCallStack, pRegisterReturn );
340cdf0e10cSrcweir 		}
341cdf0e10cSrcweir 		break;
342cdf0e10cSrcweir 	}
343cdf0e10cSrcweir 	default:
344cdf0e10cSrcweir 	{
345cdf0e10cSrcweir 		throw RuntimeException(
346cdf0e10cSrcweir             OUString::createFromAscii("no member description found!"),
347cdf0e10cSrcweir             (XInterface *)pCppI );
348cdf0e10cSrcweir 		// is here for dummy
349cdf0e10cSrcweir 		eRet = typelib_TypeClass_VOID;
350cdf0e10cSrcweir 	}
351cdf0e10cSrcweir 	}
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 	return eRet;
354cdf0e10cSrcweir }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir //==================================================================================================
357cdf0e10cSrcweir /**
358cdf0e10cSrcweir  * is called on incoming vtable calls
359cdf0e10cSrcweir  * (called by asm snippets)
360cdf0e10cSrcweir  */
361cdf0e10cSrcweir static void cpp_vtable_call( int nTableEntry, void** pCallStack ) __attribute__((regparm(2)));
362cdf0e10cSrcweir 
cpp_vtable_call(int nTableEntry,void ** pCallStack)363cdf0e10cSrcweir void cpp_vtable_call( int nTableEntry, void** pCallStack )
364cdf0e10cSrcweir {
365cdf0e10cSrcweir 	volatile long nRegReturn[2];
366cdf0e10cSrcweir 	typelib_TypeClass aType = cpp_mediate( nTableEntry, pCallStack, (sal_Int64*)nRegReturn );
367cdf0e10cSrcweir 
368cdf0e10cSrcweir 	switch( aType )
369cdf0e10cSrcweir 	{
370cdf0e10cSrcweir 		case typelib_TypeClass_HYPER:
371cdf0e10cSrcweir 		case typelib_TypeClass_UNSIGNED_HYPER:
372cdf0e10cSrcweir 			__asm__( "movl %1, %%edx\n\t"
373cdf0e10cSrcweir 					 "movl %0, %%eax\n"
374cdf0e10cSrcweir 					 : : "m"(nRegReturn[0]), "m"(nRegReturn[1]) );
375cdf0e10cSrcweir 			break;
376cdf0e10cSrcweir 		case typelib_TypeClass_FLOAT:
377cdf0e10cSrcweir 			__asm__( "flds %0\n\t"
378cdf0e10cSrcweir 					 "fstp %%st(0)\n\t"
379cdf0e10cSrcweir 					 "flds %0\n"
380cdf0e10cSrcweir 					 : : "m"(*(float *)nRegReturn) );
381cdf0e10cSrcweir 			break;
382cdf0e10cSrcweir 		case typelib_TypeClass_DOUBLE:
383cdf0e10cSrcweir 			__asm__( "fldl %0\n\t"
384cdf0e10cSrcweir 					 "fstp %%st(0)\n\t"
385cdf0e10cSrcweir 					 "fldl %0\n"
386cdf0e10cSrcweir 					 : : "m"(*(double *)nRegReturn) );
387cdf0e10cSrcweir 			break;
388cdf0e10cSrcweir // 		case typelib_TypeClass_UNSIGNED_SHORT:
389cdf0e10cSrcweir // 		case typelib_TypeClass_SHORT:
390cdf0e10cSrcweir // 			__asm__( "movswl %0, %%eax\n"
391cdf0e10cSrcweir // 					 : : "m"(nRegReturn) );
392cdf0e10cSrcweir // 		break;
393cdf0e10cSrcweir 		default:
394cdf0e10cSrcweir 			__asm__( "movl %0, %%eax\n"
395cdf0e10cSrcweir 					 : : "m"(nRegReturn[0]) );
396cdf0e10cSrcweir 			break;
397cdf0e10cSrcweir 	}
398cdf0e10cSrcweir }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 
401cdf0e10cSrcweir //==================================================================================================
402cdf0e10cSrcweir class MediateClassData
403cdf0e10cSrcweir {
404cdf0e10cSrcweir     typedef ::std::hash_map< OUString, void *, OUStringHash > t_classdata_map;
405cdf0e10cSrcweir 	t_classdata_map m_map;
406cdf0e10cSrcweir 	Mutex m_mutex;
407cdf0e10cSrcweir 
408cdf0e10cSrcweir public:
409cdf0e10cSrcweir 	void const * get_vtable( typelib_InterfaceTypeDescription * pTD ) SAL_THROW( () );
410cdf0e10cSrcweir 
411cdf0e10cSrcweir 	inline MediateClassData() SAL_THROW( () )
412cdf0e10cSrcweir         {}
413cdf0e10cSrcweir 	~MediateClassData() SAL_THROW( () );
414cdf0e10cSrcweir };
415cdf0e10cSrcweir //__________________________________________________________________________________________________
~MediateClassData()416cdf0e10cSrcweir MediateClassData::~MediateClassData() SAL_THROW( () )
417cdf0e10cSrcweir {
418cdf0e10cSrcweir 	OSL_TRACE( "> calling ~MediateClassData(): freeing mediate vtables." );
419cdf0e10cSrcweir 
420cdf0e10cSrcweir 	for ( t_classdata_map::const_iterator iPos( m_map.begin() ); iPos != m_map.end(); ++iPos )
421cdf0e10cSrcweir 	{
422cdf0e10cSrcweir 		::rtl_freeMemory( iPos->second );
423cdf0e10cSrcweir 	}
424cdf0e10cSrcweir }
425cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
codeSnippet(char * code,sal_uInt32 vtable_pos,bool simple_ret_type)426cdf0e10cSrcweir static inline void codeSnippet( char * code, sal_uInt32 vtable_pos, bool simple_ret_type ) SAL_THROW( () )
427cdf0e10cSrcweir {
428cdf0e10cSrcweir     if (! simple_ret_type)
429cdf0e10cSrcweir         vtable_pos |= 0x80000000;
430cdf0e10cSrcweir     OSL_ASSERT( sizeof (long) == 4 );
431cdf0e10cSrcweir     // mov $nPos, %eax
432cdf0e10cSrcweir     *code++ = 0xb8;
433cdf0e10cSrcweir     *(long *)code = vtable_pos;
434cdf0e10cSrcweir     code += sizeof (long);
435cdf0e10cSrcweir     // mov %esp, %edx
436cdf0e10cSrcweir     *code++ = 0x89;
437cdf0e10cSrcweir     *code++ = 0xe2;
438cdf0e10cSrcweir     // jmp cpp_vtable_call
439cdf0e10cSrcweir     *code++ = 0xe9;
440cdf0e10cSrcweir     *(long *)code = ((char *)cpp_vtable_call) - code - sizeof (long);
441cdf0e10cSrcweir }
442cdf0e10cSrcweir //__________________________________________________________________________________________________
get_vtable(typelib_InterfaceTypeDescription * pTD)443cdf0e10cSrcweir void const * MediateClassData::get_vtable( typelib_InterfaceTypeDescription * pTD ) SAL_THROW( () )
444cdf0e10cSrcweir {
445cdf0e10cSrcweir     void * buffer;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir     // avoiding locked counts
448cdf0e10cSrcweir     OUString const & unoName = *(OUString const *)&((typelib_TypeDescription *)pTD)->pTypeName;
449cdf0e10cSrcweir     {
450cdf0e10cSrcweir 	MutexGuard aGuard( m_mutex );
451cdf0e10cSrcweir 	t_classdata_map::const_iterator iFind( m_map.find( unoName ) );
452cdf0e10cSrcweir 	if (iFind == m_map.end())
453cdf0e10cSrcweir     {
454cdf0e10cSrcweir         // create new vtable
455cdf0e10cSrcweir         sal_Int32 nSlots = pTD->nMapFunctionIndexToMemberIndex;
456cdf0e10cSrcweir         buffer = ::rtl_allocateMemory( ((2+ nSlots) * sizeof (void *)) + (nSlots *20) );
457cdf0e10cSrcweir 
458cdf0e10cSrcweir         ::std::pair< t_classdata_map::iterator, bool > insertion(
459cdf0e10cSrcweir             m_map.insert( t_classdata_map::value_type( unoName, buffer ) ) );
460cdf0e10cSrcweir         OSL_ENSURE( insertion.second, "### inserting new vtable buffer failed?!" );
461cdf0e10cSrcweir 
462cdf0e10cSrcweir         void ** slots = (void **)buffer;
463cdf0e10cSrcweir         *slots++ = 0;
464cdf0e10cSrcweir         *slots++ = 0; // rtti
465cdf0e10cSrcweir         char * code = (char *)(slots + nSlots);
466cdf0e10cSrcweir 
467cdf0e10cSrcweir         sal_uInt32 vtable_pos = 0;
468cdf0e10cSrcweir         sal_Int32 nAllMembers = pTD->nAllMembers;
469cdf0e10cSrcweir         typelib_TypeDescriptionReference ** ppAllMembers = pTD->ppAllMembers;
470cdf0e10cSrcweir         for ( sal_Int32 nPos = 0; nPos < nAllMembers; ++nPos )
471cdf0e10cSrcweir         {
472cdf0e10cSrcweir             typelib_TypeDescription * pTD = 0;
473cdf0e10cSrcweir             TYPELIB_DANGER_GET( &pTD, ppAllMembers[ nPos ] );
474cdf0e10cSrcweir             OSL_ASSERT( pTD );
475cdf0e10cSrcweir             if (typelib_TypeClass_INTERFACE_ATTRIBUTE == pTD->eTypeClass)
476cdf0e10cSrcweir             {
477cdf0e10cSrcweir                 bool simple_ret = cppu_isSimpleType(
478cdf0e10cSrcweir                     ((typelib_InterfaceAttributeTypeDescription *)pTD)->pAttributeTypeRef->eTypeClass );
479cdf0e10cSrcweir                 // get method
480cdf0e10cSrcweir                 *slots++ = code;
481cdf0e10cSrcweir                 codeSnippet( code, vtable_pos++, simple_ret );
482cdf0e10cSrcweir                 code += 20;
483cdf0e10cSrcweir                 if (! ((typelib_InterfaceAttributeTypeDescription *)pTD)->bReadOnly)
484cdf0e10cSrcweir                 {
485cdf0e10cSrcweir                     // set method
486cdf0e10cSrcweir                     *slots++ = code;
487cdf0e10cSrcweir                     codeSnippet( code, vtable_pos++, true );
488cdf0e10cSrcweir                     code += 20;
489cdf0e10cSrcweir                 }
490cdf0e10cSrcweir             }
491cdf0e10cSrcweir             else
492cdf0e10cSrcweir             {
493cdf0e10cSrcweir                 bool simple_ret = cppu_isSimpleType(
494cdf0e10cSrcweir                     ((typelib_InterfaceMethodTypeDescription *)pTD)->pReturnTypeRef->eTypeClass );
495cdf0e10cSrcweir                 *slots++ = code;
496cdf0e10cSrcweir                 codeSnippet( code, vtable_pos++, simple_ret );
497cdf0e10cSrcweir                 code += 20;
498cdf0e10cSrcweir             }
499cdf0e10cSrcweir             TYPELIB_DANGER_RELEASE( pTD );
500cdf0e10cSrcweir         }
501cdf0e10cSrcweir         OSL_ASSERT( vtable_pos == nSlots );
502cdf0e10cSrcweir     }
503cdf0e10cSrcweir     else
504cdf0e10cSrcweir     {
505cdf0e10cSrcweir         buffer = iFind->second;
506cdf0e10cSrcweir     }
507cdf0e10cSrcweir     }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir     return ((void **)buffer +2);
510cdf0e10cSrcweir }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir //==================================================================================================
cppu_cppInterfaceProxy_patchVtable(XInterface * pCppI,typelib_InterfaceTypeDescription * pTypeDescr)513cdf0e10cSrcweir void SAL_CALL cppu_cppInterfaceProxy_patchVtable(
514cdf0e10cSrcweir 	XInterface * pCppI, typelib_InterfaceTypeDescription * pTypeDescr ) throw ()
515cdf0e10cSrcweir {
516cdf0e10cSrcweir 	static MediateClassData * s_pMediateClassData = 0;
517cdf0e10cSrcweir 	if (! s_pMediateClassData)
518cdf0e10cSrcweir 	{
519cdf0e10cSrcweir 		MutexGuard aGuard( Mutex::getGlobalMutex() );
520cdf0e10cSrcweir 		if (! s_pMediateClassData)
521cdf0e10cSrcweir 		{
522cdf0e10cSrcweir #ifdef LEAK_STATIC_DATA
523cdf0e10cSrcweir 			s_pMediateClassData = new MediateClassData();
524cdf0e10cSrcweir #else
525cdf0e10cSrcweir 			static MediateClassData s_aMediateClassData;
526cdf0e10cSrcweir 			s_pMediateClassData = &s_aMediateClassData;
527cdf0e10cSrcweir #endif
528cdf0e10cSrcweir 		}
529cdf0e10cSrcweir 	}
530cdf0e10cSrcweir 	*(void const **)pCppI = s_pMediateClassData->get_vtable( pTypeDescr );
531cdf0e10cSrcweir }
532cdf0e10cSrcweir 
533cdf0e10cSrcweir }
534cdf0e10cSrcweir 
535cdf0e10cSrcweir extern "C"
536cdf0e10cSrcweir {
537cdf0e10cSrcweir //##################################################################################################
component_canUnload(TimeValue * pTime)538cdf0e10cSrcweir sal_Bool SAL_CALL component_canUnload( TimeValue * pTime )
539cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
540cdf0e10cSrcweir {
541cdf0e10cSrcweir 	return CPPU_CURRENT_NAMESPACE::g_moduleCount.canUnload(
542cdf0e10cSrcweir         &CPPU_CURRENT_NAMESPACE::g_moduleCount, pTime );
543cdf0e10cSrcweir }
544cdf0e10cSrcweir //##################################################################################################
uno_initEnvironment(uno_Environment * pCppEnv)545cdf0e10cSrcweir void SAL_CALL uno_initEnvironment( uno_Environment * pCppEnv )
546cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
547cdf0e10cSrcweir {
548cdf0e10cSrcweir 	CPPU_CURRENT_NAMESPACE::cppu_cppenv_initEnvironment(
549cdf0e10cSrcweir         pCppEnv );
550cdf0e10cSrcweir }
551cdf0e10cSrcweir //##################################################################################################
uno_ext_getMapping(uno_Mapping ** ppMapping,uno_Environment * pFrom,uno_Environment * pTo)552cdf0e10cSrcweir void SAL_CALL uno_ext_getMapping(
553cdf0e10cSrcweir 	uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo )
554cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
555cdf0e10cSrcweir {
556cdf0e10cSrcweir 	CPPU_CURRENT_NAMESPACE::cppu_ext_getMapping(
557cdf0e10cSrcweir         ppMapping, pFrom, pTo );
558cdf0e10cSrcweir }
559cdf0e10cSrcweir }
560