Skip to content

Commit

Permalink
src: pass along errors from stream obj instantiation
Browse files Browse the repository at this point in the history
PR-URL: #25734
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
  • Loading branch information
addaleax committed Jan 29, 2019
1 parent 5556975 commit 4dbff09
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 44 deletions.
7 changes: 4 additions & 3 deletions src/connection_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,

if (status == 0) {
// Instantiate the client javascript object and handle.
Local<Object> client_obj = WrapType::Instantiate(env,
wrap_data,
WrapType::SOCKET);
Local<Object> client_obj;
if (!WrapType::Instantiate(env, wrap_data, WrapType::SOCKET)
.ToLocal(&client_obj))
return;

// Unwrap the client javascript object.
WrapType* wrap;
Expand Down
13 changes: 6 additions & 7 deletions src/pipe_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Int32;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Value;

using AsyncHooks = Environment::AsyncHooks;


Local<Object> PipeWrap::Instantiate(Environment* env,
AsyncWrap* parent,
PipeWrap::SocketType type) {
MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
AsyncWrap* parent,
PipeWrap::SocketType type) {
EscapableHandleScope handle_scope(env->isolate());
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
Expand All @@ -60,9 +60,8 @@ Local<Object> PipeWrap::Instantiate(Environment* env,
.ToLocalChecked();
CHECK_EQ(false, constructor.IsEmpty());
Local<Value> type_value = Int32::New(env->isolate(), type);
Local<Object> instance =
constructor->NewInstance(env->context(), 1, &type_value).ToLocalChecked();
return handle_scope.Escape(instance);
return handle_scope.EscapeMaybe(
constructor->NewInstance(env->context(), 1, &type_value));
}


Expand Down
6 changes: 3 additions & 3 deletions src/pipe_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class PipeWrap : public ConnectionWrap<PipeWrap, uv_pipe_t> {
IPC
};

static v8::Local<v8::Object> Instantiate(Environment* env,
AsyncWrap* parent,
SocketType type);
static v8::MaybeLocal<v8::Object> Instantiate(Environment* env,
AsyncWrap* parent,
SocketType type);
static void Initialize(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
Expand Down
19 changes: 10 additions & 9 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::ReadOnly;
using v8::Signature;
Expand Down Expand Up @@ -195,19 +196,17 @@ void LibuvStreamWrap::OnUvAlloc(size_t suggested_size, uv_buf_t* buf) {
*buf = EmitAlloc(suggested_size);
}



template <class WrapType>
static Local<Object> AcceptHandle(Environment* env, LibuvStreamWrap* parent) {
static MaybeLocal<Object> AcceptHandle(Environment* env,
LibuvStreamWrap* parent) {
static_assert(std::is_base_of<LibuvStreamWrap, WrapType>::value ||
std::is_base_of<UDPWrap, WrapType>::value,
"Can only accept stream handles");

EscapableHandleScope scope(env->isolate());
Local<Object> wrap_obj;

wrap_obj = WrapType::Instantiate(env, parent, WrapType::SOCKET);
if (wrap_obj.IsEmpty())
if (!WrapType::Instantiate(env, parent, WrapType::SOCKET).ToLocal(&wrap_obj))
return Local<Object>();

HandleWrap* wrap = Unwrap<HandleWrap>(wrap_obj);
Expand Down Expand Up @@ -237,7 +236,7 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
CHECK_EQ(persistent().IsEmpty(), false);

if (nread > 0) {
Local<Object> pending_obj;
MaybeLocal<Object> pending_obj;

if (type == UV_TCP) {
pending_obj = AcceptHandle<TCPWrap>(env(), this);
Expand All @@ -250,9 +249,11 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
}

if (!pending_obj.IsEmpty()) {
object()->Set(env()->context(),
env()->pending_handle_string(),
pending_obj).FromJust();
object()
->Set(env()->context(),
env()->pending_handle_string(),
pending_obj.ToLocalChecked())
.FromJust();
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ using v8::HandleScope;
using v8::Int32;
using v8::Integer;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Uint32;
using v8::Value;

using AsyncHooks = Environment::AsyncHooks;


Local<Object> TCPWrap::Instantiate(Environment* env,
AsyncWrap* parent,
TCPWrap::SocketType type) {
MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
AsyncWrap* parent,
TCPWrap::SocketType type) {
EscapableHandleScope handle_scope(env->isolate());
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false);
Expand All @@ -65,9 +65,8 @@ Local<Object> TCPWrap::Instantiate(Environment* env,
.ToLocalChecked();
CHECK_EQ(constructor.IsEmpty(), false);
Local<Value> type_value = Int32::New(env->isolate(), type);
Local<Object> instance =
constructor->NewInstance(env->context(), 1, &type_value).ToLocalChecked();
return handle_scope.Escape(instance);
return handle_scope.EscapeMaybe(
constructor->NewInstance(env->context(), 1, &type_value));
}


Expand Down
6 changes: 3 additions & 3 deletions src/tcp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
SERVER
};

static v8::Local<v8::Object> Instantiate(Environment* env,
AsyncWrap* parent,
SocketType type);
static v8::MaybeLocal<v8::Object> Instantiate(Environment* env,
AsyncWrap* parent,
SocketType type);
static void Initialize(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
Expand Down
14 changes: 5 additions & 9 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ namespace node {

using v8::Array;
using v8::Context;
using v8::EscapableHandleScope;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::PropertyAttribute;
using v8::Signature;
Expand Down Expand Up @@ -518,18 +518,14 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv);
}


Local<Object> UDPWrap::Instantiate(Environment* env,
AsyncWrap* parent,
UDPWrap::SocketType type) {
EscapableHandleScope scope(env->isolate());
MaybeLocal<Object> UDPWrap::Instantiate(Environment* env,
AsyncWrap* parent,
UDPWrap::SocketType type) {
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);

// If this assert fires then Initialize hasn't been called yet.
CHECK_EQ(env->udp_constructor_function().IsEmpty(), false);
Local<Object> instance = env->udp_constructor_function()
->NewInstance(env->context()).ToLocalChecked();
return scope.Escape(instance);
return env->udp_constructor_function()->NewInstance(env->context());
}


Expand Down
6 changes: 3 additions & 3 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class UDPWrap: public HandleWrap {
static void SetTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void BufferSize(const v8::FunctionCallbackInfo<v8::Value>& args);

static v8::Local<v8::Object> Instantiate(Environment* env,
AsyncWrap* parent,
SocketType type);
static v8::MaybeLocal<v8::Object> Instantiate(Environment* env,
AsyncWrap* parent,
SocketType type);
SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(UDPWrap)
SET_SELF_SIZE(UDPWrap)
Expand Down

0 comments on commit 4dbff09

Please sign in to comment.