Skip to content

Commit bf0937f

Browse files
committed
Add ArithmeticException
1 parent f38394a commit bf0937f

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

java_runtime/src/classes/java/lang.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod arithmetic_exception;
12
mod array_index_out_of_bounds_exception;
23
mod class;
34
mod class_loader;
@@ -31,9 +32,9 @@ mod throwable;
3132
mod unsupported_operation_exception;
3233

3334
pub use self::{
34-
array_index_out_of_bounds_exception::ArrayIndexOutOfBoundsException, class::Class, class_loader::ClassLoader,
35-
clone_not_supported_exception::CloneNotSupportedException, cloneable::Cloneable, comparable::Comparable, error::Error, exception::Exception,
36-
illegal_argument_exception::IllegalArgumentException, incompatible_class_change_error::IncompatibleClassChangeError,
35+
arithmetic_exception::ArithmeticException, array_index_out_of_bounds_exception::ArrayIndexOutOfBoundsException, class::Class,
36+
class_loader::ClassLoader, clone_not_supported_exception::CloneNotSupportedException, cloneable::Cloneable, comparable::Comparable, error::Error,
37+
exception::Exception, illegal_argument_exception::IllegalArgumentException, incompatible_class_change_error::IncompatibleClassChangeError,
3738
index_out_of_bounds_exception::IndexOutOfBoundsException, instantiation_error::InstantiationError, integer::Integer,
3839
interrupted_exception::InterruptedException, linkage_error::LinkageError, math::Math, no_class_def_found_error::NoClassDefFoundError,
3940
no_such_field_error::NoSuchFieldError, no_such_method_error::NoSuchMethodError, null_pointer_exception::NullPointerException, object::Object,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use alloc::vec;
2+
3+
use java_class_proto::JavaMethodProto;
4+
use jvm::{ClassInstanceRef, Jvm, Result};
5+
6+
use crate::{RuntimeClassProto, RuntimeContext, classes::java::lang::String};
7+
8+
// class java.lang.ArithmeticException
9+
pub struct ArithmeticException;
10+
11+
impl ArithmeticException {
12+
pub fn as_proto() -> RuntimeClassProto {
13+
RuntimeClassProto {
14+
name: "java/lang/ArithmeticException",
15+
parent_class: Some("java/lang/RuntimeException"),
16+
interfaces: vec![],
17+
methods: vec![
18+
JavaMethodProto::new("<init>", "()V", Self::init, Default::default()),
19+
JavaMethodProto::new("<init>", "(Ljava/lang/String;)V", Self::init_with_message, Default::default()),
20+
],
21+
fields: vec![],
22+
access_flags: Default::default(),
23+
}
24+
}
25+
26+
async fn init(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
27+
tracing::debug!("java.lang.ArithmeticException::<init>({:?})", &this);
28+
29+
let _: () = jvm.invoke_special(&this, "java/lang/RuntimeException", "<init>", "()V", ()).await?;
30+
31+
Ok(())
32+
}
33+
34+
async fn init_with_message(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, message: ClassInstanceRef<String>) -> Result<()> {
35+
tracing::debug!("java.lang.ArithmeticException::<init>({:?}, {:?})", &this, &message);
36+
37+
let _: () = jvm
38+
.invoke_special(&this, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", (message,))
39+
.await?;
40+
41+
Ok(())
42+
}
43+
}

jvm_rust/src/interpreter.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ impl Interpreter {
452452
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
453453
let value1: i32 = stack_frame.operand_stack.pop().unwrap().into();
454454

455+
if value2 == 0 {
456+
return Err(jvm.exception("java/lang/ArithmeticException", "Division by zero").await);
457+
}
458+
455459
stack_frame.operand_stack.push(JavaValue::Int(value1 / value2));
456460
}
457461
Opcode::IfAcmpeq(x) => {
@@ -717,6 +721,10 @@ impl Interpreter {
717721
let value2: i64 = stack_frame.operand_stack.pop().unwrap().into();
718722
let value1: i64 = stack_frame.operand_stack.pop().unwrap().into();
719723

724+
if value2 == 0 {
725+
return Err(jvm.exception("java/lang/ArithmeticException", "Division by zero").await);
726+
}
727+
720728
stack_frame.operand_stack.push(JavaValue::Long(value1 / value2));
721729
}
722730
Opcode::Lmul => {

0 commit comments

Comments
 (0)