aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/language/scriptengine.h
blob: bfe238b09e56763c18efdb8b9e0a364242a4b38b (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QBS_SCRIPTENGINE_H
#define QBS_SCRIPTENGINE_H

#include "forward_decls.h"
#include "property.h"
#include <buildgraph/requestedartifacts.h>
#include <buildgraph/requesteddependencies.h>
#include <logging/logger.h>
#include <tools/codelocation.h>
#include <tools/filetime.h>
#include <tools/set.h>

#include <QtCore/qdir.h>
#include <QtCore/qhash.h>
#include <QtCore/qlist.h>
#include <QtCore/qprocess.h>
#include <QtCore/qstring.h>

#include <QtScript/qscriptengine.h>

#include <memory>
#include <mutex>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <vector>

namespace qbs {
namespace Internal {
class Artifact;
class JsImport;
class PrepareScriptObserver;
class ScriptImporter;
class ScriptPropertyObserver;

enum class EvalContext { PropertyEvaluation, ProbeExecution, RuleExecution, JsCommand };
class DubiousContext
{
public:
    enum Suggestion { NoSuggestion, SuggestMoving };
    DubiousContext(EvalContext c, Suggestion s = NoSuggestion) : context(c), suggestion(s) { }
    EvalContext context;
    Suggestion suggestion;
};
using DubiousContextList = std::vector<DubiousContext>;


/*
 * ScriptObject that acquires resources, for example a file handle.
 * The ScriptObject should have QtOwnership and deleteLater() itself in releaseResources.
 */
class ResourceAcquiringScriptObject
{
public:
    virtual void releaseResources() = 0;
};

enum class ObserveMode { Enabled, Disabled };

class QBS_AUTOTEST_EXPORT ScriptEngine : public QScriptEngine
{
    Q_OBJECT
    ScriptEngine(Logger &logger, EvalContext evalContext, QObject *parent = nullptr);
public:
    static ScriptEngine *create(Logger &logger, EvalContext evalContext, QObject *parent = nullptr);
    ~ScriptEngine();

    Logger &logger() const { return m_logger; }
    void import(const FileContextBaseConstPtr &fileCtx, QScriptValue &targetObject,
                ObserveMode observeMode);
    void clearImportsCache();

    void setEvalContext(EvalContext c) { m_evalContext = c; }
    EvalContext evalContext() const { return m_evalContext; }
    void checkContext(const QString &operation, const DubiousContextList &dubiousContexts);

    void addPropertyRequestedInScript(const Property &property) {
        m_propertiesRequestedInScript += property;
    }
    void addDependenciesArrayRequested(const ResolvedProduct *p)
    {
        m_productsWithRequestedDependencies.insert(p);
    }
    void setArtifactsMapRequested(const ResolvedProduct *product)
    {
        m_requestedArtifacts.setAllArtifactTags(product);
    }
    void setArtifactSetRequestedForTag(const ResolvedProduct *product, const FileTag &tag)
    {
        m_requestedArtifacts.setArtifactsForTag(product, tag);
    }
    void setNonExistingArtifactSetRequested(const ResolvedProduct *product, const QString &tag)
    {
        m_requestedArtifacts.setNonExistingTagRequested(product, tag);
    }
    void setArtifactsEnumerated(const ResolvedProduct *product)
    {
        m_requestedArtifacts.setArtifactsEnumerated(product);
    }
    void addPropertyRequestedFromArtifact(const Artifact *artifact, const Property &property);
    void addRequestedExport(const ResolvedProduct *product) { m_requestedExports.insert(product); }
    void clearRequestedProperties() {
        m_propertiesRequestedInScript.clear();
        m_propertiesRequestedFromArtifact.clear();
        m_importsRequestedInScript.clear();
        m_productsWithRequestedDependencies.clear();
        m_requestedArtifacts.clear();
        m_requestedExports.clear();
    }
    PropertySet propertiesRequestedInScript() const { return m_propertiesRequestedInScript; }
    QHash<QString, PropertySet> propertiesRequestedFromArtifact() const {
        return m_propertiesRequestedFromArtifact;
    }
    Set<const ResolvedProduct *> productsWithRequestedDependencies() const
    {
        return m_productsWithRequestedDependencies;
    }
    RequestedDependencies requestedDependencies() const
    {
        return RequestedDependencies(m_productsWithRequestedDependencies);
    }
    RequestedArtifacts requestedArtifacts() const { return m_requestedArtifacts; }
    Set<const ResolvedProduct *> requestedExports() const { return m_requestedExports; }

    void addImportRequestedInScript(qint64 importValueId);
    std::vector<QString> importedFilesUsedInScript() const;

    void enableProfiling(bool enable);

    void setPropertyCacheEnabled(bool enable) { m_propertyCacheEnabled = enable; }
    bool isPropertyCacheEnabled() const { return m_propertyCacheEnabled; }
    void addToPropertyCache(const QString &moduleName, const QString &propertyName,
                            const PropertyMapConstPtr &propertyMap, const QVariant &value);
    QVariant retrieveFromPropertyCache(const QString &moduleName, const QString &propertyName,
                                       const PropertyMapConstPtr &propertyMap);

    void defineProperty(QScriptValue &object, const QString &name, const QScriptValue &descriptor);
    void setObservedProperty(QScriptValue &object, const QString &name, const QScriptValue &value);
    void unobserveProperties();
    void setDeprecatedProperty(QScriptValue &object, const QString &name, const QString &newName,
            const QScriptValue &value);
    PrepareScriptObserver *observer() const { return m_observer.get(); }

    QProcessEnvironment environment() const;
    void setEnvironment(const QProcessEnvironment &env);
    void addCanonicalFilePathResult(const QString &filePath, const QString &resultFilePath);
    void addFileExistsResult(const QString &filePath, bool exists);
    void addDirectoryEntriesResult(const QString &path, QDir::Filters filters,
                                   const QStringList &entries);
    void addFileLastModifiedResult(const QString &filePath, const FileTime &fileTime);
    QHash<QString, QString> canonicalFilePathResults() const { return m_canonicalFilePathResult; }
    QHash<QString, bool> fileExistsResults() const { return m_fileExistsResult; }
    QHash<std::pair<QString, quint32>, QStringList> directoryEntriesResults() const
    {
        return m_directoryEntriesResult;
    }

    QHash<QString, FileTime> fileLastModifiedResults() const { return m_fileLastModifiedResult; }
    Set<QString> imports() const;
    static QScriptValueList argumentList(const QStringList &argumentNames,
            const QScriptValue &context);

    QStringList uncaughtExceptionBacktraceOrEmpty() const {
        return hasUncaughtException() ? uncaughtExceptionBacktrace() : QStringList();
    }
    bool hasErrorOrException(const QScriptValue &v) const {
        return v.isError() || hasUncaughtException();
    }
    QScriptValue lastErrorValue(const QScriptValue &v) const {
        return v.isError() ? v : uncaughtException();
    }
    QString lastErrorString(const QScriptValue &v) const { return lastErrorValue(v).toString(); }
    CodeLocation lastErrorLocation(const QScriptValue &v,
                                   const CodeLocation &fallbackLocation = CodeLocation()) const;
    ErrorInfo lastError(const QScriptValue &v,
                        const CodeLocation &fallbackLocation = CodeLocation()) const;

    void cancel();

    // The active flag is different from QScriptEngine::isEvaluating.
    // It is set and cleared externally for example by the rule execution code.
    bool isActive() const { return m_active; }
    void setActive(bool on) { m_active = on; }

    using QScriptEngine::newFunction;

    template <typename T, typename E,
              typename = std::enable_if_t<std::is_pointer<T>::value>,
              typename = std::enable_if_t<std::is_pointer<E>::value>,
              typename = std::enable_if_t<std::is_base_of<
                QScriptEngine, std::remove_pointer_t<E>>::value>
              > QScriptValue newFunction(QScriptValue (*signature)(QScriptContext *, E, T), T arg) {
        return QScriptEngine::newFunction(
                    reinterpret_cast<FunctionWithArgSignature>(signature),
                    reinterpret_cast<void *>(const_cast<
                                             std::add_pointer_t<
                                             std::remove_const_t<
                                             std::remove_pointer_t<T>>>>(arg)));
    }

    QScriptClass *modulePropertyScriptClass() const;
    void setModulePropertyScriptClass(QScriptClass *modulePropertyScriptClass);

    QScriptClass *productPropertyScriptClass() const { return m_productPropertyScriptClass; }
    void setProductPropertyScriptClass(QScriptClass *productPropertyScriptClass)
    {
        m_productPropertyScriptClass = productPropertyScriptClass;
    }

    QScriptClass *artifactsScriptClass() const { return m_artifactsScriptClass; }
    void setArtifactsScriptClass(QScriptClass *artifactsScriptClass)
    {
        m_artifactsScriptClass = artifactsScriptClass;
    }

    void addResourceAcquiringScriptObject(ResourceAcquiringScriptObject *obj);
    void releaseResourcesOfScriptObjects();

    QScriptValue &productScriptValuePrototype(const ResolvedProduct *product)
    {
        return m_productScriptValues[product];
    }

    QScriptValue &projectScriptValue(const ResolvedProject *project)
    {
        return m_projectScriptValues[project];
    }

    QScriptValue &moduleScriptValuePrototype(const ResolvedModule *module)
    {
        return m_moduleScriptValues[module];
    }

private:
    QScriptValue newFunction(FunctionWithArgSignature signature, void *arg) Q_DECL_EQ_DELETE;

    void abort();

    void installQbsBuiltins();
    void extendJavaScriptBuiltins();
    void installFunction(const QString &name, int length, QScriptValue *functionValue,
                         FunctionSignature f, QScriptValue *targetObject);
    void installQbsFunction(const QString &name, int length, FunctionSignature f);
    void installConsoleFunction(const QString &name,
                                QScriptValue (*f)(QScriptContext *, QScriptEngine *, Logger *));
    void installImportFunctions();
    void uninstallImportFunctions();
    void import(const JsImport &jsImport, QScriptValue &targetObject);
    void observeImport(QScriptValue &jsImport);
    void importFile(const QString &filePath, QScriptValue &targetObject);
    static QScriptValue js_loadExtension(QScriptContext *context, QScriptEngine *qtengine);
    static QScriptValue js_loadFile(QScriptContext *context, QScriptEngine *qtengine);
    static QScriptValue js_require(QScriptContext *context, QScriptEngine *qtengine);

    class PropertyCacheKey
    {
    public:
        PropertyCacheKey(const QString &moduleName, const QString &propertyName,
                         const PropertyMapConstPtr &propertyMap);
    private:
        const QString m_moduleName;
        const QString m_propertyName;
        const PropertyMapConstPtr m_propertyMap;

        friend bool operator==(const PropertyCacheKey &lhs, const PropertyCacheKey &rhs);
        friend uint qHash(const ScriptEngine::PropertyCacheKey &k, uint seed);
    };

    friend bool operator==(const PropertyCacheKey &lhs, const PropertyCacheKey &rhs);
    friend uint qHash(const ScriptEngine::PropertyCacheKey &k, uint seed);

    static std::mutex m_creationDestructionMutex;
    ScriptImporter *m_scriptImporter;
    QScriptClass *m_modulePropertyScriptClass;
    QScriptClass *m_productPropertyScriptClass = nullptr;
    QScriptClass *m_artifactsScriptClass = nullptr;
    QHash<JsImport, QScriptValue> m_jsImportCache;
    bool m_propertyCacheEnabled;
    bool m_active;
    QHash<PropertyCacheKey, QVariant> m_propertyCache;
    PropertySet m_propertiesRequestedInScript;
    QHash<QString, PropertySet> m_propertiesRequestedFromArtifact;
    Logger &m_logger;
    QScriptValue m_definePropertyFunction;
    QScriptValue m_emptyFunction;
    QProcessEnvironment m_environment;
    QHash<QString, QString> m_canonicalFilePathResult;
    QHash<QString, bool> m_fileExistsResult;
    QHash<std::pair<QString, quint32>, QStringList> m_directoryEntriesResult;
    QHash<QString, FileTime> m_fileLastModifiedResult;
    std::stack<QString> m_currentDirPathStack;
    std::stack<QStringList> m_extensionSearchPathsStack;
    QScriptValue m_loadFileFunction;
    QScriptValue m_loadExtensionFunction;
    QScriptValue m_requireFunction;
    QScriptValue m_qbsObject;
    QScriptValue m_consoleObject;
    QScriptValue m_cancelationError;
    qint64 m_elapsedTimeImporting = -1;
    EvalContext m_evalContext;
    std::vector<ResourceAcquiringScriptObject *> m_resourceAcquiringScriptObjects;
    const std::unique_ptr<PrepareScriptObserver> m_observer;
    std::vector<std::tuple<QScriptValue, QString, QScriptValue>> m_observedProperties;
    std::vector<QScriptValue> m_requireResults;
    std::unordered_map<qint64, std::vector<QString>> m_filePathsPerImport;
    std::vector<qint64> m_importsRequestedInScript;
    Set<const ResolvedProduct *> m_productsWithRequestedDependencies;
    RequestedArtifacts m_requestedArtifacts;
    Set<const ResolvedProduct *> m_requestedExports;
    ObserveMode m_observeMode = ObserveMode::Disabled;
    std::unordered_map<const ResolvedProduct *, QScriptValue> m_productScriptValues;
    std::unordered_map<const ResolvedProject *, QScriptValue> m_projectScriptValues;
    std::unordered_map<const ResolvedModule *, QScriptValue> m_moduleScriptValues;
};

class EvalContextSwitcher
{
public:
    EvalContextSwitcher(ScriptEngine *engine, EvalContext newContext)
        : m_engine(engine), m_oldContext(engine->evalContext())
    {
        engine->setEvalContext(newContext);
    }

    ~EvalContextSwitcher() { m_engine->setEvalContext(m_oldContext); }

private:
    ScriptEngine * const m_engine;
    const EvalContext m_oldContext;
};

} // namespace Internal
} // namespace qbs

#endif // QBS_SCRIPTENGINE_H