Skip to content

Commit

Permalink
chore: make GC tools optional to support Node.js v12
Browse files Browse the repository at this point in the history
Node.js v14 provides `WeakRef` and `FinalizationRegistry` as globals. Node.js
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 change attempts to pull `WeakRef` and
`FinalizationRegistry` from the global, and deliver either the real
constructors or `undefined` to the liveslots code that uses it. We'll write
that liveslots code to tolerate their lack.

refs #1872
refs #1925
  • Loading branch information
warner committed Oct 26, 2020
1 parent 5afcfeb commit f926f6a
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Compartment WeakRef FinalizationRegistry */
/* global Compartment */

import fs from 'fs';
import path from 'path';
Expand All @@ -18,6 +18,7 @@ import { makeMeteringTransformer } from '@agoric/transform-metering';
import { makeTransform } from '@agoric/transform-eventual-send';
import { locateWorkerBin } from '@agoric/xs-vat-worker';

import { WeakRef, FinalizationRegistry } from './optional-weakref';
import { startSubprocessWorker } from './spawnSubprocessWorker';
import { waitUntilQuiescent } from './waitUntilQuiescent';
import { insistStorageAPI } from './storageAPI';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// this file is loaded at the start of a new Worker, which makes it a new JS
// environment (with it's own Realm), so we must install-ses too.
/* global WeakRef FinalizationRegistry */
import '@agoric/install-ses';
import { parentPort } from 'worker_threads';
import anylogger from 'anylogger';

import { assert } from '@agoric/assert';
import { importBundle } from '@agoric/import-bundle';
import { Remotable, getInterfaceOf, makeMarshal } from '@agoric/marshal';
import { WeakRef, FinalizationRegistry } from '../../optional-weakref';
import { waitUntilQuiescent } from '../../waitUntilQuiescent';
import { makeLiveSlots } from '../liveSlots';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// this file is loaded at the start of a new subprocess
/* global WeakRef FinalizationRegistry */
import '@agoric/install-ses';

import anylogger from 'anylogger';
Expand All @@ -8,6 +7,7 @@ import fs from 'fs';
import { assert } from '@agoric/assert';
import { importBundle } from '@agoric/import-bundle';
import { Remotable, getInterfaceOf, makeMarshal } from '@agoric/marshal';
import { WeakRef, FinalizationRegistry } from '../../optional-weakref';
import { arrayEncoderStream, arrayDecoderStream } from '../../worker-protocol';
import {
netstringEncoderStream,
Expand Down
17 changes: 17 additions & 0 deletions packages/SwingSet/src/optional-weakref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* 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.
*
* Modules should do:
*
* import { WeakRef, FinalizationRegistry } from '.../optional-weakref';
*
* and refrain from GC if `WeakRef === undefined`
*/

export const WeakRef = globalThis.WeakRef;
export const FinalizationRegistry = globalThis.FinalizationRegistry;
2 changes: 1 addition & 1 deletion packages/SwingSet/test/test-kernel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* global WeakRef FinalizationRegistry */
import '@agoric/install-ses';
import test from 'ava';
import anylogger from 'anylogger';
import { initSwingStore } from '@agoric/swing-store-simple';
import { WeakRef, FinalizationRegistry } from '../src/optional-weakref';
import { waitUntilQuiescent } from '../src/waitUntilQuiescent';

import buildKernel from '../src/kernel/index';
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/test-liveslots.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global WeakRef FinalizationRegistry */
import '@agoric/install-ses';
import test from 'ava';
import { E } from '@agoric/eventual-send';
import { WeakRef, FinalizationRegistry } from '../src/optional-weakref';
import { waitUntilQuiescent } from '../src/waitUntilQuiescent';
import { makeLiveSlots } from '../src/kernel/liveSlots';

Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/test-marshal.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* global WeakRef FinalizationRegistry */
import '@agoric/install-ses';
import test from 'ava';
import { makePromiseKit } from '@agoric/promise-kit';

import { WeakRef, FinalizationRegistry } from '../src/optional-weakref';
import { makeMarshaller } from '../src/kernel/liveSlots';

import { buildVatController } from '../src/index';
Expand Down
4 changes: 1 addition & 3 deletions packages/SwingSet/test/test-vpid-kernel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* global WeakRef FinalizationRegistry */
// eslint-disable-next-line no-redeclare

import '@agoric/install-ses';
import test from 'ava';
import anylogger from 'anylogger';
import { initSwingStore } from '@agoric/swing-store-simple';
import { WeakRef, FinalizationRegistry } from '../src/optional-weakref';
import { waitUntilQuiescent } from '../src/waitUntilQuiescent';

import buildKernel from '../src/kernel/index';
Expand Down
3 changes: 2 additions & 1 deletion packages/SwingSet/test/test-vpid-liveslots.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// eslint-disable-next-line no-redeclare
/* global setImmediate WeakRef FinalizationRegistry */
/* global setImmediate */

import '@agoric/install-ses';
import test from 'ava';

import { E } from '@agoric/eventual-send';
import { makePromiseKit } from '@agoric/promise-kit';
import { WeakRef, FinalizationRegistry } from '../src/optional-weakref';
import { makeLiveSlots } from '../src/kernel/liveSlots';

const RETIRE_VPIDS = true;
Expand Down

0 comments on commit f926f6a

Please sign in to comment.