Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
[Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect

## Unreleased

- [#779](https://github.com/squint-cljs/squint/pull/779): Added `compare-and-swap!`, `swap-vals!` and `reset-vals!`
- Multiple `:require-macros` with `:refer` now accumulate instead of overwriting
- Fix qualified symbol resolution in macro expansions when namespace has different runtime alias
- Transitively load macros from namespaces required by macro namespaces
- Add `cherry-ns-mappings` and `resolve-macro-ns` for unified namespace and macro resolution in cherry

## v0.9.182

Expand Down
3 changes: 2 additions & 1 deletion bb/tasks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
(assert (str/includes? output "my-other-src 1 2"))
(assert (str/includes? output "json!"))
(assert (str/includes? output "{ a: 1 }"))
(assert (str/includes? output "\"emit!\"")))
(assert (str/includes? output "\"emit!\""))
(assert (str/includes? output "qualified test: 142")))
(assert (fs/exists? "test-project/lib/foo.json"))
(assert (fs/exists? "test-project/lib/baz.css"))
(assert (not (fs/exists? "test-project/lib/bar.json")))))
Expand Down
5 changes: 3 additions & 2 deletions src/squint/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@
(or
;; used by cherry embed:
(some-> env :macros (get nss) (get nms))
(let [resolved-ns (get-in current-ns-state [:aliases nss] nss)]
(get-in ns-state [:macros resolved-ns nms]))))
(let [resolved-ns (get-in current-ns-state [:aliases nss] nss)
macro-ns (cc/resolve-macro-ns resolved-ns)]
(get-in ns-state [:macros macro-ns nms]))))
(let [refers (:refers current-ns-state)]
(when-let [macro-ns (get refers nms)]
(get-in ns-state [:macros macro-ns nms])))))))]
Expand Down
14 changes: 11 additions & 3 deletions src/squint/compiler/node.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
reload (concat [:reload])))
(let [publics (eval-form
`(ns-publics '~macro-ns))
ks (keys publics)
vs (vals publics)
vs (map deref vs)
macro-vars (filter (fn [[_ v]] (:macro (meta v))) publics)
ks (map first macro-vars)
vs (map (comp deref second) macro-vars)
publics (zipmap ks vs)]
publics)))]
(.then macros
Expand All @@ -53,6 +53,14 @@
(cond-> (assoc-in ns-state [:macros macro-ns] macros)
as (assoc-in [the-ns-name :aliases as] macro-ns)
refer (update-in [the-ns-name :refers] merge (zipmap refer (repeat macro-ns))))))
(let [deps (eval-form (list 'mapv 'ns-name (list 'vals (list 'ns-aliases (list 'quote macro-ns)))))]
(doseq [dep deps]
(when-not (get-in @ns-state [:macros dep])
(let [dep-publics (eval-form (list 'ns-publics (list 'quote dep)))
dep-macros (filter (fn [[_ v]] (:macro (meta v))) dep-publics)
dep-map (zipmap (map first dep-macros)
(map (comp deref second) dep-macros))]
(swap! ns-state assoc-in [:macros dep] dep-map)))))
#_(set! compiler/built-in-macros
;; hack
(assoc compiler/built-in-macros macro-ns macros))))))))
Expand Down
36 changes: 27 additions & 9 deletions src/squint/compiler_common.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@
(str "globalThis." (munge *cljs-ns*) "."))
(alias-munge sym-ns) "."
(munged-name sn)))
(when-let [alias (get (:libname->alias current-ns) (str sym-ns))]
(str (when *repl*
(str "globalThis." (munge *cljs-ns*) "."))
(alias-munge alias) "."
(munged-name sn)))
(let [ns (namespace expr)
munged (munge ns)
nm (name expr)]
Expand Down Expand Up @@ -559,6 +564,21 @@
(cond-> (format "(%sfunction () {\n %s\n})()" (if *async* "async " "") s)
*async* (wrap-await)))

(def cherry-ns-mappings
{'clojure.string {:js "cherry-cljs/lib/clojure.string.js"}
'cljs.string {:js "cherry-cljs/lib/clojure.string.js"}
'clojure.walk {:js "cherry-cljs/lib/clojure.walk.js"}
'cljs.walk {:js "cherry-cljs/lib/clojure.walk.js"}
'clojure.set {:js "cherry-cljs/lib/clojure.set.js"}
'cljs.set {:js "cherry-cljs/lib/clojure.set.js"}
'clojure.pprint {:js "cherry-cljs/lib/cljs.pprint.js"}
'cljs.pprint {:js "cherry-cljs/lib/cljs.pprint.js"}
'clojure.test {:js "cherry-cljs/lib/clojure.test.js" :macro-ns 'cherry.test}
'cljs.test {:js "cherry-cljs/lib/clojure.test.js" :macro-ns 'cherry.test}})

(defn resolve-macro-ns [alias]
(or (get-in cherry-ns-mappings [alias :macro-ns]) alias))

(defn resolve-import-map [import-maps lib]
(get import-maps lib lib))

Expand All @@ -576,13 +596,7 @@
alias)
(resolve-import-map import-maps alias)))
:cherry
(case alias
(cljs.string clojure.string) "cherry-cljs/lib/clojure.string.js"
(cljs.walk clojure.walk) "cherry-cljs/lib/clojure.walk.js"
(cljs.set clojure.set) "cherry-cljs/lib/clojure.set.js"
(cljs.pprint clojure.pprint) "cherry-cljs/lib/cljs.pprint.js"
(cljs.test clojure.test) "cherry-cljs/lib/clojure.test.js"
alias)
(or (get-in cherry-ns-mappings [alias :js]) alias)
alias)))

(defn unwrap [s]
Expand All @@ -592,6 +606,7 @@
(when-not (or (= 'squint.core libname)
(= 'cherry.core libname))
(let [env (expr-env env)
original-libname libname
libname (resolve-ns env libname)
[libname suffix] (str/split (if (string? libname) libname (str libname)) #"\$" 2)
default? (= "default" suffix) ;; we only support a default suffix for now anyway
Expand Down Expand Up @@ -674,8 +689,11 @@
(swap! (:ns-state env)
(fn [ns-state]
(let [current (:current ns-state)]
(update-in ns-state [current :aliases] (fn [aliases]
((fnil assoc {}) aliases as libname)))))))
(-> ns-state
(update-in [current :aliases] (fn [aliases]
((fnil assoc {}) aliases as libname)))
(update-in [current :libname->alias] (fn [m]
((fnil assoc {}) m (str original-libname) as))))))))
(when-not (:elide-imports env)
expr)
#_nil)))
Expand Down
6 changes: 6 additions & 0 deletions test-project/src/macros.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
(ns macros
#?(:cljs (:require ["node:fs" :as fs])))

(defn add-100 [x]
(+ x 100))

(defmacro with-add-100 [x]
`(add-100 ~x))

(defmacro debug [_kwd body]
`(println ::debug ~body))

Expand Down
5 changes: 5 additions & 0 deletions test-project/src/macros_alias.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns macros-alias
(:require [macros-impl :as impl]))

(defmacro wrapper [kwd body]
`(impl/real-debug ~kwd ~body))
4 changes: 4 additions & 0 deletions test-project/src/macros_impl.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns macros-impl)

(defmacro real-debug [kwd body]
`(println "real-debug:" ~kwd ~body))
9 changes: 7 additions & 2 deletions test-project/src/main.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns main
(:require-macros [macros :as m :refer [debug]]
(:require-macros [macros :as m :refer [debug with-add-100]]
[macros-alias :refer [wrapper]]
[macros2 :refer [also]])
(:require [other-ns]
(:require [macros :as mac]
[other-ns]
[my-other-src :as src]
["fs" :as fs]
["path" :as path]
Expand All @@ -28,3 +30,6 @@
println)

(js/console.log json)

(println "qualified test:" (m/with-add-100 42))
(wrapper :nested 444)
Loading