Skip to content

Commit 91be80c

Browse files
author
Mihail Slavchev
committed
use (fast) global refs on JellyBean and latter
1 parent d492dd8 commit 91be80c

9 files changed

Lines changed: 51 additions & 22 deletions

src/jni/JniLocalRef.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@ using namespace v8;
66
using namespace tns;
77

88
JniLocalRef::JniLocalRef()
9-
:
10-
m_obj(nullptr)
9+
: m_obj(nullptr), m_isGlobal(false)
1110
{
1211
}
1312

14-
JniLocalRef::JniLocalRef(jobject obj)
15-
:
16-
m_obj(obj)
13+
JniLocalRef::JniLocalRef(jobject obj, bool isGlobal)
14+
: m_obj(obj), m_isGlobal(isGlobal)
1715
{
1816
}
1917

2018
JniLocalRef::JniLocalRef(jclass obj)
21-
:
22-
m_obj(obj)
19+
: m_obj(obj), m_isGlobal(false)
2320
{
2421
}
2522

2623
JniLocalRef::JniLocalRef(JniLocalRef&& rhs)
27-
: m_obj(rhs.m_obj)
24+
: m_obj(rhs.m_obj), m_isGlobal(rhs.m_isGlobal)
2825
{
2926
rhs.m_obj = nullptr;
3027
}
@@ -34,6 +31,11 @@ bool JniLocalRef::IsNull() const
3431
return m_obj == nullptr;
3532
}
3633

34+
bool JniLocalRef::IsGlobal() const
35+
{
36+
return m_isGlobal;
37+
}
38+
3739
jobject JniLocalRef::Move()
3840
{
3941
auto value = m_obj;
@@ -44,6 +46,7 @@ jobject JniLocalRef::Move()
4446
JniLocalRef& JniLocalRef::operator=(JniLocalRef&& rhs)
4547
{
4648
m_obj = rhs.m_obj;
49+
m_isGlobal = rhs.m_isGlobal;
4750
rhs.m_obj = nullptr;
4851
return *this;
4952
}
@@ -126,7 +129,7 @@ JniLocalRef::operator jobjectArray() const
126129

127130
JniLocalRef::~JniLocalRef()
128131
{
129-
if (m_obj != nullptr)
132+
if ((m_obj != nullptr) && !m_isGlobal)
130133
{
131134
JEnv env;
132135
env.DeleteLocalRef(m_obj);

src/jni/JniLocalRef.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace tns
1111
public:
1212
JniLocalRef();
1313

14-
JniLocalRef(jobject obj);
14+
JniLocalRef(jobject obj, bool isGlobal = false);
1515

1616
JniLocalRef(jclass obj);
1717

@@ -21,6 +21,8 @@ namespace tns
2121

2222
bool IsNull() const;
2323

24+
bool IsGlobal() const;
25+
2426
jobject Move();
2527

2628
JniLocalRef& operator=(JniLocalRef&& rhs);
@@ -57,6 +59,7 @@ namespace tns
5759

5860
private:
5961
jobject m_obj;
62+
bool m_isGlobal;
6063
};
6164
}
6265

src/jni/JsArgConverter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ bool JsArgConverter::ConvertArg(const Local<Value>& arg, int index)
211211

212212
if (success)
213213
{
214-
SetConvertedObject(index, obj.Move());
214+
SetConvertedObject(index, obj.Move(), obj.IsGlobal());
215215
}
216216
else
217217
{
@@ -238,10 +238,10 @@ bool JsArgConverter::ConvertArg(const Local<Value>& arg, int index)
238238
return success;
239239
}
240240

241-
void JsArgConverter::SetConvertedObject(int index, jobject obj)
241+
void JsArgConverter::SetConvertedObject(int index, jobject obj, bool isGlobal)
242242
{
243243
m_args[index].l = obj;
244-
if (obj != nullptr)
244+
if ((obj != nullptr) && !isGlobal)
245245
{
246246
m_storedObjects.push_back(index);
247247
}

src/jni/JsArgConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace tns
4949

5050
bool ConvertJavaScriptString(const v8::Local<v8::Value>& jsValue, int index);
5151

52-
void SetConvertedObject(int index, jobject obj);
52+
void SetConvertedObject(int index, jobject obj, bool isGlobal = false);
5353

5454
template<typename T>
5555
bool ConvertFromCastFunctionObject(T value, int index);

src/jni/JsArgToArrayConverter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ bool JsArgToArrayConverter::ConvertArg(const Local<Value>& arg, int index)
282282
success = !obj.IsNull();
283283
if (success)
284284
{
285-
SetConvertedObject(env, index, obj.Move());
285+
SetConvertedObject(env, index, obj.Move(), obj.IsGlobal());
286286
}
287287
else
288288
{
@@ -314,10 +314,10 @@ jobject JsArgToArrayConverter::GetConvertedArg()
314314
return (m_argsLen > 0) ? m_argsAsObject[0] : nullptr;
315315
}
316316

317-
void JsArgToArrayConverter::SetConvertedObject(JEnv& env, int index, jobject obj)
317+
void JsArgToArrayConverter::SetConvertedObject(JEnv& env, int index, jobject obj, bool isGlobal)
318318
{
319319
m_argsAsObject[index] = obj;
320-
if (obj != nullptr)
320+
if ((obj != nullptr) && !isGlobal)
321321
{
322322
m_storedIndexes.push_back(index);
323323
}

src/jni/JsArgToArrayConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace tns
4343
private:
4444
bool ConvertArg(const v8::Local<v8::Value>& arg, int index);
4545

46-
void SetConvertedObject(JEnv& env, int index, jobject obj);
46+
void SetConvertedObject(JEnv& env, int index, jobject obj, bool isGlobal = false);
4747

4848
int m_argsLen;
4949

src/jni/ObjectManager.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ ObjectManager::ObjectManager()
4343
GET_NAME_METHOD_ID = env.GetMethodID(JAVA_LANG_CLASS, "getName", "()Ljava/lang/String;");
4444
assert(GET_NAME_METHOD_ID != nullptr);
4545

46+
auto useGlobalRefsMethodID = env.GetStaticMethodID(PlatformClass, "useGlobalRefs", "()Z");
47+
assert(useGlobalRefsMethodID != nullptr);
48+
49+
auto useGlobalRefs = env.CallStaticBooleanMethod(PlatformClass, useGlobalRefsMethodID);
50+
m_useGlobalRefs = useGlobalRefs == JNI_TRUE;
51+
4652
ObjectManager::instance = this;
4753
}
4854

@@ -64,17 +70,24 @@ JniLocalRef ObjectManager::GetJavaObjectByJsObjectStatic(const Local<Object>& ob
6470

6571
JniLocalRef ObjectManager::GetJavaObjectByJsObject(const Local<Object>& object)
6672
{
67-
JniLocalRef javaObject;
68-
6973
JSInstanceInfo *jsInstanceInfo = GetJSInstanceInfo(object);
7074

7175
if (jsInstanceInfo != nullptr)
7276
{
7377
JEnv env;
74-
javaObject = JniLocalRef(env.NewLocalRef(GetJavaObjectByID(jsInstanceInfo->JavaObjectID)));
78+
if (m_useGlobalRefs)
79+
{
80+
JniLocalRef javaObject(GetJavaObjectByID(jsInstanceInfo->JavaObjectID), true);
81+
return javaObject;
82+
}
83+
else
84+
{
85+
JniLocalRef javaObject(env.NewLocalRef(GetJavaObjectByID(jsInstanceInfo->JavaObjectID)));
86+
return javaObject;
87+
}
7588
}
7689

77-
return javaObject;
90+
return JniLocalRef();
7891
}
7992

8093
JSInstanceInfo* ObjectManager::GetJSInstanceInfo(const Local<Object>& object)

src/jni/ObjectManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ namespace tns
189189

190190
DirectBuffer m_outBuff;
191191

192+
bool m_useGlobalRefs;
193+
192194
jclass PlatformClass;
193195

194196
jclass JAVA_LANG_CLASS;

src/src/com/tns/Platform.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,4 +911,12 @@ private static Object createArrayHelper(String arrayClassName, int size) throws
911911

912912
return arr;
913913
}
914+
915+
@RuntimeCallable
916+
private static boolean useGlobalRefs()
917+
{
918+
int JELLY_BEAN = 16;
919+
boolean useGlobalRefs = android.os.Build.VERSION.SDK_INT >= JELLY_BEAN;
920+
return useGlobalRefs;
921+
}
914922
}

0 commit comments

Comments
 (0)