2021-01-07 16:41:46 +00:00
|
|
|
// Preserves Values.
|
|
|
|
|
|
|
|
import { PreserveOn, AsPreserve } from './symbols';
|
|
|
|
import { Tag } from './constants';
|
2021-01-29 11:03:28 +00:00
|
|
|
import { Encoder, canonicalEncode, canonicalString, Preservable } from './codec';
|
2021-01-09 15:21:25 +00:00
|
|
|
import { stringify } from './text';
|
2021-01-11 15:54:52 +00:00
|
|
|
import { _iterMap, FlexMap, FlexSet } from './flex';
|
2021-01-07 16:41:46 +00:00
|
|
|
|
|
|
|
const textEncoder = new TextEncoder();
|
|
|
|
const textDecoder = new TextDecoder();
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export type DefaultPointer = object
|
|
|
|
|
|
|
|
export type Value<T extends object = DefaultPointer> = Atom | Compound<T> | T | Annotated<T>;
|
2021-02-17 14:34:31 +00:00
|
|
|
export type Atom = boolean | SingleFloat | DoubleFloat | number | string | Bytes | symbol;
|
2021-01-29 14:35:07 +00:00
|
|
|
export type Compound<T extends object = DefaultPointer> = Record<T> | Array<Value<T>> | Set<T> | Dictionary<Value<T>, T>;
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-11 15:54:52 +00:00
|
|
|
export const IsPreservesRecord = Symbol.for('IsPreservesRecord');
|
|
|
|
export const IsPreservesBytes = Symbol.for('IsPreservesBytes');
|
|
|
|
export const IsPreservesAnnotated = Symbol.for('IsPreservesAnnotated');
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export function fromJS<T extends object = DefaultPointer>(x: any): Value<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
switch (typeof x) {
|
|
|
|
case 'number':
|
|
|
|
if (!Number.isInteger(x)) {
|
|
|
|
// We require that clients be explicit about integer vs. non-integer types.
|
|
|
|
throw new TypeError("Refusing to autoconvert non-integer number to Single or Double");
|
|
|
|
}
|
|
|
|
// FALL THROUGH
|
|
|
|
case 'string':
|
|
|
|
case 'symbol':
|
|
|
|
case 'boolean':
|
|
|
|
return x;
|
|
|
|
|
|
|
|
case 'undefined':
|
|
|
|
case 'function':
|
2021-01-29 11:03:28 +00:00
|
|
|
case 'bigint':
|
2021-01-07 16:41:46 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'object':
|
|
|
|
if (x === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (typeof x[AsPreserve] === 'function') {
|
|
|
|
return x[AsPreserve]();
|
|
|
|
}
|
2021-01-29 11:03:28 +00:00
|
|
|
if (Record.isRecord<T>(x)) {
|
2021-01-07 16:41:46 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (Array.isArray(x)) {
|
2021-01-29 11:03:28 +00:00
|
|
|
return (x as Array<Value<T>>).map<Value<T>>(fromJS);
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
if (ArrayBuffer.isView(x) || x instanceof ArrayBuffer) {
|
|
|
|
return Bytes.from(x);
|
|
|
|
}
|
2021-01-29 11:03:28 +00:00
|
|
|
// Just... assume it's a T.
|
|
|
|
return (x as T);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new TypeError("Cannot represent JavaScript value as Preserves: " + x);
|
|
|
|
}
|
|
|
|
|
|
|
|
export type FloatType = 'Single' | 'Double';
|
2021-01-11 15:54:52 +00:00
|
|
|
export const FloatType = Symbol.for('FloatType');
|
2021-01-07 16:41:46 +00:00
|
|
|
|
|
|
|
export abstract class Float {
|
|
|
|
readonly value: number;
|
|
|
|
|
|
|
|
constructor(value: number | Float) {
|
|
|
|
this.value = typeof value === 'number' ? value : value.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return this.asPreservesText();
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(other: any): boolean {
|
|
|
|
return Object.is(other.constructor, this.constructor) && (other.value === this.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashCode(): number {
|
|
|
|
return (this.value | 0); // TODO: something better?
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract asPreservesText(): string;
|
2021-01-11 15:54:52 +00:00
|
|
|
abstract get [FloatType](): FloatType;
|
|
|
|
|
|
|
|
static isFloat(x: any, t: FloatType): x is Float {
|
|
|
|
return (x?.[FloatType] === t);
|
|
|
|
}
|
|
|
|
|
2021-02-17 14:34:31 +00:00
|
|
|
static isSingle = (x: any): x is SingleFloat => Float.isFloat(x, 'Single');
|
|
|
|
static isDouble = (x: any): x is DoubleFloat => Float.isFloat(x, 'Double');
|
|
|
|
}
|
2021-01-29 15:06:00 +00:00
|
|
|
|
2021-02-17 14:34:31 +00:00
|
|
|
export function floatValue(f: any): number {
|
|
|
|
if (typeof f === 'number') {
|
|
|
|
return f;
|
|
|
|
} else if (f?.[FloatType] !== void 0) {
|
|
|
|
return f.value;
|
|
|
|
} else {
|
|
|
|
return NaN;
|
2021-01-29 15:06:00 +00:00
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 14:34:31 +00:00
|
|
|
export class SingleFloat extends Float implements Preservable<never> {
|
2021-01-29 14:35:07 +00:00
|
|
|
[AsPreserve]<T extends object = DefaultPointer>(): Value<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
[PreserveOn](encoder: Encoder<never>) {
|
2021-01-07 16:41:46 +00:00
|
|
|
encoder.emitbyte(Tag.Float);
|
|
|
|
encoder.makeroom(4);
|
|
|
|
encoder.view.setFloat32(encoder.index, this.value, false);
|
|
|
|
encoder.index += 4;
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:54:52 +00:00
|
|
|
get [FloatType](): FloatType {
|
2021-01-07 16:41:46 +00:00
|
|
|
return 'Single';
|
|
|
|
}
|
|
|
|
|
|
|
|
asPreservesText(): string {
|
|
|
|
return '' + this.value + 'f';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 14:34:31 +00:00
|
|
|
export function Single(value: number | Float) {
|
|
|
|
return new SingleFloat(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DoubleFloat extends Float implements Preservable<never> {
|
2021-01-29 14:35:07 +00:00
|
|
|
[AsPreserve]<T extends object = DefaultPointer>(): Value<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
[PreserveOn](encoder: Encoder<never>) {
|
2021-01-07 16:41:46 +00:00
|
|
|
encoder.emitbyte(Tag.Double);
|
|
|
|
encoder.makeroom(8);
|
|
|
|
encoder.view.setFloat64(encoder.index, this.value, false);
|
|
|
|
encoder.index += 8;
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:54:52 +00:00
|
|
|
get [FloatType](): FloatType {
|
2021-01-07 16:41:46 +00:00
|
|
|
return 'Double';
|
|
|
|
}
|
|
|
|
|
|
|
|
asPreservesText(): string {
|
|
|
|
return '' + this.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 14:34:31 +00:00
|
|
|
export function Double(value: number | Float) {
|
|
|
|
return new DoubleFloat(value);
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:41:46 +00:00
|
|
|
export type BytesLike = Bytes | Uint8Array;
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
export class Bytes implements Preservable<never> {
|
2021-01-07 16:41:46 +00:00
|
|
|
readonly _view: Uint8Array;
|
|
|
|
|
|
|
|
constructor(maybeByteIterable: any = new Uint8Array()) {
|
2021-01-11 15:54:52 +00:00
|
|
|
if (Bytes.isBytes(maybeByteIterable)) {
|
2021-01-07 16:41:46 +00:00
|
|
|
this._view = maybeByteIterable._view;
|
|
|
|
} else if (ArrayBuffer.isView(maybeByteIterable)) {
|
|
|
|
this._view = new Uint8Array(maybeByteIterable.buffer,
|
|
|
|
maybeByteIterable.byteOffset,
|
|
|
|
maybeByteIterable.byteLength);
|
|
|
|
} else if (maybeByteIterable instanceof ArrayBuffer) {
|
2021-01-12 13:18:26 +00:00
|
|
|
this._view = new Uint8Array(maybeByteIterable);
|
2021-01-07 16:41:46 +00:00
|
|
|
} else if (typeof maybeByteIterable === 'string') {
|
|
|
|
this._view = textEncoder.encode(maybeByteIterable);
|
|
|
|
} else if (typeof maybeByteIterable === 'number') {
|
|
|
|
this._view = new Uint8Array(maybeByteIterable);
|
|
|
|
} else if (typeof maybeByteIterable.length === 'number') {
|
|
|
|
this._view = Uint8Array.from(maybeByteIterable);
|
|
|
|
} else {
|
|
|
|
throw new TypeError("Attempt to initialize Bytes from unsupported value: " +
|
|
|
|
maybeByteIterable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get length(): number {
|
|
|
|
return this._view.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static from(x: any): Bytes {
|
|
|
|
return new Bytes(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static of(...bytes: number[]): Bytes {
|
|
|
|
return new Bytes(Uint8Array.of(...bytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromHex(s: string): Bytes {
|
|
|
|
if (s.length & 1) throw new Error("Cannot decode odd-length hexadecimal string");
|
|
|
|
const len = s.length >> 1;
|
|
|
|
const result = new Bytes(len);
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
result._view[i] =
|
|
|
|
(unhexDigit(s.charCodeAt(i << 1)) << 4) | unhexDigit(s.charCodeAt((i << 1) + 1));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromIO(io: string | BytesLike): string | Bytes {
|
|
|
|
if (typeof io === 'string') return io;
|
2021-01-11 15:54:52 +00:00
|
|
|
if (Bytes.isBytes(io)) return io;
|
2021-01-15 13:01:14 +00:00
|
|
|
return new Bytes(io);
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static toIO(b : string | BytesLike): string | Uint8Array {
|
|
|
|
if (typeof b === 'string') return b;
|
2021-01-11 15:54:52 +00:00
|
|
|
if (Bytes.isBytes(b)) return b._view;
|
2021-01-15 13:01:14 +00:00
|
|
|
return b;
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static concat = function (bss: BytesLike[]): Bytes {
|
|
|
|
let len = 0;
|
|
|
|
for (let i = 0; i < bss.length; i++) { len += underlying(bss[i]).length; }
|
|
|
|
|
|
|
|
const result = new Bytes(len);
|
|
|
|
let index = 0;
|
|
|
|
for (let i = 0; i < bss.length; i++) {
|
|
|
|
const bs = underlying(bss[i]);
|
|
|
|
result._view.set(bs, index);
|
|
|
|
index += bs.length;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(index: number): number {
|
|
|
|
return this._view[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(other: any): boolean {
|
2021-01-11 15:54:52 +00:00
|
|
|
if (!Bytes.isBytes(other)) return false;
|
2021-01-07 16:41:46 +00:00
|
|
|
if (other.length !== this.length) return false;
|
|
|
|
const va = this._view;
|
|
|
|
const vb = other._view;
|
|
|
|
for (let i = 0; i < va.length; i++) {
|
|
|
|
if (va[i] !== vb[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
hashCode(): number {
|
|
|
|
// Immutable.js uses this function for strings.
|
|
|
|
const v = this._view;
|
|
|
|
let hash = 0;
|
|
|
|
for (let i = 0; i < v.length; i++) {
|
|
|
|
hash = ((31 * hash) + v[i]) | 0;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static compare(a: Bytes, b: Bytes): number {
|
|
|
|
if (a < b) return -1;
|
|
|
|
if (b < a) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static decodeUtf8(bs: Bytes | Uint8Array): string {
|
|
|
|
return textDecoder.decode(underlying(bs));
|
|
|
|
}
|
|
|
|
|
|
|
|
fromUtf8(): string {
|
|
|
|
return textDecoder.decode(this._view);
|
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return this.asPreservesText();
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
[AsPreserve]<T extends object = DefaultPointer>(): Value<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
asPreservesText(): string {
|
|
|
|
return '#"' + this.__asciify() + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
__asciify(): string {
|
|
|
|
const pieces = [];
|
|
|
|
const v = this._view;
|
|
|
|
for (let i = 0; i < v.length; i++) {
|
|
|
|
const b = v[i];
|
|
|
|
if (b === 92 || b === 34) {
|
|
|
|
pieces.push('\\' + String.fromCharCode(b));
|
|
|
|
} else if (b >= 32 && b <= 126) {
|
|
|
|
pieces.push(String.fromCharCode(b));
|
|
|
|
} else {
|
|
|
|
pieces.push('\\x' + hexDigit(b >> 4) + hexDigit(b & 15));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pieces.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
toHex(): string {
|
|
|
|
var nibbles = [];
|
|
|
|
for (let i = 0; i < this.length; i++) {
|
|
|
|
nibbles.push(hexDigit(this._view[i] >> 4));
|
|
|
|
nibbles.push(hexDigit(this._view[i] & 15));
|
|
|
|
}
|
|
|
|
return nibbles.join('');
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
[PreserveOn](encoder: Encoder<never>) {
|
2021-01-07 16:41:46 +00:00
|
|
|
encoder.emitbyte(Tag.ByteString);
|
|
|
|
encoder.varint(this.length);
|
|
|
|
encoder.emitbytes(this._view);
|
|
|
|
}
|
2021-01-11 15:54:52 +00:00
|
|
|
|
|
|
|
get [IsPreservesBytes](): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isBytes(x: any): x is Bytes {
|
|
|
|
return !!x?.[IsPreservesBytes];
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function hexDigit(n: number): string {
|
|
|
|
return '0123456789abcdef'[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unhexDigit(asciiCode: number) {
|
|
|
|
if (asciiCode >= 48 && asciiCode <= 57) return asciiCode - 48;
|
|
|
|
if (asciiCode >= 97 && asciiCode <= 102) return asciiCode - 97 + 10;
|
|
|
|
if (asciiCode >= 65 && asciiCode <= 70) return asciiCode - 65 + 10;
|
|
|
|
throw new Error("Invalid hex digit: " + String.fromCharCode(asciiCode));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function underlying(b: Bytes | Uint8Array): Uint8Array {
|
|
|
|
return (b instanceof Uint8Array) ? b : b._view;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare global {
|
2021-01-29 11:03:28 +00:00
|
|
|
interface Object { asPreservesText(): string; }
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
Object.defineProperty(Object.prototype, 'asPreservesText', {
|
|
|
|
enumerable: false,
|
|
|
|
writable: true,
|
|
|
|
value: function(): string { return '#!' + JSON.stringify(this); }
|
|
|
|
});
|
|
|
|
|
2021-01-07 16:41:46 +00:00
|
|
|
Boolean.prototype.asPreservesText = function (): string {
|
|
|
|
return this ? '#t' : '#f';
|
|
|
|
};
|
|
|
|
|
|
|
|
Number.prototype.asPreservesText = function (): string {
|
|
|
|
return '' + this;
|
|
|
|
};
|
|
|
|
|
|
|
|
String.prototype.asPreservesText = function (): string {
|
|
|
|
return JSON.stringify(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Symbol.prototype.asPreservesText = function (): string {
|
|
|
|
// TODO: escaping
|
2021-01-15 13:01:14 +00:00
|
|
|
return this.description ?? '||';
|
2021-01-07 16:41:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Array.prototype.asPreservesText = function (): string {
|
2021-01-29 11:03:28 +00:00
|
|
|
return '[' + this.map((i: Value<any>) => i.asPreservesText()).join(', ') + ']';
|
2021-01-07 16:41:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Uint8Array / TypedArray methods
|
|
|
|
|
|
|
|
export interface Bytes {
|
|
|
|
entries(): IterableIterator<[number, number]>;
|
|
|
|
every(predicate: (value: number, index: number, array: Uint8Array) => unknown,
|
|
|
|
thisArg?: any): boolean;
|
|
|
|
find(predicate: (value: number, index: number, obj: Uint8Array) => boolean,
|
|
|
|
thisArg?: any): number;
|
|
|
|
findIndex(predicate: (value: number, index: number, obj: Uint8Array) => boolean,
|
|
|
|
thisArg?: any): number;
|
|
|
|
forEach(callbackfn: (value: number, index: number, array: Uint8Array) => void,
|
|
|
|
thisArg?: any): void;
|
|
|
|
includes(searchElement: number, fromIndex?: number): boolean;
|
|
|
|
indexOf(searchElement: number, fromIndex?: number): number;
|
|
|
|
join(separator?: string): string;
|
|
|
|
keys(): IterableIterator<number>;
|
|
|
|
lastIndexOf(searchElement: number, fromIndex?: number): number;
|
|
|
|
reduce(callbackfn: (previousValue: number,
|
|
|
|
currentValue: number,
|
|
|
|
currentIndex: number,
|
|
|
|
array: Uint8Array) => number,
|
|
|
|
initialValue?: number): number;
|
|
|
|
reduceRight(callbackfn: (previousValue: number,
|
|
|
|
currentValue: number,
|
|
|
|
currentIndex: number,
|
|
|
|
array: Uint8Array) => number,
|
|
|
|
initialValue?: number): number;
|
|
|
|
some(predicate: (value: number, index: number, array: Uint8Array) => unknown,
|
|
|
|
thisArg?: any): boolean;
|
|
|
|
toLocaleString(): string;
|
|
|
|
values(): IterableIterator<number>;
|
|
|
|
|
|
|
|
filter(predicate: (value: number, index: number, array: Uint8Array) => any,
|
|
|
|
thisArg?: any): Bytes;
|
|
|
|
map(callbackfn: (value: number, index: number, array: Uint8Array) => number,
|
|
|
|
thisArg?: any): Bytes;
|
|
|
|
slice(start?: number, end?: number): Bytes;
|
|
|
|
subarray(begin?: number, end?: number): Bytes;
|
|
|
|
|
|
|
|
reverse(): Bytes;
|
|
|
|
sort(compareFn?: (a: number, b: number) => number): Bytes;
|
|
|
|
|
|
|
|
[Symbol.iterator](): IterableIterator<number>;
|
|
|
|
}
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
for (const k of `entries every find findIndex forEach includes indexOf join
|
|
|
|
keys lastIndexOf reduce reduceRight some toLocaleString values`.split(/\s+/))
|
|
|
|
{
|
2021-01-15 13:01:14 +00:00
|
|
|
(Bytes as any).prototype[k] =
|
|
|
|
function (...args: any[]) { return this._view[k](...args); };
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const k of `filter map slice subarray`.split(/\s+/))
|
|
|
|
{
|
2021-01-15 13:01:14 +00:00
|
|
|
(Bytes as any).prototype[k] =
|
|
|
|
function (...args: any[]) { return new Bytes(this._view[k](...args)); };
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const k of `reverse sort`.split(/\s+/))
|
|
|
|
{
|
2021-01-15 13:01:14 +00:00
|
|
|
(Bytes as any).prototype[k] =
|
|
|
|
function (...args: any[]) { return new Bytes(this._view.slice()[k](...args)); };
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Bytes.prototype[Symbol.iterator] = function () { return this._view[Symbol.iterator](); };
|
|
|
|
})();
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export class Record<T extends object = DefaultPointer> extends Array<Value<T>> {
|
2021-01-29 11:03:28 +00:00
|
|
|
readonly label: Value<T>;
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
constructor(label: Value<T>, fieldsJS: any[]) {
|
2021-01-07 16:41:46 +00:00
|
|
|
if (arguments.length === 1) {
|
|
|
|
// Using things like someRecord.map() involves the runtime
|
|
|
|
// apparently instantiating instances of this.constructor
|
|
|
|
// as if it were just plain old Array, so we have to be
|
|
|
|
// somewhat calling-convention-compatible. This is
|
|
|
|
// something that isn't part of the user-facing API.
|
|
|
|
super(label);
|
2021-01-15 13:01:14 +00:00
|
|
|
this.label = label; // needed just to keep the typechecker happy
|
2021-01-07 16:41:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
super(fieldsJS.length);
|
|
|
|
fieldsJS.forEach((f, i) => this[i] = fromJS(f));
|
|
|
|
this.label = label;
|
|
|
|
Object.freeze(this);
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
get(index: number, defaultValue?: Value<T>): Value<T> | undefined {
|
2021-01-07 16:41:46 +00:00
|
|
|
return (index < this.length) ? this[index] : defaultValue;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
set(index: number, newValue: Value<T>): Record<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
return new Record(this.label, this.map((f, i) => (i === index) ? newValue : f));
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
getConstructorInfo(): RecordConstructorInfo<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
return { label: this.label, arity: this.length };
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(other: any): boolean {
|
2021-01-11 15:54:52 +00:00
|
|
|
return Record.isRecord(other) &&
|
2021-01-07 16:41:46 +00:00
|
|
|
is(this.label, other.label) &&
|
|
|
|
this.every((f, i) => is(f, other.get(i)));
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
// hashCode(): number {
|
|
|
|
// let h = hash(this.label);
|
|
|
|
// this.forEach((f) => h = ((31 * h) + hash(f)) | 0);
|
|
|
|
// return h;
|
|
|
|
// }
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
static fallbackToString: (f: Value<any>) => string = (_f) => '<unprintable_preserves_field_value>';
|
2021-01-07 16:41:46 +00:00
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return this.asPreservesText();
|
|
|
|
}
|
|
|
|
|
|
|
|
asPreservesText(): string {
|
|
|
|
if (!('label' in this)) {
|
|
|
|
// A quasi-Array from someRecord.map() or similar. See constructor.
|
|
|
|
return super.toString();
|
|
|
|
}
|
|
|
|
return this.label.asPreservesText() +
|
|
|
|
'(' + this.map((f) => {
|
|
|
|
try {
|
|
|
|
return f.asPreservesText();
|
|
|
|
} catch (e) {
|
|
|
|
return Record.fallbackToString(f);
|
|
|
|
}
|
|
|
|
}).join(', ') + ')';
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
static makeConstructor<T extends object = DefaultPointer>(labelSymbolText: string, fieldNames: string[]): RecordConstructor<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
return Record.makeBasicConstructor(Symbol.for(labelSymbolText), fieldNames);
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
static makeBasicConstructor<T extends object = DefaultPointer>(label0: any, fieldNames: string[]): RecordConstructor<T> {
|
2021-01-29 11:03:28 +00:00
|
|
|
const label = fromJS<T>(label0);
|
2021-01-07 16:41:46 +00:00
|
|
|
const arity = fieldNames.length;
|
2021-01-29 11:03:28 +00:00
|
|
|
const ctor: RecordConstructor<T> = (...fields: any[]): Record<T> => {
|
2021-01-07 16:41:46 +00:00
|
|
|
if (fields.length !== arity) {
|
|
|
|
throw new Error("Record: cannot instantiate " + (label && label.toString()) +
|
|
|
|
" expecting " + arity + " fields with " + fields.length + " fields");
|
|
|
|
}
|
2021-01-29 11:03:28 +00:00
|
|
|
return new Record<T>(label, fields);
|
2021-01-07 16:41:46 +00:00
|
|
|
};
|
2021-01-11 15:54:52 +00:00
|
|
|
const constructorInfo = { label, arity };
|
|
|
|
ctor.constructorInfo = constructorInfo;
|
2021-01-29 11:03:28 +00:00
|
|
|
ctor.isClassOf = (v: any): v is Record<T> => Record.isClassOf(constructorInfo, v);
|
2021-01-07 16:41:46 +00:00
|
|
|
ctor._ = {};
|
|
|
|
fieldNames.forEach((name, i) => {
|
2021-01-29 11:03:28 +00:00
|
|
|
ctor._[name] = function (r: any): Value<T> | undefined {
|
2021-01-07 16:41:46 +00:00
|
|
|
if (!ctor.isClassOf(r)) {
|
|
|
|
throw new Error("Record: attempt to retrieve field "+label.toString()+"."+name+
|
|
|
|
" from non-"+label.toString()+": "+(r && r.toString()));
|
|
|
|
}
|
|
|
|
return r.get(i);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return ctor;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
[PreserveOn](encoder: Encoder<T>) {
|
2021-01-07 16:41:46 +00:00
|
|
|
encoder.emitbyte(Tag.Record);
|
|
|
|
encoder.push(this.label);
|
|
|
|
this.forEach((f) => encoder.push(f));
|
|
|
|
encoder.emitbyte(Tag.End);
|
|
|
|
}
|
2021-01-11 15:54:52 +00:00
|
|
|
|
|
|
|
get [IsPreservesRecord](): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
static isRecord<T extends object = DefaultPointer>(x: any): x is Record<T> {
|
2021-01-11 15:54:52 +00:00
|
|
|
return !!x?.[IsPreservesRecord];
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
static isClassOf<T extends object = DefaultPointer>(ci: RecordConstructorInfo<T>, v: any): v is Record<T> {
|
2021-01-11 15:54:52 +00:00
|
|
|
return (Record.isRecord(v)) && is(ci.label, v.label) && (ci.arity === v.length);
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export interface RecordConstructor<T extends object = DefaultPointer> {
|
2021-01-29 11:03:28 +00:00
|
|
|
(...fields: any[]): Record<T>;
|
|
|
|
constructorInfo: RecordConstructorInfo<T>;
|
|
|
|
isClassOf(v: any): v is Record<T>;
|
|
|
|
_: { [getter: string]: (r: any) => Value<T> | undefined };
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export interface RecordConstructorInfo<T extends object = DefaultPointer> {
|
2021-01-29 11:03:28 +00:00
|
|
|
label: Value<T>;
|
2021-01-07 16:41:46 +00:00
|
|
|
arity: number;
|
|
|
|
}
|
|
|
|
|
2021-01-09 15:21:25 +00:00
|
|
|
export function is(a: any, b: any): boolean {
|
2021-01-11 15:54:52 +00:00
|
|
|
if (Annotated.isAnnotated(a)) a = a.item;
|
|
|
|
if (Annotated.isAnnotated(b)) b = b.item;
|
2021-01-07 16:41:46 +00:00
|
|
|
if (Object.is(a, b)) return true;
|
|
|
|
if (typeof a !== typeof b) return false;
|
|
|
|
if (typeof a === 'object') {
|
|
|
|
if (a === null || b === null) return false;
|
2021-01-29 11:03:28 +00:00
|
|
|
if ('equals' in a && typeof a.equals === 'function') return a.equals(b, is);
|
2021-01-07 16:41:46 +00:00
|
|
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
|
|
if (a.length !== b.length) return false;
|
|
|
|
for (let i = 0; i < a.length; i++) if (!is(a[i], b[i])) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type DictionaryType = 'Dictionary' | 'Set';
|
2021-01-11 15:54:52 +00:00
|
|
|
export const DictionaryType = Symbol.for('DictionaryType');
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export class Dictionary<V, T extends object = DefaultPointer> extends FlexMap<Value<T>, V> {
|
2021-01-11 15:54:52 +00:00
|
|
|
get [DictionaryType](): DictionaryType {
|
|
|
|
return 'Dictionary';
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
static isDictionary<V, T extends object = DefaultPointer>(x: any): x is Dictionary<V, T> {
|
2021-01-11 15:54:52 +00:00
|
|
|
return x?.[DictionaryType] === 'Dictionary';
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
static fromJS<V extends object = DefaultPointer, T extends object = DefaultPointer>(x: object): Dictionary<Value<V>, T> {
|
|
|
|
if (Dictionary.isDictionary<V, T>(x)) return x as Dictionary<Value<V>, T>;
|
|
|
|
const d = new Dictionary<Value<V>, T>();
|
2021-01-15 13:01:14 +00:00
|
|
|
Object.entries(x).forEach(([key, value]) => d.set(key, fromJS(value)));
|
2021-01-07 16:41:46 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
constructor(items?: Iterable<readonly [any, V]>) {
|
2021-01-15 13:01:14 +00:00
|
|
|
const iter = items?.[Symbol.iterator]();
|
|
|
|
super(canonicalString, iter === void 0 ? void 0 : _iterMap(iter, ([k,v]) => [fromJS(k), v]));
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
mapEntries<W, R extends object = DefaultPointer>(f: (entry: [Value<T>, V]) => [Value<R>, W]): Dictionary<W, R> {
|
|
|
|
const result = new Dictionary<W, R>();
|
2021-01-09 15:21:25 +00:00
|
|
|
for (let oldEntry of this.entries()) {
|
2021-01-07 16:41:46 +00:00
|
|
|
const newEntry = f(oldEntry);
|
2021-01-09 15:21:25 +00:00
|
|
|
result.set(newEntry[0], newEntry[1])
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
asPreservesText(): string {
|
|
|
|
return '{' +
|
2021-01-09 15:21:25 +00:00
|
|
|
Array.from(_iterMap(this.entries(), ([k, v]) =>
|
|
|
|
k.asPreservesText() + ': ' + stringify(v))).join(', ') +
|
2021-01-07 16:41:46 +00:00
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
clone(): Dictionary<V, T> {
|
2021-01-09 15:21:25 +00:00
|
|
|
return new Dictionary(this);
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 15:21:25 +00:00
|
|
|
toString(): string {
|
|
|
|
return this.asPreservesText();
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 15:33:56 +00:00
|
|
|
get [Symbol.toStringTag]() { return 'Dictionary'; }
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
[PreserveOn](encoder: Encoder<T>) {
|
2021-01-09 15:21:25 +00:00
|
|
|
if (encoder.canonical) {
|
|
|
|
const pieces = Array.from(this).map(([k, v]) =>
|
2021-01-12 13:18:26 +00:00
|
|
|
Bytes.concat([canonicalEncode(k), canonicalEncode(v)]));
|
2021-01-09 15:21:25 +00:00
|
|
|
pieces.sort(Bytes.compare);
|
|
|
|
encoder.encoderawvalues(Tag.Dictionary, pieces);
|
|
|
|
} else {
|
|
|
|
encoder.emitbyte(Tag.Dictionary);
|
|
|
|
this.forEach((v, k) => {
|
|
|
|
encoder.push(k);
|
2021-01-29 11:03:28 +00:00
|
|
|
encoder.push(v as unknown as Value<T>); // Suuuuuuuper unsound
|
2021-01-09 15:21:25 +00:00
|
|
|
});
|
|
|
|
encoder.emitbyte(Tag.End);
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
2021-01-09 15:21:25 +00:00
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export class Set<T extends object = DefaultPointer> extends FlexSet<Value<T>> {
|
2021-01-11 15:54:52 +00:00
|
|
|
get [DictionaryType](): DictionaryType {
|
|
|
|
return 'Set';
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
static isSet<T extends object = DefaultPointer>(x: any): x is Set<T> {
|
2021-01-11 15:54:52 +00:00
|
|
|
return x?.[DictionaryType] === 'Set';
|
|
|
|
}
|
|
|
|
|
2021-01-09 15:21:25 +00:00
|
|
|
constructor(items?: Iterable<any>) {
|
2021-01-15 13:01:14 +00:00
|
|
|
const iter = items?.[Symbol.iterator]();
|
2021-01-29 11:03:28 +00:00
|
|
|
super(canonicalString, iter === void 0 ? void 0 : _iterMap<any, Value<T>>(iter, fromJS));
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
map<R extends object = DefaultPointer>(f: (value: Value<T>) => Value<R>): Set<R> {
|
2021-01-09 15:21:25 +00:00
|
|
|
return new Set(_iterMap(this[Symbol.iterator](), f));
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
filter(f: (value: Value<T>) => boolean): Set<T> {
|
|
|
|
const result = new Set<T>();
|
2021-01-09 15:21:25 +00:00
|
|
|
for (let k of this) if (f(k)) result.add(k);
|
2021-01-07 16:41:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-12 13:18:02 +00:00
|
|
|
toString(): string {
|
|
|
|
return this.asPreservesText();
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:41:46 +00:00
|
|
|
asPreservesText(): string {
|
|
|
|
return '#{' +
|
2021-01-09 15:21:25 +00:00
|
|
|
Array.from(_iterMap(this.values(), v => v.asPreservesText())).join(', ') +
|
2021-01-07 16:41:46 +00:00
|
|
|
'}';
|
|
|
|
}
|
2021-01-09 15:21:25 +00:00
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
clone(): Set<T> {
|
2021-01-09 15:21:25 +00:00
|
|
|
return new Set(this);
|
|
|
|
}
|
|
|
|
|
2021-01-09 15:33:56 +00:00
|
|
|
get [Symbol.toStringTag]() { return 'Set'; }
|
2021-01-09 15:21:25 +00:00
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
[PreserveOn](encoder: Encoder<T>) {
|
2021-01-09 15:21:25 +00:00
|
|
|
if (encoder.canonical) {
|
2021-01-12 13:18:26 +00:00
|
|
|
const pieces = Array.from(this).map(k => canonicalEncode(k));
|
2021-01-09 15:21:25 +00:00
|
|
|
pieces.sort(Bytes.compare);
|
|
|
|
encoder.encoderawvalues(Tag.Set, pieces);
|
|
|
|
} else {
|
|
|
|
encoder.encodevalues(Tag.Set, this);
|
|
|
|
}
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export class Annotated<T extends object = DefaultPointer> {
|
2021-01-29 11:03:28 +00:00
|
|
|
readonly annotations: Array<Value<T>>;
|
|
|
|
readonly item: Value<T>;
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
constructor(item: Value<T>) {
|
2021-01-07 16:41:46 +00:00
|
|
|
this.annotations = [];
|
|
|
|
this.item = item;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
[AsPreserve](): Value<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
[PreserveOn](encoder: Encoder<T>) {
|
2021-01-07 16:41:46 +00:00
|
|
|
if (encoder.includeAnnotations) {
|
|
|
|
for (const a of this.annotations) {
|
|
|
|
encoder.emitbyte(Tag.Annotation);
|
|
|
|
encoder.push(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
encoder.push(this.item);
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(other: any): boolean {
|
2021-01-11 15:54:52 +00:00
|
|
|
return is(this.item, Annotated.isAnnotated(other) ? other.item : other);
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
// hashCode(): number {
|
|
|
|
// return hash(this.item);
|
|
|
|
// }
|
2021-01-07 16:41:46 +00:00
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return this.asPreservesText();
|
|
|
|
}
|
|
|
|
|
|
|
|
asPreservesText(): string {
|
|
|
|
const anns = this.annotations.map((a) => '@' + a.asPreservesText()).join(' ');
|
|
|
|
return (anns ? anns + ' ' : anns) + this.item.asPreservesText();
|
|
|
|
}
|
2021-01-11 15:54:52 +00:00
|
|
|
|
|
|
|
get [IsPreservesAnnotated](): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
static isAnnotated<T extends object = DefaultPointer>(x: any): x is Annotated<T> {
|
2021-01-11 15:54:52 +00:00
|
|
|
return !!x?.[IsPreservesAnnotated];
|
|
|
|
}
|
2021-01-07 16:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export function peel<T extends object = DefaultPointer>(v: Value<T>): Value<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
return strip(v, 1);
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export function strip<T extends object = DefaultPointer>(v: Value<T>, depth: number = Infinity): Value<T> {
|
2021-01-29 11:03:28 +00:00
|
|
|
function step(v: Value<T>, depth: number): Value<T> {
|
2021-01-07 16:41:46 +00:00
|
|
|
if (depth === 0) return v;
|
2021-01-29 11:03:28 +00:00
|
|
|
if (!Annotated.isAnnotated<T>(v)) return v;
|
2021-01-07 16:41:46 +00:00
|
|
|
|
|
|
|
const nextDepth = depth - 1;
|
2021-01-29 11:03:28 +00:00
|
|
|
function walk(v: Value<T>): Value<T> { return step(v, nextDepth); }
|
2021-01-07 16:41:46 +00:00
|
|
|
|
2021-01-29 11:03:28 +00:00
|
|
|
if (Record.isRecord<T>(v.item)) {
|
2021-01-07 16:41:46 +00:00
|
|
|
return new Record(step(v.item.label, depth), v.item.map(walk));
|
|
|
|
} else if (Array.isArray(v.item)) {
|
|
|
|
return v.item.map(walk);
|
2021-01-29 11:03:28 +00:00
|
|
|
} else if (Set.isSet<T>(v.item)) {
|
2021-01-07 16:41:46 +00:00
|
|
|
return v.item.map(walk);
|
2021-01-29 14:35:07 +00:00
|
|
|
} else if (Dictionary.isDictionary<Value<T>, T>(v.item)) {
|
2021-01-07 16:41:46 +00:00
|
|
|
return v.item.mapEntries((e) => [walk(e[0]), walk(e[1])]);
|
2021-01-11 15:54:52 +00:00
|
|
|
} else if (Annotated.isAnnotated(v.item)) {
|
2021-01-07 16:41:46 +00:00
|
|
|
throw new Error("Improper annotation structure");
|
|
|
|
} else {
|
|
|
|
return v.item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return step(v, depth);
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:35:07 +00:00
|
|
|
export function annotate<T extends object = DefaultPointer>(v0: Value<T>, ...anns: Value<T>[]): Annotated<T> {
|
2021-01-29 11:03:28 +00:00
|
|
|
const v = Annotated.isAnnotated<T>(v0) ? v0 : new Annotated(v0);
|
2021-01-07 16:41:46 +00:00
|
|
|
anns.forEach((a) => v.annotations.push(a));
|
|
|
|
return v;
|
|
|
|
}
|