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

Allow passing user signals to (signal) as keywords.

This commit is contained in:
Calvin Rose 2022-09-17 21:18:07 -05:00
parent bb8405a36e
commit 6a899968a9
3 changed files with 22 additions and 13 deletions

View File

@ -124,7 +124,8 @@ void void_fn(void) {
printf("void fn ran\n"); printf("void fn ran\n");
} }
EXPORTER void_fn_2(double y) { EXPORTER
void void_fn_2(double y) {
printf("y = %f\n", y); printf("y = %f\n", y);
} }
@ -132,3 +133,13 @@ EXPORTER
void void_ret_fn(int x) { void void_ret_fn(int x) {
printf("void fn ran: %d\n", x); printf("void fn ran: %d\n", x);
} }
typedef struct {
int a, b;
float c, d;
} Split;
EXPORTER
float split_fn(Split s) {
return s.a * s.c + s.b * s.d;
}

View File

@ -45,6 +45,8 @@
j :double]) j :double])
(ffi/defbind void-fn-2 :void [y :double]) (ffi/defbind void-fn-2 :void [y :double])
(def split (ffi/struct :int :int :float :float))
(ffi/defbind split-fn :float [s split])
# #
# Struct reading and writing # Struct reading and writing
# #
@ -85,6 +87,7 @@
# Call functions # Call functions
# #
(tracev (split-fn [5 6 1.2 3.4]))
(tracev (void-fn-2 10.3)) (tracev (void-fn-2 10.3))
(tracev (double-many 1 2 3 4 5 6)) (tracev (double-many 1 2 3 4 5 6))
(tracev (string/format "%.17g" (double-many 1 2 3 4 5 6))) (tracev (string/format "%.17g" (double-many 1 2 3 4 5 6)))

View File

@ -614,27 +614,22 @@ JANET_CORE_FN(janet_core_signal,
"(signal what x)", "(signal what x)",
"Raise a signal with payload x. ") { "Raise a signal with payload x. ") {
janet_arity(argc, 1, 2); janet_arity(argc, 1, 2);
int sig; Janet payload = argc == 2 ? argv[1] : janet_wrap_nil();
if (janet_checkint(argv[0])) { if (janet_checkint(argv[0])) {
int32_t s = janet_unwrap_integer(argv[0]); int32_t s = janet_unwrap_integer(argv[0]);
if (s < 0 || s > 9) { if (s < 0 || s > 9) {
janet_panicf("expected user signal between 0 and 9, got %d", s); janet_panicf("expected user signal between 0 and 9, got %d", s);
} }
sig = JANET_SIGNAL_USER0 + s; janet_signalv(JANET_SIGNAL_USER0 + s, payload);
} else { } else {
JanetKeyword kw = janet_getkeyword(argv, 0); JanetKeyword kw = janet_getkeyword(argv, 0);
if (!janet_cstrcmp(kw, "yield")) { for (unsigned i = 0; i < sizeof(janet_signal_names) / sizeof(char *); i++) {
sig = JANET_SIGNAL_YIELD; if (!janet_cstrcmp(kw, janet_signal_names[i])) {
} else if (!janet_cstrcmp(kw, "error")) { janet_signalv((JanetSignal) i, payload);
sig = JANET_SIGNAL_ERROR;
} else if (!janet_cstrcmp(kw, "debug")) {
sig = JANET_SIGNAL_DEBUG;
} else {
janet_panicf("unknown signal, expected :yield, :error, or :debug, got %v", argv[0]);
} }
} }
Janet payload = argc == 2 ? argv[1] : janet_wrap_nil(); }
janet_signalv(sig, payload); janet_panicf("unknown signal %v", argv[0]);
} }
#ifdef JANET_BOOTSTRAP #ifdef JANET_BOOTSTRAP