near_vm_compiler/translator/state.rs
1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/2.3.0/ATTRIBUTIONS.md
3
4use near_vm_types::entity::PrimaryMap;
5use near_vm_types::{FunctionIndex, ImportIndex, ModuleInfo, SignatureIndex};
6use std::boxed::Box;
7use std::collections::HashMap;
8
9/// Map of signatures to a function's parameter and return types.
10pub(crate) type WasmTypes =
11 PrimaryMap<SignatureIndex, (Box<[wasmparser::ValType]>, Box<[wasmparser::ValType]>)>;
12
13/// Contains information decoded from the Wasm module that must be referenced
14/// during each Wasm function's translation.
15///
16/// This is only for data that is maintained by `near-vm-compiler` itself, as
17/// opposed to being maintained by the embedder. Data that is maintained by the
18/// embedder is represented with `ModuleEnvironment`.
19#[derive(Debug)]
20pub struct ModuleTranslationState {
21 /// A map containing a Wasm module's original, raw signatures.
22 ///
23 /// This is used for translating multi-value Wasm blocks inside functions,
24 /// which are encoded to refer to their type signature via index.
25 pub(crate) wasm_types: WasmTypes,
26
27 /// Imported functions names map.
28 pub import_map: HashMap<FunctionIndex, String>,
29}
30
31impl ModuleTranslationState {
32 /// Creates a new empty ModuleTranslationState.
33 pub fn new() -> Self {
34 Self { wasm_types: PrimaryMap::new(), import_map: HashMap::new() }
35 }
36
37 /// Build map of imported functions names for intrinsification.
38 #[tracing::instrument(target = "near_vm", level = "trace", skip_all)]
39 pub fn build_import_map(&mut self, module: &ModuleInfo) {
40 for key in module.imports.keys() {
41 let value = &module.imports[key];
42 match value {
43 ImportIndex::Function(index) => {
44 self.import_map.insert(*index, key.1.clone());
45 }
46 _ => {
47 // Non-function import.
48 }
49 }
50 }
51 }
52}