set_group_whole_ban false (unmute) failed with `OIDB error 1007` while enabling worked (issue #70). OIDB (0x89A, 0) is shared by several commands (SetSearch / SetAddOption / mute-all) disambiguated by body shape. Unmute sends muteState.state=0, but proton omits zero scalars (proto3), leaving an empty `muteState` the server can't classify. Enable sends 0xFFFFFFFF, which is non-zero and survives — hence the asymmetry. Add a `pb_optional<N, T>` marker (proto3 explicit presence): a scalar so marked serialises even at its zero/default value. It is opt-in per field via a NEW `explicitPresence` flag set ONLY by this marker — never reused from the TS-`?`-derived `isOptional` (true for nearly every field). The encoder routes explicit-presence scalars to a dedicated block; every `pb<>` / `pb_repeated<>` field takes the untouched branches, so existing wire output is byte-identical (proton's 99 existing tests, incl. byte-level round-trips, stay green). Mark OidbMuteAllState.state `pb_optional<17, uint_32>` so unmute now puts field 17 (=0) on the wire. New tests: proton pb_optional encode/round-trip (incl. that sibling pb<> fields still omit zero), and a byte-level assertion that mute-all unmute carries the state field. Note: proton ships as a built Vite plugin (dist, gitignored); rebuild it (`pnpm --filter @snowluma/proton build`) for the change to reach consumers.
134 lines
5.0 KiB
TypeScript
134 lines
5.0 KiB
TypeScript
import ts from 'typescript';
|
|
import { WireType, type ProtobufField, type ProtobufMessage, type GenericProtobufTemplate } from './types.js';
|
|
import {
|
|
isKeywordTypeNode,
|
|
createImportedTypeNameResolver,
|
|
resolveSourceFileForTypeNode,
|
|
type ImportedTypeNameResolver,
|
|
typeNodeToMangledName,
|
|
} from './utils.js';
|
|
|
|
function identityImportedTypeName(name: string): string {
|
|
return name;
|
|
}
|
|
|
|
/** Resolved value for a single template type parameter. */
|
|
interface ResolvedTypeArg {
|
|
/** Mangled-name form used when substituting into a `rawTypeName` slot
|
|
* for an `isTypeParam` field. */
|
|
mangled: string;
|
|
/** Original TypeScript source text — used when re-substituting into a
|
|
* field's `genericTypeArgText` so the substituted text remains parseable. */
|
|
text: string;
|
|
}
|
|
|
|
/**
|
|
* Recursively monomorphize a generic type instantiation into concrete ProtobufMessages.
|
|
* E.g. `Wrapper<Wrapper<string>>` → creates `Wrapper__string` and `Wrapper__Wrapper__string`.
|
|
* Returns the mangled name of the concrete type, or null if it cannot be resolved.
|
|
*/
|
|
export function monomorphizeTypeNode(
|
|
typeNode: ts.TypeNode,
|
|
sf: ts.SourceFile,
|
|
templates: Map<string, GenericProtobufTemplate>,
|
|
out: Map<string, ProtobufMessage>,
|
|
resolveImportedTypeName: ImportedTypeNameResolver = identityImportedTypeName,
|
|
): string | null {
|
|
if (!ts.isTypeReferenceNode(typeNode) || !ts.isIdentifier(typeNode.typeName)) return null;
|
|
|
|
const baseName = resolveImportedTypeName(typeNode.typeName.text);
|
|
const typeArgs = typeNode.typeArguments;
|
|
if (!typeArgs || typeArgs.length === 0) return baseName; // already concrete
|
|
|
|
const tpl = templates.get(baseName);
|
|
if (!tpl || typeArgs.length !== tpl.typeParams.length) return null;
|
|
|
|
const mangledName = typeNodeToMangledName(typeNode, sf, resolveImportedTypeName);
|
|
if (out.has(mangledName)) return mangledName; // already done
|
|
|
|
// Resolve each type parameter → both mangled name AND source text.
|
|
const paramMap = new Map<string, ResolvedTypeArg>();
|
|
for (let i = 0; i < tpl.typeParams.length; i++) {
|
|
const resolved = resolveTypeArg(typeArgs[i], sf, templates, out, resolveImportedTypeName);
|
|
if (!resolved) return null;
|
|
paramMap.set(tpl.typeParams[i], resolved);
|
|
}
|
|
|
|
// Substitute type params in template fields.
|
|
const fields: ProtobufField[] = tpl.fields.map(f => {
|
|
if (f.genericTypeArgText) {
|
|
// Field type is an inner generic instantiation referencing template
|
|
// type params (e.g. `pb<N, Wrapper<U>>`). Substitute the params into
|
|
// the captured text, re-parse, and re-monomorphize so the resulting
|
|
// concrete instantiation (`Wrapper<uint_32>`) lands in `out`.
|
|
let substituted = f.genericTypeArgText;
|
|
for (const [paramName, value] of paramMap) {
|
|
substituted = substituted.replace(
|
|
new RegExp(`\\b${paramName}\\b`, 'g'),
|
|
value.text,
|
|
);
|
|
}
|
|
const innerSfPath = f.genericTypeArgSourceFilePath ?? sf.fileName;
|
|
const innerSf = ts.createSourceFile(
|
|
innerSfPath,
|
|
`type __X = ${substituted};`,
|
|
ts.ScriptTarget.Latest,
|
|
true,
|
|
);
|
|
const stmt = innerSf.statements[0];
|
|
let resolvedTypeName: string | null = null;
|
|
if (ts.isTypeAliasDeclaration(stmt)) {
|
|
const innerResolver = createImportedTypeNameResolver(innerSf);
|
|
resolvedTypeName = monomorphizeTypeNode(stmt.type, innerSf, templates, out, innerResolver);
|
|
}
|
|
return {
|
|
name: f.name,
|
|
fieldNumber: f.fieldNumber,
|
|
typeName: resolvedTypeName ?? f.rawTypeName,
|
|
wireType: WireType.Varint,
|
|
isMessage: false,
|
|
isOptional: f.isOptional,
|
|
isRepeated: f.isRepeated,
|
|
explicitPresence: f.explicitPresence,
|
|
};
|
|
}
|
|
return {
|
|
name: f.name,
|
|
fieldNumber: f.fieldNumber,
|
|
typeName: f.isTypeParam ? (paramMap.get(f.rawTypeName)?.mangled ?? f.rawTypeName) : f.rawTypeName,
|
|
wireType: WireType.Varint,
|
|
isMessage: false,
|
|
isOptional: f.isOptional,
|
|
isRepeated: f.isRepeated,
|
|
explicitPresence: f.explicitPresence,
|
|
};
|
|
});
|
|
|
|
out.set(mangledName, { name: mangledName, fields });
|
|
return mangledName;
|
|
}
|
|
|
|
function resolveTypeArg(
|
|
node: ts.TypeNode,
|
|
sf: ts.SourceFile,
|
|
templates: Map<string, GenericProtobufTemplate>,
|
|
out: Map<string, ProtobufMessage>,
|
|
resolveImportedTypeName: ImportedTypeNameResolver,
|
|
): ResolvedTypeArg | null {
|
|
const effectiveSf = resolveSourceFileForTypeNode(node, sf);
|
|
// Try recursive monomorphization first (handles nested generics).
|
|
const mono = monomorphizeTypeNode(node, effectiveSf, templates, out, resolveImportedTypeName);
|
|
if (mono) return { mangled: mono, text: node.getText(effectiveSf) };
|
|
// Keyword type (string, number, …)
|
|
if (isKeywordTypeNode(node)) {
|
|
const text = node.getText(effectiveSf);
|
|
return { mangled: text, text };
|
|
}
|
|
// Simple identifier (uint_32, SomeMsg, …)
|
|
if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName) && !node.typeArguments) {
|
|
const name = resolveImportedTypeName(node.typeName.text);
|
|
return { mangled: name, text: name };
|
|
}
|
|
return null;
|
|
}
|