Skip to content

Commit

Permalink
CLJS-3375: Bridge tools.reader (#178)
Browse files Browse the repository at this point in the history
* add cljs.vendor.bridge file to bridge tools.reader and vendorized tools.reader
* return clojure.tools.reader/*alias-map* first
* only auto-bridge when invoking the REPL
  • Loading branch information
swannodette authored May 12, 2022
1 parent ebf9c6e commit d1809c5
Showing 5 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.template.xml
Original file line number Diff line number Diff line change
@@ -278,7 +278,7 @@
<configuration>
<cleanAOTNamespaces>true</cleanAOTNamespaces>
<namespaces>
<namespace>!clojure.tools.reader.*</namespace>
<namespace>!cljs.vendor.bridge</namespace>
</namespaces>
</configuration>
</execution>
12 changes: 10 additions & 2 deletions src/main/clojure/cljs/analyzer.cljc
Original file line number Diff line number Diff line change
@@ -4395,6 +4395,14 @@
((juxt :requires :require-macros :as-aliases)
(get-namespace ns))))

(defn get-bridged-alias-map
"Returns clojure.tools.reader/*alias-map* for bridging"
[]
(try
@(ns-resolve 'clojure.tools.reader '*alias-map*)
(catch Throwable t
nil)))

#?(:clj
(defn forms-seq*
"Seq of Clojure/ClojureScript forms from rdr, a java.io.Reader. Optionally
@@ -4795,7 +4803,7 @@
*unchecked-arrays* false])
*cljs-ns* 'cljs.user
*cljs-file* nil
reader/*alias-map* (or reader/*alias-map* {})]
reader/*alias-map* (or (get-bridged-alias-map) reader/*alias-map* {})]
(loop [ns nil forms forms last-ast nil]
(if (some? forms)
(let [form (first forms)
@@ -4853,7 +4861,7 @@
(if (or skip-cache (not cache) (requires-analysis? res cache output-dir opts))
(binding [*cljs-ns* 'cljs.user
*cljs-file* path
reader/*alias-map* (or reader/*alias-map* {})]
reader/*alias-map* (or (get-bridged-alias-map) reader/*alias-map* {})]
(when (or *verbose* (:verbose opts))
(util/debug-prn "Analyzing" (str res)))
(let [env (assoc (empty-env) :build-options opts)
2 changes: 1 addition & 1 deletion src/main/clojure/cljs/compiler.cljc
Original file line number Diff line number Diff line change
@@ -1559,7 +1559,7 @@
(binding [*out* out
ana/*cljs-ns* 'cljs.user
ana/*cljs-file* (.getPath ^File src)
reader/*alias-map* (or reader/*alias-map* {})
reader/*alias-map* (or (ana/get-bridged-alias-map) reader/*alias-map* {})
ana/*checked-arrays* (or ana/*checked-arrays* (:checked-arrays opts))
ana/*cljs-static-fns* (or ana/*cljs-static-fns* (:static-fns opts))
*source-map-data* (when (:source-map opts)
4 changes: 4 additions & 0 deletions src/main/clojure/cljs/repl.cljc
Original file line number Diff line number Diff line change
@@ -1054,6 +1054,10 @@
[cljs.pprint :refer [pprint] :refer-macros [pp]]]
bind-err true}
:as opts}]
;; bridge clojure.tools.reader to satisfy the old contract
(when (and (find-ns 'clojure.tools.reader)
(not (find-ns 'cljs.vendor.bridge)))
(clojure.core/load "vendor/bridge"))
(doseq [[unknown-opt suggested-opt] (util/unknown-opts (set (keys opts)) (set/union known-repl-opts cljsc/known-opts))]
(when suggested-opt
(println (str "WARNING: Unknown option '" unknown-opt "'. Did you mean '" suggested-opt "'?"))))
42 changes: 42 additions & 0 deletions src/main/clojure/cljs/vendor/bridge.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns cljs.vendor.bridge
(:require [cljs.vendor.clojure.tools.reader.reader-types :as vendor]
[clojure.tools.reader.reader-types :as readers]))

(extend-protocol vendor/Reader
clojure.tools.reader.reader_types.Reader
(read-char [reader]
(readers/read-char reader))
(peek-char [reader]
(readers/peek-char reader)))

(extend-protocol vendor/IPushbackReader
clojure.tools.reader.reader_types.IPushbackReader
(unread [reader ch]
(readers/unread reader ch)))

(extend-protocol vendor/IndexingReader
clojure.tools.reader.reader_types.IndexingReader
(get-line-number [reader]
(readers/get-line-number reader))
(get-column-number [reader]
(readers/get-column-number reader))
(get-file-name [reader]
(readers/get-file-name reader)))

(extend-protocol vendor/ReaderCoercer
clojure.tools.reader.reader_types.ReaderCoercer
(to-rdr [reader]
(readers/to-rdr reader)))

(extend-protocol vendor/PushbackReaderCoercer
clojure.tools.reader.reader_types.PushbackReaderCoercer
(to-pbr [reader buflen]
(readers/to-pbr reader buflen)))

0 comments on commit d1809c5

Please sign in to comment.