Skip to content

Commit 95323ae

Browse files
committed
Add const-only signals for wasm32
1 parent 2c0e439 commit 95323ae

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

vm/src/stdlib/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub mod posix;
4040
pub(crate) mod msvcrt;
4141
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
4242
mod pwd;
43-
#[cfg(not(target_arch = "wasm32"))]
4443
pub(crate) mod signal;
4544
pub mod sys;
4645
#[cfg(windows)]
@@ -82,6 +81,7 @@ pub fn get_module_inits() -> StdlibMap {
8281
"_io" => io::make_module,
8382
"marshal" => marshal::make_module,
8483
"_operator" => operator::make_module,
84+
"_signal" => signal::make_module,
8585
"_sre" => sre::make_module,
8686
"_string" => string::make_module,
8787
"time" => time::make_module,
@@ -105,11 +105,6 @@ pub fn get_module_inits() -> StdlibMap {
105105
"posix" => posix::make_module,
106106
// "fcntl" => fcntl::make_module,
107107
}
108-
// disable some modules on WASM
109-
#[cfg(not(target_arch = "wasm32"))]
110-
{
111-
"_signal" => signal::make_module,
112-
}
113108
#[cfg(feature = "threading")]
114109
{
115110
"_thread" => thread::make_module,

vm/src/stdlib/signal.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{builtins::PyModule, PyRef, VirtualMachine};
33
pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
44
let module = _signal::make_module(vm);
55

6+
#[cfg(any(unix, windows))]
67
_signal::init_signal_handlers(&module, vm);
78

89
module
@@ -11,12 +12,17 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
1112
#[pymodule]
1213
pub(crate) mod _signal {
1314
use crate::{
14-
builtins::PyModule,
1515
convert::{IntoPyException, TryFromBorrowedObject},
1616
signal, Py, PyObjectRef, PyResult, VirtualMachine,
1717
};
1818
use std::sync::atomic::{self, Ordering};
1919

20+
#[cfg(any(unix, windows))]
21+
use libc::sighandler_t;
22+
#[allow(non_camel_case_types)]
23+
#[cfg(not(any(unix, windows)))]
24+
type sighandler_t = usize;
25+
2026
cfg_if::cfg_if! {
2127
if #[cfg(windows)] {
2228
type WakeupFd = libc::SOCKET;
@@ -33,23 +39,23 @@ pub(crate) mod _signal {
3339
}
3440

3541
#[cfg(unix)]
36-
pub use nix::unistd::alarm as sig_alarm;
37-
38-
#[cfg(not(windows))]
3942
pub use libc::SIG_ERR;
43+
#[cfg(unix)]
44+
pub use nix::unistd::alarm as sig_alarm;
4045

41-
#[cfg(not(windows))]
46+
#[cfg(unix)]
4247
#[pyattr]
4348
pub use libc::{SIG_DFL, SIG_IGN};
4449

45-
#[cfg(windows)]
50+
#[cfg(not(unix))]
4651
#[pyattr]
47-
pub const SIG_DFL: libc::sighandler_t = 0;
48-
#[cfg(windows)]
52+
pub const SIG_DFL: sighandler_t = 0;
53+
#[cfg(not(unix))]
4954
#[pyattr]
50-
pub const SIG_IGN: libc::sighandler_t = 1;
51-
#[cfg(windows)]
52-
pub const SIG_ERR: libc::sighandler_t = !0;
55+
pub const SIG_IGN: sighandler_t = 1;
56+
#[cfg(not(unix))]
57+
#[allow(dead_code)]
58+
pub const SIG_ERR: sighandler_t = -1 as _;
5359

5460
#[cfg(all(unix, not(target_os = "redox")))]
5561
extern "C" {
@@ -59,6 +65,7 @@ pub(crate) mod _signal {
5965
#[pyattr]
6066
use crate::signal::NSIG;
6167

68+
#[cfg(any(unix, windows))]
6269
#[pyattr]
6370
pub use libc::{SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM};
6471

@@ -80,7 +87,11 @@ pub(crate) mod _signal {
8087
#[pyattr]
8188
use libc::{SIGPWR, SIGSTKFLT};
8289

83-
pub(super) fn init_signal_handlers(module: &Py<PyModule>, vm: &VirtualMachine) {
90+
#[cfg(any(unix, windows))]
91+
pub(super) fn init_signal_handlers(
92+
module: &Py<crate::builtins::PyModule>,
93+
vm: &VirtualMachine,
94+
) {
8495
let sig_dfl = vm.new_pyobj(SIG_DFL as u8);
8596
let sig_ign = vm.new_pyobj(SIG_IGN as u8);
8697

@@ -107,6 +118,17 @@ pub(crate) mod _signal {
107118
}
108119
}
109120

121+
#[cfg(not(any(unix, windows)))]
122+
#[pyfunction]
123+
pub fn signal(
124+
_signalnum: i32,
125+
_handler: PyObjectRef,
126+
vm: &VirtualMachine,
127+
) -> PyResult<Option<PyObjectRef>> {
128+
Err(vm.new_not_implemented_error("signal is not implemented on this platform".to_owned()))
129+
}
130+
131+
#[cfg(any(unix, windows))]
110132
#[pyfunction]
111133
pub fn signal(
112134
signalnum: i32,
@@ -123,7 +145,7 @@ pub(crate) mod _signal {
123145
match usize::try_from_borrowed_object(vm, &handler).ok() {
124146
Some(SIG_DFL) => SIG_DFL,
125147
Some(SIG_IGN) => SIG_IGN,
126-
None if handler.is_callable() => run_signal as libc::sighandler_t,
148+
None if handler.is_callable() => run_signal as sighandler_t,
127149
_ => return Err(vm.new_type_error(
128150
"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"
129151
.to_owned(),
@@ -226,7 +248,7 @@ pub(crate) mod _signal {
226248
} else {
227249
false
228250
};
229-
#[cfg(not(windows))]
251+
#[cfg(unix)]
230252
if fd != INVALID_WAKEUP {
231253
use nix::fcntl;
232254
let oflags = fcntl::fcntl(fd, fcntl::F_GETFL).map_err(|e| e.into_pyexception(vm))?;
@@ -256,6 +278,7 @@ pub(crate) mod _signal {
256278
}
257279
}
258280

281+
#[cfg(any(unix, windows))]
259282
pub extern "C" fn run_signal(signum: i32) {
260283
signal::TRIGGERS[signum as usize].store(true, Ordering::Relaxed);
261284
signal::set_triggered();

0 commit comments

Comments
 (0)