mirror of
https://github.com/janet-lang/janet
synced 2025-07-06 03:52:54 +00:00
Update require to use real path name rather than module name.
This commit is contained in:
parent
f284776490
commit
0d293cd3f5
@ -1546,14 +1546,16 @@ value, one key will be ignored."
|
|||||||
@["./:all:.janet"
|
@["./:all:.janet"
|
||||||
"./:all:/init.janet"
|
"./:all:/init.janet"
|
||||||
":sys:/:all:.janet"
|
":sys:/:all:.janet"
|
||||||
":sys:/:all:/init.janet"])
|
":sys:/:all:/init.janet"
|
||||||
|
":all:"])
|
||||||
|
|
||||||
(def module/native-paths
|
(def module/native-paths
|
||||||
"See doc for module/paths"
|
"See doc for module/paths"
|
||||||
@["./:all:.:native:"
|
@["./:all:.:native:"
|
||||||
"./:all:/:name:.:native:"
|
"./:all:/:name:.:native:"
|
||||||
":sys:/:all:.:native:"
|
":sys:/:all:.:native:"
|
||||||
":sys:/:all:/:name:.:native:"])
|
":sys:/:all:/:name:.:native:"
|
||||||
|
":all:"])
|
||||||
|
|
||||||
(var module/*syspath*
|
(var module/*syspath*
|
||||||
"The path where globally installed libraries are located.
|
"The path where globally installed libraries are located.
|
||||||
@ -1576,7 +1578,7 @@ value, one key will be ignored."
|
|||||||
(string/replace ":sys:" module/*syspath*)
|
(string/replace ":sys:" module/*syspath*)
|
||||||
(string/replace ":native:" nati)
|
(string/replace ":native:" nati)
|
||||||
(string/replace ":all:" path)))
|
(string/replace ":all:" path)))
|
||||||
(array/push (map sub-path paths) path))
|
(map sub-path paths))
|
||||||
|
|
||||||
(def module/cache
|
(def module/cache
|
||||||
"Table mapping loaded module identifiers to their environments."
|
"Table mapping loaded module identifiers to their environments."
|
||||||
@ -1588,51 +1590,40 @@ value, one key will be ignored."
|
|||||||
@{})
|
@{})
|
||||||
|
|
||||||
# Require helpers
|
# Require helpers
|
||||||
(defn- check-mod
|
(defn- fexists [path]
|
||||||
[f testpath]
|
(def f (file/open path))
|
||||||
(or f (file/open testpath)))
|
(if f (do (file/close f) path)))
|
||||||
(defn- find-mod [path]
|
|
||||||
(def paths (module/find path module/paths))
|
|
||||||
(reduce check-mod nil paths))
|
|
||||||
(defn- check-native
|
|
||||||
[p testpath]
|
|
||||||
(or p
|
|
||||||
(do
|
|
||||||
(def f (file/open testpath))
|
|
||||||
(if f (do (file/close f) testpath)))))
|
|
||||||
(defn- find-native [path]
|
|
||||||
(def paths (module/find path module/native-paths))
|
|
||||||
(reduce check-native nil paths))
|
|
||||||
|
|
||||||
(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
|
||||||
returned from compiling and running the file."
|
returned from compiling and running the file."
|
||||||
[path & args]
|
[path & args]
|
||||||
(when (get module/loading path)
|
|
||||||
(error (string "circular dependency: module " path " is loading")))
|
|
||||||
(def {:exit exit-on-error} (table ;args))
|
(def {:exit exit-on-error} (table ;args))
|
||||||
(if-let [check (get module/cache path)]
|
(if-let [check (get module/cache path)]
|
||||||
check
|
check
|
||||||
(if-let [f (find-mod path)]
|
(if-let [modpath (find fexists (module/find path module/paths))]
|
||||||
(do
|
(do
|
||||||
|
(when (get module/loading modpath)
|
||||||
|
(error (string "circular dependency: file " modpath " is loading")))
|
||||||
# Normal janet module
|
# Normal janet module
|
||||||
|
(def f (file/open modpath))
|
||||||
(def newenv (make-env))
|
(def newenv (make-env))
|
||||||
(put module/loading path true)
|
(put module/loading modpath true)
|
||||||
(defn chunks [buf _] (file/read f 2048 buf))
|
(defn chunks [buf _] (file/read f 2048 buf))
|
||||||
(run-context newenv chunks
|
(run-context newenv chunks
|
||||||
(fn [sig x f source]
|
(fn [sig x f source]
|
||||||
(when (not= sig :dead)
|
(when (not= sig :dead)
|
||||||
(status-pp sig x f source)
|
(status-pp sig x f source)
|
||||||
(if exit-on-error (os/exit 1))))
|
(if exit-on-error (os/exit 1))))
|
||||||
path)
|
modpath)
|
||||||
(file/close f)
|
(file/close f)
|
||||||
(put module/loading path false)
|
(put module/loading modpath false)
|
||||||
(put module/cache path newenv)
|
(put module/cache modpath newenv)
|
||||||
newenv)
|
newenv)
|
||||||
(do
|
(do
|
||||||
# Try native module
|
# Try native module
|
||||||
(def n (find-native path))
|
(def n (find fexists (module/find path module/native-paths)))
|
||||||
(if (not n)
|
(if (not n)
|
||||||
(error (string "could not open file for module " path)))
|
(error (string "could not open file for module " path)))
|
||||||
(def e (make-env))
|
(def e (make-env))
|
||||||
@ -1640,10 +1631,7 @@ value, one key will be ignored."
|
|||||||
(put module/cache path e)
|
(put module/cache path e)
|
||||||
e))))
|
e))))
|
||||||
|
|
||||||
(put _env 'find-native nil)
|
(put _env 'fexists nil)
|
||||||
(put _env 'check-native nil)
|
|
||||||
(put _env 'find-mod nil)
|
|
||||||
(put _env 'check-mod nil)
|
|
||||||
|
|
||||||
(defn import*
|
(defn import*
|
||||||
"Import a module into a given environment table. This is the
|
"Import a module into a given environment table. This is the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user