chore: cleanup, reorganizing java glue

This commit is contained in:
zaaarf 2024-08-08 00:29:54 +02:00
parent 487422eb99
commit 515cab331c
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
20 changed files with 52 additions and 36 deletions

26
.gitignore vendored
View file

@ -14,18 +14,18 @@ index.d.ts
index.node index.node
# java # java
java/*.iml dist/java/*.iml
java/.idea/ dist/java/.idea/
java/*.h dist/java/*.h
java/**/*.class dist/java/**/*.class
java/build/ dist/java/build/
java/.classpath dist/java/.classpath
java/.gradle/ dist/java/.gradle/
java/.project dist/java/.project
java/.settings/ dist/java/.settings/
java/bin/ dist/java/bin/
# intellij insists on creating the wrapper every time even if it's not strictly necessary # intellij insists on creating the wrapper every time even if it's not strictly necessary
java/gradle/ dist/java/gradle/
java/gradlew dist/java/gradlew
java/gradlew.bat dist/java/gradlew.bat

View file

@ -60,9 +60,8 @@ napi-build = { version = "2", optional = true }
pyo3-build-config = { version = "0.19.2", optional = true } pyo3-build-config = { version = "0.19.2", optional = true }
[features] [features]
default = ["js"] default = []
lua = ["mlua", "derive_more", "lazy_static", "tracing-subscriber"] lua = ["mlua", "derive_more", "lazy_static", "tracing-subscriber"]
java = ["lazy_static", "jni", "tracing-subscriber"] java = ["lazy_static", "jni", "tracing-subscriber"]
java-artifact = ["java"] # also builds the jar
js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"] js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"]
python = ["pyo3", "pyo3-asyncio", "tracing-subscriber", "pyo3-build-config"] python = ["pyo3", "pyo3-asyncio", "tracing-subscriber", "pyo3-build-config"]

View file

@ -1,12 +1,16 @@
plugins { plugins {
id 'java' id 'java-library'
id 'maven-publish' id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '8.1.1' id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'com.palantir.git-version' version '3.1.0' id 'com.palantir.git-version' version '3.1.0'
} }
group = 'mp.code' group = 'mp.code'
version = versionDetails().lastTag version = versionDetails().lastTag.substring(1)
java {
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
}
repositories { repositories {
mavenCentral() mavenCentral()
@ -29,6 +33,7 @@ shadowJar {
} }
def rustDir = projectDir.toPath() def rustDir = projectDir.toPath()
.parent
.parent .parent
.resolve('target') .resolve('target')
.resolve('release') .resolve('release')

View file

@ -4,6 +4,7 @@ use crate::api::Controller;
use super::{util::JExceptable, RT}; use super::{util::JExceptable, RT};
/// Gets the name of the buffer.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_get_1name( pub extern "system" fn Java_mp_code_BufferController_get_1name(
env: JNIEnv, env: JNIEnv,
@ -17,6 +18,7 @@ pub extern "system" fn Java_mp_code_BufferController_get_1name(
.as_raw() .as_raw()
} }
/// Gets the contents of the buffers.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_get_1content( pub extern "system" fn Java_mp_code_BufferController_get_1content(
env: JNIEnv, env: JNIEnv,
@ -30,6 +32,7 @@ pub extern "system" fn Java_mp_code_BufferController_get_1content(
.as_raw() .as_raw()
} }
/// Tries to fetch a [crate::api::TextChange], or returns null if there's nothing.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_try_1recv( pub extern "system" fn Java_mp_code_BufferController_try_1recv(
mut env: JNIEnv, mut env: JNIEnv,
@ -41,6 +44,7 @@ pub extern "system" fn Java_mp_code_BufferController_try_1recv(
recv_jni(&mut env, change) recv_jni(&mut env, change)
} }
/// Blocks until it receives a [crate::api::TextChange].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_recv( pub extern "system" fn Java_mp_code_BufferController_recv(
mut env: JNIEnv, mut env: JNIEnv,
@ -52,6 +56,7 @@ pub extern "system" fn Java_mp_code_BufferController_recv(
recv_jni(&mut env, change) recv_jni(&mut env, change)
} }
/// Utility method to convert a [crate::api::TextChange] to its Java equivalent.
fn recv_jni(env: &mut JNIEnv, change: Option<crate::api::TextChange>) -> jobject { fn recv_jni(env: &mut JNIEnv, change: Option<crate::api::TextChange>) -> jobject {
match change { match change {
None => JObject::null().as_raw(), None => JObject::null().as_raw(),
@ -61,16 +66,16 @@ fn recv_jni(env: &mut JNIEnv, change: Option<crate::api::TextChange>) -> jobject
class, class,
"(JJLjava/lang/String;)V", "(JJLjava/lang/String;)V",
&[ &[
JValueGen::Long(event.span.start.try_into().unwrap()), JValueGen::Long(jlong::from(event.start)),
JValueGen::Long(event.span.end.try_into().unwrap()), JValueGen::Long(jlong::from(event.end)),
JValueGen::Object(&env.new_string(event.content).expect("Failed to create String!")), JValueGen::Object(&env.new_string(event.content).expect("Failed to create String!")),
] ]
).expect("failed creating object").into_raw() ).expect("failed creating object").into_raw()
} }
} }
} }
/// Receives from Java, converts and sends a [crate::api::TextChange].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_send<'local>( pub extern "system" fn Java_mp_code_BufferController_send<'local>(
mut env: JNIEnv, mut env: JNIEnv,
@ -89,11 +94,13 @@ pub extern "system" fn Java_mp_code_BufferController_send<'local>(
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) }; let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
controller.send(crate::api::TextChange { controller.send(crate::api::TextChange {
span: (start as usize)..(end as usize), start: start as u32,
end: end as u32
content content
}).jexcept(&mut env); }).jexcept(&mut env);
} }
/// Called by the Java GC to drop a [crate::buffer::Controller].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_BufferController_free( pub extern "system" fn Java_mp_code_BufferController_free(
_env: JNIEnv, _env: JNIEnv,

View file

@ -3,12 +3,6 @@ use crate::{client::Client, Workspace};
use super::{util::JExceptable, RT}; 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) {
let _ = unsafe { Box::from_raw(input as *mut Client) };
}
/// Connects to a given URL and returns a [Client] to interact with that server. /// Connects to a given URL and returns a [Client] to interact with that server.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_connect<'local>( pub extern "system" fn Java_mp_code_Client_connect<'local>(
@ -110,3 +104,9 @@ pub extern "system" fn Java_mp_code_Client_setup_1tracing<'local>(
.map(|p| env.get_string(&p).expect("couldn't get java string").into()) .map(|p| env.get_string(&p).expect("couldn't get java string").into())
); );
} }
/// 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) {
let _ = unsafe { Box::from_raw(input as *mut Client) };
}

View file

@ -3,6 +3,7 @@ use crate::{api::Controller, ffi::java::util::JExceptable};
use super::RT; use super::RT;
/// Tries to fetch a [crate::api::Cursor], or returns null if there's nothing.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_try_1recv( pub extern "system" fn Java_mp_code_CursorController_try_1recv(
mut env: JNIEnv, mut env: JNIEnv,
@ -14,6 +15,7 @@ pub extern "system" fn Java_mp_code_CursorController_try_1recv(
jni_recv(&mut env, cursor) jni_recv(&mut env, cursor)
} }
/// Blocks until it receives a [crate::api::Cursor].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_recv( pub extern "system" fn Java_mp_code_CursorController_recv(
mut env: JNIEnv, mut env: JNIEnv,
@ -25,6 +27,7 @@ pub extern "system" fn Java_mp_code_CursorController_recv(
jni_recv(&mut env, cursor) jni_recv(&mut env, cursor)
} }
/// Utility method to convert a [crate::api::Cursor] to its Java equivalent.
fn jni_recv(env: &mut JNIEnv, cursor: Option<crate::api::Cursor>) -> jobject { fn jni_recv(env: &mut JNIEnv, cursor: Option<crate::api::Cursor>) -> jobject {
match cursor { match cursor {
None => JObject::null().as_raw(), None => JObject::null().as_raw(),
@ -46,6 +49,7 @@ fn jni_recv(env: &mut JNIEnv, cursor: Option<crate::api::Cursor>) -> jobject {
} }
} }
/// Receives from Java, converts and sends a [crate::api::Cursor].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_send<'local>( pub extern "system" fn Java_mp_code_CursorController_send<'local>(
mut env: JNIEnv, mut env: JNIEnv,
@ -86,6 +90,7 @@ pub extern "system" fn Java_mp_code_CursorController_send<'local>(
}).jexcept(&mut env); }).jexcept(&mut env);
} }
/// Called by the Java GC to drop a [crate::cursor::Controller].
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_free( pub extern "system" fn Java_mp_code_CursorController_free(
_env: JNIEnv, _env: JNIEnv,

View file

@ -1,14 +1,14 @@
pub mod client; pub mod client;
pub mod workspace; pub mod workspace;
pub mod util; pub mod util;
pub mod cursor;
pub mod cursor_controller; pub mod buffer;
pub mod buffer_controller;
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub(crate) static ref RT: tokio::runtime::Runtime = tokio::runtime::Runtime::new().expect("could not create tokio runtime"); pub(crate) static ref RT: tokio::runtime::Runtime = tokio::runtime::Runtime::new().expect("could not create tokio runtime");
} }
/// Sets up logging. Useful for debugging.
pub(crate) fn setup_logger(debug: bool, path: Option<String>) { pub(crate) fn setup_logger(debug: bool, path: Option<String>) {
let format = tracing_subscriber::fmt::format() let format = tracing_subscriber::fmt::format()
.with_level(true) .with_level(true)

View file

@ -3,12 +3,6 @@ use crate::Workspace;
use super::{util::JExceptable, RT}; 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) {
let _ = unsafe { Box::from_raw(input as *mut Workspace) };
}
/// Gets the workspace id. /// Gets the workspace id.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_get_1workspace_1id<'local>( pub extern "system" fn Java_mp_code_Workspace_get_1workspace_1id<'local>(
@ -202,3 +196,9 @@ pub extern "system" fn Java_mp_code_Workspace_select_1buffer(
JObject::null().as_raw() JObject::null().as_raw()
} }
} }
/// 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) {
let _ = unsafe { Box::from_raw(input as *mut Workspace) };
}