[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openjdk-nashorn-dev
Subject:    JS DSL for java bytecode "symbolic assembler"
From:       Jaroslav Bachorik <jaroslav.bachorik () oracle ! com>
Date:       2014-02-12 16:24:18
Message-ID: 52FBA032.7080902 () oracle ! com
[Download RAW message or body]

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-
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic