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
# java
java/*.iml
java/.idea/
java/*.h
java/**/*.class
java/build/
java/.classpath
java/.gradle/
java/.project
java/.settings/
java/bin/
dist/java/*.iml
dist/java/.idea/
dist/java/*.h
dist/java/**/*.class
dist/java/build/
dist/java/.classpath
dist/java/.gradle/
dist/java/.project
dist/java/.settings/
dist/java/bin/
# intellij insists on creating the wrapper every time even if it's not strictly necessary
java/gradle/
java/gradlew
java/gradlew.bat
dist/java/gradle/
dist/java/gradlew
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 }
[features]
default = ["js"]
default = []
lua = ["mlua", "derive_more", "lazy_static", "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"]
python = ["pyo3", "pyo3-asyncio", "tracing-subscriber", "pyo3-build-config"]

View file

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

View file

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

View file

@ -3,12 +3,6 @@ use crate::{client::Client, Workspace};
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.
#[no_mangle]
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())
);
}
/// 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;
/// Tries to fetch a [crate::api::Cursor], or returns null if there's nothing.
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_try_1recv(
mut env: JNIEnv,
@ -14,6 +15,7 @@ pub extern "system" fn Java_mp_code_CursorController_try_1recv(
jni_recv(&mut env, cursor)
}
/// Blocks until it receives a [crate::api::Cursor].
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_recv(
mut env: JNIEnv,
@ -25,6 +27,7 @@ pub extern "system" fn Java_mp_code_CursorController_recv(
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 {
match cursor {
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]
pub extern "system" fn Java_mp_code_CursorController_send<'local>(
mut env: JNIEnv,
@ -86,6 +90,7 @@ pub extern "system" fn Java_mp_code_CursorController_send<'local>(
}).jexcept(&mut env);
}
/// Called by the Java GC to drop a [crate::cursor::Controller].
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_free(
_env: JNIEnv,

View file

@ -1,14 +1,14 @@
pub mod client;
pub mod workspace;
pub mod util;
pub mod cursor_controller;
pub mod buffer_controller;
pub mod cursor;
pub mod buffer;
lazy_static::lazy_static! {
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>) {
let format = tracing_subscriber::fmt::format()
.with_level(true)

View file

@ -3,12 +3,6 @@ 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) {
let _ = unsafe { Box::from_raw(input as *mut Workspace) };
}
/// Gets the workspace id.
#[no_mangle]
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()
}
}
/// 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) };
}