Skip to content

Commit

Permalink
Restore old load-file-code so as to not break public API; large-file-…
Browse files Browse the repository at this point in the history
…capable

fn moved to private load-large-file-code fn and used by default by
load-file middleware (NREPL-34)
  • Loading branch information
cemerick committed Oct 12, 2012
1 parent 2d90bdd commit b052433
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/main/clojure/clojure/tools/nrepl/middleware/load_file.clj
Expand Up @@ -13,11 +13,13 @@
:doc "An atom that temporarily holds the contents of files to :doc "An atom that temporarily holds the contents of files to
be loaded."} file-contents (atom {})) be loaded."} file-contents (atom {}))


(defn ^{:dynamic true} load-file-code (defn- load-large-file-code
"Given the contents of a file, its _source-path-relative_ path, "A variant of `load-file-code` that returns an
and its filename, returns a string of code (or seq of expressions) expression that will only work if evaluated within the same process
that, when evaluated, will load those contents with appropriate where it was called. Here to work around the JVM method size limit
filename references and line numbers in metadata, etc." so that (by default, for those tools using the load-file middleware)
loading files of any size will work when the nREPL server is running
remotely or locally."
[file file-path file-name] [file file-path file-name]
; mini TTL impl so that any code orphaned by errors that occur ; mini TTL impl so that any code orphaned by errors that occur
; between here and the evaluation of the Compiler/load expression ; between here and the evaluation of the Compiler/load expression
Expand All @@ -42,6 +44,20 @@ be loaded."} file-contents (atom {}))
(finally (finally
(swap! @(var file-contents) dissoc '~file-key)))))) (swap! @(var file-contents) dissoc '~file-key))))))


(defn ^{:dynamic true} load-file-code
"Given the contents of a file, its _source-path-relative_ path,
and its filename, returns a string of code containing a single
expression that, when evaluated, will load those contents with
appropriate filename references and line numbers in metadata, etc.
Note that because a single expression is produced, very large
file loads will fail due to the JVM method size limitation.
In such cases, see `load-file-code'`."
[file file-path file-name]
(apply format
"(clojure.lang.Compiler/load (java.io.StringReader. %s) %s %s)"
(map pr-str [file file-path file-name])))

(defn wrap-load-file (defn wrap-load-file
"Middleware that evaluates a file's contents, as per load-file, "Middleware that evaluates a file's contents, as per load-file,
but with all data supplied in the sent message (i.e. safe for use but with all data supplied in the sent message (i.e. safe for use
Expand All @@ -55,7 +71,10 @@ be loaded."} file-contents (atom {}))
(h msg) (h msg)
(h (assoc msg (h (assoc msg
:op "eval" :op "eval"
:code (load-file-code file file-path file-name)))))) :code ((if (thread-bound? #'load-file-code)
load-file-code
load-large-file-code)
file file-path file-name))))))


(set-descriptor! #'wrap-load-file (set-descriptor! #'wrap-load-file
{:requires #{} {:requires #{}
Expand Down

0 comments on commit b052433

Please sign in to comment.