@@ -216,6 +216,10 @@ Local<Object> ObjectManager::CreateJSWrapperHelper(jint javaObjectID, const stri
216
216
return jsWrapper;
217
217
}
218
218
219
+
220
+ /* *
221
+ * Link the JavaScript object and it's java counterpart with an ID
222
+ */
219
223
void ObjectManager::Link (const Local<Object>& object, uint32_t javaObjectID, jclass clazz)
220
224
{
221
225
if (!IsJsRuntimeObject (object))
@@ -228,17 +232,19 @@ void ObjectManager::Link(const Local<Object>& object, uint32_t javaObjectID, jcl
228
232
229
233
DEBUG_WRITE (" Linking js object: %d and java instance id: %d" , object->GetIdentityHash (), javaObjectID);
230
234
231
- auto jsInstanceInfo = new JSInstanceInfo (false , javaObjectID, clazz);
235
+ auto jsInstanceInfo = new JSInstanceInfo (false /* isJavaObjWeak */ , javaObjectID, clazz);
232
236
233
237
auto objectHandle = new Persistent<Object>(isolate, object);
234
238
auto state = new ObjectWeakCallbackState (this , jsInstanceInfo, objectHandle);
239
+
240
+ // subscribe for JS GC event
235
241
objectHandle->SetWeak (state, JSObjectWeakCallbackStatic);
236
242
237
243
auto jsInfoIdx = static_cast <int >(MetadataNodeKeys::JsInfo);
238
- bool alreadyLinked = !object->GetInternalField (jsInfoIdx)->IsUndefined ();
239
- // TODO: fail if alreadyLinked is true?
240
244
241
245
auto jsInfo = External::New (isolate, jsInstanceInfo);
246
+
247
+ // link
242
248
object->SetInternalField (jsInfoIdx, jsInfo);
243
249
244
250
idToObject.insert (make_pair (javaObjectID, objectHandle));
@@ -289,6 +295,14 @@ void ObjectManager::JSObjectWeakCallbackStatic(const WeakCallbackData<Object, Ob
289
295
thisPtr->JSObjectWeakCallback (isolate, callbackState);
290
296
}
291
297
298
+ /*
299
+ * When JS GC happens change state of the java counterpart to mirror state of JS object and REVIVE the JS object unconditionally
300
+ * "Regular" js objects are pushed into the "regular objects" array
301
+ * "Callback" js objects (ones that have implementation object) are pushed into two "special objects" array:
302
+ * -ones called for the first time which are originally strong in java
303
+ * -ones called for the second or next time which are already weak in java too
304
+ * These objects are categorized by "regular" and "callback" and saved in different arrays for performance optimizations during GC
305
+ * */
292
306
void ObjectManager::JSObjectWeakCallback (Isolate *isolate, ObjectWeakCallbackState *callbackState)
293
307
{
294
308
DEBUG_WRITE (" JSObjectWeakCallback called" );
@@ -304,7 +318,6 @@ void ObjectManager::JSObjectWeakCallback(Isolate *isolate, ObjectWeakCallbackSta
304
318
m_visitedPOs.insert (po);
305
319
306
320
auto obj = Local<Object>::New (isolate, *po);
307
-
308
321
JSInstanceInfo *jsInstanceInfo = GetJSInstanceInfo (obj);
309
322
int javaObjectID = jsInstanceInfo->JavaObjectID ;
310
323
@@ -369,6 +382,9 @@ void ObjectManager::ReleaseJSInstance(Persistent<Object> *po, JSInstanceInfo *js
369
382
DEBUG_WRITE (" ReleaseJSObject instance disposed. id:%d" , javaObjectID);
370
383
}
371
384
385
+ /*
386
+ * The "regular" JS objects added on ObjectManager::JSObjectWeakCallback are dealt with(released) here.
387
+ * */
372
388
void ObjectManager::ReleaseRegularObjects ()
373
389
{
374
390
Isolate *isolate = Isolate::GetCurrent ();
@@ -398,6 +414,7 @@ void ObjectManager::ReleaseRegularObjects()
398
414
{
399
415
int objGcNum = gcNum->Int32Value ();
400
416
417
+ // done so we can release only java objects from this GC stack and pass all objects that will be released in parent GC stacks
401
418
isReachableFromImplementationObject = objGcNum >= numberOfGC;
402
419
}
403
420
@@ -426,6 +443,10 @@ bool ObjectManager::HasImplObject(Isolate *isolate, const Local<Object>& obj)
426
443
return hasImplObj;
427
444
}
428
445
446
+ /*
447
+ * When "MarkReachableObjects" is called V8 has marked all JS objects that can be released.
448
+ * This method builds on top of V8s marking phase, because we need to consider the JAVA counterpart objects (is it "regular" or "callback"), when marking JS ones.
449
+ * */
429
450
void ObjectManager::MarkReachableObjects (Isolate *isolate, const Local<Object>& obj)
430
451
{
431
452
stack<Local<Value> > s;
@@ -460,6 +481,9 @@ void ObjectManager::MarkReachableObjects(Isolate *isolate, const Local<Object>&
460
481
auto hasImplObject = HasImplObject (isolate, o);
461
482
if (hasImplObject)
462
483
{
484
+ // this is a special case when one "callback1" object (one we are currently traversing)
485
+ // can reach another "callback2" object (jsInfo->JavaObjectID)
486
+ // here we are leaving "callback2" object to remain strong in java
463
487
m_implObjStrong[jsInfo->JavaObjectID ] = nullptr ;
464
488
}
465
489
o->SetHiddenValue (propName, curGCNumValue);
@@ -614,10 +638,14 @@ void ObjectManager::OnGcStarted(GCType type, GCCallbackFlags flags)
614
638
m_markedForGC.push (gcInfo);
615
639
}
616
640
641
+ /*
642
+ * When GC is called we need to evaluate the situation and decide what js objects to release
643
+ * */
617
644
void ObjectManager::OnGcFinished (GCType type, GCCallbackFlags flags)
618
645
{
619
646
assert (!m_markedForGC.empty ());
620
647
648
+ // deal with all "callback" objects
621
649
auto isolate = Isolate::GetCurrent ();
622
650
for (auto weakObj : m_implObjWeak)
623
651
{
@@ -634,6 +662,7 @@ void ObjectManager::OnGcFinished(GCType type, GCCallbackFlags flags)
634
662
}
635
663
}
636
664
665
+ // deal with regular objects
637
666
ReleaseRegularObjects ();
638
667
639
668
m_markedForGC.pop ();
@@ -655,6 +684,10 @@ void ObjectManager::OnGcFinished(GCType type, GCCallbackFlags flags)
655
684
}
656
685
}
657
686
687
+ /*
688
+ * We have all the JS "regular" objects that JS has made weak and ready to by GC'd,
689
+ * so we tell java to take the JAVA objects out of strong reference so they can be collected by JAVA GC
690
+ * */
658
691
void ObjectManager::MakeRegularObjectsWeak (const set<int >& instances, DirectBuffer& inputBuff)
659
692
{
660
693
jboolean keepAsWeak = JNI_FALSE;
@@ -681,6 +714,11 @@ void ObjectManager::MakeRegularObjectsWeak(const set<int>& instances, DirectBuff
681
714
inputBuff.Reset ();
682
715
}
683
716
717
+ /*
718
+ * We have all the JS "callback" objects that JS has made weak and ready to by GC'd,
719
+ * so we tell java to take the JAVA objects out of strong, BUT KEEP THEM AS WEEK REFERENCES,
720
+ * so that if java needs to release them, it can, on a later stage.
721
+ * */
684
722
void ObjectManager::MakeImplObjectsWeak (const map<int , Persistent<Object>*>& instances, DirectBuffer& inputBuff)
685
723
{
686
724
jboolean keepAsWeak = JNI_TRUE;
@@ -714,6 +752,10 @@ void ObjectManager::MakeImplObjectsWeak(const map<int, Persistent<Object>*>& ins
714
752
inputBuff.Reset ();
715
753
}
716
754
755
+ /*
756
+ * Consult with JAVA world to check if a java object is still in kept as a strong or weak reference
757
+ * If the JAVA objects are released, we can release the their counterpart JS objects
758
+ * */
717
759
void ObjectManager::CheckWeakObjectsAreAlive (const vector<PersistentObjectIdPair>& instances, DirectBuffer& inputBuff, DirectBuffer& outputBuff)
718
760
{
719
761
for (const auto & poIdPair : instances)
0 commit comments