1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use super::Instruction;
use crate::builtins::WasmbinCountable;
use crate::indices::{ExceptionId, LabelId};
use crate::io::{encode_decode_as, Wasmbin};
use crate::types::BlockType;
use crate::visit::Visit;

#[derive(Wasmbin)]
#[repr(u8)]
enum CatchRepr {
    Catch {
        exception: ExceptionId,
        target: LabelId,
    } = 0x00,
    CatchRef {
        exception: ExceptionId,
        target: LabelId,
    } = 0x01,
    CatchAll {
        target: LabelId,
    } = 0x02,
    CatchAllRef {
        target: LabelId,
    } = 0x03,
}

#[derive(WasmbinCountable, Debug, PartialEq, Eq, Hash, Clone, Visit)]
pub struct Catch {
    /// Whether to store an exception reference on the stack.
    pub catch_ref: bool,
    /// Catch a specific exception or any exception if set to `None`.
    pub exception_filter: Option<ExceptionId>,
    /// Target label.
    pub target: LabelId,
}

encode_decode_as!(Catch, {
    (Catch { catch_ref: false, exception_filter: Some(exception), target }) <=> (CatchRepr::Catch { exception, target }),
    (Catch { catch_ref: true, exception_filter: Some(exception), target }) <=> (CatchRepr::CatchRef { exception, target }),
    (Catch { catch_ref: false, exception_filter: None, target }) <=> (CatchRepr::CatchAll { target }),
    (Catch { catch_ref: true, exception_filter: None, target }) <=> (CatchRepr::CatchAllRef { target }),
});

#[derive(Wasmbin, Debug, PartialEq, Eq, Hash, Clone, Visit)]
pub struct TryTable {
    pub block_type: BlockType,
    pub catches: Vec<Catch>,
    pub instructions: Vec<Instruction>,
}