Skip to content

Commit

Permalink
automatically setup printing for JS environment with console availble
Browse files Browse the repository at this point in the history
as well as Nashorn
  • Loading branch information
swannodette committed Feb 11, 2018
1 parent d3b9fc0 commit af20aea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/main/cljs/cljs/core.cljs
Expand Up @@ -11447,3 +11447,20 @@ reduces them without incurring seq initialization"
{:added "1.9"}
[x]
(instance? goog.Uri x))

(defn maybe-enable-print! []
(cond
(exists? js/console)
(enable-console-print!)

(identical? *target* "nashorn")
(let [system (.type js/Java "java.lang.System")]
(set! *print-newline* false)
(set! *print-fn*
(fn [& args]
(.println (.-out system) (.join (into-array args) ""))))
(set! *print-err-fn*
(fn [& args]
(.println (.-error system) (.join (into-array args) "")))))))

(maybe-enable-print!)
2 changes: 1 addition & 1 deletion src/main/clojure/cljs/cli.clj
Expand Up @@ -102,7 +102,7 @@ present"
(util/debug-prn "Compiler options:" repl/*repl-opts*))
(comp/with-core-cljs repl/*repl-opts*
(fn []
(repl/-setup renv (merge (repl/-repl-options renv) repl/*repl-opts*))
(repl/-setup renv repl/*repl-opts*)
;; REPLs don't normally load cljs_deps.js
(when (and coptsf (.exists coptsf))
(let [depsf (io/file (:output-dir options) "cljs_deps.js")]
Expand Down
17 changes: 9 additions & 8 deletions src/main/clojure/cljs/repl/nashorn.clj
Expand Up @@ -10,6 +10,7 @@
(:require [clojure.java.io :as io]
[clojure.string :as string]
[clojure.stacktrace]
[clojure.data.json :as json]
[cljs.analyzer :as ana]
[cljs.env :as env]
[cljs.util :as util]
Expand Down Expand Up @@ -51,12 +52,15 @@
(eval-str engine (slurp r))
(when debug (println "loaded: " path))))

(defn init-engine [engine output-dir debug]
(defn init-engine [engine {:keys [output-dir] :as opts} debug]
(eval-str engine (format "var CLJS_DEBUG = %s;" (boolean debug)))
(eval-str engine (format "var CLJS_OUTPUT_DIR = \"%s\";" output-dir))
(eval-resource engine "goog/base.js" debug)
(eval-resource engine "goog/deps.js" debug)
(eval-resource engine "cljs/bootstrap_nashorn.js" debug)
(eval-str engine
(format "goog.global.CLOSURE_UNCOMPILED_DEFINES = %s;"
(json/write-str (:closure-defines opts))))
engine)

(defn load-js-file [engine file]
Expand Down Expand Up @@ -96,20 +100,17 @@
(defrecord NashornEnv [engine debug]
repl/IReplEnvOptions
(-repl-options [this]
{:output-dir ".cljs_nashorn_repl"})
{:output-dir ".cljs_nashorn_repl"
:target :nashorn})
repl/IJavaScriptEnv
(-setup [this {:keys [output-dir bootstrap output-to] :as opts}]
(init-engine engine output-dir debug)
(init-engine engine opts debug)
(let [env (ana/empty-env)]
(if output-to
(load-js-file engine output-to)
(bootstrap-repl engine output-dir opts))
(repl/evaluate-form this env repl-filename
'(do
(.require js/goog "cljs.core")
(set! *print-newline* false)
(set! *print-fn* js/print)
(set! *print-err-fn* js/print)))
'(.require js/goog "cljs.core"))
;; monkey-patch goog.isProvided_ to suppress useless errors
(repl/evaluate-form this env repl-filename
'(set! js/goog.isProvided_ (fn [ns] false)))
Expand Down

0 comments on commit af20aea

Please sign in to comment.