Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
feat: merge wasm/vexos panic impls
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Jan 5, 2024
1 parent e37480f commit 69919d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
40 changes: 39 additions & 1 deletion pros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub mod sensors;
pub mod sync;
#[macro_use]
pub mod task;

#[doc(hidden)]
pub use pros_sys as __pros_sys;
#[cfg(target_os = "vexos")]
Expand All @@ -86,7 +87,10 @@ pub use async_trait::async_trait;

pub type Result<T = ()> = core::result::Result<T, alloc::boxed::Box<dyn core::error::Error>>;

use alloc::boxed::Box;
use alloc::{boxed::Box, ffi::CString, format};

use crate::task::{suspend_all, PanicBehavior, PANIC_BEHAVIOR};

#[async_trait::async_trait]
pub trait AsyncRobot {
async fn opcontrol(&mut self) -> Result {
Expand Down Expand Up @@ -354,6 +358,40 @@ macro_rules! sync_robot {
};
}

#[panic_handler]
pub fn panic(info: &core::panic::PanicInfo) -> ! {
let suspend = unsafe { suspend_all() };
let current_task = task::current();

{
let task_name = current_task.name().unwrap_or_else(|_| "<unknown>".into());
// task 'User Initialization (PROS)' panicked at src/lib.rs:22:1:
// panic message here
let panic_msg = format!("task '{task_name}' {info}");

let c_msg =
CString::new(&*panic_msg).unwrap_or_else(|_| CString::new("Panicked!").unwrap());
unsafe {
pros_sys::puts(c_msg.as_ptr());
#[cfg(target_arch = "wasm32")]
wasm_env::sim_log_backtrace();
}

if PANIC_BEHAVIOR.with(|p| *p == PanicBehavior::Exit) {
unsafe {
pros_sys::exit(1);
}
}

println!("{panic_msg}");
}

drop(suspend);

current_task.abort();
unreachable!()
}

/// Commonly used features of pros-rs.
/// This module is meant to be glob imported.
pub mod prelude {
Expand Down
30 changes: 0 additions & 30 deletions pros/src/vexos_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,9 @@ use crate::{
};
use core::{
alloc::{GlobalAlloc, Layout},
mem::forget,
panic::PanicInfo,

Check warning on line 10 in pros/src/vexos_env.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `panic::PanicInfo`

Check warning on line 10 in pros/src/vexos_env.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `panic::PanicInfo`
};

#[panic_handler]
pub fn panic(info: &PanicInfo) -> ! {
let suspend = unsafe { suspend_all() };

let current_task = task::current();
let task_name = current_task.name().unwrap_or_else(|_| "<unknown>".into());
// task 'User Initialization (PROS)' panicked at src/lib.rs:22:1:
// panic message here
let panic_msg = format!("task '{task_name}' {info}");

let c_msg = CString::new(&*panic_msg).unwrap_or_else(|_| CString::new("Panicked!").unwrap());
unsafe {
pros_sys::puts(c_msg.as_ptr());
}

if PANIC_BEHAVIOR.with(|p| *p == PanicBehavior::Exit) {
unsafe {
exit(1);
}
}

drop(suspend);

println!("{panic_msg}");

current_task.abort();
unreachable!()
}

struct Allocator;
unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
Expand Down
15 changes: 3 additions & 12 deletions pros/src/wasm_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,9 @@ use dlmalloc::GlobalDlmalloc;
// no multithreading in wasm
static mut LAYOUTS: BTreeMap<*mut u8, Layout> = BTreeMap::new();

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
extern "C" {
fn sim_abort(msg: *const core::ffi::c_char) -> !;
}

let msg_str = format!("{info}");
let msg_c_str = CString::new(msg_str).unwrap();

unsafe {
sim_abort(msg_c_str.as_ptr());
}
extern "C" {
/// Prints a backtrace to the debug console
pub fn sim_log_backtrace();
}

#[no_mangle]
Expand Down

0 comments on commit 69919d4

Please sign in to comment.