diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 66ae9feb697a32..0c68e04e5a7b18 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -303,15 +303,14 @@ MaybeLocal New(Environment* env, size_t length) { data, length, ArrayBufferCreationMode::kInternalized); - Local ui = Uint8Array::New(ab, 0, length); - Maybe mb = - ui->SetPrototype(env->context(), env->buffer_prototype_object()); - if (mb.FromMaybe(false)) - return scope.Escape(ui); + MaybeLocal ui = Buffer::New(env, ab, 0, length); - // Object failed to be created. Clean up resources. - free(data); - return Local(); + if (ui.IsEmpty()) { + // Object failed to be created. Clean up resources. + free(data); + } + + return scope.Escape(ui.FromMaybe(Local())); } @@ -349,15 +348,14 @@ MaybeLocal Copy(Environment* env, const char* data, size_t length) { new_data, length, ArrayBufferCreationMode::kInternalized); - Local ui = Uint8Array::New(ab, 0, length); - Maybe mb = - ui->SetPrototype(env->context(), env->buffer_prototype_object()); - if (mb.FromMaybe(false)) - return scope.Escape(ui); + MaybeLocal ui = Buffer::New(env, ab, 0, length); - // Object failed to be created. Clean up resources. - free(new_data); - return Local(); + if (ui.IsEmpty()) { + // Object failed to be created. Clean up resources. + free(new_data); + } + + return scope.Escape(ui.FromMaybe(Local())); } @@ -392,15 +390,14 @@ MaybeLocal New(Environment* env, // correct. if (data == nullptr) ab->Neuter(); - Local ui = Uint8Array::New(ab, 0, length); - Maybe mb = - ui->SetPrototype(env->context(), env->buffer_prototype_object()); + MaybeLocal ui = Buffer::New(env, ab, 0, length); - if (!mb.FromMaybe(false)) + if (ui.IsEmpty()) { return Local(); + } CallbackInfo::New(env->isolate(), ab, callback, data, hint); - return scope.Escape(ui); + return scope.Escape(ui.ToLocalChecked()); } @@ -415,8 +412,6 @@ MaybeLocal New(Isolate* isolate, char* data, size_t length) { MaybeLocal New(Environment* env, char* data, size_t length) { - EscapableHandleScope scope(env->isolate()); - if (length > 0) { CHECK_NE(data, nullptr); CHECK(length <= kMaxLength); @@ -427,12 +422,7 @@ MaybeLocal New(Environment* env, char* data, size_t length) { data, length, ArrayBufferCreationMode::kInternalized); - Local ui = Uint8Array::New(ab, 0, length); - Maybe mb = - ui->SetPrototype(env->context(), env->buffer_prototype_object()); - if (mb.FromMaybe(false)) - return scope.Escape(ui); - return Local(); + return Buffer::New(env, ab, 0, length).FromMaybe(Local()); } namespace { diff --git a/src/node_internals.h b/src/node_internals.h index 8f3fb4fb9aa27d..fb288ede410ee2 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -325,6 +325,19 @@ v8::MaybeLocal New(Environment* env, // Mixing operator new and free() is undefined behavior so don't do that. v8::MaybeLocal New(Environment* env, char* data, size_t length); +inline +v8::MaybeLocal New(Environment* env, + v8::Local ab, + size_t byte_offset, + size_t length) { + v8::Local ui = v8::Uint8Array::New(ab, byte_offset, length); + v8::Maybe mb = + ui->SetPrototype(env->context(), env->buffer_prototype_object()); + if (mb.IsNothing()) + return v8::MaybeLocal(); + 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