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:
@@ -14,7 +14,11 @@ use std::fs::File;
|
||||
use std::io::{self, Read, Seek, SeekFrom, Write};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
|
||||
pub(crate) unsafe fn fd_close(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
) -> Result<()> {
|
||||
trace!("fd_close(fd={:?})", fd);
|
||||
|
||||
if let Ok(fe) = wasi_ctx.get_fd_entry(fd) {
|
||||
@@ -28,7 +32,11 @@ pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t) ->
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
|
||||
pub(crate) unsafe fn fd_datasync(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
) -> Result<()> {
|
||||
trace!("fd_datasync(fd={:?})", fd);
|
||||
|
||||
let fd = wasi_ctx
|
||||
@@ -173,6 +181,7 @@ pub(crate) unsafe fn fd_read(
|
||||
|
||||
pub(crate) unsafe fn fd_renumber(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
from: wasi::__wasi_fd_t,
|
||||
to: wasi::__wasi_fd_t,
|
||||
) -> Result<()> {
|
||||
@@ -289,6 +298,7 @@ pub(crate) unsafe fn fd_fdstat_get(
|
||||
|
||||
pub(crate) unsafe fn fd_fdstat_set_flags(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> Result<()> {
|
||||
@@ -304,6 +314,7 @@ pub(crate) unsafe fn fd_fdstat_set_flags(
|
||||
|
||||
pub(crate) unsafe fn fd_fdstat_set_rights(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
fs_rights_base: wasi::__wasi_rights_t,
|
||||
fs_rights_inheriting: wasi::__wasi_rights_t,
|
||||
@@ -327,7 +338,11 @@ pub(crate) unsafe fn fd_fdstat_set_rights(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn fd_sync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
|
||||
pub(crate) unsafe fn fd_sync(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
) -> Result<()> {
|
||||
trace!("fd_sync(fd={:?})", fd);
|
||||
|
||||
let fd = wasi_ctx
|
||||
@@ -381,6 +396,7 @@ pub(crate) unsafe fn fd_write(
|
||||
|
||||
pub(crate) unsafe fn fd_advise(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
len: wasi::__wasi_filesize_t,
|
||||
@@ -404,6 +420,7 @@ pub(crate) unsafe fn fd_advise(
|
||||
|
||||
pub(crate) unsafe fn fd_allocate(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
len: wasi::__wasi_filesize_t,
|
||||
@@ -687,6 +704,7 @@ pub(crate) unsafe fn fd_filestat_get(
|
||||
|
||||
pub(crate) unsafe fn fd_filestat_set_times(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
st_atim: wasi::__wasi_timestamp_t,
|
||||
st_mtim: wasi::__wasi_timestamp_t,
|
||||
@@ -746,6 +764,7 @@ pub(crate) fn fd_filestat_set_times_impl(
|
||||
|
||||
pub(crate) unsafe fn fd_filestat_set_size(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
st_size: wasi::__wasi_filesize_t,
|
||||
) -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user