From b283da1b9aaed89b42ab4db08cd4b2e6594b31b9 Mon Sep 17 00:00:00 2001 From: Able Date: Thu, 30 Mar 2023 02:35:02 -0500 Subject: [PATCH] implement an api for errors --- libraries/std/src/entry.rs | 26 ++++++++++++++++++-------- libraries/std/src/exit.rs | 4 ++++ libraries/std/src/lib.rs | 29 ++++++++++++++++++++++++++++- libraries/std/src/panic.rs | 10 ---------- 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 libraries/std/src/exit.rs diff --git a/libraries/std/src/entry.rs b/libraries/std/src/entry.rs index 3431b5f..97cef34 100644 --- a/libraries/std/src/entry.rs +++ b/libraries/std/src/entry.rs @@ -1,4 +1,4 @@ -use core::arch::asm; +use crate::{exit::exit, Error}; #[no_mangle] unsafe extern "C" fn _start() -> ! { @@ -8,14 +8,24 @@ unsafe extern "C" fn _start() -> ! { // TODO: grab and pass arguments to main - main(0, core::ptr::null()); + let ret = main(0, core::ptr::null()); - // asm!( - // "syscall", - // in("rax") 60, - // in("rdi") 0, - // options(noreturn) - // ); + // TODO: implement exiting properly and provide a public exit function + let err_obj = match ret { + 0 => None, + 1 => { + // 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"] diff --git a/libraries/std/src/exit.rs b/libraries/std/src/exit.rs new file mode 100644 index 0000000..aa15c06 --- /dev/null +++ b/libraries/std/src/exit.rs @@ -0,0 +1,4 @@ +use crate::Error; + +/// Exit a program +pub fn exit(error_code: Option) -> ! {} diff --git a/libraries/std/src/lib.rs b/libraries/std/src/lib.rs index 9f3fb9b..5801604 100644 --- a/libraries/std/src/lib.rs +++ b/libraries/std/src/lib.rs @@ -4,10 +4,11 @@ mod entry; pub mod env; +pub mod exit; pub mod io; pub mod panic; -use core::arch::asm; +use core::{arch::asm, fmt::Display}; #[prelude_import] 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>, + 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) + } +} diff --git a/libraries/std/src/panic.rs b/libraries/std/src/panic.rs index ad2c3f2..aa53e57 100644 --- a/libraries/std/src/panic.rs +++ b/libraries/std/src/panic.rs @@ -4,15 +4,5 @@ use core::arch::asm; fn panic_handler(pinfo: &core::panic::PanicInfo) -> ! { print("PANIC!\n"); - // #[cfg(unix)] - unsafe { - asm!( - "syscall", - in("rax") 231, - in("rdi") 1, - options(noreturn) - ); - } - loop {} }