1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_BASIC_SOURCE_INC_RUNTIME_HXX
21 #define INCLUDED_BASIC_SOURCE_INC_RUNTIME_HXX
23 #include <basic/sbx.hxx>
27 #include <rtl/ustring.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <osl/file.hxx>
30 #include <rtl/math.hxx>
31 #include <i18nlangtag/lang.h>
34 #include <com/sun/star/lang/XComponent.hpp>
35 #include <com/sun/star/container/XEnumeration.hpp>
36 #include <unotools/localedatawrapper.hxx>
38 class SbiInstance
; // active StarBASIC process
39 class SbiRuntime
; // active StarBASIC procedure instance
41 struct SbiArgvStack
; // Argv stack element
42 struct SbiGosubStack
; // GOSUB stack element
43 class SbiImage
; // Code-Image
47 class SvNumberFormatter
; // time/date functions
48 enum class SbiImageFlags
;
58 struct SbiForStack
{ // for/next stack:
59 SbiForStack
* pNext
; // Chain
60 SbxVariableRef refVar
; // loop variable
61 SbxVariableRef refEnd
; // end expression / for each: Array/BasicCollection object
62 SbxVariableRef refInc
; // increment expression
66 sal_Int32 nCurCollectionIndex
;
67 sal_Int32
* pArrayCurIndices
;
68 sal_Int32
* pArrayLowerBounds
;
69 sal_Int32
* pArrayUpperBounds
;
70 css::uno::Reference
< css::container::XEnumeration
> xEnumeration
;
75 , nCurCollectionIndex(0)
76 , pArrayCurIndices(NULL
)
77 , pArrayLowerBounds(NULL
)
78 , pArrayUpperBounds(NULL
)
82 delete[] pArrayCurIndices
;
83 delete[] pArrayLowerBounds
;
84 delete[] pArrayUpperBounds
;
88 struct SbiGosubStack
{ // GOSUB-Stack:
89 SbiGosubStack
* pNext
; // Chain
90 const sal_uInt8
* pCode
; // Return-Pointer
91 sal_uInt16 nStartForLvl
; // #118235: For Level in moment of gosub
94 #define MAXRECURSION 500
96 #define Sb_ATTR_READONLY 0x0001
97 #define Sb_ATTR_HIDDEN 0x0002
98 #define Sb_ATTR_DIRECTORY 0x0010
107 ::osl::Directory
* pDir
;
111 OUString sFullNameToBeChecked
;
114 css::uno::Sequence
< OUString
> aDirSeq
;
120 // The instance matches a running StarBASIC. Many basics running at the same
121 // time are managed by chained instances. There is all the data that only lives
122 // when the BASIC is living too, like the I/O-system.
124 typedef ::std::vector
126 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>
133 friend class SbiRuntime
;
137 SbiIoSystem
* pIosys
; // file system
138 SbiDdeControl
* pDdeCtrl
; // DDE
139 SbiDllMgr
* pDllMgr
; // DLL-Calls (DECLARE)
141 SvNumberFormatter
* pNumberFormatter
;
142 LanguageType meFormatterLangType
;
143 DateFormat meFormatterDateFormat
;
144 sal_uInt32 nStdDateIdx
, nStdTimeIdx
, nStdDateTimeIdx
;
147 OUString aErrorMsg
; // last error message for $ARG
148 sal_Int32 nErl
; // current error line
149 bool bReschedule
; // Flag: sal_True = Reschedule in main loop
150 bool bCompatibility
; // Flag: sal_True = VBA runtime compatibility mode
152 ComponentVector_t ComponentVector
;
154 SbiRuntime
* pRun
; // Call-Stack
155 SbiInstance
* pNext
; // instances chain
157 // #31460 new concept for StepInto/Over/Out,
158 // explanation see runtime.cxx at SbiInstance::CalcBreakCallLevel()
160 sal_uInt16 nBreakCallLvl
;
161 void CalcBreakCallLevel( sal_uInt16 nFlags
);
163 SbiInstance( StarBASIC
* );
166 void Error( SbError
); // trappable Error
167 void Error( SbError
, const OUString
& rMsg
); // trappable Error with message
168 void ErrorVB( sal_Int32 nVBNumber
, const OUString
& rMsg
);
169 void setErrorVB( sal_Int32 nVBNumber
, const OUString
& rMsg
);
170 void FatalError( SbError
); // non-trappable Error
171 void FatalError( SbError
, const OUString
& ); // non-trappable Error
172 void Abort(); // with current error code
175 SbError
GetErr() { return nErr
; }
176 OUString
GetErrorMsg() { return aErrorMsg
; }
177 sal_Int32
GetErl() { return nErl
; }
178 void EnableReschedule( bool bEnable
) { bReschedule
= bEnable
; }
179 bool IsReschedule() { return bReschedule
; }
180 void EnableCompatibility( bool bEnable
) { bCompatibility
= bEnable
; }
181 bool IsCompatibility() { return bCompatibility
; }
183 ComponentVector_t
& getComponentVector() { return ComponentVector
; }
185 SbMethod
* GetCaller( sal_uInt16
);
186 SbModule
* GetActiveModule();
188 SbiIoSystem
* GetIoSystem() { return pIosys
; }
189 SbiDdeControl
* GetDdeControl() { return pDdeCtrl
; }
190 StarBASIC
* GetBasic() { return pBasic
; }
191 SbiDllMgr
* GetDllMgr();
192 SbiRTLData
* GetRTLData() const { return const_cast<SbiRTLData
*>(&aRTLData
); }
194 SvNumberFormatter
* GetNumberFormatter();
195 sal_uInt32
GetStdDateIdx() const { return nStdDateIdx
; }
196 sal_uInt32
GetStdTimeIdx() const { return nStdTimeIdx
; }
197 sal_uInt32
GetStdDateTimeIdx() const { return nStdDateTimeIdx
; }
199 // offer NumberFormatter also static
200 static void PrepareNumberFormatter( SvNumberFormatter
*& rpNumberFormatter
,
201 sal_uInt32
&rnStdDateIdx
, sal_uInt32
&rnStdTimeIdx
, sal_uInt32
&rnStdDateTimeIdx
,
202 LanguageType
* peFormatterLangType
=NULL
, DateFormat
* peFormatterDateFormat
=NULL
);
205 SbiIoSystem
* SbGetIoSystem();
208 // chainable items to keep references temporary
214 RefSaveItem() { pNext
= NULL
; }
218 // There's one instance of this class for every executed sub-program.
219 // This instance is the heart of the BASIC-machine and contains only local data.
223 friend void SbRtl_CallByName( StarBASIC
* pBasic
, SbxArray
& rPar
, bool bWrite
);
225 typedef void( SbiRuntime::*pStep0
)();
226 typedef void( SbiRuntime::*pStep1
)( sal_uInt32 nOp1
);
227 typedef void( SbiRuntime::*pStep2
)( sal_uInt32 nOp1
, sal_uInt32 nOp2
);
228 static pStep0 aStep0
[]; // opcode-table group 0
229 static pStep1 aStep1
[];
230 static pStep2 aStep2
[];
232 StarBASIC
& rBasic
; // StarBASIC instance
233 SbiInstance
* pInst
; // current thread
234 SbModule
* pMod
; // current module
235 SbMethod
* pMeth
; // method instance
236 SbiIoSystem
* pIosys
; // I/O-System
237 const SbiImage
* pImg
; // Code-Image
238 SbxArrayRef refExprStk
; // expression stack
239 SbxArrayRef refCaseStk
; // CASE expression stack
240 SbxArrayRef refRedimpArray
; // Array saved to use for REDIM PRESERVE
241 SbxVariableRef refRedim
; // Array saved to use for REDIM
242 SbxVariableRef xDummyVar
; // substitute for variables that weren't found
243 SbxVariable
* mpExtCaller
; // Caller ( external - e.g. button name, shape, range object etc. - only in vba mode )
244 SbiArgvStack
* pArgvStk
; // ARGV-Stack
245 SbiGosubStack
* pGosubStk
; // GOSUB stack
246 SbiForStack
* pForStk
; // FOR/NEXT-Stack
247 sal_uInt16 nExprLvl
; // depth of the expr-stack
248 sal_uInt16 nGosubLvl
; // to prevent dead-recursions
249 sal_uInt16 nForLvl
; // #118235: Maintain for level
250 const sal_uInt8
* pCode
; // current Code-Pointer
251 const sal_uInt8
* pStmnt
; // beginning of the last statement
252 const sal_uInt8
* pError
; // address of the current error handler
253 const sal_uInt8
* pRestart
; // restart-address
254 const sal_uInt8
* pErrCode
; // restart-adresse RESUME NEXT
255 const sal_uInt8
* pErrStmnt
; // Restart-Adresse RESUMT 0
256 OUString aLibName
; // Lib-name for declare-call
257 SbxArrayRef refParams
; // current procedure parameters
258 SbxArrayRef refLocals
; // local variable
260 // #74254, one refSaveObj is not enough! new: pRefSaveList (see above)
263 bool bError
; // true: handle errors
264 bool bInError
; // true: in an error handler
265 bool bBlocked
; // true: blocked by next call level, #i48868
267 sal_uInt16 nFlags
; // Debugging-Flags
269 sal_uInt16 nOps
; // opcode counter
270 sal_uInt32 m_nLastTime
;
272 RefSaveItem
* pRefSaveList
; // #74254 save temporary references
273 RefSaveItem
* pItemStoreList
; // keep unused items
274 void SaveRef( SbxVariable
* pVar
)
276 RefSaveItem
* pItem
= pItemStoreList
;
278 pItemStoreList
= pItem
->pNext
;
280 pItem
= new RefSaveItem();
281 pItem
->pNext
= pRefSaveList
;
283 pRefSaveList
= pItem
;
287 while( pRefSaveList
)
289 RefSaveItem
* pToClearItem
= pRefSaveList
;
290 pRefSaveList
= pToClearItem
->pNext
;
291 pToClearItem
->xRef
= NULL
;
292 pToClearItem
->pNext
= pItemStoreList
;
293 pItemStoreList
= pToClearItem
;
297 SbxVariable
* FindElement
298 ( SbxObject
* pObj
, sal_uInt32 nOp1
, sal_uInt32 nOp2
, SbError
, bool bLocal
, bool bStatic
= false );
299 void SetupArgs( SbxVariable
*, sal_uInt32
);
300 SbxVariable
* CheckArray( SbxVariable
* );
302 void PushVar( SbxVariable
* );
303 SbxVariableRef
PopVar();
304 SbxVariable
* GetTOS( short=0 );
306 bool ClearExprStack();
308 void PushGosub( const sal_uInt8
* );
310 void ClearGosubStack();
314 void ClearArgvStack();
319 void ClearForStack();
321 void StepArith( SbxOperator
);
322 void StepUnary( SbxOperator
);
323 void StepCompare( SbxOperator
);
325 void SetParameters( SbxArray
* );
327 // HAS TO BE IMPLEMENTED SOME TIME
328 void DllCall( const OUString
&, const OUString
&, SbxArray
*, SbxDataType
, bool );
330 // #56204 swap out DIM-functionality into help method (step0.cxx)
331 void DimImpl( SbxVariableRef refVar
);
333 static bool implIsClass( SbxObject
* pObj
, const OUString
& aClass
);
335 void StepSETCLASS_impl( sal_uInt32 nOp1
, bool bHandleDflt
= false );
337 // the following routines are called by the single
338 // stepper and implement the single opcodes
339 void StepNOP(), StepEXP(), StepMUL(), StepDIV();
340 void StepMOD(), StepPLUS(), StepMINUS(), StepNEG();
341 void StepEQ(), StepNE(), StepLT(), StepGT();
342 void StepLE(), StepGE(), StepIDIV(), StepAND();
343 void StepOR(), StepXOR(), StepEQV(), StepIMP();
344 void StepNOT(), StepCAT(), StepLIKE(), StepIS();
345 void StepCLONE(), StepOLDBASED(), StepARGC();
346 void StepARGV(), StepINPUT(), StepLINPUT(), StepSTOP();
347 void StepGET(), StepSET(), StepVBASET(), StepPUT(), StepPUTC();
348 void StepSET_Impl( SbxVariableRef
& refVal
, SbxVariableRef
& refVar
, bool bDefaultHandling
= false );
349 void StepDIM(), StepREDIM(), StepREDIMP(), StepERASE();
350 void StepINITFOR(), StepNEXT(), StepERROR(), StepINITFOREACH();
351 void StepCASE(), StepENDCASE(), StepSTDERROR();
352 void StepNOERROR(), StepCHANNEL(), StepCHANNEL0(), StepPRINT();
353 void StepPRINTF(), StepWRITE(), StepRENAME(), StepPROMPT();
354 void StepRESTART(), StepEMPTY(), StepLEAVE();
355 void StepLSET(), StepRSET(), StepREDIMP_ERASE(), StepERASE_CLEAR();
356 void StepARRAYACCESS(), StepBYVAL();
357 // all opcodes with one operand
358 void StepLOADNC( sal_uInt32
), StepLOADSC( sal_uInt32
), StepLOADI( sal_uInt32
);
359 void StepARGN( sal_uInt32
), StepBASED( sal_uInt32
), StepPAD( sal_uInt32
);
360 void StepJUMP( sal_uInt32
), StepJUMPT( sal_uInt32
);
361 void StepJUMPF( sal_uInt32
), StepONJUMP( sal_uInt32
);
362 void StepGOSUB( sal_uInt32
), StepRETURN( sal_uInt32
);
363 void StepTESTFOR( sal_uInt32
), StepCASETO( sal_uInt32
), StepERRHDL( sal_uInt32
);
364 void StepRESUME( sal_uInt32
), StepSETCLASS( sal_uInt32
), StepVBASETCLASS( sal_uInt32
), StepTESTCLASS( sal_uInt32
), StepLIB( sal_uInt32
);
365 bool checkClass_Impl( const SbxVariableRef
& refVal
, const OUString
& aClass
, bool bRaiseErrors
, bool bDefault
= true );
366 void StepCLOSE( sal_uInt32
), StepPRCHAR( sal_uInt32
), StepARGTYP( sal_uInt32
);
367 // all opcodes with two operands
368 void StepRTL( sal_uInt32
, sal_uInt32
), StepPUBLIC( sal_uInt32
, sal_uInt32
), StepPUBLIC_P( sal_uInt32
, sal_uInt32
);
369 void StepPUBLIC_Impl( sal_uInt32
, sal_uInt32
, bool bUsedForClassModule
);
370 void StepFIND_Impl( SbxObject
* pObj
, sal_uInt32 nOp1
, sal_uInt32 nOp2
, SbError
, bool bLocal
, bool bStatic
= false );
371 void StepFIND( sal_uInt32
, sal_uInt32
), StepELEM( sal_uInt32
, sal_uInt32
);
372 void StepGLOBAL( sal_uInt32
, sal_uInt32
), StepLOCAL( sal_uInt32
, sal_uInt32
);
373 void StepPARAM( sal_uInt32
, sal_uInt32
), StepCREATE( sal_uInt32
, sal_uInt32
);
374 void StepCALL( sal_uInt32
, sal_uInt32
), StepCALLC( sal_uInt32
, sal_uInt32
);
375 void StepCASEIS( sal_uInt32
, sal_uInt32
), StepSTMNT( sal_uInt32
, sal_uInt32
);
376 SbxVariable
* StepSTATIC_Impl( OUString
& aName
, SbxDataType
& t
);
377 void StepOPEN( sal_uInt32
, sal_uInt32
), StepSTATIC( sal_uInt32
, sal_uInt32
);
378 void StepTCREATE(sal_uInt32
,sal_uInt32
), StepDCREATE(sal_uInt32
,sal_uInt32
);
379 void StepGLOBAL_P( sal_uInt32
, sal_uInt32
),StepFIND_G( sal_uInt32
, sal_uInt32
);
380 void StepDCREATE_REDIMP(sal_uInt32
,sal_uInt32
), StepDCREATE_IMPL(sal_uInt32
,sal_uInt32
);
381 void StepFIND_CM( sal_uInt32
, sal_uInt32
);
382 void StepFIND_STATIC( sal_uInt32
, sal_uInt32
);
383 static void implHandleSbxFlags( SbxVariable
* pVar
, SbxDataType t
, sal_uInt32 nOp2
);
385 void SetVBAEnabled( bool bEnabled
);
386 bool IsImageFlag( SbiImageFlags n
) const;
387 sal_uInt16
GetBase();
388 sal_Int32 nLine
,nCol1
,nCol2
;
389 SbiRuntime
* pNext
; // Stack-Chain
391 SbiRuntime( SbModule
*, SbMethod
*, sal_uInt32
);
393 void Error( SbError
, bool bVBATranslationAlreadyDone
= false ); // set error if != 0
394 void Error( SbError
, const OUString
& ); // set error if != 0
395 void FatalError( SbError
); // error handling = standard, set error
396 void FatalError( SbError
, const OUString
& ); // error handling = standard, set error
397 static sal_Int32
translateErrorToVba( SbError nError
, OUString
& rMsg
);
399 bool Step(); // single step (one opcode)
400 void Stop() { bRun
= false; }
401 void block() { bBlocked
= true; }
402 void unblock() { bBlocked
= false; }
403 SbMethod
* GetMethod() { return pMeth
; }
404 SbModule
* GetModule() { return pMod
; }
405 sal_uInt16
GetDebugFlags() { return nFlags
; }
406 void SetDebugFlags( sal_uInt16 nFl
) { nFlags
= nFl
; }
407 SbMethod
* GetCaller() { return pMeth
;}
408 SbxVariable
* GetExternalCaller(){ return mpExtCaller
; }
410 SbiForStack
* FindForStackItemForCollection( class BasicCollection
* pCollection
);
412 SbxBase
* FindElementExtern( const OUString
& rName
);
413 static bool isVBAEnabled();
417 inline void checkArithmeticOverflow( double d
)
419 if( !::rtl::math::isFinite( d
) )
420 StarBASIC::Error( SbERR_MATH_OVERFLOW
);
423 inline void checkArithmeticOverflow( SbxVariable
* pVar
)
425 if( pVar
->GetType() == SbxDOUBLE
)
427 double d
= pVar
->GetDouble();
428 checkArithmeticOverflow( d
);
433 StarBASIC
* GetCurrentBasic( StarBASIC
* pRTBasic
);
435 // Get information if security restrictions should be
436 // used (File IO based on UCB, no RTL function SHELL
437 // no DDE functionality, no DLLCALL) in basic because
438 // of portal "virtual" users (portal user != UNIX user)
439 // (Implemented in iosys.cxx)
440 bool needSecurityRestrictions();
442 // Returns true if UNO is available, otherwise the old
443 // file system implementation has to be used
444 // (Implemented in iosys.cxx)
447 // Converts possibly relative paths to absolute paths
448 // according to the setting done by ChDir/ChDrive
449 // (Implemented in methods.cxx)
450 OUString
getFullPath( const OUString
& aRelPath
);
452 // Implementation of StepRENAME with UCB
453 // (Implemented in methods.cxx, so step0.cxx
454 // has not to be infected with UNO)
455 void implStepRenameUCB( const OUString
& aSource
, const OUString
& aDest
);
457 void implStepRenameOSL( const OUString
& aSource
, const OUString
& aDest
);
458 bool IsBaseIndexOne();
460 void removeDimAsNewRecoverItem( SbxVariable
* pVar
);
464 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */