Skip to content

Commit

Permalink
feat: modify template
Browse files Browse the repository at this point in the history
  • Loading branch information
zlataovce committed Sep 2, 2024
1 parent a2f7d4d commit 806f7e5
Show file tree
Hide file tree
Showing 13 changed files with 969 additions and 152 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.idea/
dist/
node_modules/
node_modules/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Krakatau"]
path = Krakatau
url = https://github.com/run-slicer/Krakatau.git
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pnpm-lock.yaml
dist/
Krakatau/
pnpm-lock.yaml
1 change: 1 addition & 0 deletions Krakatau
Submodule Krakatau added at 3ad20f
698 changes: 674 additions & 24 deletions LICENSE

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# script-template
# script-krak

A Rollup+TypeScript template for a project based on the slicer scripting API.
A slicer script binding for the Krakatau Java decompiler.
82 changes: 82 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const krakScript = `from pyodide.http import pyfetch
response = await pyfetch("https://cdn.jsdelivr.net/gh/run-slicer/script-krak@${"1.0.0"}/dist/krak.zip")
await response.unpack_archive()
from Krakatau.java.visitor import DefaultVisitor
from Krakatau.java.javaclass import generateAST
from Krakatau.ssa import ssaFromVerified
from Krakatau.verifier.inference_verifier import verifyBytecode
from Krakatau.java.stringescape import escapeString
from Krakatau.environment import Environment
from Krakatau.classfile import ClassFile
from Krakatau.classfileformat.reader import Reader
def makeGraph(m):
v = verifyBytecode(m.code)
s = ssaFromVerified(m.code, v, opts=False)
if s.procs:
s.inlineSubprocs()
s.condenseBlocks()
s.mergeSingleSuccessorBlocks()
s.removeUnusedVariables()
s.copyPropagation()
s.abstractInterpert()
s.disconnectConstantVariables()
s.simplifyThrows()
s.simplifyCatchIgnored()
s.mergeSingleSuccessorBlocks()
s.mergeSingleSuccessorBlocks()
s.removeUnusedVariables()
return s
def decompile(data):
e = Environment()
c = ClassFile(Reader(data=bytes(data.to_py())))
c.env = e
e.classes[c.name] = c
c.loadElements()
printer = DefaultVisitor()
source = printer.visit(generateAST(c, makeGraph, skip_errors=True))
if '/' in c.name:
return f'package {escapeString(c.name.replace('/','.').rpartition('.')[0])};\\n\\n{source}'
return source
decompile`;
let decompileFunc = null;
const krak = {
id: "krak",
label: "Krakatau",
language: "java",
async run(data) {
if (!decompileFunc) {
decompileFunc = await import('https://cdn.jsdelivr.net/pyodide/v0.26.2/full/pyodide.mjs')
.then(({ loadPyodide }) => loadPyodide())
.then((pyodide) => pyodide.runPythonAsync(krakScript));
}
return decompileFunc(data);
},
};
var index = {
name: "script-krak",
description: "A script binding for the Krakatau Java decompiler.",
version: "1.0.0",
load(context) {
context.disasm.add(krak);
},
unload(context) {
context.disasm.remove(krak.id);
},
};

export { index as default };
Binary file added dist/krak.zip
Binary file not shown.
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
{
"name": "script-template",
"name": "script-krak",
"version": "1.0.0",
"type": "module",
"author": "",
"license": "Unlicense",
"description": "A Rollup+TypeScript template for a project based on the slicer scripting API.",
"author": "run-slicer",
"license": "GPL-3.0-only",
"description": "A slicer script binding for the Krakatau Java decompiler.",
"scripts": {
"build": "rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
"prepare": "mkdir -p dist; cd Krakatau; find Krakatau LICENSE.TXT -name \"*.py\" -o -name \"LICENSE.TXT\" | zip -FS -@ ../dist/krak.zip",
"build": "pnpm prepare && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
"format": "prettier . --write"
},
"dependencies": {
"@run-slicer/script": "^0.3.0"
"@run-slicer/script": "^0.8.1"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.7",
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^22.4.0",
"@types/node": "^22.5.2",
"prettier": "^3.3.3",
"rollup": "^4.21.0",
"tslib": "^2.6.3",
"rollup": "^4.21.2",
"tslib": "^2.7.0",
"typescript": "^5.5.4"
},
"packageManager": "[email protected]+sha512.faf344af2d6ca65c4c5c8c2224ea77a81a5e8859cbc4e06b1511ddce2f0151512431dd19e6aff31f2c6a8f5f2aced9bd2273e1fed7dd4de1868984059d2c4247"
Expand Down
Loading

0 comments on commit 806f7e5

Please sign in to comment.