ownCode

tcmts.cli.suites.ownCode · #ejtw2vvz · term · runs on the server

uses node:crypto

A definition's own code, as a suite's result depends on it: its IR, referring to other definitions by name rather than hash. A definition the suite didn't run changing re-hashes the ones using it, but leaves what the suite did as it was. Named as they're named now, or by nameOf (as a run started: #3h4tae6e); one with no name, by hash.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { withoutCyclePositions } from "#codebase/tcmts/cli/main";
import type { DefRecord } from "#codebase/tcmts/core/types";
import crypto from "node:crypto";

export function ownCode(
  cb: Codebase,
  rec: DefRecord,
  nameOf = (h: string): string | undefined => cb.store.pathsOf(h)[0],
): string {
  const ir = withoutCyclePositions(rec.ir).replace(/\["Ref","([a-z2-7]+)"\]/g, (_, h: string) =>
    JSON.stringify(["Ref", nameOf(h) ?? h]),
  );
  return crypto.createHash("sha256").update(ir).digest("hex").slice(0, 32);
}

uses

used by