Skip to content

Instantly share code, notes, and snippets.

@bakpakin
Last active December 7, 2020 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakpakin/b4a87d95055142fa84e952fd82507858 to your computer and use it in GitHub Desktop.
Save bakpakin/b4a87d95055142fa84e952fd82507858 to your computer and use it in GitHub Desktop.
Janet preloading
(import preloader)
# Files that a use may want to load
(def mod1 (require "mod1"))
(def mod2 (require "mod2"))
(defn main [& args]
(preloader/preload "@mod1" mod1)
(preloader/preload "@mod2" mod2)
(dofile "some-user-file.janet"))
(def hi "hi")
(def yo "yo")
(defn preload
"Add a module to the preload cache"
[name env]
(put module/cache name env))
(defn preload-getter
"Given a path name, return the module if it is in the cache."
[x]
(when (string/has-prefix? "@" x)
(if (in module/cache x) x)))
(defn preload-loader
"Just look up a module in the cache."
[path &]
(get module/cache path))
(put module/loaders :preload preload-loader)
(array/insert module/paths 0 [preload-getter :preload])
(import @mod1 :as mod1)
(import @mod2 :as mod2)
(mod1/hi)
(mod2/yo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment