Skip to content

Commit

Permalink
fixup! src: use type_int as binding data store key
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Feb 12, 2023
1 parent 0a0df16 commit 4aae96e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,11 @@ inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
context->GetAlignedPointerFromEmbedderData(
ContextEmbedderIndex::kBindingListIndex));
DCHECK_NOT_NULL(map);
auto it = map->find(static_cast<uint8_t>(T::type_int));
if (UNLIKELY(it == map->end())) return nullptr;
T* result = static_cast<T*>(it->second.get());
constexpr size_t binding_index = static_cast<size_t>(T::type_int);
static_assert(binding_index < std::tuple_size_v<BindingDataStore>);
auto ptr = (*map)[binding_index];
if (UNLIKELY(!ptr)) return nullptr;
T* result = static_cast<T*>(ptr.get());
DCHECK_NOT_NULL(result);
DCHECK_EQ(result->env(), GetCurrent(context));
return result;
Expand All @@ -237,8 +239,10 @@ inline T* Environment::AddBindingData(
context->GetAlignedPointerFromEmbedderData(
ContextEmbedderIndex::kBindingListIndex));
DCHECK_NOT_NULL(map);
auto result = map->emplace(static_cast<uint8_t>(T::type_int), item);
CHECK(result.second);
constexpr size_t binding_index = static_cast<size_t>(T::type_int);
static_assert(binding_index < std::tuple_size_v<BindingDataStore>);
CHECK(!(*map)[binding_index]); // Should not insert the binding twice.
(*map)[binding_index] = item;
DCHECK_EQ(GetBindingData<T>(context), item.get());
return item.get();
}
Expand Down
4 changes: 3 additions & 1 deletion src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,9 @@ MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
void Environment::RunCleanup() {
started_cleanup_ = true;
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
bindings_.clear();
for (size_t i = 0; i < bindings_.size(); ++i) {
bindings_[i].reset();
}
// Only BaseObject's cleanups are registered as per-realm cleanup hooks now.
// Defer the BaseObject cleanup after handles are cleaned up.
CleanupHandles();
Expand Down
4 changes: 3 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,9 @@ class Environment : public MemoryRetainer {
template <typename T>
static inline T* GetBindingData(v8::Local<v8::Context> context);

typedef std::unordered_map<uint8_t, BaseObjectPtr<BaseObject>>
typedef std::array<BaseObjectPtr<BaseObject>,
static_cast<size_t>(
EmbedderObjectType::kEmbedderObjectTypeCount)>
BindingDataStore;

// Create an Environment without initializing a main Context. Use
Expand Down
1 change: 1 addition & 0 deletions src/node_snapshotable.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum class EmbedderObjectType : uint8_t {
#define V(PropertyName, NativeType) k_##PropertyName,
SERIALIZABLE_OBJECT_TYPES(V) UNSERIALIZABLE_OBJECT_TYPES(V)
#undef V
kEmbedderObjectTypeCount
};

typedef size_t SnapshotIndex;
Expand Down

0 comments on commit 4aae96e

Please sign in to comment.