Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qjsprimitivevalue.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 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// Qt-Security score:significant
4
6
7#include <QtQml/private/qv4runtime_p.h>
8
10
11/*!
12 \since 6.1
13 \class QJSPrimitiveUndefined
14
15 \inmodule QtQml
16
17 \brief An empty marker type to signify the JavaScript Undefined type and its single value.
18 \inmodule QtQml
19 */
20
21/*!
22 \since 6.1
23 \class QJSPrimitiveNull
24
25 \inmodule QtQml
26
27 \brief An empty marker type to signify the JavaScript null value.
28 \inmodule QtQml
29 */
30
31/*!
32 \since 6.1
33 \class QJSPrimitiveValue
34
35 \brief The QJSPrimitiveValue class operates on primitive types in JavaScript semantics.
36
37 \ingroup qtjavascript
38 \inmodule QtQml
39
40 QJSPrimitiveValue supports most of the primitive types defined in the
41 \l{ECMA-262} standard, in particular Undefined, Boolean, Number, and String.
42 Additionally, you can store a JavaScript null in a QJSPrimitiveValue and as a
43 special case of Number, you can store an integer value.
44
45 All those values are stored immediately, without interacting with the
46 JavaScript heap. Therefore, you can pass QJSPrimitiveValues between different
47 JavaScript engines. In contrast to QJSManagedValue, there is also no danger
48 in destroying a QJSPrimitiveValue from a different thread than it was created
49 in. On the flip side, QJSPrimitiveValue does not hold a reference to any
50 JavaScript engine.
51
52 QJSPrimitiveValue implements the JavaScript arithmetic and comparison
53 operators on the supported types in JavaScript semantics. Types are coerced
54 like the JavaScript engine would coerce them if the operators were written
55 in a JavaScript expression.
56
57 The JavaScript Symbol type is not supported as it is of very limited utility
58 regarding arithmetic and comparison operators, the main purpose of
59 QJSPrimitiveValue. In particular, it causes an exception whenever you try to
60 coerce it to a number or a string, and we cannot throw exceptions without a
61 JavaScript Engine.
62 */
63
64/*!
65 \enum QJSPrimitiveValue::Type
66
67 This enum speicifies the types a QJSPrimitiveValue might contain.
68
69 \value Undefined The JavaScript Undefined value.
70 \value Null The JavaScript null value. This is in fact not a separate
71 JavaScript type but a special value of the Object type. As it is
72 very common and storable without JavaScript engine, it is still
73 supported.
74 \value Boolean A JavaScript Boolean value.
75 \value Integer An integer. This is a special case of the JavaScript Number
76 type. JavaScript does not have an actual integer type, but
77 the \l{ECMA-262} standard contains rules on how to transform a
78 Number in order to prepare it for certain operators that only
79 make sense on integers, in particular the bit shift operators.
80 QJSPrimitiveValue's Integer type represents the result of such
81 a transformation.
82 \value Double A JavaScript Number value.
83 \value String A JavaScript String value.
84 */
85
86/*!
87 \fn Type QJSPrimitiveValue::type() const
88
89 Returns the type of the QJSPrimitiveValue.
90 */
91
92/*!
93 \fn QJSPrimitiveValue::QJSPrimitiveValue()
94
95 Creates a QJSPrimitiveValue of type Undefined.
96 */
97
98/*!
99 \fn QJSPrimitiveValue::QJSPrimitiveValue(QJSPrimitiveUndefined undefined)
100
101 Creates a QJSPrimitiveValue of value \a undefined and type Undefined.
102 */
103
104/*!
105 \fn QJSPrimitiveValue::QJSPrimitiveValue(QJSPrimitiveNull null)
106
107 Creates a QJSPrimitiveValue of value \a null and type Null.
108 */
109
110/*!
111 \fn QJSPrimitiveValue::QJSPrimitiveValue(bool value)
112
113 Creates a QJSPrimitiveValue of value \a value and type Boolean.
114 */
115
116/*!
117 \fn QJSPrimitiveValue::QJSPrimitiveValue(int value)
118
119 Creates a QJSPrimitiveValue of value \a value and type Integer.
120 */
121
122/*!
123 \fn QJSPrimitiveValue::QJSPrimitiveValue(double value)
124
125 Creates a QJSPrimitiveValue of value \a value and type Double.
126 */
127
128/*!
129 \fn QJSPrimitiveValue::QJSPrimitiveValue(QString value)
130
131 Creates a QJSPrimitiveValue of value \a value and type String.
132 */
133
134/*!
135 \fn QJSPrimitiveValue::QJSPrimitiveValue(QMetaType type, const void *value)
136 \since 6.4
137
138 Creates a QJSPrimitiveValue of type \a type, and initializes with
139 \a value if \a type can be stored in QJSPrimtiveValue. \a value must not
140 be nullptr in that case. If \a type cannot be stored this results in a
141 QJSPrimitiveValue of type Undefined.
142
143 Note that you have to pass the address of the variable you want stored.
144
145 Usually, you never have to use this constructor, use the one taking QVariant
146 instead.
147 */
148
149/*!
150 \fn QJSPrimitiveValue::QJSPrimitiveValue(QMetaType type)
151 \since 6.6
152 \internal
153
154 Creates a QJSPrimitiveValue of type \a type, and initializes with a
155 default-constructed value if \a type can be stored in QJSPrimtiveValue.
156 If \a type cannot be stored this results in a QJSPrimitiveValue of type
157 Undefined.
158*/
159
160/*!
161 \fn QJSPrimitiveValue::QJSPrimitiveValue(const QVariant &value)
162
163 Creates a QJSPrimitiveValue from the contents of \a value if those contents
164 can be stored in QJSPrimtiveValue. Otherwise this results in a
165 QJSPrimitiveValue of type Undefined.
166 */
167
168/*!
169 \fn bool QJSPrimitiveValue::toBoolean() const
170
171 Returns the value coerced a boolean by JavaScript rules.
172 */
173
174/*!
175 \fn int QJSPrimitiveValue::toInteger() const
176
177 Returns the value coerced to an integral 32bit number by the rules JavaScript
178 would apply when preparing it for a bit shift operation.
179 */
180
181/*!
182 \fn double QJSPrimitiveValue::toDouble() const
183
184 Returns the value coerced to a JavaScript Number by JavaScript rules.
185 */
186
187/*!
188 \fn QString QJSPrimitiveValue::toString() const
189
190 Returns the value coerced to a JavaScript String by JavaScript rules.
191 */
192
193/*!
194 \fn QJSPrimitiveValue QJSPrimitiveValue::operator+(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
195
196 \since 6.1
197
198 Perfoms the JavaScript '+' operation on \a lhs and \a rhs, and returns the
199 result.
200 */
201
202/*!
203 \fn QJSPrimitiveValue QJSPrimitiveValue::operator-(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
204 \since 6.1
205
206 Performs the JavaScript '-' operation on \a lhs and \a rhs, and returns the
207 result.
208 */
209
210/*!
211 \fn QJSPrimitiveValue QJSPrimitiveValue::operator*(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
212 \since 6.1
213
214 Performs the JavaScript '*' operation on \a lhs and \a rhs, and returns the
215 result.
216 */
217
218/*!
219 \fn QJSPrimitiveValue QJSPrimitiveValue::operator/(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
220 \since 6.1
221
222 Performs the JavaScript '/' operation between \a lhs and \a rhs, and returns the
223 result.
224 */
225
226/*!
227 \fn bool QJSPrimitiveValue::strictlyEquals(const QJSPrimitiveValue &other) const
228
229 Performs the JavaScript '===' operation on this QJSPrimitiveValue and
230 \a other, and returns the result.
231 */
232
233/*!
234 \fn bool QJSPrimitiveValue::equals(const QJSPrimitiveValue &other) const
235
236 Performs the JavaScript '==' operation on this QJSPrimitiveValue and
237 \a other, and returns the result.
238 */
239
240/*!
241 \fn bool QJSPrimitiveValue::operator==(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
242 \since 6.1
243
244 Performs the JavaScript '===' operation on \a lhs and \a rhs, and returns the
245 result.
246 */
247
248/*!
249 \fn bool QJSPrimitiveValue::operator!=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
250 \since 6.1
251
252 Performs the JavaScript '!==' operation on \a lhs and \a rhs, and returns the
253 result.
254 */
255
256/*!
257 \fn bool QJSPrimitiveValue::operator<(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
258 \since 6.1
259
260 Performs the JavaScript '<' operation on \a lhs and \a rhs, and returns the
261 result.
262 */
263
264/*!
265 \fn bool QJSPrimitiveValue::operator>(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
266 \since 6.1
267
268 Performs the JavaScript '>' operation on \a lhs and \a rhs, and returns the
269 result.
270 */
271
272/*!
273 \fn bool QJSPrimitiveValue::operator<=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
274 \since 6.1
275
276 Performs the JavaScript '<=' operation on \a lhs and \a rhs, and returns the
277 result.
278 */
279
280/*!
281 \fn bool QJSPrimitiveValue::operator>=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
282 \since 6.1
283
284 Performs the JavaScript '>=' operation on \a lhs and \a rhs, and returns the
285 result.
286 */
287
288/*!
289 \fn QMetaType QJSPrimitiveValue::metaType() const
290 \since 6.6
291
292 Returns the QMetaType of the value stored in the QJSPrimitiveValue.
293 */
294
295/*!
296 \fn const void *QJSPrimitiveValue::constData() const
297 \fn const void *QJSPrimitiveValue::data() const
298 \since 6.6
299
300 Returns a pointer to the contained value as a generic void* that cannot be
301 written to.
302 */
303
304/*!
305 \fn const void *QJSPrimitiveValue::data()
306 \since 6.6
307
308 Returns a pointer to the contained data as a generic void* that can be
309 written to.
310*/
311
312/*!
313 \fn template<QJSPrimitiveValue::Type type> QJSPrimitiveValue QJSPrimitiveValue::to() const
314 \since 6.6
315
316 Coerces the value to the specified \a type and returns the result as a new
317 QJSPrimitiveValue.
318
319 \sa toBoolean(), toInteger(), toDouble(), toString()
320*/
321
322QString QJSPrimitiveValue::toString(double d)
323{
324 QString result;
325 QV4::RuntimeHelpers::numberToString(&result, d);
326 return result;
327}
328
329/*!
330 \fn double QQmlPrivate::jsExponentiate(double base, double exponent)
331 \internal
332 \since 6.4
333
334 Performs JavaScript's Number::exponentiate operation on \a base and
335 \a exponent, and returns the result.
336
337 See https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-numeric-types-number-exponentiate
338 */
339
340QT_END_NAMESPACE
Combined button and popup list for selecting options.