1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-26 07:03:16 +00:00

Add :fresh option to import.

This commit is contained in:
Calvin Rose 2020-05-15 17:19:37 -05:00
parent 4ae372262b
commit cf670153f9
4 changed files with 29 additions and 16 deletions

View File

@ -1,6 +1,10 @@
# Changelog # Changelog
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ???
- Add `:fresh` option to `(import ...)` to overwrite the module cache.
- `(range x y 0)` will return an empty array instead of hanging forever.
## 1.9.1 - 2020-05-12 ## 1.9.1 - 2020-05-12
- Add :prefix option to declare-source - Add :prefix option to declare-source
- Re-enable minimal builds with the debugger. - Re-enable minimal builds with the debugger.

View File

@ -20,7 +20,7 @@
project('janet', 'c', project('janet', 'c',
default_options : ['c_std=c99', 'b_lundef=false', 'default_library=both'], default_options : ['c_std=c99', 'b_lundef=false', 'default_library=both'],
version : '1.9.1') version : '1.10.0')
# Global settings # Global settings
janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet') janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet')

View File

@ -848,8 +848,9 @@
arr) arr)
3 (do 3 (do
(def [n m s] args) (def [n m s] args)
(if (neg? s) (cond
(seq [i :down [n m (- s)]] i) (zero? s) @[]
(neg? s) (seq [i :down [n m (- s)]] i)
(seq [i :range [n m s]] i))) (seq [i :range [n m s]] i)))
(error "expected 1 to 3 arguments to range"))) (error "expected 1 to 3 arguments to range")))
@ -2263,14 +2264,11 @@
newenv) newenv)
:image (fn [path &] (load-image (slurp path)))}) :image (fn [path &] (load-image (slurp path)))})
(defn require (defn require-1
"Require a module with the given name. Will search all of the paths in [path args kargs]
module/paths. Returns the new environment
returned from compiling and running the file."
[path & args]
(def [fullpath mod-kind] (module/find path)) (def [fullpath mod-kind] (module/find path))
(unless fullpath (error mod-kind)) (unless fullpath (error mod-kind))
(if-let [check (in module/cache fullpath)] (if-let [check (if-not (kargs :fresh) (in module/cache fullpath))]
check check
(if (module/loading fullpath) (if (module/loading fullpath)
(error (string "circular dependency " fullpath " detected")) (error (string "circular dependency " fullpath " detected"))
@ -2281,15 +2279,23 @@
(put module/cache fullpath env) (put module/cache fullpath env)
env)))) env))))
(defn require
"Require a module with the given name. Will search all of the paths in
module/paths. Returns the new environment
returned from compiling and running the file."
[path & args]
(require-1 path args (struct ;args)))
(defn import* (defn import*
"Function form of import. Same parameters, but the path "Function form of import. Same parameters, but the path
and other symbol parameters should be strings instead." and other symbol parameters should be strings instead."
[path & args] [path & args]
(def env (fiber/getenv (fiber/current))) (def env (fiber/getenv (fiber/current)))
(def kargs (table ;args))
(def {:as as (def {:as as
:prefix prefix :prefix prefix
:export ep} (table ;args)) :export ep} kargs)
(def newenv (require path ;args)) (def newenv (require-1 path args kargs))
(def prefix (or (def prefix (or
(and as (string as "/")) (and as (string as "/"))
prefix prefix
@ -2298,6 +2304,8 @@
(def newv (table/setproto @{:private (not ep)} v)) (def newv (table/setproto @{:private (not ep)} v))
(put env (symbol prefix k) newv))) (put env (symbol prefix k) newv)))
(put _env 'require-1 nil)
(defmacro import (defmacro import
"Import a module. First requires the module, and then merges its "Import a module. First requires the module, and then merges its
symbols into the current environment, prepending a given prefix as needed. symbols into the current environment, prepending a given prefix as needed.
@ -2305,7 +2313,8 @@
use the name of the module as a prefix. One can also use :export true use the name of the module as a prefix. One can also use :export true
to re-export the imported symbols. If :exit true is given as an argument, to re-export the imported symbols. If :exit true is given as an argument,
any errors encountered at the top level in the module will cause (os/exit 1) any errors encountered at the top level in the module will cause (os/exit 1)
to be called. Dynamic bindings will NOT be imported." to be called. Dynamic bindings will NOT be imported. Use :fresh to bypass the
module cache."
[path & args] [path & args]
(def argm (map |(if (keyword? $) $ (string $)) args)) (def argm (map |(if (keyword? $) $ (string $)) args))
(tuple import* (string path) ;argm)) (tuple import* (string path) ;argm))

View File

@ -27,10 +27,10 @@
#define JANETCONF_H #define JANETCONF_H
#define JANET_VERSION_MAJOR 1 #define JANET_VERSION_MAJOR 1
#define JANET_VERSION_MINOR 9 #define JANET_VERSION_MINOR 10
#define JANET_VERSION_PATCH 1 #define JANET_VERSION_PATCH 0
#define JANET_VERSION_EXTRA "" #define JANET_VERSION_EXTRA "-dev"
#define JANET_VERSION "1.9.1" #define JANET_VERSION "1.10.0"
/* #define JANET_BUILD "local" */ /* #define JANET_BUILD "local" */