Title: feat(runtime): weak collections key Java objects in canary mode by edusperoni · Pull Request #2128 · NativeScript/android · GitHub
Open Graph Title: feat(runtime): weak collections key Java objects in canary mode by edusperoni · Pull Request #2128 · NativeScript/android
X Title: feat(runtime): weak collections key Java objects in canary mode by edusperoni · Pull Request #2128 · NativeScript/android
Description: Description Fourth of the stacked wrapper-lifetime series (on #2127), the Android port of NativeScript/ios#563. With #2127 alone, a Java object's canary dies with its JS references while the Java object lives on. A later wrapper for the same object is a new JS object, so WeakMap/WeakSet entries, WeakRef targets and FinalizationRegistry registrations keyed on the old wrapper are lost with it. New behavior In canary mode a builtin (js/weak-collections.js, the iOS file with comments adapted, run from WeakRef.cpp) replaces the four globals with subclasses: A Java object whose wrapper is a canary is keyed by an opaque per-object key: frozen, null-prototype, created on first use by set/add/getOrInsert/register/new WeakRef. Lookups (get, has, delete, unregister) never create it, and neither does a call the standard collection rejects: set, add, getOrInsert and register check their receiver first (a call that cannot insert anything, whose TypeError names has, or unregister for a registry), register leaves an invalid target or token to the native method, and getOrInsertComputed runs its callback before making the key, so a callback that throws leaves none. The object's state holds the key, so it lives exactly until Java collects the object; the key does not keep the Java object alive. getOrInsert and getOrInsertComputed are keyed the same way; the computed callback receives the key it was given. WeakRef.prototype.deref (and the get alias) returns the live canary, a new one with the object's state (a rebuilt JS-extended instance, or a plain wrapper made the usual way and hydrated), or undefined once Java collected the object. Like a native target, what it returns stays alive until the job ends (the subclass keeps it alive only when wrap replaced what the native deref returned; the native method already keeps a target it returns as is). After __releaseNativeCounterpart a canary is a plain JS object, and what weak collections keyed on its Java object stays keyed on it: before the record goes, the key moves onto the canary and a private on the key leads back to it, so WeakMap/WeakSet entries, WeakRef.deref and FinalizationRegistry behave as for a plain object while the released wrapper lives, and the key and wrapper die together. A Proxy around a Java object is a key of its own, as in every other mode. register(target, target) still throws, checked on the original values; register.length is 2 and a call the native method rejects gets its error (register(1, 1) reports the invalid target). Values that are not canaries (including plain wrappers under "extended") pass through unchanged. Other modes keep V8's built-ins. The key and its reverse lookup are internal (ObjectManager::UnwrapForWeakCollection/WrapForWeakCollection, privates on the state and the key) and not exported, as on iOS. Both natives have the exception boundary other V8 callbacks have (C++ exceptions are rethrown to V8) and find no object manager, rather than throwing, when the isolate has no runtime. The builtin captures the native methods at startup through primordials (which gains WeakMap and its methods and Symbol.keyFor) instead of resolving them through super at call time. Trade-off: a WeakMap value that references its own Java key keeps the key, its state and so the Java object alive for as long as the map lives. In canary mode an operation pays at least one native call for each object key, target or token it is given (primitives pay none), and one that can create the key also pays the receiver check. deref builds a canary, which holds the Java object strongly until it dies again, so a WeakRef to a Java object dereferenced on every frame or event may keep it from ever being collected. A state made only to hold a weak-collection key counts in canaryStates and in getWrapperClassCounts()'s states, though no JS property was written. iOS #563 also moved NSArray's per-instance Symbol.iterator to the prototype, because under canary that own property was state on every array. Android installs no per-instance properties on wrappers, so there is nothing to port. Testing New testWrapperLifetimeWeakCollections.js, which holds in every mode: WeakMap, WeakSet and getOrInsert/getOrInsertComputed across a wrapper change of a Java-held object; WeakRef deref (and get) while the object lives, with its state; WeakRef cleared and FinalizationRegistry callback after Java collected the object; unregister through a later wrapper of its token; the register(target, target) rejection; a Proxy key; lookups that create no state; other keys unaffected; the builtin installed only in canary mode. Also: a WeakMap entry and WeakRef target for a Java-held plain object across a wrapper change; entries given to the WeakMap and WeakSet constructors and set through a user subclass of WeakMap; no FinalizationRegistry callback while Java holds the object and its wrapper is dead; what was keyed on a canary kept after __releaseNativeCounterpart while the wrapper lives; no key made by a rejected call; the computed callback's key; register's length and errors. Full suite on an arm64 emulator, measured on the top of the stack (#2130): 0 failures in each of resurrecting/extended (1834 specs), canary/extended and canary/all (1832 each). Summary by CodeRabbit New Features Weak collections and weak-reference APIs now support canary-wrapped Java objects across wrapper changes. This includes WeakMap, WeakSet, WeakRef, and FinalizationRegistry. Weak-collection lookups do not create keys; insertions create keys as needed. Ordinary JavaScript values retain their existing behavior. Documentation Clarified weak-collection behavior for canary-wrapped objects, including effects on object lifetime. Tests Added coverage for weak collections and weak-reference behavior across wrapper-lifetime modes, including collection and finalization behavior.
Open Graph Description: Description Fourth of the stacked wrapper-lifetime series (on #2127), the Android port of NativeScript/ios#563. With #2127 alone, a Java object's canary dies with its JS references while the Ja...
X Description: Description Fourth of the stacked wrapper-lifetime series (on #2127), the Android port of NativeScript/ios#563. With #2127 alone, a Java object's canary dies with its JS references while th...
Opengraph URL: https://github.com/NativeScript/android/pull/2128
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:052a9739-b975-ca67-7abb-9c84eded7919 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 90E8:63875:148FADE:1B32DC2:6B012729 |
| html-safe-nonce | 3c238ba4d57323a3b5d8fa3653e93c3206863897d17ec8ab3ce97d63104c9d8d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MEU4OjYzODc1OjE0OEZBREU6MUIzMkRDMjo2QjAxMjcyOSIsInZpc2l0b3JfaWQiOiI2NTYwMjYxNjE0MjMzOTQ2MDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1a8a9430a94e7237f594cc564c41890951bfe7a804fa0ddba2fe1e1ce21c684c |
| hovercard-subject-tag | pull_request:5417413795 |
| 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/2128/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 Fourth of the stacked wrapper-lifetime series (on #2127), the Android port of NativeScript/ios#563. With #2127 alone, a Java object's canary dies with its JS references while the Ja... |
| 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