Skip to content

Commit 8d8375e

Browse files
committed
fixup: review suggestions
1 parent 2910a17 commit 8d8375e

7 files changed

+104
-91
lines changed

packages/marshal/NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ User-visible changes in `@endo/marshal`:
22

33
# Next release
44

5-
- JavaScript's relational comparison operators like `<` compare strings by lexicographic UTF16 code unit order, which is exposes an internal representational detail not relevant to the string's meaning as a Unicode string. Previously, `compareRank` and associated functions compared strings using this JavaScript-native comparison. Now `compareRank` and associated functions compare strings by lexicographic Unicode Code Point order. ***This change only affects strings containing so-called supplementary characters, i.e., those whose Unicode character code does not fit in 16 bits***.
5+
- JavaScript's relational comparison operators like `<` compare strings by lexicographic UTF16 code unit order, which exposes an internal representational detail not relevant to the string's meaning as a Unicode string. Previously, `compareRank` and associated functions compared strings using this JavaScript-native comparison. Now `compareRank` and associated functions compare strings by lexicographic Unicode Code Point order. ***This change only affects strings containing so-called supplementary characters, i.e., those whose Unicode character code does not fit in 16 bits***.
66
- This release does not change the `encodePassable` encoding. But now, when we say it is order preserving, we need to be careful about which order we mean. `encodePassable` is rank-order preserving when the encoded strings are compared using `compareRank`.
77
- The key order of strings defined by the @endo/patterns module is still defined to be the same as the rank ordering of those strings. So this release changes key order among strings to also be lexicographic comparison of Unicode Code Points. To accommodate this change, you may need to adapt applications that relied on key-order being the same as JS native order. This could include the use of any patterns expressing key inequality tests, like `M.gte(string)`.
88
- These string ordering changes brings Endo into conformance with any string ordering components of the OCapN standard.

packages/marshal/src/rankOrder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88

99
/**
1010
* @import {Passable, PassStyle} from '@endo/pass-style'
11-
* @import {FullCompare, RankCompare, RankCover} from './types.js'
11+
* @import {FullCompare, RankCompare, RankCover, RankComparison} from './types.js'
1212
*/
1313

1414
const { entries, fromEntries, setPrototypeOf, is } = Object;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-disable no-bitwise, @endo/restrict-comparison-operands */
2+
import { Fail, q } from '@endo/errors';
3+
4+
import {
5+
makeEncodePassable,
6+
makeDecodePassable,
7+
} from '../src/encodePassable.js';
8+
import { compareRank, makeComparatorKit } from '../src/rankOrder.js';
9+
10+
const buffers = {
11+
__proto__: null,
12+
r: [],
13+
'?': [],
14+
'!': [],
15+
};
16+
const resetBuffers = () => {
17+
buffers.r = [];
18+
buffers['?'] = [];
19+
buffers['!'] = [];
20+
};
21+
const cursors = {
22+
__proto__: null,
23+
r: 0,
24+
'?': 0,
25+
'!': 0,
26+
};
27+
const resetCursors = () => {
28+
cursors.r = 0;
29+
cursors['?'] = 0;
30+
cursors['!'] = 0;
31+
};
32+
33+
const encodeThing = (prefix, r) => {
34+
buffers[prefix].push(r);
35+
// With this encoding, all things with the same prefix have the same rank
36+
return prefix;
37+
};
38+
39+
const decodeThing = (prefix, e) => {
40+
prefix === e ||
41+
Fail`expected encoding ${q(e)} to simply be the prefix ${q(prefix)}`;
42+
(cursors[prefix] >= 0 && cursors[prefix] < buffers[prefix].length) ||
43+
Fail`while decoding ${q(e)}, expected cursors[${q(prefix)}], i.e., ${q(
44+
cursors[prefix],
45+
)} <= ${q(buffers[prefix].length)}`;
46+
const thing = buffers[prefix][cursors[prefix]];
47+
cursors[prefix] += 1;
48+
return thing;
49+
};
50+
51+
const encodePassableInternal = makeEncodePassable({
52+
encodeRemotable: r => encodeThing('r', r),
53+
encodePromise: p => encodeThing('?', p),
54+
encodeError: er => encodeThing('!', er),
55+
});
56+
57+
export const encodePassableInternal2 = makeEncodePassable({
58+
encodeRemotable: r => encodeThing('r', r),
59+
encodePromise: p => encodeThing('?', p),
60+
encodeError: er => encodeThing('!', er),
61+
format: 'compactOrdered',
62+
});
63+
64+
export const encodePassable = passable => {
65+
resetBuffers();
66+
return encodePassableInternal(passable);
67+
};
68+
69+
export const encodePassable2 = passable => {
70+
resetBuffers();
71+
return encodePassableInternal2(passable);
72+
};
73+
export const decodePassableInternal = makeDecodePassable({
74+
decodeRemotable: e => decodeThing('r', e),
75+
decodePromise: e => decodeThing('?', e),
76+
decodeError: e => decodeThing('!', e),
77+
});
78+
79+
export const decodePassable = encoded => {
80+
resetCursors();
81+
return decodePassableInternal(encoded);
82+
};
83+
84+
const compareRemotables = (x, y) =>
85+
compareRank(encodeThing('r', x), encodeThing('r', y));
86+
87+
export const { comparator: compareFull } = makeComparatorKit(compareRemotables);

packages/marshal/test/test-encodePassable.js

+11-84
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,20 @@ import test from '@endo/ses-ava/prepare-endo.js';
55
import { fc } from '@fast-check/ava';
66
import { Remotable } from '@endo/pass-style';
77
import { arbPassable } from '@endo/pass-style/tools.js';
8-
import { Fail, q } from '@endo/errors';
8+
import { Fail } from '@endo/errors';
99

10-
import {
11-
makePassableKit,
12-
makeEncodePassable,
13-
makeDecodePassable,
14-
} from '../src/encodePassable.js';
15-
import { compareRank, makeComparatorKit } from '../src/rankOrder.js';
10+
import { makePassableKit, makeEncodePassable } from '../src/encodePassable.js';
11+
import { compareRank } from '../src/rankOrder.js';
1612
import { unsortedSample } from './marshal-test-data.js';
1713

18-
const buffers = {
19-
__proto__: null,
20-
r: [],
21-
'?': [],
22-
'!': [],
23-
};
24-
const resetBuffers = () => {
25-
buffers.r = [];
26-
buffers['?'] = [];
27-
buffers['!'] = [];
28-
};
29-
const cursors = {
30-
__proto__: null,
31-
r: 0,
32-
'?': 0,
33-
'!': 0,
34-
};
35-
const resetCursors = () => {
36-
cursors.r = 0;
37-
cursors['?'] = 0;
38-
cursors['!'] = 0;
39-
};
40-
41-
const encodeThing = (prefix, r) => {
42-
buffers[prefix].push(r);
43-
// With this encoding, all things with the same prefix have the same rank
44-
return prefix;
45-
};
46-
47-
const decodeThing = (prefix, e) => {
48-
prefix === e ||
49-
Fail`expected encoding ${q(e)} to simply be the prefix ${q(prefix)}`;
50-
(cursors[prefix] >= 0 && cursors[prefix] < buffers[prefix].length) ||
51-
Fail`while decoding ${q(e)}, expected cursors[${q(prefix)}], i.e., ${q(
52-
cursors[prefix],
53-
)} <= ${q(buffers[prefix].length)}`;
54-
const thing = buffers[prefix][cursors[prefix]];
55-
cursors[prefix] += 1;
56-
return thing;
57-
};
58-
59-
const compareRemotables = (x, y) =>
60-
compareRank(encodeThing('r', x), encodeThing('r', y));
61-
62-
const encodePassableInternal = makeEncodePassable({
63-
encodeRemotable: r => encodeThing('r', r),
64-
encodePromise: p => encodeThing('?', p),
65-
encodeError: er => encodeThing('!', er),
66-
});
67-
const encodePassableInternal2 = makeEncodePassable({
68-
encodeRemotable: r => encodeThing('r', r),
69-
encodePromise: p => encodeThing('?', p),
70-
encodeError: er => encodeThing('!', er),
71-
format: 'compactOrdered',
72-
});
73-
74-
export const encodePassable = passable => {
75-
resetBuffers();
76-
return encodePassableInternal(passable);
77-
};
78-
const encodePassable2 = passable => {
79-
resetBuffers();
80-
return encodePassableInternal2(passable);
81-
};
82-
83-
const decodePassableInternal = makeDecodePassable({
84-
decodeRemotable: e => decodeThing('r', e),
85-
decodePromise: e => decodeThing('?', e),
86-
decodeError: e => decodeThing('!', e),
87-
});
88-
89-
export const decodePassable = encoded => {
90-
resetCursors();
91-
return decodePassableInternal(encoded);
92-
};
14+
import {
15+
encodePassable,
16+
encodePassable2,
17+
encodePassableInternal2,
18+
decodePassable,
19+
decodePassableInternal,
20+
compareFull,
21+
} from './encodePassable-for-testing.js';
9322

9423
test('makePassableKit output shape', t => {
9524
const kit = makePassableKit();
@@ -133,8 +62,6 @@ test(
13362
(...args) => makePassableKit(...args).encodePassable,
13463
);
13564

136-
const { comparator: compareFull } = makeComparatorKit(compareRemotables);
137-
13865
const asNumber = new Float64Array(1);
13966
const asBits = new BigUint64Array(asNumber.buffer);
14067
const getNaN = (hexEncoding = '0008000000000000') => {

packages/marshal/test/test-string-rank-order.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { test } from './prepare-test-env-ava.js';
1+
import test from '@endo/ses-ava/prepare-endo.js';
22

33
import { compareRank } from '../src/rankOrder.js';
4-
import { encodePassable } from './test-encodePassable.js';
4+
import { encodePassable } from './encodePassable-for-testing.js';
55

66
/**
77
* Essentially a ponyfill for Array.prototype.toSorted, for use before

packages/patterns/NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ User-visible changes in `@endo/patterns`:
22

33
# Next release
44

5-
- JavaScript's relational comparison operators like `<` compare strings by lexicographic UTF16 code unit order, which is exposes an internal representational detail not relevant to the string's meaning as a Unicode string. Previously, `compareKeys` and associated functions compared strings using this JavaScript-native comparison. Now `compareKeys` and associated functions compare strings by lexicographic Unicode Code Point order. ***This change only affects strings containing so-called supplementary characters, i.e., those whose Unicode character code does not fit in 16 bits***.
5+
- JavaScript's relational comparison operators like `<` compare strings by lexicographic UTF16 code unit order, which exposes an internal representational detail not relevant to the string's meaning as a Unicode string. Previously, `compareKeys` and associated functions compared strings using this JavaScript-native comparison. Now `compareKeys` and associated functions compare strings by lexicographic Unicode Code Point order. ***This change only affects strings containing so-called supplementary characters, i.e., those whose Unicode character code does not fit in 16 bits***.
66
- See the NEWS.md of @endo/marshal for more on this change.
77

88
# v1.2.0 (2024-02-22)

packages/patterns/test/test-string-key-order.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// modeled on test-string-rank-order.js
2-
3-
import { test } from './prepare-test-env-ava.js';
2+
import test from '@endo/ses-ava/prepare-endo.js';
43

54
import { compareKeys } from '../src/keys/compareKeys.js';
65

0 commit comments

Comments
 (0)