Skip to content

Commit 78c26be

Browse files
committed
readability refactor in object manager
1 parent 773a63d commit 78c26be

File tree

4 files changed

+30
-40
lines changed

4 files changed

+30
-40
lines changed

src/jni/CallbackHandlers.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "v8.h"
88
#include "v8-debug.h"
99
#include "JEnv.h"
10-
#include "JSInstanceInfo.h"
1110
#include "ArgsWrapper.h"
1211
#include "MetadataEntry.h"
1312
#include "FieldCallbackData.h"

src/jni/JSInstanceInfo.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/jni/ObjectManager.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,17 @@ JniLocalRef ObjectManager::GetJavaObjectByJsObject(const Local<Object>& object)
8282
return JniLocalRef();
8383
}
8484

85-
JSInstanceInfo* ObjectManager::GetJSInstanceInfo(const Local<Object>& object)
85+
ObjectManager::JSInstanceInfo* ObjectManager::GetJSInstanceInfo(const Local<Object>& object)
8686
{
8787
DEBUG_WRITE("ObjectManager::GetJSInstanceInfo: called");
8888
JSInstanceInfo *jsInstanceInfo = nullptr;
8989

9090
auto isolate = Isolate::GetCurrent();
9191
HandleScope handleScope(isolate);
9292

93-
int internalFieldCound = NativeScriptExtension::GetInternalFieldCount(object);
94-
const int count = static_cast<int>(MetadataNodeKeys::END);
95-
const int jsInfoIdx = static_cast<int>(MetadataNodeKeys::JsInfo);
96-
if (internalFieldCound == count)
93+
if (IsJsRuntimeObject(object))
9794
{
95+
const int jsInfoIdx = static_cast<int>(MetadataNodeKeys::JsInfo);
9896
auto jsInfo = object->GetInternalField(jsInfoIdx);
9997
if (jsInfo->IsUndefined())
10098
{
@@ -104,8 +102,7 @@ JSInstanceInfo* ObjectManager::GetJSInstanceInfo(const Local<Object>& object)
104102
if (!prototypeObject.IsEmpty() && prototypeObject->IsObject())
105103
{
106104
DEBUG_WRITE("GetJSInstanceInfo: need to check prototype :%d", prototypeObject->GetIdentityHash());
107-
internalFieldCound = NativeScriptExtension::GetInternalFieldCount(prototypeObject);
108-
if (internalFieldCound == count)
105+
if (IsJsRuntimeObject(prototypeObject))
109106
{
110107
jsInfo = prototypeObject->GetInternalField(jsInfoIdx);
111108
}
@@ -122,6 +119,12 @@ JSInstanceInfo* ObjectManager::GetJSInstanceInfo(const Local<Object>& object)
122119
return jsInstanceInfo;
123120
}
124121

122+
bool ObjectManager::IsJsRuntimeObject(const v8::Local<v8::Object>& object) {
123+
int internalFieldCount = NativeScriptExtension::GetInternalFieldCount(object);
124+
const int count = static_cast<int>(MetadataNodeKeys::END);
125+
return internalFieldCount == count;
126+
}
127+
125128
jweak ObjectManager::GetJavaObjectByID(uint32_t javaObjectID)
126129
{
127130
jweak obj = m_cache(javaObjectID);
@@ -145,7 +148,7 @@ jclass ObjectManager::GetJavaClass(const Local<Object>& instance)
145148
DEBUG_WRITE("GetClass called");
146149

147150
JSInstanceInfo *jsInfo = GetJSInstanceInfo(instance);
148-
jclass clazz = jsInfo->clazz;
151+
jclass clazz = jsInfo->ObjectClazz;
149152

150153
return clazz;
151154
}
@@ -155,7 +158,7 @@ void ObjectManager::SetJavaClass(const Local<Object>& instance, jclass clazz)
155158
DEBUG_WRITE("SetClass called");
156159

157160
JSInstanceInfo *jsInfo = GetJSInstanceInfo(instance);
158-
jsInfo->clazz = clazz;
161+
jsInfo->ObjectClazz = clazz;
159162
}
160163

161164
int ObjectManager::GetOrCreateObjectId(jobject object)
@@ -215,9 +218,7 @@ Local<Object> ObjectManager::CreateJSWrapperHelper(jint javaObjectID, const stri
215218

216219
void ObjectManager::Link(const Local<Object>& object, uint32_t javaObjectID, jclass clazz)
217220
{
218-
int internalFieldCound = NativeScriptExtension::GetInternalFieldCount(object);
219-
const int count = static_cast<int>(MetadataNodeKeys::END);
220-
if (internalFieldCound != count)
221+
if (!IsJsRuntimeObject(object))
221222
{
222223
string errMsg("Trying to link invalid 'this' to a Java object");
223224
throw NativeScriptException(errMsg);
@@ -227,9 +228,7 @@ void ObjectManager::Link(const Local<Object>& object, uint32_t javaObjectID, jcl
227228

228229
DEBUG_WRITE("Linking js object: %d and java instance id: %d", object->GetIdentityHash(), javaObjectID);
229230

230-
auto jsInstanceInfo = new JSInstanceInfo();
231-
jsInstanceInfo->JavaObjectID = javaObjectID;
232-
jsInstanceInfo->clazz = clazz;
231+
auto jsInstanceInfo = new JSInstanceInfo(false, javaObjectID, clazz);
233232

234233
auto objectHandle = new Persistent<Object>(isolate, object);
235234
auto state = new ObjectWeakCallbackState(this, jsInstanceInfo, objectHandle);

src/jni/ObjectManager.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "JEnv.h"
66
#include "JniLocalRef.h"
77
#include "ArgsWrapper.h"
8-
#include "JSInstanceInfo.h"
98
#include "DirectBuffer.h"
109
#include "LRUCache.h"
1110
#include <map>
@@ -58,6 +57,20 @@ namespace tns
5857
};
5958

6059
private:
60+
61+
struct JSInstanceInfo
62+
{
63+
public:
64+
JSInstanceInfo(bool isJavaObjectWeak, uint32_t javaObjectID, jclass claz)
65+
:IsJavaObjectWeak(isJavaObjectWeak), JavaObjectID(javaObjectID), ObjectClazz(claz)
66+
{
67+
}
68+
69+
bool IsJavaObjectWeak;
70+
uint32_t JavaObjectID;
71+
jclass ObjectClazz;
72+
};
73+
6174
struct ObjectWeakCallbackState
6275
{
6376
ObjectWeakCallbackState(ObjectManager *_thisPtr, JSInstanceInfo *_jsInfo, v8::Persistent<v8::Object> *_target)
@@ -121,6 +134,8 @@ namespace tns
121134
int javaObjectId;
122135
};
123136

137+
bool IsJsRuntimeObject(const v8::Local<v8::Object>& object);
138+
124139
JSInstanceInfo* GetJSInstanceInfo(const v8::Local<v8::Object>& object);
125140

126141
void ReleaseJSInstance(v8::Persistent<v8::Object> *po, JSInstanceInfo *jsInstanceInfo);

0 commit comments

Comments
 (0)