diff --git a/crates/test/src/lib.rs b/crates/test/src/lib.rs index 8b036111a3..2ff31211c1 100644 --- a/crates/test/src/lib.rs +++ b/crates/test/src/lib.rs @@ -90,3 +90,38 @@ mod test { assert_eq!(h.buffer.as_mut_ptr() as usize % 4096, 0); } } + +use wiggle_runtime::GuestError; + +pub struct WasiCtx { + pub guest_errors: Vec, +} + +impl WasiCtx { + pub fn new() -> Self { + Self { + guest_errors: vec![], + } + } +} + +// Errno is used as a first return value in the functions above, therefore +// it must implement GuestErrorType with type Context = WasiCtx. +// The context type should let you do logging or debugging or whatever you need +// with these errors. We just push them to vecs. +#[macro_export] +macro_rules! impl_errno { + ( $errno:ty ) => { + impl wiggle_runtime::GuestErrorType for $errno { + type Context = WasiCtx; + fn success() -> $errno { + <$errno>::Ok + } + fn from_error(e: GuestError, ctx: &mut WasiCtx) -> $errno { + eprintln!("GUEST ERROR: {:?}", e); + ctx.guest_errors.push(e); + types::Errno::InvalidArg + } + } + }; +} diff --git a/tests/ctx.rs b/tests/ctx.rs deleted file mode 100644 index b521dbaa40..0000000000 --- a/tests/ctx.rs +++ /dev/null @@ -1,34 +0,0 @@ -use wiggle_runtime::GuestError; - -pub struct WasiCtx { - pub guest_errors: Vec, -} - -impl WasiCtx { - pub fn new() -> Self { - Self { - guest_errors: vec![], - } - } -} - -// Errno is used as a first return value in the functions above, therefore -// it must implement GuestErrorType with type Context = WasiCtx. -// The context type should let you do logging or debugging or whatever you need -// with these errors. We just push them to vecs. -#[macro_export] -macro_rules! impl_errno { - ( $errno:ty ) => { - impl wiggle_runtime::GuestErrorType for $errno { - type Context = WasiCtx; - fn success() -> $errno { - <$errno>::Ok - } - fn from_error(e: GuestError, ctx: &mut WasiCtx) -> $errno { - eprintln!("GUEST ERROR: {:?}", e); - ctx.guest_errors.push(e); - types::Errno::InvalidArg - } - } - }; -} diff --git a/tests/main.rs b/tests/main.rs index a8ec2c5096..43254739fb 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -3,16 +3,13 @@ use std::convert::TryFrom; use wiggle_runtime::{ GuestArray, GuestError, GuestPtr, GuestPtrMut, GuestRef, GuestRefMut, GuestString, }; -use wiggle_test::{HostMemory, MemArea}; +use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; wiggle_generate::from_witx!({ witx: ["tests/test.witx"], ctx: WasiCtx, }); -mod ctx; -use ctx::WasiCtx; - impl_errno!(types::Errno); impl foo::Foo for WasiCtx { diff --git a/tests/trivial.rs b/tests/trivial.rs index 36ca5d3ca5..de97dd8a05 100644 --- a/tests/trivial.rs +++ b/tests/trivial.rs @@ -1,9 +1,6 @@ use proptest::prelude::*; use wiggle_runtime::GuestError; -use wiggle_test::HostMemory; - -mod ctx; -use ctx::WasiCtx; +use wiggle_test::{impl_errno, HostMemory, WasiCtx}; wiggle_generate::from_witx!({ witx: ["tests/trivial.witx"],