Files
SnowLuma/packages/proton/src/codegen/wire.ts
T
DrSmoothl 194165f49d feat(proton): add @snowluma/proton — compile-time protobuf DSL
Vendors SnowLuma/protobuf-dsl into the monorepo as @snowluma/proton, with
edge-case fixes applied on top of the upstream source. Reference copy of
the unmodified upstream is kept under dev/protobuf-dsl/.

Edge cases fixed (each with a regression test):
  - Aliased pb / pb_repeated marker imports (`import { pb as P }`)
    — collector now resolves the marker name through the import resolver
  - Concrete interface with a generic-instantiated message field
    (`wrapped: pb<5, Wrapper<uint_32>>`) — field typeName now resolves
    to the mangled form, and the inner instantiation is queued for
    monomorphization
  - Generic template with a generic-instantiated field
    (`interface Outer<U> { x: pb<5, Wrapper<U>> }`) — the inner
    instantiation is now re-substituted and re-monomorphized at outer
    instantiation time
  - Wrapper-binding synthetic SourceFile was empty-text — getText()
    returned garbage, breaking mangled name computation downstream
  - Cross-file forwarded wrappers (chain → imported base) were not
    detected — parseFileForDefinitions now resolves imports recursively
    (value imports only, with cycle protection)
  - matchForwardedKnownWrapper did not propagate typePattern — chain
    treated <X> as the encoded type instead of Wrapper<X>
  - Synthetic-SF WeakMap was private to analyzer.ts — replacer could
    not see it. Moved to utils.ts so typeNodeToMangledName picks it up
    automatically.

Defensive guard: any field whose typeName resolves to neither a primitive
nor a known message now throws at analyze() time, instead of silently
leaking the WireType.Varint placeholder into codegen.

95/95 tests pass; tsc --noEmit clean.
2026-05-19 08:19:13 +08:00

53 lines
1.7 KiB
TypeScript

// Protobuf wire format helpers — used directly by wire.test.ts
// The generated code no longer depends on these (everything is inlined).
export function encodeVarint(value: number, buf: number[]): void {
value = value >>> 0;
while (value > 0x7f) {
buf.push((value & 0x7f) | 0x80);
value = value >>> 7;
}
buf.push(value & 0x7f);
}
export function decodeVarint(data: Uint8Array, offset: number): [number, number] {
let result = 0;
let shift = 0;
let byte: number;
do {
byte = data[offset++];
result |= (byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
return [result >>> 0, offset];
}
export function writeTag(fieldNumber: number, wireType: number, buf: number[]): void {
encodeVarint((fieldNumber << 3) | wireType, buf);
}
export function writeString(value: string, buf: number[]): void {
const encoded = new TextEncoder().encode(value);
encodeVarint(encoded.length, buf);
for (let i = 0; i < encoded.length; i++) buf.push(encoded[i]);
}
export function readString(data: Uint8Array, offset: number, length: number): string {
return new TextDecoder().decode(data.subarray(offset, offset + length));
}
export function writeBytes(value: Uint8Array, buf: number[]): void {
encodeVarint(value.length, buf);
for (let i = 0; i < value.length; i++) buf.push(value[i]);
}
export function skipField(data: Uint8Array, offset: number, wireType: number): number {
switch (wireType) {
case 0: while (data[offset] & 0x80) offset++; return offset + 1;
case 1: return offset + 8;
case 2: { const [len, o] = decodeVarint(data, offset); return o + len; }
case 5: return offset + 4;
default: throw new Error(`Unknown wire type: ${wireType}`);
}
}