Permalink
Browse files

Data structure disassembler.

  • Loading branch information...
aphyr committed Jan 9, 2014
1 parent 59c333a commit 0bfb0456c4f73d77132fb3913e9e865f8a391915
Showing with 753 additions and 5 deletions.
  1. +84 −1 README.md
  2. +2 −2 project.clj
  3. +11 −2 src/no/disassemble.clj
  4. +656 −0 src/no/disassemble/r.clj
View
@@ -64,7 +64,90 @@ WARNING: there is no cleanup of bytecode yet, so evaling a lot would surely exha
}
nil
no.disassemble=>
-
+
+You can also manipulate the disassembly as a Clojure data structure:
+
+```clj
+user=> (-> (fn []) disassemble-data pprint)
+{:minor-version 0,
+ :superclass-name clojure/lang/AFunction,
+ :class? true,
+ :major-version 49,
+ :name user$eval1321$fn__1322,
+ :interface-names [],
+ :methods
+ ({:access-flags #{:public :static},
+ :name <clinit>,
+ :clinit? true,
+ :deprecated? false,
+ :code
+ {:max-locals 0,
+ :max-stack 0,
+ :line-numbers {0 1},
+ :local-variables nil,
+ :exception-table (),
+ :raw-bytecode [-79],
+ :bytecode [[:return 0]],
+ :code-length 1},
+ :descriptor "()V",
+ :constructor? false,
+ :exception nil,
+ :synthetic? false}
+ {:access-flags #{:public},
+ :name <init>,
+ :clinit? false,
+ :deprecated? false,
+ :code
+ {:max-locals 1,
+ :max-stack 1,
+ :line-numbers {0 1},
+ :local-variables nil,
+ :exception-table (),
+ :raw-bytecode [42, -73, 0, 10, -79],
+ :bytecode
+ [[:aload_0 0]
+ [:invokespecial
+ 1
+ 10
+ {:kind :methodref,
+ :class-name "clojure/lang/AFunction",
+ :method-name "<init>",
+ :method-descriptor "()V"}]
+ [:return 4]],
+ :code-length 5},
+ :descriptor "()V",
+ :constructor? true,
+ :exception nil,
+ :synthetic? false}
+ {:access-flags #{:public},
+ :name invoke,
+ :clinit? false,
+ :deprecated? false,
+ :code
+ {:max-locals 1,
+ :max-stack 1,
+ :line-numbers {0 1},
+ :local-variables
+ ({:name this,
+ :descriptor "Ljava/lang/Object;",
+ :length 1,
+ :start-pc 0}),
+ :exception-table (),
+ :raw-bytecode [1, -80],
+ :bytecode [[:aconst_null 0] [:areturn 1]],
+ :code-length 2},
+ :descriptor "()Ljava/lang/Object;",
+ :constructor? false,
+ :exception nil,
+ :synthetic? false}),
+ :attributes
+ ("NO_SOURCE_FILE"
+ {:type org.eclipse.jdt.internal.core.util.ClassFileAttribute,
+ :name "SourceDebugExtension",
+ :length 94}),
+ :fields (),
+ :interface? false}
+```
* It can even disassemble itself
View
@@ -5,8 +5,8 @@
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
- :dependencies [[org.eclipse.jdt/core "3.3.0-v_771"]
- [org.eclipse.equinox/app "1.0.0-v20070606"]
+ :dependencies [[org.eclipse.jdt/org.eclipse.jdt.core "3.7.1"]
+; [org.eclipse.equinox/app "1.0.0-v20070606"]
[org.clojure/clojure "1.4.0"]]
:java-source-paths ["java"]
View
@@ -1,4 +1,5 @@
(ns no.disassemble
+ (:require [no.disassemble.r :as disassembler])
(:import [org.eclipse.jdt.internal.core.util Disassembler]))
(defn- classes
@@ -16,8 +17,16 @@
:compact 8
:working-copy 16})
-(defn disassemble
+(defn disassemble-str
+ "Emits a string bytecode disassembly of an object or class."
[obj]
(let [cls-name (sanitize (.getCanonicalName (if (class? obj) obj (class obj))))
bytecode (get (classes) cls-name)]
- (.disassemble (Disassembler.) bytecode "\n" (:detailed levels))))
+ (.disassemble (Disassembler.) bytecode "\n" (:detailed levels))))
+
+(defn disassemble-data
+ "Emits a data structure disassembly of an object or class."
+ [obj]
+ (disassembler/disassemble obj))
+
+(def disassemble disassemble-str)
Oops, something went wrong.

0 comments on commit 0bfb045

Please sign in to comment.