diff --git a/crates/generate/src/config.rs b/crates/generate/src/config.rs index 9e7848e82c..0941e73431 100644 --- a/crates/generate/src/config.rs +++ b/crates/generate/src/config.rs @@ -14,25 +14,32 @@ pub struct Config { pub ctx: CtxConf, } -enum ConfigField { +#[derive(Debug, Clone)] +pub enum ConfigField { Witx(WitxConf), Ctx(CtxConf), } +impl ConfigField { + pub fn parse_pair(ident: &str, value: ParseStream, err_loc: Span) -> Result { + match ident { + "witx" => Ok(ConfigField::Witx(value.parse()?)), + "ctx" => Ok(ConfigField::Ctx(value.parse()?)), + _ => Err(Error::new(err_loc, "expected `witx` or `ctx`")), + } + } +} + impl Parse for ConfigField { fn parse(input: ParseStream) -> Result { let id: Ident = input.parse()?; let _colon: Token![:] = input.parse()?; - match id.to_string().as_ref() { - "witx" => Ok(ConfigField::Witx(input.parse()?)), - "ctx" => Ok(ConfigField::Ctx(input.parse()?)), - _ => Err(Error::new(id.span(), "expected `witx` or `ctx`")), - } + Self::parse_pair(id.to_string().as_ref(), input, id.span()) } } impl Config { - fn build(fields: impl Iterator, err_loc: Span) -> Result { + pub fn build(fields: impl Iterator, err_loc: Span) -> Result { let mut witx = None; let mut ctx = None; for f in fields { diff --git a/crates/generate/src/lib.rs b/crates/generate/src/lib.rs index a84707a4da..d253f2604e 100644 --- a/crates/generate/src/lib.rs +++ b/crates/generate/src/lib.rs @@ -1,4 +1,4 @@ -mod config; +pub mod config; mod funcs; mod lifetimes; mod module_trait; diff --git a/crates/runtime/src/guest_type.rs b/crates/runtime/src/guest_type.rs index 981301da46..1a654da17e 100644 --- a/crates/runtime/src/guest_type.rs +++ b/crates/runtime/src/guest_type.rs @@ -1,7 +1,7 @@ use crate::{GuestError, GuestPtr}; use std::mem; -pub trait GuestErrorType { +pub trait GuestErrorType<'a> { type Context; fn success() -> Self; fn from_error(e: GuestError, ctx: &Self::Context) -> Self; diff --git a/crates/test/src/lib.rs b/crates/test/src/lib.rs index de4474a528..8ec8f916dd 100644 --- a/crates/test/src/lib.rs +++ b/crates/test/src/lib.rs @@ -1,5 +1,6 @@ use proptest::prelude::*; use std::cell::UnsafeCell; +use std::marker; use wiggle_runtime::GuestMemory; #[derive(Debug, Clone)] @@ -290,14 +291,18 @@ mod test { use std::cell::RefCell; use wiggle_runtime::GuestError; -pub struct WasiCtx { +// In lucet, our Ctx struct needs a lifetime, so we're using one +// on the test as well. +pub struct WasiCtx<'a> { pub guest_errors: RefCell>, + lifetime: marker::PhantomData<&'a ()>, } -impl WasiCtx { +impl<'a> WasiCtx<'a> { pub fn new() -> Self { Self { guest_errors: RefCell::new(vec![]), + lifetime: marker::PhantomData, } } } @@ -309,8 +314,8 @@ impl WasiCtx { #[macro_export] macro_rules! impl_errno { ( $errno:ty ) => { - impl wiggle_runtime::GuestErrorType for $errno { - type Context = WasiCtx; + impl<'a> wiggle_runtime::GuestErrorType<'a> for $errno { + type Context = WasiCtx<'a>; fn success() -> $errno { <$errno>::Ok } diff --git a/tests/arrays.rs b/tests/arrays.rs index 7e6e7b332f..b0dea82a54 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -9,7 +9,7 @@ wiggle::from_witx!({ impl_errno!(types::Errno); -impl arrays::Arrays for WasiCtx { +impl<'a> arrays::Arrays for WasiCtx<'a> { fn reduce_excuses( &self, excuses: &types::ConstExcuseArray, diff --git a/tests/atoms.rs b/tests/atoms.rs index 594edb13ab..d19d19c209 100644 --- a/tests/atoms.rs +++ b/tests/atoms.rs @@ -9,7 +9,7 @@ wiggle::from_witx!({ impl_errno!(types::Errno); -impl atoms::Atoms for WasiCtx { +impl<'a> atoms::Atoms for WasiCtx<'a> { fn int_float_args(&self, an_int: u32, an_float: f32) -> Result<(), types::Errno> { println!("INT FLOAT ARGS: {} {}", an_int, an_float); Ok(()) diff --git a/tests/flags.rs b/tests/flags.rs index d00c5b84f0..78939fc3d6 100644 --- a/tests/flags.rs +++ b/tests/flags.rs @@ -10,7 +10,7 @@ wiggle::from_witx!({ impl_errno!(types::Errno); -impl flags::Flags for WasiCtx { +impl<'a> flags::Flags for WasiCtx<'a> { fn configure_car( &self, old_config: types::CarConfig, diff --git a/tests/handles.rs b/tests/handles.rs index 9cb391a190..c9e44122ec 100644 --- a/tests/handles.rs +++ b/tests/handles.rs @@ -11,7 +11,7 @@ wiggle::from_witx!({ impl_errno!(types::Errno); -impl handle_examples::HandleExamples for WasiCtx { +impl<'a> handle_examples::HandleExamples for WasiCtx<'a> { fn fd_create(&self) -> Result { Ok(types::Fd::from(FD_VAL)) } diff --git a/tests/ints.rs b/tests/ints.rs index 12cb8c929c..b4adeb2969 100644 --- a/tests/ints.rs +++ b/tests/ints.rs @@ -10,7 +10,7 @@ wiggle::from_witx!({ impl_errno!(types::Errno); -impl ints::Ints for WasiCtx { +impl<'a> ints::Ints for WasiCtx<'a> { fn cookie_cutter(&self, init_cookie: types::Cookie) -> Result { let res = if init_cookie == types::Cookie::START { types::Bool::True diff --git a/tests/pointers.rs b/tests/pointers.rs index f15a53349e..188fa9822c 100644 --- a/tests/pointers.rs +++ b/tests/pointers.rs @@ -9,13 +9,13 @@ wiggle::from_witx!({ impl_errno!(types::Errno); -impl pointers::Pointers for WasiCtx { - fn pointers_and_enums<'a>( +impl<'a> pointers::Pointers for WasiCtx<'a> { + fn pointers_and_enums<'b>( &self, input1: types::Excuse, - input2_ptr: GuestPtr<'a, types::Excuse>, - input3_ptr: GuestPtr<'a, types::Excuse>, - input4_ptr_ptr: GuestPtr<'a, GuestPtr<'a, types::Excuse>>, + input2_ptr: GuestPtr<'b, types::Excuse>, + input3_ptr: GuestPtr<'b, types::Excuse>, + input4_ptr_ptr: GuestPtr<'b, GuestPtr<'b, types::Excuse>>, ) -> Result<(), types::Errno> { println!("BAZ input1 {:?}", input1); let input2: types::Excuse = input2_ptr.read().map_err(|e| { diff --git a/tests/strings.rs b/tests/strings.rs index 7cf0badf14..7fca007b4d 100644 --- a/tests/strings.rs +++ b/tests/strings.rs @@ -9,7 +9,7 @@ wiggle::from_witx!({ impl_errno!(types::Errno); -impl strings::Strings for WasiCtx { +impl<'a> strings::Strings for WasiCtx<'a> { fn hello_string(&self, a_string: &GuestPtr) -> Result { let mut bc = GuestBorrows::new(); let s = a_string.as_raw(&mut bc).expect("should be valid string"); diff --git a/tests/structs.rs b/tests/structs.rs index 69abc4b003..0b00057d9a 100644 --- a/tests/structs.rs +++ b/tests/structs.rs @@ -9,7 +9,7 @@ wiggle::from_witx!({ impl_errno!(types::Errno); -impl structs::Structs for WasiCtx { +impl<'a> structs::Structs for WasiCtx<'a> { fn sum_of_pair(&self, an_pair: &types::PairInts) -> Result { Ok(an_pair.first as i64 + an_pair.second as i64) } @@ -42,11 +42,11 @@ impl structs::Structs for WasiCtx { }) } - fn return_pair_of_ptrs<'a>( + fn return_pair_of_ptrs<'b>( &self, - first: GuestPtr<'a, i32>, - second: GuestPtr<'a, i32>, - ) -> Result, types::Errno> { + first: GuestPtr<'b, i32>, + second: GuestPtr<'b, i32>, + ) -> Result, types::Errno> { Ok(types::PairIntPtrs { first, second }) } } diff --git a/tests/union.rs b/tests/union.rs index 87f3fb5464..b2510e4ef9 100644 --- a/tests/union.rs +++ b/tests/union.rs @@ -31,7 +31,7 @@ fn mult_zero_nan(a: f32, b: u32) -> f32 { } } -impl union_example::UnionExample for WasiCtx { +impl<'a> union_example::UnionExample for WasiCtx<'a> { fn get_tag(&self, u: &types::Reason) -> Result { println!("GET TAG: {:?}", u); match u { diff --git a/tests/wasi.rs b/tests/wasi.rs index accefb7584..6ad5612b69 100644 --- a/tests/wasi.rs +++ b/tests/wasi.rs @@ -8,21 +8,21 @@ wiggle::from_witx!({ type Result = std::result::Result; -impl GuestErrorType for types::Errno { - type Context = WasiCtx; +impl<'a> GuestErrorType<'a> for types::Errno { + type Context = WasiCtx<'a>; fn success() -> types::Errno { types::Errno::Success } - fn from_error(e: GuestError, ctx: &WasiCtx) -> types::Errno { + fn from_error(e: GuestError, ctx: &Self::Context) -> types::Errno { eprintln!("GUEST ERROR: {:?}", e); ctx.guest_errors.borrow_mut().push(e); types::Errno::Io } } -impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { +impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> { fn args_get(&self, _argv: GuestPtr>, _argv_buf: GuestPtr) -> Result<()> { unimplemented!("args_get") }