summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-11-13 19:22:46 +0000
committerAnna Henningsen <anna@addaleax.net>2019-11-19 14:02:22 +0100
commite5d3c8121dd0bcc4dbf52c7b3a0521e359363a05 (patch)
tree9e7fac09163e14246056bc64228388e78c45e874 /src
parent6cb8e4b12cd16ae8ed126f98458efb78312cabf6 (diff)
downloadandroid-node-v8-e5d3c8121dd0bcc4dbf52c7b3a0521e359363a05.tar.gz
android-node-v8-e5d3c8121dd0bcc4dbf52c7b3a0521e359363a05.tar.bz2
android-node-v8-e5d3c8121dd0bcc4dbf52c7b3a0521e359363a05.zip
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 <cjihrig@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/node_buffer.cc12
-rw-r--r--src/node_buffer.h6
-rw-r--r--src/node_internals.h2
3 files changed, 19 insertions, 1 deletions
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<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,
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<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
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<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,