wasmtime/
code.rs

1use crate::signatures::SignatureCollection;
2use std::sync::Arc;
3#[cfg(feature = "component-model")]
4use wasmtime_environ::component::ComponentTypes;
5use wasmtime_environ::ModuleTypes;
6use wasmtime_jit::CodeMemory;
7
8/// Metadata in Wasmtime about a loaded compiled artifact in memory which is
9/// ready to execute.
10///
11/// This structure is used in both `Module` and `Component`. For components it's
12/// notably shared amongst the core wasm modules within a component and the
13/// component itself. For core wasm modules this is uniquely owned within a
14/// `Module`.
15pub struct CodeObject {
16    /// Actual underlying mmap which is executable and contains other compiled
17    /// information.
18    ///
19    /// Note the `Arc` here is used to share this with `CompiledModule` and the
20    /// global module registry of traps. While probably not strictly necessary
21    /// and could be avoided with some refactorings is a hopefully a relatively
22    /// minor `Arc` for now.
23    mmap: Arc<CodeMemory>,
24
25    /// Registered shared signature for the loaded object.
26    ///
27    /// Note that this type has a significant destructor which unregisters
28    /// signatures within the `Engine` it was originally tied to, and this ends
29    /// up corresponding to the liftime of a `Component` or `Module`.
30    signatures: SignatureCollection,
31
32    /// Type information for the loaded object.
33    ///
34    /// This is either a `ModuleTypes` or a `ComponentTypes` depending on the
35    /// top-level creator of this code.
36    types: Types,
37}
38
39impl CodeObject {
40    pub fn new(mmap: Arc<CodeMemory>, signatures: SignatureCollection, types: Types) -> CodeObject {
41        // The corresopnding unregister for this is below in `Drop for
42        // CodeObject`.
43        crate::module::register_code(&mmap);
44
45        CodeObject {
46            mmap,
47            signatures,
48            types,
49        }
50    }
51
52    pub fn code_memory(&self) -> &Arc<CodeMemory> {
53        &self.mmap
54    }
55
56    #[cfg(feature = "component-model")]
57    pub fn types(&self) -> &Types {
58        &self.types
59    }
60
61    pub fn module_types(&self) -> &ModuleTypes {
62        self.types.module_types()
63    }
64
65    pub fn signatures(&self) -> &SignatureCollection {
66        &self.signatures
67    }
68}
69
70impl Drop for CodeObject {
71    fn drop(&mut self) {
72        crate::module::unregister_code(&self.mmap);
73    }
74}
75
76pub enum Types {
77    Module(ModuleTypes),
78    #[cfg(feature = "component-model")]
79    Component(Arc<ComponentTypes>),
80}
81
82impl Types {
83    fn module_types(&self) -> &ModuleTypes {
84        match self {
85            Types::Module(m) => m,
86            #[cfg(feature = "component-model")]
87            Types::Component(c) => c.module_types(),
88        }
89    }
90}
91
92impl From<ModuleTypes> for Types {
93    fn from(types: ModuleTypes) -> Types {
94        Types::Module(types)
95    }
96}
97
98#[cfg(feature = "component-model")]
99impl From<Arc<ComponentTypes>> for Types {
100    fn from(types: Arc<ComponentTypes>) -> Types {
101        Types::Component(types)
102    }
103}