GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/stream_base-inl.h Lines: 51 52 98.1 %
Date: 2016-07-12 Branches: 40 74 54.1 %

Line 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
template <class Base>
27
4793
void StreamBase::AddMethods(Environment* env,
28
                            Local<FunctionTemplate> t,
29
                            int flags) {
30
9586
  HandleScope scope(env->isolate());
31
32
  enum PropertyAttribute attributes =
33
4793
      static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
34
23965
  t->InstanceTemplate()->SetAccessor(env->fd_string(),
35
                                     GetFD<Base>,
36
                                     nullptr,
37
                                     env->as_external(),
38
                                     v8::DEFAULT,
39
                                     attributes);
40
41
23965
  t->InstanceTemplate()->SetAccessor(env->external_stream_string(),
42
                                     GetExternal<Base>,
43
                                     nullptr,
44
                                     env->as_external(),
45
                                     v8::DEFAULT,
46
                                     attributes);
47
48
23965
  t->InstanceTemplate()->SetAccessor(env->bytes_read_string(),
49
                                     GetBytesRead<Base>,
50
                                     nullptr,
51
                                     env->as_external(),
52
                                     v8::DEFAULT,
53
                                     attributes);
54
55
4793
  env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>);
56
4793
  env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>);
57
4793
  if ((flags & kFlagNoShutdown) == 0)
58
3282
    env->SetProtoMethod(t, "shutdown", JSMethod<Base, &StreamBase::Shutdown>);
59
4793
  if ((flags & kFlagHasWritev) != 0)
60
1781
    env->SetProtoMethod(t, "writev", JSMethod<Base, &StreamBase::Writev>);
61
4793
  env->SetProtoMethod(t,
62
                      "writeBuffer",
63
                      JSMethod<Base, &StreamBase::WriteBuffer>);
64
4793
  env->SetProtoMethod(t,
65
                      "writeAsciiString",
66
                      JSMethod<Base, &StreamBase::WriteString<ASCII> >);
67
4793
  env->SetProtoMethod(t,
68
                      "writeUtf8String",
69
                      JSMethod<Base, &StreamBase::WriteString<UTF8> >);
70
4793
  env->SetProtoMethod(t,
71
                      "writeUcs2String",
72
                      JSMethod<Base, &StreamBase::WriteString<UCS2> >);
73
4793
  env->SetProtoMethod(t,
74
                      "writeLatin1String",
75
                      JSMethod<Base, &StreamBase::WriteString<LATIN1> >);
76
4793
}
77
78
79
template <class Base>
80
23
void StreamBase::GetFD(Local<String> key,
81
                       const PropertyCallbackInfo<Value>& args) {
82
23
  Base* handle = Unwrap<Base>(args.Holder());
83
84
  // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
85
37
  ASSIGN_OR_RETURN_UNWRAP(&handle,
86
                          args.Holder(),
87
                          args.GetReturnValue().Set(UV_EINVAL));
88
89
16
  StreamBase* wrap = static_cast<StreamBase*>(handle);
90
16
  if (!wrap->IsAlive())
91
2
    return args.GetReturnValue().Set(UV_EINVAL);
92
93
45
  args.GetReturnValue().Set(wrap->GetFD());
94
}
95
96
97
template <class Base>
98
6487
void StreamBase::GetBytesRead(Local<String> key,
99
                              const PropertyCallbackInfo<Value>& args) {
100
6487
  Base* handle = Unwrap<Base>(args.Holder());
101
102
  // The handle instance hasn't been set. So no bytes could have been read.
103
6489
  ASSIGN_OR_RETURN_UNWRAP(&handle,
104
                          args.Holder(),
105
                          args.GetReturnValue().Set(0));
106
107
6486
  StreamBase* wrap = static_cast<StreamBase*>(handle);
108
  // uint64_t -> double. 53bits is enough for all real cases.
109
12972
  args.GetReturnValue().Set(static_cast<double>(wrap->bytes_read_));
110
}
111
112
113
template <class Base>
114
4147
void StreamBase::GetExternal(Local<String> key,
115
                             const PropertyCallbackInfo<Value>& args) {
116
4147
  Base* handle = Unwrap<Base>(args.Holder());
117
118
4148
  ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder());
119
120
4146
  StreamBase* wrap = static_cast<StreamBase*>(handle);
121
4146
  Local<External> ext = External::New(args.GetIsolate(), wrap);
122
8292
  args.GetReturnValue().Set(ext);
123
}
124
125
126
template <class Base,
127
          int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
128
37174
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
129
37174
  Base* handle = Unwrap<Base>(args.Holder());
130
131
37174
  ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder());
132
133
37174
  StreamBase* wrap = static_cast<StreamBase*>(handle);
134
37174
  if (!wrap->IsAlive())
135
    return args.GetReturnValue().Set(UV_EINVAL);
136
137
111522
  args.GetReturnValue().Set((wrap->*Method)(args));
138
}
139
140
141
15133
WriteWrap* WriteWrap::New(Environment* env,
142
                          Local<Object> obj,
143
                          StreamBase* wrap,
144
                          DoneCb cb,
145
                          size_t extra) {
146
15133
  size_t storage_size = ROUND_UP(sizeof(WriteWrap), kAlignSize) + extra;
147
15133
  char* storage = new char[storage_size];
148
149
30266
  return new(storage) WriteWrap(env, obj, wrap, cb, storage_size);
150
}
151
152
153
15121
void WriteWrap::Dispose() {
154
15121
  this->~WriteWrap();
155
15121
  delete[] reinterpret_cast<char*>(this);
156
15121
}
157
158
159
char* WriteWrap::Extra(size_t offset) {
160
  return reinterpret_cast<char*>(this) +
161
         ROUND_UP(sizeof(*this), kAlignSize) +
162
27089
         offset;
163
}
164
165
}  // namespace node
166
167
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
168
169
#endif  // SRC_STREAM_BASE_INL_H_