linera_witty/imported_function_interface/
mod.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Generic representation of a function the host imports from a guest Wasm instance.
5//!
6//! This helps determining the actual signature of the imported guest Wasm function based on host
7//! types for the parameters and the results.
8//!
9//! The signature depends on the number of flat types used to represent the parameters and the
10//! results, as specified in the [canonical
11//! ABI](https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#flattening).
12
13mod parameters;
14mod results;
15
16use self::{parameters::FlatHostParameters, results::FlatHostResults};
17use crate::{
18    memory_layout::FlatLayout, InstanceWithMemory, Layout, Memory, Runtime, RuntimeError,
19    RuntimeMemory, WitLoad, WitStore,
20};
21
22/// Representation of an imported function's interface.
23///
24/// Implemented for a tuple pair of the host parameters type and the host results type, and then
25/// allows converting to the guest function's signature.
26pub trait ImportedFunctionInterface {
27    /// The type representing the host-side parameters.
28    type HostParameters: WitStore;
29
30    /// The type representing the host-side results.
31    type HostResults: WitLoad;
32
33    /// The flat layout representing the guest-side parameters.
34    type GuestParameters: FlatLayout;
35
36    /// The flat layout representing the guest-side results.
37    type GuestResults: FlatLayout;
38
39    /// Converts the host-side parameters into the guest-side parameters.
40    fn lower_parameters<Instance>(
41        parameters: Self::HostParameters,
42        memory: &mut Memory<'_, Instance>,
43    ) -> Result<Self::GuestParameters, RuntimeError>
44    where
45        Instance: InstanceWithMemory,
46        <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>;
47
48    /// Converts the guest-side results into the host-side results.
49    fn lift_results<Instance>(
50        results: Self::GuestResults,
51        memory: &Memory<'_, Instance>,
52    ) -> Result<Self::HostResults, RuntimeError>
53    where
54        Instance: InstanceWithMemory,
55        <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>;
56}
57
58impl<Parameters, Results> ImportedFunctionInterface for (Parameters, Results)
59where
60    Parameters: WitStore,
61    Results: WitLoad,
62    <Parameters::Layout as Layout>::Flat: FlatHostParameters,
63    <Results::Layout as Layout>::Flat: FlatHostResults,
64{
65    type HostParameters = Parameters;
66    type HostResults = Results;
67    type GuestParameters =
68        <<Parameters::Layout as Layout>::Flat as FlatHostParameters>::GuestParameters;
69    type GuestResults = <<Results::Layout as Layout>::Flat as FlatHostResults>::GuestResults;
70
71    fn lower_parameters<Instance>(
72        parameters: Self::HostParameters,
73        memory: &mut Memory<'_, Instance>,
74    ) -> Result<Self::GuestParameters, RuntimeError>
75    where
76        Instance: InstanceWithMemory,
77        <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
78    {
79        <<Parameters::Layout as Layout>::Flat as FlatHostParameters>::lower_parameters(
80            parameters, memory,
81        )
82    }
83
84    fn lift_results<Instance>(
85        results: Self::GuestResults,
86        memory: &Memory<'_, Instance>,
87    ) -> Result<Self::HostResults, RuntimeError>
88    where
89        Instance: InstanceWithMemory,
90        <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
91    {
92        <<Results::Layout as Layout>::Flat as FlatHostResults>::lift_results(results, memory)
93    }
94}