Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | 5x 5x 5x 5x 5x 2042x 2300x 2300x 2300x 2300x 2300x 2300x 2300x 712404x 709806x 641076x 638148x 635499x 624887x 616771x 596304x 591117x 2300x 2300x 2035x 2035x 2035x 2035x 2022x 2022x 2035x 2035x 2035x 5187x 2928x 2928x 2903x 2102x 1730x 1704x 1693x 1693x 1693x 1693x 1693x 5x 5x 1688x 66x 66x 1622x 1622x 1622x 2649x 2649x 2080x 1699x 1690x 1680x 1680x 1680x 1680x 1680x 5x 5x 1675x 47x 47x 1628x 1628x 1628x 20467x 84274x 84274x 75605x 8669x 8668x 8668x 8668x 8668x 1x 1x 1x 1x 1x 1x 68730x 68730x 68730x 68730x 13509x 13509x 68730x 68730x 10x 10x 10x 10x 3x 3x 10x 8116x 8116x 8116x 8116x 8116x 5227x 5227x 656604x 10612x 10612x 10612x 10612x 10612x 7978x 7978x 10612x 51465x 51465x | import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers/lib';
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {
AnnotationAstNode,
ArrAstNode,
type AstNode,
BinAstNode,
BoolAstNode,
FloatAstNode,
NintAstNode,
NullAstNode,
ObjAstNode,
StrAstNode,
toAst,
UintAstNode,
} from './ast';
import {TYPE_OVERLAY} from './constants';
import {Import} from './Import';
import {systemSymbolImport} from './symbols';
export class IonEncoderFast {
protected symbols?: Import;
constructor(public readonly writer: IWriter & IWriterGrowable = new Writer()) {}
public encode(value: unknown): Uint8Array {
this.writer.reset();
this.symbols = new Import(systemSymbolImport, []);
const ast = toAst(value, this.symbols);
this.writeIvm();
this.writeSymbolTable();
this.writeAny(ast);
return this.writer.flush();
}
public writeAny(value: AstNode<unknown>): void {
if (value instanceof NullAstNode) this.writer.u8(TYPE_OVERLAY.NULL + 15);
else if (value instanceof StrAstNode) this.writeStr(value);
else if (value instanceof UintAstNode) this.encodeUint(value);
else if (value instanceof NintAstNode) this.encodeNint(value);
else if (value instanceof ObjAstNode) this.writeObj(value);
else if (value instanceof ArrAstNode) this.writeArr(value);
else if (value instanceof FloatAstNode) this.writeFloat(value);
else if (value instanceof BoolAstNode) this.writeBool(value);
else if (value instanceof BinAstNode) this.writeBin(value);
}
public writeIvm(): void {
this.writer.u32(0xe00100ea);
}
public writeSymbolTable(): void {
if (!this.symbols?.length) return;
const node = new AnnotationAstNode(this.symbols!.toAst(), [3]);
this.writeAnnotations(node);
}
public writeAnnotations(node: AnnotationAstNode): void {
const writer = this.writer;
if (node.len < 14) writer.u8(TYPE_OVERLAY.ANNO + node.len);
else {
writer.u8(TYPE_OVERLAY.ANNO + 14);
this.writeVUint(node.len);
}
this.writeVUint(node.annotationLen);
for (let i = 0; i < node.annotations.length; i++) this.writeVUint(node.annotations[i]);
this.writeAny(node.val);
}
public writeBool(node: BoolAstNode): void {
this.writer.u8(TYPE_OVERLAY.BOOL + (node.val ? 1 : 0));
}
public encodeUint(node: UintAstNode): void {
const uint = node.val;
if (!uint) this.writer.u8(TYPE_OVERLAY.UINT);
else if (uint <= 0xff) this.writer.u16(((TYPE_OVERLAY.UINT + 1) << 8) + uint);
else if (uint <= 0xffff) this.writer.u8u16(TYPE_OVERLAY.UINT + 2, uint);
else if (uint <= 0xffffff) this.writer.u32(((TYPE_OVERLAY.UINT + 3) << 24) + uint);
else if (uint <= 0xffffffff) this.writer.u8u32(TYPE_OVERLAY.UINT + 4, uint);
else {
let lo = uint | 0;
if (lo < 0) lo += 4294967296;
let hi = uint - lo;
hi /= 4294967296;
if (uint <= 0xffffffffff) {
this.writer.u16(((TYPE_OVERLAY.UINT + 5) << 8) + hi);
this.writer.u32(lo);
} else if (uint <= 0xffffffffffff) {
this.writer.u8u16(TYPE_OVERLAY.UINT + 6, hi);
this.writer.u32(lo);
} else {
this.writer.u16(((TYPE_OVERLAY.UINT + 7) << 8) + (hi >> 16));
this.writer.u16(hi & 0xffff);
this.writer.u32(lo);
}
}
}
public encodeNint(node: NintAstNode): void {
const uint = -node.val;
if (uint <= 0xff) this.writer.u16(((TYPE_OVERLAY.NINT + 1) << 8) + uint);
else if (uint <= 0xffff) this.writer.u8u16(TYPE_OVERLAY.NINT + 2, uint);
else if (uint <= 0xffffff) this.writer.u32(((TYPE_OVERLAY.NINT + 3) << 24) + uint);
else if (uint <= 0xffffffff) this.writer.u8u32(TYPE_OVERLAY.NINT + 4, uint);
else {
let lo = uint | 0;
if (lo < 0) lo += 4294967296;
let hi = uint - lo;
hi /= 4294967296;
if (uint <= 0xffffffffff) {
this.writer.u16(((TYPE_OVERLAY.NINT + 5) << 8) + hi);
this.writer.u32(lo);
} else if (uint <= 0xffffffffffff) {
this.writer.u8u16(TYPE_OVERLAY.NINT + 6, hi);
this.writer.u32(lo);
} else {
this.writer.u16(((TYPE_OVERLAY.NINT + 7) << 8) + (hi >> 16));
this.writer.u16(hi & 0xffff);
this.writer.u32(lo);
}
}
}
public writeFloat(node: FloatAstNode): void {
this.writer.u8f64(TYPE_OVERLAY.FLOT + 8, node.val);
}
public writeVUint(num: number): void {
const writer = this.writer;
if (num <= 0b1111111) {
writer.u8(0b10000000 + num);
} else if (num <= 0b1111111_1111111) {
writer.ensureCapacity(2);
const uint8 = writer.uint8;
uint8[writer.x++] = num >>> 7;
uint8[writer.x++] = 0b10000000 + (num & 0b01111111);
} else if (num <= 0b1111111_1111111_1111111) {
writer.ensureCapacity(3);
const uint8 = writer.uint8;
uint8[writer.x++] = num >>> 14;
uint8[writer.x++] = (num >>> 7) & 0b01111111;
uint8[writer.x++] = 0b10000000 + (num & 0b01111111);
} else Eif (num <= 0b1111111_1111111_1111111_1111111) {
writer.ensureCapacity(4);
const uint8 = writer.uint8;
uint8[writer.x++] = num >>> 21;
uint8[writer.x++] = (num >>> 14) & 0b01111111;
uint8[writer.x++] = (num >>> 7) & 0b01111111;
uint8[writer.x++] = 0b10000000 + (num & 0b01111111);
} else {
let lo32 = num | 0;
if (lo32 < 0) lo32 += 4294967296;
const hi32 = (num - lo32) / 4294967296;
if (num <= 0b1111111_1111111_1111111_1111111_1111111) {
writer.ensureCapacity(5);
const uint8 = writer.uint8;
uint8[writer.x++] = (hi32 << 4) | (num >>> 28);
uint8[writer.x++] = (num >>> 21) & 0b01111111;
uint8[writer.x++] = (num >>> 14) & 0b01111111;
uint8[writer.x++] = (num >>> 7) & 0b01111111;
uint8[writer.x++] = 0b10000000 + (num & 0b01111111);
} else if (num <= 0b1111111_1111111_1111111_1111111_1111111_1111111) {
writer.ensureCapacity(6);
const uint8 = writer.uint8;
uint8[writer.x++] = (hi32 >>> 3) & 0b1111;
uint8[writer.x++] = ((hi32 & 0b111) << 4) | (num >>> 28);
uint8[writer.x++] = (num >>> 21) & 0b01111111;
uint8[writer.x++] = (num >>> 14) & 0b01111111;
uint8[writer.x++] = (num >>> 7) & 0b01111111;
uint8[writer.x++] = 0b10000000 + (num & 0b01111111);
}
}
}
public writeStr(node: StrAstNode): void {
const str = node.val;
const length = node.len;
const writer = this.writer;
if (length < 14) writer.u8(TYPE_OVERLAY.STRI + length);
else {
writer.u8(TYPE_OVERLAY.STRI + 14);
this.writeVUint(length);
}
writer.ensureCapacity(length * 4);
writer.utf8(str);
}
public writeBin(node: BinAstNode): void {
const buf = node.val;
const length = node.len;
const writer = this.writer;
if (length < 14) writer.u8(TYPE_OVERLAY.BINA + length);
else {
writer.u8(TYPE_OVERLAY.BINA + 14);
this.writeVUint(length);
}
writer.buf(buf, length);
}
public writeArr(node: ArrAstNode): void {
const writer = this.writer;
const arr = node.val;
Iif (arr === null) {
writer.u8(TYPE_OVERLAY.LIST + 15);
return;
}
const length = node.len;
if (length < 14) writer.u8(TYPE_OVERLAY.LIST + length);
else {
writer.u8(TYPE_OVERLAY.LIST + 14);
this.writeVUint(length);
}
for (let i = 0; i < length; i++) this.writeAny(arr[i]);
}
public writeObj(node: ObjAstNode): void {
const writer = this.writer;
const arr = node.val;
Iif (arr === null) {
writer.u8(TYPE_OVERLAY.LIST + 15);
return;
}
const length = node.len;
if (length < 14) writer.u8(TYPE_OVERLAY.STRU + length);
else {
writer.u8(TYPE_OVERLAY.STRU + 14);
this.writeVUint(length);
}
node.val!.forEach((n, symbolId) => {
this.writeVUint(symbolId);
this.writeAny(n);
});
}
}
|