|
15 | 15 | [cljs.repl :as repl]
|
16 | 16 | [cljs.compiler :as comp]
|
17 | 17 | [cljs.closure :as closure])
|
18 |
| - (:import [javax.script ScriptEngine ScriptEngineManager ScriptException] |
| 18 | + (:import [javax.script ScriptEngine ScriptEngineManager ScriptException ScriptEngineFactory] |
19 | 19 | [jdk.nashorn.api.scripting NashornException]))
|
20 | 20 |
|
21 | 21 | ;; Nashorn Clojurescript repl binding.
|
|
63 | 63 |
|
64 | 64 | ;; Implementation
|
65 | 65 |
|
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.")))))) |
74 | 81 |
|
75 | 82 | (defn eval-str [^ScriptEngine engine ^String s]
|
76 | 83 | (.eval engine s))
|
|
197 | 204 | (when-let [frames (read-string frames-str)]
|
198 | 205 | (vec (map #(update-in %1 [:file] (fn [s] (strip-file-name s output-dir))) frames)))))
|
199 | 206 |
|
| 207 | +(defn repl-env* [{:keys [debug] :as opts}] |
| 208 | + (let [engine (create-engine opts)] |
| 209 | + (merge |
| 210 | + (NashornEnv. engine debug) |
| 211 | + opts))) |
| 212 | + |
200 | 213 | (defn repl-env
|
201 | 214 | "Create a Nashorn repl-env for use with the repl/repl* method in Clojurescript and as the
|
202 | 215 | :repl-env argument to piggieback/cljs-repl. Besides the usual repl options (e.g. :source-map),
|
|
205 | 218 | :output-dir the directory of the compiled files, e.g. \"resources/public/my-app\" (mandatory).
|
206 | 219 | :output-to load this file initially into Nashorn, relative to output-dir.
|
207 | 220 | 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