Skip to content

Commit 21dd601

Browse files
addaleaxMylesBorins
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 2e43686 commit 21dd601

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
@@ -229,6 +229,18 @@ MaybeLocal<Uint8Array> New(Environment* env,
229229
return ui;
230230
}
231231

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

233245
MaybeLocal<Object> New(Isolate* isolate,
234246
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
@@ -157,7 +157,7 @@ v8::MaybeLocal<v8::Object> New(Environment* env,
157157
char* data,
158158
size_t length,
159159
bool uses_malloc);
160-
// Creates a Buffer instance over an existing Uint8Array.
160+
// Creates a Buffer instance over an existing ArrayBuffer.
161161
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
162162
v8::Local<v8::ArrayBuffer> ab,
163163
size_t byte_offset,

0 commit comments

Comments
 (0)