1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::fmt;

use thiserror::Error;

use crate::object::{mem::RefTo, builtins::Class, runtime::RuntimeValue};

pub enum VMError {
    ArrayIndexOutOfBounds {
        at: i64
    }
}

impl VMError {
    pub fn class_name(&self) -> &'static str {
        match self {
            VMError::ArrayIndexOutOfBounds { .. } => "java/lang/ArrayIndexOutOfBoundsException",
        }
    }

    pub fn message(&self) -> String {
        let ctx = match self {
            VMError::ArrayIndexOutOfBounds { at } => format!("OOB @ {}", at),
        };

        format!("{}: {}", self.class_name(), ctx)
    }
}

#[derive(Error, Debug)]
pub enum Throwable {
    #[error(transparent)]
    Runtime(RuntimeException),

    #[error(transparent)]
    Internal(#[from] anyhow::Error),
}

#[macro_export]
macro_rules! internal {
    ($msg:literal $(,)?) => {
        $crate::Throwable::Internal(anyhow::anyhow!($msg))
    };
    ($err:expr $(,)?) => {
        $crate::Throwable::Internal(anyhow::anyhow!($err))
    };
    ($fmt:expr, $($arg:tt)*) => {
        $crate::Throwable::Internal(anyhow::anyhow!($fmt, $($arg)*))
    };
}

#[macro_export]
macro_rules! internalise {
    () => {
        |f| $crate::internal!(f)
    };
}
#[derive(Error, Debug, Clone)]
#[error("at {class_name}.{method_name}")]
pub struct Frame {
    pub method_name: String,
    pub class_name: String,
}

#[derive(Error)]
#[error("{message}")]
pub struct RuntimeException {
    pub message: String,
    pub ty: RefTo<Class>,
    pub obj: RuntimeValue,
    pub sources: Vec<Frame>,
}

impl fmt::Debug for RuntimeException {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RuntimeException")
            .field("message", &self.message)
            .field("ty", &&"<ty>")
            .field("sources", &self.sources)
            .finish()
    }
}