Skip to content

Commit a399958

Browse files
author
dnolen
committed
CLJS-2093: inline ^:const var values
first cut. Store init expr in the compiler environment. When returning var AST if we are not def'ing a var then analyze :const-init if it exists and add as :const-expr to var AST. During emission of var if :const-expr key exists emit that instead.
1 parent b9d957f commit a399958

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,7 @@
12991299
([_ sym doc init] {:sym sym :doc doc :init init}))
13001300
args (apply pfn form)
13011301
sym (:sym args)
1302+
const? (-> sym meta :const)
13021303
sym-meta (meta sym)
13031304
tag (-> sym meta :tag)
13041305
protocol (-> sym meta :protocol valid-proto)
@@ -1381,6 +1382,8 @@
13811382
"cljs/core.cljs"
13821383
f))))}
13831384
(when doc {:doc doc})
1385+
(when const?
1386+
{:const-init (:init args)})
13841387
(when (true? dynamic) {:dynamic true})
13851388
(source-info var-name env)
13861389
;; the protocol a protocol fn belongs to
@@ -2914,7 +2917,10 @@
29142917
(resolve-existing-var env sym)
29152918
(resolve-var env sym))]
29162919
(if-not (true? (:def-var env))
2917-
(assoc ret :op :var :info info)
2920+
(merge
2921+
(assoc ret :op :var :info info)
2922+
(when-let [const-init (:const-init info)]
2923+
{:const-expr (analyze env const-init)}))
29182924
(let [info (resolve-var env sym)]
29192925
(assoc ret :op :var :info info))))))))
29202926

src/main/clojure/cljs/compiler.cljc

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -330,24 +330,26 @@
330330
(defmethod emit* :no-op [m])
331331

332332
(defmethod emit* :var
333-
[{:keys [info env form] :as arg}]
334-
(let [var-name (:name info)
335-
info (if (= (namespace var-name) "js")
336-
(let [js-module-name (get-in @env/*compiler* [:js-module-index (name var-name)])]
337-
(or js-module-name (name var-name)))
338-
info)]
339-
; We need a way to write bindings out to source maps and javascript
340-
; without getting wrapped in an emit-wrap calls, otherwise we get
341-
; e.g. (function greet(return x, return y) {}).
342-
(if (:binding-form? arg)
343-
; Emit the arg map so shadowing is properly handled when munging
344-
; (prevents duplicate fn-param-names)
345-
(emits (munge arg))
346-
(when-not (= :statement (:context env))
347-
(emit-wrap env
348-
(emits
349-
(cond-> info
350-
(not= form 'js/-Infinity) munge)))))))
333+
[{:keys [info env form] :as ast}]
334+
(if-let [const-expr (:const-expr ast)]
335+
(emit const-expr)
336+
(let [var-name (:name info)
337+
info (if (= (namespace var-name) "js")
338+
(let [js-module-name (get-in @env/*compiler* [:js-module-index (name var-name)])]
339+
(or js-module-name (name var-name)))
340+
info)]
341+
; We need a way to write bindings out to source maps and javascript
342+
; without getting wrapped in an emit-wrap calls, otherwise we get
343+
; e.g. (function greet(return x, return y) {}).
344+
(if (:binding-form? ast)
345+
; Emit the arg map so shadowing is properly handled when munging
346+
; (prevents duplicate fn-param-names)
347+
(emits (munge ast))
348+
(when-not (= :statement (:context env))
349+
(emit-wrap env
350+
(emits
351+
(cond-> info
352+
(not= form 'js/-Infinity) munge))))))))
351353

352354
(defmethod emit* :var-special
353355
[{:keys [env var sym meta] :as arg}]

0 commit comments

Comments
 (0)