1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSCriptTools 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 | #include "qscriptdebuggervalue_p.h" |
41 | |
42 | #include <QtScript/qscriptvalue.h> |
43 | #include <QtScript/qscriptengine.h> |
44 | #include <QtCore/qdatastream.h> |
45 | #include <QtCore/qdebug.h> |
46 | #include <QtCore/qshareddata.h> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | /*! |
51 | \since 4.5 |
52 | \class QScriptDebuggerValue |
53 | \internal |
54 | |
55 | \brief The QScriptDebuggerValue class represents a script value. |
56 | */ |
57 | |
58 | class QScriptDebuggerValuePrivate : public QSharedData |
59 | { |
60 | public: |
61 | QScriptDebuggerValuePrivate(); |
62 | ~QScriptDebuggerValuePrivate(); |
63 | |
64 | QScriptDebuggerValue::ValueType type; |
65 | union { |
66 | bool booleanValue; |
67 | QString *stringValue; |
68 | double numberValue; |
69 | qint64 objectId; |
70 | }; |
71 | }; |
72 | |
73 | QScriptDebuggerValuePrivate::QScriptDebuggerValuePrivate() |
74 | : type(QScriptDebuggerValue::NoValue) |
75 | { |
76 | } |
77 | |
78 | QScriptDebuggerValuePrivate::~QScriptDebuggerValuePrivate() |
79 | { |
80 | if (type == QScriptDebuggerValue::StringValue) |
81 | delete stringValue; |
82 | } |
83 | |
84 | QScriptDebuggerValue::QScriptDebuggerValue() |
85 | : d_ptr(0) |
86 | { |
87 | } |
88 | |
89 | QScriptDebuggerValue::QScriptDebuggerValue(const QScriptValue &value) |
90 | : d_ptr(0) |
91 | { |
92 | if (value.isValid()) { |
93 | d_ptr.reset(other: new QScriptDebuggerValuePrivate); |
94 | if (value.isUndefined()) |
95 | d_ptr->type = UndefinedValue; |
96 | else if (value.isNull()) |
97 | d_ptr->type = NullValue; |
98 | else if (value.isNumber()) { |
99 | d_ptr->type = NumberValue; |
100 | d_ptr->numberValue = value.toNumber(); |
101 | } else if (value.isBoolean()) { |
102 | d_ptr->type = BooleanValue; |
103 | d_ptr->booleanValue = value.toBoolean(); |
104 | } else if (value.isString()) { |
105 | d_ptr->type = StringValue; |
106 | d_ptr->stringValue = new QString(value.toString()); |
107 | } else { |
108 | Q_ASSERT(value.isObject()); |
109 | d_ptr->type = ObjectValue; |
110 | d_ptr->objectId = value.objectId(); |
111 | } |
112 | d_ptr->ref.ref(); |
113 | } |
114 | } |
115 | |
116 | QScriptDebuggerValue::QScriptDebuggerValue(double value) |
117 | : d_ptr(new QScriptDebuggerValuePrivate) |
118 | { |
119 | d_ptr->type = NumberValue; |
120 | d_ptr->numberValue = value; |
121 | d_ptr->ref.ref(); |
122 | } |
123 | |
124 | QScriptDebuggerValue::QScriptDebuggerValue(bool value) |
125 | : d_ptr(new QScriptDebuggerValuePrivate) |
126 | { |
127 | d_ptr->type = BooleanValue; |
128 | d_ptr->booleanValue = value; |
129 | d_ptr->ref.ref(); |
130 | } |
131 | |
132 | QScriptDebuggerValue::QScriptDebuggerValue(const QString &value) |
133 | : d_ptr(new QScriptDebuggerValuePrivate) |
134 | { |
135 | d_ptr->type = StringValue; |
136 | d_ptr->stringValue = new QString(value); |
137 | d_ptr->ref.ref(); |
138 | } |
139 | |
140 | QScriptDebuggerValue::QScriptDebuggerValue(qint64 objectId) |
141 | : d_ptr(new QScriptDebuggerValuePrivate) |
142 | { |
143 | d_ptr->type = ObjectValue; |
144 | d_ptr->objectId = objectId; |
145 | d_ptr->ref.ref(); |
146 | } |
147 | |
148 | QScriptDebuggerValue::QScriptDebuggerValue(ValueType type) |
149 | : d_ptr(new QScriptDebuggerValuePrivate) |
150 | { |
151 | d_ptr->type = type; |
152 | d_ptr->ref.ref(); |
153 | } |
154 | |
155 | QScriptDebuggerValue::QScriptDebuggerValue(const QScriptDebuggerValue &other) |
156 | : d_ptr(other.d_ptr.data()) |
157 | { |
158 | if (d_ptr) |
159 | d_ptr->ref.ref(); |
160 | } |
161 | |
162 | QScriptDebuggerValue::~QScriptDebuggerValue() |
163 | { |
164 | } |
165 | |
166 | QScriptDebuggerValue &QScriptDebuggerValue::operator=(const QScriptDebuggerValue &other) |
167 | { |
168 | d_ptr.assign(other: other.d_ptr.data()); |
169 | return *this; |
170 | } |
171 | |
172 | /*! |
173 | Returns the type of this value. |
174 | */ |
175 | QScriptDebuggerValue::ValueType QScriptDebuggerValue::type() const |
176 | { |
177 | Q_D(const QScriptDebuggerValue); |
178 | if (!d) |
179 | return NoValue; |
180 | return d->type; |
181 | } |
182 | |
183 | /*! |
184 | Returns this value as a number. |
185 | */ |
186 | double QScriptDebuggerValue::numberValue() const |
187 | { |
188 | Q_D(const QScriptDebuggerValue); |
189 | if (!d) |
190 | return 0; |
191 | Q_ASSERT(d->type == NumberValue); |
192 | return d->numberValue; |
193 | } |
194 | |
195 | /*! |
196 | Returns this value as a boolean. |
197 | */ |
198 | bool QScriptDebuggerValue::booleanValue() const |
199 | { |
200 | Q_D(const QScriptDebuggerValue); |
201 | if (!d) |
202 | return false; |
203 | Q_ASSERT(d->type == BooleanValue); |
204 | return d->booleanValue; |
205 | } |
206 | |
207 | /*! |
208 | Returns this value as a string. |
209 | */ |
210 | QString QScriptDebuggerValue::stringValue() const |
211 | { |
212 | Q_D(const QScriptDebuggerValue); |
213 | if (!d) |
214 | return QString(); |
215 | Q_ASSERT(d->type == StringValue); |
216 | return *d->stringValue; |
217 | } |
218 | |
219 | /*! |
220 | Returns this value as an object ID. |
221 | */ |
222 | qint64 QScriptDebuggerValue::objectId() const |
223 | { |
224 | Q_D(const QScriptDebuggerValue); |
225 | if (!d) |
226 | return -1; |
227 | Q_ASSERT(d->type == ObjectValue); |
228 | return d->objectId; |
229 | } |
230 | |
231 | /*! |
232 | Converts this QScriptDebuggerValue to a QScriptValue in the |
233 | given \a engine and returns the resulting value. |
234 | */ |
235 | QScriptValue QScriptDebuggerValue::toScriptValue(QScriptEngine *engine) const |
236 | { |
237 | Q_D(const QScriptDebuggerValue); |
238 | if (!d) |
239 | return QScriptValue(); |
240 | switch (d->type) { |
241 | case NoValue: |
242 | return QScriptValue(); |
243 | case UndefinedValue: |
244 | return engine->undefinedValue(); |
245 | case NullValue: |
246 | return engine->nullValue(); |
247 | case BooleanValue: |
248 | return QScriptValue(engine, d->booleanValue); |
249 | case StringValue: |
250 | return QScriptValue(engine, *d->stringValue); |
251 | case NumberValue: |
252 | return QScriptValue(engine, d->numberValue); |
253 | case ObjectValue: |
254 | return engine->objectById(id: d->objectId); |
255 | } |
256 | return QScriptValue(); |
257 | } |
258 | |
259 | /*! |
260 | Returns a string representation of this value. |
261 | */ |
262 | QString QScriptDebuggerValue::toString() const |
263 | { |
264 | Q_D(const QScriptDebuggerValue); |
265 | if (!d) |
266 | return QString(); |
267 | switch (d->type) { |
268 | case NoValue: |
269 | return QString(); |
270 | case UndefinedValue: |
271 | return QString::fromLatin1(str: "undefined" ); |
272 | case NullValue: |
273 | return QString::fromLatin1(str: "null" ); |
274 | case BooleanValue: |
275 | if (d->booleanValue) |
276 | return QString::fromLatin1(str: "true" ); |
277 | else |
278 | return QString::fromLatin1(str: "false" ); |
279 | case StringValue: |
280 | return *d->stringValue; |
281 | case NumberValue: |
282 | return QString::number(d->numberValue); // ### qScriptNumberToString() |
283 | case ObjectValue: |
284 | return QString::fromLatin1(str: "[object Object]" ); |
285 | } |
286 | return QString(); |
287 | } |
288 | |
289 | /*! |
290 | Returns true if this QScriptDebuggerValue is equal to the \a other |
291 | value, otherwise returns false. |
292 | */ |
293 | bool QScriptDebuggerValue::operator==(const QScriptDebuggerValue &other) const |
294 | { |
295 | Q_D(const QScriptDebuggerValue); |
296 | const QScriptDebuggerValuePrivate *od = other.d_func(); |
297 | if (d == od) |
298 | return true; |
299 | if (!d || !od) |
300 | return false; |
301 | if (d->type != od->type) |
302 | return false; |
303 | switch (d->type) { |
304 | case NoValue: |
305 | case UndefinedValue: |
306 | case NullValue: |
307 | return true; |
308 | case BooleanValue: |
309 | return d->booleanValue == od->booleanValue; |
310 | case StringValue: |
311 | return *d->stringValue == *od->stringValue; |
312 | case NumberValue: |
313 | return d->numberValue == od->numberValue; |
314 | case ObjectValue: |
315 | return d->objectId == od->objectId; |
316 | } |
317 | return false; |
318 | } |
319 | |
320 | /*! |
321 | Returns true if this QScriptDebuggerValue is not equal to the \a |
322 | other value, otherwise returns false. |
323 | */ |
324 | bool QScriptDebuggerValue::operator!=(const QScriptDebuggerValue &other) const |
325 | { |
326 | return !(*this == other); |
327 | } |
328 | |
329 | /*! |
330 | \relates QScriptDebuggerValue |
331 | |
332 | Writes the given \a value to the specified \a stream. |
333 | */ |
334 | QDataStream &operator<<(QDataStream &out, const QScriptDebuggerValue &value) |
335 | { |
336 | out << (quint32)value.type(); |
337 | switch (value.type()) { |
338 | case QScriptDebuggerValue::NoValue: |
339 | case QScriptDebuggerValue::UndefinedValue: |
340 | case QScriptDebuggerValue::NullValue: |
341 | break; |
342 | case QScriptDebuggerValue::BooleanValue: |
343 | out << value.booleanValue(); |
344 | break; |
345 | case QScriptDebuggerValue::StringValue: |
346 | out << value.stringValue(); |
347 | break; |
348 | case QScriptDebuggerValue::NumberValue: |
349 | out << value.numberValue(); |
350 | break; |
351 | case QScriptDebuggerValue::ObjectValue: |
352 | out << value.objectId(); |
353 | break; |
354 | } |
355 | return out; |
356 | } |
357 | |
358 | /*! |
359 | \relates QScriptDebuggerValue |
360 | |
361 | Reads a QScriptDebuggerValue from the specified \a stream into the |
362 | given \a value. |
363 | */ |
364 | QDataStream &operator>>(QDataStream &in, QScriptDebuggerValue &value) |
365 | { |
366 | quint32 type; |
367 | in >> type; |
368 | switch (QScriptDebuggerValue::ValueType(type)) { |
369 | case QScriptDebuggerValue::UndefinedValue: |
370 | case QScriptDebuggerValue::NullValue: |
371 | value = QScriptDebuggerValue(QScriptDebuggerValue::ValueType(type)); |
372 | break; |
373 | case QScriptDebuggerValue::BooleanValue: { |
374 | bool b; |
375 | in >> b; |
376 | value = QScriptDebuggerValue(b); |
377 | } break; |
378 | case QScriptDebuggerValue::StringValue: { |
379 | QString s; |
380 | in >> s; |
381 | value = QScriptDebuggerValue(s); |
382 | } break; |
383 | case QScriptDebuggerValue::NumberValue: { |
384 | double d; |
385 | in >> d; |
386 | value = QScriptDebuggerValue(d); |
387 | } break; |
388 | case QScriptDebuggerValue::ObjectValue: { |
389 | qint64 id; |
390 | in >> id; |
391 | value = QScriptDebuggerValue(id); |
392 | } break; |
393 | case QScriptDebuggerValue::NoValue: |
394 | default: |
395 | value = QScriptDebuggerValue(); |
396 | break; |
397 | } |
398 | return in; |
399 | } |
400 | |
401 | QT_END_NAMESPACE |
402 | |