2024-09-17 02:40:03 +02:00
|
|
|
use jni::{objects::{JClass, JObject}, sys::{jboolean, jlong, jobject, jstring}, JNIEnv};
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
use crate::api::{Controller, TextChange};
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
use super::{handle_error, null_check, tokio, Deobjectify, JExceptable, JObjectify};
|
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-17 17:37:22 +02:00
|
|
|
let content = 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-09-17 17:37:22 +02:00
|
|
|
/// Tries to fetch a [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-17 17:37:22 +02:00
|
|
|
tokio().block_on(controller.try_recv())
|
2024-09-17 02:40:03 +02:00
|
|
|
.jexcept(&mut env)
|
|
|
|
.map(|change| change.jobjectify(&mut env).jexcept(&mut env).as_raw())
|
|
|
|
.unwrap_or_else(std::ptr::null_mut)
|
2024-08-07 10:22:01 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
/// Blocks until it receives a [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-17 17:37:22 +02:00
|
|
|
tokio().block_on(controller.recv())
|
2024-09-17 02:40:03 +02:00
|
|
|
.jexcept(&mut env)
|
|
|
|
.jobjectify(&mut env)
|
|
|
|
.jexcept(&mut env)
|
|
|
|
.as_raw()
|
2024-08-06 23:55:57 +02:00
|
|
|
}
|
2024-09-15 01:56:51 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
/// Receive from Java, converts and sends a [TextChange].
|
2024-09-16 00:20:03 +02:00
|
|
|
#[no_mangle]
|
2024-09-17 02:40:03 +02:00
|
|
|
pub extern "system" fn Java_mp_code_BufferController_send<'local>(
|
2024-09-17 17:37:22 +02:00
|
|
|
mut env: JNIEnv<'local>,
|
2024-09-17 02:40:03 +02:00
|
|
|
_class: JClass<'local>,
|
2024-09-16 00:20:03 +02:00
|
|
|
self_ptr: jlong,
|
2024-09-17 17:37:22 +02:00
|
|
|
change: JObject<'local>,
|
2024-09-16 00:20:03 +02:00
|
|
|
) {
|
2024-09-17 17:37:22 +02:00
|
|
|
null_check!(env, change, {});
|
2024-09-17 02:40:03 +02:00
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
let change = TextChange::deobjectify(&mut env, change);
|
|
|
|
if let Ok(change) = change {
|
|
|
|
tokio().block_on(controller.send(change)).jexcept(&mut env)
|
|
|
|
} else {
|
|
|
|
handle_error!(&mut env, change, {});
|
|
|
|
}
|
2024-09-16 00:20:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-17 17:37:22 +02:00
|
|
|
null_check!(env, cb, {});
|
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
|
|
|
}
|
|
|
|
|
2024-09-17 02:40:03 +02:00
|
|
|
/// Clears the callback for buffer changes.
|
2024-09-15 01:56:51 +02:00
|
|
|
#[no_mangle]
|
2024-09-17 02:40:03 +02:00
|
|
|
pub extern "system" fn Java_mp_code_BufferController_clear_1callback(
|
|
|
|
_env: JNIEnv,
|
|
|
|
_class: JClass,
|
2024-09-15 01:56:51 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) {
|
2024-09-17 02:40:03 +02:00
|
|
|
unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) }
|
|
|
|
.clear_callback();
|
|
|
|
}
|
2024-09-15 01:56:51 +02:00
|
|
|
|
2024-09-17 02:40:03 +02:00
|
|
|
/// Blocks until there is a new value available.
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_BufferController_poll(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass,
|
|
|
|
self_ptr: jlong,
|
|
|
|
) {
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
tokio().block_on(controller.poll())
|
2024-09-15 01:56:51 +02:00
|
|
|
.jexcept(&mut env);
|
2024-09-17 02:40:03 +02:00
|
|
|
}
|
2024-09-15 01:56:51 +02:00
|
|
|
|
2024-09-17 02:40:03 +02:00
|
|
|
/// Stops the controller.
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_BufferController_stop(
|
|
|
|
_env: JNIEnv,
|
|
|
|
_class: JClass,
|
|
|
|
self_ptr: jlong,
|
|
|
|
) -> jboolean {
|
2024-09-15 01:56:51 +02:00
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-09-17 02:40:03 +02:00
|
|
|
controller.stop() as jboolean
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called by the Java GC to drop a [crate::buffer::Controller].
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_BufferController_free(
|
|
|
|
_env: JNIEnv,
|
|
|
|
_class: JClass,
|
|
|
|
self_ptr: jlong,
|
|
|
|
) {
|
|
|
|
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::buffer::Controller) };
|
2024-09-15 01:56:51 +02:00
|
|
|
}
|