Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
unify ArrayBuffer and SharedArrayBuffer info API
  • Loading branch information
mertcanaltin committed Sep 1, 2025
commit d8eca2ad012e9490c0169ccfa866f16dff8ed891
69 changes: 5 additions & 64 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3277,21 +3277,20 @@ napi_status napi_get_arraybuffer_info(napi_env env,
```

* `[in] env`: The environment that the API is invoked under.
* `[in] arraybuffer`: `napi_value` representing the `ArrayBuffer` being queried.
* `[in] arraybuffer`: `napi_value` representing the `ArrayBuffer` or `SharedArrayBuffer` being queried.
* `[out] data`: The underlying data buffer of the `ArrayBuffer`. If byte\_length
is `0`, this may be `NULL` or any other pointer value.
* `[out] byte_length`: Length in bytes of the underlying data buffer.

Returns `napi_ok` if the API succeeded.

This API is used to retrieve the underlying data buffer of an `ArrayBuffer` and
its length.
This API is used to retrieve the underlying data buffer of an `ArrayBuffer` or `SharedArrayBuffer` and its length.

_WARNING_: Use caution while using this API. The lifetime of the underlying data
buffer is managed by the `ArrayBuffer` even after it's returned. A
buffer is managed by the `ArrayBuffer` or `SharedArrayBuffer` even after it's returned. A
possible safe way to use this API is in conjunction with
[`napi_create_reference`][], which can be used to guarantee control over the
lifetime of the `ArrayBuffer`. It's also safe to use the returned data buffer
lifetime of the `ArrayBuffer` or `SharedArrayBuffer`. It's also safe to use the returned data buffer
within the same callback as long as there are no calls to other APIs that might
trigger a GC.

Expand Down Expand Up @@ -4246,7 +4245,7 @@ This API represents the invocation of the `ArrayBuffer` `IsDetachedBuffer`
operation as defined in [Section isDetachedBuffer][] of the ECMAScript Language
Specification.

## Working with JavaScript SharedArrayBuffers
### Working with JavaScript SharedArrayBuffers

SharedArrayBuffers are used to represent fixed-length binary data buffers
that can be shared across multiple workers. They are similar to ArrayBuffers
Expand Down Expand Up @@ -4307,64 +4306,6 @@ a typed array or `DataView` object would need to be created.
JavaScript `SharedArrayBuffer` objects are described in
[Section 24.2][] of the ECMAScript Language Specification.

### `napi_get_sharedarraybuffer_info`

<!-- YAML
added: REPLACEME
napiVersion: 10
-->

```c
napi_status napi_get_sharedarraybuffer_info(napi_env env,
napi_value sharedarraybuffer,
void** data,
size_t* byte_length)
```

* `[in] env`: The environment that the API is invoked under.
* `[in] sharedarraybuffer`: `napi_value` representing the `SharedArrayBuffer` being queried.
* `[out] data`: The underlying data buffer of the `SharedArrayBuffer`. If byte\_length
is `0`, this may be `NULL` or any other pointer value.
* `[out] byte_length`: The length in bytes of the underlying data buffer.

Returns `napi_ok` if the API succeeded.

This API is used to retrieve the underlying data buffer of a `SharedArrayBuffer` and
its length.

_Warning_: Use caution while using this API. The lifetime of the underlying data
buffer is managed by the `SharedArrayBuffer` even after it's returned. A
possible safe way to use this API is in conjunction with
[`napi_create_reference`][], which can be used to guarantee control over the
lifetime of the `SharedArrayBuffer`. It's also safe to use the returned data buffer
within the same callback as long as there are no calls to other APIs that might
trigger a GC.

#### Example

```c
napi_value shared_array_buffer;
void* shared_data;
size_t shared_byte_length;

// Create a SharedArrayBuffer
napi_status status = napi_create_sharedarraybuffer(env, 1024, &shared_data, &shared_array_buffer);
if (status != napi_ok) {
// Handle error
}

// Check if a value is a SharedArrayBuffer
bool is_shared_array_buffer;
status = napi_is_sharedarraybuffer(env, shared_array_buffer, &is_shared_array_buffer);
if (status == napi_ok && is_shared_array_buffer) {
// Get SharedArrayBuffer info
status = napi_get_sharedarraybuffer_info(env, shared_array_buffer, &shared_data, &shared_byte_length);
if (status == napi_ok) {
// Use shared_data and shared_byte_length
}
}
```

## Working with JavaScript properties

Node-API exposes a set of APIs to get and set properties on JavaScript
Expand Down
5 changes: 0 additions & 5 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,6 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_is_sharedarraybuffer(napi_env env,
bool* result);
NAPI_EXTERN napi_status NAPI_CDECL napi_create_sharedarraybuffer(
napi_env env, size_t byte_length, void** data, napi_value* result);
NAPI_EXTERN napi_status NAPI_CDECL
napi_get_sharedarraybuffer_info(napi_env env,
napi_value sharedarraybuffer,
void** data,
size_t* byte_length);

// version management
NAPI_EXTERN napi_status NAPI_CDECL napi_get_version(node_api_basic_env env,
Expand Down
52 changes: 20 additions & 32 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3076,16 +3076,29 @@ napi_status NAPI_CDECL napi_get_arraybuffer_info(napi_env env,
CHECK_ARG(env, arraybuffer);

v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg);

v8::Local<v8::ArrayBuffer> ab = value.As<v8::ArrayBuffer>();
if (value->IsArrayBuffer()) {
v8::Local<v8::ArrayBuffer> ab = value.As<v8::ArrayBuffer>();

if (data != nullptr) {
*data = ab->Data();
}
if (data != nullptr) {
*data = ab->Data();
}

if (byte_length != nullptr) {
*byte_length = ab->ByteLength();
if (byte_length != nullptr) {
*byte_length = ab->ByteLength();
}
} else if (value->IsSharedArrayBuffer()) {
v8::Local<v8::SharedArrayBuffer> sab = value.As<v8::SharedArrayBuffer>();

if (data != nullptr) {
*data = sab->Data();
}

if (byte_length != nullptr) {
*byte_length = sab->ByteLength();
}
} else {
return napi_set_last_error(env, napi_invalid_arg);
}

return napi_clear_last_error(env);
Expand Down Expand Up @@ -3125,31 +3138,6 @@ napi_status NAPI_CDECL napi_create_sharedarraybuffer(napi_env env,
return GET_RETURN_STATUS(env);
}

napi_status NAPI_CDECL
napi_get_sharedarraybuffer_info(napi_env env,
napi_value sharedarraybuffer,
void** data,
size_t* byte_length) {
CHECK_ENV_NOT_IN_GC(env);
CHECK_ARG(env, sharedarraybuffer);

v8::Local<v8::Value> value =
v8impl::V8LocalValueFromJsValue(sharedarraybuffer);
RETURN_STATUS_IF_FALSE(env, value->IsSharedArrayBuffer(), napi_invalid_arg);

v8::Local<v8::SharedArrayBuffer> sab = value.As<v8::SharedArrayBuffer>();

if (data != nullptr) {
*data = sab->Data();
}

if (byte_length != nullptr) {
*byte_length = sab->ByteLength();
}

return napi_clear_last_error(env);
}

napi_status NAPI_CDECL napi_is_typedarray(napi_env env,
napi_value value,
bool* result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ static napi_value TestGetSharedArrayBufferInfo(napi_env env,

void* data;
size_t byte_length;
NODE_API_CALL(
env, napi_get_sharedarraybuffer_info(env, args[0], &data, &byte_length));
NODE_API_CALL(env,
napi_get_arraybuffer_info(env, args[0], &data, &byte_length));

napi_value ret;
NODE_API_CALL(env, napi_create_uint32(env, byte_length, &ret));
Expand All @@ -95,8 +95,8 @@ static napi_value TestSharedArrayBufferData(napi_env env,

void* data;
size_t byte_length;
NODE_API_CALL(
env, napi_get_sharedarraybuffer_info(env, args[0], &data, &byte_length));
NODE_API_CALL(env,
napi_get_arraybuffer_info(env, args[0], &data, &byte_length));

// Write some test data
if (byte_length > 0) {
Expand Down Expand Up @@ -129,8 +129,8 @@ static napi_value TestSharedArrayBufferFromExisting(napi_env env,

void* data;
size_t byte_length;
NODE_API_CALL(
env, napi_get_sharedarraybuffer_info(env, args[0], &data, &byte_length));
NODE_API_CALL(env,
napi_get_arraybuffer_info(env, args[0], &data, &byte_length));

// Return the same data pointer validity
bool data_valid = (data != NULL) && (byte_length > 0);
Expand Down