Skip to content

Commit cc2e77d

Browse files
committed
test(runtime): weak collections on plain canaries without state
Under scope "extended" a plain wrapper is now a canary without state, so the spec that kept a plain object's JS property across a wrapper change expects it there only under "all" (and "resurrecting"), and the first canary of a plain object dies in either canary scope. New specs, in canary mode: a keyed Java-held plain object whose canary died is found through the canary a deref builds and through the next one a Java read makes, neither of which has the old JS properties or a state; a FinalizationRegistry callback for a plain object runs once Java collected it and not while Java holds it; a released plain canary keeps what was keyed on it. The docs suggest a WeakMap keyed by the native object for plain-object state under "extended".
1 parent f9e7a09 commit cc2e77d

3 files changed

Lines changed: 111 additions & 11 deletions

File tree

‎docs/ns-builtin-modules.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ their JS properties:
239239
object for JS; when Java hands the object back later, its wrapper is a new
240240
object without the old one's JS properties. JS properties on plain native
241241
objects are therefore not preserved under `"extended"`: keep such state on a
242-
JS object of your own, or use `"all"`, which gives every wrapper state.
242+
JS object of your own, such as a `WeakMap` keyed by the native object (in
243+
canary mode its entries live exactly as long as the Java object, see below),
244+
or use `"all"`, which gives every wrapper state.
243245
Array buffers backed by Java buffers keep the `"resurrecting"` lifetime under
244246
either scope.
245247

‎test-app/app/src/main/assets/app/tests/testWrapperLifetimeWeakCollections.js‎

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
describe("Weak collections of Java objects", function () {
44
var runtime = require("ns:runtime");
55
var mode = runtime.getConfig("wrapperLifetimeMode");
6+
var scope = runtime.getConfig("wrapperLifetimeScope");
67
var canary = mode === "canary";
78
var Holder = com.tns.tests.WrapperHolder;
89

@@ -158,8 +159,9 @@ describe("Weak collections of Java objects", function () {
158159
}, 0);
159160
});
160161

161-
// Under scope "all" a plain wrapper is a canary: its key hangs off its state, and deref
162-
// makes a new wrapper the usual way, hydrated from the state.
162+
// In canary mode a plain wrapper is a canary: its key hangs off its record, and deref
163+
// makes a new wrapper the usual way, with the old one's JS properties under scope "all"
164+
// only.
163165
it("keeps a WeakMap entry and a WeakRef target for a Java-held plain object across a wrapper change", function (done) {
164166
var map = new WeakMap();
165167
var ref = (function () {
@@ -176,11 +178,109 @@ describe("Weak collections of Java objects", function () {
176178
var target = ref.deref();
177179
expect(target).toBeDefined();
178180
expect(target.toString()).toBe("plain");
179-
expect(target.tag).toBe("state");
181+
if (canary && scope === "extended") {
182+
expect(target.tag).toBeUndefined();
183+
} else {
184+
expect(target.tag).toBe("state");
185+
}
180186
done();
181187
}, 0);
182188
});
183189

190+
// A keyed plain wrapper under scope "extended" is a canary with a record and no state:
191+
// the next one is a new, empty object that stands for the same key.
192+
it("finds a keyed Java-held plain object through its next canary, which has no state, under scope extended", function (done) {
193+
if (!canary || scope !== "extended") {
194+
done();
195+
return;
196+
}
197+
var NativeWeakRef = Object.getPrototypeOf(WeakRef);
198+
var map = new WeakMap();
199+
var set = new WeakSet();
200+
var registry = new FinalizationRegistry(function () {});
201+
var target = {};
202+
var states = runtime.getWrapperLifetimeStats().canaryStates;
203+
var first;
204+
var refs = (function () {
205+
var builder = new java.lang.StringBuilder("plain");
206+
builder.tag = "lost";
207+
map.set(builder, "value");
208+
set.add(builder);
209+
registry.register(target, "held", builder);
210+
Holder.held = builder;
211+
first = new NativeWeakRef(builder);
212+
return [new WeakRef(builder), new WeakRef(builder)];
213+
})();
214+
setTimeout(function () {
215+
collectBoth();
216+
expect(first.deref()).toBeUndefined();
217+
// No canary is alive: deref builds one.
218+
var built = refs[0].deref();
219+
expect(built).toBeDefined();
220+
expect(built.toString()).toBe("plain");
221+
expect(built.tag).toBeUndefined();
222+
expect(Holder.held === built).toBe(true);
223+
built = null;
224+
collectBoth();
225+
var again = Holder.held;
226+
expect(again.tag).toBeUndefined();
227+
expect(map.get(again)).toBe("value");
228+
expect(map.has(again)).toBe(true);
229+
expect(set.has(again)).toBe(true);
230+
expect(refs[1].deref() === again).toBe(true);
231+
expect(registry.unregister(again)).toBe(true);
232+
expect(runtime.getWrapperLifetimeStats().canaryStates).toBe(states);
233+
done();
234+
}, 0);
235+
});
236+
237+
it("runs a FinalizationRegistry callback for a plain object only once Java collected it", function (done) {
238+
if (!canary) {
239+
done();
240+
return;
241+
}
242+
var heldCleaned = false;
243+
var droppedCleaned = false;
244+
var registry = new FinalizationRegistry(function (held) {
245+
if (held === "held") {
246+
heldCleaned = true;
247+
} else if (held === "dropped") {
248+
droppedCleaned = true;
249+
}
250+
});
251+
(function () {
252+
var held = new java.lang.StringBuilder("held");
253+
Holder.held = held;
254+
registry.register(held, "held");
255+
registry.register(new java.lang.StringBuilder("dropped"), "dropped");
256+
})();
257+
function finish() {
258+
expect(heldCleaned).toBe(false);
259+
expect(Holder.held.toString()).toBe("held");
260+
done();
261+
}
262+
finish.fail = done.fail;
263+
eventually(function () {
264+
return droppedCleaned;
265+
}, finish, "the cleanup callback did not run");
266+
}, 20000);
267+
268+
it("keeps what was keyed on a plain Java object after its link is released", function () {
269+
if (!canary) {
270+
return;
271+
}
272+
var map = new WeakMap();
273+
var set = new WeakSet();
274+
var builder = new java.lang.StringBuilder("released");
275+
map.set(builder, "value");
276+
set.add(builder);
277+
__releaseNativeCounterpart(builder);
278+
collectBoth();
279+
expect(map.get(builder)).toBe("value");
280+
expect(set.has(builder)).toBe(true);
281+
expect(new WeakRef(builder).deref() === builder).toBe(true);
282+
});
283+
184284
it("clears a WeakRef once Java collected the object", function (done) {
185285
var ref = (function () {
186286
return new WeakRef(new java.lang.Runnable({ run: function () {} }));
@@ -441,10 +541,7 @@ describe("Weak collections of Java objects", function () {
441541
setTimeout(function () {
442542
collectBoth();
443543
if (canary) {
444-
// A plain wrapper is a canary under scope "all" only.
445-
if (runtime.getConfig("wrapperLifetimeScope") === "all") {
446-
expect(firstCanaries[0].deref()).toBeUndefined();
447-
}
544+
expect(firstCanaries[0].deref()).toBeUndefined();
448545
expect(firstCanaries[1].deref()).toBeUndefined();
449546
}
450547
var list = Holder.held;

‎test-app/runtime/src/main/cpp/ObjectManager.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,8 +1344,8 @@ void ObjectManager::UnwrapForWeakCollection(const FunctionCallbackInfo<Value>& i
13441344
}
13451345

13461346
// wrap(key): the Java object a key stands for, as its live canary or a new one
1347-
// with its state, or undefined once Java collected it. Anything that is not a
1348-
// key passes through.
1347+
// (with its state, when it keeps one), or undefined once Java collected it.
1348+
// Anything that is not a key passes through.
13491349
void ObjectManager::WrapForWeakCollection(const FunctionCallbackInfo<Value>& info) {
13501350
try {
13511351
Isolate* isolate = info.GetIsolate();
@@ -1385,7 +1385,8 @@ void ObjectManager::WrapForWeakCollection(const FunctionCallbackInfo<Value>& inf
13851385
}
13861386
Local<Object> result = thisPtr->RebuildCanary(id);
13871387
if (result.IsEmpty() && thisPtr->FindCanaryRecord(id) != nullptr) {
1388-
// A plain object: a wrapper made from it the usual way is hydrated from its state.
1388+
// A plain object: a wrapper made from it the usual way links to its record,
1389+
// and gets its state back in scope.
13891390
JniLocalRef javaObject(thisPtr->JavaObjectOrNull(id));
13901391
if (!javaObject.IsNull()) {
13911392
result = thisPtr->GetOrCreateJSWrapper(javaObject);

0 commit comments

Comments
 (0)