summaryrefslogtreecommitdiff
path: root/src/lib/corelib/tools/scripttools.h
blob: ea7993485e0d279f6b142b57eb17a8ec58ff5474 (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
/****************************************************************************
**
** 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_SCRIPTTOOLS_H
#define QBS_SCRIPTTOOLS_H

#include "codelocation.h"
#include "porting.h"
#include "qbs_export.h"

#include <quickjs.h>

#include <QtCore/qhash.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qvariant.h>

#include <utility>
#include <vector>

namespace qbs {
class ErrorInfo;
namespace Internal {
class ScriptEngine;

using JSValueList = std::vector<JSValue>;

void defineJsProperty(JSContext *ctx, JSValueConst obj, const QString &prop, JSValue val);
JSValue getJsProperty(JSContext *ctx, JSValueConst obj, const QString &prop);
void setJsProperty(JSContext *ctx, JSValueConst obj, const QString &prop, JSValue val);
void setJsProperty(JSContext *ctx, JSValueConst obj, const QString &prop, const QString &val);
QString getJsStringProperty(JSContext *ctx, JSValueConst obj, const QString &prop);
QStringList getJsStringListProperty(JSContext *ctx, JSValueConst obj, const QString &prop);
int getJsIntProperty(JSContext *ctx, JSValueConst obj, const QString &prop);
bool getJsBoolProperty(JSContext *ctx, JSValueConst obj, const QString &prop);
QVariant getJsVariantProperty(JSContext *ctx, JSValueConst obj, const QString &prop);
QString getJsString(JSContext *ctx, JSValueConst val);
QString getJsString(JSContext *ctx, JSAtom atom);
QBS_AUTOTEST_EXPORT QVariant getJsVariant(JSContext *ctx, JSValueConst val);
JSValue makeJsArrayBuffer(JSContext *ctx, const QByteArray &s);
JSValue makeJsString(JSContext *ctx, const QString &s);
JSValue makeJsStringList(JSContext *ctx, const QStringList &l);
JSValue makeJsVariant(JSContext *ctx, const QVariant &v);
JSValue makeJsVariantList(JSContext *ctx, const QVariantList &l);
JSValue makeJsVariantMap(JSContext *ctx, const QVariantMap &m);
QStringList getJsStringList(JSContext *ctx, JSValueConst val);
JSValue throwError(JSContext *ctx, const QString &message);
using PropertyHandler = std::function<void(const JSAtom &, const JSPropertyDescriptor &)>;
void handleJsProperties(JSContext *ctx, JSValueConst obj, const  PropertyHandler &handler);
inline quintptr jsObjectId(const JSValue &val) { return quintptr(JS_VALUE_GET_OBJ(val)); }

template <class T>
void attachPointerTo(JSValue &scriptValue, T *ptr)
{
    JS_SetOpaque(scriptValue, const_cast<std::remove_const_t<T> *>(ptr));
}

template <class T>
T *attachedPointer(const JSValue &scriptValue, JSClassID id)
{
    return reinterpret_cast<T *>(JS_GetOpaque(scriptValue, id));
}

class TemporaryGlobalObjectSetter
{
public:
    TemporaryGlobalObjectSetter(ScriptEngine *engine, const JSValue &object);
    ~TemporaryGlobalObjectSetter();

private:
    ScriptEngine * const m_engine;
    const JSValue m_oldGlobalObject;
};

class ScopedJsValue
{
public:
    ScopedJsValue(JSContext *ctx, JSValue v) : m_context(ctx), m_value(v) {}
    void setValue(JSValue v) { JS_FreeValue(m_context, m_value); m_value = v; }
    ~ScopedJsValue() { JS_FreeValue(m_context, m_value); }
    operator JSValue() const { return m_value; }
    JSValue release() { const JSValue v = m_value; m_value = JS_UNDEFINED; return v; }
    void reset(JSValue v) { JS_FreeValue(m_context, m_value); m_value = v; }

    ScopedJsValue(const ScopedJsValue &) = delete;
    ScopedJsValue &operator=(const ScopedJsValue &) = delete;

    ScopedJsValue(ScopedJsValue && other) : m_context(other.m_context), m_value(other.m_value)
    {
        other.m_value = JS_UNDEFINED;
    }

private:
    JSContext * const m_context;
    JSValue m_value;
};

class ScopedJsValueList
{
public:
    ScopedJsValueList(JSContext *ctx, const JSValueList &l) : m_context(ctx), m_list(l) {}
    ~ScopedJsValueList() { for (const JSValue v : m_list) JS_FreeValue(m_context, v); }
    operator JSValueList() const { return m_list; }

    ScopedJsValueList(const ScopedJsValueList &) = delete;
    ScopedJsValueList &operator=(const ScopedJsValueList &) = delete;

    ScopedJsValueList(ScopedJsValueList && other) noexcept
        : m_context(other.m_context), m_list(std::move(other.m_list))
    {
        other.m_list.clear();
    }

private:
    JSContext * const m_context;
    JSValueList m_list;
};

class ScopedJsAtom
{
public:
    ScopedJsAtom(JSContext *ctx, JSAtom a) : m_context(ctx), m_atom(a) {}
    ScopedJsAtom(JSContext *ctx, const QByteArray &s)
        : ScopedJsAtom(ctx, JS_NewAtom(ctx, s.constData())) {}
    ScopedJsAtom(JSContext *ctx, const QString &s) : ScopedJsAtom(ctx, s.toUtf8()) {}
    ~ScopedJsAtom() { JS_FreeAtom(m_context, m_atom); }
    operator JSAtom() const { return m_atom; }

    ScopedJsAtom(const ScopedJsAtom &) = delete;
    ScopedJsAtom&operator=(const ScopedJsAtom &) = delete;

    ScopedJsAtom(ScopedJsAtom &&other) : m_context(other.m_context), m_atom(other.m_atom)
    {
        other.m_atom = 0;
    }

private:
    JSContext * const m_context;
    JSAtom m_atom;
};

class QBS_AUTOTEST_EXPORT JsException
{
public:
    JsException(JSContext *ctx, JSValue ex, const CodeLocation &fallbackLocation)
        : m_ctx(ctx), m_exception(ex), m_fallbackLocation(fallbackLocation) {}
    JsException(JsException && other) noexcept;
    ~JsException();
    JsException(const JsException &) = delete;
    JsException &operator=(const JsException &) = delete;

    operator bool() const { return !JS_IsNull(m_exception); }
    QString message() const;
    const QStringList stackTrace() const;
    ErrorInfo toErrorInfo() const;
private:
    JSContext *m_ctx;
    JSValue m_exception;
    CodeLocation m_fallbackLocation;
};

void setConfigProperty(QVariantMap &cfg, const QStringList &name, const QVariant &value);
QVariant QBS_AUTOTEST_EXPORT getConfigProperty(const QVariantMap &cfg, const QStringList &name);

} // namespace Internal
} // namespace qbs

// Only to be used for objects!
inline bool operator==(JSValue v1, JSValue v2) { return v1.u.ptr == v2.u.ptr; }
inline bool operator!=(JSValue v1, JSValue v2) { return !(v1 == v2); }
inline bool operator<(JSValue v1, JSValue v2) { return v1.u.ptr < v2.u.ptr; }
inline qbs::QHashValueType qHash(const JSValue &v) { return QT_PREPEND_NAMESPACE(qHash)(v.u.ptr); }

#endif // QBS_SCRIPTTOOLS_H