Files
libreoffice/basic/source/inc/runtime.hxx

497 lines
19 KiB
C++
Raw Normal View History

2010-10-27 13:11:31 +01:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2000-09-18 15:18:56 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 15:18:56 +00:00
*
* This file is part of OpenOffice.org.
2000-09-18 15:18:56 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2000-09-18 15:18:56 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2000-09-18 15:18:56 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-09-18 15:18:56 +00:00
*
************************************************************************/
#ifndef _SBRUNTIME_HXX
#define _SBRUNTIME_HXX
#include <basic/sbx.hxx>
2000-09-18 15:18:56 +00:00
#include "sb.hxx"
#include <rtl/ustring.hxx>
2000-09-18 15:18:56 +00:00
#include <com/sun/star/uno/Sequence.hxx>
2000-09-26 08:02:02 +00:00
#include <osl/file.hxx>
#include <rtl/math.hxx>
#include <i18npool/lang.h>
2000-09-18 15:18:56 +00:00
#include <vector>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/container/XEnumeration.hpp>
#include <unotools/localedatawrapper.hxx>
2000-09-18 15:18:56 +00:00
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::container;
2000-09-18 15:18:56 +00:00
namespace basicEncoder
{
// TODO: Use exported functionality (code is copied from deamons2/ucb)
class AsciiEncoder
{
public:
static ::rtl::OUString decodeUnoUrlParamValue(const rtl::OUString & rSource);
};
}
class SbiInstance; // active StarBASIC process
class SbiRuntime; // active StarBASIC procedure instance
2000-09-18 15:18:56 +00:00
struct SbiArgvStack; // Argv stack element
struct SbiGosubStack; // GOSUB stack element
class SbiImage; // Code-Image
class SbiIoSystem;
class SbiDdeControl;
class SbiDllMgr;
class SvNumberFormatter; // time/date functions
2000-09-18 15:18:56 +00:00
enum ForType
{
FOR_TO,
FOR_EACH_ARRAY,
FOR_EACH_COLLECTION,
FOR_EACH_XENUMERATION
};
2000-09-18 15:18:56 +00:00
struct SbiForStack { // for/next stack:
SbiForStack* pNext; // Chain
SbxVariableRef refVar; // loop variable
SbxVariableRef refEnd; // end expression / for each: Array/BasicCollection object
SbxVariableRef refInc; // increment expression
// For each support
ForType eForType;
sal_Int32 nCurCollectionIndex;
sal_Int32* pArrayCurIndices;
sal_Int32* pArrayLowerBounds;
sal_Int32* pArrayUpperBounds;
Reference< XEnumeration > xEnumeration;
SbiForStack( void )
: pArrayCurIndices( NULL )
, pArrayLowerBounds( NULL )
, pArrayUpperBounds( NULL )
{}
~SbiForStack()
{
delete[] pArrayCurIndices;
delete[] pArrayLowerBounds;
delete[] pArrayUpperBounds;
}
2000-09-18 15:18:56 +00:00
};
struct SbiGosubStack { // GOSUB-Stack:
SbiGosubStack* pNext; // Chain
const sal_uInt8* pCode; // Return-Pointer
sal_uInt16 nStartForLvl; // #118235: For Level in moment of gosub
};
#define MAXRECURSION 500
2000-09-18 15:18:56 +00:00
#define Sb_ATTR_NORMAL 0x0000
#define Sb_ATTR_HIDDEN 0x0002
#define Sb_ATTR_SYSTEM 0x0004
#define Sb_ATTR_VOLUME 0x0008
#define Sb_ATTR_DIRECTORY 0x0010
#define Sb_ATTR_ARCHIVE 0x0020
class Dir;
class WildCard;
2000-09-18 15:18:56 +00:00
class SbiRTLData
{
public:
2000-09-26 08:02:02 +00:00
::osl::Directory* pDir;
sal_Int16 nDirFlags;
short nCurDirPos;
2000-09-26 08:02:02 +00:00
2001-03-08 12:53:18 +00:00
String sFullNameToBeChecked;
WildCard* pWildCard;
Sequence< ::rtl::OUString > aDirSeq;
2000-09-18 15:18:56 +00:00
SbiRTLData();
~SbiRTLData();
};
// The instance matches a running StarBASIC. Many basics running at the same
// time are managed by chained instances. There is all the data that only lives
// when the BASIC is living too, like the I/O-system.
2000-09-18 15:18:56 +00:00
typedef ::std::vector
<
::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
>
ComponentVector_t;
2000-09-18 15:18:56 +00:00
class SbiInstance
{
friend class SbiRuntime;
SbiRTLData aRTLData;
SbiIoSystem* pIosys; // file system
2000-09-18 15:18:56 +00:00
SbiDdeControl* pDdeCtrl; // DDE
SbiDllMgr* pDllMgr; // DLL-Calls (DECLARE)
StarBASIC* pBasic;
SvNumberFormatter* pNumberFormatter;
LanguageType meFormatterLangType;
DateFormat meFormatterDateFormat;
sal_uInt32 nStdDateIdx, nStdTimeIdx, nStdDateTimeIdx;
2000-09-18 15:18:56 +00:00
SbError nErr;
String aErrorMsg; // last error message for $ARG
sal_uInt16 nErl; // current error line
sal_Bool bReschedule; // Flag: sal_True = Reschedule in main loop
sal_Bool bCompatibility; // Flag: sal_True = VBA runtime compatibility mode
2000-09-18 15:18:56 +00:00
ComponentVector_t ComponentVector;
2000-09-18 15:18:56 +00:00
public:
SbiRuntime* pRun; // Call-Stack
SbiInstance* pNext; // instances chain
2000-09-18 15:18:56 +00:00
// #31460 new concept for StepInto/Over/Out,
// Explaination see runtime.cxx at SbiInstance::CalcBreakCallLevel()
sal_uInt16 nCallLvl;
sal_uInt16 nBreakCallLvl;
void CalcBreakCallLevel( sal_uInt16 nFlags );
2000-09-18 15:18:56 +00:00
SbiInstance( StarBASIC* );
~SbiInstance();
void Error( SbError ); // trappable Error
void Error( SbError, const String& rMsg ); // trappable Error with message
void ErrorVB( sal_Int32 nVBNumber, const String& rMsg );
void setErrorVB( sal_Int32 nVBNumber, const String& rMsg );
void FatalError( SbError ); // non-trappable Error
void FatalError( SbError, const String& ); // non-trappable Error
void Abort(); // with current error code
2000-09-18 15:18:56 +00:00
void Stop();
SbError GetErr() { return nErr; }
String GetErrorMsg() { return aErrorMsg; }
xub_StrLen GetErl() { return nErl; }
void EnableReschedule( sal_Bool bEnable ) { bReschedule = bEnable; }
sal_Bool IsReschedule( void ) { return bReschedule; }
void EnableCompatibility( sal_Bool bEnable ) { bCompatibility = bEnable; }
sal_Bool IsCompatibility( void ) { return bCompatibility; }
2000-09-18 15:18:56 +00:00
ComponentVector_t& getComponentVector( void ) { return ComponentVector; }
SbMethod* GetCaller( sal_uInt16 );
2000-09-18 15:18:56 +00:00
SbModule* GetActiveModule();
SbiIoSystem* GetIoSystem() { return pIosys; }
SbiDdeControl* GetDdeControl() { return pDdeCtrl; }
StarBASIC* GetBasic( void ) { return pBasic; }
2000-09-18 15:18:56 +00:00
SbiDllMgr* GetDllMgr();
SbiRTLData* GetRTLData() const { return (SbiRTLData*)&aRTLData; }
SvNumberFormatter* GetNumberFormatter();
sal_uInt32 GetStdDateIdx() const { return nStdDateIdx; }
sal_uInt32 GetStdTimeIdx() const { return nStdTimeIdx; }
sal_uInt32 GetStdDateTimeIdx() const { return nStdDateTimeIdx; }
2000-09-18 15:18:56 +00:00
// offer NumberFormatter also static
2000-09-18 15:18:56 +00:00
static void PrepareNumberFormatter( SvNumberFormatter*& rpNumberFormatter,
sal_uInt32 &rnStdDateIdx, sal_uInt32 &rnStdTimeIdx, sal_uInt32 &rnStdDateTimeIdx,
LanguageType* peFormatterLangType=NULL, DateFormat* peFormatterDateFormat=NULL );
2000-09-18 15:18:56 +00:00
};
SbiIoSystem* SbGetIoSystem();
2000-09-18 15:18:56 +00:00
// chainable items to keep references temporary
2000-09-18 15:18:56 +00:00
struct RefSaveItem
{
SbxVariableRef xRef;
RefSaveItem* pNext;
RefSaveItem() { pNext = NULL; }
};
// There's one instance of this class for every executed sub-program.
// This instance is the heart of the BASIC-machine and contains only local data.
2000-09-18 15:18:56 +00:00
class SbiRuntime
{
friend void SbRtl_CallByName( StarBASIC* pBasic, SbxArray& rPar, sal_Bool bWrite );
2000-09-18 15:18:56 +00:00
typedef void( SbiRuntime::*pStep0 )();
typedef void( SbiRuntime::*pStep1 )( sal_uInt32 nOp1 );
typedef void( SbiRuntime::*pStep2 )( sal_uInt32 nOp1, sal_uInt32 nOp2 );
static pStep0 aStep0[]; // opcode-table group 0
static pStep1 aStep1[];
static pStep2 aStep2[];
StarBASIC& rBasic; // StarBASIC instance
SbiInstance* pInst; // current thread
SbModule* pMod; // current module
SbMethod* pMeth; // method instance
2000-09-18 15:18:56 +00:00
SbiIoSystem* pIosys; // I/O-System
const SbiImage* pImg; // Code-Image
SbxArrayRef refExprStk; // expression stack
SbxArrayRef refCaseStk; // CASE expression stack
2000-10-18 08:00:15 +00:00
SbxArrayRef refRedimpArray; // Array saved to use for REDIM PRESERVE
SbxVariableRef refRedim; // Array saved to use for REDIM
SbxVariableRef xDummyVar; // substitute for variables that weren't found
SbxVariable* mpExtCaller; // Caller ( external - e.g. button name, shape, range object etc. - only in vba mode )
2000-09-18 15:18:56 +00:00
SbiArgvStack* pArgvStk; // ARGV-Stack
SbiGosubStack* pGosubStk; // GOSUB stack
SbiForStack* pForStk; // FOR/NEXT-Stack
sal_uInt16 nExprLvl; // depth of the expr-stack
sal_uInt16 nGosubLvl; // to prevent dead-recursions
sal_uInt16 nForLvl; // #118235: Maintain for level
const sal_uInt8* pCode; // current Code-Pointer
const sal_uInt8* pStmnt; // beginning of the last statement
const sal_uInt8* pError; // address of the current error handler
const sal_uInt8* pRestart; // restart-address
const sal_uInt8* pErrCode; // restart-adresse RESUME NEXT
const sal_uInt8* pErrStmnt; // Restart-Adresse RESUMT 0
String aLibName; // Lib-name for declare-call
SbxArrayRef refParams; // current procedure parameters
SbxArrayRef refLocals; // local variable
SbxArrayRef refArgv;
// #74254, one refSaveObj is not enough! new: pRefSaveList (see above)
short nArgc;
sal_Bool bRun;
sal_Bool bError; // sal_True: handle errors
sal_Bool bInError; // sal_True: in an error handler
sal_Bool bBlocked; // sal_True: blocked by next call level, #i48868
sal_Bool bVBAEnabled;
sal_uInt16 nFlags; // Debugging-Flags
SbError nError;
sal_uInt16 nOps; // opcode counter
sal_uInt32 m_nLastTime;
2000-09-18 15:18:56 +00:00
RefSaveItem* pRefSaveList; // #74254 save temporary references
RefSaveItem* pItemStoreList; // keep unused items
2000-09-18 15:18:56 +00:00
void SaveRef( SbxVariable* pVar )
{
RefSaveItem* pItem = pItemStoreList;
if( pItem )
pItemStoreList = pItem->pNext;
else
pItem = new RefSaveItem();
pItem->pNext = pRefSaveList;
pItem->xRef = pVar;
pRefSaveList = pItem;
}
void ClearRefs( void )
{
while( pRefSaveList )
{
RefSaveItem* pToClearItem = pRefSaveList;
pRefSaveList = pToClearItem->pNext;
pToClearItem->xRef = NULL;
pToClearItem->pNext = pItemStoreList;
pItemStoreList = pToClearItem;
}
}
SbxVariable* FindElement
( SbxObject* pObj, sal_uInt32 nOp1, sal_uInt32 nOp2, SbError, sal_Bool bLocal, sal_Bool bStatic = sal_False );
void SetupArgs( SbxVariable*, sal_uInt32 );
2000-09-18 15:18:56 +00:00
SbxVariable* CheckArray( SbxVariable* );
void PushVar( SbxVariable* );
SbxVariableRef PopVar();
SbxVariable* GetTOS( short=0 );
void TOSMakeTemp();
sal_Bool ClearExprStack();
2000-09-18 15:18:56 +00:00
void PushGosub( const sal_uInt8* );
void PopGosub();
void ClearGosubStack();
2000-09-18 15:18:56 +00:00
void PushArgv();
void PopArgv();
void ClearArgvStack();
2000-09-18 15:18:56 +00:00
void PushFor();
void PushForEach();
void PopFor();
void ClearForStack();
2000-09-18 15:18:56 +00:00
void StepArith( SbxOperator );
void StepUnary( SbxOperator );
void StepCompare( SbxOperator );
2000-09-18 15:18:56 +00:00
void SetParameters( SbxArray* );
2000-09-18 15:18:56 +00:00
// HAS TO BE IMPLEMENTED SOME TIME
void DllCall( const String&, const String&, SbxArray*, SbxDataType, sal_Bool );
2000-09-18 15:18:56 +00:00
// #56204 swap out DIM-functionality into help method (step0.cxx)
2000-09-18 15:18:56 +00:00
void DimImpl( SbxVariableRef refVar );
bool implIsClass( SbxObject* pObj, const ::rtl::OUString& aClass );
void StepSETCLASS_impl( sal_uInt32 nOp1, bool bHandleDflt = false );
// the following routines are called by the single
// stepper and implement the single opcodes
2000-09-18 15:18:56 +00:00
void StepNOP(), StepEXP(), StepMUL(), StepDIV();
void StepMOD(), StepPLUS(), StepMINUS(), StepNEG();
void StepEQ(), StepNE(), StepLT(), StepGT();
void StepLE(), StepGE(), StepIDIV(), StepAND();
void StepOR(), StepXOR(), StepEQV(), StepIMP();
void StepNOT(), StepCAT(), StepLIKE(), StepIS();
void StepCLONE(), StepOLDBASED(), StepARGC();
void StepARGV(), StepINPUT(), StepLINPUT(), StepSTOP();
void StepGET(), StepSET(), StepVBASET(), StepPUT(), StepPUTC();
void StepSET_Impl( SbxVariableRef& refVal, SbxVariableRef& refVar, bool bDefaultHandling = false );
2000-09-18 15:18:56 +00:00
void StepDIM(), StepREDIM(), StepREDIMP(), StepERASE();
void StepINITFOR(), StepNEXT(), StepERROR(), StepINITFOREACH();
2000-09-18 15:18:56 +00:00
void StepCASE(), StepENDCASE(), StepSTDERROR();
void StepNOERROR(), StepCHANNEL(), StepCHANNEL0(), StepPRINT();
void StepPRINTF(), StepWRITE(), StepRENAME(), StepPROMPT();
void StepRESTART(), StepEMPTY(), StepLEAVE();
void StepLSET(), StepRSET(), StepREDIMP_ERASE(), StepERASE_CLEAR();
void StepARRAYACCESS(), StepBYVAL();
// all opcodes with one operand
void StepLOADNC( sal_uInt32 ), StepLOADSC( sal_uInt32 ), StepLOADI( sal_uInt32 );
void StepARGN( sal_uInt32 ), StepBASED( sal_uInt32 ), StepPAD( sal_uInt32 );
void StepJUMP( sal_uInt32 ), StepJUMPT( sal_uInt32 );
void StepJUMPF( sal_uInt32 ), StepONJUMP( sal_uInt32 );
void StepGOSUB( sal_uInt32 ), StepRETURN( sal_uInt32 );
void StepTESTFOR( sal_uInt32 ), StepCASETO( sal_uInt32 ), StepERRHDL( sal_uInt32 );
void StepRESUME( sal_uInt32 ), StepSETCLASS( sal_uInt32 ), StepVBASETCLASS( sal_uInt32 ), StepTESTCLASS( sal_uInt32 ), StepLIB( sal_uInt32 );
bool checkClass_Impl( const SbxVariableRef& refVal, const ::rtl::OUString& aClass, bool bRaiseErrors, bool bDefault = true );
void StepCLOSE( sal_uInt32 ), StepPRCHAR( sal_uInt32 ), StepARGTYP( sal_uInt32 );
// all opcodes with two operands
void StepRTL( sal_uInt32, sal_uInt32 ), StepPUBLIC( sal_uInt32, sal_uInt32 ), StepPUBLIC_P( sal_uInt32, sal_uInt32 );
void StepPUBLIC_Impl( sal_uInt32, sal_uInt32, bool bUsedForClassModule );
void StepFIND_Impl( SbxObject* pObj, sal_uInt32 nOp1, sal_uInt32 nOp2, SbError, sal_Bool bLocal, sal_Bool bStatic = sal_False );
void StepFIND( sal_uInt32, sal_uInt32 ), StepELEM( sal_uInt32, sal_uInt32 );
void StepGLOBAL( sal_uInt32, sal_uInt32 ), StepLOCAL( sal_uInt32, sal_uInt32 );
void StepPARAM( sal_uInt32, sal_uInt32), StepCREATE( sal_uInt32, sal_uInt32 );
void StepCALL( sal_uInt32, sal_uInt32 ), StepCALLC( sal_uInt32, sal_uInt32 );
void StepCASEIS( sal_uInt32, sal_uInt32 ), StepSTMNT( sal_uInt32, sal_uInt32 );
SbxVariable* StepSTATIC_Impl( String& aName, SbxDataType& t );
void StepOPEN( sal_uInt32, sal_uInt32 ), StepSTATIC( sal_uInt32, sal_uInt32 );
void StepTCREATE(sal_uInt32,sal_uInt32), StepDCREATE(sal_uInt32,sal_uInt32);
void StepGLOBAL_P( sal_uInt32, sal_uInt32 ),StepFIND_G( sal_uInt32, sal_uInt32 );
void StepDCREATE_REDIMP(sal_uInt32,sal_uInt32), StepDCREATE_IMPL(sal_uInt32,sal_uInt32);
void StepFIND_CM( sal_uInt32, sal_uInt32 );
void StepFIND_STATIC( sal_uInt32, sal_uInt32 );
void implHandleSbxFlags( SbxVariable* pVar, SbxDataType t, sal_uInt32 nOp2 );
2000-09-18 15:18:56 +00:00
public:
void SetVBAEnabled( bool bEnabled );
sal_uInt16 GetImageFlag( sal_uInt16 n ) const;
sal_uInt16 GetBase();
xub_StrLen nLine,nCol1,nCol2;
2000-09-18 15:18:56 +00:00
SbiRuntime* pNext; // Stack-Chain
SbiRuntime( SbModule*, SbMethod*, sal_uInt32 );
2000-09-18 15:18:56 +00:00
~SbiRuntime();
void Error( SbError, bool bVBATranslationAlreadyDone = false ); // set error if != 0
void Error( SbError, const String& ); // set error if != 0
void FatalError( SbError ); // error handling = standard, set error
void FatalError( SbError, const String& ); // error handling = standard, set error
static sal_Int32 translateErrorToVba( SbError nError, String& rMsg );
void DumpPCode();
sal_Bool Step(); // single step (one opcode)
void Stop() { bRun = sal_False; }
sal_Bool IsRun() { return bRun; }
void block( void ) { bBlocked = sal_True; }
void unblock( void ) { bBlocked = sal_False; }
2000-09-18 15:18:56 +00:00
SbMethod* GetMethod() { return pMeth; }
SbModule* GetModule() { return pMod; }
sal_uInt16 GetDebugFlags() { return nFlags; }
void SetDebugFlags( sal_uInt16 nFl ) { nFlags = nFl; }
2000-09-18 15:18:56 +00:00
SbMethod* GetCaller();
SbxArray* GetParams();
SbxVariable* GetExternalCaller(){ return mpExtCaller; }
2000-09-18 15:18:56 +00:00
SbiForStack* FindForStackItemForCollection( class BasicCollection* pCollection );
2000-09-18 15:18:56 +00:00
SbxBase* FindElementExtern( const String& rName );
static bool isVBAEnabled();
2000-09-18 15:18:56 +00:00
};
inline void checkArithmeticOverflow( double d )
{
if( !::rtl::math::isFinite( d ) )
StarBASIC::Error( SbERR_MATH_OVERFLOW );
}
inline void checkArithmeticOverflow( SbxVariable* pVar )
{
if( pVar->GetType() == SbxDOUBLE )
{
double d = pVar->GetDouble();
checkArithmeticOverflow( d );
}
}
2000-09-18 15:18:56 +00:00
StarBASIC* GetCurrentBasic( StarBASIC* pRTBasic );
// Get information if security restrictions should be
// used (File IO based on UCB, no RTL function SHELL
// no DDE functionality, no DLLCALL) in basic because
// of portal "virtual" users (portal user != UNIX user)
// (Implemented in iosys.cxx)
sal_Bool needSecurityRestrictions( void );
2000-09-18 15:18:56 +00:00
// Returns sal_True if UNO is available, otherwise the old
2000-09-18 15:18:56 +00:00
// file system implementation has to be used
// (Implemented in iosys.cxx)
sal_Bool hasUno( void );
2000-09-18 15:18:56 +00:00
// Converts possibly relative paths to absolute paths
// according to the setting done by ChDir/ChDrive
// (Implemented in methods.cxx)
String getFullPath( const String& aRelPath );
// Implementation of StepRENAME with UCB
// (Implemented in methods.cxx, so step0.cxx
// has not to be infected with UNO)
void implStepRenameUCB( const String& aSource, const String& aDest );
2000-09-26 08:02:02 +00:00
//*** OSL file access ***
// #87427 OSL need File URLs, so map to getFullPath
inline String getFullPathUNC( const String& aRelPath )
{
return getFullPath( aRelPath );
}
2000-09-26 08:02:02 +00:00
void implStepRenameOSL( const String& aSource, const String& aDest );
bool IsBaseIndexOne();
2000-09-26 08:02:02 +00:00
2000-09-18 15:18:56 +00:00
#endif
2010-10-27 13:11:31 +01:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */