Skip to content

Commit

Permalink
Extend exception handling support
Browse files Browse the repository at this point in the history
Still some tests failing, but added catch & throw instructions as well as exception reference support.
  • Loading branch information
RReverser committed Jan 17, 2024
1 parent 30296fd commit 31731a2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/instructions/exceptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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>,
}
11 changes: 11 additions & 0 deletions src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ pub enum Instruction {
LoopStart(BlockType) = OP_CODE_LOOP_START,
IfStart(BlockType) = OP_CODE_IF_START,
IfElse = 0x05,
#[cfg(feature = "exception-handling")]
Throw(crate::indices::ExceptionId) = 0x08,
#[cfg(feature = "exception-handling")]
ThrowRef = 0x0A,
End = OP_CODE_END,
Br(LabelId) = 0x0C,
BrIf(LabelId) = 0x0D,
Expand All @@ -162,6 +166,8 @@ pub enum Instruction {
Drop = 0x1A,
Select = 0x1B,
SelectWithTypes(Vec<ValueType>) = 0x1C,
#[cfg(feature = "exception-handling")]
TryTable(TryTable) = 0x1F,
LocalGet(LocalId) = 0x20,
LocalSet(LocalId) = 0x21,
LocalTee(LocalId) = 0x22,
Expand Down Expand Up @@ -345,3 +351,8 @@ pub use simd::SIMD;
pub mod threads;
#[cfg(feature = "threads")]
pub use threads::Atomic;

#[cfg(feature = "exception-handling")]
pub mod exceptions;
#[cfg(feature = "exception-handling")]
pub use exceptions::{Catch, TryTable};
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ encode_decode_as!(MemType, {
pub enum RefType {
Func = 0x70,
Extern = 0x6F,
#[cfg(feature = "exception-handling")]
Exception = 0x69,
}

/// [Table type](https://webassembly.github.io/spec/core/binary/types.html#table-types).
Expand Down

0 comments on commit 31731a2

Please sign in to comment.