Debian Codegraph @

basic/source/runtime/runtime.cxx

raw
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 #include <stdlib.h>
21
22 #include <algorithm>
23 #include <string_view>
24 #include <unordered_map>
25
26 #include <com/sun/star/container/XEnumerationAccess.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/script/XDefaultMethod.hpp>
29 #include <com/sun/star/uno/Any.hxx>
30
31 #include <comphelper/processfactory.hxx>
32 #include <comphelper/string.hxx>
33 #include <o3tl/safeint.hxx>
34 #include <sal/log.hxx>
35
36 #include <tools/wldcrd.hxx>
37 #include <comphelper/diagnose_ex.hxx>
38
39 #include <utility>
40 #include <vcl/svapp.hxx>
41 #include <vcl/settings.hxx>
42
43 #include <rtl/math.hxx>
44 #include <rtl/ustrbuf.hxx>
45
46 #include <svl/numformat.hxx>
47 #include <svl/zforlist.hxx>
48
49 #include <unicode/regex.h>
50
51 #include <basic/sbuno.hxx>
52
53 #include <codegen.hxx>
54 #include "comenumwrapper.hxx"
55 #include "ddectrl.hxx"
56 #include "dllmgr.hxx"
57 #include <errobject.hxx>
58 #include <image.hxx>
59 #include <iosys.hxx>
60 #include <opcodes.hxx>
61 #include <runtime.hxx>
62 #include <sb.hxx>
63 #include <sbintern.hxx>
64 #include <sbprop.hxx>
65 #include <sbunoobj.hxx>
66 #include <basic/codecompletecache.hxx>
67 #include <memory>
68
69 using com::sun::star::uno::Reference;
70
71 using namespace com::sun::star::uno;
72 using namespace com::sun::star::container;
73 using namespace com::sun::star::lang;
74 using namespace com::sun::star::script;
75
76 using namespace ::com::sun::star;
77
78 #if HAVE_FEATURE_SCRIPTING
79
80 static void lcl_clearImpl( SbxVariableRef const & refVar, SbxDataType const & eType );
81 static void lcl_eraseImpl( SbxVariableRef const & refVar, bool bVBAEnabled );
82
83 namespace
84 {
85 class ScopedWritableGuard
86 {
87 public:
88 ScopedWritableGuard(const SbxVariableRef& rVar, bool bMakeWritable)
89 : m_rVar(bMakeWritable && !rVar->CanWrite() ? rVar : SbxVariableRef())
90 {
91 if (m_rVar)
92 {
93 m_rVar->SetFlag(SbxFlagBits::Write);
94 }
95 }
96 ~ScopedWritableGuard()
97 {
98 if (m_rVar)
99 {
100 m_rVar->ResetFlag(SbxFlagBits::Write);
101 }
102 }
103
104 private:
105 SbxVariableRef m_rVar;
106 };
107 }
108
109 void StarBASIC::SetVBAEnabled( bool bEnabled )
110 {
111 if ( bDocBasic )
112 {
113 bVBAEnabled = bEnabled;
114 }
115 }
116
117 bool StarBASIC::isVBAEnabled() const
118 {
119 return bDocBasic && (bVBAEnabled || SbiRuntime::isVBAEnabled());
120 }
121
122 struct SbiArgv { // Argv stack:
123 SbxArrayRef refArgv; // Argv
124 short nArgc; // Argc
125
126 SbiArgv(SbxArrayRef refArgv_, short nArgc_) :
127 refArgv(std::move(refArgv_)),
128 nArgc(nArgc_) {}
129 };
130
131 struct SbiGosub { // GOSUB-Stack:
132 const sal_uInt8* pCode; // Return-Pointer
133 sal_uInt16 nStartForLvl; // #118235: For Level in moment of gosub
134
135 SbiGosub(const sal_uInt8* pCode_, sal_uInt16 nStartForLvl_) :
136 pCode(pCode_),
137 nStartForLvl(nStartForLvl_) {}
138 };
139
140 const SbiRuntime::pStep0 SbiRuntime::aStep0[] = { // all opcodes without operands
141 &SbiRuntime::StepNOP,
142 &SbiRuntime::StepEXP,
143 &SbiRuntime::StepMUL,
144 &SbiRuntime::StepDIV,
145 &SbiRuntime::StepMOD,
146 &SbiRuntime::StepPLUS,
147 &SbiRuntime::StepMINUS,
148 &SbiRuntime::StepNEG,
149 &SbiRuntime::StepEQ,
150 &SbiRuntime::StepNE,
151 &SbiRuntime::StepLT,
152 &SbiRuntime::StepGT,
153 &SbiRuntime::StepLE,
154 &SbiRuntime::StepGE,
155 &SbiRuntime::StepIDIV,
156 &SbiRuntime::StepAND,
157 &SbiRuntime::StepOR,
158 &SbiRuntime::StepXOR,
159 &SbiRuntime::StepEQV,
160 &SbiRuntime::StepIMP,
161 &SbiRuntime::StepNOT,
162 &SbiRuntime::StepCAT,
163
164 &SbiRuntime::StepLIKE,
165 &SbiRuntime::StepIS,
166 // load/save
167 &SbiRuntime::StepARGC, // establish new Argv
168 &SbiRuntime::StepARGV, // TOS ==> current Argv
169 &SbiRuntime::StepINPUT, // Input ==> TOS
170 &SbiRuntime::StepLINPUT, // Line Input ==> TOS
171 &SbiRuntime::StepGET, // touch TOS
172 &SbiRuntime::StepSET, // save object TOS ==> TOS-1
173 &SbiRuntime::StepPUT, // TOS ==> TOS-1
174 &SbiRuntime::StepPUTC, // TOS ==> TOS-1, then ReadOnly
175 &SbiRuntime::StepDIM, // DIM
176 &SbiRuntime::StepREDIM, // REDIM
177 &SbiRuntime::StepREDIMP, // REDIM PRESERVE
178 &SbiRuntime::StepERASE, // delete TOS
179 // branch
180 &SbiRuntime::StepSTOP, // program end
181 &SbiRuntime::StepINITFOR, // initialize FOR-Variable
182 &SbiRuntime::StepNEXT, // increment FOR-Variable
183 &SbiRuntime::StepCASE, // beginning CASE
184 &SbiRuntime::StepENDCASE, // end CASE
185 &SbiRuntime::StepSTDERROR, // standard error handling
186 &SbiRuntime::StepNOERROR, // no error handling
187 &SbiRuntime::StepLEAVE, // leave UP
188 // E/A
189 &SbiRuntime::StepCHANNEL, // TOS = channel number
190 &SbiRuntime::StepPRINT, // print TOS
191 &SbiRuntime::StepPRINTF, // print TOS in field
192 &SbiRuntime::StepWRITE, // write TOS
193 &SbiRuntime::StepRENAME, // Rename Tos+1 to Tos
194 &SbiRuntime::StepPROMPT, // define Input Prompt from TOS
195 &SbiRuntime::StepRESTART, // Set restart point
196 &SbiRuntime::StepCHANNEL0, // set E/A-channel 0
197 &SbiRuntime::StepEMPTY, // empty expression on stack
198 &SbiRuntime::StepERROR, // TOS = error code
199 &SbiRuntime::StepLSET, // save object TOS ==> TOS-1
200 &SbiRuntime::StepRSET, // save object TOS ==> TOS-1
201 &SbiRuntime::StepREDIMP_ERASE,// Copy array object for REDIMP
202 &SbiRuntime::StepINITFOREACH,// Init for each loop
203 &SbiRuntime::StepVBASET,// vba-like set statement
204 &SbiRuntime::StepERASE_CLEAR,// vba-like set statement
205 &SbiRuntime::StepARRAYACCESS,// access TOS as array
206 &SbiRuntime::StepBYVAL, // access TOS as array
207 };
208
209 const SbiRuntime::pStep1 SbiRuntime::aStep1[] = { // all opcodes with one operand
210 &SbiRuntime::StepLOADNC, // loading a numeric constant (+ID)
211 &SbiRuntime::StepLOADSC, // loading a string constant (+ID)
212 &SbiRuntime::StepLOADI, // Immediate Load (+value)
213 &SbiRuntime::StepARGN, // save a named Args in Argv (+StringID)
214 &SbiRuntime::StepPAD, // bring string to a definite length (+length)
215 // branches
216 &SbiRuntime::StepJUMP, // jump (+Target)
217 &SbiRuntime::StepJUMPT, // evaluate TOS, conditional jump (+Target)
218 &SbiRuntime::StepJUMPF, // evaluate TOS, conditional jump (+Target)
219 &SbiRuntime::StepONJUMP, // evaluate TOS, jump into JUMP-table (+MaxVal)
220 &SbiRuntime::StepGOSUB, // UP-call (+Target)
221 &SbiRuntime::StepRETURN, // UP-return (+0 or Target)
222 &SbiRuntime::StepTESTFOR, // check FOR-variable, increment (+Endlabel)
223 &SbiRuntime::StepCASETO, // Tos+1 <= Case <= Tos), 2xremove (+Target)
224 &SbiRuntime::StepERRHDL, // error handler (+Offset)
225 &SbiRuntime::StepRESUME, // resume after errors (+0 or 1 or Label)
226 // E/A
227 &SbiRuntime::StepCLOSE, // (+channel/0)
228 &SbiRuntime::StepPRCHAR, // (+char)
229 // management
230 &SbiRuntime::StepSETCLASS, // check set + class names (+StringId)
231 &SbiRuntime::StepTESTCLASS, // Check TOS class (+StringId)
232 &SbiRuntime::StepLIB, // lib for declare-call (+StringId)
233 &SbiRuntime::StepBASED, // TOS is incremented by BASE, BASE is pushed before
234 &SbiRuntime::StepARGTYP, // convert last parameter in Argv (+Type)
235 &SbiRuntime::StepVBASETCLASS,// vba-like set statement
236 };
237
238 const SbiRuntime::pStep2 SbiRuntime::aStep2[] = {// all opcodes with two operands
239 &SbiRuntime::StepRTL, // load from RTL (+StringID+Typ)
240 &SbiRuntime::StepFIND, // load (+StringID+Typ)
241 &SbiRuntime::StepELEM, // load element (+StringID+Typ)
242 &SbiRuntime::StepPARAM, // Parameter (+Offset+Typ)
243 // branches
244 &SbiRuntime::StepCALL, // Declare-Call (+StringID+Typ)
245 &SbiRuntime::StepCALLC, // CDecl-Declare-Call (+StringID+Typ)
246 &SbiRuntime::StepCASEIS, // Case-Test (+Test-Opcode+False-Target)
247 // management
248 &SbiRuntime::StepSTMNT, // beginning of a statement (+Line+Col)
249 // E/A
250 &SbiRuntime::StepOPEN, // (+StreamMode+Flags)
251 // Objects
252 &SbiRuntime::StepLOCAL, // define local variable (+StringId+Typ)
253 &SbiRuntime::StepPUBLIC, // module global variable (+StringID+Typ)
254 &SbiRuntime::StepGLOBAL, // define global variable (+StringID+Typ)
255 &SbiRuntime::StepCREATE, // create object (+StringId+StringId)
256 &SbiRuntime::StepSTATIC, // static variable (+StringId+StringId)
257 &SbiRuntime::StepTCREATE, // user-defined objects (+StringId+StringId)
258 &SbiRuntime::StepDCREATE, // create object-array (+StringID+StringID)
259 &SbiRuntime::StepGLOBAL_P, // define global variable which is not overwritten
260 // by the Basic on a restart (+StringID+Typ)
261 &SbiRuntime::StepFIND_G, // finds global variable with special treatment because of _GLOBAL_P
262 &SbiRuntime::StepDCREATE_REDIMP, // redimension object array (+StringID+StringID)
263 &SbiRuntime::StepFIND_CM, // Search inside a class module (CM) to enable global search in time
264 &SbiRuntime::StepPUBLIC_P, // Search inside a class module (CM) to enable global search in time
265 &SbiRuntime::StepFIND_STATIC, // Search inside a class module (CM) to enable global search in time
266 };
267
268
269 // SbiRTLData
270
271 SbiRTLData::SbiRTLData()
272 : nDirFlags(SbAttributes::NORMAL)
273 , nCurDirPos(0)
274 {
275 }
276
277 SbiRTLData::~SbiRTLData()
278 {
279 }
280
281 // SbiInstance
282
283 // 16.10.96: #31460 new concept for StepInto/Over/Out
284 // The decision whether StepPoint shall be called is done with the help of
285 // the CallLevel. It's stopped when the current CallLevel is <= nBreakCallLvl.
286 // The current CallLevel can never be smaller than 1, as it's also incremented
287 // during the call of a method (also main). Therefore a BreakCallLvl from 0
288 // means that the program isn't stopped at all.
289 // (also have a look at: step2.cxx, SbiRuntime::StepSTMNT() )
290
291
292 void SbiInstance::CalcBreakCallLevel( BasicDebugFlags nFlags )
293 {
294
295 nFlags &= ~BasicDebugFlags::Break;
296
297 sal_uInt16 nRet;
298 if (nFlags == BasicDebugFlags::StepInto) {
299 nRet = nCallLvl + 1; // CallLevel+1 is also stopped
300 } else if (nFlags == (BasicDebugFlags::StepOver | BasicDebugFlags::StepInto)) {
301 nRet = nCallLvl; // current CallLevel is stopped
302 } else if (nFlags == BasicDebugFlags::StepOut) {
303 nRet = nCallLvl - 1; // smaller CallLevel is stopped
304 } else {
305 // Basic-IDE returns 0 instead of BasicDebugFlags::Continue, so also default=continue
306 nRet = 0; // CallLevel is always > 0 -> no StepPoint
307 }
308 nBreakCallLvl = nRet; // take result
309 }
310
311 SbiInstance::SbiInstance( StarBASIC* p )
312 : pIosys(new SbiIoSystem)
313 , pDdeCtrl(new SbiDdeControl)
314 , pBasic(p)
315 , meFormatterLangType(LANGUAGE_DONTKNOW)
316 , meFormatterDateOrder(DateOrder::YMD)
317 , nStdDateIdx(0)
318 , nStdTimeIdx(0)
319 , nStdDateTimeIdx(0)
320 , nErr(0)
321 , nErl(0)
322 , bReschedule(true)
323 , bCompatibility(false)
324 , pRun(nullptr)
325 , nCallLvl(0)
326 , nBreakCallLvl(0)
327 {
328 }
329
330 SbiInstance::~SbiInstance()
331 {
332 while( pRun )
333 {
334 SbiRuntime* p = pRun->pNext;
335 delete pRun;
336 pRun = p;
337 }
338
339 try
340 {
341 int nSize = ComponentVector.size();
342 if( nSize )
343 {
344 for( int i = nSize - 1 ; i >= 0 ; --i )
345 {
346 Reference< XComponent > xDlgComponent = ComponentVector[i];
347 if( xDlgComponent.is() )
348 xDlgComponent->dispose();
349 }
350 }
351 }
352 catch( const Exception& )
353 {
354 TOOLS_WARN_EXCEPTION("basic", "SbiInstance::~SbiInstance: caught an exception while disposing the components" );
355 }
356 }
357
358 SbiDllMgr* SbiInstance::GetDllMgr()
359 {
360 if( !pDllMgr )
361 {
362 pDllMgr.reset(new SbiDllMgr);
363 }
364 return pDllMgr.get();
365 }
366
367 #endif
368
369 bool SbiRuntime::isVBAEnabled()
370 {
371 SbiInstance* pInst = GetSbData()->pInst;
372 return pInst && pInst->pRun && pInst->pRun->bVBAEnabled;
373 }
374
375 // #39629 create NumberFormatter with the help of a static method now
376 std::shared_ptr<SvNumberFormatter> const & SbiInstance::GetNumberFormatter()
377 {
378 LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType();
379 SvtSysLocale aSysLocale;
380 DateOrder eDate = aSysLocale.GetLocaleData().getDateOrder();
381 if( pNumberFormatter )
382 {
383 if( eLangType != meFormatterLangType ||
384 eDate != meFormatterDateOrder )
385 {
386 pNumberFormatter.reset();
387 }
388 }
389 meFormatterLangType = eLangType;
390 meFormatterDateOrder = eDate;
391 if( !pNumberFormatter )
392 {
393 pNumberFormatter = PrepareNumberFormatter( nStdDateIdx, nStdTimeIdx, nStdDateTimeIdx,
394 &meFormatterLangType, &meFormatterDateOrder);
395 }
396 return pNumberFormatter;
397 }
398
399 // #39629 offer NumberFormatter static too
400 std::shared_ptr<SvNumberFormatter> SbiInstance::PrepareNumberFormatter( sal_uInt32 &rnStdDateIdx,
401 sal_uInt32 &rnStdTimeIdx, sal_uInt32 &rnStdDateTimeIdx,
402 LanguageType const * peFormatterLangType, DateOrder const * peFormatterDateOrder )
403 {
404 LanguageType eLangType;
405 if( peFormatterLangType )
406 {
407 eLangType = *peFormatterLangType;
408 }
409 else
410 {
411 eLangType = Application::GetSettings().GetLanguageTag().getLanguageType();
412 }
413 DateOrder eDate;
414 if( peFormatterDateOrder )
415 {
416 eDate = *peFormatterDateOrder;
417 }
418 else
419 {
420 SvtSysLocale aSysLocale;
421 eDate = aSysLocale.GetLocaleData().getDateOrder();
422 }
423
424 std::shared_ptr<SvNumberFormatter> pNumberFormatter =
425 std::make_shared<SvNumberFormatter>( comphelper::getProcessComponentContext(), eLangType );
426
427 // Several parser methods pass SvNumberFormatter::IsNumberFormat() a number
428 // format index to parse against. Tell the formatter the proper date
429 // evaluation order, which also determines the date acceptance patterns to
430 // use if a format was passed. NfEvalDateFormat::Format restricts to the
431 // format's locale's date patterns/order (no init/system locale match
432 // tried) and falls back to NfEvalDateFormat::International if no specific (i.e. 0)
433 // (or an unknown) format index was passed.
434 pNumberFormatter->SetEvalDateFormat( NfEvalDateFormat::Format);
435
436 sal_Int32 nCheckPos = 0;
437 SvNumFormatType nType;
438 rnStdTimeIdx = pNumberFormatter->GetStandardFormat( SvNumFormatType::TIME, eLangType );
439
440 // the formatter's standard templates have only got a two-digit date
441 // -> registering an own format
442
443 // HACK, because the numberformatter doesn't swap the place holders
444 // for month, day and year according to the system setting.
445 // Problem: Print Year(Date) under engl. BS
446 // also have a look at: basic/source/sbx/sbxdate.cxx
447
448 OUString aDateStr;
449 switch( eDate )
450 {
451 default:
452 case DateOrder::MDY: aDateStr = "MM/DD/YYYY"; break;
453 case DateOrder::DMY: aDateStr = "DD/MM/YYYY"; break;
454 case DateOrder::YMD: aDateStr = "YYYY/MM/DD"; break;
455 }
456 OUString aStr( aDateStr ); // PutandConvertEntry() modifies string!
457 pNumberFormatter->PutandConvertEntry( aStr, nCheckPos, nType,
458 rnStdDateIdx, LANGUAGE_ENGLISH_US, eLangType, true);
459 nCheckPos = 0;
460 aDateStr += " HH:MM:SS";
461 aStr = aDateStr;
462 pNumberFormatter->PutandConvertEntry( aStr, nCheckPos, nType,
463 rnStdDateTimeIdx, LANGUAGE_ENGLISH_US, eLangType, true);
464 return pNumberFormatter;
465 }
466
467 #if HAVE_FEATURE_SCRIPTING
468
469 // Let engine run. If Flags == BasicDebugFlags::Continue, take Flags over
470
471 void SbiInstance::Stop()
472 {
473 for( SbiRuntime* p = pRun; p; p = p->pNext )
474 {
475 p->Stop();
476 }
477 }
478
479 // Allows Basic IDE to set watch mode to suppress errors
480 static bool bWatchMode = false;
481
482 void setBasicWatchMode( bool bOn )
483 {
484 bWatchMode = bOn;
485 }
486
487 void SbiInstance::Error( ErrCode n )
488 {
489 Error( n, OUString() );
490 }
491
492 void SbiInstance::Error( ErrCode n, const OUString& rMsg )
493 {
494 if( !bWatchMode )
495 {
496 aErrorMsg = rMsg;
497 pRun->Error( n );
498 }
499 }
500
501 void SbiInstance::ErrorVB( sal_Int32 nVBNumber, const OUString& rMsg )
502 {
503 if( !bWatchMode )
504 {
505 ErrCode n = StarBASIC::GetSfxFromVBError( static_cast< sal_uInt16 >( nVBNumber ) );
506 if ( !n )
507 {
508 n = ErrCode(nVBNumber); // force orig number, probably should have a specific table of vb ( localized ) errors
509 }
510 aErrorMsg = rMsg;
511 SbiRuntime::translateErrorToVba( n, aErrorMsg );
512
513 pRun->Error( ERRCODE_BASIC_COMPAT, true/*bVBATranslationAlreadyDone*/ );
514 }
515 }
516
517 void SbiInstance::setErrorVB( sal_Int32 nVBNumber )
518 {
519 ErrCode n = StarBASIC::GetSfxFromVBError( static_cast< sal_uInt16 >( nVBNumber ) );
520 if( !n )
521 {
522 n = ErrCode(nVBNumber); // force orig number, probably should have a specific table of vb ( localized ) errors
523 }
524 aErrorMsg = OUString();
525 SbiRuntime::translateErrorToVba( n, aErrorMsg );
526
527 nErr = n;
528 }
529
530
531 void SbiInstance::FatalError( ErrCode n )
532 {
533 pRun->FatalError( n );
534 }
535
536 void SbiInstance::FatalError( ErrCode _errCode, const OUString& _details )
537 {
538 pRun->FatalError( _errCode, _details );
539 }
540
541 void SbiInstance::Abort()
542 {
543 StarBASIC* pErrBasic = GetCurrentBasic( pBasic );
544 pErrBasic->RTError( nErr, aErrorMsg, pRun->nLine, pRun->nCol1, pRun->nCol2 );
545 StarBASIC::Stop();
546 }
547
548 // can be unequal to pRTBasic
549 StarBASIC* GetCurrentBasic( StarBASIC* pRTBasic )
550 {
551 StarBASIC* pCurBasic = pRTBasic;
552 SbModule* pActiveModule = StarBASIC::GetActiveModule();
553 if( pActiveModule )
554 {
555 SbxObject* pParent = pActiveModule->GetParent();
556 if (StarBASIC *pBasic = dynamic_cast<StarBASIC*>(pParent))
557 pCurBasic = pBasic;
558 }
559 return pCurBasic;
560 }
561
562 SbModule* SbiInstance::GetActiveModule()
563 {
564 if( pRun )
565 {
566 return pRun->GetModule();
567 }
568 else
569 {
570 return nullptr;
571 }
572 }
573
574 SbMethod* SbiInstance::GetCaller( sal_uInt16 nLevel )
575 {
576 SbiRuntime* p = pRun;
577 while( nLevel-- && p )
578 {
579 p = p->pNext;
580 }
581 return p ? p->GetCaller() : nullptr;
582 }
583
584 // SbiInstance
585
586 // Attention: pMeth can also be NULL (on a call of the init-code)
587
588 SbiRuntime::SbiRuntime( SbModule* pm, SbMethod* pe, sal_uInt32 nStart )
589 : rBasic( *static_cast<StarBASIC*>(pm->pParent) ), pInst( GetSbData()->pInst ),
590 pMod( pm ), pMeth( pe ), pImg( pMod->pImage.get() )
591 {
592 nFlags = pe ? pe->GetDebugFlags() : BasicDebugFlags::NONE;
593 pIosys = pInst->GetIoSystem();
594 pCode =
595 pStmnt = pImg->GetCode() + nStart;
596 refExprStk = new SbxArray;
597 SetVBAEnabled( pMod->IsVBASupport() );
598 SetParameters( pe ? pe->GetParameters() : nullptr );
599 }
600
601 SbiRuntime::~SbiRuntime()
602 {
603 ClearArgvStack();
604 ClearForStack();
605 }
606
607 void SbiRuntime::SetVBAEnabled(bool bEnabled )
608 {
609 bVBAEnabled = bEnabled;
610 if ( bVBAEnabled )
611 {
612 if ( pMeth )
613 {
614 mpExtCaller = pMeth->mCaller;
615 }
616 }
617 else
618 {
619 mpExtCaller = nullptr;
620 }
621 }
622
623 // tdf#79426, tdf#125180 - adds the information about a missing parameter
624 void SbiRuntime::SetIsMissing( SbxVariable* pVar )
625 {
626 SbxInfo* pInfo = pVar->GetInfo() ? pVar->GetInfo() : new SbxInfo();
627 pInfo->AddParam( pVar->GetName(), SbxMISSING, pVar->GetFlags() );
628 pVar->SetInfo( pInfo );
629 }
630
631 // tdf#79426, tdf#125180 - checks if a variable contains the information about a missing parameter
632 bool SbiRuntime::IsMissing( SbxVariable* pVar, sal_uInt16 nIdx )
633 {
634 return pVar->GetInfo() && pVar->GetInfo()->GetParam( nIdx ) && pVar->GetInfo()->GetParam( nIdx )->eType & SbxMISSING;
635 }
636
637 // Construction of the parameter list. All ByRef-parameters are directly
638 // taken over; copies of ByVal-parameters are created. If a particular
639 // data type is requested, it is converted.
640
641 void SbiRuntime::SetParameters( SbxArray* pParams )
642 {
643 refParams = new SbxArray;
644 // for the return value
645 refParams->Put(pMeth, 0);
646
647 SbxInfo* pInfo = pMeth ? pMeth->GetInfo() : nullptr;
648 sal_uInt32 nParamCount = pParams ? pParams->Count() : 1;
649 assert(nParamCount <= std::numeric_limits<sal_uInt16>::max());
650 if( nParamCount > 1 )
651 {
652 for( sal_uInt32 i = 1 ; i < nParamCount ; i++ )
653 {
654 const SbxParamInfo* p = pInfo ? pInfo->GetParam( sal::static_int_cast<sal_uInt16>(i) ) : nullptr;
655
656 // #111897 ParamArray
657 if( p && ParamInfoFlag::checkFlagFor(p->nUserData, ParamInfo::ParamArray) )
658 {
659 SbxDimArray* pArray = new SbxDimArray( SbxVARIANT );
660 sal_uInt32 nParamArrayParamCount = nParamCount - i;
661 pArray->unoAddDim(0, nParamArrayParamCount - 1);
662 for (sal_uInt32 j = i; j < nParamCount ; ++j)
663 {
664 SbxVariable* v = pParams->Get(j);
665 sal_Int32 aDimIndex[1];
666 aDimIndex[0] = j - i;
667 pArray->Put(v, aDimIndex);
668 }
669 SbxVariable* pArrayVar = new SbxVariable( SbxVARIANT );
670 pArrayVar->SetFlag( SbxFlagBits::ReadWrite );
671 pArrayVar->PutObject( pArray );
672 refParams->Put(pArrayVar, i);
673
674 // Block ParamArray for missing parameter
675 pInfo = nullptr;
676 break;
677 }
678
679 SbxVariable* v = pParams->Get(i);
680 assert(v);
681 // methods are always byval!
682 bool bByVal = dynamic_cast<const SbxMethod *>(v) != nullptr;
683 SbxDataType t = v->GetType();
684 bool bTargetTypeIsArray = false;
685 if( p )
686 {
687 bByVal |= ( p->eType & SbxBYREF ) == 0;
688 // tdf#79426, tdf#125180 - don't convert missing arguments to the requested parameter type
689 if ( !IsMissing( v, 1 ) )
690 {
691 t = static_cast<SbxDataType>( p->eType & 0x0FFF );
692 }
693
694 if( !bByVal && t != SbxVARIANT &&
695 (!v->IsFixed() || static_cast<SbxDataType>(v->GetType() & 0x0FFF ) != t) )
696 {
697 bByVal = true;
698 }
699
700 bTargetTypeIsArray = ParamInfoFlag::checkFlagFor(p->nUserData, ParamInfo::WithBrackets);
701 }
702 if( bByVal )
703 {
704 // tdf#79426, tdf#125180 - don't convert missing arguments to the requested parameter type
705 if( bTargetTypeIsArray && !IsMissing( v, 1 ) )
706 {
707 t = SbxOBJECT;
708 }
709 SbxVariable* v2 = new SbxVariable( t );
710 v2->SetFlag( SbxFlagBits::ReadWrite );
711 // tdf#79426, tdf#125180 - if parameter was missing, readd additional information about a missing parameter
712 if ( IsMissing( v, 1 ) )
713 {
714 SetIsMissing( v2 );
715 }
716 *v2 = *v;
717 refParams->Put(v2, i);
718 }
719 else
720 {
721 // tdf#79426, tdf#125180 - don't convert missing arguments to the requested parameter type
722 if( t != SbxVARIANT && !IsMissing( v, 1 ) && t != ( v->GetType() & 0x0FFF ) )
723 {
724 if( p && (p->eType & SbxARRAY) )
725 {
726 Error( ERRCODE_BASIC_CONVERSION );
727 }
728 else
729 {
730 v->Convert( t );
731 }
732 }
733 refParams->Put(v, i);
734 }
735 if( p )
736 {
737 refParams->PutAlias(p->aName, i);
738 }
739 }
740 }
741
742 // ParamArray for missing parameter
743 if( !pInfo )
744 return;
745
746 // #111897 Check first missing parameter for ParamArray
747 const SbxParamInfo* p = pInfo->GetParam(sal::static_int_cast<sal_uInt16>(nParamCount));
748 if( p && ParamInfoFlag::checkFlagFor(p->nUserData, ParamInfo::ParamArray) )
749 {
750 SbxDimArray* pArray = new SbxDimArray( SbxVARIANT );
751 pArray->unoAddDim(0, -1);
752 SbxVariable* pArrayVar = new SbxVariable( SbxVARIANT );
753 pArrayVar->SetFlag( SbxFlagBits::ReadWrite );
754 pArrayVar->PutObject( pArray );
755 refParams->Put(pArrayVar, nParamCount);
756 }
757 }
758
759
760 // execute a P-Code
761
762 bool SbiRuntime::Step()
763 {
764 if( bRun )
765 {
766 static sal_uInt32 nLastTime = osl_getGlobalTimer();
767
768 // in any case check casually!
769 if( !( ++nOps & 0xF ) && pInst->IsReschedule() )
770 {
771 sal_uInt32 nTime = osl_getGlobalTimer();
772 if (nTime - nLastTime > 5) // 20 ms
773 {
774 nLastTime = nTime;
775 Application::Reschedule();
776 }
777 }
778
779 // #i48868 blocked by next call level?
780 while( bBlocked )
781 {
782 if( pInst->IsReschedule() ) // And what if not? Busy loop?
783 {
784 Application::Reschedule();
785 nLastTime = osl_getGlobalTimer();
786 }
787 }
788
789 SbiOpcode eOp = static_cast<SbiOpcode>( *pCode++ );
790 sal_uInt32 nOp1;
791 if (eOp <= SbiOpcode::SbOP0_END)
792 {
793 (this->*( aStep0[ int(eOp) ] ) )();
794 }
795 else if (eOp >= SbiOpcode::SbOP1_START && eOp <= SbiOpcode::SbOP1_END)
796 {
797 nOp1 = *pCode++; nOp1 |= *pCode++ << 8; nOp1 |= *pCode++ << 16; nOp1 |= *pCode++ << 24;
798
799 (this->*( aStep1[ int(eOp) - int(SbiOpcode::SbOP1_START) ] ) )( nOp1 );
800 }
801 else if (eOp >= SbiOpcode::SbOP2_START && eOp <= SbiOpcode::SbOP2_END)
802 {
803 nOp1 = *pCode++; nOp1 |= *pCode++ << 8; nOp1 |= *pCode++ << 16; nOp1 |= *pCode++ << 24;
804 sal_uInt32 nOp2 = *pCode++; nOp2 |= *pCode++ << 8; nOp2 |= *pCode++ << 16; nOp2 |= *pCode++ << 24;
805 (this->*( aStep2[ int(eOp) - int(SbiOpcode::SbOP2_START) ] ) )( nOp1, nOp2 );
806 }
807 else
808 {
809 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
810 }
811
812 ErrCode nErrCode = SbxBase::GetError();
813 Error( nErrCode.IgnoreWarning() );
814
815 // from 13.2.1997, new error handling:
816 // ATTENTION: nError can be set already even if !nErrCode
817 // since nError can now also be set from other RT-instances
818
819 if( nError )
820 {
821 SbxBase::ResetError();
822 }
823
824 // from 15.3.96: display errors only if BASIC is still active
825 // (especially not after compiler errors at the runtime)
826 if( nError && bRun )
827 {
828 ErrCode err = nError;
829 ClearExprStack();
830 nError = ERRCODE_NONE;
831 pInst->nErr = err;
832 pInst->nErl = nLine;
833 pErrCode = pCode;
834 pErrStmnt = pStmnt;
835 // An error occurred in an error handler
836 // force parent handler ( if there is one )
837 // to handle the error
838 bool bLetParentHandleThis = false;
839
840 // in the error handler? so std-error
841 if ( !bInError )
842 {
843 bInError = true;
844
845 if( !bError ) // On Error Resume Next
846 {
847 StepRESUME( 1 );
848 }
849 else if( pError ) // On Error Goto ...
850 {
851 pCode = pError;
852 }
853 else
854 {
855 bLetParentHandleThis = true;
856 }
857 }
858 else
859 {
860 bLetParentHandleThis = true;
861 pError = nullptr; //terminate the handler
862 }
863 if ( bLetParentHandleThis )
864 {
865 // from 13.2.1997, new error handling:
866 // consider superior error handlers
867
868 // there's no error handler -> find one farther above
869 SbiRuntime* pRtErrHdl = nullptr;
870 SbiRuntime* pRt = this;
871 while( (pRt = pRt->pNext) != nullptr )
872 {
873 if( !pRt->bError || pRt->pError != nullptr )
874 {
875 pRtErrHdl = pRt;
876 break;
877 }
878 }
879
880
881 if( pRtErrHdl )
882 {
883 // manipulate all the RTs that are below in the call-stack
884 pRt = this;
885 do
886 {
887 pRt->nError = err;
888 if( pRt != pRtErrHdl )
889 {
890 pRt->bRun = false;
891 }
892 else
893 {
894 break;
895 }
896 pRt = pRt->pNext;
897 }
898 while( pRt );
899 }
900 // no error-hdl found -> old behaviour
901 else
902 {
903 pInst->Abort();
904 }
905 }
906 }
907 }
908 return bRun;
909 }
910
911 void SbiRuntime::Error( ErrCode n, bool bVBATranslationAlreadyDone )
912 {
913 if( !n )
914 return;
915
916 nError = n;
917 if( !isVBAEnabled() || bVBATranslationAlreadyDone )
918 return;
919
920 OUString aMsg = pInst->GetErrorMsg();
921 sal_Int32 nVBAErrorNumber = translateErrorToVba( nError, aMsg );
922 SbxVariable* pSbxErrObjVar = SbxErrObject::getErrObject().get();
923 SbxErrObject* pGlobErr = static_cast< SbxErrObject* >( pSbxErrObjVar );
924 if( pGlobErr != nullptr )
925 {
926 pGlobErr->setNumberAndDescription( nVBAErrorNumber, aMsg );
927 }
928 pInst->aErrorMsg = aMsg;
929 nError = ERRCODE_BASIC_COMPAT;
930 }
931
932 void SbiRuntime::Error( ErrCode _errCode, const OUString& _details )
933 {
934 if ( !_errCode )
935 return;
936
937 // Not correct for class module usage, remove for now
938 //OSL_WARN_IF( pInst->pRun != this, "basic", "SbiRuntime::Error: can't propagate the error message details!" );
939 if ( pInst->pRun == this )
940 {
941 pInst->Error( _errCode, _details );
942 //OSL_WARN_IF( nError != _errCode, "basic", "SbiRuntime::Error: the instance is expected to propagate the error code back to me!" );
943 }
944 else
945 {
946 nError = _errCode;
947 }
948 }
949
950 void SbiRuntime::FatalError( ErrCode n )
951 {
952 StepSTDERROR();
953 Error( n );
954 }
955
956 void SbiRuntime::FatalError( ErrCode _errCode, const OUString& _details )
957 {
958 StepSTDERROR();
959 Error( _errCode, _details );
960 }
961
962 sal_Int32 SbiRuntime::translateErrorToVba( ErrCode nError, OUString& rMsg )
963 {
964 // If a message is defined use that ( in preference to
965 // the defined one for the error ) NB #TODO
966 // if there is an error defined it more than likely
967 // is not the one you want ( some are the same though )
968 // we really need a new vba compatible error list
969 // tdf#123144 - always translate an error number to a vba error message
970 StarBASIC::MakeErrorText( nError, rMsg );
971 rMsg = StarBASIC::GetErrorText();
972 // no num? most likely then it *is* really a vba err
973 sal_uInt16 nVBErrorCode = StarBASIC::GetVBErrorCode( nError );
974 sal_Int32 nVBAErrorNumber = ( nVBErrorCode == 0 ) ? sal_uInt32(nError) : nVBErrorCode;
975 return nVBAErrorNumber;
976 }
977
978 // Stacks
979
980 // The expression-stack is available for the continuous evaluation
981 // of expressions.
982
983 void SbiRuntime::PushVar( SbxVariable* pVar )
984 {
985 if( pVar )
986 {
987 refExprStk->Put(pVar, nExprLvl++);
988 }
989 }
990
991 SbxVariableRef SbiRuntime::PopVar()
992 {
993 #ifdef DBG_UTIL
994 if( !nExprLvl )
995 {
996 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
997 return new SbxVariable;
998 }
999 #endif
1000 SbxVariableRef xVar = refExprStk->Get(--nExprLvl);
1001 SAL_INFO_IF( xVar->GetName() == "Cells", "basic", "PopVar: Name equals 'Cells'" );
1002 // methods hold themselves in parameter 0
1003 if( dynamic_cast<const SbxMethod *>(xVar.get()) != nullptr )
1004 {
1005 xVar->SetParameters(nullptr);
1006 }
1007 return xVar;
1008 }
1009
1010 void SbiRuntime::ClearExprStack()
1011 {
1012 // Attention: Clear() doesn't suffice as methods must be deleted
1013 while ( nExprLvl )
1014 {
1015 PopVar();
1016 }
1017 refExprStk->Clear();
1018 }
1019
1020 // Take variable from the expression-stack without removing it
1021 // n counts from 0
1022
1023 SbxVariable* SbiRuntime::GetTOS()
1024 {
1025 short n = nExprLvl - 1;
1026 #ifdef DBG_UTIL
1027 if( n < 0 )
1028 {
1029 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
1030 return new SbxVariable;
1031 }
1032 #endif
1033 return refExprStk->Get(static_cast<sal_uInt32>(n));
1034 }
1035
1036
1037 void SbiRuntime::TOSMakeTemp()
1038 {
1039 SbxVariable* p = refExprStk->Get(nExprLvl - 1);
1040 if ( p->GetType() == SbxEMPTY )
1041 {
1042 p->Broadcast( SfxHintId::BasicDataWanted );
1043 }
1044
1045 SbxVariable* pDflt = nullptr;
1046 if ( bVBAEnabled && ( p->GetType() == SbxOBJECT || p->GetType() == SbxVARIANT ) && ((pDflt = getDefaultProp(p)) != nullptr) )
1047 {
1048 pDflt->Broadcast( SfxHintId::BasicDataWanted );
1049 // replacing new p on stack causes object pointed by
1050 // pDft->pParent to be deleted, when p2->Compute() is
1051 // called below pParent is accessed (but it's deleted)
1052 // so set it to NULL now
1053 pDflt->SetParent( nullptr );
1054 p = new SbxVariable( *pDflt );
1055 p->SetFlag( SbxFlagBits::ReadWrite );
1056 refExprStk->Put(p, nExprLvl - 1);
1057 }
1058 else if( p->GetRefCount() != 1 )
1059 {
1060 SbxVariable* pNew = new SbxVariable( *p );
1061 pNew->SetFlag( SbxFlagBits::ReadWrite );
1062 refExprStk->Put(pNew, nExprLvl - 1);
1063 }
1064 }
1065
1066 // the GOSUB-stack collects return-addresses for GOSUBs
1067 void SbiRuntime::PushGosub( const sal_uInt8* pc )
1068 {
1069 if( pGosubStk.size() >= MAXRECURSION )
1070 {
1071 StarBASIC::FatalError( ERRCODE_BASIC_STACK_OVERFLOW );
1072 }
1073 pGosubStk.emplace_back(pc, nForLvl);
1074 }
1075
1076 void SbiRuntime::PopGosub()
1077 {
1078 if( pGosubStk.empty() )
1079 {
1080 Error( ERRCODE_BASIC_NO_GOSUB );
1081 }
1082 else
1083 {
1084 pCode = pGosubStk.back().pCode;
1085 pGosubStk.pop_back();
1086 }
1087 }
1088
1089 // the Argv-stack collects current argument-vectors
1090
1091 void SbiRuntime::PushArgv()
1092 {
1093 pArgvStk.emplace_back(refArgv, nArgc);
1094 nArgc = 1;
1095 refArgv.clear();
1096 }
1097
1098 void SbiRuntime::PopArgv()
1099 {
1100 if( !pArgvStk.empty() )
1101 {
1102 refArgv = pArgvStk.back().refArgv;
1103 nArgc = pArgvStk.back().nArgc;
1104 pArgvStk.pop_back();
1105 }
1106 }
1107
1108
1109 void SbiRuntime::ClearArgvStack()
1110 {
1111 while( !pArgvStk.empty() )
1112 {
1113 PopArgv();
1114 }
1115 }
1116
1117 // Push of the for-stack. The stack has increment, end, begin and variable.
1118 // After the creation of the stack-element the stack's empty.
1119
1120 void SbiRuntime::PushFor()
1121 {
1122 SbiForStack* p = new SbiForStack;
1123 p->eForType = ForType::To;
1124 p->pNext = pForStk;
1125 pForStk = p;
1126
1127 p->refInc = PopVar();
1128 p->refEnd = PopVar();
1129 if (isVBAEnabled())
1130 {
1131 // tdf#150458: only calculate these once, coercing to double
1132 // tdf#150460: shouldn't we do it in non-VBA / compat mode, too?
1133 SbxVariableRef incCopy(new SbxVariable(SbxDOUBLE));
1134 *incCopy = *p->refInc;
1135 p->refInc = std::move(incCopy);
1136 SbxVariableRef endCopy(new SbxVariable(SbxDOUBLE));
1137 *endCopy = *p->refEnd;
1138 p->refEnd = std::move(endCopy);
1139 }
1140 SbxVariableRef xBgn = PopVar();
1141 p->refVar = PopVar();
1142 // tdf#85371 - grant explicitly write access to the index variable
1143 // since it could be the name of a method itself used in the next statement.
1144 ScopedWritableGuard aGuard(p->refVar, p->refVar.get() == pMeth);
1145 *(p->refVar) = *xBgn;
1146 nForLvl++;
1147 }
1148
1149 void SbiRuntime::PushForEach()
1150 {
1151 SbiForStack* p = new SbiForStack;
1152 // Set default value in case of error which is ignored in Resume Next
1153 p->eForType = ForType::EachArray;
1154 p->pNext = pForStk;
1155 pForStk = p;
1156
1157 SbxVariableRef xObjVar = PopVar();
1158 SbxBase* pObj(nullptr);
1159 if (xObjVar)
1160 {
1161 SbxValues v(SbxVARIANT);
1162 // Here it may retrieve the value, and change the type from SbxEMPTY to SbxOBJECT
1163 xObjVar->Get(v);
1164 if (v.eType == SbxOBJECT)
1165 pObj = v.pObj;
1166 }
1167
1168 if (SbxDimArray* pArray = dynamic_cast<SbxDimArray*>(pObj))
1169 {
1170 p->refEnd = reinterpret_cast<SbxVariable*>(pArray);
1171
1172 sal_Int32 nDims = pArray->GetDims();
1173 p->pArrayLowerBounds.reset( new sal_Int32[nDims] );
1174 p->pArrayUpperBounds.reset( new sal_Int32[nDims] );
1175 p->pArrayCurIndices.reset( new sal_Int32[nDims] );
1176 sal_Int32 lBound, uBound;
1177 for( sal_Int32 i = 0 ; i < nDims ; i++ )
1178 {
1179 pArray->GetDim(i + 1, lBound, uBound);
1180 p->pArrayCurIndices[i] = p->pArrayLowerBounds[i] = lBound;
1181 p->pArrayUpperBounds[i] = uBound;
1182 }
1183 }
1184 else if (BasicCollection* pCollection = dynamic_cast<BasicCollection*>(pObj))
1185 {
1186 p->eForType = ForType::EachCollection;
1187 p->refEnd = pCollection;
1188 p->nCurCollectionIndex = 0;
1189 }
1190 else if (SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>(pObj))
1191 {
1192 // XEnumerationAccess or XIndexAccess?
1193 Any aAny = pUnoObj->getUnoAny();
1194 Reference<XIndexAccess> xIndexAccess;
1195 Reference< XEnumerationAccess > xEnumerationAccess;
1196 if( aAny >>= xEnumerationAccess )
1197 {
1198 p->xEnumeration = xEnumerationAccess->createEnumeration();
1199 p->eForType = ForType::EachXEnumeration;
1200 }
1201 // tdf#130307 - support for each loop for objects exposing XIndexAccess
1202 else if (aAny >>= xIndexAccess)
1203 {
1204 p->eForType = ForType::EachXIndexAccess;
1205 p->xIndexAccess = std::move(xIndexAccess);
1206 p->nCurCollectionIndex = 0;
1207 }
1208 else if ( isVBAEnabled() && pUnoObj->isNativeCOMObject() )
1209 {
1210 uno::Reference< script::XInvocation > xInvocation;
1211 if ( ( aAny >>= xInvocation ) && xInvocation.is() )
1212 {
1213 try
1214 {
1215 p->xEnumeration = new ComEnumerationWrapper( xInvocation );
1216 p->eForType = ForType::EachXEnumeration;
1217 }
1218 catch(const uno::Exception& )
1219 {}
1220 }
1221 }
1222 }
1223
1224 // Container variable
1225 p->refVar = PopVar();
1226 nForLvl++;
1227 }
1228
1229
1230 void SbiRuntime::PopFor()
1231 {
1232 if( pForStk )
1233 {
1234 SbiForStack* p = pForStk;
1235 pForStk = p->pNext;
1236 delete p;
1237 nForLvl--;
1238 }
1239 }
1240
1241
1242 void SbiRuntime::ClearForStack()
1243 {
1244 while( pForStk )
1245 {
1246 PopFor();
1247 }
1248 }
1249
1250 SbiForStack* SbiRuntime::FindForStackItemForCollection( class BasicCollection const * pCollection )
1251 {
1252 for (SbiForStack *p = pForStk; p; p = p->pNext)
1253 {
1254 SbxVariable* pVar = p->refEnd.is() ? p->refEnd.get() : nullptr;
1255 if( p->eForType == ForType::EachCollection
1256 && pVar != nullptr
1257 && dynamic_cast<BasicCollection*>( pVar) == pCollection )
1258 {
1259 return p;
1260 }
1261 }
1262
1263 return nullptr;
1264 }
1265
1266
1267 // DLL-calls
1268
1269 void SbiRuntime::DllCall
1270 ( std::u16string_view aFuncName,
1271 std::u16string_view aDLLName,
1272 SbxArray* pArgs, // parameter (from index 1, can be NULL)
1273 SbxDataType eResType, // return value
1274 bool bCDecl ) // true: according to C-conventions
1275 {
1276 SbxVariable* pRes = new SbxVariable( eResType );
1277 SbiDllMgr* pDllMgr = pInst->GetDllMgr();
1278 ErrCode nErr = pDllMgr->Call( aFuncName, aDLLName, pArgs, *pRes, bCDecl );
1279 if( nErr )
1280 {
1281 Error( nErr );
1282 }
1283 PushVar( pRes );
1284 }
1285
1286 bool SbiRuntime::IsImageFlag( SbiImageFlags n ) const
1287 {
1288 return pImg->IsFlag( n );
1289 }
1290
1291 sal_uInt16 SbiRuntime::GetBase() const
1292 {
1293 return pImg->GetBase();
1294 }
1295
1296 void SbiRuntime::StepNOP()
1297 {}
1298
1299 void SbiRuntime::StepArith( SbxOperator eOp )
1300 {
1301 SbxVariableRef p1 = PopVar();
1302 TOSMakeTemp();
1303 SbxVariable* p2 = GetTOS();
1304
1305 // tdf#144353 - do not compute any operation with a missing optional variable
1306 if ((p1->GetType() == SbxERROR && IsMissing(p1.get(), 1))
1307 || (p2->GetType() == SbxERROR && IsMissing(p2, 1)))
1308 {
1309 Error(ERRCODE_BASIC_NOT_OPTIONAL);
1310 return;
1311 }
1312
1313 p2->ResetFlag( SbxFlagBits::Fixed );
1314 p2->Compute( eOp, *p1 );
1315
1316 checkArithmeticOverflow( p2 );
1317 }
1318
1319 void SbiRuntime::StepUnary( SbxOperator eOp )
1320 {
1321 TOSMakeTemp();
1322 SbxVariable* p = GetTOS();
1323 // tdf#144353 - do not compute any operation with a missing optional variable
1324 if (p->GetType() == SbxERROR && IsMissing(p, 1))
1325 {
1326 Error(ERRCODE_BASIC_NOT_OPTIONAL);
1327 return;
1328 }
1329 p->Compute( eOp, *p );
1330 }
1331
1332 void SbiRuntime::StepCompare( SbxOperator eOp )
1333 {
1334 SbxVariableRef p1 = PopVar();
1335 SbxVariableRef p2 = PopVar();
1336
1337 // tdf#144353 - do not compare a missing optional variable
1338 if ((p1->GetType() == SbxERROR && SbiRuntime::IsMissing(p1.get(), 1))
1339 || (p2->GetType() == SbxERROR && SbiRuntime::IsMissing(p2.get(), 1)))
1340 {
1341 SbxBase::SetError(ERRCODE_BASIC_NOT_OPTIONAL);
1342 return;
1343 }
1344
1345 // Make sure objects with default params have
1346 // values ( and type ) set as appropriate
1347 SbxDataType p1Type = p1->GetType();
1348 SbxDataType p2Type = p2->GetType();
1349 if ( p1Type == SbxEMPTY )
1350 {
1351 p1->Broadcast( SfxHintId::BasicDataWanted );
1352 p1Type = p1->GetType();
1353 }
1354 if ( p2Type == SbxEMPTY )
1355 {
1356 p2->Broadcast( SfxHintId::BasicDataWanted );
1357 p2Type = p2->GetType();
1358 }
1359 if ( p1Type == p2Type )
1360 {
1361 // if both sides are an object and have default props
1362 // then we need to use the default props
1363 // we don't need to worry if only one side ( lhs, rhs ) is an
1364 // object ( object side will get coerced to correct type in
1365 // Compare )
1366 if ( p1Type == SbxOBJECT )
1367 {
1368 SbxVariable* pDflt = getDefaultProp( p1.get() );
1369 if ( pDflt )
1370 {
1371 p1 = pDflt;
1372 p1->Broadcast( SfxHintId::BasicDataWanted );
1373 }
1374 pDflt = getDefaultProp( p2.get() );
1375 if ( pDflt )
1376 {
1377 p2 = pDflt;
1378 p2->Broadcast( SfxHintId::BasicDataWanted );
1379 }
1380 }
1381
1382 }
1383 static SbxVariable* pTRUE = nullptr;
1384 static SbxVariable* pFALSE = nullptr;
1385 // why do this on non-windows ?
1386 // why do this at all ?
1387 // I dumbly follow the pattern :-/
1388 if ( bVBAEnabled && ( p1->IsNull() || p2->IsNull() ) )
1389 {
1390 static SbxVariable* pNULL = []() {
1391 SbxVariable* p = new SbxVariable;
1392 p->PutNull();
1393 p->AddFirstRef();
1394 return p;
1395 }();
1396 PushVar( pNULL );
1397 }
1398 else if( p2->Compare( eOp, *p1 ) )
1399 {
1400 if( !pTRUE )
1401 {
1402 pTRUE = new SbxVariable;
1403 pTRUE->PutBool( true );
1404 pTRUE->AddFirstRef();
1405 }
1406 PushVar( pTRUE );
1407 }
1408 else
1409 {
1410 if( !pFALSE )
1411 {
1412 pFALSE = new SbxVariable;
1413 pFALSE->PutBool( false );
1414 pFALSE->AddFirstRef();
1415 }
1416 PushVar( pFALSE );
1417 }
1418 }
1419
1420 void SbiRuntime::StepEXP() { StepArith( SbxEXP ); }
1421 void SbiRuntime::StepMUL() { StepArith( SbxMUL ); }
1422 void SbiRuntime::StepDIV() { StepArith( SbxDIV ); }
1423 void SbiRuntime::StepIDIV() { StepArith( SbxIDIV ); }
1424 void SbiRuntime::StepMOD() { StepArith( SbxMOD ); }
1425 void SbiRuntime::StepPLUS() { StepArith( SbxPLUS ); }
1426 void SbiRuntime::StepMINUS() { StepArith( SbxMINUS ); }
1427 void SbiRuntime::StepCAT() { StepArith( SbxCAT ); }
1428 void SbiRuntime::StepAND() { StepArith( SbxAND ); }
1429 void SbiRuntime::StepOR() { StepArith( SbxOR ); }
1430 void SbiRuntime::StepXOR() { StepArith( SbxXOR ); }
1431 void SbiRuntime::StepEQV() { StepArith( SbxEQV ); }
1432 void SbiRuntime::StepIMP() { StepArith( SbxIMP ); }
1433
1434 void SbiRuntime::StepNEG() { StepUnary( SbxNEG ); }
1435 void SbiRuntime::StepNOT() { StepUnary( SbxNOT ); }
1436
1437 void SbiRuntime::StepEQ() { StepCompare( SbxEQ ); }
1438 void SbiRuntime::StepNE() { StepCompare( SbxNE ); }
1439 void SbiRuntime::StepLT() { StepCompare( SbxLT ); }
1440 void SbiRuntime::StepGT() { StepCompare( SbxGT ); }
1441 void SbiRuntime::StepLE() { StepCompare( SbxLE ); }
1442 void SbiRuntime::StepGE() { StepCompare( SbxGE ); }
1443
1444 namespace
1445 {
1446 OUString VBALikeToRegexp(std::u16string_view sIn)
1447 {
1448 OUStringBuffer sResult("\\A"); // Match at the beginning of the input
1449
1450 for (auto start = sIn.begin(), end = sIn.end(); start < end;)
1451 {
1452 switch (auto ch = *start++)
1453 {
1454 case '?':
1455 sResult.append('.');
1456 break;
1457 case '*':
1458 sResult.append(".*");
1459 break;
1460 case '#':
1461 sResult.append("[0-9]");
1462 break;
1463 case '[':
1464 sResult.append(ch);
1465 if (start < end)
1466 {
1467 if (*start == '!')
1468 {
1469 sResult.append('^');
1470 ++start;
1471 }
1472 else if (*start == '^')
1473 sResult.append('\\');
1474 }
1475 for (bool seenright = false; start < end && !seenright; ++start)
1476 {
1477 switch (*start)
1478 {
1479 case '[':
1480 case '\\':
1481 sResult.append('\\');
1482 break;
1483 case ']':
1484 seenright = true;
1485 break;
1486 }
1487 sResult.append(*start);
1488 }
1489 break;
1490 case '.':
1491 case '^':
1492 case '$':
1493 case '+':
1494 case '\\':
1495 case '|':
1496 case '{':
1497 case '}':
1498 case '(':
1499 case ')':
1500 sResult.append('\\');
1501 [[fallthrough]];
1502 default:
1503 sResult.append(ch);
1504 }
1505 }
1506
1507 sResult.append("\\z"); // Match if the current position is at the end of input
1508
1509 return sResult.makeStringAndClear();
1510 }
1511 }
1512
1513 void SbiRuntime::StepLIKE()
1514 {
1515 SbxVariableRef refVar1 = PopVar();
1516 SbxVariableRef refVar2 = PopVar();
1517
1518 OUString value = refVar2->GetOUString();
1519 OUString regex = VBALikeToRegexp(refVar1->GetOUString());
1520
1521 bool bTextMode(true);
1522 bool bCompatibility = ( GetSbData()->pInst && GetSbData()->pInst->IsCompatibility() );
1523 if( bCompatibility )
1524 {
1525 bTextMode = IsImageFlag( SbiImageFlags::COMPARETEXT );
1526 }
1527 uint32_t searchFlags = UREGEX_UWORD | UREGEX_DOTALL; // Dot matches newline
1528 if( bTextMode )
1529 {
1530 searchFlags |= UREGEX_CASE_INSENSITIVE;
1531 }
1532
1533 static uint32_t cachedSearchFlags = 0;
1534 static OUString cachedRegex;
1535 static std::optional<icu::RegexMatcher> oRegexMatcher;
1536 UErrorCode nIcuErr = U_ZERO_ERROR;
1537 if (regex != cachedRegex || searchFlags != cachedSearchFlags || !oRegexMatcher)
1538 {
1539 cachedRegex = regex;
1540 cachedSearchFlags = searchFlags;
1541 icu::UnicodeString sRegex(false, reinterpret_cast<const UChar*>(cachedRegex.getStr()),
1542 cachedRegex.getLength());
1543 oRegexMatcher.emplace(sRegex, cachedSearchFlags, nIcuErr);
1544 if (U_FAILURE(nIcuErr))
1545 {
1546 // Bad regex; oRegexMatcher has no pattern - would crash in RegexMatcher::reset
1547 oRegexMatcher.reset();
1548 return Error(ERRCODE_BASIC_BAD_PATTERN);
1549 }
1550 }
1551
1552 icu::UnicodeString sSource(false, reinterpret_cast<const UChar*>(value.getStr()),
1553 value.getLength());
1554 oRegexMatcher->reset(sSource);
1555
1556 bool bRes = oRegexMatcher->matches(nIcuErr);
1557 if (nIcuErr)
1558 {
1559 Error(ERRCODE_BASIC_INTERNAL_ERROR);
1560 }
1561 SbxVariable* pRes = new SbxVariable;
1562 pRes->PutBool( bRes );
1563
1564 PushVar( pRes );
1565 }
1566
1567 // TOS and TOS-1 are both object variables and contain the same pointer
1568
1569 void SbiRuntime::StepIS()
1570 {
1571 SbxVariableRef refVar1 = PopVar();
1572 SbxVariableRef refVar2 = PopVar();
1573
1574 SbxDataType eType1 = refVar1->GetType();
1575 SbxDataType eType2 = refVar2->GetType();
1576 if ( eType1 == SbxEMPTY )
1577 {
1578 refVar1->Broadcast( SfxHintId::BasicDataWanted );
1579 eType1 = refVar1->GetType();
1580 }
1581 if ( eType2 == SbxEMPTY )
1582 {
1583 refVar2->Broadcast( SfxHintId::BasicDataWanted );
1584 eType2 = refVar2->GetType();
1585 }
1586
1587 bool bRes = ( eType1 == SbxOBJECT && eType2 == SbxOBJECT );
1588 if ( bVBAEnabled && !bRes )
1589 {
1590 Error( ERRCODE_BASIC_INVALID_USAGE_OBJECT );
1591 }
1592 bRes = ( bRes && refVar1->GetObject() == refVar2->GetObject() );
1593 SbxVariable* pRes = new SbxVariable;
1594 pRes->PutBool( bRes );
1595 PushVar( pRes );
1596 }
1597
1598 // update the value of TOS
1599
1600 void SbiRuntime::StepGET()
1601 {
1602 SbxVariable* p = GetTOS();
1603 p->Broadcast( SfxHintId::BasicDataWanted );
1604 }
1605
1606 // #67607 copy Uno-Structs
1607 static bool checkUnoStructCopy( bool bVBA, SbxVariableRef const & refVal, SbxVariableRef const & refVar )
1608 {
1609 SbxDataType eVarType = refVar->GetType();
1610 SbxDataType eValType = refVal->GetType();
1611
1612 // tdf#144353 - do not assign a missing optional variable to a property
1613 if (refVal->GetType() == SbxERROR && SbiRuntime::IsMissing(refVal.get(), 1))
1614 {
1615 SbxBase::SetError(ERRCODE_BASIC_NOT_OPTIONAL);
1616 return true;
1617 }
1618
1619 if ( ( bVBA && ( eVarType == SbxEMPTY ) ) || !refVar->CanWrite() )
1620 return false;
1621
1622 if ( eValType != SbxOBJECT )
1623 return false;
1624 // we seem to be duplicating parts of SbxValue=operator, maybe we should just move this to
1625 // there :-/ not sure if for every '=' we would want struct handling
1626 if( eVarType != SbxOBJECT )
1627 {
1628 if ( refVar->IsFixed() )
1629 return false;
1630 }
1631 // #115826: Exclude ProcedureProperties to avoid call to Property Get procedure
1632 else if( dynamic_cast<const SbProcedureProperty*>( refVar.get() ) != nullptr )
1633 return false;
1634
1635 SbxObjectRef xValObj = static_cast<SbxObject*>(refVal->GetObject());
1636 if( !xValObj.is() || dynamic_cast<const SbUnoAnyObject*>( xValObj.get() ) != nullptr )
1637 return false;
1638
1639 SbUnoObject* pUnoVal = dynamic_cast<SbUnoObject*>( xValObj.get() );
1640 SbUnoStructRefObject* pUnoStructVal = dynamic_cast<SbUnoStructRefObject*>( xValObj.get() );
1641 Any aAny;
1642 // make doubly sure value is either a Uno object or
1643 // a uno struct
1644 if ( pUnoVal || pUnoStructVal )
1645 aAny = pUnoVal ? pUnoVal->getUnoAny() : pUnoStructVal->getUnoAny();
1646 else
1647 return false;
1648 if ( aAny.getValueTypeClass() != TypeClass_STRUCT )
1649 return false;
1650
1651 refVar->SetType( SbxOBJECT );
1652 ErrCode eOldErr = SbxBase::GetError();
1653 // There are some circumstances when calling GetObject
1654 // will trigger an error, we need to squash those here.
1655 // Alternatively it is possible that the same scenario
1656 // could overwrite and existing error. Let's prevent that
1657 SbxObjectRef xVarObj = static_cast<SbxObject*>(refVar->GetObject());
1658 if ( eOldErr != ERRCODE_NONE )
1659 SbxBase::SetError( eOldErr );
1660 else
1661 SbxBase::ResetError();
1662
1663 SbUnoStructRefObject* pUnoStructObj = dynamic_cast<SbUnoStructRefObject*>( xVarObj.get() );
1664
1665 OUString sClassName = pUnoVal ? pUnoVal->GetClassName() : pUnoStructVal->GetClassName();
1666 OUString sName = pUnoVal ? pUnoVal->GetName() : pUnoStructVal->GetName();
1667
1668 if ( pUnoStructObj )
1669 {
1670 StructRefInfo aInfo = pUnoStructObj->getStructInfo();
1671 aInfo.setValue( aAny );
1672 }
1673 else
1674 {
1675 SbUnoObject* pNewUnoObj = new SbUnoObject( sName, aAny );
1676 // #70324: adopt ClassName
1677 pNewUnoObj->SetClassName( sClassName );
1678 refVar->PutObject( pNewUnoObj );
1679 }
1680 return true;
1681 }
1682
1683
1684 // laying down TOS in TOS-1
1685
1686 void SbiRuntime::StepPUT()
1687 {
1688 SbxVariableRef refVal = PopVar();
1689 SbxVariableRef refVar = PopVar();
1690 // store on its own method (inside a function)?
1691 bool bFlagsChanged = false;
1692 SbxFlagBits n = SbxFlagBits::NONE;
1693 if( refVar.get() == pMeth )
1694 {
1695 bFlagsChanged = true;
1696 n = refVar->GetFlags();
1697 refVar->SetFlag( SbxFlagBits::Write );
1698 }
1699
1700 // if left side arg is an object or variant and right handside isn't
1701 // either an object or a variant then try and see if a default
1702 // property exists.
1703 // to use e.g. Range{"A1") = 34
1704 // could equate to Range("A1").Value = 34
1705 if ( bVBAEnabled )
1706 {
1707 // yet more hacking at this, I feel we don't quite have the correct
1708 // heuristics for dealing with obj1 = obj2 ( where obj2 ( and maybe
1709 // obj1 ) has default member/property ) ) It seems that default props
1710 // aren't dealt with if the object is a member of some parent object
1711 bool bObjAssign = false;
1712 if ( refVar->GetType() == SbxEMPTY )
1713 refVar->Broadcast( SfxHintId::BasicDataWanted );
1714 if ( refVar->GetType() == SbxOBJECT )
1715 {
1716 if ( dynamic_cast<const SbxMethod *>(refVar.get()) != nullptr || ! refVar->GetParent() )
1717 {
1718 SbxVariable* pDflt = getDefaultProp( refVar.get() );
1719
1720 if ( pDflt )
1721 refVar = pDflt;
1722 }
1723 else
1724 bObjAssign = true;
1725 }
1726 if ( refVal->GetType() == SbxOBJECT && !bObjAssign && ( dynamic_cast<const SbxMethod *>(refVal.get()) != nullptr || ! refVal->GetParent() ) )
1727 {
1728 SbxVariable* pDflt = getDefaultProp( refVal.get() );
1729 if ( pDflt )
1730 refVal = pDflt;
1731 }
1732 }
1733
1734 if ( !checkUnoStructCopy( bVBAEnabled, refVal, refVar ) )
1735 *refVar = *refVal;
1736
1737 if( bFlagsChanged )
1738 refVar->SetFlags( n );
1739 }
1740
1741 namespace {
1742
1743 // VBA Dim As New behavior handling, save init object information
1744 struct DimAsNewRecoverItem
1745 {
1746 OUString m_aObjClass;
1747 OUString m_aObjName;
1748 SbxObject* m_pObjParent;
1749 SbModule* m_pClassModule;
1750
1751 DimAsNewRecoverItem()
1752 : m_pObjParent( nullptr )
1753 , m_pClassModule( nullptr )
1754 {}
1755
1756 DimAsNewRecoverItem( OUString aObjClass, OUString aObjName,
1757 SbxObject* pObjParent, SbModule* pClassModule )
1758 : m_aObjClass(std::move( aObjClass ))
1759 , m_aObjName(std::move( aObjName ))
1760 , m_pObjParent( pObjParent )
1761 , m_pClassModule( pClassModule )
1762 {}
1763
1764 };
1765
1766
1767 struct SbxVariablePtrHash
1768 {
1769 size_t operator()( SbxVariable* pVar ) const
1770 { return reinterpret_cast<size_t>(pVar); }
1771 };
1772
1773 }
1774
1775 typedef std::unordered_map< SbxVariable*, DimAsNewRecoverItem,
1776 SbxVariablePtrHash > DimAsNewRecoverHash;
1777
1778 namespace {
1779
1780 DimAsNewRecoverHash gaDimAsNewRecoverHash;
1781
1782 }
1783
1784 void removeDimAsNewRecoverItem( SbxVariable* pVar )
1785 {
1786 DimAsNewRecoverHash::iterator it = gaDimAsNewRecoverHash.find( pVar );
1787 if( it != gaDimAsNewRecoverHash.end() )
1788 {
1789 gaDimAsNewRecoverHash.erase( it );
1790 }
1791 }
1792
1793
1794 // saving object variable
1795 // not-object variables will cause errors
1796
1797 constexpr OUString pCollectionStr = u"Collection"_ustr;
1798
1799 void SbiRuntime::StepSET_Impl( SbxVariableRef& refVal, SbxVariableRef& refVar, bool bHandleDefaultProp )
1800 {
1801 // #67733 types with array-flag are OK too
1802
1803 // Check var, !object is no error for sure if, only if type is fixed
1804 SbxDataType eVarType = refVar->GetType();
1805 if( !bHandleDefaultProp && eVarType != SbxOBJECT && !(eVarType & SbxARRAY) && refVar->IsFixed() )
1806 {
1807 Error( ERRCODE_BASIC_INVALID_USAGE_OBJECT );
1808 return;
1809 }
1810
1811 // Check value, !object is no error for sure if, only if type is fixed
1812 SbxDataType eValType = refVal->GetType();
1813 if( !bHandleDefaultProp && eValType != SbxOBJECT && !(eValType & SbxARRAY) && refVal->IsFixed() )
1814 {
1815 Error( ERRCODE_BASIC_INVALID_USAGE_OBJECT );
1816 return;
1817 }
1818
1819 // Getting in here causes problems with objects with default properties
1820 // if they are SbxEMPTY I guess
1821 if ( !bHandleDefaultProp || eValType == SbxOBJECT )
1822 {
1823 // activate GetObject for collections on refVal
1824 SbxBase* pObjVarObj = refVal->GetObject();
1825 if( pObjVarObj )
1826 {
1827 SbxVariableRef refObjVal = dynamic_cast<SbxObject*>( pObjVarObj );
1828
1829 if( refObjVal.is() )
1830 {
1831 refVal = std::move(refObjVal);
1832 }
1833 else if( !(eValType & SbxARRAY) )
1834 {
1835 refVal = nullptr;
1836 }
1837 }
1838 }
1839
1840 // #52896 refVal can be invalid here, if uno-sequences - or more
1841 // general arrays - are assigned to variables that are declared
1842 // as an object!
1843 if( !refVal.is() )
1844 {
1845 Error( ERRCODE_BASIC_INVALID_USAGE_OBJECT );
1846 }
1847 else
1848 {
1849 bool bFlagsChanged = false;
1850 SbxFlagBits n = SbxFlagBits::NONE;
1851 if( refVar.get() == pMeth )
1852 {
1853 bFlagsChanged = true;
1854 n = refVar->GetFlags();
1855 refVar->SetFlag( SbxFlagBits::Write );
1856 }
1857 SbProcedureProperty* pProcProperty = dynamic_cast<SbProcedureProperty*>( refVar.get() );
1858 if( pProcProperty )
1859 {
1860 pProcProperty->setSet( true );
1861 }
1862 if ( bHandleDefaultProp )
1863 {
1864 // get default properties for lhs & rhs where necessary
1865 // SbxVariable* defaultProp = NULL; unused variable
1866 // LHS try determine if a default prop exists
1867 // again like in StepPUT (see there too ) we are tweaking the
1868 // heuristics again for when to assign an object reference or
1869 // use default members if they exist
1870 // #FIXME we really need to get to the bottom of this mess
1871 bool bObjAssign = false;
1872 if ( refVar->GetType() == SbxOBJECT )
1873 {
1874 if ( dynamic_cast<const SbxMethod *>(refVar.get()) != nullptr || ! refVar->GetParent() )
1875 {
1876 SbxVariable* pDflt = getDefaultProp( refVar.get() );
1877 if ( pDflt )
1878 {
1879 refVar = pDflt;
1880 }
1881 }
1882 else
1883 bObjAssign = true;
1884 }
1885 // RHS only get a default prop is the rhs has one
1886 if ( refVal->GetType() == SbxOBJECT )
1887 {
1888 // check if lhs is a null object
1889 // if it is then use the object not the default property
1890 SbxObject* pObj = dynamic_cast<SbxObject*>( refVar.get() );
1891
1892 // calling GetObject on a SbxEMPTY variable raises
1893 // object not set errors, make sure it's an Object
1894 if ( !pObj && refVar->GetType() == SbxOBJECT )
1895 {
1896 SbxBase* pObjVarObj = refVar->GetObject();
1897 pObj = dynamic_cast<SbxObject*>( pObjVarObj );
1898 }
1899 SbxVariable* pDflt = nullptr;
1900 if ( pObj && !bObjAssign )
1901 {
1902 // lhs is either a valid object || or has a defaultProp
1903 pDflt = getDefaultProp( refVal.get() );
1904 }
1905 if ( pDflt )
1906 {
1907 refVal = pDflt;
1908 }
1909 }
1910 }
1911
1912 // Handle Dim As New
1913 bool bDimAsNew = bVBAEnabled && refVar->IsSet( SbxFlagBits::DimAsNew );
1914 SbxBaseRef xPrevVarObj;
1915 if( bDimAsNew )
1916 {
1917 xPrevVarObj = refVar->GetObject();
1918 }
1919 // Handle withevents
1920 bool bWithEvents = refVar->IsSet( SbxFlagBits::WithEvents );
1921 if ( bWithEvents )
1922 {
1923 Reference< XInterface > xComListener;
1924
1925 SbxBase* pObj = refVal->GetObject();
1926 SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>( pObj );
1927 if( pUnoObj != nullptr )
1928 {
1929 Any aControlAny = pUnoObj->getUnoAny();
1930 OUString aDeclareClassName = refVar->GetDeclareClassName();
1931 OUString aPrefix = refVar->GetName();
1932 SbxObjectRef xScopeObj = refVar->GetParent();
1933 xComListener = createComListener( aControlAny, aDeclareClassName, aPrefix, xScopeObj );
1934
1935 refVal->SetDeclareClassName( aDeclareClassName );
1936 refVal->SetComListener( xComListener, &rBasic ); // Hold reference
1937 }
1938
1939 }
1940
1941 // lhs is a property who's value is currently (Empty e.g. no broadcast yet)
1942 // in this case if there is a default prop involved the value of the
1943 // default property may in fact be void so the type will also be SbxEMPTY
1944 // in this case we do not want to call checkUnoStructCopy 'cause that will
1945 // cause an error also
1946 if ( !checkUnoStructCopy( bHandleDefaultProp, refVal, refVar ) )
1947 {
1948 *refVar = *refVal;
1949 }
1950 if ( bDimAsNew )
1951 {
1952 if( dynamic_cast<const SbxObject*>( refVar.get() ) == nullptr )
1953 {
1954 SbxBase* pValObjBase = refVal->GetObject();
1955 if( pValObjBase == nullptr )
1956 {
1957 if( xPrevVarObj.is() )
1958 {
1959 // Object is overwritten with NULL, instantiate init object
1960 DimAsNewRecoverHash::iterator it = gaDimAsNewRecoverHash.find( refVar.get() );
1961 if( it != gaDimAsNewRecoverHash.end() )
1962 {
1963 const DimAsNewRecoverItem& rItem = it->second;
1964 if( rItem.m_pClassModule != nullptr )
1965 {
1966 SbClassModuleObject* pNewObj = new SbClassModuleObject(*rItem.m_pClassModule);
1967 pNewObj->SetName( rItem.m_aObjName );
1968 pNewObj->SetParent( rItem.m_pObjParent );
1969 refVar->PutObject( pNewObj );
1970 }
1971 else if( rItem.m_aObjClass.equalsIgnoreAsciiCase( pCollectionStr ) )
1972 {
1973 BasicCollection* pNewCollection = new BasicCollection( pCollectionStr );
1974 pNewCollection->SetName( rItem.m_aObjName );
1975 pNewCollection->SetParent( rItem.m_pObjParent );
1976 refVar->PutObject( pNewCollection );
1977 }
1978 }
1979 }
1980 }
1981 else
1982 {
1983 // Does old value exist?
1984 bool bFirstInit = !xPrevVarObj.is();
1985 if( bFirstInit )
1986 {
1987 // Store information to instantiate object later
1988 SbxObject* pValObj = dynamic_cast<SbxObject*>( pValObjBase );
1989 if( pValObj != nullptr )
1990 {
1991 OUString aObjClass = pValObj->GetClassName();
1992
1993 SbClassModuleObject* pClassModuleObj = dynamic_cast<SbClassModuleObject*>( pValObjBase );
1994 if( pClassModuleObj != nullptr )
1995 {
1996 SbModule& rClassModule = pClassModuleObj->getClassModule();
1997 gaDimAsNewRecoverHash[refVar.get()] =
1998 DimAsNewRecoverItem( aObjClass, pValObj->GetName(), pValObj->GetParent(), &rClassModule );
1999 }
2000 else if( aObjClass.equalsIgnoreAsciiCase( "Collection" ) )
2001 {
2002 gaDimAsNewRecoverHash[refVar.get()] =
2003 DimAsNewRecoverItem( aObjClass, pValObj->GetName(), pValObj->GetParent(), nullptr );
2004 }
2005 }
2006 }
2007 }
2008 }
2009 }
2010
2011 if( bFlagsChanged )
2012 {
2013 refVar->SetFlags( n );
2014 }
2015 }
2016 }
2017
2018 void SbiRuntime::StepSET()
2019 {
2020 SbxVariableRef refVal = PopVar();
2021 SbxVariableRef refVar = PopVar();
2022 StepSET_Impl( refVal, refVar, bVBAEnabled ); // this is really assignment
2023 }
2024
2025 void SbiRuntime::StepVBASET()
2026 {
2027 SbxVariableRef refVal = PopVar();
2028 SbxVariableRef refVar = PopVar();
2029 // don't handle default property
2030 StepSET_Impl( refVal, refVar ); // set obj = something
2031 }
2032
2033
2034 void SbiRuntime::StepLSET()
2035 {
2036 SbxVariableRef refVal = PopVar();
2037 SbxVariableRef refVar = PopVar();
2038 if( refVar->GetType() != SbxSTRING ||
2039 refVal->GetType() != SbxSTRING )
2040 {
2041 Error( ERRCODE_BASIC_INVALID_USAGE_OBJECT );
2042 }
2043 else
2044 {
2045 SbxFlagBits n = refVar->GetFlags();
2046 if( refVar.get() == pMeth )
2047 {
2048 refVar->SetFlag( SbxFlagBits::Write );
2049 }
2050 OUString aRefVarString = refVar->GetOUString();
2051 OUString aRefValString = refVal->GetOUString();
2052
2053 sal_Int32 nVarStrLen = aRefVarString.getLength();
2054 sal_Int32 nValStrLen = aRefValString.getLength();
2055 OUString aNewStr;
2056 if( nVarStrLen > nValStrLen )
2057 {
2058 OUStringBuffer buf(aRefValString);
2059 comphelper::string::padToLength(buf, nVarStrLen, ' ');
2060 aNewStr = buf.makeStringAndClear();
2061 }
2062 else
2063 {
2064 aNewStr = aRefValString.copy( 0, nVarStrLen );
2065 }
2066
2067 refVar->PutString(aNewStr);
2068 refVar->SetFlags( n );
2069 }
2070 }
2071
2072 void SbiRuntime::StepRSET()
2073 {
2074 SbxVariableRef refVal = PopVar();
2075 SbxVariableRef refVar = PopVar();
2076 if( refVar->GetType() != SbxSTRING || refVal->GetType() != SbxSTRING )
2077 {
2078 Error( ERRCODE_BASIC_INVALID_USAGE_OBJECT );
2079 }
2080 else
2081 {
2082 SbxFlagBits n = refVar->GetFlags();
2083 if( refVar.get() == pMeth )
2084 {
2085 refVar->SetFlag( SbxFlagBits::Write );
2086 }
2087 OUString aRefVarString = refVar->GetOUString();
2088 OUString aRefValString = refVal->GetOUString();
2089 sal_Int32 nVarStrLen = aRefVarString.getLength();
2090 sal_Int32 nValStrLen = aRefValString.getLength();
2091
2092 if (nVarStrLen > nValStrLen)
2093 {
2094 refVar->PutString(RepeatedUChar(' ', nVarStrLen - nValStrLen) + aRefValString);
2095 }
2096 else
2097 {
2098 refVar->PutString(aRefValString.copy(0, nVarStrLen));
2099 }
2100
2101 refVar->SetFlags( n );
2102 }
2103 }
2104
2105 // laying down TOS in TOS-1, then set ReadOnly-Bit
2106
2107 void SbiRuntime::StepPUTC()
2108 {
2109 SbxVariableRef refVal = PopVar();
2110 SbxVariableRef refVar = PopVar();
2111 refVar->SetFlag( SbxFlagBits::Write );
2112 *refVar = *refVal;
2113 refVar->ResetFlag( SbxFlagBits::Write );
2114 refVar->SetFlag( SbxFlagBits::Const );
2115 }
2116
2117 // DIM
2118 // TOS = variable for the array with dimension information as parameter
2119
2120 void SbiRuntime::StepDIM()
2121 {
2122 SbxVariableRef refVar = PopVar();
2123 DimImpl( refVar );
2124 }
2125
2126 // #56204 swap out DIM-functionality into a help method (step0.cxx)
2127 void SbiRuntime::DimImpl(const SbxVariableRef& refVar)
2128 {
2129 // If refDim then this DIM statement is terminating a ReDIM and
2130 // previous StepERASE_CLEAR for an array, the following actions have
2131 // been delayed from ( StepERASE_CLEAR ) 'till here
2132 if ( refRedim.is() )
2133 {
2134 if ( !refRedimpArray.is() ) // only erase the array not ReDim Preserve
2135 {
2136 lcl_eraseImpl( refVar, bVBAEnabled );
2137 }
2138 SbxDataType eType = refVar->GetType();
2139 lcl_clearImpl( refVar, eType );
2140 refRedim = nullptr;
2141 }
2142 SbxArray* pDims = refVar->GetParameters();
2143 // must have an even number of arguments
2144 // have in mind that Arg[0] does not count!
2145 if (pDims && !(pDims->Count() & 1))
2146 {
2147 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2148 }
2149 else
2150 {
2151 SbxDataType eType = refVar->IsFixed() ? refVar->GetType() : SbxVARIANT;
2152 SbxDimArray* pArray = new SbxDimArray( eType );
2153 // allow arrays without dimension information, too (VB-compatible)
2154 if( pDims )
2155 {
2156 refVar->ResetFlag( SbxFlagBits::VarToDim );
2157
2158 for (sal_uInt32 i = 1; i < pDims->Count();)
2159 {
2160 sal_Int32 lb = pDims->Get(i++)->GetLong();
2161 sal_Int32 ub = pDims->Get(i++)->GetLong();
2162 if( ub < lb )
2163 {
2164 Error( ERRCODE_BASIC_OUT_OF_RANGE );
2165 ub = lb;
2166 }
2167 pArray->AddDim(lb, ub);
2168 if ( lb != ub )
2169 {
2170 pArray->setHasFixedSize( true );
2171 }
2172 }
2173 }
2174 else
2175 {
2176 // #62867 On creating an array of the length 0, create
2177 // a dimension (like for Uno-Sequences of the length 0)
2178 pArray->unoAddDim(0, -1);
2179 }
2180 SbxFlagBits nSavFlags = refVar->GetFlags();
2181 refVar->ResetFlag( SbxFlagBits::Fixed );
2182 refVar->PutObject( pArray );
2183 refVar->SetFlags( nSavFlags );
2184 refVar->SetParameters( nullptr );
2185 }
2186 }
2187
2188 // REDIM
2189 // TOS = variable for the array
2190 // argv = dimension information
2191
2192 void SbiRuntime::StepREDIM()
2193 {
2194 // Nothing different than dim at the moment because
2195 // a double dim is already recognized by the compiler.
2196 StepDIM();
2197 }
2198
2199
2200 // Helper function for StepREDIMP and StepDCREATE_IMPL / bRedimp = true
2201 static void implCopyDimArray( SbxDimArray* pNewArray, SbxDimArray* pOldArray, sal_Int32 nMaxDimIndex,
2202 sal_Int32 nActualDim, sal_Int32* pActualIndices, sal_Int32* pLowerBounds, sal_Int32* pUpperBounds )
2203 {
2204 sal_Int32& ri = pActualIndices[nActualDim];
2205 for( ri = pLowerBounds[nActualDim] ; ri <= pUpperBounds[nActualDim] ; ri++ )
2206 {
2207 if( nActualDim < nMaxDimIndex )
2208 {
2209 implCopyDimArray( pNewArray, pOldArray, nMaxDimIndex, nActualDim + 1,
2210 pActualIndices, pLowerBounds, pUpperBounds );
2211 }
2212 else
2213 {
2214 SbxVariable* pSource = pOldArray->Get(pActualIndices);
2215 if (pSource && pOldArray->GetRefCount() > 1)
2216 // tdf#134692: old array will stay alive after the redim - we need to copy deep
2217 pSource = new SbxVariable(*pSource);
2218 pNewArray->Put(pSource, pActualIndices);
2219 }
2220 }
2221 }
2222
2223 // Returns true when actually restored
2224 static bool implRestorePreservedArray(SbxDimArray* pNewArray, SbxArrayRef& rrefRedimpArray, bool* pbWasError = nullptr)
2225 {
2226 assert(pNewArray);
2227 bool bResult = false;
2228 if (pbWasError)
2229 *pbWasError = false;
2230 if (rrefRedimpArray)
2231 {
2232 SbxDimArray* pOldArray = static_cast<SbxDimArray*>(rrefRedimpArray.get());
2233 const sal_Int32 nDimsNew = pNewArray->GetDims();
2234 const sal_Int32 nDimsOld = pOldArray->GetDims();
2235
2236 if (nDimsOld != nDimsNew)
2237 {
2238 StarBASIC::Error(ERRCODE_BASIC_OUT_OF_RANGE);
2239 if (pbWasError)
2240 *pbWasError = true;
2241 }
2242 else if (nDimsNew > 0)
2243 {
2244 // Store dims to use them for copying later
2245 std::unique_ptr<sal_Int32[]> pLowerBounds(new sal_Int32[nDimsNew]);
2246 std::unique_ptr<sal_Int32[]> pUpperBounds(new sal_Int32[nDimsNew]);
2247 std::unique_ptr<sal_Int32[]> pActualIndices(new sal_Int32[nDimsNew]);
2248 bool bNeedsPreallocation = true;
2249
2250 // Compare bounds
2251 for (sal_Int32 i = 1; i <= nDimsNew; i++)
2252 {
2253 sal_Int32 lBoundNew, uBoundNew;
2254 sal_Int32 lBoundOld, uBoundOld;
2255 pNewArray->GetDim(i, lBoundNew, uBoundNew);
2256 pOldArray->GetDim(i, lBoundOld, uBoundOld);
2257 lBoundNew = std::max(lBoundNew, lBoundOld);
2258 uBoundNew = std::min(uBoundNew, uBoundOld);
2259 sal_Int32 j = i - 1;
2260 pActualIndices[j] = pLowerBounds[j] = lBoundNew;
2261 pUpperBounds[j] = uBoundNew;
2262 if (lBoundNew > uBoundNew) // No elements in the dimension -> no elements to restore
2263 bNeedsPreallocation = false;
2264 }
2265
2266 // Optimization: pre-allocate underlying container
2267 if (bNeedsPreallocation)
2268 pNewArray->Put(nullptr, pUpperBounds.get());
2269
2270 // Copy data from old array by going recursively through all dimensions
2271 // (It would be faster to work on the flat internal data array of an
2272 // SbyArray but this solution is clearer and easier)
2273 implCopyDimArray(pNewArray, pOldArray, nDimsNew - 1, 0, pActualIndices.get(),
2274 pLowerBounds.get(), pUpperBounds.get());
2275 bResult = true;
2276 }
2277
2278 rrefRedimpArray.clear();
2279 }
2280 return bResult;
2281 }
2282
2283 // REDIM PRESERVE
2284 // TOS = variable for the array
2285 // argv = dimension information
2286
2287 void SbiRuntime::StepREDIMP()
2288 {
2289 SbxVariableRef refVar = PopVar();
2290 DimImpl( refVar );
2291
2292 // Now check, if we can copy from the old array
2293 if( refRedimpArray.is() )
2294 {
2295 if (SbxDimArray* pNewArray = dynamic_cast<SbxDimArray*>(refVar->GetObject()))
2296 implRestorePreservedArray(pNewArray, refRedimpArray);
2297 }
2298 }
2299
2300 // REDIM_COPY
2301 // TOS = Array-Variable, Reference to array is copied
2302 // Variable is cleared as in ERASE
2303
2304 void SbiRuntime::StepREDIMP_ERASE()
2305 {
2306 SbxVariableRef refVar = PopVar();
2307 refRedim = refVar;
2308 SbxDataType eType = refVar->GetType();
2309 if( eType & SbxARRAY )
2310 {
2311 SbxBase* pElemObj = refVar->GetObject();
2312 SbxDimArray* pDimArray = dynamic_cast<SbxDimArray*>( pElemObj );
2313 if( pDimArray )
2314 {
2315 refRedimpArray = pDimArray;
2316 }
2317
2318 }
2319 else if( refVar->IsFixed() )
2320 {
2321 refVar->Clear();
2322 }
2323 else
2324 {
2325 refVar->SetType( SbxEMPTY );
2326 }
2327 }
2328
2329 static void lcl_clearImpl( SbxVariableRef const & refVar, SbxDataType const & eType )
2330 {
2331 SbxFlagBits nSavFlags = refVar->GetFlags();
2332 refVar->ResetFlag( SbxFlagBits::Fixed );
2333 refVar->SetType( SbxDataType(eType & 0x0FFF) );
2334 refVar->SetFlags( nSavFlags );
2335 refVar->Clear();
2336 }
2337
2338 static void lcl_eraseImpl( SbxVariableRef const & refVar, bool bVBAEnabled )
2339 {
2340 SbxDataType eType = refVar->GetType();
2341 if( eType & SbxARRAY )
2342 {
2343 if ( bVBAEnabled )
2344 {
2345 SbxBase* pElemObj = refVar->GetObject();
2346 SbxDimArray* pDimArray = dynamic_cast<SbxDimArray*>( pElemObj );
2347 if( pDimArray )
2348 {
2349 if ( pDimArray->hasFixedSize() )
2350 {
2351 // Clear all Value(s)
2352 pDimArray->SbxArray::Clear();
2353 }
2354 else
2355 {
2356 pDimArray->Clear(); // clear dims and values
2357 }
2358 }
2359 else
2360 {
2361 SbxArray* pArray = dynamic_cast<SbxArray*>( pElemObj );
2362 if ( pArray )
2363 {
2364 pArray->Clear();
2365 }
2366 }
2367 }
2368 else
2369 {
2370 // Arrays have on an erase to VB quite a complex behaviour. Here are
2371 // only the type problems at REDIM (#26295) removed at first:
2372 // Set type hard onto the array-type, because a variable with array is
2373 // SbxOBJECT. At REDIM there's an SbxOBJECT-array generated then and
2374 // the original type is lost -> runtime error
2375 lcl_clearImpl( refVar, eType );
2376 }
2377 }
2378 else if( refVar->IsFixed() )
2379 {
2380 refVar->Clear();
2381 }
2382 else
2383 {
2384 refVar->SetType( SbxEMPTY );
2385 }
2386 }
2387
2388 // delete variable
2389 // TOS = variable
2390
2391 void SbiRuntime::StepERASE()
2392 {
2393 SbxVariableRef refVar = PopVar();
2394 lcl_eraseImpl( refVar, bVBAEnabled );
2395 }
2396
2397 void SbiRuntime::StepERASE_CLEAR()
2398 {
2399 refRedim = PopVar();
2400 }
2401
2402 void SbiRuntime::StepARRAYACCESS()
2403 {
2404 if( !refArgv.is() )
2405 {
2406 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2407 }
2408 SbxVariableRef refVar = PopVar();
2409 refVar->SetParameters( refArgv.get() );
2410 PopArgv();
2411 PushVar( CheckArray( refVar.get() ) );
2412 }
2413
2414 void SbiRuntime::StepBYVAL()
2415 {
2416 // Copy variable on stack to break call by reference
2417 SbxVariableRef pVar = PopVar();
2418 SbxDataType t = pVar->GetType();
2419
2420 SbxVariable* pCopyVar = new SbxVariable( t );
2421 pCopyVar->SetFlag( SbxFlagBits::ReadWrite );
2422 *pCopyVar = *pVar;
2423
2424 PushVar( pCopyVar );
2425 }
2426
2427 // establishing an argv
2428 // nOp1 stays as it is -> 1st element is the return value
2429
2430 void SbiRuntime::StepARGC()
2431 {
2432 PushArgv();
2433 refArgv = new SbxArray;
2434 nArgc = 1;
2435 }
2436
2437 // storing an argument in Argv
2438
2439 void SbiRuntime::StepARGV()
2440 {
2441 if( !refArgv.is() )
2442 {
2443 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2444 }
2445 else
2446 {
2447 SbxVariableRef pVal = PopVar();
2448
2449 // Before fix of #94916:
2450 if( dynamic_cast<const SbxMethod*>( pVal.get() ) != nullptr
2451 || dynamic_cast<const SbUnoProperty*>( pVal.get() ) != nullptr
2452 || dynamic_cast<const SbProcedureProperty*>( pVal.get() ) != nullptr )
2453 {
2454 // evaluate methods and properties!
2455 SbxVariable* pRes = new SbxVariable( *pVal );
2456 pVal = pRes;
2457 }
2458 refArgv->Put(pVal.get(), nArgc++);
2459 }
2460 }
2461
2462 // Input to Variable. The variable is on TOS and is
2463 // is removed afterwards.
2464 void SbiRuntime::StepINPUT()
2465 {
2466 OUStringBuffer sin;
2467 char ch = 0;
2468 ErrCode err;
2469 // Skip whitespace
2470 while( ( err = pIosys->GetError() ) == ERRCODE_NONE )
2471 {
2472 ch = pIosys->Read();
2473 if( ch != ' ' && ch != '\t' && ch != '\n' )
2474 {
2475 break;
2476 }
2477 }
2478 if( !err )
2479 {
2480 // Scan until comma or whitespace
2481 char sep = ( ch == '"' ) ? ch : 0;
2482 if( sep )
2483 {
2484 ch = pIosys->Read();
2485 }
2486 while( ( err = pIosys->GetError() ) == ERRCODE_NONE )
2487 {
2488 if( ch == sep )
2489 {
2490 ch = pIosys->Read();
2491 if( ch != sep )
2492 {
2493 break;
2494 }
2495 }
2496 else if( !sep && (ch == ',' || ch == '\n') )
2497 {
2498 break;
2499 }
2500 sin.append( ch );
2501 ch = pIosys->Read();
2502 }
2503 // skip whitespace
2504 if( ch == ' ' || ch == '\t' )
2505 {
2506 while( ( err = pIosys->GetError() ) == ERRCODE_NONE )
2507 {
2508 if( ch != ' ' && ch != '\t' && ch != '\n' )
2509 {
2510 break;
2511 }
2512 ch = pIosys->Read();
2513 }
2514 }
2515 }
2516 if( !err )
2517 {
2518 OUString s = sin.makeStringAndClear();
2519 SbxVariableRef pVar = GetTOS();
2520 // try to fill the variable with a numeric value first,
2521 // then with a string value
2522 if( !pVar->IsFixed() || pVar->IsNumeric() )
2523 {
2524 sal_Int32 nLen = 0;
2525 if( !pVar->Scan( s, &nLen ) )
2526 {
2527 err = SbxBase::GetError();
2528 SbxBase::ResetError();
2529 }
2530 // the value has to be scanned in completely
2531 else if( nLen != s.getLength() && !pVar->PutString( s ) )
2532 {
2533 err = SbxBase::GetError();
2534 SbxBase::ResetError();
2535 }
2536 else if( nLen != s.getLength() && pVar->IsNumeric() )
2537 {
2538 err = SbxBase::GetError();
2539 SbxBase::ResetError();
2540 if( !err )
2541 {
2542 err = ERRCODE_BASIC_CONVERSION;
2543 }
2544 }
2545 }
2546 else
2547 {
2548 pVar->PutString( s );
2549 err = SbxBase::GetError();
2550 SbxBase::ResetError();
2551 }
2552 }
2553 if( err == ERRCODE_BASIC_USER_ABORT )
2554 {
2555 Error( err );
2556 }
2557 else if( err )
2558 {
2559 if( pRestart && !pIosys->GetChannel() )
2560 {
2561 pCode = pRestart;
2562 }
2563 else
2564 {
2565 Error( err );
2566 }
2567 }
2568 else
2569 {
2570 PopVar();
2571 }
2572 }
2573
2574 // Line Input to Variable. The variable is on TOS and is
2575 // deleted afterwards.
2576
2577 void SbiRuntime::StepLINPUT()
2578 {
2579 OString aInput;
2580 pIosys->Read( aInput );
2581 Error( pIosys->GetError() );
2582 SbxVariableRef p = PopVar();
2583 p->PutString(OStringToOUString(aInput, osl_getThreadTextEncoding()));
2584 }
2585
2586 // end of program
2587
2588 void SbiRuntime::StepSTOP()
2589 {
2590 pInst->Stop();
2591 }
2592
2593
2594 void SbiRuntime::StepINITFOR()
2595 {
2596 PushFor();
2597 }
2598
2599 void SbiRuntime::StepINITFOREACH()
2600 {
2601 PushForEach();
2602 }
2603
2604 // increment FOR-variable
2605
2606 void SbiRuntime::StepNEXT()
2607 {
2608 if( !pForStk )
2609 {
2610 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2611 return;
2612 }
2613 if (pForStk->eForType != ForType::To)
2614 return;
2615 if (!pForStk->refVar)
2616 {
2617 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2618 return;
2619 }
2620 // tdf#85371 - grant explicitly write access to the index variable
2621 // since it could be the name of a method itself used in the next statement.
2622 ScopedWritableGuard aGuard(pForStk->refVar, pForStk->refVar.get() == pMeth);
2623 pForStk->refVar->Compute( SbxPLUS, *pForStk->refInc );
2624 }
2625
2626 // beginning CASE: TOS in CASE-stack
2627
2628 void SbiRuntime::StepCASE()
2629 {
2630 if( !refCaseStk.is() )
2631 {
2632 refCaseStk = new SbxArray;
2633 }
2634 SbxVariableRef xVar = PopVar();
2635 refCaseStk->Put(xVar.get(), refCaseStk->Count());
2636 }
2637
2638 // end CASE: free variable
2639
2640 void SbiRuntime::StepENDCASE()
2641 {
2642 if (!refCaseStk.is() || !refCaseStk->Count())
2643 {
2644 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2645 }
2646 else
2647 {
2648 refCaseStk->Remove(refCaseStk->Count() - 1);
2649 }
2650 }
2651
2652
2653 void SbiRuntime::StepSTDERROR()
2654 {
2655 pError = nullptr; bError = true;
2656 pInst->aErrorMsg.clear();
2657 pInst->nErr = ERRCODE_NONE;
2658 pInst->nErl = 0;
2659 nError = ERRCODE_NONE;
2660 SbxErrObject::getUnoErrObject()->Clear();
2661 }
2662
2663 void SbiRuntime::StepNOERROR()
2664 {
2665 pInst->aErrorMsg.clear();
2666 pInst->nErr = ERRCODE_NONE;
2667 pInst->nErl = 0;
2668 nError = ERRCODE_NONE;
2669 SbxErrObject::getUnoErrObject()->Clear();
2670 bError = false;
2671 }
2672
2673 // leave UP
2674
2675 void SbiRuntime::StepLEAVE()
2676 {
2677 bRun = false;
2678 // If VBA and we are leaving an ErrorHandler then clear the error ( it's been processed )
2679 if ( bInError && pError )
2680 {
2681 SbxErrObject::getUnoErrObject()->Clear();
2682 }
2683 }
2684
2685 void SbiRuntime::StepCHANNEL() // TOS = channel number
2686 {
2687 SbxVariableRef pChan = PopVar();
2688 short nChan = pChan->GetInteger();
2689 pIosys->SetChannel( nChan );
2690 Error( pIosys->GetError() );
2691 }
2692
2693 void SbiRuntime::StepCHANNEL0()
2694 {
2695 pIosys->ResetChannel();
2696 }
2697
2698 void SbiRuntime::StepPRINT() // print TOS
2699 {
2700 SbxVariableRef p = PopVar();
2701 OUString s1 = p->GetOUString();
2702 OUString s;
2703 if( p->GetType() >= SbxINTEGER && p->GetType() <= SbxDOUBLE )
2704 {
2705 s = " "; // one blank before
2706 }
2707 s += s1;
2708 pIosys->Write( s );
2709 Error( pIosys->GetError() );
2710 }
2711
2712 void SbiRuntime::StepPRINTF() // print TOS in field
2713 {
2714 SbxVariableRef p = PopVar();
2715 OUString s1 = p->GetOUString();
2716 OUStringBuffer s;
2717 if( p->GetType() >= SbxINTEGER && p->GetType() <= SbxDOUBLE )
2718 {
2719 s.append(' ');
2720 }
2721 s.append(s1);
2722 comphelper::string::padToLength(s, 14, ' ');
2723 pIosys->Write( s );
2724 Error( pIosys->GetError() );
2725 }
2726
2727 void SbiRuntime::StepWRITE() // write TOS
2728 {
2729 SbxVariableRef p = PopVar();
2730 // Does the string have to be encapsulated?
2731 char ch = 0;
2732 switch (p->GetType() )
2733 {
2734 case SbxSTRING: ch = '"'; break;
2735 case SbxCURRENCY:
2736 case SbxBOOL:
2737 case SbxDATE: ch = '#'; break;
2738 default: break;
2739 }
2740 OUString s;
2741 if( ch )
2742 {
2743 s += OUStringChar(ch);
2744 }
2745 s += p->GetOUString();
2746 if( ch )
2747 {
2748 s += OUStringChar(ch);
2749 }
2750 pIosys->Write( s );
2751 Error( pIosys->GetError() );
2752 }
2753
2754 void SbiRuntime::StepRENAME() // Rename Tos+1 to Tos
2755 {
2756 SbxVariableRef pTos1 = PopVar();
2757 SbxVariableRef pTos = PopVar();
2758 OUString aDest = pTos1->GetOUString();
2759 OUString aSource = pTos->GetOUString();
2760
2761 if( hasUno() )
2762 {
2763 implStepRenameUCB( aSource, aDest );
2764 }
2765 else
2766 {
2767 implStepRenameOSL( aSource, aDest );
2768 }
2769 }
2770
2771 // TOS = Prompt
2772
2773 void SbiRuntime::StepPROMPT()
2774 {
2775 SbxVariableRef p = PopVar();
2776 pIosys->SetPrompt(p->GetOUString());
2777 }
2778
2779 // Set Restart point
2780
2781 void SbiRuntime::StepRESTART()
2782 {
2783 pRestart = pCode;
2784 }
2785
2786 // empty expression on stack for missing parameter
2787
2788 void SbiRuntime::StepEMPTY()
2789 {
2790 // #57915 The semantics of StepEMPTY() is the representation of a missing argument.
2791 // This is represented by the value 448 (ERRCODE_BASIC_NAMED_NOT_FOUND) of the type error
2792 // in VB. StepEmpty should now rather be named StepMISSING() but the name is kept
2793 // to simplify matters.
2794 SbxVariableRef xVar = new SbxVariable( SbxVARIANT );
2795 xVar->PutErr( 448 );
2796 // tdf#79426, tdf#125180 - add additional information about a missing parameter
2797 SetIsMissing( xVar.get() );
2798 PushVar( xVar.get() );
2799 }
2800
2801 // TOS = error code
2802
2803 void SbiRuntime::StepERROR()
2804 {
2805 SbxVariableRef refCode = PopVar();
2806 sal_uInt16 n = refCode->GetUShort();
2807 ErrCode error = StarBASIC::GetSfxFromVBError( n );
2808 if ( bVBAEnabled )
2809 {
2810 pInst->Error( error );
2811 }
2812 else
2813 {
2814 Error( error );
2815 }
2816 }
2817
2818 // loading a numeric constant (+ID)
2819
2820 void SbiRuntime::StepLOADNC( sal_uInt32 nOp1 )
2821 {
2822 // tdf#143707 - check if the data type character was added after the string termination symbol
2823 SbxDataType eTypeStr;
2824 // #57844 use localized function
2825 OUString aStr = pImg->GetString(nOp1, &eTypeStr);
2826 // also allow , !!!
2827 sal_Int32 iComma = aStr.indexOf(',');
2828 if( iComma >= 0 )
2829 {
2830 aStr = aStr.replaceAt(iComma, 1, u".");
2831 }
2832 sal_Int32 nParseEnd = 0;
2833 rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok;
2834 double n = ::rtl::math::stringToDouble( aStr, '.', ',', &eStatus, &nParseEnd );
2835
2836 // tdf#131296 - retrieve data type put in SbiExprNode::Gen
2837 SbxDataType eType = SbxDOUBLE;
2838 if ( nParseEnd < aStr.getLength() )
2839 {
2840 // tdf#143707 - Check if there was a data type character after the numeric constant,
2841 // added by older versions of the fix of the default values for strings.
2842 switch ( aStr[nParseEnd] )
2843 {
2844 // See GetSuffixType in basic/source/comp/scanner.cxx for type characters
2845 case '%': eType = SbxINTEGER; break;
2846 case '&': eType = SbxLONG; break;
2847 case '!': eType = SbxSINGLE; break;
2848 case '@': eType = SbxCURRENCY; break;
2849 // tdf#142460 - properly handle boolean values in string pool
2850 case 'b': eType = SbxBOOL; break;
2851 }
2852 }
2853 // tdf#143707 - if the data type character is different from the default value, it was added
2854 // in basic/source/comp/symtbl.cxx. Hence, change the type of the numeric constant to be loaded.
2855 else if (eTypeStr != SbxSTRING)
2856 {
2857 eType = eTypeStr;
2858 }
2859 SbxVariable* p = new SbxVariable( eType );
2860 p->PutDouble( n );
2861 // tdf#133913 - create variable with Variant/Type in order to prevent type conversion errors
2862 p->ResetFlag( SbxFlagBits::Fixed );
2863 PushVar( p );
2864 }
2865
2866 // loading a string constant (+ID)
2867
2868 void SbiRuntime::StepLOADSC( sal_uInt32 nOp1 )
2869 {
2870 SbxVariable* p = new SbxVariable;
2871 p->PutString( pImg->GetString( nOp1 ) );
2872 PushVar( p );
2873 }
2874
2875 // Immediate Load (+value)
2876 // The opcode is not generated in SbiExprNode::Gen anymore; used for legacy images
2877
2878 void SbiRuntime::StepLOADI( sal_uInt32 nOp1 )
2879 {
2880 SbxVariable* p = new SbxVariable;
2881 p->PutInteger( static_cast<sal_Int16>( nOp1 ) );
2882 PushVar( p );
2883 }
2884
2885 // store a named argument in Argv (+Arg-no. from 1!)
2886
2887 void SbiRuntime::StepARGN( sal_uInt32 nOp1 )
2888 {
2889 if( !refArgv.is() )
2890 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2891 else
2892 {
2893 OUString aAlias( pImg->GetString( nOp1 ) );
2894 SbxVariableRef pVal = PopVar();
2895 if( bVBAEnabled &&
2896 ( dynamic_cast<const SbxMethod*>( pVal.get()) != nullptr
2897 || dynamic_cast<const SbUnoProperty*>( pVal.get()) != nullptr
2898 || dynamic_cast<const SbProcedureProperty*>( pVal.get()) != nullptr ) )
2899 {
2900 // named variables ( that are Any especially properties ) can be empty at this point and need a broadcast
2901 if ( pVal->GetType() == SbxEMPTY )
2902 pVal->Broadcast( SfxHintId::BasicDataWanted );
2903 // evaluate methods and properties!
2904 SbxVariable* pRes = new SbxVariable( *pVal );
2905 pVal = pRes;
2906 }
2907 refArgv->Put(pVal.get(), nArgc);
2908 refArgv->PutAlias(aAlias, nArgc++);
2909 }
2910 }
2911
2912 // converting the type of an argument in Argv for DECLARE-Fkt. (+type)
2913
2914 void SbiRuntime::StepARGTYP( sal_uInt32 nOp1 )
2915 {
2916 if( !refArgv.is() )
2917 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2918 else
2919 {
2920 bool bByVal = (nOp1 & 0x8000) != 0; // Is BYVAL requested?
2921 SbxDataType t = static_cast<SbxDataType>(nOp1 & 0x7FFF);
2922 SbxVariable* pVar = refArgv->Get(refArgv->Count() - 1); // last Arg
2923
2924 // check BYVAL
2925 if( pVar->GetRefCount() > 2 ) // 2 is normal for BYVAL
2926 {
2927 // parameter is a reference
2928 if( bByVal )
2929 {
2930 // Call by Value is requested -> create a copy
2931 pVar = new SbxVariable( *pVar );
2932 pVar->SetFlag( SbxFlagBits::ReadWrite );
2933 refExprStk->Put(pVar, refArgv->Count() - 1);
2934 }
2935 else
2936 pVar->SetFlag( SbxFlagBits::Reference ); // Ref-Flag for DllMgr
2937 }
2938 else
2939 {
2940 // parameter is NO reference
2941 if( bByVal )
2942 pVar->ResetFlag( SbxFlagBits::Reference ); // no reference -> OK
2943 else
2944 Error( ERRCODE_BASIC_BAD_PARAMETERS ); // reference needed
2945 }
2946
2947 if( pVar->GetType() != t )
2948 {
2949 // variant for correct conversion
2950 // besides error, if SbxBYREF
2951 pVar->Convert( SbxVARIANT );
2952 pVar->Convert( t );
2953 }
2954 }
2955 }
2956
2957 // bring string to a definite length (+length)
2958
2959 void SbiRuntime::StepPAD( sal_uInt32 nOp1 )
2960 {
2961 SbxVariable* p = GetTOS();
2962 OUString s = p->GetOUString();
2963 sal_Int32 nLen(nOp1);
2964 if( s.getLength() == nLen )
2965 return;
2966
2967 OUStringBuffer aBuf(s);
2968 if (aBuf.getLength() > nLen)
2969 {
2970 comphelper::string::truncateToLength(aBuf, nLen);
2971 }
2972 else
2973 {
2974 comphelper::string::padToLength(aBuf, nLen, ' ');
2975 }
2976 s = aBuf.makeStringAndClear();
2977 // Do not modify the original variable inadvertently
2978 PopVar();
2979 p = new SbxVariable;
2980 p->PutString(s);
2981 PushVar(p);
2982 }
2983
2984 // jump (+target)
2985
2986 void SbiRuntime::StepJUMP( sal_uInt32 nOp1 )
2987 {
2988 #ifdef DBG_UTIL
2989 // #QUESTION shouldn't this be
2990 // if( (sal_uInt8*)( nOp1+pImagGetCode() ) >= pImg->GetCodeSize() )
2991 if( nOp1 >= pImg->GetCodeSize() )
2992 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
2993 #endif
2994 pCode = pImg->GetCode() + nOp1;
2995 }
2996
2997 bool SbiRuntime::EvaluateTopOfStackAsBool()
2998 {
2999 SbxVariableRef tos = PopVar();
3000 // In a test e.g. If Null then
3001 // will evaluate Null will act as if False
3002 if ( bVBAEnabled && tos->IsNull() )
3003 {
3004 return false;
3005 }
3006
3007 // tdf#151503 - do not evaluate a missing optional variable to a boolean
3008 if (tos->GetType() == SbxERROR && IsMissing(tos.get(), 1))
3009 {
3010 Error(ERRCODE_BASIC_NOT_OPTIONAL);
3011 return false;
3012 }
3013
3014 if ( tos->IsObject() )
3015 {
3016 //GetBool applied to an Object attempts to dereference and evaluate
3017 //the underlying value as Bool. Here, we're checking rather that
3018 //it is not null
3019 return tos->GetObject();
3020 }
3021 else
3022 {
3023 return tos->GetBool();
3024 }
3025 }
3026
3027 // evaluate TOS, conditional jump (+target)
3028
3029 void SbiRuntime::StepJUMPT( sal_uInt32 nOp1 )
3030 {
3031 if ( EvaluateTopOfStackAsBool() )
3032 {
3033 StepJUMP( nOp1 );
3034 }
3035 }
3036
3037 // evaluate TOS, conditional jump (+target)
3038
3039 void SbiRuntime::StepJUMPF( sal_uInt32 nOp1 )
3040 {
3041 if ( !EvaluateTopOfStackAsBool() )
3042 {
3043 StepJUMP( nOp1 );
3044 }
3045 }
3046
3047 // evaluate TOS, jump into JUMP-table (+MaxVal)
3048 // looks like this:
3049 // ONJUMP 2
3050 // JUMP target1
3051 // JUMP target2
3052
3053 // if 0x8000 is set in the operand, push the return address (ON..GOSUB)
3054
3055 void SbiRuntime::StepONJUMP( sal_uInt32 nOp1 )
3056 {
3057 SbxVariableRef p = PopVar();
3058 sal_Int16 n = p->GetInteger();
3059
3060 if (nOp1 & 0x8000)
3061 nOp1 &= 0x7FFF;
3062
3063 // tdf#160321 - do not execute the jump statement if the expression is out of range
3064 if (n < 1 || o3tl::make_unsigned(n) > nOp1)
3065 n = static_cast<sal_Int16>(nOp1 + 1);
3066 else if (nOp1 & 0x8000)
3067 PushGosub(pCode + 5 * nOp1);
3068
3069 nOp1 = static_cast<sal_uInt32>(pCode - pImg->GetCode()) + 5 * --n;
3070 StepJUMP( nOp1 );
3071 }
3072
3073 // UP-call (+target)
3074
3075 void SbiRuntime::StepGOSUB( sal_uInt32 nOp1 )
3076 {
3077 PushGosub( pCode );
3078 if( nOp1 >= pImg->GetCodeSize() )
3079 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
3080 pCode = pImg->GetCode() + nOp1;
3081 }
3082
3083 // UP-return (+0 or target)
3084
3085 void SbiRuntime::StepRETURN( sal_uInt32 nOp1 )
3086 {
3087 PopGosub();
3088 if( nOp1 )
3089 StepJUMP( nOp1 );
3090 }
3091
3092 // check FOR-variable (+Endlabel)
3093
3094 void SbiRuntime::StepTESTFOR( sal_uInt32 nOp1 )
3095 {
3096 if( !pForStk )
3097 {
3098 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
3099 return;
3100 }
3101
3102 bool bEndLoop = false;
3103 switch( pForStk->eForType )
3104 {
3105 case ForType::To:
3106 {
3107 SbxOperator eOp = ( pForStk->refInc->GetDouble() < 0 ) ? SbxLT : SbxGT;
3108 if( pForStk->refVar->Compare( eOp, *pForStk->refEnd ) )
3109 bEndLoop = true;
3110 if (SbxBase::IsError())
3111 pForStk->eForType = ForType::Error; // terminate loop at the next iteration
3112 break;
3113 }
3114 case ForType::EachArray:
3115 {
3116 SbiForStack* p = pForStk;
3117 if (!p->refEnd)
3118 {
3119 SbxBase::SetError(ERRCODE_BASIC_CONVERSION);
3120 pForStk->eForType = ForType::Error; // terminate loop at the next iteration
3121 }
3122 else if (p->pArrayCurIndices == nullptr)
3123 {
3124 bEndLoop = true;
3125 }
3126 else
3127 {
3128 SbxDimArray* pArray = reinterpret_cast<SbxDimArray*>(p->refEnd.get());
3129 sal_Int32 nDims = pArray->GetDims();
3130
3131 // Empty array?
3132 if( nDims == 1 && p->pArrayLowerBounds[0] > p->pArrayUpperBounds[0] )
3133 {
3134 bEndLoop = true;
3135 break;
3136 }
3137 SbxVariable* pVal = pArray->Get(p->pArrayCurIndices.get());
3138 *(p->refVar) = *pVal;
3139
3140 bool bFoundNext = false;
3141 for(sal_Int32 i = 0 ; i < nDims ; i++ )
3142 {
3143 if( p->pArrayCurIndices[i] < p->pArrayUpperBounds[i] )
3144 {
3145 bFoundNext = true;
3146 p->pArrayCurIndices[i]++;
3147 for( sal_Int32 j = i - 1 ; j >= 0 ; j-- )
3148 p->pArrayCurIndices[j] = p->pArrayLowerBounds[j];
3149 break;
3150 }
3151 }
3152 if( !bFoundNext )
3153 {
3154 p->pArrayCurIndices.reset();
3155 }
3156 }
3157 break;
3158 }
3159 case ForType::EachCollection:
3160 {
3161 if (!pForStk->refEnd)
3162 {
3163 SbxBase::SetError(ERRCODE_BASIC_CONVERSION);
3164 pForStk->eForType = ForType::Error; // terminate loop at the next iteration
3165 break;
3166 }
3167
3168 BasicCollection* pCollection = static_cast<BasicCollection*>(pForStk->refEnd.get());
3169 SbxArrayRef xItemArray = pCollection->xItemArray;
3170 sal_Int32 nCount = xItemArray->Count();
3171 if( pForStk->nCurCollectionIndex < nCount )
3172 {
3173 SbxVariable* pRes = xItemArray->Get(pForStk->nCurCollectionIndex);
3174 pForStk->nCurCollectionIndex++;
3175 (*pForStk->refVar) = *pRes;
3176 }
3177 else
3178 {
3179 bEndLoop = true;
3180 }
3181 break;
3182 }
3183 case ForType::EachXEnumeration:
3184 {
3185 SbiForStack* p = pForStk;
3186 if (!p->xEnumeration)
3187 {
3188 SbxBase::SetError(ERRCODE_BASIC_CONVERSION);
3189 pForStk->eForType = ForType::Error; // terminate loop at the next iteration
3190 }
3191 else if (p->xEnumeration->hasMoreElements())
3192 {
3193 Any aElem = p->xEnumeration->nextElement();
3194 SbxVariableRef xVar = new SbxVariable( SbxVARIANT );
3195 unoToSbxValue( xVar.get(), aElem );
3196 (*pForStk->refVar) = *xVar;
3197 }
3198 else
3199 {
3200 bEndLoop = true;
3201 }
3202 break;
3203 }
3204 // tdf#130307 - support for each loop for objects exposing XIndexAccess
3205 case ForType::EachXIndexAccess:
3206 {
3207 SbiForStack* p = pForStk;
3208 if (!p->xIndexAccess)
3209 {
3210 SbxBase::SetError(ERRCODE_BASIC_CONVERSION);
3211 pForStk->eForType = ForType::Error; // terminate loop at the next iteration
3212 }
3213 else if (pForStk->nCurCollectionIndex < p->xIndexAccess->getCount())
3214 {
3215 Any aElem = p->xIndexAccess->getByIndex(pForStk->nCurCollectionIndex);
3216 pForStk->nCurCollectionIndex++;
3217 SbxVariableRef xVar = new SbxVariable(SbxVARIANT);
3218 unoToSbxValue(xVar.get(), aElem);
3219 (*pForStk->refVar) = *xVar;
3220 }
3221 else
3222 {
3223 bEndLoop = true;
3224 }
3225 break;
3226 }
3227 case ForType::Error:
3228 {
3229 // We are in Resume Next mode after failed loop initialization
3230 bEndLoop = true;
3231 Error(ERRCODE_BASIC_BAD_PARAMETER);
3232 break;
3233 }
3234 }
3235 if( bEndLoop )
3236 {
3237 PopFor();
3238 StepJUMP( nOp1 );
3239 }
3240 }
3241
3242 // Tos+1 <= Tos+2 <= Tos, 2xremove (+Target)
3243
3244 void SbiRuntime::StepCASETO( sal_uInt32 nOp1 )
3245 {
3246 if (!refCaseStk.is() || !refCaseStk->Count())
3247 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
3248 else
3249 {
3250 SbxVariableRef xTo = PopVar();
3251 SbxVariableRef xFrom = PopVar();
3252 SbxVariableRef xCase = refCaseStk->Get(refCaseStk->Count() - 1);
3253 if( *xCase >= *xFrom && *xCase <= *xTo )
3254 StepJUMP( nOp1 );
3255 }
3256 }
3257
3258
3259 void SbiRuntime::StepERRHDL( sal_uInt32 nOp1 )
3260 {
3261 const sal_uInt8* p = pCode;
3262 StepJUMP( nOp1 );
3263 pError = pCode;
3264 pCode = p;
3265 pInst->aErrorMsg.clear();
3266 pInst->nErr = ERRCODE_NONE;
3267 pInst->nErl = 0;
3268 nError = ERRCODE_NONE;
3269 SbxErrObject::getUnoErrObject()->Clear();
3270 }
3271
3272 // Resume after errors (+0=statement, 1=next or Label)
3273
3274 void SbiRuntime::StepRESUME( sal_uInt32 nOp1 )
3275 {
3276 // #32714 Resume without error? -> error
3277 if( !bInError )
3278 {
3279 Error( ERRCODE_BASIC_BAD_RESUME );
3280 return;
3281 }
3282 if( nOp1 )
3283 {
3284 // set Code-pointer to the next statement
3285 sal_uInt16 n1, n2;
3286 pCode = pMod->FindNextStmnt( pErrCode, n1, n2, true, pImg );
3287 }
3288 else
3289 pCode = pErrStmnt;
3290 if ( pError ) // current in error handler ( and got a Resume Next statement )
3291 SbxErrObject::getUnoErrObject()->Clear();
3292
3293 if( nOp1 > 1 )
3294 StepJUMP( nOp1 );
3295 pInst->aErrorMsg.clear();
3296 pInst->nErr = ERRCODE_NONE;
3297 pInst->nErl = 0;
3298 nError = ERRCODE_NONE;
3299 bInError = false;
3300 }
3301
3302 // close channel (+channel, 0=all)
3303 void SbiRuntime::StepCLOSE( sal_uInt32 nOp1 )
3304 {
3305 ErrCode err;
3306 if( !nOp1 )
3307 pIosys->Shutdown();
3308 else
3309 {
3310 err = pIosys->GetError();
3311 if( !err )
3312 {
3313 pIosys->Close();
3314 }
3315 }
3316 err = pIosys->GetError();
3317 Error( err );
3318 }
3319
3320 // output character (+char)
3321
3322 void SbiRuntime::StepPRCHAR( sal_uInt32 nOp1 )
3323 {
3324 OUString s(static_cast<sal_Unicode>(nOp1));
3325 pIosys->Write( s );
3326 Error( pIosys->GetError() );
3327 }
3328
3329 // check whether TOS is a certain object class (+StringID)
3330
3331 bool SbiRuntime::implIsClass( SbxObject const * pObj, const OUString& aClass )
3332 {
3333 bool bRet = true;
3334
3335 if( !aClass.isEmpty() )
3336 {
3337 bRet = pObj->IsClass( aClass );
3338 if( !bRet )
3339 bRet = aClass.equalsIgnoreAsciiCase( "object" );
3340 if( !bRet )
3341 {
3342 const OUString& aObjClass = pObj->GetClassName();
3343 SbModule* pClassMod = GetSbData()->pClassFac->FindClass( aObjClass );
3344 if( pClassMod )
3345 {
3346 SbClassData* pClassData = pClassMod->pClassData.get();
3347 if (pClassData != nullptr )
3348 {
3349 SbxVariable* pClassVar = pClassData->mxIfaces->Find( aClass, SbxClassType::DontCare );
3350 bRet = (pClassVar != nullptr);
3351 }
3352 }
3353 }
3354 }
3355 return bRet;
3356 }
3357
3358 bool SbiRuntime::checkClass_Impl( const SbxVariableRef& refVal,
3359 const OUString& aClass, bool bRaiseErrors, bool bDefault )
3360 {
3361 bool bOk = bDefault;
3362
3363 SbxDataType t = refVal->GetType();
3364 SbxVariable* pVal = refVal.get();
3365 // we don't know the type of uno properties that are (maybevoid)
3366 if ( t == SbxEMPTY )
3367 {
3368 if ( auto pProp = dynamic_cast<SbUnoProperty*>( refVal.get() ) )
3369 {
3370 t = pProp->getRealType();
3371 }
3372 }
3373 if( t == SbxOBJECT || bVBAEnabled )
3374 {
3375 SbxObject* pObj = dynamic_cast<SbxObject*>(pVal);
3376 if (!pObj)
3377 {
3378 pObj = dynamic_cast<SbxObject*>(refVal->GetObject());
3379 }
3380 if( pObj )
3381 {
3382 if( !implIsClass( pObj, aClass ) )
3383 {
3384 SbUnoObject* pUnoObj(nullptr);
3385 if (bVBAEnabled || CodeCompleteOptions::IsExtendedTypeDeclaration())
3386 {
3387 pUnoObj = dynamic_cast<SbUnoObject*>(pObj);
3388 }
3389
3390 if (pUnoObj)
3391 bOk = checkUnoObjectType(*pUnoObj, aClass);
3392 else
3393 bOk = false;
3394 if ( !bOk && bRaiseErrors )
3395 Error( ERRCODE_BASIC_INVALID_USAGE_OBJECT );
3396 }
3397 else
3398 {
3399 bOk = true;
3400
3401 SbClassModuleObject* pClassModuleObject = dynamic_cast<SbClassModuleObject*>( pObj );
3402 if( pClassModuleObject != nullptr )
3403 pClassModuleObject->triggerInitializeEvent();
3404 }
3405 }
3406 }
3407 else
3408 {
3409 if( bRaiseErrors )
3410 Error( ERRCODE_BASIC_NEEDS_OBJECT );
3411 bOk = false;
3412 }
3413 return bOk;
3414 }
3415
3416 void SbiRuntime::StepSETCLASS_impl( sal_uInt32 nOp1, bool bHandleDflt )
3417 {
3418 SbxVariableRef refVal = PopVar();
3419 SbxVariableRef refVar = PopVar();
3420 OUString aClass( pImg->GetString( nOp1 ) );
3421
3422 bool bOk = checkClass_Impl( refVal, aClass, true, true );
3423 if( bOk )
3424 {
3425 StepSET_Impl( refVal, refVar, bHandleDflt ); // don't do handle default prop for a "proper" set
3426 }
3427 }
3428
3429 void SbiRuntime::StepVBASETCLASS( sal_uInt32 nOp1 )
3430 {
3431 StepSETCLASS_impl( nOp1, false );
3432 }
3433
3434 void SbiRuntime::StepSETCLASS( sal_uInt32 nOp1 )
3435 {
3436 StepSETCLASS_impl( nOp1, true );
3437 }
3438
3439 void SbiRuntime::StepTESTCLASS( sal_uInt32 nOp1 )
3440 {
3441 SbxVariableRef xObjVal = PopVar();
3442 OUString aClass( pImg->GetString( nOp1 ) );
3443 bool bDefault = !bVBAEnabled;
3444 bool bOk = checkClass_Impl( xObjVal, aClass, false, bDefault );
3445
3446 SbxVariable* pRet = new SbxVariable;
3447 pRet->PutBool( bOk );
3448 PushVar( pRet );
3449 }
3450
3451 // define library for following declare-call
3452
3453 void SbiRuntime::StepLIB( sal_uInt32 nOp1 )
3454 {
3455 aLibName = pImg->GetString( nOp1 );
3456 }
3457
3458 // TOS is incremented by BASE, BASE is pushed before (+BASE)
3459 // This opcode is pushed before DIM/REDIM-commands,
3460 // if there's been only one index named.
3461
3462 void SbiRuntime::StepBASED( sal_uInt32 nOp1 )
3463 {
3464 SbxVariable* p1 = new SbxVariable;
3465 SbxVariableRef x2 = PopVar();
3466
3467 // #109275 Check compatibility mode
3468 bool bCompatible = ((nOp1 & 0x8000) != 0);
3469 sal_uInt16 uBase = static_cast<sal_uInt16>(nOp1 & 1); // Can only be 0 or 1
3470 p1->PutInteger( uBase );
3471 if( !bCompatible )
3472 {
3473 // tdf#85371 - grant explicitly write access to the dimension variable
3474 // since in Star/OpenOffice Basic the upper index border is affected,
3475 // and the dimension variable could be the name of the method itself.
3476 ScopedWritableGuard aGuard(x2, x2.get() == pMeth);
3477 x2->Compute( SbxPLUS, *p1 );
3478 }
3479 PushVar( x2.get() ); // first the Expr
3480 PushVar( p1 ); // then the Base
3481 }
3482
3483 // the bits in the String-ID:
3484 // 0x8000 - Argv is reserved
3485
3486 SbxVariable* SbiRuntime::FindElement( SbxObject* pObj, sal_uInt32 nOp1, sal_uInt32 nOp2,
3487 ErrCode nNotFound, bool bLocal, bool bStatic )
3488 {
3489 bool bIsVBAInterOp = SbiRuntime::isVBAEnabled();
3490 if( bIsVBAInterOp )
3491 {
3492 StarBASIC* pMSOMacroRuntimeLib = GetSbData()->pMSOMacroRuntimLib;
3493 if( pMSOMacroRuntimeLib != nullptr )
3494 {
3495 pMSOMacroRuntimeLib->ResetFlag( SbxFlagBits::ExtSearch );
3496 }
3497 }
3498
3499 SbxVariable* pElem = nullptr;
3500 if( !pObj )
3501 {
3502 Error( ERRCODE_BASIC_NO_OBJECT );
3503 pElem = new SbxVariable;
3504 }
3505 else
3506 {
3507 bool bFatalError = false;
3508 SbxDataType t = static_cast<SbxDataType>(nOp2);
3509 OUString aName( pImg->GetString( nOp1 & 0x7FFF ) );
3510 // Hacky capture of Evaluate [] syntax
3511 // this should be tackled I feel at the pcode level
3512 if ( bIsVBAInterOp && aName.startsWith("[") )
3513 {
3514 // emulate pcode here
3515 StepARGC();
3516 // pseudo StepLOADSC
3517 OUString sArg = aName.copy( 1, aName.getLength() - 2 );
3518 SbxVariable* p = new SbxVariable;
3519 p->PutString( sArg );
3520 PushVar( p );
3521 StepARGV();
3522 nOp1 = nOp1 | 0x8000; // indicate params are present
3523 aName = "Evaluate";
3524 }
3525 if( bLocal )
3526 {
3527 if ( bStatic && pMeth )
3528 {
3529 pElem = pMeth->GetStatics()->Find( aName, SbxClassType::DontCare );
3530 }
3531
3532 if ( !pElem )
3533 {
3534 pElem = refLocals->Find( aName, SbxClassType::DontCare );
3535 }
3536 }
3537 if( !pElem )
3538 {
3539 bool bSave = rBasic.bNoRtl;
3540 rBasic.bNoRtl = true;
3541 pElem = pObj->Find( aName, SbxClassType::DontCare );
3542
3543 // #110004, #112015: Make private really private
3544 if( bLocal && pElem ) // Local as flag for global search
3545 {
3546 if( pElem->IsSet( SbxFlagBits::Private ) )
3547 {
3548 SbiInstance* pInst_ = GetSbData()->pInst;
3549 if( pInst_ && pInst_->IsCompatibility() && pObj != pElem->GetParent() )
3550 {
3551 pElem = nullptr; // Found but in wrong module!
3552 }
3553 // Interfaces: Use SbxFlagBits::ExtFound
3554 }
3555 }
3556 rBasic.bNoRtl = bSave;
3557
3558 // is it a global uno-identifier?
3559 if( bLocal && !pElem )
3560 {
3561 bool bSetName = true; // preserve normal behaviour
3562
3563 // i#i68894# if VBAInterOp favour searching vba globals
3564 // over searching for uno classes
3565 if ( bVBAEnabled )
3566 {
3567 // Try Find in VBA symbols space
3568 pElem = rBasic.VBAFind( aName, SbxClassType::DontCare );
3569 if ( pElem )
3570 {
3571 bSetName = false; // don't overwrite uno name
3572 }
3573 else
3574 {
3575 pElem = VBAConstantHelper::instance().getVBAConstant( aName );
3576 }
3577 }
3578
3579 if( !pElem )
3580 {
3581 // #72382 ATTENTION! ALWAYS returns a result now
3582 // because of unknown modules!
3583 SbUnoClass* pUnoClass = findUnoClass( aName );
3584 if( pUnoClass )
3585 {
3586 pElem = new SbxVariable( t );
3587 SbxValues aRes( SbxOBJECT );
3588 aRes.pObj = pUnoClass;
3589 pElem->SbxVariable::Put( aRes );
3590 }
3591 }
3592
3593 // #62939 If a uno-class has been found, the wrapper
3594 // object has to be held, because the uno-class, e. g.
3595 // "stardiv", has to be read out of the registry
3596 // every time again otherwise
3597 if( pElem )
3598 {
3599 // #63774 May not be saved too!!!
3600 pElem->SetFlag( SbxFlagBits::DontStore );
3601 pElem->SetFlag( SbxFlagBits::NoModify);
3602
3603 // #72382 save locally, all variables that have been declared
3604 // implicit would become global automatically otherwise!
3605 if ( bSetName )
3606 {
3607 pElem->SetName( aName );
3608 }
3609 refLocals->Put(pElem, refLocals->Count());
3610 }
3611 }
3612
3613 if( !pElem )
3614 {
3615 // not there and not in the object?
3616 // don't establish if that thing has parameters!
3617 if( nOp1 & 0x8000 )
3618 {
3619 bFatalError = true;
3620 }
3621
3622 // else, if there are parameters, use different error code
3623 if( !bLocal || pImg->IsFlag( SbiImageFlags::EXPLICIT ) )
3624 {
3625 // #39108 if explicit and as ELEM always a fatal error
3626 bFatalError = true;
3627
3628
3629 if( !( nOp1 & 0x8000 ) && nNotFound == ERRCODE_BASIC_PROC_UNDEFINED )
3630 {
3631 nNotFound = ERRCODE_BASIC_VAR_UNDEFINED;
3632 }
3633 }
3634 if( bFatalError )
3635 {
3636 // #39108 use dummy variable instead of fatal error
3637 if( !xDummyVar.is() )
3638 {
3639 xDummyVar = new SbxVariable( SbxVARIANT );
3640 }
3641 pElem = xDummyVar.get();
3642
3643 ClearArgvStack();
3644
3645 Error( nNotFound, aName );
3646 }
3647 else
3648 {
3649 if ( bStatic )
3650 {
3651 pElem = StepSTATIC_Impl( aName, t, 0 );
3652 }
3653 if ( !pElem )
3654 {
3655 pElem = new SbxVariable( t );
3656 if( t != SbxVARIANT )
3657 {
3658 pElem->SetFlag( SbxFlagBits::Fixed );
3659 }
3660 pElem->SetName( aName );
3661 refLocals->Put(pElem, refLocals->Count());
3662 }
3663 }
3664 }
3665 }
3666 // #39108 Args can already be deleted!
3667 if( !bFatalError )
3668 {
3669 SetupArgs( pElem, nOp1 );
3670 }
3671 // because a particular call-type is requested
3672 if (SbxMethod* pMethod = dynamic_cast<SbxMethod*>(pElem))
3673 {
3674 // shall the type be converted?
3675 SbxDataType t2 = pElem->GetType();
3676 bool bSet = false;
3677 if( (pElem->GetFlags() & SbxFlagBits::Fixed) == SbxFlagBits::NONE )
3678 {
3679 if( t != SbxVARIANT && t != t2 &&
3680 t >= SbxINTEGER && t <= SbxSTRING )
3681 {
3682 pElem->SetType( t );
3683 bSet = true;
3684 }
3685 }
3686 // assign pElem to a Ref, to delete a temp-var if applicable
3687 SbxVariableRef xDeleteRef = pElem;
3688
3689 // remove potential rests of the last call of the SbxMethod
3690 // free Write before, so that there's no error
3691 SbxFlagBits nSavFlags = pElem->GetFlags();
3692 pElem->SetFlag( SbxFlagBits::ReadWrite | SbxFlagBits::NoBroadcast );
3693 pElem->SbxValue::Clear();
3694 pElem->SetFlags( nSavFlags );
3695
3696 // don't touch before setting, as e. g. LEFT()
3697 // has to know the difference between Left$() and Left()
3698
3699 // because the methods' parameters are cut away in PopVar()
3700 SbxVariable* pNew = new SbxMethod(*pMethod);
3701 //OLD: SbxVariable* pNew = new SbxVariable( *pElem );
3702
3703 pElem->SetParameters(nullptr);
3704 pNew->SetFlag( SbxFlagBits::ReadWrite );
3705
3706 if( bSet )
3707 {
3708 pElem->SetType( t2 );
3709 }
3710 pElem = pNew;
3711 }
3712 // consider index-access for UnoObjects
3713 // definitely we want this for VBA where properties are often
3714 // collections ( which need index access ), but let's only do
3715 // this if we actually have params following
3716 else if( bVBAEnabled && dynamic_cast<const SbUnoProperty*>( pElem) != nullptr && pElem->GetParameters() )
3717 {
3718 SbxVariableRef xDeleteRef = pElem;
3719
3720 // dissolve the notify while copying variable
3721 SbxVariable* pNew = new SbxVariable( *pElem );
3722 pElem->SetParameters( nullptr );
3723 pElem = pNew;
3724 }
3725 }
3726 return CheckArray( pElem );
3727 }
3728
3729 // for current scope (e. g. query from BASIC-IDE)
3730 SbxBase* SbiRuntime::FindElementExtern( const OUString& rName )
3731 {
3732 // don't expect pMeth to be != 0, as there are none set
3733 // in the RunInit yet
3734
3735 SbxVariable* pElem = nullptr;
3736 if( !pMod || rName.isEmpty() )
3737 {
3738 return nullptr;
3739 }
3740 if( refLocals.is() )
3741 {
3742 pElem = refLocals->Find( rName, SbxClassType::DontCare );
3743 }
3744 if ( !pElem && pMeth )
3745 {
3746 const OUString aMethName = pMeth->GetName();
3747 // tdf#57308 - check if the name is the current method instance
3748 if (pMeth->GetName() == rName)
3749 {
3750 pElem = pMeth;
3751 }
3752 else
3753 {
3754 // for statics, set the method's name in front
3755 pElem = pMod->Find(aMethName + ":" + rName, SbxClassType::DontCare);
3756 }
3757 }
3758
3759
3760 // search in parameter list
3761 if( !pElem && pMeth )
3762 {
3763 SbxInfo* pInfo = pMeth->GetInfo();
3764 if( pInfo && refParams.is() )
3765 {
3766 sal_uInt32 nParamCount = refParams->Count();
3767 assert(nParamCount <= std::numeric_limits<sal_uInt16>::max());
3768 sal_uInt16 j = 1;
3769 const SbxParamInfo* pParam = pInfo->GetParam( j );
3770 while( pParam )
3771 {
3772 if( pParam->aName.equalsIgnoreAsciiCase( rName ) )
3773 {
3774 if( j >= nParamCount )
3775 {
3776 // Parameter is missing
3777 pElem = new SbxVariable( SbxSTRING );
3778 pElem->PutString( u"<missing parameter>"_ustr);
3779 }
3780 else
3781 {
3782 pElem = refParams->Get(j);
3783 }
3784 break;
3785 }
3786 pParam = pInfo->GetParam( ++j );
3787 }
3788 }
3789 }
3790
3791 // search in module
3792 if( !pElem )
3793 {
3794 bool bSave = rBasic.bNoRtl;
3795 rBasic.bNoRtl = true;
3796 pElem = pMod->Find( rName, SbxClassType::DontCare );
3797 rBasic.bNoRtl = bSave;
3798 }
3799 return pElem;
3800 }
3801
3802
3803 void SbiRuntime::SetupArgs( SbxVariable* p, sal_uInt32 nOp1 )
3804 {
3805 if( nOp1 & 0x8000 )
3806 {
3807 if( !refArgv.is() )
3808 {
3809 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
3810 }
3811 bool bHasNamed = false;
3812 sal_uInt32 i;
3813 sal_uInt32 nArgCount = refArgv->Count();
3814 for( i = 1 ; i < nArgCount ; i++ )
3815 {
3816 if (!refArgv->GetAlias(i).isEmpty())
3817 {
3818 bHasNamed = true; break;
3819 }
3820 }
3821 if( bHasNamed )
3822 {
3823 SbxInfo* pInfo = p->GetInfo();
3824 if( !pInfo )
3825 {
3826 bool bError_ = true;
3827
3828 SbUnoMethod* pUnoMethod = dynamic_cast<SbUnoMethod*>( p );
3829 SbUnoProperty* pUnoProperty = dynamic_cast<SbUnoProperty*>( p );
3830 if( pUnoMethod || pUnoProperty )
3831 {
3832 SbUnoObject* pParentUnoObj = dynamic_cast<SbUnoObject*>( p->GetParent() );
3833 if( pParentUnoObj )
3834 {
3835 Any aUnoAny = pParentUnoObj->getUnoAny();
3836 Reference< XInvocation > xInvocation;
3837 aUnoAny >>= xInvocation;
3838 if( xInvocation.is() ) // TODO: if( xOLEAutomation.is() )
3839 {
3840 bError_ = false;
3841
3842 sal_uInt32 nCurPar = 1;
3843 AutomationNamedArgsSbxArray* pArg =
3844 new AutomationNamedArgsSbxArray( nArgCount );
3845 OUString* pNames = pArg->getNames().getArray();
3846 for( i = 1 ; i < nArgCount ; i++ )
3847 {
3848 SbxVariable* pVar = refArgv->Get(i);
3849 OUString aName = refArgv->GetAlias(i);
3850 if (!aName.isEmpty())
3851 {
3852 pNames[i] = aName;
3853 }
3854 pArg->Put(pVar, nCurPar++);
3855 }
3856 refArgv = pArg;
3857 }
3858 }
3859 }
3860 else if( bVBAEnabled && p->GetType() == SbxOBJECT && (dynamic_cast<const SbxMethod*>( p) == nullptr || !p->IsBroadcaster()) )
3861 {
3862 // Check for default method with named parameters
3863 SbxBaseRef xObj = p->GetObject();
3864 if (SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>( xObj.get() ))
3865 {
3866 Any aAny = pUnoObj->getUnoAny();
3867
3868 if( aAny.getValueTypeClass() == TypeClass_INTERFACE )
3869 {
3870 Reference< XDefaultMethod > xDfltMethod( aAny, UNO_QUERY );
3871
3872 OUString sDefaultMethod;
3873 if ( xDfltMethod.is() )
3874 {
3875 sDefaultMethod = xDfltMethod->getDefaultMethodName();
3876 }
3877 if ( !sDefaultMethod.isEmpty() )
3878 {
3879 SbxVariable* meth = pUnoObj->Find( sDefaultMethod, SbxClassType::Method );
3880 if( meth != nullptr )
3881 {
3882 pInfo = meth->GetInfo();
3883 }
3884 if( pInfo )
3885 {
3886 bError_ = false;
3887 }
3888 }
3889 }
3890 }
3891 }
3892 if( bError_ )
3893 {
3894 Error( ERRCODE_BASIC_NO_NAMED_ARGS );
3895 }
3896 }
3897 else
3898 {
3899 sal_uInt32 nCurPar = 1;
3900 SbxArray* pArg = new SbxArray;
3901 for( i = 1 ; i < nArgCount ; i++ )
3902 {
3903 SbxVariable* pVar = refArgv->Get(i);
3904 OUString aName = refArgv->GetAlias(i);
3905 if (!aName.isEmpty())
3906 {
3907 // nCurPar is set to the found parameter
3908 sal_uInt16 j = 1;
3909 const SbxParamInfo* pParam = pInfo->GetParam( j );
3910 while( pParam )
3911 {
3912 if( pParam->aName.equalsIgnoreAsciiCase( aName ) )
3913 {
3914 nCurPar = j;
3915 break;
3916 }
3917 pParam = pInfo->GetParam( ++j );
3918 }
3919 if( !pParam )
3920 {
3921 Error( ERRCODE_BASIC_NAMED_NOT_FOUND ); break;
3922 }
3923 }
3924 pArg->Put(pVar, nCurPar++);
3925 }
3926 refArgv = pArg;
3927 }
3928 }
3929 // own var as parameter 0
3930 refArgv->Put(p, 0);
3931 p->SetParameters( refArgv.get() );
3932 PopArgv();
3933 }
3934 else
3935 {
3936 p->SetParameters( nullptr );
3937 }
3938 }
3939
3940 // getting an array element
3941
3942 SbxVariable* SbiRuntime::CheckArray( SbxVariable* pElem )
3943 {
3944 assert(pElem);
3945 if( ( pElem->GetType() & SbxARRAY ) && refRedim.get() != pElem )
3946 {
3947 SbxBase* pElemObj = pElem->GetObject();
3948 SbxArray* pPar = pElem->GetParameters();
3949 if (SbxDimArray* pDimArray = dynamic_cast<SbxDimArray*>(pElemObj))
3950 {
3951 // parameters may be missing, if an array is
3952 // passed as an argument
3953 if( pPar )
3954 {
3955 bool parIsArrayIndex = true;
3956 if (dynamic_cast<const SbxMethod*>(pElem))
3957 {
3958 // If this was a method, then there are two possibilities:
3959 // 1. pPar is this method's parameters.
3960 // 2. pPar is the indexes into the array returned from the method.
3961 // To disambiguate, check the 0th element of pPar.
3962 if (dynamic_cast<const SbxMethod*>(pPar->Get(0)))
3963 {
3964 // pPar was the parameters to the method, not indexes into the array
3965 parIsArrayIndex = false;
3966 }
3967 }
3968 if (parIsArrayIndex)
3969 pElem = pDimArray->Get(pPar);
3970 }
3971 }
3972 else if (SbxArray* pArray = dynamic_cast<SbxArray*>(pElemObj))
3973 {
3974 if( !pPar )
3975 {
3976 Error( ERRCODE_BASIC_OUT_OF_RANGE );
3977 pElem = new SbxVariable;
3978 }
3979 else
3980 {
3981 pElem = pArray->Get(pPar->Get(1)->GetInteger());
3982 }
3983 }
3984
3985 // #42940, set parameter 0 to NULL so that var doesn't contain itself
3986 if( pPar )
3987 {
3988 pPar->Put(nullptr, 0);
3989 }
3990 }
3991 // consider index-access for UnoObjects
3992 else if( pElem->GetType() == SbxOBJECT &&
3993 dynamic_cast<const SbxMethod*>( pElem) == nullptr &&
3994 ( !bVBAEnabled || dynamic_cast<const SbxProperty*>( pElem) == nullptr ) )
3995 {
3996 if (SbxArray* pPar = pElem->GetParameters())
3997 {
3998 // is it a uno-object?
3999 SbxBaseRef pObj = pElem->GetObject();
4000 if( pObj.is() )
4001 {
4002 if (SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>( pObj.get()))
4003 {
4004 Any aAny = pUnoObj->getUnoAny();
4005
4006 if( aAny.getValueTypeClass() == TypeClass_INTERFACE )
4007 {
4008 Reference< XIndexAccess > xIndexAccess( aAny, UNO_QUERY );
4009 if ( !bVBAEnabled )
4010 {
4011 if( xIndexAccess.is() )
4012 {
4013 sal_uInt32 nParamCount = pPar->Count() - 1;
4014 if( nParamCount != 1 )
4015 {
4016 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
4017 return pElem;
4018 }
4019
4020 // get index
4021 sal_Int32 nIndex = pPar->Get(1)->GetLong();
4022 Reference< XInterface > xRet;
4023 try
4024 {
4025 Any aAny2 = xIndexAccess->getByIndex( nIndex );
4026 aAny2 >>= xRet;
4027 }
4028 catch (const IndexOutOfBoundsException&)
4029 {
4030 // usually expect converting problem
4031 StarBASIC::Error( ERRCODE_BASIC_OUT_OF_RANGE );
4032 }
4033
4034 // #57847 always create a new variable, else error
4035 // due to PutObject(NULL) at ReadOnly-properties
4036 pElem = new SbxVariable( SbxVARIANT );
4037 if( xRet.is() )
4038 {
4039 aAny <<= xRet;
4040
4041 // #67173 don't specify a name so that the real class name is entered
4042 SbxObjectRef xWrapper = static_cast<SbxObject*>(new SbUnoObject( OUString(), aAny ));
4043 pElem->PutObject( xWrapper.get() );
4044 }
4045 else
4046 {
4047 pElem->PutObject( nullptr );
4048 }
4049 }
4050 }
4051 else
4052 {
4053 // check if there isn't a default member between the current variable
4054 // and the params, e.g.
4055 // Dim rst1 As New ADODB.Recordset
4056 // "
4057 // val = rst1("FirstName")
4058 // has the default 'Fields' member between rst1 and '("FirstName")'
4059 Any x = aAny;
4060 SbxVariable* pDflt = getDefaultProp( pElem );
4061 if ( pDflt )
4062 {
4063 pDflt->Broadcast( SfxHintId::BasicDataWanted );
4064 SbxBaseRef pDfltObj = pDflt->GetObject();
4065 if( pDfltObj.is() )
4066 {
4067 if (SbUnoObject* pSbObj = dynamic_cast<SbUnoObject*>(pDfltObj.get()))
4068 {
4069 pUnoObj = pSbObj;
4070 Any aUnoAny = pUnoObj->getUnoAny();
4071
4072 if( aUnoAny.getValueTypeClass() == TypeClass_INTERFACE )
4073 x = std::move(aUnoAny);
4074 pElem = pDflt;
4075 }
4076 }
4077 }
4078 OUString sDefaultMethod;
4079
4080 Reference< XDefaultMethod > xDfltMethod( x, UNO_QUERY );
4081
4082 if ( xDfltMethod.is() )
4083 {
4084 sDefaultMethod = xDfltMethod->getDefaultMethodName();
4085 }
4086 else if( xIndexAccess.is() )
4087 {
4088 sDefaultMethod = "getByIndex";
4089 }
4090 if ( !sDefaultMethod.isEmpty() )
4091 {
4092 SbxVariable* meth = pUnoObj->Find( sDefaultMethod, SbxClassType::Method );
4093 SbxVariableRef refTemp = meth;
4094 if ( refTemp.is() )
4095 {
4096 meth->SetParameters( pPar );
4097 SbxVariable* pNew = new SbxMethod( *static_cast<SbxMethod*>(meth) );
4098 pElem = pNew;
4099 }
4100 }
4101 }
4102 }
4103
4104 // #42940, set parameter 0 to NULL so that var doesn't contain itself
4105 pPar->Put(nullptr, 0);
4106 }
4107 else if (BasicCollection* pCol = dynamic_cast<BasicCollection*>(pObj.get()))
4108 {
4109 pElem = new SbxVariable( SbxVARIANT );
4110 pPar->Put(pElem, 0);
4111 pCol->CollItem( pPar );
4112 }
4113 }
4114 else if( bVBAEnabled ) // !pObj
4115 {
4116 SbxArray* pParam = pElem->GetParameters();
4117 if( pParam != nullptr && !pElem->IsSet( SbxFlagBits::VarToDim ) )
4118 {
4119 Error( ERRCODE_BASIC_NO_OBJECT );
4120 }
4121 }
4122 }
4123 }
4124
4125 return pElem;
4126 }
4127
4128 // loading an element from the runtime-library (+StringID+type)
4129
4130 void SbiRuntime::StepRTL( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4131 {
4132 PushVar( FindElement( rBasic.pRtl.get(), nOp1, nOp2, ERRCODE_BASIC_PROC_UNDEFINED, false ) );
4133 }
4134
4135 void SbiRuntime::StepFIND_Impl( SbxObject* pObj, sal_uInt32 nOp1, sal_uInt32 nOp2,
4136 ErrCode nNotFound, bool bStatic )
4137 {
4138 if( !refLocals.is() )
4139 {
4140 refLocals = new SbxArray;
4141 }
4142 PushVar( FindElement( pObj, nOp1, nOp2, nNotFound, true/*bLocal*/, bStatic ) );
4143 }
4144 // loading a local/global variable (+StringID+type)
4145
4146 void SbiRuntime::StepFIND( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4147 {
4148 StepFIND_Impl( pMod, nOp1, nOp2, ERRCODE_BASIC_PROC_UNDEFINED );
4149 }
4150
4151 // Search inside a class module (CM) to enable global search in time
4152 void SbiRuntime::StepFIND_CM( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4153 {
4154
4155 SbClassModuleObject* pClassModuleObject = dynamic_cast<SbClassModuleObject*>( pMod );
4156 if( pClassModuleObject )
4157 {
4158 pMod->SetFlag( SbxFlagBits::GlobalSearch );
4159 }
4160 StepFIND_Impl( pMod, nOp1, nOp2, ERRCODE_BASIC_PROC_UNDEFINED);
4161
4162 if( pClassModuleObject )
4163 {
4164 pMod->ResetFlag( SbxFlagBits::GlobalSearch );
4165 }
4166 }
4167
4168 void SbiRuntime::StepFIND_STATIC( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4169 {
4170 StepFIND_Impl( pMod, nOp1, nOp2, ERRCODE_BASIC_PROC_UNDEFINED, true );
4171 }
4172
4173 // loading an object-element (+StringID+type)
4174 // the object lies on TOS
4175
4176 void SbiRuntime::StepELEM( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4177 {
4178 SbxVariableRef pObjVar = PopVar();
4179
4180 SbxObject* pObj = dynamic_cast<SbxObject*>( pObjVar.get() );
4181 if( !pObj )
4182 {
4183 SbxBase* pObjVarObj = pObjVar->GetObject();
4184 pObj = dynamic_cast<SbxObject*>( pObjVarObj );
4185 }
4186
4187 // #56368 save reference at StepElem, otherwise objects could
4188 // lose their reference too early in qualification chains like
4189 // ActiveComponent.Selection(0).Text
4190 // #74254 now per list
4191 if( pObj )
4192 {
4193 aRefSaved.emplace_back(pObj );
4194 }
4195 PushVar( FindElement( pObj, nOp1, nOp2, ERRCODE_BASIC_NO_METHOD, false ) );
4196 }
4197
4198 /** Loading of a parameter (+offset+type)
4199 If the data type is wrong, create a copy and search for optionals including
4200 the default value. The data type SbxEMPTY shows that no parameters are given.
4201 Get( 0 ) may be EMPTY
4202
4203 @param nOp1
4204 the index of the current parameter being processed,
4205 where the entry of the index 0 is for the return value.
4206
4207 @param nOp2
4208 the data type of the parameter.
4209 */
4210 void SbiRuntime::StepPARAM( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4211 {
4212 sal_uInt16 nIdx = static_cast<sal_uInt16>( nOp1 & 0x7FFF );
4213 SbxDataType eType = static_cast<SbxDataType>(nOp2);
4214 SbxVariable* pVar;
4215
4216 // #57915 solve missing in a cleaner way
4217 sal_uInt32 nParamCount = refParams->Count();
4218 if( nIdx >= nParamCount )
4219 {
4220 sal_uInt16 iLoop = nIdx;
4221 while( iLoop >= nParamCount )
4222 {
4223 pVar = new SbxVariable();
4224 pVar->PutErr( 448 ); // like in VB: Error-Code 448 (ERRCODE_BASIC_NAMED_NOT_FOUND)
4225 // tdf#79426, tdf#125180 - add additional information about a missing parameter
4226 SetIsMissing( pVar );
4227 refParams->Put(pVar, iLoop);
4228 iLoop--;
4229 }
4230 }
4231 pVar = refParams->Get(nIdx);
4232
4233 // tdf#79426, tdf#125180 - check for optionals only if the parameter is actually missing
4234 if( pVar->GetType() == SbxERROR && IsMissing( pVar, 1 ) && nIdx )
4235 {
4236 // if there's a parameter missing, it can be OPTIONAL
4237 bool bOpt = false;
4238 if( pMeth )
4239 {
4240 SbxInfo* pInfo = pMeth->GetInfo();
4241 if ( pInfo )
4242 {
4243 const SbxParamInfo* pParam = pInfo->GetParam( nIdx );
4244 if( pParam && ( pParam->nFlags & SbxFlagBits::Optional ) )
4245 {
4246 // tdf#136143 - reset SbxFlagBits::Fixed in order to prevent type conversion errors
4247 pVar->ResetFlag( SbxFlagBits::Fixed );
4248 // Default value?
4249 sal_uInt16 nDefaultId = static_cast<sal_uInt16>(pParam->nUserData & 0x0ffff);
4250 if( nDefaultId > 0 )
4251 {
4252 // tdf#143707 - check if the data type character was added after the string
4253 // termination symbol, and convert the variable if it was present. The
4254 // data type character was added in basic/source/comp/symtbl.cxx.
4255 SbxDataType eTypeStr;
4256 OUString aDefaultStr = pImg->GetString( nDefaultId, &eTypeStr );
4257 pVar = new SbxVariable(pParam-> eType);
4258 pVar->PutString( aDefaultStr );
4259 if (eTypeStr != SbxSTRING)
4260 pVar->Convert(eTypeStr);
4261 refParams->Put(pVar, nIdx);
4262 }
4263 else if ( SbiRuntime::isVBAEnabled() && eType != SbxVARIANT )
4264 {
4265 // tdf#36737 - initialize the parameter with the default value of its type
4266 pVar = new SbxVariable( pParam->eType );
4267 refParams->Put(pVar, nIdx);
4268 }
4269 bOpt = true;
4270 }
4271 }
4272 }
4273 if( !bOpt )
4274 {
4275 Error( ERRCODE_BASIC_NOT_OPTIONAL );
4276 }
4277 }
4278 else if( eType != SbxVARIANT && static_cast<SbxDataType>(pVar->GetType() & 0x0FFF ) != eType )
4279 {
4280 // tdf#43003 - convert parameter to the requested type
4281 pVar->Convert(eType);
4282 }
4283 SetupArgs( pVar, nOp1 );
4284 PushVar( CheckArray( pVar ) );
4285 }
4286
4287 // Case-Test (+True-Target+Test-Opcode)
4288
4289 void SbiRuntime::StepCASEIS( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4290 {
4291 if (!refCaseStk.is() || !refCaseStk->Count())
4292 {
4293 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
4294 }
4295 else
4296 {
4297 SbxVariableRef xComp = PopVar();
4298 SbxVariableRef xCase = refCaseStk->Get(refCaseStk->Count() - 1);
4299 if( xCase->Compare( static_cast<SbxOperator>(nOp2), *xComp ) )
4300 {
4301 StepJUMP( nOp1 );
4302 }
4303 }
4304 }
4305
4306 // call of a DLL-procedure (+StringID+type)
4307 // the StringID's MSB shows that Argv is occupied
4308
4309 void SbiRuntime::StepCALL( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4310 {
4311 OUString aName = pImg->GetString( nOp1 & 0x7FFF );
4312 SbxArray* pArgs = nullptr;
4313 if( nOp1 & 0x8000 )
4314 {
4315 pArgs = refArgv.get();
4316 }
4317 DllCall( aName, aLibName, pArgs, static_cast<SbxDataType>(nOp2), false );
4318 aLibName.clear();
4319 if( nOp1 & 0x8000 )
4320 {
4321 PopArgv();
4322 }
4323 }
4324
4325 // call of a DLL-procedure after CDecl (+StringID+type)
4326
4327 void SbiRuntime::StepCALLC( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4328 {
4329 OUString aName = pImg->GetString( nOp1 & 0x7FFF );
4330 SbxArray* pArgs = nullptr;
4331 if( nOp1 & 0x8000 )
4332 {
4333 pArgs = refArgv.get();
4334 }
4335 DllCall( aName, aLibName, pArgs, static_cast<SbxDataType>(nOp2), true );
4336 aLibName.clear();
4337 if( nOp1 & 0x8000 )
4338 {
4339 PopArgv();
4340 }
4341 }
4342
4343
4344 // beginning of a statement (+Line+Col)
4345
4346 void SbiRuntime::StepSTMNT( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4347 {
4348 // If the Expr-Stack at the beginning of a statement contains a variable,
4349 // some fool has called X as a function, although it's a variable!
4350 bool bFatalExpr = false;
4351 OUString sUnknownMethodName;
4352 if( nExprLvl > 1 )
4353 {
4354 bFatalExpr = true;
4355 }
4356 else if( nExprLvl )
4357 {
4358 SbxVariable* p = refExprStk->Get(0);
4359 if( p->GetRefCount() > 1 &&
4360 refLocals.is() && refLocals->Find( p->GetName(), p->GetClass() ) )
4361 {
4362 sUnknownMethodName = p->GetName();
4363 bFatalExpr = true;
4364 }
4365 }
4366
4367 ClearExprStack();
4368
4369 aRefSaved.clear();
4370
4371 // We have to cancel hard here because line and column
4372 // would be wrong later otherwise!
4373 if( bFatalExpr)
4374 {
4375 StarBASIC::FatalError( ERRCODE_BASIC_NO_METHOD, sUnknownMethodName );
4376 return;
4377 }
4378 pStmnt = pCode - 9;
4379 sal_uInt16 nOld = nLine;
4380 nLine = static_cast<short>( nOp1 );
4381
4382 // #29955 & 0xFF, to filter out for-loop-level
4383 nCol1 = static_cast<short>( nOp2 & 0xFF );
4384
4385 // find the next STMNT-command to set the final column
4386 // of this statement
4387
4388 nCol2 = 0xffff;
4389 sal_uInt16 n1, n2;
4390 const sal_uInt8* p = pMod->FindNextStmnt( pCode, n1, n2 );
4391 if( p )
4392 {
4393 if( n1 == nOp1 )
4394 {
4395 // #29955 & 0xFF, to filter out for-loop-level
4396 nCol2 = (n2 & 0xFF) - 1;
4397 }
4398 }
4399
4400 // #29955 correct for-loop-level, #67452 NOT in the error-handler
4401 if( !bInError )
4402 {
4403 // (there's a difference here in case of a jump out of a loop)
4404 sal_uInt16 nExpectedForLevel = static_cast<sal_uInt16>( nOp2 / 0x100 );
4405 if( !pGosubStk.empty() )
4406 {
4407 nExpectedForLevel = nExpectedForLevel + pGosubStk.back().nStartForLvl;
4408 }
4409
4410 // if the actual for-level is too small it'd jump out
4411 // of a loop -> corrected
4412 while( nForLvl > nExpectedForLevel )
4413 {
4414 PopFor();
4415 }
4416 }
4417
4418 // 16.10.96: #31460 new concept for StepInto/Over/Out
4419 // see explanation at SbiInstance::CalcBreakCallLevel
4420 if( pInst->nCallLvl <= pInst->nBreakCallLvl )
4421 {
4422 StarBASIC* pStepBasic = GetCurrentBasic( &rBasic );
4423 BasicDebugFlags nNewFlags = pStepBasic->StepPoint( nLine, nCol1, nCol2 );
4424
4425 pInst->CalcBreakCallLevel( nNewFlags );
4426 }
4427
4428 // break points only at STMNT-commands in a new line!
4429 else if( ( nOp1 != nOld )
4430 && ( nFlags & BasicDebugFlags::Break )
4431 && pMod->IsBP( static_cast<sal_uInt16>( nOp1 ) ) )
4432 {
4433 StarBASIC* pBreakBasic = GetCurrentBasic( &rBasic );
4434 BasicDebugFlags nNewFlags = pBreakBasic->BreakPoint( nLine, nCol1, nCol2 );
4435
4436 pInst->CalcBreakCallLevel( nNewFlags );
4437 }
4438 }
4439
4440 // (+StreamMode+Flags)
4441 // Stack: block length
4442 // channel number
4443 // file name
4444
4445 void SbiRuntime::StepOPEN( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4446 {
4447 SbxVariableRef pName = PopVar();
4448 SbxVariableRef pChan = PopVar();
4449 SbxVariableRef pLen = PopVar();
4450 short nBlkLen = pLen->GetInteger();
4451 short nChan = pChan->GetInteger();
4452 OUString aName = pName->GetOUString();
4453 pIosys->Open( nChan, aName, static_cast<StreamMode>( nOp1 ),
4454 static_cast<SbiStreamFlags>( nOp2 ), nBlkLen );
4455 Error( pIosys->GetError() );
4456 }
4457
4458 // create object (+StringID+StringID)
4459
4460 void SbiRuntime::StepCREATE( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4461 {
4462 OUString aClass( pImg->GetString( nOp2 ) );
4463 SbxObjectRef pObj = SbxBase::CreateObject( aClass );
4464 if( !pObj )
4465 {
4466 Error( ERRCODE_BASIC_INVALID_OBJECT );
4467 }
4468 else
4469 {
4470 OUString aName( pImg->GetString( nOp1 ) );
4471 pObj->SetName( aName );
4472 // the object must be able to call the BASIC
4473 pObj->SetParent( &rBasic );
4474 SbxVariableRef pNew = new SbxVariable;
4475 pNew->PutObject( pObj.get() );
4476 PushVar( pNew.get() );
4477 }
4478 }
4479
4480 void SbiRuntime::StepDCREATE( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4481 {
4482 StepDCREATE_IMPL( nOp1, nOp2 );
4483 }
4484
4485 void SbiRuntime::StepDCREATE_REDIMP( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4486 {
4487 StepDCREATE_IMPL( nOp1, nOp2 );
4488 }
4489
4490 // #56204 create object array (+StringID+StringID), DCREATE == Dim-Create
4491 void SbiRuntime::StepDCREATE_IMPL( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4492 {
4493 SbxVariableRef refVar = PopVar();
4494
4495 DimImpl( refVar );
4496
4497 // fill the array with instances of the requested class
4498 SbxBase* pObj = refVar->GetObject();
4499 if (!pObj)
4500 {
4501 StarBASIC::Error( ERRCODE_BASIC_INVALID_OBJECT );
4502 return;
4503 }
4504
4505 SbxDimArray* pArray = dynamic_cast<SbxDimArray*>(pObj);
4506 if (!pArray)
4507 return;
4508
4509 const sal_Int32 nDims = pArray->GetDims();
4510 sal_Int32 nTotalSize = nDims > 0 ? 1 : 0;
4511
4512 // must be a one-dimensional array
4513 sal_Int32 nLower, nUpper;
4514 for( sal_Int32 i = 0 ; i < nDims ; ++i )
4515 {
4516 pArray->GetDim(i + 1, nLower, nUpper);
4517 const sal_Int32 nSize = nUpper - nLower + 1;
4518 nTotalSize *= nSize;
4519 }
4520
4521 // Optimization: pre-allocate underlying container
4522 if (nTotalSize > 0)
4523 pArray->SbxArray::GetRef(nTotalSize - 1);
4524
4525 // First, fill those parts of the array that are preserved
4526 bool bWasError = false;
4527 const bool bRestored = implRestorePreservedArray(pArray, refRedimpArray, &bWasError);
4528 if (bWasError)
4529 nTotalSize = 0; // on error, don't create objects
4530
4531 // create objects and insert them into the array
4532 OUString aClass( pImg->GetString( nOp2 ) );
4533 OUString aName;
4534 for( sal_Int32 i = 0 ; i < nTotalSize ; ++i )
4535 {
4536 if (!bRestored || !pArray->SbxArray::GetRef(i)) // For those left unset after preserve
4537 {
4538 SbxObjectRef pClassObj = SbxBase::CreateObject(aClass);
4539 if (!pClassObj)
4540 {
4541 Error(ERRCODE_BASIC_INVALID_OBJECT);
4542 break;
4543 }
4544 else
4545 {
4546 if (aName.isEmpty())
4547 aName = pImg->GetString(nOp1);
4548 pClassObj->SetName(aName);
4549 // the object must be able to call the basic
4550 pClassObj->SetParent(&rBasic);
4551 pArray->SbxArray::Put(pClassObj.get(), i);
4552 }
4553 }
4554 }
4555 }
4556
4557 void SbiRuntime::StepTCREATE( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4558 {
4559 OUString aName( pImg->GetString( nOp1 ) );
4560 OUString aClass( pImg->GetString( nOp2 ) );
4561
4562 SbxObjectRef pCopyObj = createUserTypeImpl( aClass );
4563 if( pCopyObj )
4564 {
4565 pCopyObj->SetName( aName );
4566 }
4567 SbxVariableRef pNew = new SbxVariable;
4568 pNew->PutObject( pCopyObj.get() );
4569 pNew->SetDeclareClassName( aClass );
4570 PushVar( pNew.get() );
4571 }
4572
4573 void SbiRuntime::implHandleSbxFlags( SbxVariable* pVar, SbxDataType t, sal_uInt32 nOp2 )
4574 {
4575 bool bWithEvents = ((t & 0xff) == SbxOBJECT && (nOp2 & SBX_TYPE_WITH_EVENTS_FLAG) != 0);
4576 if( bWithEvents )
4577 {
4578 pVar->SetFlag( SbxFlagBits::WithEvents );
4579 }
4580 bool bDimAsNew = ((nOp2 & SBX_TYPE_DIM_AS_NEW_FLAG) != 0);
4581 if( bDimAsNew )
4582 {
4583 pVar->SetFlag( SbxFlagBits::DimAsNew );
4584 }
4585 bool bFixedString = ((t & 0xff) == SbxSTRING && (nOp2 & SBX_FIXED_LEN_STRING_FLAG) != 0);
4586 if( bFixedString )
4587 {
4588 sal_uInt16 nCount = static_cast<sal_uInt16>( nOp2 >> 17 ); // len = all bits above 0x10000
4589 pVar->PutString(OUString::Concat(RepeatedUChar('\0', nCount)));
4590 }
4591
4592 bool bVarToDim = ((nOp2 & SBX_TYPE_VAR_TO_DIM_FLAG) != 0);
4593 if( bVarToDim )
4594 {
4595 pVar->SetFlag( SbxFlagBits::VarToDim );
4596 }
4597 }
4598
4599 // establishing a local variable (+StringID+type)
4600
4601 void SbiRuntime::StepLOCAL( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4602 {
4603 if( !refLocals.is() )
4604 {
4605 refLocals = new SbxArray;
4606 }
4607 OUString aName( pImg->GetString( nOp1 ) );
4608 if( refLocals->Find( aName, SbxClassType::DontCare ) == nullptr )
4609 {
4610 SbxDataType t = static_cast<SbxDataType>(nOp2 & 0xffff);
4611 SbxVariable* p = new SbxVariable( t );
4612 p->SetName( aName );
4613 implHandleSbxFlags( p, t, nOp2 );
4614 refLocals->Put(p, refLocals->Count());
4615 }
4616 }
4617
4618 // establishing a module-global variable (+StringID+type)
4619
4620 void SbiRuntime::StepPUBLIC_Impl( sal_uInt32 nOp1, sal_uInt32 nOp2, bool bUsedForClassModule )
4621 {
4622 OUString aName( pImg->GetString( nOp1 ) );
4623 SbxDataType t = static_cast<SbxDataType>(nOp2 & 0xffff);
4624 bool bFlag = pMod->IsSet( SbxFlagBits::NoModify );
4625 pMod->SetFlag( SbxFlagBits::NoModify );
4626 SbxVariableRef p = pMod->Find( aName, SbxClassType::Property );
4627 if( p.is() )
4628 {
4629 pMod->Remove (p.get());
4630 }
4631 SbProperty* pProp = pMod->GetProperty( aName, t );
4632 if( !bUsedForClassModule )
4633 {
4634 pProp->SetFlag( SbxFlagBits::Private );
4635 }
4636 if( !bFlag )
4637 {
4638 pMod->ResetFlag( SbxFlagBits::NoModify );
4639 }
4640 if( pProp )
4641 {
4642 pProp->SetFlag( SbxFlagBits::DontStore );
4643 // from 2.7.1996: HACK because of 'reference can't be saved'
4644 pProp->SetFlag( SbxFlagBits::NoModify);
4645
4646 implHandleSbxFlags( pProp, t, nOp2 );
4647 }
4648 }
4649
4650 void SbiRuntime::StepPUBLIC( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4651 {
4652 StepPUBLIC_Impl( nOp1, nOp2, false );
4653 }
4654
4655 void SbiRuntime::StepPUBLIC_P( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4656 {
4657 // Creates module variable that isn't reinitialised when
4658 // between invocations ( for VBASupport & document basic only )
4659 if( pMod->pImage->bFirstInit )
4660 {
4661 bool bUsedForClassModule = pImg->IsFlag( SbiImageFlags::CLASSMODULE );
4662 StepPUBLIC_Impl( nOp1, nOp2, bUsedForClassModule );
4663 }
4664 }
4665
4666 // establishing a global variable (+StringID+type)
4667
4668 void SbiRuntime::StepGLOBAL( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4669 {
4670 if( pImg->IsFlag( SbiImageFlags::CLASSMODULE ) )
4671 {
4672 StepPUBLIC_Impl( nOp1, nOp2, true );
4673 }
4674 OUString aName( pImg->GetString( nOp1 ) );
4675 SbxDataType t = static_cast<SbxDataType>(nOp2 & 0xffff);
4676
4677 // Store module scope variables at module scope
4678 // in non vba mode these are stored at the library level :/
4679 // not sure if this really should not be enabled for ALL basic
4680 SbxObject* pStorage = &rBasic;
4681 if ( SbiRuntime::isVBAEnabled() )
4682 {
4683 pStorage = pMod;
4684 pMod->AddVarName( aName );
4685 }
4686
4687 bool bFlag = pStorage->IsSet( SbxFlagBits::NoModify );
4688 rBasic.SetFlag( SbxFlagBits::NoModify );
4689 SbxVariableRef p = pStorage->Find( aName, SbxClassType::Property );
4690 if( p.is() )
4691 {
4692 pStorage->Remove (p.get());
4693 }
4694 p = pStorage->Make( aName, SbxClassType::Property, t );
4695 if( !bFlag )
4696 {
4697 pStorage->ResetFlag( SbxFlagBits::NoModify );
4698 }
4699 if( p.is() )
4700 {
4701 p->SetFlag( SbxFlagBits::DontStore );
4702 // from 2.7.1996: HACK because of 'reference can't be saved'
4703 p->SetFlag( SbxFlagBits::NoModify);
4704 }
4705 }
4706
4707
4708 // Creates global variable that isn't reinitialised when
4709 // basic is restarted, P=PERSIST (+StringID+Typ)
4710
4711 void SbiRuntime::StepGLOBAL_P( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4712 {
4713 if( pMod->pImage->bFirstInit )
4714 {
4715 StepGLOBAL( nOp1, nOp2 );
4716 }
4717 }
4718
4719
4720 // Searches for global variable, behavior depends on the fact
4721 // if the variable is initialised for the first time
4722
4723 void SbiRuntime::StepFIND_G( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4724 {
4725 if( pMod->pImage->bFirstInit )
4726 {
4727 // Behave like always during first init
4728 StepFIND( nOp1, nOp2 );
4729 }
4730 else
4731 {
4732 // Return dummy variable
4733 SbxDataType t = static_cast<SbxDataType>(nOp2);
4734 OUString aName( pImg->GetString( nOp1 & 0x7FFF ) );
4735
4736 SbxVariable* pDummyVar = new SbxVariable( t );
4737 pDummyVar->SetName( aName );
4738 PushVar( pDummyVar );
4739 }
4740 }
4741
4742
4743 SbxVariable* SbiRuntime::StepSTATIC_Impl(
4744 OUString const & aName, SbxDataType t, sal_uInt32 nOp2 )
4745 {
4746 SbxVariable* p = nullptr;
4747 if ( pMeth )
4748 {
4749 SbxArray* pStatics = pMeth->GetStatics();
4750 if( pStatics && ( pStatics->Find( aName, SbxClassType::DontCare ) == nullptr ) )
4751 {
4752 p = new SbxVariable( t );
4753 if( t != SbxVARIANT )
4754 {
4755 p->SetFlag( SbxFlagBits::Fixed );
4756 }
4757 p->SetName( aName );
4758 implHandleSbxFlags( p, t, nOp2 );
4759 pStatics->Put(p, pStatics->Count());
4760 }
4761 }
4762 return p;
4763 }
4764 // establishing a static variable (+StringID+type)
4765 void SbiRuntime::StepSTATIC( sal_uInt32 nOp1, sal_uInt32 nOp2 )
4766 {
4767 OUString aName( pImg->GetString( nOp1 ) );
4768 SbxDataType t = static_cast<SbxDataType>(nOp2 & 0xffff);
4769 StepSTATIC_Impl( aName, t, nOp2 );
4770 }
4771
4772 #endif
4773
4774 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */