Skip to content

Commit cde4b04

Browse files
Merge pull request #432 from theseus-rs/refactor-java-object
refactor: update JavaObject.to_object(&VM) -> JavaObject.to_object(&Thread)
2 parents 11439d1 + 4b52370 commit cde4b04

34 files changed

+433
-476
lines changed

ristretto_vm/src/instruction/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ mod tests {
389389

390390
#[tokio::test]
391391
async fn test_arraylength_invalid_type() -> Result<()> {
392-
let (vm, _thread, _frame) = crate::test::frame().await?;
392+
let (_vm, thread) = crate::test::thread().await?;
393393
let stack = &mut OperandStack::with_max_size(1);
394-
let invalid_value = "foo".to_object(&vm).await?;
394+
let invalid_value = "foo".to_object(&thread).await?;
395395
stack.push(invalid_value)?;
396396
let result = arraylength(stack);
397397
assert!(matches!(

ristretto_vm/src/instruction/branch.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ pub(crate) fn ifnonnull(stack: &mut OperandStack, address: u16) -> Result<Execut
259259
#[cfg(test)]
260260
mod test {
261261
use super::*;
262-
use crate::VM;
263262
use crate::java_object::JavaObject;
264263
use ristretto_classloader::Reference;
265264

@@ -587,10 +586,10 @@ mod test {
587586

588587
#[tokio::test]
589588
async fn test_if_acmpeq_class_equal() -> Result<()> {
590-
let vm = VM::default().await?;
591-
let class = vm.class("java.lang.String").await?;
592-
let class1 = class.to_object(&vm).await?;
593-
let class2 = class.to_object(&vm).await?;
589+
let (_vm, thread) = crate::test::thread().await?;
590+
let class = thread.class("java.lang.String").await?;
591+
let class1 = class.to_object(&thread).await?;
592+
let class2 = class.to_object(&thread).await?;
594593

595594
let stack = &mut OperandStack::with_max_size(2);
596595
stack.push_object(Some(class1.try_into()?))?;
@@ -602,11 +601,11 @@ mod test {
602601

603602
#[tokio::test]
604603
async fn test_if_acmpeq_class_not_equal() -> Result<()> {
605-
let vm = VM::default().await?;
606-
let class1 = vm.class("java.lang.Object").await?;
607-
let class1 = class1.to_object(&vm).await?;
608-
let class2 = vm.class("java.lang.String").await?;
609-
let class2 = class2.to_object(&vm).await?;
604+
let (_vm, thread) = crate::test::thread().await?;
605+
let class1 = thread.class("java.lang.Object").await?;
606+
let class1 = class1.to_object(&thread).await?;
607+
let class2 = thread.class("java.lang.String").await?;
608+
let class2 = class2.to_object(&thread).await?;
610609

611610
let stack = &mut OperandStack::with_max_size(2);
612611
stack.push_object(Some(class1.try_into()?))?;
@@ -650,10 +649,10 @@ mod test {
650649

651650
#[tokio::test]
652651
async fn test_if_acmpne_class_equal() -> Result<()> {
653-
let vm = VM::default().await?;
654-
let class = vm.class("java.lang.String").await?;
655-
let class1 = class.to_object(&vm).await?;
656-
let class2 = class.to_object(&vm).await?;
652+
let (_vm, thread) = crate::test::thread().await?;
653+
let class = thread.class("java.lang.String").await?;
654+
let class1 = class.to_object(&thread).await?;
655+
let class2 = class.to_object(&thread).await?;
657656

658657
let stack = &mut OperandStack::with_max_size(2);
659658
stack.push_object(Some(class1.try_into()?))?;
@@ -665,11 +664,11 @@ mod test {
665664

666665
#[tokio::test]
667666
async fn test_if_acmpne_class_not_equal() -> Result<()> {
668-
let vm = VM::default().await?;
669-
let class1 = vm.class("java.lang.Object").await?;
670-
let class1 = class1.to_object(&vm).await?;
671-
let class2 = vm.class("java.lang.String").await?;
672-
let class2 = class2.to_object(&vm).await?;
667+
let (_vm, thread) = crate::test::thread().await?;
668+
let class1 = thread.class("java.lang.Object").await?;
669+
let class1 = class1.to_object(&thread).await?;
670+
let class2 = thread.class("java.lang.String").await?;
671+
let class2 = class2.to_object(&thread).await?;
673672

674673
let stack = &mut OperandStack::with_max_size(2);
675674
stack.push_object(Some(class1.try_into()?))?;

ristretto_vm/src/instruction/exception.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ pub(crate) async fn convert_error_to_throwable(vm: Arc<VM>, error: Error) -> Res
8888
#[cfg(test)]
8989
mod test {
9090
use super::*;
91-
use crate::VM;
9291
use crate::java_object::JavaObject;
9392

9493
#[tokio::test]
9594
async fn test_process_throwable() -> Result<()> {
96-
let vm = VM::default().await?;
97-
let value = "foo".to_object(&vm).await?;
98-
let result = vm
95+
let (_vm, thread) = crate::test::thread().await?;
96+
let value = "foo".to_object(&thread).await?;
97+
let result = thread
9998
.invoke(
10099
"java.lang.Integer",
101100
"parseInt(Ljava/lang/String;)I",

ristretto_vm/src/instruction/invokedynamic.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ use crate::JavaError::BootstrapMethodError;
154154
use crate::frame::{ExecutionResult, Frame};
155155
use crate::operand_stack::OperandStack;
156156
use crate::thread::Thread;
157-
use crate::{JavaObject, Result, VM};
157+
use crate::{JavaObject, Result};
158158
use ristretto_classfile::attributes::{Attribute, BootstrapMethod};
159159
use ristretto_classfile::{Constant, ConstantPool, FieldType, MethodAccessFlags, ReferenceKind};
160160
use ristretto_classloader::{Class, ConcurrentVec, Method, ObjectArray, Reference, Value};
@@ -291,18 +291,14 @@ async fn resolve_method_handles_lookup(thread: &Thread) -> Result<Value> {
291291
/// that represents that type in the JVM. For primitive types and arrays, this returns the
292292
/// corresponding class objects (like `java.lang.Integer.TYPE` for `int`). For reference types, it
293293
/// loads the class for the specified type.
294-
async fn get_field_type_class(
295-
vm: &VM,
296-
thread: &Thread,
297-
field_type: Option<FieldType>,
298-
) -> Result<Value> {
294+
async fn get_field_type_class(thread: &Thread, field_type: Option<FieldType>) -> Result<Value> {
299295
let class_name = if let Some(field_type) = field_type {
300296
field_type.class_name()
301297
} else {
302298
"void".to_string()
303299
};
304300
let class = thread.class(class_name).await?;
305-
class.to_object(vm).await
301+
class.to_object(thread).await
306302
}
307303

308304
/// **Step 3.2** Create MethodType from method descriptor:
@@ -315,9 +311,9 @@ async fn get_field_type_class(
315311
/// This function parses the provided method descriptor into its constituent parts (argument types
316312
/// and return type), creates `Class` objects for each type, and then invokes the appropriate
317313
/// `MethodType.methodType()` factory method to create a `MethodType` instance.
318-
async fn get_method_type(vm: &VM, thread: &Thread, method_descriptor: &str) -> Result<Value> {
314+
async fn get_method_type(thread: &Thread, method_descriptor: &str) -> Result<Value> {
319315
let (argument_types, return_type) = FieldType::parse_method_descriptor(method_descriptor)?;
320-
let return_class = get_field_type_class(vm, thread, return_type).await?;
316+
let return_class = get_field_type_class(thread, return_type).await?;
321317

322318
let method_type_class = thread.class("java.lang.invoke.MethodType").await?;
323319
if argument_types.is_empty() {
@@ -330,10 +326,10 @@ async fn get_method_type(vm: &VM, thread: &Thread, method_descriptor: &str) -> R
330326
.await;
331327
}
332328

333-
let first_argument = get_field_type_class(vm, thread, argument_types.first().cloned()).await?;
329+
let first_argument = get_field_type_class(thread, argument_types.first().cloned()).await?;
334330
let argument_classes = ConcurrentVec::from(Vec::with_capacity(argument_types.len() - 1));
335331
for argument_type in argument_types.iter().skip(1) {
336-
let argument_class = get_field_type_class(vm, thread, Some(argument_type.clone())).await?;
332+
let argument_class = get_field_type_class(thread, Some(argument_type.clone())).await?;
337333
let argument_reference: Reference = argument_class.try_into()?;
338334
argument_classes.push(Some(argument_reference))?;
339335
}
@@ -360,17 +356,17 @@ async fn get_method_type(vm: &VM, thread: &Thread, method_descriptor: &str) -> R
360356
///
361357
/// This function obtains a lookup object that has the access privileges of the caller's class. The
362358
/// lookup object is used for finding and binding methods during dynamic method invocation.
363-
async fn resolve_method_handle(vm: &VM, thread: &Thread, frame: &Frame) -> Result<Value> {
359+
async fn resolve_method_handle(thread: &Thread, frame: &Frame) -> Result<Value> {
364360
let lookup = resolve_method_handles_lookup(thread).await?;
365361

366362
// Get the caller MethodHandle
367363
let class = frame.class();
368-
let class_object = class.to_object(vm).await?;
364+
let class_object = class.to_object(thread).await?;
369365
let method = frame.method();
370366
let method_name = method.name();
371-
let method_name_object = method_name.to_object(vm).await?;
367+
let method_name_object = method_name.to_object(thread).await?;
372368
let method_descriptor = method.descriptor();
373-
let method_type = get_method_type(vm, thread, method_descriptor).await?;
369+
let method_type = get_method_type(thread, method_descriptor).await?;
374370
let mut arguments = vec![method_name_object, method_type];
375371

376372
let lookup_class = thread
@@ -421,7 +417,7 @@ async fn resolve_method_handle(vm: &VM, thread: &Thread, frame: &Frame) -> Resul
421417
/// in the constant pool. This function resolves those arguments from the constant pool and creates
422418
/// arguments vector.
423419
async fn resolve_static_bootstrap_arguments(
424-
vm: &Arc<VM>,
420+
thread: &Arc<Thread>,
425421
constant_pool: &ConstantPool,
426422
bootstrap_method: &BootstrapMethod,
427423
) -> Result<Vec<Value>> {
@@ -443,7 +439,7 @@ async fn resolve_static_bootstrap_arguments(
443439
}
444440
Constant::String(value) => {
445441
let string = constant_pool.try_get_utf8(*value)?;
446-
let value = string.to_object(vm).await?;
442+
let value = string.to_object(thread).await?;
447443
arguments.push(value);
448444
}
449445
_ => {
@@ -511,12 +507,12 @@ pub async fn resolve_call_site(frame: &Frame, method_index: u16) -> Result<Value
511507
);
512508

513509
// Invoke the bootstrap method
514-
let vm = thread.vm()?;
515-
let method_handle = resolve_method_handle(&vm, &thread, frame).await?;
510+
let method_handle = resolve_method_handle(&thread, frame).await?;
516511
let static_arguments =
517-
resolve_static_bootstrap_arguments(&vm, constant_pool, &bootstrap_method_attribute).await?;
518-
let method_name = bootstrap_method_attr_name.to_object(&vm).await?;
519-
let method_type = get_method_type(&vm, &thread, bootstrap_method_descriptor).await?;
512+
resolve_static_bootstrap_arguments(&thread, constant_pool, &bootstrap_method_attribute)
513+
.await?;
514+
let method_name = bootstrap_method_attr_name.to_object(&thread).await?;
515+
let method_type = get_method_type(&thread, bootstrap_method_descriptor).await?;
520516

521517
// 4.1 Construct argument array for bootstrap method:
522518
let mut arguments = vec![method_handle, method_name, method_type];

ristretto_vm/src/instruction/ldc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ async fn load_constant(
4949
Constant::String(utf8_index) => {
5050
let utf8_value = constant_pool.try_get_utf8(*utf8_index)?;
5151
let thread = frame.thread()?;
52-
let vm = thread.vm()?;
53-
utf8_value.to_object(&vm).await?
52+
utf8_value.to_object(&thread).await?
5453
}
5554
Constant::Class(class_index) => {
5655
let class_name = constant_pool.try_get_utf8(*class_index)?;
5756
let thread = frame.thread()?;
58-
let vm = thread.vm()?;
5957
let class = thread.class(class_name).await?;
60-
class.to_object(&vm).await?
58+
class.to_object(&thread).await?
6159
}
6260
constant => {
6361
return Err(InvalidConstant {

ristretto_vm/src/instruction/object.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ mod tests {
641641

642642
#[tokio::test]
643643
async fn test_checkcast_string_to_object() -> Result<()> {
644-
let (vm, _thread, mut frame) = crate::test::frame().await?;
644+
let (_vm, thread, mut frame) = crate::test::frame().await?;
645645
let stack = &mut OperandStack::with_max_size(1);
646-
let string = "foo".to_object(&vm).await?;
646+
let string = "foo".to_object(&thread).await?;
647647
stack.push(string)?;
648648
let class_index = get_class_index(&mut frame, "java/lang/Object")?;
649649
let result = checkcast(&frame, stack, class_index).await?;
@@ -695,9 +695,9 @@ mod tests {
695695

696696
#[tokio::test]
697697
async fn test_instanceof_string_to_object() -> Result<()> {
698-
let (vm, _thread, mut frame) = crate::test::frame().await?;
698+
let (_vm, thread, mut frame) = crate::test::frame().await?;
699699
let stack = &mut OperandStack::with_max_size(1);
700-
let string = "foo".to_object(&vm).await?;
700+
let string = "foo".to_object(&thread).await?;
701701
stack.push(string)?;
702702
let class_index = get_class_index(&mut frame, "java/lang/Object")?;
703703
let result = instanceof(&frame, stack, class_index).await?;

ristretto_vm/src/intrinsic_methods/java/io/objectstreamclass.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ mod tests {
3939

4040
#[tokio::test]
4141
async fn test_has_static_initializer_false() -> Result<()> {
42-
let (vm, thread) = crate::test::thread().await.expect("thread");
42+
let (_vm, thread) = crate::test::thread().await.expect("thread");
4343
let class = thread.class("java/lang/Object").await?;
44-
let class_object = class.to_object(&vm).await?;
44+
let class_object = class.to_object(&thread).await?;
4545
let mut parameters = Parameters::default();
4646
parameters.push(class_object);
4747
let has_initializer = has_static_initializer(thread, parameters).await?;
@@ -51,9 +51,9 @@ mod tests {
5151

5252
#[tokio::test]
5353
async fn test_has_static_initializer_true() -> Result<()> {
54-
let (vm, thread) = crate::test::thread().await.expect("thread");
54+
let (_vm, thread) = crate::test::thread().await.expect("thread");
5555
let class = thread.class("java/lang/System").await?;
56-
let class_object = class.to_object(&vm).await?;
56+
let class_object = class.to_object(&thread).await?;
5757
let mut parameters = Parameters::default();
5858
parameters.push(class_object);
5959
let has_initializer = has_static_initializer(thread, parameters).await?;

0 commit comments

Comments
 (0)