near_vm_vm/artifact.rs
1use crate::{InstanceHandle, Resolver, Tunables, VMLocalFunction, VMSharedSignatureIndex};
2use near_vm_types::{
3 ElemIndex, FunctionIndex, GlobalInit, GlobalType, ImportCounts, InstanceConfig,
4 LocalFunctionIndex, OwnedDataInitializer, OwnedTableInitializer, entity::BoxedSlice,
5};
6use std::{any::Any, collections::BTreeMap, sync::Arc};
7
8/// [`Artifact`]s that can be instantiated.
9pub trait Instantiatable: Artifact {
10 /// The errors that can occur when instantiating.
11 type Error: std::error::Error + Send;
12
13 /// Crate an `Instance` from this `Artifact`.
14 ///
15 /// # Safety
16 ///
17 /// See [`InstanceHandle::new`].
18 unsafe fn instantiate(
19 self: Arc<Self>,
20 tunables: &dyn Tunables,
21 resolver: &dyn Resolver,
22 host_state: Box<dyn Any>,
23 config: InstanceConfig,
24 ) -> Result<InstanceHandle, Self::Error>;
25}
26
27/// A predecessor of a full module Instance.
28///
29/// This type represents parts of a compiled WASM module ([`Executable`](crate::Executable)) that
30/// are pre-allocated in within some Engine's store.
31///
32/// Some other operations such as linking, relocating and similar may also be performed during
33/// construction of the Artifact, making this type particularly well suited for caching in-memory.
34pub trait Artifact: Send + Sync {
35 /// The information about offsets into the VM context table.
36 fn offsets(&self) -> &crate::VMOffsets;
37
38 /// The count of imported entities.
39 fn import_counts(&self) -> &ImportCounts;
40
41 /// The locally defined functions.
42 ///
43 /// These are published and ready to call.
44 fn functions(&self) -> &BoxedSlice<LocalFunctionIndex, VMLocalFunction>;
45
46 /// Passive table elements.
47 fn passive_elements(&self) -> &BTreeMap<ElemIndex, Box<[FunctionIndex]>>;
48
49 /// Table initializers.
50 fn element_segments(&self) -> &[OwnedTableInitializer];
51
52 /// Memory initializers.
53 /// TODO: consider making it an iterator of `DataInitializer`s instead?
54 fn data_segments(&self) -> &[OwnedDataInitializer];
55
56 /// Passive table elements.
57 fn globals(&self) -> &[(GlobalType, GlobalInit)];
58
59 /// The function index to the start function.
60 fn start_function(&self) -> Option<FunctionIndex>;
61
62 /// Function by export name.
63 fn export_field(&self, name: &str) -> Option<near_vm_types::ExportIndex>;
64
65 /// Mapping between module SignatureIndex and VMSharedSignatureIndex.
66 fn signatures(&self) -> &[VMSharedSignatureIndex];
67
68 /// Obtain the function signature for either the import or local definition.
69 fn function_signature(&self, index: FunctionIndex) -> Option<VMSharedSignatureIndex>;
70}