|
2022 | 2022 | => 17"
|
2023 | 2023 | [psym & doc+methods]
|
2024 | 2024 | (core/let [p (:name (cljs.analyzer/resolve-var (dissoc &env :locals) psym))
|
2025 |
| - [doc methods] (if (core/string? (first doc+methods)) |
2026 |
| - [(first doc+methods) (next doc+methods)] |
2027 |
| - [nil doc+methods]) |
2028 |
| - psym (vary-meta psym assoc |
2029 |
| - :doc doc |
2030 |
| - :protocol-symbol true) |
| 2025 | + [opts methods] |
| 2026 | + (core/loop [opts {:protocol-symbol true} |
| 2027 | + methods [] |
| 2028 | + sigs doc+methods] |
| 2029 | + (core/if-not (seq sigs) |
| 2030 | + [opts methods] |
| 2031 | + (core/let [[head & tail] sigs] |
| 2032 | + (core/cond |
| 2033 | + (core/string? head) |
| 2034 | + (recur (assoc opts :doc head) methods tail) |
| 2035 | + (core/keyword? head) |
| 2036 | + (recur (assoc opts head (first tail)) methods (rest tail)) |
| 2037 | + (core/list? head) |
| 2038 | + (recur opts (conj methods head) tail) |
| 2039 | + :else |
| 2040 | + (throw #?(:clj (Exception. |
| 2041 | + (core/str "Invalid protocol, " psym " received unexpected argument")) |
| 2042 | + :cljs (js/Error. |
| 2043 | + (core/str "Invalid protocol, " psym " received unexpected argument")))) |
| 2044 | + )))) |
| 2045 | + psym (vary-meta psym merge opts) |
2031 | 2046 | ns-name (core/-> &env :ns :name)
|
2032 |
| - fqn (core/fn [n] (symbol (core/str ns-name "." n))) |
| 2047 | + fqn (core/fn [n] (symbol (core/str ns-name) (core/str n))) |
2033 | 2048 | prefix (protocol-prefix p)
|
2034 | 2049 | _ (core/doseq [[mname & arities] methods]
|
2035 | 2050 | (core/when (some #{0} (map count (filter vector? arities)))
|
|
2047 | 2062 | (core/symbol? arg) arg
|
2048 | 2063 | (core/and (map? arg) (core/some? (:as arg))) (:as arg)
|
2049 | 2064 | :else (gensym))) sig)
|
2050 |
| - sig)] |
2051 |
| - `(~sig |
2052 |
| - (if (and (not (nil? ~(first sig))) |
2053 |
| - (not (nil? (. ~(first sig) ~(symbol (core/str "-" slot)))))) ;; Property access needed here. |
2054 |
| - (. ~(first sig) ~slot ~@sig) |
2055 |
| - (let [x# (if (nil? ~(first sig)) nil ~(first sig)) |
2056 |
| - m# (unchecked-get ~(fqn fname) (goog/typeOf x#))] |
2057 |
| - (if-not (nil? m#) |
2058 |
| - (m# ~@sig) |
2059 |
| - (let [m# (unchecked-get ~(fqn fname) "_")] |
2060 |
| - (if-not (nil? m#) |
2061 |
| - (m# ~@sig) |
2062 |
| - (throw |
2063 |
| - (missing-protocol |
2064 |
| - ~(core/str psym "." fname) ~(first sig))))))))))) |
| 2065 | + sig) |
| 2066 | + |
| 2067 | + fqn-fname (fqn fname) |
| 2068 | + fsig (first sig) |
| 2069 | + |
| 2070 | + ;; construct protocol checks in reverse order |
| 2071 | + ;; check the.protocol/fn["_"] for default impl last |
| 2072 | + check |
| 2073 | + `(let [m# (unchecked-get ~fqn-fname "_")] |
| 2074 | + (if-not (nil? m#) |
| 2075 | + (m# ~@sig) |
| 2076 | + (throw |
| 2077 | + (missing-protocol |
| 2078 | + ~(core/str psym "." fname) ~fsig)))) |
| 2079 | + |
| 2080 | + ;; then check protocol fn in metadata (only when protocol is marked with :extend-via-metadata true) |
| 2081 | + check |
| 2082 | + (core/if-not (:extend-via-metadata opts) |
| 2083 | + check |
| 2084 | + `(if-let [meta-impl# (-> ~fsig (core/meta) (core/get '~fqn-fname))] |
| 2085 | + (meta-impl# ~@sig) |
| 2086 | + ~check)) |
| 2087 | + |
| 2088 | + ;; then check protocol on js string,function,array,object |
| 2089 | + check |
| 2090 | + `(let [x# (if (nil? ~fsig) nil ~fsig) |
| 2091 | + m# (unchecked-get ~fqn-fname (goog/typeOf x#))] |
| 2092 | + (if-not (nil? m#) |
| 2093 | + (m# ~@sig) |
| 2094 | + ~check)) |
| 2095 | + |
| 2096 | + ;; then check protocol property on object (first check actually executed) |
| 2097 | + check |
| 2098 | + `(if (and (not (nil? ~fsig)) |
| 2099 | + (not (nil? (. ~fsig ~(symbol (core/str "-" slot)))))) ;; Property access needed here. |
| 2100 | + (. ~fsig ~slot ~@sig) |
| 2101 | + ~check)] |
| 2102 | + `(~sig ~check))) |
2065 | 2103 | psym (core/-> psym
|
2066 | 2104 | (vary-meta update-in [:jsdoc] conj
|
2067 | 2105 | "@interface")
|
|
0 commit comments