1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Add :preload loader.

This commit is contained in:
Calvin Rose 2020-12-06 21:05:16 -06:00
parent 0f16f21677
commit cd09b696b5
2 changed files with 11 additions and 4 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ??? ## Unreleased - ???
- Adds a :preload loader which allows one to manually put things into `module/cache`.
- Add `buffer/push` function. - Add `buffer/push` function.
- Backtick delimited strings and buffers are now reindented based on the column of the - Backtick delimited strings and buffers are now reindented based on the column of the
opening delimiter. WHitespace in columns to the left of the starting column is ignored unless opening delimiter. WHitespace in columns to the left of the starting column is ignored unless

View File

@ -2560,6 +2560,10 @@
(setdyn :syspath (boot/opts "JANET_PATH")) (setdyn :syspath (boot/opts "JANET_PATH"))
(setdyn :headerpath (boot/opts "JANET_HEADERPATH")) (setdyn :headerpath (boot/opts "JANET_HEADERPATH"))
(def module/cache
"Table mapping loaded module identifiers to their environments."
@{})
(defn module/add-paths (defn module/add-paths
``` ```
Add paths to module/paths for a given loader such that Add paths to module/paths for a given loader such that
@ -2584,6 +2588,7 @@
(module/add-paths "/init.janet" :source) (module/add-paths "/init.janet" :source)
(module/add-paths ".janet" :source) (module/add-paths ".janet" :source)
(module/add-paths ".jimage" :image) (module/add-paths ".jimage" :image)
(array/insert module/paths 0 [(fn is-cached [path] (if (in module/cache path) path)) :preload])
# Version of fexists that works even with a reduced OS # Version of fexists that works even with a reduced OS
(defn fexists (defn fexists
@ -2638,10 +2643,6 @@
(undef check-.) (undef check-.)
(undef not-check-.) (undef not-check-.)
(def module/cache
"Table mapping loaded module identifiers to their environments."
@{})
(def module/loading (def module/loading
`Table mapping currently loading modules to true. Used to prevent `Table mapping currently loading modules to true. Used to prevent
circular dependencies.` circular dependencies.`
@ -2706,6 +2707,11 @@
(def newenv (dofile path ;args)) (def newenv (dofile path ;args))
(put module/loading path nil) (put module/loading path nil)
newenv) newenv)
:preload (fn [path & args]
(when-let [m (in module/cache path)]
(if (function? m)
(set (module/cache path) (m path ;args))
m)))
:image (fn [path &] (load-image (slurp path)))}) :image (fn [path &] (load-image (slurp path)))})
(defn require-1 (defn require-1