GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: stream_base-inl.h Lines: 158 163 96.9 %
Date: 2021-10-05 04:12:38 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
17016
StreamReq::StreamReq(
16
    StreamBase* stream,
17
17016
    v8::Local<v8::Object> req_wrap_obj) : stream_(stream) {
18
17016
  AttachToObject(req_wrap_obj);
19
17016
}
20
21
17016
void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
22
34032
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(
23
               StreamReq::kStreamReqField),
24
           nullptr);
25
17016
  req_wrap_obj->SetAlignedPointerInInternalField(
26
      StreamReq::kStreamReqField, this);
27
17016
}
28
29
7258
StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
30
  return static_cast<StreamReq*>(
31
7258
      req_wrap_obj->GetAlignedPointerFromInternalField(
32
7258
          StreamReq::kStreamReqField));
33
}
34
35
17004
void StreamReq::Dispose() {
36
34008
  BaseObjectPtr<AsyncWrap> destroy_me{GetAsyncWrap()};
37
34008
  object()->SetAlignedPointerInInternalField(
38
      StreamReq::kStreamReqField, nullptr);
39
17004
  destroy_me->Detach();
40
17004
}
41
42
17624
v8::Local<v8::Object> StreamReq::object() {
43
17624
  return GetAsyncWrap()->object();
44
}
45
46
9594
ShutdownWrap::ShutdownWrap(
47
    StreamBase* stream,
48
9594
    v8::Local<v8::Object> req_wrap_obj)
49
9594
    : StreamReq(stream, req_wrap_obj) { }
50
51
7422
WriteWrap::WriteWrap(
52
    StreamBase* stream,
53
7422
    v8::Local<v8::Object> req_wrap_obj)
54
7422
    : StreamReq(stream, req_wrap_obj) { }
55
56
12973
void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) {
57
12973
  CHECK_NOT_NULL(previous_listener_);
58
12973
  previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
59
12973
}
60
61
106002
void StreamResource::PushStreamListener(StreamListener* listener) {
62
106002
  CHECK_NOT_NULL(listener);
63
106002
  CHECK_NULL(listener->stream_);
64
65
106002
  listener->previous_listener_ = listener_;
66
106002
  listener->stream_ = this;
67
68
106002
  listener_ = listener;
69
106002
}
70
71
105463
void StreamResource::RemoveStreamListener(StreamListener* listener) {
72
105463
  CHECK_NOT_NULL(listener);
73
74
  StreamListener* previous;
75
  StreamListener* current;
76
77
  // Remove from the linked list.
78
105463
  for (current = listener_, previous = nullptr;
79
       /* No loop condition because we want a crash if listener is not found */
80
616
       ; previous = current, current = current->previous_listener_) {
81
106079
    CHECK_NOT_NULL(current);
82
106079
    if (current == listener) {
83
105463
      if (previous != nullptr)
84
616
        previous->previous_listener_ = current->previous_listener_;
85
      else
86
104847
        listener_ = listener->previous_listener_;
87
105463
      break;
88
    }
89
616
  }
90
91
105463
  listener->stream_ = nullptr;
92
105463
  listener->previous_listener_ = nullptr;
93
105463
}
94
95
91959
uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
96
91959
  DebugSealHandleScope seal_handle_scope;
97
91959
  return listener_->OnStreamAlloc(suggested_size);
98
}
99
100
106202
void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) {
101
106202
  DebugSealHandleScope seal_handle_scope;
102
106202
  if (nread > 0)
103
85796
    bytes_read_ += static_cast<uint64_t>(nread);
104
106202
  listener_->OnStreamRead(nread, buf);
105
106087
}
106
107
7404
void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
108
7404
  DebugSealHandleScope seal_handle_scope;
109
7404
  listener_->OnStreamAfterWrite(w, status);
110
7403
}
111
112
9587
void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
113
9587
  DebugSealHandleScope seal_handle_scope;
114
9587
  listener_->OnStreamAfterShutdown(w, status);
115
9587
}
116
117
2556
void StreamResource::EmitWantsWrite(size_t suggested_size) {
118
2556
  DebugSealHandleScope seal_handle_scope;
119
2556
  listener_->OnStreamWantsWrite(suggested_size);
120
2556
}
121
122
66053
StreamBase::StreamBase(Environment* env) : env_(env) {
123
66053
  PushStreamListener(&default_listener_);
124
66053
}
125
126
33630
int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
127
33630
  Environment* env = stream_env();
128
129
67260
  v8::HandleScope handle_scope(env->isolate());
130
131
33630
  if (req_wrap_obj.IsEmpty()) {
132
600
    if (!env->shutdown_wrap_template()
133
600
             ->NewInstance(env->context())
134
600
             .ToLocal(&req_wrap_obj)) {
135
      return UV_EBUSY;
136
    }
137
600
    StreamReq::ResetObject(req_wrap_obj);
138
  }
139
140
67260
  BaseObjectPtr<AsyncWrap> req_wrap_ptr;
141
33630
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
142
33630
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
143
33630
  if (req_wrap != nullptr)
144
9594
    req_wrap_ptr.reset(req_wrap->GetAsyncWrap());
145
33630
  int err = DoShutdown(req_wrap);
146
147

33630
  if (err != 0 && req_wrap != nullptr) {
148
5
    req_wrap->Dispose();
149
  }
150
151
33630
  const char* msg = Error();
152
33630
  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
33630
  return err;
160
}
161
162
100347
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
100347
  Environment* env = stream_env();
168
  int err;
169
170
100347
  size_t total_bytes = 0;
171
347090
  for (size_t i = 0; i < count; ++i)
172
246743
    total_bytes += bufs[i].len;
173
100347
  bytes_written_ += total_bytes;
174
175
100347
  if (send_handle == nullptr) {
176
100241
    err = DoTryWrite(&bufs, &count);
177

100241
    if (err != 0 || count == 0) {
178
92925
      return StreamWriteResult { false, err, nullptr, total_bytes, {} };
179
    }
180
  }
181
182
14843
  v8::HandleScope handle_scope(env->isolate());
183
184
7422
  if (req_wrap_obj.IsEmpty()) {
185
2181
    if (!env->write_wrap_template()
186
2181
             ->NewInstance(env->context())
187
2181
             .ToLocal(&req_wrap_obj)) {
188
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0, {} };
189
    }
190
2181
    StreamReq::ResetObject(req_wrap_obj);
191
  }
192
193
14843
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
194
7422
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
195
14843
  BaseObjectPtr<AsyncWrap> req_wrap_ptr(req_wrap->GetAsyncWrap());
196
197
7422
  err = DoWrite(req_wrap, bufs, count, send_handle);
198
7421
  bool async = err == 0;
199
200
7421
  if (!async) {
201
9
    req_wrap->Dispose();
202
9
    req_wrap = nullptr;
203
  }
204
205
7421
  const char* msg = Error();
206
7421
  if (msg != nullptr) {
207
7
    req_wrap_obj->Set(env->context(),
208
                      env->error_string(),
209
21
                      OneByteString(env->isolate(), msg)).Check();
210
7
    ClearError();
211
  }
212
213
  return StreamWriteResult {
214
7421
      async, err, req_wrap, total_bytes, std::move(req_wrap_ptr) };
215
}
216
217
template <typename OtherBase>
218
9594
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
9594
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
225
9594
}
226
227
template <typename OtherBase>
228
7422
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
7422
              AsyncWrap::PROVIDER_WRITEWRAP) {
235
7422
}
236
237
66053
void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
238
66053
  obj->SetAlignedPointerInInternalField(
239
      StreamBase::kStreamBaseField, this);
240
66053
}
241
242
295869
StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
243
591738
  if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
244
1
    return nullptr;
245
246
  return static_cast<StreamBase*>(
247
295868
      obj->GetAlignedPointerFromInternalField(
248
295868
          StreamBase::kStreamBaseField));
249
}
250
251
6649
WriteWrap* WriteWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
252
6649
  return static_cast<WriteWrap*>(StreamReq::FromObject(req_wrap_obj));
253
}
254
255
template <typename T, bool kIsWeak>
256
6649
WriteWrap* WriteWrap::FromObject(
257
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
258
6649
  if (!base_obj) return nullptr;
259
6649
  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
3419
void WriteWrap::SetAllocatedStorage(AllocatedBuffer&& storage) {
274
3419
  CHECK_NULL(storage_.data());
275
3419
  storage_ = std::move(storage);
276
3419
}
277
278
16991
void StreamReq::Done(int status, const char* error_str) {
279
16991
  AsyncWrap* async_wrap = GetAsyncWrap();
280
16991
  Environment* env = async_wrap->env();
281
16991
  if (error_str != nullptr) {
282
4
    v8::HandleScope handle_scope(env->isolate());
283
4
    async_wrap->object()->Set(env->context(),
284
                              env->error_string(),
285
16
                              OneByteString(env->isolate(), error_str))
286
                              .Check();
287
  }
288
289
16991
  OnDone(status);
290
16990
}
291
292
121599
void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
293
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
294
295
121599
  obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
296
121599
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
297
121599
}
298
299
}  // namespace node
300
301
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
302
303
#endif  // SRC_STREAM_BASE_INL_H_