Skip to content

Commit

Permalink
Fixed linter
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Jun 15, 2022
1 parent 9501ca9 commit 416cc3f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 69 deletions.
21 changes: 10 additions & 11 deletions lib/c-api/src/wasm_c_api/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::os::raw::c_char;
use std::slice;
use wasmer_api::{Exportable, Extern};
use wasmer_wasi::{
generate_import_object_from_env, get_wasi_version, WasiEnv, WasiFile, WasiState,
WasiStateBuilder, WasiVersion, Pipe,
generate_import_object_from_env, get_wasi_version, Pipe, WasiEnv, WasiFile, WasiState,
WasiStateBuilder, WasiVersion,
};

#[derive(Debug)]
Expand Down Expand Up @@ -172,15 +172,11 @@ pub struct wasi_env_t {
#[no_mangle]
pub extern "C" fn wasi_env_new(mut config: Box<wasi_config_t>) -> Option<Box<wasi_env_t>> {
if !config.inherit_stdout {
config
.state_builder
.stdout(Box::new(Pipe::new()));
config.state_builder.stdout(Box::new(Pipe::new()));
}

if !config.inherit_stderr {
config
.state_builder
.stderr(Box::new(Pipe::new()));
config.state_builder.stderr(Box::new(Pipe::new()));
}

// TODO: impl capturer for stdin
Expand Down Expand Up @@ -236,15 +232,18 @@ pub unsafe extern "C" fn wasi_env_read_stderr(
} else {
update_last_error("could not find a file handle for `stderr`");
return -1;
}
}
}

fn read_inner(wasi_file: &mut Box<dyn WasiFile + Send + Sync + 'static>, inner_buffer: &mut [u8]) -> isize {
fn read_inner(
wasi_file: &mut Box<dyn WasiFile + Send + Sync + 'static>,
inner_buffer: &mut [u8],
) -> isize {
match wasi_file.read(inner_buffer) {
Ok(a) => a as isize,
Err(err) => {
update_last_error(format!("failed to read wasi_file: {}", err));
-1
-1
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/vfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ impl OpenOptions {
self
}

pub fn open<P: AsRef<Path>>(&mut self, path: P) -> Result<Box<dyn VirtualFile + Send + Sync + 'static>> {
pub fn open<P: AsRef<Path>>(
&mut self,
path: P,
) -> Result<Box<dyn VirtualFile + Send + Sync + 'static>> {
self.opener.open(path.as_ref(), &self.conf)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use std::ops::Deref;
use thiserror::Error;
use wasmer::{
imports, Function, Imports, LazyInit, Memory, Memory32, MemoryAccessError, MemorySize, Module,
Store, WasmerEnv, TypedFunction,
Store, TypedFunction, WasmerEnv,
};

pub use runtime::{
Expand Down
53 changes: 22 additions & 31 deletions lib/wasi/src/state/guard.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{sync::{
RwLockReadGuard, RwLockWriteGuard
}, io::{Seek, Read}};
use super::*;
use std::{
io::{Read, Seek},
sync::{RwLockReadGuard, RwLockWriteGuard},
};

#[derive(Debug)]
pub(crate) struct InodeValFileReadGuard<'a> {
Expand Down Expand Up @@ -48,34 +49,30 @@ pub(crate) struct WasiStateFileGuard {
inode: generational_arena::Index,
}

impl WasiStateFileGuard
{
impl WasiStateFileGuard {
pub fn new(state: &WasiState, fd: __wasi_fd_t) -> Result<Option<Self>, FsError> {
let inodes = state.inodes.read().unwrap();
let fd_map = state.fs.fd_map.read().unwrap();
if let Some(fd) = fd_map.get(&fd) {
let guard = inodes.arena[fd.inode].read();
if let Kind::File { .. } = guard.deref() {
Ok(
Some(
Self {
inodes: state.inodes.clone(),
inode: fd.inode
}
)
)
Ok(Some(Self {
inodes: state.inodes.clone(),
inode: fd.inode,
}))
} else {
// Our public API should ensure that this is not possible
Err(FsError::NotAFile)
}

} else {
Ok(None)
}
}

pub fn lock_read<'a>(&self, inodes: &'a RwLockReadGuard<WasiInodes>) -> InodeValFileReadGuard<'a>
{
pub fn lock_read<'a>(
&self,
inodes: &'a RwLockReadGuard<WasiInodes>,
) -> InodeValFileReadGuard<'a> {
let guard = inodes.arena[self.inode].read();
if let Kind::File { .. } = guard.deref() {
InodeValFileReadGuard { guard }
Expand All @@ -85,8 +82,10 @@ impl WasiStateFileGuard
}
}

pub fn lock_write<'a>(&self, inodes: &'a RwLockReadGuard<WasiInodes>) -> InodeValFileWriteGuard<'a>
{
pub fn lock_write<'a>(
&self,
inodes: &'a RwLockReadGuard<WasiInodes>,
) -> InodeValFileWriteGuard<'a> {
let guard = inodes.arena[self.inode].write();
if let Kind::File { .. } = guard.deref() {
InodeValFileWriteGuard { guard }
Expand All @@ -97,9 +96,7 @@ impl WasiStateFileGuard
}
}

impl VirtualFile
for WasiStateFileGuard
{
impl VirtualFile for WasiStateFileGuard {
fn last_accessed(&self) -> u64 {
let inodes = self.inodes.read().unwrap();
let guard = self.lock_read(&inodes);
Expand Down Expand Up @@ -221,9 +218,7 @@ for WasiStateFileGuard
}
}

impl Write
for WasiStateFileGuard
{
impl Write for WasiStateFileGuard {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let inodes = self.inodes.read().unwrap();
let mut guard = self.lock_write(&inodes);
Expand Down Expand Up @@ -255,9 +250,7 @@ for WasiStateFileGuard
}
}

impl Read
for WasiStateFileGuard
{
impl Read for WasiStateFileGuard {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let inodes = self.inodes.read().unwrap();
let mut guard = self.lock_write(&inodes);
Expand All @@ -279,9 +272,7 @@ for WasiStateFileGuard
}
}

impl Seek
for WasiStateFileGuard
{
impl Seek for WasiStateFileGuard {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
let inodes = self.inodes.read().unwrap();
let mut guard = self.lock_write(&inodes);
Expand All @@ -291,4 +282,4 @@ for WasiStateFileGuard
Err(std::io::ErrorKind::Unsupported.into())
}
}
}
}
37 changes: 14 additions & 23 deletions lib/wasi/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
#![allow(clippy::cognitive_complexity, clippy::too_many_arguments)]

mod builder;
mod guard;
mod pipe;
mod socket;
mod types;
mod guard;

pub use self::builder::*;
pub use self::guard::*;
pub use self::pipe::*;
pub use self::socket::*;
pub use self::types::*;
pub use self::guard::*;
use crate::syscalls::types::*;
use crate::utils::map_io_err;
use crate::WasiBusProcessId;
Expand Down Expand Up @@ -1872,9 +1872,7 @@ impl WasiState {
}

/// Get the `VirtualFile` object at stdout
pub fn stdout(
&self,
) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
pub fn stdout(&self) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
self.std_dev_get(__WASI_STDOUT_FILENO)
}

Expand All @@ -1883,15 +1881,13 @@ impl WasiState {
note = "stdout_mut() is no longer needed - just use stdout() instead"
)]
pub fn stdout_mut(
&self
&self,
) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
self.stdout()
}

/// Get the `VirtualFile` object at stderr
pub fn stderr(
&self,
) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
pub fn stderr(&self) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
self.std_dev_get(__WASI_STDERR_FILENO)
}

Expand All @@ -1900,15 +1896,13 @@ impl WasiState {
note = "stderr_mut() is no longer needed - just use stderr() instead"
)]
pub fn stderr_mut(
&self
&self,
) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
self.stderr()
}

/// Get the `VirtualFile` object at stdin
pub fn stdin(
&self,
) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
pub fn stdin(&self) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
self.std_dev_get(__WASI_STDIN_FILENO)
}

Expand All @@ -1917,7 +1911,7 @@ impl WasiState {
note = "stdin_mut() is no longer needed - just use stdin() instead"
)]
pub fn stdin_mut(
&self
&self,
) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
self.stdin()
}
Expand All @@ -1928,15 +1922,12 @@ impl WasiState {
&self,
fd: __wasi_fd_t,
) -> Result<Option<Box<dyn VirtualFile + Send + Sync + 'static>>, FsError> {
let ret = WasiStateFileGuard::new(self, fd)?
.map(|a| {
let ret = Box::new(a);
let ret: Box<dyn VirtualFile + Send + Sync + 'static> = ret;
ret
});
Ok(
let ret = WasiStateFileGuard::new(self, fd)?.map(|a| {
let ret = Box::new(a);
let ret: Box<dyn VirtualFile + Send + Sync + 'static> = ret;
ret
)
});
Ok(ret)
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/wasi/tests/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod sys {
#[cfg(feature = "js")]
mod js {
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn test_stdout() {
super::test_stdout()
Expand Down Expand Up @@ -162,7 +162,7 @@ fn test_stdin() {
let start = instance.exports.get_function("_start").unwrap();
let result = start.call(&[]);
assert!(result.is_err() == false);

// We assure stdin is now empty
let mut buf = Vec::new();
stdin.read_to_end(&mut buf).unwrap();
Expand Down

0 comments on commit 416cc3f

Please sign in to comment.