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 163 81.0 %
Date: 2019-07-28 22:34:34 Branches: 61 118 51.7 %

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
15
StreamPipe::StreamPipe(StreamBase* source,
17
                       StreamBase* sink,
18
                       Local<Object> obj)
19
15
    : AsyncWrap(source->stream_env(), obj, AsyncWrap::PROVIDER_STREAMPIPE) {
20
15
  MakeWeak();
21
22
15
  CHECK_NOT_NULL(sink);
23
15
  CHECK_NOT_NULL(source);
24
25
15
  source->PushStreamListener(&readable_listener_);
26
15
  sink->PushStreamListener(&writable_listener_);
27
28
15
  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
75
  obj->Set(env()->context(), env()->source_string(), source->GetObject())
35
30
      .Check();
36
60
  source->GetObject()->Set(env()->context(), env()->pipe_target_string(), obj)
37
30
      .Check();
38
75
  obj->Set(env()->context(), env()->sink_string(), sink->GetObject())
39
30
      .Check();
40
60
  sink->GetObject()->Set(env()->context(), env()->pipe_source_string(), obj)
41
30
      .Check();
42
15
}
43
44
45
StreamPipe::~StreamPipe() {
45
15
  Unpipe();
46
30
}
47
48
685
StreamBase* StreamPipe::source() {
49
685
  return static_cast<StreamBase*>(readable_listener_.stream());
50
}
51
52
242
StreamBase* StreamPipe::sink() {
53
242
  return static_cast<StreamBase*>(writable_listener_.stream());
54
}
55
56
29
void StreamPipe::Unpipe() {
57
29
  if (is_closed_)
58
43
    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
15
  if (!source_destroyed_)
64
15
    source()->ReadStop();
65
66
15
  is_closed_ = true;
67
15
  is_reading_ = false;
68
15
  source()->RemoveStreamListener(&readable_listener_);
69
15
  sink()->RemoveStreamListener(&writable_listener_);
70
71
  // Delay the JS-facing part with SetImmediate, because this might be from
72
  // inside the garbage collector, so we can’t run JS here.
73
15
  HandleScope handle_scope(env()->isolate());
74
43
  env()->SetImmediate([](Environment* env, void* data) {
75
14
    StreamPipe* pipe = static_cast<StreamPipe*>(data);
76
77
14
    HandleScope handle_scope(env->isolate());
78
28
    Context::Scope context_scope(env->context());
79
14
    Local<Object> object = pipe->object();
80
81
    Local<Value> onunpipe;
82
56
    if (!object->Get(env->context(), env->onunpipe_string()).ToLocal(&onunpipe))
83
      return;
84

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


112
    if (!object->Get(env->context(), env->source_string()).ToLocal(&source_v) ||
95

84
        !object->Get(env->context(), env->sink_string()).ToLocal(&sink_v) ||
96

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


112
    if (object->Set(env->context(), env->source_string(), null).IsNothing() ||
101

98
        object->Set(env->context(), env->sink_string(), null).IsNothing() ||
102
        source_v.As<Object>()
103

70
            ->Set(env->context(), env->pipe_target_string(), null)
104

140
            .IsNothing() ||
105
        sink_v.As<Object>()
106

70
            ->Set(env->context(), env->pipe_source_string(), null)
107
70
            .IsNothing()) {
108
      return;
109
    }
110
58
  }, static_cast<void*>(this), object());
111
}
112
113
221
uv_buf_t StreamPipe::ReadableListener::OnStreamAlloc(size_t suggested_size) {
114
221
  StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this);
115
221
  size_t size = std::min(suggested_size, pipe->wanted_data_);
116
221
  CHECK_GT(size, 0);
117
221
  return pipe->env()->AllocateManaged(size).release();
118
}
119
120
227
void StreamPipe::ReadableListener::OnStreamRead(ssize_t nread,
121
                                                const uv_buf_t& buf_) {
122
227
  StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this);
123
227
  AllocatedBuffer buf(pipe->env(), buf_);
124
441
  AsyncScope async_scope(pipe);
125
227
  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
13
    pipe->is_eof_ = true;
129
13
    stream()->ReadStop();
130
13
    CHECK_NOT_NULL(previous_listener_);
131
13
    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
13
    if (!pipe->is_writing_) {
135
7
      pipe->ShutdownWritable();
136
7
      pipe->Unpipe();
137
    }
138
240
    return;
139
  }
140
141
428
  pipe->ProcessData(nread, std::move(buf));
142
}
143
144
214
void StreamPipe::ProcessData(size_t nread, AllocatedBuffer&& buf) {
145
214
  uv_buf_t buffer = uv_buf_init(buf.data(), nread);
146
214
  StreamWriteResult res = sink()->Write(&buffer, 1);
147
214
  if (!res.async) {
148
    writable_listener_.OnStreamAfterWrite(nullptr, res.err);
149
  } else {
150
214
    is_writing_ = true;
151
214
    is_reading_ = false;
152
214
    res.wrap->SetAllocatedStorage(std::move(buf));
153
214
    if (source() != nullptr)
154
214
      source()->ReadStop();
155
  }
156
214
}
157
158
13
void StreamPipe::ShutdownWritable() {
159
13
  sink()->Shutdown();
160
13
}
161
162
211
void StreamPipe::WritableListener::OnStreamAfterWrite(WriteWrap* w,
163
                                                      int status) {
164
211
  StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this);
165
211
  pipe->is_writing_ = false;
166
211
  if (pipe->is_eof_) {
167
6
    AsyncScope async_scope(pipe);
168
6
    pipe->ShutdownWritable();
169
6
    pipe->Unpipe();
170
6
    return;
171
  }
172
173
205
  if (status != 0) {
174
1
    CHECK_NOT_NULL(previous_listener_);
175
1
    StreamListener* prev = previous_listener_;
176
1
    pipe->Unpipe();
177
1
    prev->OnStreamAfterWrite(w, status);
178
1
    return;
179
  }
180
}
181
182
void StreamPipe::WritableListener::OnStreamAfterShutdown(ShutdownWrap* w,
183
                                                         int status) {
184
  StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this);
185
  CHECK_NOT_NULL(previous_listener_);
186
  StreamListener* prev = previous_listener_;
187
  pipe->Unpipe();
188
  prev->OnStreamAfterShutdown(w, status);
189
}
190
191
void StreamPipe::ReadableListener::OnStreamDestroy() {
192
  StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this);
193
  pipe->source_destroyed_ = true;
194
  if (!pipe->is_eof_) {
195
    OnStreamRead(UV_EPIPE, uv_buf_init(nullptr, 0));
196
  }
197
}
198
199
void StreamPipe::WritableListener::OnStreamDestroy() {
200
  StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this);
201
  pipe->sink_destroyed_ = true;
202
  pipe->is_eof_ = true;
203
  pipe->Unpipe();
204
}
205
206
227
void StreamPipe::WritableListener::OnStreamWantsWrite(size_t suggested_size) {
207
227
  StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this);
208
227
  pipe->wanted_data_ = suggested_size;
209

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