Reduce boilerplate in wasmtime-wasi (#707)

This commit uses the `*.witx` files describing the current wasi API to
reduce the boilerplate used to define implementations in the
`wasmtime-wasi` crate. Eventually I'd like to remove lots of boilerplate
in the `wasi-common` crate too, but this should at least be a good start!

The boilerplate removed here is:

* No need to list each function to add it to the
  `wasmtime_runtime::Module` being created

* No need to list the signature of the function in a separate
  `syscalls.rs` file.

Instead the `*.witx` file is processed in a single-use macro inside the
`wasmtime-wasi` crate. This macro uses the signatures known from
`*.witx` to automatically register with the right type in the wasm
module as well as define a wrapper that the wasm module will call into.
Functionally this is all the same as before, it's just defined in a
different way now!

The shim generated by this macro which wasmtime calls into only uses
`i32`/`i64`/etc wasm types, and it internally uses `as` casts to convert
to the right wasi types when delegating into the `wasi-common` crate.

One change was necessary to get this implemented, however. The functions
in `wasi-common` sometimes took `WasiCtx` and sometimes took a slice of
memory. After this PR they uniformly all require both `WasiCtx` and
memory so the wrappers can be auto-generated. The arguments are ignored
if they weren't previously required.
This commit is contained in:
Alex Crichton
2019-12-16 16:37:20 -06:00
committed by GitHub
parent c2ba419409
commit cc4be18119
13 changed files with 343 additions and 1067 deletions

View File

@@ -199,7 +199,7 @@ impl<'ctx> Drop for Dir<'ctx> {
// the file descriptor was closed or not, and if we retried (for
// something like EINTR), we might close another valid file descriptor
// opened after we closed ours.
let _ = unsafe { hostcalls::fd_close(self.ctx, self.fd) };
let _ = unsafe { hostcalls::fd_close(self.ctx, &mut [], self.fd) };
}
}

View File

@@ -35,7 +35,7 @@ impl<'ctx> File<'ctx> {
///
/// [`std::fs::File::sync_all`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.sync_all
pub fn sync_all(&self) -> io::Result<()> {
wasi_errno_to_io_error(unsafe { hostcalls::fd_sync(self.ctx, self.fd) })
wasi_errno_to_io_error(unsafe { hostcalls::fd_sync(self.ctx, &mut [], self.fd) })
}
/// This function is similar to `sync_all`, except that it may not synchronize
@@ -45,7 +45,7 @@ impl<'ctx> File<'ctx> {
///
/// [`std::fs::File::sync_data`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.sync_data
pub fn sync_data(&self) -> io::Result<()> {
wasi_errno_to_io_error(unsafe { hostcalls::fd_datasync(self.ctx, self.fd) })
wasi_errno_to_io_error(unsafe { hostcalls::fd_datasync(self.ctx, &mut [], self.fd) })
}
/// Truncates or extends the underlying file, updating the size of this file
@@ -55,7 +55,9 @@ impl<'ctx> File<'ctx> {
///
/// [`std::fs::File::set_len`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.set_len
pub fn set_len(&self, size: u64) -> io::Result<()> {
wasi_errno_to_io_error(unsafe { hostcalls::fd_filestat_set_size(self.ctx, self.fd, size) })
wasi_errno_to_io_error(unsafe {
hostcalls::fd_filestat_set_size(self.ctx, &mut [], self.fd, size)
})
}
/// Queries metadata about the underlying file.
@@ -75,7 +77,7 @@ impl<'ctx> Drop for File<'ctx> {
// the file descriptor was closed or not, and if we retried (for
// something like EINTR), we might close another valid file descriptor
// opened after we closed ours.
let _ = unsafe { hostcalls::fd_close(self.ctx, self.fd) };
let _ = unsafe { hostcalls::fd_close(self.ctx, &mut [], self.fd) };
}
}