diff --git a/lib/yunq/test/example/example.yunq b/lib/yunq/test/example/example.yunq index 7e7c927..a3c9e59 100644 --- a/lib/yunq/test/example/example.yunq +++ b/lib/yunq/test/example/example.yunq @@ -1,13 +1,9 @@ package ex; message Basic { - u64 field; -} - -message Types { u64 unsigned_int; i64 signed_int; - string str; + string strn; } message Cap { diff --git a/rust/.cargo/config.toml b/rust/.cargo/config.toml index 220a396..6e70044 100644 --- a/rust/.cargo/config.toml +++ b/rust/.cargo/config.toml @@ -4,3 +4,6 @@ build-std = ["core", "compiler_builtins", "alloc"] [build] target = "x86_64-acadia-os.json" + +[alias] +test_pc = "test --target=x86_64-unknown-linux-gnu -Z build-std=std --lib" diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 2874dee..f9d9be5 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -210,6 +210,16 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "yunq-test" +version = "0.1.0" +dependencies = [ + "mammoth", + "yunq", + "yunq-derive", + "yunqc", +] + [[package]] name = "yunqc" version = "0.1.0" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 589f7ad..b6db314 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ "lib/denali", - "lib/mammoth", "lib/victoriafalls", "lib/voyageurs", "lib/yellowstone", "lib/yunq", "lib/yunq-derive", "sys/teton", "sys/yellowstone", "usr/testbed", + "lib/mammoth", "lib/victoriafalls", "lib/voyageurs", "lib/yellowstone", "lib/yunq", "lib/yunq-derive", "lib/yunq-test", "sys/teton", "sys/yellowstone", "usr/testbed", ] resolver = "2" diff --git a/rust/lib/mammoth/Cargo.toml b/rust/lib/mammoth/Cargo.toml index f14c35d..b8fd6ef 100644 --- a/rust/lib/mammoth/Cargo.toml +++ b/rust/lib/mammoth/Cargo.toml @@ -9,4 +9,6 @@ name = "mammoth" [dependencies] linked_list_allocator = "0.10.5" -[build-dependencies] +[features] +hosted = [] +default = ["hosted"] diff --git a/rust/lib/mammoth/src/mem.rs b/rust/lib/mammoth/src/mem.rs index 89037c5..cef5dd6 100644 --- a/rust/lib/mammoth/src/mem.rs +++ b/rust/lib/mammoth/src/mem.rs @@ -2,13 +2,17 @@ use crate::cap::Capability; use crate::syscall; use crate::zion::ZError; use alloc::slice; + +#[cfg(feature = "hosted")] use linked_list_allocator::LockedHeap; +#[cfg(feature = "hosted")] #[global_allocator] static ALLOCATOR: LockedHeap = LockedHeap::empty(); pub static mut CAN_ALLOC: bool = false; +#[cfg(feature = "hosted")] pub fn init_heap() { // 1 MiB let size = 0x10_0000; diff --git a/rust/lib/mammoth/src/syscall.rs b/rust/lib/mammoth/src/syscall.rs index 2ffaca5..5b83756 100644 --- a/rust/lib/mammoth/src/syscall.rs +++ b/rust/lib/mammoth/src/syscall.rs @@ -5,6 +5,8 @@ use crate::zion; use crate::zion::z_cap_t; use crate::zion::ZError; use core::ffi::c_void; + +#[cfg(feature = "hosted")] use core::panic::PanicInfo; #[must_use] @@ -18,6 +20,7 @@ fn syscall(id: u64, req: &T) -> Result<(), ZError> { Ok(()) } +#[cfg(feature = "hosted")] #[panic_handler] fn panic(info: &PanicInfo) -> ! { unsafe { diff --git a/rust/lib/yunq-derive/src/lib.rs b/rust/lib/yunq-derive/src/lib.rs index d1652e0..bf99f3b 100644 --- a/rust/lib/yunq-derive/src/lib.rs +++ b/rust/lib/yunq-derive/src/lib.rs @@ -29,7 +29,13 @@ fn serialize_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenS } else if path.is_ident("u64") { quote! { { - buf.write_at(yunq::message::field_offset(offset, #ind), self.#name as u64)?; + buf.write_at(yunq::message::field_offset(offset, #ind), self.#name)?; + } + } + } else if path.is_ident("i64") { + quote! { + { + buf.write_at(yunq::message::field_offset(offset, #ind), self.#name)?; } } } else { @@ -67,6 +73,10 @@ fn parse_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStrea quote! { let #name = buf.at::(yunq::message::field_offset(offset, #ind))?; } + } else if path.is_ident("i64") { + quote! { + let #name = buf.at::(yunq::message::field_offset(offset, #ind))?; + } } else { quote! { let #name = { diff --git a/rust/lib/yunq-test/Cargo.toml b/rust/lib/yunq-test/Cargo.toml new file mode 100644 index 0000000..8d550ce --- /dev/null +++ b/rust/lib/yunq-test/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "yunq-test" +version = "0.1.0" +edition = "2021" + +[dependencies] +mammoth = { path = "../mammoth", default-features = false} +yunq = {path = "../yunq", default-features = false} +yunq-derive = {path = "../yunq-derive"} + +[build-dependencies] +yunqc = {path = "../../../yunq/rust"} + +[dev-dependencies] diff --git a/rust/lib/yunq-test/build.rs b/rust/lib/yunq-test/build.rs new file mode 100644 index 0000000..ef1bf8d --- /dev/null +++ b/rust/lib/yunq-test/build.rs @@ -0,0 +1,14 @@ +use std::fs; + +fn main() { + let input_file = "../../../lib/yunq/test/example/example.yunq"; + + println!("cargo::rerun-if-changed={input_file}"); + + let input = fs::read_to_string(input_file).expect("Failed to read input file"); + + let code = yunqc::codegen(&input).expect("Failed to generate yunq code."); + + let out = std::env::var("OUT_DIR").unwrap() + "/yunq.rs"; + fs::write(out, code).expect("Failed to write generated code."); +} diff --git a/rust/lib/yunq-test/src/lib.rs b/rust/lib/yunq-test/src/lib.rs new file mode 100644 index 0000000..dd2b5be --- /dev/null +++ b/rust/lib/yunq-test/src/lib.rs @@ -0,0 +1,62 @@ +#![no_std] + +include!(concat!(env!("OUT_DIR"), "/yunq.rs")); + +#[cfg(test)] +mod tests { + use super::*; + + extern crate std; + use std::vec; + + #[test] + fn basic_serialization() -> Result<(), ZError> { + let basic = Basic { unsigned_int: 82, signed_int: -1234, strn: "abc".to_string() }; + + let mut buf = ByteBuffer::<1024>::new(); + let mut caps = Vec::new(); + basic.serialize(&mut buf, 0, &mut caps)?; + + let parsed = Basic::parse(&buf, 0, &caps)?; + + assert!(parsed == basic); + + Ok(()) + } + + #[test] + fn basic_serialization_as_request() -> Result<(), ZError> { + let basic = Basic { unsigned_int: 82, signed_int: -1234, strn: "abc".to_string() }; + + let mut buf = ByteBuffer::<1024>::new(); + let mut caps = Vec::new(); + let req_id = 12; + basic.serialize_as_request(req_id, &mut buf, &mut caps)?; + + assert!(buf.at::(8)? == req_id); + + let parsed = Basic::parse_from_request(&buf, &caps)?; + + assert!(parsed == basic); + Ok(()) + } + + #[test] + fn capability_serialization() -> Result<(), ZError> { + let cap_id = 100; + let cap = Cap { cap: cap_id }; + + let mut buf = ByteBuffer::<1024>::new(); + let mut caps = Vec::new(); + cap.serialize(&mut buf, 0, &mut caps)?; + + assert!(caps.len() == 1); + assert!(caps[0] == cap_id); + + let parsed = Cap::parse(&buf, 0, &caps)?; + + assert!(parsed == cap); + + Ok(()) + } +} diff --git a/rust/lib/yunq/Cargo.toml b/rust/lib/yunq/Cargo.toml index f442771..2a75596 100644 --- a/rust/lib/yunq/Cargo.toml +++ b/rust/lib/yunq/Cargo.toml @@ -4,5 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] -mammoth = {path = "../mammoth"} +mammoth = {path = "../mammoth", default-features = false} yunq-derive = {path = "../yunq-derive"} + +[features] +default = ["hosted"] +hosted = ["mammoth/hosted"] diff --git a/yunq/rust/src/codegen.rs b/yunq/rust/src/codegen.rs index 71e493e..928ec58 100644 --- a/yunq/rust/src/codegen.rs +++ b/yunq/rust/src/codegen.rs @@ -17,7 +17,7 @@ fn generate_message(message: &Message) -> TokenStream { .iter() .map(|f| Ident::new(f.field_type.rust_type(), Span::call_site())); quote! { - #[derive(YunqMessage)] + #[derive(YunqMessage, PartialEq, Eq)] pub struct #name { #(pub #field_names: #field_types),* }