JS DSL for java bytecode "symbolic assembler"
Jaroslav Bachorik
jaroslav.bachorik at oracle.com
Wed Feb 12 08:24:18 PST 2014
Per Sundar's recommendation I am posting this small heads up.
I've created a small tool called "jvmasm", currently located at GitHub
(https://github.com/jbachorik/jvmasm) which allows hand crafting java
bytecode using JavaScript.
A sample class definition would look like this:
var classDef = {
name: "pkg.MyClass",
methods: [
ctr = {
name: "<init>",
exceptions: "java.lang.Exception",
code: function(g) {
with(g) {
ALOAD(self("java.lang.Object"))
INVOKESPECIAL("java.lang.Object", "", null, null)
ALOAD(self("pkg.MyClass"))
INVOKEVIRTUAL("pkg.MyClass", "finalMethod", null,
"java.lang.String")
RETURN()
}
}
},
finalMethod = {
name: "finalMethod",
access: Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL,
ret: "java.lang.String",
code: function(g) {
with (g) {
// need start and end labels for the var scope
var l1 = LABEL()
var l2 = LABEL()
// declare a variable
// VAR(name, type, genericSignature, startLabel,
endLabel)
var x = VAR("x", Type.INT_TYPE, null, l1, l2)
LABEL(l1)
// put an integer constant of 10 on stack
ICONST(10)
// store the stack top value to the *x* variable
ISTORE(x)
// put a long constant of 24325 on stack
LCONST(24325)
// load the *x* variable and put it on stack
ILOAD(x)
// put a string constant on stack
LDC("Got it")
LABEL(l2)
// return the stack top value (a string constant)
ARETURN()
}
}
}
]
}
Should you find this tool useful, please, let me know.
Thanks,
-JB-
More information about the nashorn-dev
mailing list