implement an api for errors

This commit is contained in:
Able 2023-03-30 02:35:02 -05:00
parent 9523507f1b
commit f16c9de3d5
4 changed files with 50 additions and 19 deletions

View file

@ -1,4 +1,4 @@
use core::arch::asm; use crate::{exit::exit, Error};
#[no_mangle] #[no_mangle]
unsafe extern "C" fn _start() -> ! { unsafe extern "C" fn _start() -> ! {
@ -8,14 +8,24 @@ unsafe extern "C" fn _start() -> ! {
// TODO: grab and pass arguments to main // TODO: grab and pass arguments to main
main(0, core::ptr::null()); let ret = main(0, core::ptr::null());
// asm!( // TODO: implement exiting properly and provide a public exit function
// "syscall", let err_obj = match ret {
// in("rax") 60, 0 => None,
// in("rdi") 0, 1 => {
// options(noreturn) // TODO: Get errorID location and error message
// );
Some(Error {
error_id: 123456789,
location: None,
a_lil_message_as_a_treat: "Unknown Cause",
})
}
_ => panic!("Program returned an invalid isize"),
};
exit(err_obj);
} }
#[lang = "start"] #[lang = "start"]

View file

@ -0,0 +1,4 @@
use crate::Error;
/// Exit a program
pub fn exit(error_code: Option<Error>) -> ! {}

View file

@ -4,10 +4,11 @@
mod entry; mod entry;
pub mod env; pub mod env;
pub mod exit;
pub mod io; pub mod io;
pub mod panic; pub mod panic;
use core::arch::asm; use core::{arch::asm, fmt::Display};
#[prelude_import] #[prelude_import]
pub use prelude::rust_2021::*; pub use prelude::rust_2021::*;
@ -49,3 +50,29 @@ pub fn print_char(c: char) {
); );
} }
} }
#[derive(Debug)]
pub struct LocationInFile<'a> {
// TODO: replace &str with ableOS path
file_path: &'a str,
line: u16,
column: u16,
}
impl<'a> Display for Error<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}{}{}", self.file_path, self.line, self.column)
}
}
pub struct Error<'a> {
error_id: u128,
// Optional location of the error
location: Option<LocationInFile<'a>>,
a_lil_message_as_a_treat: &'a str,
}
impl<'a> Display for Error<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "EID: {}\nSRC: {}", self.error_id, self.location)
}
}

View file

@ -4,15 +4,5 @@ use core::arch::asm;
fn panic_handler(pinfo: &core::panic::PanicInfo) -> ! { fn panic_handler(pinfo: &core::panic::PanicInfo) -> ! {
print("PANIC!\n"); print("PANIC!\n");
// #[cfg(unix)]
unsafe {
asm!(
"syscall",
in("rax") 231,
in("rdi") 1,
options(noreturn)
);
}
loop {} loop {}
} }