feat: buffer controller, cleanup

This commit is contained in:
zaaarf 2024-08-07 01:44:27 +02:00
parent 329287df53
commit 4bb35f1727
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
4 changed files with 102 additions and 114 deletions

View file

@ -1,18 +1,13 @@
use jni::{objects::{JClass, JObject}, sys::{jlong, jobject, jstring}, JNIEnv};
use jni::{objects::{JClass, JObject, JValueGen}, sys::{jlong, jobject, jstring}, JNIEnv};
use crate::api::Controller;
use super::util::JExceptable;
/*
* Class: mp_code_BufferController
* Method: get_name
* Signature: (J)Ljava/lang/String;
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_get_1name<'local>(
pub extern "system" fn Java_mp_code_BufferController_get_1name(
env: JNIEnv,
_class: JClass<'local>,
_class: JClass,
self_ptr: jlong,
) -> jstring {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
@ -22,15 +17,10 @@ pub extern "system" fn Java_mp_code_BufferController_get_1name<'local>(
.as_raw()
}
/*
* Class: mp_code_BufferController
* Method: get_content
* Signature: (J)Ljava/lang/String;
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_get_1content<'local>(
pub extern "system" fn Java_mp_code_BufferController_get_1content(
env: JNIEnv,
_class: JClass<'local>,
_class: JClass,
self_ptr: jlong,
) -> jstring {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
@ -40,27 +30,30 @@ pub extern "system" fn Java_mp_code_BufferController_get_1content<'local>(
.as_raw()
}
/*
* Class: mp_code_BufferController
* Method: try_recv
* Signature: (J)Lmp/code/data/TextChange;
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_try_1recv<'local>(
pub extern "system" fn Java_mp_code_BufferController_try_1recv(
mut env: JNIEnv,
_class: JClass<'local>,
_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);
todo!()
match controller.try_recv().jexcept(&mut env) {
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",
&[
JValueGen::Long(event.span.start.try_into().unwrap()),
JValueGen::Long(event.span.end.try_into().unwrap()),
JValueGen::Object(&env.new_string(event.content).expect("Failed to create String!")),
]
).expect("failed creating object").into_raw()
}
}
}
/*
* Class: mp_code_BufferController
* Method: send
* Signature: (JLmp/code/data/TextChange;)V
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_send<'local>(
mut env: JNIEnv,
@ -68,18 +61,26 @@ pub extern "system" fn Java_mp_code_BufferController_send<'local>(
self_ptr: jlong,
input: JObject<'local>
) {
todo!()
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 {
span: (start as usize)..(end as usize),
content
}).jexcept(&mut env);
}
/*
* Class: mp_code_BufferController
* Method: free
* Signature: (J)V
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_free<'local>(
pub extern "system" fn Java_mp_code_BufferController_free(
_env: JNIEnv,
_class: JClass<'local>,
_class: JClass,
self_ptr: jlong,
) {
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };

View file

@ -1,11 +1,6 @@
use jni::{objects::{JClass, JObject, JValueGen}, sys::{jlong, jobject}, JNIEnv};
use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jlong, jobject}, JNIEnv};
use crate::{api::Controller, ffi::java::util::JExceptable};
/*
* Class: mp_code_CursorController
* Method: recv
* Signature: (J)Lmp/code/data/Cursor;
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_try_1recv(
mut env: JNIEnv,
@ -32,13 +27,7 @@ pub extern "system" fn Java_mp_code_CursorController_try_1recv(
}
}
}
// public Cursor(int startRow, int startCol, int endRow, int endCol, String buffer, String user) {
/*
* Class: mp_code_CursorController
* Method: send
* Signature: (JLmp/code/data/Cursor;)V
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_send<'local>(
mut env: JNIEnv,
@ -46,18 +35,43 @@ pub extern "system" fn Java_mp_code_CursorController_send<'local>(
self_ptr: jlong,
input: JObject<'local>,
) {
todo!()
let start_row = env.get_field(&input, "startRow", "I").expect("could not get field").i().expect("field was not of expected type");
let start_col = env.get_field(&input, "startCol", "I").expect("could not get field").i().expect("field was not of expected type");
let end_row = env.get_field(&input, "endRow", "I").expect("could not get field").i().expect("field was not of expected type");
let end_col = env.get_field(&input, "endCol", "I").expect("could not get field").i().expect("field was not of expected type");
let buffer = env.get_field(&input, "buffer", "Ljava/lang/String;")
.expect("could not get field")
.l()
.expect("field was not of expected type")
.into();
let buffer = env.get_string(&buffer).expect("Failed to get String!").into();
let user: JString = env.get_field(&input, "user", "Ljava/lang/String;")
.expect("could not get field")
.l()
.expect("field was not of expected type")
.into();
let user = if user.is_null() {
None
} else {
let jstring = env.get_string(&user).expect("Failed to get String!");
Some(uuid::Uuid::parse_str(jstring.to_str().expect("Not valid UTF-8")).expect("Invalid UUI!"))
};
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::cursor::Controller)) };
controller.send(crate::api::Cursor {
start: (start_row, start_col),
end: (end_row, end_col),
buffer,
user
}).jexcept(&mut env);
}
/*
* Class: mp_code_CursorController
* Method: free
* Signature: (J)V
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_free<'local>(
pub extern "system" fn Java_mp_code_CursorController_free(
_env: JNIEnv,
_class: JClass<'local>,
_class: JClass,
self_ptr: jlong,
) {
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };

View file

@ -1,4 +1,4 @@
use jni::{JNIEnv, sys::jlong};
use jni::JNIEnv;
/// A trait meant for our [crate::Result] type to make converting it to Java easier.
pub(crate) trait JExceptable<T> {

View file

@ -46,7 +46,7 @@ pub extern "system" fn Java_mp_code_Workspace_get_1buffer<'local>(
Box::into_raw(Box::new(workspace.buffer_by_name(path.to_str().expect("Not UTF-8")))) as jlong
}
/// Creates a [Buffer]
/// Creates a new buffer.
#[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_create_1buffer<'local>(
mut env: JNIEnv,
@ -59,71 +59,56 @@ pub extern "system" fn Java_mp_code_Workspace_create_1buffer<'local>(
RT.block_on(ws.create(path.to_str().expect("Not UTF-8"))).jexcept(&mut env);
}
/*
* Class: mp_code_Workspace
* Method: get_file_tree
* Signature: (J)V
*/
/// Gets the filetree.
#[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree<'local>(
mut env: JNIEnv,
_class: JClass<'local>,
pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree(
_env: JNIEnv,
_class: JClass,
self_ptr: jlong,
) {
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
let file_tree = workspace.filetree();
todo!() // how to return Vec<String> ? []String ?
let _file_tree = workspace.filetree();
todo!() // how to return Vec<String> ? []String ?
}
/*
* Class: mp_code_Workspace
* Method: attach_to_buffer
* Signature: (J)J
*/
/// Attaches to a buffer and returns a pointer to its [crate::buffer::Controller].
#[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_attach_1to_1buffer<'local>(
mut env: JNIEnv,
_class: JClass<'local>,
_self_ptr: jlong,
self_ptr: jlong,
input: JString<'local>
) -> jlong {
todo!()
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
let path = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") };
RT.block_on(workspace.attach(path.to_str().expect("Not UTF-8!")))
.map(|buffer| Box::into_raw(Box::new(buffer)) as jlong)
.jexcept(&mut env)
}
/*
* Class: mp_code_Workspace
* Method: fetch_buffers
* Signature: (J)V
*/
/// Updates the local buffer list.
#[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers<'local>(
pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers(
mut env: JNIEnv,
_class: JClass<'local>,
_class: JClass,
self_ptr: jlong,
) {
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
RT.block_on(workspace.fetch_buffers()).jexcept(&mut env);
RT.block_on(workspace.fetch_buffers()).jexcept(&mut env);
}
/*
* Class: mp_code_Workspace
* Method: fetch_users
* Signature: (J)V
*/
/// Updates the local user list.
#[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_fetch_1users<'local>(
pub extern "system" fn Java_mp_code_Workspace_fetch_1users(
mut env: JNIEnv,
_class: JClass<'local>,
_class: JClass,
self_ptr: jlong,
) {
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
RT.block_on(workspace.fetch_users()).jexcept(&mut env);
RT.block_on(workspace.fetch_users()).jexcept(&mut env);
}
/*
* Class: mp_code_Workspace
* Method: list_buffer_users
* Signature: (JLjava/lang/String;)V
*/
/// Lists users attached to a buffer.
#[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>(
mut env: JNIEnv,
@ -132,12 +117,8 @@ pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>(
input: JString<'local>,
) {
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
let buffer : String = unsafe {
env.get_string_unchecked(&input)
.expect("Couldn't get java string!")
.into()
};
let users = RT.block_on(workspace.list_buffer_users(&buffer))
let buffer = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") };
let _users = RT.block_on(workspace.list_buffer_users(buffer.to_str().expect("Not UTF-8!")))
.jexcept(&mut env)
.into_iter()
.map(|x| x.id.to_string())
@ -145,11 +126,7 @@ pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>(
todo!() // how to return Vec<String>?
}
/*
* Class: mp_code_Workspace
* Method: delete_buffer
* Signature: (JLjava/lang/String;)V
*/
/// Deletes a buffer.
#[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_delete_1buffer<'local>(
mut env: JNIEnv,
@ -157,11 +134,7 @@ pub extern "system" fn Java_mp_code_Workspace_delete_1buffer<'local>(
self_ptr: jlong,
input: JString<'local>,
) {
let buffer : String = unsafe {
env.get_string_unchecked(&input)
.expect("Couldn't get java string!")
.into()
};
let buffer = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") };
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
RT.block_on(workspace.delete(&buffer)).jexcept(&mut env);
RT.block_on(workspace.delete(buffer.to_str().expect("Not UTF-8!"))).jexcept(&mut env);
}