-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add stubs for GC tools (no-op on Node v12)
The upcoming GC functionality will require `WeakRef` and `FinalizationRegistry`. Node.js v14 provides these as globals, but v12 does not (there might be a command-line flag to enable it, but I think it's marked as experimental). Rather than require all users upgrade to v14, we elect to disable GC when running on v12. This adds a local `weakref.js` module which attempts to pull `WeakRef` and `FinalizationRegistry` from the global, and exports either the real constructors or no-op stubs. refs #1872 refs #1925
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* global globalThis */ | ||
|
||
/* | ||
* We retain a measure of compatibility with Node.js v12, which does not | ||
* expose WeakRef or FinalizationRegistry (there is a --flag for it, but it's | ||
* * not clear how stable it is). When running on a platform without these * | ||
* tools, vats cannot do GC, and the tools they get will be no-ops. WeakRef | ||
* instances will hold a strong reference, and the FinalizationRegistry will | ||
* never invoke the callbacks. | ||
* | ||
* Modules should do: | ||
* | ||
* import { WeakRef, FinalizationRegistry } from '.../weakref'; | ||
* | ||
*/ | ||
|
||
function FakeWeakRef(obj) { | ||
const wr = Object.create({ | ||
deref: () => obj, | ||
}); | ||
delete wr.constructor; | ||
return wr; | ||
} | ||
|
||
function FakeFinalizationRegistry(_callback) { | ||
const fr = Object.create({ | ||
register: (_obj, _handle) => undefined, | ||
unregister: _handle => undefined, | ||
}); | ||
delete fr.constructor; | ||
return fr; | ||
} | ||
|
||
const WR = globalThis.WeakRef || FakeWeakRef; | ||
const FR = globalThis.FinalizationRegistry || FakeFinalizationRegistry; | ||
|
||
export const WeakRef = WR; | ||
export const FinalizationRegistry = FR; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import '@agoric/install-ses'; | ||
import test from 'ava'; | ||
import { WeakRef, FinalizationRegistry } from '../src/weakref'; | ||
|
||
// We don't test that WeakRefs actually work, we only make sure we can | ||
// interact with them without crashing. This exercises the fake no-op WeakRef | ||
// and FinalizationRegistry that our `src/weakref.js` creates on Node.js v12. | ||
// On v14 we get real constructors. | ||
|
||
test('weakref is callable', async t => { | ||
const obj = {}; | ||
const wr = new WeakRef(obj); | ||
t.is(obj, wr.deref()); | ||
|
||
const callback = () => 0; | ||
const fr = new FinalizationRegistry(callback); | ||
fr.register(obj); | ||
|
||
const obj2 = {}; | ||
const handle = {}; | ||
fr.register(obj2, handle); | ||
fr.unregister(handle); | ||
}); |