GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/stream_base-inl.h Lines: 167 171 97.7 %
Date: 2019-09-07 22:28:56 Branches: 49 64 76.6 %

Line Branch Exec Source
1
#ifndef SRC_STREAM_BASE_INL_H_
2
#define SRC_STREAM_BASE_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "stream_base.h"
7
8
#include "node.h"
9
#include "env-inl.h"
10
#include "v8.h"
11
12
namespace node {
13
14
using v8::Signature;
15
using v8::FunctionCallbackInfo;
16
using v8::FunctionTemplate;
17
using v8::HandleScope;
18
using v8::Local;
19
using v8::Object;
20
using v8::PropertyAttribute;
21
using v8::PropertyCallbackInfo;
22
using v8::String;
23
using v8::Value;
24
25
63089
inline void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
26
126178
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField),
27
           nullptr);
28
63089
  req_wrap_obj->SetAlignedPointerInInternalField(kStreamReqField, this);
29
63089
}
30
31
603
inline StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
32
  return static_cast<StreamReq*>(
33
1206
      req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField));
34
}
35
36
63067
inline void StreamReq::Dispose() {
37
126134
  object()->SetAlignedPointerInInternalField(kStreamReqField, nullptr);
38
63067
  delete this;
39
63067
}
40
41
63678
inline v8::Local<v8::Object> StreamReq::object() {
42
63678
  return GetAsyncWrap()->object();
43
}
44
45
133707
inline StreamListener::~StreamListener() {
46
133707
  if (stream_ != nullptr)
47
118029
    stream_->RemoveStreamListener(this);
48
133707
}
49
50
41005
inline void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) {
51
41005
  CHECK_NOT_NULL(previous_listener_);
52
41005
  previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
53
41005
}
54
55
30439
inline void StreamListener::OnStreamAfterShutdown(ShutdownWrap* w, int status) {
56
30439
  CHECK_NOT_NULL(previous_listener_);
57
30439
  previous_listener_->OnStreamAfterShutdown(w, status);
58
30439
}
59
60
3687
inline void StreamListener::OnStreamAfterWrite(WriteWrap* w, int status) {
61
3687
  CHECK_NOT_NULL(previous_listener_);
62
3687
  previous_listener_->OnStreamAfterWrite(w, status);
63
3687
}
64
65
93576
inline StreamResource::~StreamResource() {
66
187706
  while (listener_ != nullptr) {
67
554
    StreamListener* listener = listener_;
68
554
    listener->OnStreamDestroy();
69
    // Remove the listener if it didn’t remove itself. This makes the logic
70
    // in `OnStreamDestroy()` implementations easier, because they
71
    // may call generic cleanup functions which can just remove the
72
    // listener unconditionally.
73
554
    if (listener == listener_)
74
548
      RemoveStreamListener(listener_);
75
  }
76
93576
}
77
78
162539
inline void StreamResource::PushStreamListener(StreamListener* listener) {
79
162539
  CHECK_NOT_NULL(listener);
80
162539
  CHECK_NULL(listener->stream_);
81
82
162539
  listener->previous_listener_ = listener_;
83
162539
  listener->stream_ = this;
84
85
162539
  listener_ = listener;
86
162539
}
87
88
162048
inline void StreamResource::RemoveStreamListener(StreamListener* listener) {
89
162048
  CHECK_NOT_NULL(listener);
90
91
  StreamListener* previous;
92
  StreamListener* current;
93
94
  // Remove from the linked list.
95
162602
  for (current = listener_, previous = nullptr;
96
       /* No loop condition because we want a crash if listener is not found */
97
       ; previous = current, current = current->previous_listener_) {
98
162602
    CHECK_NOT_NULL(current);
99
162602
    if (current == listener) {
100
162048
      if (previous != nullptr)
101
554
        previous->previous_listener_ = current->previous_listener_;
102
      else
103
161494
        listener_ = listener->previous_listener_;
104
162048
      break;
105
    }
106
554
  }
107
108
162048
  listener->stream_ = nullptr;
109
162048
  listener->previous_listener_ = nullptr;
110
162048
}
111
112
511990
inline uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
113
511990
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
114
511990
  return listener_->OnStreamAlloc(suggested_size);
115
}
116
117
526144
inline void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) {
118
526144
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
119
526144
  if (nread > 0)
120
477157
    bytes_read_ += static_cast<uint64_t>(nread);
121
526144
  listener_->OnStreamRead(nread, buf);
122
526061
}
123
124
6183
inline void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
125
6183
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
126
6183
  listener_->OnStreamAfterWrite(w, status);
127
6181
}
128
129
33082
inline void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
130
33082
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
131
33082
  listener_->OnStreamAfterShutdown(w, status);
132
33082
}
133
134
3590
inline void StreamResource::EmitWantsWrite(size_t suggested_size) {
135
3590
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
136
3590
  listener_->OnStreamWantsWrite(suggested_size);
137
3590
}
138
139
94032
inline StreamBase::StreamBase(Environment* env) : env_(env) {
140
94032
  PushStreamListener(&default_listener_);
141
94032
}
142
143
1056484
inline Environment* StreamBase::stream_env() const {
144
1056484
  return env_;
145
}
146
147
56881
inline int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
148
56881
  Environment* env = stream_env();
149
150
56881
  HandleScope handle_scope(env->isolate());
151
152
56881
  if (req_wrap_obj.IsEmpty()) {
153
1054
    if (!env->shutdown_wrap_template()
154
1581
             ->NewInstance(env->context())
155
1581
             .ToLocal(&req_wrap_obj)) {
156
      return UV_EBUSY;
157
    }
158
527
    StreamReq::ResetObject(req_wrap_obj);
159
  }
160
161
113762
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
162
56881
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
163
56881
  int err = DoShutdown(req_wrap);
164
165
56881
  if (err != 0) {
166
23798
    req_wrap->Dispose();
167
  }
168
169
56881
  const char* msg = Error();
170
56881
  if (msg != nullptr) {
171
    req_wrap_obj->Set(
172
        env->context(),
173
        env->error_string(), OneByteString(env->isolate(), msg)).Check();
174
    ClearError();
175
  }
176
177
113762
  return err;
178
}
179
180
207109
inline StreamWriteResult StreamBase::Write(
181
    uv_buf_t* bufs,
182
    size_t count,
183
    uv_stream_t* send_handle,
184
    v8::Local<v8::Object> req_wrap_obj) {
185
207109
  Environment* env = stream_env();
186
  int err;
187
188
207109
  size_t total_bytes = 0;
189
1046827
  for (size_t i = 0; i < count; ++i)
190
839718
    total_bytes += bufs[i].len;
191
207109
  bytes_written_ += total_bytes;
192
193
207109
  if (send_handle == nullptr) {
194
207005
    err = DoTryWrite(&bufs, &count);
195

207005
    if (err != 0 || count == 0) {
196
200901
      return StreamWriteResult { false, err, nullptr, total_bytes };
197
    }
198
  }
199
200
6208
  HandleScope handle_scope(env->isolate());
201
202
6208
  if (req_wrap_obj.IsEmpty()) {
203
2812
    if (!env->write_wrap_template()
204
4218
             ->NewInstance(env->context())
205
4218
             .ToLocal(&req_wrap_obj)) {
206
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0 };
207
    }
208
1406
    StreamReq::ResetObject(req_wrap_obj);
209
  }
210
211
12415
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
212
6208
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
213
214
6208
  err = DoWrite(req_wrap, bufs, count, send_handle);
215
6207
  bool async = err == 0;
216
217
6207
  if (!async) {
218
6
    req_wrap->Dispose();
219
6
    req_wrap = nullptr;
220
  }
221
222
6207
  const char* msg = Error();
223
6207
  if (msg != nullptr) {
224
    req_wrap_obj->Set(env->context(),
225
                      env->error_string(),
226
15
                      OneByteString(env->isolate(), msg)).Check();
227
3
    ClearError();
228
  }
229
230
12414
  return StreamWriteResult { async, err, req_wrap, total_bytes };
231
}
232
233
template <typename OtherBase>
234
56881
SimpleShutdownWrap<OtherBase>::SimpleShutdownWrap(
235
    StreamBase* stream,
236
    v8::Local<v8::Object> req_wrap_obj)
237
  : ShutdownWrap(stream, req_wrap_obj),
238
    OtherBase(stream->stream_env(),
239
              req_wrap_obj,
240
56881
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
241
56881
}
242
243
23806
inline ShutdownWrap* StreamBase::CreateShutdownWrap(
244
    v8::Local<v8::Object> object) {
245
23806
  return new SimpleShutdownWrap<AsyncWrap>(this, object);
246
}
247
248
template <typename OtherBase>
249
6208
SimpleWriteWrap<OtherBase>::SimpleWriteWrap(
250
    StreamBase* stream,
251
    v8::Local<v8::Object> req_wrap_obj)
252
  : WriteWrap(stream, req_wrap_obj),
253
    OtherBase(stream->stream_env(),
254
              req_wrap_obj,
255
6208
              AsyncWrap::PROVIDER_WRITEWRAP) {
256
6208
}
257
258
5919
inline WriteWrap* StreamBase::CreateWriteWrap(
259
    v8::Local<v8::Object> object) {
260
5919
  return new SimpleWriteWrap<AsyncWrap>(this, object);
261
}
262
263
94032
inline void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
264
94032
  obj->SetAlignedPointerInInternalField(kStreamBaseField, this);
265
94032
}
266
267
1186443
inline StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
268
2372886
  if (obj->GetAlignedPointerFromInternalField(0) == nullptr)
269
1
    return nullptr;
270
271
  return static_cast<StreamBase*>(
272
2372884
      obj->GetAlignedPointerFromInternalField(kStreamBaseField));
273
}
274
275
276
33082
inline void ShutdownWrap::OnDone(int status) {
277
33082
  stream()->EmitAfterShutdown(this, status);
278
33082
  Dispose();
279
33082
}
280
281
3525
inline void WriteWrap::SetAllocatedStorage(AllocatedBuffer&& storage) {
282
3525
  CHECK_NULL(storage_.data());
283
3525
  storage_ = std::move(storage);
284
3525
}
285
286
6183
inline void WriteWrap::OnDone(int status) {
287
6183
  stream()->EmitAfterWrite(this, status);
288
6181
  Dispose();
289
6181
}
290
291
39265
inline void StreamReq::Done(int status, const char* error_str) {
292
39265
  AsyncWrap* async_wrap = GetAsyncWrap();
293
39265
  Environment* env = async_wrap->env();
294
39265
  if (error_str != nullptr) {
295
6
    async_wrap->object()->Set(env->context(),
296
                              env->error_string(),
297
36
                              OneByteString(env->isolate(), error_str))
298
12
                              .Check();
299
  }
300
301
39265
  OnDone(status);
302
39263
}
303
304
939935
inline void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
305
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
306
307
939935
  obj->SetAlignedPointerInInternalField(0, nullptr);  // BaseObject field.
308
939935
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
309
939935
}
310
311
312
}  // namespace node
313
314
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
315
316
#endif  // SRC_STREAM_BASE_INL_H_