Skip to content

Commit 90875ba

Browse files
jasnelltargos
authored andcommitted
src: improve error handling in node_blob
PR-URL: #57078 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 5414eb4 commit 90875ba

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

src/node_blob.cc

+35-28
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ using v8::HandleScope;
3030
using v8::Int32;
3131
using v8::Isolate;
3232
using v8::Local;
33+
using v8::NewStringType;
3334
using v8::Object;
3435
using v8::ObjectTemplate;
36+
using v8::SnapshotCreator;
3537
using v8::String;
3638
using v8::Uint32;
3739
using v8::Undefined;
@@ -58,7 +60,7 @@ void Concat(const FunctionCallbackInfo<Value>& args) {
5860
std::vector<View> views;
5961
size_t total = 0;
6062

61-
std::vector<v8::Global<Value>> buffers;
63+
std::vector<Global<Value>> buffers;
6264
if (FromV8Array(context, array, &buffers).IsNothing()) {
6365
return;
6466
}
@@ -108,17 +110,14 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
108110
std::vector<std::unique_ptr<DataQueue::Entry>> entries;
109111
entries.push_back(std::move(entry));
110112

111-
auto blob =
112-
Blob::Create(env, DataQueue::CreateIdempotent(std::move(entries)));
113-
114-
if (blob) {
115-
auto array = Array::New(env->isolate(), 2);
116-
USE(array->Set(env->context(), 0, blob->object()));
117-
USE(array->Set(env->context(),
118-
1,
119-
Uint32::NewFromUnsigned(env->isolate(), blob->length())));
120-
121-
args.GetReturnValue().Set(array);
113+
if (auto blob =
114+
Blob::Create(env, DataQueue::CreateIdempotent(std::move(entries)))) {
115+
Local<Value> vals[2]{
116+
blob->object(),
117+
Uint32::NewFromUnsigned(env->isolate(), blob->length()),
118+
};
119+
args.GetReturnValue().Set(
120+
Array::New(env->isolate(), &vals[0], arraysize(vals)));
122121
}
123122
}
124123
} // namespace
@@ -159,7 +158,7 @@ Local<FunctionTemplate> Blob::GetConstructorTemplate(Environment* env) {
159158
return tmpl;
160159
}
161160

162-
bool Blob::HasInstance(Environment* env, v8::Local<v8::Value> object) {
161+
bool Blob::HasInstance(Environment* env, Local<Value> object) {
163162
return GetConstructorTemplate(env)->HasInstance(object);
164163
}
165164

@@ -188,7 +187,7 @@ void Blob::New(const FunctionCallbackInfo<Value>& args) {
188187
Local<Array> array = args[0].As<Array>();
189188
std::vector<std::unique_ptr<DataQueue::Entry>> entries(array->Length());
190189

191-
std::vector<v8::Global<Value>> sources;
190+
std::vector<Global<Value>> sources;
192191
if (FromV8Array(context, array, &sources).IsNothing()) {
193192
return;
194193
}
@@ -197,12 +196,16 @@ void Blob::New(const FunctionCallbackInfo<Value>& args) {
197196
for (size_t i = 0; i < count; i++) {
198197
Local<Value> entry = sources[i].Get(isolate);
199198

200-
const auto entryFromArrayBuffer = [isolate](v8::Local<v8::ArrayBuffer> buf,
201-
size_t byte_length,
202-
size_t byte_offset = 0) {
199+
auto entryFromArrayBuffer =
200+
[isolate](Local<ArrayBuffer> buf,
201+
size_t byte_length,
202+
size_t byte_offset =
203+
0) mutable -> std::unique_ptr<DataQueue::Entry> {
203204
if (buf->IsDetachable()) {
204205
std::shared_ptr<BackingStore> store = buf->GetBackingStore();
205-
USE(buf->Detach(Local<Value>()));
206+
if (buf->Detach(Local<Value>()).IsNothing()) {
207+
return nullptr;
208+
}
206209
return DataQueue::CreateInMemoryEntryFromBackingStore(
207210
store, byte_offset, byte_length);
208211
}
@@ -227,11 +230,15 @@ void Blob::New(const FunctionCallbackInfo<Value>& args) {
227230
// ensuring appropriate spec compliance.
228231
if (entry->IsArrayBuffer()) {
229232
Local<ArrayBuffer> buf = entry.As<ArrayBuffer>();
230-
entries[i] = entryFromArrayBuffer(buf, buf->ByteLength());
233+
auto ret = entryFromArrayBuffer(buf, buf->ByteLength());
234+
if (!ret) return;
235+
entries[i] = std::move(ret);
231236
} else if (entry->IsArrayBufferView()) {
232237
Local<ArrayBufferView> view = entry.As<ArrayBufferView>();
233-
entries[i] = entryFromArrayBuffer(
238+
auto ret = entryFromArrayBuffer(
234239
view->Buffer(), view->ByteLength(), view->ByteOffset());
240+
if (!ret) return;
241+
entries[i] = std::move(ret);
235242
} else if (Blob::HasInstance(env, entry)) {
236243
Blob* blob;
237244
ASSIGN_OR_RETURN_UNWRAP(&blob, entry);
@@ -279,22 +286,22 @@ BaseObjectPtr<Blob> Blob::Slice(Environment* env, size_t start, size_t end) {
279286
}
280287

281288
Blob::Blob(Environment* env,
282-
v8::Local<v8::Object> obj,
289+
Local<Object> obj,
283290
std::shared_ptr<DataQueue> data_queue)
284291
: BaseObject(env, obj), data_queue_(data_queue) {
285292
MakeWeak();
286293
}
287294

288295
Blob::Reader::Reader(Environment* env,
289-
v8::Local<v8::Object> obj,
296+
Local<Object> obj,
290297
BaseObjectPtr<Blob> strong_ptr)
291298
: AsyncWrap(env, obj, AsyncWrap::PROVIDER_BLOBREADER),
292299
inner_(strong_ptr->data_queue_->get_reader()),
293300
strong_ptr_(std::move(strong_ptr)) {
294301
MakeWeak();
295302
}
296303

297-
bool Blob::Reader::HasInstance(Environment* env, v8::Local<v8::Value> value) {
304+
bool Blob::Reader::HasInstance(Environment* env, Local<Value> value) {
298305
return GetConstructorTemplate(env)->HasInstance(value);
299306
}
300307

@@ -370,7 +377,7 @@ void Blob::Reader::Pull(const FunctionCallbackInfo<Value>& args) {
370377
for (size_t n = 0; n < count; n++) total += vecs[n].len;
371378

372379
std::shared_ptr<BackingStore> store =
373-
v8::ArrayBuffer::NewBackingStore(env->isolate(), total);
380+
ArrayBuffer::NewBackingStore(env->isolate(), total);
374381
auto ptr = static_cast<uint8_t*>(store->Data());
375382
for (size_t n = 0; n < count; n++) {
376383
std::copy(vecs[n].base, vecs[n].base + vecs[n].len, ptr);
@@ -415,7 +422,7 @@ std::unique_ptr<worker::TransferData> Blob::CloneForMessaging() const {
415422
return std::make_unique<BlobTransferData>(data_queue_);
416423
}
417424

418-
void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
425+
void Blob::StoreDataObject(const FunctionCallbackInfo<Value>& args) {
419426
Realm* realm = Realm::GetCurrent(args);
420427

421428
CHECK(args[0]->IsString()); // ID key
@@ -468,7 +475,7 @@ void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
468475
}
469476
}
470477

471-
void Blob::GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
478+
void Blob::GetDataObject(const FunctionCallbackInfo<Value>& args) {
472479
CHECK(args[0]->IsString());
473480
Realm* realm = Realm::GetCurrent(args);
474481
BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>();
@@ -482,7 +489,7 @@ void Blob::GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
482489
Local<Value> type;
483490
if (!String::NewFromUtf8(isolate,
484491
stored.type.c_str(),
485-
v8::NewStringType::kNormal,
492+
NewStringType::kNormal,
486493
static_cast<int>(stored.type.length()))
487494
.ToLocal(&type)) {
488495
return;
@@ -554,7 +561,7 @@ void BlobBindingData::Deserialize(Local<Context> context,
554561
}
555562

556563
bool BlobBindingData::PrepareForSerialization(Local<Context> context,
557-
v8::SnapshotCreator* creator) {
564+
SnapshotCreator* creator) {
558565
// Stored blob objects are not actually persisted.
559566
// Return true because we need to maintain the reference to the binding from
560567
// JS land.

0 commit comments

Comments
 (0)