Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit 99d567c

Browse files
committed
hash-map and array-map
1 parent 188849b commit 99d567c

File tree

6 files changed

+20
-10
lines changed

6 files changed

+20
-10
lines changed

cherry/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ node_modules
77
lib
88
report.html
99
test/scratch.js
10+
dist

cherry/corpus/core_vars.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
(ns core-vars)
22

3-
(def js-map (clj->js (assoc nil :foo :bar)))
3+
(def js-map (clj->js {:foo :bar}))
44

55
(js/console.log js-map)
66

7-
(def clj-map (assoc nil :foo/bar (+ 1 2 3)))
7+
(def clj-map {:foo/bar (+ 1 2 3)})
88

99
(js/console.log (get clj-map :foo/bar)) ;; => 6
1010

cherry/corpus/core_vars.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { toJs, get, keyword, str, assoc } from 'cherry-cljs/cljs.core.js'
1+
import { toJs, get, keyword, str, arrayMap } from 'cherry-cljs/cljs.core.js'
22

3-
const js_map = toJs(assoc(null, keyword("foo"), keyword("bar")));
3+
const js_map = toJs(arrayMap(keyword("foo"), keyword("bar")));
44
console.log(js_map);
5-
const clj_map = assoc(null, keyword("foo/bar"), (1 + 2 + 3));
5+
const clj_map = arrayMap(keyword("foo/bar"), (1 + 2 + 3));
66
console.log(get(clj_map, keyword("foo/bar")));
77
console.log(str(clj_map));

cherry/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "module",
33
"name": "cherry-cljs",
44
"sideEffects": false,
5-
"version": "0.0.0-alpha.4",
5+
"version": "0.0.0-alpha.5",
66
"files": [
77
"cljs.core.js",
88
"lib/cljs_core.js"

cherry/shadow-cljs.edn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
toCljs cljs.core/js->clj
1919
dissoc cljs.core/dissoc
2020
conj cljs.core/conj
21-
get cljs.core/get}}
21+
get cljs.core/get
22+
arrayMap cljs.core/array-map
23+
hashMap cljs.core/hash-map}}
2224
#_#_:transpiler {}}
2325
:build-hooks [(shadow.cljs.build-report/hook
2426
{:output-to "report.html"})]}}}

cherry/src/cherry/transpiler.clj

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@
102102
dissoc conj vector clj->js js->clj get]))
103103

104104
(def core->js '{clj->js toJs
105-
js->cljs toCljs})
105+
js->cljs toCljs
106+
hash-map hashMap
107+
array-map arrayMap})
106108

107109
(def prefix-unary-operators (set ['!]))
108110

@@ -404,8 +406,13 @@
404406
(emit (into [] expr)))
405407

406408
(defmethod emit clojure.lang.IPersistentMap [expr]
407-
(letfn [(json-pair [pair] (str (emit (key pair)) ": " (emit (val pair))))]
408-
(str "{" (str/join ", " (map json-pair (seq expr))) "}")))
409+
(let [map-fn
410+
(if (<= (count expr) 8)
411+
'arrayMap
412+
'hashMap)]
413+
(swap! *imported-core-vars* conj map-fn)
414+
(letfn [(mk-pair [pair] (str (emit (key pair)) ", " (emit (val pair))))]
415+
(format "%s(%s)" map-fn (str/join ", " (map mk-pair (seq expr)))))))
409416

410417
(defn _js [forms]
411418
(with-var-declarations

0 commit comments

Comments
 (0)