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 for 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>
24        = I::UserDataReference<'a>
25    where
26        Self::UserData: 'a,
27        Self: 'a;
28    type UserDataMutReference<'a>
29        = I::UserDataMutReference<'a>
30    where
31        Self::UserData: 'a,
32        Self: 'a;
33
34    fn load_export(&mut self, name: &str) -> Option<<Self::Runtime as Runtime>::Export> {
35        I::load_export(*self, name)
36    }
37
38    fn user_data(&self) -> Self::UserDataReference<'_> {
39        I::user_data(*self)
40    }
41
42    fn user_data_mut(&mut self) -> Self::UserDataMutReference<'_> {
43        I::user_data_mut(*self)
44    }
45}
46
47impl<Parameters, Results, I> InstanceWithFunction<Parameters, Results> for &mut I
48where
49    I: InstanceWithFunction<Parameters, Results>,
50    Parameters: FlatLayout,
51    Results: FlatLayout,
52{
53    type Function = I::Function;
54
55    fn function_from_export(
56        &mut self,
57        export: <Self::Runtime as Runtime>::Export,
58    ) -> Result<Option<Self::Function>, RuntimeError> {
59        I::function_from_export(*self, export)
60    }
61
62    fn call(
63        &mut self,
64        function: &Self::Function,
65        parameters: Parameters,
66    ) -> Result<Results, RuntimeError> {
67        I::call(*self, function, parameters)
68    }
69}
70
71impl<'a, I> InstanceWithMemory for &'a mut I
72where
73    I: InstanceWithMemory,
74    &'a mut I: Instance<Runtime = I::Runtime> + CabiReallocAlias + CabiFreeAlias,
75{
76    fn memory_from_export(
77        &self,
78        export: <Self::Runtime as Runtime>::Export,
79    ) -> Result<Option<<Self::Runtime as Runtime>::Memory>, RuntimeError> {
80        I::memory_from_export(&**self, export)
81    }
82}
83
84impl<M, I> RuntimeMemory<&mut I> for M
85where
86    M: RuntimeMemory<I>,
87{
88    /// Reads `length` bytes from memory from the provided `location`.
89    fn read<'instance>(
90        &self,
91        instance: &'instance &mut I,
92        location: GuestPointer,
93        length: u32,
94    ) -> Result<Cow<'instance, [u8]>, RuntimeError> {
95        self.read(&**instance, location, length)
96    }
97
98    /// Writes the `bytes` to memory at the provided `location`.
99    fn write(
100        &mut self,
101        instance: &mut &mut I,
102        location: GuestPointer,
103        bytes: &[u8],
104    ) -> Result<(), RuntimeError> {
105        self.write(&mut **instance, location, bytes)
106    }
107}