Skip to content

Commit

Permalink
src: expose ArrayBuffer version of Buffer::New()
Browse files Browse the repository at this point in the history
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 <cjihrig@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and targos committed Jan 13, 2020
1 parent c1b73f2 commit 7adb01f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ MaybeLocal<Uint8Array> New(Environment* env,
return ui;
}

MaybeLocal<Uint8Array> New(Isolate* isolate,
Local<ArrayBuffer> 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<Uint8Array>();
}
return New(env, ab, byte_offset, length);
}


MaybeLocal<Object> New(Isolate* isolate,
Local<String> string,
Expand Down
6 changes: 6 additions & 0 deletions src/node_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
char* data,
size_t len);

// Creates a Buffer instance over an existing ArrayBuffer.
NODE_EXTERN v8::MaybeLocal<v8::Uint8Array> New(v8::Isolate* isolate,
v8::Local<v8::ArrayBuffer> 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
Expand Down
2 changes: 1 addition & 1 deletion src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ v8::MaybeLocal<v8::Object> 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<v8::Uint8Array> New(Environment* env,
v8::Local<v8::ArrayBuffer> ab,
size_t byte_offset,
Expand Down

0 comments on commit 7adb01f

Please sign in to comment.