1 use crate::runtime::vm::vmcontext::{
2     VMContext, VMFuncRef, VMGlobalDefinition, VMMemoryDefinition, VMTableDefinition,
3     VMTagDefinition,
4 };
5 use core::ptr::NonNull;
6 use wasmtime_environ::{DefinedMemoryIndex, Global, Memory, Table, Tag};
7 
8 /// The value of an export passed from one instance to another.
9 pub 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)]
28 pub 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.
39 unsafe impl Send for ExportFunction {}
40 unsafe impl Sync for ExportFunction {}
41 
42 impl 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)]
50 pub 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.
60 unsafe impl Send for ExportTable {}
61 unsafe impl Sync for ExportTable {}
62 
63 impl 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)]
71 pub 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.
83 unsafe impl Send for ExportMemory {}
84 unsafe impl Sync for ExportMemory {}
85 
86 impl 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)]
94 pub 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.
105 unsafe impl Send for ExportGlobal {}
106 unsafe impl Sync for ExportGlobal {}
107 
108 impl 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)]
116 pub 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.
124 unsafe impl Send for ExportTag {}
125 unsafe impl Sync for ExportTag {}
126 
127 impl From<ExportTag> for Export {
128     fn from(func: ExportTag) -> Export {
129         Export::Tag(func)
130     }
131 }
132