fix: ref opt native fns should be instance methods
This commit is contained in:
parent
72285ad7a1
commit
ff7c0b00e4
4
glue/Cargo.lock
generated
4
glue/Cargo.lock
generated
@ -582,9 +582,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toad-jni"
|
name = "toad-jni"
|
||||||
version = "0.13.0"
|
version = "0.14.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "93571aa0e40745d04eff12007637f0ed01ea1afbe112b1eca19a11fe30f638f6"
|
checksum = "c0bdcbce1dfef5ac76668490d12944aec41816999b2bd6ab889a5afbcbf13eb5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"embedded-time",
|
"embedded-time",
|
||||||
"jni",
|
"jni",
|
||||||
|
@ -15,7 +15,7 @@ e2e = []
|
|||||||
jni = "0.21.1"
|
jni = "0.21.1"
|
||||||
nb = "1"
|
nb = "1"
|
||||||
toad = "0.17.4"
|
toad = "0.17.4"
|
||||||
toad-jni = "0.13.0"
|
toad-jni = "0.14.0"
|
||||||
no-std-net = "0.6"
|
no-std-net = "0.6"
|
||||||
toad-msg = "0.18.1"
|
toad-msg = "0.18.1"
|
||||||
tinyvec = {version = "1.5", default_features = false, features = ["rustc_1_55"]}
|
tinyvec = {version = "1.5", default_features = false, features = ["rustc_1_55"]}
|
||||||
|
@ -5,7 +5,7 @@ use jni::sys::jobject;
|
|||||||
use toad::net::Addrd;
|
use toad::net::Addrd;
|
||||||
use toad_jni::java::lang::Throwable;
|
use toad_jni::java::lang::Throwable;
|
||||||
use toad_jni::java::net::InetSocketAddress;
|
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::ffi::Ptr;
|
||||||
use crate::dev::toad::msg::ref_::Opt;
|
use crate::dev::toad::msg::ref_::Opt;
|
||||||
@ -30,9 +30,9 @@ impl Message {
|
|||||||
PTR.get(e, self)
|
PTR.get(e, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toad_ref(&self,
|
pub fn try_deref(&self,
|
||||||
e: &mut java::Env)
|
e: &mut java::Env)
|
||||||
-> Result<&'static Addrd<toad_msg::alloc::Message>, Throwable> {
|
-> Result<&'static Addrd<toad_msg::alloc::Message>, Throwable> {
|
||||||
self.ptr(e).addr(e).map(|addr| unsafe {
|
self.ptr(e).addr(e).map(|addr| unsafe {
|
||||||
Shared::deref::<Addrd<toad_msg::alloc::Message>>(addr.inner(e)).as_ref()
|
Shared::deref::<Addrd<toad_msg::alloc::Message>>(addr.inner(e)).as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -115,13 +115,9 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_id<'local>(mut env: java::E
|
|||||||
-> jobject {
|
-> jobject {
|
||||||
let e = &mut env;
|
let e = &mut env;
|
||||||
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
||||||
.toad_ref(e)
|
.try_deref(e)
|
||||||
.map(|msg| Id::from_toad(e, msg.data().id).yield_to_java(e))
|
.map(|msg| Id::from_toad(e, msg.data().id))
|
||||||
.map_err(|err| {
|
.yield_to_java_or_throw(e)
|
||||||
let err = JThrowable::from(err.downcast(e).to_local(e));
|
|
||||||
e.throw(err).unwrap()
|
|
||||||
})
|
|
||||||
.unwrap_or(*JObject::null())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -130,15 +126,9 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_token<'local>(mut env: java
|
|||||||
-> jobject {
|
-> jobject {
|
||||||
let e = &mut env;
|
let e = &mut env;
|
||||||
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
||||||
.toad_ref(e)
|
.try_deref(e)
|
||||||
.map(|msg| {
|
.map(|msg| Token::from_toad(e, msg.data().token))
|
||||||
Token::from_toad(e, msg.data().token).yield_to_java(e)
|
.yield_to_java_or_throw(e)
|
||||||
})
|
|
||||||
.map_err(|err| {
|
|
||||||
let err = JThrowable::from(err.downcast(e).to_local(e));
|
|
||||||
e.throw(err).unwrap()
|
|
||||||
})
|
|
||||||
.unwrap_or(*JObject::null())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -147,17 +137,12 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_payloadBytes<'local>(mut en
|
|||||||
-> jobject {
|
-> jobject {
|
||||||
let e = &mut env;
|
let e = &mut env;
|
||||||
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
||||||
.toad_ref(e)
|
.try_deref(e)
|
||||||
.map(|msg| {
|
.map(|msg| {
|
||||||
e.byte_array_from_slice(&msg.data().payload.0)
|
java::lang::Object::from_local(e, e.byte_array_from_slice(&msg.data().payload.0)
|
||||||
.unwrap()
|
.unwrap())
|
||||||
.as_raw()
|
|
||||||
})
|
})
|
||||||
.map_err(|err| {
|
.yield_to_java_or_throw(e)
|
||||||
let err = JThrowable::from(err.downcast(e).to_local(e));
|
|
||||||
e.throw(err).unwrap()
|
|
||||||
})
|
|
||||||
.unwrap_or(*JObject::null())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -166,15 +151,9 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_type<'local>(mut env: java:
|
|||||||
-> jobject {
|
-> jobject {
|
||||||
let e = &mut env;
|
let e = &mut env;
|
||||||
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
||||||
.toad_ref(e)
|
.try_deref(e)
|
||||||
.map(|msg| {
|
.map(|msg| Type::from_toad(e, msg.data().ty))
|
||||||
Type::from_toad(e, msg.data().ty).yield_to_java(e)
|
.yield_to_java_or_throw(e)
|
||||||
})
|
|
||||||
.map_err(|err| {
|
|
||||||
let err = JThrowable::from(err.downcast(e).to_local(e));
|
|
||||||
e.throw(err).unwrap()
|
|
||||||
})
|
|
||||||
.unwrap_or(*JObject::null())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -183,15 +162,9 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_code<'local>(mut env: java:
|
|||||||
-> jobject {
|
-> jobject {
|
||||||
let e = &mut env;
|
let e = &mut env;
|
||||||
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
||||||
.toad_ref(e)
|
.try_deref(e)
|
||||||
.map(|msg| {
|
.map(|msg| Code::from_toad(e, msg.data().code))
|
||||||
Code::from_toad(e, msg.data().code).yield_to_java(e)
|
.yield_to_java_or_throw(e)
|
||||||
})
|
|
||||||
.map_err(|err| {
|
|
||||||
let err = JThrowable::from(err.downcast(e).to_local(e));
|
|
||||||
e.throw(err).unwrap()
|
|
||||||
})
|
|
||||||
.unwrap_or(*JObject::null())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -200,16 +173,12 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_addr<'local>(mut env: java:
|
|||||||
-> jobject {
|
-> jobject {
|
||||||
let e = &mut env;
|
let e = &mut env;
|
||||||
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
||||||
.toad_ref(e)
|
.try_deref(e)
|
||||||
.map(|msg| {
|
.map(|msg| {
|
||||||
let addr = InetSocketAddress::from_no_std(e, msg.addr());
|
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| {
|
.yield_to_java_or_throw(e)
|
||||||
let err = JThrowable::from(err.downcast(e).to_local(e));
|
|
||||||
e.throw(err).unwrap()
|
|
||||||
})
|
|
||||||
.unwrap_or(*JObject::null())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -218,24 +187,17 @@ pub extern "system" fn Java_dev_toad_msg_ref_Message_optionRefs<'local>(mut env:
|
|||||||
-> jobject {
|
-> jobject {
|
||||||
let e = &mut env;
|
let e = &mut env;
|
||||||
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
java::lang::Object::from_local(e, msg).upcast_to::<Message>(e)
|
||||||
.toad_ref(e)
|
.try_deref(e)
|
||||||
.map(|msg| {
|
.map(|msg| {
|
||||||
let opts = &msg.data().opts;
|
let opts = &msg.data().opts;
|
||||||
|
|
||||||
let refs =
|
opts.into_iter()
|
||||||
opts.into_iter()
|
.map(|(n, v)| {
|
||||||
.map(|(n, v)| {
|
Opt::new(e, v as *const _ as i64, n.0.into())
|
||||||
Opt::new(e, v as *const _ as i64, n.0.into())
|
})
|
||||||
})
|
.collect::<Vec<_>>()
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
refs.yield_to_java(e)
|
|
||||||
})
|
})
|
||||||
.map_err(|err| {
|
.yield_to_java_or_throw(e)
|
||||||
let err = JThrowable::from(err.downcast(e).to_local(e));
|
|
||||||
e.throw(err).unwrap()
|
|
||||||
})
|
|
||||||
.unwrap_or(*JObject::null())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
use jni::objects::{JClass, JObject};
|
use jni::objects::{JClass, JObject};
|
||||||
use jni::sys::jobject;
|
use jni::sys::jobject;
|
||||||
use jni::JNIEnv;
|
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 toad_msg::OptNumber;
|
||||||
|
|
||||||
use super::OptValue;
|
use super::OptValue;
|
||||||
|
use crate::dev::toad::ffi::Ptr;
|
||||||
use crate::mem::{Shared, SharedMemoryRegion};
|
use crate::mem::{Shared, SharedMemoryRegion};
|
||||||
|
|
||||||
pub struct Opt(pub java::lang::Object);
|
pub struct Opt(pub java::lang::Object);
|
||||||
@ -21,6 +23,11 @@ impl Opt {
|
|||||||
CTOR.invoke(env, addr, num)
|
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 {
|
pub fn number(&self, env: &mut java::Env) -> OptNumber {
|
||||||
static NUMBER: java::Field<Opt, i64> = java::Field::new("number");
|
static NUMBER: java::Field<Opt, i64> = java::Field::new("number");
|
||||||
OptNumber(NUMBER.get(env, self) as u32)
|
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");
|
static VALUES: java::Method<Opt, fn() -> Vec<OptValue>> = java::Method::new("valueRefs");
|
||||||
VALUES.invoke(env, self)
|
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]
|
#[no_mangle]
|
||||||
pub extern "system" fn Java_dev_toad_msg_ref_Option_number<'local>(mut env: JNIEnv<'local>,
|
pub extern "system" fn Java_dev_toad_msg_ref_Option_valueRefs<'local>(mut e: JNIEnv<'local>,
|
||||||
o: JObject<'local>,
|
o: JObject<'local>)
|
||||||
p: i64)
|
-> jobject {
|
||||||
-> i64 {
|
let e = &mut e;
|
||||||
java::lang::Object::from_local(&mut env, o).upcast_to::<Opt>(&mut env)
|
java::lang::Object::from_local(e, o).upcast_to::<Opt>(e)
|
||||||
.number(&mut env)
|
.try_deref(e)
|
||||||
.0 as i64
|
.map(|values| {
|
||||||
}
|
values.iter()
|
||||||
|
.map(|v| OptValue::new(e, (v as *const toad_msg::OptValue<Vec<u8>>).addr() as i64))
|
||||||
#[no_mangle]
|
.collect::<Vec<_>>()
|
||||||
pub extern "system" fn Java_dev_toad_msg_ref_Option_values<'local>(mut e: JNIEnv<'local>,
|
})
|
||||||
_: JClass<'local>,
|
.yield_to_java_or_throw(e)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
use jni::objects::JClass;
|
use jni::objects::{JClass, JObject};
|
||||||
use jni::sys::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};
|
use crate::mem::{Shared, SharedMemoryRegion};
|
||||||
|
|
||||||
pub struct OptValue(java::lang::Object);
|
pub struct OptValue(java::lang::Object);
|
||||||
@ -17,6 +19,20 @@ impl OptValue {
|
|||||||
CTOR.invoke(env, addr)
|
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> {
|
pub fn bytes(&self, env: &mut java::Env) -> Vec<u8> {
|
||||||
static AS_BYTES: java::Method<OptValue, fn() -> Vec<i8>> = java::Method::new("asBytes");
|
static AS_BYTES: java::Method<OptValue, fn() -> Vec<i8>> = java::Method::new("asBytes");
|
||||||
AS_BYTES.invoke(env, self)
|
AS_BYTES.invoke(env, self)
|
||||||
@ -27,13 +43,16 @@ impl OptValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "system" fn Java_dev_toad_msg_ref_OptionValue_bytes<'local>(mut env: java::Env<'local>,
|
pub extern "system" fn Java_dev_toad_msg_ref_OptionValue_asBytes<'local>(mut env: java::Env<'local>,
|
||||||
_: JClass<'local>,
|
val: JObject<'local>)
|
||||||
p: i64)
|
-> jobject {
|
||||||
-> jobject {
|
let e = &mut env;
|
||||||
let val = unsafe {
|
java::lang::Object::from_local(e, val).upcast_to::<OptValue>(e)
|
||||||
Shared::deref::<toad_msg::OptValue<Vec<u8>>>(p).as_ref()
|
.try_deref(e)
|
||||||
.unwrap()
|
.map(|val| {
|
||||||
};
|
let arr =
|
||||||
env.byte_array_from_slice(val.as_bytes()).unwrap().as_raw()
|
e.byte_array_from_slice(val.as_bytes()).unwrap();
|
||||||
|
java::lang::Object::from_local(e, arr)
|
||||||
|
})
|
||||||
|
.yield_to_java_or_throw(e)
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ public class Option implements dev.toad.msg.Option, AutoCloseable {
|
|||||||
Ptr ptr;
|
Ptr ptr;
|
||||||
final long number;
|
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) {
|
Option(long addr, long number) {
|
||||||
this.ptr = Ptr.register(this.getClass(), addr);
|
this.ptr = Ptr.register(this.getClass(), addr);
|
||||||
@ -21,12 +21,8 @@ public class Option implements dev.toad.msg.Option, AutoCloseable {
|
|||||||
return this.number;
|
return this.number;
|
||||||
}
|
}
|
||||||
|
|
||||||
public dev.toad.msg.ref.OptionValue[] valueRefs() {
|
|
||||||
return this.values(this.ptr.addr());
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<dev.toad.msg.OptionValue> values() {
|
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() {
|
public dev.toad.msg.owned.Option toOwned() {
|
||||||
|
@ -5,20 +5,16 @@ import dev.toad.ffi.Ptr;
|
|||||||
public final class OptionValue
|
public final class OptionValue
|
||||||
implements dev.toad.msg.OptionValue, AutoCloseable {
|
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) {
|
OptionValue(long addr) {
|
||||||
this.ptr = Ptr.register(this.getClass(), addr);
|
this.ptr = Ptr.register(this.getClass(), addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] asBytes() {
|
|
||||||
return this.bytes(this.ptr.addr());
|
|
||||||
}
|
|
||||||
|
|
||||||
public String asString() {
|
public String asString() {
|
||||||
return new String(this.bytes(this.ptr.addr()));
|
return new String(this.asBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
public dev.toad.msg.owned.OptionValue toOwned() {
|
public dev.toad.msg.owned.OptionValue toOwned() {
|
||||||
|
Loading…
Reference in New Issue
Block a user