Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

2.9 KB
Raw Blame
#!/usr/bin/env node
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
// Generates kernel/src/display-table.ts — the frozen set of code points a bento/id
// display name (bind. name, dev. label) may not contain.
//
//   node scripts/gen-display-table.ts
//
// It REFUSES to run unless the runtime's Unicode tables are exactly the frozen
// version, so regenerating on a newer Node can never silently change the table.
// Changing the version is a deliberate format decision: bump UNICODE, run on a
// matching runtime, regenerate identity-v1.json, and record it in DECISIONS.

import { writeFileSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const UNICODE = '17.0'
if (process.versions.unicode !== UNICODE) {
  console.error(`refusing: this runtime has Unicode ${process.versions.unicode}; the table is frozen at ${UNICODE}`)
  process.exit(1)
}

// Refused: controls, private use, UNASSIGNED (including the 66 noncharacters),
// line and paragraph separators, every format character except ZWNJ/ZWJ, and the
// Hangul fill letters. Unassigned code points are refused because one assigned
// LATER could be an invisible format character, and NFC is stable across Unicode
// versions only for assigned characters (security, #764).
const RE = /[\p{Cc}\p{Co}\p{Cn}\p{Zl}\p{Zp}ᅟᅠㅤᅠ]|(?![‌‍])\p{Cf}/u

const ranges: [number, number][] = []
let start = -1
for (let c = 0; c <= 0x10ffff; c++) {
  if (c >= 0xd800 && c <= 0xdfff) { // surrogates never survive strict UTF-8 decoding
    if (start >= 0) { ranges.push([start, c - 1]); start = -1 }
    continue
  }
  const hit = RE.test(String.fromCodePoint(c))
  if (hit && start < 0) start = c
  if (!hit && start >= 0) { ranges.push([start, c - 1]); start = -1 }
}
if (start >= 0) ranges.push([start, 0x10ffff])

const hex = (n: number) => '0x' + n.toString(16)
const body = ranges.map(([a, b]) => `${hex(a)},${hex(b)}`).join(',\n  ')
const out = `// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
// GENERATED by scripts/gen-display-table.ts — do not edit by hand.
//
// The code points a bento/id display name may not contain, frozen at Unicode
// ${UNICODE}: controls, private use, unassigned (with the noncharacters), line and
// paragraph separators, format characters except ZWNJ/ZWJ, and the Hangul fill
// letters. ${ranges.length} inclusive ranges, sorted and disjoint, as a flat list of
// [first, last] pairs. kernel/fixtures/identity-v1.json carries the same table
// for the native hosts, which embed it rather than reading their own platform's
// (drifting) Unicode tables.

export const DISPLAY_FORBIDDEN_UNICODE = '${UNICODE}'

export const DISPLAY_FORBIDDEN_FLAT: readonly number[] = Object.freeze([
  ${body},
])
`
const file = join(dirname(fileURLToPath(import.meta.url)), '..', 'kernel/src/display-table.ts')
writeFileSync(file, out)
console.log(`wrote ${file}: ${ranges.length} ranges at Unicode ${UNICODE}`)