wasmi/table/error.rs
1use super::TableType;
2use crate::core::ValType;
3use core::{fmt, fmt::Display};
4
5/// Errors that may occur upon operating with table entities.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum TableError {
9 /// Occurs when growing a table out of its set bounds.
10 GrowOutOfBounds {
11 /// The maximum allowed table size.
12 maximum: u32,
13 /// The current table size before the growth operation.
14 current: u32,
15 /// The amount of requested invalid growth.
16 delta: u32,
17 },
18 /// Occurs when operating with a [`Table`](crate::Table) and mismatching element types.
19 ElementTypeMismatch {
20 /// Expected element type for the [`Table`](crate::Table).
21 expected: ValType,
22 /// Encountered element type.
23 actual: ValType,
24 },
25 /// Occurs when accessing the table out of bounds.
26 AccessOutOfBounds {
27 /// The current size of the table.
28 current: u32,
29 /// The accessed index that is out of bounds.
30 offset: u32,
31 },
32 /// Occur when coping elements of tables out of bounds.
33 CopyOutOfBounds,
34 /// Occurs when `ty` is not a subtype of `other`.
35 InvalidSubtype {
36 /// The [`TableType`] which is not a subtype of `other`.
37 ty: TableType,
38 /// The [`TableType`] which is supposed to be a supertype of `ty`.
39 other: TableType,
40 },
41 TooManyTables,
42}
43
44impl Display for TableError {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match self {
47 Self::GrowOutOfBounds {
48 maximum,
49 current,
50 delta,
51 } => {
52 write!(
53 f,
54 "tried to grow table with size of {current} and maximum of \
55 {maximum} by {delta} out of bounds",
56 )
57 }
58 Self::ElementTypeMismatch { expected, actual } => {
59 write!(f, "encountered mismatching table element type, expected {expected:?} but found {actual:?}")
60 }
61 Self::AccessOutOfBounds { current, offset } => {
62 write!(
63 f,
64 "out of bounds access of table element {offset} \
65 of table with size {current}",
66 )
67 }
68 Self::CopyOutOfBounds => {
69 write!(f, "out of bounds access of table elements while copying")
70 }
71 Self::InvalidSubtype { ty, other } => {
72 write!(f, "table type {ty:?} is not a subtype of {other:?}",)
73 }
74 Self::TooManyTables => {
75 write!(f, "too many tables")
76 }
77 }
78 }
79}