mirror of
https://github.com/hexedtech/codemp.git
synced 2024-12-22 21:04:53 +01:00
feat: get_buffer, get_cursor, get_workspace_id
This commit is contained in:
parent
72e86a8079
commit
7d90793467
4 changed files with 59 additions and 13 deletions
|
@ -35,7 +35,7 @@ tracing-subscriber = { version = "0.3.18", optional = true }
|
|||
|
||||
# glue (java)
|
||||
jni = { version = "0.21.1", features = ["invocation"], optional = true }
|
||||
#jni-sys = { version = "0.3.0", optional = true }
|
||||
|
||||
# glue (lua)
|
||||
mlua = { version = "0.9.6", features = ["module", "luajit", "send"], optional = true }
|
||||
derive_more = { version = "0.99.17", optional = true }
|
||||
|
|
|
@ -6,7 +6,7 @@ use super::{util::JExceptable, RT};
|
|||
/// Called by the Java GC to drop a [Client].
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_mp_code_Client_free(_env: JNIEnv, _class: JClass, input: jlong) {
|
||||
super::util::dereference_and_drop::<Client>(input)
|
||||
let _ = unsafe { Box::from_raw(input as *mut Client) };
|
||||
}
|
||||
|
||||
/// Sets up tracing subscriber
|
||||
|
@ -38,6 +38,21 @@ pub extern "system" fn Java_mp_code_Client_connect<'local>(
|
|||
.jexcept(&mut env)
|
||||
}
|
||||
|
||||
/// Gets a [Workspace] by name and returns a pointer to it.
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_mp_code_Client_get_1workspace<'local>(
|
||||
env: JNIEnv<'local>,
|
||||
_class: JClass<'local>,
|
||||
self_ptr: jlong,
|
||||
input: JString<'local>
|
||||
) -> jlong {
|
||||
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
||||
let workspace_id = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") };
|
||||
client.get_workspace(workspace_id.to_str().expect("Not UTF-8"))
|
||||
.map(|workspace| Box::into_raw(Box::new(workspace)) as jlong)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Logs in to a specific [Workspace].
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_mp_code_Client_login<'local>(
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
use jni::{JNIEnv, sys::jlong};
|
||||
|
||||
/// A simple utility method that converts a pointer back into a [Box] and then drops it.
|
||||
pub(crate) fn dereference_and_drop<T>(ptr: jlong) {
|
||||
let client : Box<T> = unsafe { Box::from_raw(ptr as *mut T) };
|
||||
std::mem::drop(client)
|
||||
}
|
||||
|
||||
/// A trait meant for our [crate::Result] type to make converting it to Java easier.
|
||||
pub(crate) trait JExceptable<T> {
|
||||
/// Unwraps it and throws an appropriate Java exception if it's an error.
|
||||
|
|
|
@ -1,12 +1,49 @@
|
|||
use jni::{objects::{JClass, JString}, sys::jlong, JNIEnv};
|
||||
use crate::{Client, Workspace};
|
||||
use jni::{objects::{JClass, JString}, sys::{jlong, jstring}, JNIEnv};
|
||||
use crate::Workspace;
|
||||
|
||||
use super::{util::JExceptable, RT};
|
||||
|
||||
/// Called by the Java GC to drop a [Workspace].
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_mp_code_Workspace_free(_env: JNIEnv, _class: JClass, input: jlong) {
|
||||
super::util::dereference_and_drop::<Client>(input)
|
||||
let _ = unsafe { Box::from_raw(input as *mut Workspace) };
|
||||
}
|
||||
|
||||
/// Gets the workspace id.
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_mp_code_Client_get_1workspace_1id<'local>(
|
||||
env: JNIEnv<'local>,
|
||||
_class: JClass<'local>,
|
||||
self_ptr: jlong
|
||||
) -> jstring {
|
||||
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
||||
env.new_string(workspace.id())
|
||||
.expect("Failed to convert to Java String!")
|
||||
.as_raw()
|
||||
}
|
||||
|
||||
/// Gets a cursor controller by name and returns a pointer to it.
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_mp_code_Client_get_1cursor<'local>(
|
||||
_env: JNIEnv<'local>,
|
||||
_class: JClass<'local>,
|
||||
self_ptr: jlong
|
||||
) -> jlong {
|
||||
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
||||
Box::into_raw(Box::new(workspace.cursor())) as jlong
|
||||
}
|
||||
|
||||
/// Gets a buffer controller by name and returns a pointer to it.
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_mp_code_Client_get_1buffer<'local>(
|
||||
env: JNIEnv<'local>,
|
||||
_class: JClass<'local>,
|
||||
self_ptr: jlong,
|
||||
input: JString<'local>
|
||||
) -> jlong {
|
||||
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!") };
|
||||
Box::into_raw(Box::new(workspace.buffer_by_name(path.to_str().expect("Not UTF-8")))) as jlong
|
||||
}
|
||||
|
||||
/// Creates a [Buffer]
|
||||
|
@ -18,6 +55,6 @@ pub extern "system" fn Java_mp_code_Workspace_create_1buffer<'local>(
|
|||
input: JString<'local>
|
||||
) {
|
||||
let ws: Box<Workspace> = unsafe { Box::from_raw(self_ptr as *mut Workspace) };
|
||||
let path: String = env.get_string(&input).expect("Couldn't get java string!").into();
|
||||
RT.block_on(ws.create(&path)).jexcept(&mut env);
|
||||
let path = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") };
|
||||
RT.block_on(ws.create(path.to_str().expect("Not UTF-8"))).jexcept(&mut env);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue