// Dev utility: run one of the src/examples scripts headlessly through the // same call shape the browser runner uses, time it, and parse the produced // .seq with the JS parser. Usage: node scripts/run-example.mjs import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import { executeCode, VirtualFileSystem, BrowserFileIOAdapter, BrowserSystemAdapter, } from 'numbl' import { parseSeq } from '../src/seq/parseSeq.ts' import { reconstruct } from '../src/seq/reconstruct.ts' const name = process.argv[2] ?? 'fid' const here = path.dirname(fileURLToPath(import.meta.url)) const repoRoot = path.join(here, '..') const mrRoot = path.join(repoRoot, 'src', 'engine', 'pulseq') const MD5_OVERRIDE = `function digest = md5(message, noBuiltIn) digest = hash('MD5', char(message)); end ` const files = [] for (const entry of fs.readdirSync(path.join(mrRoot, '+mr'), { recursive: true })) { const rel = String(entry) const abs = path.join(mrRoot, '+mr', rel) if (!fs.statSync(abs).isFile() || !rel.endsWith('.m')) continue const vfsPath = `+mr/${rel}` files.push({ path: vfsPath, content: vfsPath === '+mr/+aux/md5.m' ? MD5_OVERRIDE : fs.readFileSync(abs, 'utf8'), }) } const script = fs.readFileSync(path.join(repoRoot, 'src', 'examples', `${name}.m`), 'utf8') files.push({ path: 'main.m', content: script }) const enc = new TextEncoder() const vfs = new VirtualFileSystem() for (const f of files) vfs.writeFile('/project/' + f.path, enc.encode(f.content)) vfs.setCwd('/project') const workspaceFiles = files.map((f) => ({ name: f.path, source: f.content })) const t0 = Date.now() try { executeCode( 'main;', { onOutput: (text) => process.stdout.write(text), displayResults: true, maxIterations: 1e9, optimization: '1', fileIO: new BrowserFileIOAdapter(vfs), system: new BrowserSystemAdapter(vfs), }, workspaceFiles, 'repl', [], ) } catch (err) { console.error('run failed:', err.message ?? err) if (err.file) console.error(` at ${err.file}:${err.line}`) process.exit(1) } console.log(`\nrun finished in ${((Date.now() - t0) / 1000).toFixed(1)} s`) const seqName = `${name}.seq` const text = new TextDecoder().decode(vfs.readFile('/project/' + seqName)) const seq = parseSeq(text) const rec = reconstruct(seq) console.log( `${seqName}: ${text.length} bytes, v${seq.version.major}.${seq.version.minor}.${seq.version.revision}, ` + `${seq.blocks.length} blocks, ${rec.rfSpans.length} RF, ${rec.adcSpans.length} ADC, ` + `duration ${rec.duration.toFixed(4)} s, signature ${seq.signature?.valid ? 'valid' : 'MISSING/INVALID'}`, )