1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Merge branch 'master' of github.com:janet-lang/janet

This commit is contained in:
Calvin Rose 2023-05-21 16:23:34 -05:00
commit 57b751b994
4 changed files with 129 additions and 20 deletions

View File

@ -624,12 +624,99 @@ JANET_CORE_FN(os_proc_wait,
#endif #endif
} }
struct keyword_signal {
const char *keyword;
int signal;
};
#ifndef JANET_WINDOWS
static const struct keyword_signal signal_keywords[] = {
#ifdef SIGKILL
{"kill", SIGKILL},
#endif
{"int", SIGINT},
{"abrt", SIGABRT},
{"fpe", SIGFPE},
{"ill", SIGILL},
{"segv", SIGSEGV},
#ifdef SIGTERM
{"term", SIGTERM},
#endif
#ifdef SIGARLM
{"alrm", SIGALRM},
#endif
#ifdef SIGHUP
{"hup", SIGHUP},
#endif
#ifdef SIGPIPE
{"pipe", SIGPIPE},
#endif
#ifdef SIGQUIT
{"quit", SIGQUIT},
#endif
#ifdef SIGUSR1
{"usr1", SIGUSR1},
#endif
#ifdef SIGUSR2
{"usr2", SIGUSR2},
#endif
#ifdef SIGCHLD
{"chld", SIGCHLD},
#endif
#ifdef SIGCONT
{"cont", SIGCONT},
#endif
#ifdef SIGSTOP
{"stop", SIGSTOP},
#endif
#ifdef SIGTSTP
{"tstp", SIGTSTP},
#endif
#ifdef SIGTTIN
{"ttin", SIGTTIN},
#endif
#ifdef SIGTTOU
{"ttou", SIGTTOU},
#endif
#ifdef SIGBUS
{"bus", SIGBUS},
#endif
#ifdef SIGPOLL
{"poll", SIGPOLL},
#endif
#ifdef SIGPROF
{"prof", SIGPROF},
#endif
#ifdef SIGSYS
{"sys", SIGSYS},
#endif
#ifdef SIGTRAP
{"trap", SIGTRAP},
#endif
#ifdef SIGURG
{"urg", SIGURG},
#endif
#ifdef SIGVTALRM
{"vtlarm", SIGVTALRM},
#endif
#ifdef SIGXCPU
{"xcpu", SIGXCPU},
#endif
#ifdef SIGXFSZ
{"xfsz", SIGXFSZ},
#endif
{NULL, 0},
};
#endif
JANET_CORE_FN(os_proc_kill, JANET_CORE_FN(os_proc_kill,
"(os/proc-kill proc &opt wait)", "(os/proc-kill proc &opt wait signal)",
"Kill a subprocess by sending SIGKILL to it on posix systems, or by closing the process " "Kill a subprocess by sending SIGKILL to it on posix systems, or by closing the process "
"handle on windows. If `wait` is truthy, will wait for the process to finish and " "handle on windows. If `wait` is truthy, will wait for the process to finish and "
"returns the exit code. Otherwise, returns `proc`.") { "returns the exit code. Otherwise, returns `proc`. If signal is specified send it instead."
janet_arity(argc, 1, 2); "Signal keywords are named after their C counterparts but in lowercase with the leading "
"`SIG` stripped. Signals are ignored on windows.") {
janet_arity(argc, 1, 3);
JanetProc *proc = janet_getabstract(argv, 0, &ProcAT); JanetProc *proc = janet_getabstract(argv, 0, &ProcAT);
if (proc->flags & JANET_PROC_WAITED) { if (proc->flags & JANET_PROC_WAITED) {
janet_panicf("cannot kill process that has already finished"); janet_panicf("cannot kill process that has already finished");
@ -643,7 +730,22 @@ JANET_CORE_FN(os_proc_kill,
CloseHandle(proc->pHandle); CloseHandle(proc->pHandle);
CloseHandle(proc->tHandle); CloseHandle(proc->tHandle);
#else #else
int status = kill(proc->pid, SIGKILL); int signal = -1;
if (argc == 3) {
JanetKeyword signal_kw = janet_getkeyword(argv, 2);
const struct keyword_signal *ptr = signal_keywords;
while (ptr->keyword) {
if (!janet_cstrcmp(signal_kw, ptr->keyword)) {
signal = ptr->signal;
break;
}
ptr++;
}
if (signal == -1) {
janet_panic("undefined signal");
}
}
int status = kill(proc->pid, signal == -1 ? SIGKILL : signal);
if (status) { if (status) {
janet_panic(strerror(errno)); janet_panic(strerror(errno));
} }

View File

@ -264,7 +264,7 @@ static const Janet *janetc_make_sourcemap(JanetCompiler *c) {
static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv) { static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv) {
if (argn != 2) { if (argn != 2) {
janetc_cerror(opts.compiler, "expected 2 arguments"); janetc_cerror(opts.compiler, "expected 2 arguments to set");
return janetc_cslot(janet_wrap_nil()); return janetc_cslot(janet_wrap_nil());
} }
JanetFopts subopts = janetc_fopts_default(opts.compiler); JanetFopts subopts = janetc_fopts_default(opts.compiler);
@ -335,11 +335,11 @@ static JanetTable *handleattr(JanetCompiler *c, int32_t argn, const Janet *argv)
return tab; return tab;
} }
static JanetSlot dohead(JanetCompiler *c, JanetFopts opts, Janet *head, int32_t argn, const Janet *argv) { static JanetSlot dohead(const char *kind, JanetCompiler *c, JanetFopts opts, Janet *head, int32_t argn, const Janet *argv) {
JanetFopts subopts = janetc_fopts_default(c); JanetFopts subopts = janetc_fopts_default(c);
JanetSlot ret; JanetSlot ret;
if (argn < 2) { if (argn < 2) {
janetc_cerror(c, "expected at least 2 arguments"); janetc_error(c, janet_formatc("expected at least 2 arguments to %s", kind));
return janetc_cslot(janet_wrap_nil()); return janetc_cslot(janet_wrap_nil());
} }
*head = argv[0]; *head = argv[0];
@ -404,7 +404,7 @@ static JanetSlot janetc_var(JanetFopts opts, int32_t argn, const Janet *argv) {
JanetCompiler *c = opts.compiler; JanetCompiler *c = opts.compiler;
Janet head; Janet head;
JanetTable *attr_table = handleattr(c, argn, argv); JanetTable *attr_table = handleattr(c, argn, argv);
JanetSlot ret = dohead(c, opts, &head, argn, argv); JanetSlot ret = dohead("var", c, opts, &head, argn, argv);
if (c->result.status == JANET_COMPILE_ERROR) if (c->result.status == JANET_COMPILE_ERROR)
return janetc_cslot(janet_wrap_nil()); return janetc_cslot(janet_wrap_nil());
destructure(c, argv[0], ret, varleaf, attr_table); destructure(c, argv[0], ret, varleaf, attr_table);
@ -454,7 +454,7 @@ static JanetSlot janetc_def(JanetFopts opts, int32_t argn, const Janet *argv) {
Janet head; Janet head;
opts.flags &= ~JANET_FOPTS_HINT; opts.flags &= ~JANET_FOPTS_HINT;
JanetTable *attr_table = handleattr(c, argn, argv); JanetTable *attr_table = handleattr(c, argn, argv);
JanetSlot ret = dohead(c, opts, &head, argn, argv); JanetSlot ret = dohead("def", c, opts, &head, argn, argv);
if (c->result.status == JANET_COMPILE_ERROR) if (c->result.status == JANET_COMPILE_ERROR)
return janetc_cslot(janet_wrap_nil()); return janetc_cslot(janet_wrap_nil());
destructure(c, argv[0], ret, defleaf, attr_table); destructure(c, argv[0], ret, defleaf, attr_table);
@ -708,7 +708,7 @@ static JanetSlot janetc_while(JanetFopts opts, int32_t argn, const Janet *argv)
uint8_t ifnjmp = JOP_JUMP_IF_NOT; uint8_t ifnjmp = JOP_JUMP_IF_NOT;
if (argn < 2) { if (argn < 2) {
janetc_cerror(c, "expected at least 2 arguments"); janetc_cerror(c, "expected at least 2 arguments to while");
return janetc_cslot(janet_wrap_nil()); return janetc_cslot(janet_wrap_nil());
} }

View File

@ -439,20 +439,21 @@ int janet_compare(Janet x, Janet y) {
return status - 2; return status - 2;
} }
static int32_t getter_checkint(Janet key, int32_t max) { static int32_t getter_checkint(JanetType type, Janet key, int32_t max) {
if (!janet_checkint(key)) goto bad; if (!janet_checkint(key)) goto bad;
int32_t ret = janet_unwrap_integer(key); int32_t ret = janet_unwrap_integer(key);
if (ret < 0) goto bad; if (ret < 0) goto bad;
if (ret >= max) goto bad; if (ret >= max) goto bad;
return ret; return ret;
bad: bad:
janet_panicf("expected integer key in range [0, %d), got %v", max, key); janet_panicf("expected integer key for %s in range [0, %d), got %v", janet_type_names[type], max, key);
} }
/* Gets a value and returns. Can panic. */ /* Gets a value and returns. Can panic. */
Janet janet_in(Janet ds, Janet key) { Janet janet_in(Janet ds, Janet key) {
Janet value; Janet value;
switch (janet_type(ds)) { JanetType type = janet_type(ds);
switch (type) {
default: default:
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds); janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
break; break;
@ -464,19 +465,19 @@ Janet janet_in(Janet ds, Janet key) {
break; break;
case JANET_ARRAY: { case JANET_ARRAY: {
JanetArray *array = janet_unwrap_array(ds); JanetArray *array = janet_unwrap_array(ds);
int32_t index = getter_checkint(key, array->count); int32_t index = getter_checkint(type, key, array->count);
value = array->data[index]; value = array->data[index];
break; break;
} }
case JANET_TUPLE: { case JANET_TUPLE: {
const Janet *tuple = janet_unwrap_tuple(ds); const Janet *tuple = janet_unwrap_tuple(ds);
int32_t len = janet_tuple_length(tuple); int32_t len = janet_tuple_length(tuple);
value = tuple[getter_checkint(key, len)]; value = tuple[getter_checkint(type, key, len)];
break; break;
} }
case JANET_BUFFER: { case JANET_BUFFER: {
JanetBuffer *buffer = janet_unwrap_buffer(ds); JanetBuffer *buffer = janet_unwrap_buffer(ds);
int32_t index = getter_checkint(key, buffer->count); int32_t index = getter_checkint(type, key, buffer->count);
value = janet_wrap_integer(buffer->data[index]); value = janet_wrap_integer(buffer->data[index]);
break; break;
} }
@ -484,7 +485,7 @@ Janet janet_in(Janet ds, Janet key) {
case JANET_SYMBOL: case JANET_SYMBOL:
case JANET_KEYWORD: { case JANET_KEYWORD: {
const uint8_t *str = janet_unwrap_string(ds); const uint8_t *str = janet_unwrap_string(ds);
int32_t index = getter_checkint(key, janet_string_length(str)); int32_t index = getter_checkint(type, key, janet_string_length(str));
value = janet_wrap_integer(str[index]); value = janet_wrap_integer(str[index]);
break; break;
} }
@ -752,13 +753,14 @@ void janet_putindex(Janet ds, int32_t index, Janet value) {
} }
void janet_put(Janet ds, Janet key, Janet value) { void janet_put(Janet ds, Janet key, Janet value) {
switch (janet_type(ds)) { JanetType type = janet_type(ds);
switch (type) {
default: default:
janet_panicf("expected %T, got %v", janet_panicf("expected %T, got %v",
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds); JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
case JANET_ARRAY: { case JANET_ARRAY: {
JanetArray *array = janet_unwrap_array(ds); JanetArray *array = janet_unwrap_array(ds);
int32_t index = getter_checkint(key, INT32_MAX - 1); int32_t index = getter_checkint(type, key, INT32_MAX - 1);
if (index >= array->count) { if (index >= array->count) {
janet_array_setcount(array, index + 1); janet_array_setcount(array, index + 1);
} }
@ -767,7 +769,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
} }
case JANET_BUFFER: { case JANET_BUFFER: {
JanetBuffer *buffer = janet_unwrap_buffer(ds); JanetBuffer *buffer = janet_unwrap_buffer(ds);
int32_t index = getter_checkint(key, INT32_MAX - 1); int32_t index = getter_checkint(type, key, INT32_MAX - 1);
if (!janet_checkint(value)) if (!janet_checkint(value))
janet_panicf("can only put integers in buffers, got %v", value); janet_panicf("can only put integers in buffers, got %v", value);
if (index >= buffer->count) { if (index >= buffer->count) {

View File

@ -52,6 +52,11 @@
(def retval (os/proc-wait p)) (def retval (os/proc-wait p))
(assert (not= retval 24) "Process was *not* terminated by parent")) (assert (not= retval 24) "Process was *not* terminated by parent"))
(let [p (os/spawn [janet "-e" `(do (ev/sleep 30) (os/exit 24)`] :p)]
(os/proc-kill p false :term)
(def retval (os/proc-wait p))
(assert (not= retval 24) "Process was *not* terminated by parent"))
# Parallel subprocesses # Parallel subprocesses
(defn calc-1 (defn calc-1