2024-08-06 23:30:00 +02:00
|
|
|
pub mod client;
|
|
|
|
pub mod workspace;
|
2024-08-08 00:29:54 +02:00
|
|
|
pub mod cursor;
|
|
|
|
pub mod buffer;
|
2024-09-16 00:20:03 +02:00
|
|
|
pub mod ext;
|
|
|
|
|
|
|
|
/// Gets or creates the relevant [tokio::runtime::Runtime].
|
|
|
|
fn tokio() -> &'static tokio::runtime::Runtime {
|
|
|
|
use std::sync::OnceLock;
|
|
|
|
static RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
|
|
|
|
RT.get_or_init(||
|
|
|
|
tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.expect("could not create tokio runtime")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A static reference to [jni::JavaVM] that is set on JNI load.
|
|
|
|
static mut JVM: Option<std::sync::Arc<jni::JavaVM>> = None;
|
|
|
|
|
|
|
|
/// Safe accessor for the [jni::JavaVM] static.
|
|
|
|
pub(crate) fn jvm() -> std::sync::Arc<jni::JavaVM> {
|
|
|
|
unsafe { JVM.clone() }.unwrap()
|
|
|
|
}
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-09-16 00:20:03 +02:00
|
|
|
/// Called upon initialisation of the JVM.
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn JNI_OnLoad(
|
|
|
|
vm: jni::JavaVM,
|
|
|
|
_: *mut std::ffi::c_void
|
|
|
|
) -> jni::sys::jint {
|
|
|
|
unsafe { JVM = Some(std::sync::Arc::new(vm)) };
|
|
|
|
jni::sys::JNI_VERSION_1_1
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Set up logging. Useful for debugging.
|
2024-08-06 23:30:00 +02:00
|
|
|
pub(crate) fn setup_logger(debug: bool, path: Option<String>) {
|
|
|
|
let format = tracing_subscriber::fmt::format()
|
|
|
|
.with_level(true)
|
|
|
|
.with_target(true)
|
|
|
|
.with_thread_ids(false)
|
|
|
|
.with_thread_names(false)
|
|
|
|
.with_ansi(false)
|
|
|
|
.with_file(false)
|
|
|
|
.with_line_number(false)
|
|
|
|
.with_source_location(false)
|
|
|
|
.compact();
|
|
|
|
|
|
|
|
let level = if debug { tracing::Level::DEBUG } else {tracing::Level::INFO };
|
|
|
|
|
|
|
|
let builder = tracing_subscriber::fmt()
|
|
|
|
.event_format(format)
|
|
|
|
.with_max_level(level);
|
|
|
|
|
|
|
|
if let Some(path) = path {
|
|
|
|
let logfile = std::fs::File::create(path).expect("failed creating logfile");
|
|
|
|
builder.with_writer(std::sync::Mutex::new(logfile)).init();
|
|
|
|
} else {
|
|
|
|
builder.with_writer(std::sync::Mutex::new(std::io::stdout())).init();
|
2024-03-09 23:27:08 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-10 02:45:20 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
/// Utility macro that attempts to handle an error in a [Result].
|
|
|
|
/// MUST be called within a $result.is_err() block or similar. Failure to do so is UB.
|
|
|
|
/// Will return early with a provided return value, or panic if it fails to throw a Java exception.
|
|
|
|
macro_rules! handle_error {
|
|
|
|
($env: expr, $result: ident, $return: expr) => {
|
|
|
|
{
|
|
|
|
let err = unsafe { $result.unwrap_err_unchecked() };
|
|
|
|
tracing::info!("Attempting to throw error {err:#?} as a Java exception...");
|
|
|
|
if let Err(e) = err.jobjectify($env).map(|t| t.into()).and_then(|t: jni::objects::JThrowable| $env.throw(&t)) {
|
|
|
|
panic!("Failed to throw exception: {e}");
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
};
|
2024-08-10 02:45:20 +02:00
|
|
|
}
|
2024-09-17 17:37:22 +02:00
|
|
|
pub(crate) use handle_error;
|
2024-08-10 02:45:20 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
/// Performs a null check on the given variable and throws a NullPointerException on the Java side
|
|
|
|
/// if it is null. Finally, it returns with the given default value.
|
|
|
|
macro_rules! null_check {
|
|
|
|
($env: ident, $var: ident, $return: expr) => {
|
|
|
|
if $var.is_null() {
|
|
|
|
let mut message = stringify!($var).to_string();
|
|
|
|
message.push_str(" cannot be null!");
|
|
|
|
$env.throw_new("java/lang/NullPointerException", message)
|
|
|
|
.expect("Failed to throw exception!");
|
|
|
|
return $return;
|
2024-09-05 02:45:33 +02:00
|
|
|
}
|
2024-09-17 17:37:22 +02:00
|
|
|
};
|
2024-09-05 02:45:33 +02:00
|
|
|
}
|
2024-09-17 17:37:22 +02:00
|
|
|
pub(crate) use null_check;
|
2024-09-05 02:45:33 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
|
|
|
|
/// A trait meant for our local result type to make converting it to Java easier.
|
|
|
|
/// jni-rs technically has [jni::errors::ToException], but this approach keeps it stream-like.
|
|
|
|
pub(crate) trait JExceptable<'local, T: Default> {
|
|
|
|
/// Unwrap it and throws an appropriate Java exception if it's an error.
|
|
|
|
/// Theoretically it returns the type's default value, but the exception makes the value ignored.
|
|
|
|
fn jexcept(self, env: &mut jni::JNIEnv<'local>) -> T;
|
2024-09-05 02:45:33 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
impl<'local, T: Default, E: JObjectify<'local> + std::fmt::Debug> JExceptable<'local, T> for Result<T, E> {
|
|
|
|
fn jexcept(self, env: &mut jni::JNIEnv<'local>) -> T {
|
|
|
|
if let Ok(res) = self {
|
|
|
|
res
|
|
|
|
} else {
|
|
|
|
handle_error!(env, self, Default::default());
|
2024-08-10 02:45:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
/// Allows easy conversion for various types into Java objects.
|
|
|
|
/// This is similar to [TryInto], but for Java types.
|
|
|
|
pub(crate) trait JObjectify<'local> {
|
|
|
|
/// Attempt to convert the given object to a [jni::objects::JObject].
|
|
|
|
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error>;
|
2024-08-10 02:45:20 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
macro_rules! jobjectify_error {
|
|
|
|
($self: ident, $type: ty, $jclass: expr) => {
|
|
|
|
impl<'local> JObjectify<'local> for $type {
|
|
|
|
fn jobjectify($self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
|
|
|
|
let class = env.find_class($jclass)?;
|
|
|
|
let msg = env.new_string(format!("{:#?}", $self))?;
|
2024-09-18 01:54:20 +02:00
|
|
|
env.new_object(class, "(Ljava/lang/String;)V", &[jni::objects::JValueGen::Object(&msg)])
|
2024-09-16 02:59:43 +02:00
|
|
|
}
|
2024-08-10 02:45:20 +02:00
|
|
|
}
|
2024-09-17 17:37:22 +02:00
|
|
|
};
|
2024-08-10 02:45:20 +02:00
|
|
|
}
|
2024-08-19 11:36:51 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
jobjectify_error!(self, crate::errors::RemoteError, "mp/code/exceptions/ConnectionRemoteException");
|
|
|
|
jobjectify_error!(self, jni::errors::Error, match self {
|
|
|
|
jni::errors::Error::NullPtr(_) => "java/lang/NullPointerException",
|
|
|
|
_ => "mp/code/exceptions/JNIException"
|
|
|
|
});
|
|
|
|
jobjectify_error!(self, uuid::Error, "java/lang/IllegalArgumentException");
|
|
|
|
jobjectify_error!(self, crate::errors::ConnectionError, match self {
|
|
|
|
crate::errors::ConnectionError::Transport(_) => "mp/code/exceptions/ConnectionTransportException",
|
|
|
|
crate::errors::ConnectionError::Remote(_) => "mp/code/exceptions/ConnectionRemoteException"
|
|
|
|
});
|
|
|
|
jobjectify_error!(self, crate::errors::ControllerError, match self {
|
|
|
|
crate::errors::ControllerError::Stopped => "mp/code/exceptions/ControllerStoppedException",
|
|
|
|
crate::errors::ControllerError::Unfulfilled => "mp/code/exceptions/ControllerUnfulfilledException",
|
|
|
|
});
|
2024-08-19 11:36:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
impl<'local> JObjectify<'local> for uuid::Uuid {
|
2024-09-17 17:37:22 +02:00
|
|
|
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
|
2024-09-15 02:00:04 +02:00
|
|
|
let class = env.find_class("java/util/UUID")?;
|
|
|
|
let (msb, lsb) = self.as_u64_pair();
|
|
|
|
let msb = i64::from_ne_bytes(msb.to_ne_bytes());
|
|
|
|
let lsb = i64::from_ne_bytes(lsb.to_ne_bytes());
|
|
|
|
env.new_object(&class, "(JJ)V", &[jni::objects::JValueGen::Long(msb), jni::objects::JValueGen::Long(lsb)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
/// Generates a [JObjectify] implementation for a class that is just a holder for a pointer.
|
|
|
|
macro_rules! jobjectify_ptr_class {
|
|
|
|
($type: ty, $jclass: literal) => {
|
|
|
|
impl<'local> JObjectify<'local> for $type {
|
|
|
|
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
|
|
|
|
let class = env.find_class($jclass)?;
|
|
|
|
env.new_object(
|
|
|
|
class,
|
|
|
|
"(J)V",
|
|
|
|
&[jni::objects::JValueGen::Long(Box::into_raw(Box::new(self)) as jni::sys::jlong)]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
jobjectify_ptr_class!(crate::Client, "mp/code/Client");
|
|
|
|
jobjectify_ptr_class!(crate::Workspace, "mp/code/Workspace");
|
|
|
|
jobjectify_ptr_class!(crate::cursor::Controller, "mp/code/CursorController");
|
|
|
|
jobjectify_ptr_class!(crate::buffer::Controller, "mp/code/BufferController");
|
2024-09-15 02:00:04 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
impl<'local> JObjectify<'local> for crate::api::User {
|
|
|
|
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
|
2024-09-15 02:00:04 +02:00
|
|
|
let id_field = self.id.jobjectify(env)?;
|
|
|
|
let name_field = env.new_string(self.name)?;
|
|
|
|
let class = env.find_class("mp/code/data/User")?;
|
|
|
|
env.new_object(
|
|
|
|
&class,
|
|
|
|
"(Ljava/util/UUID;Ljava/lang/String;)V",
|
|
|
|
&[
|
|
|
|
jni::objects::JValueGen::Object(&id_field),
|
|
|
|
jni::objects::JValueGen::Object(&name_field)
|
|
|
|
]
|
|
|
|
)
|
2024-08-19 11:36:51 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-15 01:56:51 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
impl<'local> JObjectify<'local> for crate::api::Event {
|
|
|
|
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
|
|
|
|
let (ordinal, arg) = match self {
|
|
|
|
crate::api::Event::UserJoin(arg) => (0, env.new_string(arg)?),
|
|
|
|
crate::api::Event::UserLeave(arg) => (1, env.new_string(arg)?),
|
|
|
|
crate::api::Event::FileTreeUpdated(arg) => (2, env.new_string(arg)?),
|
|
|
|
};
|
2024-09-15 01:56:51 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
let type_class = env.find_class("mp/code/Workspace$Event$Type")?;
|
|
|
|
let variants: jni::objects::JObjectArray = env.call_method(
|
|
|
|
type_class,
|
|
|
|
"getEnumConstants",
|
|
|
|
"()[Ljava/lang/Object;",
|
|
|
|
&[]
|
|
|
|
)?.l()?.into();
|
|
|
|
let event_type = env.get_object_array_element(variants, ordinal)?;
|
|
|
|
|
|
|
|
let event_class = env.find_class("mp/code/Workspace$Event")?;
|
2024-09-15 01:56:51 +02:00
|
|
|
env.new_object(
|
2024-09-17 17:37:22 +02:00
|
|
|
event_class,
|
|
|
|
"(Lmp/code/Workspace$Event$Type;Ljava/lang/String;)V",
|
2024-09-15 01:56:51 +02:00
|
|
|
&[
|
2024-09-17 17:37:22 +02:00
|
|
|
jni::objects::JValueGen::Object(&event_type),
|
|
|
|
jni::objects::JValueGen::Object(&arg)
|
2024-09-15 01:56:51 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
impl<'local> JObjectify<'local> for crate::workspace::DetachResult {
|
|
|
|
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
|
|
|
|
let ordinal = match self {
|
|
|
|
crate::workspace::DetachResult::NotAttached => 0,
|
|
|
|
crate::workspace::DetachResult::Detaching => 1,
|
|
|
|
crate::workspace::DetachResult::AlreadyDetached => 2
|
|
|
|
};
|
2024-09-15 01:56:51 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
let class = env.find_class("mp/code/data/DetachResult")?;
|
|
|
|
let variants: jni::objects::JObjectArray = env.call_method(
|
2024-09-15 01:56:51 +02:00
|
|
|
class,
|
2024-09-17 17:37:22 +02:00
|
|
|
"getEnumConstants",
|
|
|
|
"()[Ljava/lang/Object;",
|
|
|
|
&[]
|
|
|
|
)?.l()?.into();
|
|
|
|
env.get_object_array_element(variants, ordinal)
|
2024-09-15 01:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-17 02:40:03 +02:00
|
|
|
|
|
|
|
impl<'local> JObjectify<'local> for crate::api::TextChange {
|
2024-09-17 17:37:22 +02:00
|
|
|
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
|
2024-09-17 02:40:03 +02:00
|
|
|
let content = env.new_string(self.content)?;
|
|
|
|
|
|
|
|
let hash = env.find_class("java/util/OptionalLong").and_then(|class| {
|
|
|
|
if let Some(h) = self.hash {
|
|
|
|
env.call_static_method(class, "of", "(J)Ljava/util/OptionalLong;", &[jni::objects::JValueGen::Long(h)])
|
|
|
|
} else {
|
|
|
|
env.call_static_method(class, "empty", "()Ljava/util/OptionalLong;", &[])
|
|
|
|
}
|
|
|
|
}).and_then(|o| o.l())?;
|
|
|
|
env.find_class("mp/code/data/TextChange").and_then(|class| {
|
|
|
|
env.new_object(
|
|
|
|
class,
|
|
|
|
"(JJLjava/lang/String;Ljava/util/OptionalLong;)V",
|
|
|
|
&[
|
2024-09-17 17:37:22 +02:00
|
|
|
jni::objects::JValueGen::Long(self.start.into()),
|
|
|
|
jni::objects::JValueGen::Long(self.end.into()),
|
2024-09-17 02:40:03 +02:00
|
|
|
jni::objects::JValueGen::Object(&content),
|
|
|
|
jni::objects::JValueGen::Object(&hash)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'local> JObjectify<'local> for crate::api::Cursor {
|
2024-09-17 17:37:22 +02:00
|
|
|
fn jobjectify(self, env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JObject<'local>, jni::errors::Error> {
|
2024-09-17 02:40:03 +02:00
|
|
|
env.find_class("mp/code/data/Cursor").and_then(|class| {
|
|
|
|
let buffer = env.new_string(&self.buffer)?;
|
|
|
|
let user = if let Some(user) = self.user {
|
|
|
|
env.new_string(user)?.into()
|
|
|
|
} else {
|
|
|
|
jni::objects::JObject::null()
|
|
|
|
};
|
|
|
|
|
|
|
|
env.new_object(
|
|
|
|
class,
|
|
|
|
"(IIIILjava/lang/String;Ljava/lang/String;)V",
|
|
|
|
&[
|
|
|
|
jni::objects::JValueGen::Int(self.start.0),
|
|
|
|
jni::objects::JValueGen::Int(self.start.1),
|
|
|
|
jni::objects::JValueGen::Int(self.end.0),
|
|
|
|
jni::objects::JValueGen::Int(self.end.1),
|
|
|
|
jni::objects::JValueGen::Object(&buffer),
|
|
|
|
jni::objects::JValueGen::Object(&user)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 17:37:22 +02:00
|
|
|
|
|
|
|
/// Allows easy conversion of Java types into their Rust counterparts.
|
|
|
|
pub(crate) trait Deobjectify<'local, T: Sized> {
|
|
|
|
/// Attempt to convert the given [jni::objects::JObject] into its Rust counterpart.
|
|
|
|
fn deobjectify(env: &mut jni::JNIEnv<'local>, jobject: jni::objects::JObject<'local>) -> Result<T, jni::errors::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'local> Deobjectify<'local, Self> for crate::api::Config {
|
|
|
|
fn deobjectify(env: &mut jni::JNIEnv<'local>, config: jni::objects::JObject<'local>) -> Result<Self, jni::errors::Error> {
|
|
|
|
let username = {
|
|
|
|
let jfield = env.get_field(&config, "username", "Ljava/lang/String;")?.l()?;
|
|
|
|
if jfield.is_null() {
|
|
|
|
return Err(jni::errors::Error::NullPtr("Username can never be null!"));
|
|
|
|
}
|
|
|
|
unsafe { env.get_string_unchecked(&jfield.into()) }?.into()
|
|
|
|
};
|
|
|
|
|
|
|
|
let password = {
|
|
|
|
let jfield = env.get_field(&config, "password", "Ljava/lang/String;")?.l()?;
|
|
|
|
if jfield.is_null() {
|
|
|
|
return Err(jni::errors::Error::NullPtr("Password can never be null!"));
|
|
|
|
}
|
|
|
|
unsafe { env.get_string_unchecked(&jfield.into()) }?.into()
|
|
|
|
};
|
|
|
|
|
|
|
|
let host = {
|
|
|
|
let jfield = env.get_field(&config, "host", "Ljava/util/Optional;")?.l()?;
|
|
|
|
if env.call_method(&jfield, "isPresent", "()Z", &[])?.z()? {
|
|
|
|
let field = env.call_method(&jfield, "get", "()Ljava/lang/Object;", &[])?.l()?;
|
|
|
|
Some(unsafe { env.get_string_unchecked(&field.into()) }?.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let port = {
|
|
|
|
let jfield = env.get_field(&config, "port", "Ljava/util/OptionalInt;")?.l()?;
|
|
|
|
if env.call_method(&jfield, "isPresent", "()Z", &[])?.z()? {
|
2024-09-18 01:54:20 +02:00
|
|
|
let ivalue = env.call_method(&jfield, "getAsInt", "()I", &[])?.i()?;
|
2024-09-17 17:37:22 +02:00
|
|
|
Some(ivalue.clamp(0, 65535) as u16)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let tls = {
|
|
|
|
let jfield = env.get_field(&config, "host", "Ljava/util/Optional;")?.l()?;
|
|
|
|
if env.call_method(&jfield, "isPresent", "()Z", &[])?.z()? {
|
|
|
|
let field = env.call_method(&jfield, "get", "()Ljava/lang/Object;", &[])?.l()?;
|
2024-09-18 01:54:20 +02:00
|
|
|
let bool_true = env.get_static_field("java/lang/Boolean", "TRUE", "Ljava/lang/Boolean;")?.l()?;
|
|
|
|
Some(env.call_method(
|
|
|
|
field,
|
|
|
|
"equals",
|
|
|
|
"(Ljava/lang/Object;)Z",
|
|
|
|
&[jni::objects::JValueGen::Object(&bool_true)]
|
|
|
|
)?.z()?) // what a joke
|
2024-09-17 17:37:22 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Self { username, password, host, port, tls })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'local> Deobjectify<'local, Self> for crate::api::Cursor {
|
|
|
|
fn deobjectify(env: &mut jni::JNIEnv<'local>, cursor: jni::objects::JObject<'local>) -> Result<Self, jni::errors::Error> {
|
|
|
|
let start_row = env.get_field(&cursor, "startRow", "I")?.i()?;
|
|
|
|
let start_col = env.get_field(&cursor, "startCol", "I")?.i()?;
|
|
|
|
let end_row = env.get_field(&cursor, "endRow", "I")?.i()?;
|
|
|
|
let end_col = env.get_field(&cursor, "endCol", "I")?.i()?;
|
|
|
|
|
|
|
|
let buffer = {
|
|
|
|
let jfield = env.get_field(&cursor, "buffer", "Ljava/lang/String;")?.l()?;
|
|
|
|
if jfield.is_null() {
|
|
|
|
return Err(jni::errors::Error::NullPtr("Buffer can never be null!"));
|
|
|
|
}
|
|
|
|
unsafe { env.get_string_unchecked(&jfield.into()) }?.into()
|
|
|
|
};
|
|
|
|
|
|
|
|
let user = {
|
|
|
|
let jfield = env.get_field(&cursor, "user", "Ljava/lang/String;")?.l()?;
|
|
|
|
if jfield.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(unsafe { env.get_string_unchecked(&jfield.into()) }?.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Self { start: (start_row, start_col), end: (end_row, end_col), buffer, user })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'local> Deobjectify<'local, Self> for crate::api::TextChange {
|
|
|
|
fn deobjectify(env: &mut jni::JNIEnv<'local>, change: jni::objects::JObject<'local>) -> Result<Self, jni::errors::Error> {
|
2024-09-18 01:54:20 +02:00
|
|
|
let start = env.get_field(&change, "start", "J")?.j()?.clamp(0, u32::MAX.into()) as u32;
|
|
|
|
let end = env.get_field(&change, "end", "J")?.j()?.clamp(0, u32::MAX.into()) as u32;
|
2024-09-17 17:37:22 +02:00
|
|
|
|
|
|
|
let content = {
|
|
|
|
let jfield = env.get_field(&change, "content", "Ljava/lang/String;")?.l()?;
|
|
|
|
if jfield.is_null() {
|
|
|
|
return Err(jni::errors::Error::NullPtr("Content can never be null!"));
|
|
|
|
}
|
|
|
|
unsafe { env.get_string_unchecked(&jfield.into()) }?.into()
|
|
|
|
};
|
|
|
|
|
|
|
|
let hash = {
|
|
|
|
let jfield = env.get_field(&change, "hash", "Ljava/util/OptionalLong;")?.l()?;
|
|
|
|
if env.call_method(&jfield, "isPresent", "()Z", &[])?.z()? {
|
|
|
|
Some(env.call_method(&jfield, "getAsLong", "()J", &[])?.j()?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(Self { start, end, content, hash })
|
|
|
|
}
|
|
|
|
}
|