Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/jsruntime/qv4value.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | #include <qv4engine_p.h> | - | ||||||||||||
40 | #include <qv4runtime_p.h> | - | ||||||||||||
41 | #include <qv4string_p.h> | - | ||||||||||||
42 | #include <qv4propertykey_p.h> | - | ||||||||||||
43 | #ifndef V4_BOOTSTRAP | - | ||||||||||||
44 | #include <qv4symbol_p.h> | - | ||||||||||||
45 | #include <qv4object_p.h> | - | ||||||||||||
46 | #include <qv4objectproto_p.h> | - | ||||||||||||
47 | #include <private/qv4mm_p.h> | - | ||||||||||||
48 | #endif | - | ||||||||||||
49 | - | |||||||||||||
50 | #include <wtf/MathExtras.h> | - | ||||||||||||
51 | - | |||||||||||||
52 | using namespace QV4; | - | ||||||||||||
53 | - | |||||||||||||
54 | int Value::toUInt16() const | - | ||||||||||||
55 | { | - | ||||||||||||
56 | if (integerCompatible())
| 0 | ||||||||||||
57 | return (ushort)(uint)integerValue(); never executed: return (ushort)(uint)integerValue(); | 0 | ||||||||||||
58 | - | |||||||||||||
59 | double number = toNumber(); | - | ||||||||||||
60 | - | |||||||||||||
61 | double D16 = 65536.0; | - | ||||||||||||
62 | if ((number >= 0 && number < D16))
| 0 | ||||||||||||
63 | return static_cast<ushort>(number); never executed: return static_cast<ushort>(number); | 0 | ||||||||||||
64 | - | |||||||||||||
65 | if (!std::isfinite(number))
| 0 | ||||||||||||
66 | return +0; never executed: return +0; | 0 | ||||||||||||
67 | - | |||||||||||||
68 | double d = ::floor(::fabs(number)); | - | ||||||||||||
69 | if (std::signbit(number))
| 0 | ||||||||||||
70 | d = -d; never executed: d = -d; | 0 | ||||||||||||
71 | - | |||||||||||||
72 | number = ::fmod(d , D16); | - | ||||||||||||
73 | - | |||||||||||||
74 | if (number < 0)
| 0 | ||||||||||||
75 | number += D16; never executed: number += D16; | 0 | ||||||||||||
76 | - | |||||||||||||
77 | return (unsigned short)number; never executed: return (unsigned short)number; | 0 | ||||||||||||
78 | } | - | ||||||||||||
79 | - | |||||||||||||
80 | bool Value::toBooleanImpl(Value val) | - | ||||||||||||
81 | { | - | ||||||||||||
82 | if (val.isManagedOrUndefined()) {
| 0 | ||||||||||||
83 | Heap::Base *b = val.m(); | - | ||||||||||||
84 | if (!b)
| 0 | ||||||||||||
85 | return false; never executed: return false; | 0 | ||||||||||||
86 | #ifdef V4_BOOTSTRAP | - | ||||||||||||
87 | Q_UNIMPLEMENTED(); | - | ||||||||||||
88 | #else | - | ||||||||||||
89 | if (b->internalClass->vtable->isString) | - | ||||||||||||
90 | return static_cast<Heap::String *>(b)->length() > 0; | - | ||||||||||||
91 | #endif | - | ||||||||||||
92 | return true; never executed: return true; | 0 | ||||||||||||
93 | } | - | ||||||||||||
94 | - | |||||||||||||
95 | // double | - | ||||||||||||
96 | double d = val.doubleValue(); | - | ||||||||||||
97 | return d && !std::isnan(d); never executed: return d && !std::isnan(d); | 0 | ||||||||||||
98 | } | - | ||||||||||||
99 | - | |||||||||||||
100 | double Value::toNumberImpl(Value val) | - | ||||||||||||
101 | { | - | ||||||||||||
102 | switch (val.type()) { | - | ||||||||||||
103 | case QV4::Value::Undefined_Type: never executed: case QV4::Value::Undefined_Type: | 0 | ||||||||||||
104 | return std::numeric_limits<double>::quiet_NaN(); never executed: return std::numeric_limits<double>::quiet_NaN(); | 0 | ||||||||||||
105 | case QV4::Value::Managed_Type: never executed: case QV4::Value::Managed_Type: | 0 | ||||||||||||
106 | #ifdef V4_BOOTSTRAP | - | ||||||||||||
107 | Q_UNIMPLEMENTED(); | - | ||||||||||||
108 | Q_FALLTHROUGH(); | - | ||||||||||||
109 | #else | - | ||||||||||||
110 | if (String *s = val.stringValue()) | - | ||||||||||||
111 | return RuntimeHelpers::stringToNumber(s->toQString()); | - | ||||||||||||
112 | if (val.isSymbol()) { | - | ||||||||||||
113 | Managed &m = static_cast<Managed &>(val); | - | ||||||||||||
114 | m.engine()->throwTypeError(); | - | ||||||||||||
115 | return 0; | - | ||||||||||||
116 | } | - | ||||||||||||
117 | { | - | ||||||||||||
118 | Q_ASSERT(val.isObject()); | - | ||||||||||||
119 | Scope scope(val.objectValue()->engine()); | - | ||||||||||||
120 | ScopedValue protectThis(scope, val); | - | ||||||||||||
121 | ScopedValue prim(scope, RuntimeHelpers::toPrimitive(val, NUMBER_HINT)); | - | ||||||||||||
122 | if (scope.engine->hasException) | - | ||||||||||||
123 | return 0; | - | ||||||||||||
124 | return prim->toNumber(); | - | ||||||||||||
125 | } | - | ||||||||||||
126 | #endif | - | ||||||||||||
127 | case QV4::Value::Null_Type: code before this statement never executed: case QV4::Value::Null_Type: never executed: case QV4::Value::Null_Type: | 0 | ||||||||||||
128 | case QV4::Value::Boolean_Type: never executed: case QV4::Value::Boolean_Type: | 0 | ||||||||||||
129 | case QV4::Value::Integer_Type: never executed: case QV4::Value::Integer_Type: | 0 | ||||||||||||
130 | return val.int_32(); never executed: return val.int_32(); | 0 | ||||||||||||
131 | default: // double never executed: default: | 0 | ||||||||||||
132 | Q_UNREACHABLE(); | - | ||||||||||||
133 | } never executed: end of block | 0 | ||||||||||||
134 | } | - | ||||||||||||
135 | - | |||||||||||||
136 | #ifndef V4_BOOTSTRAP | - | ||||||||||||
137 | QString Value::toQStringNoThrow() const | - | ||||||||||||
138 | { | - | ||||||||||||
139 | switch (type()) { | - | ||||||||||||
140 | case Value::Empty_Type: | - | ||||||||||||
141 | Q_ASSERT(!"empty Value encountered"); | - | ||||||||||||
142 | Q_UNREACHABLE(); | - | ||||||||||||
143 | case Value::Undefined_Type: | - | ||||||||||||
144 | return QStringLiteral("undefined"); | - | ||||||||||||
145 | case Value::Null_Type: | - | ||||||||||||
146 | return QStringLiteral("null"); | - | ||||||||||||
147 | case Value::Boolean_Type: | - | ||||||||||||
148 | if (booleanValue()) | - | ||||||||||||
149 | return QStringLiteral("true"); | - | ||||||||||||
150 | else | - | ||||||||||||
151 | return QStringLiteral("false"); | - | ||||||||||||
152 | case Value::Managed_Type: | - | ||||||||||||
153 | if (String *s = stringValue()) | - | ||||||||||||
154 | return s->toQString(); | - | ||||||||||||
155 | if (Symbol *s = symbolValue()) | - | ||||||||||||
156 | return s->descriptiveString(); | - | ||||||||||||
157 | { | - | ||||||||||||
158 | Q_ASSERT(isObject()); | - | ||||||||||||
159 | Scope scope(objectValue()->engine()); | - | ||||||||||||
160 | ScopedValue ex(scope); | - | ||||||||||||
161 | bool caughtException = false; | - | ||||||||||||
162 | ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT)); | - | ||||||||||||
163 | if (scope.hasException()) { | - | ||||||||||||
164 | ex = scope.engine->catchException(); | - | ||||||||||||
165 | caughtException = true; | - | ||||||||||||
166 | } else if (prim->isPrimitive()) { | - | ||||||||||||
167 | return prim->toQStringNoThrow(); | - | ||||||||||||
168 | } | - | ||||||||||||
169 | // Can't nest try/catch due to CXX ABI limitations for foreign exception nesting. | - | ||||||||||||
170 | if (caughtException) { | - | ||||||||||||
171 | ScopedValue prim(scope, RuntimeHelpers::toPrimitive(ex, STRING_HINT)); | - | ||||||||||||
172 | if (scope.hasException()) { | - | ||||||||||||
173 | ex = scope.engine->catchException(); | - | ||||||||||||
174 | } else if (prim->isPrimitive()) { | - | ||||||||||||
175 | return prim->toQStringNoThrow(); | - | ||||||||||||
176 | } | - | ||||||||||||
177 | } | - | ||||||||||||
178 | return QString(); | - | ||||||||||||
179 | } | - | ||||||||||||
180 | case Value::Integer_Type: { | - | ||||||||||||
181 | QString str; | - | ||||||||||||
182 | RuntimeHelpers::numberToString(&str, (double)int_32(), 10); | - | ||||||||||||
183 | return str; | - | ||||||||||||
184 | } | - | ||||||||||||
185 | default: { // double | - | ||||||||||||
186 | QString str; | - | ||||||||||||
187 | RuntimeHelpers::numberToString(&str, doubleValue(), 10); | - | ||||||||||||
188 | return str; | - | ||||||||||||
189 | } | - | ||||||||||||
190 | } // switch | - | ||||||||||||
191 | } | - | ||||||||||||
192 | - | |||||||||||||
193 | QString Value::toQString() const | - | ||||||||||||
194 | { | - | ||||||||||||
195 | switch (type()) { | - | ||||||||||||
196 | case Value::Empty_Type: | - | ||||||||||||
197 | Q_ASSERT(!"empty Value encountered"); | - | ||||||||||||
198 | Q_UNREACHABLE(); | - | ||||||||||||
199 | case Value::Undefined_Type: | - | ||||||||||||
200 | return QStringLiteral("undefined"); | - | ||||||||||||
201 | case Value::Null_Type: | - | ||||||||||||
202 | return QStringLiteral("null"); | - | ||||||||||||
203 | case Value::Boolean_Type: | - | ||||||||||||
204 | if (booleanValue()) | - | ||||||||||||
205 | return QStringLiteral("true"); | - | ||||||||||||
206 | else | - | ||||||||||||
207 | return QStringLiteral("false"); | - | ||||||||||||
208 | case Value::Managed_Type: | - | ||||||||||||
209 | if (String *s = stringValue()) { | - | ||||||||||||
210 | return s->toQString(); | - | ||||||||||||
211 | } else if (isSymbol()) { | - | ||||||||||||
212 | static_cast<const Managed *>(this)->engine()->throwTypeError(); | - | ||||||||||||
213 | return QString(); | - | ||||||||||||
214 | } else { | - | ||||||||||||
215 | Q_ASSERT(isObject()); | - | ||||||||||||
216 | Scope scope(objectValue()->engine()); | - | ||||||||||||
217 | ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT)); | - | ||||||||||||
218 | return prim->toQString(); | - | ||||||||||||
219 | } | - | ||||||||||||
220 | case Value::Integer_Type: { | - | ||||||||||||
221 | QString str; | - | ||||||||||||
222 | RuntimeHelpers::numberToString(&str, (double)int_32(), 10); | - | ||||||||||||
223 | return str; | - | ||||||||||||
224 | } | - | ||||||||||||
225 | default: { // double | - | ||||||||||||
226 | QString str; | - | ||||||||||||
227 | RuntimeHelpers::numberToString(&str, doubleValue(), 10); | - | ||||||||||||
228 | return str; | - | ||||||||||||
229 | } | - | ||||||||||||
230 | } // switch | - | ||||||||||||
231 | } | - | ||||||||||||
232 | - | |||||||||||||
233 | QV4::PropertyKey Value::toPropertyKey(ExecutionEngine *e) const | - | ||||||||||||
234 | { | - | ||||||||||||
235 | if (isInteger() && int_32() >= 0) | - | ||||||||||||
236 | return PropertyKey::fromArrayIndex(static_cast<uint>(int_32())); | - | ||||||||||||
237 | if (isStringOrSymbol()) { | - | ||||||||||||
238 | Scope scope(e); | - | ||||||||||||
239 | ScopedStringOrSymbol s(scope, this); | - | ||||||||||||
240 | return s->toPropertyKey(); | - | ||||||||||||
241 | } | - | ||||||||||||
242 | Scope scope(e); | - | ||||||||||||
243 | ScopedValue v(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT)); | - | ||||||||||||
244 | if (!v->isStringOrSymbol()) | - | ||||||||||||
245 | v = v->toString(e); | - | ||||||||||||
246 | if (e->hasException) | - | ||||||||||||
247 | return PropertyKey::invalid(); | - | ||||||||||||
248 | ScopedStringOrSymbol s(scope, v); | - | ||||||||||||
249 | return s->toPropertyKey(); | - | ||||||||||||
250 | } | - | ||||||||||||
251 | #endif // V4_BOOTSTRAP | - | ||||||||||||
252 | - | |||||||||||||
253 | bool Value::sameValue(Value other) const { | - | ||||||||||||
254 | if (_val == other._val)
| 0 | ||||||||||||
255 | return true; never executed: return true; | 0 | ||||||||||||
256 | String *s = stringValue(); | - | ||||||||||||
257 | String *os = other.stringValue(); | - | ||||||||||||
258 | if (s && os)
| 0 | ||||||||||||
259 | return s->isEqualTo(os); never executed: return s->isEqualTo(os); | 0 | ||||||||||||
260 | if (isInteger() && other.isDouble())
| 0 | ||||||||||||
261 | return int_32() ? (double(int_32()) == other.doubleValue()) never executed: return int_32() ? (double(int_32()) == other.doubleValue()) : (other.doubleValue() == 0 && !std::signbit(other.doubleValue())); | 0 | ||||||||||||
262 | : (other.doubleValue() == 0 && !std::signbit(other.doubleValue())); never executed: return int_32() ? (double(int_32()) == other.doubleValue()) : (other.doubleValue() == 0 && !std::signbit(other.doubleValue())); | 0 | ||||||||||||
263 | if (isDouble() && other.isInteger())
| 0 | ||||||||||||
264 | return other.int_32() ? (doubleValue() == double(other.int_32())) never executed: return other.int_32() ? (doubleValue() == double(other.int_32())) : (doubleValue() == 0 && !std::signbit(doubleValue())); | 0 | ||||||||||||
265 | : (doubleValue() == 0 && !std::signbit(doubleValue())); never executed: return other.int_32() ? (doubleValue() == double(other.int_32())) : (doubleValue() == 0 && !std::signbit(doubleValue())); | 0 | ||||||||||||
266 | return false; never executed: return false; | 0 | ||||||||||||
267 | } | - | ||||||||||||
268 | - | |||||||||||||
269 | bool Value::sameValueZero(Value other) const { | - | ||||||||||||
270 | if (_val == other._val)
| 0 | ||||||||||||
271 | return true; never executed: return true; | 0 | ||||||||||||
272 | String *s = stringValue(); | - | ||||||||||||
273 | String *os = other.stringValue(); | - | ||||||||||||
274 | if (s && os)
| 0 | ||||||||||||
275 | return s->isEqualTo(os); never executed: return s->isEqualTo(os); | 0 | ||||||||||||
276 | if (isInteger() && other.isDouble())
| 0 | ||||||||||||
277 | return double(int_32()) == other.doubleValue(); never executed: return double(int_32()) == other.doubleValue(); | 0 | ||||||||||||
278 | if (isDouble() && other.isInteger())
| 0 | ||||||||||||
279 | return other.int_32() == doubleValue(); never executed: return other.int_32() == doubleValue(); | 0 | ||||||||||||
280 | if (isDouble() && other.isDouble()) {
| 0 | ||||||||||||
281 | if (doubleValue() == 0 && other.doubleValue() == 0) {
| 0 | ||||||||||||
282 | return true; never executed: return true; | 0 | ||||||||||||
283 | } | - | ||||||||||||
284 | } never executed: end of block | 0 | ||||||||||||
285 | return false; never executed: return false; | 0 | ||||||||||||
286 | } | - | ||||||||||||
287 | - | |||||||||||||
288 | #ifndef V4_BOOTSTRAP | - | ||||||||||||
289 | Heap::String *Value::toString(ExecutionEngine *e, Value val) | - | ||||||||||||
290 | { | - | ||||||||||||
291 | return RuntimeHelpers::convertToString(e, val); | - | ||||||||||||
292 | } | - | ||||||||||||
293 | - | |||||||||||||
294 | Heap::Object *Value::toObject(ExecutionEngine *e, Value val) | - | ||||||||||||
295 | { | - | ||||||||||||
296 | return RuntimeHelpers::convertToObject(e, val); | - | ||||||||||||
297 | } | - | ||||||||||||
298 | - | |||||||||||||
299 | uint Value::asArrayLength(bool *ok) const | - | ||||||||||||
300 | { | - | ||||||||||||
301 | *ok = true; | - | ||||||||||||
302 | if (isInteger()) { | - | ||||||||||||
303 | if (int_32() >= 0) { | - | ||||||||||||
304 | return (uint)int_32(); | - | ||||||||||||
305 | } else { | - | ||||||||||||
306 | *ok = false; | - | ||||||||||||
307 | return UINT_MAX; | - | ||||||||||||
308 | } | - | ||||||||||||
309 | } | - | ||||||||||||
310 | if (isNumber()) { | - | ||||||||||||
311 | double d = doubleValue(); | - | ||||||||||||
312 | uint idx = (uint)d; | - | ||||||||||||
313 | if (idx != d) { | - | ||||||||||||
314 | *ok = false; | - | ||||||||||||
315 | return UINT_MAX; | - | ||||||||||||
316 | } | - | ||||||||||||
317 | return idx; | - | ||||||||||||
318 | } | - | ||||||||||||
319 | if (String *s = stringValue()) | - | ||||||||||||
320 | return s->toUInt(ok); | - | ||||||||||||
321 | - | |||||||||||||
322 | uint idx = toUInt32(); | - | ||||||||||||
323 | double d = toNumber(); | - | ||||||||||||
324 | if (d != idx) { | - | ||||||||||||
325 | *ok = false; | - | ||||||||||||
326 | return UINT_MAX; | - | ||||||||||||
327 | } | - | ||||||||||||
328 | return idx; | - | ||||||||||||
329 | } | - | ||||||||||||
330 | #endif // V4_BOOTSTRAP | - | ||||||||||||
Source code | Switch to Preprocessed file |