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-07-27 22:37:30 Branches: 48 64 75.0 %

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
49476
inline void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
26
98952
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField),
27
           nullptr);
28
49476
  req_wrap_obj->SetAlignedPointerInInternalField(kStreamReqField, this);
29
49476
}
30
31
599
inline StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
32
  return static_cast<StreamReq*>(
33
1198
      req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField));
34
}
35
36
49455
inline void StreamReq::Dispose() {
37
98910
  object()->SetAlignedPointerInInternalField(kStreamReqField, nullptr);
38
49455
  delete this;
39
49455
}
40
41
50061
inline v8::Local<v8::Object> StreamReq::object() {
42
50061
  return GetAsyncWrap()->object();
43
}
44
45
117679
inline StreamListener::~StreamListener() {
46
117679
  if (stream_ != nullptr)
47
102072
    stream_->RemoveStreamListener(this);
48
117679
}
49
50
28000
inline void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) {
51
28000
  CHECK_NOT_NULL(previous_listener_);
52
28000
  previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
53
28000
}
54
55
17240
inline void StreamListener::OnStreamAfterShutdown(ShutdownWrap* w, int status) {
56
17240
  CHECK_NOT_NULL(previous_listener_);
57
17240
  previous_listener_->OnStreamAfterShutdown(w, status);
58
17240
}
59
60
3590
inline void StreamListener::OnStreamAfterWrite(WriteWrap* w, int status) {
61
3590
  CHECK_NOT_NULL(previous_listener_);
62
3590
  previous_listener_->OnStreamAfterWrite(w, status);
63
3590
}
64
65
78832
inline StreamResource::~StreamResource() {
66
158204
  while (listener_ != nullptr) {
67
540
    StreamListener* listener = listener_;
68
540
    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
540
    if (listener == listener_)
74
540
      RemoveStreamListener(listener_);
75
  }
76
78832
}
77
78
133457
inline void StreamResource::PushStreamListener(StreamListener* listener) {
79
133457
  CHECK_NOT_NULL(listener);
80
133457
  CHECK_NULL(listener->stream_);
81
82
133457
  listener->previous_listener_ = listener_;
83
133457
  listener->stream_ = this;
84
85
133457
  listener_ = listener;
86
133457
}
87
88
132949
inline void StreamResource::RemoveStreamListener(StreamListener* listener) {
89
132949
  CHECK_NOT_NULL(listener);
90
91
  StreamListener* previous;
92
  StreamListener* current;
93
94
  // Remove from the linked list.
95
133489
  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
133489
    CHECK_NOT_NULL(current);
99
133489
    if (current == listener) {
100
132949
      if (previous != nullptr)
101
540
        previous->previous_listener_ = current->previous_listener_;
102
      else
103
132409
        listener_ = listener->previous_listener_;
104
132949
      break;
105
    }
106
540
  }
107
108
132949
  listener->stream_ = nullptr;
109
132949
  listener->previous_listener_ = nullptr;
110
132949
}
111
112
436897
inline uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
113
436897
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
114
436897
  return listener_->OnStreamAlloc(suggested_size);
115
}
116
117
451053
inline void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) {
118
451053
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
119
451053
  if (nread > 0)
120
415176
    bytes_read_ += static_cast<uint64_t>(nread);
121
451053
  listener_->OnStreamRead(nread, buf);
122
450971
}
123
124
6086
inline void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
125
6086
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
126
6086
  listener_->OnStreamAfterWrite(w, status);
127
6084
}
128
129
19870
inline void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
130
19870
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
131
19870
  listener_->OnStreamAfterShutdown(w, status);
132
19870
}
133
134
3598
inline void StreamResource::EmitWantsWrite(size_t suggested_size) {
135
3598
  DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent());
136
3598
  listener_->OnStreamWantsWrite(suggested_size);
137
3598
}
138
139
79305
inline StreamBase::StreamBase(Environment* env) : env_(env) {
140
79305
  PushStreamListener(&default_listener_);
141
79305
}
142
143
934155
inline Environment* StreamBase::stream_env() const {
144
934155
  return env_;
145
}
146
147
43368
inline int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
148
43368
  Environment* env = stream_env();
149
150
43368
  HandleScope handle_scope(env->isolate());
151
152
43368
  if (req_wrap_obj.IsEmpty()) {
153
850
    if (!env->shutdown_wrap_template()
154
1275
             ->NewInstance(env->context())
155
1275
             .ToLocal(&req_wrap_obj)) {
156
      return UV_EBUSY;
157
    }
158
425
    StreamReq::ResetObject(req_wrap_obj);
159
  }
160
161
86736
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
162
43368
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
163
43368
  int err = DoShutdown(req_wrap);
164
165
43368
  if (err != 0) {
166
23497
    req_wrap->Dispose();
167
  }
168
169
43368
  const char* msg = Error();
170
43368
  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
86736
  return err;
178
}
179
180
168956
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
168956
  Environment* env = stream_env();
186
  int err;
187
188
168956
  size_t total_bytes = 0;
189
796272
  for (size_t i = 0; i < count; ++i)
190
627316
    total_bytes += bufs[i].len;
191
168956
  bytes_written_ += total_bytes;
192
193
168956
  if (send_handle == nullptr) {
194
168854
    err = DoTryWrite(&bufs, &count);
195

168854
    if (err != 0 || count == 0) {
196
162848
      return StreamWriteResult { false, err, nullptr, total_bytes };
197
    }
198
  }
199
200
6108
  HandleScope handle_scope(env->isolate());
201
202
6108
  if (req_wrap_obj.IsEmpty()) {
203
2790
    if (!env->write_wrap_template()
204
4185
             ->NewInstance(env->context())
205
4185
             .ToLocal(&req_wrap_obj)) {
206
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0 };
207
    }
208
1395
    StreamReq::ResetObject(req_wrap_obj);
209
  }
210
211
12215
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
212
6108
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
213
214
6108
  err = DoWrite(req_wrap, bufs, count, send_handle);
215
6107
  bool async = err == 0;
216
217
6107
  if (!async) {
218
4
    req_wrap->Dispose();
219
4
    req_wrap = nullptr;
220
  }
221
222
6107
  const char* msg = Error();
223
6107
  if (msg != nullptr) {
224
    req_wrap_obj->Set(env->context(),
225
                      env->error_string(),
226
5
                      OneByteString(env->isolate(), msg)).Check();
227
1
    ClearError();
228
  }
229
230
12214
  return StreamWriteResult { async, err, req_wrap, total_bytes };
231
}
232
233
template <typename OtherBase>
234
43368
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
43368
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
241
43368
}
242
243
23505
inline ShutdownWrap* StreamBase::CreateShutdownWrap(
244
    v8::Local<v8::Object> object) {
245
23505
  return new SimpleShutdownWrap<AsyncWrap>(this, object);
246
}
247
248
template <typename OtherBase>
249
6108
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
6108
              AsyncWrap::PROVIDER_WRITEWRAP) {
256
6108
}
257
258
5718
inline WriteWrap* StreamBase::CreateWriteWrap(
259
    v8::Local<v8::Object> object) {
260
5718
  return new SimpleWriteWrap<AsyncWrap>(this, object);
261
}
262
263
79305
inline void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
264
79305
  obj->SetAlignedPointerInInternalField(kStreamBaseField, this);
265
79305
}
266
267
1081060
inline StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
268
2162120
  if (obj->GetAlignedPointerFromInternalField(0) == nullptr)
269
1
    return nullptr;
270
271
  return static_cast<StreamBase*>(
272
2162118
      obj->GetAlignedPointerFromInternalField(kStreamBaseField));
273
}
274
275
276
19870
inline void ShutdownWrap::OnDone(int status) {
277
19870
  stream()->EmitAfterShutdown(this, status);
278
19870
  Dispose();
279
19870
}
280
281
3448
inline void WriteWrap::SetAllocatedStorage(AllocatedBuffer&& storage) {
282
3448
  CHECK_NULL(storage_.data());
283
3448
  storage_ = std::move(storage);
284
3448
}
285
286
6086
inline void WriteWrap::OnDone(int status) {
287
6086
  stream()->EmitAfterWrite(this, status);
288
6084
  Dispose();
289
6084
}
290
291
25956
inline void StreamReq::Done(int status, const char* error_str) {
292
25956
  AsyncWrap* async_wrap = GetAsyncWrap();
293
25956
  Environment* env = async_wrap->env();
294
25956
  if (error_str != nullptr) {
295
5
    async_wrap->object()->Set(env->context(),
296
                              env->error_string(),
297
30
                              OneByteString(env->isolate(), error_str))
298
10
                              .Check();
299
  }
300
301
25956
  OnDone(status);
302
25954
}
303
304
888973
inline void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
305
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
306
307
888973
  obj->SetAlignedPointerInInternalField(0, nullptr);  // BaseObject field.
308
888973
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
309
888973
}
310
311
312
}  // namespace node
313
314
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
315
316
#endif  // SRC_STREAM_BASE_INL_H_