@@ -22,8 +22,7 @@ ObjectManager::ObjectManager(jobject javaRuntimeObject) :
22
22
m_env(JEnv()),
23
23
m_numberOfGC(0 ),
24
24
m_currentObjectId(0 ),
25
- m_cache(NewWeakGlobalRefCallback, DeleteWeakGlobalRefCallback, 1000 , this ),
26
- m_markingMode(JavaScriptMarkingMode::Full) {
25
+ m_cache(NewWeakGlobalRefCallback, DeleteWeakGlobalRefCallback, 1000 , this ) {
27
26
28
27
auto runtimeClass = m_env.FindClass (" com/tns/Runtime" );
29
28
assert (runtimeClass != nullptr );
@@ -55,13 +54,9 @@ ObjectManager::ObjectManager(jobject javaRuntimeObject) :
55
54
auto useGlobalRefs = m_env.CallStaticBooleanMethod (runtimeClass, useGlobalRefsMethodID);
56
55
m_useGlobalRefs = useGlobalRefs == JNI_TRUE;
57
56
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);
65
60
}
66
61
67
62
void ObjectManager::SetInstanceIsolate (Isolate* isolate) {
@@ -99,32 +94,35 @@ JniLocalRef ObjectManager::GetJavaObjectByJsObject(const Local<Object>& object)
99
94
100
95
ObjectManager::JSInstanceInfo* ObjectManager::GetJSInstanceInfo (const Local<Object>& object) {
101
96
JSInstanceInfo* jsInstanceInfo = nullptr ;
97
+ if (IsJsRuntimeObject (object)) {
98
+ return GetJSInstanceInfoFromRuntimeObject (object);
99
+ }
100
+ return nullptr ;
101
+ }
102
102
103
- auto isolate = m_isolate;
104
- HandleScope handleScope (isolate );
103
+ ObjectManager::JSInstanceInfo* ObjectManager::GetJSInstanceInfoFromRuntimeObject ( const Local<Object>& object) {
104
+ HandleScope handleScope (m_isolate );
105
105
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);
118
116
}
119
117
}
118
+ }
120
119
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 ());
125
123
}
126
124
127
- return jsInstanceInfo ;
125
+ return nullptr ;
128
126
}
129
127
130
128
bool ObjectManager::IsJsRuntimeObject (const v8::Local<v8::Object>& object) {
@@ -297,41 +295,25 @@ void ObjectManager::JSObjectFinalizerStatic(const WeakCallbackInfo<ObjectWeakCal
297
295
}
298
296
299
297
void ObjectManager::JSObjectFinalizer (Isolate* isolate, ObjectWeakCallbackState* callbackState) {
298
+ HandleScope handleScope (m_isolate);
300
299
Persistent<Object>* po = callbackState->target ;
300
+ auto jsInstanceInfo = GetJSInstanceInfoFromRuntimeObject (po->Get (m_isolate));
301
301
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) {
318
303
po->Reset ();
319
304
return ;
320
305
}
321
306
322
- auto external = jsInfo.As <External>();
323
- auto jsInstanceInfo = static_cast <JSInstanceInfo *>(external->Value ());
324
307
auto javaObjectID = jsInstanceInfo->JavaObjectID ;
325
-
326
308
jboolean isJavaInstanceAlive = m_env.CallBooleanMethod (m_javaRuntimeObject, MAKE_INSTANCE_WEAK_AND_CHECK_IF_ALIVE_METHOD_ID, javaObjectID);
327
309
if (isJavaInstanceAlive) {
328
310
// If the Java instance is alive, keep the JavaScript instance alive.
329
- // TODO: Check should we really register the finalizer again?
330
311
po->SetWeak (callbackState, JSObjectFinalizerStatic, WeakCallbackType::kFinalizer );
331
312
} else {
332
313
// If the Java instance is dead, this JavaScript instance can be let die.
333
314
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));
335
317
po->Reset ();
336
318
}
337
319
}
0 commit comments