1use crate::{EntireHandler, FlowContext, TrapHandler};
2use core::{mem::MaybeUninit, ptr::NonNull};
3
4pub type FastHandler = extern "C" fn(
6 ctx: FastContext,
7 a1: usize,
8 a2: usize,
9 a3: usize,
10 a4: usize,
11 a5: usize,
12 a6: usize,
13 a7: usize,
14) -> FastResult;
15
16#[repr(transparent)]
20pub struct FastContext(&'static mut TrapHandler);
21
22impl FastContext {
23 #[inline]
28 pub fn a0(&self) -> usize {
29 self.0.scratch
30 }
31
32 #[inline]
34 pub fn regs(&mut self) -> &mut FlowContext {
35 unsafe { self.0.context.as_mut() }
36 }
37
38 #[inline]
40 pub fn swap_context(&mut self, new: NonNull<FlowContext>) -> NonNull<FlowContext> {
41 core::mem::replace(&mut self.0.context, new)
42 }
43
44 #[inline]
46 pub fn call(self, argc: usize) -> FastResult {
47 unsafe { self.0.context.as_ref().load_others() };
48 if argc <= 2 {
49 FastResult::FastCall
50 } else {
51 FastResult::Call
52 }
53 }
54
55 #[inline]
59 pub fn restore(self) -> FastResult {
60 FastResult::Restore
61 }
62
63 #[inline]
65 pub fn switch_to(self, others: NonNull<FlowContext>) -> FastResult {
66 unsafe { others.as_ref().load_others() };
67 self.0.context = others;
68 FastResult::Switch
69 }
70
71 #[inline]
75 pub fn continue_with<T: 'static>(self, f: EntireHandler<T>, t: T) -> FastResult {
76 unsafe { *self.0.locate_fast_mail() = MaybeUninit::new(t) };
78 self.0.scratch = f as _;
79 FastResult::Continue
80 }
81}
82
83#[repr(usize)]
85pub enum FastResult {
86 FastCall = 0,
88 Call = 1,
90 Restore = 2,
92 Switch = 3,
94 Continue = 4,
96}