GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/stream_base-inl.h Lines: 201 207 97.1 %
Date: 2019-01-07 12:15:22 Branches: 80 130 61.5 %

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 "stream_base.h"
7
8
#include "node.h"
9
#include "env-inl.h"
10
#include "v8.h"
11
12
namespace node {
13
14
using v8::Signature;
15
using v8::External;
16
using v8::FunctionCallbackInfo;
17
using v8::FunctionTemplate;
18
using v8::HandleScope;
19
using v8::Local;
20
using v8::Object;
21
using v8::PropertyAttribute;
22
using v8::PropertyCallbackInfo;
23
using v8::String;
24
using v8::Value;
25
26
using AsyncHooks = Environment::AsyncHooks;
27
28
29968
inline void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
29
59936
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField),
30
           nullptr);
31
29968
  req_wrap_obj->SetAlignedPointerInInternalField(kStreamReqField, this);
32
29968
}
33
34
584
inline StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
35
  return static_cast<StreamReq*>(
36
1168
      req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField));
37
}
38
39
29957
inline void StreamReq::Dispose() {
40
59914
  object()->SetAlignedPointerInInternalField(kStreamReqField, nullptr);
41
29957
  delete this;
42
29957
}
43
44
30544
inline v8::Local<v8::Object> StreamReq::object() {
45
30544
  return GetAsyncWrap()->object();
46
}
47
48
92899
inline StreamListener::~StreamListener() {
49
92899
  if (stream_ != nullptr)
50
78224
    stream_->RemoveStreamListener(this);
51
92899
}
52
53
10657
inline void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) {
54
10657
  CHECK_NOT_NULL(previous_listener_);
55
10657
  previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
56
10657
}
57
58
23032
inline void StreamListener::OnStreamAfterShutdown(ShutdownWrap* w, int status) {
59
23032
  CHECK_NOT_NULL(previous_listener_);
60
23032
  previous_listener_->OnStreamAfterShutdown(w, status);
61
23032
}
62
63
2539
inline void StreamListener::OnStreamAfterWrite(WriteWrap* w, int status) {
64
2539
  CHECK_NOT_NULL(previous_listener_);
65
2539
  previous_listener_->OnStreamAfterWrite(w, status);
66
2539
}
67
68
56987
inline StreamResource::~StreamResource() {
69
114481
  while (listener_ != nullptr) {
70
507
    StreamListener* listener = listener_;
71
507
    listener->OnStreamDestroy();
72
    // Remove the listener if it didn’t remove itself. This makes the logic
73
    // in `OnStreamDestroy()` implementations easier, because they
74
    // may call generic cleanup functions which can just remove the
75
    // listener unconditionally.
76
507
    if (listener == listener_)
77
507
      RemoveStreamListener(listener_);
78
  }
79
56987
}
80
81
93369
inline void StreamResource::PushStreamListener(StreamListener* listener) {
82
93369
  CHECK_NOT_NULL(listener);
83
93369
  CHECK_NULL(listener->stream_);
84
85
93369
  listener->previous_listener_ = listener_;
86
93369
  listener->stream_ = this;
87
88
93369
  listener_ = listener;
89
93369
}
90
91
93005
inline void StreamResource::RemoveStreamListener(StreamListener* listener) {
92
93005
  CHECK_NOT_NULL(listener);
93
94
  StreamListener* previous;
95
  StreamListener* current;
96
97
  // Remove from the linked list.
98
93512
  for (current = listener_, previous = nullptr;
99
       /* No loop condition because we want a crash if listener is not found */
100
       ; previous = current, current = current->previous_listener_) {
101
93512
    CHECK_NOT_NULL(current);
102
93512
    if (current == listener) {
103
93005
      if (previous != nullptr)
104
507
        previous->previous_listener_ = current->previous_listener_;
105
      else
106
92498
        listener_ = listener->previous_listener_;
107
93005
      break;
108
    }
109
507
  }
110
111
93005
  listener->stream_ = nullptr;
112
93005
  listener->previous_listener_ = nullptr;
113
93005
}
114
115
332398
inline uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
116
#ifdef DEBUG
117
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
118
#endif
119
332398
  return listener_->OnStreamAlloc(suggested_size);
120
}
121
122
343841
inline void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) {
123
#ifdef DEBUG
124
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
125
#endif
126
343841
  if (nread > 0)
127
327640
    bytes_read_ += static_cast<uint64_t>(nread);
128
343841
  listener_->OnStreamRead(nread, buf);
129
343762
}
130
131
4557
inline void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
132
#ifdef DEBUG
133
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
134
#endif
135
4557
  listener_->OnStreamAfterWrite(w, status);
136
4554
}
137
138
25401
inline void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
139
#ifdef DEBUG
140
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
141
#endif
142
25401
  listener_->OnStreamAfterShutdown(w, status);
143
25401
}
144
145
2543
inline void StreamResource::EmitWantsWrite(size_t suggested_size) {
146
#ifdef DEBUG
147
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
148
#endif
149
2543
  listener_->OnStreamWantsWrite(suggested_size);
150
2543
}
151
152
57328
inline StreamBase::StreamBase(Environment* env) : env_(env) {
153
57328
  PushStreamListener(&default_listener_);
154
57328
}
155
156
479307
inline Environment* StreamBase::stream_env() const {
157
479307
  return env_;
158
}
159
160
25403
inline int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
161
25403
  Environment* env = stream_env();
162
163
25403
  HandleScope handle_scope(env->isolate());
164
165
25403
  if (req_wrap_obj.IsEmpty()) {
166
    req_wrap_obj =
167
        env->shutdown_wrap_template()
168
1278
            ->NewInstance(env->context()).ToLocalChecked();
169
426
    StreamReq::ResetObject(req_wrap_obj);
170
  }
171
172
50806
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
173
25403
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
174
25403
  int err = DoShutdown(req_wrap);
175
176
25403
  if (err != 0) {
177
    req_wrap->Dispose();
178
  }
179
180
25403
  const char* msg = Error();
181
25403
  if (msg != nullptr) {
182
    req_wrap_obj->Set(
183
        env->context(),
184
        env->error_string(), OneByteString(env->isolate(), msg)).FromJust();
185
    ClearError();
186
  }
187
188
50806
  return err;
189
}
190
191
97812
inline StreamWriteResult StreamBase::Write(
192
    uv_buf_t* bufs,
193
    size_t count,
194
    uv_stream_t* send_handle,
195
    v8::Local<v8::Object> req_wrap_obj) {
196
97812
  Environment* env = stream_env();
197
  int err;
198
199
97812
  size_t total_bytes = 0;
200
317753
  for (size_t i = 0; i < count; ++i)
201
219941
    total_bytes += bufs[i].len;
202
97812
  bytes_written_ += total_bytes;
203
204
97812
  if (send_handle == nullptr) {
205
97712
    err = DoTryWrite(&bufs, &count);
206

97712
    if (err != 0 || count == 0) {
207
93247
      return StreamWriteResult { false, err, nullptr, total_bytes };
208
    }
209
  }
210
211
4565
  HandleScope handle_scope(env->isolate());
212
213
4565
  if (req_wrap_obj.IsEmpty()) {
214
    req_wrap_obj =
215
        env->write_wrap_template()
216
4197
            ->NewInstance(env->context()).ToLocalChecked();
217
1399
    StreamReq::ResetObject(req_wrap_obj);
218
  }
219
220
9129
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
221
4565
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
222
223
4565
  err = DoWrite(req_wrap, bufs, count, send_handle);
224
4564
  bool async = err == 0;
225
226
4564
  if (!async) {
227
2
    req_wrap->Dispose();
228
2
    req_wrap = nullptr;
229
  }
230
231
4564
  const char* msg = Error();
232
4564
  if (msg != nullptr) {
233
    req_wrap_obj->Set(env->context(),
234
                      env->error_string(),
235
                      OneByteString(env->isolate(), msg)).FromJust();
236
    ClearError();
237
  }
238
239
9128
  return StreamWriteResult { async, err, req_wrap, total_bytes };
240
}
241
242
template <typename OtherBase>
243
25403
SimpleShutdownWrap<OtherBase>::SimpleShutdownWrap(
244
    StreamBase* stream,
245
    v8::Local<v8::Object> req_wrap_obj)
246
  : ShutdownWrap(stream, req_wrap_obj),
247
    OtherBase(stream->stream_env(),
248
              req_wrap_obj,
249
25403
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
250
25403
}
251
252
21511
inline ShutdownWrap* StreamBase::CreateShutdownWrap(
253
    v8::Local<v8::Object> object) {
254
21511
  return new SimpleShutdownWrap<AsyncWrap>(this, object);
255
}
256
257
template <typename OtherBase>
258
4565
SimpleWriteWrap<OtherBase>::SimpleWriteWrap(
259
    StreamBase* stream,
260
    v8::Local<v8::Object> req_wrap_obj)
261
  : WriteWrap(stream, req_wrap_obj),
262
    OtherBase(stream->stream_env(),
263
              req_wrap_obj,
264
4565
              AsyncWrap::PROVIDER_WRITEWRAP) {
265
4565
}
266
267
4226
inline WriteWrap* StreamBase::CreateWriteWrap(
268
    v8::Local<v8::Object> object) {
269
4226
  return new SimpleWriteWrap<AsyncWrap>(this, object);
270
}
271
272
template <class Base>
273
8151
void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
274
8151
  HandleScope scope(env->isolate());
275
276
  enum PropertyAttribute attributes =
277
      static_cast<PropertyAttribute>(
278
8151
          v8::ReadOnly | v8::DontDelete | v8::DontEnum);
279
280
8151
  Local<Signature> signature = Signature::New(env->isolate(), t);
281
282
  Local<FunctionTemplate> get_fd_templ =
283
      env->NewFunctionTemplate(GetFD<Base>,
284
                               signature,
285
                               v8::ConstructorBehavior::kThrow,
286
8151
                               v8::SideEffectType::kHasNoSideEffect);
287
288
  Local<FunctionTemplate> get_external_templ =
289
      env->NewFunctionTemplate(GetExternal<Base>,
290
                               signature,
291
                               v8::ConstructorBehavior::kThrow,
292
8151
                               v8::SideEffectType::kHasNoSideEffect);
293
294
  Local<FunctionTemplate> get_bytes_read_templ =
295
      env->NewFunctionTemplate(GetBytesRead<Base>,
296
                               signature,
297
                               v8::ConstructorBehavior::kThrow,
298
8151
                               v8::SideEffectType::kHasNoSideEffect);
299
300
  Local<FunctionTemplate> get_bytes_written_templ =
301
      env->NewFunctionTemplate(GetBytesWritten<Base>,
302
                               signature,
303
                               v8::ConstructorBehavior::kThrow,
304
8151
                               v8::SideEffectType::kHasNoSideEffect);
305
306
16302
  t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(),
307
                                              get_fd_templ,
308
                                              Local<FunctionTemplate>(),
309
32604
                                              attributes);
310
311
16302
  t->PrototypeTemplate()->SetAccessorProperty(env->external_stream_string(),
312
                                              get_external_templ,
313
                                              Local<FunctionTemplate>(),
314
32604
                                              attributes);
315
316
16302
  t->PrototypeTemplate()->SetAccessorProperty(env->bytes_read_string(),
317
                                              get_bytes_read_templ,
318
                                              Local<FunctionTemplate>(),
319
32604
                                              attributes);
320
321
16302
  t->PrototypeTemplate()->SetAccessorProperty(env->bytes_written_string(),
322
                                              get_bytes_written_templ,
323
                                              Local<FunctionTemplate>(),
324
32604
                                              attributes);
325
326
8151
  env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStartJS>);
327
8151
  env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStopJS>);
328
8151
  env->SetProtoMethod(t, "shutdown", JSMethod<Base, &StreamBase::Shutdown>);
329
8151
  env->SetProtoMethod(t, "writev", JSMethod<Base, &StreamBase::Writev>);
330
8151
  env->SetProtoMethod(t,
331
                      "writeBuffer",
332
                      JSMethod<Base, &StreamBase::WriteBuffer>);
333
8151
  env->SetProtoMethod(t,
334
                      "writeAsciiString",
335
                      JSMethod<Base, &StreamBase::WriteString<ASCII> >);
336
8151
  env->SetProtoMethod(t,
337
                      "writeUtf8String",
338
                      JSMethod<Base, &StreamBase::WriteString<UTF8> >);
339
8151
  env->SetProtoMethod(t,
340
                      "writeUcs2String",
341
                      JSMethod<Base, &StreamBase::WriteString<UCS2> >);
342
8151
  env->SetProtoMethod(t,
343
                      "writeLatin1String",
344
8151
                      JSMethod<Base, &StreamBase::WriteString<LATIN1> >);
345
8151
}
346
347
348
template <class Base>
349
127
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {
350
  // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
351
  Base* handle;
352
127
  ASSIGN_OR_RETURN_UNWRAP(&handle,
353
                          args.This(),
354
                          args.GetReturnValue().Set(UV_EINVAL));
355
356
127
  StreamBase* wrap = static_cast<StreamBase*>(handle);
357
127
  if (!wrap->IsAlive())
358
    return args.GetReturnValue().Set(UV_EINVAL);
359
360
381
  args.GetReturnValue().Set(wrap->GetFD());
361
}
362
363
template <class Base>
364
32414
void StreamBase::GetBytesRead(const FunctionCallbackInfo<Value>& args) {
365
  Base* handle;
366
64830
  ASSIGN_OR_RETURN_UNWRAP(&handle,
367
                          args.This(),
368
                          args.GetReturnValue().Set(0));
369
370
32413
  StreamBase* wrap = static_cast<StreamBase*>(handle);
371
  // uint64_t -> double. 53bits is enough for all real cases.
372
97239
  args.GetReturnValue().Set(static_cast<double>(wrap->bytes_read_));
373
}
374
375
template <class Base>
376
32414
void StreamBase::GetBytesWritten(const FunctionCallbackInfo<Value>& args) {
377
  Base* handle;
378
64828
  ASSIGN_OR_RETURN_UNWRAP(&handle,
379
                          args.This(),
380
                          args.GetReturnValue().Set(0));
381
382
32414
  StreamBase* wrap = static_cast<StreamBase*>(handle);
383
  // uint64_t -> double. 53bits is enough for all real cases.
384
97242
  args.GetReturnValue().Set(static_cast<double>(wrap->bytes_written_));
385
}
386
387
template <class Base>
388
16402
void StreamBase::GetExternal(const FunctionCallbackInfo<Value>& args) {
389
  Base* handle;
390
32804
  ASSIGN_OR_RETURN_UNWRAP(&handle, args.This());
391
392
16402
  StreamBase* wrap = static_cast<StreamBase*>(handle);
393
16402
  Local<External> ext = External::New(args.GetIsolate(), wrap);
394
32804
  args.GetReturnValue().Set(ext);
395
}
396
397
398
template <class Base,
399
          int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
400
417343
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
401
  Base* handle;
402




417344
  ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder());
403
404




417343
  StreamBase* wrap = static_cast<StreamBase*>(handle);
405




417343
  if (!wrap->IsAlive())
406
2
    return args.GetReturnValue().Set(UV_EINVAL);
407
408
417342
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(handle);
409
1252026
  args.GetReturnValue().Set((wrap->*Method)(args));
410
}
411
412
413
25401
inline void ShutdownWrap::OnDone(int status) {
414
25401
  stream()->EmitAfterShutdown(this, status);
415
25401
  Dispose();
416
25401
}
417
418
2052
inline void WriteWrap::SetAllocatedStorage(char* data, size_t size) {
419
2052
  CHECK_NULL(storage_);
420
2052
  storage_ = data;
421
2052
  storage_size_ = size;
422
2052
}
423
424
inline char* WriteWrap::Storage() {
425
  return storage_;
426
}
427
428
inline size_t WriteWrap::StorageSize() const {
429
  return storage_size_;
430
}
431
432
4557
inline void WriteWrap::OnDone(int status) {
433
4557
  stream()->EmitAfterWrite(this, status);
434
4554
  Dispose();
435
4554
}
436
437
29958
inline void StreamReq::Done(int status, const char* error_str) {
438
29958
  AsyncWrap* async_wrap = GetAsyncWrap();
439
29958
  Environment* env = async_wrap->env();
440
29958
  if (error_str != nullptr) {
441
6
    async_wrap->object()->Set(env->context(),
442
                              env->error_string(),
443
36
                              OneByteString(env->isolate(), error_str))
444
12
                              .FromJust();
445
  }
446
447
29958
  OnDone(status);
448
29955
}
449
450
376612
inline void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
451
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
452
453
376612
  obj->SetAlignedPointerInInternalField(0, nullptr);  // BaseObject field.
454
376612
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
455
376612
}
456
457
458
}  // namespace node
459
460
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
461
462
#endif  // SRC_STREAM_BASE_INL_H_