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