@@ -11,7 +11,10 @@ using v8::Function;
1111using v8::FunctionCallbackInfo;
1212using v8::FunctionTemplate;
1313using v8::HandleScope;
14+ using v8::Just;
1415using v8::Local;
16+ using v8::Maybe;
17+ using v8::Nothing;
1518using v8::Object;
1619using 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
5836StreamPipe::~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+
264274void 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
274284void StreamPipe::Start (const FunctionCallbackInfo<Value>& args) {
0 commit comments