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-inl.h Lines: 87 192 45.3 %
Date: 2019-02-01 22:03:38 Branches: 28 130 21.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
inline void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
29
  CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField),
30
           nullptr);
31
  req_wrap_obj->SetAlignedPointerInInternalField(kStreamReqField, this);
32
}
33
34
inline StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
35
  return static_cast<StreamReq*>(
36
      req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField));
37
}
38
39
inline void StreamReq::Dispose() {
40
  object()->SetAlignedPointerInInternalField(kStreamReqField, nullptr);
41
  delete this;
42
}
43
44
inline v8::Local<v8::Object> StreamReq::object() {
45
  return GetAsyncWrap()->object();
46
}
47
48
670
inline StreamListener::~StreamListener() {
49
670
  if (stream_ != nullptr)
50
670
    stream_->RemoveStreamListener(this);
51
670
}
52
53
inline void StreamListener::PassReadErrorToPreviousListener(ssize_t nread) {
54
  CHECK_NOT_NULL(previous_listener_);
55
  previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0));
56
}
57
58
inline void StreamListener::OnStreamAfterShutdown(ShutdownWrap* w, int status) {
59
  CHECK_NOT_NULL(previous_listener_);
60
  previous_listener_->OnStreamAfterShutdown(w, status);
61
}
62
63
inline void StreamListener::OnStreamAfterWrite(WriteWrap* w, int status) {
64
  CHECK_NOT_NULL(previous_listener_);
65
  previous_listener_->OnStreamAfterWrite(w, status);
66
}
67
68
670
inline StreamResource::~StreamResource() {
69
1340
  while (listener_ != nullptr) {
70
    StreamListener* listener = listener_;
71
    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
    if (listener == listener_)
77
      RemoveStreamListener(listener_);
78
  }
79
670
}
80
81
704
inline void StreamResource::PushStreamListener(StreamListener* listener) {
82
704
  CHECK_NOT_NULL(listener);
83
704
  CHECK_NULL(listener->stream_);
84
85
704
  listener->previous_listener_ = listener_;
86
704
  listener->stream_ = this;
87
88
704
  listener_ = listener;
89
704
}
90
91
670
inline void StreamResource::RemoveStreamListener(StreamListener* listener) {
92
670
  CHECK_NOT_NULL(listener);
93
94
  StreamListener* previous;
95
  StreamListener* current;
96
97
  // Remove from the linked list.
98
670
  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
670
    CHECK_NOT_NULL(current);
102
670
    if (current == listener) {
103
670
      if (previous != nullptr)
104
        previous->previous_listener_ = current->previous_listener_;
105
      else
106
670
        listener_ = listener->previous_listener_;
107
670
      break;
108
    }
109
  }
110
111
670
  listener->stream_ = nullptr;
112
670
  listener->previous_listener_ = nullptr;
113
670
}
114
115
1019
inline uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) {
116
#ifdef DEBUG
117
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
118
#endif
119
1019
  return listener_->OnStreamAlloc(suggested_size);
120
}
121
122
1020
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
1020
  if (nread > 0)
127
692
    bytes_read_ += static_cast<uint64_t>(nread);
128
1020
  listener_->OnStreamRead(nread, buf);
129
1012
}
130
131
inline void StreamResource::EmitAfterWrite(WriteWrap* w, int status) {
132
#ifdef DEBUG
133
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
134
#endif
135
  listener_->OnStreamAfterWrite(w, status);
136
}
137
138
inline void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) {
139
#ifdef DEBUG
140
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
141
#endif
142
  listener_->OnStreamAfterShutdown(w, status);
143
}
144
145
inline void StreamResource::EmitWantsWrite(size_t suggested_size) {
146
#ifdef DEBUG
147
  v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent());
148
#endif
149
  listener_->OnStreamWantsWrite(suggested_size);
150
}
151
152
704
inline StreamBase::StreamBase(Environment* env) : env_(env) {
153
704
  PushStreamListener(&default_listener_);
154
704
}
155
156
1020
inline Environment* StreamBase::stream_env() const {
157
1020
  return env_;
158
}
159
160
inline int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
161
  Environment* env = stream_env();
162
163
  HandleScope handle_scope(env->isolate());
164
165
  if (req_wrap_obj.IsEmpty()) {
166
    req_wrap_obj =
167
        env->shutdown_wrap_template()
168
            ->NewInstance(env->context()).ToLocalChecked();
169
    StreamReq::ResetObject(req_wrap_obj);
170
  }
171
172
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
173
  ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj);
174
  int err = DoShutdown(req_wrap);
175
176
  if (err != 0) {
177
    req_wrap->Dispose();
178
  }
179
180
  const char* msg = Error();
181
  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
  return err;
189
}
190
191
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
  Environment* env = stream_env();
197
  int err;
198
199
  size_t total_bytes = 0;
200
  for (size_t i = 0; i < count; ++i)
201
    total_bytes += bufs[i].len;
202
  bytes_written_ += total_bytes;
203
204
  if (send_handle == nullptr) {
205
    err = DoTryWrite(&bufs, &count);
206
    if (err != 0 || count == 0) {
207
      return StreamWriteResult { false, err, nullptr, total_bytes };
208
    }
209
  }
210
211
  HandleScope handle_scope(env->isolate());
212
213
  if (req_wrap_obj.IsEmpty()) {
214
    req_wrap_obj =
215
        env->write_wrap_template()
216
            ->NewInstance(env->context()).ToLocalChecked();
217
    StreamReq::ResetObject(req_wrap_obj);
218
  }
219
220
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap());
221
  WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj);
222
223
  err = DoWrite(req_wrap, bufs, count, send_handle);
224
  bool async = err == 0;
225
226
  if (!async) {
227
    req_wrap->Dispose();
228
    req_wrap = nullptr;
229
  }
230
231
  const char* msg = Error();
232
  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
  return StreamWriteResult { async, err, req_wrap, total_bytes };
240
}
241
242
template <typename OtherBase>
243
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
              AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
250
}
251
252
inline ShutdownWrap* StreamBase::CreateShutdownWrap(
253
    v8::Local<v8::Object> object) {
254
  return new SimpleShutdownWrap<AsyncWrap>(this, object);
255
}
256
257
template <typename OtherBase>
258
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
              AsyncWrap::PROVIDER_WRITEWRAP) {
265
}
266
267
inline WriteWrap* StreamBase::CreateWriteWrap(
268
    v8::Local<v8::Object> object) {
269
  return new SimpleWriteWrap<AsyncWrap>(this, object);
270
}
271
272
template <class Base>
273
330
void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
274
330
  HandleScope scope(env->isolate());
275
276
  enum PropertyAttribute attributes =
277
      static_cast<PropertyAttribute>(
278
330
          v8::ReadOnly | v8::DontDelete | v8::DontEnum);
279
280
330
  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
330
                               v8::SideEffectType::kHasNoSideEffect);
287
288
  Local<FunctionTemplate> get_external_templ =
289
      env->NewFunctionTemplate(GetExternal<Base>,
290
                               signature,
291
                               v8::ConstructorBehavior::kThrow,
292
330
                               v8::SideEffectType::kHasNoSideEffect);
293
294
  Local<FunctionTemplate> get_bytes_read_templ =
295
      env->NewFunctionTemplate(GetBytesRead<Base>,
296
                               signature,
297
                               v8::ConstructorBehavior::kThrow,
298
330
                               v8::SideEffectType::kHasNoSideEffect);
299
300
  Local<FunctionTemplate> get_bytes_written_templ =
301
      env->NewFunctionTemplate(GetBytesWritten<Base>,
302
                               signature,
303
                               v8::ConstructorBehavior::kThrow,
304
330
                               v8::SideEffectType::kHasNoSideEffect);
305
306
660
  t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(),
307
                                              get_fd_templ,
308
                                              Local<FunctionTemplate>(),
309
1320
                                              attributes);
310
311
660
  t->PrototypeTemplate()->SetAccessorProperty(env->external_stream_string(),
312
                                              get_external_templ,
313
                                              Local<FunctionTemplate>(),
314
1320
                                              attributes);
315
316
660
  t->PrototypeTemplate()->SetAccessorProperty(env->bytes_read_string(),
317
                                              get_bytes_read_templ,
318
                                              Local<FunctionTemplate>(),
319
1320
                                              attributes);
320
321
660
  t->PrototypeTemplate()->SetAccessorProperty(env->bytes_written_string(),
322
                                              get_bytes_written_templ,
323
                                              Local<FunctionTemplate>(),
324
1320
                                              attributes);
325
326
330
  env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStartJS>);
327
330
  env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStopJS>);
328
330
  env->SetProtoMethod(t, "shutdown", JSMethod<Base, &StreamBase::Shutdown>);
329
330
  env->SetProtoMethod(t, "writev", JSMethod<Base, &StreamBase::Writev>);
330
330
  env->SetProtoMethod(t,
331
                      "writeBuffer",
332
                      JSMethod<Base, &StreamBase::WriteBuffer>);
333
330
  env->SetProtoMethod(t,
334
                      "writeAsciiString",
335
                      JSMethod<Base, &StreamBase::WriteString<ASCII> >);
336
330
  env->SetProtoMethod(t,
337
                      "writeUtf8String",
338
                      JSMethod<Base, &StreamBase::WriteString<UTF8> >);
339
330
  env->SetProtoMethod(t,
340
                      "writeUcs2String",
341
                      JSMethod<Base, &StreamBase::WriteString<UCS2> >);
342
330
  env->SetProtoMethod(t,
343
                      "writeLatin1String",
344
330
                      JSMethod<Base, &StreamBase::WriteString<LATIN1> >);
345
330
}
346
347
348
template <class Base>
349
18
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {
350
  // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
351
  Base* handle;
352
18
  ASSIGN_OR_RETURN_UNWRAP(&handle,
353
                          args.This(),
354
                          args.GetReturnValue().Set(UV_EINVAL));
355
356
18
  StreamBase* wrap = static_cast<StreamBase*>(handle);
357
18
  if (!wrap->IsAlive())
358
    return args.GetReturnValue().Set(UV_EINVAL);
359
360
54
  args.GetReturnValue().Set(wrap->GetFD());
361
}
362
363
template <class Base>
364
488
void StreamBase::GetBytesRead(const FunctionCallbackInfo<Value>& args) {
365
  Base* handle;
366
976
  ASSIGN_OR_RETURN_UNWRAP(&handle,
367
                          args.This(),
368
                          args.GetReturnValue().Set(0));
369
370
488
  StreamBase* wrap = static_cast<StreamBase*>(handle);
371
  // uint64_t -> double. 53bits is enough for all real cases.
372
1464
  args.GetReturnValue().Set(static_cast<double>(wrap->bytes_read_));
373
}
374
375
template <class Base>
376
488
void StreamBase::GetBytesWritten(const FunctionCallbackInfo<Value>& args) {
377
  Base* handle;
378
976
  ASSIGN_OR_RETURN_UNWRAP(&handle,
379
                          args.This(),
380
                          args.GetReturnValue().Set(0));
381
382
488
  StreamBase* wrap = static_cast<StreamBase*>(handle);
383
  // uint64_t -> double. 53bits is enough for all real cases.
384
1464
  args.GetReturnValue().Set(static_cast<double>(wrap->bytes_written_));
385
}
386
387
template <class Base>
388
void StreamBase::GetExternal(const FunctionCallbackInfo<Value>& args) {
389
  Base* handle;
390
  ASSIGN_OR_RETURN_UNWRAP(&handle, args.This());
391
392
  StreamBase* wrap = static_cast<StreamBase*>(handle);
393
  Local<External> ext = External::New(args.GetIsolate(), wrap);
394
  args.GetReturnValue().Set(ext);
395
}
396
397
398
template <class Base,
399
          int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
400
1211
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
401
  Base* handle;
402




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




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




1211
  if (!wrap->IsAlive())
406
    return args.GetReturnValue().Set(UV_EINVAL);
407
408
1211
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(handle);
409
3633
  args.GetReturnValue().Set((wrap->*Method)(args));
410
}
411
412
413
inline void ShutdownWrap::OnDone(int status) {
414
  stream()->EmitAfterShutdown(this, status);
415
  Dispose();
416
}
417
418
inline void WriteWrap::SetAllocatedStorage(char* data, size_t size) {
419
  CHECK_NULL(storage_);
420
  storage_ = data;
421
  storage_size_ = size;
422
}
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
inline void WriteWrap::OnDone(int status) {
433
  stream()->EmitAfterWrite(this, status);
434
  Dispose();
435
}
436
437
inline void StreamReq::Done(int status, const char* error_str) {
438
  AsyncWrap* async_wrap = GetAsyncWrap();
439
  Environment* env = async_wrap->env();
440
  if (error_str != nullptr) {
441
    async_wrap->object()->Set(env->context(),
442
                              env->error_string(),
443
                              OneByteString(env->isolate(), error_str))
444
                              .FromJust();
445
  }
446
447
  OnDone(status);
448
}
449
450
864
inline void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
451
  DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
452
453
864
  obj->SetAlignedPointerInInternalField(0, nullptr);  // BaseObject field.
454
864
  obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
455
864
}
456
457
458
}  // namespace node
459
460
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
461
462
#endif  // SRC_STREAM_BASE_INL_H_