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-20 19:51:53 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
14917
StreamReq::StreamReq(
16
    StreamBase* stream,
17
14917
    v8::Local<v8::Object> req_wrap_obj) : stream_(stream) {
18
14917
  AttachToObject(req_wrap_obj);
19
14917
}
20
21
14917
void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
22
29834
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(
23
               StreamReq::kStreamReqField),
24
           nullptr);
25
14917
  req_wrap_obj->SetAlignedPointerInInternalField(
26
14917
      StreamReq::kStreamReqField, this);
27
14917
}
28
29
7582
StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
30
  return static_cast<StreamReq*>(
31
7582
      req_wrap_obj->GetAlignedPointerFromInternalField(
32
7582
          StreamReq::kStreamReqField));
33
}
34
35
14905
void StreamReq::Dispose() {
36
29810
  BaseObjectPtr<AsyncWrap> destroy_me{GetAsyncWrap()};
37
44715
  object()->SetAlignedPointerInInternalField(
38
14905
      StreamReq::kStreamReqField, nullptr);
39
14905
  destroy_me->Detach();
40
14905
}
41
42
15523
v8::Local<v8::Object> StreamReq::object() {
43
15523
  return GetAsyncWrap()->object();
44
}
45
46
7256
ShutdownWrap::ShutdownWrap(
47
    StreamBase* stream,
48
7256
    v8::Local<v8::Object> req_wrap_obj)
49
7256
    : StreamReq(stream, req_wrap_obj) { }
50
51
7661
WriteWrap::WriteWrap(
52
    StreamBase* stream,
53
7661
    v8::Local<v8::Object> req_wrap_obj)
54
7661
    : 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
104031
void StreamResource::RemoveStreamListener(StreamListener* listener) {
72
104031
  CHECK_NOT_NULL(listener);
73
74
  StreamListener* previous;
75
  StreamListener* current;
76
77
  // Remove from the linked list.
78
104031
  for (current = listener_, previous = nullptr;
79
       /* No loop condition because we want a crash if listener is not found */
80
602
       ; previous = current, current = current->previous_listener_) {
81
104633
    CHECK_NOT_NULL(current);
82
104633
    if (current == listener) {
83
104031
      if (previous != nullptr)
84
602
        previous->previous_listener_ = current->previous_listener_;
85
      else
86
103429
        listener_ = listener->previous_listener_;
87
104031
      break;
88
    }
89
602
  }
90
91
104031
  listener->stream_ = nullptr;
92
104031
  listener->previous_listener_ = nullptr;
93
104031
}
94
95
337231
uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
96
337231
  DebugSealHandleScope seal_handle_scope;
97
337231
  return listener_->OnStreamAlloc(suggested_size);
98
}
99
100
351482
void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) {
101
351482
  DebugSealHandleScope seal_handle_scope;
102
351482
  if (nread > 0)
103
331516
    bytes_read_ += static_cast<uint64_t>(nread);
104
351482
  listener_->OnStreamRead(nread, buf);
105
351313
}
106
107
7645
void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
108
7645
  DebugSealHandleScope seal_handle_scope;
109
7645
  listener_->OnStreamAfterWrite(w, status);
110
7644
}
111
112
7251
void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
113
7251
  DebugSealHandleScope seal_handle_scope;
114
7251
  listener_->OnStreamAfterShutdown(w, status);
115
7251
}
116
117
2688
void StreamResource::EmitWantsWrite(size_t suggested_size) {
118
2688
  DebugSealHandleScope seal_handle_scope;
119
2688
  listener_->OnStreamWantsWrite(suggested_size);
120
2688
}
121
122
64603
StreamBase::StreamBase(Environment* env) : env_(env) {
123
64603
  PushStreamListener(&default_listener_);
124
64603
}
125
126
31344
int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
127
31344
  Environment* env = stream_env();
128
129
62688
  v8::HandleScope handle_scope(env->isolate());
130
131
31344
  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
62688
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
141
31344
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
142
31344
  int err = DoShutdown(req_wrap);
143
144

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

100087
    if (err != 0 || count == 0) {
175
92526
      return StreamWriteResult { false, err, nullptr, total_bytes };
176
    }
177
  }
178
179
15321
  v8::HandleScope handle_scope(env->isolate());
180
181
7661
  if (req_wrap_obj.IsEmpty()) {
182
6981
    if (!env->write_wrap_template()
183
6981
             ->NewInstance(env->context())
184
2327
             .ToLocal(&req_wrap_obj)) {
185
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0 };
186
    }
187
2327
    StreamReq::ResetObject(req_wrap_obj);
188
  }
189
190
15321
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
191
7661
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
192
193
7661
  err = DoWrite(req_wrap, bufs, count, send_handle);
194
7660
  bool async = err == 0;
195
196
7660
  if (!async) {
197
8
    req_wrap->Dispose();
198
8
    req_wrap = nullptr;
199
  }
200
201
7660
  const char* msg = Error();
202
7660
  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
7660
  return StreamWriteResult { async, err, req_wrap, total_bytes };
210
}
211
212
template <typename OtherBase>
213
7256
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
7256
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
220
7256
}
221
222
template <typename OtherBase>
223
7661
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
7661
              AsyncWrap::PROVIDER_WRITEWRAP) {
230
7661
}
231
232
64603
void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
233
64603
  obj->SetAlignedPointerInInternalField(
234
64603
      StreamBase::kStreamBaseField, this);
235
64603
}
236
237
538198
StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
238
1076396
  if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
239
1
    return nullptr;
240
241
  return static_cast<StreamBase*>(
242
538197
      obj->GetAlignedPointerFromInternalField(
243
538197
          StreamBase::kStreamBaseField));
244
}
245
246
6973
WriteWrap* WriteWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
247
6973
  return static_cast<WriteWrap*>(StreamReq::FromObject(req_wrap_obj));
248
}
249
250
template <typename T, bool kIsWeak>
251
6973
WriteWrap* WriteWrap::FromObject(
252
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
253
6973
  if (!base_obj) return nullptr;
254
6973
  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
3440
void WriteWrap::SetAllocatedStorage(AllocatedBuffer&& storage) {
269
3440
  CHECK_NULL(storage_.data());
270
3440
  storage_ = std::move(storage);
271
3440
}
272
273
14896
void StreamReq::Done(int status, const char* error_str) {
274
14896
  AsyncWrap* async_wrap = GetAsyncWrap();
275
14896
  Environment* env = async_wrap->env();
276
14896
  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
14896
  OnDone(status);
284
14895
}
285
286
367631
void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
287
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
288
289
367631
  obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
290
367631
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
291
367631
}
292
293
}  // namespace node
294
295
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
296
297
#endif  // SRC_STREAM_BASE_INL_H_