GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/node_v8.cc Lines: 55 55 100.0 %
Date: 2016-09-05 Branches: 9 10 90.0 %

Line Branch Exec Source
1
#include "node.h"
2
#include "env.h"
3
#include "env-inl.h"
4
#include "util.h"
5
#include "util-inl.h"
6
#include "v8.h"
7
8
namespace node {
9
10
using v8::Array;
11
using v8::ArrayBuffer;
12
using v8::Context;
13
using v8::FunctionCallbackInfo;
14
using v8::HeapSpaceStatistics;
15
using v8::HeapStatistics;
16
using v8::Isolate;
17
using v8::Local;
18
using v8::NewStringType;
19
using v8::Object;
20
using v8::String;
21
using v8::Uint32;
22
using v8::V8;
23
using v8::Value;
24
25
#define HEAP_STATISTICS_PROPERTIES(V)                                         \
26
  V(0, total_heap_size, kTotalHeapSizeIndex)                                  \
27
  V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex)             \
28
  V(2, total_physical_size, kTotalPhysicalSizeIndex)                          \
29
  V(3, total_available_size, kTotalAvailableSize)                             \
30
  V(4, used_heap_size, kUsedHeapSizeIndex)                                    \
31
  V(5, heap_size_limit, kHeapSizeLimitIndex)
32
33
#define V(a, b, c) +1
34
static const size_t kHeapStatisticsPropertiesCount =
35
    HEAP_STATISTICS_PROPERTIES(V);
36
#undef V
37
38
#define HEAP_SPACE_STATISTICS_PROPERTIES(V)                                   \
39
  V(0, space_size, kSpaceSizeIndex)                                           \
40
  V(1, space_used_size, kSpaceUsedSizeIndex)                                  \
41
  V(2, space_available_size, kSpaceAvailableSizeIndex)                        \
42
  V(3, physical_space_size, kPhysicalSpaceSizeIndex)
43
44
#define V(a, b, c) +1
45
static const size_t kHeapSpaceStatisticsPropertiesCount =
46
    HEAP_SPACE_STATISTICS_PROPERTIES(V);
47
#undef V
48
49
// Will be populated in InitializeV8Bindings.
50
static size_t number_of_heap_spaces = 0;
51
52
53
1
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
54
1
  Environment* env = Environment::GetCurrent(args);
55
1
  HeapStatistics s;
56
1
  env->isolate()->GetHeapStatistics(&s);
57
2
  uint32_t* const buffer = env->heap_statistics_buffer();
58
#define V(index, name, _) buffer[index] = static_cast<uint32_t>(s.name());
59
1
  HEAP_STATISTICS_PROPERTIES(V)
60
#undef V
61
1
}
62
63
64
1
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
65
1
  Environment* env = Environment::GetCurrent(args);
66
1
  HeapSpaceStatistics s;
67
1
  Isolate* const isolate = env->isolate();
68
1
  uint32_t* buffer = env->heap_space_statistics_buffer();
69
70
11
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
71
5
    isolate->GetHeapSpaceStatistics(&s, i);
72
5
    size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
73
#define V(index, name, _) buffer[property_offset + index] = \
74
                              static_cast<uint32_t>(s.name());
75
5
      HEAP_SPACE_STATISTICS_PROPERTIES(V)
76
#undef V
77
  }
78
1
}
79
80
81
4
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
82
4
  Environment* env = Environment::GetCurrent(args);
83
84
4
  if (args.Length() < 1)
85
2
    return env->ThrowTypeError("v8 flag is required");
86
6
  if (!args[0]->IsString())
87
    return env->ThrowTypeError("v8 flag must be a string");
88
89
4
  String::Utf8Value flags(args[0]);
90
2
  V8::SetFlagsFromString(*flags, flags.length());
91
}
92
93
94
3
void InitializeV8Bindings(Local<Object> target,
95
                          Local<Value> unused,
96
                          Local<Context> context) {
97
3
  Environment* env = Environment::GetCurrent(context);
98
99
  env->SetMethod(target,
100
                 "updateHeapStatisticsArrayBuffer",
101
3
                 UpdateHeapStatisticsArrayBuffer);
102
103
6
  env->set_heap_statistics_buffer(new uint32_t[kHeapStatisticsPropertiesCount]);
104
105
  const size_t heap_statistics_buffer_byte_length =
106
3
      sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
107
108
12
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
109
                                    "heapStatisticsArrayBuffer"),
110
              ArrayBuffer::New(env->isolate(),
111
                               env->heap_statistics_buffer(),
112
3
                               heap_statistics_buffer_byte_length));
113
114
#define V(i, _, name)                                                         \
115
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
116
              Uint32::NewFromUnsigned(env->isolate(), i));
117
118
39
  HEAP_STATISTICS_PROPERTIES(V)
119
#undef V
120
121
12
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
122
                                    "kHeapSpaceStatisticsPropertiesCount"),
123
              Uint32::NewFromUnsigned(env->isolate(),
124
3
                                      kHeapSpaceStatisticsPropertiesCount));
125
126
3
  number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
127
128
  // Heap space names are extracted once and exposed to JavaScript to
129
  // avoid excessive creation of heap space name Strings.
130
3
  HeapSpaceStatistics s;
131
  const Local<Array> heap_spaces = Array::New(env->isolate(),
132
3
                                              number_of_heap_spaces);
133
18
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
134
15
    env->isolate()->GetHeapSpaceStatistics(&s, i);
135
30
    Local<String> heap_space_name = String::NewFromUtf8(env->isolate(),
136
                                                        s.space_name(),
137
15
                                                        NewStringType::kNormal)
138
15
                                        .ToLocalChecked();
139
30
    heap_spaces->Set(i, heap_space_name);
140
  }
141
9
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
142
3
              heap_spaces);
143
144
  env->SetMethod(target,
145
                 "updateHeapSpaceStatisticsArrayBuffer",
146
3
                 UpdateHeapSpaceStatisticsBuffer);
147
148
  env->set_heap_space_statistics_buffer(
149
6
    new uint32_t[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
150
151
  const size_t heap_space_statistics_buffer_byte_length =
152
      sizeof(*env->heap_space_statistics_buffer()) *
153
3
      kHeapSpaceStatisticsPropertiesCount *
154
3
      number_of_heap_spaces;
155
156
12
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
157
                                    "heapSpaceStatisticsArrayBuffer"),
158
              ArrayBuffer::New(env->isolate(),
159
                               env->heap_space_statistics_buffer(),
160
3
                               heap_space_statistics_buffer_byte_length));
161
162
#define V(i, _, name)                                                         \
163
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
164
              Uint32::NewFromUnsigned(env->isolate(), i));
165
166
27
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
167
#undef V
168
169
3
  env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
170
3
}
171
172
}  // namespace node
173
174
1631
NODE_MODULE_CONTEXT_AWARE_BUILTIN(v8, node::InitializeV8Bindings)