From 1a8ae9d6c068e880c641b094b23fbaa442f0dbc0 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 13 Feb 2024 16:46:30 +0100 Subject: [PATCH] src: use callback-based array iteration in Blob PR-URL: https://github.com/nodejs/node/pull/51758 Reviewed-By: Yagiz Nizipli Reviewed-By: Chengzhong Wu --- src/node_blob.cc | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/node_blob.cc b/src/node_blob.cc index d5475de36d7738..970117efc3dc1f 100644 --- a/src/node_blob.cc +++ b/src/node_blob.cc @@ -41,7 +41,10 @@ namespace { // Concatenate multiple ArrayBufferView/ArrayBuffers into a single ArrayBuffer. // This method treats all ArrayBufferView types the same. void Concat(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); + Isolate* isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); + Environment* env = Environment::GetCurrent(context); + CHECK(args[0]->IsArray()); Local array = args[0].As(); @@ -54,9 +57,14 @@ void Concat(const FunctionCallbackInfo& args) { std::vector views; size_t total = 0; - for (uint32_t n = 0; n < array->Length(); n++) { - Local val; - if (!array->Get(env->context(), n).ToLocal(&val)) return; + std::vector> buffers; + if (FromV8Array(context, array, &buffers).IsNothing()) { + return; + } + + size_t count = buffers.size(); + for (uint32_t i = 0; i < count; i++) { + Local val = buffers[i].Get(isolate); if (val->IsArrayBuffer()) { auto ab = val.As(); views.push_back(View{ab->GetBackingStore(), ab->ByteLength(), 0}); @@ -169,21 +177,27 @@ BaseObjectPtr Blob::Create(Environment* env, } void Blob::New(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); + Isolate* isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); + Environment* env = Environment::GetCurrent(context); + CHECK(args[0]->IsArray()); // sources Local array = args[0].As(); std::vector> entries(array->Length()); - for (size_t i = 0; i < array->Length(); i++) { - Local entry; - if (!array->Get(env->context(), i).ToLocal(&entry)) { - return; - } + std::vector> sources; + if (FromV8Array(context, array, &sources).IsNothing()) { + return; + } + + size_t count = sources.size(); + for (size_t i = 0; i < count; i++) { + Local entry = sources[i].Get(isolate); - const auto entryFromArrayBuffer = [env](v8::Local buf, - size_t byte_length, - size_t byte_offset = 0) { + const auto entryFromArrayBuffer = [isolate](v8::Local buf, + size_t byte_length, + size_t byte_offset = 0) { if (buf->IsDetachable()) { std::shared_ptr store = buf->GetBackingStore(); USE(buf->Detach(Local())); @@ -193,7 +207,7 @@ void Blob::New(const FunctionCallbackInfo& args) { // If the ArrayBuffer is not detachable, we will copy from it instead. std::shared_ptr store = - ArrayBuffer::NewBackingStore(env->isolate(), byte_length); + ArrayBuffer::NewBackingStore(isolate, byte_length); uint8_t* ptr = static_cast(buf->Data()) + byte_offset; std::copy(ptr, ptr + byte_length, static_cast(store->Data())); return DataQueue::CreateInMemoryEntryFromBackingStore(