mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
src: remove usage of AllocatedBuffer from src/node_buffer.cc
Since AllocatedBuffer is just a thin wrapper around v8::BackingStore, we should prefer using v8::BackingStore directly. Refs: https://github.com/nodejs/node/blob/52abf271c563ddffdc93b444ea05e5347a7f2784/src/allocated_buffer.h#L30-L31 Signed-off-by: Darshan Sen <darshan.sen@postman.com> PR-URL: https://github.com/nodejs/node/pull/39941 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
7857e9cc77
commit
879dc4658e
1 file changed
+57
-17
+57
-17
@@ -352,16 +352,31 @@ MaybeLocal<Object> New(Isolate* isolate, size_t length) {
|
||||
|
||||
|
||||
MaybeLocal<Object> New(Environment* env, size_t length) {
|
||||
EscapableHandleScope scope(env->isolate());
|
||||
Isolate* isolate(env->isolate());
|
||||
EscapableHandleScope scope(isolate);
|
||||
|
||||
// V8 currently only allows a maximum Typed Array index of max Smi.
|
||||
if (length > kMaxLength) {
|
||||
env->isolate()->ThrowException(ERR_BUFFER_TOO_LARGE(env->isolate()));
|
||||
isolate->ThrowException(ERR_BUFFER_TOO_LARGE(isolate));
|
||||
return Local<Object>();
|
||||
}
|
||||
|
||||
return scope.EscapeMaybe(
|
||||
AllocatedBuffer::AllocateManaged(env, length).ToBuffer());
|
||||
Local<ArrayBuffer> ab;
|
||||
{
|
||||
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
|
||||
std::unique_ptr<BackingStore> bs =
|
||||
ArrayBuffer::NewBackingStore(isolate, length);
|
||||
|
||||
CHECK(bs);
|
||||
|
||||
ab = ArrayBuffer::New(isolate, std::move(bs));
|
||||
}
|
||||
|
||||
MaybeLocal<Object> obj =
|
||||
New(env, ab, 0, ab->ByteLength())
|
||||
.FromMaybe(Local<Uint8Array>());
|
||||
|
||||
return scope.EscapeMaybe(obj);
|
||||
}
|
||||
|
||||
|
||||
@@ -380,20 +395,33 @@ MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) {
|
||||
|
||||
|
||||
MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
|
||||
EscapableHandleScope scope(env->isolate());
|
||||
Isolate* isolate(env->isolate());
|
||||
EscapableHandleScope scope(isolate);
|
||||
|
||||
// V8 currently only allows a maximum Typed Array index of max Smi.
|
||||
if (length > kMaxLength) {
|
||||
env->isolate()->ThrowException(ERR_BUFFER_TOO_LARGE(env->isolate()));
|
||||
isolate->ThrowException(ERR_BUFFER_TOO_LARGE(isolate));
|
||||
return Local<Object>();
|
||||
}
|
||||
|
||||
AllocatedBuffer ret = AllocatedBuffer::AllocateManaged(env, length);
|
||||
if (length > 0) {
|
||||
memcpy(ret.data(), data, length);
|
||||
Local<ArrayBuffer> ab;
|
||||
{
|
||||
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
|
||||
std::unique_ptr<BackingStore> bs =
|
||||
ArrayBuffer::NewBackingStore(isolate, length);
|
||||
|
||||
CHECK(bs);
|
||||
|
||||
memcpy(bs->Data(), data, length);
|
||||
|
||||
ab = ArrayBuffer::New(isolate, std::move(bs));
|
||||
}
|
||||
|
||||
return scope.EscapeMaybe(ret.ToBuffer());
|
||||
MaybeLocal<Object> obj =
|
||||
New(env, ab, 0, ab->ByteLength())
|
||||
.FromMaybe(Local<Uint8Array>());
|
||||
|
||||
return scope.EscapeMaybe(obj);
|
||||
}
|
||||
|
||||
|
||||
@@ -1077,13 +1105,25 @@ static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
Local<String> str = args[0].As<String>();
|
||||
size_t length = str->Utf8Length(isolate);
|
||||
AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, length);
|
||||
str->WriteUtf8(isolate,
|
||||
buf.data(),
|
||||
-1, // We are certain that `data` is sufficiently large
|
||||
nullptr,
|
||||
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
|
||||
auto array = Uint8Array::New(buf.ToArrayBuffer(), 0, length);
|
||||
|
||||
Local<ArrayBuffer> ab;
|
||||
{
|
||||
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
|
||||
std::unique_ptr<BackingStore> bs =
|
||||
ArrayBuffer::NewBackingStore(isolate, length);
|
||||
|
||||
CHECK(bs);
|
||||
|
||||
str->WriteUtf8(isolate,
|
||||
static_cast<char*>(bs->Data()),
|
||||
-1, // We are certain that `data` is sufficiently large
|
||||
nullptr,
|
||||
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
|
||||
|
||||
ab = ArrayBuffer::New(isolate, std::move(bs));
|
||||
}
|
||||
|
||||
auto array = Uint8Array::New(ab, 0, length);
|
||||
args.GetReturnValue().Set(array);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user