Skip to content

Commit 2e181f6

Browse files
daeyeontargos
authored andcommitted
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: #43575 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 482bd53 commit 2e181f6

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/stream_wrap.cc

+27-16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "env-inl.h"
2626
#include "handle_wrap.h"
2727
#include "node_buffer.h"
28+
#include "node_errors.h"
2829
#include "node_external_reference.h"
2930
#include "pipe_wrap.h"
3031
#include "req_wrap-inl.h"
@@ -38,14 +39,18 @@
3839

3940
namespace node {
4041

42+
using errors::TryCatchScope;
4143
using v8::Context;
4244
using v8::DontDelete;
4345
using v8::EscapableHandleScope;
4446
using v8::FunctionCallbackInfo;
4547
using v8::FunctionTemplate;
4648
using v8::HandleScope;
49+
using v8::JustVoid;
4750
using v8::Local;
51+
using v8::Maybe;
4852
using v8::MaybeLocal;
53+
using v8::Nothing;
4954
using v8::Object;
5055
using v8::PropertyAttribute;
5156
using v8::ReadOnly;
@@ -191,15 +196,19 @@ bool LibuvStreamWrap::IsIPCPipe() {
191196
return is_named_pipe_ipc();
192197
}
193198

194-
195199
int LibuvStreamWrap::ReadStart() {
196-
return uv_read_start(stream(), [](uv_handle_t* handle,
197-
size_t suggested_size,
198-
uv_buf_t* buf) {
199-
static_cast<LibuvStreamWrap*>(handle->data)->OnUvAlloc(suggested_size, buf);
200-
}, [](uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
201-
static_cast<LibuvStreamWrap*>(stream->data)->OnUvRead(nread, buf);
202-
});
200+
return uv_read_start(
201+
stream(),
202+
[](uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
203+
static_cast<LibuvStreamWrap*>(handle->data)
204+
->OnUvAlloc(suggested_size, buf);
205+
},
206+
[](uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
207+
LibuvStreamWrap* wrap = static_cast<LibuvStreamWrap*>(stream->data);
208+
TryCatchScope try_catch(wrap->env());
209+
try_catch.SetVerbose(true);
210+
wrap->OnUvRead(nread, buf);
211+
});
203212
}
204213

205214

@@ -239,8 +248,7 @@ static MaybeLocal<Object> AcceptHandle(Environment* env,
239248
return scope.Escape(wrap_obj);
240249
}
241250

242-
243-
void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
251+
Maybe<void> LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
244252
HandleScope scope(env()->isolate());
245253
Context::Scope context_scope(env()->context());
246254
uv_handle_type type = UV_UNKNOWN_HANDLE;
@@ -268,18 +276,21 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
268276
}
269277

270278
Local<Object> local_pending_obj;
271-
if (pending_obj.ToLocal(&local_pending_obj) &&
272-
object()->Set(env()->context(),
273-
env()->pending_handle_string(),
274-
local_pending_obj).IsNothing()) {
275-
return;
279+
if (type != UV_UNKNOWN_HANDLE &&
280+
(!pending_obj.ToLocal(&local_pending_obj) ||
281+
object()
282+
->Set(env()->context(),
283+
env()->pending_handle_string(),
284+
local_pending_obj)
285+
.IsNothing())) {
286+
return Nothing<void>();
276287
}
277288
}
278289

279290
EmitRead(nread, *buf);
291+
return JustVoid();
280292
}
281293

282-
283294
void LibuvStreamWrap::GetWriteQueueSize(
284295
const FunctionCallbackInfo<Value>& info) {
285296
LibuvStreamWrap* wrap;

src/stream_wrap.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase {
105105

106106
// Callbacks for libuv
107107
void OnUvAlloc(size_t suggested_size, uv_buf_t* buf);
108-
// TODO(RaisinTen): Update the return type to a Maybe, so that we can indicate
109-
// if there is a pending exception/termination.
110-
void OnUvRead(ssize_t nread, const uv_buf_t* buf);
108+
v8::Maybe<void> OnUvRead(ssize_t nread, const uv_buf_t* buf);
111109

112110
static void AfterUvWrite(uv_write_t* req, int status);
113111
static void AfterUvShutdown(uv_shutdown_t* req, int status);

0 commit comments

Comments
 (0)