linera_witty/runtime/
borrowed_instance.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Implementations of Wasm instance related traits to mutable borrows of instances.
5//!
6//! This allows using the same traits without having to move the type implementation around, for
7//! example as parameters in reentrant functions.
8
9use std::borrow::Cow;
10
11use super::{
12    traits::{CabiFreeAlias, CabiReallocAlias},
13    Instance, InstanceWithFunction, InstanceWithMemory, Runtime, RuntimeError, RuntimeMemory,
14};
15use crate::{memory_layout::FlatLayout, GuestPointer};
16
17impl<I> Instance for &mut I
18where
19    I: Instance,
20{
21    type Runtime = I::Runtime;
22    type UserData = I::UserData;
23    type UserDataReference<'a> = I::UserDataReference<'a>
24    where
25        Self::UserData: 'a,
26        Self: 'a;
27    type UserDataMutReference<'a> = I::UserDataMutReference<'a>
28    where
29        Self::UserData: 'a,
30        Self: 'a;
31
32    fn load_export(&mut self, name: &str) -> Option<<Self::Runtime as Runtime>::Export> {
33        I::load_export(*self, name)
34    }
35
36    fn user_data(&self) -> Self::UserDataReference<'_> {
37        I::user_data(*self)
38    }
39
40    fn user_data_mut(&mut self) -> Self::UserDataMutReference<'_> {
41        I::user_data_mut(*self)
42    }
43}
44
45impl<Parameters, Results, I> InstanceWithFunction<Parameters, Results> for &mut I
46where
47    I: InstanceWithFunction<Parameters, Results>,
48    Parameters: FlatLayout,
49    Results: FlatLayout,
50{
51    type Function = I::Function;
52
53    fn function_from_export(
54        &mut self,
55        export: <Self::Runtime as Runtime>::Export,
56    ) -> Result<Option<Self::Function>, RuntimeError> {
57        I::function_from_export(*self, export)
58    }
59
60    fn call(
61        &mut self,
62        function: &Self::Function,
63        parameters: Parameters,
64    ) -> Result<Results, RuntimeError> {
65        I::call(*self, function, parameters)
66    }
67}
68
69impl<'a, I> InstanceWithMemory for &'a mut I
70where
71    I: InstanceWithMemory,
72    &'a mut I: Instance<Runtime = I::Runtime> + CabiReallocAlias + CabiFreeAlias,
73{
74    fn memory_from_export(
75        &self,
76        export: <Self::Runtime as Runtime>::Export,
77    ) -> Result<Option<<Self::Runtime as Runtime>::Memory>, RuntimeError> {
78        I::memory_from_export(&**self, export)
79    }
80}
81
82impl<M, I> RuntimeMemory<&mut I> for M
83where
84    M: RuntimeMemory<I>,
85{
86    /// Reads `length` bytes from memory from the provided `location`.
87    fn read<'instance>(
88        &self,
89        instance: &'instance &mut I,
90        location: GuestPointer,
91        length: u32,
92    ) -> Result<Cow<'instance, [u8]>, RuntimeError> {
93        self.read(&**instance, location, length)
94    }
95
96    /// Writes the `bytes` to memory at the provided `location`.
97    fn write(
98        &mut self,
99        instance: &mut &mut I,
100        location: GuestPointer,
101        bytes: &[u8],
102    ) -> Result<(), RuntimeError> {
103        self.write(&mut **instance, location, bytes)
104    }
105}