Title: feat: support native ES classes with lazy registration by NathanWalker · Pull Request #1983 · NativeScript/android · GitHub
Open Graph Title: feat: support native ES classes with lazy registration by NathanWalker · Pull Request #1983 · NativeScript/android
X Title: feat: support native ES classes with lazy registration by NathanWalker · Pull Request #1983 · NativeScript/android
Description: Parity with NativeScript/ios#403 Makes plain ES2015+ classes that extend native types work directly on Android, without requiring @NativeClass or ES5 downleveling: class JSClass extends android.view.View { toString() { return 'hello from ES class'; } } Lazy registration Construction is not the only way a class first crosses into native code. All of the following now trigger lazy registration, before any instance has ever been created: class JSClass extends java.lang.Object {} someNativeMethod(JSClass); // passed as a Class (or Object) argument JSClass.class; // .class before any construction JSClass.class.newInstance(); // Java reflection constructs the proxy JSClass.STATIC_FIELD; // inherited static field JSClass.someStaticMethod(); // inherited static method dispatch Instance identity new MyClass() and Java-born construction (Class.newInstance(), view inflation, framework construction) now produce the same kind of JS instance: a real construct of the ES class. Public fields, private fields (#a), and the constructor body run on both paths. The constructor → super() loop is broken with an isolate-local adopt slot (PendingESAdoptObjectId): Java already created object N1. CreateJSInstanceNative(N1) stashes N1's id and constructs MyClass. super() binds this to N1 and does not NewObject again. super(args) stays the create-path constructor picker. Adopt ignores those args so the Java constructor that already ran stays authoritative. class MyClass extends com.tns.tests.DummyClass { #a = 1; constructor() { super('from-super'); } someMethod() { return this.#a; } } new MyClass().someMethod(); // 1, nameField === 'from-super' MyClass.class.newInstance().someMethod(); // 1, nameField === 'dummy' (no-arg Java ctor) Not solved here (not deal-breakers): Constructors that require JS-only arguments cannot be invented on the native-born path (CreateJSInstanceNative constructs with zero args). JS-only methods still need to be named as Java overrides (or listed for the DexFactory scan) to be callable via invokevirtual. A constructor that throws after super() leaves a partial JS↔Java link; a later wrap of the same id does not construct again. Worker isolates: NativeClass and ES class registration are a no-op. Only the main isolate mints native subclasses. Legacy .extend() is unchanged. NativeClass decorator API @NativeClass is no longer a no-op. Optional android options map to the existing statics and can eagerly name the Java proxy class: @NativeClass({ android: { interfaces: [java.lang.Runnable], name: 'org.nativescript.example.CustomActivity', }, // accepted and ignored on this runtime ios: { name: 'MyNeatIOSClass', protocols: [UIDelegateAnything], }, }) class MyClass extends android.view.View {} All properties are optional. android.interfaces → static interfaces android.name → static nativeClassName plus eager registration via .class This runtime implements android only; ios is accepted and ignored @NativeClass and @NativeClass({ android: { … } }) are both valid. Passing the class directly (NativeClass(MyClass)) applies empty options. On worker isolates this is a no-op. Tests See test-app/app/src/main/assets/app/tests/testNativeESClasses.js, including: lazy .class / Class marshalling before construction Java virtual dispatch into ES overrides and super multi-level ES inheritance When_java_instantiates_an_es_class_the_js_constructor_and_fields_should_run When_java_instantiates_an_es_class_private_fields_should_be_readable When_java_instantiates_an_es_class_super_args_should_not_construct_again When_an_es_class_constructor_throws_both_paths_should_surface_the_error When_the_NativeClass_decorator_is_applied_it_should_apply_android_options When_NativeClass_sets_an_android_name_the_proxy_should_register_immediately When_NativeClass_runs_on_a_worker_it_should_be_a_noop Summary by CodeRabbit New Features Added support for extending native Java classes and interfaces with modern JavaScript ES classes. Added lazy native proxy registration and improved class and constructor marshalling. Added support for static members, private fields, super calls, constructor forwarding, and Java-created ES-class instances. Added the global NativeClass decorator with interface and explicit naming options. Bug Fixes Prevented duplicate superclass construction and improved constructor error propagation. Preserved compatibility with legacy .extend() classes while rejecting unsupported ES-class usage.
Open Graph Description: Parity with NativeScript/ios#403 Makes plain ES2015+ classes that extend native types work directly on Android, without requiring @NativeClass or ES5 downleveling: class JSClass extends android.vie...
X Description: Parity with NativeScript/ios#403 Makes plain ES2015+ classes that extend native types work directly on Android, without requiring @NativeClass or ES5 downleveling: class JSClass extends android.vie...
Opengraph URL: https://github.com/NativeScript/android/pull/1983
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:8db60737-70fc-91ea-e343-c543f2435835 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | CD72:3C21B:185904A:1F250AE:6B01314E |
| html-safe-nonce | ca4933d8e81260dc8bdaaa38198c6be94c118f51a7c4a0661cd6bc9d4592ab1b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDcyOjNDMjFCOjE4NTkwNEE6MUYyNTBBRTo2QjAxMzE0RSIsInZpc2l0b3JfaWQiOiI4MDYyODM1NTgyNzk3MTY0ODc4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b17a4b7a1e3dbfd9addf6d34839cf30d44c0823d37d9d50f0563064c0a2c0ea4 |
| hovercard-subject-tag | pull_request:4071067552 |
| 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/1983/files |
| twitter:image | https://avatars.githubusercontent.com/u/457187?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/457187?s=400&v=4 |
| og:image:alt | Parity with NativeScript/ios#403 Makes plain ES2015+ classes that extend native types work directly on Android, without requiring @NativeClass or ES5 downleveling: class JSClass extends android.vie... |
| 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