1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-01 04:19:55 +00:00

Strip trailing whitespace from many files.

Add native modules to import with module.native-path.
This commit is contained in:
Calvin Rose 2018-05-19 21:16:00 -04:00
parent 245fb948f1
commit fb409201b4
19 changed files with 173 additions and 114 deletions

View File

@ -950,7 +950,7 @@ onvalue."
:pc pc :pc pc
:c c :c c
:name name :name name
} (get st i)) } (get st i))
(file.write stdout " in") (file.write stdout " in")
(when c (file.write stdout " cfunction")) (when c (file.write stdout " cfunction"))
(when name (file.write stdout (string " " name))) (when name (file.write stdout (string " " name)))
@ -978,48 +978,73 @@ environment is needed, use run-context."
"./?/init.dst" "./?/init.dst"
"./dst_modules/?.dst" "./dst_modules/?.dst"
"./dst_modules/?/init.dst" "./dst_modules/?/init.dst"
"/usr/local/dst/0.0.0/?.dst"
"/usr/local/dst/0.0.0/?/init.dst"
]) ])
(defn module.find (def module.native-paths @[
"Open a file given a module name." "./?.so"
[name] "./dst_modules/?.so"
(def normname (string.replace-all "." "/" name)) "/usr/local/dst/0.0.0/?.so"
(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 require (def 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."
(do (do
(def cache @{})
(def loading @{}) (defn transform
(fn [path] [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) (when (get loading path)
(error (string "circular dependency: module " path " is loading"))) (error (string "circular dependency: module " path " is loading")))
(def check (get cache path)) (def check (get cache path))
(if check check (do (if check check (do
(if (or (def newenv (make-env))
(= ".so" (string.slice path -3 -1)) (put cache path newenv)
(= ".dll" (string.slice path -4 -1))) (put loading path true)
((native path))
(do (def f (find-mod path))
(def newenv (make-env)) (if f
(put cache path newenv) (do
(put loading path true) # Normal dst module
(def f (or (module.find path) (file.open path))) (defn chunks [buf] (file.read f 1024 buf))
(if (not f) (run-context newenv chunks identity default-error-handler)
(error (string "could not open file for module " path))) (file.close f)
(defn chunks [buf] (file.read f 1024 buf)) (put loading path nil)
(run-context newenv chunks identity default-error-handler) newenv)
(file.close f) (do
(put loading path nil) # Try native module
newenv))))))) (def n (find-native path))
(if (not n)
(error (string "could not open file for module " path)))
((native n)))))))))
(defn import* [env path & args] (defn import* [env path & args]
(def newenv (require path)) (def newenv (require path))

View File

@ -852,6 +852,39 @@ static int cfun_split(DstArgs args) {
DST_RETURN_ARRAY(args, array); 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[] = { static const DstReg cfuns[] = {
{"string.slice", cfun_slice}, {"string.slice", cfun_slice},
{"string.repeat", cfun_repeat}, {"string.repeat", cfun_repeat},
@ -865,6 +898,7 @@ static const DstReg cfuns[] = {
{"string.replace", cfun_replace}, {"string.replace", cfun_replace},
{"string.replace-all", cfun_replaceall}, {"string.replace-all", cfun_replaceall},
{"string.split", cfun_split}, {"string.split", cfun_split},
{"string.check-set", cfun_checkset},
{NULL, NULL} {NULL, NULL}
}; };