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-06-14 Branches: 43 78 55.1 %

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




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




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




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