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:

  1. eval-str for evaluation of clojurescript expressions
  2. compile-str for compilation of clojurescript expressions

In this article, we are going to let you experiment cljs.js inside KLIPSE.


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}

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.


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}


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!