Skip to content

feat: warn when Java calls a non-class method - #2139

Draft
edusperoni wants to merge 4 commits into
mainfrom
feat/warn-instance-override-dispatch
Draft

edusperoni wants to merge 4 commits into
mainfrom
feat/warn-instance-override-dispatch

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Current behaviour. A Java→JS call looks the method up on the JS instance on every call (jsObject->Get(context, methodName) in CallbackHandlers::CallJSMethod). So whatever the instance reaches at call time runs:

  • an own property of the instance: a per-instance override (listener.onClick = …), a test spy, or a TypeScript class-field arrow function (onClick = (v) => … in a @NativeClass class);
  • a method reassigned on the class or its implementation object after extend().

The iOS runtime binds a class's methods when the class is extended and never sees either case. Before deciding whether Android should match iOS, this PR measures how much real apps depend on the current behaviour.

What this adds (debug builds only, no behaviour change).

  • Capture. extend() records the implementation object's own function-valued data properties (DispatchOverrides, one per class). It reads property descriptors, so no getter runs. The record is owned by the runtime's MetadataNodeCache, so each worker isolate has its own, and its v8::Globals are released at runtime teardown while the isolate is still alive.

  • Lookup key. ObjectManager::Link stores a pointer to the class's record in the wrapper's link state. Java→JS calls already fetch that state to find the JS object, so getting the record costs one field load. This covers instances made by the JS constructor, by the TypeScript __extends path and by Java (CreateJSInstanceNative). Inside the record, entries are indexed by Java's method-name id (MethodNameIds). Those ids are assigned on Java's first call with each name, so each class+method entry is bound by name on its first call.

  • Check. After the lookup, the function the call reached is compared with the captured one (handle identity). On a mismatch the runtime logs this once per class and method under the JS tag (logcat):

    Java called "onClick" on com.tns.gen.…MyListener through a function other than the class's own implementation (an instance property or a method replaced after extend) defined at file:///…/main-page.js:12:5. This may stop working in a future version, as on iOS. Define it on the class. Please report your use case.

    If the class doesn't define the name at all, the first call is accepted as the class's own implementation when it resolves to something the class inherits. It warns only if the instance, its prototype or the implementation object owns the name; that is the class-field-only case.

  • No JS runs during the check. It doesn't call console.warn, because a JS call at depth 0 would drain microtasks before the dispatched method runs. It doesn't throw either. After the check, the call still goes to the function it reaches today.

Cost.

  • Release apps (nativescript-optimized, no APPLICATION_IN_DEBUG): the capture, the per-wrapper link-state field and the check are all behind #ifdef. What remains is two extra arguments passed through CallJSMethod, which release builds ignore, and two pointer fields in the per-class cache structs that are always null.
  • Debug apps (nativescript-optimized-with-inspector, -O3), per Java→JS call: one load of the record pointer from the link state the call already holds, then a null check, a bounds check, a flag test and a handle comparison. There is no extra property lookup and no call. In the arm64 disassembly of CallJSMethod, the inlined check is 17 instructions (6 loads, 6 well-predicted branches). The slow path runs once per class+method.
  • Full suite (1782 specs, emulator): no failures. The only warnings logged were the seven this PR's own specs expect, so no existing spec relies on an instance override being called from Java.
  • Interface literals (new SomeInterface({...}), debug builds only): one own-property lookup on the receiver per Java→JS call.
  • Extend time: one own-property-names pass with a descriptor read for each name.
  • Memory (debug only): each class record has an entry table indexed by the process-wide method-name id, so it costs 16 bytes × the highest id Java has called on that class.

new SomeInterface({...}) instances. Each of these gets its own implementation object as its prototype, so there's no per-class record to compare against. For any wrapper without a record, debug builds instead check whether the receiver itself owns the method name (HasRealNamedProperty, which runs neither interceptors nor getters). If it does, the runtime logs the same warning once per Java class and method in each runtime. A function reassigned on the implementation object itself isn't detected on these instances.

Goal. Collect reports from real apps that hit this warning before deciding whether to bind methods at extend time, as iOS does.

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

No.

Related Pull Requests

None.

Does your pull request have unit tests?

Yes. testDispatchOverrideWarnings.js covers these cases:

  • a class's own method: called, no warning;
  • an instance own property: called, warns;
  • a TypeScript class field (TS emit with @Interfaces): called, warns;
  • a method replaced on the implementation object after extend(): warns;
  • a method added to the class prototype after extend(): warns;
  • a plain new java.lang.Runnable({ run }): called, no warning;
  • the same with r.run = … afterwards: the own property is called, and two calls give one warning;
  • an instance created from Java with an own property: warns;
  • three instances called twice each: one warning.

Each spec checks that Java actually reached the JS function. It counts warnings with a new LogcatUtil.countJsWarnings, which reads logcat -d -s JS:W without clearing the log and counts only this process's lines; the existing logcatContainsString clears the log first. The branch for a name the class doesn't define but inherits has no spec: I couldn't build a binding that routes such a name to JS. The warning goes to logcat only, not to the DevTools console.

Java->JS calls look the method up on the JS instance per call, so an own
property of the instance (a per-instance override, a spy, a TypeScript
class-field arrow function) or a method replaced after extend() is what
runs. The iOS runtime binds a class's methods when it is extended and
never sees either. Before deciding whether to align Android with iOS, we
want to know how often apps rely on this.

In debug builds (APPLICATION_IN_DEBUG), each extend() now captures the
implementation object's own function-valued data properties. The class's
record is reached from the wrapper's link state, which the call path
already fetches, and it is keyed lazily by Java's method-name id. A call
that resolves to a different function than the captured one is logged
once per class and method under the JS tag, with the location of the
function that ran. For a name the class does not define, the first call
resolves to whatever the class inherits, unless the instance, its
prototype or the implementation object owns the name.

Behaviour is unchanged: the call still goes to the function it reaches
today. Release builds (nativescript-optimized) compile none of it.

Instances of `new SomeInterface({...})` are not checked yet: their
implementation object is per instance, so a per-class record does not
apply to them.
Specs for a class's own method (no warning), an instance own property, a
TypeScript class field, a method replaced on the implementation object
and one added to the class prototype after extend(), a Java-created
instance, and repeated calls on several instances (one warning). Each
spec checks the JS function Java reached and counts the warning in
logcat through a LogcatUtil helper that leaves the log in place.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

The log outlives the app, so a second run on the same device counted the
warnings of the first. Lines are now matched to android.os.Process.myPid();
logcat's --pid filter needs API 24.
`new SomeInterface({...})` gives every instance its own implementation
object, so it has no per-class record to compare against. For wrappers
without a record, debug builds now check whether the receiver itself owns
the method name and, if so, log the same warning once per Java class and
method per runtime. This costs one own-property lookup per Java->JS call
on those instances, in debug builds only; records keep the pointer
compare.

ObjectManager::GetJavaClass now returns null for an object without a
link instead of dereferencing a null info.

Specs: a plain interface literal called from Java does not warn; one whose
instance gets its own `run` still runs it and warns once over two calls.

This branch has not been deployed

No deployments
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