From e5d3c8121dd0bcc4dbf52c7b3a0521e359363a05 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 13 Nov 2019 19:22:46 +0000 Subject: src: expose ArrayBuffer version of Buffer::New() This can be useful to create `Buffer` instances for already-existing `ArrayBuffer`s, e.g. ones created manually from a backing store with a free callback (of which our variant in the public API has some limitations). PR-URL: https://github.com/nodejs/node/pull/30476 Reviewed-By: Colin Ihrig Reviewed-By: David Carlier Reviewed-By: James M Snell --- src/node_buffer.cc | 12 ++++++++++++ src/node_buffer.h | 6 ++++++ src/node_internals.h | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 3cd047c0e7..d87d38334a 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -229,6 +229,18 @@ MaybeLocal New(Environment* env, return ui; } +MaybeLocal New(Isolate* isolate, + Local ab, + size_t byte_offset, + size_t length) { + Environment* env = Environment::GetCurrent(isolate); + if (env == nullptr) { + THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate); + return MaybeLocal(); + } + return New(env, ab, byte_offset, length); +} + MaybeLocal New(Isolate* isolate, Local string, diff --git a/src/node_buffer.h b/src/node_buffer.h index 122afc3770..11010017ce 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -65,6 +65,12 @@ NODE_EXTERN v8::MaybeLocal New(v8::Isolate* isolate, char* data, size_t len); +// Creates a Buffer instance over an existing ArrayBuffer. +NODE_EXTERN v8::MaybeLocal New(v8::Isolate* isolate, + v8::Local ab, + size_t byte_offset, + size_t length); + // This is verbose to be explicit with inline commenting static inline bool IsWithinBounds(size_t off, size_t len, size_t max) { // Asking to seek too far into the buffer diff --git a/src/node_internals.h b/src/node_internals.h index 10ef3bf5ed..2bd0ff78da 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -157,7 +157,7 @@ v8::MaybeLocal New(Environment* env, char* data, size_t length, bool uses_malloc); -// Creates a Buffer instance over an existing Uint8Array. +// Creates a Buffer instance over an existing ArrayBuffer. v8::MaybeLocal New(Environment* env, v8::Local ab, size_t byte_offset, -- cgit v1.2.3