Skip to content

Commit 38c12da

Browse files
mfikesswannodette
authored andcommitted
CLJS-2117: Self-host: Port CLJS-1989 to self-hosted
1 parent f119f65 commit 38c12da

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/main/cljs/cljs/js.cljs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
(:require [clojure.string :as string]
1414
[clojure.walk :as walk]
1515
[cljs.env :as env]
16+
[cljs.spec.alpha]
1617
[cljs.analyzer :as ana]
1718
[cljs.compiler :as comp]
1819
[cljs.tools.reader :as r]
@@ -288,7 +289,8 @@
288289
((:*eval-fn* bound-vars) resource)
289290
(when cache
290291
(load-analysis-cache!
291-
(:*compiler* bound-vars) aname cache))
292+
(:*compiler* bound-vars) aname cache)
293+
(ana/register-cached-speced-vars cache))
292294
(when source-map
293295
(load-source-map!
294296
(:*compiler* bound-vars) aname source-map))
@@ -922,6 +924,7 @@
922924
(when (:source-map opts)
923925
(append-source-map env/*compiler*
924926
aname source sb @comp/*source-map-data* opts))
927+
(ana/dump-speced-vars-to-env aname)
925928
(let [js-source (.toString sb)
926929
evalm {:lang :clj
927930
:name name

src/main/clojure/cljs/analyzer.cljc

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,17 +3555,45 @@
35553555
true
35563556
(util/changed? src cache)))))))
35573557

3558+
#?(:clj
3559+
(defn- get-speced-vars
3560+
[]
3561+
(when-let [spec-ns (find-ns 'cljs.spec.alpha)]
3562+
(ns-resolve spec-ns '_speced_vars)))
3563+
:cljs
3564+
;; Here, we look up the symbol '-speced-vars because ns-interns*
3565+
;; is implemented by invoking demunge on the result of js-keys.
3566+
(let [cached-var (delay (get (ns-interns* 'cljs.spec.alpha$macros) '-speced-vars))]
3567+
(defn- get-speced-vars []
3568+
(when (some? (find-ns-obj 'cljs.spec.alpha$macros))
3569+
@cached-var))))
3570+
3571+
(defn dump-speced-vars-to-env
3572+
"Dumps registered speced vars for a given namespace into the compiler
3573+
environment."
3574+
[ns]
3575+
(when-let [speced-vars (get-speced-vars)]
3576+
(let [ns-str (str ns)]
3577+
(swap! env/*compiler* update-in [::namespaces ns :cljs.spec/speced-vars]
3578+
(fnil into #{}) (filter #(= ns-str (namespace %))) @@speced-vars))))
3579+
3580+
(defn register-cached-speced-vars
3581+
"Registers speced vars found in a namespace analysis cache."
3582+
[cached-ns]
3583+
(when-let [vars (seq (:cljs.spec/speced-vars cached-ns))]
3584+
#?(:clj (try
3585+
(require 'cljs.spec.alpha)
3586+
(catch Throwable t)))
3587+
(when-let [speced-vars (get-speced-vars)]
3588+
(swap! @speced-vars into vars))))
3589+
35583590
#?(:clj
35593591
(defn write-analysis-cache
35603592
([ns cache-file]
35613593
(write-analysis-cache ns cache-file nil))
35623594
([ns ^File cache-file src]
35633595
(util/mkdirs cache-file)
3564-
(when-let [spec-ns (find-ns 'cljs.spec.alpha)]
3565-
(when-let [speced-vars (ns-resolve spec-ns '_speced_vars)]
3566-
(let [ns-str (str ns)]
3567-
(swap! env/*compiler* update-in [::namespaces ns :cljs.spec/speced-vars]
3568-
(fnil into #{}) (filter #(= ns-str (namespace %))) @@speced-vars))))
3596+
(dump-speced-vars-to-env ns)
35693597
(let [ext (util/ext cache-file)
35703598
analysis (dissoc (get-in @env/*compiler* [::namespaces ns]) :macros)]
35713599
(case ext
@@ -3604,13 +3632,7 @@
36043632
(swap! env/*compiler*
36053633
(fn [cenv]
36063634
(do
3607-
(when-let [vars (seq (:cljs.spec/speced-vars cached-ns))]
3608-
(try
3609-
(require 'cljs.spec.alpha)
3610-
(catch Throwable t))
3611-
(when-let [spec-ns (find-ns 'cljs.spec.alpha)]
3612-
(when-let [speced-vars (ns-resolve spec-ns '_speced_vars)]
3613-
(swap! @speced-vars into vars))))
3635+
(register-cached-speced-vars cached-ns)
36143636
(doseq [x (get-in cached-ns [::constants :order])]
36153637
(register-constant! x))
36163638
(-> cenv

0 commit comments

Comments
 (0)