diff --git a/Cargo.toml b/Cargo.toml index f71705b021..1d65b86102 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "wiggle" version = "0.1.0" -authors = ["Jakub Konka "] +authors = ["Pat Hickey ", "Jakub Konka "] edition = "2018" [dependencies] -generate = { path = "crates/generate" } -memory = { path = "crates/memory" } +wiggle-generate = { path = "crates/generate" } +wiggle-runtime = { path = "crates/runtime" } [dev-dependencies] proptest = "0.9" @@ -14,6 +14,6 @@ proptest = "0.9" [workspace] members = [ "crates/generate", - "crates/memory" + "crates/runtime" ] exclude = ["crates/WASI"] diff --git a/crates/generate/Cargo.toml b/crates/generate/Cargo.toml index 886fea8b3b..9926ca7f80 100644 --- a/crates/generate/Cargo.toml +++ b/crates/generate/Cargo.toml @@ -1,14 +1,14 @@ [package] -name = "generate" +name = "wiggle-generate" version = "0.1.0" -authors = ["Jakub Konka "] +authors = ["Pat Hickey ", "Jakub Konka "] edition = "2018" [lib] proc-macro = true [dependencies] -memory = { path = "../memory" } +wiggle-runtime = { path = "../runtime" } witx = { path = "../WASI/tools/witx" } quote = "1.0" proc-macro2 = "1.0" diff --git a/crates/generate/src/funcs.rs b/crates/generate/src/funcs.rs index 336199d222..7c8ef19b90 100644 --- a/crates/generate/src/funcs.rs +++ b/crates/generate/src/funcs.rs @@ -30,7 +30,7 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream { }); let abi_args = quote!( - ctx: &mut #ctx_type, memory: &mut ::memory::GuestMemory, + ctx: &mut #ctx_type, memory: &mut wiggle_runtime::GuestMemory, #(#params),* ); let abi_ret = if let Some(ret) = &coretype.ret { @@ -62,8 +62,8 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream { }; let err_typename = names.type_ref(&tref, anon_lifetime()); quote! { - let e = ::memory::GuestError::InFunc { funcname: #funcname, location: #location, err: Box::new(e) }; - let err: #err_typename = ::memory::GuestErrorType::from_error(e, ctx); + let e = wiggle_runtime::GuestError::InFunc { funcname: #funcname, location: #location, err: Box::new(e) }; + let err: #err_typename = wiggle_runtime::GuestErrorType::from_error(e, ctx); return #abi_ret::from(err); } } else { @@ -111,7 +111,7 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream { let success = if let Some(ref err_type) = err_type { let err_typename = names.type_ref(&err_type, anon_lifetime()); quote! { - let success:#err_typename = ::memory::GuestErrorType::success(); + let success:#err_typename = wiggle_runtime::GuestErrorType::success(); #abi_ret::from(success) } } else { diff --git a/crates/generate/src/names.rs b/crates/generate/src/names.rs index 3759113218..504f9ab67b 100644 --- a/crates/generate/src/names.rs +++ b/crates/generate/src/names.rs @@ -62,11 +62,11 @@ impl Names { witx::Type::Builtin(builtin) => self.builtin_type(*builtin), witx::Type::Pointer(pointee) => { let pointee_type = self.type_ref(&pointee, lifetime.clone()); - quote!(::memory::GuestPtrMut<#lifetime, #pointee_type>) + quote!(wiggle_runtime::GuestPtrMut<#lifetime, #pointee_type>) } witx::Type::ConstPointer(pointee) => { let pointee_type = self.type_ref(&pointee, lifetime.clone()); - quote!(::memory::GuestPtr<#lifetime, #pointee_type>) + quote!(wiggle_runtime::GuestPtr<#lifetime, #pointee_type>) } _ => unimplemented!("anonymous type ref"), }, diff --git a/crates/generate/src/types.rs b/crates/generate/src/types.rs index f5bfd42bd1..10e34894db 100644 --- a/crates/generate/src/types.rs +++ b/crates/generate/src/types.rs @@ -21,11 +21,14 @@ pub fn define_datatype(names: &Names, namedtype: &witx::NamedType) -> TokenStrea witx::Type::Union(_) => unimplemented!("union types"), witx::Type::Handle(_h) => unimplemented!("handle types"), witx::Type::Builtin(b) => define_builtin(names, &namedtype.name, *b), - witx::Type::Pointer(p) => { - define_witx_pointer(names, &namedtype.name, quote!(::memory::GuestPtrMut), p) - } + witx::Type::Pointer(p) => define_witx_pointer( + names, + &namedtype.name, + quote!(wiggle_runtime::GuestPtrMut), + p, + ), witx::Type::ConstPointer(p) => { - define_witx_pointer(names, &namedtype.name, quote!(::memory::GuestPtr), p) + define_witx_pointer(names, &namedtype.name, quote!(wiggle_runtime::GuestPtr), p) } witx::Type::Array { .. } => unimplemented!("array types"), }, @@ -69,18 +72,18 @@ fn define_enum(names: &Names, name: &witx::Id, e: &witx::EnumDatatype) -> TokenS } impl ::std::convert::TryFrom<#repr> for #ident { - type Error = ::memory::GuestError; - fn try_from(value: #repr) -> Result<#ident, ::memory::GuestError> { + type Error = wiggle_runtime::GuestError; + fn try_from(value: #repr) -> Result<#ident, wiggle_runtime::GuestError> { match value as usize { #(#tryfrom_repr_cases),*, - _ => Err(::memory::GuestError::InvalidEnumValue(stringify!(#ident))), + _ => Err(wiggle_runtime::GuestError::InvalidEnumValue(stringify!(#ident))), } } } impl ::std::convert::TryFrom<#abi_repr> for #ident { - type Error = ::memory::GuestError; - fn try_from(value: #abi_repr) -> Result<#ident, ::memory::GuestError> { + type Error = wiggle_runtime::GuestError; + fn try_from(value: #abi_repr) -> Result<#ident, wiggle_runtime::GuestError> { #ident::try_from(value as #repr) } } @@ -99,7 +102,7 @@ fn define_enum(names: &Names, name: &witx::Id, e: &witx::EnumDatatype) -> TokenS } } - impl ::memory::GuestType for #ident { + impl wiggle_runtime::GuestType for #ident { fn size() -> u32 { ::std::mem::size_of::<#repr>() as u32 } @@ -109,7 +112,7 @@ fn define_enum(names: &Names, name: &witx::Id, e: &witx::EnumDatatype) -> TokenS fn name() -> String { stringify!(#ident).to_owned() } - fn validate<'a>(location: &::memory::GuestPtr<'a, #ident>) -> Result<(), ::memory::GuestError> { + fn validate<'a>(location: &wiggle_runtime::GuestPtr<'a, #ident>) -> Result<(), wiggle_runtime::GuestError> { use ::std::convert::TryFrom; let raw: #repr = unsafe { (location.as_raw() as *const #repr).read() }; let _ = #ident::try_from(raw)?; @@ -117,15 +120,15 @@ fn define_enum(names: &Names, name: &witx::Id, e: &witx::EnumDatatype) -> TokenS } } - impl ::memory::GuestTypeCopy for #ident {} - impl ::memory::GuestTypeClone for #ident { - fn read_from_guest(location: &::memory::GuestPtr<#ident>) -> Result<#ident, ::memory::GuestError> { + impl wiggle_runtime::GuestTypeCopy for #ident {} + impl wiggle_runtime::GuestTypeClone for #ident { + fn read_from_guest(location: &wiggle_runtime::GuestPtr<#ident>) -> Result<#ident, wiggle_runtime::GuestError> { use ::std::convert::TryFrom; let raw: #repr = unsafe { (location.as_raw() as *const #repr).read() }; let val = #ident::try_from(raw)?; Ok(val) } - fn write_to_guest(&self, location: &::memory::GuestPtrMut<#ident>) { + fn write_to_guest(&self, location: &wiggle_runtime::GuestPtrMut<#ident>) { let val: #repr = #repr::from(*self); unsafe { (location.as_raw() as *mut #repr).write(val) }; } @@ -192,13 +195,13 @@ fn define_copy_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) quote! { #type_::validate( &ptr.cast(#offset).map_err(|e| - ::memory::GuestError::InDataField{ + wiggle_runtime::GuestError::InDataField{ typename: stringify!(#ident).to_owned(), field: stringify!(#fieldname).to_owned(), err: Box::new(e), })? ).map_err(|e| - ::memory::GuestError::InDataField { + wiggle_runtime::GuestError::InDataField { typename: stringify!(#ident).to_owned(), field: stringify!(#fieldname).to_owned(), err: Box::new(e), @@ -213,7 +216,7 @@ fn define_copy_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) #(#member_decls),* } - impl ::memory::GuestType for #ident { + impl wiggle_runtime::GuestType for #ident { fn size() -> u32 { #size } @@ -223,12 +226,12 @@ fn define_copy_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) fn name() -> String { stringify!(#ident).to_owned() } - fn validate(ptr: &::memory::GuestPtr<#ident>) -> Result<(), ::memory::GuestError> { + fn validate(ptr: &wiggle_runtime::GuestPtr<#ident>) -> Result<(), wiggle_runtime::GuestError> { #(#member_valids)* Ok(()) } } - impl ::memory::GuestTypeCopy for #ident {} + impl wiggle_runtime::GuestTypeCopy for #ident {} } } @@ -246,11 +249,11 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) - witx::Type::Builtin(builtin) => names.builtin_type(*builtin), witx::Type::Pointer(pointee) => { let pointee_type = names.type_ref(&pointee, quote!('a)); - quote!(::memory::GuestPtrMut<'a, #pointee_type>) + quote!(wiggle_runtime::GuestPtrMut<'a, #pointee_type>) } witx::Type::ConstPointer(pointee) => { let pointee_type = names.type_ref(&pointee, quote!('a)); - quote!(::memory::GuestPtr<'a, #pointee_type>) + quote!(wiggle_runtime::GuestPtr<'a, #pointee_type>) } _ => unimplemented!("other anonymous struct members"), }, @@ -264,11 +267,11 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) - witx::Type::Builtin(builtin) => names.builtin_type(*builtin), witx::Type::Pointer(pointee) => { let pointee_type = names.type_ref(&pointee, anon_lifetime()); - quote!(::memory::GuestPtrMut::<#pointee_type>) + quote!(wiggle_runtime::GuestPtrMut::<#pointee_type>) } witx::Type::ConstPointer(pointee) => { let pointee_type = names.type_ref(&pointee, anon_lifetime()); - quote!(::memory::GuestPtr::<#pointee_type>) + quote!(wiggle_runtime::GuestPtr::<#pointee_type>) } _ => unimplemented!("other anonymous struct members"), }, @@ -278,13 +281,13 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) - quote! { #type_::validate( &ptr.cast(#offset).map_err(|e| - ::memory::GuestError::InDataField{ + wiggle_runtime::GuestError::InDataField{ typename: stringify!(#ident).to_owned(), field: stringify!(#fieldname).to_owned(), err: Box::new(e), })? ).map_err(|e| - ::memory::GuestError::InDataField { + wiggle_runtime::GuestError::InDataField { typename: stringify!(#ident).to_owned(), field: stringify!(#fieldname).to_owned(), err: Box::new(e), @@ -312,13 +315,13 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) - witx::Type::Pointer(pointee) => { let pointee_type = names.type_ref(&pointee, anon_lifetime()); quote! { - let #name = ::memory::GuestPtrMut::<#pointee_type>::read_from_guest(&location.cast(#offset)?)?; + let #name = wiggle_runtime::GuestPtrMut::<#pointee_type>::read_from_guest(&location.cast(#offset)?)?; } } witx::Type::ConstPointer(pointee) => { let pointee_type = names.type_ref(&pointee, anon_lifetime()); quote! { - let #name = ::memory::GuestPtr::<#pointee_type>::read_from_guest(&location.cast(#offset)?)?; + let #name = wiggle_runtime::GuestPtr::<#pointee_type>::read_from_guest(&location.cast(#offset)?)?; } } _ => unimplemented!("other anonymous struct members"), @@ -338,7 +341,7 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) - #(#member_decls),* } - impl<'a> ::memory::GuestType for #ident<'a> { + impl<'a> wiggle_runtime::GuestType for #ident<'a> { fn size() -> u32 { #size } @@ -348,17 +351,17 @@ fn define_ptr_struct(names: &Names, name: &witx::Id, s: &witx::StructDatatype) - fn name() -> String { stringify!(#ident).to_owned() } - fn validate(ptr: &::memory::GuestPtr<#ident>) -> Result<(), ::memory::GuestError> { + fn validate(ptr: &wiggle_runtime::GuestPtr<#ident>) -> Result<(), wiggle_runtime::GuestError> { #(#member_valids)* Ok(()) } } - impl<'a> ::memory::GuestTypePtr<'a> for #ident<'a> { - fn read_from_guest(location: &::memory::GuestPtr<'a, #ident<'a>>) -> Result<#ident<'a>, ::memory::GuestError> { + impl<'a> wiggle_runtime::GuestTypePtr<'a> for #ident<'a> { + fn read_from_guest(location: &wiggle_runtime::GuestPtr<'a, #ident<'a>>) -> Result<#ident<'a>, wiggle_runtime::GuestError> { #(#member_reads)* Ok(#ident { #(#member_names),* }) } - fn write_to_guest(&self, location: &::memory::GuestPtrMut<'a, Self>) { + fn write_to_guest(&self, location: &wiggle_runtime::GuestPtrMut<'a, Self>) { #(#member_writes)* } } diff --git a/crates/memory/Cargo.toml b/crates/memory/Cargo.toml deleted file mode 100644 index f605782c99..0000000000 --- a/crates/memory/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "memory" -version = "0.1.0" -authors = ["Pat Hickey "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -thiserror = "1" diff --git a/crates/memory/.gitignore b/crates/runtime/.gitignore similarity index 100% rename from crates/memory/.gitignore rename to crates/runtime/.gitignore diff --git a/crates/runtime/Cargo.toml b/crates/runtime/Cargo.toml new file mode 100644 index 0000000000..4e711424de --- /dev/null +++ b/crates/runtime/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "wiggle-runtime" +version = "0.1.0" +authors = ["Pat Hickey ", "Jakub Konka "] +edition = "2018" + +[dependencies] +thiserror = "1" diff --git a/crates/memory/src/borrow.rs b/crates/runtime/src/borrow.rs similarity index 100% rename from crates/memory/src/borrow.rs rename to crates/runtime/src/borrow.rs diff --git a/crates/memory/src/error.rs b/crates/runtime/src/error.rs similarity index 100% rename from crates/memory/src/error.rs rename to crates/runtime/src/error.rs diff --git a/crates/memory/src/guest_type.rs b/crates/runtime/src/guest_type.rs similarity index 100% rename from crates/memory/src/guest_type.rs rename to crates/runtime/src/guest_type.rs diff --git a/crates/memory/src/lib.rs b/crates/runtime/src/lib.rs similarity index 71% rename from crates/memory/src/lib.rs rename to crates/runtime/src/lib.rs index fa1824f3b6..6f2b17ecfc 100644 --- a/crates/memory/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -4,7 +4,7 @@ mod guest_type; mod memory; mod region; -pub use self::memory::{GuestMemory, GuestPtr, GuestPtrMut, GuestRef, GuestRefMut}; pub use error::GuestError; pub use guest_type::{GuestErrorType, GuestType, GuestTypeClone, GuestTypeCopy, GuestTypePtr}; +pub use memory::{GuestMemory, GuestPtr, GuestPtrMut, GuestRef, GuestRefMut}; pub use region::Region; diff --git a/crates/memory/src/memory/array.rs b/crates/runtime/src/memory/array.rs similarity index 100% rename from crates/memory/src/memory/array.rs rename to crates/runtime/src/memory/array.rs diff --git a/crates/memory/src/memory/mod.rs b/crates/runtime/src/memory/mod.rs similarity index 100% rename from crates/memory/src/memory/mod.rs rename to crates/runtime/src/memory/mod.rs diff --git a/crates/memory/src/memory/ptr.rs b/crates/runtime/src/memory/ptr.rs similarity index 100% rename from crates/memory/src/memory/ptr.rs rename to crates/runtime/src/memory/ptr.rs diff --git a/crates/memory/src/region.rs b/crates/runtime/src/region.rs similarity index 100% rename from crates/memory/src/region.rs rename to crates/runtime/src/region.rs diff --git a/tests/main.rs b/tests/main.rs index c03a128d5a..89bfe21f52 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,13 +1,15 @@ -use memory::GuestRef; use proptest::prelude::*; +use wiggle_runtime::{ + GuestError, GuestErrorType, GuestMemory, GuestPtr, GuestPtrMut, GuestRef, GuestRefMut, +}; -generate::from_witx!({ +wiggle_generate::from_witx!({ witx: ["tests/test.witx"], ctx: WasiCtx, }); pub struct WasiCtx { - guest_errors: Vec<::memory::GuestError>, + guest_errors: Vec, } impl WasiCtx { @@ -27,17 +29,16 @@ impl foo::Foo for WasiCtx { fn baz( &mut self, input1: types::Excuse, - input2_ptr: ::memory::GuestPtrMut, - input3_ptr: ::memory::GuestPtr, - input4_ptr_ptr: ::memory::GuestPtrMut<::memory::GuestPtr>, + input2_ptr: GuestPtrMut, + input3_ptr: GuestPtr, + input4_ptr_ptr: GuestPtrMut>, ) -> Result<(), types::Errno> { println!("BAZ input1 {:?}", input1); // Read enum value from mutable: - let mut input2_ref: ::memory::GuestRefMut = - input2_ptr.as_ref_mut().map_err(|e| { - eprintln!("input2_ptr error: {}", e); - types::Errno::InvalidArg - })?; + let mut input2_ref: GuestRefMut = input2_ptr.as_ref_mut().map_err(|e| { + eprintln!("input2_ptr error: {}", e); + types::Errno::InvalidArg + })?; let input2: types::Excuse = *input2_ref; println!("input2 {:?}", input2); @@ -53,7 +54,7 @@ impl foo::Foo for WasiCtx { println!("wrote to input2_ref {:?}", input3); // Read ptr value from mutable ptr: - let input4_ptr: ::memory::GuestPtr = + let input4_ptr: GuestPtr = input4_ptr_ptr.read_ptr_from_guest().map_err(|e| { eprintln!("input4_ptr_ptr error: {}", e); types::Errno::InvalidArg @@ -96,12 +97,12 @@ impl foo::Foo for WasiCtx { // 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. -impl ::memory::GuestErrorType for types::Errno { +impl GuestErrorType for types::Errno { type Context = WasiCtx; fn success() -> types::Errno { types::Errno::Ok } - fn from_error(e: ::memory::GuestError, ctx: &mut WasiCtx) -> types::Errno { + fn from_error(e: GuestError, ctx: &mut WasiCtx) -> types::Errno { eprintln!("GUEST ERROR: {:?}", e); ctx.guest_errors.push(e); types::Errno::InvalidArg @@ -200,8 +201,7 @@ impl BatExercise { pub fn test(&self) { let mut ctx = WasiCtx::new(); let mut host_memory = HostMemory::new(); - let mut guest_memory = - memory::GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32); + let mut guest_memory = GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32); let bat_err = foo::bat( &mut ctx, @@ -300,8 +300,7 @@ impl BazExercise { pub fn test(&self) { let mut ctx = WasiCtx::new(); let mut host_memory = HostMemory::new(); - let mut guest_memory = - memory::GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32); + let mut guest_memory = GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32); *guest_memory .ptr_mut(self.input2_loc.ptr) @@ -399,8 +398,7 @@ impl SumOfPairExercise { pub fn test(&self) { let mut ctx = WasiCtx::new(); let mut host_memory = HostMemory::new(); - let mut guest_memory = - memory::GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32); + let mut guest_memory = GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32); *guest_memory .ptr_mut(self.input_loc.ptr) @@ -492,8 +490,7 @@ impl SumPairPtrsExercise { pub fn test(&self) { let mut ctx = WasiCtx::new(); let mut host_memory = HostMemory::new(); - let mut guest_memory = - memory::GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32); + let mut guest_memory = GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32); *guest_memory .ptr_mut(self.input_first_loc.ptr)