Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344// Generates engine/jsx-entities.wat (JSX's HTML entity lookup table, a data segment// scripts/build.sh merges into build/downlevel.wasm). Records are [n][name][code point, 3 bytes LE],// terminated by n = 0. Source: TypeScript's entities map from node_modules/typescript/lib/typescript.js.// Run: node scripts/gen/jsx-entities.mjsimport fs from "node:fs";import path from "node:path";import { ENT_BASE, ENT_END, dataModule } from "./layout.mjs";
const content = fs.readFileSync(path.resolve(import.meta.dirname, "../../node_modules/typescript/lib/typescript.js"), "utf8");const start = content.indexOf("var entities = new Map(Object.entries({");if (start === -1) throw new Error("Could not find entities map in TypeScript");
const from = start + "var entities = new Map(Object.entries(".length;const match = content.substring(from).match(/({[\s\S]*?})\)\);/);if (!match) throw new Error("Could not extract entities object");
const objStr = match[1];const entities = Function("return (" + objStr + ")")();
if (Object.keys(entities).length <= 250) throw new Error(`entities has only ${Object.keys(entities).length} entries, expected > 250`);if (entities.amp !== 38) throw new Error(`amp = ${entities.amp}, expected 38`);if (entities.nbsp !== 160) throw new Error(`nbsp = ${entities.nbsp}, expected 160`);
const records = [];for (const [name, codePoint] of Object.entries(entities)) { const nameBytes = Buffer.from(name, "ascii"); if (nameBytes.length > 255) throw new Error(`entity name too long: ${name}`); if (codePoint < 0 || codePoint > 0xffffff) throw new Error(`code point out of range: ${codePoint}`);
records.push(nameBytes.length); for (const b of nameBytes) records.push(b); records.push(codePoint & 0xff); records.push((codePoint >> 8) & 0xff); records.push((codePoint >> 16) & 0xff);}records.push(0);
const data = Buffer.from(records);
const out = dataModule("jsx-entities.mjs", ENT_BASE, data, ENT_END);const dest = path.resolve(import.meta.dirname, "../../engine/jsx-entities.wat");fs.writeFileSync(dest, out);console.log(`wrote ${dest}: ${Object.keys(entities).length} entities, ${data.length} bytes of table`);