From 7054e878fb19aa7aa2468a5410a44310b1535806 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 16 May 2019 12:05:40 -0400 Subject: [PATCH] Add module/loaders for custom file types. This will allow other languages/DSLs to very easily integrate with Janet. --- src/boot/boot.janet | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 08859894..c937052c 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -1682,6 +1682,18 @@ (file/close f) (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 "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 @@ -1692,15 +1704,9 @@ (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)) - :image (load-image (slurp fullpath)))) + (def loader (module/loaders mod-kind)) + (unless loader (error (string "module type " mod-kind " unknown"))) + (def env (loader fullpath args)) (put module/cache fullpath env) (put module/cache path env) env)))