Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/main/clojure/cljs/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2072,14 +2072,6 @@
(missing-protocol
~(core/str psym "." fname) ~fsig))))

;; then check protocol fn in metadata (only when protocol is marked with :extend-via-metadata true)
check
(core/if-not (:extend-via-metadata opts)
check
`(if-let [meta-impl# (-> ~fsig (core/meta) (core/get '~fqn-fname))]
(meta-impl# ~@sig)
~check))

;; then check protocol on js string,function,array,object (first dynamic check actually executed)
check
`(let [x# (if (nil? ~fsig) nil ~fsig)
Expand All @@ -2088,9 +2080,10 @@
(m# ~@sig)
~check))]
`(~sig ~check)))
expand-sig (core/fn [dyn-name slot sig]
expand-sig (core/fn [fname dyn-name slot sig]
(core/let [sig (sig->syms sig)

fqn-fname (with-meta (fqn fname) {:cljs.analyzer/no-resolve true})
fsig (first sig)

;; check protocol property on object (first check executed)
Expand All @@ -2099,7 +2092,15 @@
;; Property access needed here.
(not (nil? (. ~fsig ~(with-meta (symbol (core/str "-" slot)) {:protocol-prop true})))))
(. ~fsig ~slot ~@sig)
(~dyn-name ~@sig))]
(~dyn-name ~@sig))

;; then check protocol fn in metadata (only when protocol is marked with :extend-via-metadata true)
check
(core/if-not (:extend-via-metadata opts)
check
`(if-let [meta-impl# (-> ~fsig (core/meta) (core/get '~fqn-fname))]
(meta-impl# ~@sig)
~check))]
`(~sig ~check)))
psym (core/-> psym
(vary-meta update-in [:jsdoc] conj "@interface")
Expand Down Expand Up @@ -2147,7 +2148,7 @@
sigs))]
(defn ~fname
~@(map (core/fn [sig]
(expand-sig dyn-name
(expand-sig fname dyn-name
(with-meta (symbol (core/str slot "$arity$" (count sig)))
{:protocol-prop true})
sig))
Expand Down
12 changes: 10 additions & 2 deletions src/test/cljs/cljs/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,13 +1770,21 @@
(deftest test-cljs-2960
;; protocol impl via metadata
(is (= 1 (ext-meta-protocol (with-meta {} {`ext-meta-protocol (fn [_] 1)}))))
;; actual impl before metadata
(is (= 2 (ext-meta-protocol (with-meta (SomeMetaImpl. 2) {`ext-meta-protocol (fn [_] 1)}))))
;; metadata before actual impl
(is (= 1 (ext-meta-protocol (with-meta (SomeMetaImpl. 2) {`ext-meta-protocol (fn [_] 1)}))))
;; protocol not marked as :extend-via-metadata so fallthrough to no impl
(is (thrown? js/Error (non-meta-protocol (with-meta {} {`non-meta-protocol (fn [_] 1)}))))
;; normal impl call just in case
(is (= 2 (non-meta-protocol (with-meta (SomeMetaImpl. 2) {`non-meta-protocol (fn [_] 1)})))))

(extend-type PersistentArrayMap
ExtMetaProtocol
(ext-meta-protocol [m] 2))

(deftest test-cljs-3313
(testing "metadata protocol fn takes precedence over direct implementation"
(= 1 (ext-meta-protocol (with-meta (array-map) {`ext-meta-protocol (fn [_] 1)})))))

(deftest test-cljs-3054
(testing "`into` behaves the same as Clojure"
(is (nil? (into nil #{})))
Expand Down