diff --git a/src/api.cc b/src/api.cc index 22b9dafa4b..744c7dc13b 100644 --- a/src/api.cc +++ b/src/api.cc @@ -2525,7 +2525,7 @@ Local NativeWeakMap::New(Isolate* v8_isolate) { i::Isolate* isolate = reinterpret_cast(v8_isolate); ENTER_V8(isolate); i::Handle weakmap = isolate->factory()->NewJSWeakMap(); - i::Runtime::WeakCollectionInitialize(isolate, weakmap); + i::JSWeakCollection::Initialize(weakmap, isolate); return Utils::NativeWeakMapToLocal(weakmap); } @@ -2548,7 +2548,7 @@ void NativeWeakMap::Set(Local v8_key, Local v8_value) { return; } int32_t hash = i::Object::GetOrCreateHash(isolate, key)->value(); - i::Runtime::WeakCollectionSet(weak_collection, key, value, hash); + i::JSWeakCollection::Set(weak_collection, key, value, hash); } @@ -2611,7 +2611,8 @@ bool NativeWeakMap::Delete(Local v8_key) { DCHECK(false); return false; } - return i::Runtime::WeakCollectionDelete(weak_collection, key); + int32_t hash = i::Object::GetOrCreateHash(isolate, key)->value(); + return i::JSWeakCollection::Delete(weak_collection, key, hash); } diff --git a/src/objects.cc b/src/objects.cc index ab32fbee15..eb627db2cc 100644 --- a/src/objects.cc +++ b/src/objects.cc @@ -15153,6 +15153,49 @@ void JSMap::Clear(Handle map) { } +void JSWeakCollection::Initialize(Handle weak_collection, + Isolate* isolate) { + DCHECK_EQ(0, weak_collection->map()->GetInObjectProperties()); + Handle table = ObjectHashTable::New(isolate, 0); + weak_collection->set_table(*table); +} + + +void JSWeakCollection::Set(Handle weak_collection, + Handle key, Handle value, + int32_t hash) { + DCHECK(key->IsJSReceiver() || key->IsSymbol()); + Handle table( + ObjectHashTable::cast(weak_collection->table())); + DCHECK(table->IsKey(*key)); + Handle new_table = + ObjectHashTable::Put(table, key, value, hash); + weak_collection->set_table(*new_table); + if (*table != *new_table) { + // Zap the old table since we didn't record slots for its elements. + table->FillWithHoles(0, table->length()); + } +} + + +bool JSWeakCollection::Delete(Handle weak_collection, + Handle key, int32_t hash) { + DCHECK(key->IsJSReceiver() || key->IsSymbol()); + Handle table( + ObjectHashTable::cast(weak_collection->table())); + DCHECK(table->IsKey(*key)); + bool was_present = false; + Handle new_table = + ObjectHashTable::Remove(table, key, &was_present, hash); + weak_collection->set_table(*new_table); + if (*table != *new_table) { + // Zap the old table since we didn't record slots for its elements. + table->FillWithHoles(0, table->length()); + } + return was_present; +} + + // Check if there is a break point at this code position. bool DebugInfo::HasBreakPoint(int code_position) { // Get the break point info object for this code position. diff --git a/src/objects.h b/src/objects.h index 6d547ef67a..19bd1654d4 100644 --- a/src/objects.h +++ b/src/objects.h @@ -9445,6 +9445,12 @@ class JSWeakCollection: public JSObject { // [next]: linked list of encountered weak maps during GC. DECL_ACCESSORS(next, Object) + static void Initialize(Handle collection, Isolate* isolate); + static void Set(Handle collection, Handle key, + Handle value, int32_t hash); + static bool Delete(Handle collection, Handle key, + int32_t hash); + static const int kTableOffset = JSObject::kHeaderSize; static const int kNextOffset = kTableOffset + kPointerSize; static const int kSize = kNextOffset + kPointerSize; diff --git a/src/runtime/runtime-collections.cc b/src/runtime/runtime-collections.cc index 0dfa1ee304..32340e5acb 100644 --- a/src/runtime/runtime-collections.cc +++ b/src/runtime/runtime-collections.cc @@ -271,19 +271,11 @@ RUNTIME_FUNCTION(Runtime_MapIteratorNext) { } -void Runtime::WeakCollectionInitialize( - Isolate* isolate, Handle weak_collection) { - DCHECK_EQ(0, weak_collection->map()->GetInObjectProperties()); - Handle table = ObjectHashTable::New(isolate, 0); - weak_collection->set_table(*table); -} - - RUNTIME_FUNCTION(Runtime_WeakCollectionInitialize) { HandleScope scope(isolate); DCHECK(args.length() == 1); CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); - Runtime::WeakCollectionInitialize(isolate, weak_collection); + JSWeakCollection::Initialize(weak_collection, isolate); return *weak_collection; } @@ -318,32 +310,6 @@ RUNTIME_FUNCTION(Runtime_WeakCollectionHas) { } -bool Runtime::WeakCollectionDelete(Handle weak_collection, - Handle key) { - int32_t hash = - Object::GetOrCreateHash(weak_collection->GetIsolate(), key)->value(); - return WeakCollectionDelete(weak_collection, key, hash); -} - - -bool Runtime::WeakCollectionDelete(Handle weak_collection, - Handle key, int32_t hash) { - DCHECK(key->IsJSReceiver() || key->IsSymbol()); - Handle table( - ObjectHashTable::cast(weak_collection->table())); - DCHECK(table->IsKey(*key)); - bool was_present = false; - Handle new_table = - ObjectHashTable::Remove(table, key, &was_present, hash); - weak_collection->set_table(*new_table); - if (*table != *new_table) { - // Zap the old table since we didn't record slots for its elements. - table->FillWithHoles(0, table->length()); - } - return was_present; -} - - RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) { HandleScope scope(isolate); DCHECK(args.length() == 3); @@ -354,28 +320,11 @@ RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) { Handle table( ObjectHashTable::cast(weak_collection->table())); RUNTIME_ASSERT(table->IsKey(*key)); - bool was_present = Runtime::WeakCollectionDelete(weak_collection, key, hash); + bool was_present = JSWeakCollection::Delete(weak_collection, key, hash); return isolate->heap()->ToBoolean(was_present); } -void Runtime::WeakCollectionSet(Handle weak_collection, - Handle key, Handle value, - int32_t hash) { - DCHECK(key->IsJSReceiver() || key->IsSymbol()); - Handle table( - ObjectHashTable::cast(weak_collection->table())); - DCHECK(table->IsKey(*key)); - Handle new_table = - ObjectHashTable::Put(table, key, value, hash); - weak_collection->set_table(*new_table); - if (*table != *new_table) { - // Zap the old table since we didn't record slots for its elements. - table->FillWithHoles(0, table->length()); - } -} - - RUNTIME_FUNCTION(Runtime_WeakCollectionSet) { HandleScope scope(isolate); DCHECK(args.length() == 4); @@ -387,7 +336,7 @@ RUNTIME_FUNCTION(Runtime_WeakCollectionSet) { Handle table( ObjectHashTable::cast(weak_collection->table())); RUNTIME_ASSERT(table->IsKey(*key)); - Runtime::WeakCollectionSet(weak_collection, key, value, hash); + JSWeakCollection::Set(weak_collection, key, value, hash); return *weak_collection; } @@ -425,7 +374,7 @@ RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { HandleScope scope(isolate); DCHECK(args.length() == 0); Handle weakmap = isolate->factory()->NewJSWeakMap(); - Runtime::WeakCollectionInitialize(isolate, weakmap); + JSWeakCollection::Initialize(weakmap, isolate); return *weakmap; } } // namespace internal diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index f0a3d452d9..afd09473ff 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -1168,7 +1168,7 @@ class Runtime : public AllStatic { SharedFlag shared = SharedFlag::kNotShared); enum TypedArrayId { - // arrayIds below should be synchromized with typedarray.js natives. + // arrayIds below should be synchronized with typedarray.js natives. ARRAY_ID_UINT8 = 1, ARRAY_ID_INT8 = 2, ARRAY_ID_UINT16 = 3, @@ -1191,16 +1191,6 @@ class Runtime : public AllStatic { Isolate* isolate, Handle literals, Handle elements, bool is_strong); - static void WeakCollectionInitialize( - Isolate* isolate, Handle weak_collection); - static void WeakCollectionSet(Handle weak_collection, - Handle key, Handle value, - int32_t hash); - static bool WeakCollectionDelete(Handle weak_collection, - Handle key); - static bool WeakCollectionDelete(Handle weak_collection, - Handle key, int32_t hash); - static MaybeHandle GetInternalProperties(Isolate* isolate, Handle); diff --git a/test/cctest/test-weakmaps.cc b/test/cctest/test-weakmaps.cc index 31b812e287..6cab081b8b 100644 --- a/test/cctest/test-weakmaps.cc +++ b/test/cctest/test-weakmaps.cc @@ -89,9 +89,9 @@ TEST(Weakness) { Handle object = factory->NewJSObjectFromMap(map); Handle smi(Smi::FromInt(23), isolate); int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); - Runtime::WeakCollectionSet(weakmap, key, object, hash); + JSWeakCollection::Set(weakmap, key, object, hash); int32_t object_hash = Object::GetOrCreateHash(isolate, object)->value(); - Runtime::WeakCollectionSet(weakmap, object, smi, object_hash); + JSWeakCollection::Set(weakmap, object, smi, object_hash); } CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); @@ -147,7 +147,7 @@ TEST(Shrinking) { Handle object = factory->NewJSObjectFromMap(map); Handle smi(Smi::FromInt(i), isolate); int32_t object_hash = Object::GetOrCreateHash(isolate, object)->value(); - Runtime::WeakCollectionSet(weakmap, object, smi, object_hash); + JSWeakCollection::Set(weakmap, object, smi, object_hash); } } @@ -196,7 +196,7 @@ TEST(Regress2060a) { CHECK(!heap->InNewSpace(object->address())); CHECK(!first_page->Contains(object->address())); int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); - Runtime::WeakCollectionSet(weakmap, key, object, hash); + JSWeakCollection::Set(weakmap, key, object, hash); } } @@ -239,7 +239,7 @@ TEST(Regress2060b) { for (int i = 0; i < 32; i++) { Handle smi(Smi::FromInt(i), isolate); int32_t hash = Object::GetOrCreateHash(isolate, keys[i])->value(); - Runtime::WeakCollectionSet(weakmap, keys[i], smi, hash); + JSWeakCollection::Set(weakmap, keys[i], smi, hash); } // Force compacting garbage collection. The subsequent collections are used diff --git a/test/cctest/test-weaksets.cc b/test/cctest/test-weaksets.cc index 3595af288f..1abf1a69a7 100644 --- a/test/cctest/test-weaksets.cc +++ b/test/cctest/test-weaksets.cc @@ -90,7 +90,7 @@ TEST(WeakSet_Weakness) { HandleScope scope(isolate); Handle smi(Smi::FromInt(23), isolate); int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); - Runtime::WeakCollectionSet(weakset, key, smi, hash); + JSWeakCollection::Set(weakset, key, smi, hash); } CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements()); @@ -146,7 +146,7 @@ TEST(WeakSet_Shrinking) { Handle object = factory->NewJSObjectFromMap(map); Handle smi(Smi::FromInt(i), isolate); int32_t hash = Object::GetOrCreateHash(isolate, object)->value(); - Runtime::WeakCollectionSet(weakset, object, smi, hash); + JSWeakCollection::Set(weakset, object, smi, hash); } } @@ -195,7 +195,7 @@ TEST(WeakSet_Regress2060a) { CHECK(!heap->InNewSpace(object->address())); CHECK(!first_page->Contains(object->address())); int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); - Runtime::WeakCollectionSet(weakset, key, object, hash); + JSWeakCollection::Set(weakset, key, object, hash); } } @@ -238,7 +238,7 @@ TEST(WeakSet_Regress2060b) { for (int i = 0; i < 32; i++) { Handle smi(Smi::FromInt(i), isolate); int32_t hash = Object::GetOrCreateHash(isolate, keys[i])->value(); - Runtime::WeakCollectionSet(weakset, keys[i], smi, hash); + JSWeakCollection::Set(weakset, keys[i], smi, hash); } // Force compacting garbage collection. The subsequent collections are used