From a17798d9d56f5a68e072ee86f98a61d7c4059a67 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 29 Nov 2024 19:13:41 +0100 Subject: [PATCH 1/7] v0.8.127 --- CHANGELOG.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40d474e9..e1c669f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ [Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect -## v0.8.126 (2024-11-29) +## v0.8.127 (2024-11-29) - [#586](https://github.com/squint-cljs/squint/issues/586): support extending protocol to `nil` diff --git a/package.json b/package.json index e28f05d5..037d6d02 100644 --- a/package.json +++ b/package.json @@ -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", From d1652acab2190217506e254ff5866dfb683d7d8c Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 2 Dec 2024 11:48:19 +0100 Subject: [PATCH 2/7] wip --- resources/squint/core.edn | 1 + src/squint/compiler.cljc | 4 ---- src/squint/core.js | 7 +++++++ test/squint/compiler_test.cljs | 6 ++++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/resources/squint/core.edn b/resources/squint/core.edn index 9fddc6a7..df6f4ddf 100644 --- a/resources/squint/core.edn +++ b/resources/squint/core.edn @@ -33,6 +33,7 @@ bounded_count butlast cat + clj__GT_js coll_QMARK_ comp compare diff --git a/src/squint/compiler.cljc b/src/squint/compiler.cljc index 794256c0..a4fc93b8 100644 --- a/src/squint/compiler.cljc +++ b/src/squint/compiler.cljc @@ -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])) @@ -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 { diff --git a/src/squint/core.js b/src/squint/core.js index cdb1c073..9260e438 100644 --- a/src/squint/core.js +++ b/src/squint/core.js @@ -2731,3 +2731,10 @@ export class Delay { } } } + +export function clj__GT_js(x) { + if (x instanceof LazyIterable) { + return vec(x); + } + return x; +} diff --git a/test/squint/compiler_test.cljs b/test/squint/compiler_test.cljs index 37034533..05e19f60 100644 --- a/test/squint/compiler_test.cljs +++ b/test/squint/compiler_test.cljs @@ -2333,5 +2333,11 @@ 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])})")))) + (defn init [] (t/run-tests 'squint.compiler-test 'squint.jsx-test 'squint.string-test 'squint.html-test)) From f3c09f833201059137b722bf2f08013d078ede44 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 2 Dec 2024 12:07:20 +0100 Subject: [PATCH 3/7] Fix #585: clj->js --- src/squint/core.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/squint/core.js b/src/squint/core.js index 9260e438..98383eb8 100644 --- a/src/squint/core.js +++ b/src/squint/core.js @@ -2733,8 +2733,12 @@ export class Delay { } export function clj__GT_js(x) { - if (x instanceof LazyIterable) { - return vec(x); + if (map_QMARK_(x)) { + return update_vals(x, clj__GT_js); + } + + if (coll_QMARK_(x)) { + return mapv(clj__GT_js, x); } return x; } From 2515d55b22723997fd9d902ce4ffa20f249b718e Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 2 Dec 2024 12:13:23 +0100 Subject: [PATCH 4/7] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1c669f0..6734d413 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ [Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect +## 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` From 271cc75331fb560d6fdd1e14bda0cd0e0b8012a9 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 2 Dec 2024 12:28:24 +0100 Subject: [PATCH 5/7] optimze mapv --- src/squint/core.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/squint/core.js b/src/squint/core.js index 98383eb8..c3e4f7c5 100644 --- a/src/squint/core.js +++ b/src/squint/core.js @@ -1056,6 +1056,10 @@ export function vector_QMARK_(x) { } export function mapv(...args) { + if (args.length === 2) { + const [f, coll] = args; + return into([], map(f), coll); + } return [...map(...args)]; } From 6fb8c7fcbbea1ef3dd6c9d8e4ac3d713570a9d31 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 2 Dec 2024 12:33:20 +0100 Subject: [PATCH 6/7] wippp --- src/squint/core.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/squint/core.js b/src/squint/core.js index c3e4f7c5..674fe868 100644 --- a/src/squint/core.js +++ b/src/squint/core.js @@ -1058,6 +1058,13 @@ 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)]; From 0967898d0e089d737aee45e152f7645b542498b2 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 2 Dec 2024 14:01:30 +0100 Subject: [PATCH 7/7] protect against circularity --- src/squint/core.js | 13 ++++++++++--- test/squint/compiler_test.cljs | 4 +++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/squint/core.js b/src/squint/core.js index 674fe868..78c6023a 100644 --- a/src/squint/core.js +++ b/src/squint/core.js @@ -2743,13 +2743,20 @@ export class Delay { } } -export function clj__GT_js(x) { +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, clj__GT_js); + return update_vals(x, x => clj__GT_js_(x, seen)); } if (coll_QMARK_(x)) { - return mapv(clj__GT_js, 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()); +} diff --git a/test/squint/compiler_test.cljs b/test/squint/compiler_test.cljs index 05e19f60..dfc8df23 100644 --- a/test/squint/compiler_test.cljs +++ b/test/squint/compiler_test.cljs @@ -2337,7 +2337,9 @@ new Foo();") (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])})")))) + (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))