wasmtime_environ/
builtin.rs

1/// Helper macro to iterate over all builtin functions and their signatures.
2#[macro_export]
3macro_rules! foreach_builtin_function {
4    ($mac:ident) => {
5        $mac! {
6            /// Returns an index for wasm's `memory.grow` builtin function.
7            memory32_grow(vmctx: vmctx, delta: i64, index: i32) -> pointer;
8            /// Returns an index for wasm's `table.copy` when both tables are locally
9            /// defined.
10            table_copy(vmctx: vmctx, dst_index: i32, src_index: i32, dst: i32, src: i32, len: i32);
11            /// Returns an index for wasm's `table.init`.
12            table_init(vmctx: vmctx, table: i32, elem: i32, dst: i32, src: i32, len: i32);
13            /// Returns an index for wasm's `elem.drop`.
14            elem_drop(vmctx: vmctx, elem: i32);
15            /// Returns an index for wasm's `memory.copy`
16            memory_copy(vmctx: vmctx, dst_index: i32, dst: i64, src_index: i32, src: i64, len: i64);
17            /// Returns an index for wasm's `memory.fill` instruction.
18            memory_fill(vmctx: vmctx, memory: i32, dst: i64, val: i32, len: i64);
19            /// Returns an index for wasm's `memory.init` instruction.
20            memory_init(vmctx: vmctx, memory: i32, data: i32, dst: i64, src: i32, len: i32);
21            /// Returns a value for wasm's `ref.func` instruction.
22            ref_func(vmctx: vmctx, func: i32) -> pointer;
23            /// Returns an index for wasm's `data.drop` instruction.
24            data_drop(vmctx: vmctx, data: i32);
25            /// Returns a table entry after lazily initializing it.
26            table_get_lazy_init_funcref(vmctx: vmctx, table: i32, index: i32) -> pointer;
27            /// Returns an index for Wasm's `table.grow` instruction for `funcref`s.
28            table_grow_funcref(vmctx: vmctx, table: i32, delta: i32, init: pointer) -> i32;
29            /// Returns an index for Wasm's `table.grow` instruction for `externref`s.
30            table_grow_externref(vmctx: vmctx, table: i32, delta: i32, init: reference) -> i32;
31            /// Returns an index for Wasm's `table.fill` instruction for `externref`s.
32            table_fill_externref(vmctx: vmctx, table: i32, dst: i32, val: reference, len: i32);
33            /// Returns an index for Wasm's `table.fill` instruction for `funcref`s.
34            table_fill_funcref(vmctx: vmctx, table: i32, dst: i32, val: pointer, len: i32);
35            /// Returns an index to drop a `VMExternRef`.
36            drop_externref(vmctx: vmctx, val: pointer);
37            /// Returns an index to do a GC and then insert a `VMExternRef` into the
38            /// `VMExternRefActivationsTable`.
39            activations_table_insert_with_gc(vmctx: vmctx, val: reference);
40            /// Returns an index for Wasm's `global.get` instruction for `externref`s.
41            externref_global_get(vmctx: vmctx, global: i32) -> reference;
42            /// Returns an index for Wasm's `global.get` instruction for `externref`s.
43            externref_global_set(vmctx: vmctx, global: i32, val: reference);
44            /// Returns an index for wasm's `memory.atomic.notify` instruction.
45            memory_atomic_notify(vmctx: vmctx, memory: i32, addr: i64, count: i32) -> i32;
46            /// Returns an index for wasm's `memory.atomic.wait32` instruction.
47            memory_atomic_wait32(vmctx: vmctx, memory: i32, addr: i64, expected: i32, timeout: i64) -> i32;
48            /// Returns an index for wasm's `memory.atomic.wait64` instruction.
49            memory_atomic_wait64(vmctx: vmctx, memory: i32, addr: i64, expected: i64, timeout: i64) -> i32;
50            /// Invoked when fuel has run out while executing a function.
51            out_of_gas(vmctx: vmctx);
52            /// Invoked when we reach a new epoch.
53            new_epoch(vmctx: vmctx) -> i64;
54        }
55    };
56}
57
58/// An index type for builtin functions.
59#[derive(Copy, Clone, Debug)]
60pub struct BuiltinFunctionIndex(u32);
61
62impl BuiltinFunctionIndex {
63    /// Create a new `BuiltinFunctionIndex` from its index
64    pub const fn from_u32(i: u32) -> Self {
65        Self(i)
66    }
67
68    /// Return the index as an u32 number.
69    pub const fn index(&self) -> u32 {
70        self.0
71    }
72}
73
74macro_rules! declare_indexes {
75    (
76        $(
77            $( #[$attr:meta] )*
78            $name:ident( $( $pname:ident: $param:ident ),* ) $( -> $result:ident )?;
79        )*
80    ) => {
81        impl BuiltinFunctionIndex {
82            declare_indexes!(
83                @indices;
84                0;
85                $( $( #[$attr] )* $name; )*
86            );
87        }
88    };
89
90    // Base case: no more indices to declare, so define the total number of
91    // function indices.
92    (
93        @indices;
94        $len:expr;
95    ) => {
96        /// Returns the total number of builtin functions.
97        pub const fn builtin_functions_total_number() -> u32 {
98            $len
99        }
100    };
101
102    // Recursive case: declare the next index, and then keep declaring the rest of
103    // the indices.
104    (
105         @indices;
106         $index:expr;
107         $( #[$this_attr:meta] )*
108         $this_name:ident;
109         $(
110             $( #[$rest_attr:meta] )*
111             $rest_name:ident;
112         )*
113    ) => {
114        $( #[$this_attr] )*
115        pub const fn $this_name() -> Self {
116            Self($index)
117        }
118
119        declare_indexes!(
120            @indices;
121            ($index + 1);
122            $( $( #[$rest_attr] )* $rest_name; )*
123        );
124    }
125}
126
127foreach_builtin_function!(declare_indexes);