aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsapi/qjsvalue_p.h
blob: 4624652c93623eab4dceb298f644abd8291a1a92 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#ifndef QJSVALUE_P_H
#define QJSVALUE_P_H

#include <qjsvalue.h>
#include <private/qtqmlglobal_p.h>
#include <private/qv4value_p.h>
#include <private/qv4string_p.h>
#include <private/qv4engine_p.h>
#include <private/qv4mm_p.h>
#include <private/qv4persistent_p.h>

#include <QtCore/qthread.h>

QT_BEGIN_NAMESPACE

class QJSValuePrivate
{
    static constexpr quint64 s_tagBits = 3; // 3 bits mask
    static constexpr quint64 s_tagMask = (1 << s_tagBits) - 1;

    static constexpr quint64 s_pointerBit = 0x1;

public:
    enum class Kind {
        Undefined   = 0x0,
        Null        = 0x2,
        IntValue    = 0x4,
        BoolValue   = 0x6,
        DoublePtr   = 0x0 | s_pointerBit,
        QV4ValuePtr = 0x2 | s_pointerBit,
        QStringPtr  = 0x4 | s_pointerBit,
    };

    static_assert(quint64(Kind::Undefined)   <= s_tagMask);
    static_assert(quint64(Kind::Null)        <= s_tagMask);
    static_assert(quint64(Kind::IntValue)    <= s_tagMask);
    static_assert(quint64(Kind::BoolValue)   <= s_tagMask);
    static_assert(quint64(Kind::DoublePtr)   <= s_tagMask);
    static_assert(quint64(Kind::QV4ValuePtr) <= s_tagMask);
    static_assert(quint64(Kind::QStringPtr)  <= s_tagMask);

    static Kind tag(quint64 raw) { return Kind(raw & s_tagMask); }

#if QT_POINTER_SIZE == 4
    static void *pointer(quint64 raw)
    {
        Q_ASSERT(quint64(tag(raw)) & s_pointerBit);
        return reinterpret_cast<void *>(raw >> 32);
    }

    static quint64 encodePointer(void *pointer, Kind tag)
    {
        Q_ASSERT(quint64(tag) & s_pointerBit);
        return (quint64(quintptr(pointer)) << 32) | quint64(tag);
    }
#else
    static constexpr quint64 s_minAlignment = 1 << s_tagBits;
    static_assert(alignof(double)     >= s_minAlignment);
    static_assert(alignof(QV4::Value) >= s_minAlignment);
    static_assert(alignof(QString)    >= s_minAlignment);

    static void *pointer(quint64 raw)
    {
        Q_ASSERT(quint64(tag(raw)) & s_pointerBit);
        return reinterpret_cast<void *>(raw & ~s_tagMask);
    }

    static quint64 encodePointer(void *pointer, Kind tag)
    {
        Q_ASSERT(quint64(tag) & s_pointerBit);
        return quintptr(pointer) | quint64(tag);
    }
#endif

    static quint64 encodeUndefined()
    {
        return quint64(Kind::Undefined);
    }

    static quint64 encodeNull()
    {
        return quint64(Kind::Null);
    }

    static int intValue(quint64 v)
    {
        Q_ASSERT(tag(v) == Kind::IntValue);
        return v >> 32;
    }

    static quint64 encode(int intValue)
    {
        return (quint64(intValue) << 32) | quint64(Kind::IntValue);
    }

    static quint64 encode(uint uintValue)
    {
        return (uintValue < uint(std::numeric_limits<int>::max()))
                ? encode(int(uintValue))
                : encode(double(uintValue));
    }

    static bool boolValue(quint64 v)
    {
        Q_ASSERT(tag(v) == Kind::BoolValue);
        return v >> 32;
    }

    static quint64 encode(bool boolValue)
    {
        return (quint64(boolValue) << 32) | quint64(Kind::BoolValue);
    }

    static double *doublePtr(quint64 v)
    {
        Q_ASSERT(tag(v) == Kind::DoublePtr);
        return static_cast<double *>(pointer(v));
    }

    static quint64 encode(double doubleValue)
    {
        return encodePointer(new double(doubleValue), Kind::DoublePtr);
    }

    static QV4::Value *qv4ValuePtr(quint64 v)
    {
        Q_ASSERT(tag(v) == Kind::QV4ValuePtr);
        return static_cast<QV4::Value *>(pointer(v));
    }

    static quint64 encode(const QV4::Value &qv4Value)
    {
        switch (qv4Value.type()) {
        case QV4::StaticValue::Boolean_Type:
            return encode(qv4Value.booleanValue());
        case QV4::StaticValue::Integer_Type:
            return encode(qv4Value.integerValue());
        case QV4::StaticValue::Managed_Type: {
            auto managed = qv4Value.as<QV4::Managed>();
            auto engine = managed->engine();
            auto mm = engine->memoryManager;
            QV4::Value *m = mm->m_persistentValues->allocate();
            Q_ASSERT(m);
            // we create a new strong reference to the heap managed object
            // to avoid having to rescan the persistent values, we mark it here
            QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack){
                if constexpr (QV4::WriteBarrier::isInsertionBarrier)
                    managed->heapObject()->mark(stack);
            });
            *m = qv4Value;
            return encodePointer(m, Kind::QV4ValuePtr);
        }
        case QV4::StaticValue::Double_Type:
            return encode(qv4Value.doubleValue());
        case QV4::StaticValue::Null_Type:
            return encodeNull();
        case QV4::StaticValue::Empty_Type:
            Q_UNREACHABLE();
            break;
        case QV4::StaticValue::Undefined_Type:
            break;
        }

        return encodeUndefined();
    }

    static QString *qStringPtr(quint64 v)
    {
        Q_ASSERT(tag(v) == Kind::QStringPtr);
        return static_cast<QString *>(pointer(v));
    }

    static quint64 encode(QString stringValue)
    {
        return encodePointer(new QString(std::move(stringValue)), Kind::QStringPtr);
    }

    static quint64 encode(QLatin1String stringValue)
    {
        return encodePointer(new QString(std::move(stringValue)), Kind::QStringPtr);
    }

    static QJSValue fromReturnedValue(QV4::ReturnedValue d)
    {
        QJSValue result;
        setValue(&result, d);
        return result;
    }

    template<typename T>
    static const T *asManagedType(const QJSValue *jsval)
    {
        if (tag(jsval->d) == Kind::QV4ValuePtr) {
            if (const QV4::Value *value = qv4ValuePtr(jsval->d))
                return value->as<T>();
        }
        return nullptr;
    }

    // This is a move operation and transfers ownership.
    static QV4::Value *takeManagedValue(QJSValue *jsval)
    {
        if (tag(jsval->d) == Kind::QV4ValuePtr) {
            if (QV4::Value *value = qv4ValuePtr(jsval->d)) {
                jsval->d = encodeUndefined();
                return value;
            }
        }
        return nullptr;
    }

    static QV4::ReturnedValue asPrimitiveType(const QJSValue *jsval)
    {
        switch (tag(jsval->d)) {
        case Kind::BoolValue:
            return QV4::Encode(boolValue(jsval->d));
        case Kind::IntValue:
            return QV4::Encode(intValue(jsval->d));
        case Kind::DoublePtr:
            return QV4::Encode(*doublePtr(jsval->d));
        case Kind::Null:
            return QV4::Encode::null();
        case Kind::Undefined:
        case Kind::QV4ValuePtr:
        case Kind::QStringPtr:
            break;
        }

        return QV4::Encode::undefined();
    }

    // Beware: This only returns a non-null string if the QJSValue actually holds one.
    //         QV4::Strings are kept as managed values. Retrieve those with getValue().
    static const QString *asQString(const QJSValue *jsval)
    {
        if (tag(jsval->d) == Kind::QStringPtr) {
            if (const QString *string = qStringPtr(jsval->d))
                return string;
        }
        return nullptr;
    }

    static QV4::ReturnedValue asReturnedValue(const QJSValue *jsval)
    {
        switch (tag(jsval->d)) {
        case Kind::BoolValue:
            return QV4::Encode(boolValue(jsval->d));
        case Kind::IntValue:
            return QV4::Encode(intValue(jsval->d));
        case Kind::DoublePtr:
            return QV4::Encode(*doublePtr(jsval->d));
        case Kind::Null:
            return QV4::Encode::null();
        case Kind::QV4ValuePtr:
            return qv4ValuePtr(jsval->d)->asReturnedValue();
        case Kind::Undefined:
        case Kind::QStringPtr:
            break;
        }

        return QV4::Encode::undefined();
    }

    static void setString(QJSValue *jsval, QString s)
    {
        jsval->d = encode(std::move(s));
    }

    // Only use this with an existing persistent value.
    // Ownership is transferred to the QJSValue.
    static void adoptPersistentValue(QJSValue *jsval, QV4::Value *v)
    {
        jsval->d = encodePointer(v, Kind::QV4ValuePtr);
    }

    static void setValue(QJSValue *jsval, const QV4::Value &v)
    {
        jsval->d = encode(v);
    }

    // Moves any QString onto the V4 heap, changing the value to reflect that.
    static void manageStringOnV4Heap(QV4::ExecutionEngine *e, QJSValue *jsval)
    {
        if (const QString *string = asQString(jsval)) {
            jsval->d = encode(QV4::Value::fromHeapObject(e->newString(*string)));
            delete string;
        }
    }

    // Converts any QString on the fly, involving an allocation.
    // Does not change the value.
    static QV4::ReturnedValue convertToReturnedValue(QV4::ExecutionEngine *e,
                                                     const QJSValue &jsval)
    {
        if (const QString *string = asQString(&jsval))
            return e->newString(*string)->asReturnedValue();
        if (const QV4::Value *val = asManagedType<QV4::Managed>(&jsval)) {
            if (QV4::PersistentValueStorage::getEngine(val) == e)
                return val->asReturnedValue();

            qWarning("JSValue can't be reassigned to another engine.");
            return QV4::Encode::undefined();
        }
        return asPrimitiveType(&jsval);
    }

    static QV4::ExecutionEngine *engine(const QJSValue *jsval)
    {
        if (tag(jsval->d) == Kind::QV4ValuePtr) {
            if (const QV4::Value *value = qv4ValuePtr(jsval->d))
                return QV4::PersistentValueStorage::getEngine(value);
        }

        return nullptr;
    }

    static bool checkEngine(QV4::ExecutionEngine *e, const QJSValue &jsval)
    {
        QV4::ExecutionEngine *v4 = engine(&jsval);
        return !v4 || v4 == e;
    }

    static void free(QJSValue *jsval)
    {
        switch (tag(jsval->d)) {
        case Kind::Undefined:
        case Kind::Null:
        case Kind::IntValue:
        case Kind::BoolValue:
            return;
        case Kind::DoublePtr:
            delete doublePtr(jsval->d);
            return;
        case Kind::QStringPtr:
            delete qStringPtr(jsval->d);
            return;
        case Kind::QV4ValuePtr:
            break;
        }

        // We need a mutable value for free(). It needs to write to the actual memory.
        QV4::Value *m = qv4ValuePtr(jsval->d);
        Q_ASSERT(m); // Otherwise it would have been undefined above.
        if (QV4::ExecutionEngine *e = QV4::PersistentValueStorage::getEngine(m)) {
            if (QJSEngine *jsEngine = e->jsEngine()) {
                if (jsEngine->thread() != QThread::currentThread()) {
                    QMetaObject::invokeMethod(
                            jsEngine, [m](){ QV4::PersistentValueStorage::free(m); });
                    return;
                }
            }
        }
        QV4::PersistentValueStorage::free(m);
    }
};

QT_END_NAMESPACE

#endif