Permalink
Browse files

use resolve instead of eval to get the var

  • Loading branch information...
1 parent a850ba1 commit 86452e53934f4b59aa3fff2dd8792b6c13b3cd2a @ztellman committed Dec 13, 2011
Showing with 30 additions and 32 deletions.
  1. +3 −3 src/potemkin.clj
  2. +20 −19 src/potemkin/namespace.clj
  3. +7 −10 test/potemkin/test/namespace.clj
View
@@ -11,10 +11,10 @@
[potemkin.namespace :as namespace]
[potemkin.map :as map]))
-(namespace/import-macro #'namespace/import-macro) ;; totally meta
-(import-macro #'namespace/import-fn)
+(namespace/import-macro namespace/import-macro) ;; totally meta
+(import-macro namespace/import-fn)
-(import-macro #'map/def-custom-map)
+(import-macro map/def-custom-map)
View
@@ -6,44 +6,45 @@
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
-(ns potemkin.namespace)
+(ns potemkin.namespace
+ (:use [clojure pprint]))
(defmacro import-fn
"Given a function in another namespace, defines a function with the same name in the
current namespace. Argument lists, doc-strings, and original line-numbers are preserved."
[sym]
- (let [m (meta (eval sym))
- m (meta (intern (:ns m) (:name m)))
+ (let [vr (resolve sym)
+ m (meta vr)
+ nspace (:name m)
n (:name m)
arglists (:arglists m)
- doc (:doc m)]
- #_`(do
- (def ~n ~(ns-resolve (:ns m) (:name m)))
- (alter-meta! ~(list 'var n) assoc
+ doc (:doc m)
+ protocol (:protocol m)]
+ `(do
+ (def ~(with-meta n {:protocol protocol}) ~sym)
+ (alter-meta! (var ~n) assoc
:doc ~doc
:arglists ~(list 'quote arglists)
:file ~(:file m)
:line ~(:line m))
- ~(list 'var n))
- (list `def (with-meta n {:doc doc :arglists (list 'quote arglists) :file (:file m) :line (:line m)}) (eval sym))))
+ ~vr)))
(defmacro import-macro
"Given a macro in another namespace, defines a macro with the same name in the
current namespace. Argument lists, doc-strings, and original line-numbers are preserved."
[sym]
- (let [sym (eval sym)
- m (meta sym)
- m (meta (intern (:ns m) (:name m)))
+ (let [vr (resolve sym)
+ m (meta vr)
n (:name m)
+ nspace (:ns m)
arglists (:arglists m)
- doc (:doc m)
- args-sym (gensym "args")]
+ doc (:doc m)]
`(do
- (defmacro ~(with-meta n {:arglists arglists})
- [~'& ~args-sym]
- (list* ~sym ~args-sym))
- (alter-meta! ~(list 'var n) assoc
+ (def ~n ~(resolve sym))
+ (alter-meta! (var ~n) assoc
:doc ~doc
+ :arglists ~(list 'quote arglists)
:file ~(:file m)
:line ~(:line m))
- ~(list 'var n))))
+ (.setMacro (var ~n))
+ ~vr)))
@@ -15,11 +15,11 @@
[clojure.string :as str]
[potemkin.test.protocol :as p]))
-(import-macro #'repl/source)
-(import-macro #'repl/doc)
-(import-fn #'repl/find-doc)
-(import-fn #'p/multi-arity)
-(import-fn #'p/protocol-function)
+(import-macro repl/source)
+(import-macro repl/doc)
+(import-fn repl/find-doc)
+(import-fn p/multi-arity)
+(import-fn p/protocol-function)
(defn drop-lines [n s]
(->> s str/split-lines (drop n) (interpose "\n") (apply str)))
@@ -28,9 +28,7 @@
`(= ~@(map (fn [x] `(with-out-str ~x)) args)))
(defmacro rest-out= [& args]
- `(do
- ;;(do ~@(map (fn [x] `(println (with-out-str ~x))) args))
- (= ~@(map (fn [x] `(drop-lines 2 (with-out-str ~x))) args))))
+ `(= ~@(map (fn [x] `(drop-lines 2 (with-out-str ~x))) args)))
(deftest test-import-macro
(is (out= (source repl/source) (source source)))
@@ -41,7 +39,6 @@
(is (rest-out= (doc repl/find-doc) (doc find-doc)))
(is (out= (source p/multi-arity) (source multi-arity)))
(is (rest-out= (doc p/multi-arity) (doc multi-arity)))
- (is (rest-out= (doc p/protocol-function) (doc protocol-function)))
- )
+ (is (rest-out= (doc p/protocol-function) (doc protocol-function))))

0 comments on commit 86452e5

Please sign in to comment.