Add Protobuf with Testing + Codegen #7

Merged
colonelpanic merged 12 commits from mk/add-protobuf into master 2024-05-22 11:27:55 -06:00
2 changed files with 132 additions and 72 deletions
Showing only changes of commit 81742efdcb - Show all commits

View File

@ -1,5 +1,5 @@
NIX := "LD_LIBRARY_PATH='' nix"
PROTOBUF_DIR := "$PWD/protobuf"
PROTOBUF_DIR := "$PWD/rbproto"
prettier-gql:
#!/usr/bin/env bash
@ -20,7 +20,7 @@ protobuf-compile-a-python name:
protobuf-compile-a-js name:
#!/usr/bin/env bash
set -euxo pipefail
{{NIX}} develop --impure --command bash -c 'yarn pbjs {{ PROTOBUF_DIR }}/shot.proto --ts {{ PROTOBUF_DIR }}/ts/shot.ts'
{{NIX}} develop --impure --command bash -c 'yarn pbjs {{ PROTOBUF_DIR }}/shot.proto --ts {{ PROTOBUF_DIR }}/ts/shot.ts && yarn prettier ./rbproto --write'
protobuf-compile-all-js:
#!/usr/bin/env bash

View File

@ -346,11 +346,20 @@ function pushTemporaryLength(bb: ByteBuffer): number {
function skipUnknownField(bb: ByteBuffer, type: number): void {
switch (type) {
case 0: while (readByte(bb) & 0x80) { } break;
case 2: skip(bb, readVarint32(bb)); break;
case 5: skip(bb, 4); break;
case 1: skip(bb, 8); break;
default: throw new Error("Unimplemented type: " + type);
case 0:
while (readByte(bb) & 0x80) {}
break;
case 2:
skip(bb, readVarint32(bb));
break;
case 5:
skip(bb, 4);
break;
case 1:
skip(bb, 8);
break;
default:
throw new Error("Unimplemented type: " + type);
}
}
@ -366,10 +375,11 @@ function longToString(value: Long): string {
let low = value.low;
let high = value.high;
return String.fromCharCode(
low & 0xFFFF,
low & 0xffff,
low >>> 16,
high & 0xFFFF,
high >>> 16);
high & 0xffff,
high >>> 16,
);
}
// The code below was modified from https://github.com/protobufjs/bytebuffer.js
@ -415,7 +425,7 @@ function toUint8Array(bb: ByteBuffer): Uint8Array {
function skip(bb: ByteBuffer, offset: number): void {
if (bb.offset + offset > bb.limit) {
throw new Error('Skip past limit');
throw new Error("Skip past limit");
}
bb.offset += offset;
}
@ -444,7 +454,7 @@ function grow(bb: ByteBuffer, count: number): number {
function advance(bb: ByteBuffer, count: number): number {
let offset = bb.offset;
if (offset + count > bb.limit) {
throw new Error('Read past limit');
throw new Error("Read past limit");
}
bb.offset += count;
return offset;
@ -465,11 +475,15 @@ function readString(bb: ByteBuffer, count: number): string {
let offset = advance(bb, count);
let fromCharCode = String.fromCharCode;
let bytes = bb.bytes;
let invalid = '\uFFFD';
let text = '';
let invalid = "\uFFFD";
let text = "";
for (let i = 0; i < count; i++) {
let c1 = bytes[i + offset], c2: number, c3: number, c4: number, c: number;
let c1 = bytes[i + offset],
c2: number,
c3: number,
c4: number,
c: number;
// 1 byte
if ((c1 & 0x80) === 0) {
@ -477,13 +491,13 @@ function readString(bb: ByteBuffer, count: number): string {
}
// 2 bytes
else if ((c1 & 0xE0) === 0xC0) {
else if ((c1 & 0xe0) === 0xc0) {
if (i + 1 >= count) text += invalid;
else {
c2 = bytes[i + offset + 1];
if ((c2 & 0xC0) !== 0x80) text += invalid;
if ((c2 & 0xc0) !== 0x80) text += invalid;
else {
c = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
c = ((c1 & 0x1f) << 6) | (c2 & 0x3f);
if (c < 0x80) text += invalid;
else {
text += fromCharCode(c);
@ -494,15 +508,15 @@ function readString(bb: ByteBuffer, count: number): string {
}
// 3 bytes
else if ((c1 & 0xF0) == 0xE0) {
else if ((c1 & 0xf0) == 0xe0) {
if (i + 2 >= count) text += invalid;
else {
c2 = bytes[i + offset + 1];
c3 = bytes[i + offset + 2];
if (((c2 | (c3 << 8)) & 0xC0C0) !== 0x8080) text += invalid;
if (((c2 | (c3 << 8)) & 0xc0c0) !== 0x8080) text += invalid;
else {
c = ((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
if (c < 0x0800 || (c >= 0xD800 && c <= 0xDFFF)) text += invalid;
c = ((c1 & 0x0f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f);
if (c < 0x0800 || (c >= 0xd800 && c <= 0xdfff)) text += invalid;
else {
text += fromCharCode(c);
i += 2;
@ -512,26 +526,29 @@ function readString(bb: ByteBuffer, count: number): string {
}
// 4 bytes
else if ((c1 & 0xF8) == 0xF0) {
else if ((c1 & 0xf8) == 0xf0) {
if (i + 3 >= count) text += invalid;
else {
c2 = bytes[i + offset + 1];
c3 = bytes[i + offset + 2];
c4 = bytes[i + offset + 3];
if (((c2 | (c3 << 8) | (c4 << 16)) & 0xC0C0C0) !== 0x808080) text += invalid;
if (((c2 | (c3 << 8) | (c4 << 16)) & 0xc0c0c0) !== 0x808080)
text += invalid;
else {
c = ((c1 & 0x07) << 0x12) | ((c2 & 0x3F) << 0x0C) | ((c3 & 0x3F) << 0x06) | (c4 & 0x3F);
if (c < 0x10000 || c > 0x10FFFF) text += invalid;
c =
((c1 & 0x07) << 0x12) |
((c2 & 0x3f) << 0x0c) |
((c3 & 0x3f) << 0x06) |
(c4 & 0x3f);
if (c < 0x10000 || c > 0x10ffff) text += invalid;
else {
c -= 0x10000;
text += fromCharCode((c >> 10) + 0xD800, (c & 0x3FF) + 0xDC00);
text += fromCharCode((c >> 10) + 0xd800, (c & 0x3ff) + 0xdc00);
i += 3;
}
}
}
}
else text += invalid;
} else text += invalid;
}
return text;
@ -545,8 +562,8 @@ function writeString(bb: ByteBuffer, text: string): void {
// Write the byte count first
for (let i = 0; i < n; i++) {
let c = text.charCodeAt(i);
if (c >= 0xD800 && c <= 0xDBFF && i + 1 < n) {
c = (c << 10) + text.charCodeAt(++i) - 0x35FDC00;
if (c >= 0xd800 && c <= 0xdbff && i + 1 < n) {
c = (c << 10) + text.charCodeAt(++i) - 0x35fdc00;
}
byteCount += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
}
@ -558,24 +575,24 @@ function writeString(bb: ByteBuffer, text: string): void {
// Then write the bytes
for (let i = 0; i < n; i++) {
let c = text.charCodeAt(i);
if (c >= 0xD800 && c <= 0xDBFF && i + 1 < n) {
c = (c << 10) + text.charCodeAt(++i) - 0x35FDC00;
if (c >= 0xd800 && c <= 0xdbff && i + 1 < n) {
c = (c << 10) + text.charCodeAt(++i) - 0x35fdc00;
}
if (c < 0x80) {
bytes[offset++] = c;
} else {
if (c < 0x800) {
bytes[offset++] = ((c >> 6) & 0x1F) | 0xC0;
bytes[offset++] = ((c >> 6) & 0x1f) | 0xc0;
} else {
if (c < 0x10000) {
bytes[offset++] = ((c >> 12) & 0x0F) | 0xE0;
bytes[offset++] = ((c >> 12) & 0x0f) | 0xe0;
} else {
bytes[offset++] = ((c >> 18) & 0x07) | 0xF0;
bytes[offset++] = ((c >> 12) & 0x3F) | 0x80;
bytes[offset++] = ((c >> 18) & 0x07) | 0xf0;
bytes[offset++] = ((c >> 12) & 0x3f) | 0x80;
}
bytes[offset++] = ((c >> 6) & 0x3F) | 0x80;
bytes[offset++] = ((c >> 6) & 0x3f) | 0x80;
}
bytes[offset++] = (c & 0x3F) | 0x80;
bytes[offset++] = (c & 0x3f) | 0x80;
}
}
}
@ -695,7 +712,7 @@ function readVarint32(bb: ByteBuffer): number {
let b: number;
do {
b = readByte(bb);
if (c < 32) value |= (b & 0x7F) << c;
if (c < 32) value |= (b & 0x7f) << c;
c += 7;
} while (b & 0x80);
return value;
@ -716,18 +733,35 @@ function readVarint64(bb: ByteBuffer, unsigned: boolean): Long {
let part2 = 0;
let b: number;
b = readByte(bb); part0 = (b & 0x7F); if (b & 0x80) {
b = readByte(bb); part0 |= (b & 0x7F) << 7; if (b & 0x80) {
b = readByte(bb); part0 |= (b & 0x7F) << 14; if (b & 0x80) {
b = readByte(bb); part0 |= (b & 0x7F) << 21; if (b & 0x80) {
b = readByte(bb); part1 = (b & 0x7F); if (b & 0x80) {
b = readByte(bb); part1 |= (b & 0x7F) << 7; if (b & 0x80) {
b = readByte(bb); part1 |= (b & 0x7F) << 14; if (b & 0x80) {
b = readByte(bb); part1 |= (b & 0x7F) << 21; if (b & 0x80) {
b = readByte(bb); part2 = (b & 0x7F); if (b & 0x80) {
b = readByte(bb); part2 |= (b & 0x7F) << 7;
b = readByte(bb);
part0 = b & 0x7f;
if (b & 0x80) {
b = readByte(bb);
part0 |= (b & 0x7f) << 7;
if (b & 0x80) {
b = readByte(bb);
part0 |= (b & 0x7f) << 14;
if (b & 0x80) {
b = readByte(bb);
part0 |= (b & 0x7f) << 21;
if (b & 0x80) {
b = readByte(bb);
part1 = b & 0x7f;
if (b & 0x80) {
b = readByte(bb);
part1 |= (b & 0x7f) << 7;
if (b & 0x80) {
b = readByte(bb);
part1 |= (b & 0x7f) << 14;
if (b & 0x80) {
b = readByte(bb);
part1 |= (b & 0x7f) << 21;
if (b & 0x80) {
b = readByte(bb);
part2 = b & 0x7f;
if (b & 0x80) {
b = readByte(bb);
part2 |= (b & 0x7f) << 7;
}
}
}
@ -752,30 +786,56 @@ function writeVarint64(bb: ByteBuffer, value: Long): void {
// ref: src/google/protobuf/io/coded_stream.cc
let size =
part2 === 0 ?
part1 === 0 ?
part0 < 1 << 14 ?
part0 < 1 << 7 ? 1 : 2 :
part0 < 1 << 21 ? 3 : 4 :
part1 < 1 << 14 ?
part1 < 1 << 7 ? 5 : 6 :
part1 < 1 << 21 ? 7 : 8 :
part2 < 1 << 7 ? 9 : 10;
part2 === 0
? part1 === 0
? part0 < 1 << 14
? part0 < 1 << 7
? 1
: 2
: part0 < 1 << 21
? 3
: 4
: part1 < 1 << 14
? part1 < 1 << 7
? 5
: 6
: part1 < 1 << 21
? 7
: 8
: part2 < 1 << 7
? 9
: 10;
let offset = grow(bb, size);
let bytes = bb.bytes;
switch (size) {
case 10: bytes[offset + 9] = (part2 >>> 7) & 0x01;
case 9: bytes[offset + 8] = size !== 9 ? part2 | 0x80 : part2 & 0x7F;
case 8: bytes[offset + 7] = size !== 8 ? (part1 >>> 21) | 0x80 : (part1 >>> 21) & 0x7F;
case 7: bytes[offset + 6] = size !== 7 ? (part1 >>> 14) | 0x80 : (part1 >>> 14) & 0x7F;
case 6: bytes[offset + 5] = size !== 6 ? (part1 >>> 7) | 0x80 : (part1 >>> 7) & 0x7F;
case 5: bytes[offset + 4] = size !== 5 ? part1 | 0x80 : part1 & 0x7F;
case 4: bytes[offset + 3] = size !== 4 ? (part0 >>> 21) | 0x80 : (part0 >>> 21) & 0x7F;
case 3: bytes[offset + 2] = size !== 3 ? (part0 >>> 14) | 0x80 : (part0 >>> 14) & 0x7F;
case 2: bytes[offset + 1] = size !== 2 ? (part0 >>> 7) | 0x80 : (part0 >>> 7) & 0x7F;
case 1: bytes[offset] = size !== 1 ? part0 | 0x80 : part0 & 0x7F;
case 10:
bytes[offset + 9] = (part2 >>> 7) & 0x01;
case 9:
bytes[offset + 8] = size !== 9 ? part2 | 0x80 : part2 & 0x7f;
case 8:
bytes[offset + 7] =
size !== 8 ? (part1 >>> 21) | 0x80 : (part1 >>> 21) & 0x7f;
case 7:
bytes[offset + 6] =
size !== 7 ? (part1 >>> 14) | 0x80 : (part1 >>> 14) & 0x7f;
case 6:
bytes[offset + 5] =
size !== 6 ? (part1 >>> 7) | 0x80 : (part1 >>> 7) & 0x7f;
case 5:
bytes[offset + 4] = size !== 5 ? part1 | 0x80 : part1 & 0x7f;
case 4:
bytes[offset + 3] =
size !== 4 ? (part0 >>> 21) | 0x80 : (part0 >>> 21) & 0x7f;
case 3:
bytes[offset + 2] =
size !== 3 ? (part0 >>> 14) | 0x80 : (part0 >>> 14) & 0x7f;
case 2:
bytes[offset + 1] =
size !== 2 ? (part0 >>> 7) | 0x80 : (part0 >>> 7) & 0x7f;
case 1:
bytes[offset] = size !== 1 ? part0 | 0x80 : part0 & 0x7f;
}
}