Skip to content

node-api: add napi_create_external_sharedarraybuffer#62623

Open
bnoordhuis wants to merge 9 commits intonodejs:mainfrom
bnoordhuis:fix62259
Open

node-api: add napi_create_external_sharedarraybuffer#62623
bnoordhuis wants to merge 9 commits intonodejs:mainfrom
bnoordhuis:fix62259

Conversation

@bnoordhuis
Copy link
Copy Markdown
Member

Creates a SharedArrayBuffer from externally managed memory.

Fixes: #62259

Creates a SharedArrayBuffer from externally managed memory.

Fixes: nodejs#62259
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/node-api

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API. labels Apr 7, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 7, 2026

Codecov Report

❌ Patch coverage is 72.00000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.80%. Comparing base (f48ac91) to head (a021c17).
⚠️ Report is 41 commits behind head on main.

Files with missing lines Patch % Lines
src/js_native_api_v8.cc 72.00% 0 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62623      +/-   ##
==========================================
+ Coverage   89.77%   89.80%   +0.03%     
==========================================
  Files         697      699       +2     
  Lines      215749   216254     +505     
  Branches    41304    41350      +46     
==========================================
+ Hits       193681   194204     +523     
+ Misses      14161    14139      -22     
- Partials     7907     7911       +4     
Files with missing lines Coverage Δ
src/js_native_api_v8.cc 76.43% <72.00%> (-0.06%) ⬇️

... and 53 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.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ronag ronag added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 7, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 7, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@ronag ronag added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 7, 2026
@ronag ronag requested a review from vmoroz April 7, 2026 14:16
@legendecas legendecas removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 7, 2026
@legendecas legendecas moved this from Need Triage to In Progress in Node-API Team Project Apr 7, 2026
napi_env env,
void* external_data,
size_t byte_length,
void (*finalize_cb)(void* external_data, void* finalize_hint),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically do not use the inline function pointer types.
Can we use the node_api_basic_finalize here?
I guess it should OK to document there that env is null there.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that intentionally, of course. Passing nullptr is just setting up users for segfaults.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the semantic of this finalizer is different from node_api_basic_finalize. node_api_basic_finalize is still invoked on the JS thread. But this finalizer could be invoked on non-main-JS thread. I think it is worth a dedicated type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it would be better to define a new type alias for it. It is better to follow the established patterns.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on dedicated type

if (finalize_cb != nullptr) {
deleter_data = new FinalizerData{finalize_cb, finalize_hint};
}
auto unique_backing_store = v8::SharedArrayBuffer::NewBackingStore(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to avoid auto for readability when possible.
IMHO, we should only use it for lambdas and iterators.
In other trivial cases like here using the real types can help readability and see the issues sooner like with the missing std::move below.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The counterargument is that repeating oneself at the left- and right-hand side of an assignment often results in unwieldy line noise. auto is also used quite a bit elsewhere in this file, and not just for lambdas.

For the record: I'm pretty much the founder of Node's C++ house style and I've always been a-okay with judicious use of auto.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is okay to use auto when the type is clear: https://github.com/nodejs/node/blob/main/doc/contributing/cpp-style-guide.md#using-auto

I would find this auto good because the right hand side is clear that this is a v8 backing store.

@bnoordhuis
Copy link
Copy Markdown
Member Author

Review feedback incorporated, PTAL.

// Caveat emptor: it's indeterminate when the SharedArrayBuffer's backing
// store is reclaimed; at least some of the time it happens even before
// calling gc().
let sab = binding.newExternalSharedArrayBuffer();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion:

Suggested change
let sab = binding.newExternalSharedArrayBuffer();
let sab = binding.newExternalSharedArrayBuffer();
assert(util.types.isSharedArrayBuffer(sab));

"defines": [
'NAPI_VERSION=10'
"NAPI_EXPERIMENTAL",
"NAPI_VERSION=10",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NAPI_EXPERIMENTAL overrides NAPI_VERSION=10. It is better to avoid confusion and remove the NAPI_VERSION=10.

@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 10, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 10, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

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++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API.

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Missing napi_create_external_sharedarraybuffer

7 participants