From c4e3fa03fad0eb8f2d141838679386e48e4c4f0f Mon Sep 17 00:00:00 2001 From: sarna Date: Sat, 12 Jul 2025 14:38:22 +0200 Subject: [PATCH 1/3] Clarify :fresh usage in import --- src/boot/boot.janet | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 0e357eec..8f2c7041 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -3181,9 +3181,9 @@ 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, any errors encountered at the top level in the module will cause `(os/exit 1)` - to be called. Dynamic bindings will NOT be imported. Use :fresh to bypass the - module cache. Use `:only [foo bar baz]` to only import select bindings into the - current environment.`` + to be called. Dynamic bindings will NOT be imported. Use :fresh with a truthy + value to bypass the module cache. Use `:only [foo bar baz]` to only import + select bindings into the current environment.`` [path & args] (def ps (partition 2 args)) (def argm (mapcat (fn [[k v]] [k (case k :as (string v) :only ~(quote ,v) v)]) ps)) From 2af3f21d6965703a337ab694da834e9cc395b420 Mon Sep 17 00:00:00 2001 From: sarna Date: Sat, 12 Jul 2025 17:06:38 +0200 Subject: [PATCH 2/3] Validate optional args to import --- CHANGELOG.md | 1 + src/boot/boot.janet | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9005d294..c6e1847a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Raise helpful errors for incorrect arguments to `import`. - Allow configuring `JANET_THREAD_LOCAL` during builds to allow multi-threading on unknown compilers. - Make `ffi/write` append to a buffer instead of insert at 0 by default. - Add `os/getpid` to get the current process id. diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 8f2c7041..61f6fd63 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -3185,8 +3185,13 @@ value to bypass the module cache. Use `:only [foo bar baz]` to only import select bindings into the current environment.`` [path & args] + (assertf (even? (length args)) "args should have even length: %n" args) (def ps (partition 2 args)) - (def argm (mapcat (fn [[k v]] [k (case k :as (string v) :only ~(quote ,v) v)]) ps)) + (def argm + (mapcat (fn [[k v]] + (assertf (keyword? k) "expected keyword, got %s: %n" (type k) k) + [k (case k :as (string v) :only ~(quote ,v) v)]) + ps)) (tuple import* (string path) ;argm)) (defmacro use From 8c9cd63cb17b9523d33ca80bc34385af3b5bbcd2 Mon Sep 17 00:00:00 2001 From: sarna Date: Sun, 13 Jul 2025 13:22:55 +0200 Subject: [PATCH 3/3] Add tests for import arg validation --- test/suite-boot.janet | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/suite-boot.janet b/test/suite-boot.janet index 59c9f211..7532a972 100644 --- a/test/suite-boot.janet +++ b/test/suite-boot.janet @@ -865,6 +865,13 @@ (assert (deep= ~(,import* "a" :as "b" :fresh maybe) (macex '(import a :as b :fresh maybe))) "import macro 2") +# 2af3f21d +(assert-error "import macro 2" (macex '(import a :fresh))) +(assert-error "import macro 3" (macex '(import a :as b :fresh))) +(assert-error "import macro 4" (macex '(import b "notakeyword" value))) +(assert (deep= ~(,import* "a" :fresh nil) + (macex '(import a :fresh nil))) "import macro 5") + # #477 walk preserving bracket type # 0a1d902f4 (assert (= :brackets (tuple/type (postwalk identity '[])))