fix: ref opt native fns should be instance methods

This commit is contained in:
Orion Kindel 2023-04-18 17:42:03 -05:00
parent 72285ad7a1
commit ff7c0b00e4
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
7 changed files with 97 additions and 120 deletions

4
glue/Cargo.lock generated
View File

@ -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",

View File

@ -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"]}

View File

@ -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<toad_msg::alloc::Message>, Throwable> {
pub fn try_deref(&self,
e: &mut java::Env)
-> Result<&'static Addrd<toad_msg::alloc::Message>, Throwable> {
self.ptr(e).addr(e).map(|addr| unsafe {
Shared::deref::<Addrd<toad_msg::alloc::Message>>(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::<Message>(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::<Message>(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::<Message>(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::<Message>(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::<Message>(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::<Message>(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::<Message>(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::<Vec<_>>();
refs.yield_to_java(e)
opts.into_iter()
.map(|(n, v)| {
Opt::new(e, v as *const _ as i64, n.0.into())
})
.collect::<Vec<_>>()
})
.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)]

View File

@ -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<Opt, Ptr> = java::Field::new("ptr");
PTR.get(e, self)
}
pub fn number(&self, env: &mut java::Env) -> OptNumber {
static NUMBER: java::Field<Opt, i64> = java::Field::new("number");
OptNumber(NUMBER.get(env, self) as u32)
@ -30,31 +37,28 @@ impl Opt {
static VALUES: java::Method<Opt, fn() -> Vec<OptValue>> = java::Method::new("valueRefs");
VALUES.invoke(env, self)
}
pub fn try_deref(&self,
e: &mut java::Env)
-> Result<&'static Vec<toad_msg::OptValue<Vec<u8>>>, Throwable> {
self.ptr(e).addr(e).map(|addr| unsafe {
Shared::deref::<Vec<toad_msg::OptValue<Vec<u8>>>>(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::<Opt>(&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::<Vec<toad_msg::OptValue<Vec<u8>>>>(p).as_ref()
.unwrap()
};
let refs = o.iter()
.map(|v| OptValue::new(&mut e, (&v.0 as *const Vec<u8>).addr() as i64))
.collect::<Vec<_>>();
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::<Opt>(e)
.try_deref(e)
.map(|values| {
values.iter()
.map(|v| OptValue::new(e, (v as *const toad_msg::OptValue<Vec<u8>>).addr() as i64))
.collect::<Vec<_>>()
})
.yield_to_java_or_throw(e)
}

View File

@ -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<OptValue, Ptr> = java::Field::new("ptr");
PTR.get(e, self)
}
pub fn try_deref(&self,
e: &mut java::Env)
-> Result<&'static toad_msg::OptValue<Vec<u8>>, Throwable> {
self.ptr(e).addr(e).map(|addr| unsafe {
Shared::deref::<toad_msg::OptValue<Vec<u8>>>(addr.inner(e)).as_ref()
.unwrap()
})
}
pub fn bytes(&self, env: &mut java::Env) -> Vec<u8> {
static AS_BYTES: java::Method<OptValue, fn() -> Vec<i8>> = 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::<toad_msg::OptValue<Vec<u8>>>(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::<OptValue>(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)
}

View File

@ -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<dev.toad.msg.OptionValue> values() {
return Arrays.asList(this.values(this.ptr.addr()));
return Arrays.asList(this.valueRefs());
}
public dev.toad.msg.owned.Option toOwned() {

View File

@ -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() {