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

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

100836
    if (err != 0 || count == 0) {
179
93334
      return StreamWriteResult { false, err, nullptr, total_bytes, {} };
180
    }
181
  }
182
183
15215
  v8::HandleScope handle_scope(env->isolate());
184
185
7608
  if (req_wrap_obj.IsEmpty()) {
186
2274
    if (!env->write_wrap_template()
187
2274
             ->NewInstance(env->context())
188
2274
             .ToLocal(&req_wrap_obj)) {
189
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0, {} };
190
    }
191
2274
    StreamReq::ResetObject(req_wrap_obj);
192
  }
193
194
15215
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
195
7608
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
196
15215
  BaseObjectPtr<AsyncWrap> req_wrap_ptr(req_wrap->GetAsyncWrap());
197
198
7608
  err = DoWrite(req_wrap, bufs, count, send_handle);
199
7607
  bool async = err == 0;
200
201
7607
  if (!async) {
202
9
    req_wrap->Dispose();
203
9
    req_wrap = nullptr;
204
  }
205
206
7607
  const char* msg = Error();
207
7607
  if (msg != nullptr) {
208
8
    if (req_wrap_obj->Set(env->context(),
209
                          env->error_string(),
210
16
                          OneByteString(env->isolate(), msg)).IsNothing()) {
211
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0, {} };
212
    }
213
4
    ClearError();
214
  }
215
216
  return StreamWriteResult {
217
7607
      async, err, req_wrap, total_bytes, std::move(req_wrap_ptr) };
218
}
219
220
template <typename OtherBase>
221
9604
SimpleShutdownWrap<OtherBase>::SimpleShutdownWrap(
222
    StreamBase* stream,
223
    v8::Local<v8::Object> req_wrap_obj)
224
  : ShutdownWrap(stream, req_wrap_obj),
225
    OtherBase(stream->stream_env(),
226
              req_wrap_obj,
227
9604
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
228
9604
}
229
230
template <typename OtherBase>
231
7608
SimpleWriteWrap<OtherBase>::SimpleWriteWrap(
232
    StreamBase* stream,
233
    v8::Local<v8::Object> req_wrap_obj)
234
  : WriteWrap(stream, req_wrap_obj),
235
    OtherBase(stream->stream_env(),
236
              req_wrap_obj,
237
7608
              AsyncWrap::PROVIDER_WRITEWRAP) {
238
7608
}
239
240
66196
void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
241
66196
  obj->SetAlignedPointerInInternalField(
242
      StreamBase::kStreamBaseField, this);
243
66196
}
244
245
296312
StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
246
592624
  if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
247
1
    return nullptr;
248
249
  return static_cast<StreamBase*>(
250
296311
      obj->GetAlignedPointerFromInternalField(
251
296311
          StreamBase::kStreamBaseField));
252
}
253
254
6848
WriteWrap* WriteWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
255
6848
  return static_cast<WriteWrap*>(StreamReq::FromObject(req_wrap_obj));
256
}
257
258
template <typename T, bool kIsWeak>
259
6848
WriteWrap* WriteWrap::FromObject(
260
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
261
6848
  if (!base_obj) return nullptr;
262
6848
  return FromObject(base_obj->object());
263
}
264
265
ShutdownWrap* ShutdownWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
266
  return static_cast<ShutdownWrap*>(StreamReq::FromObject(req_wrap_obj));
267
}
268
269
template <typename T, bool kIsWeak>
270
ShutdownWrap* ShutdownWrap::FromObject(
271
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
272
  if (!base_obj) return nullptr;
273
  return FromObject(base_obj->object());
274
}
275
276
3426
void WriteWrap::SetBackingStore(std::unique_ptr<v8::BackingStore> bs) {
277
3426
  CHECK(!backing_store_);
278
3426
  backing_store_ = std::move(bs);
279
3426
}
280
281
17187
void StreamReq::Done(int status, const char* error_str) {
282
17187
  AsyncWrap* async_wrap = GetAsyncWrap();
283
17187
  Environment* env = async_wrap->env();
284
17187
  if (error_str != nullptr) {
285
4
    v8::HandleScope handle_scope(env->isolate());
286
4
    if (async_wrap->object()->Set(
287
            env->context(),
288
            env->error_string(),
289
16
            OneByteString(env->isolate(), error_str)).IsNothing()) {
290
      return;
291
    }
292
  }
293
294
17187
  OnDone(status);
295
}
296
297
121971
void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
298
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
299
300
121971
  obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
301
121971
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
302
121971
}
303
304
}  // namespace node
305
306
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
307
308
#endif  // SRC_STREAM_BASE_INL_H_