Skip to content

Commit

Permalink
how we handled externs in compiler state was just not very functional
Browse files Browse the repository at this point in the history
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
  • Loading branch information
swannodette committed Apr 13, 2020
1 parent 5d15026 commit c1cf559
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
7 changes: 5 additions & 2 deletions src/main/clojure/cljs/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@
(defn compiler-options []
(get @env/*compiler* :options))

(defn get-externs []
(::externs @env/*compiler*))

(defn checked-arrays
"Returns false-y, :warn, or :error based on configuration and the
current value of *unchecked-arrays*."
Expand Down Expand Up @@ -1005,7 +1008,7 @@

(defn has-extern?
([pre]
(has-extern? pre (get @env/*compiler* ::externs)))
(has-extern? pre (get-externs)))
([pre externs]
(or (has-extern?* pre externs)
(when (= 1 (count pre))
Expand All @@ -1018,7 +1021,7 @@
([pre]
(js-tag pre :tag))
([pre tag-type]
(js-tag pre tag-type (get @env/*compiler* ::externs)))
(js-tag pre tag-type (get-externs)))
([pre tag-type externs]
(js-tag pre tag-type externs externs))
([pre tag-type externs top]
Expand Down
11 changes: 5 additions & 6 deletions src/main/clojure/cljs/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
(sorted-map))))))))))
(emit* ast))

(defn emits
(defn emits
([])
([^Object a]
(cond
Expand Down Expand Up @@ -587,12 +587,12 @@
(defn emit-js-array [items comma-sep]
(emits "[" (comma-sep items) "]"))

(defmethod emit* :js-object
(defmethod emit* :js-object
[{:keys [keys vals env]}]
(emit-wrap env
(emit-js-object (map vector keys vals) identity)))

(defmethod emit* :js-array
(defmethod emit* :js-array
[{:keys [items env]}]
(emit-wrap env
(emit-js-array items comma-sep)))
Expand Down Expand Up @@ -1133,7 +1133,7 @@
protocol (:protocol info)
tag (ana/infer-tag env (first (:args expr)))
proto? (and protocol tag
(or (and ana/*cljs-static-fns* protocol (= tag 'not-native))
(or (and ana/*cljs-static-fns* protocol (= tag 'not-native))
(and
(or ana/*cljs-static-fns*
(:protocol-inline env))
Expand Down Expand Up @@ -1817,8 +1817,7 @@
(defn emit-externs
([externs]
(emit-externs [] externs (atom #{})
(when env/*compiler*
(::ana/externs @env/*compiler*))))
(when env/*compiler* (ana/get-externs))))
([prefix externs top-level known-externs]
(loop [ks (seq (keys externs))]
(when ks
Expand Down
11 changes: 5 additions & 6 deletions src/main/clojure/cljs/env.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ state that is accessed/maintained by many different components."}

(defn default-compiler-env* [options]
(merge
{:cljs.analyzer/namespaces {'cljs.user {:name 'cljs.user}}
{:cljs.analyzer/namespaces {'cljs.user {:name 'cljs.user}}
:cljs.analyzer/constant-table {}
:cljs.analyzer/data-readers {}
:cljs.analyzer/externs #?(:clj (when (:infer-externs options)
(externs/externs-map (:externs-sources options)))
:cljs nil)
:options options}
:cljs.analyzer/data-readers {}
:cljs.analyzer/externs #?(:clj (externs/externs-map (:externs-sources options))
:cljs nil)
:options options}
#?@(:clj [(when (= (:target options) :nodejs)
{:node-module-index deps/native-node-modules})
{:js-dependency-index (deps/js-dependency-index options)}])))
Expand Down
8 changes: 5 additions & 3 deletions src/main/clojure/cljs/externs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@
(seq xs) (update-in xs merge {})))
{} externs))

(defn externs-map
(defn externs-map*
([]
(externs-map (CommandLineRunner/getDefaultExterns)))
(externs-map* (CommandLineRunner/getDefaultExterns)))
([sources]
(externs-map sources
(externs-map* sources
'{eval {}
global {}
goog {nodeGlobalRequire {}}
Expand All @@ -204,6 +204,8 @@
externs (index-externs (parse-externs externs-file))))
defaults sources))))

(def externs-map (memoize externs-map*))

(defn ns-match? [ns-segs var-segs]
(or
;; exact match (i.e. ctors)
Expand Down
4 changes: 2 additions & 2 deletions src/test/clojure/cljs/analyzer_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@
(.getMessage (.getCause e))))
"Only one ")))

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

(binding [ana/*unchecked-if* false
ana/*analyze-deps* false]
Expand Down

0 comments on commit c1cf559

Please sign in to comment.