|
| 1 | +(ns sci.impl.core-protocols |
| 2 | + (:refer-clojure :exclude [deref -deref -swap! -reset!]) |
| 3 | + (:require [sci.impl.types :as types] |
| 4 | + [sci.impl.vars :as vars])) |
| 5 | + |
| 6 | + |
| 7 | +;;;; IDeref |
| 8 | + |
| 9 | +(defmulti #?(:clj deref :cljs -deref) types/type-impl) |
| 10 | + |
| 11 | +(defmethod #?(:clj deref :cljs -deref) :sci.impl.protocols/reified [ref] |
| 12 | + (let [methods (types/getMethods ref)] |
| 13 | + ((get methods #?(:clj 'deref :cljs '-deref)) ref))) |
| 14 | + |
| 15 | +(defmethod #?(:clj deref :cljs -deref) :default [ref] |
| 16 | + (clojure.core/deref ref)) |
| 17 | + |
| 18 | +(defn deref* |
| 19 | + ([x] |
| 20 | + #?(:clj (deref x) |
| 21 | + :cljs (-deref x))) |
| 22 | + #?(:clj |
| 23 | + ([x & args] |
| 24 | + (apply clojure.core/deref x args)))) |
| 25 | + |
| 26 | +#?(:clj |
| 27 | + (def clj-lang-ns (vars/->SciNamespace 'clojure.lang nil))) |
| 28 | +#?(:cljs |
| 29 | + (def cljs-core-ns (vars/->SciNamespace 'cljs.core nil))) |
| 30 | + |
| 31 | +(def deref-protocol |
| 32 | + #?(:clj |
| 33 | + (vars/new-var |
| 34 | + 'clojure.lang.IDeref |
| 35 | + {:class clojure.lang.IDeref |
| 36 | + :methods #{deref} |
| 37 | + :ns clj-lang-ns} |
| 38 | + {:ns clj-lang-ns}) |
| 39 | + :cljs |
| 40 | + (vars/new-var |
| 41 | + 'cljs.core.IDeref |
| 42 | + {:methods #{-deref} |
| 43 | + :ns cljs-core-ns} |
| 44 | + {:ns cljs-core-ns}))) |
| 45 | + |
| 46 | +;;;; end IDeref |
| 47 | + |
| 48 | +;;;; IAtom |
| 49 | + |
| 50 | +;; ;; You can use multiarity in multimethods |
| 51 | +;; (defmulti foo (fn [x & _] x)) |
| 52 | + |
| 53 | +;; (defmethod foo :default [_ & _] "DEFAULT VALUE DISPACHED") |
| 54 | + |
| 55 | +;; ;; Like a standar multi-arity function |
| 56 | +;; (defmethod foo :bar |
| 57 | +;; ([_ _] "ONE ARGUMENT") |
| 58 | +;; ([_ _ _] "TWO ARGUMENTs") |
| 59 | +;; ([_ _ _ _] "THREE ARGUMENTs") |
| 60 | +;; ([_ _ _ _ & more] (cl-format nil "~d ARGUMENTS" (+ 3 (count more))))) |
| 61 | + |
| 62 | +(defmulti #?(:clj swap :cljs -swap!) types/type-impl) |
| 63 | +(defmulti #?(:clj reset :cljs -reset!) types/type-impl) |
| 64 | +#?(:clj (defmulti compareAndSet types/type-impl)) |
| 65 | +#?(:clj (defmulti swapVals types/type-impl)) |
| 66 | +#?(:clj (defmulti resetVals types/type-impl)) |
| 67 | + |
| 68 | +;;;; Protocol methods |
| 69 | + |
| 70 | +(defmethod #?(:clj swap :cljs -swap!) :sci.impl.protocols/reified |
| 71 | + ([ref f] |
| 72 | + (let [methods (types/getMethods ref)] |
| 73 | + ((get methods #?(:clj 'swap :cljs '-swap!)) ref f))) |
| 74 | + ([ref f a1] |
| 75 | + (let [methods (types/getMethods ref)] |
| 76 | + ((get methods #?(:clj 'swap :cljs '-swap!)) ref f a1))) |
| 77 | + ([ref f a1 a2] |
| 78 | + (let [methods (types/getMethods ref)] |
| 79 | + ((get methods #?(:clj 'swap :cljs '-swap!)) ref f a1 a2))) |
| 80 | + ([ref f a1 a2 & args] |
| 81 | + (let [methods (types/getMethods ref)] |
| 82 | + (apply (get methods #?(:clj 'swap :cljs '-swap!)) ref f a1 a2 args)))) |
| 83 | + |
| 84 | +(defmethod #?(:clj reset :cljs -reset!) :sci.impl.protocols/reified [ref v] |
| 85 | + (let [methods (types/getMethods ref)] |
| 86 | + ((get methods #?(:clj 'reset :cljs '-reset!)) ref v))) |
| 87 | + |
| 88 | +#?(:clj |
| 89 | + (defmethod compareAndSet :sci.impl.protocols/reified [ref old new] |
| 90 | + (let [methods (types/getMethods ref)] |
| 91 | + ((get methods 'compareAndSet) ref old new)))) |
| 92 | + |
| 93 | +#?(:clj |
| 94 | + (defmethod swapVals :sci.impl.protocols/reified |
| 95 | + ([ref f] |
| 96 | + (let [methods (types/getMethods ref)] |
| 97 | + ((get methods 'swapVals) ref f))) |
| 98 | + ([ref f a1] |
| 99 | + (let [methods (types/getMethods ref)] |
| 100 | + ((get methods 'swapVals) ref f a1))) |
| 101 | + ([ref f a1 a2] |
| 102 | + (let [methods (types/getMethods ref)] |
| 103 | + ((get methods 'swapVals) ref f a1 a2))) |
| 104 | + ([ref f a1 a2 & args] |
| 105 | + (let [methods (types/getMethods ref)] |
| 106 | + (apply (get methods 'swapVals) ref f a1 a2 args))))) |
| 107 | + |
| 108 | +#?(:clj |
| 109 | + (defmethod resetVals :sci.impl.protocols/reified [ref v] |
| 110 | + (let [methods (types/getMethods ref)] |
| 111 | + ((get methods 'resetVals) ref v)))) |
| 112 | + |
| 113 | +;;;; Defaults |
| 114 | + |
| 115 | +(defmethod #?(:clj swap :cljs -swap!) :default [ref f & args] |
| 116 | + ;; TODO: optimize arities |
| 117 | + (apply clojure.core/swap! ref f args)) |
| 118 | + |
| 119 | +(defmethod #?(:clj reset :cljs -reset!) :default [ref v] |
| 120 | + (reset! ref v)) |
| 121 | + |
| 122 | +#?(:clj |
| 123 | + (defmethod compareAndSet :default [ref old new] |
| 124 | + (compare-and-set! ref old new))) |
| 125 | + |
| 126 | +#?(:clj |
| 127 | + (defmethod swapVals :default [ref & args] |
| 128 | + (apply swap-vals! ref args))) |
| 129 | + |
| 130 | +#?(:clj |
| 131 | + (defmethod resetVals :default [ref v] |
| 132 | + (reset-vals! ref v))) |
| 133 | + |
| 134 | +;;;; Re-routing |
| 135 | + |
| 136 | +(defn swap!* [ref f & args] |
| 137 | + ;; TODO: optimize arities - maybe test first how much this matters at all |
| 138 | + ;; For CLJ I guess we can directly use the multimethods |
| 139 | + (if args |
| 140 | + (apply #?(:clj swap :cljs -swap!) ref f args) |
| 141 | + (#?(:clj swap :cljs -swap!) ref f))) |
| 142 | + |
| 143 | +(defn reset!* [ref v] |
| 144 | + (#?(:clj reset :cljs -reset!) ref v)) |
| 145 | + |
| 146 | +#?(:clj |
| 147 | + (defn compare-and-set!* [ref old new] |
| 148 | + (compareAndSet ref old new))) |
| 149 | + |
| 150 | +#?(:clj |
| 151 | + (defn swap-vals!* [ref f & args] |
| 152 | + (apply swapVals ref f args))) |
| 153 | + |
| 154 | +#?(:clj |
| 155 | + (defn reset-vals!* [ref v] |
| 156 | + (resetVals ref v))) |
| 157 | + |
| 158 | +;;;; Protocol vars |
| 159 | + |
| 160 | +(def swap-protocol |
| 161 | + #?(:clj |
| 162 | + (vars/new-var |
| 163 | + 'clojure.lang.IAtom |
| 164 | + {:class clojure.lang.IAtom |
| 165 | + :methods #{swap, reset, compareAndSet} |
| 166 | + :ns clj-lang-ns} |
| 167 | + {:ns clj-lang-ns}) |
| 168 | + :cljs |
| 169 | + (vars/new-var |
| 170 | + 'cljs.core.ISwap |
| 171 | + {:methods #{-swap!} |
| 172 | + :ns cljs-core-ns} |
| 173 | + {:ns cljs-core-ns}))) |
| 174 | + |
| 175 | +#?(:cljs |
| 176 | + (def reset-protocol |
| 177 | + (vars/new-var |
| 178 | + 'cljs.core.IReset |
| 179 | + {:methods #{-reset!} |
| 180 | + :ns cljs-core-ns} |
| 181 | + {:ns cljs-core-ns}))) |
| 182 | + |
| 183 | +#?(:clj |
| 184 | + (def iatom2-protocol |
| 185 | + (vars/new-var |
| 186 | + 'clojure.lang.IAtom2 |
| 187 | + {:class clojure.lang.IAtom2 |
| 188 | + :methods #{swap, reset, compareAndSet, swapVals, resetVals} |
| 189 | + :ns clj-lang-ns} |
| 190 | + {:ns clj-lang-ns}))) |
| 191 | + |
| 192 | +;;;; end IAtom |
0 commit comments