@@ -11,7 +11,10 @@ using v8::Function; |
11 | 11 | using v8::FunctionCallbackInfo; |
12 | 12 | using v8::FunctionTemplate; |
13 | 13 | using v8::HandleScope; |
| 14 | +using v8::Just; |
14 | 15 | using v8::Local; |
| 16 | +using v8::Maybe; |
| 17 | +using v8::Nothing; |
15 | 18 | using v8::Object; |
16 | 19 | using v8::Value; |
17 | 20 |
|
@@ -28,31 +31,6 @@ StreamPipe::StreamPipe(StreamBase* source, |
28 | 31 | sink->PushStreamListener(&writable_listener_); |
29 | 32 |
|
30 | 33 | 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 | | - } |
56 | 34 | } |
57 | 35 |
|
58 | 36 | StreamPipe::~StreamPipe() { |
@@ -261,14 +239,46 @@ void StreamPipe::WritableListener::OnStreamRead(ssize_t nread, |
261 | 239 | return previous_listener_->OnStreamRead(nread, buf); |
262 | 240 | } |
263 | 241 |
|
| 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 | + |
264 | 274 | void StreamPipe::New(const FunctionCallbackInfo<Value>& args) { |
265 | 275 | CHECK(args.IsConstructCall()); |
266 | 276 | CHECK(args[0]->IsObject()); |
267 | 277 | CHECK(args[1]->IsObject()); |
268 | 278 | StreamBase* source = StreamBase::FromObject(args[0].As<Object>()); |
269 | 279 | StreamBase* sink = StreamBase::FromObject(args[1].As<Object>()); |
270 | 280 |
|
271 | | - new StreamPipe(source, sink, args.This()); |
| 281 | + if (StreamPipe::New(source, sink, args.This()).IsNothing()) return; |
272 | 282 | } |
273 | 283 |
|
274 | 284 | void StreamPipe::Start(const FunctionCallbackInfo<Value>& args) { |
|