Skip to content
Merged
15 changes: 14 additions & 1 deletion NativeScript/runtime/ObjectManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ void DisposeHandle(v8::Isolate* isolate,
if (data != nil) {
std::shared_ptr<Caches> cache = Caches::Get(isolate);
auto it = cache->Instances.find(data);
bool canary = false;
if (ObjectWeakCallbackState* managed = objcWrapper->OwnershipState()) {
// The handle ReconcileOwnership manages is this wrapper's own, whatever
// Instances maps the object to now.
Expand All @@ -845,7 +846,8 @@ void DisposeHandle(v8::Isolate* isolate,
// walk, or an outer call still writing the canary's key below.
return;
}
if (adopted->lifetimeCanary_) {
canary = adopted->lifetimeCanary_;
if (canary) {
// Before the release below, which can run the object's -dealloc. The
// write can allocate, and the finalizers of the collection that
// triggers can run JS that calls this builtin on the same wrapper.
Expand Down Expand Up @@ -893,6 +895,17 @@ void DisposeHandle(v8::Isolate* isolate,
}
}

if (!canary && !objcWrapper->IsAdapterClaim()) {
// Before the release below, which can run the object's -dealloc. The
// write can allocate, and the finalizers of the collection that
// triggers can run JS that calls this builtin on the same wrapper: a
// release that got through freed the wrapper.
WrapperLifetimeCanary::KeepPlainWeakCollectionKey(isolate, value.As<Object>(), data);
if (tns::GetValue(isolate, value) != wrapper) {
return;
}
}

// Release the runtime's strong reference (taken when the object was first
// wrapped or adopted from an alloc/new/copy return). For instances solely
// owned by JS this deallocates them, on the runtime's thread (at once when
Expand Down
3 changes: 3 additions & 0 deletions NativeScript/runtime/WrapperLifetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class WrapperLifetime {
// of a JS-extended class.
static bool InScope(id obj);

// A tagged pointer or a block: never in scope, whatever the scope.
static bool Excluded(id obj);

// A newly wrapped ObjC object: `handle` is its Instances entry, registered by
// ObjectManager::Register and holding the entry's +1 on `target`.
// `kind`: the canary handlers the wrapper carries
Expand Down
2 changes: 2 additions & 0 deletions NativeScript/runtime/WrapperLifetime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ void ReleaseAll(Isolate* isolate, RootedState* rooted) {

bool WrapperLifetime::InScope(id obj) { return IsInScope(obj); }

bool WrapperLifetime::Excluded(id obj) { return IsTaggedPointer(obj) || IsBlock(obj); }

void WrapperLifetime::Adopt(Isolate* isolate, ObjCDataWrapper* wrapper,
const std::shared_ptr<Persistent<Value>>& handle, id target,
CanaryKind kind) {
Expand Down
22 changes: 19 additions & 3 deletions NativeScript/runtime/WrapperLifetimeCanary.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,16 @@ class WrapperLifetimeCanary {
// anew, as it does any result whose kind differs from the template's.
static CanaryKind NativeInstancesKind();

// 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.
// Adds `unwrap` (a canary's native object -> its weak-collection key),
// `wrap` (key -> live or new wrapper, or undefined once the object is gone)
// and `cache` (the Uint32Array that tells the builtin when a wrapper it
// cached from wrap is stale) to `binding`, for the weak-collections builtin.
// Canary mode only. Under scope "extended", a plain object (not JS-extended,
// not a tagged pointer or a block) gets a key-only record the first time a
// weak collection keys it: its wrappers are no canaries, so unwrap finds
// that record by the object's address, and wrap builds a plain wrapper.
// Nothing is attached to the object: a weak reference tells when it is
// gone, read by a sweep after each major collection.
static bool RegisterWrapFunctions(v8::Local<v8::Context> context,
v8::Local<v8::Object> binding);

Expand All @@ -119,6 +126,15 @@ class WrapperLifetimeCanary {
static void KeepWeakCollectionKey(v8::Isolate* isolate,
v8::Local<v8::Object> canary);

// KeepWeakCollectionKey for a wrapper of `object` that is not a canary,
// before the object is released: with the key-only record of a plain object
// a weak collection keyed, or with none. No-op unless the mode is canary,
// and for objects that can never have a key (a tagged pointer, a block,
// or under scope "all" anything that is not a canary).
static void KeepPlainWeakCollectionKey(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
id object);

// Takes a collected or released canary's reference to its record. A record
// that died under that canary kept its state for it (it is the forwarding
// canary's only copy), which can go now. Isolate thread.
Expand Down
Loading
Loading