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 <memory>
35 : #include <com/sun/star/lang/XComponent.hpp>
36 : #include <com/sun/star/container/XEnumeration.hpp>
37 : #include <unotools/localedatawrapper.hxx>
38 :
39 : class SbiInstance; // active StarBASIC process
40 : class SbiRuntime; // active StarBASIC procedure instance
41 :
42 : struct SbiArgvStack; // Argv stack element
43 : struct SbiGosubStack; // GOSUB stack element
44 : class SbiImage; // Code-Image
45 : class SbiIoSystem;
46 : class SbiDdeControl;
47 : class SbiDllMgr;
48 : class SvNumberFormatter; // time/date functions
49 : enum class SbiImageFlags;
50 :
51 : enum ForType
52 : {
53 : FOR_TO,
54 : FOR_EACH_ARRAY,
55 : FOR_EACH_COLLECTION,
56 : FOR_EACH_XENUMERATION
57 : };
58 :
59 : struct SbiForStack { // for/next stack:
60 : SbiForStack* pNext; // Chain
61 : SbxVariableRef refVar; // loop variable
62 : SbxVariableRef refEnd; // end expression / for each: Array/BasicCollection object
63 : SbxVariableRef refInc; // increment expression
64 :
65 : // For each support
66 : ForType eForType;
67 : sal_Int32 nCurCollectionIndex;
68 : sal_Int32* pArrayCurIndices;
69 : sal_Int32* pArrayLowerBounds;
70 : sal_Int32* pArrayUpperBounds;
71 : css::uno::Reference< css::container::XEnumeration > xEnumeration;
72 :
73 87 : SbiForStack()
74 : : pNext(NULL)
75 : , eForType(FOR_TO)
76 : , nCurCollectionIndex(0)
77 : , pArrayCurIndices(NULL)
78 : , pArrayLowerBounds(NULL)
79 87 : , pArrayUpperBounds(NULL)
80 87 : {}
81 87 : ~SbiForStack()
82 87 : {
83 87 : delete[] pArrayCurIndices;
84 87 : delete[] pArrayLowerBounds;
85 87 : delete[] pArrayUpperBounds;
86 87 : }
87 : };
88 :
89 : struct SbiGosubStack { // GOSUB-Stack:
90 : SbiGosubStack* pNext; // Chain
91 : const sal_uInt8* pCode; // Return-Pointer
92 : sal_uInt16 nStartForLvl; // #118235: For Level in moment of gosub
93 : };
94 :
95 : #define MAXRECURSION 500
96 :
97 : #define Sb_ATTR_READONLY 0x0001
98 : #define Sb_ATTR_HIDDEN 0x0002
99 : #define Sb_ATTR_DIRECTORY 0x0010
100 :
101 :
102 : class WildCard;
103 :
104 : class SbiRTLData
105 : {
106 : public:
107 :
108 : ::osl::Directory* pDir;
109 : sal_Int16 nDirFlags;
110 : short nCurDirPos;
111 :
112 : OUString sFullNameToBeChecked;
113 : WildCard* pWildCard;
114 :
115 : css::uno::Sequence< OUString > aDirSeq;
116 :
117 : SbiRTLData();
118 : ~SbiRTLData();
119 : };
120 :
121 : // The instance matches a running StarBASIC. Many basics running at the same
122 : // time are managed by chained instances. There is all the data that only lives
123 : // when the BASIC is living too, like the I/O-system.
124 :
125 : typedef ::std::vector
126 : <
127 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
128 : >
129 : ComponentVector_t;
130 :
131 :
132 : class SbiInstance
133 : {
134 : friend class SbiRuntime;
135 :
136 : SbiRTLData aRTLData;
137 :
138 : // file system
139 : std::unique_ptr<SbiIoSystem> pIosys;
140 : // DDE
141 : std::unique_ptr<SbiDdeControl> pDdeCtrl;
142 : // DLL-Calls (DECLARE)
143 : std::unique_ptr<SbiDllMgr> pDllMgr;
144 : std::unique_ptr<SvNumberFormatter> pNumberFormatter;
145 : StarBASIC* pBasic;
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 : bool bReschedule; // Flag: sal_True = Reschedule in main loop
154 : 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 : // explanation 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 23 : OUString GetErrorMsg() { return aErrorMsg; }
181 0 : sal_Int32 GetErl() { return nErl; }
182 0 : void EnableReschedule( bool bEnable ) { bReschedule = bEnable; }
183 3477 : bool IsReschedule() { return bReschedule; }
184 1035 : void EnableCompatibility( bool bEnable ) { bCompatibility = bEnable; }
185 18954 : bool IsCompatibility() { return bCompatibility; }
186 :
187 0 : ComponentVector_t& getComponentVector() { return ComponentVector; }
188 :
189 : SbMethod* GetCaller( sal_uInt16 );
190 : SbModule* GetActiveModule();
191 :
192 1435 : SbiIoSystem* GetIoSystem() { return pIosys.get(); }
193 0 : SbiDdeControl* GetDdeControl() { return pDdeCtrl.get(); }
194 0 : StarBASIC* GetBasic() { return pBasic; }
195 : SbiDllMgr* GetDllMgr();
196 1 : SbiRTLData* GetRTLData() const { return const_cast<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 SvNumberFormatter* PrepareNumberFormatter( sal_uInt32 &rnStdDateIdx,
205 : 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 491 : struct RefSaveItem
214 : {
215 : SbxVariableRef xRef;
216 : RefSaveItem* pNext;
217 :
218 491 : 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, 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 3125 : void SaveRef( SbxVariable* pVar )
279 : {
280 3125 : RefSaveItem* pItem = pItemStoreList;
281 3125 : if( pItem )
282 2634 : pItemStoreList = pItem->pNext;
283 : else
284 491 : pItem = new RefSaveItem();
285 3125 : pItem->pNext = pRefSaveList;
286 3125 : pItem->xRef = pVar;
287 3125 : pRefSaveList = pItem;
288 3125 : }
289 13444 : void ClearRefs()
290 : {
291 30013 : while( pRefSaveList )
292 : {
293 3125 : RefSaveItem* pToClearItem = pRefSaveList;
294 3125 : pRefSaveList = pToClearItem->pNext;
295 3125 : pToClearItem->xRef = NULL;
296 3125 : pToClearItem->pNext = pItemStoreList;
297 3125 : pItemStoreList = pToClearItem;
298 : }
299 13444 : }
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 : static 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 : static void implHandleSbxFlags( SbxVariable* pVar, SbxDataType t, sal_uInt32 nOp2 );
388 : public:
389 : void SetVBAEnabled( bool bEnabled );
390 : bool IsImageFlag( SbiImageFlags 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 1316 : void block() { bBlocked = true; }
406 1316 : void unblock() { bBlocked = false; }
407 : SbMethod* GetMethod() { return pMeth; }
408 0 : SbModule* GetModule() { return pMod; }
409 1316 : sal_uInt16 GetDebugFlags() { return nFlags; }
410 0 : void SetDebugFlags( sal_uInt16 nFl ) { nFlags = nFl; }
411 0 : SbMethod* GetCaller() { return pMeth;}
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 907 : inline void checkArithmeticOverflow( double d )
422 : {
423 907 : if( !::rtl::math::isFinite( d ) )
424 0 : StarBASIC::Error( SbERR_MATH_OVERFLOW );
425 907 : }
426 :
427 2874 : inline void checkArithmeticOverflow( SbxVariable* pVar )
428 : {
429 2874 : if( pVar->GetType() == SbxDOUBLE )
430 : {
431 907 : double d = pVar->GetDouble();
432 907 : checkArithmeticOverflow( d );
433 : }
434 2874 : }
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();
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();
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 : void implStepRenameOSL( const OUString& aSource, const OUString& aDest );
462 : bool IsBaseIndexOne();
463 :
464 : void removeDimAsNewRecoverItem( SbxVariable* pVar );
465 :
466 : #endif
467 :
468 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|