macro_rules! for_each_operator { ($mac:ident) => { ... }; }
Expand description
A helper macro to conveniently iterate over all opcodes recognized by this
crate. This can be used to work with either the Operator
enumeration or
the VisitOperator
trait if your use case uniformly handles all operators
the same way.
It is also possible to specialize handling of operators depending on the Wasm proposal from which they are originating.
This is an “iterator macro” where this macro is invoked with the name of another macro, and then that macro is invoked with the list of all operators. An example invocation of this looks like:
The list of specializable Wasm proposals is as follows:
@mvp
: Denoting a Wasm operator from the initial Wasm MVP version.@exceptions
: Wasmexpection-handling
proposal@tail_call
: Wasmtail-calls
proposal@reference_types
: Wasmreference-types
proposal@sign_extension
: Wasmsign-extension-ops
proposal@saturating_float_to_int
: Wasmnon_trapping_float-to-int-conversions
proposal@bulk_memory
:Wasmbulk-memory
proposal@threads
: Wasmthreads
proposal@simd
: Wasmsimd
proposal@relaxed_simd
: Wasmrelaxed-simd
proposal
macro_rules! define_visit_operator {
// The outer layer of repetition represents how all operators are
// provided to the macro at the same time.
//
// The `$proposal` identifier indicates the Wasm proposals from which
// the Wasm operator is originating.
// For example to specialize the macro match arm for Wasm SIMD proposal
// operators you could write `@simd` instead of `@$proposal:ident` to
// only catch those operators.
//
// The `$op` name is bound to the `Operator` variant name. The
// payload of the operator is optionally specified (the `$(...)?`
// clause) since not all instructions have payloads. Within the payload
// each argument is named and has its type specified.
//
// The `$visit` name is bound to the corresponding name in the
// `VisitOperator` trait that this corresponds to.
($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident)*) => {
$(
fn $visit(&mut self $($(,$arg: $argty)*)?) {
// do nothing for this example
}
)*
}
}
pub struct VisitAndDoNothing;
impl<'a> wasmparser::VisitOperator<'a> for VisitAndDoNothing {
type Output = ();
wasmparser::for_each_operator!(define_visit_operator);
}