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
qv4identifierhash.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 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
5
#
include
<
private
/
qv4identifierhash_p
.
h
>
6
#
include
<
private
/
qv4identifiertable_p
.
h
>
7
#
include
<
private
/
qv4string_p
.
h
>
8
#
include
<
private
/
qv4identifierhashdata_p
.
h
>
9
#
include
<
private
/
qprimefornumbits_p
.
h
>
10
11
QT_BEGIN_NAMESPACE
12
13
namespace
QV4
{
14
15
IdentifierHash
::
IdentifierHash
(
ExecutionEngine
*
engine
)
16
{
17
d
=
new
IdentifierHashData
(
engine
->
identifierTable
, 3);
18
Q_ASSERT
(!
isEmpty
());
19
}
20
21
void
IdentifierHash
::
detach
()
22
{
23
if
(!
d
||
d
->
refCount
.
loadAcquire
() == 1)
24
return
;
25
IdentifierHashData
*
newData
=
new
IdentifierHashData
(
d
);
26
if
(
d
&& !
d
->
refCount
.
deref
())
27
delete
d
;
28
d
=
newData
;
29
}
30
31
inline
32
IdentifierHashEntry
*
IdentifierHash
::
addEntry
(
PropertyKey
identifier
)
33
{
34
Q_ASSERT
(
identifier
.
isStringOrSymbol
());
35
36
// fill up to max 50%
37
bool
grow
= (
d
->
alloc
<=
d
->
size
*2);
38
39
if
(
grow
) {
40
++
d
->
numBits
;
41
int
newAlloc
=
qPrimeForNumBits
(
d
->
numBits
);
42
IdentifierHashEntry
*
newEntries
= (
IdentifierHashEntry
*)
malloc
(
newAlloc
*
sizeof
(
IdentifierHashEntry
));
43
memset
(
newEntries
, 0,
newAlloc
*
sizeof
(
IdentifierHashEntry
));
44
for
(
int
i
= 0;
i
<
d
->
alloc
; ++
i
) {
45
const
IdentifierHashEntry
&
e
=
d
->
entries
[
i
];
46
if
(!
e
.
identifier
.
isValid
())
47
continue
;
48
uint
idx
=
e
.
identifier
.
id
() %
newAlloc
;
49
while
(
newEntries
[
idx
].
identifier
.
isValid
()) {
50
++
idx
;
51
idx
%=
newAlloc
;
52
}
53
newEntries
[
idx
] =
e
;
54
}
55
free
(
d
->
entries
);
56
d
->
entries
=
newEntries
;
57
d
->
alloc
=
newAlloc
;
58
}
59
60
uint
idx
=
identifier
.
id
() %
d
->
alloc
;
61
while
(
d
->
entries
[
idx
].
identifier
.
isValid
()) {
62
Q_ASSERT
(
d
->
entries
[
idx
].
identifier
!=
identifier
);
63
++
idx
;
64
idx
%=
d
->
alloc
;
65
}
66
d
->
entries
[
idx
].
identifier
=
identifier
;
67
++
d
->
size
;
68
return
d
->
entries
+
idx
;
69
}
70
71
inline
72
const
IdentifierHashEntry
*
IdentifierHash
::
lookup
(
PropertyKey
identifier
)
const
73
{
74
if
(!
d
|| !
identifier
.
isStringOrSymbol
())
75
return
nullptr
;
76
Q_ASSERT
(
d
->
entries
);
77
78
uint
idx
=
identifier
.
id
() %
d
->
alloc
;
79
while
(1) {
80
if
(!
d
->
entries
[
idx
].
identifier
.
isValid
())
81
return
nullptr
;
82
if
(
d
->
entries
[
idx
].
identifier
==
identifier
)
83
return
d
->
entries
+
idx
;
84
++
idx
;
85
idx
%=
d
->
alloc
;
86
}
87
}
88
89
inline
90
const
IdentifierHashEntry
*
IdentifierHash
::
lookup
(
const
QString
&
str
)
const
91
{
92
if
(!
d
)
93
return
nullptr
;
94
95
PropertyKey
id
=
d
->
identifierTable
->
asPropertyKey
(
str
,
IdentifierTable
::
ForceConversionToId
);
96
return
lookup
(
id
);
97
}
98
99
inline
100
const
IdentifierHashEntry
*
IdentifierHash
::
lookup
(
String
*
str
)
const
101
{
102
if
(!
d
)
103
return
nullptr
;
104
PropertyKey
id
=
d
->
identifierTable
->
asPropertyKey
(
str
);
105
if
(
id
.
isValid
())
106
return
lookup
(
id
);
107
return
lookup
(
str
->
toQString
());
108
}
109
110
inline
111
const
PropertyKey
IdentifierHash
::
toIdentifier
(
const
QString
&
str
)
const
112
{
113
Q_ASSERT
(
d
);
114
return
d
->
identifierTable
->
asPropertyKey
(
str
,
IdentifierTable
::
ForceConversionToId
);
115
}
116
117
inline
118
const
PropertyKey
IdentifierHash
::
toIdentifier
(
Heap
::
String
*
str
)
const
119
{
120
Q_ASSERT
(
d
);
121
return
d
->
identifierTable
->
asPropertyKey
(
str
);
122
}
123
124
QString
QV4
::
IdentifierHash
::
findId
(
int
value
)
const
125
{
126
IdentifierHashEntry
*
e
=
d
->
entries
;
127
IdentifierHashEntry
*
end
=
e
+
d
->
alloc
;
128
while
(
e
<
end
) {
129
if
(
e
->
identifier
.
isValid
() &&
e
->
value
==
value
)
130
return
e
->
identifier
.
toQString
();
131
++
e
;
132
}
133
return
QString
();
134
}
135
136
QV4
::
IdentifierHash
::
IdentifierHash
(
const
IdentifierHash
&
other
)
137
{
138
d
=
other
.
d
;
139
if
(
d
)
140
d
->
refCount
.
ref
();
141
}
142
143
QV4
::
IdentifierHash
::~
IdentifierHash
()
144
{
145
if
(
d
&& !
d
->
refCount
.
deref
())
146
delete
d
;
147
}
148
149
IdentifierHash
&
QV4
::
IdentifierHash
::
operator
=(
const
IdentifierHash
&
other
)
150
{
151
if
(
other
.
d
)
152
other
.
d
->
refCount
.
ref
();
153
if
(
d
&& !
d
->
refCount
.
deref
())
154
delete
d
;
155
d
=
other
.
d
;
156
return
*
this
;
157
}
158
159
int
QV4
::
IdentifierHash
::
count
()
const
160
{
161
return
d
?
d
->
size
: 0;
162
}
163
164
void
QV4
::
IdentifierHash
::
add
(
const
QString
&
str
,
int
value
)
165
{
166
IdentifierHashEntry
*
e
=
addEntry
(
toIdentifier
(
str
));
167
e
->
value
=
value
;
168
}
169
170
void
QV4
::
IdentifierHash
::
add
(
Heap
::
String
*
str
,
int
value
)
171
{
172
IdentifierHashEntry
*
e
=
addEntry
(
toIdentifier
(
str
));
173
e
->
value
=
value
;
174
}
175
176
int
QV4
::
IdentifierHash
::
value
(
const
QString
&
str
)
const
177
{
178
const
IdentifierHashEntry
*
e
=
lookup
(
str
);
179
return
e
?
e
->
value
: -1;
180
}
181
182
int
QV4
::
IdentifierHash
::
value
(
String
*
str
)
const
183
{
184
const
IdentifierHashEntry
*
e
=
lookup
(
str
);
185
return
e
?
e
->
value
: -1;
186
}
187
188
189
}
190
191
QT_END_NAMESPACE
QV4
Definition
qjsvalue.h:23
qtdeclarative
src
qml
jsruntime
qv4identifierhash.cpp
Generated on
for Qt by
1.14.0