Skip to content

fix(runtime): keep a constructed instance's recorded class - #2132

Merged
edusperoni merged 1 commit into
mainfrom
fix/constructor-class-ref
Nov 20, 2026
Merged

edusperoni merged 1 commit into
mainfrom
fix/constructor-class-ref

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Runtime::CallJSMethodNative, on the Java constructor's call into JS (isConstructor), replaced the class recorded for the instance with env.GetObjectClass(obj). obj is the receiver of the native method, the com.tns.Runtime object, not the instance, and the result is a JNI local reference that is freed when the native call returns. So for a JS-extended instance whose constructor calls into JS, the recorded class became a dead local ref to com.tns.Runtime's class.

On main only one place reads it: the "Failed to get Java object by ID. id=…, class=…" error, which therefore named com.tns.Runtime or read a freed reference. Registration (CallbackHandlers::RegisterInstance, Runtime::CreateJSInstanceNative) already records the instance's real class as a cached global ref, so the overwrite is simply removed. Found while porting the canary wrapper lifetime mode (#2127), which keeps the recorded class.

Testing

Full suite on an arm64 API 36.1 emulator: 1773 specs, 0 failures. No spec: the only observable effect on main is the class name in that error message, reached when a Java object lookup fails.

Summary by CodeRabbit

  • Behavior Changes
    • During JavaScript constructor calls, the linked Java instance’s class is no longer updated based on the supplied object. This may affect how the instance is associated with its Java class after construction.

CallJSMethodNative replaced the class recorded for a JS-extended
instance, on the Java constructor's call into JS, with
env.GetObjectClass(obj): a JNI local ref, freed when the call returned,
to com.tns.Runtime's class (obj is the receiver of the native method,
not the instance). The error a failed Java object lookup reports names
that class, so it named com.tns.Runtime or read a dead reference.
Registration already records the instance's class as a global ref.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The constructor callback path in Runtime::CallJSMethodNative no longer reads obj’s Java class or sets that class on the linked JavaScript instance.

Changes: Constructor Callback

Layer / File(s) Summary
Remove constructor class update
test-app/runtime/src/main/cpp/Runtime.cpp
The isConstructor branch that obtained obj’s class and called SetJavaClass was removed.

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

Change: Bug fix

Merge Risk: Minimal · up to 3f56f

This change stops a JS-extended instance's recorded Java class from being overwritten with an invalid, wrong reference during a constructor callback. The class registered by the runtime is now left intact. The change is small and affects only an error-message path, so the risk of merging 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 change: the constructor path in Runtime::CallJSMethodNative no longer overwrites the recorded class of a constructed instance. The raw summary confirms the removal of the isConstru…
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 change: the constructor path in Runtime::CallJSMethodNative no longer overwrites the recorded class of a constructed instance. The raw summary confirms the removal of the isConstructor block that updated the Java class from obj, and the objectives describe the same intent of keeping the class reference recorded during registration. The title is specific, follows the conventional prefix style, and is 58 characters long.

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 a121063 and 3f56f9c.

Files selected for processing: 1

  • test-app/runtime/src/main/cpp/Runtime.cpp
Files with no reviewable changes: 1

  • test-app/runtime/src/main/cpp/Runtime.cpp

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

@edusperoni
edusperoni marked this pull request as ready for review November 20, 2026 19:55
@edusperoni
edusperoni merged commit 5e43948 into main Nov 20, 2026
8 checks passed
@edusperoni
edusperoni deleted the fix/constructor-class-ref branch November 20, 2026 22:31
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