Skip to content

Commit

Permalink
fix: remove unnecessary clone() for Value::TryInto and Reference::Try…
Browse files Browse the repository at this point in the history
…Into
  • Loading branch information
brianheineman committed Jan 20, 2025
1 parent 5bdfce6 commit 1cdbc81
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 49 deletions.
60 changes: 42 additions & 18 deletions ristretto_classloader/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Reference {
pub fn to_object(&self) -> Result<Object> {
match self {
Reference::Object(object) => Ok(object.clone()),
_ => Err(InvalidValueType("Expected array".to_string())),
_ => Err(InvalidValueType("Expected object".to_string())),
}
}

Expand Down Expand Up @@ -491,135 +491,159 @@ impl TryInto<bool> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<bool> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<char> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<char> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<i8> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<i8> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<u8> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<u8> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<i16> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<i16> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<u16> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<u16> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<i32> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<i32> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<u32> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<u32> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<i64> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<i64> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<u64> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<u64> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<isize> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<isize> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<usize> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<usize> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<f32> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<f32> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<f64> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<f64> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<Object> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<Object> {
self.to_object()
match self {
Reference::Object(object) => Ok(object),
_ => Err(InvalidValueType("Expected object".to_string())),
}
}
}

impl TryInto<String> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<String> {
self.to_object()?.try_into()
let object: Object = self.try_into()?;
object.try_into()
}
}

impl TryInto<Arc<Class>> for Reference {
type Error = crate::Error;

fn try_into(self) -> Result<Arc<Class>> {
self.class()
match self {
Reference::Object(object) => {
let class: Arc<Class> = object.try_into()?;
Ok(class)
}
_ => self.class(),
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions ristretto_classloader/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,10 @@ impl TryInto<Reference> for Value {
type Error = crate::Error;

fn try_into(self) -> Result<Reference> {
self.to_reference()?
.ok_or(InvalidValueType("Expected a reference value".to_string()))
match self {
Value::Object(Some(reference)) => Ok(reference),
_ => Err(InvalidValueType("Expected a reference value".to_string())),
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ mod tests {
let _ = get_unused_input(thread, Arguments::default()).await;
}

#[tokio::test]
async fn test_init_ids() -> Result<()> {
let (_vm, thread) = crate::test::thread().await.expect("thread");
let result = init_ids(thread, Arguments::default()).await?;
assert_eq!(result, None);
Ok(())
}

#[tokio::test]
#[should_panic(
expected = "not yet implemented: com.sun.java.util.jar.pack.NativeUnpack.setOption(Ljava/lang/String;Ljava/lang/String;)Z"
Expand Down
7 changes: 7 additions & 0 deletions ristretto_vm/src/native_methods/java/io/filedescriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,11 @@ mod tests {
let (_vm, thread) = crate::test::thread().await.expect("thread");
let _ = sync(thread, Arguments::default()).await;
}

#[tokio::test]
#[should_panic(expected = "not yet implemented: java.io.FileDescriptor.sync0()V")]
async fn test_sync_0() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
let _ = sync_0(thread, Arguments::default()).await;
}
}
7 changes: 7 additions & 0 deletions ristretto_vm/src/native_methods/java/io/randomaccessfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ async fn write_bytes_0(_thread: Arc<Thread>, _arguments: Arguments) -> Result<Op
mod tests {
use super::*;

#[tokio::test]
#[should_panic(expected = "not yet implemented: java.io.RandomAccessFile.close0()V")]
async fn test_close_0() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
let _ = close_0(thread, Arguments::default()).await;
}

#[tokio::test]
#[should_panic(expected = "not yet implemented: java.io.RandomAccessFile.length()J")]
async fn test_length() {
Expand Down
47 changes: 28 additions & 19 deletions ristretto_vm/src/native_methods/java/io/unixfilesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ async fn get_last_modified_time_0(
}

#[async_recursion(?Send)]
async fn get_length(_thread: Arc<Thread>, _arguments: Arguments) -> Result<Option<Value>> {
todo!("java.io.UnixFileSystem.getLength(Ljava/io/File;)J")
async fn get_length(thread: Arc<Thread>, arguments: Arguments) -> Result<Option<Value>> {
get_length_0(thread, arguments).await
}

#[async_recursion(?Send)]
Expand All @@ -271,8 +271,8 @@ async fn get_name_max_0(_thread: Arc<Thread>, _arguments: Arguments) -> Result<O
}

#[async_recursion(?Send)]
async fn get_space(_thread: Arc<Thread>, _arguments: Arguments) -> Result<Option<Value>> {
todo!("java.io.UnixFileSystem.getSpace(Ljava/io/File;I)J")
async fn get_space(thread: Arc<Thread>, arguments: Arguments) -> Result<Option<Value>> {
get_space_0(thread, arguments).await
}

#[async_recursion(?Send)]
Expand All @@ -286,8 +286,8 @@ async fn init_ids(_thread: Arc<Thread>, _arguments: Arguments) -> Result<Option<
}

#[async_recursion(?Send)]
async fn list(_thread: Arc<Thread>, _arguments: Arguments) -> Result<Option<Value>> {
todo!("java.io.UnixFileSystem.list(Ljava/io/File;)[Ljava/lang/String;")
async fn list(thread: Arc<Thread>, arguments: Arguments) -> Result<Option<Value>> {
list_0(thread, arguments).await
}

#[async_recursion(?Send)]
Expand All @@ -302,10 +302,10 @@ async fn rename_0(_thread: Arc<Thread>, _arguments: Arguments) -> Result<Option<

#[async_recursion(?Send)]
async fn set_last_modified_time(
_thread: Arc<Thread>,
_arguments: Arguments,
thread: Arc<Thread>,
arguments: Arguments,
) -> Result<Option<Value>> {
todo!("java.io.UnixFileSystem.setLastModifiedTime(Ljava/io/File;J)Z")
set_last_modified_time_0(thread, arguments).await
}

#[async_recursion(?Send)]
Expand All @@ -317,8 +317,8 @@ async fn set_last_modified_time_0(
}

#[async_recursion(?Send)]
async fn set_permission(_thread: Arc<Thread>, _arguments: Arguments) -> Result<Option<Value>> {
todo!("java.io.UnixFileSystem.setPermission(Ljava/io/File;IZZ)Z")
async fn set_permission(thread: Arc<Thread>, arguments: Arguments) -> Result<Option<Value>> {
set_permission_0(thread, arguments).await
}

#[async_recursion(?Send)]
Expand All @@ -327,8 +327,8 @@ async fn set_permission_0(_thread: Arc<Thread>, _arguments: Arguments) -> Result
}

#[async_recursion(?Send)]
async fn set_read_only(_thread: Arc<Thread>, _arguments: Arguments) -> Result<Option<Value>> {
todo!("java.io.UnixFileSystem.setReadOnly(Ljava/io/File;)Z")
async fn set_read_only(thread: Arc<Thread>, arguments: Arguments) -> Result<Option<Value>> {
set_read_only_0(thread, arguments).await
}

#[async_recursion(?Send)]
Expand Down Expand Up @@ -432,7 +432,7 @@ mod tests {

#[tokio::test]
#[should_panic(
expected = "not yet implemented: java.io.UnixFileSystem.getLength(Ljava/io/File;)J"
expected = "not yet implemented: java.io.UnixFileSystem.getLength0(Ljava/io/File;)J"
)]
async fn test_get_length() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
Expand All @@ -459,7 +459,7 @@ mod tests {

#[tokio::test]
#[should_panic(
expected = "not yet implemented: java.io.UnixFileSystem.getSpace(Ljava/io/File;I)J"
expected = "not yet implemented: java.io.UnixFileSystem.getSpace0(Ljava/io/File;I)J"
)]
async fn test_get_space() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
Expand All @@ -485,7 +485,7 @@ mod tests {

#[tokio::test]
#[should_panic(
expected = "not yet implemented: java.io.UnixFileSystem.list(Ljava/io/File;)[Ljava/lang/String;"
expected = "not yet implemented: java.io.UnixFileSystem.list0(Ljava/io/File;)[Ljava/lang/String;"
)]
async fn test_list() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
Expand All @@ -512,7 +512,7 @@ mod tests {

#[tokio::test]
#[should_panic(
expected = "not yet implemented: java.io.UnixFileSystem.setLastModifiedTime(Ljava/io/File;J)Z"
expected = "not yet implemented: java.io.UnixFileSystem.setLastModifiedTime0(Ljava/io/File;J)Z"
)]
async fn test_set_last_modified_time() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
Expand All @@ -530,7 +530,7 @@ mod tests {

#[tokio::test]
#[should_panic(
expected = "not yet implemented: java.io.UnixFileSystem.setPermission(Ljava/io/File;IZZ)Z"
expected = "not yet implemented: java.io.UnixFileSystem.setPermission0(Ljava/io/File;IZZ)Z"
)]
async fn test_set_permission() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
Expand All @@ -548,10 +548,19 @@ mod tests {

#[tokio::test]
#[should_panic(
expected = "not yet implemented: java.io.UnixFileSystem.setReadOnly(Ljava/io/File;)Z"
expected = "not yet implemented: java.io.UnixFileSystem.setReadOnly0(Ljava/io/File;)Z"
)]
async fn test_set_read_only() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
let _ = set_read_only(thread, Arguments::default()).await;
}

#[tokio::test]
#[should_panic(
expected = "not yet implemented: java.io.UnixFileSystem.setReadOnly0(Ljava/io/File;)Z"
)]
async fn test_set_read_only_0() {
let (_vm, thread) = crate::test::thread().await.expect("thread");
let _ = set_read_only_0(thread, Arguments::default()).await;
}
}
Loading

0 comments on commit 1cdbc81

Please sign in to comment.