2024-08-19 11:36:51 +02:00
|
|
|
use jni::{objects::{JClass, JObject, JValueGen}, sys::{jlong, jobject, jstring}, JNIEnv};
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-09-16 00:20:03 +02:00
|
|
|
use crate::api::Controller;
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-09-16 00:20:03 +02:00
|
|
|
use super::JExceptable;
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Gets the name of the buffer.
|
2024-08-06 23:55:57 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_BufferController_get_1name(
|
2024-08-10 02:45:20 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-06 23:55:57 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) -> jstring {
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-09-05 02:45:33 +02:00
|
|
|
let content = controller.path();
|
2024-08-06 23:55:57 +02:00
|
|
|
env.new_string(content)
|
2024-08-10 02:45:20 +02:00
|
|
|
.jexcept(&mut env)
|
2024-08-06 23:55:57 +02:00
|
|
|
.as_raw()
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Gets the contents of the buffers.
|
2024-08-06 23:55:57 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_BufferController_get_1content(
|
2024-08-10 02:45:20 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-06 23:55:57 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) -> jstring {
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-09-16 00:20:03 +02:00
|
|
|
let content = super::tokio().block_on(controller.content())
|
2024-08-13 21:58:40 +02:00
|
|
|
.jexcept(&mut env);
|
2024-08-06 23:55:57 +02:00
|
|
|
env.new_string(content)
|
2024-08-10 02:45:20 +02:00
|
|
|
.jexcept(&mut env)
|
2024-08-06 23:55:57 +02:00
|
|
|
.as_raw()
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Tries to fetch a [crate::api::TextChange], or returns null if there's nothing.
|
2024-08-06 23:55:57 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_BufferController_try_1recv(
|
2024-08-06 23:55:57 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-06 23:55:57 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) -> jobject {
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-09-16 00:20:03 +02:00
|
|
|
let change = super::tokio().block_on(controller.try_recv()).jexcept(&mut env);
|
2024-08-07 10:22:01 +02:00
|
|
|
recv_jni(&mut env, change)
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Blocks until it receives a [crate::api::TextChange].
|
2024-08-07 10:22:01 +02:00
|
|
|
#[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)) };
|
2024-09-16 00:20:03 +02:00
|
|
|
let change = super::tokio().block_on(controller.recv()).map(Some).jexcept(&mut env);
|
2024-08-07 10:22:01 +02:00
|
|
|
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.
|
2024-08-07 10:22:01 +02:00
|
|
|
fn recv_jni(env: &mut JNIEnv, change: Option<crate::api::TextChange>) -> jobject {
|
|
|
|
match change {
|
2024-08-10 02:45:20 +02:00
|
|
|
None => JObject::default(),
|
2024-08-07 01:44:27 +02:00
|
|
|
Some(event) => {
|
2024-08-10 02:45:20 +02:00
|
|
|
let content = env.new_string(event.content).jexcept(env);
|
2024-08-16 01:21:21 +02:00
|
|
|
|
|
|
|
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);
|
2024-08-10 02:45:20 +02:00
|
|
|
env.find_class("mp/code/data/TextChange")
|
|
|
|
.and_then(|class| {
|
|
|
|
env.new_object(
|
|
|
|
class,
|
2024-08-16 01:21:21 +02:00
|
|
|
"(JJLjava/lang/String;Ljava/util/OptionalLong;)V",
|
2024-08-10 02:45:20 +02:00
|
|
|
&[
|
|
|
|
JValueGen::Long(jlong::from(event.start)),
|
|
|
|
JValueGen::Long(jlong::from(event.end)),
|
|
|
|
JValueGen::Object(&content),
|
2024-08-16 01:21:21 +02:00
|
|
|
JValueGen::Object(&hash)
|
2024-08-10 02:45:20 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}).jexcept(env)
|
2024-08-07 01:44:27 +02:00
|
|
|
}
|
2024-08-10 02:45:20 +02:00
|
|
|
}.as_raw()
|
2024-08-06 23:55:57 +02:00
|
|
|
}
|
2024-09-15 01:56:51 +02:00
|
|
|
|
2024-09-16 00:20:03 +02:00
|
|
|
/// Clears the callback for buffer changes.
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_BufferController_clear_1callback(
|
|
|
|
_env: JNIEnv,
|
|
|
|
_class: JClass,
|
|
|
|
self_ptr: jlong,
|
|
|
|
) {
|
|
|
|
unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) }
|
|
|
|
.clear_callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Registers a callback for buffer changes.
|
2024-09-15 01:56:51 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_BufferController_callback<'local>(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
cb: JObject<'local>,
|
|
|
|
) {
|
2024-09-16 00:20:03 +02:00
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
|
|
|
|
|
|
|
let Ok(cb_ref) = env.new_global_ref(cb) else {
|
|
|
|
env.throw_new("mp/code/exceptions/JNIException", "Failed to pin callback reference!")
|
|
|
|
.expect("Failed to throw exception!");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
controller.callback(move |controller: crate::buffer::Controller| {
|
|
|
|
let jvm = super::jvm();
|
|
|
|
let mut env = jvm.attach_current_thread_permanently()
|
|
|
|
.expect("failed attaching to main JVM thread");
|
|
|
|
if let Err(e) = env.with_local_frame(5, |env| {
|
|
|
|
use crate::ffi::java::JObjectify;
|
|
|
|
let jcontroller = controller.jobjectify(env)?;
|
|
|
|
let sig = format!("(L{};)V", "java/lang/Object");
|
|
|
|
if let Err(e) = env.call_method(
|
|
|
|
&cb_ref,
|
|
|
|
"invoke",
|
|
|
|
&sig,
|
|
|
|
&[jni::objects::JValueGen::Object(&jcontroller)]
|
|
|
|
) {
|
|
|
|
tracing::error!("error invoking callback: {e:?}");
|
|
|
|
};
|
|
|
|
Ok::<(), jni::errors::Error>(())
|
|
|
|
}) {
|
|
|
|
tracing::error!("error invoking callback: {e}");
|
|
|
|
let _ = env.exception_describe();
|
|
|
|
}
|
|
|
|
});
|
2024-09-15 01:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive 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-09-16 02:59:43 +02:00
|
|
|
let Ok(start) = env.get_field(&input, "start", "J")
|
|
|
|
.and_then(|sr| sr.j())
|
2024-09-15 01:56:51 +02:00
|
|
|
.jexcept(&mut env)
|
|
|
|
.try_into()
|
|
|
|
else {
|
|
|
|
env.throw_new("java/lang/IllegalArgumentException", "Start index cannot be negative!")
|
|
|
|
.expect("Failed to throw exception!");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2024-09-16 02:59:43 +02:00
|
|
|
let Ok(end) = env.get_field(&input, "end", "J")
|
|
|
|
.and_then(|er| er.j())
|
2024-09-15 01:56:51 +02:00
|
|
|
.jexcept(&mut env)
|
|
|
|
.try_into()
|
|
|
|
else {
|
|
|
|
env.throw_new("java/lang/IllegalArgumentException", "End index cannot be negative!")
|
|
|
|
.expect("Failed to throw exception!");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let content = env.get_field(&input, "content", "Ljava/lang/String;")
|
|
|
|
.and_then(|b| b.l())
|
|
|
|
.map(|b| b.into())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
let content = env.get_string(&content)
|
|
|
|
.map(|b| b.into())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
|
2024-09-16 02:59:43 +02:00
|
|
|
let hash = env.get_field(&input, "hash", "Ljava/util/OptionalLong;")
|
2024-09-15 01:56:51 +02:00
|
|
|
.and_then(|hash| hash.l())
|
|
|
|
.and_then(|hash| {
|
|
|
|
if env.call_method(&hash, "isPresent", "()Z", &[]).and_then(|r| r.z()).jexcept(&mut env) {
|
|
|
|
env.call_method(&hash, "getAsLong", "()J", &[])
|
|
|
|
.and_then(|r| r.j())
|
|
|
|
.map(Some)
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}).jexcept(&mut env);
|
|
|
|
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-09-16 00:20:03 +02:00
|
|
|
super::tokio().block_on(controller.send(crate::api::TextChange {
|
2024-09-15 01:56:51 +02:00
|
|
|
start,
|
|
|
|
end,
|
|
|
|
content,
|
|
|
|
hash,
|
|
|
|
})).jexcept(&mut env);
|
|
|
|
}
|