1
0
mirror of https://github.com/janet-lang/janet synced 2025-02-26 04:40:01 +00:00

Merge branch 'master' into longstring-autoindent

This commit is contained in:
Calvin Rose 2020-11-27 10:40:10 -06:00
commit dadd6037bb
3 changed files with 17 additions and 5 deletions

View File

@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Argument to `(error)` combinator in PEGs is now optional. - Argument to `(error)` combinator in PEGs is now optional.
- Add `(line)` and `(column)` combinators to PEGs to capture source line and column. - Add `(line)` and `(column)` combinators to PEGs to capture source line and column.
This should make error reporting a bit easier. This should make error reporting a bit easier.
- Add `merge-module` to core.
- During installation and release, merge janetconf.h into janet.h for easier install. - During installation and release, merge janetconf.h into janet.h for easier install.
- Add `upscope` special form. - Add `upscope` special form.
- `os/execute` and `os/spawn` can take streams for redirecting IO. - `os/execute` and `os/spawn` can take streams for redirecting IO.

View File

@ -2153,7 +2153,7 @@
:parser parser :parser parser
:read read :read read
:expander expand} opts) :expander expand} opts)
(default env (fiber/getenv (fiber/current))) (default env (or (fiber/getenv (fiber/current)) @{}))
(default chunks (fn [buf p] (getline "" buf env))) (default chunks (fn [buf p] (getline "" buf env)))
(default onstatus debug/stacktrace) (default onstatus debug/stacktrace)
(default on-compile-error bad-compile) (default on-compile-error bad-compile)
@ -2530,11 +2530,22 @@
[path & args] [path & args]
(require-1 path args (struct ;args))) (require-1 path args (struct ;args)))
(defn merge-module
"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))]
(def newv (table/setproto @{:private (not export)} v))
(put target (symbol prefix k) newv))
target)
(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 (curenv))
(def kargs (table ;args)) (def kargs (table ;args))
(def {:as as (def {:as as
:prefix prefix :prefix prefix
@ -2544,9 +2555,7 @@
(and as (string as "/")) (and as (string as "/"))
prefix prefix
(string (last (string/split "/" path)) "/"))) (string (last (string/split "/" path)) "/")))
(loop [[k v] :pairs newenv :when (symbol? k) :when (not (v :private))] (merge-module env newenv prefix ep))
(def newv (table/setproto @{:private (not ep)} v))
(put env (symbol prefix k) newv)))
(undef require-1) (undef require-1)

View File

@ -1684,6 +1684,8 @@ static Janet cfun_ev_call(int32_t argc, Janet *argv) {
janet_arity(argc, 1, -1); janet_arity(argc, 1, -1);
JanetFunction *fn = janet_getfunction(argv, 0); JanetFunction *fn = janet_getfunction(argv, 0);
JanetFiber *fiber = janet_fiber(fn, 64, argc - 1, argv + 1); JanetFiber *fiber = janet_fiber(fn, 64, argc - 1, argv + 1);
fiber->env = janet_table(0);
fiber->env->proto = janet_current_fiber()->env;
if (NULL == fiber) janet_panicf("invalid arity to function %v", argv[0]); if (NULL == fiber) janet_panicf("invalid arity to function %v", argv[0]);
janet_schedule(fiber, janet_wrap_nil()); janet_schedule(fiber, janet_wrap_nil());
return janet_wrap_fiber(fiber); return janet_wrap_fiber(fiber);