Skip to content

Commit

Permalink
Fix varargs in REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Dec 2, 2023
1 parent 2291cca commit d9b4900
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
33 changes: 17 additions & 16 deletions src/squint/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -340,22 +340,23 @@
(defn transpile-form
([f] (transpile-form f nil))
([f env]
(str
(emit f (merge {:ns-state (atom {})
:context :statement
:top-level true
:core-vars core-vars
:gensym (let [ctr (volatile! 0)]
(fn [sym]
(let [next-id (vswap! ctr inc)]
(symbol (str (if sym (munge sym)
"G__") next-id)))))
:emit {::cc/list emit-list
::cc/vector emit-vector
::cc/map emit-map
::cc/keyword emit-keyword
::cc/set emit-set
::cc/special emit-special}} env)))))
(binding [cc/*repl* (:repl env cc/*repl*)]
(str
(emit f (merge {:ns-state (atom {})
:context :statement
:top-level true
:core-vars core-vars
:gensym (let [ctr (volatile! 0)]
(fn [sym]
(let [next-id (vswap! ctr inc)]
(symbol (str (if sym (munge sym)
"G__") next-id)))))
:emit {::cc/list emit-list
::cc/vector emit-vector
::cc/map emit-map
::cc/keyword emit-keyword
::cc/set emit-set
::cc/special emit-special}} env))))))

(def ^:dynamic *jsx* false)

Expand Down
13 changes: 9 additions & 4 deletions src/squint/internal/fn.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
(defn- variadic-fn*
([sym method]
(variadic-fn* sym method true))
([sym [arglist & body :as method] solo]
([sym [arglist & body :as _method] solo]
(let [sig (remove '#{&} arglist)
restarg (gensym "seq")
async (:async (meta sym))]
Expand Down Expand Up @@ -94,7 +94,8 @@
(range c)))
(fixed-arity [rname sig]
(let [c (count sig)]
[c `(. ~rname
[c `(. ;; prevent resolving rname, for REPL mode
~(list 'js* (str rname))
(~(symbol
(str "cljs$core$IFn$_invoke$arity$" c))
~@(dest-args c)))]))
Expand Down Expand Up @@ -160,7 +161,9 @@
(let [argseq# (when (< ~maxfa (.-length ~args-arr))
(.slice ~args-arr ~maxfa) #_(new #_:ana/no-resolve cljs.core/IndexedSeq
0 nil))]
(. ~rname
(.
;; prevent resolving rname, for REPL mode
~(list 'js* (str rname))
(~'cljs$core$IFn$_invoke$arity$variadic
~@(dest-args maxfa)
argseq#))))
Expand Down Expand Up @@ -216,7 +219,9 @@
(let [argseq# (when (< ~c-1 (.-length ~args-sym))
(.slice ~args-sym ~c-1) #_(new ^:ana/no-resolve cljs.core/IndexedSeq
0 nil))]
(. ~rname (~'cljs$core$IFn$_invoke$arity$variadic ~@(dest-args c-1) argseq#)))))
(.
;; prevent resolving rname, for REPL mode
~(list 'js* (str rname)) (~'cljs$core$IFn$_invoke$arity$variadic ~@(dest-args c-1) argseq#)))))
{:async async})]
~(variadic-fn* name method)
~name))))
Expand Down
22 changes: 20 additions & 2 deletions test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,27 @@
(is (= 1 ((js/eval s) 1))))
(is (= 1 (jsv! '(do (defn foo [] (fn [x] x)) ((foo) 1))))))

(deftest jss!-test
(is (not (str/includes? (jss! '(def x 1) {:repl false}) "globalThis")))
(is (str/includes? (jss! '(def x 1) {:repl true}) "globalThis")))

(deftest fn-varargs-test
(is (eq #js [3 4] (jsv! '(let [f (fn foo [x y & zs] zs)] (f 1 2 3 4)))))
(is (nil? (jsv! '(let [f (fn foo [x y & zs] zs)] (f 1 2))))))
(doseq [repl [true false]]
(testing "vararg fixed arity"
(is (nil? (jsv! '(let [f (fn foo [x y & zs] zs)] (f 1 2)) {:repl repl}))))
(testing "vararg vararg arity"
(is (eq #js [3 4] (jsv! '(let [f (fn foo [x y & zs] zs)] (f 1 2 3 4)) {:repl repl}))))
(testing "multi vararg fixed arity"
(is (eq 1 (jsv! '(let [f (fn foo
([y] 1)
([y & zs] zs))]
(f 1))
{:repl repl}))))
(testing "multi vararg vararg arity"
(is (eq [2] (jsv! '(let [f (fn foo
([y] 1)
([y & zs] zs))]
(f 1 2)) {:repl repl}))))))

(deftest fn-multi-arity-test
(is (= 1 (jsv! '(let [f (fn foo ([x] x) ([x y] y))] (f 1)))))
Expand Down

0 comments on commit d9b4900

Please sign in to comment.