Skip to content

Commit 9e00804

Browse files
author
dnolen
committed
CLJS-1078: Nashorn REPL should use persistent code cache
Use code cache by default. Can be disabled with :code-cache false. Add plain repl-env* constructor.
1 parent 547d032 commit 9e00804

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/clj/cljs/repl/nashorn.clj

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
[cljs.repl :as repl]
1616
[cljs.compiler :as comp]
1717
[cljs.closure :as closure])
18-
(:import [javax.script ScriptEngine ScriptEngineManager ScriptException]
18+
(:import [javax.script ScriptEngine ScriptEngineManager ScriptException ScriptEngineFactory]
1919
[jdk.nashorn.api.scripting NashornException]))
2020

2121
;; Nashorn Clojurescript repl binding.
@@ -63,14 +63,21 @@
6363

6464
;; Implementation
6565

66-
(defn create-engine []
67-
(if-let [engine (.getEngineByName (ScriptEngineManager.) "nashorn")]
68-
(let [context (.getContext engine)]
69-
(.setWriter context *out*)
70-
(.setErrorWriter context *err*)
71-
engine)
72-
(throw (IllegalArgumentException.
73-
"Cannot find the Nashorn script engine, use a JDK version 8 or higher."))))
66+
(defn create-engine
67+
([] (create-engine nil))
68+
([{:keys [code-cache] :or {code-cache true}}]
69+
(let [args (when code-cache ["-pcc"])
70+
factories (.getEngineFactories (ScriptEngineManager.))
71+
factory (get (zipmap (map #(.getEngineName %) factories) factories) "Oracle Nashorn")]
72+
(if-let [engine (if-not (empty? args)
73+
(.getScriptEngine ^ScriptEngineFactory factory (into-array args))
74+
(.getScriptEngine ^ScriptEngineFactory factory))]
75+
(let [context (.getContext engine)]
76+
(.setWriter context *out*)
77+
(.setErrorWriter context *err*)
78+
engine)
79+
(throw (IllegalArgumentException.
80+
"Cannot find the Nashorn script engine, use a JDK version 8 or higher."))))))
7481

7582
(defn eval-str [^ScriptEngine engine ^String s]
7683
(.eval engine s))
@@ -197,6 +204,12 @@
197204
(when-let [frames (read-string frames-str)]
198205
(vec (map #(update-in %1 [:file] (fn [s] (strip-file-name s output-dir))) frames)))))
199206

207+
(defn repl-env* [{:keys [debug] :as opts}]
208+
(let [engine (create-engine opts)]
209+
(merge
210+
(NashornEnv. engine debug)
211+
opts)))
212+
200213
(defn repl-env
201214
"Create a Nashorn repl-env for use with the repl/repl* method in Clojurescript and as the
202215
:repl-env argument to piggieback/cljs-repl. Besides the usual repl options (e.g. :source-map),
@@ -205,8 +218,5 @@
205218
:output-dir the directory of the compiled files, e.g. \"resources/public/my-app\" (mandatory).
206219
:output-to load this file initially into Nashorn, relative to output-dir.
207220
Use a minimal bootstrapped cljs.core environment if not specified."
208-
[& {:keys [debug] :as opts}]
209-
(let [engine (create-engine)]
210-
(merge
211-
(NashornEnv. engine debug)
212-
opts)))
221+
[& {:as opts}]
222+
(repl-env* opts))

0 commit comments

Comments
 (0)