Skip to content

Instantly share code, notes, and snippets.

@mfikes
Created July 17, 2018 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfikes/d823034ca93281223ffff23c972bc156 to your computer and use it in GitHub Desktop.
Save mfikes/d823034ca93281223ffff23c972bc156 to your computer and use it in GitHub Desktop.
Diff from Nashorn to Graal.js REPL env code
$ diff nashorn.clj graaljs.clj
9c9
< (ns cljs.repl.nashorn
---
> (ns cljs.repl.graaljs
24c24
< (util/compile-if (Class/forName "jdk.nashorn.api.scripting.NashornException")
---
> (util/compile-if (Class/forName "com.oracle.truffle.js.scriptengine.GraalJSScriptEngine")
26c26,28
< (import 'jdk.nashorn.api.scripting.NashornException)
---
> #_(import '(com.oracle.truffle.js.runtime GraalJSException))
> (import '(com.oracle.truffle.js.scriptengine GraalJSScriptEngine))
> (import '(org.graalvm.polyglot Context))
30,37c32,39
< ([] (create-engine nil))
< ([{:keys [code-cache] :or {code-cache true}}]
< (let [args (when code-cache ["-pcc"])
< factories (.getEngineFactories (ScriptEngineManager.))
< factory (get (zipmap (map #(.getEngineName %) factories) factories) "Oracle Nashorn")]
< (if-let [engine (if-not (empty? args)
< (.getScriptEngine ^ScriptEngineFactory factory (into-array args))
< (.getScriptEngine ^ScriptEngineFactory factory))]
---
> ([opts]
> (let [factories (.getEngineFactories (ScriptEngineManager.))
> factory (get (zipmap (map #(.getEngineName %) factories) factories) "Graal.js")]
> (if-let [#_#_engine (.getScriptEngine ^ScriptEngineFactory factory)
> engine (try
> (GraalJSScriptEngine/create nil (-> (Context/newBuilder (make-array String 0)) (.allowHostAccess true) (.allowCreateThread true) (.allowNativeAccess true)))
> (catch Throwable t
> (.printStackTrace t)))]
43c45
< "Cannot find the Nashorn script engine, use a JDK version 8 or higher."))))))
---
> "Cannot find the Graal.js script engine, GraalVM."))))))
60c62
< (eval-resource engine "cljs/bootstrap_nashorn.js" debug)
---
> (eval-resource engine "cljs/bootstrap_graaljs.js" debug)
67c69
< (eval-str engine "nashorn_tear_down();"))
---
> (eval-str engine "graaljs_tear_down();"))
70c72
< (eval-str engine (format "nashorn_load(\"%s\");" file)))
---
> (eval-str engine (format "graaljs_load(\"%s\");" file)))
72c74
< ;; Create a minimal build of Clojurescript from the core library.
---
> ;; Create a minimal build of ClojureScript from the core library.
76c78
< (let [deps-file ".nashorn_repl_deps.js"
---
> (let [deps-file ".graaljs_repl_deps.js"
80,81c82,83
< (closure/src-file->target-file
< core (dissoc opts :output-dir))))
---
> (closure/src-file->target-file
> core (dissoc opts :output-dir))))
95c97
< ;; Nashorn script stacktraces have a relative path which includes the output-dir
---
> ;; Graal.js script stacktraces have a relative path which includes the output-dir
104c106
< (defrecord NashornEnv [engine debug]
---
> (defrecord GraalJSEnv [engine debug]
107,108c109,110
< {:output-dir ".cljs_nashorn_repl"
< :target :nashorn})
---
> {:output-dir ".cljs_graaljs_repl"
> :target :graaljs})
142c144
< :stacktrace (NashornException/getScriptStackString root-cause)}))
---
> :stacktrace nil #_(GraalJSException/getScriptStackString root-cause)})) ; FIXME
148,151c150,153
< (apply str
< (interpose "\n"
< (map str
< (.getStackTrace root-cause))))}))))
---
> (apply str
> (interpose "\n"
> (map str
> (.getStackTrace root-cause))))}))))
159c161
< (assoc ret :ua-product :nashorn) opts))
---
> (assoc ret :ua-product :graaljs) opts))
169c171
< (NashornEnv. engine debug)
---
> (GraalJSEnv. engine debug)
173c175
< "Create a Nashorn repl-env for use with the repl/repl* method in Clojurescript."
---
> "Create a Graal.js repl-env for use with the repl/repl* method in ClojureScript."
185c187
< (throw (ex-info "Nashorn not supported" {:type :repl-error})))
---
> (throw (ex-info "Graal.js not supported" {:type :repl-error})))
188c190
< "Create a Nashorn repl-env for use with the repl/repl* method in Clojurescript."
---
> "Create a Graal.js repl-env for use with the repl/repl* method in ClojureScript."
190c192
< (throw (ex-info "Nashorn not available under this Java runtime" {:type :repl-error})))
---
> (throw (ex-info "Graal.js not available under this Java runtime" {:type :repl-error})))
193,196c195
< (throw (ex-info "Nashorn not available under this Java runtime" {:type :repl-error})))))
<
<
<
---
> (throw (ex-info "Graal.js not available under this Java runtime" {:type :repl-error})))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment