Skip to content

Commit

Permalink
src: allow ArrayBufferView as instance of Buffer
Browse files Browse the repository at this point in the history
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 12, 2017
1 parent ec53921 commit faa447b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace Buffer {

using v8::ArrayBuffer;
using v8::ArrayBufferCreationMode;
using v8::ArrayBufferView;
using v8::Context;
using v8::EscapableHandleScope;
using v8::FunctionCallbackInfo;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(); \
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-write-noassert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/
);
}
Expand Down

1 comment on commit faa447b

@rimmartin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.