From bfe1ab524ea6132010286d7d298001a2ca560ebe Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 2 Dec 2024 14:05:03 +0100 Subject: [PATCH] Fix #585: clj->js (#591) --- CHANGELOG.md | 6 +++++- package.json | 2 +- resources/squint/core.edn | 1 + src/squint/compiler.cljc | 4 ---- src/squint/core.js | 29 +++++++++++++++++++++++++++++ test/squint/compiler_test.cljs | 8 ++++++++ 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40d474e9..6734d413 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` 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", 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..78c6023a 100644 --- a/src/squint/core.js +++ b/src/squint/core.js @@ -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)]; } @@ -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()); +} diff --git a/test/squint/compiler_test.cljs b/test/squint/compiler_test.cljs index 37034533..dfc8df23 100644 --- a/test/squint/compiler_test.cljs +++ b/test/squint/compiler_test.cljs @@ -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))