|
| 1 | +/* global globalThis */ |
| 2 | + |
| 3 | +import { assert, details as d } from '@agoric/assert'; |
| 4 | + |
| 5 | +const { defineProperties } = Object; |
| 6 | + |
| 7 | +/* |
| 8 | + * We retain a measure of compatibility with Node.js v12, which does not |
| 9 | + * expose WeakRef or FinalizationRegistry (there is a --flag for it, but it's |
| 10 | + * * not clear how stable it is). When running on a platform without these * |
| 11 | + * tools, vats cannot do GC, and the tools they get will be no-ops. WeakRef |
| 12 | + * instances will hold a strong reference, and the FinalizationRegistry will |
| 13 | + * never invoke the callbacks. |
| 14 | + * |
| 15 | + * Modules should do: |
| 16 | + * |
| 17 | + * import { WeakRef, FinalizationRegistry } from '.../weakref'; |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +// TODO We need to migrate this into a ses-level tame-weakref.js, to happen |
| 22 | +// as part of `lockdown`. In anticipation, this uses some of the patterns |
| 23 | +// followed by the other tamings there. |
| 24 | + |
| 25 | +// Emulate the internal [[WeakRefTarget]] slot. Despite the term "Weak" in the |
| 26 | +// "WeakMap" used in the emulation, this map holds the target strongly. The only |
| 27 | +// weakness here is that the weakref,target pair can go away together if the |
| 28 | +// weakref is not reachable. |
| 29 | +const weakRefTarget = new WeakMap(); |
| 30 | + |
| 31 | +const FakeWeakRef = function WeakRef(target) { |
| 32 | + assert( |
| 33 | + new.target !== undefined, |
| 34 | + d`WeakRef Constructor requires 'new'`, |
| 35 | + TypeError, |
| 36 | + ); |
| 37 | + assert.equal( |
| 38 | + Object(target), |
| 39 | + target, |
| 40 | + d`WeakRef target must be an object`, |
| 41 | + TypeError, |
| 42 | + ); |
| 43 | + weakRefTarget.set(this, target); |
| 44 | +}; |
| 45 | + |
| 46 | +const InertWeakRef = function WeakRef(_target) { |
| 47 | + throw new TypeError('Not available'); |
| 48 | +}; |
| 49 | + |
| 50 | +const FakeWeakRefPrototype = { |
| 51 | + deref() { |
| 52 | + return weakRefTarget.get(this); |
| 53 | + }, |
| 54 | + [Symbol.toStringTag]: 'WeakRef', |
| 55 | +}; |
| 56 | + |
| 57 | +defineProperties(FakeWeakRef, { |
| 58 | + prototype: { value: FakeWeakRefPrototype }, |
| 59 | +}); |
| 60 | + |
| 61 | +const WeakRef = globalThis.WeakRef || FakeWeakRef; |
| 62 | + |
| 63 | +// If there is a real WeakRef constructor, we still make it safe before |
| 64 | +// exporting it. Unlike https://github.com/tc39/ecma262/issues/2214 |
| 65 | +// rather than deleting the `constructor` property, we follow the other |
| 66 | +// taming patterns and point it at a throw-only inert one. |
| 67 | +defineProperties(WeakRef.prototype, { |
| 68 | + constructor: { value: InertWeakRef }, |
| 69 | +}); |
| 70 | + |
| 71 | +harden(WeakRef); |
| 72 | + |
| 73 | +export { WeakRef }; |
| 74 | + |
| 75 | +// ///////////////////////////////////////////////////////////////////////////// |
| 76 | + |
| 77 | +const FakeFinalizationRegistry = function FinalizationRegistry( |
| 78 | + cleanupCallback, |
| 79 | +) { |
| 80 | + assert( |
| 81 | + new.target !== undefined, |
| 82 | + d`FinalizationRegistry Constructor requires 'new'`, |
| 83 | + TypeError, |
| 84 | + ); |
| 85 | + assert.typeof( |
| 86 | + cleanupCallback, |
| 87 | + 'function', |
| 88 | + d`cleanupCallback must be a function`, |
| 89 | + ); |
| 90 | + // fall off the end with an empty instance |
| 91 | +}; |
| 92 | + |
| 93 | +const InertFinalizationRegistry = function FinalizationRegistry( |
| 94 | + _cleanupCallback, |
| 95 | +) { |
| 96 | + throw new TypeError('Not available'); |
| 97 | +}; |
| 98 | + |
| 99 | +const FakeFinalizationRegistryPrototype = { |
| 100 | + register() {}, |
| 101 | + unregister() {}, |
| 102 | + [Symbol.toStringTag]: 'FinalizationRegistry', |
| 103 | +}; |
| 104 | + |
| 105 | +defineProperties(FakeFinalizationRegistry, { |
| 106 | + prototype: { value: FakeFinalizationRegistryPrototype }, |
| 107 | +}); |
| 108 | + |
| 109 | +const FinalizationRegistry = |
| 110 | + globalThis.FinalizationRegistry || FakeFinalizationRegistry; |
| 111 | + |
| 112 | +// If there is a real FinalizationRegistry constructor, we still make it safe |
| 113 | +// before exporting it. Rather than deleting the `constructor` property, we |
| 114 | +// follow the other taming patterns and point it at a throw-only inert one. |
| 115 | +defineProperties(FinalizationRegistry.prototype, { |
| 116 | + constructor: { value: InertFinalizationRegistry }, |
| 117 | +}); |
| 118 | + |
| 119 | +harden(FinalizationRegistry); |
| 120 | + |
| 121 | +export { FinalizationRegistry }; |
0 commit comments