waforth/scripts/generate-dictionary-entry.js
2022-05-08 14:31:43 +02:00

52 lines
1.2 KiB
JavaScript
Executable file

#!/usr/bin/env node
// Usage: ./scripts/generate-dictionary-entry.js "\$foo" FOO 0 0x21820 0x2182c 0xa1
const _ = require("lodash");
const process = require("process");
function encodeLE(n, align) {
return (
"\\" +
_.padStart(n.toString(16), align * 2, "0")
.match(/.{2}/g)
.reverse()
.join("\\")
);
}
const funcName = process.argv[2];
const name = process.argv[3];
const flags = parseInt(process.argv[4]);
const latest = parseInt(process.argv[5]);
const here = parseInt(process.argv[6]);
const nextTableIndex = parseInt(process.argv[7]);
const dictionaryEntry = [
encodeLE(latest, 4),
encodeLE(name.length | flags, 1),
_.padEnd(
name +
_.repeat(
"\\00",
4 * Math.floor((name.length + 4) / 4) - (name.length + 1)
)
),
encodeLE(nextTableIndex, 4),
];
console.log(
"(data (i32.const 0x" +
here.toString(16) +
') "' +
dictionaryEntry.join('" "') +
'")'
);
console.log(
"(elem (i32.const 0x" + nextTableIndex.toString(16) + ") " + funcName + ")"
);
console.log("latest: 0x" + here.toString(16));
console.log(
"here: 0x" +
(here + dictionaryEntry.join("").replace(/\\../g, "_").length).toString(16)
);
console.log("!nextTableIndex: 0x" + (nextTableIndex + 1).toString(16));