Skip to content

Commit

Permalink
Rollup merge of rust-lang#66750 - alexcrichton:update-wasi, r=sfackler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Centril authored Dec 3, 2019
2 parents f577b0e + f3fb1c5 commit 1b83dcf
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 364 deletions.
10 changes: 8 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ checksum = "473a1265acc8ff1e808cd0a1af8cee3c2ee5200916058a2ca113c29f2d903571"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.7.0",
]

[[package]]
Expand Down Expand Up @@ -4301,7 +4301,7 @@ dependencies = [
"rustc_msan",
"rustc_tsan",
"unwind",
"wasi",
"wasi 0.9.0+wasi-snapshot-preview1",
]

[[package]]
Expand Down Expand Up @@ -5172,6 +5172,12 @@ name = "wasi"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d"

[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
dependencies = [
"compiler_builtins",
"rustc-std-workspace-alloc",
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }
hermit-abi = { version = "0.1", features = ['rustc-dep-of-std'] }

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

[features]
default = ["std_detect_file_io", "std_detect_dlsym_getauxval"]
Expand Down
39 changes: 21 additions & 18 deletions src/libstd/sys/wasi/args.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use crate::ffi::OsString;
use crate::ffi::{CStr, OsStr, OsString};
use crate::marker::PhantomData;
use crate::os::wasi::ffi::OsStringExt;
use crate::os::wasi::ffi::OsStrExt;
use crate::vec;

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

pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
}

pub unsafe fn cleanup() {
}
pub unsafe fn cleanup() {}

pub struct Args {
iter: vec::IntoIter<OsString>,
Expand All @@ -18,17 +14,24 @@ pub struct Args {

/// Returns the command line arguments
pub fn args() -> Args {
let buf = wasi::args_sizes_get().and_then(|args_sizes| {
let mut buf = Vec::with_capacity(args_sizes.get_count());
wasi::args_get(args_sizes, |arg| {
let arg = OsString::from_vec(arg.to_vec());
buf.push(arg);
})?;
Ok(buf)
}).unwrap_or(vec![]);
Args {
iter: buf.into_iter(),
_dont_send_or_sync_me: PhantomData
iter: maybe_args().unwrap_or(Vec::new()).into_iter(),
_dont_send_or_sync_me: PhantomData,
}
}

fn maybe_args() -> Option<Vec<OsString>> {
unsafe {
let (argc, buf_size) = wasi::args_sizes_get().ok()?;
let mut argv = Vec::with_capacity(argc);
let mut buf = Vec::with_capacity(buf_size);
wasi::args_get(argv.as_mut_ptr(), buf.as_mut_ptr()).ok()?;
let mut ret = Vec::with_capacity(argc);
for ptr in argv {
let s = CStr::from_ptr(ptr.cast());
ret.push(OsStr::from_bytes(s.to_bytes()).to_owned());
}
Some(ret)
}
}

Expand Down
Loading

0 comments on commit 1b83dcf

Please sign in to comment.