Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created August 12, 2013 20:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiredman/6214648 to your computer and use it in GitHub Desktop.
Save hiredman/6214648 to your computer and use it in GitHub Desktop.
capture class bytes
(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