Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_BASIC_SOURCE_INC_RUNTIME_HXX
21 : #define INCLUDED_BASIC_SOURCE_INC_RUNTIME_HXX
22 :
23 : #include <basic/sbx.hxx>
24 :
25 : #include "sb.hxx"
26 :
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>
32 :
33 : #include <vector>
34 : #include <com/sun/star/lang/XComponent.hpp>
35 : #include <com/sun/star/container/XEnumeration.hpp>
36 : #include <unotools/localedatawrapper.hxx>
37 :
38 : class SbiInstance; // active StarBASIC process
39 : class SbiRuntime; // active StarBASIC procedure instance
40 :
41 : struct SbiArgvStack; // Argv stack element
42 : struct SbiGosubStack; // GOSUB stack element
43 : class SbiImage; // Code-Image
44 : class SbiIoSystem;
45 : class SbiDdeControl;
46 : class SbiDllMgr;
47 : class SvNumberFormatter; // time/date functions
48 :
49 : enum ForType
50 : {
51 : FOR_TO,
52 : FOR_EACH_ARRAY,
53 : FOR_EACH_COLLECTION,
54 : FOR_EACH_XENUMERATION
55 : };
56 :
57 : struct SbiForStack { // for/next stack:
58 : SbiForStack* pNext; // Chain
59 : SbxVariableRef refVar; // loop variable
60 : SbxVariableRef refEnd; // end expression / for each: Array/BasicCollection object
61 : SbxVariableRef refInc; // increment expression
62 :
63 : // For each support
64 : ForType eForType;
65 : sal_Int32 nCurCollectionIndex;
66 : sal_Int32* pArrayCurIndices;
67 : sal_Int32* pArrayLowerBounds;
68 : sal_Int32* pArrayUpperBounds;
69 : css::uno::Reference< css::container::XEnumeration > xEnumeration;
70 :
71 172 : SbiForStack( void )
72 : : pNext(NULL)
73 : , eForType(FOR_TO)
74 : , nCurCollectionIndex(0)
75 : , pArrayCurIndices(NULL)
76 : , pArrayLowerBounds(NULL)
77 172 : , pArrayUpperBounds(NULL)
78 172 : {}
79 172 : ~SbiForStack()
80 172 : {
81 172 : delete[] pArrayCurIndices;
82 172 : delete[] pArrayLowerBounds;
83 172 : delete[] pArrayUpperBounds;
84 172 : }
85 : };
86 :
87 : struct SbiGosubStack { // GOSUB-Stack:
88 : SbiGosubStack* pNext; // Chain
89 : const sal_uInt8* pCode; // Return-Pointer
90 : sal_uInt16 nStartForLvl; // #118235: For Level in moment of gosub
91 : };
92 :
93 : #define MAXRECURSION 500
94 :
95 : #define Sb_ATTR_NORMAL 0x0000
96 : #define Sb_ATTR_READONLY 0x0001
97 : #define Sb_ATTR_HIDDEN 0x0002
98 : #define Sb_ATTR_SYSTEM 0x0004
99 : #define Sb_ATTR_VOLUME 0x0008
100 : #define Sb_ATTR_DIRECTORY 0x0010
101 : #define Sb_ATTR_ARCHIVE 0x0020
102 :
103 :
104 : class WildCard;
105 :
106 : class SbiRTLData
107 : {
108 : public:
109 :
110 : ::osl::Directory* pDir;
111 : sal_Int16 nDirFlags;
112 : short nCurDirPos;
113 :
114 : OUString sFullNameToBeChecked;
115 : WildCard* pWildCard;
116 :
117 : css::uno::Sequence< OUString > aDirSeq;
118 :
119 : SbiRTLData();
120 : ~SbiRTLData();
121 : };
122 :
123 : // The instance matches a running StarBASIC. Many basics running at the same
124 : // time are managed by chained instances. There is all the data that only lives
125 : // when the BASIC is living too, like the I/O-system.
126 :
127 : typedef ::std::vector
128 : <
129 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
130 : >
131 : ComponentVector_t;
132 :
133 :
134 : class SbiInstance
135 : {
136 : friend class SbiRuntime;
137 :
138 : SbiRTLData aRTLData;
139 :
140 : SbiIoSystem* pIosys; // file system
141 : SbiDdeControl* pDdeCtrl; // DDE
142 : SbiDllMgr* pDllMgr; // DLL-Calls (DECLARE)
143 : StarBASIC* pBasic;
144 : SvNumberFormatter* pNumberFormatter;
145 : LanguageType meFormatterLangType;
146 : DateFormat meFormatterDateFormat;
147 : sal_uInt32 nStdDateIdx, nStdTimeIdx, nStdDateTimeIdx;
148 :
149 : SbError nErr;
150 : OUString aErrorMsg; // last error message for $ARG
151 : sal_Int32 nErl; // current error line
152 : bool bReschedule; // Flag: sal_True = Reschedule in main loop
153 : bool bCompatibility; // Flag: sal_True = VBA runtime compatibility mode
154 :
155 : ComponentVector_t ComponentVector;
156 : public:
157 : SbiRuntime* pRun; // Call-Stack
158 : SbiInstance* pNext; // instances chain
159 :
160 : // #31460 new concept for StepInto/Over/Out,
161 : // Explaination see runtime.cxx at SbiInstance::CalcBreakCallLevel()
162 : sal_uInt16 nCallLvl;
163 : sal_uInt16 nBreakCallLvl;
164 : void CalcBreakCallLevel( sal_uInt16 nFlags );
165 :
166 : SbiInstance( StarBASIC* );
167 : ~SbiInstance();
168 :
169 : void Error( SbError ); // trappable Error
170 : void Error( SbError, const OUString& rMsg ); // trappable Error with message
171 : void ErrorVB( sal_Int32 nVBNumber, const OUString& rMsg );
172 : void setErrorVB( sal_Int32 nVBNumber, const OUString& rMsg );
173 : void FatalError( SbError ); // non-trappable Error
174 : void FatalError( SbError, const OUString& ); // non-trappable Error
175 : void Abort(); // with current error code
176 :
177 : void Stop();
178 0 : SbError GetErr() { return nErr; }
179 46 : OUString GetErrorMsg() { return aErrorMsg; }
180 0 : sal_Int32 GetErl() { return nErl; }
181 0 : void EnableReschedule( bool bEnable ) { bReschedule = bEnable; }
182 6528 : bool IsReschedule( void ) { return bReschedule; }
183 2036 : void EnableCompatibility( bool bEnable ) { bCompatibility = bEnable; }
184 31370 : bool IsCompatibility( void ) { return bCompatibility; }
185 :
186 0 : ComponentVector_t& getComponentVector( void ) { return ComponentVector; }
187 :
188 : SbMethod* GetCaller( sal_uInt16 );
189 : SbModule* GetActiveModule();
190 :
191 24 : SbiIoSystem* GetIoSystem() { return pIosys; }
192 0 : SbiDdeControl* GetDdeControl() { return pDdeCtrl; }
193 0 : StarBASIC* GetBasic( void ) { return pBasic; }
194 : SbiDllMgr* GetDllMgr();
195 2 : SbiRTLData* GetRTLData() const { return (SbiRTLData*)&aRTLData; }
196 :
197 : SvNumberFormatter* GetNumberFormatter();
198 0 : sal_uInt32 GetStdDateIdx() const { return nStdDateIdx; }
199 0 : sal_uInt32 GetStdTimeIdx() const { return nStdTimeIdx; }
200 0 : sal_uInt32 GetStdDateTimeIdx() const { return nStdDateTimeIdx; }
201 :
202 : // offer NumberFormatter also static
203 : static void PrepareNumberFormatter( SvNumberFormatter*& rpNumberFormatter,
204 : sal_uInt32 &rnStdDateIdx, sal_uInt32 &rnStdTimeIdx, sal_uInt32 &rnStdDateTimeIdx,
205 : LanguageType* peFormatterLangType=NULL, DateFormat* peFormatterDateFormat=NULL );
206 : };
207 :
208 : SbiIoSystem* SbGetIoSystem();
209 :
210 :
211 : // chainable items to keep references temporary
212 938 : struct RefSaveItem
213 : {
214 : SbxVariableRef xRef;
215 : RefSaveItem* pNext;
216 :
217 938 : RefSaveItem() { pNext = NULL; }
218 : };
219 :
220 :
221 : // There's one instance of this class for every executed sub-program.
222 : // This instance is the heart of the BASIC-machine and contains only local data.
223 :
224 : class SbiRuntime
225 : {
226 : friend void SbRtl_CallByName( StarBASIC* pBasic, SbxArray& rPar, bool bWrite );
227 :
228 : typedef void( SbiRuntime::*pStep0 )();
229 : typedef void( SbiRuntime::*pStep1 )( sal_uInt32 nOp1 );
230 : typedef void( SbiRuntime::*pStep2 )( sal_uInt32 nOp1, sal_uInt32 nOp2 );
231 : static pStep0 aStep0[]; // opcode-table group 0
232 : static pStep1 aStep1[];
233 : static pStep2 aStep2[];
234 :
235 : StarBASIC& rBasic; // StarBASIC instance
236 : SbiInstance* pInst; // current thread
237 : SbModule* pMod; // current module
238 : SbMethod* pMeth; // method instance
239 : SbiIoSystem* pIosys; // I/O-System
240 : const SbiImage* pImg; // Code-Image
241 : SbxArrayRef refExprStk; // expression stack
242 : SbxArrayRef refCaseStk; // CASE expression stack
243 : SbxArrayRef refRedimpArray; // Array saved to use for REDIM PRESERVE
244 : SbxVariableRef refRedim; // Array saved to use for REDIM
245 : SbxVariableRef xDummyVar; // substitute for variables that weren't found
246 : SbxVariable* mpExtCaller; // Caller ( external - e.g. button name, shape, range object etc. - only in vba mode )
247 : SbiArgvStack* pArgvStk; // ARGV-Stack
248 : SbiGosubStack* pGosubStk; // GOSUB stack
249 : SbiForStack* pForStk; // FOR/NEXT-Stack
250 : sal_uInt16 nExprLvl; // depth of the expr-stack
251 : sal_uInt16 nGosubLvl; // to prevent dead-recursions
252 : sal_uInt16 nForLvl; // #118235: Maintain for level
253 : const sal_uInt8* pCode; // current Code-Pointer
254 : const sal_uInt8* pStmnt; // beginning of the last statement
255 : const sal_uInt8* pError; // address of the current error handler
256 : const sal_uInt8* pRestart; // restart-address
257 : const sal_uInt8* pErrCode; // restart-adresse RESUME NEXT
258 : const sal_uInt8* pErrStmnt; // Restart-Adresse RESUMT 0
259 : OUString aLibName; // Lib-name for declare-call
260 : SbxArrayRef refParams; // current procedure parameters
261 : SbxArrayRef refLocals; // local variable
262 : SbxArrayRef refArgv;
263 : // #74254, one refSaveObj is not enough! new: pRefSaveList (see above)
264 : short nArgc;
265 : bool bRun;
266 : bool bError; // true: handle errors
267 : bool bInError; // true: in an error handler
268 : bool bBlocked; // true: blocked by next call level, #i48868
269 : bool bVBAEnabled;
270 : sal_uInt16 nFlags; // Debugging-Flags
271 : SbError nError;
272 : sal_uInt16 nOps; // opcode counter
273 : sal_uInt32 m_nLastTime;
274 :
275 : RefSaveItem* pRefSaveList; // #74254 save temporary references
276 : RefSaveItem* pItemStoreList; // keep unused items
277 5370 : void SaveRef( SbxVariable* pVar )
278 : {
279 5370 : RefSaveItem* pItem = pItemStoreList;
280 5370 : if( pItem )
281 4432 : pItemStoreList = pItem->pNext;
282 : else
283 938 : pItem = new RefSaveItem();
284 5370 : pItem->pNext = pRefSaveList;
285 5370 : pItem->xRef = pVar;
286 5370 : pRefSaveList = pItem;
287 5370 : }
288 26418 : void ClearRefs( void )
289 : {
290 58206 : while( pRefSaveList )
291 : {
292 5370 : RefSaveItem* pToClearItem = pRefSaveList;
293 5370 : pRefSaveList = pToClearItem->pNext;
294 5370 : pToClearItem->xRef = NULL;
295 5370 : pToClearItem->pNext = pItemStoreList;
296 5370 : pItemStoreList = pToClearItem;
297 : }
298 26418 : }
299 :
300 : SbxVariable* FindElement
301 : ( SbxObject* pObj, sal_uInt32 nOp1, sal_uInt32 nOp2, SbError, bool bLocal, bool bStatic = false );
302 : void SetupArgs( SbxVariable*, sal_uInt32 );
303 : SbxVariable* CheckArray( SbxVariable* );
304 :
305 : void PushVar( SbxVariable* );
306 : SbxVariableRef PopVar();
307 : SbxVariable* GetTOS( short=0 );
308 : void TOSMakeTemp();
309 : bool ClearExprStack();
310 :
311 : void PushGosub( const sal_uInt8* );
312 : void PopGosub();
313 : void ClearGosubStack();
314 :
315 : void PushArgv();
316 : void PopArgv();
317 : void ClearArgvStack();
318 :
319 : void PushFor();
320 : void PushForEach();
321 : void PopFor();
322 : void ClearForStack();
323 :
324 : void StepArith( SbxOperator );
325 : void StepUnary( SbxOperator );
326 : void StepCompare( SbxOperator );
327 :
328 : void SetParameters( SbxArray* );
329 :
330 : // HAS TO BE IMPLEMENTED SOME TIME
331 : void DllCall( const OUString&, const OUString&, SbxArray*, SbxDataType, bool );
332 :
333 : // #56204 swap out DIM-functionality into help method (step0.cxx)
334 : void DimImpl( SbxVariableRef refVar );
335 :
336 : bool implIsClass( SbxObject* pObj, const OUString& aClass );
337 :
338 : void StepSETCLASS_impl( sal_uInt32 nOp1, bool bHandleDflt = false );
339 :
340 : // the following routines are called by the single
341 : // stepper and implement the single opcodes
342 : void StepNOP(), StepEXP(), StepMUL(), StepDIV();
343 : void StepMOD(), StepPLUS(), StepMINUS(), StepNEG();
344 : void StepEQ(), StepNE(), StepLT(), StepGT();
345 : void StepLE(), StepGE(), StepIDIV(), StepAND();
346 : void StepOR(), StepXOR(), StepEQV(), StepIMP();
347 : void StepNOT(), StepCAT(), StepLIKE(), StepIS();
348 : void StepCLONE(), StepOLDBASED(), StepARGC();
349 : void StepARGV(), StepINPUT(), StepLINPUT(), StepSTOP();
350 : void StepGET(), StepSET(), StepVBASET(), StepPUT(), StepPUTC();
351 : void StepSET_Impl( SbxVariableRef& refVal, SbxVariableRef& refVar, bool bDefaultHandling = false );
352 : void StepDIM(), StepREDIM(), StepREDIMP(), StepERASE();
353 : void StepINITFOR(), StepNEXT(), StepERROR(), StepINITFOREACH();
354 : void StepCASE(), StepENDCASE(), StepSTDERROR();
355 : void StepNOERROR(), StepCHANNEL(), StepCHANNEL0(), StepPRINT();
356 : void StepPRINTF(), StepWRITE(), StepRENAME(), StepPROMPT();
357 : void StepRESTART(), StepEMPTY(), StepLEAVE();
358 : void StepLSET(), StepRSET(), StepREDIMP_ERASE(), StepERASE_CLEAR();
359 : void StepARRAYACCESS(), StepBYVAL();
360 : // all opcodes with one operand
361 : void StepLOADNC( sal_uInt32 ), StepLOADSC( sal_uInt32 ), StepLOADI( sal_uInt32 );
362 : void StepARGN( sal_uInt32 ), StepBASED( sal_uInt32 ), StepPAD( sal_uInt32 );
363 : void StepJUMP( sal_uInt32 ), StepJUMPT( sal_uInt32 );
364 : void StepJUMPF( sal_uInt32 ), StepONJUMP( sal_uInt32 );
365 : void StepGOSUB( sal_uInt32 ), StepRETURN( sal_uInt32 );
366 : void StepTESTFOR( sal_uInt32 ), StepCASETO( sal_uInt32 ), StepERRHDL( sal_uInt32 );
367 : void StepRESUME( sal_uInt32 ), StepSETCLASS( sal_uInt32 ), StepVBASETCLASS( sal_uInt32 ), StepTESTCLASS( sal_uInt32 ), StepLIB( sal_uInt32 );
368 : bool checkClass_Impl( const SbxVariableRef& refVal, const OUString& aClass, bool bRaiseErrors, bool bDefault = true );
369 : void StepCLOSE( sal_uInt32 ), StepPRCHAR( sal_uInt32 ), StepARGTYP( sal_uInt32 );
370 : // all opcodes with two operands
371 : void StepRTL( sal_uInt32, sal_uInt32 ), StepPUBLIC( sal_uInt32, sal_uInt32 ), StepPUBLIC_P( sal_uInt32, sal_uInt32 );
372 : void StepPUBLIC_Impl( sal_uInt32, sal_uInt32, bool bUsedForClassModule );
373 : void StepFIND_Impl( SbxObject* pObj, sal_uInt32 nOp1, sal_uInt32 nOp2, SbError, bool bLocal, bool bStatic = false );
374 : void StepFIND( sal_uInt32, sal_uInt32 ), StepELEM( sal_uInt32, sal_uInt32 );
375 : void StepGLOBAL( sal_uInt32, sal_uInt32 ), StepLOCAL( sal_uInt32, sal_uInt32 );
376 : void StepPARAM( sal_uInt32, sal_uInt32), StepCREATE( sal_uInt32, sal_uInt32 );
377 : void StepCALL( sal_uInt32, sal_uInt32 ), StepCALLC( sal_uInt32, sal_uInt32 );
378 : void StepCASEIS( sal_uInt32, sal_uInt32 ), StepSTMNT( sal_uInt32, sal_uInt32 );
379 : SbxVariable* StepSTATIC_Impl( OUString& aName, SbxDataType& t );
380 : void StepOPEN( sal_uInt32, sal_uInt32 ), StepSTATIC( sal_uInt32, sal_uInt32 );
381 : void StepTCREATE(sal_uInt32,sal_uInt32), StepDCREATE(sal_uInt32,sal_uInt32);
382 : void StepGLOBAL_P( sal_uInt32, sal_uInt32 ),StepFIND_G( sal_uInt32, sal_uInt32 );
383 : void StepDCREATE_REDIMP(sal_uInt32,sal_uInt32), StepDCREATE_IMPL(sal_uInt32,sal_uInt32);
384 : void StepFIND_CM( sal_uInt32, sal_uInt32 );
385 : void StepFIND_STATIC( sal_uInt32, sal_uInt32 );
386 : void implHandleSbxFlags( SbxVariable* pVar, SbxDataType t, sal_uInt32 nOp2 );
387 : public:
388 : void SetVBAEnabled( bool bEnabled );
389 : sal_uInt16 GetImageFlag( sal_uInt16 n ) const;
390 : sal_uInt16 GetBase();
391 : sal_Int32 nLine,nCol1,nCol2;
392 : SbiRuntime* pNext; // Stack-Chain
393 :
394 : SbiRuntime( SbModule*, SbMethod*, sal_uInt32 );
395 : ~SbiRuntime();
396 : void Error( SbError, bool bVBATranslationAlreadyDone = false ); // set error if != 0
397 : void Error( SbError, const OUString& ); // set error if != 0
398 : void FatalError( SbError ); // error handling = standard, set error
399 : void FatalError( SbError, const OUString& ); // error handling = standard, set error
400 : static sal_Int32 translateErrorToVba( SbError nError, OUString& rMsg );
401 : void DumpPCode();
402 : bool Step(); // single step (one opcode)
403 0 : void Stop() { bRun = false; }
404 2602 : void block( void ) { bBlocked = true; }
405 2602 : void unblock( void ) { bBlocked = false; }
406 : SbMethod* GetMethod() { return pMeth; }
407 0 : SbModule* GetModule() { return pMod; }
408 2602 : sal_uInt16 GetDebugFlags() { return nFlags; }
409 0 : void SetDebugFlags( sal_uInt16 nFl ) { nFlags = nFl; }
410 0 : SbMethod* GetCaller() { return pMeth;}
411 0 : SbxVariable* GetExternalCaller(){ return mpExtCaller; }
412 :
413 : SbiForStack* FindForStackItemForCollection( class BasicCollection* pCollection );
414 :
415 : SbxBase* FindElementExtern( const OUString& rName );
416 : static bool isVBAEnabled();
417 :
418 : };
419 :
420 1794 : inline void checkArithmeticOverflow( double d )
421 : {
422 1794 : if( !::rtl::math::isFinite( d ) )
423 0 : StarBASIC::Error( SbERR_MATH_OVERFLOW );
424 1794 : }
425 :
426 5700 : inline void checkArithmeticOverflow( SbxVariable* pVar )
427 : {
428 5700 : if( pVar->GetType() == SbxDOUBLE )
429 : {
430 1794 : double d = pVar->GetDouble();
431 1794 : checkArithmeticOverflow( d );
432 : }
433 5700 : }
434 :
435 :
436 : StarBASIC* GetCurrentBasic( StarBASIC* pRTBasic );
437 :
438 : // Get information if security restrictions should be
439 : // used (File IO based on UCB, no RTL function SHELL
440 : // no DDE functionality, no DLLCALL) in basic because
441 : // of portal "virtual" users (portal user != UNIX user)
442 : // (Implemented in iosys.cxx)
443 : bool needSecurityRestrictions( void );
444 :
445 : // Returns true if UNO is available, otherwise the old
446 : // file system implementation has to be used
447 : // (Implemented in iosys.cxx)
448 : bool hasUno( void );
449 :
450 : // Converts possibly relative paths to absolute paths
451 : // according to the setting done by ChDir/ChDrive
452 : // (Implemented in methods.cxx)
453 : OUString getFullPath( const OUString& aRelPath );
454 :
455 : // Implementation of StepRENAME with UCB
456 : // (Implemented in methods.cxx, so step0.cxx
457 : // has not to be infected with UNO)
458 : void implStepRenameUCB( const OUString& aSource, const OUString& aDest );
459 :
460 : void implStepRenameOSL( const OUString& aSource, const OUString& aDest );
461 : bool IsBaseIndexOne();
462 :
463 : void removeDimAsNewRecoverItem( SbxVariable* pVar );
464 :
465 : #endif
466 :
467 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|