The C++ implementation can now be done entirely in JS using WeakRef. Re-implement it in JS instead to simplify the code. PR-URL: https://github.com/nodejs/node/pull/49053 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
30 lines
569 B
JavaScript
30 lines
569 B
JavaScript
'use strict';
|
|
|
|
const { WeakReference } = require('internal/util');
|
|
const {
|
|
setDeserializeMainFunction
|
|
} = require('v8').startupSnapshot
|
|
|
|
let obj = { hello: 'world' };
|
|
const ref = new WeakReference(obj);
|
|
let gcCount = 0;
|
|
let maxGC = 10;
|
|
|
|
function run() {
|
|
globalThis.gc();
|
|
setImmediate(() => {
|
|
gcCount++;
|
|
if (ref.get() === undefined) {
|
|
return;
|
|
} else if (gcCount < maxGC) {
|
|
run();
|
|
} else {
|
|
throw new Error(`Reference is still around after ${maxGC} GC`);
|
|
}
|
|
});
|
|
}
|
|
|
|
setDeserializeMainFunction(() => {
|
|
obj = null;
|
|
run();
|
|
});
|