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 MylesBorins committed Nov 21, 2019
1 parent 2e43686 commit 21dd601
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
Expand Up @@ -229,6 +229,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
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
Expand Up @@ -157,7 +157,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 21dd601

Please sign in to comment.