From c36c08b5915700774b01a3da82dc5aa271903f5a Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 13 Nov 2026 02:28:08 -0300 Subject: [PATCH 01/12] perf(runtime): give NSArray wrappers Symbol.iterator through the prototype Arrays returned from native methods got a per-instance Symbol.iterator function. Installed once on NSArray's prototype template instead, every array wrapper inherits it, whichever path built it, and none carries an own property: under the canary wrapper lifetime mode that property was state, one state object and associated token per array. --- NativeScript/runtime/Interop.mm | 6 ----- NativeScript/runtime/MetadataBuilder.mm | 4 +++ NativeScript/runtime/SymbolIterator.h | 3 ++- NativeScript/runtime/SymbolIterator.mm | 36 +++++-------------------- 4 files changed, 13 insertions(+), 36 deletions(-) diff --git a/NativeScript/runtime/Interop.mm b/NativeScript/runtime/Interop.mm index fc4cf2414..9036ede51 100644 --- a/NativeScript/runtime/Interop.mm +++ b/NativeScript/runtime/Interop.mm @@ -17,7 +17,6 @@ #include "Reference.h" #include "Runtime.h" #include "RuntimeConfig.h" -#include "SymbolIterator.h" #include "SymbolLoader.h" #include "UnmanagedType.h" #include "WrapperLifetime.h" @@ -1497,11 +1496,6 @@ static bool SnapshotArrayLike(Isolate* isolate, Local object, LocalPrototypeTemplate()->Set( tns::ToV8String(isolate, "toString"), FunctionTemplate::New(isolate, MetadataBuilder::ToStringFunctionCallback)); + if (meta->type() == MetaType::Interface && strcmp(meta->name(), "NSArray") == 0) { + SymbolIterator::Install(isolate, ctorFuncTemplate->PrototypeTemplate()); + } if (meta->type() == MetaType::Interface) { const InterfaceMeta* interfaceMeta = static_cast(meta); diff --git a/NativeScript/runtime/SymbolIterator.h b/NativeScript/runtime/SymbolIterator.h index 6cf66fc06..800da55bb 100644 --- a/NativeScript/runtime/SymbolIterator.h +++ b/NativeScript/runtime/SymbolIterator.h @@ -7,7 +7,8 @@ namespace tns { class SymbolIterator { public: - static void Set(v8::Local context, v8::Local object); + // On NSArray's prototype template, so every array wrapper inherits it. + static void Install(v8::Isolate* isolate, v8::Local prototypeTemplate); private: static v8::Local CreateIteratorObject(v8::Local context, v8::Local object); static void NextCallback(const v8::FunctionCallbackInfo& args); diff --git a/NativeScript/runtime/SymbolIterator.mm b/NativeScript/runtime/SymbolIterator.mm index 72477f605..123b4e269 100644 --- a/NativeScript/runtime/SymbolIterator.mm +++ b/NativeScript/runtime/SymbolIterator.mm @@ -2,40 +2,18 @@ #include #include "ArgConverter.h" #include "Helpers.h" -#include "WrapperLifetime.h" using namespace v8; namespace tns { -void SymbolIterator::Set(Local context, Local object) { - Isolate* isolate = v8::Isolate::GetCurrent(); - - // In the canary wrapper lifetime mode it iterates its receiver: this - // property lives on the object's state there, which a reference back to the - // object would keep alive until native death. Elsewhere it keeps `object`, so - // an unbound call still iterates it. - Local captured = - GetWrapperLifetimeMode() == WrapperLifetimeMode::kCanary ? Local() : object; - Local iterator; - bool success = v8::Function::New( - context, - [](const FunctionCallbackInfo& args) { - Isolate* isolate = args.GetIsolate(); - Local context = isolate->GetCurrentContext(); - Local target = args.Data(); - if (target.IsEmpty() || target->IsUndefined()) { - target = args.This(); - } - Local result = CreateIteratorObject(context, target); - args.GetReturnValue().Set(result); - }, - captured) - .ToLocal(&iterator); - tns::Assert(success, isolate); - - Local symbolIteratorKey = Symbol::GetIterator(isolate); - success = object.As()->Set(context, symbolIteratorKey, iterator).FromMaybe(false); +void SymbolIterator::Install(Isolate* isolate, Local prototypeTemplate) { + prototypeTemplate->Set( + Symbol::GetIterator(isolate), + FunctionTemplate::New(isolate, [](const FunctionCallbackInfo& args) { + Local context = args.GetIsolate()->GetCurrentContext(); + args.GetReturnValue().Set(CreateIteratorObject(context, args.This())); + })); } Local SymbolIterator::CreateIteratorObject(Local context, Local object) { From 290b3523e05b9719790553129e055285e83f730a Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 13 Nov 2026 02:28:08 -0300 Subject: [PATCH 02/12] feat(runtime): weak collections key native objects by lifetime in canary mode In canary mode a native object's wrapper dies with its JS references while the object lives on, so WeakMap/WeakSet entries, WeakRef targets and FinalizationRegistry registrations keyed on the wrapper were lost with it. A canary-only builtin replaces the four globals with subclasses that key a native object by an opaque per-object key (frozen, null-prototype), held by the object's state and so alive exactly until the object is deallocated; WeakRef.prototype.deref (and the legacy get) return its live wrapper, a new one, or undefined once the object is gone. Values that are not canaries pass through unchanged, and other modes keep the built-ins. The key and its reverse lookup (a private on the key naming the object, checked against the registry, then an ObjC weak load so a deallocating object is never retained) are internal to the builtin. --- NativeScript/runtime/WeakRef.cpp | 14 ++ NativeScript/runtime/WrapperLifetimeCanary.h | 5 + NativeScript/runtime/WrapperLifetimeCanary.mm | 127 ++++++++++++++- NativeScript/runtime/js/weak-collections.js | 98 +++++++++++ .../WrapperLifetimeWeakCollectionsTests.js | 154 ++++++++++++++++++ TestRunner/app/tests/index.js | 1 + tools/js2c-inputs.xcfilelist | 1 + 7 files changed, 393 insertions(+), 7 deletions(-) create mode 100644 NativeScript/runtime/js/weak-collections.js create mode 100644 TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js diff --git a/NativeScript/runtime/WeakRef.cpp b/NativeScript/runtime/WeakRef.cpp index de04d3acc..7831b1571 100644 --- a/NativeScript/runtime/WeakRef.cpp +++ b/NativeScript/runtime/WeakRef.cpp @@ -2,6 +2,8 @@ #include "BuiltinLoader.h" #include "Helpers.h" +#include "WrapperLifetime.h" +#include "WrapperLifetimeCanary.h" using namespace v8; @@ -18,6 +20,18 @@ void WeakRef::Init(Local context) { tns::LogError(isolate, tc); } tns::Assert(success, isolate); + + if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) { + return; + } + Local binding = Object::New(isolate); + success = WrapperLifetimeCanary::RegisterWrapFunctions(context, binding) && + BuiltinLoader::RunBuiltin(context, BuiltinId::kWeakCollections, binding) + .ToLocal(&result); + if (!success && tc.HasCaught()) { + tns::LogError(isolate, tc); + } + tns::Assert(success, isolate); } } // namespace tns diff --git a/NativeScript/runtime/WrapperLifetimeCanary.h b/NativeScript/runtime/WrapperLifetimeCanary.h index d41f3191e..1bb2cf0af 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.h +++ b/NativeScript/runtime/WrapperLifetimeCanary.h @@ -71,6 +71,11 @@ class WrapperLifetimeCanary { // returned instead), which ArgConverter::ConstructObject wraps anew. static bool NativeInstancesIntercept(); + // Adds `unwrap` (a canary's native object -> its weak-collection key) and + // `wrap` (key -> live or new canary, or undefined once the object is gone) + // to `binding`, for the weak-collections builtin. Canary mode only. + static bool RegisterWrapFunctions(v8::Local context, v8::Local binding); + // The record of an object that already has state in this isolate, for a new // canary to share. Null otherwise. static std::shared_ptr Find(v8::Isolate* isolate, id object); diff --git a/NativeScript/runtime/WrapperLifetimeCanary.mm b/NativeScript/runtime/WrapperLifetimeCanary.mm index 99aeac1f5..d77fd7601 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.mm +++ b/NativeScript/runtime/WrapperLifetimeCanary.mm @@ -7,6 +7,7 @@ #include #include #include +#include "ArgConverter.h" #include "Caches.h" #include "DataWrapper.h" #include "EventLoop.h" @@ -31,9 +32,8 @@ #define NS_CANARY_DEATH_SIGNAL NS_CANARY_DEATH_ASSOCIATION #endif -#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP extern "C" id objc_initWeakOrNil(id* location, id value); -#endif +extern "C" id objc_loadWeakRetained(id* location); extern "C" void objc_destroyWeak(id* location); using namespace v8; @@ -57,8 +57,10 @@ // tells, when a sweep reads it. bool reportsDeath = false; // An ObjC weak reference to the object, zeroed by the runtime when it is - // deallocated, on any thread. Set for records nothing reports. + // deallocated, on any thread. Set for records nothing reports, and for any + // record whose object became a weak-collection key (Wrap reads it). id object = nil; + bool weakSet = false; ~CanaryRecord() { objc_destroyWeak(&object); } @@ -96,6 +98,10 @@ bool ObjectAlive() const { struct CanaryState { std::shared_ptr registry = std::make_shared(); + // The weak collections' key for a native object hangs off its state, and + // names the object it stands for. + Global keyOfState; + Global objectOfKey; }; void DrainDead(const std::shared_ptr& registry) { @@ -465,10 +471,13 @@ bool StateForWrite(Isolate* isolate, Local holder, Local* out) { auto record = std::make_shared(); record->reportsDeath = extended || NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION; #if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP - if (!record->reportsDeath && objc_initWeakOrNil(&record->object, object) == nil) { - // The class refuses weak references. - registry->records.erase((__bridge void*)object); - return false; + if (!record->reportsDeath) { + if (objc_initWeakOrNil(&record->object, object) == nil) { + // The class refuses weak references. + registry->records.erase((__bridge void*)object); + return false; + } + record->weakSet = true; } #endif record->state.Reset(isolate, freshState); @@ -651,6 +660,98 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) { } #endif +// The weak collections' key for a canary's native object: a frozen, +// null-prototype object that lives exactly as long as the object's state, that +// is, until the native object is deallocated. The holder itself for anything +// that is not a canary. +void UnwrapCallback(const FunctionCallbackInfo& info) { + Isolate* isolate = info.GetIsolate(); + Local value = info[0]; + info.GetReturnValue().Set(value); + CanaryState* canaryState = Caches::StateFor(isolate); + Local state; + if (canaryState == nullptr || !value->IsObject() || + !StateForWrite(isolate, value.As(), &state)) { + return; + } + Local context = isolate->GetCurrentContext(); + Local keyOfState = canaryState->keyOfState.Get(isolate); + Local key; + if (state->GetPrivate(context, keyOfState).ToLocal(&key) && key->IsObject()) { + info.GetReturnValue().Set(key); + return; + } + ObjectWeakCallbackState* canary = CanaryOf(isolate, value.As()); + CanaryRecord* record = canary->canaryRecord_.get(); + if (!record->weakSet) { + if (objc_initWeakOrNil(&record->object, canary->lifetimeTarget_) == nil) { + return; + } + record->weakSet = true; + } + Local newKey = Object::New(isolate, Null(isolate), nullptr, nullptr, 0); + uint64_t address = reinterpret_cast((__bridge void*)canary->lifetimeTarget_); + if (!newKey + ->SetPrivate(context, canaryState->objectOfKey.Get(isolate), + BigInt::NewFromUnsigned(isolate, address)) + .FromMaybe(false) || + !newKey->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromMaybe(false) || + !state->SetPrivate(context, keyOfState, newKey).FromMaybe(false)) { + return; + } + info.GetReturnValue().Set(newKey); +} + +// The object a key stands for: its live canary or a new one, or undefined once +// the native object is gone. Anything that is not a key passes through. +void WrapCallback(const FunctionCallbackInfo& info) { + Isolate* isolate = info.GetIsolate(); + Local value = info[0]; + info.GetReturnValue().Set(value); + CanaryState* canaryState = Caches::StateFor(isolate); + if (canaryState == nullptr || !value->IsObject()) { + return; + } + Local context = isolate->GetCurrentContext(); + Local key = value.As(); + Local address; + if (!key->GetPrivate(context, canaryState->objectOfKey.Get(isolate)).ToLocal(&address) || + !address->IsBigInt()) { + return; + } + info.GetReturnValue().SetUndefined(); + std::shared_ptr record; + { + std::lock_guard lock(canaryState->registry->mutex); + auto it = canaryState->registry->records.find( + reinterpret_cast(static_cast(address.As()->Uint64Value()))); + if (it == canaryState->registry->records.end()) { + return; + } + record = it->second; + } + if (!Usable(record) || !record->weakSet) { + return; + } + // A record at that address may belong to a later object: it must be the + // key's own. + Local recordKey; + Local state = Local::New(isolate, record->state); + if (!state->GetPrivate(context, canaryState->keyOfState.Get(isolate)).ToLocal(&recordKey) || + !recordKey->StrictEquals(key)) { + return; + } + id object = objc_loadWeakRetained(&record->object); + if (object == nil) { + return; + } + ObjCDataWrapper* wrapper = new ObjCDataWrapper(object); + Local result = ArgConverter::ConvertArgument(context, wrapper); + tns::DeleteWrapperIfUnused(isolate, result, wrapper); + [object release]; + info.GetReturnValue().Set(result); +} + } // namespace void WrapperLifetimeCanary::Init(Isolate* isolate) { @@ -662,6 +763,8 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) { return; } state->registry->isolate = isolate; + state->keyOfState.Reset(isolate, Private::New(isolate)); + state->objectOfKey.Reset(isolate, Private::New(isolate)); if (Runtime* runtime = Runtime::GetRuntime(isolate)) { state->registry->loop = runtime->GetEventLoop(); } @@ -674,6 +777,16 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) { #endif } +bool WrapperLifetimeCanary::RegisterWrapFunctions(Local context, Local binding) { + Isolate* isolate = v8::Isolate::GetCurrent(); + Local unwrap; + Local wrap; + return v8::Function::New(context, UnwrapCallback).ToLocal(&unwrap) && + v8::Function::New(context, WrapCallback).ToLocal(&wrap) && + binding->Set(context, tns::ToV8String(isolate, "unwrap"), unwrap).FromMaybe(false) && + binding->Set(context, tns::ToV8String(isolate, "wrap"), wrap).FromMaybe(false); +} + void WrapperLifetimeCanary::InstallDeallocHook(Class extendedClass) { if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) { return; diff --git a/NativeScript/runtime/js/weak-collections.js b/NativeScript/runtime/js/weak-collections.js new file mode 100644 index 000000000..34ed856e4 --- /dev/null +++ b/NativeScript/runtime/js/weak-collections.js @@ -0,0 +1,98 @@ +"use strict"; + +// Canary wrapper lifetime mode only (run by WeakRef.cpp): WeakMap, WeakSet, +// WeakRef and FinalizationRegistry key a native object by its lifetime key +// instead of its wrapper. The wrapper is a canary that dies with its JS +// references while the native object lives on, and a later wrapper for the same +// object is a new JS object; keyed by the wrapper, entries and targets would be +// lost with it. Every other value passes through unchanged. + +const { unwrap, wrap } = binding; +const { + FinalizationRegistry: NativeFinalizationRegistry, + WeakMap: NativeWeakMap, + WeakRef: NativeWeakRef, + WeakSet: NativeWeakSet, +} = globalThis; + +class WeakMap extends NativeWeakMap { + get(key) { + return super.get(unwrap(key)); + } + + set(key, value) { + super.set(unwrap(key), value); + return this; + } + + has(key) { + return super.has(unwrap(key)); + } + + delete(key) { + return super.delete(unwrap(key)); + } +} + +class WeakSet extends NativeWeakSet { + add(value) { + super.add(unwrap(value)); + return this; + } + + has(value) { + return super.has(unwrap(value)); + } + + delete(value) { + return super.delete(unwrap(value)); + } +} + +class WeakRef extends NativeWeakRef { + constructor(target) { + super(unwrap(target)); + } + + // The live wrapper, a new one if the old one died while the native object + // lived, or undefined once the native object is gone. + deref() { + return wrap(super.deref()); + } +} + +// weak-ref.js gave the native prototype a `get` alias of `deref`; inherited +// as is, it would hand out the lifetime key. +Object.defineProperty(WeakRef.prototype, "get", { + value: WeakRef.prototype.deref, + writable: true, + configurable: true, +}); + +class FinalizationRegistry extends NativeFinalizationRegistry { + register(target, heldValue, unregisterToken) { + super.register( + unwrap(target), + heldValue, + unregisterToken === undefined ? undefined : unwrap(unregisterToken), + ); + } + + unregister(unregisterToken) { + return super.unregister(unwrap(unregisterToken)); + } +} + +for (const [name, value] of [ + ["WeakMap", WeakMap], + ["WeakSet", WeakSet], + ["WeakRef", WeakRef], + ["FinalizationRegistry", FinalizationRegistry], +]) { + Object.defineProperty(globalThis, name, { + value, + writable: true, + configurable: true, + enumerable: false, + }); +} diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js new file mode 100644 index 000000000..d019f4314 --- /dev/null +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -0,0 +1,154 @@ +// WeakMap, WeakSet, WeakRef and FinalizationRegistry keyed on native objects. +// The expectations hold in every wrapper lifetime mode; under "canary" they +// depend on the weak collections keying a native object by its lifetime key +// rather than by its current wrapper. +describe("Weak collections of native objects", function () { + + var Extended = (function (_super) { + __extends(Extended, _super); + function Extended() { + return (_super !== null && _super.apply(this, arguments)) || this; + } + return Extended; + })(TNSAllocLog); + + function scrub(n) { + return n > 0 ? scrub(n - 1) + n : 0; + } + + function settle(rounds, callback) { + scrub(300); + __collect(); + __collect(); + setTimeout(function () { + NSObject.new(); + if (rounds > 1) { + settle(rounds - 1, callback); + } else { + callback(); + } + }, 10); + } + + it("keeps a WeakMap entry for a native object across a wrapper change", function (done) { + var owner = NSMutableArray.new(); + var map = new WeakMap(); + (function () { + var obj = Extended.new(); + map.set(obj, "value"); + owner.addObject(obj); + })(); + settle(2, function () { + var obj = owner.objectAtIndex(0); + expect(map.get(obj)).toBe("value"); + expect(map.has(obj)).toBe(true); + expect(map.delete(obj)).toBe(true); + expect(map.has(obj)).toBe(false); + done(); + }); + }); + + it("keeps a WeakSet member for a native object across a wrapper change", function (done) { + var owner = NSMutableArray.new(); + var set = new WeakSet(); + (function () { + var obj = Extended.new(); + set.add(obj); + owner.addObject(obj); + })(); + settle(2, function () { + expect(set.has(owner.objectAtIndex(0))).toBe(true); + done(); + }); + }); + + it("dereferences a WeakRef to a natively owned object while the object lives", function (done) { + var owner = NSMutableArray.new(); + var ref; + (function () { + var obj = Extended.new(); + obj.tag = "kept"; + ref = new WeakRef(obj); + owner.addObject(obj); + })(); + settle(2, function () { + var target = ref.deref(); + expect(target).toBe(owner.objectAtIndex(0)); + expect(target.tag).toBe("kept"); + expect(ref.get()).toBe(target); + done(); + }); + }); + + it("clears a WeakRef once the native object is gone", function (done) { + var owner = NSMutableArray.new(); + var ref; + (function () { + var obj = Extended.new(); + ref = new WeakRef(obj); + owner.addObject(obj); + })(); + settle(2, function () { + owner.removeAllObjects(); + var attempts = 10; + (function poll() { + settle(1, function () { + if (ref.deref() === undefined || --attempts === 0) { + expect(ref.deref()).toBeUndefined(); + done(); + return; + } + poll(); + }); + })(); + }); + }); + + it("runs a FinalizationRegistry callback once the native object is gone", function (done) { + var owner = NSMutableArray.new(); + var held = []; + var registry = new FinalizationRegistry(function (value) { + held.push(value); + }); + (function () { + var obj = Extended.new(); + registry.register(obj, "held"); + owner.addObject(obj); + })(); + settle(2, function () { + expect(held).toEqual([]); + owner.removeAllObjects(); + var attempts = 15; + (function poll() { + settle(1, function () { + if (held.length > 0 || --attempts === 0) { + expect(held).toEqual(["held"]); + done(); + return; + } + poll(); + }); + })(); + }); + }); + + it("keeps working as the standard collections for other keys", function () { + var key = {}; + var map = new WeakMap([[key, 1]]); + expect(map.get(key)).toBe(1); + expect(map instanceof WeakMap).toBe(true); + expect(new WeakSet([key]).has(key)).toBe(true); + expect(new WeakRef(key).deref()).toBe(key); + }); + + it("iterates NSArray wrappers through the prototype", function () { + var array = NSArray.arrayWithArray([1, 2, 3]); + var seen = []; + for (var value of array) { + seen.push(value); + } + expect(seen).toEqual([1, 2, 3]); + expect([].concat(Array.from(NSMutableArray.arrayWithArray(["a"])))).toEqual(["a"]); + expect(Object.getOwnPropertySymbols(array).length).toBe(0); + }); +}); diff --git a/TestRunner/app/tests/index.js b/TestRunner/app/tests/index.js index a3ddf8ddb..6f3bbed63 100644 --- a/TestRunner/app/tests/index.js +++ b/TestRunner/app/tests/index.js @@ -142,6 +142,7 @@ require("./JSAccessorEncodingTests"); require("./JSAccessorReceiverTests"); require("./WrapperLifetimeTests"); require("./WrapperLifetimeModesTests"); +require("./WrapperLifetimeWeakCollectionsTests"); require("./BlockCacheRaceTests"); require("./WorkerConcurrentStartupTests"); require("./WorkerOptionsTests"); diff --git a/tools/js2c-inputs.xcfilelist b/tools/js2c-inputs.xcfilelist index dd339c974..ca3913c1d 100644 --- a/tools/js2c-inputs.xcfilelist +++ b/tools/js2c-inputs.xcfilelist @@ -30,5 +30,6 @@ $(SRCROOT)/NativeScript/runtime/js/require-factory.js $(SRCROOT)/NativeScript/runtime/js/structured-clone.js $(SRCROOT)/NativeScript/runtime/js/text-encoding.js $(SRCROOT)/NativeScript/runtime/js/ts-helpers.js +$(SRCROOT)/NativeScript/runtime/js/weak-collections.js $(SRCROOT)/NativeScript/runtime/js/weak-ref.js $(SRCROOT)/NativeScript/runtime/js/worker-events.js From 7e8f55043ace96d3a2cf99f41f470bc1f03d3e23 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 13 Nov 2026 02:28:48 -0300 Subject: [PATCH 03/12] docs(runtime): weak collections under canary mode --- docs/knowledge/v8-resurrecting-finalizers.md | 8 ++++++-- types/ns-runtime.d.ts | 7 ++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/knowledge/v8-resurrecting-finalizers.md b/docs/knowledge/v8-resurrecting-finalizers.md index 60ff2e150..e823661d6 100644 --- a/docs/knowledge/v8-resurrecting-finalizers.md +++ b/docs/knowledge/v8-resurrecting-finalizers.md @@ -303,8 +303,12 @@ rooted at canary death instead, because a state reachable only through the canar the same collection. The cost: state that references its own object (a closure over `this`) keeps it alive. A canary built after a death is a new object, but `===` cannot tell: only one canary lives at a time, and anything still referencing the old one would have kept it alive. -What does notice is whatever keys on the canary without keeping it: `WeakMap` keys and `WeakRef`s -of the old canary, and native `#private` fields, which are branded on it. Other +What could notice is whatever keys on the canary without keeping it. The weak collections +don't: in canary mode a builtin (`js/weak-collections.js`) replaces `WeakMap`, `WeakSet`, +`WeakRef` and `FinalizationRegistry` with subclasses that key a native object by an opaque, +frozen key held by its state, so entries and targets live exactly as long as the native object, +and `WeakRef.prototype.deref` returns the live canary, a new one, or `undefined` once the object +is gone. Native `#private` fields are branded on the canary and do not carry over. Other limits: `Object.freeze`, `seal` and `preventExtensions` throw on a canary (V8 refuses them on objects with interceptors); index-keyed properties are not mirrored and die with the canary; a write through an object whose prototype is a canary lands on that object, since diff --git a/types/ns-runtime.d.ts b/types/ns-runtime.d.ts index 56e920d8c..7465a3545 100644 --- a/types/ns-runtime.d.ts +++ b/types/ns-runtime.d.ts @@ -57,9 +57,10 @@ declare module "ns:runtime" { * also copied to a state object kept until the native object is * deallocated, and a wrapper built for the object later gets them back * with their attributes. Native property writes still reach the native - * setter. Writes cost more than in the other modes; reads do not. Weak - * references to the old wrapper (WeakMap keys, WeakRef), native - * `#private` fields and index-keyed properties do not carry over. + * setter. Writes cost more than in the other modes; reads do not. + * WeakMap, WeakSet, WeakRef and FinalizationRegistry key native objects + * by their lifetime in this mode, so they carry over too. Native + * `#private` fields and index-keyed properties do not. * State that references its own object keeps that object alive. */ wrapperLifetimeMode: "resurrecting" | "rooted" | "canary"; From 92cb7bd636ae754a09c40a231cc82c319ba29b14 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 17 Nov 2026 01:06:35 -0300 Subject: [PATCH 04/12] fix(runtime): weak collections review fixes - Lookups (get, has, delete, unregister) never create a native object's state or key: with no key yet, no entry can be keyed by it. - A Proxy around a native object is a key of its own, as in the other modes. - WeakMap.prototype.getOrInsert and getOrInsertComputed (shipped by V8) key by lifetime too; the computed callback gets the key it was given. - WeakRef.prototype.deref hands back a live wrapper as is, keeping a protocol-typed wrapper's prototype, and keeps what it returns alive until the job ends, like a native WeakRef target. - FinalizationRegistry.length is 1. - ExposeDynamicMethods installs a fast-enumeration override only for an iterator the JS class defines itself: with Symbol.iterator on the NSArray prototype, a JS subclass of NSArray made through __extends or an ES class inherited it and got an override that enumerated nothing, in every mode. - NSArray's prototype Symbol.iterator throws a TypeError for a receiver that is not an NSArray, like Array.prototype[Symbol.iterator]. - Docs: which objects are keyed by lifetime, and that a WeakMap value referencing its own native key keeps that object alive while the map lives. - Specs for each, and the canary hand-back spec now expects a WeakRef to the old wrapper to hand back the live one. --- NativeScript/runtime/ClassBuilder.mm | 8 +- NativeScript/runtime/SymbolIterator.mm | 11 +- NativeScript/runtime/WrapperLifetimeCanary.mm | 30 ++++- NativeScript/runtime/js/weak-collections.js | 64 ++++++++--- .../app/tests/WrapperLifetimeModesTests.js | 23 ++-- .../WrapperLifetimeWeakCollectionsTests.js | 106 ++++++++++++++++++ docs/knowledge/v8-resurrecting-finalizers.md | 13 ++- types/ns-runtime.d.ts | 8 +- 8 files changed, 223 insertions(+), 40 deletions(-) diff --git a/NativeScript/runtime/ClassBuilder.mm b/NativeScript/runtime/ClassBuilder.mm index 411a24d95..701e64e00 100644 --- a/NativeScript/runtime/ClassBuilder.mm +++ b/NativeScript/runtime/ClassBuilder.mm @@ -762,9 +762,13 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { Local propertyNames; + // Only an iterator the class defines itself: an inherited one (NSArray's, + // or a JS superclass's) already backs the superclass's own enumeration. Local symbolIterator; - bool success = - implementationObject->Get(context, Symbol::GetIterator(isolate)).ToLocal(&symbolIterator); + bool ownsIterator = + implementationObject->HasOwnProperty(context, Symbol::GetIterator(isolate)).FromMaybe(false); + bool success = !ownsIterator || implementationObject->Get(context, Symbol::GetIterator(isolate)) + .ToLocal(&symbolIterator); tns::Assert(success, isolate); if (!symbolIterator.IsEmpty() && symbolIterator->IsFunction()) { diff --git a/NativeScript/runtime/SymbolIterator.mm b/NativeScript/runtime/SymbolIterator.mm index 123b4e269..46797c7da 100644 --- a/NativeScript/runtime/SymbolIterator.mm +++ b/NativeScript/runtime/SymbolIterator.mm @@ -11,7 +11,16 @@ prototypeTemplate->Set( Symbol::GetIterator(isolate), FunctionTemplate::New(isolate, [](const FunctionCallbackInfo& args) { - Local context = args.GetIsolate()->GetCurrentContext(); + Isolate* isolate = args.GetIsolate(); + // Like Array.prototype[Symbol.iterator], it needs its array as `this`. + BaseDataWrapper* wrapper = tns::GetValue(isolate, args.This()); + if (wrapper == nullptr || wrapper->Type() != WrapperType::ObjCObject || + ![static_cast(wrapper)->Data() isKindOfClass:[NSArray class]]) { + isolate->ThrowException(Exception::TypeError(tns::ToV8String( + isolate, "NSArray.prototype[Symbol.iterator] called on a non-NSArray"))); + return; + } + Local context = isolate->GetCurrentContext(); args.GetReturnValue().Set(CreateIteratorObject(context, args.This())); })); } diff --git a/NativeScript/runtime/WrapperLifetimeCanary.mm b/NativeScript/runtime/WrapperLifetimeCanary.mm index d77fd7601..3b6a0f439 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.mm +++ b/NativeScript/runtime/WrapperLifetimeCanary.mm @@ -660,18 +660,24 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) { } #endif -// The weak collections' key for a canary's native object: a frozen, -// null-prototype object that lives exactly as long as the object's state, that -// is, until the native object is deallocated. The holder itself for anything -// that is not a canary. +// unwrap(value, create): the weak collections' key for a canary's native +// object, a frozen, null-prototype object that lives exactly as long as the +// object's state, that is, until the native object is deallocated. Lookups pass +// `create` false and never make one: with no key yet, no entry can be keyed by +// it. The value itself for anything that is not a canary, a Proxy included (it +// is a key of its own, as in every other mode). void UnwrapCallback(const FunctionCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); Local value = info[0]; + bool create = info[1]->IsTrue(); info.GetReturnValue().Set(value); CanaryState* canaryState = Caches::StateFor(isolate); Local state; - if (canaryState == nullptr || !value->IsObject() || - !StateForWrite(isolate, value.As(), &state)) { + if (canaryState == nullptr || !value->IsObject() || value->IsProxy()) { + return; + } + if (create ? !StateForWrite(isolate, value.As(), &state) + : !ExistingState(isolate, value.As(), &state)) { return; } Local context = isolate->GetCurrentContext(); @@ -681,6 +687,9 @@ void UnwrapCallback(const FunctionCallbackInfo& info) { info.GetReturnValue().Set(key); return; } + if (!create) { + return; + } ObjectWeakCallbackState* canary = CanaryOf(isolate, value.As()); CanaryRecord* record = canary->canaryRecord_.get(); if (!record->weakSet) { @@ -745,6 +754,15 @@ void WrapCallback(const FunctionCallbackInfo& info) { if (object == nil) { return; } + // A live wrapper is handed back as is: building one through ConvertArgument + // would reset a protocol-typed wrapper's prototype to its class's. + std::shared_ptr cache = Caches::Get(isolate); + std::shared_ptr> cached = ArgConverter::FindCachedInstance(isolate, cache, object); + if (cached != nullptr && !cached->IsEmpty()) { + [object release]; + info.GetReturnValue().Set(cached->Get(isolate)); + return; + } ObjCDataWrapper* wrapper = new ObjCDataWrapper(object); Local result = ArgConverter::ConvertArgument(context, wrapper); tns::DeleteWrapperIfUnused(isolate, result, wrapper); diff --git a/NativeScript/runtime/js/weak-collections.js b/NativeScript/runtime/js/weak-collections.js index 34ed856e4..9e1a2ffae 100644 --- a/NativeScript/runtime/js/weak-collections.js +++ b/NativeScript/runtime/js/weak-collections.js @@ -15,49 +15,83 @@ const { WeakSet: NativeWeakSet, } = globalThis; +// unwrap(value, true) makes the key on first use; lookups pass false so they +// never create state for an object that was never a key. class WeakMap extends NativeWeakMap { get(key) { - return super.get(unwrap(key)); + return super.get(unwrap(key, false)); } set(key, value) { - super.set(unwrap(key), value); + super.set(unwrap(key, true), value); return this; } has(key) { - return super.has(unwrap(key)); + return super.has(unwrap(key, false)); } delete(key) { - return super.delete(unwrap(key)); + return super.delete(unwrap(key, false)); } } +// V8 ships the upsert proposal; inherited, these would key on the wrapper. +if (typeof NativeWeakMap.prototype.getOrInsert === "function") { + Object.defineProperty(WeakMap.prototype, "getOrInsert", { + value: function getOrInsert(key, value) { + return NativeWeakMap.prototype.getOrInsert.call(this, unwrap(key, true), value); + }, + writable: true, + configurable: true, + }); +} +if (typeof NativeWeakMap.prototype.getOrInsertComputed === "function") { + Object.defineProperty(WeakMap.prototype, "getOrInsertComputed", { + value: function getOrInsertComputed(key, callbackfn) { + if (typeof callbackfn !== "function") { + throw new TypeError("WeakMap.prototype.getOrInsertComputed: callback is not a function"); + } + // The callback sees the key it was given, not the lifetime key. + return NativeWeakMap.prototype.getOrInsertComputed.call(this, unwrap(key, true), () => + callbackfn(key), + ); + }, + writable: true, + configurable: true, + }); +} + class WeakSet extends NativeWeakSet { add(value) { - super.add(unwrap(value)); + super.add(unwrap(value, true)); return this; } has(value) { - return super.has(unwrap(value)); + return super.has(unwrap(value, false)); } delete(value) { - return super.delete(unwrap(value)); + return super.delete(unwrap(value, false)); } } class WeakRef extends NativeWeakRef { constructor(target) { - super(unwrap(target)); + super(unwrap(target, true)); } // The live wrapper, a new one if the old one died while the native object - // lived, or undefined once the native object is gone. + // lived, or undefined once the native object is gone. Like a native WeakRef + // target, the wrapper it returns stays alive until the job ends, so a second + // deref in the same job sees the same object. deref() { - return wrap(super.deref()); + const result = wrap(super.deref()); + if (typeof result === "object" && result !== null) { + new NativeWeakRef(result); + } + return result; } } @@ -70,16 +104,20 @@ Object.defineProperty(WeakRef.prototype, "get", { }); class FinalizationRegistry extends NativeFinalizationRegistry { + constructor(cleanupCallback) { + super(cleanupCallback); + } + register(target, heldValue, unregisterToken) { super.register( - unwrap(target), + unwrap(target, true), heldValue, - unregisterToken === undefined ? undefined : unwrap(unregisterToken), + unregisterToken === undefined ? undefined : unwrap(unregisterToken, true), ); } unregister(unregisterToken) { - return super.unregister(unwrap(unregisterToken)); + return super.unregister(unwrap(unregisterToken, false)); } } diff --git a/TestRunner/app/tests/WrapperLifetimeModesTests.js b/TestRunner/app/tests/WrapperLifetimeModesTests.js index faa701756..10f560f42 100644 --- a/TestRunner/app/tests/WrapperLifetimeModesTests.js +++ b/TestRunner/app/tests/WrapperLifetimeModesTests.js @@ -493,6 +493,7 @@ describe("Wrapper lifetime modes", function () { it("hands back a native object's JS properties on a new wrapper after the old one died", function (done) { var owner = NSMutableArray.new(); var first; + var before = runtime.getWrapperLifetimeStats(); (function () { var obj = Extended.new(); obj.tag = "kept"; @@ -501,7 +502,10 @@ describe("Wrapper lifetime modes", function () { })(); settle(2, function () { if (canary) { - expect(first.deref()).toBeUndefined(); + // The old canary died; a WeakRef keys the native object by its + // lifetime in this mode, so it hands back the live one. + expect(runtime.getWrapperLifetimeStats().queuedReleases).toBeGreaterThan(before.queuedReleases); + expect(first.deref().tag).toBe("kept"); } var obj = owner.objectAtIndex(0); if (keepsState) { @@ -788,18 +792,15 @@ describe("Wrapper lifetime modes", function () { }); }); - it("iterates an NSArray through an unbound Symbol.iterator outside canary mode", function () { - if (canary) { - return; - } + // Inherited from the NSArray prototype, so like Array's it needs `this`. + it("iterates an NSArray through its inherited Symbol.iterator and rejects an unbound call", function () { var array = NSArray.arrayWithArray(["a", "b"]); + expect(Object.prototype.hasOwnProperty.call(array, Symbol.iterator)).toBe(false); + expect(Array.from(array)).toEqual(["a", "b"]); var iterate = array[Symbol.iterator]; - var iterator = iterate(); - var values = []; - for (var step = iterator.next(); !step.done; step = iterator.next()) { - values.push(step.value); - } - expect(values).toEqual(["a", "b"]); + expect(function () { + iterate(); + }).toThrowError(TypeError, /non-NSArray/); }); it("leaves plain objects outside the extended scope without forwarding", function (done) { diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js index d019f4314..96cfacc49 100644 --- a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -3,6 +3,8 @@ // depend on the weak collections keying a native object by its lifetime key // rather than by its current wrapper. describe("Weak collections of native objects", function () { + var runtime = require("ns:runtime"); + var canary = runtime.getConfig("wrapperLifetimeMode") === "canary"; var Extended = (function (_super) { __extends(Extended, _super); @@ -33,12 +35,17 @@ describe("Weak collections of native objects", function () { it("keeps a WeakMap entry for a native object across a wrapper change", function (done) { var owner = NSMutableArray.new(); var map = new WeakMap(); + var before = runtime.getWrapperLifetimeStats(); (function () { var obj = Extended.new(); map.set(obj, "value"); owner.addObject(obj); })(); settle(2, function () { + if (canary) { + // The first wrapper died; the entry belongs to the native object. + expect(runtime.getWrapperLifetimeStats().queuedReleases).toBeGreaterThan(before.queuedReleases); + } var obj = owner.objectAtIndex(0); expect(map.get(obj)).toBe("value"); expect(map.has(obj)).toBe(true); @@ -132,6 +139,78 @@ describe("Weak collections of native objects", function () { }); }); + it("keeps a getOrInsert entry for a native object across a wrapper change", function (done) { + if (typeof WeakMap.prototype.getOrInsert !== "function") { + done(); + return; + } + var owner = NSMutableArray.new(); + var map = new WeakMap(); + var seenKey; + (function () { + var obj = Extended.new(); + expect(map.getOrInsert(obj, "value")).toBe("value"); + var other = Extended.new(); + expect(map.getOrInsertComputed(other, function (key) { + seenKey = key; + return "computed"; + })).toBe("computed"); + expect(seenKey).toBe(other); + owner.addObject(obj); + owner.addObject(other); + })(); + settle(2, function () { + expect(map.get(owner.objectAtIndex(0))).toBe("value"); + expect(map.get(owner.objectAtIndex(1))).toBe("computed"); + done(); + }); + }); + + it("unregisters a FinalizationRegistry target through a later wrapper of its token", function (done) { + var owner = NSMutableArray.new(); + var registry = new FinalizationRegistry(function () {}); + var target = {}; + (function () { + var obj = Extended.new(); + registry.register(target, "held", obj); + owner.addObject(obj); + })(); + settle(2, function () { + expect(registry.unregister(owner.objectAtIndex(0))).toBe(true); + target = null; + expect(FinalizationRegistry.length).toBe(1); + done(); + }); + }); + + it("keys a Proxy around a native object by the Proxy", function () { + var obj = Extended.new(); + var proxy = new Proxy(obj, {}); + var map = new WeakMap(); + map.set(obj, "target"); + expect(map.has(proxy)).toBe(false); + map.set(proxy, "proxy"); + expect(map.get(obj)).toBe("target"); + expect(map.get(proxy)).toBe("proxy"); + expect(new WeakRef(proxy).deref()).toBe(proxy); + }); + + it("does not give state to a native object a lookup only probes", function () { + var before = runtime.getWrapperLifetimeStats(); + var map = new WeakMap(); + var set = new WeakSet(); + for (var i = 0; i < 8; i++) { + var obj = Extended.new(); + map.has(obj); + map.get(obj); + map.delete(obj); + set.has(obj); + } + if (canary) { + expect(runtime.getWrapperLifetimeStats().canaryStates).toBe(before.canaryStates); + } + }); + it("keeps working as the standard collections for other keys", function () { var key = {}; var map = new WeakMap([[key, 1]]); @@ -151,4 +230,31 @@ describe("Weak collections of native objects", function () { expect([].concat(Array.from(NSMutableArray.arrayWithArray(["a"])))).toEqual(["a"]); expect(Object.getOwnPropertySymbols(array).length).toBe(0); }); + + // A JS class gets a fast-enumeration override only for an iterator it + // defines itself, not for the one it inherits from NSArray. + it("enumerates natively a JS subclass of NSArray that inherits the iterator", function () { + var Pair = (function (_super) { + __extends(Pair, _super); + function Pair() { + return (_super !== null && _super.apply(this, arguments)) || this; + } + Pair.prototype.objectAtIndex = function (index) { + return index === 0 ? "first" : "second"; + }; + Object.defineProperty(Pair.prototype, "count", { + get: function () { + return 2; + }, + enumerable: true, + configurable: true + }); + return Pair; + })(NSArray); + var pair = Pair.alloc().init(); + TNSClearOutput(); + TNSObjCTypes.alloc().init().methodWithNSArray(pair); + expect(TNSGetOutput()).toBe("firstsecond"); + TNSClearOutput(); + }); }); diff --git a/docs/knowledge/v8-resurrecting-finalizers.md b/docs/knowledge/v8-resurrecting-finalizers.md index e823661d6..5444ebabd 100644 --- a/docs/knowledge/v8-resurrecting-finalizers.md +++ b/docs/knowledge/v8-resurrecting-finalizers.md @@ -305,10 +305,15 @@ keeps it alive. A canary built after a death is a new object, but `===` cannot t canary lives at a time, and anything still referencing the old one would have kept it alive. What could notice is whatever keys on the canary without keeping it. The weak collections don't: in canary mode a builtin (`js/weak-collections.js`) replaces `WeakMap`, `WeakSet`, -`WeakRef` and `FinalizationRegistry` with subclasses that key a native object by an opaque, -frozen key held by its state, so entries and targets live exactly as long as the native object, -and `WeakRef.prototype.deref` returns the live canary, a new one, or `undefined` once the object -is gone. Native `#private` fields are branded on the canary and do not carry over. Other +`WeakRef` and `FinalizationRegistry` with subclasses that key an in-scope native object (a +canary: not a plain object under `"extended"`, a tagged pointer, a block, or an object of a class +refusing weak references) by an opaque, frozen key held by its state, so entries and targets live +exactly as long as the native object, and `WeakRef.prototype.deref` returns the live canary, a new +one, or `undefined` once the object is gone. Lookups never create that key, and a Proxy around a +native object stays a key of its own. The cost is the other side of the same rooting: a `WeakMap` +value or a `FinalizationRegistry` held value that references its native key keeps the key, its +state and so the native object alive for as long as the collection lives, where in the other +modes the entry would be collectable. Native `#private` fields are branded on the canary and do not carry over. Other limits: `Object.freeze`, `seal` and `preventExtensions` throw on a canary (V8 refuses them on objects with interceptors); index-keyed properties are not mirrored and die with the canary; a write through an object whose prototype is a canary lands on that object, since diff --git a/types/ns-runtime.d.ts b/types/ns-runtime.d.ts index 7465a3545..ba86f6d78 100644 --- a/types/ns-runtime.d.ts +++ b/types/ns-runtime.d.ts @@ -58,9 +58,11 @@ declare module "ns:runtime" { * deallocated, and a wrapper built for the object later gets them back * with their attributes. Native property writes still reach the native * setter. Writes cost more than in the other modes; reads do not. - * WeakMap, WeakSet, WeakRef and FinalizationRegistry key native objects - * by their lifetime in this mode, so they carry over too. Native - * `#private` fields and index-keyed properties do not. + * WeakMap, WeakSet, WeakRef and FinalizationRegistry key such native + * objects by their lifetime in this mode, so they carry over too; a + * WeakMap value that references its own native key keeps that object + * alive while the map lives. Native `#private` fields and index-keyed + * properties do not. * State that references its own object keeps that object alive. */ wrapperLifetimeMode: "resurrecting" | "rooted" | "canary"; From c5791433e7e9fc3ac3b31ebd971c5550a5620db4 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 17 Nov 2026 02:31:00 -0300 Subject: [PATCH 05/12] fix(runtime): weak collections follow-up fixes - Unwrapping a receiver lent to JS from -dealloc (no canary) for a key returned early instead of reading a null canary. - FinalizationRegistry.register rejects a target that is its own held value, checked before unwrapping; new WeakRef keeps its target, not only the lifetime key, alive until the job ends. - An inherited JS iterator still gets the fast-enumeration override when no superclass enumerates natively. - Docs: only a WeakMap value pins its native key; a FinalizationRegistry keeps its held values in every mode. --- NativeScript/runtime/ClassBuilder.mm | 11 +++-- NativeScript/runtime/WrapperLifetimeCanary.mm | 4 ++ NativeScript/runtime/js/weak-collections.js | 11 ++++- .../WrapperLifetimeWeakCollectionsTests.js | 48 +++++++++++++++++++ docs/knowledge/v8-resurrecting-finalizers.md | 5 +- 5 files changed, 71 insertions(+), 8 deletions(-) diff --git a/NativeScript/runtime/ClassBuilder.mm b/NativeScript/runtime/ClassBuilder.mm index 701e64e00..f3b74fae3 100644 --- a/NativeScript/runtime/ClassBuilder.mm +++ b/NativeScript/runtime/ClassBuilder.mm @@ -762,13 +762,16 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { Local propertyNames; - // Only an iterator the class defines itself: an inherited one (NSArray's, - // or a JS superclass's) already backs the superclass's own enumeration. + // An inherited iterator only when no superclass enumerates natively: NSArray's + // prototype iterator, or a JS superclass's, already backs that enumeration. Local symbolIterator; bool ownsIterator = implementationObject->HasOwnProperty(context, Symbol::GetIterator(isolate)).FromMaybe(false); - bool success = !ownsIterator || implementationObject->Get(context, Symbol::GetIterator(isolate)) - .ToLocal(&symbolIterator); + bool inheritsEnumeration = class_respondsToSelector( + class_getSuperclass(extendedClass), @selector(countByEnumeratingWithState:objects:count:)); + bool success = (!ownsIterator && inheritsEnumeration) || + implementationObject->Get(context, Symbol::GetIterator(isolate)) + .ToLocal(&symbolIterator); tns::Assert(success, isolate); if (!symbolIterator.IsEmpty() && symbolIterator->IsFunction()) { diff --git a/NativeScript/runtime/WrapperLifetimeCanary.mm b/NativeScript/runtime/WrapperLifetimeCanary.mm index 3b6a0f439..8e3e60b2c 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.mm +++ b/NativeScript/runtime/WrapperLifetimeCanary.mm @@ -691,6 +691,10 @@ void UnwrapCallback(const FunctionCallbackInfo& info) { return; } ObjectWeakCallbackState* canary = CanaryOf(isolate, value.As()); + if (canary == nullptr) { + // A receiver lent to JS from -dealloc: its object is going away. + return; + } CanaryRecord* record = canary->canaryRecord_.get(); if (!record->weakSet) { if (objc_initWeakOrNil(&record->object, canary->lifetimeTarget_) == nil) { diff --git a/NativeScript/runtime/js/weak-collections.js b/NativeScript/runtime/js/weak-collections.js index 9e1a2ffae..2113ab7a1 100644 --- a/NativeScript/runtime/js/weak-collections.js +++ b/NativeScript/runtime/js/weak-collections.js @@ -79,7 +79,12 @@ class WeakSet extends NativeWeakSet { class WeakRef extends NativeWeakRef { constructor(target) { - super(unwrap(target, true)); + const key = unwrap(target, true); + super(key); + // The target itself, not only its key, stays alive until the job ends. + if (key !== target) { + new NativeWeakRef(target); + } } // The live wrapper, a new one if the old one died while the native object @@ -109,6 +114,10 @@ class FinalizationRegistry extends NativeFinalizationRegistry { } register(target, heldValue, unregisterToken) { + // Checked on the original values: unwrapping would make them differ. + if (target === heldValue) { + throw new TypeError("FinalizationRegistry.prototype.register: target and holdings must not be same"); + } super.register( unwrap(target, true), heldValue, diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js index 96cfacc49..d998e1aac 100644 --- a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -211,6 +211,39 @@ describe("Weak collections of native objects", function () { } }); + it("rejects a FinalizationRegistry registration whose target is its held value", function () { + var registry = new FinalizationRegistry(function () {}); + var obj = Extended.new(); + expect(function () { + registry.register(obj, obj); + }).toThrowError(TypeError); + }); + + it("lets a JS dealloc key a weak collection on its receiver", function (done) { + var map = new WeakMap(); + var ran = 0; + var Keyed = TNSAllocLog.extend({ + dealloc: function () { + map.set(this, "dying"); + ran++; + this.super.dealloc(); + } + }, { name: "WrapperLifetimeWeakCollectionsDeallocKey" }); + var owner = NSMutableArray.new(); + (function () { + var obj = Keyed.new(); + obj.tag = "state"; + owner.addObject(obj); + })(); + settle(2, function () { + owner.removeAllObjects(); + settle(2, function () { + expect(ran).toBe(1); + done(); + }); + }); + }); + it("keeps working as the standard collections for other keys", function () { var key = {}; var map = new WeakMap([[key, 1]]); @@ -257,4 +290,19 @@ describe("Weak collections of native objects", function () { expect(TNSGetOutput()).toBe("firstsecond"); TNSClearOutput(); }); + + // With no native enumeration to inherit, an inherited JS iterator still + // backs native enumeration. + it("enumerates natively a JS class whose iterator comes from its implementation's prototype", function () { + var Iterated = NSObject.extend(Object.create({ + [Symbol.iterator]: function* () { + yield "only"; + } + }), { name: "WrapperLifetimeWeakCollectionsInheritedIterator" }); + var obj = Iterated.alloc().init(); + TNSClearOutput(); + TNSObjCTypes.alloc().init().methodWithNSArray(obj); + expect(TNSGetOutput()).toBe("only"); + TNSClearOutput(); + }); }); diff --git a/docs/knowledge/v8-resurrecting-finalizers.md b/docs/knowledge/v8-resurrecting-finalizers.md index 5444ebabd..7dbddf9e5 100644 --- a/docs/knowledge/v8-resurrecting-finalizers.md +++ b/docs/knowledge/v8-resurrecting-finalizers.md @@ -311,9 +311,8 @@ refusing weak references) by an opaque, frozen key held by its state, so entries exactly as long as the native object, and `WeakRef.prototype.deref` returns the live canary, a new one, or `undefined` once the object is gone. Lookups never create that key, and a Proxy around a native object stays a key of its own. The cost is the other side of the same rooting: a `WeakMap` -value or a `FinalizationRegistry` held value that references its native key keeps the key, its -state and so the native object alive for as long as the collection lives, where in the other -modes the entry would be collectable. Native `#private` fields are branded on the canary and do not carry over. Other +value that references its native key keeps the key, its state and so the native object alive for +as long as the map lives, where in the other modes the entry would be collectable. Native `#private` fields are branded on the canary and do not carry over. Other limits: `Object.freeze`, `seal` and `preventExtensions` throw on a canary (V8 refuses them on objects with interceptors); index-keyed properties are not mirrored and die with the canary; a write through an object whose prototype is a canary lands on that object, since From 785391696e37c3c50fa8ffb56bc1a0009dd5cb47 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 17 Nov 2026 02:53:29 -0300 Subject: [PATCH 06/12] fix(runtime): weak collections builtin reads intrinsics from primordials TypeError and Object.defineProperty came off globals user code can replace; the builtins' lint rule requires the primordials. --- NativeScript/runtime/js/weak-collections.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/NativeScript/runtime/js/weak-collections.js b/NativeScript/runtime/js/weak-collections.js index 2113ab7a1..27d57b761 100644 --- a/NativeScript/runtime/js/weak-collections.js +++ b/NativeScript/runtime/js/weak-collections.js @@ -8,6 +8,7 @@ // lost with it. Every other value passes through unchanged. const { unwrap, wrap } = binding; +const { ObjectDefineProperty, TypeError } = primordials; const { FinalizationRegistry: NativeFinalizationRegistry, WeakMap: NativeWeakMap, @@ -38,7 +39,7 @@ class WeakMap extends NativeWeakMap { // V8 ships the upsert proposal; inherited, these would key on the wrapper. if (typeof NativeWeakMap.prototype.getOrInsert === "function") { - Object.defineProperty(WeakMap.prototype, "getOrInsert", { + ObjectDefineProperty(WeakMap.prototype, "getOrInsert", { value: function getOrInsert(key, value) { return NativeWeakMap.prototype.getOrInsert.call(this, unwrap(key, true), value); }, @@ -47,7 +48,7 @@ if (typeof NativeWeakMap.prototype.getOrInsert === "function") { }); } if (typeof NativeWeakMap.prototype.getOrInsertComputed === "function") { - Object.defineProperty(WeakMap.prototype, "getOrInsertComputed", { + ObjectDefineProperty(WeakMap.prototype, "getOrInsertComputed", { value: function getOrInsertComputed(key, callbackfn) { if (typeof callbackfn !== "function") { throw new TypeError("WeakMap.prototype.getOrInsertComputed: callback is not a function"); @@ -102,7 +103,7 @@ class WeakRef extends NativeWeakRef { // weak-ref.js gave the native prototype a `get` alias of `deref`; inherited // as is, it would hand out the lifetime key. -Object.defineProperty(WeakRef.prototype, "get", { +ObjectDefineProperty(WeakRef.prototype, "get", { value: WeakRef.prototype.deref, writable: true, configurable: true, @@ -136,7 +137,7 @@ for (const [name, value] of [ ["WeakRef", WeakRef], ["FinalizationRegistry", FinalizationRegistry], ]) { - Object.defineProperty(globalThis, name, { + ObjectDefineProperty(globalThis, name, { value, writable: true, configurable: true, From 357ee6373979ae617b65f59c4740a54a50d79b86 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 17 Nov 2026 03:37:29 -0300 Subject: [PATCH 07/12] test(runtime): canary rewrap specs check the old wrapper died by the release counter With weak collections keying native objects by lifetime, a WeakRef to the first canary hands back the live one, so it can no longer show that the first wrapper died. --- TestRunner/app/tests/WrapperLifetimeModesTests.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TestRunner/app/tests/WrapperLifetimeModesTests.js b/TestRunner/app/tests/WrapperLifetimeModesTests.js index 10f560f42..baff844ae 100644 --- a/TestRunner/app/tests/WrapperLifetimeModesTests.js +++ b/TestRunner/app/tests/WrapperLifetimeModesTests.js @@ -59,15 +59,15 @@ describe("Wrapper lifetime modes", function () { // canary mode on a new wrapper. function rewrap(make, callback) { var owner = NSMutableArray.new(); - var first; + var before = runtime.getWrapperLifetimeStats(); (function () { - var obj = make(); - owner.addObject(obj); - first = new WeakRef(obj); + owner.addObject(make()); })(); settle(2, function () { if (canary) { - expect(first.deref()).toBeUndefined(); + // The first wrapper died. A WeakRef can't show it here: the + // weak collections key a native object by its lifetime. + expect(runtime.getWrapperLifetimeStats().queuedReleases).toBeGreaterThan(before.queuedReleases); } callback(owner.objectAtIndex(0)); }); From 603d8e2a55b4a5558c7c750f53298fc92e546a5e Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 17 Nov 2026 03:53:15 -0300 Subject: [PATCH 08/12] style(runtime): clang-format --- NativeScript/runtime/ClassBuilder.mm | 6 +++--- NativeScript/runtime/MetadataBuilder.mm | 2 +- NativeScript/runtime/SymbolIterator.h | 6 ++++-- NativeScript/runtime/WeakRef.cpp | 7 ++++--- NativeScript/runtime/WrapperLifetimeCanary.h | 3 ++- NativeScript/runtime/WrapperLifetimeCanary.mm | 3 ++- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/NativeScript/runtime/ClassBuilder.mm b/NativeScript/runtime/ClassBuilder.mm index f3b74fae3..153794a1e 100644 --- a/NativeScript/runtime/ClassBuilder.mm +++ b/NativeScript/runtime/ClassBuilder.mm @@ -769,9 +769,9 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { implementationObject->HasOwnProperty(context, Symbol::GetIterator(isolate)).FromMaybe(false); bool inheritsEnumeration = class_respondsToSelector( class_getSuperclass(extendedClass), @selector(countByEnumeratingWithState:objects:count:)); - bool success = (!ownsIterator && inheritsEnumeration) || - implementationObject->Get(context, Symbol::GetIterator(isolate)) - .ToLocal(&symbolIterator); + bool success = + (!ownsIterator && inheritsEnumeration) || + implementationObject->Get(context, Symbol::GetIterator(isolate)).ToLocal(&symbolIterator); tns::Assert(success, isolate); if (!symbolIterator.IsEmpty() && symbolIterator->IsFunction()) { diff --git a/NativeScript/runtime/MetadataBuilder.mm b/NativeScript/runtime/MetadataBuilder.mm index c35f528b8..6bd249b64 100644 --- a/NativeScript/runtime/MetadataBuilder.mm +++ b/NativeScript/runtime/MetadataBuilder.mm @@ -12,10 +12,10 @@ #include "ObjectManager.h" #include "Runtime.h" #include "RuntimeConfig.h" +#include "SymbolIterator.h" #include "SymbolLoader.h" #include "Tasks.h" #include "Worker.h" -#include "SymbolIterator.h" #include "WrapperLifetimeCanary.h" using namespace v8; diff --git a/NativeScript/runtime/SymbolIterator.h b/NativeScript/runtime/SymbolIterator.h index 800da55bb..392f35412 100644 --- a/NativeScript/runtime/SymbolIterator.h +++ b/NativeScript/runtime/SymbolIterator.h @@ -7,8 +7,10 @@ namespace tns { class SymbolIterator { public: - // On NSArray's prototype template, so every array wrapper inherits it. - static void Install(v8::Isolate* isolate, v8::Local prototypeTemplate); + // On NSArray's prototype template, so every array wrapper inherits it. + static void Install(v8::Isolate* isolate, + v8::Local prototypeTemplate); + private: static v8::Local CreateIteratorObject(v8::Local context, v8::Local object); static void NextCallback(const v8::FunctionCallbackInfo& args); diff --git a/NativeScript/runtime/WeakRef.cpp b/NativeScript/runtime/WeakRef.cpp index 7831b1571..5523247a7 100644 --- a/NativeScript/runtime/WeakRef.cpp +++ b/NativeScript/runtime/WeakRef.cpp @@ -25,9 +25,10 @@ void WeakRef::Init(Local context) { return; } Local binding = Object::New(isolate); - success = WrapperLifetimeCanary::RegisterWrapFunctions(context, binding) && - BuiltinLoader::RunBuiltin(context, BuiltinId::kWeakCollections, binding) - .ToLocal(&result); + success = + WrapperLifetimeCanary::RegisterWrapFunctions(context, binding) && + BuiltinLoader::RunBuiltin(context, BuiltinId::kWeakCollections, binding) + .ToLocal(&result); if (!success && tc.HasCaught()) { tns::LogError(isolate, tc); } diff --git a/NativeScript/runtime/WrapperLifetimeCanary.h b/NativeScript/runtime/WrapperLifetimeCanary.h index 1bb2cf0af..d71242185 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.h +++ b/NativeScript/runtime/WrapperLifetimeCanary.h @@ -74,7 +74,8 @@ class WrapperLifetimeCanary { // Adds `unwrap` (a canary's native object -> its weak-collection key) and // `wrap` (key -> live or new canary, or undefined once the object is gone) // to `binding`, for the weak-collections builtin. Canary mode only. - static bool RegisterWrapFunctions(v8::Local context, v8::Local binding); + static bool RegisterWrapFunctions(v8::Local context, + v8::Local binding); // The record of an object that already has state in this isolate, for a new // canary to share. Null otherwise. diff --git a/NativeScript/runtime/WrapperLifetimeCanary.mm b/NativeScript/runtime/WrapperLifetimeCanary.mm index 8e3e60b2c..ce5ff0a72 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.mm +++ b/NativeScript/runtime/WrapperLifetimeCanary.mm @@ -761,7 +761,8 @@ void WrapCallback(const FunctionCallbackInfo& info) { // A live wrapper is handed back as is: building one through ConvertArgument // would reset a protocol-typed wrapper's prototype to its class's. std::shared_ptr cache = Caches::Get(isolate); - std::shared_ptr> cached = ArgConverter::FindCachedInstance(isolate, cache, object); + std::shared_ptr> cached = + ArgConverter::FindCachedInstance(isolate, cache, object); if (cached != nullptr && !cached->IsEmpty()) { [object release]; info.GetReturnValue().Set(cached->Get(isolate)); From 17a8868f5f47664208ac6f113b85c67fac16d643 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 17 Nov 2026 11:09:36 -0300 Subject: [PATCH 09/12] test(runtime): native constructor canary spec checks the old wrapper died by the release counter With weak collections keying native objects by lifetime, a WeakRef to the first canary hands back the live one. --- TestRunner/app/tests/WrapperLifetimeModesTests.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/TestRunner/app/tests/WrapperLifetimeModesTests.js b/TestRunner/app/tests/WrapperLifetimeModesTests.js index baff844ae..1cadf4cdc 100644 --- a/TestRunner/app/tests/WrapperLifetimeModesTests.js +++ b/TestRunner/app/tests/WrapperLifetimeModesTests.js @@ -1476,20 +1476,21 @@ describe("Wrapper lifetime modes", function () { it("gives a native constructor's JS-extended result the write handler", function (done) { var owner = NSMutableArray.new(); var handle; - var first; + var released = stats().queuedReleases; (function () { var obj = Extended.new(); obj.tag = "kept"; owner.addObject(obj); handle = interop.handleof(obj); - first = new WeakRef(obj); })(); settle(2, function () { var before = stats(); var again = new TNSAllocLog(handle); expect(again instanceof TNSAllocLog).toBe(true); if (canary) { - expect(first.deref()).toBeUndefined(); + // The first wrapper died (a WeakRef can't show it: weak + // collections key a native object by its lifetime). + expect(before.queuedReleases).toBeGreaterThan(released); expect(Object.getPrototypeOf(again)).toBe(TNSAllocLog.prototype); } if (keepsState) { From 7a6010a3693de36801337ee17e8aed6106752436 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Wed, 18 Nov 2026 01:53:10 -0300 Subject: [PATCH 10/12] fix(runtime): canary mode gives no state to objects whose class forbids associated objects objc_setAssociatedObject aborts the process for an instance of a class flagged RW_FORBIDS_ASSOCIATED_OBJECTS (Swift actors, for one). Under "scope": "all", a JS property write or a weak-collection key on such an object attached the canary's lifecycle token and terminated the app. StateForWrite now reads that flag on the object's class through objc_debug_class_rw_data_mask (resolved with dlsym) before creating a record, and declines when it is set or cannot be read: the object's JS properties stay on the canary and the weak collections key its wrapper, as WEAK_SWEEP already does for classes refusing weak references. Adds a fixture class flagged at runtime and a spec covering WeakMap, WeakSet, WeakRef, FinalizationRegistry and property writes on it, and extends the plain-object WeakMap/WeakRef spec to canary mode. --- NativeScript/runtime/WrapperLifetimeCanary.mm | 43 +++++++++++++++++-- TestFixtures/Marshalling/TNSAllocLog.h | 11 +++++ TestFixtures/Marshalling/TNSAllocLog.m | 31 +++++++++++++ .../app/tests/WrapperLifetimeModesTests.js | 4 +- .../WrapperLifetimeWeakCollectionsTests.js | 25 +++++++++++ docs/knowledge/v8-resurrecting-finalizers.md | 16 ++++--- 6 files changed, 119 insertions(+), 11 deletions(-) diff --git a/NativeScript/runtime/WrapperLifetimeCanary.mm b/NativeScript/runtime/WrapperLifetimeCanary.mm index ce5ff0a72..880e0fe02 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.mm +++ b/NativeScript/runtime/WrapperLifetimeCanary.mm @@ -1,5 +1,6 @@ #include "WrapperLifetimeCanary.h" #import +#include #include #include #include @@ -22,10 +23,14 @@ // their class's own -dealloc (InstallDeallocHook) either way; for others: // - ASSOCIATION: a token associated with the object reports it from its // -dealloc, immediately and on any thread. The ObjC runtime aborts the -// process for a class that forbids associated objects (no public query). +// process for a class that forbids associated objects (Swift actors, for +// one) and has no public query for it: ForbidsAssociatedObjects reads the +// runtime's private flag instead. // - WEAK_SWEEP: each record holds a weak reference to the object, checked by a -// sweep after every major collection, so state lingers until then. Classes -// that refuse weak references keep their JS properties on the canary. +// sweep after every major collection, so state lingers until then. +// An object the chosen signal cannot watch (a class forbidding associated +// objects, or refusing weak references) gets no state: its JS properties stay +// on the canary and the weak collections key its wrapper. #define NS_CANARY_DEATH_ASSOCIATION 1 #define NS_CANARY_DEATH_WEAK_SWEEP 2 #ifndef NS_CANARY_DEATH_SIGNAL @@ -221,6 +226,33 @@ bool IsExtendedInstance(id object) { return false; } +#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION +// The check objc_setAssociatedObject aborts on: RW_FORBIDS_ASSOCIATED_OBJECTS +// in the class_rw_t flags of the object's own class, reached through the mask +// libobjc exports for debuggers. Private layout (objc_class keeps `bits` after +// isa, superclass and the cache; the flags lead class_rw_t), so anything it +// cannot read, a missing mask or an unrealized class, counts as forbidding. Not +// a message, as IsExtendedInstance. +bool ForbidsAssociatedObjects(id object) { + static const uintptr_t* rwDataMask = + static_cast(dlsym(RTLD_DEFAULT, "objc_debug_class_rw_data_mask")); + if (rwDataMask == nullptr) { + return true; + } + struct ClassLayout { + void* isa; + void* superclass; + void* cache[2]; + uintptr_t bits; + }; + const auto* cls = static_cast((__bridge const void*)object_getClass(object)); + const auto* flags = reinterpret_cast(cls->bits & *rwDataMask); + constexpr uint32_t kRealized = 1u << 31; + constexpr uint32_t kForbidsAssociatedObjects = 1u << 20; + return flags == nullptr || (*flags & kRealized) == 0 || (*flags & kForbidsAssociatedObjects) != 0; +} +#endif + #if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP // Lets go of the state of every object that was deallocated. Runs as an event // loop task (under the Locker, on the runtime's thread), never inside the GC. @@ -452,6 +484,11 @@ bool StateForWrite(Isolate* isolate, Local holder, Local* out) { bool replacesDied = canary->canaryRecord_ != nullptr; // A JS-extended class's own -dealloc reports its instances' deaths. bool extended = IsExtendedInstance(object); +#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION + if (!extended && ForbidsAssociatedObjects(object)) { + return false; + } +#endif // Allocated before the lock: a collection this allocation starts can run // callbacks (the weak sweep's, a finalizer's -dealloc) that take it. Local freshState = Object::New(isolate, Null(isolate), nullptr, nullptr, 0); diff --git a/TestFixtures/Marshalling/TNSAllocLog.h b/TestFixtures/Marshalling/TNSAllocLog.h index e9e035362..9fdcda2ef 100644 --- a/TestFixtures/Marshalling/TNSAllocLog.h +++ b/TestFixtures/Marshalling/TNSAllocLog.h @@ -248,4 +248,15 @@ id TNSOwnedPrimitivesObjectAt(TNSOwnedPrimitives* holder, NSUInteger index); @end +// Hands out instances of a class made at runtime and flagged the way the ObjC +// runtime flags Swift actors: objc_setAssociatedObject on one aborts the +// process. +@interface TNSAssociationForbidder : NSObject + ++ (id)forbiddingInstance; +// Whether the class of `object` carries that flag. ++ (BOOL)forbidsAssociatedObjects:(id)object; + +@end + #endif /* TNSAllocLog_h */ diff --git a/TestFixtures/Marshalling/TNSAllocLog.m b/TestFixtures/Marshalling/TNSAllocLog.m index 190f365a2..b409e2244 100644 --- a/TestFixtures/Marshalling/TNSAllocLog.m +++ b/TestFixtures/Marshalling/TNSAllocLog.m @@ -1,5 +1,6 @@ #import "TNSAllocLog.h" #import +#import #import "../TNSTestCommon.h" @implementation TNSAllocLog @@ -488,3 +489,33 @@ + (void)hookPlaceholder { } @end + +extern uintptr_t objc_debug_class_rw_data_mask; + +// RW_FORBIDS_ASSOCIATED_OBJECTS, in the class_rw_t flags objc_class reaches +// through `bits`, after isa, superclass and the cache. +static const uint32_t TNSForbidsAssociatedObjectsFlag = 1u << 20; + +static uint32_t* TNSClassRWFlags(Class cls) { + uintptr_t bits = ((const uintptr_t*)(__bridge const void*)cls)[4]; + return (uint32_t*)(bits & objc_debug_class_rw_data_mask); +} + +@implementation TNSAssociationForbidder + ++ (id)forbiddingInstance { + static Class forbidding; + static dispatch_once_t once; + dispatch_once(&once, ^{ + forbidding = objc_allocateClassPair([NSObject class], "TNSAssociationForbiddingObject", 0); + *TNSClassRWFlags(forbidding) |= TNSForbidsAssociatedObjectsFlag; + objc_registerClassPair(forbidding); + }); + return [[forbidding alloc] init]; +} + ++ (BOOL)forbidsAssociatedObjects:(id)object { + return (*TNSClassRWFlags(object_getClass(object)) & TNSForbidsAssociatedObjectsFlag) != 0; +} + +@end diff --git a/TestRunner/app/tests/WrapperLifetimeModesTests.js b/TestRunner/app/tests/WrapperLifetimeModesTests.js index 1cadf4cdc..53b3bd2dd 100644 --- a/TestRunner/app/tests/WrapperLifetimeModesTests.js +++ b/TestRunner/app/tests/WrapperLifetimeModesTests.js @@ -136,7 +136,7 @@ describe("Wrapper lifetime modes", function () { }); }); - it("keeps WeakMap entries and WeakRef targets of natively owned objects when every wrapper is rooted", function (done) { + it("keeps WeakMap entries and WeakRef targets of natively owned objects when every wrapper is in scope", function (done) { var owner = NSMutableArray.new(); var map = new WeakMap(); var ref; @@ -148,7 +148,7 @@ describe("Wrapper lifetime modes", function () { })(); settle(2, function () { var obj = owner.objectAtIndex(0); - if (rooted && scope === "all") { + if (keepsState && scope === "all") { expect(map.get(obj)).toBe("value"); expect(ref.deref()).toBe(obj); } diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js index d998e1aac..a3f5832b4 100644 --- a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -244,6 +244,31 @@ describe("Weak collections of native objects", function () { }); }); + // Under canary mode such an object gets no state: the token that would + // report its deallocation cannot be associated with it. + it("keys an object whose class forbids associated objects by its wrapper", function () { + var obj = TNSAssociationForbidder.forbiddingInstance(); + expect(TNSAssociationForbidder.forbidsAssociatedObjects(obj)).toBe(true); + var before = runtime.getWrapperLifetimeStats(); + var map = new WeakMap(); + map.set(obj, "value"); + expect(map.get(obj)).toBe("value"); + var set = new WeakSet(); + set.add(obj); + expect(set.has(obj)).toBe(true); + expect(new WeakRef(obj).deref()).toBe(obj); + var registry = new FinalizationRegistry(function () {}); + registry.register(obj, "held", obj); + expect(registry.unregister(obj)).toBe(true); + obj.tag = "kept"; + Object.defineProperty(obj, "defined", { value: 1, configurable: true }); + expect(obj.tag).toBe("kept"); + expect(obj.defined).toBe(1); + if (canary) { + expect(runtime.getWrapperLifetimeStats().canaryStates).toBe(before.canaryStates); + } + }); + it("keeps working as the standard collections for other keys", function () { var key = {}; var map = new WeakMap([[key, 1]]); diff --git a/docs/knowledge/v8-resurrecting-finalizers.md b/docs/knowledge/v8-resurrecting-finalizers.md index 7dbddf9e5..d33923946 100644 --- a/docs/knowledge/v8-resurrecting-finalizers.md +++ b/docs/knowledge/v8-resurrecting-finalizers.md @@ -288,17 +288,21 @@ inherits, whose receiver still reads the state), and then retires them. For othe a compile-time choice (`NS_CANARY_DEATH_SIGNAL` in `WrapperLifetimeCanary.mm`): by default a token associated with the object reports it from its `-dealloc`, immediately and on any thread, but the ObjC runtime aborts -the process for a class that forbids associated objects, and there is no public query for that. -Code that calls `objc_removeAssociatedObjects` on a live object releases the token too, which +the process for a class that forbids associated objects (Swift actors, for one), and there is no +public query for that. The runtime checks the flag `objc_setAssociatedObject` aborts on +(`RW_FORBIDS_ASSOCIATED_OBJECTS` in the object's class's `class_rw_t`, reached through the +`objc_debug_class_rw_data_mask` libobjc exports for debuggers) before it attaches a token; an +object it flags, or whose class it cannot read, gets no state: its JS properties stay on the +canary and are gone after a replacement, and the weak collections key its wrapper. Code that +calls `objc_removeAssociatedObjects` on a live object releases the token too, which retires the state as if the object had died: the next write through the canary starts a new state from the canary's properties, but if the canary dies before such a write, a canary built later starts without them. The alternative keeps an ObjC weak reference in each record (`objc_initWeakOrNil`, which declines instead of aborting for classes that refuse weak references) and sweeps the records after every major collection, so state lingers until then. An instance of a class that refuses weak references and is not JS-extended gets no state -under it: its properties live on the canary alone and are gone after a replacement. With the -default, `"scope": "all"` is experimental because of that abort; `"extended"` attaches no -tokens at all. It cannot be +under it, in the same way. With the default, `"scope": "all"` stays experimental because that +check reads private runtime layout; `"extended"` attaches no tokens at all. It cannot be rooted at canary death instead, because a state reachable only through the canary is garbage in the same collection. The cost: state that references its own object (a closure over `this`) keeps it alive. A canary built after a death is a new object, but `===` cannot tell: only one @@ -307,7 +311,7 @@ What could notice is whatever keys on the canary without keeping it. The weak co don't: in canary mode a builtin (`js/weak-collections.js`) replaces `WeakMap`, `WeakSet`, `WeakRef` and `FinalizationRegistry` with subclasses that key an in-scope native object (a canary: not a plain object under `"extended"`, a tagged pointer, a block, or an object of a class -refusing weak references) by an opaque, frozen key held by its state, so entries and targets live +refusing weak references or forbidding associated objects) by an opaque, frozen key held by its state, so entries and targets live exactly as long as the native object, and `WeakRef.prototype.deref` returns the live canary, a new one, or `undefined` once the object is gone. Lookups never create that key, and a Proxy around a native object stays a key of its own. The cost is the other side of the same rooting: a `WeakMap` From c57272ad6a7bb966e62c0319ce6685a2af723da1 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Wed, 18 Nov 2026 10:24:52 -0300 Subject: [PATCH 11/12] docs(runtime): weak collections lose a live object's keys to a removed canary token --- docs/knowledge/v8-resurrecting-finalizers.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/knowledge/v8-resurrecting-finalizers.md b/docs/knowledge/v8-resurrecting-finalizers.md index d33923946..9e05926e4 100644 --- a/docs/knowledge/v8-resurrecting-finalizers.md +++ b/docs/knowledge/v8-resurrecting-finalizers.md @@ -297,7 +297,14 @@ canary and are gone after a replacement, and the weak collections key its wrappe calls `objc_removeAssociatedObjects` on a live object releases the token too, which retires the state as if the object had died: the next write through the canary starts a new state from the canary's properties, but if the canary dies before such a write, a canary built -later starts without them. The alternative keeps an ObjC weak reference in each record +later starts without them. The weak collections' key for the object hangs off that state, so +what they keyed on the object is lost with it: `WeakMap` and `WeakSet` lookups miss, +`WeakRef.prototype.deref` returns `undefined`, and once the old key is collected the +`FinalizationRegistry` cleanup for it runs although the object lives. A JS write through the +canary starts a new state but no key; the next `set`, `add`, `new WeakRef` or `register` on the +object makes a new key, and entries keyed on the old one never come back. This stays a +limitation: `objc_removeAssociatedObjects` removes every client's associations, and Apple +documents it as not for general use. The alternative keeps an ObjC weak reference in each record (`objc_initWeakOrNil`, which declines instead of aborting for classes that refuse weak references) and sweeps the records after every major collection, so state lingers until then. An instance of a class that refuses weak references and is not JS-extended gets no state From 983741fe67bc7db3c6facc7cfca5b1097f9fd4dd Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Wed, 18 Nov 2026 10:24:52 -0300 Subject: [PATCH 12/12] test(runtime): canary specs check the old wrapper died through the engine's WeakRef --- .../app/tests/WrapperLifetimeModesTests.js | 33 +++++++++++-------- .../WrapperLifetimeWeakCollectionsTests.js | 7 ++-- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/TestRunner/app/tests/WrapperLifetimeModesTests.js b/TestRunner/app/tests/WrapperLifetimeModesTests.js index 53b3bd2dd..197721051 100644 --- a/TestRunner/app/tests/WrapperLifetimeModesTests.js +++ b/TestRunner/app/tests/WrapperLifetimeModesTests.js @@ -34,6 +34,10 @@ describe("Wrapper lifetime modes", function () { return table.allObjects.count; } + // The engine's WeakRef: in canary mode the global one keys a native object + // by its lifetime, so it outlives the wrapper it was given. + var WrapperWeakRef = canary ? Object.getPrototypeOf(WeakRef) : WeakRef; + function deallocs() { return (TNSGetOutput().match(/TNSAllocLog dealloc/g) || []).length; } @@ -59,15 +63,15 @@ describe("Wrapper lifetime modes", function () { // canary mode on a new wrapper. function rewrap(make, callback) { var owner = NSMutableArray.new(); - var before = runtime.getWrapperLifetimeStats(); + var first; (function () { - owner.addObject(make()); + var obj = make(); + owner.addObject(obj); + first = new WrapperWeakRef(obj); })(); settle(2, function () { if (canary) { - // The first wrapper died. A WeakRef can't show it here: the - // weak collections key a native object by its lifetime. - expect(runtime.getWrapperLifetimeStats().queuedReleases).toBeGreaterThan(before.queuedReleases); + expect(first.deref()).toBeUndefined(); } callback(owner.objectAtIndex(0)); }); @@ -493,18 +497,20 @@ describe("Wrapper lifetime modes", function () { it("hands back a native object's JS properties on a new wrapper after the old one died", function (done) { var owner = NSMutableArray.new(); var first; - var before = runtime.getWrapperLifetimeStats(); + var firstWrapper; (function () { var obj = Extended.new(); obj.tag = "kept"; owner.addObject(obj); first = new WeakRef(obj); + firstWrapper = new WrapperWeakRef(obj); })(); settle(2, function () { if (canary) { - // The old canary died; a WeakRef keys the native object by its - // lifetime in this mode, so it hands back the live one. - expect(runtime.getWrapperLifetimeStats().queuedReleases).toBeGreaterThan(before.queuedReleases); + expect(WrapperWeakRef).not.toBe(WeakRef); + expect(firstWrapper.deref()).toBeUndefined(); + // The global WeakRef keys the native object, so it hands back + // the live one on a new wrapper. expect(first.deref().tag).toBe("kept"); } var obj = owner.objectAtIndex(0); @@ -1476,21 +1482,22 @@ describe("Wrapper lifetime modes", function () { it("gives a native constructor's JS-extended result the write handler", function (done) { var owner = NSMutableArray.new(); var handle; - var released = stats().queuedReleases; + var first; (function () { var obj = Extended.new(); obj.tag = "kept"; owner.addObject(obj); handle = interop.handleof(obj); + first = new WrapperWeakRef(obj); })(); settle(2, function () { + if (canary) { + expect(first.deref()).toBeUndefined(); + } var before = stats(); var again = new TNSAllocLog(handle); expect(again instanceof TNSAllocLog).toBe(true); if (canary) { - // The first wrapper died (a WeakRef can't show it: weak - // collections key a native object by its lifetime). - expect(before.queuedReleases).toBeGreaterThan(released); expect(Object.getPrototypeOf(again)).toBe(TNSAllocLog.prototype); } if (keepsState) { diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js index a3f5832b4..968eda912 100644 --- a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -5,6 +5,8 @@ describe("Weak collections of native objects", function () { var runtime = require("ns:runtime"); var canary = runtime.getConfig("wrapperLifetimeMode") === "canary"; + // The engine's WeakRef, which keys the wrapper itself. + var WrapperWeakRef = canary ? Object.getPrototypeOf(WeakRef) : WeakRef; var Extended = (function (_super) { __extends(Extended, _super); @@ -35,16 +37,17 @@ describe("Weak collections of native objects", function () { it("keeps a WeakMap entry for a native object across a wrapper change", function (done) { var owner = NSMutableArray.new(); var map = new WeakMap(); - var before = runtime.getWrapperLifetimeStats(); + var first; (function () { var obj = Extended.new(); map.set(obj, "value"); owner.addObject(obj); + first = new WrapperWeakRef(obj); })(); settle(2, function () { if (canary) { // The first wrapper died; the entry belongs to the native object. - expect(runtime.getWrapperLifetimeStats().queuedReleases).toBeGreaterThan(before.queuedReleases); + expect(first.deref()).toBeUndefined(); } var obj = owner.objectAtIndex(0); expect(map.get(obj)).toBe("value");