1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 #include <sal/alloca.h>
33 #include <com/sun/star/uno/genfunc.hxx>
34 #include "com/sun/star/uno/RuntimeException.hpp"
37 #include "bridges/cpp_uno/shared/bridge.hxx"
38 #include "bridges/cpp_uno/shared/types.hxx"
39 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
40 #include "bridges/cpp_uno/shared/vtables.hxx"
44 using namespace ::rtl
;
45 using namespace ::com::sun::star::uno
;
50 //==================================================================================================
51 static void callVirtualMethod(
52 void * pAdjustedThisPtr
,
53 sal_Int32 nVtableIndex
,
54 void * pRegisterReturn
,
55 typelib_TypeClass eReturnType
,
56 sal_Int32
* pStackLongs
,
57 sal_Int32 nStackLongs
)
59 // parameter list is mixed list of * and values
60 // reference parameters are pointers
62 OSL_ENSURE( pStackLongs
&& pAdjustedThisPtr
, "### null ptr!" );
63 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32
) == 4), "### unexpected size of int!" );
64 OSL_ENSURE( nStackLongs
&& pStackLongs
, "### no stack in callVirtualMethod !" );
67 if (! pAdjustedThisPtr
) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
69 volatile long edx
= 0, eax
= 0; // for register returns
75 "mov %%eax, %%edx\n\t"
86 "mov 0(%%edx), %%edx\n\t"
89 "add %%eax, %%edx\n\t"
90 "mov 0(%%edx), %%edx\n\t"
92 // save return registers
98 : "m"(nStackLongs
), "m"(pStackLongs
), "m"(pAdjustedThisPtr
),
99 "m"(nVtableIndex
), "m"(eax
), "m"(edx
), "m"(stackptr
)
101 switch( eReturnType
)
103 case typelib_TypeClass_HYPER
:
104 case typelib_TypeClass_UNSIGNED_HYPER
:
105 ((long*)pRegisterReturn
)[1] = edx
;
106 case typelib_TypeClass_LONG
:
107 case typelib_TypeClass_UNSIGNED_LONG
:
108 case typelib_TypeClass_CHAR
:
109 case typelib_TypeClass_ENUM
:
110 ((long*)pRegisterReturn
)[0] = eax
;
112 case typelib_TypeClass_SHORT
:
113 case typelib_TypeClass_UNSIGNED_SHORT
:
114 *(unsigned short*)pRegisterReturn
= eax
;
116 case typelib_TypeClass_BOOLEAN
:
117 case typelib_TypeClass_BYTE
:
118 *(unsigned char*)pRegisterReturn
= eax
;
120 case typelib_TypeClass_FLOAT
:
121 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn
) );
123 case typelib_TypeClass_DOUBLE
:
124 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn
) );
129 //==================================================================================================
130 static void cpp_call(
131 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
132 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
133 typelib_TypeDescriptionReference
* pReturnTypeRef
,
134 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
135 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
137 // max space for: [complex ret ptr], values|ptr ...
139 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
140 char * pCppStackStart
= pCppStack
;
143 typelib_TypeDescription
* pReturnTypeDescr
= 0;
144 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
145 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
147 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
149 if (pReturnTypeDescr
)
151 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
153 pCppReturn
= pUnoReturn
; // direct way for simple types
157 // complex return via ptr
158 pCppReturn
= *(void **)pCppStack
159 = (bridges::cpp_uno::shared::relatesToInterfaceType(
161 ? alloca( pReturnTypeDescr
->nSize
)
162 : pUnoReturn
); // direct way
163 pCppStack
+= sizeof(void *);
167 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
168 + aVtableSlot
.offset
;
169 *(void**)pCppStack
= pAdjustedThisPtr
;
170 pCppStack
+= sizeof( void* );
173 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
175 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
176 // indizes of values this have to be converted (interface conversion cpp<=>uno)
177 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
178 // type descriptions for reconversions
179 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
181 sal_Int32 nTempIndizes
= 0;
183 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
185 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
186 typelib_TypeDescription
* pParamTypeDescr
= 0;
187 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
190 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
192 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
193 pThis
->getBridge()->getUno2Cpp() );
195 switch (pParamTypeDescr
->eTypeClass
)
197 case typelib_TypeClass_HYPER
:
198 case typelib_TypeClass_UNSIGNED_HYPER
:
199 case typelib_TypeClass_DOUBLE
:
200 pCppStack
+= sizeof(sal_Int32
); // extra long
203 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
205 else // ptr to complex value | ref
207 if (! rParam
.bIn
) // is pure out
209 // cpp out is constructed mem, uno out is not!
211 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
213 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
214 // will be released at reconversion
215 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
218 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
221 uno_copyAndConvertData(
222 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
223 pUnoArgs
[nPos
], pParamTypeDescr
,
224 pThis
->getBridge()->getUno2Cpp() );
226 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
227 // will be released at reconversion
228 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
232 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
234 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
237 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
242 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
244 pAdjustedThisPtr
, aVtableSlot
.index
,
245 pCppReturn
, pReturnTypeDescr
->eTypeClass
,
246 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
247 // NO exception occurred...
250 // reconvert temporary params
251 for ( ; nTempIndizes
--; )
253 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
254 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
256 if (pParams
[nIndex
].bIn
)
258 if (pParams
[nIndex
].bOut
) // inout
260 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
261 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
262 pThis
->getBridge()->getCpp2Uno() );
267 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
268 pThis
->getBridge()->getCpp2Uno() );
270 // destroy temp cpp param => cpp: every param was constructed
271 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
273 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
276 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
278 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
279 pThis
->getBridge()->getCpp2Uno() );
280 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
285 // fill uno exception
286 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
, *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
289 for ( ; nTempIndizes
--; )
291 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
292 // destroy temp cpp param => cpp: every param was constructed
293 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
294 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
297 if (pReturnTypeDescr
)
298 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
304 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
306 void unoInterfaceProxyDispatch(
307 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
308 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
311 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
312 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
313 typelib_InterfaceTypeDescription
* pTypeDescr
= pThis
->pTypeDescr
;
315 switch (pMemberDescr
->eTypeClass
)
317 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
319 VtableSlot
aVtableSlot(
322 typelib_InterfaceAttributeTypeDescription
const * >(
326 // dependent dispatch
329 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
331 pReturn
, pArgs
, ppException
);
336 typelib_MethodParameter aParam
;
338 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
339 aParam
.bIn
= sal_True
;
340 aParam
.bOut
= sal_False
;
342 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
343 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
344 typelib_typedescriptionreference_new(
345 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
347 // dependent dispatch
348 aVtableSlot
.index
+= 1; // get, then set method
353 pReturn
, pArgs
, ppException
);
355 typelib_typedescriptionreference_release( pReturnTypeRef
);
360 case typelib_TypeClass_INTERFACE_METHOD
:
362 VtableSlot
aVtableSlot(
365 typelib_InterfaceMethodTypeDescription
const * >(
367 switch (aVtableSlot
.index
)
370 case 1: // acquire uno interface
371 (*pUnoI
->acquire
)( pUnoI
);
374 case 2: // release uno interface
375 (*pUnoI
->release
)( pUnoI
);
378 case 0: // queryInterface() opt
380 typelib_TypeDescription
* pTD
= 0;
381 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
384 uno_Interface
* pInterface
= 0;
385 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
386 pThis
->pBridge
->getUnoEnv(),
387 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
392 reinterpret_cast< uno_Any
* >( pReturn
),
393 &pInterface
, pTD
, 0 );
394 (*pInterface
->release
)( pInterface
);
395 TYPELIB_DANGER_RELEASE( pTD
);
399 TYPELIB_DANGER_RELEASE( pTD
);
401 } // else perform queryInterface()
403 // dependent dispatch
406 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
407 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
408 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
409 pReturn
, pArgs
, ppException
);
415 ::com::sun::star::uno::RuntimeException
aExc(
416 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
417 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
419 Type
const & rExcType
= ::getCppuType( &aExc
);
420 // binary identical null reference
421 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
428 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */