Created
August 12, 2013 20:10
-
-
Save hiredman/6214648 to your computer and use it in GitHub Desktop.
capture class bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def classbytes (atom {})) | |
(defn bytes-of-forms [form] | |
(push-thread-bindings | |
{clojure.lang.Compiler/LOADER | |
(proxy [clojure.lang.DynamicClassLoader] [@clojure.lang.Compiler/LOADER] | |
(defineClass | |
([name bytes src] | |
(swap! classbytes assoc name bytes) | |
(proxy-super defineClass name bytes src))))}) | |
(try | |
(let [line @clojure.lang.Compiler/LINE | |
column @clojure.lang.Compiler/COLUMN | |
line (if-let [line (:line (meta form))] | |
line | |
line) | |
column (if-let [column (:column (meta form))] | |
column | |
column)] | |
(push-thread-bindings {clojure.lang.Compiler/LINE line | |
clojure.lang.Compiler/COLUMN column}) | |
(try | |
(let [form (macroexpand form)] | |
(cond | |
(and (seq? form) (= 'do (first form))) | |
(do | |
(doseq [f (butlast (rest form))] | |
(bytes-of-forms f)) | |
(bytes-of-forms (last form))) | |
(and (or (instance? clojure.lang.IType form) | |
(coll? form)) | |
(not (and (symbol? (first form)) | |
(.startsWith (name (first form)) "def")))) | |
(let [exp (clojure.lang.Compiler/analyze | |
clojure.lang.Compiler$C/EXPRESSION | |
`(fn [] ~form))] | |
((.eval exp))) | |
:else (let [exp (clojure.lang.Compiler/analyze | |
clojure.lang.Compiler$C/EVAL | |
form)] | |
(.eval exp)))) | |
(finally | |
(pop-thread-bindings)))) | |
(finally | |
(pop-thread-bindings)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment