Skip to content

Commit f92f3eb

Browse files
committed
Address #1434 - add dynamic bindings for module state.
1 parent 89e74dc commit f92f3eb

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

src/boot/boot.janet

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,6 +2767,11 @@
27672767
(defn- check-is-dep [x] (unless (or (string/has-prefix? "/" x) (string/has-prefix? "@" x) (string/has-prefix? "." x)) x))
27682768
(defn- check-project-relative [x] (if (string/has-prefix? "/" x) x))
27692769

2770+
(defdyn *module/cache* "Dynamic binding for overriding `module/cache`")
2771+
(defdyn *module/paths* "Dynamic binding for overriding `module/cache`")
2772+
(defdyn *module/loading* "Dynamic binding for overriding `module/cache`")
2773+
(defdyn *module/loaders* "Dynamic binding for overriding `module/loaders`")
2774+
27702775
(def module/cache
27712776
"A table, mapping loaded module identifiers to their environments."
27722777
@{})
@@ -2795,24 +2800,25 @@
27952800
keyword name of a loader in `module/loaders`. Returns the modified `module/paths`.
27962801
```
27972802
[ext loader]
2803+
(def mp (dyn *module/paths* module/paths))
27982804
(defn- find-prefix
27992805
[pre]
2800-
(or (find-index |(and (string? ($ 0)) (string/has-prefix? pre ($ 0))) module/paths) 0))
2806+
(or (find-index |(and (string? ($ 0)) (string/has-prefix? pre ($ 0))) mp) 0))
28012807
(def dyn-index (find-prefix ":@all:"))
2802-
(array/insert module/paths dyn-index [(string ":@all:" ext) loader check-dyn-relative])
2808+
(array/insert mp dyn-index [(string ":@all:" ext) loader check-dyn-relative])
28032809
(def all-index (find-prefix ".:all:"))
2804-
(array/insert module/paths all-index [(string ".:all:" ext) loader check-project-relative])
2810+
(array/insert mp all-index [(string ".:all:" ext) loader check-project-relative])
28052811
(def sys-index (find-prefix ":sys:"))
2806-
(array/insert module/paths sys-index [(string ":sys:/:all:" ext) loader check-is-dep])
2812+
(array/insert mp sys-index [(string ":sys:/:all:" ext) loader check-is-dep])
28072813
(def curall-index (find-prefix ":cur:/:all:"))
2808-
(array/insert module/paths curall-index [(string ":cur:/:all:" ext) loader check-relative])
2809-
module/paths)
2814+
(array/insert mp curall-index [(string ":cur:/:all:" ext) loader check-relative])
2815+
mp)
28102816

28112817
(module/add-paths ":native:" :native)
28122818
(module/add-paths "/init.janet" :source)
28132819
(module/add-paths ".janet" :source)
28142820
(module/add-paths ".jimage" :image)
2815-
(array/insert module/paths 0 [(fn is-cached [path] (if (in module/cache path) path)) :preload check-not-relative])
2821+
(array/insert module/paths 0 [(fn is-cached [path] (if (in (dyn *module/cache* module/cache) path) path)) :preload check-not-relative])
28162822

28172823
# Version of fexists that works even with a reduced OS
28182824
(defn- fexists
@@ -2842,7 +2848,8 @@
28422848
```
28432849
[path]
28442850
(var ret nil)
2845-
(each [p mod-kind checker] module/paths
2851+
(def mp (dyn *module/paths* module/paths))
2852+
(each [p mod-kind checker] mp
28462853
(when (mod-filter checker path)
28472854
(if (function? p)
28482855
(when-let [res (p path)]
@@ -2858,7 +2865,7 @@
28582865
(when (string? t)
28592866
(when (mod-filter chk path)
28602867
(module/expand-path path t))))
2861-
paths (filter identity (map expander module/paths))
2868+
paths (filter identity (map expander mp))
28622869
str-parts (interpose "\n " paths)]
28632870
[nil (string "could not find module " path ":\n " ;str-parts)])))
28642871

@@ -3013,29 +3020,34 @@
30133020
of files as modules.``
30143021
@{:native (fn native-loader [path &] (native path (make-env)))
30153022
:source (fn source-loader [path args]
3016-
(put module/loading path true)
3017-
(defer (put module/loading path nil)
3023+
(def ml (dyn *module/loading* module/loading))
3024+
(put ml path true)
3025+
(defer (put ml path nil)
30183026
(dofile path ;args)))
30193027
:preload (fn preload-loader [path & args]
3020-
(when-let [m (in module/cache path)]
3028+
(def mc (dyn *module/cache* module/cache))
3029+
(when-let [m (in mc path)]
30213030
(if (function? m)
3022-
(set (module/cache path) (m path ;args))
3031+
(set (mc path) (m path ;args))
30233032
m)))
30243033
:image (fn image-loader [path &] (load-image (slurp path)))})
30253034

30263035
(defn- require-1
30273036
[path args kargs]
30283037
(def [fullpath mod-kind] (module/find path))
30293038
(unless fullpath (error mod-kind))
3030-
(if-let [check (if-not (kargs :fresh) (in module/cache fullpath))]
3039+
(def mc (dyn *module/cache* module/cache))
3040+
(def ml (dyn *module/loading* module/loading))
3041+
(def mls (dyn *module/loaders* module/loaders))
3042+
(if-let [check (if-not (kargs :fresh) (in mc fullpath))]
30313043
check
3032-
(if (module/loading fullpath)
3044+
(if (ml fullpath)
30333045
(error (string "circular dependency " fullpath " detected"))
30343046
(do
3035-
(def loader (if (keyword? mod-kind) (module/loaders mod-kind) mod-kind))
3047+
(def loader (if (keyword? mod-kind) (mls mod-kind) mod-kind))
30363048
(unless loader (error (string "module type " mod-kind " unknown")))
30373049
(def env (loader fullpath args))
3038-
(put module/cache fullpath env)
3050+
(put mc fullpath env)
30393051
env))))
30403052

30413053
(defn require

src/core/os.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,15 +1595,16 @@ JANET_CORE_FN(os_clock,
15951595
struct timespec tv;
15961596
if (janet_gettime(&tv, source)) janet_panic("could not get time");
15971597

1598-
JanetKeyword formatstr = janet_optkeyword(argv, argc, 1 , (const uint8_t *) "double");
1598+
JanetKeyword formatstr = janet_optkeyword(argv, argc, 1, (const uint8_t *) "double");
15991599
if (janet_cstrcmp(formatstr, "double") == 0) {
16001600
double dtime = tv.tv_sec + (tv.tv_nsec / 1E9);
16011601
return janet_wrap_number(dtime);
16021602
} else if (janet_cstrcmp(formatstr, "int") == 0) {
16031603
return janet_wrap_number(tv.tv_sec);
16041604
} else if (janet_cstrcmp(formatstr, "tuple") == 0) {
16051605
Janet tup[2] = {janet_wrap_integer(tv.tv_sec),
1606-
janet_wrap_integer(tv.tv_nsec)};
1606+
janet_wrap_integer(tv.tv_nsec)
1607+
};
16071608
return janet_wrap_tuple(janet_tuple_n(tup, 2));
16081609
} else {
16091610
janet_panicf("expected :double, :int, or :tuple, got %v", argv[1]);

0 commit comments

Comments
 (0)