Skip to content

Commit f2ac1c1

Browse files
authored
Merge pull request #735 from sogaiu/new-style-core-fn-decl-for-fiber
Update fiber.c with new style core function declarations.
2 parents eaf8f19 + 2955286 commit f2ac1c1

File tree

1 file changed

+78
-99
lines changed

1 file changed

+78
-99
lines changed

src/core/fiber.c

Lines changed: 78 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,21 @@ JanetFiber *janet_root_fiber(void) {
451451

452452
/* CFuns */
453453

454-
static Janet cfun_fiber_getenv(int32_t argc, Janet *argv) {
454+
JANET_CORE_FN(cfun_fiber_getenv,
455+
"(fiber/getenv fiber)",
456+
"Gets the environment for a fiber. Returns nil if no such table is "
457+
"set yet.") {
455458
janet_fixarity(argc, 1);
456459
JanetFiber *fiber = janet_getfiber(argv, 0);
457460
return fiber->env ?
458461
janet_wrap_table(fiber->env) :
459462
janet_wrap_nil();
460463
}
461464

462-
static Janet cfun_fiber_setenv(int32_t argc, Janet *argv) {
465+
JANET_CORE_FN(cfun_fiber_setenv,
466+
"(fiber/setenv fiber table)",
467+
"Sets the environment table for a fiber. Set to nil to remove the current "
468+
"environment.") {
463469
janet_fixarity(argc, 2);
464470
JanetFiber *fiber = janet_getfiber(argv, 0);
465471
if (janet_checktype(argv[1], JANET_NIL)) {
@@ -470,7 +476,30 @@ static Janet cfun_fiber_setenv(int32_t argc, Janet *argv) {
470476
return argv[0];
471477
}
472478

473-
static Janet cfun_fiber_new(int32_t argc, Janet *argv) {
479+
JANET_CORE_FN(cfun_fiber_new,
480+
"(fiber/new func &opt sigmask)",
481+
"Create a new fiber with function body func. Can optionally "
482+
"take a set of signals to block from the current parent fiber "
483+
"when called. The mask is specified as a keyword where each character "
484+
"is used to indicate a signal to block. If the ev module is enabled, and "
485+
"this fiber is used as an argument to `ev/go`, these \"blocked\" signals "
486+
"will result in messages being sent to the supervisor channel. "
487+
"The default sigmask is :y. "
488+
"For example,\n\n"
489+
" (fiber/new myfun :e123)\n\n"
490+
"blocks error signals and user signals 1, 2 and 3. The signals are "
491+
"as follows:\n\n"
492+
"* :a - block all signals\n"
493+
"* :d - block debug signals\n"
494+
"* :e - block error signals\n"
495+
"* :t - block termination signals: error + user[0-4]\n"
496+
"* :u - block user signals\n"
497+
"* :y - block yield signals\n"
498+
"* :0-9 - block a specific user signal\n\n"
499+
"The sigmask argument also can take environment flags. If any mutually "
500+
"exclusive flags are present, the last flag takes precedence.\n\n"
501+
"* :i - inherit the environment from the current fiber\n"
502+
"* :p - the environment table's prototype is the current environment table") {
474503
janet_arity(argc, 1, 2);
475504
JanetFunction *func = janet_getfunction(argv, 0);
476505
JanetFiber *fiber;
@@ -539,32 +568,53 @@ static Janet cfun_fiber_new(int32_t argc, Janet *argv) {
539568
return janet_wrap_fiber(fiber);
540569
}
541570

542-
static Janet cfun_fiber_status(int32_t argc, Janet *argv) {
571+
JANET_CORE_FN(cfun_fiber_status,
572+
"(fiber/status fib)",
573+
"Get the status of a fiber. The status will be one of:\n\n"
574+
"* :dead - the fiber has finished\n"
575+
"* :error - the fiber has errored out\n"
576+
"* :debug - the fiber is suspended in debug mode\n"
577+
"* :pending - the fiber has been yielded\n"
578+
"* :user(0-9) - the fiber is suspended by a user signal\n"
579+
"* :alive - the fiber is currently running and cannot be resumed\n"
580+
"* :new - the fiber has just been created and not yet run") {
543581
janet_fixarity(argc, 1);
544582
JanetFiber *fiber = janet_getfiber(argv, 0);
545583
uint32_t s = janet_fiber_status(fiber);
546584
return janet_ckeywordv(janet_status_names[s]);
547585
}
548586

549-
static Janet cfun_fiber_current(int32_t argc, Janet *argv) {
587+
JANET_CORE_FN(cfun_fiber_current,
588+
"(fiber/current)",
589+
"Returns the currently running fiber.") {
550590
(void) argv;
551591
janet_fixarity(argc, 0);
552592
return janet_wrap_fiber(janet_vm.fiber);
553593
}
554594

555-
static Janet cfun_fiber_root(int32_t argc, Janet *argv) {
595+
JANET_CORE_FN(cfun_fiber_root,
596+
"(fiber/root)",
597+
"Returns the current root fiber. The root fiber is the oldest ancestor "
598+
"that does not have a parent.") {
556599
(void) argv;
557600
janet_fixarity(argc, 0);
558601
return janet_wrap_fiber(janet_vm.root_fiber);
559602
}
560603

561-
static Janet cfun_fiber_maxstack(int32_t argc, Janet *argv) {
604+
JANET_CORE_FN(cfun_fiber_maxstack,
605+
"(fiber/maxstack fib)",
606+
"Gets the maximum stack size in janet values allowed for a fiber. While memory for "
607+
"the fiber's stack is not allocated up front, the fiber will not allocated more "
608+
"than this amount and will throw a stack-overflow error if more memory is needed. ") {
562609
janet_fixarity(argc, 1);
563610
JanetFiber *fiber = janet_getfiber(argv, 0);
564611
return janet_wrap_integer(fiber->maxstack);
565612
}
566613

567-
static Janet cfun_fiber_setmaxstack(int32_t argc, Janet *argv) {
614+
JANET_CORE_FN(cfun_fiber_setmaxstack,
615+
"(fiber/setmaxstack fib maxstack)",
616+
"Sets the maximum stack size in janet values for a fiber. By default, the "
617+
"maximum stack size is usually 8192.") {
568618
janet_fixarity(argc, 2);
569619
JanetFiber *fiber = janet_getfiber(argv, 0);
570620
int32_t maxs = janet_getinteger(argv, 1);
@@ -575,7 +625,9 @@ static Janet cfun_fiber_setmaxstack(int32_t argc, Janet *argv) {
575625
return argv[0];
576626
}
577627

578-
static Janet cfun_fiber_can_resume(int32_t argc, Janet *argv) {
628+
JANET_CORE_FN(cfun_fiber_can_resume,
629+
"(fiber/can-resume? fiber)",
630+
"Check if a fiber is finished and cannot be resumed.") {
579631
janet_fixarity(argc, 1);
580632
JanetFiber *fiber = janet_getfiber(argv, 0);
581633
JanetFiberStatus s = janet_fiber_status(fiber);
@@ -589,101 +641,28 @@ static Janet cfun_fiber_can_resume(int32_t argc, Janet *argv) {
589641
return janet_wrap_boolean(!isFinished);
590642
}
591643

592-
static Janet cfun_fiber_last_value(int32_t argc, Janet *argv) {
644+
JANET_CORE_FN(cfun_fiber_last_value,
645+
"(fiber/last-value",
646+
"Get the last value returned or signaled from the fiber.") {
593647
janet_fixarity(argc, 1);
594648
JanetFiber *fiber = janet_getfiber(argv, 0);
595649
return fiber->last_value;
596650
}
597651

598-
static const JanetReg fiber_cfuns[] = {
599-
{
600-
"fiber/new", cfun_fiber_new,
601-
JDOC("(fiber/new func &opt sigmask)\n\n"
602-
"Create a new fiber with function body func. Can optionally "
603-
"take a set of signals to block from the current parent fiber "
604-
"when called. The mask is specified as a keyword where each character "
605-
"is used to indicate a signal to block. If the ev module is enabled, and "
606-
"this fiber is used as an argument to `ev/go`, these \"blocked\" signals "
607-
"will result in messages being sent to the supervisor channel. "
608-
"The default sigmask is :y. "
609-
"For example,\n\n"
610-
" (fiber/new myfun :e123)\n\n"
611-
"blocks error signals and user signals 1, 2 and 3. The signals are "
612-
"as follows:\n\n"
613-
"* :a - block all signals\n"
614-
"* :d - block debug signals\n"
615-
"* :e - block error signals\n"
616-
"* :t - block termination signals: error + user[0-4]\n"
617-
"* :u - block user signals\n"
618-
"* :y - block yield signals\n"
619-
"* :0-9 - block a specific user signal\n\n"
620-
"The sigmask argument also can take environment flags. If any mutually "
621-
"exclusive flags are present, the last flag takes precedence.\n\n"
622-
"* :i - inherit the environment from the current fiber\n"
623-
"* :p - the environment table's prototype is the current environment table")
624-
},
625-
{
626-
"fiber/status", cfun_fiber_status,
627-
JDOC("(fiber/status fib)\n\n"
628-
"Get the status of a fiber. The status will be one of:\n\n"
629-
"* :dead - the fiber has finished\n"
630-
"* :error - the fiber has errored out\n"
631-
"* :debug - the fiber is suspended in debug mode\n"
632-
"* :pending - the fiber has been yielded\n"
633-
"* :user(0-9) - the fiber is suspended by a user signal\n"
634-
"* :alive - the fiber is currently running and cannot be resumed\n"
635-
"* :new - the fiber has just been created and not yet run")
636-
},
637-
{
638-
"fiber/root", cfun_fiber_root,
639-
JDOC("(fiber/root)\n\n"
640-
"Returns the current root fiber. The root fiber is the oldest ancestor "
641-
"that does not have a parent.")
642-
},
643-
{
644-
"fiber/current", cfun_fiber_current,
645-
JDOC("(fiber/current)\n\n"
646-
"Returns the currently running fiber.")
647-
},
648-
{
649-
"fiber/maxstack", cfun_fiber_maxstack,
650-
JDOC("(fiber/maxstack fib)\n\n"
651-
"Gets the maximum stack size in janet values allowed for a fiber. While memory for "
652-
"the fiber's stack is not allocated up front, the fiber will not allocated more "
653-
"than this amount and will throw a stack-overflow error if more memory is needed. ")
654-
},
655-
{
656-
"fiber/setmaxstack", cfun_fiber_setmaxstack,
657-
JDOC("(fiber/setmaxstack fib maxstack)\n\n"
658-
"Sets the maximum stack size in janet values for a fiber. By default, the "
659-
"maximum stack size is usually 8192.")
660-
},
661-
{
662-
"fiber/getenv", cfun_fiber_getenv,
663-
JDOC("(fiber/getenv fiber)\n\n"
664-
"Gets the environment for a fiber. Returns nil if no such table is "
665-
"set yet.")
666-
},
667-
{
668-
"fiber/setenv", cfun_fiber_setenv,
669-
JDOC("(fiber/setenv fiber table)\n\n"
670-
"Sets the environment table for a fiber. Set to nil to remove the current "
671-
"environment.")
672-
},
673-
{
674-
"fiber/can-resume?", cfun_fiber_can_resume,
675-
JDOC("(fiber/can-resume? fiber)\n\n"
676-
"Check if a fiber is finished and cannot be resumed.")
677-
},
678-
{
679-
"fiber/last-value", cfun_fiber_last_value,
680-
JDOC("(fiber/last-value\n\n"
681-
"Get the last value returned or signaled from the fiber.")
682-
},
683-
{NULL, NULL, NULL}
684-
};
685-
686652
/* Module entry point */
687653
void janet_lib_fiber(JanetTable *env) {
688-
janet_core_cfuns(env, NULL, fiber_cfuns);
654+
JanetRegExt fiber_cfuns[] = {
655+
JANET_CORE_REG("fiber/new", cfun_fiber_new),
656+
JANET_CORE_REG("fiber/status", cfun_fiber_status),
657+
JANET_CORE_REG("fiber/root", cfun_fiber_root),
658+
JANET_CORE_REG("fiber/current", cfun_fiber_current),
659+
JANET_CORE_REG("fiber/maxstack", cfun_fiber_maxstack),
660+
JANET_CORE_REG("fiber/setmaxstack", cfun_fiber_setmaxstack),
661+
JANET_CORE_REG("fiber/getenv", cfun_fiber_getenv),
662+
JANET_CORE_REG("fiber/setenv", cfun_fiber_setenv),
663+
JANET_CORE_REG("fiber/can-resume?", cfun_fiber_can_resume),
664+
JANET_CORE_REG("fiber/last-value", cfun_fiber_last_value),
665+
JANET_REG_END
666+
};
667+
janet_core_cfuns_ext(env, NULL, fiber_cfuns);
689668
}

0 commit comments

Comments
 (0)