Skip to content

Commit 56c7b0b

Browse files
authored
perf(runtime): hot-path quick wins on the JNI bridge (#2141)
### Description A batch of small, mechanical cuts to per-call work on the JS ↔ Java bridge: heap allocations, string copies, thread-local reads, out-of-line trivial calls and lookups repeated within one call. No locks, copies or process-wide `Isolate*`-keyed state are added; the new per-isolate state goes through `V8StringConstants` and `MetadataNodeCache` like the existing keys. **Allocations and copies** (`perf(runtime): drop per-call copies and dead work on the call paths`) - `JsArgConverter::ConvertArg` zero-filled a 1 KB error buffer for every argument; it is now only NUL-terminated (every reader uses the C string). - The object/cast branch of `ConvertArg` created an unused `JEnv` and looked up the runtime for every object argument. The object manager is now resolved once per converter, and only for a plain object argument. The destructor skips its `JEnv` when no local refs were recorded. - `instrumentation::Frame` copied its name into a `std::string`, so every `TNSPERF()` frame heap-allocated for names longer than the SSO buffer, even with instrumentation off. These frames cover the class, extended-class and interface constructors and `GetImplementationObject`. Literal and `__func__` names are now kept as pointers; dynamic names still own a copy. - The class, extended-class and interface constructor callbacks copied their class names on every `new`. They now pass the stored names by reference, and build the `-defaults` name only when it is needed. **TLS / isolate passing** (`perf(runtime): pass the isolate instead of reading it from TLS`) - `ArgConverter::ConvertToJavaString`, `NumericCasts::GetCastValue`, `MetadataNode::GetNodeFromHandle`, the single-value `JsArgToArrayConverter` and the array element accessors now take the isolate from their callers instead of calling `Isolate::GetCurrent()` (emulated TLS below API 29). - `CallJavaMethod` looked the runtime up twice per call; it reuses the first lookup. - The instance field getter and setter looked up the runtime only to call the static `ObjectManager::IsJsRuntimeObject`. **Inlining** (`perf(runtime): inline trivial accessors used on every bridge call`) - `Runtime::GetIsolate/GetJavaRuntime/GetObjectManager/GetContext/GetId` and `JEnv`'s `JNIEnv*` conversion are now inline. The `Runtime` definitions sit after the class body, and its declarations are unchanged. - The `MetadataEntry` getters now inline only the check for an already-read value; reading the metadata stays out of line. **Return values and array elements** (`perf(runtime): cheaper JS -> Java return values and array element access`) - Returning a JS value to Java used to cost a `new jobject[1]`, a vector push, a `DeleteGlobalRef(nullptr)`, and a `NewLocalRef` + `DeleteLocalRef` pair. The single-value converter now writes into an inline slot. `ConvertJsValueToJavaObject` takes the converted local ref over through `ReleaseConvertedArg()`; a borrowed (global/weak) ref is still copied into a new local, as before. - `CreateJSInstanceNative` no longer reads the instance's class name back through `GetObjectClass` + `Class.getName()`. Java already passes `instance.getClass().getName()`, which is the same name. - Java array element access copied the element signature and compared it against up to nine strings per access. It now switches on the element type. - Object elements go through `ObjectManager::GetOrCreateJSWrapper`, as method results and field reads do. This replaces `GetOrCreateObjectId` + `GetClassName` + a node lookup under the global node mutex. **Caching** (`perf(runtime): cache per-isolate keys and names used per access`) - The implementation-object, `supervalue` and `typemetadata` private keys were rebuilt from strings, and resolved through V8's private-symbol registry, on every constructor call, `super` read and `GetTypeMetadata`. They are now created once per isolate in `V8StringConstants`. `Private::ForApi` on the same names returns the same symbols. - Kotlin property accessors copied the accessor's method name and created a new V8 string on every get and set. `PropertyCallbackData` now holds the internalized names. **No spec reaches this path:** the Kotlin property specs in `tests/kotlin/properties/testPropertiesSupport.js` are commented out. I re-enabled "Test Kotlin public properties should work" locally, and it fails here the same way it is expected to fail on `main`: the property reads as `undefined`, because no accessor is installed in the test app. The `@get:JvmName` spec calls methods, not properties. - `CreateArrayWrapper` took the global node mutex to look up `java/lang/Object` for every Java array it wrapped. Nodes are process-wide and never freed, so the pointer is resolved once. - `MethodCallback` decoded its `External` twice. #### Not changed (from the audit list) - `CanBeJavaString` (6 compares per object-returning call) and the `== "java/lang/String"` per object field access: libc++ compares against a literal by length first, and the length is constant-folded. That leaves a handful of integer compares next to a JNI call, so precomputing them is not worth a new field on `MetadataEntry`/`CacheMethodInfo`/`FieldCallbackData`. #### Benchmark rows added `test(bench): ...` adds `array get int[i]`, `array set int[i]`, `array get Object[i]` and `callback.objectReturn() -> Object` to the #2092 harness. The base APK below includes them. #### Measurements Samsung SM-G780G (Android 13). Optimized release APKs (`-Poptimized -Prelease -Pabis=arm64-v8a`). The #2092 JNI harness ran with `debug.ns.jnibench`. - **Labels:** `base` = `main` plus the bench-row commit; `new` = this branch; `base2` = the `base` APK under a second label, as an A/A control. - **Procedure:** labels interleaved base → new → base2 for 4 rounds. Every label was reinstalled and speed-compiled each round, and the first launch after each install was discarded; only the relaunch is measured. All 12 measured launches were in the same clock state (sentinel row 7.4–8.6 ns). - **Columns:** each cell is the median over launches of the per-launch median, in ns/op. "new vs base+base2" compares against the pooled 8 base launches. The phone runs predate the rebase onto #2140 and #2143. Neither touches the measured paths, except `array get Object[i]`: #2143 changes only the branch for elements that are themselves arrays. Rows below are **beyond A/A noise**: `new`'s range does not overlap the pooled base range, and the change is more than twice the A/A delta. | row | base | base2 (A/A) | new | A/A | new vs base+base2 | new range | base+base2 range | |---|---:|---:|---:|---:|---:|---:|---:| | static.noop() | 104.5 | 104.8 | 90.0 | +0.3% | **-14.0%** | 89.8–90.4 | 104.2–105.1 | | static.add(int,int) | 186.9 | 187.9 | 146.2 | +0.5% | **-21.8%** | 145.2–146.5 | 186.4–188.8 | | static.addDouble(double,double) | 230.7 | 233.8 | 198.1 | +1.3% | **-14.3%** | 197.5–198.6 | 230.1–236.1 | | static.addLong(long,long) | 195.3 | 194.9 | 155.5 | -0.2% | **-20.2%** | 155.0–156.5 | 194.6–199.0 | | instance.noop() | 238.1 | 238.6 | 228.9 | +0.2% | **-3.9%** | 228.5–229.3 | 237.7–239.2 | | instance.getAlpha() [android.view.View] | 245.3 | 244.2 | 234.9 | -0.4% | **-4.0%** | 234.3–238.7 | 243.7–246.9 | | instance.getInt() | 240.1 | 239.2 | 230.0 | -0.4% | **-3.9%** | 228.5–231.1 | 238.7–241.5 | | instance.getChar() | 253.7 | 252.1 | 242.1 | -0.6% | **-4.1%** | 240.6–243.5 | 251.6–254.5 | | instance.getString() | 425.6 | 424.8 | 416.4 | -0.2% | **-2.0%** | 412.1–417.6 | 423.7–426.8 | | instance.self() [wrapped return] | 414.7 | 419.8 | 404.9 | +1.2% | **-2.5%** | 402.1–407.0 | 412.7–421.9 | | instance.getOther() [wrapped return] | 412.9 | 416.8 | 403.4 | +0.9% | **-2.3%** | 400.5–410.5 | 411.4–421.0 | | instance.takeObject(Object) | 513.6 | 511.0 | 487.1 | -0.5% | **-4.9%** | 483.9–492.8 | 509.9–519.3 | | instance.takeObjects(Object x3) | 1078.7 | 1077.8 | 1013.5 | -0.1% | **-6.0%** | 1008.8–1019.9 | 1074.0–1083.1 | | static.bufferCapacity(ArrayBuffer) | 443.8 | 444.5 | 412.1 | +0.2% | **-7.2%** | 401.0–412.3 | 439.1–456.5 | | static field get int [small class] | 70.9 | 70.9 | 62.3 | +0.1% | **-12.1%** | 62.1–62.5 | 70.4–71.2 | | static field get final int [small class] | 70.8 | 70.9 | 62.0 | +0.2% | **-12.5%** | 61.8–62.3 | 70.5–71.4 | | static field set int [small class] | 73.5 | 73.3 | 65.2 | -0.3% | **-11.0%** | 65.1–65.7 | 73.2–74.0 | | field.get int | 162.6 | 162.1 | 153.1 | -0.3% | **-5.7%** | 151.4–153.6 | 161.5–163.6 | | field.set int | 162.0 | 162.3 | 152.8 | +0.2% | **-5.8%** | 152.6–153.5 | 161.8–162.5 | | field.get Object | 333.3 | 331.6 | 321.1 | -0.5% | **-3.4%** | 318.9–324.6 | 327.3–336.9 | | field.get String | 361.6 | 361.2 | 350.6 | -0.1% | **-3.1%** | 350.1–352.0 | 358.9–364.0 | | array get int[i] | 231.9 | 233.3 | 215.0 | +0.6% | **-7.4%** | 214.2–215.9 | 230.9–236.5 | | array set int[i] | 267.2 | 266.4 | 257.5 | -0.3% | **-3.5%** | 256.7–258.7 | 266.1–269.7 | | array get Object[i] | 408.1 | 405.3 | 363.6 | -0.7% | **-10.7%** | 360.2–369.1 | 401.4–409.8 | | instance.self() [50k live wrappers] | 417.0 | 422.4 | 406.8 | +1.3% | **-2.8%** | 403.4–407.1 | 414.8–430.4 | | contention 1 thread(s), 3 new objects | 8851.6 | 8924.8 | 8308.3 | +0.8% | **-6.2%** | 8146.3–8474.8 | 8582.3–9704.9 | Static calls get the most out of the 1 KB `memset` per argument and the inlined `MetadataEntry` getters. The array rows show the element-type switch and the `GetOrCreateJSWrapper` path. **Within noise:** - The first-access rows: one sample per launch, with A/A up to ±20%. - Object creation and lifecycle rows. - `instance.echo(String 1KB)`, which is bimodal in all three labels. - Contention rows other than the one above. - **Callbacks: the harness cannot resolve them.** Callback rows divided by the reference callback show A/A of 15–24%, and raw callback medians and the raw reference itself show A/A up to ±27–38%. So the Java → JS return-value change (`callback.objectReturn() -> Object`: −1.7% raw, −3.9% normalized) is not measured by this harness. Note too that the reference callback goes through the same code, so normalizing by it would cancel out any change that speeds up every callback. ### Does your commit message include the wording below to reference a specific issue in this repo? No. ### Related Pull Requests - **Stacked on #2143** (`fix/nested-object-array-element`), which fixes the crash reading elements of nested object arrays. This PR's base is that branch, so the diff here shows only the perf changes. Once #2143 lands I'll rebase this branch onto `main` and retarget it. - #2137 also touches `Runtime.h` and `MethodCache.cpp`. This branch leaves the `Runtime.h` declarations alone, puts its inline definitions after the class, and merges cleanly with #2137 (`git merge-tree`). - Rebased onto `main` after #2140. #2140's char conversions (`ArgConverter::ToJavaChar`, `jcharToV8String` for element reads) and the boolean-field `Escape` are kept as they are. ### Does your pull request have [unit tests](https://github.com/NativeScript/NativeScript/blob/master/running-tests.md)? No new specs: these are behavior-preserving changes, covered by the existing suite. The full suite (`./gradlew runtestsAndVerifyResults`, arm64 emulator, android-36.1 google_apis image) passes on the rebased branch: 1788 specs, 0 failures. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Added Java array benchmarks for indexed integer reads and writes, object reads, and callbacks returning objects. * **Bug Fixes** * Improved Java array access and value conversion, including handling primitive values and null references. * Improved management of converted Java references during calls between JavaScript and Java. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 6a73e75 commit 56c7b0b

26 files changed

Lines changed: 373 additions & 283 deletions

‎test-app/app/src/main/assets/app/tests/benchmarks/jniBenchmarks.js‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,15 @@ function buildBenchmarks() {
177177
twoInts: function (a, b) { },
178178
oneString: function (s) { },
179179
intReturn: function (a) { return a + 1; },
180-
oneObject: function (o) { }
180+
oneObject: function (o) { },
181+
objectReturn: function () { return other; }
181182
});
183+
var ints = Array.create("int", 16);
184+
var objects = Array.create(java.lang.Object, 16);
185+
for (var k = 0; k < 16; k++) {
186+
ints[k] = k;
187+
objects[k] = new java.lang.Object();
188+
}
182189

183190
return [
184191
function () {
@@ -407,6 +414,26 @@ function buildBenchmarks() {
407414
return s;
408415
});
409416
},
417+
function () {
418+
return measureJs("array get int[i]", 50000, function (n) {
419+
var s = 0;
420+
for (var i = 0; i < n; i++) s += ints[i & 15];
421+
return s;
422+
});
423+
},
424+
function () {
425+
return measureJs("array set int[i]", 50000, function (n) {
426+
for (var i = 0; i < n; i++) ints[i & 15] = i;
427+
return n;
428+
});
429+
},
430+
function () {
431+
return measureJs("array get Object[i]", 50000, function (n) {
432+
var s;
433+
for (var i = 0; i < n; i++) s = objects[i & 15];
434+
return s;
435+
});
436+
},
410437
function () {
411438
return measureLifecycle("lifecycle new java.lang.Object()", 20000, function () {
412439
return new java.lang.Object();
@@ -470,6 +497,9 @@ function buildBenchmarks() {
470497
},
471498
function () {
472499
return measureCallback("callback.oneObject(binding)", callback, 7, 20000);
500+
},
501+
function () {
502+
return measureCallback("callback.objectReturn() -> Object", callback, 8, 20000);
473503
}
474504
];
475505
}

‎test-app/app/src/main/java/com/tns/tests/JniBench.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public interface Callback {
1919
int intReturn(int a);
2020

2121
void oneObject(Object o);
22+
23+
Object objectReturn();
2224
}
2325

2426
public int intField = 7;
@@ -251,6 +253,12 @@ public static long runCallbacks(Callback cb, int kind, int n) {
251253
// The callback is itself a binding: the argument carries its object id.
252254
for (int i = 0; i < n; i++) cb.oneObject(cb);
253255
break;
256+
case 8: {
257+
Object sink = null;
258+
for (int i = 0; i < n; i++) sink = cb.objectReturn();
259+
if (sink == null) throw new IllegalStateException("objectReturn returned null");
260+
break;
261+
}
254262
default:
255263
throw new IllegalArgumentException("kind " + kind);
256264
}

‎test-app/runtime/src/main/cpp/ArgConverter.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ class ArgConverter {
154154

155155
static std::u16string ConvertToUtf16String(const v8::Local<v8::String>& s);
156156

157-
inline static jstring ConvertToJavaString(const v8::Local<v8::Value>& jsValue) {
157+
inline static jstring ConvertToJavaString(v8::Isolate* isolate, const v8::Local<v8::Value>& jsValue) {
158158
JEnv env;
159-
auto isolate = v8::Isolate::GetCurrent();
160159
v8::Local<v8::String> str;
161160
if (jsValue->IsString()) {
162161
str = jsValue.As<v8::String>();

‎test-app/runtime/src/main/cpp/ArrayElementAccessor.cpp‎

Lines changed: 80 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ using namespace v8;
99
using namespace std;
1010
using namespace tns;
1111

12-
Local<Value> ArrayElementAccessor::GetArrayElement(Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature) {
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+
19+
Local<Value> ArrayElementAccessor::GetArrayElement(Isolate* isolate, Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature) {
1320
JEnv env;
1421

15-
Isolate* isolate = v8::Isolate::GetCurrent();
1622
EscapableHandleScope handleScope(isolate);
1723
auto runtime = Runtime::GetRuntime(isolate);
1824
auto objectManager = runtime->GetObjectManager();
@@ -25,61 +31,75 @@ Local<Value> ArrayElementAccessor::GetArrayElement(Local<Context> context, const
2531
jsize startIndex = index;
2632
const jsize length = 1;
2733

28-
const string elementSignature = arraySignature.substr(1);
29-
30-
if (elementSignature == "Z") {
31-
jbooleanArray boolArr = static_cast<jbooleanArray>(arr);
32-
jboolean boolArrValue;
33-
env.GetBooleanArrayRegion(boolArr, startIndex, length, &boolArrValue);
34-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &boolArrValue);
35-
} else if (elementSignature == "B") {
36-
jbyteArray byteArr = static_cast<jbyteArray>(arr);
37-
jbyte byteArrValue;
38-
env.GetByteArrayRegion(byteArr, startIndex, length, &byteArrValue);
39-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &byteArrValue);
40-
} else if (elementSignature == "C") {
41-
jcharArray charArr = static_cast<jcharArray>(arr);
42-
jchar charArrValue;
43-
env.GetCharArrayRegion(charArr, startIndex, length, &charArrValue);
44-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &charArrValue);
45-
} else if (elementSignature == "S") {
46-
jshortArray shortArr = static_cast<jshortArray>(arr);
47-
jshort shortArrValue;
48-
env.GetShortArrayRegion(shortArr, startIndex, length, &shortArrValue);
49-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &shortArrValue);
50-
} else if (elementSignature == "I") {
51-
jintArray intArr = static_cast<jintArray>(arr);
52-
jint intArrValue;
53-
env.GetIntArrayRegion(intArr, startIndex, length, &intArrValue);
54-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &intArrValue);
55-
} else if (elementSignature == "J") {
56-
jlongArray longArr = static_cast<jlongArray>(arr);
57-
jlong longArrValue;
58-
env.GetLongArrayRegion(longArr, startIndex, length, &longArrValue);
59-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &longArrValue);
60-
} else if (elementSignature == "F") {
61-
jfloatArray floatArr = static_cast<jfloatArray>(arr);
62-
jfloat floatArrValue;
63-
env.GetFloatArrayRegion(floatArr, startIndex, length, &floatArrValue);
64-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &floatArrValue);
65-
} else if (elementSignature == "D") {
66-
jdoubleArray doubleArr = static_cast<jdoubleArray>(arr);
67-
jdouble doubleArrValue;
68-
env.GetDoubleArrayRegion(doubleArr, startIndex, length, &doubleArrValue);
69-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &doubleArrValue);
70-
} else {
71-
jobject result = env.GetObjectArrayElement(static_cast<jobjectArray>(arr), index);
72-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &result);
73-
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+
}
7495
}
7596

7697
return handleScope.Escape(value);
7798
}
7899

79-
void ArrayElementAccessor::SetArrayElement(Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature, Local<Value>& value) {
100+
void ArrayElementAccessor::SetArrayElement(Isolate* isolate, Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature, Local<Value>& value) {
80101
JEnv env;
81102

82-
Isolate* isolate = v8::Isolate::GetCurrent();
83103
HandleScope handleScope(isolate);
84104
auto runtime = Runtime::GetRuntime(isolate);
85105
auto objectManager = runtime->GetObjectManager();
@@ -88,29 +108,29 @@ void ArrayElementAccessor::SetArrayElement(Local<Context> context, const Local<O
88108

89109
assertNonNullNativeArray(arr);
90110

91-
const string elementSignature = arraySignature.substr(1);
111+
const char elementType = PrimitiveElementType(arraySignature);
92112

93-
if (elementSignature == "Z") { //bool
113+
if (elementType == 'Z') { //bool
94114
jboolean boolElementValue = (jboolean) value->BooleanValue(isolate);
95115
jbooleanArray boolArr = static_cast<jbooleanArray>(arr);
96116
env.SetBooleanArrayRegion(boolArr, index, 1, &boolElementValue);
97-
} else if (elementSignature == "B") { //byte
117+
} else if (elementType == 'B') { //byte
98118
jbyte byteElementValue = (jbyte) value->Int32Value(context).ToChecked();
99119
jbyteArray byteArr = static_cast<jbyteArray>(arr);
100120
env.SetByteArrayRegion(byteArr, index, 1, &byteElementValue);
101-
} else if (elementSignature == "C") { //char
121+
} else if (elementType == 'C') { //char
102122
jchar charElementValue = ArgConverter::ToJavaChar(isolate, value->ToString(context).ToLocalChecked());
103123
jcharArray charArr = static_cast<jcharArray>(arr);
104124
env.SetCharArrayRegion(charArr, index, 1, &charElementValue);
105-
} else if (elementSignature == "S") { //short
125+
} else if (elementType == 'S') { //short
106126
jshort shortElementValue = (jshort) value->Int32Value(context).ToChecked();
107127
jshortArray shortArr = static_cast<jshortArray>(arr);
108128
env.SetShortArrayRegion(shortArr, index, 1, &shortElementValue);
109-
} else if (elementSignature == "I") { //int
129+
} else if (elementType == 'I') { //int
110130
jint intElementValue = value->Int32Value(context).ToChecked();
111131
jintArray intArr = static_cast<jintArray>(arr);
112132
env.SetIntArrayRegion(intArr, index, 1, &intElementValue);
113-
} else if (elementSignature == "J") { //long
133+
} else if (elementType == 'J') { //long
114134
jlong longElementValue;
115135
if (value->IsObject()) {
116136
longElementValue = (jlong) ArgConverter::ConvertToJavaLong(isolate, value);
@@ -119,11 +139,11 @@ void ArrayElementAccessor::SetArrayElement(Local<Context> context, const Local<O
119139
}
120140
jlongArray longArr = static_cast<jlongArray>(arr);
121141
env.SetLongArrayRegion(longArr, index, 1, &longElementValue);
122-
} else if (elementSignature == "F") { //float
142+
} else if (elementType == 'F') { //float
123143
jfloat floatElementValue = (jfloat) value->NumberValue(context).ToChecked();
124144
jfloatArray floatArr = static_cast<jfloatArray>(arr);
125145
env.SetFloatArrayRegion(floatArr, index, 1, &floatElementValue);
126-
} else if (elementSignature == "D") { //double
146+
} else if (elementType == 'D') { //double
127147
jdouble doubleElementValue = (jdouble) value->NumberValue(context).ToChecked();
128148
jdoubleArray doubleArr = static_cast<jdoubleArray>(arr);
129149
env.SetDoubleArrayRegion(doubleArr, index, 1, &doubleElementValue);
@@ -132,7 +152,7 @@ void ArrayElementAccessor::SetArrayElement(Local<Context> context, const Local<O
132152
if (isReferenceType) {
133153
auto object = value.As<Object>();
134154

135-
JsArgToArrayConverter argConverter(context, value, false, (int) Type::Null);
155+
JsArgToArrayConverter argConverter(isolate, context, value, false, (int) Type::Null);
136156
if (argConverter.IsValid()) {
137157
jobjectArray objArr = static_cast<jobjectArray>(arr);
138158
jobject objectElementValue = argConverter.GetConvertedArg();
@@ -147,51 +167,6 @@ void ArrayElementAccessor::SetArrayElement(Local<Context> context, const Local<O
147167
}
148168
}
149169

150-
Local<Value> ArrayElementAccessor::ConvertToJsValue(Isolate* isolate, ObjectManager* objectManager, JEnv& env, const string& elementSignature, const void* value) {
151-
Local<Value> jsValue;
152-
153-
if (elementSignature == "Z") {
154-
jsValue = Boolean::New(isolate, *(jboolean*) value);
155-
} else if (elementSignature == "B") {
156-
jsValue = Integer::New(isolate, *(jbyte*) value);
157-
} else if (elementSignature == "C") {
158-
jsValue = ArgConverter::jcharToV8String(isolate, *(jchar*) value);
159-
} else if (elementSignature == "S") {
160-
jsValue = Integer::New(isolate, *(jshort*) value);
161-
} else if (elementSignature == "I") {
162-
jsValue = Integer::New(isolate, *(jint*) value);
163-
} else if (elementSignature == "J") {
164-
jsValue = ArgConverter::ConvertFromJavaLong(isolate, *(jlong*) value);
165-
} else if (elementSignature == "F") {
166-
jsValue = Number::New(isolate, *(jfloat*) value);
167-
} else if (elementSignature == "D") {
168-
jsValue = Number::New(isolate, *(jdouble*) value);
169-
} else {
170-
if (nullptr != (*(jobject*) value)) {
171-
bool isString = elementSignature == "Ljava/lang/String;";
172-
173-
if (isString) {
174-
jsValue = ArgConverter::jstringToV8String(isolate, *(jstring*) value);
175-
} else {
176-
jint javaObjectID = objectManager->GetOrCreateObjectId(*(jobject*) value);
177-
jsValue = objectManager->GetJsObjectByJavaObject(javaObjectID);
178-
179-
if (jsValue.IsEmpty()) {
180-
// Named after the element's runtime class, as a method result is: the
181-
// wrapper's name must be a JNI class name, and it is later read back as
182-
// the array signature of the element itself.
183-
auto className = objectManager->GetClassName(*(jobject*) value);
184-
jsValue = objectManager->CreateJSWrapper(javaObjectID, className);
185-
}
186-
}
187-
} else {
188-
jsValue = Null(isolate);
189-
}
190-
}
191-
192-
return jsValue;
193-
}
194-
195170
void ArrayElementAccessor::assertNonNullNativeArray(tns::JniLocalRef& arrayReference) {
196171
if(arrayReference.IsNull()){
197172
throw NativeScriptException("Failed calling indexer operator on native array. The JavaScript instance no longer has available Java instance counterpart.");

‎test-app/runtime/src/main/cpp/ArrayElementAccessor.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
namespace tns {
1010
class ArrayElementAccessor {
1111
public:
12-
v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature);
12+
v8::Local<v8::Value> GetArrayElement(v8::Isolate* isolate, v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature);
1313

14-
void SetArrayElement(v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature, v8::Local<v8::Value>& value);
14+
void SetArrayElement(v8::Isolate* isolate, v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature, v8::Local<v8::Value>& value);
1515

1616
private:
17-
v8::Local<v8::Value> ConvertToJsValue(v8::Isolate* isolate, ObjectManager* objectManager, JEnv& env, const std::string& elementSignature, const void* value);
1817
void assertNonNullNativeArray(tns::JniLocalRef& arrayReference);
1918
};
2019
}

0 commit comments

Comments
 (0)