ndless_handler/
lib.rs

1#![no_std]
2#![feature(lang_items)]
3#![feature(alloc_error_handler)]
4#![feature(panic_info_message)]
5extern crate alloc;
6
7use alloc::format;
8use alloc::string::ToString;
9
10use crate::allocator::CAllocator;
11use core::slice;
12
13mod allocator;
14
15#[cfg(feature = "eh-personality")]
16#[lang = "eh_personality"]
17extern "C" fn eh_personality() {}
18
19#[cfg(feature = "lang-start")]
20#[lang = "start"]
21fn lang_start<T: ndless::process::Termination + 'static>(
22	main: fn() -> T,
23	argc: isize,
24	argv: *const *const u8,
25) -> isize {
26	unsafe {
27		ndless::__init(slice::from_raw_parts(argv as *const _, argc as usize));
28	}
29	main().report() as isize
30}
31
32#[cfg(feature = "oom-handler")]
33#[alloc_error_handler]
34fn on_oom(_layout: core::alloc::Layout) -> ! {
35	unsafe {
36		ndless_sys::abort();
37	}
38}
39
40#[cfg(feature = "panic-handler")]
41#[panic_handler]
42fn panic(info: &core::panic::PanicInfo) -> ! {
43	{
44		let msg = match info.message() {
45			Some(err) => format!("An error occured: {}", err),
46			None => "An error occured!".to_string(),
47		};
48		let location = match info.location() {
49			Some(loc) => format!(
50				"In file {} at line {} column {}",
51				loc.file(),
52				loc.line(),
53				loc.column()
54			),
55			None => "".to_string(),
56		};
57		ndless::msg::msg("Error", &format!("{}\n{}", msg, location));
58	}
59	ndless::process::abort();
60}
61
62#[cfg(feature = "allocator")]
63#[global_allocator]
64static A: CAllocator = CAllocator;
65
66#[cfg(feature = "ctype-ptr")]
67#[no_mangle]
68pub static __ctype_ptr__: [u8; 128 + 256] = [0; 128 + 256];