112 lines
2.9 KiB
Rust
Executable File
112 lines
2.9 KiB
Rust
Executable File
use alloc::boxed::Box;
|
|
|
|
#[macro_export]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
macro_rules! println { ($($args:tt)*) => { crate::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }
|
|
|
|
#[macro_export]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
#[cfg(debug_assertions)]
|
|
macro_rules! dbg { ($($args:tt)*) => { crate::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }
|
|
|
|
#[macro_export]
|
|
#[cfg(not(debug_assertions))]
|
|
macro_rules! dbg {
|
|
($($args:tt)*) => {{}};
|
|
}
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
mod implementation {
|
|
use alloc::alloc::alloc;
|
|
use core::alloc::Layout;
|
|
use core::panic::PanicInfo;
|
|
use cstr_core::{c_char, CString};
|
|
|
|
extern "wasm" {
|
|
fn _print(s: *const u8);
|
|
fn _error(message: *const u8, file: *const u8, line: u32, position: u32);
|
|
}
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
pub fn print_raw(s: &[u8]) {
|
|
unsafe {
|
|
let cstring = CString::from_vec_unchecked(s.to_vec());
|
|
_print(cstring.as_ptr());
|
|
}
|
|
}
|
|
|
|
#[panic_handler]
|
|
#[no_mangle]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
#[cfg(not(test))]
|
|
pub fn begin_panic_handler(panic_info: &PanicInfo<'_>) -> ! {
|
|
let msg = CString::new(panic_info.message().unwrap().as_str().unwrap()).unwrap();
|
|
let mut line = 0;
|
|
let mut position = 0;
|
|
let mut file = CString::default();
|
|
if let Some(s) = panic_info.location() {
|
|
line = s.line();
|
|
position = s.column();
|
|
file = CString::new(s.file()).unwrap();
|
|
}
|
|
|
|
unsafe {
|
|
_error(msg.as_ptr(), file.as_ptr(), line, position);
|
|
}
|
|
loop {}
|
|
}
|
|
|
|
#[alloc_error_handler]
|
|
#[no_mangle]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
#[cfg(not(test))]
|
|
fn allocation_error_handler(layout: core::alloc::Layout) -> ! {
|
|
panic!("memory allocation of {} bytes failed", layout.size())
|
|
}
|
|
|
|
#[no_mangle]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
unsafe extern "wasm" fn allocate_mem(len: u32, align: u32) -> *mut u8 {
|
|
alloc(Layout::from_size_align(len as usize, align as usize).unwrap())
|
|
}
|
|
|
|
#[no_mangle]
|
|
#[cfg(not(feature = "mock_data"))]
|
|
unsafe extern "wasm" fn dealloc_cstring(ptr: *mut c_char) {
|
|
CString::from_raw(ptr);
|
|
}
|
|
}
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
pub use implementation::*;
|
|
|
|
pub struct ExternIterator<'a, T> {
|
|
len: usize,
|
|
index: usize,
|
|
getter: Box<dyn Fn(usize) -> T + 'a>,
|
|
}
|
|
|
|
impl<'a, T> ExternIterator<'a, T> {
|
|
pub fn new(len: usize, f: Box<dyn Fn(usize) -> T + 'a>) -> Self {
|
|
Self {
|
|
len,
|
|
index: 0,
|
|
getter: f,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, T> Iterator for ExternIterator<'a, T> {
|
|
type Item = T;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
let index = self.index;
|
|
if index >= self.len {
|
|
None
|
|
} else {
|
|
self.index += 1;
|
|
Some((self.getter)(index))
|
|
}
|
|
}
|
|
}
|