Permalink
Browse files

src: allow ArrayBufferView as instance of Buffer

PR-URL: #12223
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information...
TimothyGu committed Apr 4, 2017
1 parent ec53921 commit faa447b256f61d46b4b4963d6bf3e77ccf229e97
Showing with 15 additions and 14 deletions.
  1. +12 −11 src/node_buffer.cc
  2. +2 −2 src/util.h
  3. +1 −1 test/parallel/test-buffer-write-noassert.js
View
@@ -70,6 +70,7 @@ namespace Buffer {
using v8::ArrayBuffer;
using v8::ArrayBufferCreationMode;
using v8::ArrayBufferView;
using v8::Context;
using v8::EscapableHandleScope;
using v8::FunctionCallbackInfo;
@@ -195,41 +196,41 @@ inline MUST_USE_RESULT bool ParseArrayIndex(Local<Value> arg,
// Buffer methods
bool HasInstance(Local<Value> val) {
return val->IsUint8Array();
return val->IsArrayBufferView();
}
bool HasInstance(Local<Object> obj) {
return obj->IsUint8Array();
return obj->IsArrayBufferView();
}
char* Data(Local<Value> val) {
CHECK(val->IsUint8Array());
Local<Uint8Array> ui = val.As<Uint8Array>();
CHECK(val->IsArrayBufferView());
Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents();
return static_cast<char*>(ab_c.Data()) + ui->ByteOffset();
}
char* Data(Local<Object> obj) {
CHECK(obj->IsUint8Array());
Local<Uint8Array> ui = obj.As<Uint8Array>();
CHECK(obj->IsArrayBufferView());
Local<ArrayBufferView> ui = obj.As<ArrayBufferView>();
ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents();
return static_cast<char*>(ab_c.Data()) + ui->ByteOffset();
}
size_t Length(Local<Value> val) {
CHECK(val->IsUint8Array());
Local<Uint8Array> ui = val.As<Uint8Array>();
CHECK(val->IsArrayBufferView());
Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
return ui->ByteLength();
}
size_t Length(Local<Object> obj) {
CHECK(obj->IsUint8Array());
Local<Uint8Array> ui = obj.As<Uint8Array>();
CHECK(obj->IsArrayBufferView());
Local<ArrayBufferView> ui = obj.As<ArrayBufferView>();
return ui->ByteLength();
}
@@ -800,7 +801,7 @@ void WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
}
Local<Uint8Array> ts_obj = args[0].As<Uint8Array>();
Local<ArrayBufferView> ts_obj = args[0].As<ArrayBufferView>();
ArrayBuffer::Contents ts_obj_c = ts_obj->Buffer()->GetContents();
const size_t ts_obj_offset = ts_obj->ByteOffset();
const size_t ts_obj_length = ts_obj->ByteLength();
View
@@ -439,8 +439,8 @@ class BufferValue : public MaybeStackBuffer<char> {
} while (0)
#define SPREAD_BUFFER_ARG(val, name) \
CHECK((val)->IsUint8Array()); \
v8::Local<v8::Uint8Array> name = (val).As<v8::Uint8Array>(); \
CHECK((val)->IsArrayBufferView()); \
v8::Local<v8::ArrayBufferView> name = (val).As<v8::ArrayBufferView>(); \
v8::ArrayBuffer::Contents name##_c = name->Buffer()->GetContents(); \
const size_t name##_offset = name->ByteOffset(); \
const size_t name##_length = name->ByteLength(); \
@@ -18,7 +18,7 @@ function write(funx, args, result, res) {
if (!/Int/.test(funx)) {
assert.throws(
() => Buffer.alloc(9)[funx].apply(new Uint32Array(1), args),
() => Buffer.alloc(9)[funx].apply(new Map(), args),
/^TypeError: argument should be a Buffer$/
);
}

1 comment on commit faa447b

@rimmartin

This comment has been minimized.

Show comment
Hide comment
@rimmartin

rimmartin Sep 2, 2017

Are all typed Arrays now buffers? The reason I ask is because I use the javascript type to know what to make datasets in hdf5.
https://github.com/HDF-NI/hdf5.node/blob/master/src/h5_lt.hpp#L501

if (args[2]>IsFloat64Array()) {
}

for example.

rimmartin commented on faa447b Sep 2, 2017

Are all typed Arrays now buffers? The reason I ask is because I use the javascript type to know what to make datasets in hdf5.
https://github.com/HDF-NI/hdf5.node/blob/master/src/h5_lt.hpp#L501

if (args[2]>IsFloat64Array()) {
}

for example.

Please sign in to comment.