1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 17:57:17 +00:00

Add module/loaders for custom file types.

This will allow other languages/DSLs to very easily
integrate with Janet.
This commit is contained in:
Calvin Rose 2019-05-16 12:05:40 -04:00
parent dde5351d11
commit 7054e878fb

View File

@ -1682,6 +1682,18 @@
(file/close f) (file/close f)
(table/setproto newenv nil)) (table/setproto newenv nil))
(def module/loaders
"A table of loading method names to loading functions.
This table lets require and import load many different kinds
of files as module."
@{:native (fn [path &] (native path (make-env)))
:source (fn [path args]
(put module/loading path true)
(def newenv (dofile path ;args))
(put module/loading path nil)
newenv)
:image (fn [path &] (load-image (slurp path)))})
(defn require (defn require
"Require a module with the given name. Will search all of the paths in "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 module/paths, then the path as a raw file path. Returns the new environment
@ -1692,15 +1704,9 @@
(do (do
(def [fullpath mod-kind] (module/find path)) (def [fullpath mod-kind] (module/find path))
(unless fullpath (error mod-kind)) (unless fullpath (error mod-kind))
(def env (def loader (module/loaders mod-kind))
(case mod-kind (unless loader (error (string "module type " mod-kind " unknown")))
:source (do (def env (loader fullpath args))
(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) (put module/cache fullpath env)
(put module/cache path env) (put module/cache path env)
env))) env)))