Skip to content

Commit 6ce53c9

Browse files
committed
fix: implement Class.is_array
1 parent 1f6b91f commit 6ce53c9

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

ristretto_vm/src/native_methods/java_lang_class.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) fn register(registry: &mut MethodRegistry) {
2323
"(Ljava/lang/String;)Ljava/lang/Class;",
2424
get_primitive_class,
2525
);
26+
registry.register(class_name, "isArray", "()Z", is_array);
2627
registry.register(class_name, "isPrimitive", "()Z", is_primitive);
2728
registry.register(class_name, "registerNatives", "()V", register_natives);
2829
}
@@ -68,6 +69,24 @@ fn get_primitive_class(
6869
})
6970
}
7071

72+
#[expect(clippy::needless_pass_by_value)]
73+
fn is_array(
74+
_call_stack: Arc<CallStack>,
75+
mut arguments: Arguments,
76+
) -> Pin<Box<dyn Future<Output = Result<Option<Value>>>>> {
77+
Box::pin(async move {
78+
let Some(Reference::Object(object)) = arguments.pop_object()? else {
79+
return Err(RuntimeError("isPrimitive: no arguments".to_string()));
80+
};
81+
let class = object.class();
82+
if class.is_array() {
83+
Ok(Some(Value::Int(1)))
84+
} else {
85+
Ok(Some(Value::Int(0)))
86+
}
87+
})
88+
}
89+
7190
#[expect(clippy::needless_pass_by_value)]
7291
fn is_primitive(
7392
_call_stack: Arc<CallStack>,

ristretto_vm/src/vm.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::call_stack::CallStack;
2-
use crate::Error::UnsupportedClassFileVersion;
2+
use crate::Error::{RuntimeError, UnsupportedClassFileVersion};
33
use crate::{Configuration, Result};
44
use ristretto_classfile::{mutf8, Version};
55
use ristretto_classloader::manifest::MAIN_CLASS;
@@ -144,22 +144,24 @@ impl VM {
144144
.await?;
145145

146146
// TODO: Implement System::initPhase2()
147-
// let init_phase2_method = system_class.try_get_method("initPhase2", "(ZZ)I")?;
148-
// let phase2_result = self.invoke(
149-
// &system_class,
150-
// &init_phase2_method,
151-
// vec![Value::Int(1), Value::Int(1)],
152-
// )?;
153-
// let Some(Value::Int(result)) = phase2_result else {
154-
// return Err(RuntimeError(format!(
155-
// "System::initPhase2() call failed: {phase2_result:?}"
156-
// )));
157-
// };
158-
// if result != 0 {
159-
// return Err(RuntimeError(format!(
160-
// "System::initPhase2() call failed: {result}"
161-
// )));
162-
// }
147+
let init_phase2_method = system_class.try_get_method("initPhase2", "(ZZ)I")?;
148+
let phase2_result = self
149+
.invoke(
150+
&system_class,
151+
&init_phase2_method,
152+
vec![Value::Int(1), Value::Int(1)],
153+
)
154+
.await?;
155+
let Some(Value::Int(result)) = phase2_result else {
156+
return Err(RuntimeError(format!(
157+
"System::initPhase2() call failed: {phase2_result:?}"
158+
)));
159+
};
160+
if result != 0 {
161+
return Err(RuntimeError(format!(
162+
"System::initPhase2() call failed: {result}"
163+
)));
164+
}
163165

164166
// TODO: Implement System::initPhase3()
165167
// let init_phase3_method = system_class.try_get_method("initPhase3", "()V")?;

0 commit comments

Comments
 (0)