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.cc Lines: 303 319 95.0 %
Date: 2021-02-11 04:11:15 Branches: 210 336 62.5 %

Line Branch Exec Source
1
#include "stream_base.h"  // NOLINT(build/include_inline)
2
#include "stream_base-inl.h"
3
#include "stream_wrap.h"
4
#include "allocated_buffer-inl.h"
5
6
#include "node.h"
7
#include "node_buffer.h"
8
#include "node_errors.h"
9
#include "env-inl.h"
10
#include "js_stream.h"
11
#include "string_bytes.h"
12
#include "util-inl.h"
13
#include "v8.h"
14
15
#include <climits>  // INT_MAX
16
17
namespace node {
18
19
using v8::Array;
20
using v8::ArrayBuffer;
21
using v8::ConstructorBehavior;
22
using v8::Context;
23
using v8::DontDelete;
24
using v8::DontEnum;
25
using v8::External;
26
using v8::Function;
27
using v8::FunctionCallbackInfo;
28
using v8::FunctionTemplate;
29
using v8::HandleScope;
30
using v8::Integer;
31
using v8::Local;
32
using v8::MaybeLocal;
33
using v8::Object;
34
using v8::PropertyAttribute;
35
using v8::ReadOnly;
36
using v8::SideEffectType;
37
using v8::Signature;
38
using v8::String;
39
using v8::Value;
40
41
template int StreamBase::WriteString<ASCII>(
42
    const FunctionCallbackInfo<Value>& args);
43
template int StreamBase::WriteString<UTF8>(
44
    const FunctionCallbackInfo<Value>& args);
45
template int StreamBase::WriteString<UCS2>(
46
    const FunctionCallbackInfo<Value>& args);
47
template int StreamBase::WriteString<LATIN1>(
48
    const FunctionCallbackInfo<Value>& args);
49
50
51
54119
int StreamBase::ReadStartJS(const FunctionCallbackInfo<Value>& args) {
52
54119
  return ReadStart();
53
}
54
55
56
32063
int StreamBase::ReadStopJS(const FunctionCallbackInfo<Value>& args) {
57
32063
  return ReadStop();
58
}
59
60
12
int StreamBase::UseUserBuffer(const FunctionCallbackInfo<Value>& args) {
61
12
  CHECK(Buffer::HasInstance(args[0]));
62
63
24
  uv_buf_t buf = uv_buf_init(Buffer::Data(args[0]), Buffer::Length(args[0]));
64
12
  PushStreamListener(new CustomBufferJSListener(buf));
65
12
  return 0;
66
}
67
68
30753
int StreamBase::Shutdown(const FunctionCallbackInfo<Value>& args) {
69
61506
  CHECK(args[0]->IsObject());
70
61506
  Local<Object> req_wrap_obj = args[0].As<Object>();
71
72
30753
  return Shutdown(req_wrap_obj);
73
}
74
75
334587
void StreamBase::SetWriteResult(const StreamWriteResult& res) {
76
334587
  env_->stream_base_state()[kBytesWritten] = res.bytes;
77
334587
  env_->stream_base_state()[kLastWriteWasAsync] = res.async;
78
334587
}
79
80
13068
int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
81
13068
  Environment* env = Environment::GetCurrent(args);
82
83
26136
  CHECK(args[0]->IsObject());
84
26136
  CHECK(args[1]->IsArray());
85
86
26136
  Local<Object> req_wrap_obj = args[0].As<Object>();
87
26136
  Local<Array> chunks = args[1].As<Array>();
88
26136
  bool all_buffers = args[2]->IsTrue();
89
90
  size_t count;
91
13068
  if (all_buffers)
92
289
    count = chunks->Length();
93
  else
94
12779
    count = chunks->Length() >> 1;
95
96
26136
  MaybeStackBuffer<uv_buf_t, 16> bufs(count);
97
98
13068
  size_t storage_size = 0;
99
  size_t offset;
100
101
13068
  if (!all_buffers) {
102
    // Determine storage size first
103
83010
    for (size_t i = 0; i < count; i++) {
104
210693
      Local<Value> chunk = chunks->Get(env->context(), i * 2).ToLocalChecked();
105
106
70231
      if (Buffer::HasInstance(chunk))
107
30052
        continue;
108
        // Buffer chunk, no additional storage required
109
110
      // String chunk
111
120537
      Local<String> string = chunk->ToString(env->context()).ToLocalChecked();
112
40179
      enum encoding encoding = ParseEncoding(env->isolate(),
113
120537
          chunks->Get(env->context(), i * 2 + 1).ToLocalChecked());
114
      size_t chunk_size;
115


87790
      if (encoding == UTF8 && string->Length() > 65535 &&
116
40205
          !StringBytes::Size(env->isolate(), string, encoding).To(&chunk_size))
117
        return 0;
118
80358
      else if (!StringBytes::StorageSize(env->isolate(), string, encoding)
119
40179
                    .To(&chunk_size))
120
        return 0;
121
40179
      storage_size += chunk_size;
122
    }
123
124
12779
    if (storage_size > INT_MAX)
125
      return UV_ENOBUFS;
126
  } else {
127
26840
    for (size_t i = 0; i < count; i++) {
128
79653
      Local<Value> chunk = chunks->Get(env->context(), i).ToLocalChecked();
129
26551
      bufs[i].base = Buffer::Data(chunk);
130
26551
      bufs[i].len = Buffer::Length(chunk);
131
    }
132
  }
133
134
26136
  AllocatedBuffer storage;
135
13068
  if (storage_size > 0)
136
12733
    storage = AllocatedBuffer::AllocateManaged(env, storage_size);
137
138
13068
  offset = 0;
139
13068
  if (!all_buffers) {
140
83010
    for (size_t i = 0; i < count; i++) {
141
210693
      Local<Value> chunk = chunks->Get(env->context(), i * 2).ToLocalChecked();
142
143
      // Write buffer
144
70231
      if (Buffer::HasInstance(chunk)) {
145
30052
        bufs[i].base = Buffer::Data(chunk);
146
30052
        bufs[i].len = Buffer::Length(chunk);
147
30052
        continue;
148
      }
149
150
      // Write string
151
40179
      CHECK_LE(offset, storage_size);
152
40179
      char* str_storage = storage.data() + offset;
153
40179
      size_t str_size = storage.size() - offset;
154
155
120537
      Local<String> string = chunk->ToString(env->context()).ToLocalChecked();
156
40179
      enum encoding encoding = ParseEncoding(env->isolate(),
157
120537
          chunks->Get(env->context(), i * 2 + 1).ToLocalChecked());
158
80358
      str_size = StringBytes::Write(env->isolate(),
159
                                    str_storage,
160
                                    str_size,
161
                                    string,
162
40179
                                    encoding);
163
40179
      bufs[i].base = str_storage;
164
40179
      bufs[i].len = str_size;
165
40179
      offset += str_size;
166
    }
167
  }
168
169
26136
  StreamWriteResult res = Write(*bufs, count, nullptr, req_wrap_obj);
170
13068
  SetWriteResult(res);
171

13068
  if (res.wrap != nullptr && storage_size > 0) {
172
319
    res.wrap->SetAllocatedStorage(std::move(storage));
173
  }
174
13068
  return res.err;
175
}
176
177
178
45267
int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
179
90534
  CHECK(args[0]->IsObject());
180
181
45267
  Environment* env = Environment::GetCurrent(args);
182
183
90534
  if (!args[1]->IsUint8Array()) {
184
1
    node::THROW_ERR_INVALID_ARG_TYPE(env, "Second argument must be a buffer");
185
1
    return 0;
186
  }
187
188
90532
  Local<Object> req_wrap_obj = args[0].As<Object>();
189
  uv_buf_t buf;
190
45266
  buf.base = Buffer::Data(args[1]);
191
45266
  buf.len = Buffer::Length(args[1]);
192
193
45266
  uv_stream_t* send_handle = nullptr;
194
195

90532
  if (args[2]->IsObject() && IsIPCPipe()) {
196
    Local<Object> send_handle_obj = args[2].As<Object>();
197
198
    HandleWrap* wrap;
199
    ASSIGN_OR_RETURN_UNWRAP(&wrap, send_handle_obj, UV_EINVAL);
200
    send_handle = reinterpret_cast<uv_stream_t*>(wrap->GetHandle());
201
    // Reference LibuvStreamWrap instance to prevent it from being garbage
202
    // collected before `AfterWrite` is called.
203
    req_wrap_obj->Set(env->context(),
204
                      env->handle_string(),
205
                      send_handle_obj).Check();
206
  }
207
208
90532
  StreamWriteResult res = Write(&buf, 1, send_handle, req_wrap_obj);
209
45266
  SetWriteResult(res);
210
211
45266
  return res.err;
212
}
213
214
215
template <enum encoding enc>
216
276253
int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
217
276253
  Environment* env = Environment::GetCurrent(args);
218


552506
  CHECK(args[0]->IsObject());
219


828759
  CHECK(args[1]->IsString());
220
221
552506
  Local<Object> req_wrap_obj = args[0].As<Object>();
222
552506
  Local<String> string = args[1].As<String>();
223
  Local<Object> send_handle_obj;
224


552506
  if (args[2]->IsObject())
225
204
    send_handle_obj = args[2].As<Object>();
226
227
  // Compute the size of the storage that the string will be flattened into.
228
  // For UTF8 strings that are very long, go ahead and take the hit for
229
  // computing their actual size, rather than tripling the storage.
230
  size_t storage_size;
231



549544
  if (enc == UTF8 && string->Length() > 65535 &&
232
273362
      !StringBytes::Size(env->isolate(), string, enc).To(&storage_size))
233
    return 0;
234


552506
  else if (!StringBytes::StorageSize(env->isolate(), string, enc)
235
276253
                .To(&storage_size))
236
    return 0;
237
238


276253
  if (storage_size > INT_MAX)
239
    return UV_ENOBUFS;
240
241
  // Try writing immediately if write size isn't too big
242
  char stack_storage[16384];  // 16kb
243
  size_t data_size;
244
276253
  size_t synchronously_written = 0;
245
  uv_buf_t buf;
246
247




826457
  bool try_write = storage_size <= sizeof(stack_storage) &&
248


556772
                   (!IsIPCPipe() || send_handle_obj.IsEmpty());
249


276253
  if (try_write) {
250
275052
    data_size = StringBytes::Write(env->isolate(),
251
                                   stack_storage,
252
                                   storage_size,
253
                                   string,
254
                                   enc);
255
275052
    buf = uv_buf_init(stack_storage, data_size);
256
257
275052
    uv_buf_t* bufs = &buf;
258
275052
    size_t count = 1;
259
275052
    const int err = DoTryWrite(&bufs, &count);
260
    // Keep track of the bytes written here, because we're taking a shortcut
261
    // by using `DoTryWrite()` directly instead of using the utilities
262
    // provided by `Write()`.
263


275052
    synchronously_written = count == 0 ? data_size : data_size - buf.len;
264
275052
    bytes_written_ += synchronously_written;
265
266
    // Immediate failure or success
267




275052
    if (err != 0 || count == 0) {
268
273298
      SetWriteResult(StreamWriteResult { false, err, nullptr, data_size, {} });
269
273298
      return err;
270
    }
271
272
    // Partial write
273


1754
    CHECK_EQ(count, 1);
274
  }
275
276
5910
  AllocatedBuffer data;
277
278


2955
  if (try_write) {
279
    // Copy partial data
280
1754
    data = AllocatedBuffer::AllocateManaged(env, buf.len);
281
1754
    memcpy(data.data(), buf.base, buf.len);
282
1754
    data_size = buf.len;
283
  } else {
284
    // Write it
285
1201
    data = AllocatedBuffer::AllocateManaged(env, storage_size);
286
1201
    data_size = StringBytes::Write(env->isolate(),
287
                                   data.data(),
288
                                   storage_size,
289
                                   string,
290
                                   enc);
291
  }
292
293


2955
  CHECK_LE(data_size, storage_size);
294
295
2955
  buf = uv_buf_init(data.data(), data_size);
296
297
2955
  uv_stream_t* send_handle = nullptr;
298
299






3059
  if (IsIPCPipe() && !send_handle_obj.IsEmpty()) {
300
    HandleWrap* wrap;
301


102
    ASSIGN_OR_RETURN_UNWRAP(&wrap, send_handle_obj, UV_EINVAL);
302
102
    send_handle = reinterpret_cast<uv_stream_t*>(wrap->GetHandle());
303
    // Reference LibuvStreamWrap instance to prevent it from being garbage
304
    // collected before `AfterWrite` is called.
305
408
    req_wrap_obj->Set(env->context(),
306
                      env->handle_string(),
307
                      send_handle_obj).Check();
308
  }
309
310
5910
  StreamWriteResult res = Write(&buf, 1, send_handle, req_wrap_obj);
311
2955
  res.bytes += synchronously_written;
312
313
2955
  SetWriteResult(res);
314


2955
  if (res.wrap != nullptr) {
315
2932
    res.wrap->SetAllocatedStorage(std::move(data));
316
  }
317
318
2955
  return res.err;
319
}
320
321
322
311324
MaybeLocal<Value> StreamBase::CallJSOnreadMethod(ssize_t nread,
323
                                                 Local<ArrayBuffer> ab,
324
                                                 size_t offset,
325
                                                 StreamBaseJSChecks checks) {
326
311324
  Environment* env = env_;
327
328
  DCHECK_EQ(static_cast<int32_t>(nread), nread);
329
  DCHECK_LE(offset, INT32_MAX);
330
331
311324
  if (checks == DONT_SKIP_NREAD_CHECKS) {
332
309234
    if (ab.IsEmpty()) {
333
      DCHECK_EQ(offset, 0);
334
      DCHECK_LE(nread, 0);
335
    } else {
336
      DCHECK_GE(nread, 0);
337
    }
338
  }
339
340
311324
  env->stream_base_state()[kReadBytesOrError] = nread;
341
311324
  env->stream_base_state()[kArrayBufferOffset] = offset;
342
343
  Local<Value> argv[] = {
344
666574
    ab.IsEmpty() ? Undefined(env->isolate()).As<Value>() : ab.As<Value>()
345
1245296
  };
346
347
311324
  AsyncWrap* wrap = GetAsyncWrap();
348
311324
  CHECK_NOT_NULL(wrap);
349
622648
  Local<Value> onread = wrap->object()->GetInternalField(
350
622648
      StreamBase::kOnReadFunctionField);
351
311324
  CHECK(onread->IsFunction());
352
622648
  return wrap->MakeCallback(onread.As<Function>(), arraysize(argv), argv);
353
}
354
355
356
3459
bool StreamBase::IsIPCPipe() {
357
3459
  return false;
358
}
359
360
361
int StreamBase::GetFD() {
362
  return -1;
363
}
364
365
366
49350
Local<Object> StreamBase::GetObject() {
367
49350
  return GetAsyncWrap()->object();
368
}
369
370
43204
void StreamBase::AddMethod(Environment* env,
371
                           Local<Signature> signature,
372
                           enum PropertyAttribute attributes,
373
                           Local<FunctionTemplate> t,
374
                           JSMethodFunction* stream_method,
375
                           Local<String> string) {
376
  Local<FunctionTemplate> templ =
377
      env->NewFunctionTemplate(stream_method,
378
                               signature,
379
                               ConstructorBehavior::kThrow,
380
43204
                               SideEffectType::kHasNoSideEffect);
381
129612
  t->PrototypeTemplate()->SetAccessorProperty(
382
43204
      string, templ, Local<FunctionTemplate>(), attributes);
383
43204
}
384
385
10801
void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
386
21602
  HandleScope scope(env->isolate());
387
388
  enum PropertyAttribute attributes =
389
10801
      static_cast<PropertyAttribute>(ReadOnly | DontDelete | DontEnum);
390
10801
  Local<Signature> sig = Signature::New(env->isolate(), t);
391
392
10801
  AddMethod(env, sig, attributes, t, GetFD, env->fd_string());
393
10801
  AddMethod(
394
10801
      env, sig, attributes, t, GetExternal, env->external_stream_string());
395
10801
  AddMethod(env, sig, attributes, t, GetBytesRead, env->bytes_read_string());
396
10801
  AddMethod(
397
10801
      env, sig, attributes, t, GetBytesWritten, env->bytes_written_string());
398
10801
  env->SetProtoMethod(t, "readStart", JSMethod<&StreamBase::ReadStartJS>);
399
10801
  env->SetProtoMethod(t, "readStop", JSMethod<&StreamBase::ReadStopJS>);
400
10801
  env->SetProtoMethod(t, "shutdown", JSMethod<&StreamBase::Shutdown>);
401
  env->SetProtoMethod(t,
402
                      "useUserBuffer",
403
10801
                      JSMethod<&StreamBase::UseUserBuffer>);
404
10801
  env->SetProtoMethod(t, "writev", JSMethod<&StreamBase::Writev>);
405
10801
  env->SetProtoMethod(t, "writeBuffer", JSMethod<&StreamBase::WriteBuffer>);
406
  env->SetProtoMethod(
407
10801
      t, "writeAsciiString", JSMethod<&StreamBase::WriteString<ASCII>>);
408
  env->SetProtoMethod(
409
10801
      t, "writeUtf8String", JSMethod<&StreamBase::WriteString<UTF8>>);
410
  env->SetProtoMethod(
411
10801
      t, "writeUcs2String", JSMethod<&StreamBase::WriteString<UCS2>>);
412
  env->SetProtoMethod(
413
10801
      t, "writeLatin1String", JSMethod<&StreamBase::WriteString<LATIN1>>);
414
54005
  t->PrototypeTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
415
                                                    "isStreamBase"),
416
10801
                              True(env->isolate()));
417
43204
  t->PrototypeTemplate()->SetAccessor(
418
      FIXED_ONE_BYTE_STRING(env->isolate(), "onread"),
419
      BaseObject::InternalFieldGet<
420
          StreamBase::kOnReadFunctionField>,
421
      BaseObject::InternalFieldSet<
422
          StreamBase::kOnReadFunctionField,
423
10801
          &Value::IsFunction>);
424
10801
}
425
426
774
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {
427
  // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
428
1548
  StreamBase* wrap = StreamBase::FromObject(args.This().As<Object>());
429
774
  if (wrap == nullptr) return args.GetReturnValue().Set(UV_EINVAL);
430
431
774
  if (!wrap->IsAlive()) return args.GetReturnValue().Set(UV_EINVAL);
432
433
2322
  args.GetReturnValue().Set(wrap->GetFD());
434
}
435
436
35365
void StreamBase::GetBytesRead(const FunctionCallbackInfo<Value>& args) {
437
70730
  StreamBase* wrap = StreamBase::FromObject(args.This().As<Object>());
438
35367
  if (wrap == nullptr) return args.GetReturnValue().Set(0);
439
440
  // uint64_t -> double. 53bits is enough for all real cases.
441
106092
  args.GetReturnValue().Set(static_cast<double>(wrap->bytes_read_));
442
}
443
444
35391
void StreamBase::GetBytesWritten(const FunctionCallbackInfo<Value>& args) {
445
70782
  StreamBase* wrap = StreamBase::FromObject(args.This().As<Object>());
446
35391
  if (wrap == nullptr) return args.GetReturnValue().Set(0);
447
448
  // uint64_t -> double. 53bits is enough for all real cases.
449
106173
  args.GetReturnValue().Set(static_cast<double>(wrap->bytes_written_));
450
}
451
452
2
void StreamBase::GetExternal(const FunctionCallbackInfo<Value>& args) {
453
4
  StreamBase* wrap = StreamBase::FromObject(args.This().As<Object>());
454
2
  if (wrap == nullptr) return;
455
456
2
  Local<External> ext = External::New(args.GetIsolate(), wrap);
457
4
  args.GetReturnValue().Set(ext);
458
}
459
460
template <int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
461
451536
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
462
903072
  StreamBase* wrap = StreamBase::FromObject(args.Holder().As<Object>());
463





451537
  if (wrap == nullptr) return;
464
465





451538
  if (!wrap->IsAlive()) return args.GetReturnValue().Set(UV_EINVAL);
466
467
903070
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap->GetAsyncWrap());
468
1354605
  args.GetReturnValue().Set((wrap->*Method)(args));
469
}
470
471
9194
int StreamResource::DoTryWrite(uv_buf_t** bufs, size_t* count) {
472
  // No TryWrite by default
473
9194
  return 0;
474
}
475
476
477
46198
const char* StreamResource::Error() const {
478
46198
  return nullptr;
479
}
480
481
482
void StreamResource::ClearError() {
483
  // No-op
484
}
485
486
487
280916
uv_buf_t EmitToJSStreamListener::OnStreamAlloc(size_t suggested_size) {
488
280916
  CHECK_NOT_NULL(stream_);
489
280916
  Environment* env = static_cast<StreamBase*>(stream_)->stream_env();
490
280916
  return AllocatedBuffer::AllocateManaged(env, suggested_size).release();
491
}
492
493
295447
void EmitToJSStreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
494
295447
  CHECK_NOT_NULL(stream_);
495
295447
  StreamBase* stream = static_cast<StreamBase*>(stream_);
496
295447
  Environment* env = stream->stream_env();
497
570969
  HandleScope handle_scope(env->isolate());
498
570969
  Context::Scope context_scope(env->context());
499
570969
  AllocatedBuffer buf(env, buf_);
500
501
295447
  if (nread <= 0)  {
502
19874
    if (nread < 0)
503
19867
      stream->CallJSOnreadMethod(nread, Local<ArrayBuffer>());
504
19846
    return;
505
  }
506
507
275573
  CHECK_LE(static_cast<size_t>(nread), buf.size());
508
275573
  buf.Resize(nread);
509
510
275573
  stream->CallJSOnreadMethod(nread, buf.ToArrayBuffer());
511
}
512
513
514
2090
uv_buf_t CustomBufferJSListener::OnStreamAlloc(size_t suggested_size) {
515
2090
  return buffer_;
516
}
517
518
519
2096
void CustomBufferJSListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
520
2096
  CHECK_NOT_NULL(stream_);
521
522
2096
  StreamBase* stream = static_cast<StreamBase*>(stream_);
523
2096
  Environment* env = stream->stream_env();
524
4186
  HandleScope handle_scope(env->isolate());
525
4186
  Context::Scope context_scope(env->context());
526
527
  // To deal with the case where POLLHUP is received and UV_EOF is returned, as
528
  // libuv returns an empty buffer (on unices only).
529

2096
  if (nread == UV_EOF && buf.base == nullptr) {
530
6
    stream->CallJSOnreadMethod(nread, Local<ArrayBuffer>());
531
6
    return;
532
  }
533
534
2090
  CHECK_EQ(buf.base, buffer_.base);
535
536
  MaybeLocal<Value> ret = stream->CallJSOnreadMethod(nread,
537
                             Local<ArrayBuffer>(),
538
                             0,
539
2090
                             StreamBase::SKIP_NREAD_CHECKS);
540
  Local<Value> next_buf_v;
541

6270
  if (ret.ToLocal(&next_buf_v) && !next_buf_v->IsUndefined()) {
542
24
    buffer_.base = Buffer::Data(next_buf_v);
543
24
    buffer_.len = Buffer::Length(next_buf_v);
544
  }
545
}
546
547
548
12614
void ReportWritesToJSStreamListener::OnStreamAfterReqFinished(
549
    StreamReq* req_wrap, int status) {
550
12614
  StreamBase* stream = static_cast<StreamBase*>(stream_);
551
12614
  Environment* env = stream->stream_env();
552
12614
  AsyncWrap* async_wrap = req_wrap->GetAsyncWrap();
553
25227
  HandleScope handle_scope(env->isolate());
554
12614
  Context::Scope context_scope(env->context());
555
25228
  CHECK(!async_wrap->persistent().IsEmpty());
556
12614
  Local<Object> req_wrap_obj = async_wrap->object();
557
558
  Local<Value> argv[] = {
559
    Integer::New(env->isolate(), status),
560
12614
    stream->GetObject(),
561
    Undefined(env->isolate())
562
63070
  };
563
564
12614
  const char* msg = stream->Error();
565
12614
  if (msg != nullptr) {
566
    argv[2] = OneByteString(env->isolate(), msg);
567
    stream->ClearError();
568
  }
569
570
50456
  if (req_wrap_obj->Has(env->context(), env->oncomplete_string()).FromJust())
571
12613
    async_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
572
12613
}
573
574
5333
void ReportWritesToJSStreamListener::OnStreamAfterWrite(
575
    WriteWrap* req_wrap, int status) {
576
5333
  OnStreamAfterReqFinished(req_wrap, status);
577
5332
}
578
579
7281
void ReportWritesToJSStreamListener::OnStreamAfterShutdown(
580
    ShutdownWrap* req_wrap, int status) {
581
7281
  OnStreamAfterReqFinished(req_wrap, status);
582
7281
}
583
584
7281
void ShutdownWrap::OnDone(int status) {
585
7281
  stream()->EmitAfterShutdown(this, status);
586
7281
  Dispose();
587
7281
}
588
589
7659
void WriteWrap::OnDone(int status) {
590
7659
  stream()->EmitAfterWrite(this, status);
591
7658
  Dispose();
592
7658
}
593
594
209334
StreamListener::~StreamListener() {
595
104667
  if (stream_ != nullptr)
596
88281
    stream_->RemoveStreamListener(this);
597
104667
}
598
599
3485
void StreamListener::OnStreamAfterShutdown(ShutdownWrap* w, int status) {
600
3485
  CHECK_NOT_NULL(previous_listener_);
601
3485
  previous_listener_->OnStreamAfterShutdown(w, status);
602
3485
}
603
604
4081
void StreamListener::OnStreamAfterWrite(WriteWrap* w, int status) {
605
4081
  CHECK_NOT_NULL(previous_listener_);
606
4081
  previous_listener_->OnStreamAfterWrite(w, status);
607
4081
}
608
609
128844
StreamResource::~StreamResource() {
610
65640
  while (listener_ != nullptr) {
611
609
    StreamListener* listener = listener_;
612
609
    listener->OnStreamDestroy();
613
    // Remove the listener if it didn’t remove itself. This makes the logic
614
    // in `OnStreamDestroy()` implementations easier, because they
615
    // may call generic cleanup functions which can just remove the
616
    // listener unconditionally.
617
609
    if (listener == listener_)
618
597
      RemoveStreamListener(listener_);
619
  }
620
64422
}
621
622
12
ShutdownWrap* StreamBase::CreateShutdownWrap(
623
    Local<Object> object) {
624
12
  auto* wrap = new SimpleShutdownWrap<AsyncWrap>(this, object);
625
12
  wrap->MakeWeak();
626
12
  return wrap;
627
}
628
629
7440
WriteWrap* StreamBase::CreateWriteWrap(
630
    Local<Object> object) {
631
7440
  auto* wrap = new SimpleWriteWrap<AsyncWrap>(this, object);
632
7440
  wrap->MakeWeak();
633
7440
  return wrap;
634
}
635
636

14097
}  // namespace node