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.
This commit is contained in:
DrSmoothl committed 2026-05-19 08:19:13 +08:00
1 parent 02d1848d39
commit 194165f49d
62 files changed
+4714

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
import type { MessageRegistry, ProtobufField, ProtobufMessage } from './ast/types.js';
export type RuntimeMapFnName = 'protobuf_encode' | 'protobuf_decode';
export interface RuntimeMapCallSite {
file: string;
line: number;
column: number;
fnName: RuntimeMapFnName;
typeName: string;
}
export interface ProtobufRuntimeMap {
version: 1;
messages: ProtobufMessage[];
callSites: RuntimeMapCallSite[];
}
function cloneField(field: ProtobufField): ProtobufField {
return {
name: field.name,
fieldNumber: field.fieldNumber,
typeName: field.typeName,
wireType: field.wireType,
isMessage: field.isMessage,
isOptional: field.isOptional,
isRepeated: field.isRepeated,
};
}
function cloneMessage(msg: ProtobufMessage): ProtobufMessage {
return {
name: msg.name,
fields: msg.fields.map(cloneField),
};
}
export function registryToRuntimeMapMessages(registry: MessageRegistry): ProtobufMessage[] {
return [...registry.values()].map(cloneMessage);
}
export function runtimeMapToRegistry(map: ProtobufRuntimeMap): MessageRegistry {
const registry: MessageRegistry = new Map();
for (const msg of map.messages) {
registry.set(msg.name, cloneMessage(msg));
}
return registry;
}
export function normalizeRuntimeMapPath(filePath: string): string {
const noQuery = filePath.replace(/[?#].*$/, '');
return noQuery.replace(/\\/g, '/').replace(/^file:\/\//, '');
}
export function createRuntimeMap(map: { messages: MessageRegistry; callSites: RuntimeMapCallSite[] }): ProtobufRuntimeMap {
const dedupedCallSites = new Map<string, RuntimeMapCallSite>();
for (const cs of map.callSites) {
const normalized = {
...cs,
file: normalizeRuntimeMapPath(cs.file),
};
const key = `${normalized.file}:${normalized.line}:${normalized.column}:${normalized.fnName}:${normalized.typeName}`;
dedupedCallSites.set(key, normalized);
}
return {
version: 1,
messages: registryToRuntimeMapMessages(map.messages),
callSites: [...dedupedCallSites.values()],
};
}