Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve imported vars at runtime, not read time #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
30 changes: 15 additions & 15 deletions src/potemkin/namespaces.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
(throw (IllegalArgumentException.
(str "Calling import-fn on a macro: " sym))))

`(do
(def ~(with-meta n {:protocol protocol}) (deref ~vr))
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
(link-vars ~vr (var ~n))
~vr))))
`(let [vr# (resolve '~sym)]
(def ~(with-meta n {:protocol protocol}) (deref vr#))
(alter-meta! (var ~n) merge (dissoc (meta vr#) :name))
(link-vars vr# (var ~n))
vr#))))

(defmacro import-macro
"Given a macro in another namespace, defines a macro with the same
Expand All @@ -48,12 +48,12 @@
(when-not (:macro m)
(throw (IllegalArgumentException.
(str "Calling import-macro on a non-macro: " sym))))
`(do
(def ~n ~(resolve sym))
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
`(let [vr# (resolve '~sym)]
(def ~n (deref vr#))
(alter-meta! (var ~n) merge (dissoc (meta vr#) :name))
(.setMacro (var ~n))
(link-vars ~vr (var ~n))
~vr))))
(link-vars vr# (var ~n))
vr#))))

(defmacro import-def
"Given a regular def'd var from another namespace, defined a new var with the
Expand All @@ -68,11 +68,11 @@
nspace (:ns m)]
(when-not vr
(throw (IllegalArgumentException. (str "Don't recognize " sym))))
`(do
(def ~n @~vr)
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
(link-vars ~vr (var ~n))
~vr))))
`(let [vr# (resolve '~sym)]
(def ~n (deref vr#))
(alter-meta! (var ~n) merge (dissoc (meta vr#) :name))
(link-vars vr# (var ~n))
vr#))))

(defmacro import-vars
"Imports a list of vars from other namespaces."
Expand Down
3 changes: 3 additions & 0 deletions test/potemkin/imports_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@
x)

(def some-value 1)

(defn ^clojure.lang.ExceptionInfo ex-info-2 [msg data]
(ex-info msg data))
3 changes: 3 additions & 0 deletions test/potemkin/namespaces_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@
(is false "`import-vars` should have thrown an exception")
(catch Exception ex
(is "`clojure.set/onion-misspelled` does not exist" (.getMessage ex)))))

;; This is the whole test for CLJ-1929
(import-vars [potemkin.imports-test ex-info-2])