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 "qscriptobjectsnapshot_p.h"
41
42#include <QtCore/qmap.h>
43#include <QtCore/qset.h>
44#include <QtCore/qvector.h>
45#include <QtCore/qnumeric.h>
46#include <QtScript/qscriptvalueiterator.h>
47
48QT_BEGIN_NAMESPACE
49
50QScriptObjectSnapshot::QScriptObjectSnapshot()
51{
52}
53
54QScriptObjectSnapshot::~QScriptObjectSnapshot()
55{
56}
57
58static bool _q_equal(const QScriptValue &v1, const QScriptValue &v2)
59{
60 if (v1.strictlyEquals(other: v2))
61 return true;
62 if (v1.isNumber() && v2.isNumber() && qIsNaN(d: v1.toNumber()) && qIsNaN(d: v2.toNumber()))
63 return true;
64 return false;
65}
66
67QScriptObjectSnapshot::Delta QScriptObjectSnapshot::capture(const QScriptValue &object)
68{
69 Delta result;
70 QMap<QString, QScriptValueProperty> currProps;
71 QHash<QString, int> propertyNameToIndex;
72 {
73 int i = 0;
74 QScriptValueIterator it(object);
75 while (it.hasNext()) {
76 it.next();
77 QScriptValueProperty prop(it.name(), it.value(), it.flags());
78 currProps.insert(akey: it.name(), avalue: prop);
79 propertyNameToIndex.insert(akey: it.name(), avalue: i);
80 ++i;
81 }
82 if (object.prototype().isValid()) {
83 QString __proto__ = QString::fromLatin1(str: "__proto__");
84 QScriptValueProperty protoProp(
85 __proto__, object.prototype(),
86 QScriptValue::Undeletable | QScriptValue::ReadOnly);
87 currProps.insert(akey: __proto__, avalue: protoProp);
88 propertyNameToIndex.insert(akey: __proto__, avalue: i);
89 ++i;
90 }
91 }
92
93 QSet<QString> prevSet;
94 for (int i = 0; i < m_properties.size(); ++i)
95 prevSet.insert(value: m_properties.at(i).name());
96 QSet<QString> currSet = currProps.keys().toSet();
97 QSet<QString> removedProperties = prevSet - currSet;
98 QSet<QString> addedProperties = currSet - prevSet;
99 QSet<QString> maybeChangedProperties = currSet & prevSet;
100
101 {
102 QMap<int, QScriptValueProperty> am;
103 QSet<QString>::const_iterator it;
104 for (it = addedProperties.constBegin(); it != addedProperties.constEnd(); ++it) {
105 int idx = propertyNameToIndex[*it];
106 am[idx] = currProps[*it];
107 }
108 result.addedProperties = am.values();
109 }
110
111 {
112 QSet<QString>::const_iterator it;
113 for (it = maybeChangedProperties.constBegin(); it != maybeChangedProperties.constEnd(); ++it) {
114 const QScriptValueProperty &p1 = currProps[*it];
115 const QScriptValueProperty &p2 = findProperty(name: *it);
116 if (!_q_equal(v1: p1.value(), v2: p2.value())
117 || (p1.flags() != p2.flags())) {
118 result.changedProperties.append(t: p1);
119 }
120 }
121 }
122
123 result.removedProperties = removedProperties.toList();
124
125 m_properties = currProps.values();
126
127 return result;
128}
129
130QScriptValueProperty QScriptObjectSnapshot::findProperty(const QString &name) const
131{
132 for (int i = 0; i < m_properties.size(); ++i) {
133 if (m_properties.at(i).name() == name)
134 return m_properties.at(i);
135 }
136 return QScriptValueProperty();
137}
138
139QScriptValuePropertyList QScriptObjectSnapshot::properties() const
140{
141 return m_properties;
142}
143
144QT_END_NAMESPACE
145

source code of qtscript/src/scripttools/debugging/qscriptobjectsnapshot.cpp