Skip to content

Commit

Permalink
Merge pull request #608 from stlankes/wasmtime
Browse files Browse the repository at this point in the history
improve compatibility to WASI preview1
  • Loading branch information
stlankes authored Jul 21, 2024
2 parents 847a042 + 9b118bf commit 5a4d7ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
39 changes: 37 additions & 2 deletions examples/wasmtime/src/preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use std::cmp::Ordering;
use std::mem::MaybeUninit;
use std::sync::{Mutex, OnceLock};
use std::time::{Instant, SystemTime};
use std::time::{Instant, SystemTime, UNIX_EPOCH};

use anyhow::Result;
use bitflags::bitflags;
Expand Down Expand Up @@ -361,6 +361,10 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
filetype: REGULAR_FILE,
..Default::default()
},
Descriptor::Directory(_) => FdStat {
filetype: DIRECTORY,
..Default::default()
},
_ => {
return ERRNO_INVAL.raw() as i32;
}
Expand Down Expand Up @@ -398,6 +402,30 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
let filestat = FileStat {
filetype: REGULAR_FILE,
size: metadata.len(),
mtim: metadata
.modified()
.unwrap()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
.try_into()
.unwrap(),
atim: metadata
.accessed()
.unwrap()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
.try_into()
.unwrap(),
ctim: metadata
.created()
.unwrap()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
.try_into()
.unwrap(),
..Default::default()
};

Expand Down Expand Up @@ -573,6 +601,13 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
let mut i = 0;
while i < iovs.len() {
let len = iovs[i + 1];

// len = 0 => ignore entry nothing to write
if len == 0 {
i += 2;
continue;
}

let mut data: Vec<MaybeUninit<u8>> =
Vec::with_capacity(len.try_into().unwrap());
unsafe {
Expand All @@ -591,7 +626,7 @@ pub(crate) fn init<T>(linker: &mut wasmtime::Linker<T>) -> Result<()> {
)
};

if result > 0 {
if result >= 0 {
nwritten_bytes += result as i32;
if result < len.try_into().unwrap() {
break;
Expand Down
2 changes: 1 addition & 1 deletion kernel

0 comments on commit 5a4d7ef

Please sign in to comment.