Skip to content

Commit 27af25a

Browse files
committed
CLJS-1409: Allow basic type checking of protocols
include :protocols in the deftype & defrecord ASTs. emit function(){} instead of {} for protocol to make Closure happy. emit @implements for each protocol implemented by a type.
1 parent 3ecf5af commit 27af25a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,8 @@
19631963
:shadow (m fld)}))
19641964
{} (if (= :defrecord* op)
19651965
(concat fields '[__meta __extmap ^:mutable __hash])
1966-
fields))]
1966+
fields))
1967+
protocols (-> tsym meta :protocols)]
19671968
(swap! env/*compiler* update-in [::namespaces (-> env :ns :name) :defs tsym]
19681969
(fn [m]
19691970
(let [m (assoc (or m {})
@@ -1973,9 +1974,11 @@
19731974
:record (= :defrecord* op))]
19741975
(merge m
19751976
(dissoc (meta tsym) :protocols)
1976-
{:protocols (-> tsym meta :protocols)}
1977+
{:protocols protocols}
19771978
(source-info tsym env)))))
1978-
{:op op :env env :form form :t t :fields fields :pmasks pmasks :body (analyze (assoc env :locals locals) body)}))
1979+
{:op op :env env :form form :t t :fields fields :pmasks pmasks
1980+
:protocols (disj protocols 'cljs.core/Object)
1981+
:body (analyze (assoc env :locals locals) body)}))
19791982

19801983
(defmethod parse 'deftype*
19811984
[_ env form _ _]

src/main/clojure/cljs/compiler.cljc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,11 +1042,13 @@
10421042
(load-libs uses requires (:use reloads)))
10431043

10441044
(defmethod emit* :deftype*
1045-
[{:keys [t fields pmasks body]}]
1045+
[{:keys [t fields pmasks body protocols]}]
10461046
(let [fields (map munge fields)]
10471047
(emitln "")
10481048
(emitln "/**")
10491049
(emitln "* @constructor")
1050+
(doseq [protocol protocols]
1051+
(emitln " * @implements {" (munge (str protocol)) "}"))
10501052
(emitln "*/")
10511053
(emitln (munge t) " = (function (" (comma-sep fields) "){")
10521054
(doseq [fld fields]
@@ -1057,11 +1059,13 @@
10571059
(emit body)))
10581060

10591061
(defmethod emit* :defrecord*
1060-
[{:keys [t fields pmasks body]}]
1062+
[{:keys [t fields pmasks body protocols]}]
10611063
(let [fields (concat (map munge fields) '[__meta __extmap __hash])]
10621064
(emitln "")
10631065
(emitln "/**")
10641066
(emitln "* @constructor")
1067+
(doseq [protocol protocols]
1068+
(emitln " * @implements {" (munge (str protocol)) "}"))
10651069
(emitln "*/")
10661070
(emitln (munge t) " = (function (" (comma-sep fields) "){")
10671071
(doseq [fld fields]

src/main/clojure/cljs/core.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@
18951895
sigs))))]
18961896
`(do
18971897
(set! ~'*unchecked-if* true)
1898-
(def ~psym (js-obj))
1898+
(def ~psym (~'js* "function(){}"))
18991899
~@(map method methods)
19001900
(set! ~'*unchecked-if* false))))
19011901

0 commit comments

Comments
 (0)