From 92ff1d3be43f725121764ecc67d1fd992e15d7ce Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 14 Jun 2024 16:58:57 -0500 Subject: [PATCH] Add `only` option to `merge-module` and `import`. This allows importing only selected bindings. For example, (import foo :only [bar baz]) (foo/bar) # works (foo/buzz) # doesn't work, even if the foo module has a buzz function. --- CHANGELOG.md | 1 + src/boot/boot.janet | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10987dda..6a7597ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Add `:only` argument to `import` to allow for easier control over imported bindings. - Add extra optional `env` argument to `eval` and `eval-string`. - Allow naming function literals with a keyword. This allows better stacktraces for macros without accidentally adding new bindings. diff --git a/src/boot/boot.janet b/src/boot/boot.janet index dd793013..9878e669 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -3040,9 +3040,10 @@ ``Merge a module source into the `target` environment with a `prefix`, as with the `import` macro. This lets users emulate the behavior of `import` with a custom module table. If `export` is truthy, then merged functions are not marked as private. Returns - the modified target environment.`` - [target source &opt prefix export] - (loop [[k v] :pairs source :when (symbol? k) :when (not (v :private))] + the modified target environment. If an array `only` is passed, only merge keys in `only`.`` + [target source &opt prefix export only] + (def only-set (if only (invert only))) + (loop [[k v] :pairs source :when (symbol? k) :when (not (v :private)) :when (or (not only) (in only-set k))] (def newv (table/setproto @{:private (not export)} v)) (put target (symbol prefix k) newv)) target) @@ -3055,13 +3056,14 @@ (def kargs (table ;args)) (def {:as as :prefix prefix - :export ep} kargs) + :export ep + :only only} kargs) (def newenv (require-1 path args kargs)) (def prefix (or (and as (string as "/")) prefix (string (last (string/split "/" path)) "/"))) - (merge-module env newenv prefix ep)) + (merge-module env newenv prefix ep only)) (defmacro import ``Import a module. First requires the module, and then merges its