Skip to content

Commit

Permalink
Fix #585: clj->js (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Dec 2, 2024
1 parent 9b15f6e commit bfe1ab5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

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

## v0.8.126 (2024-11-29)
## v0.8.128 (2024-12-02)

- [#585](https://github.com/squint-cljs/squint/issues/585): fix `clj->js` to realize lazy seqs into arrays

## v0.8.127 (2024-11-29)

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "squint-cljs",
"type": "module",
"sideEffects": false,
"version": "0.8.126",
"version": "0.8.127",
"files": [
"core.js",
"src/squint/core.js",
Expand Down
1 change: 1 addition & 0 deletions resources/squint/core.edn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
bounded_count
butlast
cat
clj__GT_js
coll_QMARK_
comp
compare
Expand Down
4 changes: 0 additions & 4 deletions src/squint/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
'squint-compiler-html
'squint.impl/deref
'require 'squint.defclass/defclass* 'squint.defclass/super*
'clj->js
'squint.impl/for-of
'squint.impl/defonce]))

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

(defmethod emit-special 'clj->js [_ env [_ form]]
(emit form env))

(defmethod emit-special 'deftype* [_ env [_ t fields pmasks body]]
(let [fields (map munge fields)]
(str "var " (munge t) " = " (format "function %s {
Expand Down
29 changes: 29 additions & 0 deletions src/squint/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,17 @@ export function vector_QMARK_(x) {
}

export function mapv(...args) {
if (args.length === 2) {
const [f, coll] = args;
if (coll instanceof LazyIterable) {
var ret = [];
for (const x of coll) {
ret.push(f(x));
}
return ret;
}
return into([], map(f), coll);
}
return [...map(...args)];
}

Expand Down Expand Up @@ -2731,3 +2742,21 @@ export class Delay {
}
}
}

function clj__GT_js_(x, seen) {
// we need to protect against circular objects
if (seen.has(x)) return x;
seen.add(x);
if (map_QMARK_(x)) {
return update_vals(x, x => clj__GT_js_(x, seen));
}

if (coll_QMARK_(x)) {
return mapv(x => clj__GT_js_(x, seen), x);
}
return x;
}

export function clj__GT_js(x) {
return clj__GT_js_(x, new Set());
}
8 changes: 8 additions & 0 deletions test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2333,5 +2333,13 @@ new Foo();")
(is (eq [true true false]
(jsv! "[(map? {}) (map? (new Map [])) (map? [])]"))))

(deftest clj->js-test
(is (eq [2 3 4]
(jsv! "(clj->js (map inc [1 2 3]))")))
(is (eq {:a [2 3 4]}
(jsv! "(clj->js {:a (map inc [1 2 3])})")))
(is (eq [2 3 4]
(jsv! "(def x {:a 1 :b (map inc [1 2 3])}) (set! (.-a x) x) (:b (clj->js x))"))))

(defn init []
(t/run-tests 'squint.compiler-test 'squint.jsx-test 'squint.string-test 'squint.html-test))

0 comments on commit bfe1ab5

Please sign in to comment.