From cd09b696b50733474732a349206b31408d3ea35c Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 6 Dec 2020 21:05:16 -0600 Subject: [PATCH] Add :preload loader. --- CHANGELOG.md | 1 + src/boot/boot.janet | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d8c7291..e42a2286 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Adds a :preload loader which allows one to manually put things into `module/cache`. - Add `buffer/push` function. - 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 diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 9676be74..1dc035a8 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2560,6 +2560,10 @@ (setdyn :syspath (boot/opts "JANET_PATH")) (setdyn :headerpath (boot/opts "JANET_HEADERPATH")) +(def module/cache + "Table mapping loaded module identifiers to their environments." + @{}) + (defn module/add-paths ``` Add paths to module/paths for a given loader such that @@ -2584,6 +2588,7 @@ (module/add-paths "/init.janet" :source) (module/add-paths ".janet" :source) (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 (defn fexists @@ -2638,10 +2643,6 @@ (undef check-.) (undef not-check-.) -(def module/cache - "Table mapping loaded module identifiers to their environments." - @{}) - (def module/loading `Table mapping currently loading modules to true. Used to prevent circular dependencies.` @@ -2706,6 +2707,11 @@ (def newenv (dofile path ;args)) (put module/loading path nil) 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)))}) (defn require-1