Skip to content

Commit

Permalink
src: introduce internal buffer slice constructor
Browse files Browse the repository at this point in the history
Add a C++ variant of `Buffer.from(arrayBuffer, offset, length)`.

PR-URL: #18030
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and evanlucas committed Jan 30, 2018
1 parent f25cba3 commit 7219a13
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
48 changes: 19 additions & 29 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,14 @@ MaybeLocal<Object> New(Environment* env, size_t length) {
data,
length,
ArrayBufferCreationMode::kInternalized);
Local<Uint8Array> ui = Uint8Array::New(ab, 0, length);
Maybe<bool> mb =
ui->SetPrototype(env->context(), env->buffer_prototype_object());
if (mb.FromMaybe(false))
return scope.Escape(ui);
MaybeLocal<Uint8Array> ui = Buffer::New(env, ab, 0, length);

// Object failed to be created. Clean up resources.
free(data);
return Local<Object>();
if (ui.IsEmpty()) {
// Object failed to be created. Clean up resources.
free(data);
}

return scope.Escape(ui.FromMaybe(Local<Uint8Array>()));
}


Expand Down Expand Up @@ -349,15 +348,14 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
new_data,
length,
ArrayBufferCreationMode::kInternalized);
Local<Uint8Array> ui = Uint8Array::New(ab, 0, length);
Maybe<bool> mb =
ui->SetPrototype(env->context(), env->buffer_prototype_object());
if (mb.FromMaybe(false))
return scope.Escape(ui);
MaybeLocal<Uint8Array> ui = Buffer::New(env, ab, 0, length);

// Object failed to be created. Clean up resources.
free(new_data);
return Local<Object>();
if (ui.IsEmpty()) {
// Object failed to be created. Clean up resources.
free(new_data);
}

return scope.Escape(ui.FromMaybe(Local<Uint8Array>()));
}


Expand Down Expand Up @@ -392,15 +390,14 @@ MaybeLocal<Object> New(Environment* env,
// correct.
if (data == nullptr)
ab->Neuter();
Local<Uint8Array> ui = Uint8Array::New(ab, 0, length);
Maybe<bool> mb =
ui->SetPrototype(env->context(), env->buffer_prototype_object());
MaybeLocal<Uint8Array> ui = Buffer::New(env, ab, 0, length);

if (!mb.FromMaybe(false))
if (ui.IsEmpty()) {
return Local<Object>();
}

CallbackInfo::New(env->isolate(), ab, callback, data, hint);
return scope.Escape(ui);
return scope.Escape(ui.ToLocalChecked());
}


Expand All @@ -415,8 +412,6 @@ MaybeLocal<Object> New(Isolate* isolate, char* data, size_t length) {


MaybeLocal<Object> New(Environment* env, char* data, size_t length) {
EscapableHandleScope scope(env->isolate());

if (length > 0) {
CHECK_NE(data, nullptr);
CHECK(length <= kMaxLength);
Expand All @@ -427,12 +422,7 @@ MaybeLocal<Object> New(Environment* env, char* data, size_t length) {
data,
length,
ArrayBufferCreationMode::kInternalized);
Local<Uint8Array> ui = Uint8Array::New(ab, 0, length);
Maybe<bool> mb =
ui->SetPrototype(env->context(), env->buffer_prototype_object());
if (mb.FromMaybe(false))
return scope.Escape(ui);
return Local<Object>();
return Buffer::New(env, ab, 0, length).FromMaybe(Local<Object>());
}

namespace {
Expand Down
13 changes: 13 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,19 @@ v8::MaybeLocal<v8::Object> New(Environment* env,
// Mixing operator new and free() is undefined behavior so don't do that.
v8::MaybeLocal<v8::Object> New(Environment* env, char* data, size_t length);

inline
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
v8::Local<v8::ArrayBuffer> ab,
size_t byte_offset,
size_t length) {
v8::Local<v8::Uint8Array> ui = v8::Uint8Array::New(ab, byte_offset, length);
v8::Maybe<bool> mb =
ui->SetPrototype(env->context(), env->buffer_prototype_object());
if (mb.IsNothing())
return v8::MaybeLocal<v8::Uint8Array>();
return ui;
}

// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
// Utf8Value and TwoByteValue).
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
Expand Down

0 comments on commit 7219a13

Please sign in to comment.