Skip to content

Commit bfe1ab5

Browse files
authored
Fix #585: clj->js (#591)
1 parent 9b15f6e commit bfe1ab5

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
[Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect
44

5-
## v0.8.126 (2024-11-29)
5+
## v0.8.128 (2024-12-02)
6+
7+
- [#585](https://github.com/squint-cljs/squint/issues/585): fix `clj->js` to realize lazy seqs into arrays
8+
9+
## v0.8.127 (2024-11-29)
610

711
- [#586](https://github.com/squint-cljs/squint/issues/586): support extending protocol to `nil`
812

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "squint-cljs",
33
"type": "module",
44
"sideEffects": false,
5-
"version": "0.8.126",
5+
"version": "0.8.127",
66
"files": [
77
"core.js",
88
"src/squint/core.js",

resources/squint/core.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
bounded_count
3434
butlast
3535
cat
36+
clj__GT_js
3637
coll_QMARK_
3738
comp
3839
compare

src/squint/compiler.cljc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
'squint-compiler-html
4646
'squint.impl/deref
4747
'require 'squint.defclass/defclass* 'squint.defclass/super*
48-
'clj->js
4948
'squint.impl/for-of
5049
'squint.impl/defonce]))
5150

@@ -128,9 +127,6 @@
128127
(defmethod emit-special 'js/typeof [_ env [_ form]]
129128
(emit-return (str "typeof " (emit form (expr-env env))) env))
130129

131-
(defmethod emit-special 'clj->js [_ env [_ form]]
132-
(emit form env))
133-
134130
(defmethod emit-special 'deftype* [_ env [_ t fields pmasks body]]
135131
(let [fields (map munge fields)]
136132
(str "var " (munge t) " = " (format "function %s {

src/squint/core.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,17 @@ export function vector_QMARK_(x) {
10561056
}
10571057

10581058
export function mapv(...args) {
1059+
if (args.length === 2) {
1060+
const [f, coll] = args;
1061+
if (coll instanceof LazyIterable) {
1062+
var ret = [];
1063+
for (const x of coll) {
1064+
ret.push(f(x));
1065+
}
1066+
return ret;
1067+
}
1068+
return into([], map(f), coll);
1069+
}
10591070
return [...map(...args)];
10601071
}
10611072

@@ -2731,3 +2742,21 @@ export class Delay {
27312742
}
27322743
}
27332744
}
2745+
2746+
function clj__GT_js_(x, seen) {
2747+
// we need to protect against circular objects
2748+
if (seen.has(x)) return x;
2749+
seen.add(x);
2750+
if (map_QMARK_(x)) {
2751+
return update_vals(x, x => clj__GT_js_(x, seen));
2752+
}
2753+
2754+
if (coll_QMARK_(x)) {
2755+
return mapv(x => clj__GT_js_(x, seen), x);
2756+
}
2757+
return x;
2758+
}
2759+
2760+
export function clj__GT_js(x) {
2761+
return clj__GT_js_(x, new Set());
2762+
}

test/squint/compiler_test.cljs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,5 +2333,13 @@ new Foo();")
23332333
(is (eq [true true false]
23342334
(jsv! "[(map? {}) (map? (new Map [])) (map? [])]"))))
23352335

2336+
(deftest clj->js-test
2337+
(is (eq [2 3 4]
2338+
(jsv! "(clj->js (map inc [1 2 3]))")))
2339+
(is (eq {:a [2 3 4]}
2340+
(jsv! "(clj->js {:a (map inc [1 2 3])})")))
2341+
(is (eq [2 3 4]
2342+
(jsv! "(def x {:a 1 :b (map inc [1 2 3])}) (set! (.-a x) x) (:b (clj->js x))"))))
2343+
23362344
(defn init []
23372345
(t/run-tests 'squint.compiler-test 'squint.jsx-test 'squint.string-test 'squint.html-test))

0 commit comments

Comments
 (0)