ses 1.13.1 → 1.14.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/README.md +68 -50
- package/dist/lockdown.cjs +946 -695
- package/dist/lockdown.mjs +946 -695
- package/dist/lockdown.umd.js +946 -695
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses-hermes.cjs +929 -552
- package/dist/ses.cjs +946 -695
- package/dist/ses.mjs +946 -695
- package/dist/ses.umd.js +946 -695
- package/dist/ses.umd.min.js +1 -1
- package/dist/types.d.cts +5 -0
- package/package.json +9 -85
- package/src/cauterize-property.js +2 -2
- package/src/commons.js +17 -16
- package/src/compartment.js +25 -8
- package/src/enable-property-overrides.js +4 -4
- package/src/error/assert.js +2 -2
- package/src/error/note-log-args.js +5 -4
- package/src/global-object.js +3 -3
- package/src/intrinsics.js +8 -8
- package/src/make-hardener.js +4 -4
- package/src/module-link.js +4 -4
- package/src/module-load.js +48 -13
- package/src/permits-intrinsics.js +8 -8
- package/src/scope-constants.js +2 -2
- package/src/tame-locale-methods.js +2 -2
- package/src/tame-regenerator-runtime.js +2 -2
- package/types.d.ts +5 -0
- package/src/make-lru-cachemap.js +0 -221
package/src/module-load.js
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
generatorThrow,
|
|
14
14
|
getOwnPropertyNames,
|
|
15
15
|
isArray,
|
|
16
|
-
|
|
16
|
+
isPrimitive,
|
|
17
17
|
mapGet,
|
|
18
18
|
mapHas,
|
|
19
19
|
mapSet,
|
|
@@ -186,7 +186,7 @@ function* loadWithoutErrorAnnotation(
|
|
|
186
186
|
)} in parent compartment, use {source} module descriptor`,
|
|
187
187
|
TypeError,
|
|
188
188
|
);
|
|
189
|
-
} else if (
|
|
189
|
+
} else if (!isPrimitive(moduleDescriptor)) {
|
|
190
190
|
// In this shim (and not in XS, and not in the standard we imagine), we
|
|
191
191
|
// allow a module namespace object to stand in for a module descriptor that
|
|
192
192
|
// describes its original {compartment, specifier} so that it can be used
|
|
@@ -209,7 +209,7 @@ function* loadWithoutErrorAnnotation(
|
|
|
209
209
|
namespace: aliasSpecifier,
|
|
210
210
|
} = moduleDescriptor;
|
|
211
211
|
if (
|
|
212
|
-
|
|
212
|
+
isPrimitive(aliasCompartment) ||
|
|
213
213
|
!weakmapHas(compartmentPrivateFields, aliasCompartment)
|
|
214
214
|
) {
|
|
215
215
|
throw makeError(
|
|
@@ -233,7 +233,7 @@ function* loadWithoutErrorAnnotation(
|
|
|
233
233
|
|
|
234
234
|
// All remaining objects must either be a module namespace, or be
|
|
235
235
|
// promoted into a module namespace with a virtual module source.
|
|
236
|
-
if (
|
|
236
|
+
if (!isPrimitive(moduleDescriptor.namespace)) {
|
|
237
237
|
const { namespace } = moduleDescriptor;
|
|
238
238
|
// Brand-check SES shim module exports namespaces:
|
|
239
239
|
aliasDescriptor = weakmapGet(moduleAliases, namespace);
|
|
@@ -406,7 +406,7 @@ function* loadWithoutErrorAnnotation(
|
|
|
406
406
|
moduleDescriptor.specifier !== undefined
|
|
407
407
|
) {
|
|
408
408
|
if (
|
|
409
|
-
|
|
409
|
+
isPrimitive(moduleDescriptor.compartment) ||
|
|
410
410
|
!weakmapHas(compartmentPrivateFields, moduleDescriptor.compartment) ||
|
|
411
411
|
typeof moduleDescriptor.specifier !== 'string'
|
|
412
412
|
) {
|
|
@@ -507,7 +507,12 @@ const memoizedLoadWithErrorAnnotation = (
|
|
|
507
507
|
return moduleLoading;
|
|
508
508
|
};
|
|
509
509
|
|
|
510
|
-
|
|
510
|
+
/**
|
|
511
|
+
* If `aggregateErrors` is `false`, the `errors` property of the fulfilled object
|
|
512
|
+
* will always be empty.
|
|
513
|
+
* @param {{errors?: Error[], noAggregateErrors?: boolean}} [options]
|
|
514
|
+
*/
|
|
515
|
+
const asyncJobQueue = ({ errors = [], noAggregateErrors = false } = {}) => {
|
|
511
516
|
/** @type {Set<Promise<undefined>>} */
|
|
512
517
|
const pendingJobs = new Set();
|
|
513
518
|
|
|
@@ -523,7 +528,11 @@ const asyncJobQueue = (errors = []) => {
|
|
|
523
528
|
setAdd(
|
|
524
529
|
pendingJobs,
|
|
525
530
|
promiseThen(func(...args), noop, error => {
|
|
526
|
-
|
|
531
|
+
if (noAggregateErrors) {
|
|
532
|
+
throw error;
|
|
533
|
+
} else {
|
|
534
|
+
arrayPush(errors, error);
|
|
535
|
+
}
|
|
527
536
|
}),
|
|
528
537
|
);
|
|
529
538
|
};
|
|
@@ -540,7 +549,12 @@ const asyncJobQueue = (errors = []) => {
|
|
|
540
549
|
return { enqueueJob, drainQueue, errors };
|
|
541
550
|
};
|
|
542
551
|
|
|
543
|
-
|
|
552
|
+
/**
|
|
553
|
+
* If `aggregateErrors` is `false`, the `errors` property of the returned object
|
|
554
|
+
* will always be empty.
|
|
555
|
+
* @param {{errors?: Error[], noAggregateErrors?: boolean}} [options]
|
|
556
|
+
*/
|
|
557
|
+
const syncJobQueue = ({ errors = [], noAggregateErrors = false } = {}) => {
|
|
544
558
|
let current = [];
|
|
545
559
|
let next = [];
|
|
546
560
|
|
|
@@ -564,7 +578,11 @@ const syncJobQueue = (errors = []) => {
|
|
|
564
578
|
try {
|
|
565
579
|
func(...args);
|
|
566
580
|
} catch (error) {
|
|
567
|
-
|
|
581
|
+
if (noAggregateErrors) {
|
|
582
|
+
throw error;
|
|
583
|
+
} else {
|
|
584
|
+
arrayPush(errors, error);
|
|
585
|
+
}
|
|
568
586
|
}
|
|
569
587
|
}
|
|
570
588
|
current = next;
|
|
@@ -597,18 +615,24 @@ const throwAggregateError = ({ errors, errorPrefix }) => {
|
|
|
597
615
|
const preferSync = (_asyncImpl, syncImpl) => syncImpl;
|
|
598
616
|
const preferAsync = (asyncImpl, _syncImpl) => asyncImpl;
|
|
599
617
|
|
|
600
|
-
|
|
618
|
+
/**
|
|
601
619
|
* `load` asynchronously gathers the module records for a module and its
|
|
602
620
|
* transitive dependencies.
|
|
603
621
|
* The module records refer to each other by a reference to the dependency's
|
|
604
622
|
* compartment and the specifier of the module within its own compartment.
|
|
605
623
|
* This graph is then ready to be synchronously linked and executed.
|
|
624
|
+
* @param {WeakMap<Compartment, any>} compartmentPrivateFields
|
|
625
|
+
* @param {WeakMap<object, object>} moduleAliases
|
|
626
|
+
* @param {Compartment} compartment
|
|
627
|
+
* @param {string} moduleSpecifier - The module specifier to load.
|
|
628
|
+
* @param {{ noAggregateErrors?: boolean}} options
|
|
606
629
|
*/
|
|
607
630
|
export const load = async (
|
|
608
631
|
compartmentPrivateFields,
|
|
609
632
|
moduleAliases,
|
|
610
633
|
compartment,
|
|
611
634
|
moduleSpecifier,
|
|
635
|
+
{ noAggregateErrors = false } = {},
|
|
612
636
|
) => {
|
|
613
637
|
const { name: compartmentName } = weakmapGet(
|
|
614
638
|
compartmentPrivateFields,
|
|
@@ -618,7 +642,9 @@ export const load = async (
|
|
|
618
642
|
/** @type {Map<object, Map<string, Promise<Record<any, any>>>>} */
|
|
619
643
|
const moduleLoads = new Map();
|
|
620
644
|
|
|
621
|
-
const { enqueueJob, drainQueue, errors } = asyncJobQueue(
|
|
645
|
+
const { enqueueJob, drainQueue, errors } = asyncJobQueue({
|
|
646
|
+
noAggregateErrors,
|
|
647
|
+
});
|
|
622
648
|
|
|
623
649
|
enqueueJob(memoizedLoadWithErrorAnnotation, [
|
|
624
650
|
compartmentPrivateFields,
|
|
@@ -640,18 +666,25 @@ export const load = async (
|
|
|
640
666
|
});
|
|
641
667
|
};
|
|
642
668
|
|
|
643
|
-
|
|
669
|
+
/**
|
|
644
670
|
* `loadNow` synchronously gathers the module records for a specified module
|
|
645
671
|
* and its transitive dependencies.
|
|
646
672
|
* The module records refer to each other by a reference to the dependency's
|
|
647
673
|
* compartment and the specifier of the module within its own compartment.
|
|
648
674
|
* This graph is then ready to be synchronously linked and executed.
|
|
675
|
+
* @param {WeakMap<Compartment, any>} compartmentPrivateFields
|
|
676
|
+
* @param {WeakMap<object, object>} moduleAliases
|
|
677
|
+
* @param {Compartment} compartment
|
|
678
|
+
* @param {string} moduleSpecifier - The module specifier to load.
|
|
679
|
+
* @param {{ noAggregateErrors?: boolean}} options
|
|
649
680
|
*/
|
|
681
|
+
|
|
650
682
|
export const loadNow = (
|
|
651
683
|
compartmentPrivateFields,
|
|
652
684
|
moduleAliases,
|
|
653
685
|
compartment,
|
|
654
686
|
moduleSpecifier,
|
|
687
|
+
{ noAggregateErrors = false } = {},
|
|
655
688
|
) => {
|
|
656
689
|
const { name: compartmentName } = weakmapGet(
|
|
657
690
|
compartmentPrivateFields,
|
|
@@ -661,7 +694,9 @@ export const loadNow = (
|
|
|
661
694
|
/** @type {Map<object, Map<string, Promise<Record<any, any>>>>} */
|
|
662
695
|
const moduleLoads = new Map();
|
|
663
696
|
|
|
664
|
-
const { enqueueJob, drainQueue, errors } = syncJobQueue(
|
|
697
|
+
const { enqueueJob, drainQueue, errors } = syncJobQueue({
|
|
698
|
+
noAggregateErrors,
|
|
699
|
+
});
|
|
665
700
|
|
|
666
701
|
enqueueJob(memoizedLoadWithErrorAnnotation, [
|
|
667
702
|
compartmentPrivateFields,
|
|
@@ -55,9 +55,9 @@ import {
|
|
|
55
55
|
entries,
|
|
56
56
|
getOwnPropertyDescriptor,
|
|
57
57
|
getPrototypeOf,
|
|
58
|
-
|
|
58
|
+
isPrimitive,
|
|
59
59
|
mapGet,
|
|
60
|
-
|
|
60
|
+
hasOwn,
|
|
61
61
|
ownKeys,
|
|
62
62
|
symbolKeyFor,
|
|
63
63
|
} from './commons.js';
|
|
@@ -131,8 +131,8 @@ export default function removeUnpermittedIntrinsics(
|
|
|
131
131
|
* Validate the object's [[prototype]] against a permit.
|
|
132
132
|
*/
|
|
133
133
|
function visitPrototype(path, obj, protoName) {
|
|
134
|
-
if (
|
|
135
|
-
throw TypeError(`Object expected: ${path}, ${obj}, ${protoName}`);
|
|
134
|
+
if (isPrimitive(obj)) {
|
|
135
|
+
throw TypeError(`Object expected: ${path}, ${String(obj)}, ${protoName}`);
|
|
136
136
|
}
|
|
137
137
|
const proto = getPrototypeOf(obj);
|
|
138
138
|
|
|
@@ -185,7 +185,7 @@ export default function removeUnpermittedIntrinsics(
|
|
|
185
185
|
// Assert: the permit is the name of an intrinsic.
|
|
186
186
|
// Assert: the property value is equal to that intrinsic.
|
|
187
187
|
|
|
188
|
-
if (
|
|
188
|
+
if (hasOwn(intrinsics, permit)) {
|
|
189
189
|
if (value !== intrinsics[permit]) {
|
|
190
190
|
throw TypeError(`Does not match permit for ${path}`);
|
|
191
191
|
}
|
|
@@ -225,7 +225,7 @@ export default function removeUnpermittedIntrinsics(
|
|
|
225
225
|
}
|
|
226
226
|
|
|
227
227
|
// Is this a value property?
|
|
228
|
-
if (
|
|
228
|
+
if (hasOwn(desc, 'value')) {
|
|
229
229
|
if (isAccessorPermit(permit)) {
|
|
230
230
|
throw TypeError(`Accessor expected at ${path}`);
|
|
231
231
|
}
|
|
@@ -245,12 +245,12 @@ export default function removeUnpermittedIntrinsics(
|
|
|
245
245
|
*/
|
|
246
246
|
function getSubPermit(obj, permit, prop) {
|
|
247
247
|
const permitProp = prop === '__proto__' ? '--proto--' : prop;
|
|
248
|
-
if (
|
|
248
|
+
if (hasOwn(permit, permitProp)) {
|
|
249
249
|
return permit[permitProp];
|
|
250
250
|
}
|
|
251
251
|
|
|
252
252
|
if (typeof obj === 'function') {
|
|
253
|
-
if (
|
|
253
|
+
if (hasOwn(FunctionInstance, permitProp)) {
|
|
254
254
|
return FunctionInstance[permitProp];
|
|
255
255
|
}
|
|
256
256
|
}
|
package/src/scope-constants.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
arrayIncludes,
|
|
4
4
|
getOwnPropertyDescriptor,
|
|
5
5
|
getOwnPropertyNames,
|
|
6
|
-
|
|
6
|
+
hasOwn,
|
|
7
7
|
regexpTest,
|
|
8
8
|
} from './commons.js';
|
|
9
9
|
|
|
@@ -131,7 +131,7 @@ function isImmutableDataProperty(obj, name) {
|
|
|
131
131
|
// can't have accessors and value properties at the same time, therefore
|
|
132
132
|
// this check is sufficient. Using explicit own property deal with the
|
|
133
133
|
// case where Object.prototype has been poisoned.
|
|
134
|
-
|
|
134
|
+
hasOwn(desc, 'value')
|
|
135
135
|
);
|
|
136
136
|
}
|
|
137
137
|
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
TypeError,
|
|
5
5
|
defineProperty,
|
|
6
6
|
getOwnPropertyNames,
|
|
7
|
-
|
|
7
|
+
isPrimitive,
|
|
8
8
|
regexpExec,
|
|
9
9
|
} from './commons.js';
|
|
10
10
|
import { assert } from './error/assert.js';
|
|
@@ -54,7 +54,7 @@ export default function tameLocaleMethods(intrinsics, localeTaming = 'safe') {
|
|
|
54
54
|
|
|
55
55
|
for (const intrinsicName of getOwnPropertyNames(intrinsics)) {
|
|
56
56
|
const intrinsic = intrinsics[intrinsicName];
|
|
57
|
-
if (
|
|
57
|
+
if (!isPrimitive(intrinsic)) {
|
|
58
58
|
for (const methodName of getOwnPropertyNames(intrinsic)) {
|
|
59
59
|
const match = regexpExec(localePattern, methodName);
|
|
60
60
|
if (match) {
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
defineProperty,
|
|
3
3
|
iteratorPrototype,
|
|
4
4
|
iteratorSymbol,
|
|
5
|
-
|
|
5
|
+
hasOwn,
|
|
6
6
|
} from './commons.js';
|
|
7
7
|
|
|
8
8
|
export const tameRegeneratorRuntime = () => {
|
|
@@ -15,7 +15,7 @@ export const tameRegeneratorRuntime = () => {
|
|
|
15
15
|
set(value) {
|
|
16
16
|
// ignore the assignment on IteratorPrototype
|
|
17
17
|
if (this === iteratorPrototype) return;
|
|
18
|
-
if (
|
|
18
|
+
if (hasOwn(this, iteratorSymbol)) {
|
|
19
19
|
this[iteratorSymbol] = value;
|
|
20
20
|
}
|
|
21
21
|
defineProperty(this, iteratorSymbol, {
|
package/types.d.ts
CHANGED
|
@@ -172,6 +172,11 @@ export interface CompartmentOptions {
|
|
|
172
172
|
/** @deprecated */
|
|
173
173
|
loadNowHook?: (specifier: string) => ModuleDescriptor;
|
|
174
174
|
__native__?: boolean;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* If `true`, the first error encountered during module loading will be thrown immediately
|
|
178
|
+
*/
|
|
179
|
+
noAggregateLoadErrors?: boolean;
|
|
175
180
|
}
|
|
176
181
|
|
|
177
182
|
export interface EvaluateOptions {
|
package/src/make-lru-cachemap.js
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
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);
|