Skip to content

Commit

Permalink
src,stream: change return type to Maybe
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
daeyeon committed Jul 7, 2022
1 parent e9f69e8 commit 141052d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
43 changes: 27 additions & 16 deletions src/stream_wrap.cc
Expand Up @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -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<LibuvStreamWrap*>(handle->data)->OnUvAlloc(suggested_size, buf);
}, [](uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
static_cast<LibuvStreamWrap*>(stream->data)->OnUvRead(nread, buf);
});
return uv_read_start(
stream(),
[](uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
static_cast<LibuvStreamWrap*>(handle->data)
->OnUvAlloc(suggested_size, buf);
},
[](uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
LibuvStreamWrap* wrap = static_cast<LibuvStreamWrap*>(stream->data);
TryCatchScope try_catch(wrap->env());
try_catch.SetVerbose(true);
wrap->OnUvRead(nread, buf);
});
}


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


void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
Maybe<void> 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;
Expand Down Expand Up @@ -268,18 +276,21 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
}

Local<Object> 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<void>();
}
}

EmitRead(nread, *buf);
return JustVoid();
}


void LibuvStreamWrap::GetWriteQueueSize(
const FunctionCallbackInfo<Value>& info) {
LibuvStreamWrap* wrap;
Expand Down
4 changes: 1 addition & 3 deletions src/stream_wrap.h
Expand Up @@ -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<void> 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);
Expand Down

0 comments on commit 141052d

Please sign in to comment.