samedi 20 février 2016

java8 nashorn javascript and clojure run into a repl

Java javascript engine (nashorn) is easily accessible through the javax.script fAPI. So I popped a clojure repl to instanciate a javascript compiler so Java could run my js bytecode.

(ns three.lisp.tower)

(def js (.getEngineByName (javax.script.ScriptEngineManager.) "nashorn"))

(defn jsc [code]
          (let [prelude "{add: function(a,b){return a+b;}, inc: function(n){return n+1;}}",
             link "(function(prelude){return (function(prelude){ %s })(prelude);})(%s)"
             emit (fn [l p s] (format l s p))
             js (fn [s] (.eval js s))]
        (js (emit link prelude code))))


user=> (jsc "return Array(1,2,3).map(prelude.inc).reduce(prelude.add)")
9.0

#EnoughProcrastination

Aucun commentaire:

Enregistrer un commentaire