Permalink
Browse files

PersistentHashMap ported from Clojure

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 11, 2012
1 parent 053b7fd commit ef0d52541b02bcf767f2f54f497c8808c6afa91d
Showing with 479 additions and 13 deletions.
  1. +5 −12 src/clj/cljs/compiler.clj
  2. +15 −0 src/clj/cljs/core.clj
  3. +459 −1 src/cljs/cljs/core.cljs
View
@@ -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]}]
View
@@ -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)))
@@ -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) \. \$) "$"))
Oops, something went wrong.

0 comments on commit ef0d525

Please sign in to comment.