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
(
isValid
());
19
}
20
21
void
IdentifierHash
::
addEntry
(
PropertyKey
identifier
,
int
value
)
22
{
23
Q_ASSERT
(
identifier
.
isStringOrSymbol
());
24
25
// fill up to max 50%
26
bool
grow
= (
d
->
alloc
<=
d
->
size
*2);
27
28
if
(
grow
) {
29
++
d
->
numBits
;
30
int
newAlloc
=
qPrimeForNumBits
(
d
->
numBits
);
31
IdentifierHashEntry
*
newEntries
= (
IdentifierHashEntry
*)
malloc
(
newAlloc
*
sizeof
(
IdentifierHashEntry
));
32
memset
(
newEntries
, 0,
newAlloc
*
sizeof
(
IdentifierHashEntry
));
33
for
(
int
i
= 0;
i
<
d
->
alloc
; ++
i
) {
34
const
IdentifierHashEntry
&
e
=
d
->
entries
[
i
];
35
if
(!
e
.
identifier
.
isValid
())
36
continue
;
37
uint
idx
=
e
.
identifier
.
id
() %
newAlloc
;
38
while
(
newEntries
[
idx
].
identifier
.
isValid
()) {
39
++
idx
;
40
idx
%=
newAlloc
;
41
}
42
newEntries
[
idx
] =
e
;
43
}
44
free
(
d
->
entries
);
45
d
->
entries
=
newEntries
;
46
d
->
alloc
=
newAlloc
;
47
}
48
49
uint
idx
=
identifier
.
id
() %
d
->
alloc
;
50
while
(
d
->
entries
[
idx
].
identifier
.
isValid
()) {
51
Q_ASSERT
(
d
->
entries
[
idx
].
identifier
!=
identifier
);
52
++
idx
;
53
idx
%=
d
->
alloc
;
54
}
55
d
->
entries
[
idx
].
identifier
=
identifier
;
56
++
d
->
size
;
57
(
d
->
entries
+
idx
)->
value
=
value
;
58
}
59
60
int
IdentifierHash
::
lookup
(
PropertyKey
identifier
)
const
61
{
62
if
(!
d
|| !
identifier
.
isStringOrSymbol
())
63
return
-1;
64
Q_ASSERT
(
d
->
entries
);
65
66
uint
idx
=
identifier
.
id
() %
d
->
alloc
;
67
while
(1) {
68
if
(!
d
->
entries
[
idx
].
identifier
.
isValid
())
69
return
-1;
70
if
(
d
->
entries
[
idx
].
identifier
==
identifier
)
71
return
(
d
->
entries
+
idx
)->
value
;
72
++
idx
;
73
idx
%=
d
->
alloc
;
74
}
75
}
76
77
PropertyKey
IdentifierHash
::
toIdentifier
(
const
QString
&
str
)
const
78
{
79
Q_ASSERT
(
d
);
80
return
d
->
identifierTable
->
asPropertyKey
(
str
,
IdentifierTable
::
ForceConversionToId
);
81
}
82
83
PropertyKey
IdentifierHash
::
toIdentifier
(
Heap
::
String
*
str
)
const
84
{
85
Q_ASSERT
(
d
);
86
return
d
->
identifierTable
->
asPropertyKey
(
str
);
87
}
88
89
PropertyKey
IdentifierHash
::
reverseLookup
(
int
value
)
const
90
{
91
IdentifierHashEntry
*
e
=
d
->
entries
;
92
IdentifierHashEntry
*
end
=
e
+
d
->
alloc
;
93
while
(
e
<
end
) {
94
if
(
e
->
identifier
.
isValid
() &&
e
->
value
==
value
)
95
return
e
->
identifier
;
96
++
e
;
97
}
98
return
PropertyKey
::
invalid
();
99
}
100
101
template
<>
102
QString
IdentifierHash
::
toString
<
QString
>(
PropertyKey
key
)
const
103
{
104
return
key
.
isValid
() ?
key
.
toQString
() :
QString
();
105
}
106
107
template
<>
108
Heap
::
String
*
IdentifierHash
::
toString
<
Heap
::
String
*>(
PropertyKey
key
)
const
109
{
110
return
key
.
isString
() ?
static_cast
<
Heap
::
String
*>(
key
.
asStringOrSymbol
()) :
nullptr
;
111
}
112
113
QV4
::
IdentifierHash
::
IdentifierHash
(
const
IdentifierHash
&
other
)
114
{
115
d
=
other
.
d
;
116
if
(
d
)
117
d
->
refCount
.
ref
();
118
}
119
120
QV4
::
IdentifierHash
::~
IdentifierHash
()
121
{
122
if
(
d
&& !
d
->
refCount
.
deref
())
123
delete
d
;
124
}
125
126
IdentifierHash
&
QV4
::
IdentifierHash
::
operator
=(
const
IdentifierHash
&
other
)
127
{
128
if
(
other
.
d
)
129
other
.
d
->
refCount
.
ref
();
130
if
(
d
&& !
d
->
refCount
.
deref
())
131
delete
d
;
132
d
=
other
.
d
;
133
return
*
this
;
134
}
135
136
int
IdentifierHash
::
size
()
const
137
{
138
return
d
?
d
->
size
: 0;
139
}
140
141
}
// namespace QV4
142
143
QT_END_NAMESPACE
QV4
Definition
qjsvalue.h:24
qtdeclarative
src
qml
jsruntime
qv4identifierhash.cpp
Generated on
for Qt by
1.16.1