1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00

refactor: use macros for signal table (#26214)

This commit is contained in:
Patrick Uftring 2024-10-17 13:35:57 +02:00 committed by GitHub
parent 5689585888
commit e61e2e4b5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -147,438 +147,226 @@ impl Resource for SignalStreamResource {
} }
} }
#[cfg(target_os = "freebsd")] macro_rules! first_literal {
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { ($head:literal $(, $tail:literal)*) => {
match s { $head
"SIGHUP" => Ok(1), };
"SIGINT" => Ok(2), }
"SIGQUIT" => Ok(3), macro_rules! signal_dict {
"SIGILL" => Ok(4), ($error_msg:expr, $(($number:literal, $($name:literal)|+)),*) => {
"SIGTRAP" => Ok(5), pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
"SIGIOT" => Ok(6), match s {
"SIGABRT" => Ok(6), $($($name)|* => Ok($number),)*
"SIGEMT" => Ok(7), _ => Err(type_error($error_msg(s))),
"SIGFPE" => Ok(8), }
"SIGKILL" => Ok(9), }
"SIGBUS" => Ok(10),
"SIGSEGV" => Ok(11), pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
"SIGSYS" => Ok(12), match s {
"SIGPIPE" => Ok(13), $($number => Ok(first_literal!($($name),+)),)*
"SIGALRM" => Ok(14), _ => Err(type_error($error_msg(s))),
"SIGTERM" => Ok(15), }
"SIGURG" => Ok(16), }
"SIGSTOP" => Ok(17),
"SIGTSTP" => Ok(18),
"SIGCONT" => Ok(19),
"SIGCHLD" => Ok(20),
"SIGTTIN" => Ok(21),
"SIGTTOU" => Ok(22),
"SIGIO" => Ok(23),
"SIGXCPU" => Ok(24),
"SIGXFSZ" => Ok(25),
"SIGVTALRM" => Ok(26),
"SIGPROF" => Ok(27),
"SIGWINCH" => Ok(28),
"SIGINFO" => Ok(29),
"SIGUSR1" => Ok(30),
"SIGUSR2" => Ok(31),
"SIGTHR" => Ok(32),
"SIGLIBRT" => Ok(33),
_ => Err(type_error(format!("Invalid signal : {}", s))),
} }
} }
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {}", s) },
1 => Ok("SIGHUP"), (1, "SIGHUP"),
2 => Ok("SIGINT"), (2, "SIGINT"),
3 => Ok("SIGQUIT"), (3, "SIGQUIT"),
4 => Ok("SIGILL"), (4, "SIGILL"),
5 => Ok("SIGTRAP"), (5, "SIGTRAP"),
6 => Ok("SIGABRT"), (6, "SIGABRT" | "SIGIOT"),
7 => Ok("SIGEMT"), (7, "SIGEMT"),
8 => Ok("SIGFPE"), (8, "SIGFPE"),
9 => Ok("SIGKILL"), (9, "SIGKILL"),
10 => Ok("SIGBUS"), (10, "SIGBUS"),
11 => Ok("SIGSEGV"), (11, "SIGSEGV"),
12 => Ok("SIGSYS"), (12, "SIGSYS"),
13 => Ok("SIGPIPE"), (13, "SIGPIPE"),
14 => Ok("SIGALRM"), (14, "SIGALRM"),
15 => Ok("SIGTERM"), (15, "SIGTERM"),
16 => Ok("SIGURG"), (16, "SIGURG"),
17 => Ok("SIGSTOP"), (17, "SIGSTOP"),
18 => Ok("SIGTSTP"), (18, "SIGTSTP"),
19 => Ok("SIGCONT"), (19, "SIGCONT"),
20 => Ok("SIGCHLD"), (20, "SIGCHLD"),
21 => Ok("SIGTTIN"), (21, "SIGTTIN"),
22 => Ok("SIGTTOU"), (22, "SIGTTOU"),
23 => Ok("SIGIO"), (23, "SIGIO"),
24 => Ok("SIGXCPU"), (24, "SIGXCPU"),
25 => Ok("SIGXFSZ"), (25, "SIGXFSZ"),
26 => Ok("SIGVTALRM"), (26, "SIGVTALRM"),
27 => Ok("SIGPROF"), (27, "SIGPROF"),
28 => Ok("SIGWINCH"), (28, "SIGWINCH"),
29 => Ok("SIGINFO"), (29, "SIGINFO"),
30 => Ok("SIGUSR1"), (30, "SIGUSR1"),
31 => Ok("SIGUSR2"), (31, "SIGUSR2"),
32 => Ok("SIGTHR"), (32, "SIGTHR"),
33 => Ok("SIGLIBRT"), (33, "SIGLIBRT")
_ => Err(type_error(format!("Invalid signal : {}", s))), );
}
}
#[cfg(target_os = "openbsd")] #[cfg(target_os = "openbsd")]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {}", s) },
"SIGHUP" => Ok(1), (1, "SIGHUP"),
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGQUIT" => Ok(3), (3, "SIGQUIT"),
"SIGILL" => Ok(4), (4, "SIGILL"),
"SIGTRAP" => Ok(5), (5, "SIGTRAP"),
"SIGIOT" => Ok(6), (6, "SIGABRT" | "SIGIOT"),
"SIGABRT" => Ok(6), (7, "SIGEMT"),
"SIGEMT" => Ok(7), (8, "SIGKILL"),
"SIGFPE" => Ok(8), (10, "SIGBUS"),
"SIGKILL" => Ok(9), (11, "SIGSEGV"),
"SIGBUS" => Ok(10), (12, "SIGSYS"),
"SIGSEGV" => Ok(11), (13, "SIGPIPE"),
"SIGSYS" => Ok(12), (14, "SIGALRM"),
"SIGPIPE" => Ok(13), (15, "SIGTERM"),
"SIGALRM" => Ok(14), (16, "SIGURG"),
"SIGTERM" => Ok(15), (17, "SIGSTOP"),
"SIGURG" => Ok(16), (18, "SIGTSTP"),
"SIGSTOP" => Ok(17), (19, "SIGCONT"),
"SIGTSTP" => Ok(18), (20, "SIGCHLD"),
"SIGCONT" => Ok(19), (21, "SIGTTIN"),
"SIGCHLD" => Ok(20), (22, "SIGTTOU"),
"SIGTTIN" => Ok(21), (23, "SIGIO"),
"SIGTTOU" => Ok(22), (24, "SIGXCPU"),
"SIGIO" => Ok(23), (25, "SIGXFSZ"),
"SIGXCPU" => Ok(24), (26, "SIGVTALRM"),
"SIGXFSZ" => Ok(25), (27, "SIGPROF"),
"SIGVTALRM" => Ok(26), (28, "SIGWINCH"),
"SIGPROF" => Ok(27), (29, "SIGINFO"),
"SIGWINCH" => Ok(28), (30, "SIGUSR1"),
"SIGINFO" => Ok(29), (31, "SIGUSR2"),
"SIGUSR1" => Ok(30), (32, "SIGTHR")
"SIGUSR2" => Ok(31), );
"SIGTHR" => Ok(32),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
}
#[cfg(target_os = "openbsd")]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
1 => Ok("SIGHUP"),
2 => Ok("SIGINT"),
3 => Ok("SIGQUIT"),
4 => Ok("SIGILL"),
5 => Ok("SIGTRAP"),
6 => Ok("SIGABRT"),
7 => Ok("SIGEMT"),
8 => Ok("SIGFPE"),
9 => Ok("SIGKILL"),
10 => Ok("SIGBUS"),
11 => Ok("SIGSEGV"),
12 => Ok("SIGSYS"),
13 => Ok("SIGPIPE"),
14 => Ok("SIGALRM"),
15 => Ok("SIGTERM"),
16 => Ok("SIGURG"),
17 => Ok("SIGSTOP"),
18 => Ok("SIGTSTP"),
19 => Ok("SIGCONT"),
20 => Ok("SIGCHLD"),
21 => Ok("SIGTTIN"),
22 => Ok("SIGTTOU"),
23 => Ok("SIGIO"),
24 => Ok("SIGXCPU"),
25 => Ok("SIGXFSZ"),
26 => Ok("SIGVTALRM"),
27 => Ok("SIGPROF"),
28 => Ok("SIGWINCH"),
29 => Ok("SIGINFO"),
30 => Ok("SIGUSR1"),
31 => Ok("SIGUSR2"),
32 => Ok("SIGTHR"),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
}
#[cfg(any(target_os = "android", target_os = "linux"))] #[cfg(any(target_os = "android", target_os = "linux"))]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {s}") },
"SIGHUP" => Ok(1), (1, "SIGHUP"),
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGQUIT" => Ok(3), (3, "SIGQUIT"),
"SIGILL" => Ok(4), (4, "SIGILL"),
"SIGTRAP" => Ok(5), (5, "SIGTRAP"),
"SIGIOT" => Ok(6), (6, "SIGABRT" | "SIGIOT"),
"SIGABRT" => Ok(6), (7, "SIGBUS"),
"SIGBUS" => Ok(7), (8, "SIGFPE"),
"SIGFPE" => Ok(8), (9, "SIGKILL"),
"SIGKILL" => Ok(9), (10, "SIGUSR1"),
"SIGUSR1" => Ok(10), (11, "SIGSEGV"),
"SIGSEGV" => Ok(11), (12, "SIGUSR2"),
"SIGUSR2" => Ok(12), (13, "SIGPIPE"),
"SIGPIPE" => Ok(13), (14, "SIGALRM"),
"SIGALRM" => Ok(14), (15, "SIGTERM"),
"SIGTERM" => Ok(15), (16, "SIGSTKFLT"),
"SIGSTKFLT" => Ok(16), (17, "SIGCHLD"),
"SIGCHLD" => Ok(17), (18, "SIGCONT"),
"SIGCONT" => Ok(18), (19, "SIGSTOP"),
"SIGSTOP" => Ok(19), (20, "SIGTSTP"),
"SIGTSTP" => Ok(20), (21, "SIGTTIN"),
"SIGTTIN" => Ok(21), (22, "SIGTTOU"),
"SIGTTOU" => Ok(22), (23, "SIGURG"),
"SIGURG" => Ok(23), (24, "SIGXCPU"),
"SIGXCPU" => Ok(24), (25, "SIGXFSZ"),
"SIGXFSZ" => Ok(25), (26, "SIGVTALRM"),
"SIGVTALRM" => Ok(26), (27, "SIGPROF"),
"SIGPROF" => Ok(27), (28, "SIGWINCH"),
"SIGWINCH" => Ok(28), (29, "SIGIO" | "SIGPOLL"),
"SIGIO" | "SIGPOLL" => Ok(29), (30, "SIGPWR"),
"SIGPWR" => Ok(30), (31, "SIGSYS" | "SIGUNUSED")
"SIGSYS" | "SIGUNUSED" => Ok(31), );
_ => Err(type_error(format!("Invalid signal : {s}"))),
}
}
#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
1 => Ok("SIGHUP"),
2 => Ok("SIGINT"),
3 => Ok("SIGQUIT"),
4 => Ok("SIGILL"),
5 => Ok("SIGTRAP"),
6 => Ok("SIGABRT"),
7 => Ok("SIGBUS"),
8 => Ok("SIGFPE"),
9 => Ok("SIGKILL"),
10 => Ok("SIGUSR1"),
11 => Ok("SIGSEGV"),
12 => Ok("SIGUSR2"),
13 => Ok("SIGPIPE"),
14 => Ok("SIGALRM"),
15 => Ok("SIGTERM"),
16 => Ok("SIGSTKFLT"),
17 => Ok("SIGCHLD"),
18 => Ok("SIGCONT"),
19 => Ok("SIGSTOP"),
20 => Ok("SIGTSTP"),
21 => Ok("SIGTTIN"),
22 => Ok("SIGTTOU"),
23 => Ok("SIGURG"),
24 => Ok("SIGXCPU"),
25 => Ok("SIGXFSZ"),
26 => Ok("SIGVTALRM"),
27 => Ok("SIGPROF"),
28 => Ok("SIGWINCH"),
29 => Ok("SIGIO"),
30 => Ok("SIGPWR"),
31 => Ok("SIGSYS"),
_ => Err(type_error(format!("Invalid signal : {s}"))),
}
}
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {s}") },
"SIGHUP" => Ok(1), (1, "SIGHUP"),
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGQUIT" => Ok(3), (3, "SIGQUIT"),
"SIGILL" => Ok(4), (4, "SIGILL"),
"SIGTRAP" => Ok(5), (5, "SIGTRAP"),
"SIGIOT" => Ok(6), (6, "SIGABRT" | "SIGIOT"),
"SIGABRT" => Ok(6), (7, "SIGEMT"),
"SIGEMT" => Ok(7), (8, "SIGFPE"),
"SIGFPE" => Ok(8), (9, "SIGKILL"),
"SIGKILL" => Ok(9), (10, "SIGBUS"),
"SIGBUS" => Ok(10), (11, "SIGSEGV"),
"SIGSEGV" => Ok(11), (12, "SIGSYS"),
"SIGSYS" => Ok(12), (13, "SIGPIPE"),
"SIGPIPE" => Ok(13), (14, "SIGALRM"),
"SIGALRM" => Ok(14), (15, "SIGTERM"),
"SIGTERM" => Ok(15), (16, "SIGURG"),
"SIGURG" => Ok(16), (17, "SIGSTOP"),
"SIGSTOP" => Ok(17), (18, "SIGTSTP"),
"SIGTSTP" => Ok(18), (19, "SIGCONT"),
"SIGCONT" => Ok(19), (20, "SIGCHLD"),
"SIGCHLD" => Ok(20), (21, "SIGTTIN"),
"SIGTTIN" => Ok(21), (22, "SIGTTOU"),
"SIGTTOU" => Ok(22), (23, "SIGIO"),
"SIGIO" => Ok(23), (24, "SIGXCPU"),
"SIGXCPU" => Ok(24), (25, "SIGXFSZ"),
"SIGXFSZ" => Ok(25), (26, "SIGVTALRM"),
"SIGVTALRM" => Ok(26), (27, "SIGPROF"),
"SIGPROF" => Ok(27), (28, "SIGWINCH"),
"SIGWINCH" => Ok(28), (29, "SIGINFO"),
"SIGINFO" => Ok(29), (30, "SIGUSR1"),
"SIGUSR1" => Ok(30), (31, "SIGUSR2")
"SIGUSR2" => Ok(31), );
_ => Err(type_error(format!("Invalid signal: {s}"))),
}
}
#[cfg(target_os = "macos")]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
1 => Ok("SIGHUP"),
2 => Ok("SIGINT"),
3 => Ok("SIGQUIT"),
4 => Ok("SIGILL"),
5 => Ok("SIGTRAP"),
6 => Ok("SIGABRT"),
7 => Ok("SIGEMT"),
8 => Ok("SIGFPE"),
9 => Ok("SIGKILL"),
10 => Ok("SIGBUS"),
11 => Ok("SIGSEGV"),
12 => Ok("SIGSYS"),
13 => Ok("SIGPIPE"),
14 => Ok("SIGALRM"),
15 => Ok("SIGTERM"),
16 => Ok("SIGURG"),
17 => Ok("SIGSTOP"),
18 => Ok("SIGTSTP"),
19 => Ok("SIGCONT"),
20 => Ok("SIGCHLD"),
21 => Ok("SIGTTIN"),
22 => Ok("SIGTTOU"),
23 => Ok("SIGIO"),
24 => Ok("SIGXCPU"),
25 => Ok("SIGXFSZ"),
26 => Ok("SIGVTALRM"),
27 => Ok("SIGPROF"),
28 => Ok("SIGWINCH"),
29 => Ok("SIGINFO"),
30 => Ok("SIGUSR1"),
31 => Ok("SIGUSR2"),
_ => Err(type_error(format!("Invalid signal: {s}"))),
}
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))] #[cfg(any(target_os = "solaris", target_os = "illumos"))]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {s}") },
"SIGHUP" => Ok(1), (1, "SIGHUP"),
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGQUIT" => Ok(3), (3, "SIGQUIT"),
"SIGILL" => Ok(4), (4, "SIGILL"),
"SIGTRAP" => Ok(5), (5, "SIGTRAP"),
"SIGIOT" => Ok(6), (6, "SIGABRT" | "SIGIOT"),
"SIGABRT" => Ok(6), (7, "SIGEMT"),
"SIGEMT" => Ok(7), (8, "SIGFPE"),
"SIGFPE" => Ok(8), (9, "SIGKILL"),
"SIGKILL" => Ok(9), (10, "SIGBUS"),
"SIGBUS" => Ok(10), (11, "SIGSEGV"),
"SIGSEGV" => Ok(11), (12, "SIGSYS"),
"SIGSYS" => Ok(12), (13, "SIGPIPE"),
"SIGPIPE" => Ok(13), (14, "SIGALRM"),
"SIGALRM" => Ok(14), (15, "SIGTERM"),
"SIGTERM" => Ok(15), (16, "SIGUSR1"),
"SIGUSR1" => Ok(16), (17, "SIGUSR2"),
"SIGUSR2" => Ok(17), (18, "SIGCHLD"),
"SIGCLD" => Ok(18), (19, "SIGPWR"),
"SIGCHLD" => Ok(18), (20, "SIGWINCH"),
"SIGPWR" => Ok(19), (21, "SIGURG"),
"SIGWINCH" => Ok(20), (22, "SIGPOLL"),
"SIGURG" => Ok(21), (23, "SIGSTOP"),
"SIGPOLL" => Ok(22), (24, "SIGTSTP"),
"SIGIO" => Ok(22), (25, "SIGCONT"),
"SIGSTOP" => Ok(23), (26, "SIGTTIN"),
"SIGTSTP" => Ok(24), (27, "SIGTTOU"),
"SIGCONT" => Ok(25), (28, "SIGVTALRM"),
"SIGTTIN" => Ok(26), (29, "SIGPROF"),
"SIGTTOU" => Ok(27), (30, "SIGXCPU"),
"SIGVTALRM" => Ok(28), (31, "SIGXFSZ"),
"SIGPROF" => Ok(29), (32, "SIGWAITING"),
"SIGXCPU" => Ok(30), (33, "SIGLWP"),
"SIGXFSZ" => Ok(31), (34, "SIGFREEZE"),
"SIGWAITING" => Ok(32), (35, "SIGTHAW"),
"SIGLWP" => Ok(33), (36, "SIGCANCEL"),
"SIGFREEZE" => Ok(34), (37, "SIGLOST"),
"SIGTHAW" => Ok(35), (38, "SIGXRES"),
"SIGCANCEL" => Ok(36), (39, "SIGJVM1"),
"SIGLOST" => Ok(37), (40, "SIGJVM2")
"SIGXRES" => Ok(38), );
"SIGJVM1" => Ok(39),
"SIGJVM2" => Ok(40),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
1 => Ok("SIGHUP"),
2 => Ok("SIGINT"),
3 => Ok("SIGQUIT"),
4 => Ok("SIGILL"),
5 => Ok("SIGTRAP"),
6 => Ok("SIGABRT"),
7 => Ok("SIGEMT"),
8 => Ok("SIGFPE"),
9 => Ok("SIGKILL"),
10 => Ok("SIGBUS"),
11 => Ok("SIGSEGV"),
12 => Ok("SIGSYS"),
13 => Ok("SIGPIPE"),
14 => Ok("SIGALRM"),
15 => Ok("SIGTERM"),
16 => Ok("SIGUSR1"),
17 => Ok("SIGUSR2"),
18 => Ok("SIGCHLD"),
19 => Ok("SIGPWR"),
20 => Ok("SIGWINCH"),
21 => Ok("SIGURG"),
22 => Ok("SIGPOLL"),
23 => Ok("SIGSTOP"),
24 => Ok("SIGTSTP"),
25 => Ok("SIGCONT"),
26 => Ok("SIGTTIN"),
27 => Ok("SIGTTOU"),
28 => Ok("SIGVTALRM"),
29 => Ok("SIGPROF"),
30 => Ok("SIGXCPU"),
31 => Ok("SIGXFSZ"),
32 => Ok("SIGWAITING"),
33 => Ok("SIGLWP"),
34 => Ok("SIGFREEZE"),
35 => Ok("SIGTHAW"),
36 => Ok("SIGCANCEL"),
37 => Ok("SIGLOST"),
38 => Ok("SIGXRES"),
39 => Ok("SIGJVM1"),
40 => Ok("SIGJVM2"),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
}
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |_| { "Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK)." },
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGBREAK" => Ok(21), (21, "SIGBREAK")
_ => Err(type_error( );
"Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).",
)),
}
}
#[cfg(target_os = "windows")]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
2 => Ok("SIGINT"),
21 => Ok("SIGBREAK"),
_ => Err(type_error(
"Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).",
)),
}
}
#[cfg(unix)] #[cfg(unix)]
#[op2(fast)] #[op2(fast)]