Skip to content

Commit

Permalink
[symbol-as-weakmap-key] Add tests to check weak collection size
Browse files Browse the repository at this point in the history
... after gc.

This CL also adds a runtime test function GetWeakCollectionSize
to get the weak collection size.

Bug: v8:12947
Change-Id: I4aff39165a54b63b3d690bfea71c2a439da01d00
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3905071
Reviewed-by: Marja Hölttä <marja@chromium.org>
Commit-Queue: 王澳 <wangao.james@bytedance.com>
Cr-Commit-Position: refs/heads/main@{#83464}
  • Loading branch information
jameslahm authored and V8 LUCI CQ committed Sep 28, 2022
1 parent 320edbe commit 7f5daed
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/runtime/runtime-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "src/heap/heap-inl.h" // For ToBoolean. TODO(jkummerow): Drop.
#include "src/heap/heap-write-barrier-inl.h"
#include "src/ic/stub-cache.h"
#include "src/objects/js-collection-inl.h"
#ifdef V8_ENABLE_MAGLEV
#include "src/maglev/maglev-concurrent-dispatcher.h"
#endif // V8_ENABLE_MAGLEV
Expand Down Expand Up @@ -1764,5 +1765,14 @@ RUNTIME_FUNCTION(Runtime_AtomicsConditionNumWaitersForTesting) {
return cv->NumWaitersForTesting(isolate);
}

RUNTIME_FUNCTION(Runtime_GetWeakCollectionSize) {
HandleScope scope(isolate);
DCHECK_EQ(1, args.length());
Handle<JSWeakCollection> collection = args.at<JSWeakCollection>(0);

return Smi::FromInt(
EphemeronHashTable::cast(collection->table()).NumberOfElements());
}

} // namespace internal
} // namespace v8
1 change: 1 addition & 0 deletions src/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ namespace internal {
F(GetInitializerFunction, 1, 1) \
F(GetOptimizationStatus, 1, 1) \
F(GetUndetectable, 0, 1) \
F(GetWeakCollectionSize, 1, 1) \
F(GlobalPrint, 1, 1) \
F(HasDictionaryElements, 1, 1) \
F(HasDoubleElements, 1, 1) \
Expand Down
14 changes: 8 additions & 6 deletions test/mjsunit/harmony/symbol-as-weakmap-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --harmony-symbol-as-weakmap-key --expose-gc
// Flags: --harmony-symbol-as-weakmap-key --expose-gc --allow-natives-syntax --noincremental-marking

(function TestWeakMapWithNonRegisteredSymbolKey() {
const key = Symbol('123');
Expand All @@ -28,16 +28,17 @@
const outerKey = Symbol('234');
const outerValue = 1;
map.set(outerKey, outerValue);
{
(function () {
const innerKey = Symbol('123');
const innerValue = 1;
map.set(innerKey, innerValue);
assertTrue(map.has(innerKey));
assertSame(innerValue, map.get(innerKey));
}
})();
gc();
assertTrue(map.has(outerKey));
assertSame(outerValue, map.get(outerKey));
assertEquals(1, %GetWeakCollectionSize(map));
})();

(function TestWeakMapWithRegisteredSymbolKey() {
Expand Down Expand Up @@ -74,13 +75,14 @@
const set = new WeakSet();
const outerKey = Symbol('234');
set.add(outerKey);
{
(function () {
const innerKey = Symbol('123');
set.add(innerKey);
assertTrue(set.has(innerKey));
}
gc();
})();
assertTrue(set.has(outerKey));
gc();
assertEquals(1, %GetWeakCollectionSize(set));
})();

(function TestWeakSetWithRegisteredSymbolKey() {
Expand Down
14 changes: 11 additions & 3 deletions test/unittests/objects/weakmaps-unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,22 @@ TEST_F(WeakMapsTest, Weakness) {
int32_t object_hash = object->GetOrCreateHash(isolate).value();
JSWeakCollection::Set(weakmap, object, smi, object_hash);
}
CHECK_EQ(2, EphemeronHashTable::cast(weakmap->table()).NumberOfElements());
// Put a symbol key into weak map.
{
HandleScope inner_scope(isolate);
Handle<Symbol> symbol = factory->NewSymbol();
Handle<Smi> smi(Smi::FromInt(23), isolate);
JSWeakCollection::Set(weakmap, symbol, smi, symbol->hash());
}
CHECK_EQ(3, EphemeronHashTable::cast(weakmap->table()).NumberOfElements());

// Force a full GC.
PreciseCollectAllGarbage();
CHECK_EQ(0, NumberOfWeakCalls);
// Symbol key should be deleted.
CHECK_EQ(2, EphemeronHashTable::cast(weakmap->table()).NumberOfElements());
CHECK_EQ(
0, EphemeronHashTable::cast(weakmap->table()).NumberOfDeletedElements());
1, EphemeronHashTable::cast(weakmap->table()).NumberOfDeletedElements());

// Make the global reference to the key weak.
std::pair<Handle<Object>*, int> handle_and_id(&key, 1234);
Expand All @@ -103,7 +111,7 @@ TEST_F(WeakMapsTest, Weakness) {
CHECK_EQ(1, NumberOfWeakCalls);
CHECK_EQ(0, EphemeronHashTable::cast(weakmap->table()).NumberOfElements());
CHECK_EQ(
2, EphemeronHashTable::cast(weakmap->table()).NumberOfDeletedElements());
3, EphemeronHashTable::cast(weakmap->table()).NumberOfDeletedElements());
}

TEST_F(WeakMapsTest, Shrinking) {
Expand Down

0 comments on commit 7f5daed

Please sign in to comment.