@@ -216,6 +216,10 @@ Local<Object> ObjectManager::CreateJSWrapperHelper(jint javaObjectID, const stri
216216 return jsWrapper;
217217}
218218
219+
220+ /* *
221+ * Link the JavaScript object and it's java counterpart with an ID
222+ */
219223void ObjectManager::Link (const Local<Object>& object, uint32_t javaObjectID, jclass clazz)
220224{
221225 if (!IsJsRuntimeObject (object))
@@ -228,17 +232,19 @@ void ObjectManager::Link(const Local<Object>& object, uint32_t javaObjectID, jcl
228232
229233 DEBUG_WRITE (" Linking js object: %d and java instance id: %d" , object->GetIdentityHash (), javaObjectID);
230234
231- auto jsInstanceInfo = new JSInstanceInfo (false , javaObjectID, clazz);
235+ auto jsInstanceInfo = new JSInstanceInfo (false /* isJavaObjWeak */ , javaObjectID, clazz);
232236
233237 auto objectHandle = new Persistent<Object>(isolate, object);
234238 auto state = new ObjectWeakCallbackState (this , jsInstanceInfo, objectHandle);
239+
240+ // subscribe for JS GC event
235241 objectHandle->SetWeak (state, JSObjectWeakCallbackStatic);
236242
237243 auto jsInfoIdx = static_cast <int >(MetadataNodeKeys::JsInfo);
238- bool alreadyLinked = !object->GetInternalField (jsInfoIdx)->IsUndefined ();
239- // TODO: fail if alreadyLinked is true?
240244
241245 auto jsInfo = External::New (isolate, jsInstanceInfo);
246+
247+ // link
242248 object->SetInternalField (jsInfoIdx, jsInfo);
243249
244250 idToObject.insert (make_pair (javaObjectID, objectHandle));
@@ -289,6 +295,14 @@ void ObjectManager::JSObjectWeakCallbackStatic(const WeakCallbackData<Object, Ob
289295 thisPtr->JSObjectWeakCallback (isolate, callbackState);
290296}
291297
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+ * */
292306void ObjectManager::JSObjectWeakCallback (Isolate *isolate, ObjectWeakCallbackState *callbackState)
293307{
294308 DEBUG_WRITE (" JSObjectWeakCallback called" );
@@ -304,7 +318,6 @@ void ObjectManager::JSObjectWeakCallback(Isolate *isolate, ObjectWeakCallbackSta
304318 m_visitedPOs.insert (po);
305319
306320 auto obj = Local<Object>::New (isolate, *po);
307-
308321 JSInstanceInfo *jsInstanceInfo = GetJSInstanceInfo (obj);
309322 int javaObjectID = jsInstanceInfo->JavaObjectID ;
310323
@@ -369,6 +382,9 @@ void ObjectManager::ReleaseJSInstance(Persistent<Object> *po, JSInstanceInfo *js
369382 DEBUG_WRITE (" ReleaseJSObject instance disposed. id:%d" , javaObjectID);
370383}
371384
385+ /*
386+ * The "regular" JS objects added on ObjectManager::JSObjectWeakCallback are dealt with(released) here.
387+ * */
372388void ObjectManager::ReleaseRegularObjects ()
373389{
374390 Isolate *isolate = Isolate::GetCurrent ();
@@ -398,6 +414,7 @@ void ObjectManager::ReleaseRegularObjects()
398414 {
399415 int objGcNum = gcNum->Int32Value ();
400416
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
401418 isReachableFromImplementationObject = objGcNum >= numberOfGC;
402419 }
403420
@@ -426,6 +443,10 @@ bool ObjectManager::HasImplObject(Isolate *isolate, const Local<Object>& obj)
426443 return hasImplObj;
427444}
428445
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+ * */
429450void ObjectManager::MarkReachableObjects (Isolate *isolate, const Local<Object>& obj)
430451{
431452 stack<Local<Value> > s;
@@ -460,6 +481,9 @@ void ObjectManager::MarkReachableObjects(Isolate *isolate, const Local<Object>&
460481 auto hasImplObject = HasImplObject (isolate, o);
461482 if (hasImplObject)
462483 {
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
463487 m_implObjStrong[jsInfo->JavaObjectID ] = nullptr ;
464488 }
465489 o->SetHiddenValue (propName, curGCNumValue);
@@ -614,10 +638,14 @@ void ObjectManager::OnGcStarted(GCType type, GCCallbackFlags flags)
614638 m_markedForGC.push (gcInfo);
615639}
616640
641+ /*
642+ * When GC is called we need to evaluate the situation and decide what js objects to release
643+ * */
617644void ObjectManager::OnGcFinished (GCType type, GCCallbackFlags flags)
618645{
619646 assert (!m_markedForGC.empty ());
620647
648+ // deal with all "callback" objects
621649 auto isolate = Isolate::GetCurrent ();
622650 for (auto weakObj : m_implObjWeak)
623651 {
@@ -634,6 +662,7 @@ void ObjectManager::OnGcFinished(GCType type, GCCallbackFlags flags)
634662 }
635663 }
636664
665+ // deal with regular objects
637666 ReleaseRegularObjects ();
638667
639668 m_markedForGC.pop ();
@@ -655,6 +684,10 @@ void ObjectManager::OnGcFinished(GCType type, GCCallbackFlags flags)
655684 }
656685}
657686
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+ * */
658691void ObjectManager::MakeRegularObjectsWeak (const set<int >& instances, DirectBuffer& inputBuff)
659692{
660693 jboolean keepAsWeak = JNI_FALSE;
@@ -681,6 +714,11 @@ void ObjectManager::MakeRegularObjectsWeak(const set<int>& instances, DirectBuff
681714 inputBuff.Reset ();
682715}
683716
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+ * */
684722void ObjectManager::MakeImplObjectsWeak (const map<int , Persistent<Object>*>& instances, DirectBuffer& inputBuff)
685723{
686724 jboolean keepAsWeak = JNI_TRUE;
@@ -714,6 +752,10 @@ void ObjectManager::MakeImplObjectsWeak(const map<int, Persistent<Object>*>& ins
714752 inputBuff.Reset ();
715753}
716754
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+ * */
717759void ObjectManager::CheckWeakObjectsAreAlive (const vector<PersistentObjectIdPair>& instances, DirectBuffer& inputBuff, DirectBuffer& outputBuff)
718760{
719761 for (const auto & poIdPair : instances)
0 commit comments