GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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.

Sign up for GitHub

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

Closed
jasnell wants to merge 4 commits into nodejs:main from jasnell:consolidate-cpp-structural

Uh oh!

There was an error while loading. Please reload this page.

Closed

src: reduce duplication through consolidation #67497

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
58bca61
dns: share reply parsing skeleton between record parsers
jasnell Nov 15, 2026
b6fcc7f
src: share snapshot Serialize()/Deserialize() logic for binding data
jasnell Nov 15, 2026
6e1015f
fs: check permissions once in bindings with async and sync paths
jasnell Nov 15, 2026
5f73d63
fs: use GetAsyncReqWrap() in the remaining async/sync bindings
jasnell Nov 15, 2026
File filter
Filter by extension

Clear filters
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
Diff view
Prev Previous commit
Next Next commit
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.

@jasnell
jasnell committed Nov 15, 2026
commit b6fcc7f4df57ba4498f176a63dbfbe2cb79d5b4b

There are no files selected for viewing

15 changes: 3 additions & 12 deletions 15 src/encoding_binding.cc
Show comments
View file
Open in desktop
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
5 changes: 1 addition & 4 deletions 5 src/glob/node_glob.cc
Show comments
View file
Open in desktop
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
9 changes: 2 additions & 7 deletions 9 src/node_blob.cc
Show comments
View file
Open in desktop
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
12 changes: 3 additions & 9 deletions 12 src/node_diagnostics_channel.cc
Show comments
View file
Open in desktop
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
13 changes: 3 additions & 10 deletions 13 src/node_file.cc
Show comments
View file
Open in desktop
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
9 changes: 2 additions & 7 deletions 9 src/node_modules.cc
Show comments
View file
Open in desktop
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
14 changes: 3 additions & 11 deletions 14 src/node_process_methods.cc
Show comments
View file
Open in desktop
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
9 changes: 9 additions & 0 deletions 9 src/node_realm-inl.h
Show comments
View file
Open in desktop
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
7 changes: 7 additions & 0 deletions 7 src/node_realm.h
Show comments
View file
Open in desktop
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
14 changes: 3 additions & 11 deletions 14 src/node_snapshotable.cc
Show comments
View file
Open in desktop
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
11 changes: 11 additions & 0 deletions 11 src/node_snapshotable.h
Show comments
View file
Open in desktop
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
9 changes: 2 additions & 7 deletions 9 src/node_url.cc
Show comments
View file
Open in desktop
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
14 changes: 3 additions & 11 deletions 14 src/node_v8.cc
Show comments
View file
Open in desktop
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
10 changes: 2 additions & 8 deletions 10 src/timers.cc
Show comments
View file
Open in desktop
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.