Skip to content

Commit

Permalink
hash-map and array-map
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Jul 20, 2022
1 parent 51b0b35 commit 6bce9b2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ node_modules
lib
report.html
test/scratch.js
dist
4 changes: 2 additions & 2 deletions corpus/core_vars.cljs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(ns core-vars)

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

(js/console.log js-map)

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

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

Expand Down
6 changes: 3 additions & 3 deletions corpus/core_vars.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { toJs, get, keyword, str, assoc } from 'cherry-cljs/cljs.core.js'
import { toJs, get, keyword, str, arrayMap } from 'cherry-cljs/cljs.core.js'

const js_map = toJs(assoc(null, keyword("foo"), keyword("bar")));
const js_map = toJs(arrayMap(keyword("foo"), keyword("bar")));
console.log(js_map);
const clj_map = assoc(null, keyword("foo/bar"), (1 + 2 + 3));
const clj_map = arrayMap(keyword("foo/bar"), (1 + 2 + 3));
console.log(get(clj_map, keyword("foo/bar")));
console.log(str(clj_map));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "module",
"name": "cherry-cljs",
"sideEffects": false,
"version": "0.0.0-alpha.4",
"version": "0.0.0-alpha.5",
"files": [
"cljs.core.js",
"lib/cljs_core.js"
Expand Down
4 changes: 3 additions & 1 deletion shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
toCljs cljs.core/js->clj
dissoc cljs.core/dissoc
conj cljs.core/conj
get cljs.core/get}}
get cljs.core/get
arrayMap cljs.core/array-map
hashMap cljs.core/hash-map}}
#_#_:transpiler {}}
:build-hooks [(shadow.cljs.build-report/hook
{:output-to "report.html"})]}}}
13 changes: 10 additions & 3 deletions src/cherry/transpiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@
dissoc conj vector clj->js js->clj get]))

(def core->js '{clj->js toJs
js->cljs toCljs})
js->cljs toCljs
hash-map hashMap
array-map arrayMap})

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

Expand Down Expand Up @@ -404,8 +406,13 @@
(emit (into [] expr)))

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

(defn _js [forms]
(with-var-declarations
Expand Down

0 comments on commit 6bce9b2

Please sign in to comment.