Skip to content

Commit

Permalink
Merge pull request #153 from theseus-rs/correct-unicode-debug-support
Browse files Browse the repository at this point in the history
fix: correct Java 8 string handling to encode Rust strings as UTF-16
  • Loading branch information
brianheineman authored Nov 28, 2024
2 parents 289a4b8 + 0d580a9 commit 09385a5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
10 changes: 3 additions & 7 deletions ristretto_vm/src/java_object.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::Error::InternalError;
use crate::{Result, VM};
use ristretto_classfile::{mutf8, Version};
use ristretto_classloader::Error::ParseError;
use ristretto_classloader::{Class, Object, Reference, Value};
use ristretto_classloader::{Class, ConcurrentVec, Object, Reference, Value};
use std::sync::Arc;

const JAVA_8: Version = Version::Java8 { minor: 0 };
Expand Down Expand Up @@ -190,11 +189,8 @@ impl JavaObject for &str {
// In Java 9 and later, the value field is a byte array.
let java_class_file_version = vm.java_class_file_version();
let array = if java_class_file_version <= &JAVA_8 {
let bytes = mutf8::to_bytes(self)?;
let utf8_string =
String::from_utf8(bytes).map_err(|error| ParseError(error.to_string()))?;
let chars: Vec<char> = utf8_string.chars().collect();
Reference::from(chars)
let chars = self.encode_utf16().collect::<Vec<u16>>();
Reference::CharArray(ConcurrentVec::from(chars))
} else {
if java_class_file_version >= &JAVA_17 {
object.set_value("hashIsZero", Value::Int(0))?;
Expand Down
6 changes: 4 additions & 2 deletions ristretto_vm/src/local_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ impl Display for LocalVariables {
continue;
};
let value = local.to_string();
if value.len() > 100 {
locals.push(format!("{}...", &value[..97]));
let chars: Vec<char> = value.chars().collect();
if chars.len() > 100 {
let value = chars.iter().take(97).collect::<String>();
locals.push(format!("{value}..."));
} else {
locals.push(value);
}
Expand Down
6 changes: 4 additions & 2 deletions ristretto_vm/src/operand_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ impl Display for OperandStack {
let mut values = Vec::new();
for stack_entry in &stack {
let value = stack_entry.to_string();
if value.len() > 100 {
values.push(format!("{}...", &value[..97]));
let chars: Vec<char> = value.chars().collect();
if chars.len() > 100 {
let value = chars.iter().take(97).collect::<String>();
values.push(format!("{value}..."));
} else {
values.push(value);
}
Expand Down

0 comments on commit 09385a5

Please sign in to comment.