ses 1.1.0 → 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 +7 -0
- package/README.md +1 -1
- package/dist/lockdown.cjs +394 -299
- package/dist/lockdown.mjs +394 -299
- package/dist/lockdown.umd.js +394 -299
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +394 -299
- package/dist/ses.mjs +394 -299
- package/dist/ses.umd.js +394 -299
- package/dist/ses.umd.min.js +1 -1
- package/package.json +11 -7
- package/src/compartment.js +13 -4
- package/src/enablements.js +3 -1
- package/src/error/note-log-args.js +1 -205
- package/src/error/tame-console.js +8 -8
- package/src/make-lru-cachemap.js +221 -0
- package/src/permits.js +53 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Hardened JavaScript for Fearless Cooperation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lockdown",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"homepage": "https://github.com/Agoric/SES-shim/tree/master/packages/ses#readme",
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
|
26
|
-
"url": "git+https://github.com/endojs/endo.git"
|
|
26
|
+
"url": "git+https://github.com/endojs/endo.git",
|
|
27
|
+
"directory": "packages/ses"
|
|
27
28
|
},
|
|
28
29
|
"bugs": {
|
|
29
30
|
"url": "https://github.com/endojs/endo/issues"
|
|
@@ -45,6 +46,9 @@
|
|
|
45
46
|
"require": "./dist/ses.cjs"
|
|
46
47
|
},
|
|
47
48
|
"./tools.js": "./tools.js",
|
|
49
|
+
"./assert-shim.js": "./assert-shim.js",
|
|
50
|
+
"./lockdown-shim.js": "./lockdown-shim.js",
|
|
51
|
+
"./compartment-shim.js": "./compartment-shim.js",
|
|
48
52
|
"./package.json": "./package.json"
|
|
49
53
|
},
|
|
50
54
|
"scripts": {
|
|
@@ -62,12 +66,12 @@
|
|
|
62
66
|
"test:platform-compatibility": "node test/package/test.cjs"
|
|
63
67
|
},
|
|
64
68
|
"dependencies": {
|
|
65
|
-
"@endo/env-options": "^1.1.
|
|
69
|
+
"@endo/env-options": "^1.1.1"
|
|
66
70
|
},
|
|
67
71
|
"devDependencies": {
|
|
68
|
-
"@endo/compartment-mapper": "^1.1.
|
|
69
|
-
"@endo/static-module-record": "^1.0.
|
|
70
|
-
"@endo/test262-runner": "^0.1.
|
|
72
|
+
"@endo/compartment-mapper": "^1.1.1",
|
|
73
|
+
"@endo/static-module-record": "^1.0.3",
|
|
74
|
+
"@endo/test262-runner": "^0.1.33",
|
|
71
75
|
"ava": "^5.3.0",
|
|
72
76
|
"babel-eslint": "^10.0.3",
|
|
73
77
|
"c8": "^7.14.0",
|
|
@@ -184,5 +188,5 @@
|
|
|
184
188
|
"typeCoverage": {
|
|
185
189
|
"atLeast": 81.17
|
|
186
190
|
},
|
|
187
|
-
"gitHead": "
|
|
191
|
+
"gitHead": "f731c5c12f8d185dbf2daf53ec6a57e5ac56e4e9"
|
|
188
192
|
}
|
package/src/compartment.js
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
defineProperties,
|
|
12
12
|
entries,
|
|
13
13
|
promiseThen,
|
|
14
|
+
toStringTagSymbol,
|
|
14
15
|
weakmapGet,
|
|
15
16
|
weakmapSet,
|
|
16
17
|
} from './commons.js';
|
|
@@ -105,10 +106,6 @@ export const CompartmentPrototype = {
|
|
|
105
106
|
return compartmentEvaluate(compartmentFields, source, options);
|
|
106
107
|
},
|
|
107
108
|
|
|
108
|
-
toString() {
|
|
109
|
-
return '[object Compartment]';
|
|
110
|
-
},
|
|
111
|
-
|
|
112
109
|
module(specifier) {
|
|
113
110
|
if (typeof specifier !== 'string') {
|
|
114
111
|
throw TypeError('first argument of module() must be a string');
|
|
@@ -168,6 +165,18 @@ export const CompartmentPrototype = {
|
|
|
168
165
|
},
|
|
169
166
|
};
|
|
170
167
|
|
|
168
|
+
// This causes `String(new Compartment())` to evaluate to `[object Compartment]`.
|
|
169
|
+
// The descriptor follows the conventions of other globals with @@toStringTag
|
|
170
|
+
// properties, e.g. Math.
|
|
171
|
+
defineProperties(CompartmentPrototype, {
|
|
172
|
+
[toStringTagSymbol]: {
|
|
173
|
+
value: 'Compartment',
|
|
174
|
+
writable: false,
|
|
175
|
+
enumerable: false,
|
|
176
|
+
configurable: true,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
171
180
|
defineProperties(InertCompartment, {
|
|
172
181
|
prototype: { value: CompartmentPrototype },
|
|
173
182
|
});
|
package/src/enablements.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { toStringTagSymbol } from './commons.js';
|
|
1
|
+
import { toStringTagSymbol, iteratorSymbol } from './commons.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @file Exports {@code enablements}, a recursively defined
|
|
@@ -97,6 +97,8 @@ export const moderateEnablements = {
|
|
|
97
97
|
'%ArrayPrototype%': {
|
|
98
98
|
toString: true,
|
|
99
99
|
push: true, // set by "Google Analytics"
|
|
100
|
+
concat: true, // set by mobx generated code (old TS compiler?)
|
|
101
|
+
[iteratorSymbol]: true, // set by mobx generated code (old TS compiler?)
|
|
100
102
|
},
|
|
101
103
|
|
|
102
104
|
// Function.prototype has no 'prototype' property to enable.
|
|
@@ -2,216 +2,12 @@
|
|
|
2
2
|
/* eslint-disable @endo/no-polymorphic-call */
|
|
3
3
|
/* eslint-disable no-restricted-globals */
|
|
4
4
|
|
|
5
|
+
import { makeLRUCacheMap } from '../make-lru-cachemap.js';
|
|
5
6
|
import './internal-types.js';
|
|
6
7
|
|
|
7
8
|
const { freeze } = Object;
|
|
8
9
|
const { isSafeInteger } = Number;
|
|
9
10
|
|
|
10
|
-
/**
|
|
11
|
-
* @template Data
|
|
12
|
-
* @typedef {object} DoublyLinkedCell
|
|
13
|
-
* A cell of a doubly-linked ring, i.e., a doubly-linked circular list.
|
|
14
|
-
* DoublyLinkedCells are not frozen, and so should be closely encapsulated by
|
|
15
|
-
* any abstraction that uses them.
|
|
16
|
-
* @property {DoublyLinkedCell<Data>} next
|
|
17
|
-
* @property {DoublyLinkedCell<Data>} prev
|
|
18
|
-
* @property {Data} data
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Makes a new self-linked cell. There are two reasons to do so:
|
|
23
|
-
* * To make the head sigil of a new initially-empty doubly-linked ring.
|
|
24
|
-
* * To make a non-sigil cell to be `spliceAfter`ed.
|
|
25
|
-
*
|
|
26
|
-
* @template Data
|
|
27
|
-
* @param {Data} data
|
|
28
|
-
* @returns {DoublyLinkedCell<Data>}
|
|
29
|
-
*/
|
|
30
|
-
const makeSelfCell = data => {
|
|
31
|
-
/** @type {Partial<DoublyLinkedCell<Data>>} */
|
|
32
|
-
const incompleteCell = {
|
|
33
|
-
next: undefined,
|
|
34
|
-
prev: undefined,
|
|
35
|
-
data,
|
|
36
|
-
};
|
|
37
|
-
const selfCell = /** @type {DoublyLinkedCell<Data>} */ (incompleteCell);
|
|
38
|
-
selfCell.next = selfCell;
|
|
39
|
-
selfCell.prev = selfCell;
|
|
40
|
-
// Not frozen!
|
|
41
|
-
return selfCell;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Splices a self-linked non-sigil cell into a ring after `prev`.
|
|
46
|
-
* `prev` could be the head sigil, or it could be some other non-sigil
|
|
47
|
-
* cell within a ring.
|
|
48
|
-
*
|
|
49
|
-
* @template Data
|
|
50
|
-
* @param {DoublyLinkedCell<Data>} prev
|
|
51
|
-
* @param {DoublyLinkedCell<Data>} selfCell
|
|
52
|
-
*/
|
|
53
|
-
const spliceAfter = (prev, selfCell) => {
|
|
54
|
-
if (prev === selfCell) {
|
|
55
|
-
throw TypeError('Cannot splice a cell into itself');
|
|
56
|
-
}
|
|
57
|
-
if (selfCell.next !== selfCell || selfCell.prev !== selfCell) {
|
|
58
|
-
throw TypeError('Expected self-linked cell');
|
|
59
|
-
}
|
|
60
|
-
const cell = selfCell;
|
|
61
|
-
// rename variable cause it isn't self-linked after this point.
|
|
62
|
-
|
|
63
|
-
const next = prev.next;
|
|
64
|
-
cell.prev = prev;
|
|
65
|
-
cell.next = next;
|
|
66
|
-
prev.next = cell;
|
|
67
|
-
next.prev = cell;
|
|
68
|
-
// Not frozen!
|
|
69
|
-
return cell;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* @template Data
|
|
74
|
-
* @param {DoublyLinkedCell<Data>} cell
|
|
75
|
-
* No-op if the cell is self-linked.
|
|
76
|
-
*/
|
|
77
|
-
const spliceOut = cell => {
|
|
78
|
-
const { prev, next } = cell;
|
|
79
|
-
prev.next = next;
|
|
80
|
-
next.prev = prev;
|
|
81
|
-
cell.prev = cell;
|
|
82
|
-
cell.next = cell;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The LRUCacheMap is used within the implementation of `assert` and so
|
|
87
|
-
* at a layer below SES or harden. Thus, we give it a `WeakMap`-like interface
|
|
88
|
-
* rather than a `WeakMapStore`-like interface. To work before `lockdown`,
|
|
89
|
-
* the implementation must use `freeze` manually, but still exhaustively.
|
|
90
|
-
*
|
|
91
|
-
* It implements the WeakMap interface, and holds its keys weakly. Cached
|
|
92
|
-
* values are only held while the key is held by the user and the key/value
|
|
93
|
-
* bookkeeping cell has not been pushed off the end of the cache by `budget`
|
|
94
|
-
* number of more recently referenced cells. If the key is dropped by the user,
|
|
95
|
-
* the value will no longer be held by the cache, but the bookkeeping cell
|
|
96
|
-
* itself will stay in memory.
|
|
97
|
-
*
|
|
98
|
-
* @template {{}} K
|
|
99
|
-
* @template {unknown} V
|
|
100
|
-
* @param {number} keysBudget
|
|
101
|
-
* @returns {WeakMap<K,V>}
|
|
102
|
-
*/
|
|
103
|
-
export const makeLRUCacheMap = keysBudget => {
|
|
104
|
-
if (!isSafeInteger(keysBudget) || keysBudget < 0) {
|
|
105
|
-
throw TypeError('keysBudget must be a safe non-negative integer number');
|
|
106
|
-
}
|
|
107
|
-
/** @typedef {DoublyLinkedCell<WeakMap<K, V> | undefined>} LRUCacheCell */
|
|
108
|
-
/** @type {WeakMap<K, LRUCacheCell>} */
|
|
109
|
-
const keyToCell = new WeakMap();
|
|
110
|
-
let size = 0; // `size` must remain <= `keysBudget`
|
|
111
|
-
// As a sigil, `head` uniquely is not in the `keyToCell` map.
|
|
112
|
-
/** @type {LRUCacheCell} */
|
|
113
|
-
const head = makeSelfCell(undefined);
|
|
114
|
-
|
|
115
|
-
const touchCell = key => {
|
|
116
|
-
const cell = keyToCell.get(key);
|
|
117
|
-
if (cell === undefined || cell.data === undefined) {
|
|
118
|
-
// Either the key was GCed, or the cell was condemned.
|
|
119
|
-
return undefined;
|
|
120
|
-
}
|
|
121
|
-
// Becomes most recently used
|
|
122
|
-
spliceOut(cell);
|
|
123
|
-
spliceAfter(head, cell);
|
|
124
|
-
return cell;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* @param {K} key
|
|
129
|
-
*/
|
|
130
|
-
const has = key => touchCell(key) !== undefined;
|
|
131
|
-
freeze(has);
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* @param {K} key
|
|
135
|
-
*/
|
|
136
|
-
// UNTIL https://github.com/endojs/endo/issues/1514
|
|
137
|
-
// Prefer: const get = key => touchCell(key)?.data?.get(key);
|
|
138
|
-
const get = key => {
|
|
139
|
-
const cell = touchCell(key);
|
|
140
|
-
return cell && cell.data && cell.data.get(key);
|
|
141
|
-
};
|
|
142
|
-
freeze(get);
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* @param {K} key
|
|
146
|
-
* @param {V} value
|
|
147
|
-
*/
|
|
148
|
-
const set = (key, value) => {
|
|
149
|
-
if (keysBudget < 1) {
|
|
150
|
-
// eslint-disable-next-line no-use-before-define
|
|
151
|
-
return lruCacheMap; // Implements WeakMap.set
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
let cell = touchCell(key);
|
|
155
|
-
if (cell === undefined) {
|
|
156
|
-
cell = makeSelfCell(undefined);
|
|
157
|
-
spliceAfter(head, cell); // start most recently used
|
|
158
|
-
}
|
|
159
|
-
if (!cell.data) {
|
|
160
|
-
// Either a fresh cell or a reused condemned cell.
|
|
161
|
-
size += 1;
|
|
162
|
-
// Add its data.
|
|
163
|
-
cell.data = new WeakMap();
|
|
164
|
-
// Advertise the cell for this key.
|
|
165
|
-
keyToCell.set(key, cell);
|
|
166
|
-
while (size > keysBudget) {
|
|
167
|
-
const condemned = head.prev;
|
|
168
|
-
spliceOut(condemned); // Drop least recently used
|
|
169
|
-
condemned.data = undefined;
|
|
170
|
-
size -= 1;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Update the data.
|
|
175
|
-
cell.data.set(key, value);
|
|
176
|
-
|
|
177
|
-
// eslint-disable-next-line no-use-before-define
|
|
178
|
-
return lruCacheMap; // Implements WeakMap.set
|
|
179
|
-
};
|
|
180
|
-
freeze(set);
|
|
181
|
-
|
|
182
|
-
// "delete" is a keyword.
|
|
183
|
-
/**
|
|
184
|
-
* @param {K} key
|
|
185
|
-
*/
|
|
186
|
-
const deleteIt = key => {
|
|
187
|
-
const cell = keyToCell.get(key);
|
|
188
|
-
if (cell === undefined) {
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
spliceOut(cell);
|
|
192
|
-
keyToCell.delete(key);
|
|
193
|
-
if (cell.data === undefined) {
|
|
194
|
-
// Already condemned.
|
|
195
|
-
return false;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
cell.data = undefined;
|
|
199
|
-
size -= 1;
|
|
200
|
-
return true;
|
|
201
|
-
};
|
|
202
|
-
freeze(deleteIt);
|
|
203
|
-
|
|
204
|
-
const lruCacheMap = freeze({
|
|
205
|
-
has,
|
|
206
|
-
get,
|
|
207
|
-
set,
|
|
208
|
-
delete: deleteIt,
|
|
209
|
-
[Symbol.toStringTag]: 'LRUCacheMap',
|
|
210
|
-
});
|
|
211
|
-
return lruCacheMap;
|
|
212
|
-
};
|
|
213
|
-
freeze(makeLRUCacheMap);
|
|
214
|
-
|
|
215
11
|
const defaultLoggedErrorsBudget = 1000;
|
|
216
12
|
const defaultArgsPerErrorBudget = 100;
|
|
217
13
|
|
|
@@ -58,14 +58,14 @@ export const tameConsole = (
|
|
|
58
58
|
typeof globalThis.console !== 'undefined'
|
|
59
59
|
? globalThis.console
|
|
60
60
|
: typeof globalThis.print === 'function'
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
69
|
);
|
|
70
70
|
|
|
71
71
|
// Upgrade a log-only console (as in `eshost -h SpiderMonkey`).
|
|
@@ -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);
|