@@ -9,6 +9,13 @@ using namespace v8;
99using namespace std ;
1010using namespace tns ;
1111
12+ namespace {
13+ // The element type of a primitive array's signature ("[I" -> 'I'), else '\0'.
14+ char PrimitiveElementType (const string& arraySignature) {
15+ return arraySignature.size () == 2 ? arraySignature[1 ] : ' \0 ' ;
16+ }
17+ }
18+
1219Local<Value> ArrayElementAccessor::GetArrayElement (Isolate* isolate, Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature) {
1320 JEnv env;
1421
@@ -24,52 +31,67 @@ Local<Value> ArrayElementAccessor::GetArrayElement(Isolate* isolate, Local<Conte
2431 jsize startIndex = index;
2532 const jsize length = 1 ;
2633
27- const string elementSignature = arraySignature.substr (1 );
28-
29- if (elementSignature == " Z" ) {
30- jbooleanArray boolArr = static_cast <jbooleanArray>(arr);
31- jboolean boolArrValue;
32- env.GetBooleanArrayRegion (boolArr, startIndex, length, &boolArrValue);
33- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &boolArrValue);
34- } else if (elementSignature == " B" ) {
35- jbyteArray byteArr = static_cast <jbyteArray>(arr);
36- jbyte byteArrValue;
37- env.GetByteArrayRegion (byteArr, startIndex, length, &byteArrValue);
38- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &byteArrValue);
39- } else if (elementSignature == " C" ) {
40- jcharArray charArr = static_cast <jcharArray>(arr);
41- jchar charArrValue;
42- env.GetCharArrayRegion (charArr, startIndex, length, &charArrValue);
43- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &charArrValue);
44- } else if (elementSignature == " S" ) {
45- jshortArray shortArr = static_cast <jshortArray>(arr);
46- jshort shortArrValue;
47- env.GetShortArrayRegion (shortArr, startIndex, length, &shortArrValue);
48- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &shortArrValue);
49- } else if (elementSignature == " I" ) {
50- jintArray intArr = static_cast <jintArray>(arr);
51- jint intArrValue;
52- env.GetIntArrayRegion (intArr, startIndex, length, &intArrValue);
53- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &intArrValue);
54- } else if (elementSignature == " J" ) {
55- jlongArray longArr = static_cast <jlongArray>(arr);
56- jlong longArrValue;
57- env.GetLongArrayRegion (longArr, startIndex, length, &longArrValue);
58- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &longArrValue);
59- } else if (elementSignature == " F" ) {
60- jfloatArray floatArr = static_cast <jfloatArray>(arr);
61- jfloat floatArrValue;
62- env.GetFloatArrayRegion (floatArr, startIndex, length, &floatArrValue);
63- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &floatArrValue);
64- } else if (elementSignature == " D" ) {
65- jdoubleArray doubleArr = static_cast <jdoubleArray>(arr);
66- jdouble doubleArrValue;
67- env.GetDoubleArrayRegion (doubleArr, startIndex, length, &doubleArrValue);
68- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &doubleArrValue);
69- } else {
70- jobject result = env.GetObjectArrayElement (static_cast <jobjectArray>(arr), index);
71- value = ConvertToJsValue (isolate, objectManager, env, elementSignature, &result);
72- env.DeleteLocalRef (result);
34+ switch (PrimitiveElementType (arraySignature)) {
35+ case ' Z' : {
36+ jboolean boolArrValue;
37+ env.GetBooleanArrayRegion (static_cast <jbooleanArray>(arr), startIndex, length, &boolArrValue);
38+ value = Boolean::New (isolate, boolArrValue);
39+ break ;
40+ }
41+ case ' B' : {
42+ jbyte byteArrValue;
43+ env.GetByteArrayRegion (static_cast <jbyteArray>(arr), startIndex, length, &byteArrValue);
44+ value = Integer::New (isolate, byteArrValue);
45+ break ;
46+ }
47+ case ' C' : {
48+ jchar charArrValue;
49+ env.GetCharArrayRegion (static_cast <jcharArray>(arr), startIndex, length, &charArrValue);
50+ value = ArgConverter::jcharToV8String (isolate, charArrValue);
51+ break ;
52+ }
53+ case ' S' : {
54+ jshort shortArrValue;
55+ env.GetShortArrayRegion (static_cast <jshortArray>(arr), startIndex, length, &shortArrValue);
56+ value = Integer::New (isolate, shortArrValue);
57+ break ;
58+ }
59+ case ' I' : {
60+ jint intArrValue;
61+ env.GetIntArrayRegion (static_cast <jintArray>(arr), startIndex, length, &intArrValue);
62+ value = Integer::New (isolate, intArrValue);
63+ break ;
64+ }
65+ case ' J' : {
66+ jlong longArrValue;
67+ env.GetLongArrayRegion (static_cast <jlongArray>(arr), startIndex, length, &longArrValue);
68+ value = ArgConverter::ConvertFromJavaLong (isolate, longArrValue);
69+ break ;
70+ }
71+ case ' F' : {
72+ jfloat floatArrValue;
73+ env.GetFloatArrayRegion (static_cast <jfloatArray>(arr), startIndex, length, &floatArrValue);
74+ value = Number::New (isolate, floatArrValue);
75+ break ;
76+ }
77+ case ' D' : {
78+ jdouble doubleArrValue;
79+ env.GetDoubleArrayRegion (static_cast <jdoubleArray>(arr), startIndex, length, &doubleArrValue);
80+ value = Number::New (isolate, doubleArrValue);
81+ break ;
82+ }
83+ default : {
84+ jobject result = env.GetObjectArrayElement (static_cast <jobjectArray>(arr), index);
85+ if (result == nullptr ) {
86+ value = Null (isolate);
87+ } else if (arraySignature.compare (1 , string::npos, " Ljava/lang/String;" ) == 0 ) {
88+ value = ArgConverter::jstringToV8String (isolate, static_cast <jstring>(result));
89+ } else {
90+ value = objectManager->GetOrCreateJSWrapper (result);
91+ }
92+ env.DeleteLocalRef (result);
93+ break ;
94+ }
7395 }
7496
7597 return handleScope.Escape (value);
@@ -86,29 +108,29 @@ void ArrayElementAccessor::SetArrayElement(Isolate* isolate, Local<Context> cont
86108
87109 assertNonNullNativeArray (arr);
88110
89- const string elementSignature = arraySignature. substr ( 1 );
111+ const char elementType = PrimitiveElementType (arraySignature );
90112
91- if (elementSignature == " Z " ) { // bool
113+ if (elementType == ' Z ' ) { // bool
92114 jboolean boolElementValue = (jboolean) value->BooleanValue (isolate);
93115 jbooleanArray boolArr = static_cast <jbooleanArray>(arr);
94116 env.SetBooleanArrayRegion (boolArr, index, 1 , &boolElementValue);
95- } else if (elementSignature == " B " ) { // byte
117+ } else if (elementType == ' B ' ) { // byte
96118 jbyte byteElementValue = (jbyte) value->Int32Value (context).ToChecked ();
97119 jbyteArray byteArr = static_cast <jbyteArray>(arr);
98120 env.SetByteArrayRegion (byteArr, index, 1 , &byteElementValue);
99- } else if (elementSignature == " C " ) { // char
121+ } else if (elementType == ' C ' ) { // char
100122 jchar charElementValue = ArgConverter::ToJavaChar (isolate, value->ToString (context).ToLocalChecked ());
101123 jcharArray charArr = static_cast <jcharArray>(arr);
102124 env.SetCharArrayRegion (charArr, index, 1 , &charElementValue);
103- } else if (elementSignature == " S " ) { // short
125+ } else if (elementType == ' S ' ) { // short
104126 jshort shortElementValue = (jshort) value->Int32Value (context).ToChecked ();
105127 jshortArray shortArr = static_cast <jshortArray>(arr);
106128 env.SetShortArrayRegion (shortArr, index, 1 , &shortElementValue);
107- } else if (elementSignature == " I " ) { // int
129+ } else if (elementType == ' I ' ) { // int
108130 jint intElementValue = value->Int32Value (context).ToChecked ();
109131 jintArray intArr = static_cast <jintArray>(arr);
110132 env.SetIntArrayRegion (intArr, index, 1 , &intElementValue);
111- } else if (elementSignature == " J " ) { // long
133+ } else if (elementType == ' J ' ) { // long
112134 jlong longElementValue;
113135 if (value->IsObject ()) {
114136 longElementValue = (jlong) ArgConverter::ConvertToJavaLong (isolate, value);
@@ -117,11 +139,11 @@ void ArrayElementAccessor::SetArrayElement(Isolate* isolate, Local<Context> cont
117139 }
118140 jlongArray longArr = static_cast <jlongArray>(arr);
119141 env.SetLongArrayRegion (longArr, index, 1 , &longElementValue);
120- } else if (elementSignature == " F " ) { // float
142+ } else if (elementType == ' F ' ) { // float
121143 jfloat floatElementValue = (jfloat) value->NumberValue (context).ToChecked ();
122144 jfloatArray floatArr = static_cast <jfloatArray>(arr);
123145 env.SetFloatArrayRegion (floatArr, index, 1 , &floatElementValue);
124- } else if (elementSignature == " D " ) { // double
146+ } else if (elementType == ' D ' ) { // double
125147 jdouble doubleElementValue = (jdouble) value->NumberValue (context).ToChecked ();
126148 jdoubleArray doubleArr = static_cast <jdoubleArray>(arr);
127149 env.SetDoubleArrayRegion (doubleArr, index, 1 , &doubleElementValue);
@@ -145,51 +167,6 @@ void ArrayElementAccessor::SetArrayElement(Isolate* isolate, Local<Context> cont
145167 }
146168}
147169
148- Local<Value> ArrayElementAccessor::ConvertToJsValue (Isolate* isolate, ObjectManager* objectManager, JEnv& env, const string& elementSignature, const void * value) {
149- Local<Value> jsValue;
150-
151- if (elementSignature == " Z" ) {
152- jsValue = Boolean::New (isolate, *(jboolean*) value);
153- } else if (elementSignature == " B" ) {
154- jsValue = Integer::New (isolate, *(jbyte*) value);
155- } else if (elementSignature == " C" ) {
156- jsValue = ArgConverter::jcharToV8String (isolate, *(jchar*) value);
157- } else if (elementSignature == " S" ) {
158- jsValue = Integer::New (isolate, *(jshort*) value);
159- } else if (elementSignature == " I" ) {
160- jsValue = Integer::New (isolate, *(jint*) value);
161- } else if (elementSignature == " J" ) {
162- jsValue = ArgConverter::ConvertFromJavaLong (isolate, *(jlong*) value);
163- } else if (elementSignature == " F" ) {
164- jsValue = Number::New (isolate, *(jfloat*) value);
165- } else if (elementSignature == " D" ) {
166- jsValue = Number::New (isolate, *(jdouble*) value);
167- } else {
168- if (nullptr != (*(jobject*) value)) {
169- bool isString = elementSignature == " Ljava/lang/String;" ;
170-
171- if (isString) {
172- jsValue = ArgConverter::jstringToV8String (isolate, *(jstring*) value);
173- } else {
174- jint javaObjectID = objectManager->GetOrCreateObjectId (*(jobject*) value);
175- jsValue = objectManager->GetJsObjectByJavaObject (javaObjectID);
176-
177- if (jsValue.IsEmpty ()) {
178- // Named after the element's runtime class, as a method result is: the
179- // wrapper's name must be a JNI class name, and it is later read back as
180- // the array signature of the element itself.
181- auto className = objectManager->GetClassName (*(jobject*) value);
182- jsValue = objectManager->CreateJSWrapper (javaObjectID, className);
183- }
184- }
185- } else {
186- jsValue = Null (isolate);
187- }
188- }
189-
190- return jsValue;
191- }
192-
193170void ArrayElementAccessor::assertNonNullNativeArray (tns::JniLocalRef& arrayReference) {
194171 if (arrayReference.IsNull ()){
195172 throw NativeScriptException (" Failed calling indexer operator on native array. The JavaScript instance no longer has available Java instance counterpart." );
0 commit comments