ses 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NEWS.md +25 -1
- package/README.md +1 -1
- package/dist/lockdown.cjs +962 -453
- package/dist/lockdown.mjs +962 -453
- package/dist/lockdown.umd.js +962 -453
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +962 -453
- package/dist/ses.mjs +962 -453
- package/dist/ses.umd.js +962 -453
- package/dist/ses.umd.min.js +1 -1
- package/package.json +15 -11
- package/src/compartment.js +13 -4
- package/src/enable-property-overrides.js +40 -36
- package/src/enablements.js +15 -0
- package/src/error/README.md +1 -1
- package/src/error/fatal-assert.js +1 -1
- package/src/error/note-log-args.js +1 -205
- package/src/error/tame-console.js +72 -51
- package/src/lockdown.js +43 -12
- package/src/make-lru-cachemap.js +221 -0
- package/src/module-instance.js +10 -10
- package/src/module-proxy.js +42 -14
- package/src/permits-intrinsics.js +29 -15
- package/src/permits.js +67 -2
- package/src/tame-domains.js +4 -6
- package/src/tame-faux-data-properties.js +210 -0
- package/types.d.ts +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
+
// Using TypeError minimizes risk of exposing the feral Error constructor
|
|
4
5
|
TypeError,
|
|
5
6
|
apply,
|
|
6
7
|
defineProperty,
|
|
@@ -13,36 +14,13 @@ import { makeRejectionHandlers } from './unhandled-rejection.js';
|
|
|
13
14
|
import './types.js';
|
|
14
15
|
import './internal-types.js';
|
|
15
16
|
|
|
17
|
+
const failFast = message => {
|
|
18
|
+
throw TypeError(message);
|
|
19
|
+
};
|
|
20
|
+
|
|
16
21
|
const wrapLogger = (logger, thisArg) =>
|
|
17
22
|
freeze((...args) => apply(logger, thisArg, args));
|
|
18
23
|
|
|
19
|
-
// eslint-disable-next-line no-restricted-globals
|
|
20
|
-
const originalConsole = /** @type {VirtualConsole} */ (
|
|
21
|
-
// eslint-disable-next-line no-nested-ternary
|
|
22
|
-
typeof console !== 'undefined'
|
|
23
|
-
? console
|
|
24
|
-
: typeof print === 'function'
|
|
25
|
-
? // Make a good-enough console for eshost (including only functions that
|
|
26
|
-
// log at a specific level with no special argument interpretation).
|
|
27
|
-
// https://console.spec.whatwg.org/#logging
|
|
28
|
-
(p => freeze({ debug: p, log: p, info: p, warn: p, error: p }))(
|
|
29
|
-
// eslint-disable-next-line no-undef
|
|
30
|
-
wrapLogger(print),
|
|
31
|
-
)
|
|
32
|
-
: undefined
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
// Upgrade a log-only console (as in `eshost -h SpiderMonkey`).
|
|
36
|
-
if (originalConsole && originalConsole.log) {
|
|
37
|
-
for (const methodName of ['warn', 'error']) {
|
|
38
|
-
if (!originalConsole[methodName]) {
|
|
39
|
-
defineProperty(originalConsole, methodName, {
|
|
40
|
-
value: wrapLogger(originalConsole.log, originalConsole),
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
24
|
/**
|
|
47
25
|
* Wrap console unless suppressed.
|
|
48
26
|
* At the moment, the console is considered a host power in the start
|
|
@@ -60,9 +38,9 @@ export const tameConsole = (
|
|
|
60
38
|
unhandledRejectionTrapping = 'report',
|
|
61
39
|
optGetStackString = undefined,
|
|
62
40
|
) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
41
|
+
consoleTaming === 'safe' ||
|
|
42
|
+
consoleTaming === 'unsafe' ||
|
|
43
|
+
failFast(`unrecognized consoleTaming ${consoleTaming}`);
|
|
66
44
|
|
|
67
45
|
let loggedErrorHandler;
|
|
68
46
|
if (optGetStackString === undefined) {
|
|
@@ -73,6 +51,34 @@ export const tameConsole = (
|
|
|
73
51
|
getStackString: optGetStackString,
|
|
74
52
|
};
|
|
75
53
|
}
|
|
54
|
+
|
|
55
|
+
// eslint-disable-next-line no-restricted-globals
|
|
56
|
+
const originalConsole = /** @type {VirtualConsole} */ (
|
|
57
|
+
// eslint-disable-next-line no-nested-ternary
|
|
58
|
+
typeof globalThis.console !== 'undefined'
|
|
59
|
+
? globalThis.console
|
|
60
|
+
: typeof globalThis.print === 'function'
|
|
61
|
+
? // Make a good-enough console for eshost (including only functions that
|
|
62
|
+
// log at a specific level with no special argument interpretation).
|
|
63
|
+
// https://console.spec.whatwg.org/#logging
|
|
64
|
+
(p => freeze({ debug: p, log: p, info: p, warn: p, error: p }))(
|
|
65
|
+
// eslint-disable-next-line no-undef
|
|
66
|
+
wrapLogger(globalThis.print),
|
|
67
|
+
)
|
|
68
|
+
: undefined
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// Upgrade a log-only console (as in `eshost -h SpiderMonkey`).
|
|
72
|
+
if (originalConsole && originalConsole.log) {
|
|
73
|
+
for (const methodName of ['warn', 'error']) {
|
|
74
|
+
if (!originalConsole[methodName]) {
|
|
75
|
+
defineProperty(originalConsole, methodName, {
|
|
76
|
+
value: wrapLogger(originalConsole.log, originalConsole),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
76
82
|
const ourConsole = /** @type {VirtualConsole} */ (
|
|
77
83
|
consoleTaming === 'unsafe'
|
|
78
84
|
? originalConsole
|
|
@@ -95,21 +101,36 @@ export const tameConsole = (
|
|
|
95
101
|
/* eslint-disable @endo/no-polymorphic-call */
|
|
96
102
|
|
|
97
103
|
// Node.js
|
|
98
|
-
|
|
99
|
-
|
|
104
|
+
const globalProcess = globalThis.process || undefined;
|
|
105
|
+
if (
|
|
106
|
+
errorTrapping !== 'none' &&
|
|
107
|
+
typeof globalProcess === 'object' &&
|
|
108
|
+
typeof globalProcess.on === 'function'
|
|
109
|
+
) {
|
|
110
|
+
let terminate;
|
|
111
|
+
if (errorTrapping === 'platform' || errorTrapping === 'exit') {
|
|
112
|
+
const { exit } = globalProcess;
|
|
113
|
+
// If there is a function-valued process.on but no function-valued process.exit,
|
|
114
|
+
// fail early without caring whether errorTrapping is "platform" only by default.
|
|
115
|
+
typeof exit === 'function' || failFast('missing process.exit');
|
|
116
|
+
terminate = () => exit(globalProcess.exitCode || -1);
|
|
117
|
+
} else if (errorTrapping === 'abort') {
|
|
118
|
+
terminate = globalProcess.abort;
|
|
119
|
+
typeof terminate === 'function' || failFast('missing process.abort');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
globalProcess.on('uncaughtException', error => {
|
|
100
123
|
// causalConsole is born frozen so not vulnerable to method tampering.
|
|
101
124
|
ourConsole.error(error);
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
} else if (errorTrapping === 'abort') {
|
|
105
|
-
globalThis.process.abort();
|
|
125
|
+
if (terminate) {
|
|
126
|
+
terminate();
|
|
106
127
|
}
|
|
107
128
|
});
|
|
108
129
|
}
|
|
109
|
-
|
|
110
130
|
if (
|
|
111
131
|
unhandledRejectionTrapping !== 'none' &&
|
|
112
|
-
|
|
132
|
+
typeof globalProcess === 'object' &&
|
|
133
|
+
typeof globalProcess.on === 'function'
|
|
113
134
|
) {
|
|
114
135
|
const handleRejection = reason => {
|
|
115
136
|
// 'platform' and 'report' just log the reason.
|
|
@@ -119,32 +140,32 @@ export const tameConsole = (
|
|
|
119
140
|
const h = makeRejectionHandlers(handleRejection);
|
|
120
141
|
if (h) {
|
|
121
142
|
// Rejection handlers are supported.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
143
|
+
globalProcess.on('unhandledRejection', h.unhandledRejectionHandler);
|
|
144
|
+
globalProcess.on('rejectionHandled', h.rejectionHandledHandler);
|
|
145
|
+
globalProcess.on('exit', h.processTerminationHandler);
|
|
125
146
|
}
|
|
126
147
|
}
|
|
127
148
|
|
|
128
149
|
// Browser
|
|
150
|
+
const globalWindow = globalThis.window || undefined;
|
|
129
151
|
if (
|
|
130
152
|
errorTrapping !== 'none' &&
|
|
131
|
-
|
|
132
|
-
|
|
153
|
+
typeof globalWindow === 'object' &&
|
|
154
|
+
typeof globalWindow.addEventListener === 'function'
|
|
133
155
|
) {
|
|
134
|
-
|
|
156
|
+
globalWindow.addEventListener('error', event => {
|
|
135
157
|
event.preventDefault();
|
|
136
158
|
// 'platform' and 'report' just log the reason.
|
|
137
159
|
ourConsole.error(event.error);
|
|
138
160
|
if (errorTrapping === 'exit' || errorTrapping === 'abort') {
|
|
139
|
-
|
|
161
|
+
globalWindow.location.href = `about:blank`;
|
|
140
162
|
}
|
|
141
163
|
});
|
|
142
164
|
}
|
|
143
|
-
|
|
144
165
|
if (
|
|
145
166
|
unhandledRejectionTrapping !== 'none' &&
|
|
146
|
-
|
|
147
|
-
|
|
167
|
+
typeof globalWindow === 'object' &&
|
|
168
|
+
typeof globalWindow.addEventListener === 'function'
|
|
148
169
|
) {
|
|
149
170
|
const handleRejection = reason => {
|
|
150
171
|
ourConsole.error('SES_UNHANDLED_REJECTION:', reason);
|
|
@@ -153,17 +174,17 @@ export const tameConsole = (
|
|
|
153
174
|
const h = makeRejectionHandlers(handleRejection);
|
|
154
175
|
if (h) {
|
|
155
176
|
// Rejection handlers are supported.
|
|
156
|
-
|
|
177
|
+
globalWindow.addEventListener('unhandledrejection', event => {
|
|
157
178
|
event.preventDefault();
|
|
158
179
|
h.unhandledRejectionHandler(event.reason, event.promise);
|
|
159
180
|
});
|
|
160
181
|
|
|
161
|
-
|
|
182
|
+
globalWindow.addEventListener('rejectionhandled', event => {
|
|
162
183
|
event.preventDefault();
|
|
163
184
|
h.rejectionHandledHandler(event.promise);
|
|
164
185
|
});
|
|
165
186
|
|
|
166
|
-
|
|
187
|
+
globalWindow.addEventListener('beforeunload', _event => {
|
|
167
188
|
h.processTerminationHandler();
|
|
168
189
|
});
|
|
169
190
|
}
|
package/src/lockdown.js
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
// @ts-check
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import { getEnvironmentOption as getenv } from '@endo/env-options';
|
|
18
18
|
import {
|
|
19
19
|
FERAL_FUNCTION,
|
|
20
20
|
FERAL_EVAL,
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
stringSplit,
|
|
27
27
|
noEvalEvaluate,
|
|
28
28
|
getOwnPropertyNames,
|
|
29
|
+
getPrototypeOf,
|
|
29
30
|
} from './commons.js';
|
|
30
31
|
import { makeHardener } from './make-hardener.js';
|
|
31
32
|
import { makeIntrinsicsCollector } from './intrinsics.js';
|
|
@@ -53,6 +54,7 @@ import { getAnonymousIntrinsics } from './get-anonymous-intrinsics.js';
|
|
|
53
54
|
import { makeCompartmentConstructor } from './compartment.js';
|
|
54
55
|
import { tameHarden } from './tame-harden.js';
|
|
55
56
|
import { tameSymbolConstructor } from './tame-symbol-constructor.js';
|
|
57
|
+
import { tameFauxDataProperties } from './tame-faux-data-properties.js';
|
|
56
58
|
|
|
57
59
|
/** @typedef {import('../types.js').LockdownOptions} LockdownOptions */
|
|
58
60
|
|
|
@@ -155,8 +157,6 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
155
157
|
// [`stackFiltering` options](https://github.com/Agoric/SES-shim/blob/master/packages/ses/docs/lockdown.md#stackfiltering-options)
|
|
156
158
|
// for an explanation.
|
|
157
159
|
|
|
158
|
-
const { getEnvironmentOption: getenv } = makeEnvironmentCaptor(globalThis);
|
|
159
|
-
|
|
160
160
|
const {
|
|
161
161
|
errorTaming = getenv('LOCKDOWN_ERROR_TAMING', 'safe'),
|
|
162
162
|
errorTrapping = /** @type {"platform" | "none" | "report" | "abort" | "exit" | undefined} */ (
|
|
@@ -281,6 +281,14 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
281
281
|
|
|
282
282
|
const intrinsics = finalIntrinsics();
|
|
283
283
|
|
|
284
|
+
const hostIntrinsics = { __proto__: null };
|
|
285
|
+
|
|
286
|
+
// The Node.js Buffer is a derived class of Uint8Array, and as such is often
|
|
287
|
+
// passed around where a Uint8Array is expected.
|
|
288
|
+
if (typeof globalThis.Buffer === 'function') {
|
|
289
|
+
hostIntrinsics.Buffer = globalThis.Buffer;
|
|
290
|
+
}
|
|
291
|
+
|
|
284
292
|
/**
|
|
285
293
|
* Wrap console unless suppressed.
|
|
286
294
|
* At the moment, the console is considered a host power in the start
|
|
@@ -301,6 +309,19 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
301
309
|
);
|
|
302
310
|
globalThis.console = /** @type {Console} */ (consoleRecord.console);
|
|
303
311
|
|
|
312
|
+
// The untamed Node.js console cannot itself be hardened as it has mutable
|
|
313
|
+
// internal properties, but some of these properties expose internal versions
|
|
314
|
+
// of classes from node's "primordials" concept.
|
|
315
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
316
|
+
if (typeof (/** @type {any} */ (consoleRecord.console)._times) === 'object') {
|
|
317
|
+
// SafeMap is a derived Map class used internally by Node
|
|
318
|
+
// There doesn't seem to be a cleaner way to reach it.
|
|
319
|
+
hostIntrinsics.SafeMap = getPrototypeOf(
|
|
320
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
321
|
+
/** @type {any} */ (consoleRecord.console)._times,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
304
325
|
// @ts-ignore assert is absent on globalThis type def.
|
|
305
326
|
if (errorTaming === 'unsafe' && globalThis.assert === assert) {
|
|
306
327
|
// If errorTaming is 'unsafe' we replace the global assert with
|
|
@@ -315,6 +336,8 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
315
336
|
// Replace *Locale* methods with their non-locale equivalents
|
|
316
337
|
tameLocaleMethods(intrinsics, localeTaming);
|
|
317
338
|
|
|
339
|
+
tameFauxDataProperties(intrinsics);
|
|
340
|
+
|
|
318
341
|
/**
|
|
319
342
|
* 2. WHITELIST to standardize the environment.
|
|
320
343
|
*/
|
|
@@ -388,20 +411,28 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
388
411
|
|
|
389
412
|
// Finally register and optionally freeze all the intrinsics. This
|
|
390
413
|
// must be the operation that modifies the intrinsics.
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
414
|
+
const toHarden = {
|
|
415
|
+
intrinsics,
|
|
416
|
+
hostIntrinsics,
|
|
417
|
+
globals: {
|
|
418
|
+
// Harden evaluators
|
|
419
|
+
Function: globalThis.Function,
|
|
420
|
+
eval: globalThis.eval,
|
|
421
|
+
// @ts-ignore Compartment does exist on globalThis
|
|
422
|
+
Compartment: globalThis.Compartment,
|
|
423
|
+
|
|
424
|
+
// Harden Symbol
|
|
425
|
+
Symbol: globalThis.Symbol,
|
|
426
|
+
},
|
|
427
|
+
};
|
|
398
428
|
|
|
399
429
|
// Harden Symbol and properties for initialGlobalPropertyNames in the host realm
|
|
400
|
-
tamedHarden(globalThis.Symbol);
|
|
401
430
|
for (const prop of getOwnPropertyNames(initialGlobalPropertyNames)) {
|
|
402
|
-
|
|
431
|
+
toHarden.globals[prop] = globalThis[prop];
|
|
403
432
|
}
|
|
404
433
|
|
|
434
|
+
tamedHarden(toHarden);
|
|
435
|
+
|
|
405
436
|
return tamedHarden;
|
|
406
437
|
};
|
|
407
438
|
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/* eslint-disable @endo/no-polymorphic-call */
|
|
3
|
+
|
|
4
|
+
// eslint-disable-next-line no-restricted-globals
|
|
5
|
+
const { isSafeInteger } = Number;
|
|
6
|
+
// eslint-disable-next-line no-restricted-globals
|
|
7
|
+
const { freeze } = Object;
|
|
8
|
+
// eslint-disable-next-line no-restricted-globals
|
|
9
|
+
const { toStringTag: toStringTagSymbol } = Symbol;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @template Data
|
|
13
|
+
* @typedef {object} DoublyLinkedCell
|
|
14
|
+
* A cell of a doubly-linked ring, i.e., a doubly-linked circular list.
|
|
15
|
+
* DoublyLinkedCells are not frozen, and so should be closely encapsulated by
|
|
16
|
+
* any abstraction that uses them.
|
|
17
|
+
* @property {DoublyLinkedCell<Data>} next
|
|
18
|
+
* @property {DoublyLinkedCell<Data>} prev
|
|
19
|
+
* @property {Data} data
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Makes a new self-linked cell. There are two reasons to do so:
|
|
24
|
+
* * To make the head sigil of a new initially-empty doubly-linked ring.
|
|
25
|
+
* * To make a non-sigil cell to be `spliceAfter`ed.
|
|
26
|
+
*
|
|
27
|
+
* @template Data
|
|
28
|
+
* @param {Data} data
|
|
29
|
+
* @returns {DoublyLinkedCell<Data>}
|
|
30
|
+
*/
|
|
31
|
+
const makeSelfCell = data => {
|
|
32
|
+
/** @type {Partial<DoublyLinkedCell<Data>>} */
|
|
33
|
+
const incompleteCell = {
|
|
34
|
+
next: undefined,
|
|
35
|
+
prev: undefined,
|
|
36
|
+
data,
|
|
37
|
+
};
|
|
38
|
+
const selfCell = /** @type {DoublyLinkedCell<Data>} */ (incompleteCell);
|
|
39
|
+
selfCell.next = selfCell;
|
|
40
|
+
selfCell.prev = selfCell;
|
|
41
|
+
// Not frozen!
|
|
42
|
+
return selfCell;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Splices a self-linked non-sigil cell into a ring after `prev`.
|
|
47
|
+
* `prev` could be the head sigil, or it could be some other non-sigil
|
|
48
|
+
* cell within a ring.
|
|
49
|
+
*
|
|
50
|
+
* @template Data
|
|
51
|
+
* @param {DoublyLinkedCell<Data>} prev
|
|
52
|
+
* @param {DoublyLinkedCell<Data>} selfCell
|
|
53
|
+
*/
|
|
54
|
+
const spliceAfter = (prev, selfCell) => {
|
|
55
|
+
if (prev === selfCell) {
|
|
56
|
+
// eslint-disable-next-line no-restricted-globals
|
|
57
|
+
throw TypeError('Cannot splice a cell into itself');
|
|
58
|
+
}
|
|
59
|
+
if (selfCell.next !== selfCell || selfCell.prev !== selfCell) {
|
|
60
|
+
// eslint-disable-next-line no-restricted-globals
|
|
61
|
+
throw TypeError('Expected self-linked cell');
|
|
62
|
+
}
|
|
63
|
+
const cell = selfCell;
|
|
64
|
+
// rename variable cause it isn't self-linked after this point.
|
|
65
|
+
|
|
66
|
+
const next = prev.next;
|
|
67
|
+
cell.prev = prev;
|
|
68
|
+
cell.next = next;
|
|
69
|
+
prev.next = cell;
|
|
70
|
+
next.prev = cell;
|
|
71
|
+
// Not frozen!
|
|
72
|
+
return cell;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @template Data
|
|
77
|
+
* @param {DoublyLinkedCell<Data>} cell
|
|
78
|
+
* No-op if the cell is self-linked.
|
|
79
|
+
*/
|
|
80
|
+
const spliceOut = cell => {
|
|
81
|
+
const { prev, next } = cell;
|
|
82
|
+
prev.next = next;
|
|
83
|
+
next.prev = prev;
|
|
84
|
+
cell.prev = cell;
|
|
85
|
+
cell.next = cell;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The LRUCacheMap is used within the implementation of `assert` and so
|
|
90
|
+
* at a layer below SES or harden. Thus, we give it a `WeakMap`-like interface
|
|
91
|
+
* rather than a `WeakMapStore`-like interface. To work before `lockdown`,
|
|
92
|
+
* the implementation must use `freeze` manually, but still exhaustively.
|
|
93
|
+
*
|
|
94
|
+
* It implements the WeakMap interface, and holds its keys weakly. Cached
|
|
95
|
+
* values are only held while the key is held by the user and the key/value
|
|
96
|
+
* bookkeeping cell has not been pushed off the end of the cache by `budget`
|
|
97
|
+
* number of more recently referenced cells. If the key is dropped by the user,
|
|
98
|
+
* the value will no longer be held by the cache, but the bookkeeping cell
|
|
99
|
+
* itself will stay in memory.
|
|
100
|
+
*
|
|
101
|
+
* @template {{}} K
|
|
102
|
+
* @template {unknown} V
|
|
103
|
+
* @param {number} keysBudget
|
|
104
|
+
* @returns {WeakMap<K,V>}
|
|
105
|
+
*/
|
|
106
|
+
export const makeLRUCacheMap = keysBudget => {
|
|
107
|
+
if (!isSafeInteger(keysBudget) || keysBudget < 0) {
|
|
108
|
+
// eslint-disable-next-line no-restricted-globals
|
|
109
|
+
throw TypeError('keysBudget must be a safe non-negative integer number');
|
|
110
|
+
}
|
|
111
|
+
/** @typedef {DoublyLinkedCell<WeakMap<K, V> | undefined>} LRUCacheCell */
|
|
112
|
+
/** @type {WeakMap<K, LRUCacheCell>} */
|
|
113
|
+
// eslint-disable-next-line no-restricted-globals
|
|
114
|
+
const keyToCell = new WeakMap();
|
|
115
|
+
let size = 0; // `size` must remain <= `keysBudget`
|
|
116
|
+
// As a sigil, `head` uniquely is not in the `keyToCell` map.
|
|
117
|
+
/** @type {LRUCacheCell} */
|
|
118
|
+
const head = makeSelfCell(undefined);
|
|
119
|
+
|
|
120
|
+
const touchCell = key => {
|
|
121
|
+
const cell = keyToCell.get(key);
|
|
122
|
+
if (cell === undefined || cell.data === undefined) {
|
|
123
|
+
// Either the key was GCed, or the cell was condemned.
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
// Becomes most recently used
|
|
127
|
+
spliceOut(cell);
|
|
128
|
+
spliceAfter(head, cell);
|
|
129
|
+
return cell;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @param {K} key
|
|
134
|
+
*/
|
|
135
|
+
const has = key => touchCell(key) !== undefined;
|
|
136
|
+
freeze(has);
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @param {K} key
|
|
140
|
+
*/
|
|
141
|
+
// UNTIL https://github.com/endojs/endo/issues/1514
|
|
142
|
+
// Prefer: const get = key => touchCell(key)?.data?.get(key);
|
|
143
|
+
const get = key => {
|
|
144
|
+
const cell = touchCell(key);
|
|
145
|
+
return cell && cell.data && cell.data.get(key);
|
|
146
|
+
};
|
|
147
|
+
freeze(get);
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @param {K} key
|
|
151
|
+
* @param {V} value
|
|
152
|
+
*/
|
|
153
|
+
const set = (key, value) => {
|
|
154
|
+
if (keysBudget < 1) {
|
|
155
|
+
// eslint-disable-next-line no-use-before-define
|
|
156
|
+
return lruCacheMap; // Implements WeakMap.set
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let cell = touchCell(key);
|
|
160
|
+
if (cell === undefined) {
|
|
161
|
+
cell = makeSelfCell(undefined);
|
|
162
|
+
spliceAfter(head, cell); // start most recently used
|
|
163
|
+
}
|
|
164
|
+
if (!cell.data) {
|
|
165
|
+
// Either a fresh cell or a reused condemned cell.
|
|
166
|
+
size += 1;
|
|
167
|
+
// Add its data.
|
|
168
|
+
// eslint-disable-next-line no-restricted-globals
|
|
169
|
+
cell.data = new WeakMap();
|
|
170
|
+
// Advertise the cell for this key.
|
|
171
|
+
keyToCell.set(key, cell);
|
|
172
|
+
while (size > keysBudget) {
|
|
173
|
+
const condemned = head.prev;
|
|
174
|
+
spliceOut(condemned); // Drop least recently used
|
|
175
|
+
condemned.data = undefined;
|
|
176
|
+
size -= 1;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Update the data.
|
|
181
|
+
cell.data.set(key, value);
|
|
182
|
+
|
|
183
|
+
// eslint-disable-next-line no-use-before-define
|
|
184
|
+
return lruCacheMap; // Implements WeakMap.set
|
|
185
|
+
};
|
|
186
|
+
freeze(set);
|
|
187
|
+
|
|
188
|
+
// "delete" is a keyword.
|
|
189
|
+
/**
|
|
190
|
+
* @param {K} key
|
|
191
|
+
*/
|
|
192
|
+
const deleteIt = key => {
|
|
193
|
+
const cell = keyToCell.get(key);
|
|
194
|
+
if (cell === undefined) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
spliceOut(cell);
|
|
198
|
+
keyToCell.delete(key);
|
|
199
|
+
if (cell.data === undefined) {
|
|
200
|
+
// Already condemned.
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
cell.data = undefined;
|
|
205
|
+
size -= 1;
|
|
206
|
+
return true;
|
|
207
|
+
};
|
|
208
|
+
freeze(deleteIt);
|
|
209
|
+
|
|
210
|
+
const lruCacheMap = freeze({
|
|
211
|
+
has,
|
|
212
|
+
get,
|
|
213
|
+
set,
|
|
214
|
+
delete: deleteIt,
|
|
215
|
+
// eslint-disable-next-line jsdoc/check-types
|
|
216
|
+
[/** @type {typeof Symbol.toStringTag} */ (toStringTagSymbol)]:
|
|
217
|
+
'LRUCacheMap',
|
|
218
|
+
});
|
|
219
|
+
return lruCacheMap;
|
|
220
|
+
};
|
|
221
|
+
freeze(makeLRUCacheMap);
|
package/src/module-instance.js
CHANGED
|
@@ -32,7 +32,7 @@ export const makeThirdPartyModuleInstance = (
|
|
|
32
32
|
moduleSpecifier,
|
|
33
33
|
resolvedImports,
|
|
34
34
|
) => {
|
|
35
|
-
const { exportsProxy,
|
|
35
|
+
const { exportsProxy, exportsTarget, activate } = getDeferredExports(
|
|
36
36
|
compartment,
|
|
37
37
|
weakmapGet(compartmentPrivateFields, compartment),
|
|
38
38
|
moduleAliases,
|
|
@@ -51,7 +51,7 @@ export const makeThirdPartyModuleInstance = (
|
|
|
51
51
|
);
|
|
52
52
|
}
|
|
53
53
|
arrayForEach(staticModuleRecord.exports, name => {
|
|
54
|
-
let value =
|
|
54
|
+
let value = exportsTarget[name];
|
|
55
55
|
const updaters = [];
|
|
56
56
|
|
|
57
57
|
const get = () => value;
|
|
@@ -63,7 +63,7 @@ export const makeThirdPartyModuleInstance = (
|
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
defineProperty(
|
|
66
|
+
defineProperty(exportsTarget, name, {
|
|
67
67
|
get,
|
|
68
68
|
set,
|
|
69
69
|
enumerable: true,
|
|
@@ -75,9 +75,9 @@ export const makeThirdPartyModuleInstance = (
|
|
|
75
75
|
update(value);
|
|
76
76
|
};
|
|
77
77
|
});
|
|
78
|
-
// This is enough to support import * from cjs - the '*' field doesn't need to be in exports nor
|
|
78
|
+
// This is enough to support import * from cjs - the '*' field doesn't need to be in exports nor exportsTarget because import will only ever access it via notifiers
|
|
79
79
|
notifiers['*'] = update => {
|
|
80
|
-
update(
|
|
80
|
+
update(exportsTarget);
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
83
|
|
|
@@ -97,7 +97,7 @@ export const makeThirdPartyModuleInstance = (
|
|
|
97
97
|
try {
|
|
98
98
|
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
99
99
|
staticModuleRecord.execute(
|
|
100
|
-
|
|
100
|
+
exportsTarget,
|
|
101
101
|
compartment,
|
|
102
102
|
resolvedImports,
|
|
103
103
|
);
|
|
@@ -143,7 +143,7 @@ export const makeModuleInstance = (
|
|
|
143
143
|
|
|
144
144
|
const { __shimTransforms__, importMetaHook } = compartmentFields;
|
|
145
145
|
|
|
146
|
-
const { exportsProxy,
|
|
146
|
+
const { exportsProxy, exportsTarget, activate } = getDeferredExports(
|
|
147
147
|
compartment,
|
|
148
148
|
compartmentFields,
|
|
149
149
|
moduleAliases,
|
|
@@ -342,7 +342,7 @@ export const makeModuleInstance = (
|
|
|
342
342
|
);
|
|
343
343
|
|
|
344
344
|
const notifyStar = update => {
|
|
345
|
-
update(
|
|
345
|
+
update(exportsTarget);
|
|
346
346
|
};
|
|
347
347
|
notifiers['*'] = notifyStar;
|
|
348
348
|
|
|
@@ -435,10 +435,10 @@ export const makeModuleInstance = (
|
|
|
435
435
|
// and the string must correspond to a valid identifier, sorting these
|
|
436
436
|
// properties works for this specific case.
|
|
437
437
|
arrayForEach(arraySort(keys(exportsProps)), k =>
|
|
438
|
-
defineProperty(
|
|
438
|
+
defineProperty(exportsTarget, k, exportsProps[k]),
|
|
439
439
|
);
|
|
440
440
|
|
|
441
|
-
freeze(
|
|
441
|
+
freeze(exportsTarget);
|
|
442
442
|
activate();
|
|
443
443
|
}
|
|
444
444
|
|