Bootstrapping ClojureScript on iOS

June 18, 2015

Recently, David Nolen has been working on revising the ClojureScript compiler codebase, using reader conditionals, so that the analyzer and compiler can be compiled in ClojureScript itself.

One use case I envision is a future standalone iOS REPL that lets you play around with ClojureScript on a device without a network connection. (Think of kids using an iPad to learn ClojureScript.)

So, I've been following along, trying David's stuff on iOS.



With David's cljs-bootstrap repository, this is actually quite easy to do. Simply add Ambly as an additional dependency to his project.clj (add [org.omcljs/ambly "0.6.0"] to the :dependencies section) and create an alternative REPL script that starts Ambly instead of the Node REPL:

(require '[cljs.repl :as repl])
(require '[ambly.core :as ambly])

(repl/repl (ambly/repl-env))

After this, check out Ambly and run its included iOS demo app. With this in place you can try things out, and the analyzer and compiler will be running directly in either your iOS simulator or device!

As of this writing, the analyzer works for simple expressions!

If you evaluate

(require-macros 
  '[cljs.env.macros :refer [ensure]])
(require '[cljs.analyzer :as ana])
(pprint 
  (ensure 
    (ana/analyze-keyword
      (assoc (ana/empty-env) 
        :context :expr)
      :foo)))

you will get back

{:op :constant,
 :env
 {:ns nil,
  :context :expr,
  :locals {},
  :fn-scope [],
  :js-globals
  {console {:name console},
   location {:name location},
   escape {:name escape},
   screen {:name screen},
   global {:name global},
   process {:name process},
   require {:name require},
   alert {:name alert},
   history {:name history},
   window {:name window},
   module {:name module},
   exports {:name exports},
   document {:name document},
   navigator {:name navigator},
   unescape {:name unescape}}}, 
 :form :foo, 
 :tag cljs.core/Keyword}

My intent is to follow along the progress in the compiler. In my view, the ability to bootstrap ClojureScript in JavaScriptCore on iOS is way too much fun to be missed out on!

Tags: Ambly ClojureScript Bootstrap