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

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

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