1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_cli_ure.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <sal/alloca.h> 32*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx" 33*cdf0e10cSrcweir #include "cli_base.h" 34*cdf0e10cSrcweir #include "cli_bridge.h" 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir namespace sr=System::Reflection; 37*cdf0e10cSrcweir namespace css=com::sun::star; 38*cdf0e10cSrcweir using namespace rtl; 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir namespace cli_uno 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir union largest 44*cdf0e10cSrcweir { 45*cdf0e10cSrcweir sal_Int64 n; 46*cdf0e10cSrcweir double d; 47*cdf0e10cSrcweir void * p; 48*cdf0e10cSrcweir uno_Any a; 49*cdf0e10cSrcweir }; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir System::Object* Bridge::call_uno(uno_Interface * pUnoI, 52*cdf0e10cSrcweir typelib_TypeDescription* member_td, 53*cdf0e10cSrcweir typelib_TypeDescriptionReference * return_type, 54*cdf0e10cSrcweir sal_Int32 nParams, typelib_MethodParameter const * pParams, 55*cdf0e10cSrcweir System::Object * args[], System::Type* argTypes[], 56*cdf0e10cSrcweir System::Object** ppExc) const 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir // return mem 59*cdf0e10cSrcweir sal_Int32 return_size = sizeof (largest); 60*cdf0e10cSrcweir if ((0 != return_type) && 61*cdf0e10cSrcweir (typelib_TypeClass_STRUCT == return_type->eTypeClass || 62*cdf0e10cSrcweir typelib_TypeClass_EXCEPTION == return_type->eTypeClass)) 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir TypeDescr return_td( return_type ); 65*cdf0e10cSrcweir if (return_td.get()->nSize > sizeof (largest)) 66*cdf0e10cSrcweir return_size = return_td.get()->nSize; 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir //Prepare memory that contains all converted arguments and return valuse 69*cdf0e10cSrcweir //The memory block contains first pointers to the arguments which are in the same block 70*cdf0e10cSrcweir // For example, 2 arguments, 1 ret. 71*cdf0e10cSrcweir // 72*cdf0e10cSrcweir // | Pointer 73*cdf0e10cSrcweir // | Pointer 74*cdf0e10cSrcweir // | Return value 75*cdf0e10cSrcweir // | Arg 1 76*cdf0e10cSrcweir // | Arg 2 77*cdf0e10cSrcweir // 78*cdf0e10cSrcweir // If an argument is larger then union largest, such as some structures, then the pointer 79*cdf0e10cSrcweir // points to an extra block of memory. The same goes for a big return value. 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir char * mem = (char *)alloca( 82*cdf0e10cSrcweir (nParams * sizeof (void *)) + return_size + (nParams * sizeof (largest)) ); 83*cdf0e10cSrcweir //array of pointers to args 84*cdf0e10cSrcweir void ** uno_args = (void **)mem; 85*cdf0e10cSrcweir //If an attribute is set, then uno_ret must be null, e.g void setAttribute(int ) 86*cdf0e10cSrcweir void * uno_ret= NULL; 87*cdf0e10cSrcweir if ( !(member_td->eTypeClass == typelib_TypeClass_INTERFACE_ATTRIBUTE && nParams == 1)) 88*cdf0e10cSrcweir uno_ret = (mem + (nParams * sizeof (void *))); 89*cdf0e10cSrcweir largest * uno_args_mem = (largest *)(mem + (nParams * sizeof (void *)) + return_size); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir OSL_ASSERT( (0 == nParams) || (nParams == args->get_Length()) ); 92*cdf0e10cSrcweir for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos ) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir typelib_MethodParameter const & param = pParams[ nPos ]; 95*cdf0e10cSrcweir typelib_TypeDescriptionReference * type = param.pTypeRef; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir uno_args[ nPos ] = &uno_args_mem[ nPos ]; 98*cdf0e10cSrcweir if (typelib_TypeClass_STRUCT == type->eTypeClass || 99*cdf0e10cSrcweir typelib_TypeClass_EXCEPTION == type->eTypeClass) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir TypeDescr td( type ); 102*cdf0e10cSrcweir if (td.get()->nSize > sizeof (largest)) 103*cdf0e10cSrcweir uno_args[ nPos ] = alloca( td.get()->nSize ); 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir if (param.bIn) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir try 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir // in, in/out params 111*cdf0e10cSrcweir map_to_uno( 112*cdf0e10cSrcweir uno_args[ nPos ],args[nPos] , type, false /* no assign */); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir catch (...) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir // cleanup uno in args 117*cdf0e10cSrcweir for (sal_Int32 n = 0; n < nPos; ++n) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir typelib_MethodParameter const & param = pParams[n]; 120*cdf0e10cSrcweir if (param.bIn) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir uno_type_destructData(uno_args[n], param.pTypeRef, 0); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir throw; 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir uno_Any uno_exc_holder; 130*cdf0e10cSrcweir uno_Any * uno_exc = &uno_exc_holder; 131*cdf0e10cSrcweir // call binary uno 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir (*pUnoI->pDispatcher)( pUnoI, member_td, uno_ret, uno_args, &uno_exc ); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir if (0 == uno_exc) 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir // convert out args; destruct uno args 138*cdf0e10cSrcweir for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir typelib_MethodParameter const & param = pParams[ nPos ]; 141*cdf0e10cSrcweir typelib_TypeDescriptionReference * type = param.pTypeRef; 142*cdf0e10cSrcweir if (param.bOut) 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir try 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir map_to_cli( 147*cdf0e10cSrcweir &args[nPos], uno_args[nPos], param.pTypeRef, 148*cdf0e10cSrcweir argTypes != NULL ? argTypes[nPos] : NULL, false ); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir catch (...) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir // cleanup further uno args 153*cdf0e10cSrcweir for ( sal_Int32 n = nPos; n < nParams; ++n ) 154*cdf0e10cSrcweir { 155*cdf0e10cSrcweir uno_type_destructData( uno_args[n], pParams[n].pTypeRef, 0 ); 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir // cleanup uno return value 158*cdf0e10cSrcweir uno_type_destructData( uno_ret, return_type, 0 ); 159*cdf0e10cSrcweir throw; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir //cleanup args 163*cdf0e10cSrcweir if (typelib_TypeClass_DOUBLE < type->eTypeClass && 164*cdf0e10cSrcweir typelib_TypeClass_ENUM != type->eTypeClass) // opt 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir uno_type_destructData(uno_args[nPos], type, 0); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir if ((0 != return_type) && 171*cdf0e10cSrcweir (typelib_TypeClass_VOID != return_type->eTypeClass)) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir // convert uno return value 174*cdf0e10cSrcweir try 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir System::Object* cli_ret; 177*cdf0e10cSrcweir map_to_cli( 178*cdf0e10cSrcweir &cli_ret, uno_ret, return_type, 0, false); 179*cdf0e10cSrcweir uno_type_destructData(uno_ret, return_type, 0); 180*cdf0e10cSrcweir return cli_ret; 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir catch (...) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir uno_type_destructData(uno_ret, return_type, 0); 185*cdf0e10cSrcweir throw; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir return 0; // void return 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir else // exception occured 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir // destruct uno in args 193*cdf0e10cSrcweir for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos ) 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir typelib_MethodParameter const & param = pParams[ nPos ]; 196*cdf0e10cSrcweir if (param.bIn) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir uno_type_destructData( uno_args[ nPos ], param.pTypeRef, 0 ); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir map_to_cli(ppExc, uno_exc_holder.pData, 202*cdf0e10cSrcweir uno_exc_holder.pType, NULL, false); 203*cdf0e10cSrcweir return 0; 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir void Bridge::call_cli( 208*cdf0e10cSrcweir System::Object* cliI, 209*cdf0e10cSrcweir sr::MethodInfo* method, 210*cdf0e10cSrcweir typelib_TypeDescriptionReference * return_type, 211*cdf0e10cSrcweir typelib_MethodParameter * params, int nParams, 212*cdf0e10cSrcweir void * uno_ret, void * uno_args [], uno_Any ** uno_exc ) const 213*cdf0e10cSrcweir { 214*cdf0e10cSrcweir System::Object *args[]= new System::Object*[nParams]; 215*cdf0e10cSrcweir for (int nPos= 0; nPos < nParams; nPos++) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir typelib_MethodParameter const & param= params[nPos]; 218*cdf0e10cSrcweir if (param.bIn) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir map_to_cli( &args[nPos], 221*cdf0e10cSrcweir uno_args[nPos], param.pTypeRef, 0, false); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir System::Object* retInvoke= NULL; 225*cdf0e10cSrcweir try 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir retInvoke= method->Invoke(cliI, args); 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir catch (sr::TargetInvocationException* e) 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir System::Exception* exc= e->get_InnerException(); 232*cdf0e10cSrcweir css::uno::TypeDescription td(mapCliType(exc->GetType())); 233*cdf0e10cSrcweir // memory for exception 234*cdf0e10cSrcweir std::auto_ptr< rtl_mem > memExc(rtl_mem::allocate(td.get()->nSize)); 235*cdf0e10cSrcweir map_to_uno(memExc.get(), exc, td.get()->pWeakRef, false); 236*cdf0e10cSrcweir (*uno_exc)->pType= td.get()->pWeakRef; 237*cdf0e10cSrcweir (*uno_exc)->pData= memExc.release(); 238*cdf0e10cSrcweir return; 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir catch (System::Exception* e) 241*cdf0e10cSrcweir { 242*cdf0e10cSrcweir OUStringBuffer buf( 128 ); 243*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( 244*cdf0e10cSrcweir "Unexspected exception during invocation of cli object. " 245*cdf0e10cSrcweir "Original message is: \n") ); 246*cdf0e10cSrcweir buf.append(mapCliString(e->get_Message())); 247*cdf0e10cSrcweir throw BridgeRuntimeError( buf.makeStringAndClear() ); 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir //convert out, in/out params 251*cdf0e10cSrcweir for (int nPos = 0; nPos < nParams; ++nPos ) 252*cdf0e10cSrcweir { 253*cdf0e10cSrcweir typelib_MethodParameter const & param = params[ nPos ]; 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir if (param.bOut) 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir try 258*cdf0e10cSrcweir { 259*cdf0e10cSrcweir map_to_uno( 260*cdf0e10cSrcweir uno_args[ nPos ], args[ nPos ], param.pTypeRef, 261*cdf0e10cSrcweir sal_True == param.bIn /* assign if inout */); 262*cdf0e10cSrcweir // out array 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir catch (...) 265*cdf0e10cSrcweir { 266*cdf0e10cSrcweir // cleanup uno pure out 267*cdf0e10cSrcweir for ( sal_Int32 n = 0; n < nPos; ++n ) 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir typelib_MethodParameter const & param = params[ n ]; 270*cdf0e10cSrcweir if (! param.bIn) 271*cdf0e10cSrcweir uno_type_destructData( uno_args[ n ], param.pTypeRef, 0 ); 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir throw; 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir } 276*cdf0e10cSrcweir } 277*cdf0e10cSrcweir // return value 278*cdf0e10cSrcweir if (0 != return_type) 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir map_to_uno( 281*cdf0e10cSrcweir uno_ret, retInvoke, return_type, false /* no assign */); 282*cdf0e10cSrcweir } 283*cdf0e10cSrcweir // no exception occured 284*cdf0e10cSrcweir *uno_exc = 0; 285*cdf0e10cSrcweir } 286*cdf0e10cSrcweir 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir 290*cdf0e10cSrcweir } 291