1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | #ifndef QV4VALUE_P_H |
4 | #define QV4VALUE_P_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <limits.h> |
18 | #include <cmath> |
19 | |
20 | #include <QtCore/QString> |
21 | #include "qv4global_p.h" |
22 | #include <private/qv4heap_p.h> |
23 | #include <private/qv4internalclass_p.h> |
24 | #include <private/qv4staticvalue_p.h> |
25 | |
26 | #include <private/qnumeric_p.h> |
27 | #include <private/qv4calldata_p.h> |
28 | |
29 | QT_BEGIN_NAMESPACE |
30 | |
31 | namespace QV4 { |
32 | |
33 | namespace Heap { |
34 | struct Base; |
35 | } |
36 | |
37 | struct Q_QML_EXPORT Value : public StaticValue |
38 | { |
39 | using ManagedPtr = Managed *; |
40 | |
41 | Value() = default; |
42 | constexpr Value(quint64 val) : StaticValue(val) {} |
43 | |
44 | static constexpr Value fromStaticValue(StaticValue staticValue) |
45 | { |
46 | return {staticValue._val}; |
47 | } |
48 | |
49 | inline bool isString() const; |
50 | inline bool isStringOrSymbol() const; |
51 | inline bool isSymbol() const; |
52 | inline bool isObject() const; |
53 | inline bool isFunctionObject() const; |
54 | |
55 | QML_NEARLY_ALWAYS_INLINE String *stringValue() const { |
56 | if (!isString()) |
57 | return nullptr; |
58 | return reinterpret_cast<String *>(const_cast<Value *>(this)); |
59 | } |
60 | QML_NEARLY_ALWAYS_INLINE StringOrSymbol *stringOrSymbolValue() const { |
61 | if (!isStringOrSymbol()) |
62 | return nullptr; |
63 | return reinterpret_cast<StringOrSymbol *>(const_cast<Value *>(this)); |
64 | } |
65 | QML_NEARLY_ALWAYS_INLINE Symbol *symbolValue() const { |
66 | if (!isSymbol()) |
67 | return nullptr; |
68 | return reinterpret_cast<Symbol *>(const_cast<Value *>(this)); |
69 | } |
70 | QML_NEARLY_ALWAYS_INLINE Object *objectValue() const { |
71 | if (!isObject()) |
72 | return nullptr; |
73 | return reinterpret_cast<Object*>(const_cast<Value *>(this)); |
74 | } |
75 | QML_NEARLY_ALWAYS_INLINE ManagedPtr managed() const { |
76 | if (!isManaged()) |
77 | return nullptr; |
78 | return reinterpret_cast<Managed*>(const_cast<Value *>(this)); |
79 | } |
80 | QML_NEARLY_ALWAYS_INLINE Value::HeapBasePtr heapObject() const { |
81 | return isManagedOrUndefined() ? m() : nullptr; |
82 | } |
83 | |
84 | static inline Value fromHeapObject(HeapBasePtr m) |
85 | { |
86 | Value v; |
87 | v.setM(m); |
88 | return v; |
89 | } |
90 | |
91 | int toUInt16() const; |
92 | inline int toInt32() const; |
93 | inline unsigned int toUInt32() const; |
94 | qint64 toLength() const; |
95 | inline qint64 toIndex() const; |
96 | |
97 | bool toBoolean() const { |
98 | if (integerCompatible()) |
99 | return static_cast<bool>(int_32()); |
100 | |
101 | return toBooleanImpl(val: *this); |
102 | } |
103 | static bool toBooleanImpl(Value val); |
104 | double toInteger() const; |
105 | inline ReturnedValue convertedToNumber() const; |
106 | inline double toNumber() const; |
107 | static double toNumberImpl(Value v); |
108 | double toNumberImpl() const { return toNumberImpl(v: *this); } |
109 | |
110 | QString toQStringNoThrow() const; |
111 | QString toQString() const; |
112 | QString toQString(bool *ok) const; |
113 | |
114 | Heap::String *toString(ExecutionEngine *e) const { |
115 | if (isString()) |
116 | return reinterpret_cast<Heap::String *>(m()); |
117 | return toString(e, val: *this); |
118 | } |
119 | QV4::PropertyKey toPropertyKey(ExecutionEngine *e) const; |
120 | |
121 | static Heap::String *toString(ExecutionEngine *e, Value val); |
122 | Heap::Object *toObject(ExecutionEngine *e) const { |
123 | if (isObject()) |
124 | return reinterpret_cast<Heap::Object *>(m()); |
125 | return toObject(e, val: *this); |
126 | } |
127 | static Heap::Object *toObject(ExecutionEngine *e, Value val); |
128 | |
129 | inline bool isPrimitive() const; |
130 | |
131 | template <typename T> |
132 | const T *as() const { |
133 | if (!isManaged()) |
134 | return nullptr; |
135 | |
136 | Q_ASSERT(m()->internalClass->vtable); |
137 | #if !defined(QT_NO_QOBJECT_CHECK) |
138 | static_cast<const T *>(this)->qt_check_for_QMANAGED_macro(static_cast<const T *>(this)); |
139 | #endif |
140 | const VTable *vt = m()->internalClass->vtable; |
141 | while (vt) { |
142 | if (vt == T::staticVTable()) |
143 | return static_cast<const T *>(this); |
144 | vt = vt->parent; |
145 | } |
146 | return nullptr; |
147 | } |
148 | template <typename T> |
149 | T *as() { |
150 | if (isManaged()) |
151 | return const_cast<T *>(const_cast<const Value *>(this)->as<T>()); |
152 | else |
153 | return nullptr; |
154 | } |
155 | |
156 | template<typename T> inline T *cast() { |
157 | return static_cast<T *>(managed()); |
158 | } |
159 | template<typename T> inline const T *cast() const { |
160 | return static_cast<const T *>(managed()); |
161 | } |
162 | |
163 | uint asArrayLength(bool *ok) const; |
164 | |
165 | static constexpr Value fromReturnedValue(ReturnedValue val) |
166 | { |
167 | return fromStaticValue(staticValue: StaticValue::fromReturnedValue(val)); |
168 | } |
169 | |
170 | // As per ES specs |
171 | bool sameValue(Value other) const; |
172 | bool sameValueZero(Value other) const; |
173 | |
174 | inline void mark(MarkStack *markStack); |
175 | |
176 | static double toInteger(double d) { return StaticValue::toInteger(d); } |
177 | static int toInt32(double d) { return StaticValue::toInt32(d); } |
178 | static unsigned int toUInt32(double d) { return StaticValue::toUInt32(d); } |
179 | inline static constexpr Value emptyValue() |
180 | { |
181 | return fromStaticValue(staticValue: StaticValue::emptyValue()); |
182 | } |
183 | static inline constexpr Value fromBoolean(bool b) |
184 | { |
185 | return fromStaticValue(staticValue: StaticValue::fromBoolean(b)); |
186 | } |
187 | static inline constexpr Value fromInt32(int i) |
188 | { |
189 | return fromStaticValue(staticValue: StaticValue::fromInt32(i)); |
190 | } |
191 | inline static constexpr Value undefinedValue() |
192 | { |
193 | return fromStaticValue(staticValue: StaticValue::undefinedValue()); |
194 | } |
195 | static inline constexpr Value nullValue() |
196 | { |
197 | return fromStaticValue(staticValue: StaticValue::nullValue()); |
198 | } |
199 | static inline Value fromDouble(double d) |
200 | { |
201 | return fromStaticValue(staticValue: StaticValue::fromDouble(d)); |
202 | } |
203 | static inline Value fromUInt32(uint i) |
204 | { |
205 | return fromStaticValue(staticValue: StaticValue::fromUInt32(i)); |
206 | } |
207 | |
208 | Value &operator =(const ScopedValue &v); |
209 | Value &operator=(ReturnedValue v) |
210 | { |
211 | StaticValue::operator=(v); |
212 | return *this; |
213 | } |
214 | Value &operator=(ManagedPtr m) { |
215 | if (!m) { |
216 | setM(nullptr); |
217 | } else { |
218 | _val = reinterpret_cast<Value *>(m)->_val; |
219 | } |
220 | return *this; |
221 | } |
222 | Value &operator=(HeapBasePtr o) { |
223 | setM(o); |
224 | return *this; |
225 | } |
226 | |
227 | template<typename T> |
228 | Value &operator=(const Scoped<T> &t); |
229 | }; |
230 | Q_STATIC_ASSERT(std::is_trivial_v<Value>); |
231 | Q_STATIC_ASSERT(sizeof(Value) == sizeof(StaticValue)); |
232 | |
233 | template<> |
234 | inline StaticValue &StaticValue::operator=<Value>(const Value &value) |
235 | { |
236 | _val = value._val; |
237 | return *this; |
238 | } |
239 | |
240 | template<typename Managed> |
241 | inline StaticValue &StaticValue::operator=(const Managed &m) |
242 | { |
243 | *static_cast<Value *>(this) = m; |
244 | return *this; |
245 | } |
246 | |
247 | template<> |
248 | inline Value &StaticValue::asValue<Value>() |
249 | { |
250 | return *static_cast<Value *>(this); |
251 | } |
252 | |
253 | template<> |
254 | inline const Value &StaticValue::asValue<Value>() const |
255 | { |
256 | return *static_cast<const Value *>(this); |
257 | } |
258 | |
259 | template<> |
260 | inline Value *CallData::argValues<Value>() |
261 | { |
262 | return static_cast<Value *>(static_cast<StaticValue *>(args)); |
263 | } |
264 | |
265 | template<> |
266 | inline const Value *CallData::argValues<Value>() const |
267 | { |
268 | return static_cast<const Value *>(static_cast<const StaticValue *>(args)); |
269 | } |
270 | |
271 | template<typename HeapBase> |
272 | inline Encode::Encode(HeapBase *o) |
273 | { |
274 | val = Value::fromHeapObject(m: o).asReturnedValue(); |
275 | } |
276 | |
277 | inline void Value::mark(MarkStack *markStack) |
278 | { |
279 | HeapBasePtr o = heapObject(); |
280 | if (o) |
281 | o->mark(markStack); |
282 | } |
283 | |
284 | inline bool Value::isString() const |
285 | { |
286 | HeapBasePtr b = heapObject(); |
287 | return b && b->internalClass->vtable->isString; |
288 | } |
289 | |
290 | bool Value::isStringOrSymbol() const |
291 | { |
292 | HeapBasePtr b = heapObject(); |
293 | return b && b->internalClass->vtable->isStringOrSymbol; |
294 | } |
295 | |
296 | bool Value::isSymbol() const |
297 | { |
298 | HeapBasePtr b = heapObject(); |
299 | return b && b->internalClass->vtable->isStringOrSymbol && !b->internalClass->vtable->isString; |
300 | } |
301 | |
302 | inline bool Value::isObject() const |
303 | |
304 | { |
305 | HeapBasePtr b = heapObject(); |
306 | return b && b->internalClass->vtable->isObject; |
307 | } |
308 | |
309 | inline bool Value::isFunctionObject() const |
310 | { |
311 | HeapBasePtr b = heapObject(); |
312 | if (!b) |
313 | return false; |
314 | const VTable *vtable = b->internalClass->vtable; |
315 | return vtable->call || vtable->callAsConstructor; |
316 | } |
317 | |
318 | inline bool Value::isPrimitive() const |
319 | { |
320 | return !isObject(); |
321 | } |
322 | |
323 | inline double Value::toNumber() const |
324 | { |
325 | if (isInteger()) |
326 | return int_32(); |
327 | if (isDouble()) |
328 | return doubleValue(); |
329 | return toNumberImpl(); |
330 | } |
331 | |
332 | inline ReturnedValue Value::convertedToNumber() const |
333 | { |
334 | if (isInteger() || isDouble()) |
335 | return asReturnedValue(); |
336 | Value v; |
337 | v.setDouble(toNumberImpl()); |
338 | return v.asReturnedValue(); |
339 | } |
340 | |
341 | inline |
342 | ReturnedValue Heap::Base::asReturnedValue() const |
343 | { |
344 | return Value::fromHeapObject(m: const_cast<Value::HeapBasePtr>(this)).asReturnedValue(); |
345 | } |
346 | |
347 | // For source compat with older code in other modules |
348 | using Primitive = Value; |
349 | |
350 | template<typename T> |
351 | ReturnedValue value_convert(ExecutionEngine *e, const Value &v); |
352 | |
353 | inline int Value::toInt32() const |
354 | { |
355 | if (Q_LIKELY(integerCompatible())) |
356 | return int_32(); |
357 | |
358 | if (Q_LIKELY(isDouble())) |
359 | return QJSNumberCoercion::toInteger(d: doubleValue()); |
360 | |
361 | return QJSNumberCoercion::toInteger(d: toNumberImpl()); |
362 | } |
363 | |
364 | inline unsigned int Value::toUInt32() const |
365 | { |
366 | return static_cast<unsigned int>(toInt32()); |
367 | } |
368 | |
369 | inline qint64 Value::toLength() const |
370 | { |
371 | if (Q_LIKELY(integerCompatible())) |
372 | return int_32() < 0 ? 0 : int_32(); |
373 | double i = Value::toInteger(d: isDouble() ? doubleValue() : toNumberImpl()); |
374 | if (i <= 0) |
375 | return 0; |
376 | if (i > (static_cast<qint64>(1) << 53) - 1) |
377 | return (static_cast<qint64>(1) << 53) - 1; |
378 | return static_cast<qint64>(i); |
379 | } |
380 | |
381 | inline qint64 Value::toIndex() const |
382 | { |
383 | qint64 idx; |
384 | if (Q_LIKELY(integerCompatible())) { |
385 | idx = int_32(); |
386 | } else { |
387 | idx = static_cast<qint64>(Value::toInteger(d: isDouble() ? doubleValue() : toNumberImpl())); |
388 | } |
389 | if (idx > (static_cast<qint64>(1) << 53) - 1) |
390 | idx = -1; |
391 | return idx; |
392 | } |
393 | |
394 | inline double Value::toInteger() const |
395 | { |
396 | if (integerCompatible()) |
397 | return int_32(); |
398 | |
399 | return Value::toInteger(d: isDouble() ? doubleValue() : toNumberImpl()); |
400 | } |
401 | |
402 | |
403 | template <size_t o> |
404 | struct HeapValue : Value { |
405 | static constexpr size_t offset = o; |
406 | HeapBasePtr base() { |
407 | HeapBasePtr base = reinterpret_cast<HeapBasePtr>(this) - (offset/sizeof(Heap::Base)); |
408 | Q_ASSERT(base->inUse()); |
409 | return base; |
410 | } |
411 | |
412 | void set(EngineBase *e, const Value &newVal) { |
413 | WriteBarrier::write(e, base(), data_ptr(), newVal.asReturnedValue()); |
414 | } |
415 | void set(EngineBase *e, HeapBasePtr b) { |
416 | WriteBarrier::write(e, base(), data_ptr(), b->asReturnedValue()); |
417 | } |
418 | }; |
419 | |
420 | template <size_t o> |
421 | struct ValueArray { |
422 | static constexpr size_t offset = o; |
423 | uint size; |
424 | uint alloc; |
425 | Value values[1]; |
426 | |
427 | Value::HeapBasePtr base() { |
428 | Value::HeapBasePtr base = reinterpret_cast<Value::HeapBasePtr>(this) |
429 | - (offset/sizeof(Heap::Base)); |
430 | Q_ASSERT(base->inUse()); |
431 | return base; |
432 | } |
433 | |
434 | void set(EngineBase *e, uint index, Value v) { |
435 | WriteBarrier::write(e, base(), values[index].data_ptr(), v.asReturnedValue()); |
436 | } |
437 | void set(EngineBase *e, uint index, Value::HeapBasePtr b) { |
438 | WriteBarrier::write(e, base(), values[index].data_ptr(), Value::fromHeapObject(m: b).asReturnedValue()); |
439 | } |
440 | inline const Value &operator[] (uint index) const { |
441 | Q_ASSERT(index < alloc); |
442 | return values[index]; |
443 | } |
444 | inline const Value *data() const { |
445 | return values; |
446 | } |
447 | |
448 | void mark(MarkStack *markStack) { |
449 | for (Value *v = values, *end = values + alloc; v < end; ++v) |
450 | v->mark(markStack); |
451 | } |
452 | }; |
453 | |
454 | // It's really important that the offset of values in this structure is |
455 | // constant across all architecture, otherwise JIT cross-compiled code will |
456 | // have wrong offsets between host and target. |
457 | Q_STATIC_ASSERT(offsetof(ValueArray<0>, values) == 8); |
458 | |
459 | class OptionalReturnedValue { |
460 | ReturnedValue value; |
461 | public: |
462 | |
463 | OptionalReturnedValue() : value(Value::emptyValue().asReturnedValue()) {} |
464 | explicit OptionalReturnedValue(ReturnedValue v) |
465 | : value(v) |
466 | { |
467 | Q_ASSERT(!Value::fromReturnedValue(v).isEmpty()); |
468 | } |
469 | |
470 | ReturnedValue operator->() const { return value; } |
471 | ReturnedValue operator*() const { return value; } |
472 | explicit operator bool() const { return !Value::fromReturnedValue(val: value).isEmpty(); } |
473 | }; |
474 | |
475 | } |
476 | |
477 | QT_END_NAMESPACE |
478 | |
479 | #endif // QV4VALUE_DEF_P_H |
480 |
Definitions
- Value
- Value
- Value
- fromStaticValue
- stringValue
- stringOrSymbolValue
- symbolValue
- objectValue
- managed
- heapObject
- fromHeapObject
- toBoolean
- toNumberImpl
- toString
- toObject
- as
- as
- cast
- cast
- fromReturnedValue
- toInteger
- toInt32
- toUInt32
- emptyValue
- fromBoolean
- fromInt32
- undefinedValue
- nullValue
- fromDouble
- fromUInt32
- operator=
- operator=
- operator=
- operator
- operator=
- asValue
- asValue
- argValues
- argValues
- Encode
- mark
- isString
- isStringOrSymbol
- isSymbol
- isObject
- isFunctionObject
- isPrimitive
- toNumber
- convertedToNumber
- asReturnedValue
- toInt32
- toUInt32
- toLength
- toIndex
- toInteger
- HeapValue
- offset
- base
- set
- set
- ValueArray
- offset
- base
- set
- set
- operator[]
- data
- mark
- OptionalReturnedValue
- OptionalReturnedValue
- OptionalReturnedValue
- operator->
- operator*
Learn Advanced QML with KDAB
Find out more