|
| 1 | +// Copyright (c) Zefchain Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Contains a [`StubInstance`] type and the code it requires to implement the necessary |
| 5 | +//! trait to be used as a Wasm instance type during compile time. |
| 6 | +
|
| 7 | +use std::{borrow::Cow, marker::PhantomData}; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + memory_layout::FlatLayout, GuestPointer, Instance, InstanceWithFunction, InstanceWithMemory, |
| 11 | + Runtime, RuntimeError, RuntimeMemory, |
| 12 | +}; |
| 13 | + |
| 14 | +/// A stub Wasm instance. |
| 15 | +/// |
| 16 | +/// This is when a Wasm instance type is needed for type-checking but is not needed during |
| 17 | +/// runtime. |
| 18 | +pub struct StubInstance<UserData = ()> { |
| 19 | + _user_data: PhantomData<UserData>, |
| 20 | +} |
| 21 | + |
| 22 | +impl<UserData> Default for StubInstance<UserData> { |
| 23 | + fn default() -> Self { |
| 24 | + StubInstance { |
| 25 | + _user_data: PhantomData, |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl<UserData> Instance for StubInstance<UserData> { |
| 31 | + type Runtime = StubRuntime; |
| 32 | + type UserData = UserData; |
| 33 | + type UserDataReference<'a> = &'a UserData |
| 34 | + where |
| 35 | + Self::UserData: 'a, |
| 36 | + Self: 'a; |
| 37 | + type UserDataMutReference<'a> = &'a mut UserData |
| 38 | + where |
| 39 | + Self::UserData: 'a, |
| 40 | + Self: 'a; |
| 41 | + |
| 42 | + fn load_export(&mut self, _name: &str) -> Option<()> { |
| 43 | + unimplemented!("`StubInstance` can not be used as a real `Instance`"); |
| 44 | + } |
| 45 | + |
| 46 | + fn user_data(&self) -> Self::UserDataReference<'_> { |
| 47 | + unimplemented!("`StubInstance` can not be used as a real `Instance`"); |
| 48 | + } |
| 49 | + |
| 50 | + fn user_data_mut(&mut self) -> Self::UserDataMutReference<'_> { |
| 51 | + unimplemented!("`StubInstance` can not be used as a real `Instance`"); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<Parameters, Results, UserData> InstanceWithFunction<Parameters, Results> |
| 56 | + for StubInstance<UserData> |
| 57 | +where |
| 58 | + Parameters: FlatLayout + 'static, |
| 59 | + Results: FlatLayout + 'static, |
| 60 | +{ |
| 61 | + type Function = (); |
| 62 | + |
| 63 | + fn function_from_export( |
| 64 | + &mut self, |
| 65 | + _name: <Self::Runtime as Runtime>::Export, |
| 66 | + ) -> Result<Option<Self::Function>, RuntimeError> { |
| 67 | + unimplemented!("`StubInstance` can not be used as a real `InstanceWithFunction`"); |
| 68 | + } |
| 69 | + |
| 70 | + fn call( |
| 71 | + &mut self, |
| 72 | + _function: &Self::Function, |
| 73 | + _parameters: Parameters, |
| 74 | + ) -> Result<Results, RuntimeError> { |
| 75 | + unimplemented!("`StubInstance` can not be used as a real `InstanceWithFunction`"); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<UserData> InstanceWithMemory for StubInstance<UserData> { |
| 80 | + fn memory_from_export(&self, _export: ()) -> Result<Option<StubMemory>, RuntimeError> { |
| 81 | + unimplemented!("`StubInstance` can not be used as a real `InstanceWithMemory`"); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// A stub Wasm runtime. |
| 86 | +pub struct StubRuntime; |
| 87 | + |
| 88 | +impl Runtime for StubRuntime { |
| 89 | + type Export = (); |
| 90 | + type Memory = StubMemory; |
| 91 | +} |
| 92 | + |
| 93 | +/// A stub Wasm runtime memory. |
| 94 | +pub struct StubMemory; |
| 95 | + |
| 96 | +impl<UserData> RuntimeMemory<StubInstance<UserData>> for StubMemory { |
| 97 | + fn read<'instance>( |
| 98 | + &self, |
| 99 | + _instance: &'instance StubInstance<UserData>, |
| 100 | + _location: GuestPointer, |
| 101 | + _length: u32, |
| 102 | + ) -> Result<Cow<'instance, [u8]>, RuntimeError> { |
| 103 | + unimplemented!("`StubMemory` can not be used as a real `RuntimeMemory`"); |
| 104 | + } |
| 105 | + |
| 106 | + fn write( |
| 107 | + &mut self, |
| 108 | + _instance: &mut StubInstance<UserData>, |
| 109 | + _location: GuestPointer, |
| 110 | + _bytes: &[u8], |
| 111 | + ) -> Result<(), RuntimeError> { |
| 112 | + unimplemented!("`StubMemory` can not be used as a real `RuntimeMemory`"); |
| 113 | + } |
| 114 | +} |
0 commit comments