mirror of
https://github.com/janet-lang/janet
synced 2025-04-29 06:03:21 +00:00
Fix native module issue.
This commit is contained in:
parent
7b28032f5c
commit
b535c91ee1
@ -1568,7 +1568,7 @@ value, one key will be ignored."
|
|||||||
path))
|
path))
|
||||||
|
|
||||||
(def require
|
(def require
|
||||||
"(require module)\n\n
|
"(require module & args)\n\n
|
||||||
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."
|
||||||
@ -1596,21 +1596,18 @@ value, one key will be ignored."
|
|||||||
|
|
||||||
(def cache @{})
|
(def cache @{})
|
||||||
(def loading @{})
|
(def loading @{})
|
||||||
(fn require [path args &]
|
(fn require [path & args]
|
||||||
(when loading.path
|
(when loading.path
|
||||||
(error (string "circular dependency: module " path " is loading")))
|
(error (string "circular dependency: module " path " is loading")))
|
||||||
(def {:exit exit-on-error} (or args {}))
|
(def {:exit exit-on-error} (table ;args))
|
||||||
(def check cache.path)
|
(if-let [check cache.path]
|
||||||
(if check
|
|
||||||
check
|
check
|
||||||
|
(if-let [f (find-mod path)]
|
||||||
(do
|
(do
|
||||||
|
# Normal janet module
|
||||||
(def newenv (make-env))
|
(def newenv (make-env))
|
||||||
(set cache.path newenv)
|
(set cache.path newenv)
|
||||||
(set loading.path true)
|
(set loading.path true)
|
||||||
(def f (find-mod path))
|
|
||||||
(if f
|
|
||||||
(do
|
|
||||||
# Normal janet module
|
|
||||||
(defn chunks [buf _] (file/read f 1024 buf))
|
(defn chunks [buf _] (file/read f 1024 buf))
|
||||||
(run-context newenv chunks
|
(run-context newenv chunks
|
||||||
(fn [sig x f source]
|
(fn [sig x f source]
|
||||||
@ -1618,34 +1615,28 @@ value, one key will be ignored."
|
|||||||
(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)
|
path)
|
||||||
(file/close f))
|
(file/close f)
|
||||||
|
(set loading.path false)
|
||||||
|
newenv)
|
||||||
(do
|
(do
|
||||||
# Try native module
|
# Try native module
|
||||||
(def n (find-native path))
|
(def n (find-native path))
|
||||||
(if (not n)
|
(if (not n)
|
||||||
(error (string "could not open file for module " path)))
|
(error (string "could not open file for module " path)))
|
||||||
((native n) newenv)))
|
(native n)))))))
|
||||||
(set loading.path false)
|
|
||||||
newenv)))))
|
|
||||||
|
|
||||||
(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
|
||||||
functional form of (import ...) that expects and explicit environment
|
functional form of (import ...) that expects and explicit environment
|
||||||
table."
|
table."
|
||||||
[env path & args]
|
[env path & args]
|
||||||
(def targs (table ;args))
|
|
||||||
(def {:as as
|
(def {:as as
|
||||||
:prefix prefix} targs)
|
:prefix prefix} (table ;args))
|
||||||
(def newenv (require path targs))
|
(def newenv (require path ;args))
|
||||||
(var k (next newenv nil))
|
|
||||||
(def {:meta meta} newenv)
|
|
||||||
(def prefix (or (and as (string as "/")) prefix (string path "/")))
|
(def prefix (or (and as (string as "/")) prefix (string path "/")))
|
||||||
(while k
|
(loop [[k v] :pairs newenv :when (not v:private)]
|
||||||
(def v newenv.k)
|
|
||||||
(when (not v:private)
|
|
||||||
(def newv (table/setproto @{:private true} v))
|
(def newv (table/setproto @{:private true} v))
|
||||||
(put env (symbol prefix k) newv))
|
(put env (symbol prefix k) newv)))
|
||||||
(set k (next newenv k))))
|
|
||||||
|
|
||||||
(defmacro import
|
(defmacro import
|
||||||
"Import a module. First requires the module, and then merges its
|
"Import a module. First requires the module, and then merges its
|
||||||
|
Loading…
x
Reference in New Issue
Block a user