mirror of
https://github.com/janet-lang/janet
synced 2024-11-25 01:37:19 +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
@ -978,25 +978,47 @@ 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
|
||||
|
||||
(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]
|
||||
@ -1004,22 +1026,25 @@ returned from compiling and running the file."
|
||||
(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)))
|
||||
|
||||
(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)))))))
|
||||
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