1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-26 07:03:16 +00:00

Allow filters on templates in module/paths

This lets us make loaders depend on file suffixes, which
lets us more efficiently use full paths.
This commit is contained in:
Calvin Rose 2019-05-25 16:13:02 -04:00
parent 4d5a95784a
commit 0280deccae

View File

@ -1585,8 +1585,13 @@
:native: becomes the dynamic library file extension, usually dll :native: becomes the dynamic library file extension, usually dll
or so. Each element is a two element tuple, containing the path or so. Each element is a two element tuple, containing the path
template and a keyword :source, :native, or :image indicating how template and a keyword :source, :native, or :image indicating how
require should load files found at these paths." require should load files found at these paths.\n\nA tuple can also
@[[":all:" :source] contain a third element, specifying a filter that prevents module/find
from searching that path template if the filter doesn't match the input
path."
@[[":all:" :source ".janet"]
[":all:" :native (if (= (os/which) :windows) ".dll" ".so")]
[":all:" :image ".jimage"]
["./:all:.janet" :source] ["./:all:.janet" :source]
["./:all:/init.janet" :source] ["./:all:/init.janet" :source]
[":sys:/:all:.janet" :source] [":sys:/:all:.janet" :source]
@ -1622,6 +1627,21 @@
(file/close f) (file/close f)
res)))) res))))
(def nati (if (= :windows (os/which)) "dll" "so"))
(defn- expand-path-name
[template name path]
(->> template
(string/replace ":name:" name)
(string/replace ":sys:" module/*syspath*)
(string/replace ":native:" nati)
(string/replace ":all:" path)))
(defn- mod-filter
[x path]
(case (type x)
:nil true
:string (string/has-suffix? x path)
(x path)))
(defn module/find (defn module/find
"Try to match a module or path name from the patterns in module/paths. "Try to match a module or path name from the patterns in module/paths.
Returns a tuple (fullpath kind) where the kind is one of :source, :native, Returns a tuple (fullpath kind) where the kind is one of :source, :native,
@ -1629,25 +1649,26 @@
an error message." an error message."
[path] [path]
(def parts (string/split "/" path)) (def parts (string/split "/" path))
(def name (get parts (- (length parts) 1))) (def name (last parts))
(def nati (if (= :windows (os/which)) "dll" "so")) (var ret nil)
(defn make-full (each [p mod-kind checker] module/paths
[[p mod-kind]] (when (mod-filter checker path)
(def fullpath (->> p (def fullpath (expand-path-name p name path))
(string/replace ":name:" name) (when (fexists fullpath)
(string/replace ":sys:" module/*syspath*) (set ret [fullpath mod-kind])
(string/replace ":native:" nati) (break))))
(string/replace ":all:" path))) (if ret ret
[fullpath mod-kind]) (let [expander (fn [[t _ chk]]
(defn check-path [x] (if (fexists (x 0)) x)) (when (mod-filter chk path)
(def paths (map make-full module/paths)) (expand-path-name t name path)))
(def res (find check-path paths)) paths (filter identity (map expander module/paths))
(if res res [nil (string "could not find module " str-parts (interpose "\n " paths)]
path [nil (string "could not find module " path ":\n " ;str-parts)])))
":\n "
;(interpose "\n " (map 0 paths)))]))
(put _env 'fexists nil) (put _env 'fexists nil)
(put _env 'nati nil)
(put _env 'expand-path-name nil)
(put _env 'mod-filter nil)
(def module/cache (def module/cache
"Table mapping loaded module identifiers to their environments." "Table mapping loaded module identifiers to their environments."