mirror of
https://github.com/janet-lang/janet
synced 2025-06-14 20:44:13 +00:00
Add dofile function.
Abstracts the actually running of a file from the require function, so a file can be easily evaluated without being cached.
This commit is contained in:
parent
52ab9fb475
commit
3388acd2db
@ -1646,24 +1646,12 @@
|
|||||||
circular dependencies."
|
circular dependencies."
|
||||||
@{})
|
@{})
|
||||||
|
|
||||||
(defn require
|
(defn dofile
|
||||||
"Require a module with the given name. Will search all of the paths in
|
"Evaluate a file in a new environment and return the new environment."
|
||||||
module/paths, then the path as a raw file path. Returns the new environment
|
|
||||||
returned from compiling and running the file."
|
|
||||||
[path & args]
|
[path & args]
|
||||||
(def {:exit exit-on-error} (table ;args))
|
(def {:exit exit-on-error} (table ;args))
|
||||||
(if-let [check (get module/cache path)]
|
(def f (file/open 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 newenv (make-env))
|
(def newenv (make-env))
|
||||||
(put module/loading fullpath true)
|
|
||||||
(defn chunks [buf _] (file/read f 2048 buf))
|
(defn chunks [buf _] (file/read f 2048 buf))
|
||||||
(defn bp [&opt x y]
|
(defn bp [&opt x y]
|
||||||
(def ret (bad-parse x y))
|
(def ret (bad-parse x y))
|
||||||
@ -1681,10 +1669,27 @@
|
|||||||
(when (not= (fiber/status f) :dead)
|
(when (not= (fiber/status f) :dead)
|
||||||
(debug/stacktrace f x)
|
(debug/stacktrace f x)
|
||||||
(if exit-on-error (os/exit 1))))
|
(if exit-on-error (os/exit 1))))
|
||||||
:source fullpath})
|
:source path})
|
||||||
(file/close f)
|
(file/close f)
|
||||||
(put module/loading fullpath nil)
|
|
||||||
(table/setproto newenv 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))
|
:native (native fullpath (make-env))
|
||||||
:image (load-image (slurp fullpath))))
|
:image (load-image (slurp fullpath))))
|
||||||
(put module/cache fullpath env)
|
(put module/cache fullpath env)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user