2022-07-24 07:25:37 +00:00
|
|
|
use alloc::alloc::alloc;
|
|
|
|
use core::alloc::Layout;
|
2022-08-17 16:05:38 +00:00
|
|
|
#[cfg(not(feature = "mock_data"))]
|
2022-07-24 07:25:37 +00:00
|
|
|
use core::panic::PanicInfo;
|
2022-08-28 13:50:12 +00:00
|
|
|
#[cfg(feature = "mock_data")]
|
2022-09-16 09:01:17 +00:00
|
|
|
use cstr_core::CStr;
|
|
|
|
use cstr_core::{c_char, CString};
|
2022-07-24 07:25:37 +00:00
|
|
|
|
2022-08-17 16:05:38 +00:00
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
2022-07-24 07:25:37 +00:00
|
|
|
extern "wasm" {
|
2022-09-16 09:01:17 +00:00
|
|
|
fn _print(s: *const u8);
|
|
|
|
fn _error(message: *const u8, file: *const u8, line: u32, position: u32);
|
2022-07-24 07:25:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:05:38 +00:00
|
|
|
#[cfg(not(feature = "mock_data"))]
|
2022-09-16 09:01:17 +00:00
|
|
|
pub fn print_raw(s: CString) {
|
2022-07-24 07:25:37 +00:00
|
|
|
unsafe {
|
2022-09-16 09:01:17 +00:00
|
|
|
_print(s.as_ptr());
|
2022-07-24 07:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:05:38 +00:00
|
|
|
#[cfg(feature = "mock_data")]
|
2022-09-16 09:01:17 +00:00
|
|
|
pub fn print_raw(s: CString) {
|
2022-08-17 16:05:38 +00:00
|
|
|
unsafe {
|
2022-09-16 09:01:17 +00:00
|
|
|
println!("{}", s.into_string().unwrap());
|
2022-08-17 16:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 07:25:37 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! println { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
macro_rules! dbg { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
macro_rules! dbg {
|
|
|
|
($($args:tt)*) => {{}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[panic_handler]
|
|
|
|
#[no_mangle]
|
2022-08-17 16:05:38 +00:00
|
|
|
#[cfg(not(feature = "mock_data"))]
|
2022-07-24 07:25:37 +00:00
|
|
|
#[cfg(not(test))]
|
|
|
|
pub fn begin_panic_handler(panic_info: &PanicInfo<'_>) -> ! {
|
2022-09-16 09:01:17 +00:00
|
|
|
let msg = CString::new(panic_info.message().unwrap().as_str().unwrap()).unwrap();
|
2022-07-24 07:25:37 +00:00
|
|
|
let mut line = 0;
|
|
|
|
let mut position = 0;
|
2022-09-16 09:01:17 +00:00
|
|
|
let mut file = CString::default();
|
2022-07-24 07:25:37 +00:00
|
|
|
if let Some(s) = panic_info.location() {
|
|
|
|
line = s.line();
|
|
|
|
position = s.column();
|
2022-09-16 09:01:17 +00:00
|
|
|
file = CString::new(s.file()).unwrap();
|
2022-07-24 07:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2022-09-16 09:01:17 +00:00
|
|
|
_error(msg.as_ptr(), file.as_ptr(), line, position);
|
2022-07-24 07:25:37 +00:00
|
|
|
}
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[alloc_error_handler]
|
|
|
|
#[no_mangle]
|
2022-08-17 16:05:38 +00:00
|
|
|
#[cfg(not(feature = "mock_data"))]
|
2022-07-24 07:25:37 +00:00
|
|
|
#[cfg(not(test))]
|
|
|
|
fn allocation_error_handler(layout: core::alloc::Layout) -> ! {
|
|
|
|
panic!("memory allocation of {} bytes failed", layout.size())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2022-08-17 16:05:38 +00:00
|
|
|
#[cfg(not(feature = "mock_data"))]
|
2022-07-24 07:25:37 +00:00
|
|
|
unsafe extern "wasm" fn allocate_mem(len: u32, align: u32) -> *mut u8 {
|
|
|
|
alloc(Layout::from_size_align(len as usize, align as usize).unwrap())
|
|
|
|
}
|
2022-09-16 09:01:17 +00:00
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
|
|
unsafe extern "wasm" fn dealloc_cstring(ptr: *mut c_char) {
|
|
|
|
CString::from_raw(ptr);
|
|
|
|
}
|