Skip to content

Commit 5e65c1f

Browse files
RaisinTendanielleadams
authored andcommitted
src: convey potential exceptions during StreamPipe construction
This moves the V8 calls during the StreamPipe construction to an overload of StreamPipe::New(), so that it is possible to indicate if there is a pending exception/termination. Refs: #40425 (comment) Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #43240 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 10f7994 commit 5e65c1f

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

src/stream_pipe.cc

+36-26
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ using v8::Function;
1111
using v8::FunctionCallbackInfo;
1212
using v8::FunctionTemplate;
1313
using v8::HandleScope;
14+
using v8::Just;
1415
using v8::Local;
16+
using v8::Maybe;
17+
using v8::Nothing;
1518
using v8::Object;
1619
using v8::Value;
1720

@@ -28,31 +31,6 @@ StreamPipe::StreamPipe(StreamBase* source,
2831
sink->PushStreamListener(&writable_listener_);
2932

3033
uses_wants_write_ = sink->HasWantsWrite();
31-
32-
// Set up links between this object and the source/sink objects.
33-
// In particular, this makes sure that they are garbage collected as a group,
34-
// if that applies to the given streams (for example, Http2Streams use
35-
// weak references).
36-
if (obj->Set(env()->context(),
37-
env()->source_string(),
38-
source->GetObject()).IsNothing()) {
39-
return;
40-
}
41-
if (source->GetObject()->Set(env()->context(),
42-
env()->pipe_target_string(),
43-
obj).IsNothing()) {
44-
return;
45-
}
46-
if (obj->Set(env()->context(),
47-
env()->sink_string(),
48-
sink->GetObject()).IsNothing()) {
49-
return;
50-
}
51-
if (sink->GetObject()->Set(env()->context(),
52-
env()->pipe_source_string(),
53-
obj).IsNothing()) {
54-
return;
55-
}
5634
}
5735

5836
StreamPipe::~StreamPipe() {
@@ -261,14 +239,46 @@ void StreamPipe::WritableListener::OnStreamRead(ssize_t nread,
261239
return previous_listener_->OnStreamRead(nread, buf);
262240
}
263241

242+
Maybe<StreamPipe*> StreamPipe::New(StreamBase* source,
243+
StreamBase* sink,
244+
Local<Object> obj) {
245+
std::unique_ptr<StreamPipe> stream_pipe(new StreamPipe(source, sink, obj));
246+
247+
// Set up links between this object and the source/sink objects.
248+
// In particular, this makes sure that they are garbage collected as a group,
249+
// if that applies to the given streams (for example, Http2Streams use
250+
// weak references).
251+
Environment* env = source->stream_env();
252+
if (obj->Set(env->context(), env->source_string(), source->GetObject())
253+
.IsNothing()) {
254+
return Nothing<StreamPipe*>();
255+
}
256+
if (source->GetObject()
257+
->Set(env->context(), env->pipe_target_string(), obj)
258+
.IsNothing()) {
259+
return Nothing<StreamPipe*>();
260+
}
261+
if (obj->Set(env->context(), env->sink_string(), sink->GetObject())
262+
.IsNothing()) {
263+
return Nothing<StreamPipe*>();
264+
}
265+
if (sink->GetObject()
266+
->Set(env->context(), env->pipe_source_string(), obj)
267+
.IsNothing()) {
268+
return Nothing<StreamPipe*>();
269+
}
270+
271+
return Just(stream_pipe.release());
272+
}
273+
264274
void StreamPipe::New(const FunctionCallbackInfo<Value>& args) {
265275
CHECK(args.IsConstructCall());
266276
CHECK(args[0]->IsObject());
267277
CHECK(args[1]->IsObject());
268278
StreamBase* source = StreamBase::FromObject(args[0].As<Object>());
269279
StreamBase* sink = StreamBase::FromObject(args[1].As<Object>());
270280

271-
new StreamPipe(source, sink, args.This());
281+
if (StreamPipe::New(source, sink, args.This()).IsNothing()) return;
272282
}
273283

274284
void StreamPipe::Start(const FunctionCallbackInfo<Value>& args) {

src/stream_pipe.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ class StreamPipe : public AsyncWrap {
1313

1414
void Unpipe(bool is_in_deletion = false);
1515

16-
// TODO(RaisinTen): Just like MessagePort, add the following overload:
17-
// static StreamPipe* New(StreamBase* source, StreamBase* sink,
18-
// v8::Local<v8::Object> obj);
19-
// so that we can indicate if there is a pending exception/termination.
16+
static v8::Maybe<StreamPipe*> New(StreamBase* source,
17+
StreamBase* sink,
18+
v8::Local<v8::Object> obj);
2019
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
2120
static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
2221
static void Unpipe(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)