Last active
December 11, 2015 00:36
-
-
Save mjg123/ae4a6e995645c1b64254 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn ensure-ns [ns-name] | |
(when-not (find-ns (symbol ns-name)) | |
(.setDynamic | |
(intern (create-ns (symbol ns-name)) '*me* "never-seen"))) | |
(the-ns (symbol ns-name))) | |
(defn eval-w-bindings [user-code ns-name user] | |
(let [my-ns (ensure-ns ns-name)] | |
(with-bindings {#'*ns* my-ns | |
(ns-resolve my-ns '*me*) user} | |
(clojure.core/refer 'clojure.core) | |
(eval (read-string user-code))))) | |
(eval-w-bindings "(str \"hello \" *me*)" "mattermost.malory" "andrew") |
This is a bit of a problem though:
;; good
user=> (eval-w-bindings "(defn x [] *me*)" "ns1" "mjg")
#'ns1/x
;; excellent
user=> (eval-w-bindings "(x)" "ns1" "mjg")
"mjg"
;; oh heck
user=> (eval-w-bindings "(ns1/x)" "ns2" "mjg")
"never-seen"
This happens because it's calling the fn from ns1 but the eval-with-bindings has bound the *me*
in ns2. Perhaps the thing to do is to have those vars in the mattermost
namespace. In that case the defn above would look like (defn x [] mattermost/*me*)
, which isn't too bad.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That could use some tidying, but behaves as desired: