From b71250aaf985613b77d9044f9d46e216905816bb Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 16 Jul 2024 06:56:15 -0700 Subject: [PATCH] src: replace ToLocalChecked uses with ToLocal in node-file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/53869 Reviewed-By: Yagiz Nizipli Reviewed-By: Juan José Arboleda Reviewed-By: Gerhard Stöbich Reviewed-By: Tobias Nießen Reviewed-By: Anna Henningsen --- src/node_file-inl.h | 32 +++++++++++++++++++++++--------- src/node_file.cc | 33 ++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/node_file-inl.h b/src/node_file-inl.h index 6c059add3bfc02..36c2f8067c6e49 100644 --- a/src/node_file-inl.h +++ b/src/node_file-inl.h @@ -221,9 +221,15 @@ void FSReqPromise::Reject(v8::Local reject) { finished_ = true; v8::HandleScope scope(env()->isolate()); InternalCallbackScope callback_scope(this); - v8::Local value = - object()->Get(env()->context(), - env()->promise_string()).ToLocalChecked(); + v8::Local value; + if (!object() + ->Get(env()->context(), env()->promise_string()) + .ToLocal(&value)) { + // If we hit this, getting the value from the object failed and + // an error was likely scheduled. We could try to reject the promise + // but let's just allow the error to propagate. + return; + } v8::Local resolver = value.As(); USE(resolver->Reject(env()->context(), reject).FromJust()); } @@ -233,9 +239,13 @@ void FSReqPromise::Resolve(v8::Local value) { finished_ = true; v8::HandleScope scope(env()->isolate()); InternalCallbackScope callback_scope(this); - v8::Local val = - object()->Get(env()->context(), - env()->promise_string()).ToLocalChecked(); + v8::Local val; + if (!object()->Get(env()->context(), env()->promise_string()).ToLocal(&val)) { + // If we hit this, getting the value from the object failed and + // an error was likely scheduled. We could try to reject the promise + // but let's just allow the error to propagate. + return; + } v8::Local resolver = val.As(); USE(resolver->Resolve(env()->context(), value).FromJust()); } @@ -255,9 +265,13 @@ void FSReqPromise::ResolveStatFs(const uv_statfs_t* stat) { template void FSReqPromise::SetReturnValue( const v8::FunctionCallbackInfo& args) { - v8::Local val = - object()->Get(env()->context(), - env()->promise_string()).ToLocalChecked(); + v8::Local val; + if (!object()->Get(env()->context(), env()->promise_string()).ToLocal(&val)) { + // If we hit this, getting the value from the object failed and + // an error was likely scheduled. We could try to reject the promise + // but let's just allow the error to propagate. + return; + } v8::Local resolver = val.As(); args.GetReturnValue().Set(resolver->GetPromise()); } diff --git a/src/node_file.cc b/src/node_file.cc index b0aa53420c4efb..4583026ec5c8e4 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -460,7 +460,8 @@ MaybeLocal FileHandle::ClosePromise() { auto maybe_resolver = Promise::Resolver::New(context); CHECK(!maybe_resolver.IsEmpty()); - Local resolver = maybe_resolver.ToLocalChecked(); + Local resolver; + if (!maybe_resolver.ToLocal(&resolver)) return {}; Local promise = resolver.As(); Local close_req_obj; @@ -868,10 +869,12 @@ void AfterStringPath(uv_fs_t* req) { req->path, req_wrap->encoding(), &error); - if (link.IsEmpty()) + if (link.IsEmpty()) { req_wrap->Reject(error); - else - req_wrap->Resolve(link.ToLocalChecked()); + } else { + Local val; + if (link.ToLocal(&val)) req_wrap->Resolve(val); + } } } @@ -888,10 +891,12 @@ void AfterStringPtr(uv_fs_t* req) { static_cast(req->ptr), req_wrap->encoding(), &error); - if (link.IsEmpty()) + if (link.IsEmpty()) { req_wrap->Reject(error); - else - req_wrap->Resolve(link.ToLocalChecked()); + } else { + Local val; + if (link.ToLocal(&val)) req_wrap->Resolve(val); + } } } @@ -2308,7 +2313,8 @@ static void WriteBuffers(const FunctionCallbackInfo& args) { MaybeStackBuffer iovs(chunks->Length()); for (uint32_t i = 0; i < iovs.length(); i++) { - Local chunk = chunks->Get(env->context(), i).ToLocalChecked(); + Local chunk; + if (!chunks->Get(env->context(), i).ToLocal(&chunk)) return; CHECK(Buffer::HasInstance(chunk)); iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk)); } @@ -2642,8 +2648,12 @@ static void ReadFileUtf8(const FunctionCallbackInfo& args) { } FS_SYNC_TRACE_END(read); - args.GetReturnValue().Set( - ToV8Value(env->context(), result, isolate).ToLocalChecked()); + Local val; + if (!ToV8Value(env->context(), result, isolate).ToLocal(&val)) { + return; + } + + args.GetReturnValue().Set(val); } // Wrapper for readv(2). @@ -2671,7 +2681,8 @@ static void ReadBuffers(const FunctionCallbackInfo& args) { // Init uv buffers from ArrayBufferViews for (uint32_t i = 0; i < iovs.length(); i++) { - Local buffer = buffers->Get(env->context(), i).ToLocalChecked(); + Local buffer; + if (!buffers->Get(env->context(), i).ToLocal(&buffer)) return; CHECK(Buffer::HasInstance(buffer)); iovs[i] = uv_buf_init(Buffer::Data(buffer), Buffer::Length(buffer)); }