Skip to content

src: deduplicate/consolidate multiple sources - #67496

Open
jasnell wants to merge 11 commits into
nodejs:mainfrom
jasnell:consolidate-cpp-duplicates-2
Open

jasnell wants to merge 11 commits into
nodejs:mainfrom
jasnell:consolidate-cpp-duplicates-2

Conversation

@jasnell

@jasnell jasnell commented Nov 15, 2026

Copy link
Copy Markdown
Member

See individual commits. Deduplicate/consolidate multiple bits.

The loong64, ppc64, riscv64, and s390x fast trampoline emitters each
duplicated the signature validation, executable memory allocation,
cache flush and mprotect sequence, trampoline release, and (for the
fixed-width 32-bit ISAs) the instruction word emitters. Move them into
a shared scalar_trampoline.h header so each platform file only
contains its architecture-specific instruction encoding.

Assisted-by: Claude
The System V and Win64 halves of the x64 fast trampoline emitter each
defined identical copies of the register constants, the byte/word
emitters, the REX/ModRM, mov, call, jmp, stack adjustment, and ret
encoders, and the narrow return normalization helpers. Define them
once ahead of the ABI-specific code.

Assisted-by: Claude
napi_create_error(), napi_create_type_error(), napi_create_range_error()
and node_api_create_syntax_error() differed only in the v8::Exception
factory used, as did the corresponding napi_throw_*() functions. Move
the bodies into internal CreateError() and ThrowError() helpers that
take the factory. The exported functions and their signatures are
unchanged, so there is no ABI impact.

Assisted-by: Claude
DynamicLibrary::UnregisterCallback(), RefCallback(), and UnrefCallback()
each duplicated the closed-library check, pointer argument validation,
and callback map lookup. Move them into a shared FindCallback() helper.

Assisted-by: Claude
The network and DOM storage agents repeated the same pattern dozens of
times: read a property with an ObjectGet*() helper and throw a
"Missing <name> in event" TypeError if it was absent. Add
ObjectGetRequired() overloads that combine the two, move the duplicated
ThrowEventError() into inspector_object_utils, and use them throughout
both agents. Error messages are unchanged.

Assisted-by: Claude
The SetMethod(), SetFastMethod(), SetMethodNoSideEffect(),
SetProtoMethod(), SetInstanceMethod(), and SetProto*Dispose() variants
each repeated the same function template construction, internalized
name string creation, and installation code, differing only in side
effect type, receiver signature, and target. Factor that into a small
set of internal helpers.

Assisted-by: Claude
When the creation context of the import.meta object has no associated
Realm, the getter threw ERR_INVALID_INVOCATION but then fell through
and dereferenced the null Realm. Return after throwing, as the other
error paths in the getter already do.

Assisted-by: Claude
ImportMetaResolveLazyGetter() and PathHelpersLazyGetter() duplicated
the code that retrieves the creation context from the holder object.
Move it into a shared GetHolderCreationContext() helper.

Assisted-by: Claude
All three callers of report::WriteNodeReport() computed the
excludeNetwork and excludeEnv options the same way, falling back to the
process-wide defaults when there is no Environment. Do that once inside
WriteNodeReport() instead.

Assisted-by: Claude
FastHas() duplicated the scope parsing in FastHasResource(). Move the
shared logic into FastHasImpl(), with FastHas() passing an empty
resource.

Assisted-by: Claude
StreamBase::WriteBuffer() and WriteString() duplicated the code that
creates a request object when needed and attaches the handle being sent
over an IPC pipe. Move it into a shared PrepareSendHandle() helper.

Assisted-by: Claude
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/ffi
  • @nodejs/gyp
  • @nodejs/inspector
  • @nodejs/loaders
  • @nodejs/node-api
  • @nodejs/security-wg
  • @nodejs/vm

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Nov 15, 2026
@jasnell
jasnell requested review from addaleax and mcollina November 15, 2026 19:01
@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.41497% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.69%. Comparing base (d105ef7) to head (88b79b8).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/stream_base.cc 34.78% 11 Missing and 4 partials ⚠️
src/util.cc 92.30% 9 Missing ⚠️
src/module_wrap.cc 45.45% 3 Missing and 3 partials ⚠️
src/js_native_api_v8.cc 85.00% 0 Missing and 3 partials ⚠️
src/permission/permission.cc 50.00% 2 Missing and 1 partial ⚠️
src/node_ffi.cc 95.23% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67496      +/-   ##
==========================================
+ Coverage   90.67%   90.69%   +0.02%     
==========================================
  Files         799      799              
  Lines      280245   280016     -229     
  Branches    54125    54073      -52     
==========================================
- Hits       254100   253968     -132     
+ Misses      16280    16194      -86     
+ Partials     9865     9854      -11     
Files with missing lines Coverage Δ
src/inspector/dom_storage_agent.cc 81.28% <100.00%> (-1.98%) ⬇️
src/inspector/inspector_object_utils.cc 93.05% <100.00%> (+3.25%) ⬆️
src/inspector/network_agent.cc 85.51% <100.00%> (-2.52%) ⬇️
src/node_ffi.h 82.60% <ø> (ø)
src/node_report.cc 91.92% <100.00%> (-0.47%) ⬇️
src/node_ffi.cc 75.93% <95.23%> (+1.11%) ⬆️
src/js_native_api_v8.cc 77.29% <85.00%> (+0.65%) ⬆️
src/permission/permission.cc 78.90% <50.00%> (+3.90%) ⬆️
src/module_wrap.cc 74.80% <45.45%> (+0.39%) ⬆️
src/util.cc 88.24% <92.30%> (+0.61%) ⬆️
... and 1 more

... and 26 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants