Skip to content

Commit 7adb01f

Browse files
addaleaxtargos
authored andcommitted
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: #30476 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c1b73f2 commit 7adb01f

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/node_buffer.cc

+12
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ MaybeLocal<Uint8Array> New(Environment* env,
231231
return ui;
232232
}
233233

234+
MaybeLocal<Uint8Array> New(Isolate* isolate,
235+
Local<ArrayBuffer> ab,
236+
size_t byte_offset,
237+
size_t length) {
238+
Environment* env = Environment::GetCurrent(isolate);
239+
if (env == nullptr) {
240+
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
241+
return MaybeLocal<Uint8Array>();
242+
}
243+
return New(env, ab, byte_offset, length);
244+
}
245+
234246

235247
MaybeLocal<Object> New(Isolate* isolate,
236248
Local<String> string,

src/node_buffer.h

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
6565
char* data,
6666
size_t len);
6767

68+
// Creates a Buffer instance over an existing ArrayBuffer.
69+
NODE_EXTERN v8::MaybeLocal<v8::Uint8Array> New(v8::Isolate* isolate,
70+
v8::Local<v8::ArrayBuffer> ab,
71+
size_t byte_offset,
72+
size_t length);
73+
6874
// This is verbose to be explicit with inline commenting
6975
static inline bool IsWithinBounds(size_t off, size_t len, size_t max) {
7076
// Asking to seek too far into the buffer

src/node_internals.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ v8::MaybeLocal<v8::Object> New(Environment* env,
160160
char* data,
161161
size_t length,
162162
bool uses_malloc);
163-
// Creates a Buffer instance over an existing Uint8Array.
163+
// Creates a Buffer instance over an existing ArrayBuffer.
164164
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
165165
v8::Local<v8::ArrayBuffer> ab,
166166
size_t byte_offset,

0 commit comments

Comments
 (0)