From 0e83bae7cbb537fa10d7a4a577c2aaa3a7402997 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 17 Apr 2026 17:10:36 +0200 Subject: [PATCH] node-api: add napi_create_external_sharedarraybuffer Creates a SharedArrayBuffer from externally managed memory. Fixes: https://github.com/nodejs/node/issues/62259 PR-URL: https://github.com/nodejs/node/pull/62623 Reviewed-By: Matteo Collina Reviewed-By: Chengzhong Wu Reviewed-By: Vladimir Morozov Reviewed-By: Robert Nagy --- doc/api/n-api.md | 35 +++++++++++++++++++++ src/js_native_api.h | 10 ++++++ src/js_native_api_types.h | 4 +++ src/js_native_api_v8.cc | 42 +++++++++++++++++++++++++ test/node-api/test_buffer/binding.gyp | 3 +- test/node-api/test_buffer/test.js | 14 ++++++++- test/node-api/test_buffer/test_buffer.c | 26 +++++++++++++++ 7 files changed, 132 insertions(+), 2 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index b18048338f8..3616c55ee2d 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2565,6 +2565,41 @@ object just created has been garbage collected. JavaScript `ArrayBuffer`s are described in [Section ArrayBuffer objects][] of the ECMAScript Language Specification. +#### `node_api_create_external_sharedarraybuffer` + + + +```c +napi_status +node_api_create_external_sharedarraybuffer(napi_env env, + void* external_data, + size_t byte_length, + node_api_noenv_finalize finalize_cb, + void* finalize_hint, + napi_value* result) +``` + +* `[in] env`: The environment that the API is invoked under. +* `[in] external_data`: Pointer to the underlying byte buffer of the + `SharedArrayBuffer`. +* `[in] byte_length`: The length in bytes of the underlying buffer. +* `[in] finalize_cb`: Optional callback to call when the `SharedArrayBuffer` is + being collected. Called on an arbitrary thread. Because a `SharedArrayBuffer` + can outlive the environment it's created in, the callback does not receive a + reference to `env`. +* `[in] finalize_hint`: Optional hint to pass to the finalize callback during + collection. +* `[out] result`: A `napi_value` representing a JavaScript `SharedArrayBuffer`. + +Returns `napi_ok` if the API succeeded. + +Create a `SharedArrayBuffer` with externally managed memory. + +See the entry on [`napi_create_external_arraybuffer`][] for runtime +compatibility. + #### `napi_create_external_buffer`