8
8
namespace node {
9
9
10
10
using v8::Context;
11
+ using v8::ExternalArrayType;
11
12
using v8::Function;
12
13
using v8::FunctionCallbackInfo;
13
14
using v8::Handle ;
@@ -20,25 +21,41 @@ using v8::Uint32;
20
21
using v8::V8;
21
22
using v8::Value;
22
23
24
+ #define HEAP_STATISTICS_PROPERTIES (V ) \
25
+ V (0 , total_heap_size, kTotalHeapSizeIndex ) \
26
+ V (1 , total_heap_size_executable, kTotalHeapSizeExecutableIndex ) \
27
+ V (2 , total_physical_size, kTotalPhysicalSizeIndex ) \
28
+ V (3 , used_heap_size, kUsedHeapSizeIndex ) \
29
+ V (4 , heap_size_limit, kHeapSizeLimitIndex )
30
+
31
+ #define V (a, b, c ) +1
32
+ static const size_t kHeapStatisticsBufferLength = HEAP_STATISTICS_PROPERTIES(V);
33
+ #undef V
34
+
35
+ static const ExternalArrayType kHeapStatisticsBufferType =
36
+ v8::kExternalUint32Array ;
23
37
24
38
void GetHeapStatistics (const FunctionCallbackInfo<Value>& args) {
25
- Environment* env = Environment::GetCurrent (args);
39
+ CHECK (args.Length () == 1 && args[0 ]->IsObject ());
40
+
26
41
Isolate* isolate = args.GetIsolate ();
27
42
HeapStatistics s;
28
43
isolate->GetHeapStatistics (&s);
29
- Local<Object> info = Object::New (isolate);
30
- // TODO(trevnorris): Setting many object properties in C++ is a significant
31
- // performance hit. Redo this to pass the results to JS and create/set the
32
- // properties there.
33
- #define V (name ) \
34
- info->Set (env->name ## _string (), Uint32::NewFromUnsigned (isolate, s.name ()))
35
- V (total_heap_size);
36
- V (total_heap_size_executable);
37
- V (total_physical_size);
38
- V (used_heap_size);
39
- V (heap_size_limit);
44
+ Local<Object> obj = args[0 ].As <Object>();
45
+ uint32_t * data =
46
+ static_cast <uint32_t *>(obj->GetIndexedPropertiesExternalArrayData ());
47
+
48
+ CHECK_NE (data, nullptr );
49
+ ASSERT_EQ (obj->GetIndexedPropertiesExternalArrayDataType (),
50
+ kHeapStatisticsBufferType );
51
+ ASSERT_EQ (obj->GetIndexedPropertiesExternalArrayDataLength (),
52
+ kHeapStatisticsBufferLength );
53
+
54
+ #define V (i, name, _ ) \
55
+ data[i] = static_cast <uint32_t >(s.name ());
56
+
57
+ HEAP_STATISTICS_PROPERTIES (V)
40
58
#undef V
41
- args.GetReturnValue ().Set (info);
42
59
}
43
60
44
61
@@ -54,6 +71,23 @@ void InitializeV8Bindings(Handle<Object> target,
54
71
Environment* env = Environment::GetCurrent (context);
55
72
env->SetMethod (target, " getHeapStatistics" , GetHeapStatistics);
56
73
env->SetMethod (target, " setFlagsFromString" , SetFlagsFromString);
74
+
75
+ target->Set (FIXED_ONE_BYTE_STRING (env->isolate (),
76
+ " kHeapStatisticsBufferLength" ),
77
+ Uint32::NewFromUnsigned (env->isolate (),
78
+ kHeapStatisticsBufferLength ));
79
+
80
+ target->Set (FIXED_ONE_BYTE_STRING (env->isolate (),
81
+ " kHeapStatisticsBufferType" ),
82
+ Uint32::NewFromUnsigned (env->isolate (),
83
+ kHeapStatisticsBufferType ));
84
+
85
+ #define V (i, _, name ) \
86
+ target->Set (FIXED_ONE_BYTE_STRING (env->isolate (), #name), \
87
+ Uint32::NewFromUnsigned (env->isolate (), i));
88
+
89
+ HEAP_STATISTICS_PROPERTIES (V)
90
+ #undef V
57
91
}
58
92
59
93
} // namespace node
0 commit comments