Skip to content

Commit b457caa

Browse files
Rollup merge of rust-lang#63676 - newpavlov:wasi, r=alexcrichton
Use wasi crate for Core API Blocked by: bytecodealliance/wasi-rs#5 Blocks: rust-lang/libc#1461 cc @sunfishcode @alexcrichton
2 parents 6187684 + 0662fcf commit b457caa

File tree

14 files changed

+340
-434
lines changed

14 files changed

+340
-434
lines changed

Cargo.lock

+12
Original file line numberDiff line numberDiff line change
@@ -3870,6 +3870,7 @@ dependencies = [
38703870
"rustc_msan",
38713871
"rustc_tsan",
38723872
"unwind",
3873+
"wasi",
38733874
]
38743875

38753876
[[package]]
@@ -4686,6 +4687,17 @@ dependencies = [
46864687
"try-lock",
46874688
]
46884689

4690+
[[package]]
4691+
name = "wasi"
4692+
version = "0.7.0"
4693+
source = "registry+https://github.com/rust-lang/crates.io-index"
4694+
checksum = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d"
4695+
dependencies = [
4696+
"compiler_builtins",
4697+
"rustc-std-workspace-alloc",
4698+
"rustc-std-workspace-core",
4699+
]
4700+
46894701
[[package]]
46904702
name = "winapi"
46914703
version = "0.2.8"

src/libstd/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ dlmalloc = { version = "0.1", features = ['rustc-dep-of-std'] }
5656
[target.x86_64-fortanix-unknown-sgx.dependencies]
5757
fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }
5858

59+
[target.wasm32-wasi.dependencies]
60+
wasi = { version = "0.7.0", features = ['rustc-dep-of-std', 'alloc'] }
61+
5962
[build-dependencies]
6063
cc = "1.0"
6164

src/libstd/sys/wasi/args.rs

+13-28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::ffi::CStr;
2-
use crate::io;
3-
use crate::sys::cvt_wasi;
41
use crate::ffi::OsString;
52
use crate::marker::PhantomData;
63
use crate::os::wasi::ffi::OsStringExt;
74
use crate::vec;
85

6+
use ::wasi::wasi_unstable as wasi;
7+
98
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
109
}
1110

@@ -19,31 +18,17 @@ pub struct Args {
1918

2019
/// Returns the command line arguments
2120
pub fn args() -> Args {
22-
maybe_args().unwrap_or_else(|_| {
23-
Args {
24-
iter: Vec::new().into_iter(),
25-
_dont_send_or_sync_me: PhantomData
26-
}
27-
})
28-
}
29-
30-
fn maybe_args() -> io::Result<Args> {
31-
unsafe {
32-
let (mut argc, mut argv_buf_size) = (0, 0);
33-
cvt_wasi(libc::__wasi_args_sizes_get(&mut argc, &mut argv_buf_size))?;
34-
35-
let mut argc = vec![core::ptr::null_mut::<libc::c_char>(); argc];
36-
let mut argv_buf = vec![0; argv_buf_size];
37-
cvt_wasi(libc::__wasi_args_get(argc.as_mut_ptr(), argv_buf.as_mut_ptr()))?;
38-
39-
let args = argc.into_iter()
40-
.map(|ptr| CStr::from_ptr(ptr).to_bytes().to_vec())
41-
.map(|bytes| OsString::from_vec(bytes))
42-
.collect::<Vec<_>>();
43-
Ok(Args {
44-
iter: args.into_iter(),
45-
_dont_send_or_sync_me: PhantomData,
46-
})
21+
let buf = wasi::args_sizes_get().and_then(|args_sizes| {
22+
let mut buf = Vec::with_capacity(args_sizes.get_count());
23+
wasi::args_get(args_sizes, |arg| {
24+
let arg = OsString::from_vec(arg.to_vec());
25+
buf.push(arg);
26+
})?;
27+
Ok(buf)
28+
}).unwrap_or(vec![]);
29+
Args {
30+
iter: buf.into_iter(),
31+
_dont_send_or_sync_me: PhantomData
4732
}
4833
}
4934

src/libstd/sys/wasi/ext/fs.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::os::wasi::ffi::OsStrExt;
88
use crate::path::{Path, PathBuf};
99
use crate::sys_common::{AsInner, AsInnerMut, FromInner};
1010

11+
use ::wasi::wasi_unstable as wasi;
12+
1113
/// WASI-specific extensions to [`File`].
1214
///
1315
/// [`File`]: ../../../../std/fs/struct.File.html
@@ -336,16 +338,16 @@ pub trait FileTypeExt {
336338

337339
impl FileTypeExt for fs::FileType {
338340
fn is_block_device(&self) -> bool {
339-
self.as_inner().bits() == libc::__WASI_FILETYPE_BLOCK_DEVICE
341+
self.as_inner().bits() == wasi::FILETYPE_BLOCK_DEVICE
340342
}
341343
fn is_character_device(&self) -> bool {
342-
self.as_inner().bits() == libc::__WASI_FILETYPE_CHARACTER_DEVICE
344+
self.as_inner().bits() == wasi::FILETYPE_CHARACTER_DEVICE
343345
}
344346
fn is_socket_dgram(&self) -> bool {
345-
self.as_inner().bits() == libc::__WASI_FILETYPE_SOCKET_DGRAM
347+
self.as_inner().bits() == wasi::FILETYPE_SOCKET_DGRAM
346348
}
347349
fn is_socket_stream(&self) -> bool {
348-
self.as_inner().bits() == libc::__WASI_FILETYPE_SOCKET_STREAM
350+
self.as_inner().bits() == wasi::FILETYPE_SOCKET_STREAM
349351
}
350352
}
351353

src/libstd/sys/wasi/ext/io.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::sys;
88
use crate::net;
99
use crate::sys_common::{AsInner, FromInner, IntoInner};
1010

11+
use ::wasi::wasi_unstable as wasi;
12+
1113
/// Raw file descriptors.
1214
pub type RawFd = u32;
1315

@@ -125,18 +127,18 @@ impl IntoRawFd for fs::File {
125127

126128
impl AsRawFd for io::Stdin {
127129
fn as_raw_fd(&self) -> RawFd {
128-
libc::STDIN_FILENO as u32
130+
wasi::STDIN_FD
129131
}
130132
}
131133

132134
impl AsRawFd for io::Stdout {
133135
fn as_raw_fd(&self) -> RawFd {
134-
libc::STDOUT_FILENO as u32
136+
wasi::STDOUT_FD
135137
}
136138
}
137139

138140
impl AsRawFd for io::Stderr {
139141
fn as_raw_fd(&self) -> RawFd {
140-
libc::STDERR_FILENO as u32
142+
wasi::STDERR_FD
141143
}
142144
}

0 commit comments

Comments
 (0)