ClojureScript can compile itself
Like the whole clojurescript community, we were excited when David Nolen published on July 29, 2015:
ClojureScript can compile itself.
And actually, it’s quite simple: the cljs.js namespace provides two functions for evaluation and compilation of clojurescript expressions:
eval-strfor evaluation ofclojurescriptexpressionscompile-strfor compilation ofclojurescriptexpressions
In this article, we are going to let you experiment cljs.js inside KLIPSE.
Evaluation
Let’s start by evaluating (map inc [1 2 3]):
(ns my.main
(:require [cljs.js :as cljs]))
(cljs/eval-str (cljs/empty-state)
"(ns my.user) (map inc [1 2 3])"
""
{:eval cljs/js-eval}
identity)
Feel free to play with the second argument to eval-str and see the result of the evaluation.
Here is eval-str documentation and if you are really curious you can also read empty-state documentation.
Compilation
You can also play with compile-str:
(cljs/compile-str (cljs/empty-state)
"(ns my.user) (map inc [1 2 3])"
""
{:eval cljs/js-eval}
identity)
Vertigo
The funny thing about this article is that KLIPSE itself evaluates the code through cljs.js.
Every time I am trying to think about it, I feel dizzy !?!?!
A piece of code that evaluates a piece of code that evaluates a piece of code!
Please share your thoughts (and your feelings) in the comments below.
In an upcoming article, we will demonstrate advanced features of cljs.js like: expression vs. statement, macros, advanced compilation and more…
Clojurescript rocks!