GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: stream_base-inl.h Lines: 158 165 95.8 %
Date: 2022-06-23 04:15:39 Branches: 46 60 76.7 %

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

37983
  if (err != 0 && req_wrap != nullptr) {
147
5
    req_wrap->Dispose();
148
  }
149
150
37983
  const char* msg = Error();
151
37983
  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
37983
  return err;
161
}
162
163
103868
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
103868
  Environment* env = stream_env();
169
  int err;
170
171
103868
  size_t total_bytes = 0;
172
361424
  for (size_t i = 0; i < count; ++i)
173
257556
    total_bytes += bufs[i].len;
174
103868
  bytes_written_ += total_bytes;
175
176
103868
  if (send_handle == nullptr) {
177
103762
    err = DoTryWrite(&bufs, &count);
178

103762
    if (err != 0 || count == 0) {
179
94468
      return StreamWriteResult { false, err, nullptr, total_bytes, {} };
180
    }
181
  }
182
183
18799
  v8::HandleScope handle_scope(env->isolate());
184
185
9400
  if (req_wrap_obj.IsEmpty()) {
186
3547
    if (!env->write_wrap_template()
187
3547
             ->NewInstance(env->context())
188
3547
             .ToLocal(&req_wrap_obj)) {
189
3
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0, {} };
190
    }
191
3544
    StreamReq::ResetObject(req_wrap_obj);
192
  }
193
194
18793
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
195
9397
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
196
18793
  BaseObjectPtr<AsyncWrap> req_wrap_ptr(req_wrap->GetAsyncWrap());
197
198
9397
  err = DoWrite(req_wrap, bufs, count, send_handle);
199
9396
  bool async = err == 0;
200
201
9396
  if (!async) {
202
410
    req_wrap->Dispose();
203
410
    req_wrap = nullptr;
204
  }
205
206
9396
  const char* msg = Error();
207
9396
  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
9396
      async, err, req_wrap, total_bytes, std::move(req_wrap_ptr) };
218
}
219
220
template <typename OtherBase>
221
9899
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
9899
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
228
9899
}
229
230
template <typename OtherBase>
231
9397
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
9397
              AsyncWrap::PROVIDER_WRITEWRAP) {
238
9397
}
239
240
70584
void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
241
70584
  obj->SetAlignedPointerInInternalField(
242
      StreamBase::kStreamBaseField, this);
243
70584
}
244
245
282982
StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
246
565964
  if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
247
1
    return nullptr;
248
249
  return static_cast<StreamBase*>(
250
282981
      obj->GetAlignedPointerFromInternalField(
251
282981
          StreamBase::kStreamBaseField));
252
}
253
254
7349
WriteWrap* WriteWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
255
7349
  return static_cast<WriteWrap*>(StreamReq::FromObject(req_wrap_obj));
256
}
257
258
template <typename T, bool kIsWeak>
259
7349
WriteWrap* WriteWrap::FromObject(
260
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
261
7349
  if (!base_obj) return nullptr;
262
7349
  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
4001
void WriteWrap::SetBackingStore(std::unique_ptr<v8::BackingStore> bs) {
277
4001
  CHECK(!backing_store_);
278
4001
  backing_store_ = std::move(bs);
279
4001
}
280
281
18847
void StreamReq::Done(int status, const char* error_str) {
282
18847
  AsyncWrap* async_wrap = GetAsyncWrap();
283
18847
  Environment* env = async_wrap->env();
284
18847
  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
18847
  OnDone(status);
295
}
296
297
130026
void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
298
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
299
300
130026
  obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
301
130026
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
302
130026
}
303
304
}  // namespace node
305
306
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
307
308
#endif  // SRC_STREAM_BASE_INL_H_