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: 156 161 96.9 %
Date: 2020-11-21 04:10:54 Branches: 41 52 78.8 %

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
14738
StreamReq::StreamReq(
16
    StreamBase* stream,
17
14738
    v8::Local<v8::Object> req_wrap_obj) : stream_(stream) {
18
14738
  AttachToObject(req_wrap_obj);
19
14738
}
20
21
14738
void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
22
29476
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(
23
               StreamReq::kStreamReqField),
24
           nullptr);
25
14738
  req_wrap_obj->SetAlignedPointerInInternalField(
26
14738
      StreamReq::kStreamReqField, this);
27
14738
}
28
29
7399
StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
30
  return static_cast<StreamReq*>(
31
7399
      req_wrap_obj->GetAlignedPointerFromInternalField(
32
7399
          StreamReq::kStreamReqField));
33
}
34
35
14726
void StreamReq::Dispose() {
36
29452
  BaseObjectPtr<AsyncWrap> destroy_me{GetAsyncWrap()};
37
44178
  object()->SetAlignedPointerInInternalField(
38
14726
      StreamReq::kStreamReqField, nullptr);
39
14726
  destroy_me->Detach();
40
14726
}
41
42
15344
v8::Local<v8::Object> StreamReq::object() {
43
15344
  return GetAsyncWrap()->object();
44
}
45
46
7257
ShutdownWrap::ShutdownWrap(
47
    StreamBase* stream,
48
7257
    v8::Local<v8::Object> req_wrap_obj)
49
7257
    : StreamReq(stream, req_wrap_obj) { }
50
51
7481
WriteWrap::WriteWrap(
52
    StreamBase* stream,
53
7481
    v8::Local<v8::Object> req_wrap_obj)
54
7481
    : StreamReq(stream, req_wrap_obj) { }
55
56
12956
void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) {
57
12956
  CHECK_NOT_NULL(previous_listener_);
58
12956
  previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
59
12956
}
60
61
104500
void StreamResource::PushStreamListener(StreamListener* listener) {
62
104500
  CHECK_NOT_NULL(listener);
63
104500
  CHECK_NULL(listener->stream_);
64
65
104500
  listener->previous_listener_ = listener_;
66
104500
  listener->stream_ = this;
67
68
104500
  listener_ = listener;
69
104500
}
70
71
104118
void StreamResource::RemoveStreamListener(StreamListener* listener) {
72
104118
  CHECK_NOT_NULL(listener);
73
74
  StreamListener* previous;
75
  StreamListener* current;
76
77
  // Remove from the linked list.
78
104118
  for (current = listener_, previous = nullptr;
79
       /* No loop condition because we want a crash if listener is not found */
80
608
       ; previous = current, current = current->previous_listener_) {
81
104726
    CHECK_NOT_NULL(current);
82
104726
    if (current == listener) {
83
104118
      if (previous != nullptr)
84
608
        previous->previous_listener_ = current->previous_listener_;
85
      else
86
103510
        listener_ = listener->previous_listener_;
87
104118
      break;
88
    }
89
608
  }
90
91
104118
  listener->stream_ = nullptr;
92
104118
  listener->previous_listener_ = nullptr;
93
104118
}
94
95
338341
uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
96
338341
  DebugSealHandleScope seal_handle_scope;
97
338341
  return listener_->OnStreamAlloc(suggested_size);
98
}
99
100
352610
void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) {
101
352610
  DebugSealHandleScope seal_handle_scope;
102
352610
  if (nread > 0)
103
332641
    bytes_read_ += static_cast<uint64_t>(nread);
104
352610
  listener_->OnStreamRead(nread, buf);
105
352529
}
106
107
7465
void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
108
7465
  DebugSealHandleScope seal_handle_scope;
109
7465
  listener_->OnStreamAfterWrite(w, status);
110
7464
}
111
112
7252
void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
113
7252
  DebugSealHandleScope seal_handle_scope;
114
7252
  listener_->OnStreamAfterShutdown(w, status);
115
7252
}
116
117
2569
void StreamResource::EmitWantsWrite(size_t suggested_size) {
118
2569
  DebugSealHandleScope seal_handle_scope;
119
2569
  listener_->OnStreamWantsWrite(suggested_size);
120
2569
}
121
122
64603
StreamBase::StreamBase(Environment* env) : env_(env) {
123
64603
  PushStreamListener(&default_listener_);
124
64603
}
125
126
31345
int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
127
31345
  Environment* env = stream_env();
128
129
62690
  v8::HandleScope handle_scope(env->isolate());
130
131
31345
  if (req_wrap_obj.IsEmpty()) {
132
1851
    if (!env->shutdown_wrap_template()
133
1851
             ->NewInstance(env->context())
134
617
             .ToLocal(&req_wrap_obj)) {
135
      return UV_EBUSY;
136
    }
137
617
    StreamReq::ResetObject(req_wrap_obj);
138
  }
139
140
62690
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
141
31345
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
142
31345
  int err = DoShutdown(req_wrap);
143
144

31345
  if (err != 0 && req_wrap != nullptr) {
145
2
    req_wrap->Dispose();
146
  }
147
148
31345
  const char* msg = Error();
149
31345
  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
31345
  return err;
157
}
158
159
100050
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
100050
  Environment* env = stream_env();
165
  int err;
166
167
100050
  size_t total_bytes = 0;
168
346094
  for (size_t i = 0; i < count; ++i)
169
246044
    total_bytes += bufs[i].len;
170
100050
  bytes_written_ += total_bytes;
171
172
100050
  if (send_handle == nullptr) {
173
99951
    err = DoTryWrite(&bufs, &count);
174

99951
    if (err != 0 || count == 0) {
175
92569
      return StreamWriteResult { false, err, nullptr, total_bytes };
176
    }
177
  }
178
179
14961
  v8::HandleScope handle_scope(env->isolate());
180
181
7481
  if (req_wrap_obj.IsEmpty()) {
182
6624
    if (!env->write_wrap_template()
183
6624
             ->NewInstance(env->context())
184
2208
             .ToLocal(&req_wrap_obj)) {
185
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0 };
186
    }
187
2208
    StreamReq::ResetObject(req_wrap_obj);
188
  }
189
190
14961
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
191
7481
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
192
193
7481
  err = DoWrite(req_wrap, bufs, count, send_handle);
194
7480
  bool async = err == 0;
195
196
7480
  if (!async) {
197
8
    req_wrap->Dispose();
198
8
    req_wrap = nullptr;
199
  }
200
201
7480
  const char* msg = Error();
202
7480
  if (msg != nullptr) {
203
10
    req_wrap_obj->Set(env->context(),
204
                      env->error_string(),
205
25
                      OneByteString(env->isolate(), msg)).Check();
206
5
    ClearError();
207
  }
208
209
7480
  return StreamWriteResult { async, err, req_wrap, total_bytes };
210
}
211
212
template <typename OtherBase>
213
7257
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
7257
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
220
7257
}
221
222
template <typename OtherBase>
223
7481
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
7481
              AsyncWrap::PROVIDER_WRITEWRAP) {
230
7481
}
231
232
64603
void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
233
64603
  obj->SetAlignedPointerInInternalField(
234
64603
      StreamBase::kStreamBaseField, this);
235
64603
}
236
237
538066
StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
238
1076132
  if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
239
1
    return nullptr;
240
241
  return static_cast<StreamBase*>(
242
538065
      obj->GetAlignedPointerFromInternalField(
243
538065
          StreamBase::kStreamBaseField));
244
}
245
246
6790
WriteWrap* WriteWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
247
6790
  return static_cast<WriteWrap*>(StreamReq::FromObject(req_wrap_obj));
248
}
249
250
template <typename T, bool kIsWeak>
251
6790
WriteWrap* WriteWrap::FromObject(
252
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
253
6790
  if (!base_obj) return nullptr;
254
6790
  return FromObject(base_obj->object());
255
}
256
257
ShutdownWrap* ShutdownWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
258
  return static_cast<ShutdownWrap*>(StreamReq::FromObject(req_wrap_obj));
259
}
260
261
template <typename T, bool kIsWeak>
262
ShutdownWrap* ShutdownWrap::FromObject(
263
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
264
  if (!base_obj) return nullptr;
265
  return FromObject(base_obj->object());
266
}
267
268
3439
void WriteWrap::SetAllocatedStorage(AllocatedBuffer&& storage) {
269
3439
  CHECK_NULL(storage_.data());
270
3439
  storage_ = std::move(storage);
271
3439
}
272
273
14717
void StreamReq::Done(int status, const char* error_str) {
274
14717
  AsyncWrap* async_wrap = GetAsyncWrap();
275
14717
  Environment* env = async_wrap->env();
276
14717
  if (error_str != nullptr) {
277
9
    async_wrap->object()->Set(env->context(),
278
                              env->error_string(),
279
15
                              OneByteString(env->isolate(), error_str))
280
                              .Check();
281
  }
282
283
14717
  OnDone(status);
284
14716
}
285
286
367440
void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
287
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
288
289
367440
  obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
290
367440
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
291
367440
}
292
293
}  // namespace node
294
295
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
296
297
#endif  // SRC_STREAM_BASE_INL_H_