1 use crate::runtime::vm::vmcontext::{
2     VMContext, VMFuncRef, VMGlobalDefinition, VMMemoryDefinition, VMTableDefinition,
3 };
4 use core::ptr::NonNull;
5 use wasmtime_environ::{DefinedMemoryIndex, Global, MemoryPlan, TablePlan};
6 
7 /// The value of an export passed from one instance to another.
8 pub 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)]
24 pub struct ExportFunction {
25     /// The `VMFuncRef` 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 func_ref: NonNull<VMFuncRef>,
30 }
31 
32 // As part of the contract for using `ExportFunction`, synchronization
33 // properties must be upheld. Therefore, despite containing raw pointers,
34 // it is declared as Send/Sync.
35 unsafe impl Send for ExportFunction {}
36 unsafe impl Sync for ExportFunction {}
37 
38 impl 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)]
46 pub 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 compatibility checking.
52     pub table: TablePlan,
53 }
54 
55 // See docs on send/sync for `ExportFunction` above.
56 unsafe impl Send for ExportTable {}
57 unsafe impl Sync for ExportTable {}
58 
59 impl 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)]
67 pub 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.
79 unsafe impl Send for ExportMemory {}
80 unsafe impl Sync for ExportMemory {}
81 
82 impl 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)]
90 pub struct ExportGlobal {
91     /// The address of the global storage.
92     pub definition: *mut VMGlobalDefinition,
93     /// Pointer to the containing `VMContext`. May be null for host-created
94     /// globals.
95     pub vmctx: *mut VMContext,
96     /// The global declaration, used for compatibility checking.
97     pub global: Global,
98 }
99 
100 // See docs on send/sync for `ExportFunction` above.
101 unsafe impl Send for ExportGlobal {}
102 unsafe impl Sync for ExportGlobal {}
103 
104 impl From<ExportGlobal> for Export {
105     fn from(func: ExportGlobal) -> Export {
106         Export::Global(func)
107     }
108 }
109