Browse files

add 'auto require' for macro usage

This automatically adds anything required by a module that implements
a macro to the list of required modules for the module using the
macros. It should reduce the chance of first time macro errors
  • Loading branch information...
1 parent dfcd9cd commit 67268548dbbd90f4378342d1f7bcf98acd7c3d15 @ericbmerritt committed Apr 12, 2012
Showing with 26 additions and 0 deletions.
  1. +26 −0 src/joxa/compiler.jxa
@@ -110,6 +110,17 @@
(erlang/raise type body (erlang/get_stacktrace)))))))
+(defn get-joxa-info (type module)
+ (__try
+ (let (info-fun (erlang/make_fun module :--joxa-info 1))
+ (info-fun type))
+ (catch (type body)
+ (case {type body}
+ ({:error :undef}
+ :false)
+ (_
+ (erlang/raise type body (erlang/get_stacktrace)))))))
;; The Compile Context
;; -------------------
@@ -2309,6 +2320,19 @@
(defn call-macro (module function args)
(erlang/apply module function args))
+;; We auto require anything required by the module exporting the
+;; macro. The reduces by quite a bit the confusion involved in using
+;; macros
+(defn auto-require (ctx module)
+ (case (get-joxa-info :requires module)
+ (:false :ok)
+ (external-requires
+ (lists/foreach (fn (mod-info)
+ (case mod-info
+ ({mod-name _}
+ (add-require-ctx ctx mod-name))))
+ external-requires))))
(defn+ make-call (path0 ctx val args macro-only?)
(let (annots (annots-from-context [] path0 ctx))
(let (possible-arity (erlang/length args)
@@ -2323,10 +2347,12 @@
(call-macro (module-name-ctx ctx) function
(runtime-args-to-arity args 1 arity []))))
({:remote :not-rest :macro {module function arity}}
+ (auto-require ctx module)
(make-expr path0 ctx
(call-macro module function
({:remote :rest :macro {module function arity}}
+ (auto-require ctx module)
(make-expr path0 ctx
(call-macro module function
(runtime-args-to-arity args 1 arity []))))

2 comments on commit 6726854


Do you think we should have a test case for this stuff?


It actually is being tested but implicitly in the modules themselves. we an explicitly test if you think its a good idea

Please sign in to comment.