codemp/src/ffi/java/buffer.rs

90 lines
2.7 KiB
Rust
Raw Normal View History

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 = RT.block_on(controller.content())
.jexcept(&mut env);
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)) };
2024-08-14 00:27:26 +02:00
let change = RT.block_on(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);
let hash = env.find_class("java/util/OptionalLong").and_then(|class| {
if let Some(h) = event.hash {
env.call_static_method(class, "of", "(J)Ljava/util/OptionalLong;", &[JValueGen::Long(h)])
} else {
env.call_static_method(class, "empty", "()Ljava/util/OptionalLong;", &[])
}
}).and_then(|o| o.l()).jexcept(env);
env.find_class("mp/code/data/TextChange")
.and_then(|class| {
env.new_object(
class,
"(JJLjava/lang/String;Ljava/util/OptionalLong;)V",
&[
JValueGen::Long(jlong::from(event.start)),
JValueGen::Long(jlong::from(event.end)),
JValueGen::Object(&content),
JValueGen::Object(&hash)
]
)
}).jexcept(env)
2024-08-07 01:44:27 +02:00
}
}.as_raw()
}