Skip to content

Commit d492dd8

Browse files
author
Mihail Slavchev
committed
refactor GetJavaObjectByJsObject to return JniLocalRef
1 parent 75214bf commit d492dd8

13 files changed

Lines changed: 94 additions & 115 deletions

src/jni/ArrayElementAccessor.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Local<Value> ArrayElementAccessor::GetArrayElement(const Local<Object>& array, u
2525
Isolate* isolate = Isolate::GetCurrent();
2626
EscapableHandleScope handleScope(isolate);
2727

28-
jweak arr = objectManager->GetJavaObjectByJsObject(array);
28+
auto arr = objectManager->GetJavaObjectByJsObject(array);
2929

3030
Local<Value> value;
3131
jsize startIndex = index;
@@ -36,21 +36,21 @@ Local<Value> ArrayElementAccessor::GetArrayElement(const Local<Object>& array, u
3636

3737
if (elementSignature == "Z")
3838
{
39-
jbooleanArray boolArr = reinterpret_cast<jbooleanArray>(arr);
39+
jbooleanArray boolArr = static_cast<jbooleanArray>(arr);
4040
jboolean boolArrValue;
4141
env.GetBooleanArrayRegion(boolArr, startIndex, length, &boolArrValue);
4242
value = ConvertToJsValue(env, elementSignature, &boolArrValue);
4343
}
4444
else if (elementSignature == "B")
4545
{
46-
jbyteArray byteArr = reinterpret_cast<jbyteArray>(arr);
46+
jbyteArray byteArr = static_cast<jbyteArray>(arr);
4747
jbyte byteArrValue;
4848
env.GetByteArrayRegion(byteArr, startIndex, length, &byteArrValue);
4949
value = ConvertToJsValue(env, elementSignature, &byteArrValue);
5050
}
5151
else if (elementSignature == "C")
5252
{
53-
jcharArray charArr = reinterpret_cast<jcharArray>(arr);
53+
jcharArray charArr = static_cast<jcharArray>(arr);
5454
jchar charArrValue;
5555
env.GetCharArrayRegion(charArr, startIndex, length, &charArrValue);
5656
JniLocalRef s(env.NewString(&charArrValue, 1));
@@ -60,42 +60,42 @@ Local<Value> ArrayElementAccessor::GetArrayElement(const Local<Object>& array, u
6060
}
6161
else if (elementSignature == "S")
6262
{
63-
jshortArray shortArr = reinterpret_cast<jshortArray>(arr);
63+
jshortArray shortArr = static_cast<jshortArray>(arr);
6464
jshort shortArrValue;
6565
env.GetShortArrayRegion(shortArr, startIndex, length, &shortArrValue);
6666
value = ConvertToJsValue(env, elementSignature, &shortArrValue);
6767
}
6868
else if (elementSignature == "I")
6969
{
70-
jintArray intArr = reinterpret_cast<jintArray>(arr);
70+
jintArray intArr = static_cast<jintArray>(arr);
7171
jint intArrValue;
7272
env.GetIntArrayRegion(intArr, startIndex, length, &intArrValue);
7373
value = ConvertToJsValue(env, elementSignature, &intArrValue);
7474
}
7575
else if (elementSignature == "J")
7676
{
77-
jlongArray longArr = reinterpret_cast<jlongArray>(arr);
77+
jlongArray longArr = static_cast<jlongArray>(arr);
7878
jlong longArrValue;
7979
env.GetLongArrayRegion(longArr, startIndex, length, &longArrValue);
8080
value = ConvertToJsValue(env, elementSignature, &longArrValue);
8181
}
8282
else if (elementSignature == "F")
8383
{
84-
jfloatArray floatArr = reinterpret_cast<jfloatArray>(arr);
84+
jfloatArray floatArr = static_cast<jfloatArray>(arr);
8585
jfloat floatArrValue;
8686
env.GetFloatArrayRegion(floatArr, startIndex, length, &floatArrValue);
8787
value = ConvertToJsValue(env, elementSignature, &floatArrValue);
8888
}
8989
else if (elementSignature == "D")
9090
{
91-
jdoubleArray doubleArr = reinterpret_cast<jdoubleArray>(arr);
91+
jdoubleArray doubleArr = static_cast<jdoubleArray>(arr);
9292
jdouble doubleArrValue;
9393
env.GetDoubleArrayRegion(doubleArr, startIndex, length, &doubleArrValue);
9494
value = ConvertToJsValue(env, elementSignature, &doubleArrValue);
9595
}
9696
else
9797
{
98-
jobject result = env.GetObjectArrayElement(reinterpret_cast<jobjectArray>(arr), index);
98+
jobject result = env.GetObjectArrayElement(static_cast<jobjectArray>(arr), index);
9999
value = ConvertToJsValue(env, elementSignature, &result);
100100
env.DeleteLocalRef(result);
101101
}
@@ -110,21 +110,21 @@ void ArrayElementAccessor::SetArrayElement(const Local<Object>& array, uint32_t
110110
Isolate* isolate = Isolate::GetCurrent();
111111
HandleScope handleScope(isolate);
112112

113-
jweak arr = objectManager->GetJavaObjectByJsObject(array);
113+
auto arr = objectManager->GetJavaObjectByJsObject(array);
114114

115115
const string elementSignature = arraySignature.substr(1);
116116
jboolean isCopy = false;
117117

118118
if (elementSignature == "Z") //bool
119119
{
120120
jboolean boolElementValue = (jboolean) value->BooleanValue();
121-
jbooleanArray boolArr = reinterpret_cast<jbooleanArray>(arr);
121+
jbooleanArray boolArr = static_cast<jbooleanArray>(arr);
122122
env.SetBooleanArrayRegion(boolArr, index, 1, &boolElementValue);
123123
}
124124
else if (elementSignature == "B") //byte
125125
{
126126
jbyte byteElementValue = (jbyte) value->Int32Value();
127-
jbyteArray byteArr = reinterpret_cast<jbyteArray>(arr);
127+
jbyteArray byteArr = static_cast<jbyteArray>(arr);
128128
env.SetByteArrayRegion(byteArr, index, 1, &byteElementValue);
129129
}
130130
else if (elementSignature == "C") //char
@@ -134,19 +134,19 @@ void ArrayElementAccessor::SetArrayElement(const Local<Object>& array, uint32_t
134134
const char* singleChar = env.GetStringUTFChars(s, &isCopy);
135135
jchar charElementValue = *singleChar;
136136
env.ReleaseStringUTFChars(s, singleChar);
137-
jcharArray charArr = reinterpret_cast<jcharArray>(arr);
137+
jcharArray charArr = static_cast<jcharArray>(arr);
138138
env.SetCharArrayRegion(charArr, index, 1, &charElementValue);
139139
}
140140
else if (elementSignature == "S") //short
141141
{
142142
jshort shortElementValue = (jshort) value->Int32Value();
143-
jshortArray shortArr = reinterpret_cast<jshortArray>(arr);
143+
jshortArray shortArr = static_cast<jshortArray>(arr);
144144
env.SetShortArrayRegion(shortArr, index, 1, &shortElementValue);
145145
}
146146
else if (elementSignature == "I") //int
147147
{
148148
jint intElementValue = value->Int32Value();
149-
jintArray intArr = reinterpret_cast<jintArray>(arr);
149+
jintArray intArr = static_cast<jintArray>(arr);
150150
env.SetIntArrayRegion(intArr, index, 1, &intElementValue);
151151
}
152152
else if (elementSignature == "J") //long
@@ -160,19 +160,19 @@ void ArrayElementAccessor::SetArrayElement(const Local<Object>& array, uint32_t
160160
{
161161
longElementValue = (jlong) value->IntegerValue();
162162
}
163-
jlongArray longArr = reinterpret_cast<jlongArray>(arr);
163+
jlongArray longArr = static_cast<jlongArray>(arr);
164164
env.SetLongArrayRegion(longArr, index, 1, &longElementValue);
165165
}
166166
else if (elementSignature == "F") //float
167167
{
168168
jfloat floatElementValue = (jfloat) value->NumberValue();
169-
jfloatArray floatArr = reinterpret_cast<jfloatArray>(arr);
169+
jfloatArray floatArr = static_cast<jfloatArray>(arr);
170170
env.SetFloatArrayRegion(floatArr, index, 1, &floatElementValue);
171171
}
172172
else if (elementSignature == "D") //double
173173
{
174174
jdouble doubleElementValue = (jdouble) value->NumberValue();
175-
jdoubleArray doubleArr = reinterpret_cast<jdoubleArray>(arr);
175+
jdoubleArray doubleArr = static_cast<jdoubleArray>(arr);
176176
env.SetDoubleArrayRegion(doubleArr, index, 1, &doubleElementValue);
177177
}
178178
else //string or object
@@ -185,7 +185,7 @@ void ArrayElementAccessor::SetArrayElement(const Local<Object>& array, uint32_t
185185
JsArgToArrayConverter argConverter(value, false, (int) Type::Null);
186186
if (argConverter.IsValid())
187187
{
188-
jobjectArray objArr = reinterpret_cast<jobjectArray>(arr);
188+
jobjectArray objArr = static_cast<jobjectArray>(arr);
189189
jobject objectElementValue = argConverter.GetConvertedArg();
190190
env.SetObjectArrayElement(objArr, index, objectElementValue);
191191
}

src/jni/FieldAccessor.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Local<Value> FieldAccessor::GetJavaField(const Local<Object>& target, FieldCallb
2727

2828
Local<Value> fieldResult;
2929

30-
jweak targetJavaObject;
30+
JniLocalRef targetJavaObject;
3131

3232
const auto& fieldTypeName = fieldData->signature;
3333
auto isStatic = fieldData->isStatic;
@@ -61,7 +61,7 @@ Local<Value> FieldAccessor::GetJavaField(const Local<Object>& target, FieldCallb
6161
{
6262
targetJavaObject = objectManager->GetJavaObjectByJsObject(target);
6363

64-
if (targetJavaObject == nullptr)
64+
if (targetJavaObject.IsNull())
6565
{
6666
stringstream ss;
6767
ss << "Cannot access property '" << fieldData->name << "' because there is no corresponding Java object";
@@ -253,7 +253,7 @@ void FieldAccessor::SetJavaField(const Local<Object>& target, const Local<Value>
253253
auto isolate = Isolate::GetCurrent();
254254
HandleScope handleScope(isolate);
255255

256-
jweak targetJavaObject;
256+
JniLocalRef targetJavaObject;
257257

258258
const auto& fieldTypeName = fieldData->signature;
259259
auto isStatic = fieldData->isStatic;
@@ -291,7 +291,7 @@ void FieldAccessor::SetJavaField(const Local<Object>& target, const Local<Value>
291291
{
292292
targetJavaObject = objectManager->GetJavaObjectByJsObject(target);
293293

294-
if (targetJavaObject == nullptr)
294+
if (targetJavaObject.IsNull())
295295
{
296296
stringstream ss;
297297
ss << "Cannot access property '" << fieldData->name << "' because there is no corresponding Java object";
@@ -425,7 +425,7 @@ void FieldAccessor::SetJavaField(const Local<Object>& target, const Local<Value>
425425
else
426426
{
427427
bool isString = fieldTypeName == "java/lang/String";
428-
jobject result = nullptr;
428+
JniLocalRef result;
429429

430430
if (!value->IsNull())
431431
{
@@ -449,10 +449,5 @@ void FieldAccessor::SetJavaField(const Local<Object>& target, const Local<Value>
449449
{
450450
env.SetObjectField(targetJavaObject, fieldId, result);
451451
}
452-
453-
if (isString)
454-
{
455-
env.DeleteLocalRef(result);
456-
}
457452
}
458453
}

src/jni/JniLocalRef.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,28 @@ JniLocalRef::JniLocalRef(jclass obj)
2323
{
2424
}
2525

26-
JniLocalRef::JniLocalRef(const JniLocalRef& rhs)
26+
JniLocalRef::JniLocalRef(JniLocalRef&& rhs)
27+
: m_obj(rhs.m_obj)
2728
{
28-
JEnv env;
29-
30-
m_obj = env.NewLocalRef(rhs.m_obj);
29+
rhs.m_obj = nullptr;
3130
}
3231

3332
bool JniLocalRef::IsNull() const
3433
{
3534
return m_obj == nullptr;
3635
}
3736

38-
JniLocalRef& JniLocalRef::operator=(const JniLocalRef& rhs)
37+
jobject JniLocalRef::Move()
3938
{
40-
if (this != &rhs)
41-
{
42-
JEnv env;
43-
if (m_obj != nullptr)
44-
{
45-
env.DeleteLocalRef(m_obj);
46-
}
47-
m_obj = (rhs.m_obj != nullptr)
48-
? env.NewLocalRef(rhs.m_obj)
49-
: nullptr;
50-
}
39+
auto value = m_obj;
40+
m_obj = nullptr;
41+
return value;
42+
}
43+
44+
JniLocalRef& JniLocalRef::operator=(JniLocalRef&& rhs)
45+
{
46+
m_obj = rhs.m_obj;
47+
rhs.m_obj = nullptr;
5148
return *this;
5249
}
5350

src/jni/JniLocalRef.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ namespace tns
1515

1616
JniLocalRef(jclass obj);
1717

18-
JniLocalRef(const JniLocalRef& rhs);
18+
JniLocalRef(JniLocalRef&& rhs);
1919

2020
~JniLocalRef();
2121

2222
bool IsNull() const;
2323

24-
JniLocalRef& operator=(const JniLocalRef& rhs);
24+
jobject Move();
25+
26+
JniLocalRef& operator=(JniLocalRef&& rhs);
2527

2628
operator jobject() const;
2729

src/jni/JsArgConverter.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "JsArgConverter.h"
22
#include <sstream>
33
#include "ObjectManager.h"
4-
#include "NativeScriptAssert.h"
54
#include "ArgConverter.h"
65
#include "JniLocalRef.h"
76
#include "JniSignatureParser.h"
@@ -207,11 +206,14 @@ bool JsArgConverter::ConvertArg(const Local<Value>& arg, int index)
207206
return success;
208207
}
209208

210-
jweak obj = ObjectManager::GetJavaObjectByJsObjectStatic(jsObject);
211-
SetConvertedObject(index, obj, true);
212-
success = obj != nullptr;
209+
auto obj = ObjectManager::GetJavaObjectByJsObjectStatic(jsObject);
210+
success = !obj.IsNull();
213211

214-
if (!success)
212+
if (success)
213+
{
214+
SetConvertedObject(index, obj.Move());
215+
}
216+
else
215217
{
216218
sprintf(buff, "Cannot convert object to %s at index %d", typeSignature.c_str(), index);
217219
}
@@ -236,20 +238,12 @@ bool JsArgConverter::ConvertArg(const Local<Value>& arg, int index)
236238
return success;
237239
}
238240

239-
void JsArgConverter::SetConvertedObject(int index, jobject obj, bool isGlobalRef)
241+
void JsArgConverter::SetConvertedObject(int index, jobject obj)
240242
{
241-
if (obj == nullptr)
242-
{
243-
m_args[index].l = obj;
244-
}
245-
else if (isGlobalRef)
246-
{
247-
m_args[index].l = obj;
248-
}
249-
else
243+
m_args[index].l = obj;
244+
if (obj != nullptr)
250245
{
251246
m_storedObjects.push_back(index);
252-
m_args[index].l = obj;
253247
}
254248
}
255249

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, bool isGlobalRef = false);
52+
void SetConvertedObject(int index, jobject obj);
5353

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

0 commit comments

Comments
 (0)