wasmtime_environ/
module_types.rs

1use crate::{PrimaryMap, SignatureIndex, WasmFuncType};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use std::ops::Index;
5
6/// All types used in a core wasm module.
7///
8/// At this time this only contains function types. Note, though, that function
9/// types are deduplicated within this [`ModuleTypes`].
10///
11/// Note that accesing this type is primarily done through the `Index`
12/// implementations for this type.
13#[derive(Default, Serialize, Deserialize)]
14#[allow(missing_docs)]
15pub struct ModuleTypes {
16    wasm_signatures: PrimaryMap<SignatureIndex, WasmFuncType>,
17}
18
19impl ModuleTypes {
20    /// Returns an iterator over all the wasm function signatures found within
21    /// this module.
22    pub fn wasm_signatures(&self) -> impl Iterator<Item = (SignatureIndex, &WasmFuncType)> {
23        self.wasm_signatures.iter()
24    }
25}
26
27impl Index<SignatureIndex> for ModuleTypes {
28    type Output = WasmFuncType;
29
30    fn index(&self, sig: SignatureIndex) -> &WasmFuncType {
31        &self.wasm_signatures[sig]
32    }
33}
34
35/// A builder for [`ModuleTypes`].
36#[derive(Default)]
37#[allow(missing_docs)]
38pub struct ModuleTypesBuilder {
39    types: ModuleTypes,
40    interned_func_types: HashMap<WasmFuncType, SignatureIndex>,
41}
42
43impl ModuleTypesBuilder {
44    /// Reserves space for `amt` more type signatures.
45    pub fn reserve_wasm_signatures(&mut self, amt: usize) {
46        self.types.wasm_signatures.reserve(amt);
47    }
48
49    /// Interns the `sig` specified and returns a unique `SignatureIndex` that
50    /// can be looked up within [`ModuleTypes`] to recover the [`WasmFuncType`]
51    /// at runtime.
52    pub fn wasm_func_type(&mut self, sig: WasmFuncType) -> SignatureIndex {
53        if let Some(idx) = self.interned_func_types.get(&sig) {
54            return *idx;
55        }
56
57        let idx = self.types.wasm_signatures.push(sig.clone());
58        self.interned_func_types.insert(sig, idx);
59        return idx;
60    }
61
62    /// Returns the result [`ModuleTypes`] of this builder.
63    pub fn finish(self) -> ModuleTypes {
64        self.types
65    }
66}
67
68// Forward the indexing impl to the internal `ModuleTypes`
69impl<T> Index<T> for ModuleTypesBuilder
70where
71    ModuleTypes: Index<T>,
72{
73    type Output = <ModuleTypes as Index<T>>::Output;
74
75    fn index(&self, sig: T) -> &Self::Output {
76        &self.types[sig]
77    }
78}