diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2021-09-06 10:05:33 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-06 10:05:33 -0400 |
commit | c132c8690bc92fa306b2082992f3b7108e5a4c9e (patch) | |
tree | df152f94815b8ace8fd99696d003eb8adef06d53 /runtime/ops/signal.rs | |
parent | b7c2902c9752e83dc467ce6a812dab4726f200d9 (diff) |
BREAKING(unstable): Remove Deno.Signals enum, Deno.signals.* (#11909)
Diffstat (limited to 'runtime/ops/signal.rs')
-rw-r--r-- | runtime/ops/signal.rs | 92 |
1 files changed, 90 insertions, 2 deletions
diff --git a/runtime/ops/signal.rs b/runtime/ops/signal.rs index 889021706..eea8161cd 100644 --- a/runtime/ops/signal.rs +++ b/runtime/ops/signal.rs @@ -1,4 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op_async_unref; use deno_core::op_sync; @@ -53,15 +54,102 @@ impl Resource for SignalStreamResource { } } +#[cfg(target_os = "linux")] +fn signal_str_to_int(s: &str) -> Option<libc::c_int> { + match s { + "SIGHUP" => Some(1), + "SIGINT" => Some(2), + "SIGQUIT" => Some(3), + "SIGILL" => Some(4), + "SIGTRAP" => Some(5), + "SIGABRT" => Some(6), + "SIGBUS" => Some(7), + "SIGFPE" => Some(8), + "SIGKILL" => Some(9), + "SIGUSR1" => Some(10), + "SIGSEGV" => Some(11), + "SIGUSR2" => Some(12), + "SIGPIPE" => Some(13), + "SIGALRM" => Some(14), + "SIGTERM" => Some(15), + "SIGSTKFLT" => Some(16), + "SIGCHLD" => Some(17), + "SIGCONT" => Some(18), + "SIGSTOP" => Some(19), + "SIGTSTP" => Some(20), + "SIGTTIN" => Some(21), + "SIGTTOU" => Some(22), + "SIGURG" => Some(23), + "SIGXCPU" => Some(24), + "SIGXFSZ" => Some(25), + "SIGVTALRM" => Some(26), + "SIGPROF" => Some(27), + "SIGWINCH" => Some(28), + "SIGIO" => Some(29), + "SIGPWR" => Some(30), + "SIGSYS" => Some(31), + _ => None, + } +} + +#[cfg(target_os = "macos")] +fn signal_str_to_int(s: &str) -> Option<libc::c_int> { + match s { + "SIGHUP" => Some(1), + "SIGINT" => Some(2), + "SIGQUIT" => Some(3), + "SIGILL" => Some(4), + "SIGTRAP" => Some(5), + "SIGABRT" => Some(6), + "SIGEMT" => Some(7), + "SIGFPE" => Some(8), + "SIGKILL" => Some(9), + "SIGBUS" => Some(10), + "SIGSEGV" => Some(11), + "SIGSYS" => Some(12), + "SIGPIPE" => Some(13), + "SIGALRM" => Some(14), + "SIGTERM" => Some(15), + "SIGURG" => Some(16), + "SIGSTOP" => Some(17), + "SIGTSTP" => Some(18), + "SIGCONT" => Some(19), + "SIGCHLD" => Some(20), + "SIGTTIN" => Some(21), + "SIGTTOU" => Some(22), + "SIGIO" => Some(23), + "SIGXCPU" => Some(24), + "SIGXFSZ" => Some(25), + "SIGVTALRM" => Some(26), + "SIGPROF" => Some(27), + "SIGWINCH" => Some(28), + "SIGINFO" => Some(29), + "SIGUSR1" => Some(30), + "SIGUSR2" => Some(31), + _ => None, + } +} + +#[cfg(target_os = "windows")] +fn signal_str_to_int(s: &str) -> Option<libc::c_int> { + unimplemented!() +} + +pub fn signal_str_to_int_unwrap(s: &str) -> Result<libc::c_int, AnyError> { + signal_str_to_int(s) + .ok_or_else(|| type_error(format!("Invalid signal : {}", s))) +} + #[cfg(unix)] fn op_signal_bind( state: &mut OpState, - signo: i32, + sig: String, _: (), ) -> Result<ResourceId, AnyError> { super::check_unstable(state, "Deno.signal"); + let signo = signal_str_to_int_unwrap(&sig)?; let resource = SignalStreamResource { - signal: AsyncRefCell::new(signal(SignalKind::from_raw(signo)).expect("")), + signal: AsyncRefCell::new(signal(SignalKind::from_raw(signo)).unwrap()), cancel: Default::default(), }; let rid = state.resource_table.add(resource); |