dev / templates / bot / scripts / normalize-gensyms.mjs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// The CLJS compiler numbers protocol `…$dyn_<N>` vars with the JVM-global
// gensym counter, so two otherwise-identical release builds differ in those
// digits alone (verified: renumbering them is the ONLY delta between clean
// builds). These vars are file-local (`var` in the ESM module scope) and only
// ever referenced within the same file, so a consistent per-file renumbering
// in first-occurrence order is semantics-preserving — and makes the committed
// dist byte-deterministic, which deploy/check-dist.sh requires.
// (Canon copy: train-kit; this one walks dist/ recursively — rl/, fleet/.)
import { readdirSync, readFileSync, writeFileSync, statSync } from "node:fs";
import { join } from "node:path";

const DIST = new URL("../dist", import.meta.url).pathname;
const DYN = /([A-Za-z0-9_$]+\$dyn)_(\d+)/g;

function walk(dir) {
  for (const name of readdirSync(dir)) {
    const p = join(dir, name);
    if (statSync(p).isDirectory()) walk(p);
    else if (name.endsWith(".js")) fix(p);
  }
}

function fix(file) {
  const src = readFileSync(file, "utf8");
  const mapping = new Map();
  const out = src.replace(DYN, (m, stem) => {
    if (!mapping.has(m)) mapping.set(m, `${stem}_${mapping.size + 1}`);
    return mapping.get(m);
  });
  if (out !== src) writeFileSync(file, out);
}

walk(DIST);
console.log("normalize-gensyms: dyn vars renumbered deterministically");

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/dev.git