Skip to content

Commit 3786fd2

Browse files
committed
fix(java): restore ptr impls
1 parent 9e3f7bf commit 3786fd2

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

src/ffi/java/mod.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,56 @@ impl jni_toolbox::IntoException for crate::errors::ControllerError {
9797
}
9898
}
9999

100+
101+
macro_rules! from_java_ptr {
102+
($type: ty) => {
103+
impl<'j> jni_toolbox::FromJava<'j> for &mut $type {
104+
type From = jni::sys::jobject;
105+
fn from_java(
106+
_env: &mut jni::Env<'j>,
107+
value: Self::From,
108+
) -> Result<Self, jni::errors::Error> {
109+
Ok(unsafe { Box::leak(Box::from_raw(value as *mut $type)) })
110+
}
111+
112+
fn from_jvalue(
113+
env: &mut jni::Env<'j>,
114+
value: jni::JValueOwned,
115+
) -> Result<Self, jni::errors::Error> {
116+
Self::from_java(env, value.l()?.into_raw())
117+
}
118+
}
119+
};
120+
}
121+
122+
from_java_ptr!(crate::Client);
123+
from_java_ptr!(crate::Workspace);
124+
from_java_ptr!(crate::cursor::Controller);
125+
from_java_ptr!(crate::buffer::Controller);
126+
127+
/// Generates a [JObjectify] implementation for a class that is just a holder for a pointer.
128+
macro_rules! into_java_ptr_class {
129+
($type: ty, $jclass: literal) => {
130+
impl<'j> jni_toolbox::IntoJavaObject<'j> for $type {
131+
const CLASS: &'static str = $jclass;
132+
fn into_java_object(
133+
self,
134+
env: &mut jni::Env<'j>,
135+
) -> Result<jni::objects::JObject<'j>, jni::errors::Error> {
136+
let class = env.find_class(jni::strings::JNIString::new(Self::CLASS))?;
137+
env.new_object(
138+
class,
139+
jni::jni_sig!((ptr: i64) -> ()),
140+
&[jni::objects::JValue::Long(
141+
Box::into_raw(Box::new(self)) as jni::sys::jlong
142+
)],
143+
)
144+
}
145+
}
146+
};
147+
}
148+
149+
into_java_ptr_class!(crate::Client, "mp/code/Client");
150+
into_java_ptr_class!(crate::Workspace, "mp/code/Workspace");
151+
into_java_ptr_class!(crate::cursor::Controller, "mp/code/CursorController");
152+
into_java_ptr_class!(crate::buffer::Controller, "mp/code/BufferController");

0 commit comments

Comments
 (0)