From c71993ba2463305a98a3ef64f416e30320cc3aef Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Thu, 20 Apr 2023 11:33:12 -0500 Subject: [PATCH] fix: oops --- glue/src/dev/toad/msg/mod.rs | 20 +++++++++++++++++++- glue/src/dev/toad/msg/option/mod.rs | 6 ++---- glue/src/dev/toad/msg/owned/opt.rs | 5 +++++ glue/src/dev/toad/msg/owned/opt_value.rs | 8 ++++++++ glue/src/dev/toad/msg/payload.rs | 5 ++++- glue/src/dev/toad/msg/ref_/msg.rs | 18 ++++++++++++++---- 6 files changed, 52 insertions(+), 10 deletions(-) diff --git a/glue/src/dev/toad/msg/mod.rs b/glue/src/dev/toad/msg/mod.rs index ae3460c..d0e1945 100644 --- a/glue/src/dev/toad/msg/mod.rs +++ b/glue/src/dev/toad/msg/mod.rs @@ -3,7 +3,7 @@ pub mod owned; pub mod ref_; mod ty; - +use toad_jni::java; pub use ty::Type; mod payload; @@ -17,3 +17,21 @@ pub use id::Id; mod token; pub use token::Token; + +pub struct Message(pub java::lang::Object); +java::object_newtype!(Message); +impl java::Class for Message { + const PATH: &'static str = package!(dev.toad.msg.Message); +} + +pub struct Option(pub java::lang::Object); +java::object_newtype!(Option); +impl java::Class for Option { + const PATH: &'static str = package!(dev.toad.msg.Option); +} + +pub struct OptionValue(pub java::lang::Object); +java::object_newtype!(OptionValue); +impl java::Class for OptionValue { + const PATH: &'static str = package!(dev.toad.msg.OptionValue); +} diff --git a/glue/src/dev/toad/msg/option/mod.rs b/glue/src/dev/toad/msg/option/mod.rs index ddd028d..356ab5a 100644 --- a/glue/src/dev/toad/msg/option/mod.rs +++ b/glue/src/dev/toad/msg/option/mod.rs @@ -1,7 +1,5 @@ use toad_jni::java; -use super::owned::Opt; - pub struct ContentFormat(java::lang::Object); java::object_newtype!(ContentFormat); impl java::Class for ContentFormat { @@ -9,8 +7,8 @@ impl java::Class for ContentFormat { } impl ContentFormat { - pub fn new(e: &mut java::Env, o: Opt) -> Self { - static CTOR: java::Constructor = java::Constructor::new(); + pub fn new(e: &mut java::Env, o: super::Option) -> Self { + static CTOR: java::Constructor = java::Constructor::new(); CTOR.invoke(e, o) } } diff --git a/glue/src/dev/toad/msg/owned/opt.rs b/glue/src/dev/toad/msg/owned/opt.rs index 3b9a6cf..805d64f 100644 --- a/glue/src/dev/toad/msg/owned/opt.rs +++ b/glue/src/dev/toad/msg/owned/opt.rs @@ -12,6 +12,11 @@ impl java::Class for Opt { } impl Opt { + pub fn new(e: &mut java::Env, num: i64, vals: ArrayList) -> Self { + static CTOR: java::Constructor)> = java::Constructor::new(); + CTOR.invoke(e, num, vals) + } + pub fn number(&self, e: &mut java::Env) -> OptNumber { static NUMBER: java::Field = java::Field::new("number"); OptNumber(NUMBER.get(e, self).to_rust(e)) diff --git a/glue/src/dev/toad/msg/owned/opt_value.rs b/glue/src/dev/toad/msg/owned/opt_value.rs index 2f076e5..2a928f1 100644 --- a/glue/src/dev/toad/msg/owned/opt_value.rs +++ b/glue/src/dev/toad/msg/owned/opt_value.rs @@ -7,6 +7,14 @@ impl java::Class for OptValue { } impl OptValue { + pub fn new(e: &mut java::Env, bytes: impl IntoIterator) -> Self { + static CTOR: java::Constructor)> = java::Constructor::new(); + CTOR.invoke(e, + bytes.into_iter() + .map(|b| i8::from_be_bytes(b.to_be_bytes())) + .collect()) + } + pub fn bytes(&self, e: &mut java::Env) -> Vec { static BYTES: java::Field> = java::Field::new("bytes"); BYTES.get(e, self) diff --git a/glue/src/dev/toad/msg/payload.rs b/glue/src/dev/toad/msg/payload.rs index 7216a8d..f9ae64d 100644 --- a/glue/src/dev/toad/msg/payload.rs +++ b/glue/src/dev/toad/msg/payload.rs @@ -17,7 +17,10 @@ impl Payload { .collect()) } - pub fn new_content_format(e: &mut java::Env, bytes: Vec, f: ContentFormat) -> Self { + pub fn new_content_format(e: &mut java::Env, + bytes: impl IntoIterator, + f: ContentFormat) + -> Self { static CTOR: java::Constructor)> = java::Constructor::new(); CTOR.invoke(e, f, diff --git a/glue/src/dev/toad/msg/ref_/msg.rs b/glue/src/dev/toad/msg/ref_/msg.rs index 32beac0..9fdcde4 100644 --- a/glue/src/dev/toad/msg/ref_/msg.rs +++ b/glue/src/dev/toad/msg/ref_/msg.rs @@ -5,10 +5,13 @@ use jni::sys::jobject; use toad::net::Addrd; use toad_jni::java::lang::Throwable; use toad_jni::java::net::InetSocketAddress; -use toad_jni::java::{self, ResultYieldToJavaOrThrow}; +use toad_jni::java::util::ArrayList; +use toad_jni::java::{self, Object, ResultYieldToJavaOrThrow}; +use toad_msg::no_repeat::CONTENT_FORMAT; use toad_msg::MessageOptions; use crate::dev::toad::ffi::Ptr; +use crate::dev::toad::msg::option::ContentFormat; use crate::dev::toad::msg::ref_::Opt; use crate::dev::toad::msg::{Code, Id, Payload, Token, Type}; use crate::mem::{Shared, SharedMemoryRegion}; @@ -137,8 +140,15 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_payload<'local>(mut env: ja .map(|msg| { msg.data() .content_format() - .map(|_f| { - Payload::new(e, msg.data().payload.0.iter().copied()) + .map(|f| { + use crate::dev::toad::msg::owned::{Opt, OptValue}; + + let arr = ArrayList::::new(e); + let val = OptValue::new(e, f.bytes()); + arr.append(e, val); + let o = Opt::new(e, CONTENT_FORMAT.0.into(), arr).downcast(e); + let f = ContentFormat::new(e, crate::dev::toad::msg::Option(o)); + Payload::new_content_format(e, msg.data().payload.0.iter().copied(), f) }) .unwrap_or_else(|| { Payload::new(e, msg.data().payload.0.iter().copied()) @@ -204,7 +214,7 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_optionRefs<'local>(mut env: #[cfg(test)] mod tests { - use toad_jni::java::Signature; + use toad_jni::java::{Object, Signature}; use toad_msg::{MessageOptions, Payload}; use super::*;