Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions java_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dyn-clone = { workspace = true }
hashbrown = { workspace = true }
libm = { version = "^0.2", default-features = false }
parking_lot = { workspace = true }
regex = { version = "1.13.1", default-features = false, features = ["unicode"] }
tracing = { workspace = true }

chrono = { version = "^0.4", default-features = false }
Expand Down
31 changes: 16 additions & 15 deletions java_runtime/src/classes/java/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod array_index_out_of_bounds_exception;
mod array_store_exception;
mod boolean;
mod byte;
mod char_sequence;
mod character;
mod class;
mod class_cast_exception;
Expand Down Expand Up @@ -69,21 +70,21 @@ mod virtual_machine_error;
pub use self::{
abstract_method_error::AbstractMethodError, arithmetic_exception::ArithmeticException,
array_index_out_of_bounds_exception::ArrayIndexOutOfBoundsException, array_store_exception::ArrayStoreException, boolean::Boolean, byte::Byte,
character::Character, class::Class, class_cast_exception::ClassCastException, class_circularity_error::ClassCircularityError,
class_format_error::ClassFormatError, class_loader::ClassLoader, class_not_found_exception::ClassNotFoundException,
clone_not_supported_exception::CloneNotSupportedException, cloneable::Cloneable, comparable::Comparable, double::Double, error::Error,
exception::Exception, exception_in_initializer_error::ExceptionInInitializerError, float::Float, illegal_access_error::IllegalAccessError,
illegal_access_exception::IllegalAccessException, illegal_argument_exception::IllegalArgumentException,
illegal_monitor_state_exception::IllegalMonitorStateException, illegal_state_exception::IllegalStateException,
illegal_thread_state_exception::IllegalThreadStateException, incompatible_class_change_error::IncompatibleClassChangeError,
index_out_of_bounds_exception::IndexOutOfBoundsException, instantiation_error::InstantiationError,
instantiation_exception::InstantiationException, integer::Integer, internal_error::InternalError, interrupted_exception::InterruptedException,
linkage_error::LinkageError, long::Long, math::Math, negative_array_size_exception::NegativeArraySizeException,
no_class_def_found_error::NoClassDefFoundError, no_such_field_error::NoSuchFieldError, no_such_field_exception::NoSuchFieldException,
no_such_method_error::NoSuchMethodError, no_such_method_exception::NoSuchMethodException, null_pointer_exception::NullPointerException,
number::Number, number_format_exception::NumberFormatException, object::Object, out_of_memory_error::OutOfMemoryError, runnable::Runnable,
runtime::Runtime, runtime_exception::RuntimeException, security_exception::SecurityException, short::Short,
stack_overflow_error::StackOverflowError, string::String, string_buffer::StringBuffer,
char_sequence::CharSequence, character::Character, class::Class, class_cast_exception::ClassCastException,
class_circularity_error::ClassCircularityError, class_format_error::ClassFormatError, class_loader::ClassLoader,
class_not_found_exception::ClassNotFoundException, clone_not_supported_exception::CloneNotSupportedException, cloneable::Cloneable,
comparable::Comparable, double::Double, error::Error, exception::Exception, exception_in_initializer_error::ExceptionInInitializerError,
float::Float, illegal_access_error::IllegalAccessError, illegal_access_exception::IllegalAccessException,
illegal_argument_exception::IllegalArgumentException, illegal_monitor_state_exception::IllegalMonitorStateException,
illegal_state_exception::IllegalStateException, illegal_thread_state_exception::IllegalThreadStateException,
incompatible_class_change_error::IncompatibleClassChangeError, index_out_of_bounds_exception::IndexOutOfBoundsException,
instantiation_error::InstantiationError, instantiation_exception::InstantiationException, integer::Integer, internal_error::InternalError,
interrupted_exception::InterruptedException, linkage_error::LinkageError, long::Long, math::Math,
negative_array_size_exception::NegativeArraySizeException, no_class_def_found_error::NoClassDefFoundError, no_such_field_error::NoSuchFieldError,
no_such_field_exception::NoSuchFieldException, no_such_method_error::NoSuchMethodError, no_such_method_exception::NoSuchMethodException,
null_pointer_exception::NullPointerException, number::Number, number_format_exception::NumberFormatException, object::Object,
out_of_memory_error::OutOfMemoryError, runnable::Runnable, runtime::Runtime, runtime_exception::RuntimeException,
security_exception::SecurityException, short::Short, stack_overflow_error::StackOverflowError, string::String, string_buffer::StringBuffer,
string_index_out_of_bounds_exception::StringIndexOutOfBoundsException, system::System, thread::Thread, thread_death::ThreadDeath,
throwable::Throwable, unknown_error::UnknownError, unsatisfied_link_error::UnsatisfiedLinkError,
unsupported_class_version_error::UnsupportedClassVersionError, unsupported_operation_exception::UnsupportedOperationException,
Expand Down
35 changes: 35 additions & 0 deletions java_runtime/src/classes/java/lang/char_sequence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use alloc::vec;

use java_class_proto::JavaMethodProto;
use java_constants::{ClassAccessFlags, MethodAccessFlags};

use crate::RuntimeClassProto;

// public interface java.lang.CharSequence
pub struct CharSequence;

impl CharSequence {
pub fn as_proto() -> RuntimeClassProto {
RuntimeClassProto {
name: "java/lang/CharSequence",
parent_class: None,
interfaces: vec![],
methods: vec![
JavaMethodProto::new_abstract("length", "()I", MethodAccessFlags::PUBLIC | MethodAccessFlags::ABSTRACT),
JavaMethodProto::new_abstract("charAt", "(I)C", MethodAccessFlags::PUBLIC | MethodAccessFlags::ABSTRACT),
JavaMethodProto::new_abstract(
"subSequence",
"(II)Ljava/lang/CharSequence;",
MethodAccessFlags::PUBLIC | MethodAccessFlags::ABSTRACT,
),
JavaMethodProto::new_abstract(
"toString",
"()Ljava/lang/String;",
MethodAccessFlags::PUBLIC | MethodAccessFlags::ABSTRACT,
),
],
fields: vec![],
access_flags: ClassAccessFlags::PUBLIC | ClassAccessFlags::INTERFACE | ClassAccessFlags::ABSTRACT,
}
}
}
154 changes: 151 additions & 3 deletions java_runtime/src/classes/java/lang/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ use jvm::{Array, ClassInstanceRef, JavaChar, Jvm, Result, runtime::JavaLangStrin

use crate::{
RuntimeClassProto, RuntimeContext,
classes::java::lang::{Object, System},
classes::java::{
lang::{Object, System},
util::regex::{Matcher, Pattern},
},
};

use super::StringBuffer;
use super::{CharSequence, StringBuffer};

// class java.lang.String
pub struct String;
Expand All @@ -26,7 +29,7 @@ impl String {
RuntimeClassProto {
name: "java/lang/String",
parent_class: Some("java/lang/Object"),
interfaces: vec!["java/io/Serializable", "java/lang/Comparable"],
interfaces: vec!["java/io/Serializable", "java/lang/Comparable", "java/lang/CharSequence"],
methods: vec![
JavaMethodProto::new("<init>", "()V", Self::init_empty, Default::default()),
JavaMethodProto::new("<init>", "([B)V", Self::init_with_byte_array, Default::default()),
Expand Down Expand Up @@ -83,7 +86,33 @@ impl String {
JavaMethodProto::new("concat", "(Ljava/lang/String;)Ljava/lang/String;", Self::concat, Default::default()),
JavaMethodProto::new("substring", "(I)Ljava/lang/String;", Self::substring, Default::default()),
JavaMethodProto::new("substring", "(II)Ljava/lang/String;", Self::substring_with_end, Default::default()),
JavaMethodProto::new(
"subSequence",
"(II)Ljava/lang/CharSequence;",
Self::sub_sequence,
MethodAccessFlags::PUBLIC,
),
JavaMethodProto::new("replace", "(CC)Ljava/lang/String;", Self::replace, Default::default()),
JavaMethodProto::new("matches", "(Ljava/lang/String;)Z", Self::matches, MethodAccessFlags::PUBLIC),
JavaMethodProto::new(
"replaceFirst",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
Self::replace_first,
MethodAccessFlags::PUBLIC,
),
JavaMethodProto::new(
"replaceAll",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
Self::replace_all,
MethodAccessFlags::PUBLIC,
),
JavaMethodProto::new("split", "(Ljava/lang/String;)[Ljava/lang/String;", Self::split, MethodAccessFlags::PUBLIC),
JavaMethodProto::new(
"split",
"(Ljava/lang/String;I)[Ljava/lang/String;",
Self::split_with_limit,
MethodAccessFlags::PUBLIC,
),
JavaMethodProto::new(
"regionMatches",
"(ILjava/lang/String;II)Z",
Expand Down Expand Up @@ -600,6 +629,19 @@ impl String {
Ok(new_string.into())
}

async fn sub_sequence(
jvm: &Jvm,
_: &mut RuntimeContext,
this: ClassInstanceRef<Self>,
begin_index: i32,
end_index: i32,
) -> Result<ClassInstanceRef<CharSequence>> {
tracing::debug!("java.lang.String::subSequence({this:?}, {begin_index}, {end_index})");

jvm.invoke_virtual(&this, "substring", "(II)Ljava/lang/String;", (begin_index, end_index))
.await
}

async fn value_of_char(jvm: &Jvm, _: &mut RuntimeContext, value: JavaChar) -> Result<ClassInstanceRef<Self>> {
tracing::debug!("java.lang.String::valueOf({value})");

Expand Down Expand Up @@ -926,6 +968,112 @@ impl String {
Ok(new_string.into())
}

async fn matches(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, regex: ClassInstanceRef<Self>) -> Result<bool> {
tracing::debug!("java.lang.String::matches({this:?}, {regex:?})");

let input: ClassInstanceRef<CharSequence> = ClassInstanceRef::new(this.instance);
jvm.invoke_static(
"java/util/regex/Pattern",
"matches",
"(Ljava/lang/String;Ljava/lang/CharSequence;)Z",
(regex, input),
)
.await
}

async fn replace_first(
jvm: &Jvm,
_: &mut RuntimeContext,
this: ClassInstanceRef<Self>,
regex: ClassInstanceRef<Self>,
replacement: ClassInstanceRef<Self>,
) -> Result<ClassInstanceRef<Self>> {
tracing::debug!("java.lang.String::replaceFirst({this:?}, {regex:?}, {replacement:?})");

let pattern: ClassInstanceRef<Pattern> = jvm
.invoke_static(
"java/util/regex/Pattern",
"compile",
"(Ljava/lang/String;)Ljava/util/regex/Pattern;",
(regex,),
)
.await?;
let input: ClassInstanceRef<CharSequence> = ClassInstanceRef::new(this.instance);
let matcher: ClassInstanceRef<Matcher> = jvm
.invoke_virtual(&pattern, "matcher", "(Ljava/lang/CharSequence;)Ljava/util/regex/Matcher;", (input,))
.await?;
jvm.invoke_virtual(&matcher, "replaceFirst", "(Ljava/lang/String;)Ljava/lang/String;", (replacement,))
.await
}

async fn replace_all(
jvm: &Jvm,
_: &mut RuntimeContext,
this: ClassInstanceRef<Self>,
regex: ClassInstanceRef<Self>,
replacement: ClassInstanceRef<Self>,
) -> Result<ClassInstanceRef<Self>> {
tracing::debug!("java.lang.String::replaceAll({this:?}, {regex:?}, {replacement:?})");

let pattern: ClassInstanceRef<Pattern> = jvm
.invoke_static(
"java/util/regex/Pattern",
"compile",
"(Ljava/lang/String;)Ljava/util/regex/Pattern;",
(regex,),
)
.await?;
let input: ClassInstanceRef<CharSequence> = ClassInstanceRef::new(this.instance);
let matcher: ClassInstanceRef<Matcher> = jvm
.invoke_virtual(&pattern, "matcher", "(Ljava/lang/CharSequence;)Ljava/util/regex/Matcher;", (input,))
.await?;
jvm.invoke_virtual(&matcher, "replaceAll", "(Ljava/lang/String;)Ljava/lang/String;", (replacement,))
.await
}

async fn split(
jvm: &Jvm,
_: &mut RuntimeContext,
this: ClassInstanceRef<Self>,
regex: ClassInstanceRef<Self>,
) -> Result<ClassInstanceRef<Array<Self>>> {
tracing::debug!("java.lang.String::split({this:?}, {regex:?})");

let pattern: ClassInstanceRef<Pattern> = jvm
.invoke_static(
"java/util/regex/Pattern",
"compile",
"(Ljava/lang/String;)Ljava/util/regex/Pattern;",
(regex,),
)
.await?;
let input: ClassInstanceRef<CharSequence> = ClassInstanceRef::new(this.instance);
jvm.invoke_virtual(&pattern, "split", "(Ljava/lang/CharSequence;)[Ljava/lang/String;", (input,))
.await
}

async fn split_with_limit(
jvm: &Jvm,
_: &mut RuntimeContext,
this: ClassInstanceRef<Self>,
regex: ClassInstanceRef<Self>,
limit: i32,
) -> Result<ClassInstanceRef<Array<Self>>> {
tracing::debug!("java.lang.String::split({this:?}, {regex:?}, {limit})");

let pattern: ClassInstanceRef<Pattern> = jvm
.invoke_static(
"java/util/regex/Pattern",
"compile",
"(Ljava/lang/String;)Ljava/util/regex/Pattern;",
(regex,),
)
.await?;
let input: ClassInstanceRef<CharSequence> = ClassInstanceRef::new(this.instance);
jvm.invoke_virtual(&pattern, "split", "(Ljava/lang/CharSequence;I)[Ljava/lang/String;", (input, limit))
.await
}

#[allow(clippy::too_many_arguments)]
async fn region_matches(
jvm: &Jvm,
Expand Down
22 changes: 20 additions & 2 deletions java_runtime/src/classes/java/lang/string_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use jvm::{Array, ClassInstanceRef, JavaChar, Jvm, Result, runtime::JavaLangStrin

use crate::{
RuntimeClassProto, RuntimeContext,
classes::java::lang::{Object, String},
classes::java::lang::{CharSequence, Object, String},
};

// public final class java.lang.StringBuffer
Expand All @@ -17,7 +17,7 @@ impl StringBuffer {
RuntimeClassProto {
name: "java/lang/StringBuffer",
parent_class: Some("java/lang/Object"),
interfaces: vec![],
interfaces: vec!["java/lang/CharSequence"],
methods: vec![
JavaMethodProto::new("<init>", "()V", Self::init, MethodAccessFlags::PUBLIC),
JavaMethodProto::new("<init>", "(I)V", Self::init_with_capacity, MethodAccessFlags::PUBLIC),
Expand Down Expand Up @@ -166,6 +166,12 @@ impl StringBuffer {
Self::substring_range,
MethodAccessFlags::PUBLIC | MethodAccessFlags::SYNCHRONIZED,
),
JavaMethodProto::new(
"subSequence",
"(II)Ljava/lang/CharSequence;",
Self::sub_sequence,
MethodAccessFlags::PUBLIC | MethodAccessFlags::SYNCHRONIZED,
),
JavaMethodProto::new(
"reverse",
"()Ljava/lang/StringBuffer;",
Expand Down Expand Up @@ -648,6 +654,18 @@ impl StringBuffer {
Ok(jvm.new_class("java/lang/String", "([CII)V", (value, start, end - start)).await?.into())
}

async fn sub_sequence(
jvm: &Jvm,
_: &mut RuntimeContext,
this: ClassInstanceRef<Self>,
start: i32,
end: i32,
) -> Result<ClassInstanceRef<CharSequence>> {
tracing::debug!("java.lang.StringBuffer::subSequence({this:?}, {start}, {end})");

jvm.invoke_virtual(&this, "substring", "(II)Ljava/lang/String;", (start, end)).await
}

async fn reverse(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<Self>> {
tracing::debug!("java.lang.StringBuffer::reverse({this:?})");

Expand Down
1 change: 1 addition & 0 deletions java_runtime/src/classes/java/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod jar;
pub mod regex;
pub mod zip;

mod abstract_collection;
Expand Down
5 changes: 5 additions & 0 deletions java_runtime/src/classes/java/util/regex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod matcher;
mod pattern;
mod pattern_syntax_exception;

pub use self::{matcher::Matcher, pattern::Pattern, pattern_syntax_exception::PatternSyntaxException};
Loading