forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_v8.cc
More file actions
107 lines (82 loc) · 3.16 KB
/
node_v8.cc
File metadata and controls
107 lines (82 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "node.h"
#include "env.h"
#include "env-inl.h"
#include "util.h"
#include "util-inl.h"
#include "v8.h"
namespace node {
using v8::ArrayBuffer;
using v8::Context;
using v8::ExternalArrayType;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Handle;
using v8::HeapStatistics;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Uint32;
using v8::Uint32Array;
using v8::V8;
using v8::Value;
#define HEAP_STATISTICS_PROPERTIES(V) \
V(0, total_heap_size, kTotalHeapSizeIndex) \
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \
V(2, total_physical_size, kTotalPhysicalSizeIndex) \
V(3, used_heap_size, kUsedHeapSizeIndex) \
V(4, heap_size_limit, kHeapSizeLimitIndex)
#define V(a, b, c) +1
static const size_t kHeapStatisticsBufferLength = HEAP_STATISTICS_PROPERTIES(V);
#undef V
void GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Environment* env = Environment::GetCurrent(isolate);
HeapStatistics s;
isolate->GetHeapStatistics(&s);
uint32_t* data =
static_cast<uint32_t*>(env->heap_stats_buffer());
CHECK_NE(data, nullptr);
#define V(i, name, _) \
data[i] = static_cast<uint32_t>(s.name());
HEAP_STATISTICS_PROPERTIES(V)
#undef V
}
void GetHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Environment* env = Environment::GetCurrent(isolate);
void* buffer = env->heap_stats_buffer();
if (buffer == nullptr) {
buffer = malloc(kHeapStatisticsBufferLength * sizeof(uint32_t));
env->set_heap_stats_buffer(buffer);
}
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate,
buffer, kHeapStatisticsBufferLength * sizeof(uint32_t));
Local<Uint32Array> array =
Uint32Array::New(ab, 0, kHeapStatisticsBufferLength);
args.GetReturnValue().Set(array);
}
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (args.Length() < 1)
return env->ThrowTypeError("v8 flag is required");
if (!args[0]->IsString())
return env->ThrowTypeError("v8 flag must be a string");
String::Utf8Value flags(args[0]);
V8::SetFlagsFromString(*flags, flags.length());
}
void InitializeV8Bindings(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Environment* env = Environment::GetCurrent(context);
env->SetMethod(target, "getHeapStatistics", GetHeapStatistics);
env->SetMethod(target, "getHeapStatisticsBuffer", GetHeapStatisticsBuffer);
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
#define V(i, _, name) \
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
Uint32::NewFromUnsigned(env->isolate(), i));
HEAP_STATISTICS_PROPERTIES(V)
#undef V
}
} // namespace node
NODE_MODULE_CONTEXT_AWARE_BUILTIN(v8, node::InitializeV8Bindings)