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