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_extensions.hxx" 30*cdf0e10cSrcweir #include <stdio.h> 31*cdf0e10cSrcweir #include <stardiv/uno/repos/implementationregistration.hxx> 32*cdf0e10cSrcweir #include <stardiv/uno/script/script.hxx> 33*cdf0e10cSrcweir #include <stardiv/uno/beans/exactname.hxx> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <rtl/ustring.hxx> 36*cdf0e10cSrcweir #include <vos/dynload.hxx> 37*cdf0e10cSrcweir #include <vos/diagnose.hxx> 38*cdf0e10cSrcweir #include <usr/services.hxx> 39*cdf0e10cSrcweir #include <vcl/svapp.hxx> 40*cdf0e10cSrcweir #include <usr/ustring.hxx> 41*cdf0e10cSrcweir #include <usr/weak.hxx> 42*cdf0e10cSrcweir #include <tools/string.hxx> 43*cdf0e10cSrcweir #include <vos/conditn.hxx> 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir using namespace rtl; 46*cdf0e10cSrcweir using namespace vos; 47*cdf0e10cSrcweir using namespace usr; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir #define PCHAR_TO_USTRING(x) StringToOUString(String(x),CHARSET_SYSTEM) 50*cdf0e10cSrcweir #define USTRING_TO_PCHAR(x) OUStringToString(x , CHARSET_DONTKNOW ).GetCharStr() 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /***** 57*cdf0e10cSrcweir * 58*cdf0e10cSrcweir * A Test root object ! 59*cdf0e10cSrcweir * 60*cdf0e10cSrcweir *****/ 61*cdf0e10cSrcweir class MyPythonRoot : 62*cdf0e10cSrcweir public XInvokation, 63*cdf0e10cSrcweir public OWeakObject 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir public: 66*cdf0e10cSrcweir MyPythonRoot() { m_iTestValue = 15; } 67*cdf0e10cSrcweir BOOL queryInterface( Uik aUik, XInterfaceRef & rOut ); 68*cdf0e10cSrcweir void acquire() { OWeakObject::acquire(); } 69*cdf0e10cSrcweir void release() { OWeakObject::release(); } 70*cdf0e10cSrcweir void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); } 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir public: 73*cdf0e10cSrcweir XIntrospectionAccessRef getIntrospection(void) const THROWS( (UsrSystemException) ) 74*cdf0e10cSrcweir { return XIntrospectionAccessRef(); } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir UsrAny invoke( const UString& FunctionName, 77*cdf0e10cSrcweir const Sequence< UsrAny >& Params, 78*cdf0e10cSrcweir Sequence< INT16 >& OutParamIndex, 79*cdf0e10cSrcweir Sequence< UsrAny >& OutParam) 80*cdf0e10cSrcweir THROWS( ( IllegalArgumentException, 81*cdf0e10cSrcweir CannotConvertException, 82*cdf0e10cSrcweir InvocationTargetException, 83*cdf0e10cSrcweir UsrSystemException) ); 84*cdf0e10cSrcweir void setValue(const UString& PropertyName, const UsrAny& Value) 85*cdf0e10cSrcweir THROWS( ( UnknownPropertyException, 86*cdf0e10cSrcweir CannotConvertException, 87*cdf0e10cSrcweir InvocationTargetException, 88*cdf0e10cSrcweir UsrSystemException) ); 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir UsrAny getValue(const UString& PropertyName) 91*cdf0e10cSrcweir THROWS( ( UnknownPropertyException, 92*cdf0e10cSrcweir UsrSystemException) ); 93*cdf0e10cSrcweir BOOL hasMethod(const UString& Name) const THROWS( (UsrSystemException) ); 94*cdf0e10cSrcweir BOOL hasProperty(const UString& Name) const THROWS( (UsrSystemException) ); 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir void getTestValueViaInout( int &inout ) 98*cdf0e10cSrcweir { inout = m_iTestValue; } 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir INT32 getTestValue() const 101*cdf0e10cSrcweir { return m_iTestValue; } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir void setTestValue( INT32 i ) 104*cdf0e10cSrcweir { m_iTestValue = i; } 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir void printTestValue() 107*cdf0e10cSrcweir { fprintf( stderr, "TestValue : %d\n" , getTestValue() ); } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir void addTestValue( INT32 i ) 110*cdf0e10cSrcweir { m_iTestValue += i; } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir private: 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir INT32 m_iTestValue; 115*cdf0e10cSrcweir }; 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir BOOL MyPythonRoot::queryInterface( Uik aUik, XInterfaceRef &rOut ) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir if( aUik == XInvokation::getSmartUik() ) { 120*cdf0e10cSrcweir rOut = ( XInvokation * ) this; 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir else { 123*cdf0e10cSrcweir return OWeakObject::queryInterface( aUik , rOut ); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir return TRUE; 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir UsrAny MyPythonRoot::invoke( const UString& FunctionName, 130*cdf0e10cSrcweir const Sequence< UsrAny >& Params, 131*cdf0e10cSrcweir Sequence< INT16 >& OutParamIndex, 132*cdf0e10cSrcweir Sequence< UsrAny >& OutParam) 133*cdf0e10cSrcweir THROWS( ( IllegalArgumentException, 134*cdf0e10cSrcweir CannotConvertException, 135*cdf0e10cSrcweir InvocationTargetException, 136*cdf0e10cSrcweir UsrSystemException) ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir if( L"printTestValue" == FunctionName ) { 139*cdf0e10cSrcweir printTestValue(); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir else if( L"addTestValue" == FunctionName ) { 142*cdf0e10cSrcweir addTestValue( Params.getConstArray()[0].getINT32() ); 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir else if( L"getTestValueViaInout" == FunctionName ) { 145*cdf0e10cSrcweir int i = Params.getConstArray()[0].getINT32(); 146*cdf0e10cSrcweir getTestValueViaInout( i ); 147*cdf0e10cSrcweir OutParam.getArray()[0].setINT32( i ); 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir else { 150*cdf0e10cSrcweir THROW( InvocationTargetException() ); 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir return UsrAny(); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir void MyPythonRoot::setValue(const UString& PropertyName, const UsrAny& Value) 157*cdf0e10cSrcweir THROWS( ( UnknownPropertyException, 158*cdf0e10cSrcweir CannotConvertException, 159*cdf0e10cSrcweir InvocationTargetException, 160*cdf0e10cSrcweir UsrSystemException) ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir if( L"TestValue" == PropertyName ) { 163*cdf0e10cSrcweir setTestValue( Value.getINT32() ); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir else { 166*cdf0e10cSrcweir THROW( UnknownPropertyException() ); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir UsrAny MyPythonRoot::getValue(const UString& PropertyName) 171*cdf0e10cSrcweir THROWS( ( UnknownPropertyException, 172*cdf0e10cSrcweir UsrSystemException) ) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir UsrAny aRet; 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir if( L"TestValue" == PropertyName ) { 177*cdf0e10cSrcweir aRet.setINT32( getTestValue() ); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir else { 180*cdf0e10cSrcweir THROW( UnknownPropertyException() ); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir return aRet; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir BOOL MyPythonRoot::hasMethod(const UString& Name) const THROWS( (UsrSystemException) ) 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir if( L"printTestValue" == Name ) { 190*cdf0e10cSrcweir return TRUE; 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir else if( L"addTestValue" == Name ) { 193*cdf0e10cSrcweir return TRUE; 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir else if( L"getTestValueViaInout" == Name ) { 196*cdf0e10cSrcweir return TRUE; 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir return FALSE; 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir BOOL MyPythonRoot::hasProperty(const UString& Name) const THROWS( (UsrSystemException) ) 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir if( L"TestValue" == Name ) { 205*cdf0e10cSrcweir return TRUE; 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir return FALSE; 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir /***** 213*cdf0e10cSrcweir * 214*cdf0e10cSrcweir * A test engine listener to check the debug interface 215*cdf0e10cSrcweir * 216*cdf0e10cSrcweir *****/ 217*cdf0e10cSrcweir class TestListener : 218*cdf0e10cSrcweir public XEngineListener, 219*cdf0e10cSrcweir public OWeakObject 220*cdf0e10cSrcweir { 221*cdf0e10cSrcweir public: 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir TestListener() 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir m_pDebuggingRef = 0; 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir TestListener( XDebuggingRef *p ) 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir attach( p ); 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir ~TestListener() 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir if( m_pDebuggingRef ) { 237*cdf0e10cSrcweir detach(); 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir BOOL queryInterface( Uik aUik, XInterfaceRef & rOut ); 242*cdf0e10cSrcweir void acquire() { OWeakObject::acquire(); } 243*cdf0e10cSrcweir void release() { OWeakObject::release(); } 244*cdf0e10cSrcweir void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); } 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir void attach( XDebuggingRef *p ) 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir m_pDebuggingRef = p; 250*cdf0e10cSrcweir } 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir void detach( ); 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir virtual void disposing( const EventObject &o ) 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir if( m_pDebuggingRef ) { 258*cdf0e10cSrcweir detach(); 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) ) 262*cdf0e10cSrcweir { 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) ) 266*cdf0e10cSrcweir { 267*cdf0e10cSrcweir (*m_pDebuggingRef)->stop(); 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir m_aDebugCondition.set(); 270*cdf0e10cSrcweir } 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) ) 273*cdf0e10cSrcweir { 274*cdf0e10cSrcweir m_aDebugCondition.set(); 275*cdf0e10cSrcweir } 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir void cmdLine(); 279*cdf0e10cSrcweir protected: 280*cdf0e10cSrcweir 281*cdf0e10cSrcweir OCondition m_aDebugCondition; 282*cdf0e10cSrcweir XDebuggingRef *m_pDebuggingRef; 283*cdf0e10cSrcweir }; 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir 287*cdf0e10cSrcweir void TestListener::cmdLine() 288*cdf0e10cSrcweir { 289*cdf0e10cSrcweir // Condition is set by running listener 290*cdf0e10cSrcweir m_aDebugCondition.wait(); 291*cdf0e10cSrcweir m_aDebugCondition.reset(); 292*cdf0e10cSrcweir (*m_pDebuggingRef)->doContinue(); 293*cdf0e10cSrcweir m_aDebugCondition.wait(); 294*cdf0e10cSrcweir } 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir void TestListener::detach() 297*cdf0e10cSrcweir { 298*cdf0e10cSrcweir OSL_ASSERT( m_pDebuggingRef ); 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir m_pDebuggingRef = 0; 301*cdf0e10cSrcweir } 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir BOOL TestListener::queryInterface( Uik aUik, XInterfaceRef & rOut ) 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir if( aUik == XEngineListener::getSmartUik() ) 306*cdf0e10cSrcweir rOut = (XEngineListener*)this; 307*cdf0e10cSrcweir else 308*cdf0e10cSrcweir return OWeakObject::queryInterface( aUik, rOut ); 309*cdf0e10cSrcweir return TRUE; 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir void checkInvokation( const XInvokationRef &xInvoke ) 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir UsrAny anyList; 316*cdf0e10cSrcweir 317*cdf0e10cSrcweir // check exporting an object as an invokation 318*cdf0e10cSrcweir OSL_ASSERT( xInvoke->hasProperty( L"list" ) ); 319*cdf0e10cSrcweir anyList = xInvoke->getValue( L"list" ); 320*cdf0e10cSrcweir 321*cdf0e10cSrcweir OSL_ASSERT( anyList.getReflection() == XInvokation_getReflection() ); 322*cdf0e10cSrcweir XInvokationRef *pRef = ( XInvokationRef * ) anyList.get(); 323*cdf0e10cSrcweir OSL_ASSERT( (*pRef).is() ); 324*cdf0e10cSrcweir 325*cdf0e10cSrcweir OSL_ASSERT( (*pRef)->hasMethod( L"append" ) ); 326*cdf0e10cSrcweir OSL_ASSERT( (*pRef)->hasMethod( L"count" ) ); 327*cdf0e10cSrcweir 328*cdf0e10cSrcweir Sequence<UsrAny> seq(1); 329*cdf0e10cSrcweir UsrAny any( (INT32) 1); 330*cdf0e10cSrcweir (seq.getArray())[0] = any; 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir any = (*pRef)->invoke( L"count" , seq , Sequence<INT16>(), Sequence<UsrAny>() ); 333*cdf0e10cSrcweir int nOldSize = any.getINT32(); 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir any = (*pRef)->invoke( L"append" , seq , Sequence<INT16>(), Sequence<UsrAny>() ); 336*cdf0e10cSrcweir any = (*pRef)->invoke( L"count" , seq , Sequence<INT16>(), Sequence<UsrAny>() ); 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir OSL_ASSERT( nOldSize + 1 == any.getINT32() ); 339*cdf0e10cSrcweir } 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir // just for testing ! 342*cdf0e10cSrcweir class PythonCodeLibrary : 343*cdf0e10cSrcweir public XLibraryAccess, 344*cdf0e10cSrcweir public OWeakObject 345*cdf0e10cSrcweir { 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir BOOL queryInterface( Uik aUik, XInterfaceRef & rOut ); 348*cdf0e10cSrcweir void acquire() { OWeakObject::acquire(); } 349*cdf0e10cSrcweir void release() { OWeakObject::release(); } 350*cdf0e10cSrcweir void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); } 351*cdf0e10cSrcweir 352*cdf0e10cSrcweir 353*cdf0e10cSrcweir virtual BOOL isFunction(const UString& FunctionName) THROWS( (UsrSystemException) ) 354*cdf0e10cSrcweir { 355*cdf0e10cSrcweir return FALSE; 356*cdf0e10cSrcweir } 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir virtual BOOL isValidPath(const UString& PathName) THROWS( (UsrSystemException) ) 359*cdf0e10cSrcweir { 360*cdf0e10cSrcweir return FALSE; 361*cdf0e10cSrcweir } 362*cdf0e10cSrcweir 363*cdf0e10cSrcweir virtual Sequence< UString > getModuleNames(void) THROWS( (UsrSystemException) ) 364*cdf0e10cSrcweir { 365*cdf0e10cSrcweir return Sequence<UString> (); 366*cdf0e10cSrcweir } 367*cdf0e10cSrcweir 368*cdf0e10cSrcweir virtual UString getModuleSource(const UString& ModulName) THROWS( (UsrSystemException) ) 369*cdf0e10cSrcweir { 370*cdf0e10cSrcweir if( ModulName == L"testmodul" ) { 371*cdf0e10cSrcweir return UString( L"def testmethod():\n" 372*cdf0e10cSrcweir L" return 42\n"); 373*cdf0e10cSrcweir } 374*cdf0e10cSrcweir return UString(); 375*cdf0e10cSrcweir } 376*cdf0e10cSrcweir 377*cdf0e10cSrcweir virtual Sequence< BYTE > getModuleCode(const UString& ModuleName) THROWS( (UsrSystemException) ) 378*cdf0e10cSrcweir { 379*cdf0e10cSrcweir return Sequence< BYTE > (); 380*cdf0e10cSrcweir } 381*cdf0e10cSrcweir 382*cdf0e10cSrcweir virtual UString getFunctionSource(const UString& FunctionName) THROWS( (UsrSystemException) ) 383*cdf0e10cSrcweir { 384*cdf0e10cSrcweir return UString(); 385*cdf0e10cSrcweir } 386*cdf0e10cSrcweir virtual Sequence< BYTE > getFunctionCode(const UString& FunctionName) THROWS( (UsrSystemException) ) 387*cdf0e10cSrcweir { 388*cdf0e10cSrcweir return Sequence< BYTE > (); 389*cdf0e10cSrcweir } 390*cdf0e10cSrcweir }; 391*cdf0e10cSrcweir 392*cdf0e10cSrcweir BOOL PythonCodeLibrary::queryInterface( Uik aUik, XInterfaceRef & rOut ) 393*cdf0e10cSrcweir { 394*cdf0e10cSrcweir if( XLibraryAccess::getSmartUik() == aUik ) { 395*cdf0e10cSrcweir rOut = (XLibraryAccess* ) this; 396*cdf0e10cSrcweir } 397*cdf0e10cSrcweir else { 398*cdf0e10cSrcweir return OWeakObject::queryInterface( aUik , rOut ); 399*cdf0e10cSrcweir } 400*cdf0e10cSrcweir 401*cdf0e10cSrcweir return TRUE; 402*cdf0e10cSrcweir } 403*cdf0e10cSrcweir 404*cdf0e10cSrcweir 405*cdf0e10cSrcweir 406*cdf0e10cSrcweir /* 407*cdf0e10cSrcweir * main. 408*cdf0e10cSrcweir */ 409*cdf0e10cSrcweir int __LOADONCALLAPI main (int argc, char **argv) 410*cdf0e10cSrcweir { 411*cdf0e10cSrcweir // necessary startup code 412*cdf0e10cSrcweir XMultiServiceFactoryRef xSMgr = createRegistryServiceManager(); 413*cdf0e10cSrcweir registerUsrServices( xSMgr ); 414*cdf0e10cSrcweir setProcessServiceManager( xSMgr ); 415*cdf0e10cSrcweir 416*cdf0e10cSrcweir XInterfaceRef x = xSMgr->createInstance( L"stardiv.uno.repos.ImplementationRegistration" ); 417*cdf0e10cSrcweir XImplementationRegistrationRef xReg( x, USR_QUERY ); 418*cdf0e10cSrcweir sal_Char szBuf[1024]; 419*cdf0e10cSrcweir 420*cdf0e10cSrcweir ORealDynamicLoader::computeModuleName( "pythonengine", szBuf, 1024 ); 421*cdf0e10cSrcweir UString aDllName( StringToOUString( szBuf, CHARSET_SYSTEM ) ); 422*cdf0e10cSrcweir xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() ); 423*cdf0e10cSrcweir 424*cdf0e10cSrcweir x = xSMgr->createInstance( L"stardiv.script.Python" ); 425*cdf0e10cSrcweir XEngineRef xEngine( x, USR_QUERY ); 426*cdf0e10cSrcweir XInvokationRef xInvoke( x, USR_QUERY ); 427*cdf0e10cSrcweir XDebuggingRef xDebug( x , USR_QUERY ); 428*cdf0e10cSrcweir 429*cdf0e10cSrcweir XInterfaceRef rRoot( (XInvokation * )new MyPythonRoot , USR_QUERY ); 430*cdf0e10cSrcweir xEngine->setRoot( rRoot ); 431*cdf0e10cSrcweir 432*cdf0e10cSrcweir 433*cdf0e10cSrcweir // execute a simple script 434*cdf0e10cSrcweir xEngine->run( L"nIntTest = 5\n" 435*cdf0e10cSrcweir L"list = [2,3,4]\n" , XInterfaceRef(), Sequence<UsrAny> () ); 436*cdf0e10cSrcweir 437*cdf0e10cSrcweir /**** 438*cdf0e10cSrcweir * 439*cdf0e10cSrcweir * Xinvokation - Test 440*cdf0e10cSrcweir * 441*cdf0e10cSrcweir *****/ 442*cdf0e10cSrcweir // get/set an int ! 443*cdf0e10cSrcweir { 444*cdf0e10cSrcweir OSL_ASSERT( xInvoke->hasProperty( L"nIntTest" ) ); 445*cdf0e10cSrcweir UsrAny any = xInvoke->getValue( L"nIntTest" ); 446*cdf0e10cSrcweir 447*cdf0e10cSrcweir OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG ); 448*cdf0e10cSrcweir OSL_ASSERT( any.getINT32() == 5 ); 449*cdf0e10cSrcweir 450*cdf0e10cSrcweir // simple test: set an int ! 451*cdf0e10cSrcweir xInvoke->setValue( L"nIntTest" , UsrAny( (INT32) 10 ) ); 452*cdf0e10cSrcweir any = xInvoke->getValue( L"nIntTest" ); 453*cdf0e10cSrcweir 454*cdf0e10cSrcweir OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG ); 455*cdf0e10cSrcweir OSL_ASSERT( any.getINT32() == 10 ); 456*cdf0e10cSrcweir } 457*cdf0e10cSrcweir 458*cdf0e10cSrcweir // call a python method ! 459*cdf0e10cSrcweir { 460*cdf0e10cSrcweir xEngine->run( L"def foo():\n" 461*cdf0e10cSrcweir L" return 'this is foo'\n" , XInterfaceRef() , Sequence<UsrAny> () ); 462*cdf0e10cSrcweir OSL_ASSERT( xInvoke->hasMethod( L"foo" ) ); 463*cdf0e10cSrcweir UsrAny any = xInvoke->invoke( L"foo" , 464*cdf0e10cSrcweir Sequence<UsrAny>(), 465*cdf0e10cSrcweir Sequence<INT16>() , 466*cdf0e10cSrcweir Sequence<UsrAny> () ); 467*cdf0e10cSrcweir OSL_ASSERT( any.getString() == L"this is foo" ); 468*cdf0e10cSrcweir } 469*cdf0e10cSrcweir 470*cdf0e10cSrcweir 471*cdf0e10cSrcweir // check exception handling ! 472*cdf0e10cSrcweir { 473*cdf0e10cSrcweir try { 474*cdf0e10cSrcweir xInvoke->invoke( L"foo" , Sequence<UsrAny>(1) , Sequence<INT16>(), Sequence<UsrAny> () ); 475*cdf0e10cSrcweir // wrong number of arguments 476*cdf0e10cSrcweir OSL_ASSERT( 0 ); 477*cdf0e10cSrcweir } 478*cdf0e10cSrcweir catch ( IllegalArgumentException& e ) { 479*cdf0e10cSrcweir } 480*cdf0e10cSrcweir catch ( InvocationTargetException& e ) { 481*cdf0e10cSrcweir } 482*cdf0e10cSrcweir catch ( CannotConvertException& e ) { 483*cdf0e10cSrcweir // empty any cannot be converted 484*cdf0e10cSrcweir } 485*cdf0e10cSrcweir } 486*cdf0e10cSrcweir 487*cdf0e10cSrcweir // check InOut-Parameter 488*cdf0e10cSrcweir checkInvokation( xInvoke ); 489*cdf0e10cSrcweir 490*cdf0e10cSrcweir /******* 491*cdf0e10cSrcweir * 492*cdf0e10cSrcweir * Check Introspection Access 493*cdf0e10cSrcweir * 494*cdf0e10cSrcweir *******/ 495*cdf0e10cSrcweir { 496*cdf0e10cSrcweir XIntrospectionAccessRef xIntrospection = xInvoke->getIntrospection(); 497*cdf0e10cSrcweir OSL_ASSERT( xIntrospection.is() ); 498*cdf0e10cSrcweir 499*cdf0e10cSrcweir // no further test, simply call them 500*cdf0e10cSrcweir xIntrospection->getMethods(0); 501*cdf0e10cSrcweir xIntrospection->getProperties(0); 502*cdf0e10cSrcweir 503*cdf0e10cSrcweir OSL_ASSERT( xIntrospection->getSuppliedMethodConcepts() == 0 ); 504*cdf0e10cSrcweir OSL_ASSERT( xIntrospection->getSuppliedPropertyConcepts() == 0 ); 505*cdf0e10cSrcweir 506*cdf0e10cSrcweir Property prop = xIntrospection->getProperty( L"nIntTest" ,0 ); 507*cdf0e10cSrcweir OSL_ASSERT( prop.Name == L"nIntTest" ); 508*cdf0e10cSrcweir OSL_ASSERT( prop.Type->getTypeClass() == TypeClass_LONG ); 509*cdf0e10cSrcweir 510*cdf0e10cSrcweir XIdlMethodRef method = xIntrospection->getMethod( L"foo" , 0 ); 511*cdf0e10cSrcweir OSL_ASSERT( method->getName() == L"foo" ); 512*cdf0e10cSrcweir } 513*cdf0e10cSrcweir 514*cdf0e10cSrcweir 515*cdf0e10cSrcweir /****** 516*cdf0e10cSrcweir * 517*cdf0e10cSrcweir * Multithreading test 518*cdf0e10cSrcweir * 519*cdf0e10cSrcweir *******/ 520*cdf0e10cSrcweir 521*cdf0e10cSrcweir /****** 522*cdf0e10cSrcweir * 523*cdf0e10cSrcweir * XDebuggingTest 524*cdf0e10cSrcweir * 525*cdf0e10cSrcweir ******/ 526*cdf0e10cSrcweir // stop/doContinue + runAsync listener 527*cdf0e10cSrcweir { 528*cdf0e10cSrcweir // test hangs, if something is wrong 529*cdf0e10cSrcweir 530*cdf0e10cSrcweir TestListener *pListener = new TestListener( &xDebug ); 531*cdf0e10cSrcweir XEngineListenerRef ref( (XEngineListener * ) pListener , USR_QUERY ); 532*cdf0e10cSrcweir 533*cdf0e10cSrcweir // single listener check ! 534*cdf0e10cSrcweir xEngine->runAsync( L"pass\n" 535*cdf0e10cSrcweir , XInterfaceRef() , Sequence<UsrAny> () , ref ); 536*cdf0e10cSrcweir pListener->cmdLine(); 537*cdf0e10cSrcweir } 538*cdf0e10cSrcweir 539*cdf0e10cSrcweir // ListenerAdministration check ! 540*cdf0e10cSrcweir { 541*cdf0e10cSrcweir // test hangs, if something is wrong 542*cdf0e10cSrcweir 543*cdf0e10cSrcweir TestListener *pListener = new TestListener( &xDebug ); 544*cdf0e10cSrcweir XEngineListenerRef ref( (XEngineListener * ) pListener , USR_QUERY ); 545*cdf0e10cSrcweir 546*cdf0e10cSrcweir // engine listener check ! 547*cdf0e10cSrcweir xEngine->addEngineListener( ref ); 548*cdf0e10cSrcweir xEngine->runAsync( L"pass\n" 549*cdf0e10cSrcweir , XInterfaceRef() , Sequence<UsrAny> () , XEngineListenerRef() ); 550*cdf0e10cSrcweir pListener->cmdLine(); 551*cdf0e10cSrcweir xEngine->removeEngineListener( ref); 552*cdf0e10cSrcweir 553*cdf0e10cSrcweir } 554*cdf0e10cSrcweir 555*cdf0e10cSrcweir // check the import mechanism 556*cdf0e10cSrcweir { 557*cdf0e10cSrcweir XLibraryAccessRef xLibrary( ( XLibraryAccess * ) new PythonCodeLibrary , USR_QUERY ); 558*cdf0e10cSrcweir xEngine->setLibraryAccess( xLibrary ); 559*cdf0e10cSrcweir 560*cdf0e10cSrcweir xEngine->run( L"import testmodul\n" 561*cdf0e10cSrcweir L"x = testmodul.testmethod()\n" , XInterfaceRef() , Sequence<UsrAny>() ); 562*cdf0e10cSrcweir UsrAny any = xInvoke->getValue( L"x" ); 563*cdf0e10cSrcweir OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG ); 564*cdf0e10cSrcweir OSL_ASSERT( any.getINT32() == 42 ); 565*cdf0e10cSrcweir } 566*cdf0e10cSrcweir 567*cdf0e10cSrcweir // check other imports 568*cdf0e10cSrcweir { 569*cdf0e10cSrcweir // Check, if the libraries are available at run time 570*cdf0e10cSrcweir xEngine->run( L"import math\n" 571*cdf0e10cSrcweir L"dMathTest = math.exp(0)\n" , XInterfaceRef() , Sequence<UsrAny> () ); 572*cdf0e10cSrcweir 573*cdf0e10cSrcweir OSL_ASSERT( xInvoke->hasProperty( L"dMathTest" ) ); 574*cdf0e10cSrcweir UsrAny any = xInvoke->getValue( L"dMathTest" ); 575*cdf0e10cSrcweir 576*cdf0e10cSrcweir OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_DOUBLE ); 577*cdf0e10cSrcweir OSL_ASSERT( any.getDouble() == 1. ); 578*cdf0e10cSrcweir } 579*cdf0e10cSrcweir 580*cdf0e10cSrcweir // Test connection to root object ! 581*cdf0e10cSrcweir { 582*cdf0e10cSrcweir xEngine->run( L"x = stardiv.root.TestValue\n" 583*cdf0e10cSrcweir L"y = stardiv.inout(5)\n" 584*cdf0e10cSrcweir L"stardiv.root.getTestValueViaInout(y)\n" 585*cdf0e10cSrcweir L"z = y.value\n" , XInterfaceRef() , Sequence<UsrAny> () ); 586*cdf0e10cSrcweir 587*cdf0e10cSrcweir UsrAny any = xInvoke->getValue( L"x" ); 588*cdf0e10cSrcweir OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG ); 589*cdf0e10cSrcweir OSL_ASSERT( any.getINT32() == 15 ); 590*cdf0e10cSrcweir 591*cdf0e10cSrcweir any = xInvoke->getValue( L"z" ); 592*cdf0e10cSrcweir OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG ); 593*cdf0e10cSrcweir OSL_ASSERT( any.getINT32() == 15 ); 594*cdf0e10cSrcweir } 595*cdf0e10cSrcweir 596*cdf0e10cSrcweir // Test exactName interface 597*cdf0e10cSrcweir { 598*cdf0e10cSrcweir UsrAny any = xInvoke->getValue( L"__builtins__" ); 599*cdf0e10cSrcweir OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_INTERFACE ); 600*cdf0e10cSrcweir 601*cdf0e10cSrcweir XInvokationRef rInv( *((XInterfaceRef *) any.get() ), USR_QUERY ); 602*cdf0e10cSrcweir OSL_ASSERT( rInv.is() ); 603*cdf0e10cSrcweir 604*cdf0e10cSrcweir XExactNameRef rName( *((XInterfaceRef*) any.get() ), USR_QUERY ); 605*cdf0e10cSrcweir OSL_ASSERT( rName.is() ); 606*cdf0e10cSrcweir 607*cdf0e10cSrcweir UString str = rName->getExactName( L"SYNTAXERROR" ); 608*cdf0e10cSrcweir OSL_ASSERT( str.len() ); 609*cdf0e10cSrcweir } 610*cdf0e10cSrcweir 611*cdf0e10cSrcweir 612*cdf0e10cSrcweir // Test exactName interface of the engine itself 613*cdf0e10cSrcweir { 614*cdf0e10cSrcweir XExactNameRef rName( xInvoke , USR_QUERY ); 615*cdf0e10cSrcweir OSL_ASSERT( rName.is() ); 616*cdf0e10cSrcweir UString str = rName->getExactName( L"STARDIV" ); 617*cdf0e10cSrcweir OSL_ASSERT( str.len() ); 618*cdf0e10cSrcweir } 619*cdf0e10cSrcweir 620*cdf0e10cSrcweir 621*cdf0e10cSrcweir return 0; 622*cdf0e10cSrcweir } 623*cdf0e10cSrcweir 624