diff --git a/src/squint/compiler.cljc b/src/squint/compiler.cljc index d44f3261..09fa4ec8 100644 --- a/src/squint/compiler.cljc +++ b/src/squint/compiler.cljc @@ -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) diff --git a/src/squint/internal/fn.cljc b/src/squint/internal/fn.cljc index fa0f4e1e..973bca06 100644 --- a/src/squint/internal/fn.cljc +++ b/src/squint/internal/fn.cljc @@ -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))] @@ -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)))])) @@ -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#)))) @@ -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)))) diff --git a/test/squint/compiler_test.cljs b/test/squint/compiler_test.cljs index 622eafb0..cbc098ee 100644 --- a/test/squint/compiler_test.cljs +++ b/test/squint/compiler_test.cljs @@ -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)))))