wasmer_vm/
tunables.rs

1use crate::MemoryError;
2use crate::{Memory, Table};
3use crate::{MemoryStyle, TableStyle};
4use crate::{VMMemoryDefinition, VMTableDefinition};
5use std::ptr::NonNull;
6use std::sync::Arc;
7use wasmer_types::{MemoryType, TableType};
8
9/// An engine delegates the creation of memories, tables, and globals
10/// to a foreign implementor of this trait.
11pub trait Tunables {
12    /// Construct a `MemoryStyle` for the provided `MemoryType`
13    fn memory_style(&self, memory: &MemoryType) -> MemoryStyle;
14
15    /// Construct a `TableStyle` for the provided `TableType`
16    fn table_style(&self, table: &TableType) -> TableStyle;
17
18    /// Create a memory owned by the host given a [`MemoryType`] and a [`MemoryStyle`].
19    fn create_host_memory(
20        &self,
21        ty: &MemoryType,
22        style: &MemoryStyle,
23    ) -> Result<Arc<dyn Memory>, MemoryError>;
24
25    /// Create a memory owned by the VM given a [`MemoryType`] and a [`MemoryStyle`].
26    ///
27    /// # Safety
28    /// - `vm_definition_location` must point to a valid location in VM memory.
29    unsafe fn create_vm_memory(
30        &self,
31        ty: &MemoryType,
32        style: &MemoryStyle,
33        vm_definition_location: NonNull<VMMemoryDefinition>,
34    ) -> Result<Arc<dyn Memory>, MemoryError>;
35
36    /// Create a table owned by the host given a [`TableType`] and a [`TableStyle`].
37    fn create_host_table(
38        &self,
39        ty: &TableType,
40        style: &TableStyle,
41    ) -> Result<Arc<dyn Table>, String>;
42
43    /// Create a table owned by the VM given a [`TableType`] and a [`TableStyle`].
44    ///
45    /// # Safety
46    /// - `vm_definition_location` must point to a valid location in VM memory.
47    unsafe fn create_vm_table(
48        &self,
49        ty: &TableType,
50        style: &TableStyle,
51        vm_definition_location: NonNull<VMTableDefinition>,
52    ) -> Result<Arc<dyn Table>, String>;
53}