Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use wasi crate for Core API #63676

Merged
merged 23 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3866,6 +3866,7 @@ dependencies = [
"rustc_msan",
"rustc_tsan",
"unwind",
"wasi",
]

[[package]]
Expand Down Expand Up @@ -4664,6 +4665,17 @@ dependencies = [
"try-lock",
]

[[package]]
name = "wasi"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d"
dependencies = [
"compiler_builtins",
"rustc-std-workspace-alloc",
"rustc-std-workspace-core",
]

[[package]]
name = "winapi"
version = "0.2.8"
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ dlmalloc = { version = "0.1", features = ['rustc-dep-of-std'] }
[target.x86_64-fortanix-unknown-sgx.dependencies]
fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }

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

[build-dependencies]
cc = "1.0"

Expand Down
41 changes: 13 additions & 28 deletions src/libstd/sys/wasi/args.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::ffi::CStr;
use crate::io;
use crate::sys::cvt_wasi;
use crate::ffi::OsString;
use crate::marker::PhantomData;
use crate::os::wasi::ffi::OsStringExt;
use crate::vec;

use ::wasi::wasi_unstable as wasi;

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

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

/// Returns the command line arguments
pub fn args() -> Args {
maybe_args().unwrap_or_else(|_| {
Args {
iter: Vec::new().into_iter(),
_dont_send_or_sync_me: PhantomData
}
})
}

fn maybe_args() -> io::Result<Args> {
unsafe {
let (mut argc, mut argv_buf_size) = (0, 0);
cvt_wasi(libc::__wasi_args_sizes_get(&mut argc, &mut argv_buf_size))?;

let mut argc = vec![core::ptr::null_mut::<libc::c_char>(); argc];
let mut argv_buf = vec![0; argv_buf_size];
cvt_wasi(libc::__wasi_args_get(argc.as_mut_ptr(), argv_buf.as_mut_ptr()))?;

let args = argc.into_iter()
.map(|ptr| CStr::from_ptr(ptr).to_bytes().to_vec())
.map(|bytes| OsString::from_vec(bytes))
.collect::<Vec<_>>();
Ok(Args {
iter: args.into_iter(),
_dont_send_or_sync_me: PhantomData,
})
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
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/libstd/sys/wasi/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::os::wasi::ffi::OsStrExt;
use crate::path::{Path, PathBuf};
use crate::sys_common::{AsInner, AsInnerMut, FromInner};

use ::wasi::wasi_unstable as wasi;

/// WASI-specific extensions to [`File`].
///
/// [`File`]: ../../../../std/fs/struct.File.html
Expand Down Expand Up @@ -336,16 +338,16 @@ pub trait FileTypeExt {

impl FileTypeExt for fs::FileType {
fn is_block_device(&self) -> bool {
self.as_inner().bits() == libc::__WASI_FILETYPE_BLOCK_DEVICE
self.as_inner().bits() == wasi::FILETYPE_BLOCK_DEVICE
}
fn is_character_device(&self) -> bool {
self.as_inner().bits() == libc::__WASI_FILETYPE_CHARACTER_DEVICE
self.as_inner().bits() == wasi::FILETYPE_CHARACTER_DEVICE
}
fn is_socket_dgram(&self) -> bool {
self.as_inner().bits() == libc::__WASI_FILETYPE_SOCKET_DGRAM
self.as_inner().bits() == wasi::FILETYPE_SOCKET_DGRAM
}
fn is_socket_stream(&self) -> bool {
self.as_inner().bits() == libc::__WASI_FILETYPE_SOCKET_STREAM
self.as_inner().bits() == wasi::FILETYPE_SOCKET_STREAM
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/libstd/sys/wasi/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::sys;
use crate::net;
use crate::sys_common::{AsInner, FromInner, IntoInner};

use ::wasi::wasi_unstable as wasi;

/// Raw file descriptors.
pub type RawFd = u32;

Expand Down Expand Up @@ -125,18 +127,18 @@ impl IntoRawFd for fs::File {

impl AsRawFd for io::Stdin {
fn as_raw_fd(&self) -> RawFd {
libc::STDIN_FILENO as u32
wasi::STDIN_FD
}
}

impl AsRawFd for io::Stdout {
fn as_raw_fd(&self) -> RawFd {
libc::STDOUT_FILENO as u32
wasi::STDOUT_FD
}
}

impl AsRawFd for io::Stderr {
fn as_raw_fd(&self) -> RawFd {
libc::STDERR_FILENO as u32
wasi::STDERR_FD
}
}
Loading