Skip to content

Commit

Permalink
Fix #556: fix referring to var in other namespace via global object
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Oct 5, 2024
1 parent daa29fb commit 707b1e1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## v0.8.116 (2024-10-05)

- [#556](https://github.com/squint-cljs/squint/issues/556): fix referring to var in other namespace via global object in REPL mode

## v0.8.115 (2024-10-01)

- Pass `--repl` opts to `watch` subcommand in CLI
Expand Down
3 changes: 3 additions & 0 deletions bb/node_repl_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
(let [out (:out (p/shell {:out :string} "node test-resources/js_api.mjs"))]
(is (= ["1" "1" "6"] (str/split-lines out)))))

(deftest repl-namespace-global-test
(is (str/includes? (:out (repl "(ns foo.bar) (def x 1) (ns other.ns) (= 1 foo.bar/x)")) "true")))

(defn run-tests [_]
(let [{:keys [fail error]}
(t/run-tests 'node-repl-tests)]
Expand Down
9 changes: 7 additions & 2 deletions src/squint/compiler_common.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,14 @@
(when (contains? @*aliases* (symbol sym-ns))
(str sym-ns "."
(munged-name sn)))
(let [munged (munge (namespace expr))]
(let [ns (namespace expr)
munged (munge ns)
nm (name expr)]
(if (and *repl* (not= "Math" munged))
(str "globalThis." (munge *cljs-ns*) "." munged "." (munge (name expr)))
(let [ns-state (some-> env :ns-state deref)]
(if (get-in ns-state [(symbol ns) (symbol nm)])
(str (munge ns) "." (munge nm))
(str "globalThis." (munge *cljs-ns*) "." munged "." (munge nm))))
(str munged "." (munge (name expr)))))))
(if-let [renamed (get (:var->ident env) expr)]
(cond-> (munge** (str renamed))
Expand Down
11 changes: 6 additions & 5 deletions src/squint/repl/node.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
["net" :as net]
["readline" :as readline]
["squint-cljs/core.js" :as squint]
["vm" :as vm]
["node:util" :as util]
[clojure.string :as str]
[edamame.core :as e]
Expand Down Expand Up @@ -44,14 +43,16 @@
:as new-state} (binding [*cljs-ns* @last-ns]
(compiler/compile-string* (binding [*print-meta* true]
(pr-str the-val)) {:context :return
:elide-exports true}
:elide-exports true
:repl true}
@state))
_ (reset! state new-state)
js-str (str/replace "(async function () {\n%s\n}) ()" "%s" js-str)]
(reset! last-ns cljs-ns)
;; (println "---")
;; (println js-str)
;; (println "---")
#_(binding [*print-fn* *print-err-fn*]
(println "---")
(println js-str)
(println "---"))
(->
(js/Promise.resolve (js/eval js-str))
(.then (fn [^js val]
Expand Down
12 changes: 11 additions & 1 deletion test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@
(squint/compile-string (pr-str '(ns foo (:require ["./popup.css" :as pop]))))))
(t/async done
(let [js (squint/compile-string "(ns foo (:require [clojure.string :as str])) (str 1 2 3 (str/join \",\" [1 2 3]))" {:repl true
:context :return})]
:context :return})]
(-> (.then (js/eval (str/replace "(async function () {\n%s\n})()" "%s" js))
(fn [v]
(is (= "1231,2,3" v))))
Expand All @@ -1625,6 +1625,16 @@
(str/trim (squint/compile-string "(ns foo (:require [\"fs\" :refer [readFileSync]])) readFileSync" {:repl true}))
"globalThis.foo.readFileSync;")))

(deftest global-ns-test
(t/async done
(-> (p/let [js (squint/compile-string "(ns foo.bar) (def x 1) (ns other.ns) foo.bar/x" {:repl true
:context :return
:elide-exports true})
v (js/eval (str/replace "(async function () {\n%s\n})()" "%s" js))]
(is (= 1 v)))
(p/catch (fn [err] (is (throw err))))
(p/finally done))))

(deftest letfn-test
(is (= 3 (jsv! '(letfn [(f [x] (g x)) (g [x] (inc x))] (f 2)))))
(is (= 20 (jsv! '(do (defn foo [] (letfn [(g [x] (f x)) (f [x] (* 2 x))] (g 10))) (foo))))))
Expand Down

0 comments on commit 707b1e1

Please sign in to comment.