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 YieldOutsideAsyncContext = 13,
73}
74
75impl TrapCode {
76 pub fn message(&self) -> &str {
78 match self {
79 Self::StackOverflow => "call stack exhausted",
80 Self::HeapAccessOutOfBounds => "out of bounds memory access",
81 Self::HeapMisaligned => "misaligned heap",
82 Self::TableAccessOutOfBounds => "undefined element: out of bounds table access",
83 Self::IndirectCallToNull => "uninitialized element",
84 Self::BadSignature => "indirect call type mismatch",
85 Self::IntegerOverflow => "integer overflow",
86 Self::IntegerDivisionByZero => "integer divide by zero",
87 Self::BadConversionToInteger => "invalid conversion to integer",
88 Self::UnreachableCodeReached => "unreachable",
89 Self::UnalignedAtomic => "unaligned atomic access",
90 Self::UncaughtException => "uncaught exception",
91 Self::UninitializedExnRef => "uninitialized exnref",
92 Self::YieldOutsideAsyncContext => {
93 "async imported function yielded when not called via `Function::call_async`"
94 }
95 }
96 }
97}
98
99impl Display for TrapCode {
100 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
101 let identifier = match *self {
102 Self::StackOverflow => "stk_ovf",
103 Self::HeapAccessOutOfBounds => "heap_get_oob",
104 Self::HeapMisaligned => "heap_misaligned",
105 Self::TableAccessOutOfBounds => "table_get_oob",
106 Self::IndirectCallToNull => "icall_null",
107 Self::BadSignature => "bad_sig",
108 Self::IntegerOverflow => "int_ovf",
109 Self::IntegerDivisionByZero => "int_divz",
110 Self::BadConversionToInteger => "bad_toint",
111 Self::UnreachableCodeReached => "unreachable",
112 Self::UnalignedAtomic => "unalign_atom",
113 Self::UncaughtException => "uncaught_exception",
114 Self::UninitializedExnRef => "uninitialized_exnref",
115 Self::YieldOutsideAsyncContext => "yield_outside_async_context",
116 };
117 f.write_str(identifier)
118 }
119}
120
121impl FromStr for TrapCode {
122 type Err = ();
123
124 fn from_str(s: &str) -> Result<Self, Self::Err> {
125 match s {
126 "stk_ovf" => Ok(Self::StackOverflow),
127 "heap_get_oob" => Ok(Self::HeapAccessOutOfBounds),
128 "heap_misaligned" => Ok(Self::HeapMisaligned),
129 "table_get_oob" => Ok(Self::TableAccessOutOfBounds),
130 "icall_null" => Ok(Self::IndirectCallToNull),
131 "bad_sig" => Ok(Self::BadSignature),
132 "int_ovf" => Ok(Self::IntegerOverflow),
133 "int_divz" => Ok(Self::IntegerDivisionByZero),
134 "bad_toint" => Ok(Self::BadConversionToInteger),
135 "unreachable" => Ok(Self::UnreachableCodeReached),
136 "unalign_atom" => Ok(Self::UnalignedAtomic),
137 "uncaught_exception" => Ok(Self::UncaughtException),
138 "uninitialized_exnref" => Ok(Self::UninitializedExnRef),
139 "yield_outside_async_context" => Ok(Self::YieldOutsideAsyncContext),
140 _ => Err(()),
141 }
142 }
143}
144
145#[derive(Debug)]
149pub enum OnCalledAction {
150 InvokeAgain,
152 Finish,
154 Trap(Box<dyn std::error::Error + Send + Sync>),
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161
162 const CODES: [TrapCode; 11] = [
164 TrapCode::StackOverflow,
165 TrapCode::HeapAccessOutOfBounds,
166 TrapCode::HeapMisaligned,
167 TrapCode::TableAccessOutOfBounds,
168 TrapCode::IndirectCallToNull,
169 TrapCode::BadSignature,
170 TrapCode::IntegerOverflow,
171 TrapCode::IntegerDivisionByZero,
172 TrapCode::BadConversionToInteger,
173 TrapCode::UnreachableCodeReached,
174 TrapCode::UnalignedAtomic,
175 ];
176
177 #[test]
178 fn display() {
179 for r in &CODES {
180 let tc = *r;
181 assert_eq!(tc.to_string().parse(), Ok(tc));
182 }
183 assert_eq!("bogus".parse::<TrapCode>(), Err(()));
184
185 assert_eq!("user".parse::<TrapCode>(), Err(()));
188 assert_eq!("user-1".parse::<TrapCode>(), Err(()));
189 assert_eq!("users".parse::<TrapCode>(), Err(()));
190 }
191}