1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-17 22:24:49 +00:00

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.
This commit is contained in:
Calvin Rose 2024-06-14 16:58:57 -05:00
parent 58441dc49f
commit 92ff1d3be4
2 changed files with 8 additions and 5 deletions

View File

@ -2,6 +2,7 @@
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 - ??? ## Unreleased - ???
- Add `:only` argument to `import` to allow for easier control over imported bindings.
- Add extra optional `env` argument to `eval` and `eval-string`. - Add extra optional `env` argument to `eval` and `eval-string`.
- Allow naming function literals with a keyword. This allows better stacktraces for macros without - Allow naming function literals with a keyword. This allows better stacktraces for macros without
accidentally adding new bindings. accidentally adding new bindings.

View File

@ -3040,9 +3040,10 @@
``Merge a module source into the `target` environment with a `prefix`, as with the `import` macro. ``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. 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 If `export` is truthy, then merged functions are not marked as private. Returns
the modified target environment.`` the modified target environment. If an array `only` is passed, only merge keys in `only`.``
[target source &opt prefix export] [target source &opt prefix export only]
(loop [[k v] :pairs source :when (symbol? k) :when (not (v :private))] (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)) (def newv (table/setproto @{:private (not export)} v))
(put target (symbol prefix k) newv)) (put target (symbol prefix k) newv))
target) target)
@ -3055,13 +3056,14 @@
(def kargs (table ;args)) (def kargs (table ;args))
(def {:as as (def {:as as
:prefix prefix :prefix prefix
:export ep} kargs) :export ep
:only only} kargs)
(def newenv (require-1 path args kargs)) (def newenv (require-1 path args kargs))
(def prefix (or (def prefix (or
(and as (string as "/")) (and as (string as "/"))
prefix prefix
(string (last (string/split "/" path)) "/"))) (string (last (string/split "/" path)) "/")))
(merge-module env newenv prefix ep)) (merge-module env newenv prefix ep only))
(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