From a61ba3c6995bef58f508a34e537932284692c294 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:56:57 -0700 Subject: [PATCH 1/3] fix(net): don't try to set nodelay on upgrade streams (#26342) Fixes https://github.com/denoland/deno/issues/26341. We try to call `op_set_nodelay` on an `UpgradeStream`, which doesn't support that operation. --- ext/http/00_serve.ts | 8 ++++++-- ext/net/01_net.js | 15 +++++++++++++++ ext/node/polyfills/http.ts | 4 ++-- ext/node/polyfills/internal_binding/tcp_wrap.ts | 4 +++- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index 3b9b085a22..1b70cf2129 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -76,7 +76,11 @@ import { ReadableStreamPrototype, resourceForReadableStream, } from "ext:deno_web/06_streams.js"; -import { listen, listenOptionApiName, TcpConn } from "ext:deno_net/01_net.js"; +import { + listen, + listenOptionApiName, + UpgradedConn, +} from "ext:deno_net/01_net.js"; import { hasTlsKeyPairOptions, listenTls } from "ext:deno_net/02_tls.js"; import { SymbolAsyncDispose } from "ext:deno_web/00_infra.js"; @@ -189,7 +193,7 @@ class InnerRequest { const upgradeRid = op_http_upgrade_raw(external); - const conn = new TcpConn( + const conn = new UpgradedConn( upgradeRid, underlyingConn?.remoteAddr, underlyingConn?.localAddr, diff --git a/ext/net/01_net.js b/ext/net/01_net.js index 5b894947ee..c3e5f9e5ca 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -194,6 +194,20 @@ class Conn { } } +class UpgradedConn extends Conn { + #rid = 0; + + constructor(rid, remoteAddr, localAddr) { + super(rid, remoteAddr, localAddr); + ObjectDefineProperty(this, internalRidSymbol, { + __proto__: null, + enumerable: false, + value: rid, + }); + this.#rid = rid; + } +} + class TcpConn extends Conn { #rid = 0; @@ -601,5 +615,6 @@ export { resolveDns, TcpConn, UnixConn, + UpgradedConn, validatePort, }; diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index e117a0ec24..20bef30099 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -67,7 +67,7 @@ import { headersEntries } from "ext:deno_fetch/20_headers.js"; import { timerId } from "ext:deno_web/03_abort_signal.js"; import { clearTimeout as webClearTimeout } from "ext:deno_web/02_timers.js"; import { resourceForReadableStream } from "ext:deno_web/06_streams.js"; -import { TcpConn } from "ext:deno_net/01_net.js"; +import { UpgradedConn } from "ext:deno_net/01_net.js"; import { STATUS_CODES } from "node:_http_server"; import { methods as METHODS } from "node:_http_common"; @@ -517,7 +517,7 @@ class ClientRequest extends OutgoingMessage { ); assert(typeof res.remoteAddrIp !== "undefined"); assert(typeof res.remoteAddrIp !== "undefined"); - const conn = new TcpConn( + const conn = new UpgradedConn( upgradeRid, { transport: "tcp", diff --git a/ext/node/polyfills/internal_binding/tcp_wrap.ts b/ext/node/polyfills/internal_binding/tcp_wrap.ts index 1321cc6277..2856f808a1 100644 --- a/ext/node/polyfills/internal_binding/tcp_wrap.ts +++ b/ext/node/polyfills/internal_binding/tcp_wrap.ts @@ -300,7 +300,9 @@ export class TCP extends ConnectionWrap { * @return An error status code. */ setNoDelay(noDelay: boolean): number { - this[kStreamBaseField].setNoDelay(noDelay); + if ("setNoDelay" in this[kStreamBaseField]) { + this[kStreamBaseField].setNoDelay(noDelay); + } return 0; } From 56895858885297537204a16dd463f28b73d25551 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 17 Oct 2024 18:44:51 +0800 Subject: [PATCH 2/3] fix(cli): set napi object property properly (#26344) --- cli/napi/js_native_api.rs | 29 ++++++++++++++++------------- tests/napi/object_test.js | 25 ++++++++++++++++++++++++- tests/napi/src/object.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 14 deletions(-) diff --git a/cli/napi/js_native_api.rs b/cli/napi/js_native_api.rs index 1d2c99c2c0..35e7690c3c 100644 --- a/cli/napi/js_native_api.rs +++ b/cli/napi/js_native_api.rs @@ -264,6 +264,16 @@ fn napi_define_class<'s>( Err(status) => return status, }; + let mut accessor_property = v8::PropertyAttribute::NONE; + + if p.attributes & napi_enumerable == 0 { + accessor_property = accessor_property | v8::PropertyAttribute::DONT_ENUM; + } + if p.attributes & napi_configurable == 0 { + accessor_property = + accessor_property | v8::PropertyAttribute::DONT_DELETE; + } + if p.getter.is_some() || p.setter.is_some() { let getter = p.getter.map(|g| { create_function_template(&mut env.scope(), env_ptr, None, g, p.data) @@ -271,8 +281,6 @@ fn napi_define_class<'s>( let setter = p.setter.map(|s| { create_function_template(&mut env.scope(), env_ptr, None, s, p.data) }); - - let mut accessor_property = v8::PropertyAttribute::NONE; if getter.is_some() && setter.is_some() && (p.attributes & napi_writable) == 0 @@ -280,15 +288,6 @@ fn napi_define_class<'s>( accessor_property = accessor_property | v8::PropertyAttribute::READ_ONLY; } - if p.attributes & napi_enumerable == 0 { - accessor_property = - accessor_property | v8::PropertyAttribute::DONT_ENUM; - } - if p.attributes & napi_configurable == 0 { - accessor_property = - accessor_property | v8::PropertyAttribute::DONT_DELETE; - } - let proto = tpl.prototype_template(&mut env.scope()); proto.set_accessor_property(name, getter, setter, accessor_property); } else if let Some(method) = p.method { @@ -300,10 +299,14 @@ fn napi_define_class<'s>( p.data, ); let proto = tpl.prototype_template(&mut env.scope()); - proto.set(name, function.into()); + proto.set_with_attr(name, function.into(), accessor_property); } else { let proto = tpl.prototype_template(&mut env.scope()); - proto.set(name, p.value.unwrap().into()); + if (p.attributes & napi_writable) == 0 { + accessor_property = + accessor_property | v8::PropertyAttribute::READ_ONLY; + } + proto.set_with_attr(name, p.value.unwrap().into(), accessor_property); } } diff --git a/tests/napi/object_test.js b/tests/napi/object_test.js index 4bc5c3c9c4..6226b0138c 100644 --- a/tests/napi/object_test.js +++ b/tests/napi/object_test.js @@ -1,6 +1,11 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals, loadTestLibrary } from "./common.js"; +import { + assert, + assertEquals, + assertThrows, + loadTestLibrary, +} from "./common.js"; const object = loadTestLibrary(); @@ -12,4 +17,22 @@ Deno.test("napi object", function () { const r1 = object.test_object_get(r); assert(r === r1); + + const r2 = object.test_object_attr_property(r); + assert(r === r2); + assertThrows( + () => { + r2.self = "2"; + }, + Error, + "Cannot assign to read only property 'self' of object '#'", + ); + + assertThrows( + () => { + r2.method = () => {}; + }, + Error, + "Cannot assign to read only property 'method' of object '#'", + ); }); diff --git a/tests/napi/src/object.rs b/tests/napi/src/object.rs index aa34133dcf..9876f4dae0 100644 --- a/tests/napi/src/object.rs +++ b/tests/napi/src/object.rs @@ -40,10 +40,39 @@ extern "C" fn test_object_get( obj } +extern "C" fn test_object_attr_property( + env: napi_env, + info: napi_callback_info, +) -> napi_value { + let (args, argc, _) = napi_get_callback_info!(env, info, 1); + assert_eq!(argc, 1); + + let obj = args[0]; + let mut property = napi_new_property!(env, "self", test_object_new); + property.attributes = PropertyAttributes::enumerable; + property.method = None; + property.value = obj; + let mut method_property = napi_new_property!(env, "method", test_object_new); + method_property.attributes = PropertyAttributes::enumerable; + let properties = &[property, method_property]; + assert_napi_ok!(napi_define_properties( + env, + obj, + properties.len(), + properties.as_ptr() + )); + obj +} + pub fn init(env: napi_env, exports: napi_value) { let properties = &[ napi_new_property!(env, "test_object_new", test_object_new), napi_new_property!(env, "test_object_get", test_object_get), + napi_new_property!( + env, + "test_object_attr_property", + test_object_attr_property + ), ]; assert_napi_ok!(napi_define_properties( From e61e2e4b5cc10bd3e1d92472e035edd794ab0311 Mon Sep 17 00:00:00 2001 From: Patrick Uftring <14282874+pjuftring@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:35:57 +0200 Subject: [PATCH 3/3] refactor: use macros for signal table (#26214) --- runtime/ops/signal.rs | 624 ++++++++++++++---------------------------- 1 file changed, 206 insertions(+), 418 deletions(-) diff --git a/runtime/ops/signal.rs b/runtime/ops/signal.rs index ebc6db6d1b..3bf43d6e15 100644 --- a/runtime/ops/signal.rs +++ b/runtime/ops/signal.rs @@ -147,438 +147,226 @@ impl Resource for SignalStreamResource { } } -#[cfg(target_os = "freebsd")] -pub fn signal_str_to_int(s: &str) -> Result { - match s { - "SIGHUP" => Ok(1), - "SIGINT" => Ok(2), - "SIGQUIT" => Ok(3), - "SIGILL" => Ok(4), - "SIGTRAP" => Ok(5), - "SIGIOT" => Ok(6), - "SIGABRT" => Ok(6), - "SIGEMT" => Ok(7), - "SIGFPE" => Ok(8), - "SIGKILL" => Ok(9), - "SIGBUS" => Ok(10), - "SIGSEGV" => Ok(11), - "SIGSYS" => Ok(12), - "SIGPIPE" => Ok(13), - "SIGALRM" => Ok(14), - "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))), +macro_rules! first_literal { + ($head:literal $(, $tail:literal)*) => { + $head + }; +} +macro_rules! signal_dict { + ($error_msg:expr, $(($number:literal, $($name:literal)|+)),*) => { + pub fn signal_str_to_int(s: &str) -> Result { + match s { + $($($name)|* => Ok($number),)* + _ => Err(type_error($error_msg(s))), + } + } + + pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> { + match s { + $($number => Ok(first_literal!($($name),+)),)* + _ => Err(type_error($error_msg(s))), + } + } } } #[cfg(target_os = "freebsd")] -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"), - 33 => Ok("SIGLIBRT"), - _ => Err(type_error(format!("Invalid signal : {}", s))), - } -} +signal_dict!( + |s| { format!("Invalid signal : {}", s) }, + (1, "SIGHUP"), + (2, "SIGINT"), + (3, "SIGQUIT"), + (4, "SIGILL"), + (5, "SIGTRAP"), + (6, "SIGABRT" | "SIGIOT"), + (7, "SIGEMT"), + (8, "SIGFPE"), + (9, "SIGKILL"), + (10, "SIGBUS"), + (11, "SIGSEGV"), + (12, "SIGSYS"), + (13, "SIGPIPE"), + (14, "SIGALRM"), + (15, "SIGTERM"), + (16, "SIGURG"), + (17, "SIGSTOP"), + (18, "SIGTSTP"), + (19, "SIGCONT"), + (20, "SIGCHLD"), + (21, "SIGTTIN"), + (22, "SIGTTOU"), + (23, "SIGIO"), + (24, "SIGXCPU"), + (25, "SIGXFSZ"), + (26, "SIGVTALRM"), + (27, "SIGPROF"), + (28, "SIGWINCH"), + (29, "SIGINFO"), + (30, "SIGUSR1"), + (31, "SIGUSR2"), + (32, "SIGTHR"), + (33, "SIGLIBRT") +); #[cfg(target_os = "openbsd")] -pub fn signal_str_to_int(s: &str) -> Result { - match s { - "SIGHUP" => Ok(1), - "SIGINT" => Ok(2), - "SIGQUIT" => Ok(3), - "SIGILL" => Ok(4), - "SIGTRAP" => Ok(5), - "SIGIOT" => Ok(6), - "SIGABRT" => Ok(6), - "SIGEMT" => Ok(7), - "SIGFPE" => Ok(8), - "SIGKILL" => Ok(9), - "SIGBUS" => Ok(10), - "SIGSEGV" => Ok(11), - "SIGSYS" => Ok(12), - "SIGPIPE" => Ok(13), - "SIGALRM" => Ok(14), - "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), - _ => 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))), - } -} +signal_dict!( + |s| { format!("Invalid signal : {}", s) }, + (1, "SIGHUP"), + (2, "SIGINT"), + (3, "SIGQUIT"), + (4, "SIGILL"), + (5, "SIGTRAP"), + (6, "SIGABRT" | "SIGIOT"), + (7, "SIGEMT"), + (8, "SIGKILL"), + (10, "SIGBUS"), + (11, "SIGSEGV"), + (12, "SIGSYS"), + (13, "SIGPIPE"), + (14, "SIGALRM"), + (15, "SIGTERM"), + (16, "SIGURG"), + (17, "SIGSTOP"), + (18, "SIGTSTP"), + (19, "SIGCONT"), + (20, "SIGCHLD"), + (21, "SIGTTIN"), + (22, "SIGTTOU"), + (23, "SIGIO"), + (24, "SIGXCPU"), + (25, "SIGXFSZ"), + (26, "SIGVTALRM"), + (27, "SIGPROF"), + (28, "SIGWINCH"), + (29, "SIGINFO"), + (30, "SIGUSR1"), + (31, "SIGUSR2"), + (32, "SIGTHR") +); #[cfg(any(target_os = "android", target_os = "linux"))] -pub fn signal_str_to_int(s: &str) -> Result { - match s { - "SIGHUP" => Ok(1), - "SIGINT" => Ok(2), - "SIGQUIT" => Ok(3), - "SIGILL" => Ok(4), - "SIGTRAP" => Ok(5), - "SIGIOT" => Ok(6), - "SIGABRT" => Ok(6), - "SIGBUS" => Ok(7), - "SIGFPE" => Ok(8), - "SIGKILL" => Ok(9), - "SIGUSR1" => Ok(10), - "SIGSEGV" => Ok(11), - "SIGUSR2" => Ok(12), - "SIGPIPE" => Ok(13), - "SIGALRM" => Ok(14), - "SIGTERM" => Ok(15), - "SIGSTKFLT" => Ok(16), - "SIGCHLD" => Ok(17), - "SIGCONT" => Ok(18), - "SIGSTOP" => Ok(19), - "SIGTSTP" => Ok(20), - "SIGTTIN" => Ok(21), - "SIGTTOU" => Ok(22), - "SIGURG" => Ok(23), - "SIGXCPU" => Ok(24), - "SIGXFSZ" => Ok(25), - "SIGVTALRM" => Ok(26), - "SIGPROF" => Ok(27), - "SIGWINCH" => Ok(28), - "SIGIO" | "SIGPOLL" => Ok(29), - "SIGPWR" => Ok(30), - "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}"))), - } -} +signal_dict!( + |s| { format!("Invalid signal : {s}") }, + (1, "SIGHUP"), + (2, "SIGINT"), + (3, "SIGQUIT"), + (4, "SIGILL"), + (5, "SIGTRAP"), + (6, "SIGABRT" | "SIGIOT"), + (7, "SIGBUS"), + (8, "SIGFPE"), + (9, "SIGKILL"), + (10, "SIGUSR1"), + (11, "SIGSEGV"), + (12, "SIGUSR2"), + (13, "SIGPIPE"), + (14, "SIGALRM"), + (15, "SIGTERM"), + (16, "SIGSTKFLT"), + (17, "SIGCHLD"), + (18, "SIGCONT"), + (19, "SIGSTOP"), + (20, "SIGTSTP"), + (21, "SIGTTIN"), + (22, "SIGTTOU"), + (23, "SIGURG"), + (24, "SIGXCPU"), + (25, "SIGXFSZ"), + (26, "SIGVTALRM"), + (27, "SIGPROF"), + (28, "SIGWINCH"), + (29, "SIGIO" | "SIGPOLL"), + (30, "SIGPWR"), + (31, "SIGSYS" | "SIGUNUSED") +); #[cfg(target_os = "macos")] -pub fn signal_str_to_int(s: &str) -> Result { - match s { - "SIGHUP" => Ok(1), - "SIGINT" => Ok(2), - "SIGQUIT" => Ok(3), - "SIGILL" => Ok(4), - "SIGTRAP" => Ok(5), - "SIGIOT" => Ok(6), - "SIGABRT" => Ok(6), - "SIGEMT" => Ok(7), - "SIGFPE" => Ok(8), - "SIGKILL" => Ok(9), - "SIGBUS" => Ok(10), - "SIGSEGV" => Ok(11), - "SIGSYS" => Ok(12), - "SIGPIPE" => Ok(13), - "SIGALRM" => Ok(14), - "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), - _ => 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}"))), - } -} +signal_dict!( + |s| { format!("Invalid signal : {s}") }, + (1, "SIGHUP"), + (2, "SIGINT"), + (3, "SIGQUIT"), + (4, "SIGILL"), + (5, "SIGTRAP"), + (6, "SIGABRT" | "SIGIOT"), + (7, "SIGEMT"), + (8, "SIGFPE"), + (9, "SIGKILL"), + (10, "SIGBUS"), + (11, "SIGSEGV"), + (12, "SIGSYS"), + (13, "SIGPIPE"), + (14, "SIGALRM"), + (15, "SIGTERM"), + (16, "SIGURG"), + (17, "SIGSTOP"), + (18, "SIGTSTP"), + (19, "SIGCONT"), + (20, "SIGCHLD"), + (21, "SIGTTIN"), + (22, "SIGTTOU"), + (23, "SIGIO"), + (24, "SIGXCPU"), + (25, "SIGXFSZ"), + (26, "SIGVTALRM"), + (27, "SIGPROF"), + (28, "SIGWINCH"), + (29, "SIGINFO"), + (30, "SIGUSR1"), + (31, "SIGUSR2") +); #[cfg(any(target_os = "solaris", target_os = "illumos"))] -pub fn signal_str_to_int(s: &str) -> Result { - match s { - "SIGHUP" => Ok(1), - "SIGINT" => Ok(2), - "SIGQUIT" => Ok(3), - "SIGILL" => Ok(4), - "SIGTRAP" => Ok(5), - "SIGIOT" => Ok(6), - "SIGABRT" => Ok(6), - "SIGEMT" => Ok(7), - "SIGFPE" => Ok(8), - "SIGKILL" => Ok(9), - "SIGBUS" => Ok(10), - "SIGSEGV" => Ok(11), - "SIGSYS" => Ok(12), - "SIGPIPE" => Ok(13), - "SIGALRM" => Ok(14), - "SIGTERM" => Ok(15), - "SIGUSR1" => Ok(16), - "SIGUSR2" => Ok(17), - "SIGCLD" => Ok(18), - "SIGCHLD" => Ok(18), - "SIGPWR" => Ok(19), - "SIGWINCH" => Ok(20), - "SIGURG" => Ok(21), - "SIGPOLL" => Ok(22), - "SIGIO" => Ok(22), - "SIGSTOP" => Ok(23), - "SIGTSTP" => Ok(24), - "SIGCONT" => Ok(25), - "SIGTTIN" => Ok(26), - "SIGTTOU" => Ok(27), - "SIGVTALRM" => Ok(28), - "SIGPROF" => Ok(29), - "SIGXCPU" => Ok(30), - "SIGXFSZ" => Ok(31), - "SIGWAITING" => Ok(32), - "SIGLWP" => Ok(33), - "SIGFREEZE" => Ok(34), - "SIGTHAW" => Ok(35), - "SIGCANCEL" => Ok(36), - "SIGLOST" => Ok(37), - "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))), - } -} +signal_dict!( + |s| { format!("Invalid signal : {s}") }, + (1, "SIGHUP"), + (2, "SIGINT"), + (3, "SIGQUIT"), + (4, "SIGILL"), + (5, "SIGTRAP"), + (6, "SIGABRT" | "SIGIOT"), + (7, "SIGEMT"), + (8, "SIGFPE"), + (9, "SIGKILL"), + (10, "SIGBUS"), + (11, "SIGSEGV"), + (12, "SIGSYS"), + (13, "SIGPIPE"), + (14, "SIGALRM"), + (15, "SIGTERM"), + (16, "SIGUSR1"), + (17, "SIGUSR2"), + (18, "SIGCHLD"), + (19, "SIGPWR"), + (20, "SIGWINCH"), + (21, "SIGURG"), + (22, "SIGPOLL"), + (23, "SIGSTOP"), + (24, "SIGTSTP"), + (25, "SIGCONT"), + (26, "SIGTTIN"), + (27, "SIGTTOU"), + (28, "SIGVTALRM"), + (29, "SIGPROF"), + (30, "SIGXCPU"), + (31, "SIGXFSZ"), + (32, "SIGWAITING"), + (33, "SIGLWP"), + (34, "SIGFREEZE"), + (35, "SIGTHAW"), + (36, "SIGCANCEL"), + (37, "SIGLOST"), + (38, "SIGXRES"), + (39, "SIGJVM1"), + (40, "SIGJVM2") +); #[cfg(target_os = "windows")] -pub fn signal_str_to_int(s: &str) -> Result { - match s { - "SIGINT" => Ok(2), - "SIGBREAK" => Ok(21), - _ => 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).", - )), - } -} +signal_dict!( + |_| { "Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK)." }, + (2, "SIGINT"), + (21, "SIGBREAK") +); #[cfg(unix)] #[op2(fast)]