ses 1.1.0 → 1.3.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.
@@ -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/permits.js CHANGED
@@ -1,4 +1,8 @@
1
1
  /* eslint-disable no-restricted-globals */
2
+ /* eslint max-lines: 0 */
3
+
4
+ import { arrayPush } from './commons.js';
5
+
2
6
  /**
3
7
  * @file Exports {@code whitelist}, a recursively defined
4
8
  * JSON record enumerating all intrinsics and their properties
@@ -8,8 +12,6 @@
8
12
  * @author Mark S. Miller
9
13
  */
10
14
 
11
- /* eslint max-lines: 0 */
12
-
13
15
  /**
14
16
  * constantProperties
15
17
  * non-configurable, non-writable data properties of all global objects.
@@ -53,6 +55,8 @@ export const universalPropertyNames = {
53
55
  Boolean: 'Boolean',
54
56
  DataView: 'DataView',
55
57
  EvalError: 'EvalError',
58
+ // https://github.com/tc39/proposal-float16array
59
+ Float16Array: 'Float16Array',
56
60
  Float32Array: 'Float32Array',
57
61
  Float64Array: 'Float64Array',
58
62
  Int8Array: 'Int8Array',
@@ -80,6 +84,8 @@ export const universalPropertyNames = {
80
84
  Iterator: 'Iterator',
81
85
  // https://github.com/tc39/proposal-async-iterator-helpers
82
86
  AsyncIterator: 'AsyncIterator',
87
+ // https://github.com/endojs/endo/issues/550
88
+ AggregateError: 'AggregateError',
83
89
 
84
90
  // *** Other Properties of the Global Object
85
91
 
@@ -183,16 +189,27 @@ export const uniqueGlobalPropertyNames = {
183
189
 
184
190
  // All the "subclasses" of Error. These are collectively represented in the
185
191
  // ECMAScript spec by the meta variable NativeError.
186
- // TODO Add AggregateError https://github.com/Agoric/SES-shim/issues/550
187
- export const NativeErrors = [
192
+ /** @type {GenericErrorConstructor[]} */
193
+ const NativeErrors = [
188
194
  EvalError,
189
195
  RangeError,
190
196
  ReferenceError,
191
197
  SyntaxError,
192
198
  TypeError,
193
199
  URIError,
200
+ // https://github.com/endojs/endo/issues/550
201
+ // Commented out to accommodate platforms prior to AggregateError.
202
+ // Instead, conditional push below.
203
+ // AggregateError,
194
204
  ];
195
205
 
206
+ if (typeof AggregateError !== 'undefined') {
207
+ // Conditional, to accommodate platforms prior to AggregateError
208
+ arrayPush(NativeErrors, AggregateError);
209
+ }
210
+
211
+ export { NativeErrors };
212
+
196
213
  /**
197
214
  * <p>Each JSON record enumerates the disposition of the properties on
198
215
  * some corresponding intrinsic object.
@@ -382,6 +399,8 @@ const CommonMath = {
382
399
  irem: false,
383
400
  // See https://github.com/Moddable-OpenSource/moddable/issues/523
384
401
  mod: false,
402
+ // See https://github.com/Moddable-OpenSource/moddable/issues/523#issuecomment-1942904505
403
+ irandom: false,
385
404
  };
386
405
 
387
406
  export const permitted = {
@@ -443,6 +462,8 @@ export const permitted = {
443
462
  values: fn,
444
463
  // https://github.com/tc39/proposal-array-grouping
445
464
  groupBy: fn,
465
+ // Seen on QuickJS
466
+ __getClass: false,
446
467
  },
447
468
 
448
469
  '%ObjectPrototype%': {
@@ -489,6 +510,10 @@ export const permitted = {
489
510
  caller: false,
490
511
  // proposed but not yet std. To be removed if there
491
512
  arguments: false,
513
+ // Seen on QuickJS. TODO grab getter for use by console
514
+ fileName: false,
515
+ // Seen on QuickJS. TODO grab getter for use by console
516
+ lineNumber: false,
492
517
  },
493
518
 
494
519
  Boolean: {
@@ -528,6 +553,8 @@ export const permitted = {
528
553
  useSimple: false,
529
554
  // Seen at core-js https://github.com/zloirock/core-js#ecmascript-symbol
530
555
  useSetter: false,
556
+ // Seen on QuickJS
557
+ operatorSet: false,
531
558
  },
532
559
 
533
560
  '%SymbolPrototype%': {
@@ -587,6 +614,8 @@ export const permitted = {
587
614
  SyntaxError: NativeError('%SyntaxErrorPrototype%'),
588
615
  TypeError: NativeError('%TypeErrorPrototype%'),
589
616
  URIError: NativeError('%URIErrorPrototype%'),
617
+ // https://github.com/endojs/endo/issues/550
618
+ AggregateError: NativeError('%AggregateErrorPrototype%'),
590
619
 
591
620
  '%EvalErrorPrototype%': NativeErrorPrototype('EvalError'),
592
621
  '%RangeErrorPrototype%': NativeErrorPrototype('RangeError'),
@@ -594,6 +623,8 @@ export const permitted = {
594
623
  '%SyntaxErrorPrototype%': NativeErrorPrototype('SyntaxError'),
595
624
  '%TypeErrorPrototype%': NativeErrorPrototype('TypeError'),
596
625
  '%URIErrorPrototype%': NativeErrorPrototype('URIError'),
626
+ // https://github.com/endojs/endo/issues/550
627
+ '%AggregateErrorPrototype%': NativeErrorPrototype('AggregateError'),
597
628
 
598
629
  // *** Numbers and Dates
599
630
 
@@ -638,6 +669,30 @@ export const permitted = {
638
669
  bitLength: false,
639
670
  // See https://github.com/Moddable-OpenSource/moddable/issues/523
640
671
  fromArrayBuffer: false,
672
+ // Seen on QuickJS
673
+ tdiv: false,
674
+ // Seen on QuickJS
675
+ fdiv: false,
676
+ // Seen on QuickJS
677
+ cdiv: false,
678
+ // Seen on QuickJS
679
+ ediv: false,
680
+ // Seen on QuickJS
681
+ tdivrem: false,
682
+ // Seen on QuickJS
683
+ fdivrem: false,
684
+ // Seen on QuickJS
685
+ cdivrem: false,
686
+ // Seen on QuickJS
687
+ edivrem: false,
688
+ // Seen on QuickJS
689
+ sqrt: false,
690
+ // Seen on QuickJS
691
+ sqrtrem: false,
692
+ // Seen on QuickJS
693
+ floorLog2: false,
694
+ // Seen on QuickJS
695
+ ctz: false,
641
696
  },
642
697
 
643
698
  '%BigIntPrototype%': {
@@ -806,6 +861,8 @@ export const permitted = {
806
861
  isWellFormed: fn,
807
862
  toWellFormed: fn,
808
863
  unicodeSets: fn,
864
+ // Seen on QuickJS
865
+ __quote: false,
809
866
  },
810
867
 
811
868
  '%StringIteratorPrototype%': {
@@ -1045,6 +1102,8 @@ export const permitted = {
1045
1102
 
1046
1103
  BigInt64Array: TypedArray('%BigInt64ArrayPrototype%'),
1047
1104
  BigUint64Array: TypedArray('%BigUint64ArrayPrototype%'),
1105
+ // https://github.com/tc39/proposal-float16array
1106
+ Float16Array: TypedArray('%Float16ArrayPrototype%'),
1048
1107
  Float32Array: TypedArray('%Float32ArrayPrototype%'),
1049
1108
  Float64Array: TypedArray('%Float64ArrayPrototype%'),
1050
1109
  Int16Array: TypedArray('%Int16ArrayPrototype%'),
@@ -1057,6 +1116,8 @@ export const permitted = {
1057
1116
 
1058
1117
  '%BigInt64ArrayPrototype%': TypedArrayPrototype('BigInt64Array'),
1059
1118
  '%BigUint64ArrayPrototype%': TypedArrayPrototype('BigUint64Array'),
1119
+ // https://github.com/tc39/proposal-float16array
1120
+ '%Float16ArrayPrototype%': TypedArrayPrototype('Float16Array'),
1060
1121
  '%Float32ArrayPrototype%': TypedArrayPrototype('Float32Array'),
1061
1122
  '%Float64ArrayPrototype%': TypedArrayPrototype('Float64Array'),
1062
1123
  '%Int16ArrayPrototype%': TypedArrayPrototype('Int16Array'),
@@ -1106,6 +1167,8 @@ export const permitted = {
1106
1167
  '[[Proto]]': '%FunctionPrototype%',
1107
1168
  prototype: '%SetPrototype%',
1108
1169
  '@@species': getter,
1170
+ // Seen on QuickJS
1171
+ groupBy: false,
1109
1172
  },
1110
1173
 
1111
1174
  '%SetPrototype%': {
@@ -1222,6 +1285,8 @@ export const permitted = {
1222
1285
  constructor: 'DataView',
1223
1286
  getBigInt64: fn,
1224
1287
  getBigUint64: fn,
1288
+ // https://github.com/tc39/proposal-float16array
1289
+ getFloat16: fn,
1225
1290
  getFloat32: fn,
1226
1291
  getFloat64: fn,
1227
1292
  getInt8: fn,
@@ -1232,6 +1297,8 @@ export const permitted = {
1232
1297
  getUint32: fn,
1233
1298
  setBigInt64: fn,
1234
1299
  setBigUint64: fn,
1300
+ // https://github.com/tc39/proposal-float16array
1301
+ setFloat16: fn,
1235
1302
  setFloat32: fn,
1236
1303
  setFloat64: fn,
1237
1304
  setInt8: fn,
@@ -1284,6 +1351,8 @@ export const permitted = {
1284
1351
  '@@toStringTag': 'string',
1285
1352
  // https://github.com/tc39/proposal-async-iterator-helpers
1286
1353
  toAsync: fn,
1354
+ // See https://github.com/Moddable-OpenSource/moddable/issues/523#issuecomment-1942904505
1355
+ '@@dispose': false,
1287
1356
  },
1288
1357
 
1289
1358
  // https://github.com/tc39/proposal-iterator-helpers
@@ -1326,6 +1395,8 @@ export const permitted = {
1326
1395
  every: fn,
1327
1396
  find: fn,
1328
1397
  '@@toStringTag': 'string',
1398
+ // See https://github.com/Moddable-OpenSource/moddable/issues/523#issuecomment-1942904505
1399
+ '@@asyncDispose': false,
1329
1400
  },
1330
1401
 
1331
1402
  // https://github.com/tc39/proposal-async-iterator-helpers
@@ -1421,9 +1492,8 @@ export const permitted = {
1421
1492
  '[[Proto]]': '%FunctionPrototype%',
1422
1493
  all: fn,
1423
1494
  allSettled: fn,
1424
- // To transition from `false` to `fn` once we also have `AggregateError`
1425
- // TODO https://github.com/Agoric/SES-shim/issues/550
1426
- any: false, // ES2021
1495
+ // https://github.com/Agoric/SES-shim/issues/550
1496
+ any: fn,
1427
1497
  prototype: '%PromisePrototype%',
1428
1498
  race: fn,
1429
1499
  reject: fn,
@@ -1516,12 +1586,11 @@ export const permitted = {
1516
1586
  evaluate: fn,
1517
1587
  globalThis: getter,
1518
1588
  name: getter,
1519
- // Should this be proposed?
1520
- toString: fn,
1521
1589
  import: asyncFn,
1522
1590
  load: asyncFn,
1523
1591
  importNow: fn,
1524
1592
  module: fn,
1593
+ '@@toStringTag': 'string',
1525
1594
  },
1526
1595
 
1527
1596
  lockdown: fn,
package/types.d.ts CHANGED
@@ -119,6 +119,8 @@ export type Details = string | DetailsToken;
119
119
 
120
120
  export interface AssertMakeErrorOptions {
121
121
  errorName?: string;
122
+ cause?: Error;
123
+ errors?: Error[];
122
124
  }
123
125
 
124
126
  type AssertTypeofBigint = (
@@ -175,6 +177,10 @@ interface ToStringable {
175
177
  toString(): string;
176
178
  }
177
179
 
180
+ export type GenericErrorConstructor =
181
+ | ErrorConstructor
182
+ | AggregateErrorConstructor;
183
+
178
184
  export type Raise = (reason: Error) => void;
179
185
  // Behold: recursion.
180
186
  // eslint-disable-next-line no-use-before-define
@@ -184,23 +190,29 @@ export interface AssertionFunctions {
184
190
  (
185
191
  value: any,
186
192
  details?: Details,
187
- errorConstructor?: ErrorConstructor,
193
+ errConstructor?: GenericErrorConstructor,
194
+ options?: AssertMakeErrorOptions,
188
195
  ): asserts value;
189
196
  typeof: AssertTypeof;
190
197
  equal(
191
198
  left: any,
192
199
  right: any,
193
200
  details?: Details,
194
- errorConstructor?: ErrorConstructor,
201
+ errConstructor?: GenericErrorConstructor,
202
+ options?: AssertMakeErrorOptions,
195
203
  ): void;
196
204
  string(specimen: any, details?: Details): asserts specimen is string;
197
- fail(details?: Details, errorConstructor?: ErrorConstructor): never;
205
+ fail(
206
+ details?: Details,
207
+ errConstructor?: GenericErrorConstructor,
208
+ options?: AssertMakeErrorOptions,
209
+ ): never;
198
210
  }
199
211
 
200
212
  export interface AssertionUtilities {
201
213
  error(
202
214
  details?: Details,
203
- errorConstructor?: ErrorConstructor,
215
+ errConstructor?: GenericErrorConstructor,
204
216
  options?: AssertMakeErrorOptions,
205
217
  ): Error;
206
218
  note(error: Error, details: Details): void;