codemp/src/ffi/java/buffer.rs

117 lines
3.4 KiB
Rust
Raw Normal View History

2024-08-07 01:44:27 +02:00
use jni::{objects::{JClass, JObject, JValueGen}, sys::{jlong, jobject, jstring}, JNIEnv};
use crate::api::Controller;
use super::{JExceptable, RT};
2024-08-08 00:29:54 +02:00
/// Gets the name of the buffer.
#[no_mangle]
2024-08-07 01:44:27 +02:00
pub extern "system" fn Java_mp_code_BufferController_get_1name(
mut env: JNIEnv,
2024-08-07 01:44:27 +02:00
_class: JClass,
self_ptr: jlong,
) -> jstring {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
let content = controller.name();
env.new_string(content)
.jexcept(&mut env)
.as_raw()
}
2024-08-08 00:29:54 +02:00
/// Gets the contents of the buffers.
#[no_mangle]
2024-08-07 01:44:27 +02:00
pub extern "system" fn Java_mp_code_BufferController_get_1content(
mut env: JNIEnv,
2024-08-07 01:44:27 +02:00
_class: JClass,
self_ptr: jlong,
) -> jstring {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
let content = controller.content();
env.new_string(content)
.jexcept(&mut env)
.as_raw()
}
2024-08-08 00:29:54 +02:00
/// Tries to fetch a [crate::api::TextChange], or returns null if there's nothing.
#[no_mangle]
2024-08-07 01:44:27 +02:00
pub extern "system" fn Java_mp_code_BufferController_try_1recv(
mut env: JNIEnv,
2024-08-07 01:44:27 +02:00
_class: JClass,
self_ptr: jlong,
) -> jobject {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
let change = controller.try_recv().jexcept(&mut env);
recv_jni(&mut env, change)
}
2024-08-08 00:29:54 +02:00
/// Blocks until it receives a [crate::api::TextChange].
#[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_recv(
mut env: JNIEnv,
_class: JClass,
self_ptr: jlong,
) -> jobject {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
let change = RT.block_on(controller.recv()).map(Some).jexcept(&mut env);
recv_jni(&mut env, change)
}
2024-08-08 00:29:54 +02:00
/// Utility method to convert a [crate::api::TextChange] to its Java equivalent.
fn recv_jni(env: &mut JNIEnv, change: Option<crate::api::TextChange>) -> jobject {
match change {
None => JObject::default(),
2024-08-07 01:44:27 +02:00
Some(event) => {
let content = env.new_string(event.content).jexcept(env);
env.find_class("mp/code/data/TextChange")
.and_then(|class| {
env.new_object(
class,
"(JJLjava/lang/String;)V",
&[
JValueGen::Long(jlong::from(event.start)),
JValueGen::Long(jlong::from(event.end)),
JValueGen::Object(&content),
]
)
}).jexcept(env)
2024-08-07 01:44:27 +02:00
}
}.as_raw()
}
2024-08-08 00:29:54 +02:00
/// Receives from Java, converts and sends a [crate::api::TextChange].
#[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_send<'local>(
mut env: JNIEnv,
_class: JClass<'local>,
self_ptr: jlong,
input: JObject<'local>
) {
let start = env.get_field(&input, "start", "J").and_then(|s| s.j()).jexcept(&mut env);
let end = env.get_field(&input, "end", "J").and_then(|e| e.j()).jexcept(&mut env);
2024-08-07 01:44:27 +02:00
let content = env.get_field(&input, "content", "Ljava/lang/String;")
.and_then(|c| c.l())
.map(|c| c.into())
.jexcept(&mut env);
let content = unsafe { env.get_string_unchecked(&content) }
.map(|c| c.to_string_lossy().to_string())
.jexcept(&mut env);
2024-08-07 01:44:27 +02:00
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
controller.send(crate::api::TextChange {
2024-08-08 00:29:54 +02:00
start: start as u32,
end: end as u32,
2024-08-07 01:44:27 +02:00
content
}).jexcept(&mut env);
}
2024-08-08 00:29:54 +02:00
/// Called by the Java GC to drop a [crate::buffer::Controller].
#[no_mangle]
2024-08-07 01:44:27 +02:00
pub extern "system" fn Java_mp_code_BufferController_free(
_env: JNIEnv,
2024-08-07 01:44:27 +02:00
_class: JClass,
self_ptr: jlong,
) {
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
}