Skip to content

Instantly share code, notes, and snippets.

@boomboompower
Created May 31, 2023 06:48
Show Gist options
  • Save boomboompower/cc08e94942363c18bfa3982bab8d5eb9 to your computer and use it in GitHub Desktop.
Save boomboompower/cc08e94942363c18bfa3982bab8d5eb9 to your computer and use it in GitHub Desktop.
Parses the latest hermes operands into a json format file.
import fetch from 'node-fetch'
interface GroupedRegExpMatchArray extends RegExpMatchArray {
groups?: {
define: string,
name: string,
options?: string
}
}
interface StringRegExpMatchArray extends RegExpMatchArray {
groups?: {
name: string,
index: string
}
}
(async () => {
const url = 'https://raw.githubusercontent.com/facebook/hermes/main/include/hermes/BCGen/HBC/BytecodeList.def';
const latestOpcodes = await fetch(url).then(async (o) => {
return await o.text()
})
const lines = latestOpcodes.split('\n')
const regex = new RegExp(/^DEFINE_(?<define>(OPCODE|JUMP)_\d)\((?<name>[A-Za-z0-9]+)(?<options>(, [A-Za-z0-9]+)+?)?\)/);
const stringRegex = new RegExp(/OPERAND_STRING_ID\((?<name>[A-Za-z0-9]+), (?<index>\d+)\)/);
const resultingObjects: {[name: string]: Array<string>} = {}
function handleLine(line: string) {
const result: GroupedRegExpMatchArray | null = line.match(regex) as any;
if (!result) {
return;
}
if (resultingObjects[result.groups.name]) {
console.log('Already registered ', result.groups.name)
} else {
let options = result.groups.options ? result.groups.options.substring(2) : '';
switch (result.groups.define) {
case 'JUMP_1': {
handleLine(`DEFINE_OPCODE_1(${result.groups.name}, Addr8)`);
handleLine(`DEFINE_OPCODE_1(${result.groups.name}Long, Addr32)`);
return;
}
case 'JUMP_2': {
handleLine(`DEFINE_OPCODE_2(${result.groups.name}, Addr8, Reg8)`);
handleLine(`DEFINE_OPCODE_2(${result.groups.name}Long, Addr32, Reg8)`);
return;
}
case 'JUMP_3': {
handleLine(`DEFINE_OPCODE_3(${result.groups.name}, Addr8, Reg8, Reg8)`);
handleLine(`DEFINE_OPCODE_3(${result.groups.name}Long, Addr32, Reg8, Reg8)`);
return;
}
}
let array: Array<string> = [];
if (options.indexOf(', ') >= 0) {
array.push(...options.split(', '))
} else if (options.trim().length > 0) {
array.push(options)
}
resultingObjects[result.groups.name] = array;
return result.groups.name
}
}
function handleStringInjection(line: string) {
const result: StringRegExpMatchArray | null = line.match(stringRegex) as any;
if (!result) {
console.error('Unhandled string line ' + line)
return;
}
let arr = resultingObjects[result.groups.name];
arr[parseInt(result.groups.index) - 1] += ':S'
resultingObjects[result.groups.name] = arr;
}
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.indexOf('OPERAND_STRING_ID') >= 0) {
handleStringInjection(line)
} else {
handleLine(line);
}
}
console.log(JSON.stringify(resultingObjects, null, 4))
console.log(`Ended up with ${Object.keys(resultingObjects).length} keys.`)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment