|
2767 | 2767 | (defn- check-is-dep [x] (unless (or (string/has-prefix? "/" x) (string/has-prefix? "@" x) (string/has-prefix? "." x)) x))
|
2768 | 2768 | (defn- check-project-relative [x] (if (string/has-prefix? "/" x) x))
|
2769 | 2769 |
|
| 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 | + |
2770 | 2775 | (def module/cache
|
2771 | 2776 | "A table, mapping loaded module identifiers to their environments."
|
2772 | 2777 | @{})
|
|
2795 | 2800 | keyword name of a loader in `module/loaders`. Returns the modified `module/paths`.
|
2796 | 2801 | ```
|
2797 | 2802 | [ext loader]
|
| 2803 | + (def mp (dyn *module/paths* module/paths)) |
2798 | 2804 | (defn- find-prefix
|
2799 | 2805 | [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)) |
2801 | 2807 | (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]) |
2803 | 2809 | (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]) |
2805 | 2811 | (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]) |
2807 | 2813 | (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) |
2810 | 2816 |
|
2811 | 2817 | (module/add-paths ":native:" :native)
|
2812 | 2818 | (module/add-paths "/init.janet" :source)
|
2813 | 2819 | (module/add-paths ".janet" :source)
|
2814 | 2820 | (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]) |
2816 | 2822 |
|
2817 | 2823 | # Version of fexists that works even with a reduced OS
|
2818 | 2824 | (defn- fexists
|
|
2842 | 2848 | ```
|
2843 | 2849 | [path]
|
2844 | 2850 | (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 |
2846 | 2853 | (when (mod-filter checker path)
|
2847 | 2854 | (if (function? p)
|
2848 | 2855 | (when-let [res (p path)]
|
|
2858 | 2865 | (when (string? t)
|
2859 | 2866 | (when (mod-filter chk path)
|
2860 | 2867 | (module/expand-path path t))))
|
2861 |
| - paths (filter identity (map expander module/paths)) |
| 2868 | + paths (filter identity (map expander mp)) |
2862 | 2869 | str-parts (interpose "\n " paths)]
|
2863 | 2870 | [nil (string "could not find module " path ":\n " ;str-parts)])))
|
2864 | 2871 |
|
|
3013 | 3020 | of files as modules.``
|
3014 | 3021 | @{:native (fn native-loader [path &] (native path (make-env)))
|
3015 | 3022 | :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) |
3018 | 3026 | (dofile path ;args)))
|
3019 | 3027 | :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)] |
3021 | 3030 | (if (function? m)
|
3022 |
| - (set (module/cache path) (m path ;args)) |
| 3031 | + (set (mc path) (m path ;args)) |
3023 | 3032 | m)))
|
3024 | 3033 | :image (fn image-loader [path &] (load-image (slurp path)))})
|
3025 | 3034 |
|
3026 | 3035 | (defn- require-1
|
3027 | 3036 | [path args kargs]
|
3028 | 3037 | (def [fullpath mod-kind] (module/find path))
|
3029 | 3038 | (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))] |
3031 | 3043 | check
|
3032 |
| - (if (module/loading fullpath) |
| 3044 | + (if (ml fullpath) |
3033 | 3045 | (error (string "circular dependency " fullpath " detected"))
|
3034 | 3046 | (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)) |
3036 | 3048 | (unless loader (error (string "module type " mod-kind " unknown")))
|
3037 | 3049 | (def env (loader fullpath args))
|
3038 |
| - (put module/cache fullpath env) |
| 3050 | + (put mc fullpath env) |
3039 | 3051 | env))))
|
3040 | 3052 |
|
3041 | 3053 | (defn require
|
|
0 commit comments