FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
WebKit/Source/JavaScriptCore/create_hash_table at main · WebKit/WebKit · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
WebKit
/
WebKit
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Pull requests
2.6k
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
create_hash_table
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
444 lines (387 loc) · 13.8 KB
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
create_hash_table
Copy path
File metadata and controls
executable file
·
444 lines (387 loc) · 13.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#
! /usr/bin/env perl
#
#
Static Hashtable Generator
#
#
(c) 2000-2002 by Harri Porten <porten@kde.org> and
#
David Faure <faure@kde.org>
#
Modified (c) 2004 by Nikolas Zimmermann <wildfox@kde.org>
#
Copyright (C) 2007-2023 Apple Inc. All rights reserved.
#
#
This library is free software; you can redistribute it and/or
#
modify it under the terms of the GNU Lesser General Public
#
License as published by the Free Software Foundation; either
#
version 2 of the License, or (at your option) any later version.
#
#
This library is distributed in the hope that it will be useful,
#
but WITHOUT ANY WARRANTY; without even the implied warranty of
#
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#
Lesser General Public License for more details.
#
#
You should have received a copy of the GNU Lesser General Public
#
License along with this library; if not, write to the Free Software
#
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
use
strict;
use
warnings;
use
bigint;
use
Getopt::Long
qw(
:config pass_through
)
;
my
$file
=
shift
@ARGV
or
die
(
"
Must provide source file as final argument.
"
);
open
(IN,
$file
)
or
die
"
No such file
$file
"
;
my
@keys
= ();
my
@attrs
= ();
my
@values
= ();
my
@table
= ();
my
@links
= ();
my
$hasSetter
=
"
false
"
;
my
$includeBuiltin
= 0;
my
$inside
= 0;
my
$name
;
my
$perfectHashSize
;
my
$compactSize
;
my
$compactHashSizeMask
;
my
$banner
= 0;
my
$mask64
= 2**64 - 1;
my
$mask32
= 2**32 - 1;
sub
calcPerfectHashSize
();
sub
calcCompactHashSize
();
sub
output
();
sub
jsc_ucfirst
($);
sub
hashValue
($);
while
(<IN>) {
chomp
;
s
/
^
\s
+
//
;
next
if
/
^
\#
|^
$
/
;
#
Comment or blank line. Do nothing.
if
(!
$inside
&&
/
^
\@
begin
/
) {
if
(
/
^
\@
begin
\s
*([:_
\w
]+)
\s
*
\d
*
\s
*
$
/
) {
$inside
= 1;
$name
=
$1
;
}
else
{
print
STDERR
"
WARNING:
\@
begin without table name, skipping
$_
\n
"
;
}
}
elsif
(
$inside
&&
/
^
\@
end
\s
*
$
/
) {
output();
@keys
= ();
@attrs
= ();
@values
= ();
$includeBuiltin
= 0;
$inside
= 0;
}
elsif
(
$inside
&&
/
^(
\S
+)
\s
*(
\S
+)
\s
*([
\w\|
]*)
\s
*(
\w
*)
\s
*(
\w
*)
\s
*
$
/
) {
my
$key
=
$1
;
my
$val
=
$2
;
my
$att
=
$3
;
my
$param
=
$4
;
my
$intrinsic
=
$5
;
push
(
@keys
,
$key
);
push
(
@attrs
,
length
(
$att
) > 0 ?
$att
:
"
None
"
);
if
(
$val
eq
"
JSBuiltin
"
) {
$includeBuiltin
= 1;
}
if
(
$att
=~
m
/
Function
/
) {
push
(
@values
, {
"
type
"
=>
"
PropertyAttribute::Function
"
,
"
function
"
=>
$val
,
"
params
"
=>
(
length
(
$param
) ?
$param
:
"
"
),
"
intrinsic
"
=>
(
length
(
$intrinsic
) ?
$intrinsic
:
"
NoIntrinsic
"
) });
#
printf STDERR "WARNING: Number of arguments missing for $key/$val\n" if (length($param) == 0);
}
elsif
(
$att
=~
m
/
CellProperty
/
) {
my
$property
=
$val
;
push
(
@values
, {
"
type
"
=>
"
PropertyAttribute::CellProperty
"
,
"
property
"
=>
$property
});
}
elsif
(
$att
=~
m
/
ClassStructure
/
) {
my
$property
=
$val
;
push
(
@values
, {
"
type
"
=>
"
PropertyAttribute::ClassStructure
"
,
"
property
"
=>
$property
});
}
elsif
(
$att
=~
m
/
PropertyCallback
/
) {
my
$cback
=
$val
;
push
(
@values
, {
"
type
"
=>
"
PropertyAttribute::PropertyCallback
"
,
"
cback
"
=>
$cback
});
}
elsif
(
length
(
$att
)) {
my
$get
=
$val
;
my
$put
=
"
0
"
;
if
(!(
$att
=~
m
/
ReadOnly
/
)) {
$put
=
"
set
"
. jsc_ucfirst(
$val
);
}
$hasSetter
=
"
true
"
;
push
(
@values
, {
"
type
"
=>
"
PropertyAttribute::Property
"
,
"
get
"
=>
$get
,
"
put
"
=>
$put
});
}
else
{
push
(
@values
, {
"
type
"
=>
"
Lexer
"
,
"
value
"
=>
$val
});
}
}
elsif
(
$inside
) {
die
"
invalid data {
"
.
$_
.
"
}
"
;
}
}
die
"
missing closing
\@
end
"
if
(
$inside
);
sub
jsc_ucfirst
($)
{
my
(
$value
) =
@_
;
if
(
$value
=~
/
js
/
) {
$value
=~
s
/
js
/
JS
/
;
return
$value
;
}
return
ucfirst
(
$value
);
}
sub
ceilingToPowerOf2
{
my
(
$perfectHashSize
) =
@_
;
my
$powerOf2
= 1;
while
(
$perfectHashSize
>
$powerOf2
) {
$powerOf2
<<= 1;
}
return
$powerOf2
;
}
sub
calcPerfectHashSize
()
{
tableSizeLoop:
for
(
$perfectHashSize
= ceilingToPowerOf2(
scalar
@keys
); ;
$perfectHashSize
+=
$perfectHashSize
) {
if
(
$perfectHashSize
> 2**15) {
die
"
The hash size is far too big. This should not be reached.
"
;
}
my
@table
= ();
foreach
my
$key
(
@keys
) {
my
$h
= hashValue(
$key
) %
$perfectHashSize
;
next
tableSizeLoop
if
$table
[
$h
];
$table
[
$h
] = 1;
}
last
;
}
}
sub
calcCompactHashSize
()
{
my
$compactHashSize
= ceilingToPowerOf2(2 *
@keys
);
$compactHashSizeMask
=
$compactHashSize
- 1;
$compactSize
=
$compactHashSize
;
my
$collisions
= 0;
my
$maxdepth
= 0;
my
$i
= 0;
foreach
my
$key
(
@keys
) {
my
$depth
= 0;
my
$h
= hashValue(
$key
) %
$compactHashSize
;
while
(
defined
(
$table
[
$h
])) {
if
(
$compactSize
> 1000) {
die
"
The hash size is far too big. This should not be reached.
"
;
}
if
(
$depth
> 100) {
die
"
The depth is far too big. This should not be reached.
"
;
}
if
(
defined
(
$links
[
$h
])) {
$h
=
$links
[
$h
];
$depth
++;
}
else
{
$collisions
++;
$links
[
$h
] =
$compactSize
;
$h
=
$compactSize
;
$compactSize
++;
}
}
$table
[
$h
] =
$i
;
$i
++;
$maxdepth
=
$depth
if
(
$depth
>
$maxdepth
);
}
}
sub
maskTop8BitsAndAvoidZero
($) {
my
(
$value
) =
@_
;
$value
&=
$mask32
;
#
Save 8 bits for StringImpl to use as flags.
$value
&= 0xffffff;
#
This avoids ever returning a hash code of 0, since that is used to
#
signal "hash not computed yet". Setting the high bit maintains
#
reasonable fidelity to a hash code of 0 because it is likely to yield
#
exactly 0 when hash lookup masks out the high bits.
$value
= (0x80000000 >> 8)
if
(
$value
== 0);
return
$value
;
}
sub
uint64_add
($$) {
my
(
$a
,
$b
) =
@_
;
my
$sum
=
$a
+
$b
;
return
$sum
&
$mask64
;
}
sub
uint64_multi
($$) {
my
(
$a
,
$b
) =
@_
;
my
$product
=
$a
*
$b
;
return
$product
&
$mask64
;
}
sub
rapid_mul128
($$) {
my
(
$A
,
$B
) =
@_
;
my
$ha
=
$A
>> 32;
my
$hb
=
$B
>> 32;
my
$la
=
$A
&
$mask32
;
my
$lb
=
$B
&
$mask32
;
my
$hi
;
my
$lo
;
my
$rh
= uint64_multi(
$ha
,
$hb
);
my
$rm0
= uint64_multi(
$ha
,
$lb
);
my
$rm1
= uint64_multi(
$hb
,
$la
);
my
$rl
= uint64_multi(
$la
,
$lb
);
my
$t
= uint64_add(
$rl
, (
$rm0
<< 32));
my
$c
=
int
(
$t
<
$rl
);
$lo
= uint64_add(
$t
, (
$rm1
<< 32));
$c
+=
int
(
$lo
<
$t
);
$hi
= uint64_add(
$rh
, uint64_add((
$rm0
>> 32), uint64_add((
$rm1
>> 32),
$c
)));
return
(
$lo
,
$hi
);
};
sub
rapid_mix
($$) {
my
(
$A
,
$B
) =
@_
;
(
$A
,
$B
) = rapid_mul128(
$A
,
$B
);
return
$A
^
$B
;
}
sub
rapidhash
{
#
https://github.com/Nicoshev/rapidhash
#
Hashes raw ASCII bytes (1 byte per character).
my
@chars
=
@_
;
my
$len
=
scalar
@chars
;
my
@secret
= ( 3257665815644502181, 10067880064238660809, 5418857496715711651 );
my
$seed
= rapid_mix(0 ^
$secret
[0],
$secret
[1]) ^
$len
;
my
$a
= 0;
my
$b
= 0;
local
*read64 =
sub
{
my
(
$i
) =
@_
;
return
ord
(
$chars
[
$i
])
| (
ord
(
$chars
[
$i
+ 1]) << 8)
| (
ord
(
$chars
[
$i
+ 2]) << 16)
| (
ord
(
$chars
[
$i
+ 3]) << 24)
| (
ord
(
$chars
[
$i
+ 4]) << 32)
| (
ord
(
$chars
[
$i
+ 5]) << 40)
| (
ord
(
$chars
[
$i
+ 6]) << 48)
| (
ord
(
$chars
[
$i
+ 7]) << 56);
};
local
*read32 =
sub
{
my
(
$i
) =
@_
;
return
ord
(
$chars
[
$i
])
| (
ord
(
$chars
[
$i
+ 1]) << 8)
| (
ord
(
$chars
[
$i
+ 2]) << 16)
| (
ord
(
$chars
[
$i
+ 3]) << 24);
};
local
*readSmall =
sub
{
my
(
$i
,
$k
) =
@_
;
return
(
ord
(
$chars
[
$i
]) << 56)
| (
ord
(
$chars
[
$i
+ (
$k
>> 1)]) << 32)
|
ord
(
$chars
[
$i
+
$k
- 1]);
};
if
(
$len
<= 16) {
if
(
$len
>= 4) {
my
$delta
= (
$len
>= 8) ? 4 : 0;
$a
= (read32(0) << 32) | read32(
$len
- 4);
$b
= (read32(
$delta
) << 32) | read32(
$len
- 4 -
$delta
);
}
elsif
(
$len
> 0) {
$a
= readSmall(0,
$len
);
$b
= 0;
}
else
{
$a
=
$b
= 0;
}
}
else
{
my
$i
=
$len
;
my
$off
= 0;
if
(
$i
> 48) {
my
$see1
=
$seed
;
my
$see2
=
$seed
;
do
{
$seed
= rapid_mix(read64(
$off
) ^
$secret
[0], read64(
$off
+ 8) ^
$seed
);
$see1
= rapid_mix(read64(
$off
+ 16) ^
$secret
[1], read64(
$off
+ 24) ^
$see1
);
$see2
= rapid_mix(read64(
$off
+ 32) ^
$secret
[2], read64(
$off
+ 40) ^
$see2
);
$off
+= 48;
$i
-= 48;
}
while
(
$i
>= 48);
$seed
^=
$see1
^
$see2
;
}
if
(
$i
> 16) {
$seed
= rapid_mix(read64(
$off
) ^
$secret
[2], read64(
$off
+ 8) ^
$seed
^
$secret
[1]);
if
(
$i
> 32) {
$seed
= rapid_mix(read64(
$off
+ 16) ^
$secret
[2], read64(
$off
+ 24) ^
$seed
);
}
}
$a
= read64(
$off
+
$i
- 16);
$b
= read64(
$off
+
$i
- 8);
}
$a
^=
$secret
[1];
$b
^=
$seed
;
(
$a
,
$b
) = rapid_mul128(
$a
,
$b
);
my
$hash
= rapid_mix(
$a
^
$secret
[0] ^
$len
,
$b
^
$secret
[1]) &
$mask32
;
return
maskTop8BitsAndAvoidZero(
$hash
);
}
sub
hashValue
($) {
my
(
$string
) =
@_
;
my
@chars
=
split
(
/
*
/
,
$string
);
return
rapidhash(
@chars
);
}
sub
output
() {
if
(!
$banner
) {
$banner
= 1;
my
(
$srcName
) =
$file
=~
m
|
([^/]+)
$
|
;
my
(
$selfName
) =
$0
=~
m
|
([^/]+)
$
|
;
print
"
// Automatically generated from
$srcName
using
$selfName
. DO NOT EDIT!
\n
"
;
}
my
$nameEntries
=
"
${name}
Values
"
;
$nameEntries
=~
s
/
:
/
_
/
g
;
my
$nameIndex
=
"
${name}
Index
"
;
$nameIndex
=~
s
/
:
/
_
/
g
;
print
"
\n
"
;
print
"
#include
\"
JSCBuiltins.h
\"\n
"
if
(
$includeBuiltin
);
print
"
#include
\"
Lookup.h
\"\n
"
;
print
"
\n
"
;
print
"
namespace JSC {
\n
"
;
print
"
\n
"
;
local
*generateHashTableHelper =
sub
{
calcCompactHashSize();
my
$hashTableString
=
"
"
;
$hashTableString
.=
"
static constinit const struct CompactHashIndex
${nameIndex}
\[
$compactSize
\]
= {
\n
"
;
for
(
my
$i
= 0;
$i
<
$compactSize
;
$i
++) {
my
$T
= -1;
if
(
defined
(
$table
[
$i
])) {
$T
=
$table
[
$i
]; }
my
$L
= -1;
if
(
defined
(
$links
[
$i
])) {
$L
=
$links
[
$i
]; }
$hashTableString
.=
"
{
$T
,
$L
},
\n
"
;
}
$hashTableString
.=
"
};
\n
"
;
$hashTableString
.=
"
\n
"
;
my
$packedSize
=
scalar
@keys
;
$hashTableString
.=
"
static constinit const struct HashTableValue
${nameEntries}
\[
$packedSize
\]
= {
\n
"
;
my
$i
= 0;
foreach
my
$key
(
@keys
) {
my
$typeTag
=
"
"
;
my
$firstValue
=
"
"
;
my
$secondValue
=
"
"
;
my
$hasSecondValue
= 1;
my
$intrinsic
=
"
NoIntrinsic
"
;
if
(
$values
[
$i
]{
"
type
"
}
eq
"
PropertyAttribute::Function
"
) {
$typeTag
=
"
NativeFunction
"
;
$firstValue
=
$values
[
$i
]{
"
function
"
};
$secondValue
=
$values
[
$i
]{
"
params
"
};
$intrinsic
=
$values
[
$i
]{
"
intrinsic
"
};
}
elsif
(
$values
[
$i
]{
"
type
"
}
eq
"
PropertyAttribute::Property
"
) {
$typeTag
=
"
GetterSetter
"
;
$firstValue
=
$values
[
$i
]{
"
get
"
};
$secondValue
=
$values
[
$i
]{
"
put
"
};
}
elsif
(
$values
[
$i
]{
"
type
"
}
eq
"
Lexer
"
) {
$typeTag
=
"
Lexer
"
;
$firstValue
=
$values
[
$i
]{
"
value
"
};
$hasSecondValue
= 0;
}
elsif
(
$values
[
$i
]{
"
type
"
}
eq
"
PropertyAttribute::CellProperty
"
||
$values
[
$i
]{
"
type
"
}
eq
"
PropertyAttribute::ClassStructure
"
) {
$typeTag
= (
$values
[
$i
]{
"
type
"
}
eq
"
PropertyAttribute::CellProperty
"
) ?
"
LazyCellProperty
"
:
"
LazyClassStructure
"
;
$values
[
$i
]{
"
property
"
} =~
/
\A
([a-zA-Z0-9_]+)::(.*)
\Z
/
or
die
;
$firstValue
=
"
OBJECT_OFFSETOF(
$1
,
$2
)
"
;
$hasSecondValue
= 0;
}
elsif
(
$values
[
$i
]{
"
type
"
}
eq
"
PropertyAttribute::PropertyCallback
"
) {
$typeTag
=
"
LazyProperty
"
;
$firstValue
=
$values
[
$i
]{
"
cback
"
};
$hasSecondValue
= 0;
}
my
$attributes
=
"
PropertyAttribute::
"
.
$attrs
[
$i
];
$attributes
=~
s
/
\|
/
\|
PropertyAttribute::
/
g
;
$attributes
=
"
static_cast<unsigned>(
"
.
$attributes
.
"
)
"
;
if
(
$values
[
$i
]{
"
type
"
}
eq
"
PropertyAttribute::Function
"
&&
$firstValue
eq
"
JSBuiltin
"
) {
$typeTag
=
"
BuiltinGenerator
"
;
my
$tableHead
=
$name
;
$tableHead
=~
s
/
Table$
//
;
$hashTableString
.=
"
{
\"
$key
\"
_s, ((
$attributes
) & ~PropertyAttribute::Function) | PropertyAttribute::Builtin,
$intrinsic
, { HashTableValue::
"
.
$typeTag
.
"
Type,
"
.
$tableHead
.
ucfirst
(
$key
) .
"
CodeGenerator,
$secondValue
} },
\n
"
;
}
else
{
$hashTableString
.=
"
{
\"
$key
\"
_s,
$attributes
,
$intrinsic
, { HashTableValue::
"
.
$typeTag
.
"
Type,
$firstValue
"
. (
$hasSecondValue
?
"
,
"
.
$secondValue
:
"
"
) .
"
} },
\n
"
;
}
$i
++;
}
$hashTableString
.=
"
};
\n
"
;
$hashTableString
.=
"
\n
"
;
$hashTableString
.=
"
static constinit const struct HashTable
$name
=
\n
"
;
$hashTableString
.=
"
\{
$packedSize
,
$compactHashSizeMask
,
$hasSetter
, nullptr,
$nameEntries
,
$nameIndex
\}
;
\n
"
;
$hashTableString
.=
"
\n
"
;
@table
= ();
@links
= ();
return
$hashTableString
;
};
my
$hashTableToWrite
= generateHashTableHelper();
print
$hashTableToWrite
;
print
"
} // namespace JSC
\n
"
;
}
Back
|
FazBrowse Home
|
New Git URL