Skip to content

Commit a2b5ae5

Browse files
authored
Add V8 GC hook only on markSweepCompact phase (#1486)
Add V8 GC hook only on markSweepCompact phase * Remove forgotten commented line
1 parent 37dfe05 commit a2b5ae5

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

test-app/runtime/src/main/cpp/ObjectManager.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ void ObjectManager::Init(Isolate *isolate) {
8585
m_poJsWrapperFunc = new Persistent<Function>(isolate, jsWrapperFunc);
8686

8787
if (m_markingMode != JavaScriptMarkingMode::None) {
88-
isolate->AddGCPrologueCallback(ObjectManager::OnGcStartedStatic, kGCTypeAll);
89-
isolate->AddGCEpilogueCallback(ObjectManager::OnGcFinishedStatic, kGCTypeAll);
88+
isolate->AddGCPrologueCallback(ObjectManager::OnGcStartedStatic, kGCTypeMarkSweepCompact);
89+
isolate->AddGCEpilogueCallback(ObjectManager::OnGcFinishedStatic, kGCTypeMarkSweepCompact);
9090
}
9191
}
9292

@@ -380,9 +380,18 @@ void ObjectManager::JSObjectWeakCallback(Isolate *isolate, ObjectWeakCallbackSta
380380
jsInstanceInfo->IsJavaObjectWeak = true;
381381
}
382382
} else {
383-
assert(!m_markedForGC.empty());
384-
auto &topGCInfo = m_markedForGC.top();
385-
topGCInfo.markedForGC.push_back(po);
383+
if(m_markedForGC.empty()){
384+
// Emulates the behavior in the OnGcStarted callback. Тhis is necessary as the hooking
385+
// to the V8 GC is done only on the markSweepCompact phase. The JSObjectWeakCallback
386+
// however is still triggered in other V8 GC phases (scavenger for example). This
387+
// creates a problem that there is no 'top' on the m_markedForGC stack.
388+
GarbageCollectionInfo gcInfo(++m_numberOfGC);
389+
gcInfo.markedForGC.push_back(po);
390+
m_markedForGC.push(gcInfo);
391+
} else {
392+
auto &topGCInfo = m_markedForGC.top();
393+
topGCInfo.markedForGC.push_back(po);
394+
}
386395
}
387396
}
388397
}
@@ -634,9 +643,15 @@ void ObjectManager::MarkReachableArrayElements(Local<Object> &o, stack<Local<Val
634643

635644
void ObjectManager::OnGcStartedStatic(Isolate *isolate, GCType type, GCCallbackFlags flags) {
636645
try {
646+
tns::instrumentation::Frame frame;
647+
637648
auto runtime = Runtime::GetRuntime(isolate);
638649
auto objectManager = runtime->GetObjectManager();
639650
objectManager->OnGcStarted(type, flags);
651+
652+
if(frame.check()){
653+
frame.log("OnGcStartedStatic");
654+
}
640655
} catch (NativeScriptException &e) {
641656
e.ReThrowToV8();
642657
} catch (std::exception e) {
@@ -652,9 +667,15 @@ void ObjectManager::OnGcStartedStatic(Isolate *isolate, GCType type, GCCallbackF
652667

653668
void ObjectManager::OnGcFinishedStatic(Isolate *isolate, GCType type, GCCallbackFlags flags) {
654669
try {
670+
tns::instrumentation::Frame frame;
671+
655672
auto runtime = Runtime::GetRuntime(isolate);
656673
auto objectManager = runtime->GetObjectManager();
657674
objectManager->OnGcFinished(type, flags);
675+
676+
if(frame.check()){
677+
frame.log("OnGcFinishedStatic");
678+
}
658679
} catch (NativeScriptException &e) {
659680
e.ReThrowToV8();
660681
} catch (std::exception e) {

test-app/runtime/src/main/cpp/ObjectManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class ObjectManager {
7777
None
7878
};
7979

80+
8081
private:
8182

8283
struct JSInstanceInfo {
@@ -165,6 +166,8 @@ class ObjectManager {
165166

166167
static void JSObjectFinalizerStatic(const v8::WeakCallbackInfo<ObjectWeakCallbackState>& data);
167168

169+
170+
168171
void JSObjectWeakCallback(v8::Isolate* isolate, ObjectWeakCallbackState* callbackState);
169172

170173
void JSObjectFinalizer(v8::Isolate* isolate, ObjectWeakCallbackState* callbackState);

0 commit comments

Comments
 (0)