// writes the language server's javascript image: build/js/lsp.bin, the server's // genome (build/lsp/lsp_genome.bin, made by src/tools/mklsp.sh), and // build/js/lsp.json, the map it was built for, read from build/lsp/memmap.h. the // vm needs the layout, because the server's map is not the compiler's. the image // is generated, so it goes to build/js/ and nothing is left in the tree. // // node src/js/extract_lsp.js [--lsp DIR] [--out DIR] // // build the lsp first (`make lsp`, or src/tools/mklsp.sh). const fs = require('fs'); const path = require('path'); const root = path.join(__dirname, '..', '..'); const args = process.argv.slice(2); if (args.includes('--help') || args.includes('-h')) { console.log([ 'usage: node src/js/extract_lsp.js [--lsp DIR] [--out DIR]', 'copies the language server genome beside a json description of its memory map.', 'options:', ' --lsp DIR the language server build to read; defaults to build/lsp', ' --out DIR where to write lsp.bin and lsp.json; defaults to build/js', ' --help, -h show this help and exit', ].join('\n')); process.exit(0); } let out = path.join(root, 'build', 'js'), lsp = path.join(root, 'build', 'lsp'); for (let i = 0; i < args.length; i++) { if (args[i] === '--out' && args[i + 1]) out = args[++i]; else if (args[i] === '--lsp' && args[i + 1]) lsp = args[++i]; else { console.error('extract_lsp: unexpected arguments; use --help for usage'); process.exit(2); } } const genomePath = path.join(lsp, 'lsp_genome.bin'); const mapPath = path.join(lsp, 'memmap.h'); if (!fs.existsSync(genomePath)) { console.error('extract_lsp: no language server image at ' + genomePath + '; run `make lsp` first'); process.exit(1); } const { mapValues } = require('./mapvalues.js'); const layout = mapValues(mapPath, ['GORG', 'DS0', 'DSLIM', 'RS0', 'RSLIM', 'SYSGATE', 'ZFP', 'HEAP_LO', 'HEAP_HI']); const image = fs.readFileSync(genomePath); if (image.length % 8) throw new Error(genomePath + ' is not a whole number of words'); const glen = image.length / 8; fs.mkdirSync(out, { recursive: true }); fs.copyFileSync(genomePath, path.join(out, 'lsp.bin')); fs.writeFileSync(path.join(out, 'lsp.json'), JSON.stringify({ gorg: layout.GORG, glen, layout })); console.log('wrote ' + path.relative(root, path.join(out, 'lsp.bin')) + ': genome ' + glen + ' words at ' + layout.GORG + ', heap ' + layout.HEAP_LO + '..' + layout.HEAP_HI);