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: 56 56 100.0 %
Date: 2017-10-21 Branches: 46 78 59.0 %

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.h"
10
#include "env-inl.h"
11
#include "v8.h"
12
13
namespace node {
14
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
template <class Base>
29
10334
void StreamBase::AddMethods(Environment* env,
30
                            Local<FunctionTemplate> t,
31
                            int flags) {
32
10334
  HandleScope scope(env->isolate());
33
34
  enum PropertyAttribute attributes =
35
10334
      static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
36
41336
  t->InstanceTemplate()->SetAccessor(env->fd_string(),
37
                                     GetFD<Base>,
38
                                     nullptr,
39
                                     env->as_external(),
40
                                     v8::DEFAULT,
41
                                     attributes);
42
43
41336
  t->InstanceTemplate()->SetAccessor(env->external_stream_string(),
44
                                     GetExternal<Base>,
45
                                     nullptr,
46
                                     env->as_external(),
47
                                     v8::DEFAULT,
48
                                     attributes);
49
50
41336
  t->InstanceTemplate()->SetAccessor(env->bytes_read_string(),
51
                                     GetBytesRead<Base>,
52
                                     nullptr,
53
                                     env->as_external(),
54
                                     v8::DEFAULT,
55
                                     attributes);
56
57
10334
  env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>);
58
10334
  env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>);
59
10334
  if ((flags & kFlagNoShutdown) == 0)
60
6995
    env->SetProtoMethod(t, "shutdown", JSMethod<Base, &StreamBase::Shutdown>);
61
10334
  if ((flags & kFlagHasWritev) != 0)
62
7134
    env->SetProtoMethod(t, "writev", JSMethod<Base, &StreamBase::Writev>);
63
10334
  env->SetProtoMethod(t,
64
                      "writeBuffer",
65
                      JSMethod<Base, &StreamBase::WriteBuffer>);
66
10334
  env->SetProtoMethod(t,
67
                      "writeAsciiString",
68
                      JSMethod<Base, &StreamBase::WriteString<ASCII> >);
69
10334
  env->SetProtoMethod(t,
70
                      "writeUtf8String",
71
                      JSMethod<Base, &StreamBase::WriteString<UTF8> >);
72
10334
  env->SetProtoMethod(t,
73
                      "writeUcs2String",
74
                      JSMethod<Base, &StreamBase::WriteString<UCS2> >);
75
10334
  env->SetProtoMethod(t,
76
                      "writeLatin1String",
77
10334
                      JSMethod<Base, &StreamBase::WriteString<LATIN1> >);
78
10334
}
79
80
81
template <class Base>
82
108
void StreamBase::GetFD(Local<String> key,
83
                       const PropertyCallbackInfo<Value>& args) {
84
108
  Base* handle = Unwrap<Base>(args.Holder());
85
86
  // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
87
116
  ASSIGN_OR_RETURN_UNWRAP(&handle,
88
                          args.Holder(),
89
                          args.GetReturnValue().Set(UV_EINVAL));
90
91
106
  StreamBase* wrap = static_cast<StreamBase*>(handle);
92
106
  if (!wrap->IsAlive())
93
4
    return args.GetReturnValue().Set(UV_EINVAL);
94
95
312
  args.GetReturnValue().Set(wrap->GetFD());
96
}
97
98
99
template <class Base>
100
7136
void StreamBase::GetBytesRead(Local<String> key,
101
                              const PropertyCallbackInfo<Value>& args) {
102
7136
  Base* handle = Unwrap<Base>(args.Holder());
103
104
  // The handle instance hasn't been set. So no bytes could have been read.
105
14278
  ASSIGN_OR_RETURN_UNWRAP(&handle,
106
                          args.Holder(),
107
                          args.GetReturnValue().Set(0));
108
109
7133
  StreamBase* wrap = static_cast<StreamBase*>(handle);
110
  // uint64_t -> double. 53bits is enough for all real cases.
111
21399
  args.GetReturnValue().Set(static_cast<double>(wrap->bytes_read_));
112
}
113
114
115
template <class Base>
116
3785
void StreamBase::GetExternal(Local<String> key,
117
                             const PropertyCallbackInfo<Value>& args) {
118
3785
  Base* handle = Unwrap<Base>(args.Holder());
119
120
7570
  ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder());
121
122
3783
  StreamBase* wrap = static_cast<StreamBase*>(handle);
123
3783
  Local<External> ext = External::New(args.GetIsolate(), wrap);
124
7566
  args.GetReturnValue().Set(ext);
125
}
126
127
128
template <class Base,
129
          int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
130
77050
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
131
77050
  Base* handle = Unwrap<Base>(args.Holder());
132
133




77051
  ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder());
134
135




77050
  StreamBase* wrap = static_cast<StreamBase*>(handle);
136




77050
  if (!wrap->IsAlive())
137
2
    return args.GetReturnValue().Set(UV_EINVAL);
138
139
77049
  AsyncHooks::InitScope init_scope(handle->env(), handle->get_async_id());
140
231147
  args.GetReturnValue().Set((wrap->*Method)(args));
141
}
142
143
144
17810
WriteWrap* WriteWrap::New(Environment* env,
145
                          Local<Object> obj,
146
                          StreamBase* wrap,
147
                          DoneCb cb,
148
                          size_t extra) {
149
17810
  size_t storage_size = ROUND_UP(sizeof(WriteWrap), kAlignSize) + extra;
150
17810
  char* storage = new char[storage_size];
151
152
17810
  return new(storage) WriteWrap(env, obj, wrap, cb, storage_size);
153
}
154
155
156
17797
void WriteWrap::Dispose() {
157
17797
  this->~WriteWrap();
158
17797
  delete[] reinterpret_cast<char*>(this);
159
17797
}
160
161
162
31314
char* WriteWrap::Extra(size_t offset) {
163
  return reinterpret_cast<char*>(this) +
164
62628
         ROUND_UP(sizeof(*this), kAlignSize) +
165
62628
         offset;
166
}
167
168
}  // namespace node
169
170
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
171
172
#endif  // SRC_STREAM_BASE_INL_H_