Skip to content

Commit

Permalink
Use load instead of require to load core cljs macros
Browse files Browse the repository at this point in the history
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
raph-amiard authored and David Nolen committed Jun 15, 2012
1 parent f30cd5e commit b261447
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/clj/cljs/compiler.clj
Expand Up @@ -22,7 +22,6 @@
(declare confirm-bindings)
(declare munge)
(declare ^:dynamic *cljs-file*)
(require 'cljs.core)

(def js-reserved
#{"abstract" "boolean" "break" "byte" "case"
Expand Down Expand Up @@ -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 {}})
Expand Down Expand Up @@ -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 #(-> %
Expand Down Expand Up @@ -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)
Expand Down

3 comments on commit b261447

@cemerick
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

@swannodette
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@cemerick
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.