From ee5c746e6bc4b020c310178df197dcffc80745a6 Mon Sep 17 00:00:00 2001 From: Jem Bezooyen Date: Mon, 9 Oct 2017 13:35:36 -0700 Subject: [PATCH] src: accept ArrayBuffer in node_buffer --- src/node_buffer.cc | 24 ++++++++++++++++++++++-- src/util.h | 22 +++++++++++++++------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index dc67844553f64e..8bc8309ab63a1b 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -199,16 +199,21 @@ inline MUST_USE_RESULT bool ParseArrayIndex(Local arg, // Buffer methods bool HasInstance(Local val) { - return val->IsArrayBufferView(); + return val->IsArrayBufferView() || val->IsArrayBuffer(); } bool HasInstance(Local obj) { - return obj->IsArrayBufferView(); + return obj->IsArrayBufferView() || obj->IsArrayBuffer(); } char* Data(Local val) { + if (val->IsArrayBuffer()) { + Local ab = val.As(); + ArrayBuffer::Contents ab_c = ab->GetContents(); + return static_cast(ab_c.Data()); + } CHECK(val->IsArrayBufferView()); Local ui = val.As(); ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents(); @@ -217,6 +222,11 @@ char* Data(Local val) { char* Data(Local obj) { + if (obj->IsArrayBuffer()) { + Local ab = obj.As(); + ArrayBuffer::Contents ab_c = ab->GetContents(); + return static_cast(ab_c.Data()); + } CHECK(obj->IsArrayBufferView()); Local ui = obj.As(); ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents(); @@ -225,6 +235,11 @@ char* Data(Local obj) { size_t Length(Local val) { + if (val->IsArrayBuffer()) { + Local ab = val.As(); + ArrayBuffer::Contents ab_c = ab->GetContents(); + return ab_c.ByteLength(); + } CHECK(val->IsArrayBufferView()); Local ui = val.As(); return ui->ByteLength(); @@ -232,6 +247,11 @@ size_t Length(Local val) { size_t Length(Local obj) { + if (obj->IsArrayBuffer()) { + Local ab = obj.As(); + ArrayBuffer::Contents ab_c = ab->GetContents(); + return ab_c.ByteLength(); + } CHECK(obj->IsArrayBufferView()); Local ui = obj.As(); return ui->ByteLength(); diff --git a/src/util.h b/src/util.h index 08308d837fbf8b..8c52554dc83705 100644 --- a/src/util.h +++ b/src/util.h @@ -418,13 +418,21 @@ class BufferValue : public MaybeStackBuffer { } while (0) #define SPREAD_BUFFER_ARG(val, name) \ - CHECK((val)->IsArrayBufferView()); \ - v8::Local name = (val).As(); \ - v8::ArrayBuffer::Contents name##_c = name->Buffer()->GetContents(); \ - const size_t name##_offset = name->ByteOffset(); \ - const size_t name##_length = name->ByteLength(); \ - char* const name##_data = \ - static_cast(name##_c.Data()) + name##_offset; \ + size_t name##_length; \ + char* name##_data; \ + if ((val)->IsArrayBuffer()) { \ + v8::Local name = (val).As(); \ + v8::ArrayBuffer::Contents name##_c = name->GetContents(); \ + name##_length = name##_c.ByteLength(); \ + name##_data = static_cast(name##_c.Data()); \ + } else { \ + CHECK((val)->IsArrayBufferView()); \ + v8::Local name = (val).As(); \ + v8::ArrayBuffer::Contents name##_c = name->Buffer()->GetContents(); \ + const size_t name##_offset = name->ByteOffset(); \ + name##_length = name->ByteLength(); \ + name##_data = static_cast(name##_c.Data()) + name##_offset; \ + } \ if (name##_length > 0) \ CHECK_NE(name##_data, nullptr);