Skip to content

Instantly share code, notes, and snippets.

@ArjixWasTaken
Last active March 21, 2022 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArjixWasTaken/ef18ac61a090471c25e1442cfd52eb7b to your computer and use it in GitHub Desktop.
Save ArjixWasTaken/ef18ac61a090471c25e1442cfd52eb7b to your computer and use it in GitHub Desktop.
function_signature_to_json
const func = `
private static i(int arg0, boolean arg1) { //(IZ)Z
iconst_3
istare2
ilosasd
asdauishd
asdasd
goto L2
L2 {
asdasd
asnjma
}
}
`;
const func2sig = (/**@type {string}*/ funcText, name) => {
const accessors = funcText
.trim()
.match(/^[\w\s]+/)?.[0]
.split(/\s+/g)
.map((i) => i.toUpperCase());
accessors.pop();
const returns = funcText.match(/\/\/.*?\)(.*)$/m)?.[1];
const params = funcText
.match(/\((.*?)\)/)?.[1]
.split(",")
.map((item) => item.trim().split(/\s+/)?.[0]);
const parameters = [];
for (const param of params) {
if (funcText.includes("L" + param.replace(/\./g, "/"))) {
parameters.push("O");
} else {
switch (param) {
case "boolean":
parameters.push("Z");
break;
case "byte":
parameters.push("B");
break;
case "short":
parameters.push("S");
break;
case "char":
parameters.push("C");
break;
case "int":
parameters.push("I");
break;
case "long":
parameters.push("J");
break;
case "float":
parameters.push("F");
break;
case "double":
parameters.push("D");
break;
default:
break;
}
}
}
const opRegex = /{.*?\x29\w(.*?)\s*}\s*$/s;
const opcodes = funcText
.match(opRegex)?.[1]
.replace(/(\w+\s+\{.*?\})/gs, "")
.trim()
.split("\n")
.map((a) => a.trim())
.filter(Boolean)
.map((a) => a.split(/\s+/)?.[0])
.map((item) => {
if (item.includes("_")) return item;
const match = item.match(/(.*?)(\d+)/);
if (!match) return item;
return `${match[1]}_${match[2]}`;
});
return {
name,
returns,
accessors,
parameters,
opcodes,
};
};
console.log(JSON.stringify(func2sig(func, "whatev-patch"), null, 3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment