Skip to content

Commit 4aae96e

Browse files
committed
fixup! src: use type_int as binding data store key
1 parent 0a0df16 commit 4aae96e

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

src/env-inl.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,11 @@ inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
218218
context->GetAlignedPointerFromEmbedderData(
219219
ContextEmbedderIndex::kBindingListIndex));
220220
DCHECK_NOT_NULL(map);
221-
auto it = map->find(static_cast<uint8_t>(T::type_int));
222-
if (UNLIKELY(it == map->end())) return nullptr;
223-
T* result = static_cast<T*>(it->second.get());
221+
constexpr size_t binding_index = static_cast<size_t>(T::type_int);
222+
static_assert(binding_index < std::tuple_size_v<BindingDataStore>);
223+
auto ptr = (*map)[binding_index];
224+
if (UNLIKELY(!ptr)) return nullptr;
225+
T* result = static_cast<T*>(ptr.get());
224226
DCHECK_NOT_NULL(result);
225227
DCHECK_EQ(result->env(), GetCurrent(context));
226228
return result;
@@ -237,8 +239,10 @@ inline T* Environment::AddBindingData(
237239
context->GetAlignedPointerFromEmbedderData(
238240
ContextEmbedderIndex::kBindingListIndex));
239241
DCHECK_NOT_NULL(map);
240-
auto result = map->emplace(static_cast<uint8_t>(T::type_int), item);
241-
CHECK(result.second);
242+
constexpr size_t binding_index = static_cast<size_t>(T::type_int);
243+
static_assert(binding_index < std::tuple_size_v<BindingDataStore>);
244+
CHECK(!(*map)[binding_index]); // Should not insert the binding twice.
245+
(*map)[binding_index] = item;
242246
DCHECK_EQ(GetBindingData<T>(context), item.get());
243247
return item.get();
244248
}

src/env.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,9 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
10181018
void Environment::RunCleanup() {
10191019
started_cleanup_ = true;
10201020
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
1021-
bindings_.clear();
1021+
for (size_t i = 0; i < bindings_.size(); ++i) {
1022+
bindings_[i].reset();
1023+
}
10221024
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
10231025
// Defer the BaseObject cleanup after handles are cleaned up.
10241026
CleanupHandles();

src/env.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,9 @@ class Environment : public MemoryRetainer {
601601
template <typename T>
602602
static inline T* GetBindingData(v8::Local<v8::Context> context);
603603

604-
typedef std::unordered_map<uint8_t, BaseObjectPtr<BaseObject>>
604+
typedef std::array<BaseObjectPtr<BaseObject>,
605+
static_cast<size_t>(
606+
EmbedderObjectType::kEmbedderObjectTypeCount)>
605607
BindingDataStore;
606608

607609
// Create an Environment without initializing a main Context. Use

src/node_snapshotable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum class EmbedderObjectType : uint8_t {
3737
#define V(PropertyName, NativeType) k_##PropertyName,
3838
SERIALIZABLE_OBJECT_TYPES(V) UNSERIALIZABLE_OBJECT_TYPES(V)
3939
#undef V
40+
kEmbedderObjectTypeCount
4041
};
4142

4243
typedef size_t SnapshotIndex;

0 commit comments

Comments
 (0)