aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4value_p.h
blob: f2d01c56cd24e1a8367fcc74af0e61a07d7bfa4c (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// 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
#ifndef QV4VALUE_P_H
#define QV4VALUE_P_H

//
//  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.
//

#include <limits.h>
#include <cmath>

#include <QtCore/QString>
#include "qv4global_p.h"
#include <private/qv4heap_p.h>
#include <private/qv4internalclass_p.h>
#include <private/qv4staticvalue_p.h>

#include <private/qnumeric_p.h>
#include <private/qv4calldata_p.h>

QT_BEGIN_NAMESPACE

namespace QV4 {

namespace Heap {
    struct Base;
}

struct Q_QML_EXPORT Value : public StaticValue
{
    using ManagedPtr = Managed *;

    Value() = default;
    constexpr Value(quint64 val) : StaticValue(val) {}

    static constexpr Value fromStaticValue(StaticValue staticValue)
    {
        return {staticValue._val};
    }

    inline bool isString() const;
    inline bool isStringOrSymbol() const;
    inline bool isSymbol() const;
    inline bool isObject() const;
    inline bool isFunctionObject() const;

    QML_NEARLY_ALWAYS_INLINE String *stringValue() const {
        if (!isString())
            return nullptr;
        return reinterpret_cast<String *>(const_cast<Value *>(this));
    }
    QML_NEARLY_ALWAYS_INLINE StringOrSymbol *stringOrSymbolValue() const {
        if (!isStringOrSymbol())
            return nullptr;
        return reinterpret_cast<StringOrSymbol *>(const_cast<Value *>(this));
    }
    QML_NEARLY_ALWAYS_INLINE Symbol *symbolValue() const {
        if (!isSymbol())
            return nullptr;
        return reinterpret_cast<Symbol *>(const_cast<Value *>(this));
    }
    QML_NEARLY_ALWAYS_INLINE Object *objectValue() const {
        if (!isObject())
            return nullptr;
        return reinterpret_cast<Object*>(const_cast<Value *>(this));
    }
    QML_NEARLY_ALWAYS_INLINE ManagedPtr managed() const {
        if (!isManaged())
            return nullptr;
        return reinterpret_cast<Managed*>(const_cast<Value *>(this));
    }
    QML_NEARLY_ALWAYS_INLINE Value::HeapBasePtr heapObject() const {
        return isManagedOrUndefined() ? m() : nullptr;
    }

    static inline Value fromHeapObject(HeapBasePtr m)
    {
        Value v;
        v.setM(m);
        return v;
    }

    int toUInt16() const;
    inline int toInt32() const;
    inline unsigned int toUInt32() const;
    qint64 toLength() const;
    inline qint64 toIndex() const;

    bool toBoolean() const {
        if (integerCompatible())
            return static_cast<bool>(int_32());

        return toBooleanImpl(*this);
    }
    static bool toBooleanImpl(Value val);
    double toInteger() const;
    inline ReturnedValue convertedToNumber() const;
    inline double toNumber() const;
    static double toNumberImpl(Value v);
    double toNumberImpl() const { return toNumberImpl(*this); }

    QString toQStringNoThrow() const;
    QString toQString() const;
    QString toQString(bool *ok) const;

    Heap::String *toString(ExecutionEngine *e) const {
        if (isString())
            return reinterpret_cast<Heap::String *>(m());
        return toString(e, *this);
    }
    QV4::PropertyKey toPropertyKey(ExecutionEngine *e) const;

    static Heap::String *toString(ExecutionEngine *e, Value val);
    Heap::Object *toObject(ExecutionEngine *e) const {
        if (isObject())
            return reinterpret_cast<Heap::Object *>(m());
        return toObject(e, *this);
    }
    static Heap::Object *toObject(ExecutionEngine *e, Value val);

    inline bool isPrimitive() const;

    template <typename T>
    const T *as() const {
        if (!isManaged())
            return nullptr;

        Q_ASSERT(m()->internalClass->vtable);
#if !defined(QT_NO_QOBJECT_CHECK)
        static_cast<const T *>(this)->qt_check_for_QMANAGED_macro(static_cast<const T *>(this));
#endif
        const VTable *vt = m()->internalClass->vtable;
        while (vt) {
            if (vt == T::staticVTable())
                return static_cast<const T *>(this);
            vt = vt->parent;
        }
        return nullptr;
    }
    template <typename T>
    T *as() {
        if (isManaged())
            return const_cast<T *>(const_cast<const Value *>(this)->as<T>());
        else
            return nullptr;
    }

    template<typename T> inline T *cast() {
        return static_cast<T *>(managed());
    }
    template<typename T> inline const T *cast() const {
        return static_cast<const T *>(managed());
    }

    uint asArrayLength(bool *ok) const;

    static constexpr Value fromReturnedValue(ReturnedValue val)
    {
        return fromStaticValue(StaticValue::fromReturnedValue(val));
    }

    // As per ES specs
    bool sameValue(Value other) const;
    bool sameValueZero(Value other) const;

    inline void mark(MarkStack *markStack);

    static double toInteger(double d) { return StaticValue::toInteger(d); }
    static int toInt32(double d) { return StaticValue::toInt32(d); }
    static unsigned int toUInt32(double d) { return StaticValue::toUInt32(d); }
    inline static constexpr Value emptyValue()
    {
        return fromStaticValue(StaticValue::emptyValue());
    }
    static inline constexpr Value fromBoolean(bool b)
    {
        return fromStaticValue(StaticValue::fromBoolean(b));
    }
    static inline constexpr Value fromInt32(int i)
    {
        return fromStaticValue(StaticValue::fromInt32(i));
    }
    inline static constexpr Value undefinedValue()
    {
        return fromStaticValue(StaticValue::undefinedValue());
    }
    static inline constexpr Value nullValue()
    {
        return fromStaticValue(StaticValue::nullValue());
    }
    static inline Value fromDouble(double d)
    {
        return fromStaticValue(StaticValue::fromDouble(d));
    }
    static inline Value fromUInt32(uint i)
    {
        return fromStaticValue(StaticValue::fromUInt32(i));
    }

    Value &operator =(const ScopedValue &v);
    Value &operator=(ReturnedValue v)
    {
        StaticValue::operator=(v);
        return *this;
    }
    Value &operator=(ManagedPtr m) {
        if (!m) {
            setM(nullptr);
        } else {
            _val = reinterpret_cast<Value *>(m)->_val;
        }
        return *this;
    }
    Value &operator=(HeapBasePtr o) {
        setM(o);
        return *this;
    }

    template<typename T>
    Value &operator=(const Scoped<T> &t);
};
Q_STATIC_ASSERT(std::is_trivial_v<Value>);
Q_STATIC_ASSERT(sizeof(Value) == sizeof(StaticValue));

template<>
inline StaticValue &StaticValue::operator=<Value>(const Value &value)
{
    _val = value._val;
    return *this;
}

template<typename Managed>
inline StaticValue &StaticValue::operator=(const Managed &m)
{
    *static_cast<Value *>(this) = m;
    return *this;
}

template<>
inline Value &StaticValue::asValue<Value>()
{
    return *static_cast<Value *>(this);
}

template<>
inline const Value &StaticValue::asValue<Value>() const
{
    return *static_cast<const Value *>(this);
}

template<>
inline Value *CallData::argValues<Value>()
{
    return static_cast<Value *>(static_cast<StaticValue *>(args));
}

template<>
inline const Value *CallData::argValues<Value>() const
{
    return static_cast<const Value *>(static_cast<const StaticValue *>(args));
}

template<typename HeapBase>
inline Encode::Encode(HeapBase *o)
{
    val = Value::fromHeapObject(o).asReturnedValue();
}

inline void Value::mark(MarkStack *markStack)
{
    HeapBasePtr o = heapObject();
    if (o)
        o->mark(markStack);
}

inline bool Value::isString() const
{
    HeapBasePtr b = heapObject();
    return b && b->internalClass->vtable->isString;
}

bool Value::isStringOrSymbol() const
{
    HeapBasePtr b = heapObject();
    return b && b->internalClass->vtable->isStringOrSymbol;
}

bool Value::isSymbol() const
{
    HeapBasePtr b = heapObject();
    return b && b->internalClass->vtable->isStringOrSymbol && !b->internalClass->vtable->isString;
}

inline bool Value::isObject() const

{
    HeapBasePtr b = heapObject();
    return b && b->internalClass->vtable->isObject;
}

inline bool Value::isFunctionObject() const
{
    HeapBasePtr b = heapObject();
    return b && b->internalClass->vtable->isFunctionObject;
}

inline bool Value::isPrimitive() const
{
    return !isObject();
}

inline double Value::toNumber() const
{
    if (isInteger())
        return int_32();
    if (isDouble())
        return doubleValue();
    return toNumberImpl();
}

inline ReturnedValue Value::convertedToNumber() const
{
    if (isInteger() || isDouble())
        return asReturnedValue();
    Value v;
    v.setDouble(toNumberImpl());
    return v.asReturnedValue();
}

inline
ReturnedValue Heap::Base::asReturnedValue() const
{
    return Value::fromHeapObject(const_cast<Value::HeapBasePtr>(this)).asReturnedValue();
}

// For source compat with older code in other modules
using Primitive = Value;

template<typename T>
ReturnedValue value_convert(ExecutionEngine *e, const Value &v);

inline int Value::toInt32() const
{
    if (Q_LIKELY(integerCompatible()))
        return int_32();

    if (Q_LIKELY(isDouble()))
        return QJSNumberCoercion::toInteger(doubleValue());

    return QJSNumberCoercion::toInteger(toNumberImpl());
}

inline unsigned int Value::toUInt32() const
{
    return static_cast<unsigned int>(toInt32());
}

inline qint64 Value::toLength() const
{
    if (Q_LIKELY(integerCompatible()))
        return int_32() < 0 ? 0 : int_32();
    double i = Value::toInteger(isDouble() ? doubleValue() : toNumberImpl());
    if (i <= 0)
        return 0;
    if (i > (static_cast<qint64>(1) << 53) - 1)
        return (static_cast<qint64>(1) << 53) - 1;
    return static_cast<qint64>(i);
}

inline qint64 Value::toIndex() const
{
    qint64 idx;
    if (Q_LIKELY(integerCompatible())) {
        idx = int_32();
    } else {
        idx = static_cast<qint64>(Value::toInteger(isDouble() ? doubleValue() : toNumberImpl()));
    }
    if (idx > (static_cast<qint64>(1) << 53) - 1)
        idx = -1;
    return idx;
}

inline double Value::toInteger() const
{
    if (integerCompatible())
        return int_32();

    return Value::toInteger(isDouble() ? doubleValue() : toNumberImpl());
}


template <size_t o>
struct HeapValue : Value {
    static constexpr size_t offset = o;
    HeapBasePtr base() {
        HeapBasePtr base = reinterpret_cast<HeapBasePtr>(this) - (offset/sizeof(Heap::Base));
        Q_ASSERT(base->inUse());
        return base;
    }

    void set(EngineBase *e, const Value &newVal) {
        WriteBarrier::write(e, base(), data_ptr(), newVal.asReturnedValue());
    }
    void set(EngineBase *e, HeapBasePtr b) {
        WriteBarrier::write(e, base(), data_ptr(), b->asReturnedValue());
    }
};

template <size_t o>
struct ValueArray {
    static constexpr size_t offset = o;
    uint size;
    uint alloc;
    Value values[1];

    Value::HeapBasePtr base() {
        Value::HeapBasePtr base = reinterpret_cast<Value::HeapBasePtr>(this)
                - (offset/sizeof(Heap::Base));
        Q_ASSERT(base->inUse());
        return base;
    }

    void set(EngineBase *e, uint index, Value v) {
        WriteBarrier::write(e, base(), values[index].data_ptr(), v.asReturnedValue());
    }
    void set(EngineBase *e, uint index, Value::HeapBasePtr b) {
        WriteBarrier::write(e, base(), values[index].data_ptr(), Value::fromHeapObject(b).asReturnedValue());
    }
    inline const Value &operator[] (uint index) const {
        Q_ASSERT(index < alloc);
        return values[index];
    }
    inline const Value *data() const {
        return values;
    }

    void mark(MarkStack *markStack) {
        for (Value *v = values, *end = values + alloc; v < end; ++v)
            v->mark(markStack);
    }
};

// It's really important that the offset of values in this structure is
// constant across all architecture,  otherwise JIT cross-compiled code will
// have wrong offsets between host and target.
Q_STATIC_ASSERT(offsetof(ValueArray<0>, values) == 8);

class OptionalReturnedValue {
    ReturnedValue value;
public:

    OptionalReturnedValue() : value(Value::emptyValue().asReturnedValue()) {}
    explicit OptionalReturnedValue(ReturnedValue v)
        : value(v)
    {
        Q_ASSERT(!Value::fromReturnedValue(v).isEmpty());
    }

    ReturnedValue operator->() const { return value; }
    ReturnedValue operator*() const { return value; }
    explicit operator bool() const { return !Value::fromReturnedValue(value).isEmpty(); }
};

}

QT_END_NAMESPACE

#endif // QV4VALUE_DEF_P_H