GeezerVison is showing github.com — src: reduce duplication through consolidation by jasnell · Pull Request #67497 · nodejs/node
src: reduce duplication through consolidation
#67497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.cc
(11)
.h
(3)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
src: share snapshot Serialize()/Deserialize() logic for binding data
Eleven snapshotable binding data classes implemented Deserialize() with
the same sequence of opening a handle scope, looking up the Realm, and
recreating the binding data on the holder, and six of them implemented
Serialize() by handing over the InternalFieldInfo allocated in
PrepareForSerialization(). Add Realm::DeserializeBindingData() and
TakeInternalFieldInfo() helpers and use them.
Assisted-by: Claude
Signed-off-by: James M Snell <[email protected] >
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit b6fcc7f4df57ba4498f176a63dbfbe2cb79d5b4b
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -20,7 +20,6 @@ using v8::BackingStoreInitializationMode;
using v8::BackingStoreOnFailureMode;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
Expand Down
Expand Up
@@ -67,10 +66,7 @@ bool BindingData::PrepareForSerialization(Local<Context> context,
}
InternalFieldInfoBase* BindingData::Serialize (int index) {
DCHECK_IS_SNAPSHOT_SLOT (index);
InternalFieldInfo* info = internal_field_info_;
internal_field_info_ = nullptr ;
return info;
return TakeInternalFieldInfo (index, &internal_field_info_);
}
// The following code is adapted from Cloudflare workers.
Expand Down
Expand Up
@@ -185,13 +181,8 @@ void BindingData::Deserialize(Local<Context> context,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT (index);
HandleScope scope (Isolate::GetCurrent ());
Realm* realm = Realm::GetCurrent (context);
// Recreate the buffer in the constructor.
InternalFieldInfo* casted_info = static_cast <InternalFieldInfo*>(info);
BindingData* binding =
realm->AddBindingData <BindingData>(holder, casted_info);
CHECK_NOT_NULL (binding);
Realm::DeserializeBindingData<BindingData>(
context, holder, static_cast <InternalFieldInfo*>(info));
}
void BindingData::EncodeInto (const FunctionCallbackInfo<Value>& args) {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -178,10 +178,7 @@ void BindingData::Deserialize(Local<Context> context,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT(index);
Realm* realm = Realm::GetCurrent(context);
HandleScope scope(realm->isolate());
BindingData* binding = realm->AddBindingData<BindingData>(holder, info);
CHECK_NOT_NULL(binding);
Realm::DeserializeBindingData<BindingData>(context, holder, info);
}
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -584,10 +584,7 @@ void BlobBindingData::Deserialize(Local<Context> context,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT (index);
HandleScope scope (Isolate::GetCurrent ());
Realm* realm = Realm::GetCurrent (context);
BlobBindingData* binding = realm->AddBindingData <BlobBindingData>(holder);
CHECK_NOT_NULL (binding);
Realm::DeserializeBindingData<BlobBindingData>(context, holder);
}
bool BlobBindingData::PrepareForSerialization (Local<Context> context,
Expand All
@@ -600,9 +597,7 @@ bool BlobBindingData::PrepareForSerialization(Local<Context> context,
InternalFieldInfoBase* BlobBindingData::Serialize (int index) {
DCHECK_IS_SNAPSHOT_SLOT (index);
InternalFieldInfo* info =
InternalFieldInfoBase::New<InternalFieldInfo>(type ());
return info;
return InternalFieldInfoBase::New<InternalFieldInfo>(type ());
}
void Blob::RegisterExternalReferences (ExternalReferenceRegistry* registry) {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -125,22 +125,16 @@ bool BindingData::PrepareForSerialization(Local<Context> context,
}
InternalFieldInfoBase* BindingData::Serialize(int index) {
DCHECK_IS_SNAPSHOT_SLOT(index);
InternalFieldInfo* info = internal_field_info_;
internal_field_info_ = nullptr;
return info;
return TakeInternalFieldInfo(index, &internal_field_info_);
}
void BindingData::Deserialize(Local<Context> context,
Local<Object> holder,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT(index);
HandleScope scope(Isolate::GetCurrent());
Realm* realm = Realm::GetCurrent(context);
BindingData* binding = realm->AddBindingData<BindingData>(
holder, static_cast<InternalFieldInfo*>(info));
CHECK_NOT_NULL(binding);
Realm::DeserializeBindingData<BindingData>(
context, holder, static_cast<InternalFieldInfo*>(info));
}
void BindingData::SetChannelStatusCallback(uint32_t index,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -5632,12 +5632,8 @@ void BindingData::Deserialize(Local<Context> context,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT(index);
HandleScope scope(Isolate::GetCurrent());
Realm* realm = Realm::GetCurrent(context);
InternalFieldInfo* casted_info = static_cast<InternalFieldInfo*>(info);
BindingData* binding =
realm->AddBindingData<BindingData>(holder, casted_info);
CHECK_NOT_NULL(binding);
Realm::DeserializeBindingData<BindingData>(
context, holder, static_cast<InternalFieldInfo*>(info));
}
bool BindingData::PrepareForSerialization(Local<Context> context,
Expand All
@@ -5659,10 +5655,7 @@ bool BindingData::PrepareForSerialization(Local<Context> context,
}
InternalFieldInfoBase* BindingData::Serialize(int index) {
DCHECK_IS_SNAPSHOT_SLOT(index);
InternalFieldInfo* info = internal_field_info_;
internal_field_info_ = nullptr;
return info;
return TakeInternalFieldInfo(index, &internal_field_info_);
}
#ifdef _WIN32
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -67,20 +67,15 @@ bool BindingData::PrepareForSerialization(v8::Local<v8::Context> context,
InternalFieldInfoBase* BindingData::Serialize (int index) {
DCHECK_IS_SNAPSHOT_SLOT (index);
InternalFieldInfo* info =
InternalFieldInfoBase::New<InternalFieldInfo>(type ());
return info;
return InternalFieldInfoBase::New<InternalFieldInfo>(type ());
}
void BindingData::Deserialize (v8::Local<v8::Context> context,
v8::Local<v8::Object> holder,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT (index);
HandleScope scope (Isolate::GetCurrent ());
Realm* realm = Realm::GetCurrent (context);
BindingData* binding = realm->AddBindingData <BindingData>(holder);
CHECK_NOT_NULL (binding);
Realm::DeserializeBindingData<BindingData>(context, holder);
}
Local<Array> BindingData::PackageConfig::Serialize (Realm* realm) const {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -752,24 +752,16 @@ bool BindingData::PrepareForSerialization(Local<Context> context,
}
InternalFieldInfoBase* BindingData::Serialize (int index) {
DCHECK_IS_SNAPSHOT_SLOT (index);
InternalFieldInfo* info = internal_field_info_;
internal_field_info_ = nullptr ;
return info;
return TakeInternalFieldInfo (index, &internal_field_info_);
}
void BindingData::Deserialize (Local<Context> context,
Local<Object> holder,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT (index);
v8::HandleScope scope (Isolate::GetCurrent ());
Realm* realm = Realm::GetCurrent (context);
// Recreate the buffer in the constructor.
InternalFieldInfo* casted_info = static_cast <InternalFieldInfo*>(info);
BindingData* binding =
realm->AddBindingData <BindingData>(holder, casted_info);
CHECK_NOT_NULL (binding);
Realm::DeserializeBindingData<BindingData>(
context, holder, static_cast <InternalFieldInfo*>(info));
}
static void SetEmitWarningSync (const FunctionCallbackInfo<Value>& args) {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -108,6 +108,15 @@ inline T* Realm::AddBindingData(v8::Local<v8::Object> target, Args&&... args) {
return item.get ();
}
template <std::derived_from<BaseObject> T, typename ... Args>
void Realm::DeserializeBindingData (v8::Local<v8::Context> context,
v8::Local<v8::Object> holder,
Args&&... args) {
v8::HandleScope scope (v8::Isolate::GetCurrent ());
Realm* realm = Realm::GetCurrent (context);
CHECK_NOT_NULL (realm->AddBindingData <T>(holder, std::forward<Args>(args)...));
}
inline BindingDataStore* Realm::binding_data_store () {
return &binding_data_store_;
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -128,6 +128,13 @@ class Realm : public MemoryRetainer {
// GetBindingData<T>(args) later.
template <std::derived_from<BaseObject> T, typename ... Args>
T* AddBindingData (v8::Local<v8::Object> target, Args&&... args);
// Recreates binding data of type T on `holder` in the realm of `context`
// when deserializing a snapshot. Used to implement the static Deserialize()
// method of snapshotable binding data.
template <std::derived_from<BaseObject> T, typename ... Args>
static void DeserializeBindingData (v8::Local<v8::Context> context,
v8::Local<v8::Object> holder,
Args&&... args);
template <typename T, typename U>
static inline T* GetBindingData (const v8::PropertyCallbackInfo<U>& info);
template <typename T>
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1701,24 +1701,16 @@ bool BindingData::PrepareForSerialization(Local<Context> context,
}
InternalFieldInfoBase* BindingData::Serialize (int index) {
DCHECK_IS_SNAPSHOT_SLOT (index);
InternalFieldInfo* info = internal_field_info_;
internal_field_info_ = nullptr ;
return info;
return TakeInternalFieldInfo (index, &internal_field_info_);
}
void BindingData::Deserialize (Local<Context> context,
Local<Object> holder,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT (index);
v8::HandleScope scope (Isolate::GetCurrent ());
Realm* realm = Realm::GetCurrent (context);
// Recreate the buffer in the constructor.
InternalFieldInfo* casted_info = static_cast <InternalFieldInfo*>(info);
BindingData* binding =
realm->AddBindingData <BindingData>(holder, casted_info);
CHECK_NOT_NULL (binding);
Realm::DeserializeBindingData<BindingData>(
context, holder, static_cast <InternalFieldInfo*>(info));
}
void BindingData::MemoryInfo (MemoryTracker* tracker) const {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -8,6 +8,8 @@
#include " base_object.h"
#include " util.h"
#include < utility>
namespace node {
class Environment ;
Expand Down
Expand Up
@@ -140,6 +142,15 @@ void SerializeSnapshotableObjects(Realm* realm,
#define DCHECK_IS_SNAPSHOT_SLOT (index ) DCHECK_EQ (index, BaseObject::kSlot )
// Implements SnapshotableObject::Serialize() for objects that allocate their
// InternalFieldInfo in PrepareForSerialization(): ownership of `*info` is
// handed over to the caller and the field is reset.
template <typename Info>
InternalFieldInfoBase* TakeInternalFieldInfo (int index, Info** info) {
DCHECK_IS_SNAPSHOT_SLOT (index);
return std::exchange (*info, nullptr );
}
namespace mksnapshot {
class BindingData : public SnapshotableObject {
public:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -97,20 +97,15 @@ bool BindingData::PrepareForSerialization(Local<Context> context,
InternalFieldInfoBase* BindingData::Serialize (int index) {
DCHECK_IS_SNAPSHOT_SLOT (index);
InternalFieldInfo* info =
InternalFieldInfoBase::New<InternalFieldInfo>(type ());
return info;
return InternalFieldInfoBase::New<InternalFieldInfo>(type ());
}
void BindingData::Deserialize (Local<Context> context,
Local<Object> holder,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT (index);
HandleScope scope (Isolate::GetCurrent ());
Realm* realm = Realm::GetCurrent (context);
BindingData* binding = realm->AddBindingData <BindingData>(holder);
CHECK_NOT_NULL (binding);
Realm::DeserializeBindingData<BindingData>(context, holder);
}
#ifndef LARGEST_ASCII_CHAR_CODE_TO_ENCODE
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -166,20 +166,12 @@ void BindingData::Deserialize(Local<Context> context,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT(index);
HandleScope scope(Isolate::GetCurrent());
Realm* realm = Realm::GetCurrent(context);
// Recreate the buffer in the constructor.
InternalFieldInfo* casted_info = static_cast<InternalFieldInfo*>(info);
BindingData* binding =
realm->AddBindingData<BindingData>(holder, casted_info);
CHECK_NOT_NULL(binding);
Realm::DeserializeBindingData<BindingData>(
context, holder, static_cast<InternalFieldInfo*>(info));
}
InternalFieldInfoBase* BindingData::Serialize(int index) {
DCHECK_IS_SNAPSHOT_SLOT(index);
InternalFieldInfo* info = internal_field_info_;
internal_field_info_ = nullptr;
return info;
return TakeInternalFieldInfo(index, &internal_field_info_);
}
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -104,21 +104,15 @@ bool BindingData::PrepareForSerialization(Local<Context> context,
InternalFieldInfoBase* BindingData::Serialize (int index) {
DCHECK_IS_SNAPSHOT_SLOT (index);
InternalFieldInfo* info =
InternalFieldInfoBase::New<InternalFieldInfo>(type ());
return info;
return InternalFieldInfoBase::New<InternalFieldInfo>(type ());
}
void BindingData::Deserialize (Local<Context> context,
Local<Object> holder,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT (index);
v8::HandleScope scope (Isolate::GetCurrent ());
Realm* realm = Realm::GetCurrent (context);
// Recreate the buffer in the constructor.
BindingData* binding = realm->AddBindingData <BindingData>(holder);
CHECK_NOT_NULL (binding);
Realm::DeserializeBindingData<BindingData>(context, holder);
}
v8::CFunction BindingData::fast_get_libuv_now_ (
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.