1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | // |
41 | // W A R N I N G |
42 | // ------------- |
43 | // |
44 | // This file is not part of the Qt API. It exists purely as an |
45 | // implementation detail. This header file may change from version to |
46 | // version without notice, or even be removed. |
47 | // |
48 | // We mean it. |
49 | // |
50 | |
51 | #ifndef QJSVALUE_P_H |
52 | #define QJSVALUE_P_H |
53 | |
54 | #include <qjsvalue.h> |
55 | #include <private/qtqmlglobal_p.h> |
56 | #include <private/qv4value_p.h> |
57 | #include <private/qv4string_p.h> |
58 | #include <private/qv4engine_p.h> |
59 | #include <private/qflagpointer_p.h> |
60 | #include <private/qv4mm_p.h> |
61 | #include <private/qv4persistent_p.h> |
62 | |
63 | #include <QtCore/qthread.h> |
64 | |
65 | QT_BEGIN_NAMESPACE |
66 | |
67 | class Q_AUTOTEST_EXPORT QJSValuePrivate |
68 | { |
69 | public: |
70 | static inline QV4::Value *getValue(const QJSValue *jsval) |
71 | { |
72 | if (jsval->d & 3) |
73 | return nullptr; |
74 | return reinterpret_cast<QV4::Value *>(jsval->d); |
75 | } |
76 | |
77 | static inline QVariant *getVariant(const QJSValue *jsval) |
78 | { |
79 | if (jsval->d & 1) |
80 | return reinterpret_cast<QVariant *>(jsval->d & ~3); |
81 | return nullptr; |
82 | } |
83 | |
84 | static inline void setRawValue(QJSValue *jsval, QV4::Value *v) |
85 | { |
86 | jsval->d = reinterpret_cast<quintptr>(v); |
87 | } |
88 | |
89 | static inline void setVariant(QJSValue *jsval, const QVariant &v) { |
90 | QVariant *val = new QVariant(v); |
91 | jsval->d = reinterpret_cast<quintptr>(val) | 1; |
92 | } |
93 | |
94 | static inline void setValue(QJSValue *jsval, QV4::ExecutionEngine *engine, const QV4::Value &v) { |
95 | QV4::Value *value = engine->memoryManager->m_persistentValues->allocate(); |
96 | *value = v; |
97 | jsval->d = reinterpret_cast<quintptr>(value); |
98 | } |
99 | |
100 | static inline void setValue(QJSValue *jsval, QV4::ExecutionEngine *engine, QV4::ReturnedValue v) { |
101 | QV4::Value *value = engine->memoryManager->m_persistentValues->allocate(); |
102 | *value = v; |
103 | jsval->d = reinterpret_cast<quintptr>(value); |
104 | } |
105 | |
106 | static QV4::ReturnedValue convertedToValue(QV4::ExecutionEngine *e, const QJSValue &jsval) |
107 | { |
108 | QV4::Value *v = getValue(jsval: &jsval); |
109 | if (!v) { |
110 | QVariant *variant = getVariant(jsval: &jsval); |
111 | v = e->memoryManager->m_persistentValues->allocate(); |
112 | *v = variant ? e->fromVariant(*variant) : QV4::Encode::undefined(); |
113 | jsval.d = reinterpret_cast<quintptr>(v); |
114 | delete variant; |
115 | } |
116 | |
117 | if (QV4::PersistentValueStorage::getEngine(v) != e) { |
118 | qWarning(msg: "JSValue can't be reassigned to another engine." ); |
119 | return QV4::Encode::undefined(); |
120 | } |
121 | |
122 | return v->asReturnedValue(); |
123 | } |
124 | |
125 | static QV4::Value *valueForData(const QJSValue *jsval, QV4::Value *scratch) |
126 | { |
127 | QV4::Value *v = getValue(jsval); |
128 | if (v) |
129 | return v; |
130 | v = scratch; |
131 | QVariant *variant = getVariant(jsval); |
132 | if (!variant) { |
133 | *v = QV4::Encode::undefined(); |
134 | return v; |
135 | } |
136 | |
137 | switch (variant->userType()) { |
138 | case QMetaType::UnknownType: |
139 | case QMetaType::Void: |
140 | *v = QV4::Encode::undefined(); |
141 | break; |
142 | case QMetaType::Nullptr: |
143 | case QMetaType::VoidStar: |
144 | *v = QV4::Encode::null(); |
145 | break; |
146 | case QMetaType::Bool: |
147 | *v = QV4::Encode(variant->toBool()); |
148 | break; |
149 | case QMetaType::Double: |
150 | *v = QV4::Encode(variant->toDouble()); |
151 | break; |
152 | case QMetaType::Int: |
153 | case QMetaType::Short: |
154 | case QMetaType::UShort: |
155 | case QMetaType::Char: |
156 | case QMetaType::UChar: |
157 | *v = QV4::Encode(variant->toInt()); |
158 | break; |
159 | case QMetaType::UInt: |
160 | *v = QV4::Encode(variant->toUInt()); |
161 | break; |
162 | default: |
163 | return nullptr; |
164 | } |
165 | return v; |
166 | } |
167 | |
168 | static QV4::ExecutionEngine *engine(const QJSValue *jsval) { |
169 | QV4::Value *v = getValue(jsval); |
170 | return v ? QV4::PersistentValueStorage::getEngine(v) : nullptr; |
171 | } |
172 | |
173 | static inline bool checkEngine(QV4::ExecutionEngine *e, const QJSValue &jsval) { |
174 | QV4::ExecutionEngine *v4 = engine(jsval: &jsval); |
175 | return !v4 || v4 == e; |
176 | } |
177 | |
178 | static inline void free(QJSValue *jsval) { |
179 | if (QV4::Value *v = QJSValuePrivate::getValue(jsval)) { |
180 | if (QV4::ExecutionEngine *e = engine(jsval)) { |
181 | if (QJSEngine *jsEngine = e->jsEngine()) { |
182 | if (jsEngine->thread() != QThread::currentThread()) { |
183 | QMetaObject::invokeMethod( |
184 | context: jsEngine, function: [v](){ QV4::PersistentValueStorage::free(e: v); }); |
185 | return; |
186 | } |
187 | } |
188 | } |
189 | QV4::PersistentValueStorage::free(e: v); |
190 | } else if (QVariant *v = QJSValuePrivate::getVariant(jsval)) { |
191 | delete v; |
192 | } |
193 | } |
194 | }; |
195 | |
196 | QT_END_NAMESPACE |
197 | |
198 | #endif |
199 | |