OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 var $getHash; |
| 6 var $getExistingHash; |
| 7 |
5 (function(global, utils) { | 8 (function(global, utils) { |
6 | |
7 "use strict"; | 9 "use strict"; |
8 | 10 |
9 %CheckIsBootstrapping(); | 11 %CheckIsBootstrapping(); |
10 | 12 |
11 // ------------------------------------------------------------------- | 13 // ------------------------------------------------------------------- |
12 // Imports | 14 // Imports |
13 | 15 |
14 var GlobalMap = global.Map; | 16 var GlobalMap = global.Map; |
15 var GlobalObject = global.Object; | 17 var GlobalObject = global.Object; |
16 var GlobalSet = global.Set; | 18 var GlobalSet = global.Set; |
| 19 var IntRandom; |
| 20 |
| 21 utils.Import(function(from) { |
| 22 IntRandom = from.IntRandom; |
| 23 }); |
17 | 24 |
18 var NumberIsNaN; | 25 var NumberIsNaN; |
19 | 26 |
20 utils.Import(function(from) { | 27 utils.Import(function(from) { |
21 NumberIsNaN = from.NumberIsNaN; | 28 NumberIsNaN = from.NumberIsNaN; |
22 }); | 29 }); |
23 | 30 |
24 // ------------------------------------------------------------------- | 31 // ------------------------------------------------------------------- |
25 | 32 |
26 function HashToEntry(table, hash, numBuckets) { | 33 function HashToEntry(table, hash, numBuckets) { |
27 var bucket = ORDERED_HASH_TABLE_HASH_TO_BUCKET(hash, numBuckets); | 34 var bucket = ORDERED_HASH_TABLE_HASH_TO_BUCKET(hash, numBuckets); |
28 return ORDERED_HASH_TABLE_BUCKET_AT(table, bucket); | 35 return ORDERED_HASH_TABLE_BUCKET_AT(table, bucket); |
29 } | 36 } |
30 %SetForceInlineFlag(HashToEntry); | 37 %SetForceInlineFlag(HashToEntry); |
31 | 38 |
32 | 39 |
33 function SetFindEntry(table, numBuckets, key, hash) { | 40 function SetFindEntry(table, numBuckets, key, hash) { |
| 41 var entry = HashToEntry(table, hash, numBuckets); |
| 42 if (entry === NOT_FOUND) return entry; |
| 43 var candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); |
| 44 if (key === candidate) return entry; |
34 var keyIsNaN = NumberIsNaN(key); | 45 var keyIsNaN = NumberIsNaN(key); |
35 for (var entry = HashToEntry(table, hash, numBuckets); | 46 while (true) { |
36 entry !== NOT_FOUND; | |
37 entry = ORDERED_HASH_SET_CHAIN_AT(table, entry, numBuckets)) { | |
38 var candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); | |
39 if (key === candidate) { | |
40 return entry; | |
41 } | |
42 if (keyIsNaN && NumberIsNaN(candidate)) { | 47 if (keyIsNaN && NumberIsNaN(candidate)) { |
43 return entry; | 48 return entry; |
44 } | 49 } |
| 50 entry = ORDERED_HASH_SET_CHAIN_AT(table, entry, numBuckets); |
| 51 if (entry === NOT_FOUND) return entry; |
| 52 candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); |
| 53 if (key === candidate) return entry; |
45 } | 54 } |
46 return NOT_FOUND; | 55 return NOT_FOUND; |
47 } | 56 } |
48 %SetForceInlineFlag(SetFindEntry); | 57 %SetForceInlineFlag(SetFindEntry); |
49 | 58 |
50 | 59 |
51 function MapFindEntry(table, numBuckets, key, hash) { | 60 function MapFindEntry(table, numBuckets, key, hash) { |
| 61 var entry = HashToEntry(table, hash, numBuckets); |
| 62 if (entry === NOT_FOUND) return entry; |
| 63 var candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); |
| 64 if (key === candidate) return entry; |
52 var keyIsNaN = NumberIsNaN(key); | 65 var keyIsNaN = NumberIsNaN(key); |
53 for (var entry = HashToEntry(table, hash, numBuckets); | 66 while (true) { |
54 entry !== NOT_FOUND; | |
55 entry = ORDERED_HASH_MAP_CHAIN_AT(table, entry, numBuckets)) { | |
56 var candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); | |
57 if (key === candidate) { | |
58 return entry; | |
59 } | |
60 if (keyIsNaN && NumberIsNaN(candidate)) { | 67 if (keyIsNaN && NumberIsNaN(candidate)) { |
61 return entry; | 68 return entry; |
62 } | 69 } |
| 70 entry = ORDERED_HASH_MAP_CHAIN_AT(table, entry, numBuckets); |
| 71 if (entry === NOT_FOUND) return entry; |
| 72 candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); |
| 73 if (key === candidate) return entry; |
63 } | 74 } |
64 return NOT_FOUND; | 75 return NOT_FOUND; |
65 } | 76 } |
66 %SetForceInlineFlag(MapFindEntry); | 77 %SetForceInlineFlag(MapFindEntry); |
67 | 78 |
68 | 79 |
69 function ComputeIntegerHash(key, seed) { | 80 function ComputeIntegerHash(key, seed) { |
70 var hash = key; | 81 var hash = key; |
71 hash = hash ^ seed; | 82 hash = hash ^ seed; |
72 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; | 83 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; |
73 hash = hash ^ (hash >>> 12); | 84 hash = hash ^ (hash >>> 12); |
74 hash = hash + (hash << 2); | 85 hash = hash + (hash << 2); |
75 hash = hash ^ (hash >>> 4); | 86 hash = hash ^ (hash >>> 4); |
76 hash = (hash * 2057) | 0; // hash = (hash + (hash << 3)) + (hash << 11); | 87 hash = (hash * 2057) | 0; // hash = (hash + (hash << 3)) + (hash << 11); |
77 hash = hash ^ (hash >>> 16); | 88 hash = hash ^ (hash >>> 16); |
78 return hash; | 89 return hash & 0x3fffffff; |
79 } | 90 } |
80 %SetForceInlineFlag(ComputeIntegerHash); | 91 %SetForceInlineFlag(ComputeIntegerHash); |
81 | 92 |
| 93 var hashCodeSymbol = GLOBAL_PRIVATE("hash_code_symbol"); |
82 | 94 |
83 function GetHash(key) { | 95 function GetExistingHash(key) { |
84 if (%_IsSmi(key)) { | 96 if (%_IsSmi(key)) { |
85 return ComputeIntegerHash(key, 0); | 97 return ComputeIntegerHash(key, 0); |
86 } | 98 } |
87 if (IS_STRING(key)) { | 99 if (IS_STRING(key)) { |
88 var field = %_StringGetRawHashField(key); | 100 var field = %_StringGetRawHashField(key); |
89 if ((field & 1 /* Name::kHashNotComputedMask */) === 0) { | 101 if ((field & 1 /* Name::kHashNotComputedMask */) === 0) { |
90 return field >>> 2 /* Name::kHashShift */; | 102 return field >>> 2 /* Name::kHashShift */; |
91 } | 103 } |
| 104 } else if (IS_SPEC_OBJECT(key) && !%_IsJSProxy(key) && !IS_GLOBAL(key)) { |
| 105 var hash = GET_PRIVATE(key, hashCodeSymbol); |
| 106 return hash; |
92 } | 107 } |
93 return %GenericHash(key); | 108 return %GenericHash(key); |
94 } | 109 } |
| 110 %SetForceInlineFlag(GetExistingHash); |
| 111 |
| 112 |
| 113 function GetHash(key) { |
| 114 var hash = GetExistingHash(key); |
| 115 if (IS_UNDEFINED(hash)) { |
| 116 hash = IntRandom() | 0; |
| 117 if (hash === 0) hash = 1; |
| 118 SET_PRIVATE(key, hashCodeSymbol, hash); |
| 119 } |
| 120 return hash; |
| 121 } |
95 %SetForceInlineFlag(GetHash); | 122 %SetForceInlineFlag(GetHash); |
96 | 123 |
97 | 124 |
98 // ------------------------------------------------------------------- | 125 // ------------------------------------------------------------------- |
99 // Harmony Set | 126 // Harmony Set |
100 | 127 |
101 function SetConstructor(iterable) { | 128 function SetConstructor(iterable) { |
102 if (!%_IsConstructCall()) { | 129 if (!%_IsConstructCall()) { |
103 throw MakeTypeError(kConstructorNotFunction, "Set"); | 130 throw MakeTypeError(kConstructorNotFunction, "Set"); |
104 } | 131 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 return this; | 184 return this; |
158 } | 185 } |
159 | 186 |
160 | 187 |
161 function SetHas(key) { | 188 function SetHas(key) { |
162 if (!IS_SET(this)) { | 189 if (!IS_SET(this)) { |
163 throw MakeTypeError(kIncompatibleMethodReceiver, 'Set.prototype.has', this); | 190 throw MakeTypeError(kIncompatibleMethodReceiver, 'Set.prototype.has', this); |
164 } | 191 } |
165 var table = %_JSCollectionGetTable(this); | 192 var table = %_JSCollectionGetTable(this); |
166 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 193 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
167 var hash = GetHash(key); | 194 var hash = GetExistingHash(key); |
| 195 if (IS_UNDEFINED(hash)) return false; |
168 return SetFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; | 196 return SetFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; |
169 } | 197 } |
170 | 198 |
171 | 199 |
172 function SetDelete(key) { | 200 function SetDelete(key) { |
173 if (!IS_SET(this)) { | 201 if (!IS_SET(this)) { |
174 throw MakeTypeError(kIncompatibleMethodReceiver, | 202 throw MakeTypeError(kIncompatibleMethodReceiver, |
175 'Set.prototype.delete', this); | 203 'Set.prototype.delete', this); |
176 } | 204 } |
177 var table = %_JSCollectionGetTable(this); | 205 var table = %_JSCollectionGetTable(this); |
178 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 206 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
179 var hash = GetHash(key); | 207 var hash = GetExistingHash(key); |
| 208 if (IS_UNDEFINED(hash)) return false; |
180 var entry = SetFindEntry(table, numBuckets, key, hash); | 209 var entry = SetFindEntry(table, numBuckets, key, hash); |
181 if (entry === NOT_FOUND) return false; | 210 if (entry === NOT_FOUND) return false; |
182 | 211 |
183 var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1; | 212 var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1; |
184 var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1; | 213 var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1; |
185 var index = ORDERED_HASH_SET_ENTRY_TO_INDEX(entry, numBuckets); | 214 var index = ORDERED_HASH_SET_ENTRY_TO_INDEX(entry, numBuckets); |
186 FIXED_ARRAY_SET(table, index, %_TheHole()); | 215 FIXED_ARRAY_SET(table, index, %_TheHole()); |
187 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof); | 216 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof); |
188 ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod); | 217 ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod); |
189 if (nof < (numBuckets >>> 1)) %SetShrink(this); | 218 if (nof < (numBuckets >>> 1)) %SetShrink(this); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 } | 313 } |
285 | 314 |
286 | 315 |
287 function MapGet(key) { | 316 function MapGet(key) { |
288 if (!IS_MAP(this)) { | 317 if (!IS_MAP(this)) { |
289 throw MakeTypeError(kIncompatibleMethodReceiver, | 318 throw MakeTypeError(kIncompatibleMethodReceiver, |
290 'Map.prototype.get', this); | 319 'Map.prototype.get', this); |
291 } | 320 } |
292 var table = %_JSCollectionGetTable(this); | 321 var table = %_JSCollectionGetTable(this); |
293 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 322 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
294 var hash = GetHash(key); | 323 var hash = GetExistingHash(key); |
| 324 if (IS_UNDEFINED(hash)) return UNDEFINED; |
295 var entry = MapFindEntry(table, numBuckets, key, hash); | 325 var entry = MapFindEntry(table, numBuckets, key, hash); |
296 if (entry === NOT_FOUND) return UNDEFINED; | 326 if (entry === NOT_FOUND) return UNDEFINED; |
297 return ORDERED_HASH_MAP_VALUE_AT(table, entry, numBuckets); | 327 return ORDERED_HASH_MAP_VALUE_AT(table, entry, numBuckets); |
298 } | 328 } |
299 | 329 |
300 | 330 |
301 function MapSet(key, value) { | 331 function MapSet(key, value) { |
302 if (!IS_MAP(this)) { | 332 if (!IS_MAP(this)) { |
303 throw MakeTypeError(kIncompatibleMethodReceiver, | 333 throw MakeTypeError(kIncompatibleMethodReceiver, |
304 'Map.prototype.set', this); | 334 'Map.prototype.set', this); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 utils.InstallGetter(GlobalMap.prototype, "size", MapGetSize); | 469 utils.InstallGetter(GlobalMap.prototype, "size", MapGetSize); |
440 utils.InstallFunctions(GlobalMap.prototype, DONT_ENUM, [ | 470 utils.InstallFunctions(GlobalMap.prototype, DONT_ENUM, [ |
441 "get", MapGet, | 471 "get", MapGet, |
442 "set", MapSet, | 472 "set", MapSet, |
443 "has", MapHas, | 473 "has", MapHas, |
444 "delete", MapDelete, | 474 "delete", MapDelete, |
445 "clear", MapClearJS, | 475 "clear", MapClearJS, |
446 "forEach", MapForEach | 476 "forEach", MapForEach |
447 ]); | 477 ]); |
448 | 478 |
| 479 // Expose to the global scope. |
| 480 $getHash = GetHash; |
| 481 $getExistingHash = GetExistingHash; |
| 482 |
449 }) | 483 }) |
OLD | NEW |