GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/stream_pipe.cc Lines: 132 164 80.5 %
Date: 2020-02-19 22:14:06 Branches: 45 86 52.3 %

Line Branch Exec Source
1
#include "stream_pipe.h"
2
#include "stream_base-inl.h"
3
#include "node_buffer.h"
4
#include "util-inl.h"
5
6
using v8::Context;
7
using v8::Function;
8
using v8::FunctionCallbackInfo;
9
using v8::FunctionTemplate;
10
using v8::Local;
11
using v8::Object;
12
using v8::Value;
13
14
namespace node {
15
16
14
StreamPipe::StreamPipe(StreamBase* source,
17
                       StreamBase* sink,
18
14
                       Local<Object> obj)
19
14
    : AsyncWrap(source->stream_env(), obj, AsyncWrap::PROVIDER_STREAMPIPE) {
20
14
  MakeWeak();
21
22
14
  CHECK_NOT_NULL(sink);
23
14
  CHECK_NOT_NULL(source);
24
25
14
  source->PushStreamListener(&readable_listener_);
26
14
  sink->PushStreamListener(&writable_listener_);
27
28
14
  CHECK(sink->HasWantsWrite());
29
30
  // Set up links between this object and the source/sink objects.
31
  // In particular, this makes sure that they are garbage collected as a group,
32
  // if that applies to the given streams (for example, Http2Streams use
33
  // weak references).
34
70
  obj->Set(env()->context(), env()->source_string(), source->GetObject())
35
      .Check();
36
56
  source->GetObject()->Set(env()->context(), env()->pipe_target_string(), obj)
37
      .Check();
38
70
  obj->Set(env()->context(), env()->sink_string(), sink->GetObject())
39
      .Check();
40
56
  sink->GetObject()->Set(env()->context(), env()->pipe_source_string(), obj)
41
      .Check();
42
14
}
43
44
42
StreamPipe::~StreamPipe() {
45
14
  Unpipe(true);
46
28
}
47
48
655
StreamBase* StreamPipe::source() {
49
655
  return static_cast<StreamBase*>(readable_listener_.stream());
50
}
51
52
231
StreamBase* StreamPipe::sink() {
53
231
  return static_cast<StreamBase*>(writable_listener_.stream());
54
}
55
56
27
void StreamPipe::Unpipe(bool is_in_deletion) {
57
27
  if (is_closed_)
58
27
    return;
59
60
  // Note that we possibly cannot use virtual methods on `source` and `sink`
61
  // here, because this function can be called from their destructors via
62
  // `OnStreamDestroy()`.
63
14
  if (!source_destroyed_)
64
14
    source()->ReadStop();
65
66
14
  is_closed_ = true;
67
14
  is_reading_ = false;
68
14
  source()->RemoveStreamListener(&readable_listener_);
69
14
  sink()->RemoveStreamListener(&writable_listener_);
70
71
14
  if (is_in_deletion) return;
72
73
  // Delay the JS-facing part with SetImmediate, because this might be from
74
  // inside the garbage collector, so we can’t run JS here.
75
26
  HandleScope handle_scope(env()->isolate());
76
26
  BaseObjectPtr<StreamPipe> strong_ref{this};
77
104
  env()->SetImmediate([this, strong_ref](Environment* env) {
78
26
    HandleScope handle_scope(env->isolate());
79
26
    Context::Scope context_scope(env->context());
80
13
    Local<Object> object = this->object();
81
82
    Local<Value> onunpipe;
83
52
    if (!object->Get(env->context(), env->onunpipe_string()).ToLocal(&onunpipe))
84
      return;
85

39
    if (onunpipe->IsFunction() &&
86
39
        MakeCallback(onunpipe.As<Function>(), 0, nullptr).IsEmpty()) {
87
      return;
88
    }
89
90
    // Set all the links established in the constructor to `null`.
91
13
    Local<Value> null = Null(env->isolate());
92
93
    Local<Value> source_v;
94
    Local<Value> sink_v;
95

91
    if (!object->Get(env->context(), env->source_string()).ToLocal(&source_v) ||
96
78
        !object->Get(env->context(), env->sink_string()).ToLocal(&sink_v) ||
97

39
        !source_v->IsObject() || !sink_v->IsObject()) {
98
      return;
99
    }
100
101

91
    if (object->Set(env->context(), env->source_string(), null).IsNothing() ||
102
78
        object->Set(env->context(), env->sink_string(), null).IsNothing() ||
103
26
        source_v.As<Object>()
104
65
            ->Set(env->context(), env->pipe_target_string(), null)
105

39
            .IsNothing() ||
106
26
        sink_v.As<Object>()
107
65
            ->Set(env->context(), env->pipe_source_string(), null)
108
13
            .IsNothing()) {
109
      return;
110
    }
111
13
  });
112
}
113
114
212
uv_buf_t StreamPipe::ReadableListener::OnStreamAlloc(size_t suggested_size) {
115
212
  StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this);
116
212
  size_t size = std::min(suggested_size, pipe->wanted_data_);
117
212
  CHECK_GT(size, 0);
118
212
  return pipe->env()->AllocateManaged(size).release();
119
}
120
121
217
void StreamPipe::ReadableListener::OnStreamRead(ssize_t nread,
122
                                                const uv_buf_t& buf_) {
123
217
  StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this);
124
422
  AllocatedBuffer buf(pipe->env(), buf_);
125
217
  if (nread < 0) {
126
    // EOF or error; stop reading and pass the error to the previous listener
127
    // (which might end up in JS).
128
12
    pipe->is_eof_ = true;
129
12
    stream()->ReadStop();
130
12
    CHECK_NOT_NULL(previous_listener_);
131
12
    previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
132
    // If we’re not writing, close now. Otherwise, we’ll do that in
133
    // `OnStreamAfterWrite()`.
134
12
    if (!pipe->is_writing_) {
135
7
      pipe->ShutdownWritable();
136
7
      pipe->Unpipe();
137
    }
138
12
    return;
139
  }
140
141
205
  pipe->ProcessData(nread, std::move(buf));
142
}
143
144
205
void StreamPipe::ProcessData(size_t nread, AllocatedBuffer&& buf) {
145
205
  uv_buf_t buffer = uv_buf_init(buf.data(), nread);
146
205
  StreamWriteResult res = sink()->Write(&buffer, 1);
147
205
  if (!res.async) {
148
    writable_listener_.OnStreamAfterWrite(nullptr, res.err);
149
  } else {
150
205
    is_writing_ = true;
151
205
    is_reading_ = false;
152
205
    res.wrap->SetAllocatedStorage(std::move(buf));
153
205
    if (source() != nullptr)
154
205
      source()->ReadStop();
155
  }
156
205
}
157
158
12
void StreamPipe::ShutdownWritable() {
159
12
  sink()->Shutdown();
160
12
}
161
162
203
void StreamPipe::WritableListener::OnStreamAfterWrite(WriteWrap* w,
163
                                                      int status) {
164
203
  StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this);
165
203
  pipe->is_writing_ = false;
166
203
  if (pipe->is_eof_) {
167
10
    HandleScope handle_scope(pipe->env()->isolate());
168
    InternalCallbackScope callback_scope(pipe,
169
10
        InternalCallbackScope::kSkipTaskQueues);
170
5
    pipe->ShutdownWritable();
171
5
    pipe->Unpipe();
172
5
    return;
173
  }
174
175
198
  if (status != 0) {
176
1
    CHECK_NOT_NULL(previous_listener_);
177
1
    StreamListener* prev = previous_listener_;
178
1
    pipe->Unpipe();
179
1
    prev->OnStreamAfterWrite(w, status);
180
1
    return;
181
  }
182
}
183
184
void StreamPipe::WritableListener::OnStreamAfterShutdown(ShutdownWrap* w,
185
                                                         int status) {
186
  StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this);
187
  CHECK_NOT_NULL(previous_listener_);
188
  StreamListener* prev = previous_listener_;
189
  pipe->Unpipe();
190
  prev->OnStreamAfterShutdown(w, status);
191
}
192
193
void StreamPipe::ReadableListener::OnStreamDestroy() {
194
  StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this);
195
  pipe->source_destroyed_ = true;
196
  if (!pipe->is_eof_) {
197
    OnStreamRead(UV_EPIPE, uv_buf_init(nullptr, 0));
198
  }
199
}
200
201
void StreamPipe::WritableListener::OnStreamDestroy() {
202
  StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this);
203
  pipe->sink_destroyed_ = true;
204
  pipe->is_eof_ = true;
205
  pipe->Unpipe();
206
}
207
208
217
void StreamPipe::WritableListener::OnStreamWantsWrite(size_t suggested_size) {
209
217
  StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this);
210
217
  pipe->wanted_data_ = suggested_size;
211

217
  if (pipe->is_reading_ || pipe->is_closed_)
212
    return;
213
434
  HandleScope handle_scope(pipe->env()->isolate());
214
  InternalCallbackScope callback_scope(pipe,
215
434
      InternalCallbackScope::kSkipTaskQueues);
216
217
  pipe->is_reading_ = true;
217
217
  pipe->source()->ReadStart();
218
}
219
220
uv_buf_t StreamPipe::WritableListener::OnStreamAlloc(size_t suggested_size) {
221
  CHECK_NOT_NULL(previous_listener_);
222
  return previous_listener_->OnStreamAlloc(suggested_size);
223
}
224
225
void StreamPipe::WritableListener::OnStreamRead(ssize_t nread,
226
                                                const uv_buf_t& buf) {
227
  CHECK_NOT_NULL(previous_listener_);
228
  return previous_listener_->OnStreamRead(nread, buf);
229
}
230
231
14
void StreamPipe::New(const FunctionCallbackInfo<Value>& args) {
232
14
  CHECK(args.IsConstructCall());
233
28
  CHECK(args[0]->IsObject());
234
28
  CHECK(args[1]->IsObject());
235
28
  StreamBase* source = StreamBase::FromObject(args[0].As<Object>());
236
28
  StreamBase* sink = StreamBase::FromObject(args[1].As<Object>());
237
238
14
  new StreamPipe(source, sink, args.This());
239
14
}
240
241
14
void StreamPipe::Start(const FunctionCallbackInfo<Value>& args) {
242
  StreamPipe* pipe;
243
14
  ASSIGN_OR_RETURN_UNWRAP(&pipe, args.Holder());
244
14
  pipe->is_closed_ = false;
245
14
  if (pipe->wanted_data_ > 0)
246
    pipe->writable_listener_.OnStreamWantsWrite(pipe->wanted_data_);
247
}
248
249
void StreamPipe::Unpipe(const FunctionCallbackInfo<Value>& args) {
250
  StreamPipe* pipe;
251
  ASSIGN_OR_RETURN_UNWRAP(&pipe, args.Holder());
252
  pipe->Unpipe();
253
}
254
255
namespace {
256
257
221
void InitializeStreamPipe(Local<Object> target,
258
                          Local<Value> unused,
259
                          Local<Context> context,
260
                          void* priv) {
261
221
  Environment* env = Environment::GetCurrent(context);
262
263
  // Create FunctionTemplate for FileHandle::CloseReq
264
221
  Local<FunctionTemplate> pipe = env->NewFunctionTemplate(StreamPipe::New);
265
  Local<String> stream_pipe_string =
266
221
      FIXED_ONE_BYTE_STRING(env->isolate(), "StreamPipe");
267
221
  env->SetProtoMethod(pipe, "unpipe", StreamPipe::Unpipe);
268
221
  env->SetProtoMethod(pipe, "start", StreamPipe::Start);
269
442
  pipe->Inherit(AsyncWrap::GetConstructorTemplate(env));
270
221
  pipe->SetClassName(stream_pipe_string);
271
442
  pipe->InstanceTemplate()->SetInternalFieldCount(1);
272
  target
273
442
      ->Set(context, stream_pipe_string,
274
663
            pipe->GetFunction(context).ToLocalChecked())
275
      .Check();
276
221
}
277
278
}  // anonymous namespace
279
280
}  // namespace node
281
282
4185
NODE_MODULE_CONTEXT_AWARE_INTERNAL(stream_pipe,
283
                                   node::InitializeStreamPipe)