Skip to content

Commit d92b300

Browse files
committed
CLJS-1386: Symbols should be added to the constants table
1 parent 24e63f4 commit d92b300

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,16 @@
357357

358358
(defn gen-constant-id [value]
359359
(let [prefix (cond
360-
(keyword? value) "constant$keyword$"
360+
(keyword? value) "cst$kw$"
361+
(symbol? value) "cst$sym$"
361362
:else
362363
(throw
363364
#?(:clj (Exception. (str "constant type " (type value) " not supported"))
364365
:cljs (js/Error. (str "constant type " (type value) " not supported")))))
365-
name (-> value (str) (subs 1) (string/replace "-" "_DASH_") (munge) (string/replace "." "$"))]
366+
name (if (keyword? value)
367+
(subs (str value) 1)
368+
(str value))
369+
name (-> name (string/replace "-" "_DASH_") (munge) (string/replace "." "$"))]
366370
(symbol (str prefix name))))
367371

368372
(defn- register-constant!
@@ -2227,7 +2231,9 @@
22272231
"Finds the var associated with sym"
22282232
[env sym]
22292233
(if ^boolean (:quoted? env)
2230-
(analyze-wrap-meta {:op :constant :env env :form sym :tag 'cljs.core/Symbol})
2234+
(do
2235+
(register-constant! env sym)
2236+
(analyze-wrap-meta {:op :constant :env env :form sym :tag 'cljs.core/Symbol}))
22312237
(let [{:keys [line column]} (meta sym)
22322238
env (if-not (nil? line)
22332239
(assoc env :line line)

src/main/clojure/cljs/compiler.cljc

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,9 @@
250250
(emit-constant (hash kw))
251251
(emits ")")))
252252

253-
(defmethod emit-constant #?(:clj clojure.lang.Keyword :cljs Keyword) [x]
254-
(if (-> @env/*compiler* :options :emit-constants)
255-
(let [value (-> @env/*compiler* ::ana/constant-table x)]
256-
(emits "cljs.core." value))
257-
(emits-keyword x)
258-
))
259-
260-
(defmethod emit-constant #?(:clj clojure.lang.Symbol :cljs Symbol) [x]
261-
(let [ns (namespace x)
262-
name (name x)
253+
(defn emits-symbol [sym]
254+
(let [ns (namespace sym)
255+
name (name sym)
263256
symstr (if-not (nil? ns)
264257
(str ns "/" name)
265258
name)]
@@ -270,11 +263,23 @@
270263
(emits ",")
271264
(emit-constant symstr)
272265
(emits ",")
273-
(emit-constant (hash x))
266+
(emit-constant (hash sym))
274267
(emits ",")
275268
(emit-constant nil)
276269
(emits ")")))
277270

271+
(defmethod emit-constant #?(:clj clojure.lang.Keyword :cljs Keyword) [x]
272+
(if (-> @env/*compiler* :options :emit-constants)
273+
(let [value (-> @env/*compiler* ::ana/constant-table x)]
274+
(emits "cljs.core." value))
275+
(emits-keyword x)))
276+
277+
(defmethod emit-constant #?(:clj clojure.lang.Symbol :cljs Symbol) [x]
278+
(if (-> @env/*compiler* :options :emit-constants)
279+
(let [value (-> @env/*compiler* ::ana/constant-table x)]
280+
(emits "cljs.core." value))
281+
(emits-symbol x)))
282+
278283
;; tagged literal support
279284

280285
(defmethod emit-constant #?(:clj java.util.Date :cljs js/Date) [^java.util.Date date]
@@ -1293,11 +1298,17 @@
12931298
;; TODO: needs fixing, table will include other things than keywords - David
12941299

12951300
(defn emit-constants-table [table]
1296-
(doseq [[keyword value] table]
1297-
(let [ns (namespace keyword)
1298-
name (name keyword)]
1301+
(doseq [[sym value] table]
1302+
(let [ns (namespace sym)
1303+
name (name sym)]
12991304
(emits "cljs.core." value " = ")
1300-
(emits-keyword keyword)
1305+
(cond
1306+
(keyword? sym) (emits-keyword sym)
1307+
(symbol? sym) (emits-symbol sym)
1308+
:else (throw
1309+
(ex-info
1310+
(str "Cannot emit constant for type " (type sym))
1311+
{:error :invalid-constant-type})))
13011312
(emits ";\n"))))
13021313

13031314
#?(:clj

0 commit comments

Comments
 (0)