tdf#146742 - Move the initialization code of a method to SbMethod::Call

Change-Id: I516bacdcd22ed094a0e9b6e125e941f32fe88c82
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129238
Tested-by: Jenkins
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
diff --git a/basic/source/classes/sbxmod.cxx b/basic/source/classes/sbxmod.cxx
index 2d38de1..e9502a2 100644
--- a/basic/source/classes/sbxmod.cxx
+++ b/basic/source/classes/sbxmod.cxx
@@ -2062,6 +2062,12 @@ ErrCode SbMethod::Call( SbxValue* pRet, SbxVariable* pCaller )
    if( bInvalid && !pMod_->Compile() )
        StarBASIC::Error( ERRCODE_BASIC_BAD_PROP_VALUE );

    // tdf#143582 - clear return value of the method before calling it
    const SbxFlagBits nSavFlags = GetFlags();
    SetFlag(SbxFlagBits::ReadWrite | SbxFlagBits::NoBroadcast);
    Clear();
    SetFlags(nSavFlags);

    Get( aVals );
    if ( pRet )
        pRet->Put( aVals );
diff --git a/sc/qa/extras/macros-test.cxx b/sc/qa/extras/macros-test.cxx
index 2326f10..a774129 100644
--- a/sc/qa/extras/macros-test.cxx
+++ b/sc/qa/extras/macros-test.cxx
@@ -66,6 +66,7 @@ public:
    void testTdf143582();
    void testTdf144085();
    void testTdf130307();
    void testTdf146742();
    void testMacroButtonFormControlXlsxExport();

    CPPUNIT_TEST_SUITE(ScMacrosTest);
@@ -92,6 +93,7 @@ public:
    CPPUNIT_TEST(testTdf143582);
    CPPUNIT_TEST(testTdf144085);
    CPPUNIT_TEST(testTdf130307);
    CPPUNIT_TEST(testTdf146742);
    CPPUNIT_TEST(testMacroButtonFormControlXlsxExport);

    CPPUNIT_TEST_SUITE_END();
@@ -266,6 +268,34 @@ void ScMacrosTest::testRowColumn()
    pDocSh->DoClose();
}

void ScMacrosTest::testTdf146742()
{
    OUString aFileName;
    createFileURL(u"tdf146742.ods", aFileName);
    uno::Reference<css::lang::XComponent> xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");

    // Export to ODS and reload the file
    saveAndReload(xComponent, "calc8");
    CPPUNIT_ASSERT(xComponent);

    SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
    CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
    ScDocShell* pDocSh = static_cast<ScDocShell*>(pFoundShell);
    ScDocument& rDoc = pDocSh->GetDocument();

    CPPUNIT_ASSERT_EQUAL(OUString("1"), rDoc.GetString(ScAddress(0,0,0)));
    CPPUNIT_ASSERT_EQUAL(OUString("2"), rDoc.GetString(ScAddress(0,1,0)));

    CPPUNIT_ASSERT_EQUAL(OUString("TRUE"), rDoc.GetString(ScAddress(1,0,0)));
    // Without the fix in place, this test would have failed with
    // - Expected: FALSE
    // - Actual  : TRUE
    CPPUNIT_ASSERT_EQUAL(OUString("FALSE"), rDoc.GetString(ScAddress(1,1,0)));

    css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
    xCloseable->close(true);
}

void ScMacrosTest::testMacroButtonFormControlXlsxExport()
{
    // Given a button form control with an associated macro:
diff --git a/sc/qa/extras/testdocuments/tdf146742.ods b/sc/qa/extras/testdocuments/tdf146742.ods
new file mode 100644
index 0000000..18ede8a
--- /dev/null
+++ b/sc/qa/extras/testdocuments/tdf146742.ods
Binary files differ
diff --git a/scripting/source/basprov/basscript.cxx b/scripting/source/basprov/basscript.cxx
index 2cc67d4..9054ee4 100644
--- a/scripting/source/basprov/basscript.cxx
+++ b/scripting/source/basprov/basscript.cxx
@@ -244,11 +244,6 @@ constexpr OUStringLiteral BASSCRIPT_PROPERTY_CALLER = u"Caller";
            if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
                aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) );

            // tdf#143582 - clear return value of the method before calling it
            const SbxFlagBits nSavFlags = m_xMethod->GetFlags();
            m_xMethod->SetFlag(SbxFlagBits::ReadWrite | SbxFlagBits::NoBroadcast);
            m_xMethod->Clear();
            m_xMethod->SetFlags(nSavFlags);
            if ( m_caller.hasElements() && m_caller[ 0 ].hasValue()  )
            {
                SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );