Skip to content

Commit

Permalink
use wasi crate
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 19, 2019
1 parent bd1da18 commit 6896ed3
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 376 deletions.
16 changes: 13 additions & 3 deletions src/libstd/sys/wasi/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,24 @@ pub fn args() -> Args {
})
}

fn cvt_wasi(r: u16) -> crate::io::Result<()> {
if r != 0 {
Err(Error::from_raw_os_error(r as i32))
} else {
Ok(())
}
}

fn maybe_args() -> io::Result<Args> {
// FIXME: replace with safe functions
use wasi::wasi_unstable::raw::{__wasi_args_sizes_get, __wasi_args_get};
unsafe {
let (mut argc, mut argv_buf_size) = (0, 0);
cvt_wasi(libc::__wasi_args_sizes_get(&mut argc, &mut argv_buf_size))?;
cvt_wasi(__wasi_args_sizes_get(&mut argc, &mut argv_buf_size))?;

let mut argc = vec![core::ptr::null_mut::<libc::c_char>(); argc];
let mut argc = vec![core::ptr::null_mut::<u8>(); 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()))?;
cvt_wasi(__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())
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/sys/wasi/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,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 6896ed3

Please sign in to comment.