1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 01:37:19 +00:00

Add 3 argument form to fiber/new

Allow passing in environment table at fiber creation since
it is a fairly common thing to do.
This commit is contained in:
Calvin Rose 2023-06-04 14:17:18 -05:00
parent 88a8e2c1df
commit 1efb0adb35
2 changed files with 11 additions and 9 deletions

View File

@ -2530,8 +2530,8 @@
(set good false) (set good false)
(def {:error err :line line :column column :fiber errf} res) (def {:error err :line line :column column :fiber errf} res)
(on-compile-error err errf where (or line l) (or column c)))))) (on-compile-error err errf where (or line l) (or column c))))))
guard)) guard
(fiber/setenv f env) env))
(while (fiber/can-resume? f) (while (fiber/can-resume? f)
(def res (resume f resumeval)) (def res (resume f resumeval))
(when good (set resumeval (onstatus f res))))) (when good (set resumeval (onstatus f res)))))
@ -3868,8 +3868,7 @@
(def guard (if (get env :debug) :ydt :y)) (def guard (if (get env :debug) :ydt :y))
(defn wrap-main [&] (defn wrap-main [&]
(main ;subargs)) (main ;subargs))
(def f (fiber/new wrap-main guard)) (def f (fiber/new wrap-main guard env))
(fiber/setenv f env)
(var res nil) (var res nil)
(while (fiber/can-resume? f) (while (fiber/can-resume? f)
(set res (resume f res)) (set res (resume f res))

View File

@ -478,10 +478,10 @@ JANET_CORE_FN(cfun_fiber_setenv,
} }
JANET_CORE_FN(cfun_fiber_new, JANET_CORE_FN(cfun_fiber_new,
"(fiber/new func &opt sigmask)", "(fiber/new func &opt sigmask env)",
"Create a new fiber with function body func. Can optionally " "Create a new fiber with function body func. Can optionally "
"take a set of signals to block from the current parent fiber " "take a set of signals `sigmask` to capture from child fibers, "
"when called. The mask is specified as a keyword where each character " "and an environment table `env`. The mask is specified as a keyword where each character "
"is used to indicate a signal to block. If the ev module is enabled, and " "is used to indicate a signal to block. If the ev module is enabled, and "
"this fiber is used as an argument to `ev/go`, these \"blocked\" signals " "this fiber is used as an argument to `ev/go`, these \"blocked\" signals "
"will result in messages being sent to the supervisor channel. " "will result in messages being sent to the supervisor channel. "
@ -503,7 +503,7 @@ JANET_CORE_FN(cfun_fiber_new,
"exclusive flags are present, the last flag takes precedence.\n\n" "exclusive flags are present, the last flag takes precedence.\n\n"
"* :i - inherit the environment from the current fiber\n" "* :i - inherit the environment from the current fiber\n"
"* :p - the environment table's prototype is the current environment table") { "* :p - the environment table's prototype is the current environment table") {
janet_arity(argc, 1, 2); janet_arity(argc, 1, 3);
JanetFunction *func = janet_getfunction(argv, 0); JanetFunction *func = janet_getfunction(argv, 0);
JanetFiber *fiber; JanetFiber *fiber;
if (func->def->min_arity > 1) { if (func->def->min_arity > 1) {
@ -511,7 +511,10 @@ JANET_CORE_FN(cfun_fiber_new,
} }
fiber = janet_fiber(func, 64, func->def->min_arity, NULL); fiber = janet_fiber(func, 64, func->def->min_arity, NULL);
janet_assert(fiber != NULL, "bad fiber arity check"); janet_assert(fiber != NULL, "bad fiber arity check");
if (argc == 2) { if (argc == 3 && !janet_checktype(argv[2], JANET_NIL)) {
fiber->env = janet_gettable(argv, 2);
}
if (argc >= 2) {
int32_t i; int32_t i;
JanetByteView view = janet_getbytes(argv, 1); JanetByteView view = janet_getbytes(argv, 1);
fiber->flags = JANET_FIBER_RESUME_NO_USEVAL | JANET_FIBER_RESUME_NO_SKIP; fiber->flags = JANET_FIBER_RESUME_NO_USEVAL | JANET_FIBER_RESUME_NO_SKIP;