diff --git a/src/node_blob.cc b/src/node_blob.cc index 8afc6d3aa0d76f..bc17a5d3c20ee7 100644 --- a/src/node_blob.cc +++ b/src/node_blob.cc @@ -30,8 +30,10 @@ using v8::HandleScope; using v8::Int32; using v8::Isolate; using v8::Local; +using v8::NewStringType; using v8::Object; using v8::ObjectTemplate; +using v8::SnapshotCreator; using v8::String; using v8::Uint32; using v8::Undefined; @@ -58,7 +60,7 @@ void Concat(const FunctionCallbackInfo& args) { std::vector views; size_t total = 0; - std::vector> buffers; + std::vector> buffers; if (FromV8Array(context, array, &buffers).IsNothing()) { return; } @@ -108,17 +110,14 @@ void BlobFromFilePath(const FunctionCallbackInfo& args) { std::vector> entries; entries.push_back(std::move(entry)); - auto blob = - Blob::Create(env, DataQueue::CreateIdempotent(std::move(entries))); - - if (blob) { - auto array = Array::New(env->isolate(), 2); - USE(array->Set(env->context(), 0, blob->object())); - USE(array->Set(env->context(), - 1, - Uint32::NewFromUnsigned(env->isolate(), blob->length()))); - - args.GetReturnValue().Set(array); + if (auto blob = + Blob::Create(env, DataQueue::CreateIdempotent(std::move(entries)))) { + Local vals[2]{ + blob->object(), + Uint32::NewFromUnsigned(env->isolate(), blob->length()), + }; + args.GetReturnValue().Set( + Array::New(env->isolate(), &vals[0], arraysize(vals))); } } } // namespace @@ -159,7 +158,7 @@ Local Blob::GetConstructorTemplate(Environment* env) { return tmpl; } -bool Blob::HasInstance(Environment* env, v8::Local object) { +bool Blob::HasInstance(Environment* env, Local object) { return GetConstructorTemplate(env)->HasInstance(object); } @@ -188,7 +187,7 @@ void Blob::New(const FunctionCallbackInfo& args) { Local array = args[0].As(); std::vector> entries(array->Length()); - std::vector> sources; + std::vector> sources; if (FromV8Array(context, array, &sources).IsNothing()) { return; } @@ -197,12 +196,16 @@ void Blob::New(const FunctionCallbackInfo& args) { for (size_t i = 0; i < count; i++) { Local entry = sources[i].Get(isolate); - const auto entryFromArrayBuffer = [isolate](v8::Local buf, - size_t byte_length, - size_t byte_offset = 0) { + auto entryFromArrayBuffer = + [isolate](Local buf, + size_t byte_length, + size_t byte_offset = + 0) mutable -> std::unique_ptr { if (buf->IsDetachable()) { std::shared_ptr store = buf->GetBackingStore(); - USE(buf->Detach(Local())); + if (buf->Detach(Local()).IsNothing()) { + return nullptr; + } return DataQueue::CreateInMemoryEntryFromBackingStore( store, byte_offset, byte_length); } @@ -227,11 +230,15 @@ void Blob::New(const FunctionCallbackInfo& args) { // ensuring appropriate spec compliance. if (entry->IsArrayBuffer()) { Local buf = entry.As(); - entries[i] = entryFromArrayBuffer(buf, buf->ByteLength()); + auto ret = entryFromArrayBuffer(buf, buf->ByteLength()); + if (!ret) return; + entries[i] = std::move(ret); } else if (entry->IsArrayBufferView()) { Local view = entry.As(); - entries[i] = entryFromArrayBuffer( + auto ret = entryFromArrayBuffer( view->Buffer(), view->ByteLength(), view->ByteOffset()); + if (!ret) return; + entries[i] = std::move(ret); } else if (Blob::HasInstance(env, entry)) { Blob* blob; ASSIGN_OR_RETURN_UNWRAP(&blob, entry); @@ -279,14 +286,14 @@ BaseObjectPtr Blob::Slice(Environment* env, size_t start, size_t end) { } Blob::Blob(Environment* env, - v8::Local obj, + Local obj, std::shared_ptr data_queue) : BaseObject(env, obj), data_queue_(data_queue) { MakeWeak(); } Blob::Reader::Reader(Environment* env, - v8::Local obj, + Local obj, BaseObjectPtr strong_ptr) : AsyncWrap(env, obj, AsyncWrap::PROVIDER_BLOBREADER), inner_(strong_ptr->data_queue_->get_reader()), @@ -294,7 +301,7 @@ Blob::Reader::Reader(Environment* env, MakeWeak(); } -bool Blob::Reader::HasInstance(Environment* env, v8::Local value) { +bool Blob::Reader::HasInstance(Environment* env, Local value) { return GetConstructorTemplate(env)->HasInstance(value); } @@ -370,7 +377,7 @@ void Blob::Reader::Pull(const FunctionCallbackInfo& args) { for (size_t n = 0; n < count; n++) total += vecs[n].len; std::shared_ptr store = - v8::ArrayBuffer::NewBackingStore(env->isolate(), total); + ArrayBuffer::NewBackingStore(env->isolate(), total); auto ptr = static_cast(store->Data()); for (size_t n = 0; n < count; n++) { std::copy(vecs[n].base, vecs[n].base + vecs[n].len, ptr); @@ -415,7 +422,7 @@ std::unique_ptr Blob::CloneForMessaging() const { return std::make_unique(data_queue_); } -void Blob::StoreDataObject(const v8::FunctionCallbackInfo& args) { +void Blob::StoreDataObject(const FunctionCallbackInfo& args) { Realm* realm = Realm::GetCurrent(args); CHECK(args[0]->IsString()); // ID key @@ -468,7 +475,7 @@ void Blob::RevokeObjectURL(const FunctionCallbackInfo& args) { } } -void Blob::GetDataObject(const v8::FunctionCallbackInfo& args) { +void Blob::GetDataObject(const FunctionCallbackInfo& args) { CHECK(args[0]->IsString()); Realm* realm = Realm::GetCurrent(args); BlobBindingData* binding_data = realm->GetBindingData(); @@ -482,7 +489,7 @@ void Blob::GetDataObject(const v8::FunctionCallbackInfo& args) { Local type; if (!String::NewFromUtf8(isolate, stored.type.c_str(), - v8::NewStringType::kNormal, + NewStringType::kNormal, static_cast(stored.type.length())) .ToLocal(&type)) { return; @@ -554,7 +561,7 @@ void BlobBindingData::Deserialize(Local context, } bool BlobBindingData::PrepareForSerialization(Local context, - v8::SnapshotCreator* creator) { + SnapshotCreator* creator) { // Stored blob objects are not actually persisted. // Return true because we need to maintain the reference to the binding from // JS land.