LibreOffice Module scripting (master) 1
basscript.cxx
Go to the documentation of this file.
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 "basscript.hxx"
21#include <utility>
22#include <vcl/svapp.hxx>
23#include <basic/sbx.hxx>
24#include <basic/sbmod.hxx>
25#include <basic/sbmeth.hxx>
26#include <basic/sbuno.hxx>
27#include <basic/basmgr.hxx>
28#include <com/sun/star/script/provider/ScriptFrameworkErrorException.hpp>
29#include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
30#include <bcholder.hxx>
32#include <com/sun/star/beans/PropertyAttribute.hpp>
33#include <map>
34
35
36using namespace ::com::sun::star;
37using namespace ::com::sun::star::lang;
38using namespace ::com::sun::star::uno;
39using namespace ::com::sun::star::script;
40using namespace ::com::sun::star::document;
41using namespace ::com::sun::star::beans;
42
43
44namespace basprov
45{
46
47#define BASSCRIPT_PROPERTY_ID_CALLER 1
48constexpr OUStringLiteral BASSCRIPT_PROPERTY_CALLER = u"Caller";
49
50#define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
51
52 typedef ::std::map< sal_Int16, Any > OutParamMap;
53
54
55 // BasicScriptImpl
56
57
58 BasicScriptImpl::BasicScriptImpl( OUString funcName, SbMethodRef xMethod )
59 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
60 ,OPropertyContainer( GetBroadcastHelper() )
61 ,m_xMethod(std::move( xMethod ))
62 ,m_funcName(std::move( funcName ))
63 ,m_documentBasicManager( nullptr )
64 ,m_xDocumentScriptContext()
65 {
67 }
68
69
70 BasicScriptImpl::BasicScriptImpl( OUString funcName, SbMethodRef xMethod,
71 BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
72 ,OPropertyContainer( GetBroadcastHelper() )
73 ,m_xMethod(std::move( xMethod ))
74 ,m_funcName(std::move( funcName ))
75 ,m_documentBasicManager( &documentBasicManager )
76 ,m_xDocumentScriptContext( documentScriptContext )
77 {
80 }
81
82
84 {
86
89 }
90
91
92 // SfxListener
93
95 {
96 if ( &rBC != m_documentBasicManager )
97 {
98 OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
99 // not interested in
100 return;
101 }
102 if ( rHint.GetId() == SfxHintId::Dying )
103 {
104 m_documentBasicManager = nullptr;
105 EndListening( rBC ); // prevent multiple notifications
106 }
107 }
108
109
110 // XInterface
111
112
114
115
116 // XTypeProvider
117
118
120
121
122 // OPropertySetHelper
123
124
125 ::cppu::IPropertyArrayHelper& BasicScriptImpl::getInfoHelper( )
126 {
127 return *getArrayHelper();
128 }
129
130
131 // OPropertyArrayUsageHelper
132
133
135 {
136 Sequence< Property > aProps;
137 describeProperties( aProps );
138 return new ::cppu::OPropertyArrayHelper( aProps );
139 }
140
141
142 // XPropertySet
143
144
145 Reference< XPropertySetInfo > BasicScriptImpl::getPropertySetInfo( )
146 {
147 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
148 return xInfo;
149 }
150
151
152 // XScript
153
154
155 Any BasicScriptImpl::invoke( const Sequence< Any >& aParams, Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
156 {
157 // TODO: throw CannotConvertException
158 // TODO: check length of aOutParamIndex, aOutParam
159
160 SolarMutexGuard aGuard;
161
162 Any aReturn;
163
164 if ( m_xMethod.is() )
165 {
166 // check if compiled
167 SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() );
168 if ( pModule && !pModule->IsCompiled() )
169 pModule->Compile();
170
171 // check number of parameters
172 sal_Int32 nParamsCount = aParams.getLength();
173 SbxInfo* pInfo = m_xMethod->GetInfo();
174 if ( pInfo )
175 {
176 sal_Int32 nSbxOptional = 0;
177 sal_uInt16 n = 1;
178 for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) )
179 {
180 if ( pParamInfo->nFlags & SbxFlagBits::Optional )
181 ++nSbxOptional;
182 else
183 nSbxOptional = 0;
184 }
185 sal_Int32 nSbxCount = n - 1;
186 if ( nParamsCount < nSbxCount - nSbxOptional )
187 {
188 throw provider::ScriptFrameworkErrorException(
189 "wrong number of parameters!",
190 Reference< XInterface >(),
192 "Basic",
193 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
194 }
195 }
196
197 // set parameters
198 SbxArrayRef xSbxParams;
199 if ( nParamsCount > 0 )
200 {
201 xSbxParams = new SbxArray;
202 const Any* pParams = aParams.getConstArray();
203 for ( sal_Int32 i = 0; i < nParamsCount; ++i )
204 {
205 SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
206 unoToSbxValue( xSbxVar.get(), pParams[i] );
207 xSbxParams->Put(xSbxVar.get(), static_cast<sal_uInt32>(i) + 1);
208
209 if (pInfo)
210 {
211 if (auto* p = pInfo->GetParam(static_cast<sal_uInt16>(i) + 1))
212 {
213 SbxDataType t = static_cast<SbxDataType>(p->eType & 0x0FFF);
214 // tdf#133889 Revert the downcasting performed in sbxToUnoValueImpl
215 // to allow passing by reference.
216 SbxDataType a = xSbxVar->GetType();
217 if (t == SbxSINGLE && (a == SbxINTEGER || a == SbxLONG))
218 {
219 sal_Int32 val = xSbxVar->GetLong();
220 if (val >= -16777216 && val <= 16777215)
221 xSbxVar->SetType(t);
222 }
223 else if (t == SbxDOUBLE && (a == SbxINTEGER || a == SbxLONG))
224 xSbxVar->SetType(t);
225 else if (t == SbxLONG && a == SbxINTEGER)
226 xSbxVar->SetType(t);
227 else if (t == SbxULONG && a == SbxUSHORT)
228 xSbxVar->SetType(t);
229 // Enable passing by ref
230 if (t != SbxVARIANT)
231 xSbxVar->SetFlag(SbxFlagBits::Fixed);
232 }
233 }
234 }
235 }
236 if ( xSbxParams.is() )
237 m_xMethod->SetParameters( xSbxParams.get() );
238
239 // call method
240 SbxVariableRef xReturn = new SbxVariable;
241 ErrCode nErr = ERRCODE_NONE;
242
243 // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
244 Any aOldThisComponent;
246 m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", Any( m_xDocumentScriptContext ), &aOldThisComponent );
247
248 if ( m_caller.hasElements() && m_caller[ 0 ].hasValue() )
249 {
250 SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
251 unoToSbxValue( xCallerVar.get(), m_caller[ 0 ] );
252 nErr = m_xMethod->Call( xReturn.get(), xCallerVar.get() );
253 }
254 else
255 nErr = m_xMethod->Call( xReturn.get() );
256
258 m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );
259
260 if ( nErr != ERRCODE_NONE )
261 {
262 // TODO: throw InvocationTargetException ?
263 }
264
265 // get output parameters
266 if ( xSbxParams.is() )
267 {
268 SbxInfo* pInfo_ = m_xMethod->GetInfo();
269 if ( pInfo_ )
270 {
271 OutParamMap aOutParamMap;
272 for (sal_uInt32 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n)
273 {
274 assert(nCount <= std::numeric_limits<sal_uInt16>::max());
275 const SbxParamInfo* pParamInfo = pInfo_->GetParam( sal::static_int_cast<sal_uInt16>(n) );
276 if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 )
277 {
278 SbxVariable* pVar = xSbxParams->Get(n);
279 if ( pVar )
280 {
281 SbxVariableRef xVar = pVar;
282 aOutParamMap.emplace( n - 1, sbxToUnoValue( xVar.get() ) );
283 }
284 }
285 }
286 sal_Int32 nOutParamCount = aOutParamMap.size();
287 aOutParamIndex.realloc( nOutParamCount );
288 aOutParam.realloc( nOutParamCount );
289 sal_Int16* pOutParamIndex = aOutParamIndex.getArray();
290 Any* pOutParam = aOutParam.getArray();
291 for ( const auto& rEntry : aOutParamMap )
292 {
293 *pOutParamIndex = rEntry.first;
294 ++pOutParamIndex;
295 *pOutParam = rEntry.second;
296 ++pOutParam;
297 }
298 }
299 }
300
301 // get return value
302 aReturn = sbxToUnoValue( xReturn.get() );
303
304 // reset parameters
305 m_xMethod->SetParameters( nullptr );
306 }
307
308 return aReturn;
309 }
310
311
312} // namespace basprov
313
314
315/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
SbMethodRef xMethod
#define BASSCRIPT_PROPERTY_ID_CALLER
Definition: basscript.cxx:47
#define BASSCRIPT_DEFAULT_ATTRIBS()
Definition: basscript.cxx:50
void SetGlobalUNOConstant(const OUString &rName, const css::uno::Any &_rValue, css::uno::Any *pOldValue=nullptr)
bool Compile()
bool IsCompiled() const
SfxHintId GetId() const
void StartListening(SfxBroadcaster &rBroadcaster, DuplicateHandling eDuplicateHanding=DuplicateHandling::Unexpected)
void EndListening(SfxBroadcaster &rBroadcaster, bool bRemoveAllDuplicates=false)
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
Definition: basscript.cxx:125
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
Definition: basscript.cxx:145
BasicManager * m_documentBasicManager
Definition: basscript.hxx:54
css::uno::Reference< css::document::XScriptInvocationContext > m_xDocumentScriptContext
Definition: basscript.hxx:56
virtual ~BasicScriptImpl() override
Definition: basscript.cxx:83
css::uno::Sequence< css::uno::Any > m_caller
Definition: basscript.hxx:60
BasicScriptImpl(OUString funcName, SbMethodRef xMethod)
Definition: basscript.cxx:58
virtual css::uno::Any SAL_CALL invoke(const css::uno::Sequence< css::uno::Any > &aParams, css::uno::Sequence< sal_Int16 > &aOutParamIndex, css::uno::Sequence< css::uno::Any > &aOutParam) override
Definition: basscript.cxx:155
virtual ::cppu::IPropertyArrayHelper * createArrayHelper() const override
Definition: basscript.cxx:134
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: basscript.cxx:94
void describeProperties(css::uno::Sequence< css::beans::Property > &_rProps) const
void registerProperty(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, void *_pPointerToMember, const css::uno::Type &_rMemberType)
T * get() const
bool is() const
int nCount
float u
#define ERRCODE_NONE
std::mutex m_aMutex
void * p
sal_Int64 n
uno_Any a
::cppu::WeakImplHelper< css::script::provider::XScript > BasicScriptImpl_BASE
Definition: basscript.hxx:42
constexpr OUStringLiteral BASSCRIPT_PROPERTY_CALLER
Definition: basscript.cxx:48
::std::map< sal_Int16, Any > OutParamMap
Definition: basscript.cxx:52
int i
IMPLEMENT_FORWARD_XTYPEPROVIDER2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
IMPLEMENT_FORWARD_XINTERFACE2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
void unoToSbxValue(SbxVariable *pVar, const Any &aValue)
Any sbxToUnoValue(const SbxValue *pVar)
SbxDataType
SbxLONG
SbxULONG
SbxUSHORT
SbxVARIANT
SbxSINGLE
SbxBYREF
SbxINTEGER
SbxDOUBLE
SbxDataType eType