Skip to content

Commit f3fb1c5

Browse files
committed
Update the wasi crate for wasm32-wasi
This commit updates the `wasi` crate used by the standard library which is used to implement most of the functionality of libstd on the `wasm32-wasi` target. This update comes with a brand new crate structure in the `wasi` crate which caused quite a few changes for the wasi target here, but it also comes with a significant change to where the functionality is coming from. The WASI specification is organized into "snapshots" and a new snapshot happened recently, so the WASI APIs themselves have changed since the previous revision. This had only minor impact on the public facing surface area of libstd, only changing on `u32` to a `u64` in an unstable API. The actual source for all of these types and such, however, is now coming from the `wasi_preview_snapshot1` module instead of the `wasi_unstable` module like before. This means that any implementors generating binaries will need to ensure that their embedding environment handles the `wasi_preview_snapshot1` module.
1 parent 7d80865 commit f3fb1c5

File tree

12 files changed

+304
-364
lines changed

12 files changed

+304
-364
lines changed

Cargo.lock

+8-2
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ checksum = "473a1265acc8ff1e808cd0a1af8cee3c2ee5200916058a2ca113c29f2d903571"
12941294
dependencies = [
12951295
"cfg-if",
12961296
"libc",
1297-
"wasi",
1297+
"wasi 0.7.0",
12981298
]
12991299

13001300
[[package]]
@@ -4301,7 +4301,7 @@ dependencies = [
43014301
"rustc_msan",
43024302
"rustc_tsan",
43034303
"unwind",
4304-
"wasi",
4304+
"wasi 0.9.0+wasi-snapshot-preview1",
43054305
]
43064306

43074307
[[package]]
@@ -5172,6 +5172,12 @@ name = "wasi"
51725172
version = "0.7.0"
51735173
source = "registry+https://github.com/rust-lang/crates.io-index"
51745174
checksum = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d"
5175+
5176+
[[package]]
5177+
name = "wasi"
5178+
version = "0.9.0+wasi-snapshot-preview1"
5179+
source = "registry+https://github.com/rust-lang/crates.io-index"
5180+
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
51755181
dependencies = [
51765182
"compiler_builtins",
51775183
"rustc-std-workspace-alloc",

src/libstd/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }
5454
hermit-abi = { version = "0.1", features = ['rustc-dep-of-std'] }
5555

5656
[target.wasm32-wasi.dependencies]
57-
wasi = { version = "0.7.0", features = ['rustc-dep-of-std', 'alloc'] }
57+
wasi = { version = "0.9.0", features = ['rustc-dep-of-std'], default-features = false }
5858

5959
[features]
6060
default = ["std_detect_file_io", "std_detect_dlsym_getauxval"]

src/libstd/sys/wasi/args.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
use crate::ffi::OsString;
1+
use crate::ffi::{CStr, OsStr, OsString};
22
use crate::marker::PhantomData;
3-
use crate::os::wasi::ffi::OsStringExt;
3+
use crate::os::wasi::ffi::OsStrExt;
44
use crate::vec;
55

6-
use ::wasi::wasi_unstable as wasi;
6+
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
77

8-
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
9-
}
10-
11-
pub unsafe fn cleanup() {
12-
}
8+
pub unsafe fn cleanup() {}
139

1410
pub struct Args {
1511
iter: vec::IntoIter<OsString>,
@@ -18,17 +14,24 @@ pub struct Args {
1814

1915
/// Returns the command line arguments
2016
pub fn args() -> Args {
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![]);
2917
Args {
30-
iter: buf.into_iter(),
31-
_dont_send_or_sync_me: PhantomData
18+
iter: maybe_args().unwrap_or(Vec::new()).into_iter(),
19+
_dont_send_or_sync_me: PhantomData,
20+
}
21+
}
22+
23+
fn maybe_args() -> Option<Vec<OsString>> {
24+
unsafe {
25+
let (argc, buf_size) = wasi::args_sizes_get().ok()?;
26+
let mut argv = Vec::with_capacity(argc);
27+
let mut buf = Vec::with_capacity(buf_size);
28+
wasi::args_get(argv.as_mut_ptr(), buf.as_mut_ptr()).ok()?;
29+
let mut ret = Vec::with_capacity(argc);
30+
for ptr in argv {
31+
let s = CStr::from_ptr(ptr.cast());
32+
ret.push(OsStr::from_bytes(s.to_bytes()).to_owned());
33+
}
34+
Some(ret)
3235
}
3336
}
3437

0 commit comments

Comments
 (0)