OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/runtime/runtime-utils.h" | 8 #include "src/runtime/runtime-utils.h" |
9 | 9 |
10 | 10 |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 DCHECK(args.length() == 2); | 387 DCHECK(args.length() == 2); |
388 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); | 388 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
389 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); | 389 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); |
390 RUNTIME_ASSERT(max_values >= 0); | 390 RUNTIME_ASSERT(max_values >= 0); |
391 | 391 |
392 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); | 392 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
393 if (max_values == 0 || max_values > table->NumberOfElements()) { | 393 if (max_values == 0 || max_values > table->NumberOfElements()) { |
394 max_values = table->NumberOfElements(); | 394 max_values = table->NumberOfElements(); |
395 } | 395 } |
396 Handle<FixedArray> values = isolate->factory()->NewFixedArray(max_values); | 396 Handle<FixedArray> values = isolate->factory()->NewFixedArray(max_values); |
| 397 // Recompute max_values because GC could have removed elements from the table. |
| 398 if (max_values > table->NumberOfElements()) { |
| 399 max_values = table->NumberOfElements(); |
| 400 } |
397 { | 401 { |
398 DisallowHeapAllocation no_gc; | 402 DisallowHeapAllocation no_gc; |
399 int count = 0; | 403 int count = 0; |
400 for (int i = 0; count < max_values && i < table->Capacity(); i++) { | 404 for (int i = 0; count < max_values && i < table->Capacity(); i++) { |
401 Handle<Object> key(table->KeyAt(i), isolate); | 405 Handle<Object> key(table->KeyAt(i), isolate); |
402 if (table->IsKey(*key)) values->set(count++, *key); | 406 if (table->IsKey(*key)) values->set(count++, *key); |
403 } | 407 } |
404 DCHECK_EQ(max_values, count); | 408 DCHECK_EQ(max_values, count); |
405 } | 409 } |
406 return *isolate->factory()->NewJSArrayWithElements(values); | 410 return *isolate->factory()->NewJSArrayWithElements(values); |
407 } | 411 } |
408 | 412 |
409 | 413 |
410 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { | 414 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { |
411 HandleScope scope(isolate); | 415 HandleScope scope(isolate); |
412 DCHECK(args.length() == 0); | 416 DCHECK(args.length() == 0); |
413 // TODO(adamk): Currently this runtime function is only called three times per | 417 // TODO(adamk): Currently this runtime function is only called three times per |
414 // isolate. If it's called more often, the map should be moved into the | 418 // isolate. If it's called more often, the map should be moved into the |
415 // strong root list. | 419 // strong root list. |
416 Handle<Map> map = | 420 Handle<Map> map = |
417 isolate->factory()->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); | 421 isolate->factory()->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); |
418 Handle<JSWeakMap> weakmap = | 422 Handle<JSWeakMap> weakmap = |
419 Handle<JSWeakMap>::cast(isolate->factory()->NewJSObjectFromMap(map)); | 423 Handle<JSWeakMap>::cast(isolate->factory()->NewJSObjectFromMap(map)); |
420 return *WeakCollectionInitialize(isolate, weakmap); | 424 return *WeakCollectionInitialize(isolate, weakmap); |
421 } | 425 } |
422 } | 426 } |
423 } // namespace v8::internal | 427 } // namespace v8::internal |
OLD | NEW |