1use core::fmt::{self, Display, Formatter};
7use core::str::FromStr;
8use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
9#[cfg(feature = "enable-serde")]
10use serde::{Deserialize, Serialize};
11use thiserror::Error;
12
13#[derive(
17 Clone, Copy, PartialEq, Eq, Debug, Hash, Error, RkyvSerialize, RkyvDeserialize, Archive,
18)]
19#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
20#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
21#[rkyv(derive(Debug), compare(PartialEq))]
22#[repr(u32)]
23pub enum TrapCode {
24 StackOverflow = 0,
29
30 HeapAccessOutOfBounds = 1,
36
37 HeapMisaligned = 2,
39
40 TableAccessOutOfBounds = 3,
42
43 IndirectCallToNull = 4,
45
46 BadSignature = 5,
48
49 IntegerOverflow = 6,
51
52 IntegerDivisionByZero = 7,
54
55 BadConversionToInteger = 8,
57
58 UnreachableCodeReached = 9,
60
61 UnalignedAtomic = 10,
63
64 UncaughtException = 11,
66
67 UninitializedExnRef = 12,
69}
70
71impl TrapCode {
72 pub fn message(&self) -> &str {
74 match self {
75 Self::StackOverflow => "call stack exhausted",
76 Self::HeapAccessOutOfBounds => "out of bounds memory access",
77 Self::HeapMisaligned => "misaligned heap",
78 Self::TableAccessOutOfBounds => "undefined element: out of bounds table access",
79 Self::IndirectCallToNull => "uninitialized element",
80 Self::BadSignature => "indirect call type mismatch",
81 Self::IntegerOverflow => "integer overflow",
82 Self::IntegerDivisionByZero => "integer divide by zero",
83 Self::BadConversionToInteger => "invalid conversion to integer",
84 Self::UnreachableCodeReached => "unreachable",
85 Self::UnalignedAtomic => "unaligned atomic access",
86 Self::UncaughtException => "uncaught exception",
87 Self::UninitializedExnRef => "uninitialized exnref",
88 }
89 }
90}
91
92impl Display for TrapCode {
93 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
94 let identifier = match *self {
95 Self::StackOverflow => "stk_ovf",
96 Self::HeapAccessOutOfBounds => "heap_get_oob",
97 Self::HeapMisaligned => "heap_misaligned",
98 Self::TableAccessOutOfBounds => "table_get_oob",
99 Self::IndirectCallToNull => "icall_null",
100 Self::BadSignature => "bad_sig",
101 Self::IntegerOverflow => "int_ovf",
102 Self::IntegerDivisionByZero => "int_divz",
103 Self::BadConversionToInteger => "bad_toint",
104 Self::UnreachableCodeReached => "unreachable",
105 Self::UnalignedAtomic => "unalign_atom",
106 Self::UncaughtException => "uncaught_exception",
107 Self::UninitializedExnRef => "uninitialized_exnref",
108 };
109 f.write_str(identifier)
110 }
111}
112
113impl FromStr for TrapCode {
114 type Err = ();
115
116 fn from_str(s: &str) -> Result<Self, Self::Err> {
117 match s {
118 "stk_ovf" => Ok(Self::StackOverflow),
119 "heap_get_oob" => Ok(Self::HeapAccessOutOfBounds),
120 "heap_misaligned" => Ok(Self::HeapMisaligned),
121 "table_get_oob" => Ok(Self::TableAccessOutOfBounds),
122 "icall_null" => Ok(Self::IndirectCallToNull),
123 "bad_sig" => Ok(Self::BadSignature),
124 "int_ovf" => Ok(Self::IntegerOverflow),
125 "int_divz" => Ok(Self::IntegerDivisionByZero),
126 "bad_toint" => Ok(Self::BadConversionToInteger),
127 "unreachable" => Ok(Self::UnreachableCodeReached),
128 "unalign_atom" => Ok(Self::UnalignedAtomic),
129 "uncaught_exception" => Ok(Self::UncaughtException),
130 "uninitialized_exnref" => Ok(Self::UninitializedExnRef),
131 _ => Err(()),
132 }
133 }
134}
135
136#[derive(Debug)]
140pub enum OnCalledAction {
141 InvokeAgain,
143 Finish,
145 Trap(Box<dyn std::error::Error + Send + Sync>),
147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 const CODES: [TrapCode; 11] = [
155 TrapCode::StackOverflow,
156 TrapCode::HeapAccessOutOfBounds,
157 TrapCode::HeapMisaligned,
158 TrapCode::TableAccessOutOfBounds,
159 TrapCode::IndirectCallToNull,
160 TrapCode::BadSignature,
161 TrapCode::IntegerOverflow,
162 TrapCode::IntegerDivisionByZero,
163 TrapCode::BadConversionToInteger,
164 TrapCode::UnreachableCodeReached,
165 TrapCode::UnalignedAtomic,
166 ];
167
168 #[test]
169 fn display() {
170 for r in &CODES {
171 let tc = *r;
172 assert_eq!(tc.to_string().parse(), Ok(tc));
173 }
174 assert_eq!("bogus".parse::<TrapCode>(), Err(()));
175
176 assert_eq!("user".parse::<TrapCode>(), Err(()));
179 assert_eq!("user-1".parse::<TrapCode>(), Err(()));
180 assert_eq!("users".parse::<TrapCode>(), Err(()));
181 }
182}