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:
commit
57b751b994
110
src/core/os.c
110
src/core/os.c
@ -624,12 +624,99 @@ JANET_CORE_FN(os_proc_wait,
|
||||
#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,
|
||||
"(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 "
|
||||
"handle on windows. If `wait` is truthy, will wait for the process to finish and "
|
||||
"returns the exit code. Otherwise, returns `proc`.") {
|
||||
janet_arity(argc, 1, 2);
|
||||
"returns the exit code. Otherwise, returns `proc`. If signal is specified send it instead."
|
||||
"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);
|
||||
if (proc->flags & JANET_PROC_WAITED) {
|
||||
janet_panicf("cannot kill process that has already finished");
|
||||
@ -643,7 +730,22 @@ JANET_CORE_FN(os_proc_kill,
|
||||
CloseHandle(proc->pHandle);
|
||||
CloseHandle(proc->tHandle);
|
||||
#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) {
|
||||
janet_panic(strerror(errno));
|
||||
}
|
||||
|
@ -264,7 +264,7 @@ static const Janet *janetc_make_sourcemap(JanetCompiler *c) {
|
||||
|
||||
static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
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());
|
||||
}
|
||||
JanetFopts subopts = janetc_fopts_default(opts.compiler);
|
||||
@ -335,11 +335,11 @@ static JanetTable *handleattr(JanetCompiler *c, int32_t argn, const Janet *argv)
|
||||
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);
|
||||
JanetSlot ret;
|
||||
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());
|
||||
}
|
||||
*head = argv[0];
|
||||
@ -404,7 +404,7 @@ static JanetSlot janetc_var(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
JanetCompiler *c = opts.compiler;
|
||||
Janet head;
|
||||
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)
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
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;
|
||||
opts.flags &= ~JANET_FOPTS_HINT;
|
||||
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)
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
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;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
@ -439,20 +439,21 @@ int janet_compare(Janet x, Janet y) {
|
||||
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;
|
||||
int32_t ret = janet_unwrap_integer(key);
|
||||
if (ret < 0) goto bad;
|
||||
if (ret >= max) goto bad;
|
||||
return ret;
|
||||
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. */
|
||||
Janet janet_in(Janet ds, Janet key) {
|
||||
Janet value;
|
||||
switch (janet_type(ds)) {
|
||||
JanetType type = janet_type(ds);
|
||||
switch (type) {
|
||||
default:
|
||||
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
|
||||
break;
|
||||
@ -464,19 +465,19 @@ Janet janet_in(Janet ds, Janet key) {
|
||||
break;
|
||||
case JANET_ARRAY: {
|
||||
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];
|
||||
break;
|
||||
}
|
||||
case JANET_TUPLE: {
|
||||
const Janet *tuple = janet_unwrap_tuple(ds);
|
||||
int32_t len = janet_tuple_length(tuple);
|
||||
value = tuple[getter_checkint(key, len)];
|
||||
value = tuple[getter_checkint(type, key, len)];
|
||||
break;
|
||||
}
|
||||
case JANET_BUFFER: {
|
||||
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]);
|
||||
break;
|
||||
}
|
||||
@ -484,7 +485,7 @@ Janet janet_in(Janet ds, Janet key) {
|
||||
case JANET_SYMBOL:
|
||||
case JANET_KEYWORD: {
|
||||
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]);
|
||||
break;
|
||||
}
|
||||
@ -752,13 +753,14 @@ void janet_putindex(Janet ds, int32_t index, Janet value) {
|
||||
}
|
||||
|
||||
void janet_put(Janet ds, Janet key, Janet value) {
|
||||
switch (janet_type(ds)) {
|
||||
JanetType type = janet_type(ds);
|
||||
switch (type) {
|
||||
default:
|
||||
janet_panicf("expected %T, got %v",
|
||||
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
|
||||
case JANET_ARRAY: {
|
||||
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) {
|
||||
janet_array_setcount(array, index + 1);
|
||||
}
|
||||
@ -767,7 +769,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
|
||||
}
|
||||
case JANET_BUFFER: {
|
||||
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))
|
||||
janet_panicf("can only put integers in buffers, got %v", value);
|
||||
if (index >= buffer->count) {
|
||||
|
@ -52,6 +52,11 @@
|
||||
(def retval (os/proc-wait p))
|
||||
(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
|
||||
|
||||
(defn calc-1
|
||||
|
Loading…
Reference in New Issue
Block a user