Compiling clojure fragments (script) in javascript

Where in the clojurescript library can I access a function to compile clojure fragments in js?

I need this to execute in clojure (not clojurescript):

(->js '(fn [x y] (+ x y)))
=> "function(x,y){return x+y}" 
+4
source share
2 answers

Collecting Slices from Clojure REPL

(require '[cljs.analyzer.api :refer [analyze empty-env]])
(require '[cljs.compiler.api :refer [emit]])

(let [ast (analyze (empty-env) '(defn plus [a b] (+ a b)))]
  (emit ast))

;; result
"cljs.user.plus = (function cljs$user$plus(a,b){\nreturn (a + b);\n});\n"

Collecting fragments from ClojureScript REPL:

(require '[cljs.js :refer [empty-state compile-str]])

(compile-str (empty-state) "(defn add [x y] (+ x y))" #(println (:value %)))

;; Output (manually formatted for easier reading)
cljs.user.add = (function cljs$user$add(x,y){
  return (x + y);
});

compile-strtakes a callback as the last argument. It will be called with a card with either a key :valuecontaining the JS result as a string, or :errorwith a compilation error.

In both cases, org.clojure/tools.readernecessary for your class path.

+5
source

All Articles