summaryrefslogtreecommitdiffstats
path: root/scripting/source/basprov/basscript.cxx
blob: b5a3f613653454ed5423308c00e03c59545bb77f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "basscript.hxx"
#include <vcl/svapp.hxx>
#include <basic/sbx.hxx>
#include <basic/sbmod.hxx>
#include <basic/sbmeth.hxx>
#include <basic/sbuno.hxx>
#include <basic/basmgr.hxx>
#include <com/sun/star/script/provider/ScriptFrameworkErrorException.hpp>
#include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
#include <bcholder.hxx>
#include <comphelper/propertycontainer.hxx>
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <map>


using namespace ::com::sun::star;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::script;
using namespace ::com::sun::star::document;
using namespace ::com::sun::star::beans;


namespace basprov
{

#define BASSCRIPT_PROPERTY_ID_CALLER         1
constexpr OUStringLiteral BASSCRIPT_PROPERTY_CALLER = u"Caller";

#define BASSCRIPT_DEFAULT_ATTRIBS()       PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT

    typedef ::std::map< sal_Int16, Any > OutParamMap;


    // BasicScriptImpl


    BasicScriptImpl::BasicScriptImpl( const OUString& funcName, SbMethodRef const & xMethod )
        : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
        ,OPropertyContainer( GetBroadcastHelper() )
        ,m_xMethod( xMethod )
        ,m_funcName( funcName )
        ,m_documentBasicManager( nullptr )
        ,m_xDocumentScriptContext()
    {
        registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, cppu::UnoType<decltype(m_caller)>::get() );
    }


    BasicScriptImpl::BasicScriptImpl( const OUString& funcName, SbMethodRef const & xMethod,
        BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
        ,OPropertyContainer( GetBroadcastHelper() )
        ,m_xMethod( xMethod )
        ,m_funcName( funcName )
        ,m_documentBasicManager( &documentBasicManager )
        ,m_xDocumentScriptContext( documentScriptContext )
    {
        StartListening( *m_documentBasicManager );
        registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, cppu::UnoType<decltype(m_caller)>::get() );
    }


    BasicScriptImpl::~BasicScriptImpl()
    {
        SolarMutexGuard g;

        if ( m_documentBasicManager )
            EndListening( *m_documentBasicManager );
    }


    // SfxListener

    void BasicScriptImpl::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
    {
        if ( &rBC != m_documentBasicManager )
        {
            OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
            // not interested in
            return;
        }
        if ( rHint.GetId() == SfxHintId::Dying )
        {
            m_documentBasicManager = nullptr;
            EndListening( rBC );    // prevent multiple notifications
        }
    }


    // XInterface


    IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )


    // XTypeProvider


    IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )


    // OPropertySetHelper


    ::cppu::IPropertyArrayHelper& BasicScriptImpl::getInfoHelper(  )
    {
        return *getArrayHelper();
    }


    // OPropertyArrayUsageHelper


    ::cppu::IPropertyArrayHelper* BasicScriptImpl::createArrayHelper(  ) const
    {
        Sequence< Property > aProps;
        describeProperties( aProps );
        return new ::cppu::OPropertyArrayHelper( aProps );
    }


    // XPropertySet


    Reference< XPropertySetInfo > BasicScriptImpl::getPropertySetInfo(  )
    {
        Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
        return xInfo;
    }


    // XScript


    Any BasicScriptImpl::invoke( const Sequence< Any >& aParams, Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
    {
        // TODO: throw CannotConvertException
        // TODO: check length of aOutParamIndex, aOutParam

        SolarMutexGuard aGuard;

        Any aReturn;

        if ( m_xMethod.is() )
        {
            // check if compiled
            SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() );
            if ( pModule && !pModule->IsCompiled() )
                pModule->Compile();

            // check number of parameters
            sal_Int32 nParamsCount = aParams.getLength();
            SbxInfo* pInfo = m_xMethod->GetInfo();
            if ( pInfo )
            {
                sal_Int32 nSbxOptional = 0;
                sal_uInt16 n = 1;
                for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) )
                {
                    if ( pParamInfo->nFlags & SbxFlagBits::Optional )
                        ++nSbxOptional;
                    else
                        nSbxOptional = 0;
                }
                sal_Int32 nSbxCount = n - 1;
                if ( nParamsCount < nSbxCount - nSbxOptional )
                {
                    throw provider::ScriptFrameworkErrorException(
                         "wrong number of parameters!",
                         Reference< XInterface >(),
                         m_funcName,
                         "Basic",
                         provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT  );
                }
            }

            // set parameters
            SbxArrayRef xSbxParams;
            if ( nParamsCount > 0 )
            {
                xSbxParams = new SbxArray;
                const Any* pParams = aParams.getConstArray();
                for ( sal_Int32 i = 0; i < nParamsCount; ++i )
                {
                    SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
                    unoToSbxValue( xSbxVar.get(), pParams[i] );
                    xSbxParams->Put(xSbxVar.get(), static_cast<sal_uInt32>(i) + 1);

                    if (pInfo)
                    {
                        if (auto* p = pInfo->GetParam(static_cast<sal_uInt16>(i) + 1))
                        {
                            SbxDataType t = static_cast<SbxDataType>(p->eType & 0x0FFF);
                            // tdf#133889 Revert the downcasting performed in sbxToUnoValueImpl
                            // to allow passing by reference.
                            SbxDataType a = xSbxVar->GetType();
                            if (t == SbxSINGLE && (a == SbxINTEGER || a == SbxLONG))
                            {
                                sal_Int32 val = xSbxVar->GetLong();
                                if (val >= -16777216 && val <= 16777215)
                                    xSbxVar->SetType(t);
                            }
                            else if (t == SbxDOUBLE && (a == SbxINTEGER || a == SbxLONG))
                                xSbxVar->SetType(t);
                            else if (t == SbxLONG && a == SbxINTEGER)
                                xSbxVar->SetType(t);
                            else if (t == SbxULONG && a == SbxUSHORT)
                                xSbxVar->SetType(t);
                            // Enable passing by ref
                            if (t != SbxVARIANT)
                                xSbxVar->SetFlag(SbxFlagBits::Fixed);
                        }
                    }
                }
            }
            if ( xSbxParams.is() )
                m_xMethod->SetParameters( xSbxParams.get() );

            // call method
            SbxVariableRef xReturn = new SbxVariable;
            ErrCode nErr = ERRCODE_NONE;

            // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
            Any aOldThisComponent;
            if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
                m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", Any( m_xDocumentScriptContext ), &aOldThisComponent );

            if ( m_caller.hasElements() && m_caller[ 0 ].hasValue()  )
            {
                SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
                unoToSbxValue( xCallerVar.get(), m_caller[ 0 ] );
                nErr = m_xMethod->Call( xReturn.get(), xCallerVar.get() );
            }
            else
                nErr = m_xMethod->Call( xReturn.get() );

            if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
                m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );

            if ( nErr != ERRCODE_NONE )
            {
                // TODO: throw InvocationTargetException ?
            }

            // get output parameters
            if ( xSbxParams.is() )
            {
                SbxInfo* pInfo_ = m_xMethod->GetInfo();
                if ( pInfo_ )
                {
                    OutParamMap aOutParamMap;
                    for (sal_uInt32 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n)
                    {
                        assert(nCount <= std::numeric_limits<sal_uInt16>::max());
                        const SbxParamInfo* pParamInfo = pInfo_->GetParam( sal::static_int_cast<sal_uInt16>(n) );
                        if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 )
                        {
                            SbxVariable* pVar = xSbxParams->Get(n);
                            if ( pVar )
                            {
                                SbxVariableRef xVar = pVar;
                                aOutParamMap.emplace( n - 1, sbxToUnoValue( xVar.get() ) );
                            }
                        }
                    }
                    sal_Int32 nOutParamCount = aOutParamMap.size();
                    aOutParamIndex.realloc( nOutParamCount );
                    aOutParam.realloc( nOutParamCount );
                    sal_Int16* pOutParamIndex = aOutParamIndex.getArray();
                    Any* pOutParam = aOutParam.getArray();
                    for ( const auto& rEntry : aOutParamMap )
                    {
                        *pOutParamIndex = rEntry.first;
                        ++pOutParamIndex;
                        *pOutParam = rEntry.second;
                        ++pOutParam;
                    }
                }
            }

            // get return value
            aReturn = sbxToUnoValue( xReturn.get() );

            // reset parameters
            m_xMethod->SetParameters( nullptr );
        }

        return aReturn;
    }


}   // namespace basprov


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */