wasmtime/runtime/vm/
export.rs

1use crate::runtime::vm::vmcontext::{
2    VMContext, VMFuncRef, VMGlobalDefinition, VMMemoryDefinition, VMTableDefinition,
3    VMTagDefinition,
4};
5use core::ptr::NonNull;
6use wasmtime_environ::{DefinedMemoryIndex, Global, Memory, Table, Tag};
7
8/// The value of an export passed from one instance to another.
9pub enum Export {
10    /// A function export value.
11    Function(ExportFunction),
12
13    /// A table export value.
14    Table(ExportTable),
15
16    /// A memory export value.
17    Memory(ExportMemory),
18
19    /// A global export value.
20    Global(ExportGlobal),
21
22    /// A tag export value.
23    Tag(ExportTag),
24}
25
26/// A function export value.
27#[derive(Debug, Clone, Copy)]
28pub struct ExportFunction {
29    /// The `VMFuncRef` for this exported function.
30    ///
31    /// Note that exported functions cannot be a null funcref, so this is a
32    /// non-null pointer.
33    pub func_ref: NonNull<VMFuncRef>,
34}
35
36// As part of the contract for using `ExportFunction`, synchronization
37// properties must be upheld. Therefore, despite containing raw pointers,
38// it is declared as Send/Sync.
39unsafe impl Send for ExportFunction {}
40unsafe impl Sync for ExportFunction {}
41
42impl From<ExportFunction> for Export {
43    fn from(func: ExportFunction) -> Export {
44        Export::Function(func)
45    }
46}
47
48/// A table export value.
49#[derive(Debug, Clone)]
50pub struct ExportTable {
51    /// The address of the table descriptor.
52    pub definition: NonNull<VMTableDefinition>,
53    /// Pointer to the containing `VMContext`.
54    pub vmctx: NonNull<VMContext>,
55    /// The table declaration, used for compatibility checking.
56    pub table: Table,
57}
58
59// See docs on send/sync for `ExportFunction` above.
60unsafe impl Send for ExportTable {}
61unsafe impl Sync for ExportTable {}
62
63impl From<ExportTable> for Export {
64    fn from(func: ExportTable) -> Export {
65        Export::Table(func)
66    }
67}
68
69/// A memory export value.
70#[derive(Debug, Clone)]
71pub struct ExportMemory {
72    /// The address of the memory descriptor.
73    pub definition: NonNull<VMMemoryDefinition>,
74    /// Pointer to the containing `VMContext`.
75    pub vmctx: NonNull<VMContext>,
76    /// The memory declaration, used for compatibility checking.
77    pub memory: Memory,
78    /// The index at which the memory is defined within the `vmctx`.
79    pub index: DefinedMemoryIndex,
80}
81
82// See docs on send/sync for `ExportFunction` above.
83unsafe impl Send for ExportMemory {}
84unsafe impl Sync for ExportMemory {}
85
86impl From<ExportMemory> for Export {
87    fn from(func: ExportMemory) -> Export {
88        Export::Memory(func)
89    }
90}
91
92/// A global export value.
93#[derive(Debug, Clone)]
94pub struct ExportGlobal {
95    /// The address of the global storage.
96    pub definition: NonNull<VMGlobalDefinition>,
97    /// Pointer to the containing `VMContext`. May be null for host-created
98    /// globals.
99    pub vmctx: Option<NonNull<VMContext>>,
100    /// The global declaration, used for compatibility checking.
101    pub global: Global,
102}
103
104// See docs on send/sync for `ExportFunction` above.
105unsafe impl Send for ExportGlobal {}
106unsafe impl Sync for ExportGlobal {}
107
108impl From<ExportGlobal> for Export {
109    fn from(func: ExportGlobal) -> Export {
110        Export::Global(func)
111    }
112}
113
114/// A tag export value.
115#[derive(Debug, Clone)]
116pub struct ExportTag {
117    /// The address of the global storage.
118    pub definition: NonNull<VMTagDefinition>,
119    /// The global declaration, used for compatibility checking.
120    pub tag: Tag,
121}
122
123// See docs on send/sync for `ExportFunction` above.
124unsafe impl Send for ExportTag {}
125unsafe impl Sync for ExportTag {}
126
127impl From<ExportTag> for Export {
128    fn from(func: ExportTag) -> Export {
129        Export::Tag(func)
130    }
131}