Skip to content

Commit c1cf559

Browse files
committed
how we handled externs in compiler state was just not very functional
one problem is that we always *compute* extern information when analyzing files, not having externs in compiler state just means that extern information is wrong. another problem is that :infer-externs may be discovered *after* the compiler evironment is built, meaning we end up back at problem one. just memoize it, make it easier to change our mind later, fix tests
1 parent 5d15026 commit c1cf559

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
(defn compiler-options []
169169
(get @env/*compiler* :options))
170170

171+
(defn get-externs []
172+
(::externs @env/*compiler*))
173+
171174
(defn checked-arrays
172175
"Returns false-y, :warn, or :error based on configuration and the
173176
current value of *unchecked-arrays*."
@@ -1005,7 +1008,7 @@
10051008

10061009
(defn has-extern?
10071010
([pre]
1008-
(has-extern? pre (get @env/*compiler* ::externs)))
1011+
(has-extern? pre (get-externs)))
10091012
([pre externs]
10101013
(or (has-extern?* pre externs)
10111014
(when (= 1 (count pre))
@@ -1018,7 +1021,7 @@
10181021
([pre]
10191022
(js-tag pre :tag))
10201023
([pre tag-type]
1021-
(js-tag pre tag-type (get @env/*compiler* ::externs)))
1024+
(js-tag pre tag-type (get-externs)))
10221025
([pre tag-type externs]
10231026
(js-tag pre tag-type externs externs))
10241027
([pre tag-type externs top]

src/main/clojure/cljs/compiler.cljc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
(sorted-map))))))))))
202202
(emit* ast))
203203

204-
(defn emits
204+
(defn emits
205205
([])
206206
([^Object a]
207207
(cond
@@ -587,12 +587,12 @@
587587
(defn emit-js-array [items comma-sep]
588588
(emits "[" (comma-sep items) "]"))
589589

590-
(defmethod emit* :js-object
590+
(defmethod emit* :js-object
591591
[{:keys [keys vals env]}]
592592
(emit-wrap env
593593
(emit-js-object (map vector keys vals) identity)))
594594

595-
(defmethod emit* :js-array
595+
(defmethod emit* :js-array
596596
[{:keys [items env]}]
597597
(emit-wrap env
598598
(emit-js-array items comma-sep)))
@@ -1133,7 +1133,7 @@
11331133
protocol (:protocol info)
11341134
tag (ana/infer-tag env (first (:args expr)))
11351135
proto? (and protocol tag
1136-
(or (and ana/*cljs-static-fns* protocol (= tag 'not-native))
1136+
(or (and ana/*cljs-static-fns* protocol (= tag 'not-native))
11371137
(and
11381138
(or ana/*cljs-static-fns*
11391139
(:protocol-inline env))
@@ -1817,8 +1817,7 @@
18171817
(defn emit-externs
18181818
([externs]
18191819
(emit-externs [] externs (atom #{})
1820-
(when env/*compiler*
1821-
(::ana/externs @env/*compiler*))))
1820+
(when env/*compiler* (ana/get-externs))))
18221821
([prefix externs top-level known-externs]
18231822
(loop [ks (seq (keys externs))]
18241823
(when ks

src/main/clojure/cljs/env.cljc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ state that is accessed/maintained by many different components."}
4545

4646
(defn default-compiler-env* [options]
4747
(merge
48-
{:cljs.analyzer/namespaces {'cljs.user {:name 'cljs.user}}
48+
{:cljs.analyzer/namespaces {'cljs.user {:name 'cljs.user}}
4949
:cljs.analyzer/constant-table {}
50-
:cljs.analyzer/data-readers {}
51-
:cljs.analyzer/externs #?(:clj (when (:infer-externs options)
52-
(externs/externs-map (:externs-sources options)))
53-
:cljs nil)
54-
:options options}
50+
:cljs.analyzer/data-readers {}
51+
:cljs.analyzer/externs #?(:clj (externs/externs-map (:externs-sources options))
52+
:cljs nil)
53+
:options options}
5554
#?@(:clj [(when (= (:target options) :nodejs)
5655
{:node-module-index deps/native-node-modules})
5756
{:js-dependency-index (deps/js-dependency-index options)}])))

src/main/clojure/cljs/externs.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@
182182
(seq xs) (update-in xs merge {})))
183183
{} externs))
184184

185-
(defn externs-map
185+
(defn externs-map*
186186
([]
187-
(externs-map (CommandLineRunner/getDefaultExterns)))
187+
(externs-map* (CommandLineRunner/getDefaultExterns)))
188188
([sources]
189-
(externs-map sources
189+
(externs-map* sources
190190
'{eval {}
191191
global {}
192192
goog {nodeGlobalRequire {}}
@@ -204,6 +204,8 @@
204204
externs (index-externs (parse-externs externs-file))))
205205
defaults sources))))
206206

207+
(def externs-map (memoize externs-map*))
208+
207209
(defn ns-match? [ns-segs var-segs]
208210
(or
209211
;; exact match (i.e. ctors)

src/test/clojure/cljs/analyzer_tests.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@
179179
(.getMessage (.getCause e))))
180180
"Only one ")))
181181

182-
(def test-cenv (atom {}))
182+
(def test-cenv (env/default-compiler-env))
183183
(def test-env (assoc-in (ana/empty-env) [:ns :name] 'cljs.core))
184-
(def test-core-env (atom {}))
184+
(def test-core-env (env/default-compiler-env))
185185

186186
(binding [ana/*unchecked-if* false
187187
ana/*analyze-deps* false]

0 commit comments

Comments
 (0)