| 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 "use strict"; | 5 "use strict"; |
| 6 | 6 |
| 7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
| 8 // in runtime.js: | 8 // in runtime.js: |
| 9 // var $Array = global.Array; | 9 // var $Array = global.Array; |
| 10 | 10 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 function SetConstructor(iterable) { | 95 function SetConstructor(iterable) { |
| 96 if (!%_IsConstructCall()) { | 96 if (!%_IsConstructCall()) { |
| 97 throw MakeTypeError('constructor_not_function', ['Set']); | 97 throw MakeTypeError('constructor_not_function', ['Set']); |
| 98 } | 98 } |
| 99 | 99 |
| 100 %_SetInitialize(this); | 100 %_SetInitialize(this); |
| 101 | 101 |
| 102 if (!IS_NULL_OR_UNDEFINED(iterable)) { | 102 if (!IS_NULL_OR_UNDEFINED(iterable)) { |
| 103 var adder = this.add; | 103 var adder = this.add; |
| 104 if (!IS_SPEC_FUNCTION(adder)) { | 104 if (!IS_SPEC_FUNCTION(adder)) { |
| 105 throw MakeTypeError(kPropertyNotFunction, ['add', this]); | 105 throw MakeTypeError(kPropertyNotFunction, 'add', this); |
| 106 } | 106 } |
| 107 | 107 |
| 108 for (var value of iterable) { | 108 for (var value of iterable) { |
| 109 %_CallFunction(this, value, adder); | 109 %_CallFunction(this, value, adder); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 | 114 |
| 115 function SetAdd(key) { | 115 function SetAdd(key) { |
| 116 if (!IS_SET(this)) { | 116 if (!IS_SET(this)) { |
| 117 throw MakeTypeError('incompatible_method_receiver', | 117 throw MakeTypeError(kIncompatibleMethodReceiver, 'Set.prototype.add', this); |
| 118 ['Set.prototype.add', this]); | |
| 119 } | 118 } |
| 120 // Normalize -0 to +0 as required by the spec. | 119 // Normalize -0 to +0 as required by the spec. |
| 121 // Even though we use SameValueZero as the comparison for the keys we don't | 120 // Even though we use SameValueZero as the comparison for the keys we don't |
| 122 // want to ever store -0 as the key since the key is directly exposed when | 121 // want to ever store -0 as the key since the key is directly exposed when |
| 123 // doing iteration. | 122 // doing iteration. |
| 124 if (key === 0) { | 123 if (key === 0) { |
| 125 key = 0; | 124 key = 0; |
| 126 } | 125 } |
| 127 var table = %_JSCollectionGetTable(this); | 126 var table = %_JSCollectionGetTable(this); |
| 128 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 127 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 148 ORDERED_HASH_TABLE_SET_BUCKET_AT(table, bucket, entry); | 147 ORDERED_HASH_TABLE_SET_BUCKET_AT(table, bucket, entry); |
| 149 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof + 1); | 148 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof + 1); |
| 150 FIXED_ARRAY_SET(table, index, key); | 149 FIXED_ARRAY_SET(table, index, key); |
| 151 FIXED_ARRAY_SET_SMI(table, index + 1, chainEntry); | 150 FIXED_ARRAY_SET_SMI(table, index + 1, chainEntry); |
| 152 return this; | 151 return this; |
| 153 } | 152 } |
| 154 | 153 |
| 155 | 154 |
| 156 function SetHas(key) { | 155 function SetHas(key) { |
| 157 if (!IS_SET(this)) { | 156 if (!IS_SET(this)) { |
| 158 throw MakeTypeError('incompatible_method_receiver', | 157 throw MakeTypeError(kIncompatibleMethodReceiver, 'Set.prototype.has', this); |
| 159 ['Set.prototype.has', this]); | |
| 160 } | 158 } |
| 161 var table = %_JSCollectionGetTable(this); | 159 var table = %_JSCollectionGetTable(this); |
| 162 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 160 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| 163 var hash = GetHash(key); | 161 var hash = GetHash(key); |
| 164 return SetFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; | 162 return SetFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; |
| 165 } | 163 } |
| 166 | 164 |
| 167 | 165 |
| 168 function SetDelete(key) { | 166 function SetDelete(key) { |
| 169 if (!IS_SET(this)) { | 167 if (!IS_SET(this)) { |
| 170 throw MakeTypeError('incompatible_method_receiver', | 168 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 171 ['Set.prototype.delete', this]); | 169 'Set.prototype.delete', this); |
| 172 } | 170 } |
| 173 var table = %_JSCollectionGetTable(this); | 171 var table = %_JSCollectionGetTable(this); |
| 174 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 172 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| 175 var hash = GetHash(key); | 173 var hash = GetHash(key); |
| 176 var entry = SetFindEntry(table, numBuckets, key, hash); | 174 var entry = SetFindEntry(table, numBuckets, key, hash); |
| 177 if (entry === NOT_FOUND) return false; | 175 if (entry === NOT_FOUND) return false; |
| 178 | 176 |
| 179 var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1; | 177 var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1; |
| 180 var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1; | 178 var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1; |
| 181 var index = ORDERED_HASH_SET_ENTRY_TO_INDEX(entry, numBuckets); | 179 var index = ORDERED_HASH_SET_ENTRY_TO_INDEX(entry, numBuckets); |
| 182 FIXED_ARRAY_SET(table, index, %_TheHole()); | 180 FIXED_ARRAY_SET(table, index, %_TheHole()); |
| 183 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof); | 181 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof); |
| 184 ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod); | 182 ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod); |
| 185 if (nof < (numBuckets >>> 1)) %SetShrink(this); | 183 if (nof < (numBuckets >>> 1)) %SetShrink(this); |
| 186 return true; | 184 return true; |
| 187 } | 185 } |
| 188 | 186 |
| 189 | 187 |
| 190 function SetGetSize() { | 188 function SetGetSize() { |
| 191 if (!IS_SET(this)) { | 189 if (!IS_SET(this)) { |
| 192 throw MakeTypeError('incompatible_method_receiver', | 190 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 193 ['Set.prototype.size', this]); | 191 'Set.prototype.size', this); |
| 194 } | 192 } |
| 195 var table = %_JSCollectionGetTable(this); | 193 var table = %_JSCollectionGetTable(this); |
| 196 return ORDERED_HASH_TABLE_ELEMENT_COUNT(table); | 194 return ORDERED_HASH_TABLE_ELEMENT_COUNT(table); |
| 197 } | 195 } |
| 198 | 196 |
| 199 | 197 |
| 200 function SetClearJS() { | 198 function SetClearJS() { |
| 201 if (!IS_SET(this)) { | 199 if (!IS_SET(this)) { |
| 202 throw MakeTypeError('incompatible_method_receiver', | 200 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 203 ['Set.prototype.clear', this]); | 201 'Set.prototype.clear', this); |
| 204 } | 202 } |
| 205 %_SetClear(this); | 203 %_SetClear(this); |
| 206 } | 204 } |
| 207 | 205 |
| 208 | 206 |
| 209 function SetForEach(f, receiver) { | 207 function SetForEach(f, receiver) { |
| 210 if (!IS_SET(this)) { | 208 if (!IS_SET(this)) { |
| 211 throw MakeTypeError('incompatible_method_receiver', | 209 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 212 ['Set.prototype.forEach', this]); | 210 'Set.prototype.forEach', this); |
| 213 } | 211 } |
| 214 | 212 |
| 215 if (!IS_SPEC_FUNCTION(f)) { | 213 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); |
| 216 throw MakeTypeError('called_non_callable', [f]); | |
| 217 } | |
| 218 var needs_wrapper = false; | 214 var needs_wrapper = false; |
| 219 if (IS_NULL_OR_UNDEFINED(receiver)) { | 215 if (IS_NULL_OR_UNDEFINED(receiver)) { |
| 220 receiver = %GetDefaultReceiver(f) || receiver; | 216 receiver = %GetDefaultReceiver(f) || receiver; |
| 221 } else { | 217 } else { |
| 222 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); | 218 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); |
| 223 } | 219 } |
| 224 | 220 |
| 225 var iterator = new SetIterator(this, ITERATOR_KIND_VALUES); | 221 var iterator = new SetIterator(this, ITERATOR_KIND_VALUES); |
| 226 var key; | 222 var key; |
| 227 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); | 223 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 function MapConstructor(iterable) { | 257 function MapConstructor(iterable) { |
| 262 if (!%_IsConstructCall()) { | 258 if (!%_IsConstructCall()) { |
| 263 throw MakeTypeError('constructor_not_function', ['Map']); | 259 throw MakeTypeError('constructor_not_function', ['Map']); |
| 264 } | 260 } |
| 265 | 261 |
| 266 %_MapInitialize(this); | 262 %_MapInitialize(this); |
| 267 | 263 |
| 268 if (!IS_NULL_OR_UNDEFINED(iterable)) { | 264 if (!IS_NULL_OR_UNDEFINED(iterable)) { |
| 269 var adder = this.set; | 265 var adder = this.set; |
| 270 if (!IS_SPEC_FUNCTION(adder)) { | 266 if (!IS_SPEC_FUNCTION(adder)) { |
| 271 throw MakeTypeError(kPropertyNotFunction, ['set', this]); | 267 throw MakeTypeError(kPropertyNotFunction, 'set', this); |
| 272 } | 268 } |
| 273 | 269 |
| 274 for (var nextItem of iterable) { | 270 for (var nextItem of iterable) { |
| 275 if (!IS_SPEC_OBJECT(nextItem)) { | 271 if (!IS_SPEC_OBJECT(nextItem)) { |
| 276 throw MakeTypeError('iterator_value_not_an_object', [nextItem]); | 272 throw MakeTypeError('iterator_value_not_an_object', [nextItem]); |
| 277 } | 273 } |
| 278 %_CallFunction(this, nextItem[0], nextItem[1], adder); | 274 %_CallFunction(this, nextItem[0], nextItem[1], adder); |
| 279 } | 275 } |
| 280 } | 276 } |
| 281 } | 277 } |
| 282 | 278 |
| 283 | 279 |
| 284 function MapGet(key) { | 280 function MapGet(key) { |
| 285 if (!IS_MAP(this)) { | 281 if (!IS_MAP(this)) { |
| 286 throw MakeTypeError('incompatible_method_receiver', | 282 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 287 ['Map.prototype.get', this]); | 283 'Map.prototype.get', this); |
| 288 } | 284 } |
| 289 var table = %_JSCollectionGetTable(this); | 285 var table = %_JSCollectionGetTable(this); |
| 290 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 286 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| 291 var hash = GetHash(key); | 287 var hash = GetHash(key); |
| 292 var entry = MapFindEntry(table, numBuckets, key, hash); | 288 var entry = MapFindEntry(table, numBuckets, key, hash); |
| 293 if (entry === NOT_FOUND) return UNDEFINED; | 289 if (entry === NOT_FOUND) return UNDEFINED; |
| 294 return ORDERED_HASH_MAP_VALUE_AT(table, entry, numBuckets); | 290 return ORDERED_HASH_MAP_VALUE_AT(table, entry, numBuckets); |
| 295 } | 291 } |
| 296 | 292 |
| 297 | 293 |
| 298 function MapSet(key, value) { | 294 function MapSet(key, value) { |
| 299 if (!IS_MAP(this)) { | 295 if (!IS_MAP(this)) { |
| 300 throw MakeTypeError('incompatible_method_receiver', | 296 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 301 ['Map.prototype.set', this]); | 297 'Map.prototype.set', this); |
| 302 } | 298 } |
| 303 // Normalize -0 to +0 as required by the spec. | 299 // Normalize -0 to +0 as required by the spec. |
| 304 // Even though we use SameValueZero as the comparison for the keys we don't | 300 // Even though we use SameValueZero as the comparison for the keys we don't |
| 305 // want to ever store -0 as the key since the key is directly exposed when | 301 // want to ever store -0 as the key since the key is directly exposed when |
| 306 // doing iteration. | 302 // doing iteration. |
| 307 if (key === 0) { | 303 if (key === 0) { |
| 308 key = 0; | 304 key = 0; |
| 309 } | 305 } |
| 310 | 306 |
| 311 var table = %_JSCollectionGetTable(this); | 307 var table = %_JSCollectionGetTable(this); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 338 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof + 1); | 334 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof + 1); |
| 339 FIXED_ARRAY_SET(table, index, key); | 335 FIXED_ARRAY_SET(table, index, key); |
| 340 FIXED_ARRAY_SET(table, index + 1, value); | 336 FIXED_ARRAY_SET(table, index + 1, value); |
| 341 FIXED_ARRAY_SET(table, index + 2, chainEntry); | 337 FIXED_ARRAY_SET(table, index + 2, chainEntry); |
| 342 return this; | 338 return this; |
| 343 } | 339 } |
| 344 | 340 |
| 345 | 341 |
| 346 function MapHas(key) { | 342 function MapHas(key) { |
| 347 if (!IS_MAP(this)) { | 343 if (!IS_MAP(this)) { |
| 348 throw MakeTypeError('incompatible_method_receiver', | 344 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 349 ['Map.prototype.has', this]); | 345 'Map.prototype.has', this); |
| 350 } | 346 } |
| 351 var table = %_JSCollectionGetTable(this); | 347 var table = %_JSCollectionGetTable(this); |
| 352 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 348 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| 353 var hash = GetHash(key); | 349 var hash = GetHash(key); |
| 354 return MapFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; | 350 return MapFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; |
| 355 } | 351 } |
| 356 | 352 |
| 357 | 353 |
| 358 function MapDelete(key) { | 354 function MapDelete(key) { |
| 359 if (!IS_MAP(this)) { | 355 if (!IS_MAP(this)) { |
| 360 throw MakeTypeError('incompatible_method_receiver', | 356 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 361 ['Map.prototype.delete', this]); | 357 'Map.prototype.delete', this); |
| 362 } | 358 } |
| 363 var table = %_JSCollectionGetTable(this); | 359 var table = %_JSCollectionGetTable(this); |
| 364 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 360 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| 365 var hash = GetHash(key); | 361 var hash = GetHash(key); |
| 366 var entry = MapFindEntry(table, numBuckets, key, hash); | 362 var entry = MapFindEntry(table, numBuckets, key, hash); |
| 367 if (entry === NOT_FOUND) return false; | 363 if (entry === NOT_FOUND) return false; |
| 368 | 364 |
| 369 var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1; | 365 var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1; |
| 370 var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1; | 366 var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1; |
| 371 var index = ORDERED_HASH_MAP_ENTRY_TO_INDEX(entry, numBuckets); | 367 var index = ORDERED_HASH_MAP_ENTRY_TO_INDEX(entry, numBuckets); |
| 372 FIXED_ARRAY_SET(table, index, %_TheHole()); | 368 FIXED_ARRAY_SET(table, index, %_TheHole()); |
| 373 FIXED_ARRAY_SET(table, index + 1, %_TheHole()); | 369 FIXED_ARRAY_SET(table, index + 1, %_TheHole()); |
| 374 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof); | 370 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof); |
| 375 ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod); | 371 ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod); |
| 376 if (nof < (numBuckets >>> 1)) %MapShrink(this); | 372 if (nof < (numBuckets >>> 1)) %MapShrink(this); |
| 377 return true; | 373 return true; |
| 378 } | 374 } |
| 379 | 375 |
| 380 | 376 |
| 381 function MapGetSize() { | 377 function MapGetSize() { |
| 382 if (!IS_MAP(this)) { | 378 if (!IS_MAP(this)) { |
| 383 throw MakeTypeError('incompatible_method_receiver', | 379 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 384 ['Map.prototype.size', this]); | 380 'Map.prototype.size', this); |
| 385 } | 381 } |
| 386 var table = %_JSCollectionGetTable(this); | 382 var table = %_JSCollectionGetTable(this); |
| 387 return ORDERED_HASH_TABLE_ELEMENT_COUNT(table); | 383 return ORDERED_HASH_TABLE_ELEMENT_COUNT(table); |
| 388 } | 384 } |
| 389 | 385 |
| 390 | 386 |
| 391 function MapClearJS() { | 387 function MapClearJS() { |
| 392 if (!IS_MAP(this)) { | 388 if (!IS_MAP(this)) { |
| 393 throw MakeTypeError('incompatible_method_receiver', | 389 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 394 ['Map.prototype.clear', this]); | 390 'Map.prototype.clear', this); |
| 395 } | 391 } |
| 396 %_MapClear(this); | 392 %_MapClear(this); |
| 397 } | 393 } |
| 398 | 394 |
| 399 | 395 |
| 400 function MapForEach(f, receiver) { | 396 function MapForEach(f, receiver) { |
| 401 if (!IS_MAP(this)) { | 397 if (!IS_MAP(this)) { |
| 402 throw MakeTypeError('incompatible_method_receiver', | 398 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 403 ['Map.prototype.forEach', this]); | 399 'Map.prototype.forEach', this); |
| 404 } | 400 } |
| 405 | 401 |
| 406 if (!IS_SPEC_FUNCTION(f)) { | 402 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); |
| 407 throw MakeTypeError('called_non_callable', [f]); | |
| 408 } | |
| 409 var needs_wrapper = false; | 403 var needs_wrapper = false; |
| 410 if (IS_NULL_OR_UNDEFINED(receiver)) { | 404 if (IS_NULL_OR_UNDEFINED(receiver)) { |
| 411 receiver = %GetDefaultReceiver(f) || receiver; | 405 receiver = %GetDefaultReceiver(f) || receiver; |
| 412 } else { | 406 } else { |
| 413 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); | 407 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); |
| 414 } | 408 } |
| 415 | 409 |
| 416 var iterator = new MapIterator(this, ITERATOR_KIND_ENTRIES); | 410 var iterator = new MapIterator(this, ITERATOR_KIND_ENTRIES); |
| 417 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); | 411 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); |
| 418 var value_array = [UNDEFINED, UNDEFINED]; | 412 var value_array = [UNDEFINED, UNDEFINED]; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 438 InstallFunctions($Map.prototype, DONT_ENUM, [ | 432 InstallFunctions($Map.prototype, DONT_ENUM, [ |
| 439 "get", MapGet, | 433 "get", MapGet, |
| 440 "set", MapSet, | 434 "set", MapSet, |
| 441 "has", MapHas, | 435 "has", MapHas, |
| 442 "delete", MapDelete, | 436 "delete", MapDelete, |
| 443 "clear", MapClearJS, | 437 "clear", MapClearJS, |
| 444 "forEach", MapForEach | 438 "forEach", MapForEach |
| 445 ]); | 439 ]); |
| 446 | 440 |
| 447 })(); | 441 })(); |
| OLD | NEW |