@@ -22,8 +22,7 @@ ObjectManager::ObjectManager(jobject javaRuntimeObject) :
2222 m_env(JEnv()),
2323 m_numberOfGC(0 ),
2424 m_currentObjectId(0 ),
25- m_cache(NewWeakGlobalRefCallback, DeleteWeakGlobalRefCallback, 1000 , this ),
26- m_markingMode(JavaScriptMarkingMode::Full) {
25+ m_cache(NewWeakGlobalRefCallback, DeleteWeakGlobalRefCallback, 1000 , this ) {
2726
2827 auto runtimeClass = m_env.FindClass (" com/tns/Runtime" );
2928 assert (runtimeClass != nullptr );
@@ -55,13 +54,9 @@ ObjectManager::ObjectManager(jobject javaRuntimeObject) :
5554 auto useGlobalRefs = m_env.CallStaticBooleanMethod (runtimeClass, useGlobalRefsMethodID);
5655 m_useGlobalRefs = useGlobalRefs == JNI_TRUE;
5756
58- auto getMarkingModeMethodID = m_env.GetMethodID (runtimeClass, " getMarkingMode" , " ()I" );
59- jint markingMode = m_env.CallIntMethod (m_javaRuntimeObject, getMarkingModeMethodID);
60- switch (markingMode) {
61- case 1 :
62- m_markingMode = JavaScriptMarkingMode::None;
63- break ;
64- }
57+ auto getMarkingModeOrdinalMethodID = m_env.GetMethodID (runtimeClass, " getMarkingModeOrdinal" , " ()I" );
58+ jint markingMode = m_env.CallIntMethod (m_javaRuntimeObject, getMarkingModeOrdinalMethodID);
59+ m_markingMode = static_cast <JavaScriptMarkingMode>(markingMode);
6560}
6661
6762void ObjectManager::SetInstanceIsolate (Isolate* isolate) {
@@ -99,32 +94,35 @@ JniLocalRef ObjectManager::GetJavaObjectByJsObject(const Local<Object>& object)
9994
10095ObjectManager::JSInstanceInfo* ObjectManager::GetJSInstanceInfo (const Local<Object>& object) {
10196 JSInstanceInfo* jsInstanceInfo = nullptr ;
97+ if (IsJsRuntimeObject (object)) {
98+ return GetJSInstanceInfoFromRuntimeObject (object);
99+ }
100+ return nullptr ;
101+ }
102102
103- auto isolate = m_isolate;
104- HandleScope handleScope (isolate );
103+ ObjectManager::JSInstanceInfo* ObjectManager::GetJSInstanceInfoFromRuntimeObject ( const Local<Object>& object) {
104+ HandleScope handleScope (m_isolate );
105105
106- if (IsJsRuntimeObject (object)) {
107- const int jsInfoIdx = static_cast <int >(MetadataNodeKeys::JsInfo);
108- auto jsInfo = object->GetInternalField (jsInfoIdx);
109- if (jsInfo->IsUndefined ()) {
110- // Typescript object layout has an object instance as child of the actual registered instance. checking for that
111- auto prototypeObject = object->GetPrototype ().As <Object>();
112-
113- if (!prototypeObject.IsEmpty () && prototypeObject->IsObject ()) {
114- DEBUG_WRITE (" GetJSInstanceInfo: need to check prototype :%d" , prototypeObject->GetIdentityHash ());
115- if (IsJsRuntimeObject (prototypeObject)) {
116- jsInfo = prototypeObject->GetInternalField (jsInfoIdx);
117- }
106+ const int jsInfoIdx = static_cast <int >(MetadataNodeKeys::JsInfo);
107+ auto jsInfo = object->GetInternalField (jsInfoIdx);
108+ if (jsInfo->IsUndefined ()) {
109+ // Typescript object layout has an object instance as child of the actual registered instance. checking for that
110+ auto prototypeObject = object->GetPrototype ().As <Object>();
111+
112+ if (!prototypeObject.IsEmpty () && prototypeObject->IsObject ()) {
113+ DEBUG_WRITE (" GetJSInstanceInfo: need to check prototype :%d" , prototypeObject->GetIdentityHash ());
114+ if (IsJsRuntimeObject (prototypeObject)) {
115+ jsInfo = prototypeObject->GetInternalField (jsInfoIdx);
118116 }
119117 }
118+ }
120119
121- if (!jsInfo.IsEmpty () && jsInfo->IsExternal ()) {
122- auto external = jsInfo.As <External>();
123- jsInstanceInfo = static_cast <JSInstanceInfo*>(external->Value ());
124- }
120+ if (!jsInfo.IsEmpty () && jsInfo->IsExternal ()) {
121+ auto external = jsInfo.As <External>();
122+ return static_cast <JSInstanceInfo*>(external->Value ());
125123 }
126124
127- return jsInstanceInfo ;
125+ return nullptr ;
128126}
129127
130128bool ObjectManager::IsJsRuntimeObject (const v8::Local<v8::Object>& object) {
@@ -297,41 +295,25 @@ void ObjectManager::JSObjectFinalizerStatic(const WeakCallbackInfo<ObjectWeakCal
297295}
298296
299297void ObjectManager::JSObjectFinalizer (Isolate* isolate, ObjectWeakCallbackState* callbackState) {
298+ HandleScope handleScope (m_isolate);
300299 Persistent<Object>* po = callbackState->target ;
300+ auto jsInstanceInfo = GetJSInstanceInfoFromRuntimeObject (po->Get (m_isolate));
301301
302- auto jsInfoIdx = static_cast <int >(MetadataNodeKeys::JsInfo);
303- auto jsInstance = po->Get (m_isolate);
304- auto jsInfo = jsInstance->GetInternalField (jsInfoIdx);
305- if (jsInfo->IsUndefined ()) {
306- // Typescript object layout has an object instance as child of the actual registered instance. checking for that
307- auto prototypeObject = jsInstance->GetPrototype ().As <Object>();
308- if (!prototypeObject.IsEmpty () && prototypeObject->IsObject ()) {
309- DEBUG_WRITE (" GetJSInstanceInfo: need to check prototype :%d" , prototypeObject->GetIdentityHash ());
310- if (IsJsRuntimeObject (prototypeObject)) {
311- jsInfo = prototypeObject->GetInternalField (jsInfoIdx);
312- }
313- }
314- }
315-
316- if (jsInfo.IsEmpty () || !jsInfo->IsExternal ()) {
317- // The JavaScript instance has been forcefully disconnected from the Java instance.
302+ if (!jsInstanceInfo) {
318303 po->Reset ();
319304 return ;
320305 }
321306
322- auto external = jsInfo.As <External>();
323- auto jsInstanceInfo = static_cast <JSInstanceInfo *>(external->Value ());
324307 auto javaObjectID = jsInstanceInfo->JavaObjectID ;
325-
326308 jboolean isJavaInstanceAlive = m_env.CallBooleanMethod (m_javaRuntimeObject, MAKE_INSTANCE_WEAK_AND_CHECK_IF_ALIVE_METHOD_ID, javaObjectID);
327309 if (isJavaInstanceAlive) {
328310 // If the Java instance is alive, keep the JavaScript instance alive.
329- // TODO: Check should we really register the finalizer again?
330311 po->SetWeak (callbackState, JSObjectFinalizerStatic, WeakCallbackType::kFinalizer );
331312 } else {
332313 // If the Java instance is dead, this JavaScript instance can be let die.
333314 delete jsInstanceInfo;
334- jsInstance->SetInternalField (jsInfoIdx, Undefined (m_isolate));
315+ auto jsInfoIdx = static_cast <int >(MetadataNodeKeys::JsInfo);
316+ po->Get (m_isolate)->SetInternalField (jsInfoIdx, Undefined (m_isolate));
335317 po->Reset ();
336318 }
337319}
0 commit comments