linera_witty/runtime/wasmtime/
parameters.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Representation of Wasmtime function parameter types.
5
6use frunk::{hlist, hlist_pat, HList};
7use wasmtime::{WasmParams, WasmTy};
8
9use crate::{primitive_types::FlatType, Layout};
10
11/// Conversions between flat layouts and Wasmtime parameter types.
12pub trait WasmtimeParameters {
13    /// The type Wasmtime uses to represent the parameters.
14    type Parameters: WasmParams;
15
16    /// Converts from this flat layout into Wasmtime's representation.
17    fn into_wasmtime(self) -> Self::Parameters;
18
19    /// Converts from Wasmtime's representation into a flat layout.
20    fn from_wasmtime(parameters: Self::Parameters) -> Self;
21}
22
23/// Helper macro to implement [`WasmtimeParameters`] for flat layouts up to the maximum limit.
24///
25/// The maximum number of parameters is defined by the [canonical
26/// ABI](https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#flattening)
27/// as the `MAX_FLAT_PARAMS` constant. There is no equivalent constant defined in Witty. Instead,
28/// any attempt to use more than the limit should lead to a compiler error. Therefore, this macro
29/// only implements the trait up to the limit. The same is done in other parts of the code, like
30/// for example in
31/// [`FlatHostParameters`][`crate::imported_function_interface::FlatHostParameters`].
32macro_rules! parameters {
33    ($( $names:ident : $types:ident ),*) => {
34        impl<$( $types ),*> WasmtimeParameters for HList![$( $types ),*]
35        where
36            $( $types: FlatType + WasmTy, )*
37        {
38            type Parameters = ($( $types, )*);
39
40            #[allow(clippy::unused_unit)]
41            fn into_wasmtime(self) -> Self::Parameters {
42                let hlist_pat![$( $names ),*] = self;
43
44                ($( $names, )*)
45            }
46
47            fn from_wasmtime(($( $names, )*): Self::Parameters) -> Self {
48                hlist![$( $names ),*]
49            }
50        }
51    };
52}
53
54repeat_macro!(parameters =>
55    a: A,
56    b: B,
57    c: C,
58    d: D,
59    e: E,
60    f: F,
61    g: G,
62    h: H,
63    i: I,
64    j: J,
65    k: K,
66    l: L,
67    m: M,
68    n: N,
69    o: O,
70    p: P,
71    q: Q
72);
73
74impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, Rest> WasmtimeParameters for HList![A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, ...Rest]
75where
76    A: FlatType,
77    B: FlatType,
78    C: FlatType,
79    D: FlatType,
80    E: FlatType,
81    F: FlatType,
82    G: FlatType,
83    H: FlatType,
84    I: FlatType,
85    J: FlatType,
86    K: FlatType,
87    L: FlatType,
88    M: FlatType,
89    N: FlatType,
90    O: FlatType,
91    P: FlatType,
92    Q: FlatType,
93    R: FlatType,
94    Rest: Layout,
95{
96    type Parameters = (i32,);
97
98    fn into_wasmtime(self) -> Self::Parameters {
99        unreachable!("Attempt to convert a list of flat parameters larger than the maximum limit");
100    }
101
102    fn from_wasmtime(_: Self::Parameters) -> Self {
103        unreachable!(
104            "Attempt to convert into a list of flat parameters larger than the maximum limit"
105        );
106    }
107}