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