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

32362
  if (err != 0 && req_wrap != nullptr) {
147
5
    req_wrap->Dispose();
148
  }
149
150
32362
  const char* msg = Error();
151
32362
  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
32362
  return err;
161
}
162
163
81997
StreamWriteResult StreamBase::Write(uv_buf_t* bufs,
164
                                    size_t count,
165
                                    uv_stream_t* send_handle,
166
                                    v8::Local<v8::Object> req_wrap_obj,
167
                                    bool skip_try_write) {
168
81997
  Environment* env = stream_env();
169
  int err;
170
171
81997
  size_t total_bytes = 0;
172
314401
  for (size_t i = 0; i < count; ++i)
173
232404
    total_bytes += bufs[i].len;
174
81997
  bytes_written_ += total_bytes;
175
176

81997
  if (send_handle == nullptr && !skip_try_write) {
177
80037
    err = DoTryWrite(&bufs, &count);
178

80037
    if (err != 0 || count == 0) {
179
74269
      return StreamWriteResult { false, err, nullptr, total_bytes, {} };
180
    }
181
  }
182
183
15455
  v8::HandleScope handle_scope(env->isolate());
184
185
7728
  if (req_wrap_obj.IsEmpty()) {
186
2376
    if (!env->write_wrap_template()
187
2376
             ->NewInstance(env->context())
188
2376
             .ToLocal(&req_wrap_obj)) {
189
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0, {} };
190
    }
191
2376
    StreamReq::ResetObject(req_wrap_obj);
192
  }
193
194
15455
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
195
7728
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
196
15455
  BaseObjectPtr<AsyncWrap> req_wrap_ptr(req_wrap->GetAsyncWrap());
197
198
7728
  err = DoWrite(req_wrap, bufs, count, send_handle);
199
7727
  bool async = err == 0;
200
201
7727
  if (!async) {
202
9
    req_wrap->Dispose();
203
9
    req_wrap = nullptr;
204
  }
205
206
7727
  const char* msg = Error();
207
7727
  if (msg != nullptr) {
208
10
    if (req_wrap_obj->Set(env->context(),
209
                          env->error_string(),
210
20
                          OneByteString(env->isolate(), msg)).IsNothing()) {
211
      return StreamWriteResult { false, UV_EBUSY, nullptr, 0, {} };
212
    }
213
5
    ClearError();
214
  }
215
216
  return StreamWriteResult {
217
7727
      async, err, req_wrap, total_bytes, std::move(req_wrap_ptr) };
218
}
219
220
template <typename OtherBase>
221
7635
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
7635
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
228
7635
}
229
230
template <typename OtherBase>
231
7728
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
7728
              AsyncWrap::PROVIDER_WRITEWRAP) {
238
7728
}
239
240
68469
void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
241
68469
  obj->SetAlignedPointerInInternalField(
242
      StreamBase::kStreamBaseField, this);
243
68469
}
244
245
257887
StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
246
515774
  if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
247
1
    return nullptr;
248
249
  return static_cast<StreamBase*>(
250
257886
      obj->GetAlignedPointerFromInternalField(
251
257886
          StreamBase::kStreamBaseField));
252
}
253
254
6719
WriteWrap* WriteWrap::FromObject(v8::Local<v8::Object> req_wrap_obj) {
255
6719
  return static_cast<WriteWrap*>(StreamReq::FromObject(req_wrap_obj));
256
}
257
258
template <typename T, bool kIsWeak>
259
6719
WriteWrap* WriteWrap::FromObject(
260
    const BaseObjectPtrImpl<T, kIsWeak>& base_obj) {
261
6719
  if (!base_obj) return nullptr;
262
6719
  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
3604
void WriteWrap::SetBackingStore(std::unique_ptr<v8::BackingStore> bs) {
277
3604
  CHECK(!backing_store_);
278
3604
  backing_store_ = std::move(bs);
279
3604
}
280
281
15244
void StreamReq::Done(int status, const char* error_str) {
282
15244
  AsyncWrap* async_wrap = GetAsyncWrap();
283
15244
  Environment* env = async_wrap->env();
284
15244
  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
15244
  OnDone(status);
295
}
296
297
103280
void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
298
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
299
300
103280
  obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
301
103280
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
302
103280
}
303
304
}  // namespace node
305
306
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
307
308
#endif  // SRC_STREAM_BASE_INL_H_