From 2e181f68a37f94f57313577189b889159675b7ed Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Fri, 8 Jul 2022 05:37:50 +0900 Subject: [PATCH] src,stream: change return type to `Maybe` This changes the return types of some functions to indicate that the functions may have a pending exception, and removes some of todos related. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: https://github.com/nodejs/node/pull/43575 Reviewed-By: Darshan Sen Reviewed-By: Anna Henningsen --- src/stream_wrap.cc | 43 +++++++++++++++++++++++++++---------------- src/stream_wrap.h | 4 +--- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index ef85ba681f8588..d0c5664adcd897 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -25,6 +25,7 @@ #include "env-inl.h" #include "handle_wrap.h" #include "node_buffer.h" +#include "node_errors.h" #include "node_external_reference.h" #include "pipe_wrap.h" #include "req_wrap-inl.h" @@ -38,14 +39,18 @@ namespace node { +using errors::TryCatchScope; using v8::Context; using v8::DontDelete; using v8::EscapableHandleScope; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; +using v8::JustVoid; using v8::Local; +using v8::Maybe; using v8::MaybeLocal; +using v8::Nothing; using v8::Object; using v8::PropertyAttribute; using v8::ReadOnly; @@ -191,15 +196,19 @@ bool LibuvStreamWrap::IsIPCPipe() { return is_named_pipe_ipc(); } - int LibuvStreamWrap::ReadStart() { - return uv_read_start(stream(), [](uv_handle_t* handle, - size_t suggested_size, - uv_buf_t* buf) { - static_cast(handle->data)->OnUvAlloc(suggested_size, buf); - }, [](uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { - static_cast(stream->data)->OnUvRead(nread, buf); - }); + return uv_read_start( + stream(), + [](uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { + static_cast(handle->data) + ->OnUvAlloc(suggested_size, buf); + }, + [](uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { + LibuvStreamWrap* wrap = static_cast(stream->data); + TryCatchScope try_catch(wrap->env()); + try_catch.SetVerbose(true); + wrap->OnUvRead(nread, buf); + }); } @@ -239,8 +248,7 @@ static MaybeLocal AcceptHandle(Environment* env, return scope.Escape(wrap_obj); } - -void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) { +Maybe LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) { HandleScope scope(env()->isolate()); Context::Scope context_scope(env()->context()); uv_handle_type type = UV_UNKNOWN_HANDLE; @@ -268,18 +276,21 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) { } Local local_pending_obj; - if (pending_obj.ToLocal(&local_pending_obj) && - object()->Set(env()->context(), - env()->pending_handle_string(), - local_pending_obj).IsNothing()) { - return; + if (type != UV_UNKNOWN_HANDLE && + (!pending_obj.ToLocal(&local_pending_obj) || + object() + ->Set(env()->context(), + env()->pending_handle_string(), + local_pending_obj) + .IsNothing())) { + return Nothing(); } } EmitRead(nread, *buf); + return JustVoid(); } - void LibuvStreamWrap::GetWriteQueueSize( const FunctionCallbackInfo& info) { LibuvStreamWrap* wrap; diff --git a/src/stream_wrap.h b/src/stream_wrap.h index 8cbce923bfed89..197303990c90bc 100644 --- a/src/stream_wrap.h +++ b/src/stream_wrap.h @@ -105,9 +105,7 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase { // Callbacks for libuv void OnUvAlloc(size_t suggested_size, uv_buf_t* buf); - // TODO(RaisinTen): Update the return type to a Maybe, so that we can indicate - // if there is a pending exception/termination. - void OnUvRead(ssize_t nread, const uv_buf_t* buf); + v8::Maybe OnUvRead(ssize_t nread, const uv_buf_t* buf); static void AfterUvWrite(uv_write_t* req, int status); static void AfterUvShutdown(uv_shutdown_t* req, int status);