Skip to content

Commit

Permalink
Rollup merge of rust-lang#63676 - newpavlov:wasi, r=alexcrichton
Browse files Browse the repository at this point in the history
Use wasi crate for Core API

Blocked by: bytecodealliance/wasi-rs#5

Blocks: rust-lang/libc#1461

cc @sunfishcode @alexcrichton
  • Loading branch information
Centril authored Sep 6, 2019
2 parents 6b5f9b2 + 0662fcf commit 5b99539
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 434 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3870,6 +3870,7 @@ dependencies = [
"rustc_msan",
"rustc_tsan",
"unwind",
"wasi",
]

[[package]]
Expand Down Expand Up @@ -4686,6 +4687,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

0 comments on commit 5b99539

Please sign in to comment.