Permalink
Browse files

Use load instead of require to load core cljs macros

Loading now happens dynamically and once when the
analyzer is first ran.
Also provides macros to facilitate overriding of the
default path provided (defaults to cljs/core).
  • Loading branch information...
1 parent f30cd5e commit b261447e598dd9463ace406337c9f2d1a02bb799 @raph-amiard raph-amiard committed with David Nolen Jun 15, 2012
Showing with 29 additions and 2 deletions.
  1. +29 −2 src/clj/cljs/compiler.clj
View
@@ -22,7 +22,6 @@
(declare confirm-bindings)
(declare munge)
(declare ^:dynamic *cljs-file*)
-(require 'cljs.core)
(def js-reserved
#{"abstract" "boolean" "break" "byte" "case"
@@ -63,6 +62,33 @@
(def ^:dynamic *unchecked-if* (atom false))
(def ^:dynamic *cljs-static-fns* false)
(def ^:dynamic *position* nil)
+(def ^:dynamic *cljs-macros-path* "/cljs/core")
+(def ^:dynamic *cljs-macros-is-classpath* true)
+(def -cljs-macros-loaded (atom false))
+
+(defn load-core []
+ (when (not @-cljs-macros-loaded)
+ (reset! -cljs-macros-loaded true)
+ (if *cljs-macros-is-classpath*
+ (load *cljs-macros-path*)
+ (load-file *cljs-macros-path*))))
+
+(defmacro with-core-macros
+ [path & body]
+ `(do
+ (when (not= *cljs-macros-path* ~path)
+ (reset! -cljs-macros-loaded false))
+ (binding [*cljs-macros-path* ~path]
+ ~@body)))
+
+(defmacro with-core-macros-file
+ [path & body]
+ `(do
+ (when (not= *cljs-macros-path* ~path)
+ (reset! -cljs-macros-loaded false))
+ (binding [*cljs-macros-path* ~path
+ *cljs-macros-is-classpath* false]
+ ~@body)))
(defn empty-env []
{:ns (@namespaces *cljs-ns*) :context :statement :locals {}})
@@ -1308,7 +1334,7 @@
(when (seq @deps)
(analyze-deps @deps))
(set! *cljs-ns* name)
- (require 'cljs.core)
+ (load-core)
(doseq [nsym (concat (vals requires-macros) (vals uses-macros))]
(clojure.core/require nsym))
(swap! namespaces #(-> %
@@ -1581,6 +1607,7 @@
(let [form (if (instance? clojure.lang.LazySeq form)
(or (seq form) ())
form)]
+ (load-core)
(cond
(symbol? form) (analyze-symbol env form)
(and (seq? form) (seq form)) (analyze-seq env form name)

3 comments on commit b261447

Member

cemerick commented on b261447 Nov 1, 2013

Hi @raph-amiard! I'm working on a bit of a refactor to the state changed and maintained by the cljs compiler (see CLJS-643, with progress happening over here), and I have a question about the cljs-macro-loading bits that you put together in this commit: what are the use cases that this load redirection supports? I have some guesses (and maybe @swannodette knows, too), but thought it'd be worth asking the source.

(BTW, I'm not planning on changing this for CLJS-643 at all, just trying to grok more of the internals.)

Member

swannodette replied Nov 1, 2013

I think this was just to parameterize how macros are loaded, this was to allow Lua ClojureScript to reuse analyzer.clj.

Member

cemerick replied Nov 1, 2013

Yeah, figured it was "emitter"-related, thanks, I had forgotten about clojurescript-lua.

Please sign in to comment.