@@ -30,6 +30,7 @@ using v8::Array;
3030using v8::ArrayBuffer;
3131using v8::Context;
3232using v8::FunctionCallbackInfo;
33+ using v8::HeapCodeStatistics;
3334using v8::HeapSpaceStatistics;
3435using v8::HeapStatistics;
3536using v8::Integer;
@@ -43,6 +44,7 @@ using v8::Uint32;
4344using v8::V8;
4445using v8::Value;
4546
47+
4648#define HEAP_STATISTICS_PROPERTIES(V) \
4749 V(0, total_heap_size, kTotalHeapSizeIndex) \
4850 V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \
@@ -61,6 +63,7 @@ static const size_t kHeapStatisticsPropertiesCount =
6163 HEAP_STATISTICS_PROPERTIES(V);
6264#undef V
6365
66+
6467#define HEAP_SPACE_STATISTICS_PROPERTIES(V) \
6568 V(0, space_size, kSpaceSizeIndex) \
6669 V(1, space_used_size, kSpaceUsedSizeIndex) \
@@ -73,6 +76,16 @@ static const size_t kHeapSpaceStatisticsPropertiesCount =
7376#undef V
7477
7578
79+ #define HEAP_CODE_STATISTICS_PROPERTIES(V) \
80+ V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \
81+ V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \
82+ V(2, external_script_source_size, kExternalScriptSourceSizeIndex)
83+
84+ #define V(a, b, c) +1
85+ static const size_t kHeapCodeStatisticsPropertiesCount =
86+ HEAP_CODE_STATISTICS_PROPERTIES(V);
87+ #undef V
88+
7689void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
7790 Environment* env = Environment::GetCurrent(args);
7891 Local<Integer> result =
@@ -111,6 +124,18 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
111124}
112125
113126
127+ void UpdateHeapCodeStatisticsArrayBuffer(
128+ const FunctionCallbackInfo<Value>& args) {
129+ Environment* env = Environment::GetCurrent(args);
130+ HeapCodeStatistics s;
131+ env->isolate()->GetHeapCodeAndMetadataStatistics(&s);
132+ double* const buffer = env->heap_code_statistics_buffer();
133+ #define V(index, name, _) buffer[index] = static_cast<double>(s.name());
134+ HEAP_CODE_STATISTICS_PROPERTIES(V)
135+ #undef V
136+ }
137+
138+
114139void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
115140 CHECK(args[0]->IsString());
116141 String::Utf8Value flags(args.GetIsolate(), args[0]);
@@ -127,6 +152,7 @@ void Initialize(Local<Object> target,
127152 env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
128153 CachedDataVersionTag);
129154
155+ // Export symbols used by v8.getHeapStatistics()
130156 env->SetMethod(target,
131157 "updateHeapStatisticsArrayBuffer",
132158 UpdateHeapStatisticsArrayBuffer);
@@ -151,6 +177,35 @@ void Initialize(Local<Object> target,
151177 HEAP_STATISTICS_PROPERTIES(V)
152178#undef V
153179
180+ // Export symbols used by v8.getHeapCodeStatistics()
181+ env->SetMethod(target,
182+ "updateHeapCodeStatisticsArrayBuffer",
183+ UpdateHeapCodeStatisticsArrayBuffer);
184+
185+ env->set_heap_code_statistics_buffer(
186+ new double[kHeapCodeStatisticsPropertiesCount]);
187+
188+ const size_t heap_code_statistics_buffer_byte_length =
189+ sizeof(*env->heap_code_statistics_buffer())
190+ * kHeapCodeStatisticsPropertiesCount;
191+
192+ target->Set(env->context(),
193+ FIXED_ONE_BYTE_STRING(env->isolate(),
194+ "heapCodeStatisticsArrayBuffer"),
195+ ArrayBuffer::New(env->isolate(),
196+ env->heap_code_statistics_buffer(),
197+ heap_code_statistics_buffer_byte_length))
198+ .Check();
199+
200+ #define V(i, _, name) \
201+ target->Set(env->context(), \
202+ FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
203+ Uint32::NewFromUnsigned(env->isolate(), i)).Check();
204+
205+ HEAP_CODE_STATISTICS_PROPERTIES(V)
206+ #undef V
207+
208+ // Export symbols used by v8.getHeapSpaceStatistics()
154209 target->Set(env->context(),
155210 FIXED_ONE_BYTE_STRING(env->isolate(),
156211 "kHeapSpaceStatisticsPropertiesCount"),
@@ -205,6 +260,7 @@ void Initialize(Local<Object> target,
205260 HEAP_SPACE_STATISTICS_PROPERTIES(V)
206261#undef V
207262
263+ // Export symbols used by v8.setFlagsFromString()
208264 env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
209265}
210266
0 commit comments