Skip to content

fix(runtime): read elements of nested object arrays - #2143

Merged
edusperoni merged 1 commit into
mainfrom
fix/nested-object-array-element
Nov 21, 2026
Merged

edusperoni merged 1 commit into
mainfrom
fix/nested-object-array-element

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Current behavior. Reading an element of a nested object array crashes the app. For example, outer[0] where outer is a String[][]:

var strings = java.lang.reflect.Array.newInstance(java.lang.String.class, [2, 3]);
var row = strings[1]; // aborts

When the element has no JS wrapper yet, ArrayElementAccessor::ConvertToJsValue names the new wrapper from the declared element signature, passed through Util::JniClassPathToCanonicalName. For [Ljava/lang/String; that yields "[java.lang.String", which is not a JNI class name. ObjectManager::CreateJSWrapperHelper passes it to FindClass. A debug build of main running the new spec on an arm64 emulator aborts under CheckJNI:

JNI DETECTED ERROR IN APPLICATION: illegal class name '[java.lang.String'
    (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')
    in call to FindClass

The name is also not one the metadata reader resolves: it splits names on / and $ only. The wrapper's node name is later read back as the array signature for indexing into that element.

New behavior. The element's wrapper is named after its runtime class (ObjectManager::GetClassName, e.g. [Ljava/lang/String;). Nested arrays returned from methods or read from fields are already named this way. Non-array elements already took this branch and are unchanged.

Nested primitive arrays (int[][]) were not affected, since their canonical and JNI names are the same ([I). I checked this on main: the new int[][] spec passes there without the fix. The same is true of the new Class/null element spec.

Does your commit message include the wording below to reference a specific issue in this repo?

No.

Related Pull Requests

Split out of #2141, which is stacked on this branch.

Does your pull request have unit tests?

Yes. java-array-test.js gains specs for reading and writing elements of nested object arrays (String[][]) and nested primitive arrays (int[][]), and for Class and null elements of an Object[]. The full suite passes on an arm64 emulator (android-36.1 google_apis image): 1788 specs, 0 failures.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of nested Java arrays, including reading and updating elements while preserving row identity and type information.
    • Java objects stored in object arrays now retain their runtime class when accessed, and uninitialized elements remain null.
    • These changes support arrays created dynamically through Java reflection.

Reading an element of a nested object array (String[][] and the like)
named the element's wrapper by running the declared element signature
through JniClassPathToCanonicalName, which turns "[Ljava/lang/String;"
into "[java.lang.String". That name reached FindClass, which rejects it:
under CheckJNI the process aborts with "illegal class name
'[java.lang.String'". It was also not a name the metadata reader can
resolve, and the wrapper's node name is later read back as the element
array's own signature.

The element's wrapper is now named after its runtime class, the same
way a nested array returned from a method or read from a field is.
Nested primitive arrays (int[][]) were not affected, since their
canonical and JNI names are the same.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 719f538 and 719f538.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The runtime now uses each Java object’s runtime class name when wrapping array elements. Tests cover nested primitive and String arrays, row identity and updates, and Java class objects stored in Object arrays.

Changes: Java Array Element Wrapping

Layer / File(s) Summary
Runtime array element wrapping and tests
test-app/runtime/src/main/cpp/ArrayElementAccessor.cpp, test-app/app/src/main/assets/app/tests/java-array-test.js
ConvertToJsValue uses the Java object’s runtime class name for wrapped objects, including array elements. Tests cover nested primitive and String array rows, and Object-array values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 719f5

This fixes reading elements of nested object arrays such as String[][], which could previously abort under CheckJNI. The change is small and targeted, and it is covered by new specs. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: reading elements of nested object arrays in the runtime. The raw summary says ConvertToJsValue now takes the wrapper class name from the Java object's runtime class in…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: reading elements of nested object arrays in the runtime. The raw summary says ConvertToJsValue now takes the wrapper class name from the Java object's runtime class instead of the declared element signature. That matches the fix described in the title. The title uses the conventional fix(runtime) prefix and is concise, specific, and free of file lists.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 48d1fb1 and 719f538.

Files selected for processing: 2

  • test-app/app/src/main/assets/app/tests/java-array-test.js
  • test-app/runtime/src/main/cpp/ArrayElementAccessor.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@edusperoni
edusperoni added this pull request to stack #2144 November 21, 2026 19:50
@edusperoni
edusperoni merged commit 6a73e75 into main Nov 21, 2026
8 checks passed
@edusperoni
edusperoni deleted the fix/nested-object-array-element branch November 21, 2026 20:04
edusperoni added a commit that referenced this pull request Nov 21, 2026
### 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 -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant