mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 15:33:56 +08:00
* feat(managed): generate WPF's known BAML type and property IDs BAML names WPF's own types, properties and strings by negative IDs. The table is generated from KnownTypes.cs and Baml2006KnownTypes.cs at a pinned dotnet/wpf commit, with the source digests recorded and the MIT license preserved. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(managed): describe the BAML document report and its read budgets Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(managed): decode BAML record streams into their element tree Read the BAML 0.96 records WPF's Baml2006Reader reads, without loading any type: the element tree with names, keys and code-behind connection IDs; property values with their converters, markup extensions and known binary serializers; dictionary keys and their static resources from the deferred key section; bindings and resource references. WPF's own types, properties and strings are named from the generated known-type table. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(analysis): summarize retained BAML documents Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(managed): read BAML files through an identity-checked provider The provider reads one bounded regular file and decodes it; a refusal at max_records, max_elements or max_file_bytes names the budget to raise. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(managed): add the inspect_baml tool CLI inspect-baml and MCP inspect_baml decode a WPF BAML document, such as a view extracted from <Assembly>.g.resources, without loading WPF or the document's assemblies. Large reports return the retained summary within the agent budget. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(artifacts): point .NET resource sets with BAML views at inspect_baml A resource set now proposes extraction, and inspect_baml when it holds .baml members. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * docs(managed): describe WPF BAML document inspection Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * docs(skill): route extracted BAML views to inspect_baml Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * ci: scope the BAML reader with the managed formats Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * build: expose the BAML provider and its identity as dotnet entry files Composition constructs the provider and the capability declarations name its identity, as they do for the managed static provider. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * refactor(managed): fold BAML coverage through the coverage accumulator Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * refactor(managed): refuse oversized BAML files through the shared parse-limit owner Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * chore(schemas): record the advertised inspect_baml catalog summary Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * build: fetch the pinned WPF sources for the BAML known-type table The generator now downloads KnownTypes.cs and Baml2006KnownTypes.cs at the pinned dotnet/wpf commit and checks their recorded digests, instead of reading a caller-supplied checkout, and runs as npm run generate:wpf-baml-known-types. The regenerated table is unchanged. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * test(managed): cover inspect-baml through the CLI and declare its conformance case Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(artifacts): recognise BAML documents and route them to inspect_baml A file that starts with the MSBAML FormatVersion header is a standalone baml artifact; inspect_artifact proposes inspect_baml for it, as it does for other formats a dedicated tool reads. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(managed): add a max_text_bytes budget for reported BAML text Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * fix(managed): bound BAML expansion and validate its nesting and dictionaries - Every reported copy of a declared name counts against max_text_bytes, so repeated references to one long name cannot expand without bound. - The nearest open element is kept on its own stack, and a property or constructor scope must open directly inside an element. - Deferred content must fit its document and enclosing section; each dictionary key's value position must start a value element. - Bindings inside MultiBinding or PriorityBinding report the container's target property. - A test names WPF's known IDs by the literal numbers of the pinned source. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * docs(managed): describe BAML recognition and the text budget Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * chore(schemas): record the inspect_baml max_text_bytes input Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * fix(managed): inherit BAML binding targets instead of walking ancestors Each element records the binding target it inherits, so a binding inside nested MultiBinding or PriorityBinding containers resolves its target in constant time. TemplateBinding and resource-extension elements now also charge their reported copies against max_text_bytes. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * fix(managed): count declared BAML type and assembly names against the text budget Every declared type reports its assembly's name, so many types can repeat one long assembly name; those copies now count against max_text_bytes too. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --------- Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
146 lines
5.3 KiB
JavaScript
146 lines
5.3 KiB
JavaScript
#!/usr/bin/env node
|
|
// Generate src/domain/managed/wpfBamlKnownTypes.ts from pinned dotnet/wpf
|
|
// sources.
|
|
//
|
|
// npm run generate:wpf-baml-known-types
|
|
//
|
|
// BAML names WPF's own types and properties by negative IDs. The IDs are the
|
|
// order of the KnownElements and KnownProperties enums in KnownTypes.cs; the
|
|
// property owners come from Baml2006KnownTypes.cs, which the runtime's
|
|
// Baml2006Reader uses. Each source is fetched at the pinned commit and must
|
|
// match its recorded digest, so a regenerated table is reproducible.
|
|
|
|
import { createHash } from "node:crypto";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const COMMIT = "bddc4298f5d714c9bc5d8a7441a14ef415c4be32";
|
|
const MARKUP =
|
|
"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup";
|
|
const SOURCES = {
|
|
knownTypes: {
|
|
path: `${MARKUP}/KnownTypes.cs`,
|
|
sha256: "cbfa1cff7bcc88a7b8dd92f9d7e0c9aef22d8c850cde35e96dae83393573c864",
|
|
},
|
|
baml2006: {
|
|
path: `${MARKUP}/Baml2006/Baml2006KnownTypes.cs`,
|
|
sha256: "4fc0f27f7a312e5f433bef93548a15e66e0645ea984a9abd9e28676519fd22e6",
|
|
},
|
|
};
|
|
const TARGET = fileURLToPath(
|
|
new URL("../src/domain/managed/wpfBamlKnownTypes.ts", import.meta.url),
|
|
);
|
|
|
|
const fetchSource = async ({ path, sha256 }) => {
|
|
const response = await fetch(
|
|
`https://raw.githubusercontent.com/dotnet/wpf/${COMMIT}/${path}`,
|
|
);
|
|
if (!response.ok) throw new Error(`${path}: HTTP ${String(response.status)}`);
|
|
const bytes = Buffer.from(await response.arrayBuffer());
|
|
const digest = createHash("sha256").update(bytes).digest("hex");
|
|
if (digest !== sha256)
|
|
throw new Error(`${path}: SHA-256 ${digest} differs from ${sha256}`);
|
|
return { text: bytes.toString("utf8").replace(/^\uFEFF/u, ""), sha256 };
|
|
};
|
|
const knownTypes = await fetchSource(SOURCES.knownTypes);
|
|
const baml2006 = await fetchSource(SOURCES.baml2006);
|
|
const commit = COMMIT;
|
|
|
|
const enumMembers = (source, name) => {
|
|
const body = new RegExp(
|
|
`enum ${name} : short\\s*\\{([\\s\\S]*?)\\}`,
|
|
"u",
|
|
).exec(source)?.[1];
|
|
if (body === undefined) throw new Error(`enum ${name} not found`);
|
|
return body
|
|
.split("\n")
|
|
.map((line) => line.trim())
|
|
.filter((line) => line !== "" && !line.startsWith("//"))
|
|
.map((line) => line.replace(/,$/u, "").split("=")[0].trim());
|
|
};
|
|
|
|
const ASSEMBLIES = {
|
|
_asmFramework: "PresentationFramework",
|
|
_asmCore: "PresentationCore",
|
|
_asmBase: "WindowsBase",
|
|
};
|
|
const qualified = new Map();
|
|
for (const [, element, assembly, fullName] of knownTypes.text.matchAll(
|
|
/case KnownElements\.(\w+): t = (_asm\w+)\.GetType\("([\w.]+)"\)/gu,
|
|
))
|
|
qualified.set(element, [fullName, ASSEMBLIES[assembly] ?? null]);
|
|
for (const [, element, fullName] of knownTypes.text.matchAll(
|
|
/case KnownElements\.(\w+): t = typeof\(([\w.]+)\)/gu,
|
|
))
|
|
if (!qualified.has(element) && fullName.includes("."))
|
|
qualified.set(element, [fullName, null]);
|
|
|
|
const elements = enumMembers(knownTypes.text, "KnownElements");
|
|
if (elements[0] !== "UnknownElement" || elements.at(-1) !== "MaxElement")
|
|
throw new Error(
|
|
"KnownElements does not run from UnknownElement to MaxElement",
|
|
);
|
|
const types = elements.slice(1, -1).map((element) => {
|
|
const known = qualified.get(element);
|
|
if (known === undefined) throw new Error(`no full name for ${element}`);
|
|
return known;
|
|
});
|
|
|
|
const properties = new Map();
|
|
for (const [, id, ownerId, name] of baml2006.text.matchAll(
|
|
/case (-\d+): \/\/\w+\.\w+\s*\n\s*typeId = (-\d+);\s*\n\s*propertyName = "(\w+)";/gu,
|
|
))
|
|
properties.set(-Number(id), [-Number(ownerId), name]);
|
|
const maxProperty = Math.max(...properties.keys());
|
|
const propertyRows = [];
|
|
for (let id = 1; id <= maxProperty; id += 1) {
|
|
const property = properties.get(id);
|
|
propertyRows.push(
|
|
property === undefined
|
|
? " null,"
|
|
: ` [${property[0]}, ${JSON.stringify(property[1])}],`,
|
|
);
|
|
}
|
|
|
|
const strings = [
|
|
...baml2006.text.matchAll(/case (-\d+):\s*\n\s*result = "(\w+)";/gu),
|
|
].map(([, id, value]) => [-Number(id), value]);
|
|
|
|
const output = `// Generated by scripts/generate-wpf-baml-known-types.mjs; do not edit.
|
|
/**
|
|
* WPF's known BAML types, properties and strings, derived from dotnet/wpf
|
|
* \`${commit}\` (MIT licence):
|
|
* - \`${MARKUP}/KnownTypes.cs\` (SHA-256 \`${knownTypes.sha256}\`);
|
|
* - \`${MARKUP}/Baml2006/Baml2006KnownTypes.cs\` (SHA-256 \`${baml2006.sha256}\`).
|
|
*
|
|
* BAML stores the negative of an ID: type -1 is the first entry below.
|
|
*/
|
|
export const WPF_BAML_KNOWN_SOURCE = "dotnet/wpf ${commit.slice(0, 8)}";
|
|
|
|
/** Known type ID 1..${types.length}: full name and its WPF assembly, or null for a BCL type. */
|
|
export const WPF_BAML_KNOWN_TYPES: readonly (readonly [
|
|
fullName: string,
|
|
assembly: string | null,
|
|
])[] = [
|
|
${types.map(([name, assembly]) => ` [${JSON.stringify(name)}, ${JSON.stringify(assembly)}],`).join("\n")}
|
|
];
|
|
|
|
/** Known property ID 1..${maxProperty}: owner known type ID and name; null IDs are unused. */
|
|
export const WPF_BAML_KNOWN_PROPERTIES: readonly (readonly [
|
|
ownerTypeId: number,
|
|
name: string,
|
|
] | null)[] = [
|
|
${propertyRows.join("\n")}
|
|
];
|
|
|
|
/** Known string IDs. */
|
|
export const WPF_BAML_KNOWN_STRINGS: ReadonlyMap<number, string> = new Map([
|
|
${strings.map(([id, value]) => ` [${id}, ${JSON.stringify(value)}],`).join("\n")}
|
|
]);
|
|
`;
|
|
const target = TARGET;
|
|
await writeFile(target, output);
|
|
console.log(
|
|
`${types.length} types, ${properties.size} properties, ${strings.length} strings -> ${target}`,
|
|
);
|