From fdf838aee69939db91bdaf7192a2cf176e3e5fb0 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Fri, 11 Oct 2024 18:21:57 +0300 Subject: [PATCH] node-api: add napi_create_buffer_from_arraybuffer method PR-URL: https://github.com/nodejs/node/pull/54505 Fixes: https://github.com/nodejs/node/issues/54440 Reviewed-By: Robert Nagy Reviewed-By: Gabriel Schulhof Reviewed-By: Chengzhong Wu Reviewed-By: Vladimir Morozov --- doc/api/n-api.md | 31 +++++++++++++ src/node_api.cc | 35 ++++++++++++++ src/node_api.h | 12 +++++ test/node-api/test_buffer/binding.gyp | 3 ++ test/node-api/test_buffer/test.js | 3 ++ test/node-api/test_buffer/test_buffer.c | 62 ++++++++++++++++++++----- 6 files changed, 134 insertions(+), 12 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 839f0229912..1cee948a022 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2696,6 +2696,37 @@ is raised. JavaScript `TypedArray` objects are described in [Section 22.2][] of the ECMAScript Language Specification. +#### `node_api_create_buffer_from_arraybuffer` + + + +> Stability: 1 - Experimental + +```c +napi_status NAPI_CDECL node_api_create_buffer_from_arraybuffer(napi_env env, + napi_value arraybuffer, + size_t byte_offset, + size_t byte_length, + napi_value* result) +``` + +* **`[in] env`**: The environment that the API is invoked under. +* **`[in] arraybuffer`**: The `ArrayBuffer` from which the buffer will be created. +* **`[in] byte_offset`**: The byte offset within the `ArrayBuffer` from which to start creating the buffer. +* **`[in] byte_length`**: The length in bytes of the buffer to be created from the `ArrayBuffer`. +* **`[out] result`**: A `napi_value` representing the created JavaScript `Buffer` object. + +Returns `napi_ok` if the API succeeded. + +This API creates a JavaScript `Buffer` object from an existing `ArrayBuffer`. +The `Buffer` object is a Node.js-specific class that provides a way to work with binary data directly in JavaScript. + +The byte range `[byte_offset, byte_offset + byte_length)` +must be within the bounds of the `ArrayBuffer`. If `byte_offset + byte_length` +exceeds the size of the `ArrayBuffer`, a `RangeError` exception is raised. + #### `napi_create_dataview`