Skip to content

Higher Level API

Justin Conklin edited this page Sep 16, 2020 · 5 revisions

Since method bytecode is built from arbitrarily nested Clojure sequences, making your own application-specific API is easy.

(require '[insn.core :as insn])

(defn cat-str [s]
  [[:ldc s]
   [:invokevirtual String "concat" [String String]]])

(def class-data
  {:methods [{:name "toString", :desc [String]
              :emit [[:ldc ""]
                     (map cat-str ["foo," "bar," "quux"])
                     [:areturn]]}]})

(str (insn/new-instance class-data))
;; => "foo,bar,quux"

For more on organizing and combining bytecode snippets, see the jmh-clojure project (it is one of the reasons this library was written). In particular, the jmh.instrument namespace.

Chris Nuernberger has also written an excellent blog post that has a section detailing the use of insn as the bytecode-layer in a translation of a custom AST that describes efficient N-dimensional numerical transformations.