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: 160 165 97.0 %
Date: 2021-04-22 04:13:51 Branches: 43 54 79.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 "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
15093
StreamReq::StreamReq(
16
    StreamBase* stream,
17
15093
    v8::Local<v8::Object> req_wrap_obj) : stream_(stream) {
18
15093
  AttachToObject(req_wrap_obj);
19
15093
}
20
21
15093
void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
22
30186
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(
23
               StreamReq::kStreamReqField),
24
           nullptr);
25
15093
  req_wrap_obj->SetAlignedPointerInInternalField(
26
15093
      StreamReq::kStreamReqField, this);
27
15093
}
28
29
7715
StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
30
  return static_cast<StreamReq*>(
31
7715
      req_wrap_obj->GetAlignedPointerFromInternalField(
32
7715
          StreamReq::kStreamReqField));
33
}
34
35
15081
void StreamReq::Dispose() {
36
30162
  BaseObjectPtr<AsyncWrap> destroy_me{GetAsyncWrap()};
37
45243
  object()->SetAlignedPointerInInternalField(
38
15081
      StreamReq::kStreamReqField, nullptr);
39
15081
  destroy_me->Detach();
40
15081
}
41
42
15699
v8::Local<v8::Object> StreamReq::object() {
43
15699
  return GetAsyncWrap()->object();
44
}
45
46
7299
ShutdownWrap::ShutdownWrap(
47
    StreamBase* stream,
48
7299
    v8::Local<v8::Object> req_wrap_obj)
49
7299
    : StreamReq(stream, req_wrap_obj) { }
50
51
7794
WriteWrap::WriteWrap(
52
    StreamBase* stream,
53
7794
    v8::Local<v8::Object> req_wrap_obj)
54
7794
    : StreamReq(stream, req_wrap_obj) { }
55
56
12964
void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) {
57
12964
  CHECK_NOT_NULL(previous_listener_);
58
12964
  previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
59
12964
}
60
61
104904
void StreamResource::PushStreamListener(StreamListener* listener) {
62
104904
  CHECK_NOT_NULL(listener);
63
104904
  CHECK_NULL(listener->stream_);
64
65
104904
  listener->previous_listener_ = listener_;
66
104904
  listener->stream_ = this;
67
68
104904
  listener_ = listener;
69
104904
}
70
71
104506
void StreamResource::RemoveStreamListener(StreamListener* listener) {
72
104506
  CHECK_NOT_NULL(listener);
73
74
  StreamListener* previous;
75
  StreamListener* current;
76
77
  // Remove from the linked list.
78
104506
  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
105114
    CHECK_NOT_NULL(current);
82
105114
    if (current == listener) {
83
104506
      if (previous != nullptr)
84
608
        previous->previous_listener_ = current->previous_listener_;
85
      else
86
103898
        listener_ = listener->previous_listener_;
87
104506
      break;
88
    }
89
608
  }
90
91
104506
  listener->stream_ = nullptr;
92
104506
  listener->previous_listener_ = nullptr;
93
104506
}
94
95
338826
uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
96
338826
  DebugSealHandleScope seal_handle_scope;
97
338826
  return listener_->OnStreamAlloc(suggested_size);
98
}
99
100
353108
void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) {
101
353108
  DebugSealHandleScope seal_handle_scope;
102
353108
  if (nread > 0)
103
333027
    bytes_read_ += static_cast<uint64_t>(nread);
104
353108
  listener_->OnStreamRead(nread, buf);
105
353022
}
106
107
7778
void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
108
7778
  DebugSealHandleScope seal_handle_scope;
109
7778
  listener_->OnStreamAfterWrite(w, status);
110
7777
}
111
112
7294
void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
113
7294
  DebugSealHandleScope seal_handle_scope;
114
7294
  listener_->OnStreamAfterShutdown(w, status);
115
7294
}
116
117
2741
void StreamResource::EmitWantsWrite(size_t suggested_size) {
118
2741
  DebugSealHandleScope seal_handle_scope;
119
2741
  listener_->OnStreamWantsWrite(suggested_size);
120
2741
}
121
122
64954
StreamBase::StreamBase(Environment* env) : env_(env) {
123
64954
  PushStreamListener(&default_listener_);
124
64954
}
125
126
31374
int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
127
31374
  Environment* env = stream_env();
128
129
62748
  v8::HandleScope handle_scope(env->isolate());
130
131
31374
  if (req_wrap_obj.IsEmpty()) {
132
1839
    if (!env->shutdown_wrap_template()
133
1839
             ->NewInstance(env->context())
134
613
             .ToLocal(&req_wrap_obj)) {
135
      return UV_EBUSY;
136
    }
137
613
    StreamReq::ResetObject(req_wrap_obj);
138
  }
139
140
62748
  BaseObjectPtr<AsyncWrap> req_wrap_ptr;
141
62748
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
142
31374
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
143
31374
  if (req_wrap != nullptr)
144
7299
    req_wrap_ptr.reset(req_wrap->GetAsyncWrap());
145
31374
  int err = DoShutdown(req_wrap);
146
147

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

100679
    if (err != 0 || count == 0) {
178
92991
      return StreamWriteResult { false, err, nullptr, total_bytes, {} };
179
    }
180
  }
181
182
15587
  v8::HandleScope handle_scope(env->isolate());
183
184
7794
  if (req_wrap_obj.IsEmpty()) {
185
7020
    if (!env->write_wrap_template()
186
7020
             ->NewInstance(env->context())
187
2340
             .ToLocal(&req_wrap_obj)) {
188
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0, {} };
189
    }
190
2340
    StreamReq::ResetObject(req_wrap_obj);
191
  }
192
193
15587
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
194
7794
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
195
15587
  BaseObjectPtr<AsyncWrap> req_wrap_ptr(req_wrap->GetAsyncWrap());
196
197
7794
  err = DoWrite(req_wrap, bufs, count, send_handle);
198
7793
  bool async = err == 0;
199
200
7793
  if (!async) {
201
8
    req_wrap->Dispose();
202
8
    req_wrap = nullptr;
203
  }
204
205
7793
  const char* msg = Error();
206
7793
  if (msg != nullptr) {
207
12
    req_wrap_obj->Set(env->context(),
208
                      env->error_string(),
209
30
                      OneByteString(env->isolate(), msg)).Check();
210
6
    ClearError();
211
  }
212
213
  return StreamWriteResult {
214
7793
      async, err, req_wrap, total_bytes, std::move(req_wrap_ptr) };
215
}
216
217
template <typename OtherBase>
218
7299
SimpleShutdownWrap<OtherBase>::SimpleShutdownWrap(
219
    StreamBase* stream,
220
    v8::Local<v8::Object> req_wrap_obj)
221
  : ShutdownWrap(stream, req_wrap_obj),
222
    OtherBase(stream->stream_env(),
223
              req_wrap_obj,
224
7299
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
225
7299
}
226
227
template <typename OtherBase>
228
7794
SimpleWriteWrap<OtherBase>::SimpleWriteWrap(
229
    StreamBase* stream,
230
    v8::Local<v8::Object> req_wrap_obj)
231
  : WriteWrap(stream, req_wrap_obj),
232
    OtherBase(stream->stream_env(),
233
              req_wrap_obj,
234
7794
              AsyncWrap::PROVIDER_WRITEWRAP) {
235
7794
}
236
237
64954
void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
238
64954
  obj->SetAlignedPointerInInternalField(
239
64954
      StreamBase::kStreamBaseField, this);
240
64954
}
241
242
540114
StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
243
1080228
  if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
244
1
    return nullptr;
245
246
  return static_cast<StreamBase*>(
247
540113
      obj->GetAlignedPointerFromInternalField(
248
540113
          StreamBase::kStreamBaseField));
249
}
250
251
7106
WriteWrap* WriteWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
252
7106
  return static_cast<WriteWrap*>(StreamReq::FromObject(req_wrap_obj));
253
}
254
255
template <typename T, bool kIsWeak>
256
7106
WriteWrap* WriteWrap::FromObject(
257
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
258
7106
  if (!base_obj) return nullptr;
259
7106
  return FromObject(base_obj->object());
260
}
261
262
ShutdownWrap* ShutdownWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
263
  return static_cast<ShutdownWrap*>(StreamReq::FromObject(req_wrap_obj));
264
}
265
266
template <typename T, bool kIsWeak>
267
ShutdownWrap* ShutdownWrap::FromObject(
268
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
269
  if (!base_obj) return nullptr;
270
  return FromObject(base_obj->object());
271
}
272
273
3481
void WriteWrap::SetAllocatedStorage(AllocatedBuffer&& storage) {
274
3481
  CHECK_NULL(storage_.data());
275
3481
  storage_ = std::move(storage);
276
3481
}
277
278
15072
void StreamReq::Done(int status, const char* error_str) {
279
15072
  AsyncWrap* async_wrap = GetAsyncWrap();
280
15072
  Environment* env = async_wrap->env();
281
15072
  if (error_str != nullptr) {
282
12
    async_wrap->object()->Set(env->context(),
283
                              env->error_string(),
284
20
                              OneByteString(env->isolate(), error_str))
285
                              .Check();
286
  }
287
288
15072
  OnDone(status);
289
15071
}
290
291
368657
void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
292
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
293
294
368657
  obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
295
368657
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
296
368657
}
297
298
}  // namespace node
299
300
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
301
302
#endif  // SRC_STREAM_BASE_INL_H_