codemp/src/ffi/java/buffer.rs

113 lines
3.5 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::{util::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(
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)
.expect("could not create jstring")
.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(
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)
.expect("could not create jstring")
.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 {
2024-08-07 01:44:27 +02:00
None => JObject::null().as_raw(),
Some(event) => {
let class = env.find_class("mp/code/data/TextChange").expect("Couldn't find class!");
env.new_object(
class,
"(JJLjava/lang/String;)V",
&[
2024-08-08 00:29:54 +02:00
JValueGen::Long(jlong::from(event.start)),
JValueGen::Long(jlong::from(event.end)),
2024-08-07 01:44:27 +02:00
JValueGen::Object(&env.new_string(event.content).expect("Failed to create String!")),
]
).expect("failed creating object").into_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>
) {
2024-08-07 01:44:27 +02:00
let start = env.get_field(&input, "start", "J").expect("could not get field").j().expect("field was not of expected type");
let end = env.get_field(&input, "end", "J").expect("could not get field").j().expect("field was not of expected type");
let content = env.get_field(&input, "content", "Ljava/lang/String;")
.expect("could not get field")
.l()
.expect("field was not of expected type")
.into();
let content = env.get_string(&content).expect("Failed to get String!").into();
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) };
}