From ff7c0b00e42d49b35f2ba04c7026d76fba5bbc35 Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Tue, 18 Apr 2023 17:42:03 -0500 Subject: [PATCH] fix: ref opt native fns should be instance methods --- glue/Cargo.lock | 4 +- glue/Cargo.toml | 2 +- glue/src/dev/toad/msg/ref_/msg.rs | 98 ++++++------------- glue/src/dev/toad/msg/ref_/opt.rs | 54 +++++----- glue/src/dev/toad/msg/ref_/opt_value.rs | 41 +++++--- src/main/java/dev.toad/msg/ref/Option.java | 8 +- .../java/dev.toad/msg/ref/OptionValue.java | 10 +- 7 files changed, 97 insertions(+), 120 deletions(-) diff --git a/glue/Cargo.lock b/glue/Cargo.lock index 98cb857..a220312 100644 --- a/glue/Cargo.lock +++ b/glue/Cargo.lock @@ -582,9 +582,9 @@ dependencies = [ [[package]] name = "toad-jni" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93571aa0e40745d04eff12007637f0ed01ea1afbe112b1eca19a11fe30f638f6" +checksum = "c0bdcbce1dfef5ac76668490d12944aec41816999b2bd6ab889a5afbcbf13eb5" dependencies = [ "embedded-time", "jni", diff --git a/glue/Cargo.toml b/glue/Cargo.toml index d048399..8674be8 100644 --- a/glue/Cargo.toml +++ b/glue/Cargo.toml @@ -15,7 +15,7 @@ e2e = [] jni = "0.21.1" nb = "1" toad = "0.17.4" -toad-jni = "0.13.0" +toad-jni = "0.14.0" no-std-net = "0.6" toad-msg = "0.18.1" tinyvec = {version = "1.5", default_features = false, features = ["rustc_1_55"]} diff --git a/glue/src/dev/toad/msg/ref_/msg.rs b/glue/src/dev/toad/msg/ref_/msg.rs index 05b6f32..3748d78 100644 --- a/glue/src/dev/toad/msg/ref_/msg.rs +++ b/glue/src/dev/toad/msg/ref_/msg.rs @@ -5,7 +5,7 @@ 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, Object}; +use toad_jni::java::{self, Object, ResultYieldToJavaOrThrow}; use crate::dev::toad::ffi::Ptr; use crate::dev::toad::msg::ref_::Opt; @@ -30,9 +30,9 @@ impl Message { PTR.get(e, self) } - pub fn toad_ref(&self, - e: &mut java::Env) - -> Result<&'static Addrd, Throwable> { + pub fn try_deref(&self, + e: &mut java::Env) + -> Result<&'static Addrd, Throwable> { self.ptr(e).addr(e).map(|addr| unsafe { Shared::deref::>(addr.inner(e)).as_ref() .unwrap() @@ -115,13 +115,9 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_id<'local>(mut env: java::E -> jobject { let e = &mut env; java::lang::Object::from_local(e, msg).upcast_to::(e) - .toad_ref(e) - .map(|msg| Id::from_toad(e, msg.data().id).yield_to_java(e)) - .map_err(|err| { - let err = JThrowable::from(err.downcast(e).to_local(e)); - e.throw(err).unwrap() - }) - .unwrap_or(*JObject::null()) + .try_deref(e) + .map(|msg| Id::from_toad(e, msg.data().id)) + .yield_to_java_or_throw(e) } #[no_mangle] @@ -130,15 +126,9 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_token<'local>(mut env: java -> jobject { let e = &mut env; java::lang::Object::from_local(e, msg).upcast_to::(e) - .toad_ref(e) - .map(|msg| { - Token::from_toad(e, msg.data().token).yield_to_java(e) - }) - .map_err(|err| { - let err = JThrowable::from(err.downcast(e).to_local(e)); - e.throw(err).unwrap() - }) - .unwrap_or(*JObject::null()) + .try_deref(e) + .map(|msg| Token::from_toad(e, msg.data().token)) + .yield_to_java_or_throw(e) } #[no_mangle] @@ -147,17 +137,12 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_payloadBytes<'local>(mut en -> jobject { let e = &mut env; java::lang::Object::from_local(e, msg).upcast_to::(e) - .toad_ref(e) + .try_deref(e) .map(|msg| { - e.byte_array_from_slice(&msg.data().payload.0) - .unwrap() - .as_raw() + java::lang::Object::from_local(e, e.byte_array_from_slice(&msg.data().payload.0) + .unwrap()) }) - .map_err(|err| { - let err = JThrowable::from(err.downcast(e).to_local(e)); - e.throw(err).unwrap() - }) - .unwrap_or(*JObject::null()) + .yield_to_java_or_throw(e) } #[no_mangle] @@ -166,15 +151,9 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_type<'local>(mut env: java: -> jobject { let e = &mut env; java::lang::Object::from_local(e, msg).upcast_to::(e) - .toad_ref(e) - .map(|msg| { - Type::from_toad(e, msg.data().ty).yield_to_java(e) - }) - .map_err(|err| { - let err = JThrowable::from(err.downcast(e).to_local(e)); - e.throw(err).unwrap() - }) - .unwrap_or(*JObject::null()) + .try_deref(e) + .map(|msg| Type::from_toad(e, msg.data().ty)) + .yield_to_java_or_throw(e) } #[no_mangle] @@ -183,15 +162,9 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_code<'local>(mut env: java: -> jobject { let e = &mut env; java::lang::Object::from_local(e, msg).upcast_to::(e) - .toad_ref(e) - .map(|msg| { - Code::from_toad(e, msg.data().code).yield_to_java(e) - }) - .map_err(|err| { - let err = JThrowable::from(err.downcast(e).to_local(e)); - e.throw(err).unwrap() - }) - .unwrap_or(*JObject::null()) + .try_deref(e) + .map(|msg| Code::from_toad(e, msg.data().code)) + .yield_to_java_or_throw(e) } #[no_mangle] @@ -200,16 +173,12 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_addr<'local>(mut env: java: -> jobject { let e = &mut env; java::lang::Object::from_local(e, msg).upcast_to::(e) - .toad_ref(e) + .try_deref(e) .map(|msg| { let addr = InetSocketAddress::from_no_std(e, msg.addr()); - java::util::Optional::of(e, addr).yield_to_java(e) + java::util::Optional::of(e, addr) }) - .map_err(|err| { - let err = JThrowable::from(err.downcast(e).to_local(e)); - e.throw(err).unwrap() - }) - .unwrap_or(*JObject::null()) + .yield_to_java_or_throw(e) } #[no_mangle] @@ -218,24 +187,17 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_optionRefs<'local>(mut env: -> jobject { let e = &mut env; java::lang::Object::from_local(e, msg).upcast_to::(e) - .toad_ref(e) + .try_deref(e) .map(|msg| { let opts = &msg.data().opts; - let refs = - opts.into_iter() - .map(|(n, v)| { - Opt::new(e, v as *const _ as i64, n.0.into()) - }) - .collect::>(); - - refs.yield_to_java(e) + opts.into_iter() + .map(|(n, v)| { + Opt::new(e, v as *const _ as i64, n.0.into()) + }) + .collect::>() }) - .map_err(|err| { - let err = JThrowable::from(err.downcast(e).to_local(e)); - e.throw(err).unwrap() - }) - .unwrap_or(*JObject::null()) + .yield_to_java_or_throw(e) } #[cfg(test)] diff --git a/glue/src/dev/toad/msg/ref_/opt.rs b/glue/src/dev/toad/msg/ref_/opt.rs index a13c2c2..9e1062b 100644 --- a/glue/src/dev/toad/msg/ref_/opt.rs +++ b/glue/src/dev/toad/msg/ref_/opt.rs @@ -1,10 +1,12 @@ use jni::objects::{JClass, JObject}; use jni::sys::jobject; use jni::JNIEnv; -use toad_jni::java::{self, Object}; +use toad_jni::java::lang::Throwable; +use toad_jni::java::{self, Object, ResultYieldToJavaOrThrow}; use toad_msg::OptNumber; use super::OptValue; +use crate::dev::toad::ffi::Ptr; use crate::mem::{Shared, SharedMemoryRegion}; pub struct Opt(pub java::lang::Object); @@ -21,6 +23,11 @@ impl Opt { CTOR.invoke(env, addr, num) } + pub fn ptr(&self, e: &mut java::Env) -> Ptr { + static PTR: java::Field = java::Field::new("ptr"); + PTR.get(e, self) + } + pub fn number(&self, env: &mut java::Env) -> OptNumber { static NUMBER: java::Field = java::Field::new("number"); OptNumber(NUMBER.get(env, self) as u32) @@ -30,31 +37,28 @@ impl Opt { static VALUES: java::Method Vec> = java::Method::new("valueRefs"); VALUES.invoke(env, self) } + + pub fn try_deref(&self, + e: &mut java::Env) + -> Result<&'static Vec>>, Throwable> { + self.ptr(e).addr(e).map(|addr| unsafe { + Shared::deref::>>>(addr.inner(e)).as_ref() + .unwrap() + }) + } } #[no_mangle] -pub extern "system" fn Java_dev_toad_msg_ref_Option_number<'local>(mut env: JNIEnv<'local>, - o: JObject<'local>, - p: i64) - -> i64 { - java::lang::Object::from_local(&mut env, o).upcast_to::(&mut env) - .number(&mut env) - .0 as i64 -} - -#[no_mangle] -pub extern "system" fn Java_dev_toad_msg_ref_Option_values<'local>(mut e: JNIEnv<'local>, - _: JClass<'local>, - p: i64) - -> jobject { - let o = unsafe { - Shared::deref::>>>(p).as_ref() - .unwrap() - }; - - let refs = o.iter() - .map(|v| OptValue::new(&mut e, (&v.0 as *const Vec).addr() as i64)) - .collect::>(); - - refs.yield_to_java(&mut e) +pub extern "system" fn Java_dev_toad_msg_ref_Option_valueRefs<'local>(mut e: JNIEnv<'local>, + o: JObject<'local>) + -> jobject { + let e = &mut e; + java::lang::Object::from_local(e, o).upcast_to::(e) + .try_deref(e) + .map(|values| { + values.iter() + .map(|v| OptValue::new(e, (v as *const toad_msg::OptValue>).addr() as i64)) + .collect::>() + }) + .yield_to_java_or_throw(e) } diff --git a/glue/src/dev/toad/msg/ref_/opt_value.rs b/glue/src/dev/toad/msg/ref_/opt_value.rs index 9752177..7756941 100644 --- a/glue/src/dev/toad/msg/ref_/opt_value.rs +++ b/glue/src/dev/toad/msg/ref_/opt_value.rs @@ -1,7 +1,9 @@ -use jni::objects::JClass; +use jni::objects::{JClass, JObject}; use jni::sys::jobject; -use toad_jni::java; +use toad_jni::java::lang::Throwable; +use toad_jni::java::{self, ResultYieldToJavaOrThrow}; +use crate::dev::toad::ffi::Ptr; use crate::mem::{Shared, SharedMemoryRegion}; pub struct OptValue(java::lang::Object); @@ -17,6 +19,20 @@ impl OptValue { CTOR.invoke(env, addr) } + pub fn ptr(&self, e: &mut java::Env) -> Ptr { + static PTR: java::Field = java::Field::new("ptr"); + PTR.get(e, self) + } + + pub fn try_deref(&self, + e: &mut java::Env) + -> Result<&'static toad_msg::OptValue>, Throwable> { + self.ptr(e).addr(e).map(|addr| unsafe { + Shared::deref::>>(addr.inner(e)).as_ref() + .unwrap() + }) + } + pub fn bytes(&self, env: &mut java::Env) -> Vec { static AS_BYTES: java::Method Vec> = java::Method::new("asBytes"); AS_BYTES.invoke(env, self) @@ -27,13 +43,16 @@ impl OptValue { } #[no_mangle] -pub extern "system" fn Java_dev_toad_msg_ref_OptionValue_bytes<'local>(mut env: java::Env<'local>, - _: JClass<'local>, - p: i64) - -> jobject { - let val = unsafe { - Shared::deref::>>(p).as_ref() - .unwrap() - }; - env.byte_array_from_slice(val.as_bytes()).unwrap().as_raw() +pub extern "system" fn Java_dev_toad_msg_ref_OptionValue_asBytes<'local>(mut env: java::Env<'local>, + val: JObject<'local>) + -> jobject { + let e = &mut env; + java::lang::Object::from_local(e, val).upcast_to::(e) + .try_deref(e) + .map(|val| { + let arr = + e.byte_array_from_slice(val.as_bytes()).unwrap(); + java::lang::Object::from_local(e, arr) + }) + .yield_to_java_or_throw(e) } diff --git a/src/main/java/dev.toad/msg/ref/Option.java b/src/main/java/dev.toad/msg/ref/Option.java index bfcb7d4..a9acba9 100644 --- a/src/main/java/dev.toad/msg/ref/Option.java +++ b/src/main/java/dev.toad/msg/ref/Option.java @@ -10,7 +10,7 @@ public class Option implements dev.toad.msg.Option, AutoCloseable { Ptr ptr; final long number; - native dev.toad.msg.ref.OptionValue[] values(long ptr); + public native dev.toad.msg.ref.OptionValue[] valueRefs(); Option(long addr, long number) { this.ptr = Ptr.register(this.getClass(), addr); @@ -21,12 +21,8 @@ public class Option implements dev.toad.msg.Option, AutoCloseable { return this.number; } - public dev.toad.msg.ref.OptionValue[] valueRefs() { - return this.values(this.ptr.addr()); - } - public List values() { - return Arrays.asList(this.values(this.ptr.addr())); + return Arrays.asList(this.valueRefs()); } public dev.toad.msg.owned.Option toOwned() { diff --git a/src/main/java/dev.toad/msg/ref/OptionValue.java b/src/main/java/dev.toad/msg/ref/OptionValue.java index 96e0b3c..eea8003 100644 --- a/src/main/java/dev.toad/msg/ref/OptionValue.java +++ b/src/main/java/dev.toad/msg/ref/OptionValue.java @@ -5,20 +5,16 @@ import dev.toad.ffi.Ptr; public final class OptionValue implements dev.toad.msg.OptionValue, AutoCloseable { - private final Ptr ptr; + final Ptr ptr; - private native byte[] bytes(long addr); + public native byte[] asBytes(); OptionValue(long addr) { this.ptr = Ptr.register(this.getClass(), addr); } - public byte[] asBytes() { - return this.bytes(this.ptr.addr()); - } - public String asString() { - return new String(this.bytes(this.ptr.addr())); + return new String(this.asBytes()); } public dev.toad.msg.owned.OptionValue toOwned() {