Skip to content

Commit

Permalink
fixup: review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Feb 3, 2024
1 parent e636479 commit b6580c5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 79 deletions.
2 changes: 1 addition & 1 deletion packages/marshal/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ User-visible changes in `@endo/marshal`:

# next release

- 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***.
- 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***.
- 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`.
- 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)`.
- These string ordering changes brings Endo into conformance with any string ordering components of the OCapN standard.
Expand Down
76 changes: 76 additions & 0 deletions packages/marshal/test/encodePassable-for-testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable no-bitwise, @endo/restrict-comparison-operands */
import { Fail, q } from '@endo/errors';

import {
makeEncodePassable,
makeDecodePassable,
} from '../src/encodePassable.js';
import { compareRank, makeComparatorKit } from '../src/rankOrder.js';

const buffers = {
__proto__: null,
r: [],
'?': [],
'!': [],
};
const resetBuffers = () => {
buffers.r = [];
buffers['?'] = [];
buffers['!'] = [];
};
const cursors = {
__proto__: null,
r: 0,
'?': 0,
'!': 0,
};
const resetCursors = () => {
cursors.r = 0;
cursors['?'] = 0;
cursors['!'] = 0;
};

const encodeThing = (prefix, r) => {
buffers[prefix].push(r);
// With this encoding, all things with the same prefix have the same rank
return prefix;
};

const decodeThing = (prefix, e) => {
prefix === e ||
Fail`expected encoding ${q(e)} to simply be the prefix ${q(prefix)}`;
(cursors[prefix] >= 0 && cursors[prefix] < buffers[prefix].length) ||
Fail`while decoding ${q(e)}, expected cursors[${q(prefix)}], i.e., ${q(
cursors[prefix],
)} <= ${q(buffers[prefix].length)}`;
const thing = buffers[prefix][cursors[prefix]];
cursors[prefix] += 1;
return thing;
};

const encodePassableInternal = makeEncodePassable({
encodeRemotable: r => encodeThing('r', r),
encodePromise: p => encodeThing('?', p),
encodeError: er => encodeThing('!', er),
});

export const encodePassable = passable => {
resetBuffers();
return encodePassableInternal(passable);
};

const decodePassableInternal = makeDecodePassable({
decodeRemotable: e => decodeThing('r', e),
decodePromise: e => decodeThing('?', e),
decodeError: e => decodeThing('!', e),
});

export const decodePassable = encoded => {
resetCursors();
return decodePassableInternal(encoded);
};

const compareRemotables = (x, y) =>
compareRank(encodeThing('r', x), encodeThing('r', y));

export const { comparator: compareFull } = makeComparatorKit(compareRemotables);
84 changes: 8 additions & 76 deletions packages/marshal/test/test-encodePassable.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,21 @@
// @ts-nocheck
/* eslint-disable no-bitwise, @endo/restrict-comparison-operands */
// eslint-disable-next-line import/order
import { test } from './prepare-test-env-ava.js';

// eslint-disable-next-line import/order
import { fc } from '@fast-check/ava';
import { arbPassable } from '@endo/pass-style/tools.js';
import { Fail, q } from '@endo/errors';
import { Fail } from '@endo/errors';

// eslint-disable-next-line import/no-extraneous-dependencies

import {
makeEncodePassable,
makeDecodePassable,
} from '../src/encodePassable.js';
import { compareRank, makeComparatorKit } from '../src/rankOrder.js';
import { compareRank } from '../src/rankOrder.js';
import { sample } from './test-rankOrder.js';

const buffers = {
__proto__: null,
r: [],
'?': [],
'!': [],
};
const resetBuffers = () => {
buffers.r = [];
buffers['?'] = [];
buffers['!'] = [];
};
const cursors = {
__proto__: null,
r: 0,
'?': 0,
'!': 0,
};
const resetCursors = () => {
cursors.r = 0;
cursors['?'] = 0;
cursors['!'] = 0;
};

const encodeThing = (prefix, r) => {
buffers[prefix].push(r);
// With this encoding, all things with the same prefix have the same rank
return prefix;
};

const decodeThing = (prefix, e) => {
prefix === e ||
Fail`expected encoding ${q(e)} to simply be the prefix ${q(prefix)}`;
(cursors[prefix] >= 0 && cursors[prefix] < buffers[prefix].length) ||
Fail`while decoding ${q(e)}, expected cursors[${q(prefix)}], i.e., ${q(
cursors[prefix],
)} <= ${q(buffers[prefix].length)}`;
const thing = buffers[prefix][cursors[prefix]];
cursors[prefix] += 1;
return thing;
};

const compareRemotables = (x, y) =>
compareRank(encodeThing('r', x), encodeThing('r', y));

const encodePassableInternal = makeEncodePassable({
encodeRemotable: r => encodeThing('r', r),
encodePromise: p => encodeThing('?', p),
encodeError: er => encodeThing('!', er),
});

export const encodePassable = passable => {
resetBuffers();
return encodePassableInternal(passable);
};

const decodePassableInternal = makeDecodePassable({
decodeRemotable: e => decodeThing('r', e),
decodePromise: e => decodeThing('?', e),
decodeError: e => decodeThing('!', e),
});

export const decodePassable = encoded => {
resetCursors();
return decodePassableInternal(encoded);
};

const { comparator: compareFull } = makeComparatorKit(compareRemotables);
import {
encodePassable,
decodePassable,
compareFull,
} from './encodePassable-for-testing.js';

const asNumber = new Float64Array(1);
const asBits = new BigUint64Array(asNumber.buffer);
Expand All @@ -99,7 +31,7 @@ const getNaN = (hexEncoding = '0008000000000000') => {

const NegativeNaN = getNaN('ffffffffffffffff');

/** @type {[Key, string][]} */
/** @type {[number | bigint, string][]} */
const goldenPairs = harden([
[1, 'fbff0000000000000'],
[-1, 'f400fffffffffffff'],
Expand Down
2 changes: 1 addition & 1 deletion packages/marshal/test/test-string-rank-order.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from './prepare-test-env-ava.js';

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

/**
* Essentially a ponyfill for Array.prototype.toSorted, for use before
Expand Down
2 changes: 1 addition & 1 deletion packages/patterns/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ User-visible changes in `@endo/patterns`:

# next release

- 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***.
- 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***.
- See the NEWS.md of @endo/marshal for more on this change.

# v0.2.6 (2023-09-11)
Expand Down

0 comments on commit b6580c5

Please sign in to comment.