Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change node_buffer to accept ArrayBuffer #16111

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,21 @@ inline MUST_USE_RESULT bool ParseArrayIndex(Local<Value> arg,
// Buffer methods

bool HasInstance(Local<Value> val) {
return val->IsArrayBufferView();
return val->IsArrayBufferView() || val->IsArrayBuffer();
}


bool HasInstance(Local<Object> obj) {
return obj->IsArrayBufferView();
return obj->IsArrayBufferView() || obj->IsArrayBuffer();
}


char* Data(Local<Value> val) {
if (val->IsArrayBuffer()) {
Local<ArrayBuffer> ab = val.As<v8::ArrayBuffer>();
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't need the v8:: prefixes.

ArrayBuffer::Contents ab_c = ab->GetContents();
return static_cast<char*>(ab_c.Data());
}
CHECK(val->IsArrayBufferView());
Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents();
Expand All @@ -217,6 +222,11 @@ char* Data(Local<Value> val) {


char* Data(Local<Object> obj) {
if (obj->IsArrayBuffer()) {
Local<ArrayBuffer> ab = obj.As<v8::ArrayBuffer>();
ArrayBuffer::Contents ab_c = ab->GetContents();
return static_cast<char*>(ab_c.Data());
}
CHECK(obj->IsArrayBufferView());
Local<ArrayBufferView> ui = obj.As<ArrayBufferView>();
ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents();
Expand All @@ -225,13 +235,23 @@ char* Data(Local<Object> obj) {


size_t Length(Local<Value> val) {
if (val->IsArrayBuffer()) {
Local<ArrayBuffer> ab = val.As<v8::ArrayBuffer>();
ArrayBuffer::Contents ab_c = ab->GetContents();
return ab_c.ByteLength();
}
CHECK(val->IsArrayBufferView());
Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
return ui->ByteLength();
}


size_t Length(Local<Object> obj) {
if (obj->IsArrayBuffer()) {
Local<ArrayBuffer> ab = obj.As<v8::ArrayBuffer>();
ArrayBuffer::Contents ab_c = ab->GetContents();
return ab_c.ByteLength();
}
CHECK(obj->IsArrayBufferView());
Local<ArrayBufferView> ui = obj.As<ArrayBufferView>();
return ui->ByteLength();
Expand Down
22 changes: 15 additions & 7 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,21 @@ class BufferValue : public MaybeStackBuffer<char> {
} while (0)

#define SPREAD_BUFFER_ARG(val, name) \
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(); \
char* const name##_data = \
static_cast<char*>(name##_c.Data()) + name##_offset; \
size_t name##_length; \
char* name##_data; \
if ((val)->IsArrayBuffer()) { \
v8::Local<v8::ArrayBuffer> name = (val).As<v8::ArrayBuffer>(); \
v8::ArrayBuffer::Contents name##_c = name->GetContents(); \
name##_length = name##_c.ByteLength(); \
name##_data = static_cast<char*>(name##_c.Data()); \
} else { \
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(); \
name##_length = name->ByteLength(); \
name##_data = static_cast<char*>(name##_c.Data()) + name##_offset; \
} \
if (name##_length > 0) \
CHECK_NE(name##_data, nullptr);

Expand Down