Add dofile function.
Abstracts the actually running of a file from
the require function, so a file can be easily
evaluated without being cached.
circular dependencies."
@{})
-(defn require
- "Require a module with the given name. Will search all of the paths in
- module/paths, then the path as a raw file path. Returns the new environment
- returned from compiling and running the file."
+(defn dofile
+ "Evaluate a file in a new environment and return the new environment."
[path & args]
(def {:exit exit-on-error} (table ;args))
- (if-let [check (get module/cache path)]
- check
- (do
- (def [fullpath mod-kind] (module/find path))
- (unless fullpath (error mod-kind))
- (def env
- (case mod-kind
- :source (do
- # Normal janet module
- (def f (file/open fullpath))
+ (def f (file/open path))
(def newenv (make-env))
- (put module/loading fullpath true)
(defn chunks [buf _] (file/read f 2048 buf))
(defn bp [&opt x y]
(def ret (bad-parse x y))
(when (not= (fiber/status f) :dead)
(debug/stacktrace f x)
(if exit-on-error (os/exit 1))))
- :source fullpath})
+ :source path})
(file/close f)
- (put module/loading fullpath nil)
(table/setproto newenv nil))
+
+(defn require
+ "Require a module with the given name. Will search all of the paths in
+ module/paths, then the path as a raw file path. Returns the new environment
+ returned from compiling and running the file."
+ [path & args]
+ (if-let [check (get module/cache path)]
+ check
+ (do
+ (def [fullpath mod-kind] (module/find path))
+ (unless fullpath (error mod-kind))
+ (def env
+ (case mod-kind
+ :source (do
+ (put module/loading fullpath true)
+ (def newenv (dofile fullpath ;args))
+ (put module/loading fullpath nil)
+ newenv)
:native (native fullpath (make-env))
:image (load-image (slurp fullpath))))
(put module/cache fullpath env)
