How are Atoms implemented in Clojurescript? How are Atoms implemented in Clojurescript? multithreading multithreading

How are Atoms implemented in Clojurescript?


It just returns and assigns the value.

In the sourcehttps://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L4081

(deftype Atom [state meta validator watches]  ...  IDeref  (-deref [_] state)   ...)

andhttps://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L4110

(defn atom  "Creates and returns an Atom ..."  ([x] (Atom. x nil nil nil))  ([x & {:keys [meta validator]}] (Atom. x meta validator nil)))

check the implementation of swap! and reset! you will find out:

(set! (.-state a) new-value)

then , go to https://github.com/clojure/clojurescript/blob/3bb97961cbc958aeaeac506222dc7b9dcb0e9fc1/src/clj/cljs/compiler.clj#L771 the set!, you will find the compiler just emits an 'assignment statement':

(defmethod emit* :set!  [{:keys [target val env]}]  (emit-wrap env (emits target " = " val)))