Skip to content

Commit

Permalink
PersistentHashMap ported from Clojure
Browse files Browse the repository at this point in the history
In addition to the PHM implementation, this commit introduces two new
bit ops, bit-shift-right-zero-fill (with accompanying compiler macro)
and bit-count.

An internal general purpose coercive-= compiler macro and two special
purpose internal compiler macros mask and bitpos are also introduced.

INode operations are implemented directly on the node objects (via
Object in deftype).

cljs.core/hash-map and compiler's emit :map now use PersistentHashMap,
so maps and sets are now persistent by default.

ObjMap and HashMap are now marked deprecated.
  • Loading branch information
michalmarczyk authored and David Nolen committed Apr 20, 2012
1 parent 053b7fd commit ef0d525
Show file tree
Hide file tree
Showing 3 changed files with 479 additions and 13 deletions.
17 changes: 5 additions & 12 deletions src/clj/cljs/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,11 @@
(defmethod emit :map
[{:keys [children env simple-keys? keys vals]}]
(emit-wrap env
(if simple-keys?
(emits "cljs.core.ObjMap.fromObject(["
(comma-sep keys) ; keys
"],{"
(comma-sep (map (fn [k v] #(emits k ":" v))
keys vals)) ; js obj
"})")
(emits "cljs.core.HashMap.fromArrays(["
(comma-sep keys)
"],["
(comma-sep vals)
"])"))))
(emits "cljs.core.PersistentHashMap.fromArrays(["
(comma-sep keys)
"],["
(comma-sep vals)
"])")))

(defmethod emit :vector
[{:keys [children env]}]
Expand Down
15 changes: 15 additions & 0 deletions src/clj/cljs/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
(defmacro coercive-not= [x y]
(bool-expr (list 'js* "(~{} != ~{})" x y)))

;; internal - do not use.
(defmacro coercive-= [x y]
(bool-expr (list 'js* "(~{} == ~{})" x y)))

(defmacro true? [x]
(bool-expr (list 'js* "~{} === true" x)))

Expand Down Expand Up @@ -183,9 +187,20 @@
(defmacro bit-shift-right [x n]
(list 'js* "(~{} >> ~{})" x n))

(defmacro bit-shift-right-zero-fill [x n]
(list 'js* "(~{} >>> ~{})" x n))

(defmacro bit-set [x n]
(list 'js* "(~{} | (1 << ~{}))" x n))

;; internal
(defmacro mask [hash shift]
(list 'js* "((~{} >>> ~{}) & 0x01f)" hash shift))

;; internal
(defmacro bitpos [hash shift]
(list 'js* "(1 << ~{})" `(mask ~hash ~shift)))

(defn- protocol-prefix [psym]
(str (.replace (str psym) \. \$) "$"))

Expand Down
Loading

0 comments on commit ef0d525

Please sign in to comment.