mirror of
https://github.com/janet-lang/janet
synced 2024-11-28 19:19:53 +00:00
Strip trailing whitespace from many files.
Add native modules to import with module.native-path.
This commit is contained in:
parent
245fb948f1
commit
fb409201b4
@ -950,7 +950,7 @@ onvalue."
|
||||
:pc pc
|
||||
:c c
|
||||
:name name
|
||||
} (get st i))
|
||||
} (get st i))
|
||||
(file.write stdout " in")
|
||||
(when c (file.write stdout " cfunction"))
|
||||
(when name (file.write stdout (string " " name)))
|
||||
@ -978,48 +978,73 @@ environment is needed, use run-context."
|
||||
"./?/init.dst"
|
||||
"./dst_modules/?.dst"
|
||||
"./dst_modules/?/init.dst"
|
||||
"/usr/local/dst/0.0.0/?.dst"
|
||||
"/usr/local/dst/0.0.0/?/init.dst"
|
||||
])
|
||||
|
||||
(defn module.find
|
||||
"Open a file given a module name."
|
||||
[name]
|
||||
(def normname (string.replace-all "." "/" name))
|
||||
(defn checkmodule
|
||||
[f testpath]
|
||||
(if f f (do
|
||||
(def p (string.replace-all "?" normname testpath))
|
||||
(def p (string.replace-all "?" normname testpath))
|
||||
(file.open p))))
|
||||
(reduce checkmodule nil module.paths))
|
||||
(def module.native-paths @[
|
||||
"./?.so"
|
||||
"./dst_modules/?.so"
|
||||
"/usr/local/dst/0.0.0/?.so"
|
||||
])
|
||||
|
||||
(def 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
|
||||
returned from compiling and running the file."
|
||||
(do
|
||||
(def cache @{})
|
||||
(def loading @{})
|
||||
(fn [path]
|
||||
|
||||
(defn transform
|
||||
[path paths]
|
||||
(def normname (string.replace-all "." "/" path))
|
||||
(array.push
|
||||
(map (fn [x] (string.replace "?" normname x)) paths)
|
||||
path))
|
||||
|
||||
(defn check-mod
|
||||
[f testpath]
|
||||
(if f f (file.open testpath)))
|
||||
|
||||
(defn find-mod [path]
|
||||
(def paths (transform path module.paths))
|
||||
(reduce check-mod nil paths))
|
||||
|
||||
(defn check-native
|
||||
[p testpath]
|
||||
(if p p (do
|
||||
(def f (file.open testpath))
|
||||
(if f (do (file.close f) testpath)))))
|
||||
|
||||
(defn find-native [path]
|
||||
(def paths (transform path module.native-paths))
|
||||
(reduce check-native nil paths))
|
||||
|
||||
(def cache @{})
|
||||
(def loading @{})
|
||||
(fn [path]
|
||||
(when (get loading path)
|
||||
(error (string "circular dependency: module " path " is loading")))
|
||||
(def check (get cache path))
|
||||
(if check check (do
|
||||
(if (or
|
||||
(= ".so" (string.slice path -3 -1))
|
||||
(= ".dll" (string.slice path -4 -1)))
|
||||
((native path))
|
||||
(do
|
||||
(def newenv (make-env))
|
||||
(put cache path newenv)
|
||||
(put loading path true)
|
||||
(def f (or (module.find path) (file.open path)))
|
||||
(if (not f)
|
||||
(error (string "could not open file for module " path)))
|
||||
(defn chunks [buf] (file.read f 1024 buf))
|
||||
(run-context newenv chunks identity default-error-handler)
|
||||
(file.close f)
|
||||
(put loading path nil)
|
||||
newenv)))))))
|
||||
(def newenv (make-env))
|
||||
(put cache path newenv)
|
||||
(put loading path true)
|
||||
|
||||
(def f (find-mod path))
|
||||
(if f
|
||||
(do
|
||||
# Normal dst module
|
||||
(defn chunks [buf] (file.read f 1024 buf))
|
||||
(run-context newenv chunks identity default-error-handler)
|
||||
(file.close f)
|
||||
(put loading path nil)
|
||||
newenv)
|
||||
(do
|
||||
# Try native module
|
||||
(def n (find-native path))
|
||||
(if (not n)
|
||||
(error (string "could not open file for module " path)))
|
||||
((native n)))))))))
|
||||
|
||||
(defn import* [env path & args]
|
||||
(def newenv (require path))
|
||||
|
@ -852,6 +852,39 @@ static int cfun_split(DstArgs args) {
|
||||
DST_RETURN_ARRAY(args, array);
|
||||
}
|
||||
|
||||
static int cfun_checkset(DstArgs args) {
|
||||
const uint8_t *set, *str;
|
||||
int32_t setlen, strlen, i;
|
||||
uint32_t bitset[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
DST_MINARITY(args, 2);
|
||||
DST_MAXARITY(args, 3);
|
||||
DST_ARG_BYTES(set, setlen, args, 0);
|
||||
DST_ARG_BYTES(str, strlen, args, 1);
|
||||
/* Populate set */
|
||||
for (i = 0; i < setlen; i++) {
|
||||
int index = set[i] >> 5;
|
||||
uint32_t mask = 1 << (set[i] & 7);
|
||||
bitset[index] |= mask;
|
||||
}
|
||||
if (args.n == 3) {
|
||||
int invert;
|
||||
DST_ARG_BOOLEAN(invert, args, 2);
|
||||
if (invert) {
|
||||
for (i = 0; i < 8; i++)
|
||||
bitset[i] = ~bitset[i];
|
||||
}
|
||||
}
|
||||
/* Check set */
|
||||
for (i = 0; i < strlen; i++) {
|
||||
int index = str[i] >> 5;
|
||||
uint32_t mask = 1 << (str[i] & 7);
|
||||
if (!(bitset[index] & mask)) {
|
||||
DST_RETURN_FALSE(args);
|
||||
}
|
||||
}
|
||||
DST_RETURN_TRUE(args);
|
||||
}
|
||||
|
||||
static const DstReg cfuns[] = {
|
||||
{"string.slice", cfun_slice},
|
||||
{"string.repeat", cfun_repeat},
|
||||
@ -865,6 +898,7 @@ static const DstReg cfuns[] = {
|
||||
{"string.replace", cfun_replace},
|
||||
{"string.replace-all", cfun_replaceall},
|
||||
{"string.split", cfun_split},
|
||||
{"string.check-set", cfun_checkset},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user