wasmtime_runtime/
export.rs

1use crate::vmcontext::{
2    VMCallerCheckedFuncRef, VMContext, VMGlobalDefinition, VMMemoryDefinition, VMTableDefinition,
3};
4use std::ptr::NonNull;
5use wasmtime_environ::{DefinedMemoryIndex, Global, MemoryPlan, TablePlan};
6
7/// The value of an export passed from one instance to another.
8pub enum Export {
9    /// A function export value.
10    Function(ExportFunction),
11
12    /// A table export value.
13    Table(ExportTable),
14
15    /// A memory export value.
16    Memory(ExportMemory),
17
18    /// A global export value.
19    Global(ExportGlobal),
20}
21
22/// A function export value.
23#[derive(Debug, Clone, Copy)]
24pub struct ExportFunction {
25    /// The `VMCallerCheckedFuncRef` for this exported function.
26    ///
27    /// Note that exported functions cannot be a null funcref, so this is a
28    /// non-null pointer.
29    pub anyfunc: NonNull<VMCallerCheckedFuncRef>,
30}
31
32// It's part of the contract of using `ExportFunction` that synchronization
33// properties are upheld, so declare that despite the raw pointers inside this
34// is send/sync.
35unsafe impl Send for ExportFunction {}
36unsafe impl Sync for ExportFunction {}
37
38impl From<ExportFunction> for Export {
39    fn from(func: ExportFunction) -> Export {
40        Export::Function(func)
41    }
42}
43
44/// A table export value.
45#[derive(Debug, Clone)]
46pub struct ExportTable {
47    /// The address of the table descriptor.
48    pub definition: *mut VMTableDefinition,
49    /// Pointer to the containing `VMContext`.
50    pub vmctx: *mut VMContext,
51    /// The table declaration, used for compatibilty checking.
52    pub table: TablePlan,
53}
54
55// See docs on send/sync for `ExportFunction` above.
56unsafe impl Send for ExportTable {}
57unsafe impl Sync for ExportTable {}
58
59impl From<ExportTable> for Export {
60    fn from(func: ExportTable) -> Export {
61        Export::Table(func)
62    }
63}
64
65/// A memory export value.
66#[derive(Debug, Clone)]
67pub struct ExportMemory {
68    /// The address of the memory descriptor.
69    pub definition: *mut VMMemoryDefinition,
70    /// Pointer to the containing `VMContext`.
71    pub vmctx: *mut VMContext,
72    /// The memory declaration, used for compatibility checking.
73    pub memory: MemoryPlan,
74    /// The index at which the memory is defined within the `vmctx`.
75    pub index: DefinedMemoryIndex,
76}
77
78// See docs on send/sync for `ExportFunction` above.
79unsafe impl Send for ExportMemory {}
80unsafe impl Sync for ExportMemory {}
81
82impl From<ExportMemory> for Export {
83    fn from(func: ExportMemory) -> Export {
84        Export::Memory(func)
85    }
86}
87
88/// A global export value.
89#[derive(Debug, Clone)]
90pub struct ExportGlobal {
91    /// The address of the global storage.
92    pub definition: *mut VMGlobalDefinition,
93    /// The global declaration, used for compatibilty checking.
94    pub global: Global,
95}
96
97// See docs on send/sync for `ExportFunction` above.
98unsafe impl Send for ExportGlobal {}
99unsafe impl Sync for ExportGlobal {}
100
101impl From<ExportGlobal> for Export {
102    fn from(func: ExportGlobal) -> Export {
103        Export::Global(func)
104    }
105}