Skip to content

Commit ed3811a

Browse files
committed
wasi select module
1 parent 4e915de commit ed3811a

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎stdlib/src/lib.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod grp;
5858
mod resource;
5959
#[cfg(target_os = "macos")]
6060
mod scproxy;
61-
#[cfg(not(target_arch = "wasm32"))]
61+
#[cfg(any(unix, windows, target_os = "wasi"))]
6262
mod select;
6363
#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
6464
mod sqlite;
@@ -130,10 +130,13 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
130130
{
131131
"fcntl" => fcntl::make_module,
132132
}
133+
#[cfg(any(unix, windows, target_os = "wasi"))]
134+
{
135+
"select" => select::make_module,
136+
}
133137
#[cfg(not(target_arch = "wasm32"))]
134138
{
135139
"_multiprocessing" => multiprocessing::make_module,
136-
"select" => select::make_module,
137140
"_socket" => socket::make_module,
138141
"faulthandler" => faulthandler::make_module,
139142
}

‎stdlib/src/select.rs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,63 @@ mod platform {
6565
}
6666
}
6767

68+
#[cfg(target_os = "wasi")]
69+
mod platform {
70+
pub use libc::{timeval, FD_SETSIZE};
71+
pub use std::os::wasi::io::RawFd;
72+
73+
pub fn check_err(x: i32) -> bool {
74+
x < 0
75+
}
76+
77+
#[repr(C)]
78+
pub struct fd_set {
79+
__nfds: usize,
80+
__fds: [libc::c_int; FD_SETSIZE],
81+
}
82+
83+
#[allow(non_snake_case)]
84+
pub unsafe fn FD_ISSET(fd: RawFd, set: *const fd_set) -> bool {
85+
let set = &*set;
86+
let n = set.__nfds;
87+
for p in &set.__fds[..n] {
88+
if *p == fd {
89+
return true;
90+
}
91+
}
92+
false
93+
}
94+
95+
#[allow(non_snake_case)]
96+
pub unsafe fn FD_SET(fd: RawFd, set: *mut fd_set) {
97+
let set = &mut *set;
98+
let n = set.__nfds;
99+
for p in &set.__fds[..n] {
100+
if *p == fd {
101+
return;
102+
}
103+
}
104+
set.__nfds = n + 1;
105+
set.__fds[n] = fd;
106+
}
107+
108+
#[allow(non_snake_case)]
109+
pub unsafe fn FD_ZERO(set: *mut fd_set) {
110+
let set = &mut *set;
111+
set.__nfds = 0;
112+
}
113+
114+
extern "C" {
115+
pub fn select(
116+
nfds: libc::c_int,
117+
readfds: *mut fd_set,
118+
writefds: *mut fd_set,
119+
errorfds: *mut fd_set,
120+
timeout: *const timeval,
121+
) -> libc::c_int;
122+
}
123+
}
124+
68125
pub use platform::timeval;
69126
use platform::RawFd;
70127

0 commit comments

Comments
 (0)