1
1
/// <reference types="ses"/>
2
2
3
- import { X , q , Fail } from '@endo/errors' ;
4
- import { identChecker } from '@endo/common/ident-checker.js' ;
5
- import {
6
- assertChecker ,
7
- Far ,
8
- getTag ,
9
- makeTagged ,
10
- passStyleOf ,
11
- isAtom ,
12
- } from '@endo/pass-style' ;
3
+ import { Fail , q , hideAndHardenFunction } from '@endo/errors' ;
4
+ import { Far , getTag , makeTagged , passStyleOf , isAtom } from '@endo/pass-style' ;
13
5
import {
14
6
compareAntiRank ,
15
7
makeFullOrderComparatorKit ,
16
8
sortByRank ,
17
9
} from '@endo/marshal' ;
18
10
19
- import { checkElements , makeSetOfElements } from './copySet.js' ;
20
- import { checkBagEntries , makeBagOfEntries } from './copyBag.js' ;
11
+ import { confirmElements , makeSetOfElements } from './copySet.js' ;
12
+ import { confirmBagEntries , makeBagOfEntries } from './copyBag.js' ;
21
13
22
14
const { ownKeys } = Reflect ;
23
15
24
16
/**
25
- * @import {Passable, Atom, Checker} from '@endo/pass-style'
26
- * @import {CopyBag, CopyMap, CopySet, Key, ScalarKey} from '../types.js'
17
+ * @import {Rejector} from '@endo/errors/rejector.js';
18
+ * @import {Passable, Atom} from '@endo/pass-style';
19
+ * @import {CopyBag, CopyMap, CopySet, Key, ScalarKey} from '../types.js';
27
20
*/
28
21
29
22
// ////////////////// Atom and Scalar keys ////////////////////////////////
30
23
31
24
/**
32
25
* @param {any } val
33
- * @param {Checker } check
26
+ * @param {Rejector } reject
34
27
* @returns {boolean }
35
28
*/
36
- export const checkScalarKey = ( val , check ) => {
29
+ export const confirmScalarKey = ( val , reject ) => {
37
30
if ( isAtom ( val ) ) {
38
31
return true ;
39
32
}
40
33
const passStyle = passStyleOf ( val ) ;
41
34
if ( passStyle === 'remotable' ) {
42
35
return true ;
43
36
}
44
- return check ( false , X `A ${ q ( passStyle ) } cannot be a scalar key: ${ val } ` ) ;
37
+ return reject && reject `A ${ q ( passStyle ) } cannot be a scalar key: ${ val } ` ;
45
38
} ;
46
39
47
40
/**
48
41
* @param {any } val
49
42
* @returns {val is ScalarKey }
50
43
*/
51
- export const isScalarKey = val => checkScalarKey ( val , identChecker ) ;
52
- harden ( isScalarKey ) ;
44
+ export const isScalarKey = val => confirmScalarKey ( val , false ) ;
45
+ hideAndHardenFunction ( isScalarKey ) ;
53
46
54
47
/**
55
48
* @param {Passable } val
56
49
* @returns {asserts val is ScalarKey }
57
50
*/
58
51
export const assertScalarKey = val => {
59
- checkScalarKey ( val , assertChecker ) ;
52
+ confirmScalarKey ( val , Fail ) ;
60
53
} ;
61
- harden ( assertScalarKey ) ;
54
+ hideAndHardenFunction ( assertScalarKey ) ;
62
55
63
56
// ////////////////////////////// Keys /////////////////////////////////////////
64
57
@@ -69,10 +62,10 @@ const keyMemo = new WeakSet();
69
62
70
63
/**
71
64
* @param {unknown } val
72
- * @param {Checker } check
65
+ * @param {Rejector } reject
73
66
* @returns {boolean }
74
67
*/
75
- export const checkKey = ( val , check ) => {
68
+ export const confirmKey = ( val , reject ) => {
76
69
if ( isAtom ( val ) ) {
77
70
return true ;
78
71
}
@@ -81,36 +74,36 @@ export const checkKey = (val, check) => {
81
74
return true ;
82
75
}
83
76
// eslint-disable-next-line no-use-before-define
84
- const result = checkKeyInternal ( val , check ) ;
77
+ const result = confirmKeyInternal ( val , reject ) ;
85
78
if ( result ) {
86
79
// Don't cache the undefined cases, so that if it is tried again
87
- // with `assertChecker ` it'll throw a diagnostic again
80
+ // with `Fail ` it'll throw a diagnostic again
88
81
// @ts -expect-error narrowed
89
82
keyMemo . add ( val ) ;
90
83
}
91
- // Note that we do not memoize a negative judgement, so that if it is tried
92
- // again with a checker , it will still produce a useful diagnostic.
84
+ // Note that we must not memoize a negative judgement, so that if it is tried
85
+ // again with `Fail` , it will still produce a useful diagnostic.
93
86
return result ;
94
87
} ;
95
- harden ( checkKey ) ;
88
+ harden ( confirmKey ) ;
96
89
97
90
/**
98
91
* @type {{
99
92
* (val: Passable): val is Key;
100
93
* (val: any): boolean;
101
94
* }}
102
95
*/
103
- export const isKey = val => checkKey ( val , identChecker ) ;
104
- harden ( isKey ) ;
96
+ export const isKey = val => confirmKey ( val , false ) ;
97
+ hideAndHardenFunction ( isKey ) ;
105
98
106
99
/**
107
100
* @param {Key } val
108
101
* @returns {asserts val is Key }
109
102
*/
110
103
export const assertKey = val => {
111
- checkKey ( val , assertChecker ) ;
104
+ confirmKey ( val , Fail ) ;
112
105
} ;
113
- harden ( assertKey ) ;
106
+ hideAndHardenFunction ( assertKey ) ;
114
107
115
108
// //////////////////////////// CopySet ////////////////////////////////////////
116
109
@@ -122,31 +115,31 @@ const copySetMemo = new WeakSet();
122
115
123
116
/**
124
117
* @param {any } s
125
- * @param {Checker } check
118
+ * @param {Rejector } reject
126
119
* @returns {boolean }
127
120
*/
128
- export const checkCopySet = ( s , check ) => {
121
+ export const confirmCopySet = ( s , reject ) => {
129
122
if ( copySetMemo . has ( s ) ) {
130
123
return true ;
131
124
}
132
125
const result =
133
126
( ( passStyleOf ( s ) === 'tagged' && getTag ( s ) === 'copySet' ) ||
134
- check ( false , X `Not a copySet: ${ s } ` ) ) &&
135
- checkElements ( s . payload , check ) &&
136
- checkKey ( s . payload , check ) ;
127
+ ( reject && reject `Not a copySet: ${ s } ` ) ) &&
128
+ confirmElements ( s . payload , reject ) &&
129
+ confirmKey ( s . payload , reject ) ;
137
130
if ( result ) {
138
131
copySetMemo . add ( s ) ;
139
132
}
140
133
return result ;
141
134
} ;
142
- harden ( checkCopySet ) ;
135
+ harden ( confirmCopySet ) ;
143
136
144
137
/**
145
138
* @param {any } s
146
139
* @returns {s is CopySet }
147
140
*/
148
- export const isCopySet = s => checkCopySet ( s , identChecker ) ;
149
- harden ( isCopySet ) ;
141
+ export const isCopySet = s => confirmCopySet ( s , false ) ;
142
+ hideAndHardenFunction ( isCopySet ) ;
150
143
151
144
/**
152
145
* @callback AssertCopySet
@@ -156,9 +149,9 @@ harden(isCopySet);
156
149
157
150
/** @type {AssertCopySet } */
158
151
export const assertCopySet = s => {
159
- checkCopySet ( s , assertChecker ) ;
152
+ confirmCopySet ( s , Fail ) ;
160
153
} ;
161
- harden ( assertCopySet ) ;
154
+ hideAndHardenFunction ( assertCopySet ) ;
162
155
163
156
/**
164
157
* @template {Key} K
@@ -203,31 +196,31 @@ const copyBagMemo = new WeakSet();
203
196
204
197
/**
205
198
* @param {any } b
206
- * @param {Checker } check
199
+ * @param {Rejector } reject
207
200
* @returns {boolean }
208
201
*/
209
- export const checkCopyBag = ( b , check ) => {
202
+ export const confirmCopyBag = ( b , reject ) => {
210
203
if ( copyBagMemo . has ( b ) ) {
211
204
return true ;
212
205
}
213
206
const result =
214
207
( ( passStyleOf ( b ) === 'tagged' && getTag ( b ) === 'copyBag' ) ||
215
- check ( false , X `Not a copyBag: ${ b } ` ) ) &&
216
- checkBagEntries ( b . payload , check ) &&
217
- checkKey ( b . payload , check ) ;
208
+ ( reject && reject `Not a copyBag: ${ b } ` ) ) &&
209
+ confirmBagEntries ( b . payload , reject ) &&
210
+ confirmKey ( b . payload , reject ) ;
218
211
if ( result ) {
219
212
copyBagMemo . add ( b ) ;
220
213
}
221
214
return result ;
222
215
} ;
223
- harden ( checkCopyBag ) ;
216
+ harden ( confirmCopyBag ) ;
224
217
225
218
/**
226
219
* @param {any } b
227
220
* @returns {b is CopyBag }
228
221
*/
229
- export const isCopyBag = b => checkCopyBag ( b , identChecker ) ;
230
- harden ( isCopyBag ) ;
222
+ export const isCopyBag = b => confirmCopyBag ( b , false ) ;
223
+ hideAndHardenFunction ( isCopyBag ) ;
231
224
232
225
/**
233
226
* @callback AssertCopyBag
@@ -237,9 +230,9 @@ harden(isCopyBag);
237
230
238
231
/** @type {AssertCopyBag } */
239
232
export const assertCopyBag = b => {
240
- checkCopyBag ( b , assertChecker ) ;
233
+ confirmCopyBag ( b , Fail ) ;
241
234
} ;
242
- harden ( assertCopyBag ) ;
235
+ hideAndHardenFunction ( assertCopyBag ) ;
243
236
244
237
/**
245
238
* @template {Key} K
@@ -309,58 +302,54 @@ const copyMapMemo = new WeakSet();
309
302
310
303
/**
311
304
* @param {any } m
312
- * @param {Checker } check
305
+ * @param {Rejector } reject
313
306
* @returns {boolean }
314
307
*/
315
- export const checkCopyMap = ( m , check ) => {
308
+ export const confirmCopyMap = ( m , reject ) => {
316
309
if ( copyMapMemo . has ( m ) ) {
317
310
return true ;
318
311
}
319
312
if ( ! ( passStyleOf ( m ) === 'tagged' && getTag ( m ) === 'copyMap' ) ) {
320
- return check ( false , X `Not a copyMap: ${ m } ` ) ;
313
+ return reject && reject `Not a copyMap: ${ m } ` ;
321
314
}
322
315
const { payload } = m ;
323
316
if ( passStyleOf ( payload ) !== 'copyRecord' ) {
324
- return check ( false , X `A copyMap's payload must be a record: ${ m } ` ) ;
317
+ return reject && reject `A copyMap's payload must be a record: ${ m } ` ;
325
318
}
326
319
const { keys, values, ...rest } = payload ;
327
320
const result =
328
321
( ownKeys ( rest ) . length === 0 ||
329
- check (
330
- false ,
331
- X `A copyMap's payload must only have .keys and .values: ${ m } ` ,
332
- ) ) &&
333
- checkElements ( keys , check ) &&
334
- checkKey ( keys , check ) &&
322
+ ( reject &&
323
+ reject `A copyMap's payload must only have .keys and .values: ${ m } ` ) ) &&
324
+ confirmElements ( keys , reject ) &&
325
+ confirmKey ( keys , reject ) &&
335
326
( passStyleOf ( values ) === 'copyArray' ||
336
- check ( false , X `A copyMap's .values must be a copyArray: ${ m } ` ) ) &&
327
+ ( reject && reject `A copyMap's .values must be a copyArray: ${ m } ` ) ) &&
337
328
( keys . length === values . length ||
338
- check (
339
- false ,
340
- X `A copyMap must have the same number of keys and values: ${ m } ` ,
341
- ) ) ;
329
+ ( reject &&
330
+ reject `A copyMap must have the same number of keys and values: ${ m } ` ) ) ;
342
331
if ( result ) {
343
332
copyMapMemo . add ( m ) ;
344
333
}
345
334
return result ;
346
335
} ;
347
- harden ( checkCopyMap ) ;
336
+ harden ( confirmCopyMap ) ;
348
337
349
338
/**
350
339
* @param {any } m
351
340
* @returns {m is CopyMap<Key, Passable> }
352
341
*/
353
- export const isCopyMap = m => checkCopyMap ( m , identChecker ) ;
354
- harden ( isCopyMap ) ;
342
+ export const isCopyMap = m => confirmCopyMap ( m , false ) ;
343
+ hideAndHardenFunction ( isCopyMap ) ;
355
344
356
345
/**
357
346
* @param {Passable } m
358
347
* @returns {asserts m is CopyMap<Key, Passable> }
359
348
*/
360
349
export const assertCopyMap = m => {
361
- checkCopyMap ( m , assertChecker ) ;
350
+ confirmCopyMap ( m , Fail ) ;
362
351
} ;
363
- harden ( assertCopyMap ) ;
352
+ hideAndHardenFunction ( assertCopyMap ) ;
364
353
365
354
/**
366
355
* @template {Key} K
@@ -497,14 +486,14 @@ harden(makeCopyMap);
497
486
// //////////////////////// Keys Recur /////////////////////////////////////////
498
487
499
488
/**
500
- * `checkKeyInternal ` is only called if `val` is Passable but is not an Atom.
489
+ * `confirmKeyInternal ` is only called if `val` is Passable but is not an Atom.
501
490
*
502
491
* @param {any } val
503
- * @param {Checker } check
492
+ * @param {Rejector } reject
504
493
* @returns {boolean }
505
494
*/
506
- const checkKeyInternal = ( val , check ) => {
507
- const checkIt = child => checkKey ( child , check ) ;
495
+ const confirmKeyInternal = ( val , reject ) => {
496
+ const checkIt = child => confirmKey ( child , reject ) ;
508
497
509
498
const passStyle = passStyleOf ( val ) ;
510
499
switch ( passStyle ) {
@@ -524,31 +513,30 @@ const checkKeyInternal = (val, check) => {
524
513
const tag = getTag ( val ) ;
525
514
switch ( tag ) {
526
515
case 'copySet' : {
527
- return checkCopySet ( val , check ) ;
516
+ return confirmCopySet ( val , reject ) ;
528
517
}
529
518
case 'copyBag' : {
530
- return checkCopyBag ( val , check ) ;
519
+ return confirmCopyBag ( val , reject ) ;
531
520
}
532
521
case 'copyMap' : {
533
522
return (
534
- checkCopyMap ( val , check ) &&
523
+ confirmCopyMap ( val , reject ) &&
535
524
// For a copyMap to be a key, all its keys and values must
536
- // be keys. Keys already checked by `checkCopyMap ` since
525
+ // be keys. Keys already checked by `confirmCopyMap ` since
537
526
// that's a copyMap requirement in general.
538
527
everyCopyMapValue ( val , checkIt )
539
528
) ;
540
529
}
541
530
default : {
542
531
return (
543
- check !== identChecker &&
544
- check ( false , X `A passable tagged ${ q ( tag ) } is not a key: ${ val } ` )
532
+ reject && reject `A passable tagged ${ q ( tag ) } is not a key: ${ val } `
545
533
) ;
546
534
}
547
535
}
548
536
}
549
537
case 'error' :
550
538
case 'promise' : {
551
- return check ( false , X `A ${ q ( passStyle ) } cannot be a key` ) ;
539
+ return reject && reject `A ${ q ( passStyle ) } cannot be a key` ;
552
540
}
553
541
default : {
554
542
// Unexpected tags are just non-keys, but an unexpected passStyle
0 commit comments