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: 151 156 96.8 %
Date: 2020-07-19 22:14:24 Branches: 40 50 80.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 "allocated_buffer-inl.h"
7
#include "async_wrap-inl.h"
8
#include "base_object-inl.h"
9
#include "node.h"
10
#include "stream_base.h"
11
#include "v8.h"
12
13
namespace node {
14
15
15194
StreamReq::StreamReq(
16
    StreamBase* stream,
17
15194
    v8::Local<v8::Object> req_wrap_obj) : stream_(stream) {
18
15194
  AttachToObject(req_wrap_obj);
19
15194
}
20
21
15194
void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
22
30388
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(
23
               StreamReq::kStreamReqField),
24
           nullptr);
25
15194
  req_wrap_obj->SetAlignedPointerInInternalField(
26
15194
      StreamReq::kStreamReqField, this);
27
15194
}
28
29
616
StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
30
  return static_cast<StreamReq*>(
31
616
      req_wrap_obj->GetAlignedPointerFromInternalField(
32
616
          StreamReq::kStreamReqField));
33
}
34
35
15145
void StreamReq::Dispose() {
36
30290
  BaseObjectPtr<AsyncWrap> destroy_me{GetAsyncWrap()};
37
45435
  object()->SetAlignedPointerInInternalField(
38
15145
      StreamReq::kStreamReqField, nullptr);
39
15145
  destroy_me->Detach();
40
15145
}
41
42
15773
v8::Local<v8::Object> StreamReq::object() {
43
15773
  return GetAsyncWrap()->object();
44
}
45
46
6659
ShutdownWrap::ShutdownWrap(
47
    StreamBase* stream,
48
6659
    v8::Local<v8::Object> req_wrap_obj)
49
6659
    : StreamReq(stream, req_wrap_obj) { }
50
51
8535
WriteWrap::WriteWrap(
52
    StreamBase* stream,
53
8535
    v8::Local<v8::Object> req_wrap_obj)
54
8535
    : StreamReq(stream, req_wrap_obj) { }
55
56
13074
void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) {
57
13074
  CHECK_NOT_NULL(previous_listener_);
58
13074
  previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
59
13070
}
60
61
104891
void StreamResource::PushStreamListener(StreamListener* listener) {
62
104891
  CHECK_NOT_NULL(listener);
63
104891
  CHECK_NULL(listener->stream_);
64
65
104891
  listener->previous_listener_ = listener_;
66
104891
  listener->stream_ = this;
67
68
104891
  listener_ = listener;
69
104891
}
70
71
104296
void StreamResource::RemoveStreamListener(StreamListener* listener) {
72
104296
  CHECK_NOT_NULL(listener);
73
74
  StreamListener* previous;
75
  StreamListener* current;
76
77
  // Remove from the linked list.
78
104296
  for (current = listener_, previous = nullptr;
79
       /* No loop condition because we want a crash if listener is not found */
80
566
       ; previous = current, current = current->previous_listener_) {
81
104862
    CHECK_NOT_NULL(current);
82
104862
    if (current == listener) {
83
104296
      if (previous != nullptr)
84
566
        previous->previous_listener_ = current->previous_listener_;
85
      else
86
103730
        listener_ = listener->previous_listener_;
87
104296
      break;
88
    }
89
566
  }
90
91
104296
  listener->stream_ = nullptr;
92
104296
  listener->previous_listener_ = nullptr;
93
104296
}
94
95
412605
uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
96
412605
  DebugSealHandleScope seal_handle_scope;
97
412605
  return listener_->OnStreamAlloc(suggested_size);
98
}
99
100
426882
void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) {
101
426882
  DebugSealHandleScope seal_handle_scope;
102
426882
  if (nread > 0)
103
406623
    bytes_read_ += static_cast<uint64_t>(nread);
104
426882
  listener_->OnStreamRead(nread, buf);
105
426794
}
106
107
8487
void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
108
8487
  DebugSealHandleScope seal_handle_scope;
109
8487
  listener_->OnStreamAfterWrite(w, status);
110
8486
}
111
112
6650
void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
113
6650
  DebugSealHandleScope seal_handle_scope;
114
6650
  listener_->OnStreamAfterShutdown(w, status);
115
6650
}
116
117
3923
void StreamResource::EmitWantsWrite(size_t suggested_size) {
118
3923
  DebugSealHandleScope seal_handle_scope;
119
3923
  listener_->OnStreamWantsWrite(suggested_size);
120
3923
}
121
122
64940
StreamBase::StreamBase(Environment* env) : env_(env) {
123
64940
  PushStreamListener(&default_listener_);
124
64940
}
125
126
30996
int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
127
30996
  Environment* env = stream_env();
128
129
61992
  v8::HandleScope handle_scope(env->isolate());
130
131
30996
  if (req_wrap_obj.IsEmpty()) {
132
2163
    if (!env->shutdown_wrap_template()
133
2163
             ->NewInstance(env->context())
134
721
             .ToLocal(&req_wrap_obj)) {
135
      return UV_EBUSY;
136
    }
137
721
    StreamReq::ResetObject(req_wrap_obj);
138
  }
139
140
61992
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
141
30996
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
142
30996
  int err = DoShutdown(req_wrap);
143
144

30996
  if (err != 0 && req_wrap != nullptr) {
145
2
    req_wrap->Dispose();
146
  }
147
148
30996
  const char* msg = Error();
149
30996
  if (msg != nullptr) {
150
    req_wrap_obj->Set(
151
        env->context(),
152
        env->error_string(), OneByteString(env->isolate(), msg)).Check();
153
    ClearError();
154
  }
155
156
30996
  return err;
157
}
158
159
140510
StreamWriteResult StreamBase::Write(
160
    uv_buf_t* bufs,
161
    size_t count,
162
    uv_stream_t* send_handle,
163
    v8::Local<v8::Object> req_wrap_obj) {
164
140510
  Environment* env = stream_env();
165
  int err;
166
167
140510
  size_t total_bytes = 0;
168
7877713
  for (size_t i = 0; i < count; ++i)
169
7737203
    total_bytes += bufs[i].len;
170
140510
  bytes_written_ += total_bytes;
171
172
140510
  if (send_handle == nullptr) {
173
140408
    err = DoTryWrite(&bufs, &count);
174

140408
    if (err != 0 || count == 0) {
175
131975
      return StreamWriteResult { false, err, nullptr, total_bytes };
176
    }
177
  }
178
179
17069
  v8::HandleScope handle_scope(env->isolate());
180
181
8535
  if (req_wrap_obj.IsEmpty()) {
182
6687
    if (!env->write_wrap_template()
183
6687
             ->NewInstance(env->context())
184
2229
             .ToLocal(&req_wrap_obj)) {
185
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0 };
186
    }
187
2229
    StreamReq::ResetObject(req_wrap_obj);
188
  }
189
190
17069
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
191
8535
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
192
193
8535
  err = DoWrite(req_wrap, bufs, count, send_handle);
194
8534
  bool async = err == 0;
195
196
8534
  if (!async) {
197
7
    req_wrap->Dispose();
198
7
    req_wrap = nullptr;
199
  }
200
201
8534
  const char* msg = Error();
202
8534
  if (msg != nullptr) {
203
8
    req_wrap_obj->Set(env->context(),
204
                      env->error_string(),
205
20
                      OneByteString(env->isolate(), msg)).Check();
206
4
    ClearError();
207
  }
208
209
8534
  return StreamWriteResult { async, err, req_wrap, total_bytes };
210
}
211
212
template <typename OtherBase>
213
6659
SimpleShutdownWrap<OtherBase>::SimpleShutdownWrap(
214
    StreamBase* stream,
215
    v8::Local<v8::Object> req_wrap_obj)
216
  : ShutdownWrap(stream, req_wrap_obj),
217
    OtherBase(stream->stream_env(),
218
              req_wrap_obj,
219
6659
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
220
6659
}
221
222
template <typename OtherBase>
223
8535
SimpleWriteWrap<OtherBase>::SimpleWriteWrap(
224
    StreamBase* stream,
225
    v8::Local<v8::Object> req_wrap_obj)
226
  : WriteWrap(stream, req_wrap_obj),
227
    OtherBase(stream->stream_env(),
228
              req_wrap_obj,
229
8535
              AsyncWrap::PROVIDER_WRITEWRAP) {
230
8535
}
231
232
64940
void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
233
64940
  obj->SetAlignedPointerInInternalField(
234
64940
      StreamBase::kStreamBaseField, this);
235
64940
}
236
237
675315
StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
238
1350630
  if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
239
1
    return nullptr;
240
241
  return static_cast<StreamBase*>(
242
675314
      obj->GetAlignedPointerFromInternalField(
243
675314
          StreamBase::kStreamBaseField));
244
}
245
246
3620
void WriteWrap::SetAllocatedStorage(AllocatedBuffer&& storage) {
247
3620
  CHECK_NULL(storage_.data());
248
3620
  storage_ = std::move(storage);
249
3620
}
250
251
15137
void StreamReq::Done(int status, const char* error_str) {
252
15137
  AsyncWrap* async_wrap = GetAsyncWrap();
253
15137
  Environment* env = async_wrap->env();
254
15137
  if (error_str != nullptr) {
255
9
    async_wrap->object()->Set(env->context(),
256
                              env->error_string(),
257
15
                              OneByteString(env->isolate(), error_str))
258
                              .Check();
259
  }
260
261
15137
  OnDone(status);
262
15136
}
263
264
523483
void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
265
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
266
267
523483
  obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
268
523483
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
269
523483
}
270
271
}  // namespace node
272
273
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
274
275
#endif  // SRC_STREAM_BASE_INL_H_