Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inspect java bytecode generated by Clojure from the repl?

I couldn't find much information on this topic. It looks like jswat can be used to accomplish this, but I couldn't find any step-by-step tutorials.

like image 715
crinklywrappr Avatar asked Apr 16 '12 15:04

crinklywrappr


1 Answers

You can use the no-disassemble library for this. https://github.com/gtrak/no.disassemble

Add as a plugin in your Leiningen project: :plugins [[lein-nodisassemble "0.1.3"]]

=> (use 'no.disassemble)
nil
=> (println (disassemble (fn [] (+ 1 2))))
// Compiled from form-init9238501799627991.clj (version 1.5 : 49.0, super bit)
public final class vecperf.bench$eval1426$fn__1427 extends clojure.lang.AFunction {

  // Field descriptor #7 Lclojure/lang/Var;
  public static final clojure.lang.Var const__0;

  // Field descriptor #9 Ljava/lang/Object;
  public static final java.lang.Object const__1;

  // Field descriptor #9 Ljava/lang/Object;
  public static final java.lang.Object const__2;

  // Method descriptor #12 ()V
  // Stack: 2, Locals: 0
  public static {};
     0  ldc <String "clojure.core"> [14]
     2  ldc <String "+"> [16]
     4  invokestatic clojure.lang.RT.var(java.lang.String, java.lang.String) : clojure.lang.Var [22]
     7  checkcast clojure.lang.Var [24]
    10  putstatic vecperf.bench$eval1426$fn__1427.const__0 : clojure.lang.Var [26]
    13  lconst_1
    14  invokestatic java.lang.Long.valueOf(long) : java.lang.Long [32]
    17  putstatic vecperf.bench$eval1426$fn__1427.const__1 : java.lang.Object [34]
    20  ldc2_w <Long 2> [35]
    23  invokestatic java.lang.Long.valueOf(long) : java.lang.Long [32]
    26  putstatic vecperf.bench$eval1426$fn__1427.const__2 : java.lang.Object [38]
    29  return
      Line numbers:
        [pc: 0, line: 1]

  // Method descriptor #12 ()V
  // Stack: 1, Locals: 1
  public bench$eval1426$fn__1427();
    0  aload_0 [this]
    1  invokespecial clojure.lang.AFunction() [41]
    4  return
      Line numbers:
        [pc: 0, line: 1]

  // Method descriptor #43 ()Ljava/lang/Object;
  // Stack: 4, Locals: 1
  public java.lang.Object invoke();
     0  lconst_1
     1  ldc2_w <Long 2> [35]
     4  invokestatic clojure.lang.Numbers.add(long, long) : long [49]
     7  invokestatic clojure.lang.Numbers.num(long) : java.lang.Number [53]
    10  areturn
      Line numbers:
        [pc: 0, line: 1]
        [pc: 0, line: 1]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Object

}
like image 59
Alex Miller Avatar answered Oct 22 '22 05:10

Alex Miller