Title: fix: call JS overrides of default methods by edusperoni · Pull Request #2118 · NativeScript/android · GitHub
Open Graph Title: fix: call JS overrides of default methods by edusperoni · Pull Request #2118 · NativeScript/android
X Title: fix: call JS overrides of default methods by edusperoni · Pull Request #2118 · NativeScript/android
Description: Description A JS implementation of an interface default method was never called from Java when the class was generated on the device. This makes it work. The change is unconditional, not opt-in. Stacked on #2117 (both change the runtime class generator). Review the commits after #2117's; the base will become main once #2117 is merged. The bug The on-device generator stubbed only an interface's abstract methods and ignored which methods the JS object defines. So for an instance whose class was generated at runtime, a default method always ran its Java body. At minSdk 21 the build desugars a library's default methods into abstract ones, so they were stubbed and JS was called. From minSdk 24 they stay real default methods and JS was silently never called. The current app template uses minSdk 24. Platform default methods (Iterator.remove, Comparator.reversed, Map.getOrDefault, …) were affected at any minSdk. A class is generated at runtime whenever the build cannot see the full interface name: a namespace alias (const ns = com.example; new ns.Listener({...})), destructuring, or code served by a dev server during HMR. The typical victim is a listener interface whose methods are all default: every callback silently did nothing. This was first worked around in a plugin by spelling out the full name (nstudio/nativescript-plugins#95). A prebuilt class (full name visible to the static binding generator) already overrides the default methods the JS object lists, which is why the full name worked. The fix Nothing changes for an instance whose JS object defines no default method: it gets the same class as before, with no added cost per call. When the JS object does define one, the instance gets a second generated class for that interface (the usual name plus -defaults): It stubs every abstract method as before, and every real default method. Each instance records which default methods its JS object implements, in a bit mask written right after construction. Implemented: the call goes to JS, like any abstract method. Not implemented: the Java body runs on the calling thread with invoke-super on the interface the class implements, exactly what D8 emits for I.super.m(). JS is not entered and nothing is boxed. A JS property counts as an implementation when it is an own data property holding a function, or an own accessor. Getters are not run at construction, including when the second class is first resolved. A cached copy of the second class is keyed by the interface's default method signatures, so it is not reused if a platform update adds a default method or an overload of one. This covers new Interface({...}) and Interface.extend({...}). Two further commits cover the other shapes: Class extends: a JS method that overrides a default method the class inherits, or one from interfaces: [...], is now called. Named interface extends (Interface.extend("a.b.C", {...}), @JavaProxy on an interface) when the class is generated at runtime. A named class belongs to one extend call, so it needs neither the second class nor the mask: it stubs exactly the default methods its JS object implements. When the app has a prebuilt class for the interface, it is still used, as before. Safety net for invoke-super The generated class is written as dex 037 and carries a small probe class in the same file. The first -defaults class a process loads runs that probe once (0.3–0.6 ms on the phone): it calls defaults through invoke-super, including one declared in a super-interface, one through a bridge, and ones that call back into the instance, and checks the results and that nothing re-entered. If the probe fails, that process generates the class again under -defaults-jni with stubs that run the Java body through a JNI non-virtual call instead, and uses those from then on. The suite exercises these stubs too, on interfaces reserved for it. The bundled dex writer now takes the dex version per writer instead of from a static constant. Cost Unimplemented default called from Java, phone (API 33), release build, ns per call: no arguments one object argument (long, double) → long Plain inherited default (instance implements none) 1.1–1.2 2.8–3.0 1.3–2.1 -defaults class, invoke-super 2.8–2.9 4.6–4.7 3.5–3.6 -defaults-jni fallback 387–390 598–603 828–834 These were measured on the experimental branch this was folded in from; the stub code is unchanged. new on a debuggable build: 12.6–15.7 µs for an interface without default methods, before and after (one atomic load added); 15.8–17.1 µs for Iterator with one default implemented, against 13.1–14.3 µs. Verification Reproduced on an API 33 phone before the fix: an aliased java.util.Iterator with a JS remove ran the Java body and never called JS; an aliased app interface with only default methods called JS at minSdk 21 and never at minSdk 24. Full suite at minSdk 21 and at minSdk 24, 1640 specs: Device Result Phone, API 33 0 failures Emulators, API 29, 30, 31, 32, 34, 35, 36 0 failures Emulators, API 24, 25, 26, 27 2 failures: HTTP module-import timeouts, also on the base commit Emulator, API 28 3 failures: ConcurrentHashMap iterator specs, also on the base commit and fixed separately in #2122; plus one timer spec once, which did not recur The 29 default-method specs pass in every run, and the probe passed on every level. invoke-super was also run as ahead-of-time compiled code on the API 24, 26, 28, 30 and 31 emulators (forced as root) and under the JIT everywhere. :runtime:testDebugUnitTest: 46 tests, 0 failures, including tests that read each variant's dex back. CI runs the suite at minSdk 21 on API 33 only. The specs use platform interfaces, whose default methods are real at any minSdk, so both paths are exercised there; the minSdk 24 runs and the other API levels were local. Not verified: API levels above 36, and compiled code on a physical device. Limits An interface with more than 64 distinct default method names: JS implementations of those past the 64th are not called (their Java bodies run), and a warning is logged. An instance Java constructs by class name runs every default's Java body. At minSdk 21, an app base class whose default method was desugared is unchanged: the forwarding method is synthetic and still skipped. getClass().getName() of an instance that implements a default method ends in -defaults. Not included The static binding generator still takes a prebuilt interface class's default-method overrides from the first call site it parses. An interface extend that also lists extra interfaces: [...]: see #2121. At minSdk 21 a desugared default that the JS object omits still ends in AbstractMethodError. toString/hashCode/equals of a runtime-generated interface implementation are still not routed to JS. A getter that throws still aborts the process when Java later calls that method, for default and abstract methods alike; that is in the existing Java-to-JS call path. Two unnamed .extend calls on the same interface still share one JS constructor. That defect exists on main for every interface extend; #2116 fixes it with a per-call-site cache key. Interaction with #2116 Whichever of the two merges second needs a follow-up: The merge conflicts in eight files. Most are both sides adding a parameter or field in the same place; the non-virtual call helper is byte-identical in both. #2116's opt-in interface Proxy copies the old behaviour (a default method always runs its Java body), and two of its specs pin it. The handler needs the same per-instance record, and those specs must flip. A proxy cannot use invoke-super, so it keeps the JNI helper. Related Pull Requests #2117 (base of this PR) #2121 (stacked on this one) #2116 #2122 (fixes the API 28 failures listed under Verification) Does your pull request have unit tests? Yes: 29 specs in testInterfaceDefaultMethodOverrides.js (implemented and unimplemented defaults, calling thread, mixed instances, accessors, argument and return kinds, a generic default narrowed by a sub-interface, a checked exception, .extend, a worker, class extends, named extends through a dotted name and through JavaProxy with their prebuilt counterparts, an all-default app interface with expectations keyed on whether the build desugared it, and the fallback stubs), plus JVM tests of the method collection and of both generated variants.
Open Graph Description: Description A JS implementation of an interface default method was never called from Java when the class was generated on the device. This makes it work. The change is unconditional, not opt-in. S...
X Description: Description A JS implementation of an interface default method was never called from Java when the class was generated on the device. This makes it work. The change is unconditional, not opt-in. S...
Opengraph URL: https://github.com/NativeScript/android/pull/2118
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:bfe45544-55f8-ef32-4eb3-a5ad133c8bc8 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C0E2:24A0A:E16073:11B6FEB:6B013896 |
| html-safe-nonce | eb60aefe32893be84a9e516daa466214de793bc2c842d7ed5920294d8369d598 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMEUyOjI0QTBBOkUxNjA3MzoxMUI2RkVCOjZCMDEzODk2IiwidmlzaXRvcl9pZCI6IjIxODU3NTUxMjQxMTczNTQ2NDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6e47f45e5d53cfe33d1802caaacd71d697dea415737cea0b77d3fcb0c2f9df54 |
| hovercard-subject-tag | pull_request:5351966164 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/android/pull/2118/files |
| twitter:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| og:image:alt | Description A JS implementation of an interface default method was never called from Java when the class was generated on the device. This makes it work. The change is unconditional, not opt-in. S... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/android git https://github.com/NativeScript/android.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 29137950 |
| octolytics-dimension-repository_nwo | NativeScript/android |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 29137950 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/android |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width