Skip to content

Commit 3ec5dd8

Browse files
gsathyachromium-wpt-export-bot
authored andcommitted
[WeakRefs] Implement WeakRefs integration
This uses the V8 API to register a clean up task that will execute some time later at idle time. The JavaScript spec is defined here: https://tc39.es/proposal-weakrefs/ The HTML integration is defined here: whatwg/html#4571 (Note that this CL doesn't implement ClearKeptObjects part of the spec yet, a follow on CL will do that.) TODO (before sumbitting this CL): - Add tests Bug: 1016767 Change-Id: I2db82dc9d037d1e3bc0ec8c192d5b06908161adc
1 parent 71aff5b commit 3ec5dd8

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

lint.whitelist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ CONSOLE: resources/check-layout-th.js
108108
CONSOLE: resources/chromium/*
109109
CONSOLE: resources/idlharness.js
110110
CONSOLE: streams/resources/test-utils.js
111+
CONSOLE: weakrefs/resources/test-utils.js
111112
CONSOLE: service-workers/service-worker/resources/navigation-redirect-other-origin.html
112113
CONSOLE: service-workers/service-worker/navigation-redirect.https.html
113114
CONSOLE: service-workers/service-worker/resources/clients-get-other-origin.html

weakrefs/META.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spec: https://tc39.es/proposal-weakrefs/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<script type="module">
5+
import garbageCollect from './resources/test-utils.js';
6+
7+
async_test(function() {
8+
let called = false;
9+
10+
const callback = this.step_func(function(iter) {
11+
const values = [...iter];
12+
assert_equals(values[0], 'holdings',
13+
'holdings should be initialized correctly');
14+
this.done();
15+
});
16+
17+
const fg = new FinalizationGroup(callback);
18+
19+
(function() {
20+
let x = {};
21+
fg.register(x, 'holdings');
22+
x = null;
23+
})();
24+
25+
assert_false(called, 'finalizer should not be called in the same turn');
26+
27+
garbageCollect();
28+
29+
assert_false(called, 'finalizer should not be called in the same turn');
30+
31+
}, `FinalizationGroup registers an object and calls finalizer`);
32+
</script>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<script type="module">
5+
import garbageCollect from './resources/test-utils.js';
6+
7+
test(function() {
8+
let called = false;
9+
function callback(iter) {
10+
called = true;
11+
const values = [...iter];
12+
assert_equals(values[0], 'holdings',
13+
'holdings should be initialized correctly');
14+
};
15+
16+
const fg = new FinalizationGroup(callback);
17+
18+
(function() {
19+
let x = {};
20+
fg.register(x, 'holdings');
21+
x = null;
22+
})();
23+
24+
assert_false(called, 'finalizer should not be called yet');
25+
26+
garbageCollect();
27+
28+
fg.cleanupSome();
29+
assert_true(called, 'finalizer should be called');
30+
31+
}, `FinalizationGroup.cleanupSome calls finalizer in same turn`);
32+
</script>

weakrefs/resources/test-utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default function() {
2+
if (self.gc) {
3+
// Use --expose_gc for V8 (and Node.js)
4+
// to pass this flag at chrome launch use: --js-flags="--expose-gc"
5+
// Exposed in SpiderMonkey shell as well
6+
self.gc();
7+
} else if (self.GCController) {
8+
// Present in some WebKit development environments
9+
GCController.collect();
10+
} else {
11+
/* eslint-disable no-console */
12+
console.warn('Tests are running without the ability to do manual garbage collection. They will still work, but ' +
13+
'coverage will be suboptimal.');
14+
/* eslint-enable no-console */
15+
}
16+
};

0 commit comments

Comments
 (0)