marine/crates/it-interfaces/src/mit_interfaces.rs

208 lines
6.3 KiB
Rust
Raw Normal View History

2020-06-04 12:46:12 +03:00
/*
2024-06-26 12:37:14 +03:00
* Marine WebAssembly runtime
2020-06-04 12:46:12 +03:00
*
2024-06-26 12:37:14 +03:00
* Copyright (C) 2024 Fluence DAO
2020-06-04 12:46:12 +03:00
*
2024-06-26 12:37:14 +03:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation version 3 of the
* License.
2020-06-04 12:46:12 +03:00
*
2024-06-26 12:37:14 +03:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-06-04 12:46:12 +03:00
*/
2021-05-10 12:51:22 +03:00
use super::errors::MITInterfacesError;
2020-06-04 19:54:23 +03:00
2021-05-10 12:51:22 +03:00
use wasmer_it::interpreter::Instruction;
use wasmer_it::ast::*;
use wasmer_it::IRecordType;
2020-06-04 12:46:12 +03:00
use multimap::MultiMap;
use std::iter::Iterator;
use std::collections::HashMap;
use std::sync::Arc;
2020-06-04 12:46:12 +03:00
pub type CoreFunctionType = u32;
pub type AdapterFunctionType = u32;
2020-06-04 19:06:23 +03:00
pub type ExportName<'a> = &'a str;
pub type ImportName<'a> = &'a str;
pub type ImportNamespace<'a> = &'a str;
2021-05-10 12:51:22 +03:00
pub type ITAstType = Type;
2021-05-11 15:44:11 +03:00
#[derive(Debug)]
2021-05-10 12:51:22 +03:00
pub struct MITInterfaces<'a> {
2020-06-04 12:46:12 +03:00
/// All the types.
2021-05-10 12:51:22 +03:00
types: Vec<ITAstType>,
2020-06-04 12:46:12 +03:00
/// All the imported functions.
2020-06-07 12:33:26 +03:00
imports: Vec<Import<'a>>,
core_type_to_imports: MultiMap<CoreFunctionType, (ImportName<'a>, ImportNamespace<'a>)>,
2020-06-04 12:46:12 +03:00
/// All the adapters.
adapters: HashMap<AdapterFunctionType, Vec<Instruction>>,
/// All the exported functions.
2020-06-07 12:33:26 +03:00
exports: Vec<Export<'a>>,
core_type_to_exports: MultiMap<CoreFunctionType, ExportName<'a>>,
2020-06-04 12:46:12 +03:00
/// All the implementations.
adapter_type_to_core: MultiMap<AdapterFunctionType, CoreFunctionType>,
core_type_to_adapter: MultiMap<CoreFunctionType, AdapterFunctionType>,
}
2021-05-10 12:51:22 +03:00
impl<'a> MITInterfaces<'a> {
2020-06-04 19:06:23 +03:00
pub fn new(interfaces: Interfaces<'a>) -> Self {
2020-06-07 12:33:26 +03:00
let core_type_to_imports = interfaces
2020-06-04 12:46:12 +03:00
.imports
2020-06-07 12:33:26 +03:00
.iter()
2020-06-04 19:06:23 +03:00
.map(|import| (import.function_type, (import.namespace, import.name)))
2020-06-06 21:34:13 +03:00
.collect::<MultiMap<_, _>>();
2020-06-04 12:46:12 +03:00
let adapters = interfaces
.adapters
.into_iter()
.map(|adapter| (adapter.function_type, adapter.instructions))
.collect::<HashMap<_, _>>();
2020-06-07 12:33:26 +03:00
let core_type_to_exports = interfaces
2020-06-04 12:46:12 +03:00
.exports
2020-06-07 12:33:26 +03:00
.iter()
2020-06-04 19:06:23 +03:00
.map(|export| (export.function_type, export.name))
2020-06-06 21:34:13 +03:00
.collect::<MultiMap<_, _>>();
2020-06-04 12:46:12 +03:00
let adapter_type_to_core = interfaces
.implementations
.iter()
.map(|implementation| {
(
implementation.adapter_function_type,
implementation.core_function_type,
)
})
.collect::<MultiMap<_, _>>();
let core_type_to_adapter = interfaces
.implementations
.iter()
.map(|implementation| {
(
implementation.core_function_type,
implementation.adapter_function_type,
)
})
.collect::<MultiMap<_, _>>();
Self {
types: interfaces.types,
2020-06-07 12:33:26 +03:00
imports: interfaces.imports,
core_type_to_imports,
2020-06-04 12:46:12 +03:00
adapters,
2020-06-07 12:33:26 +03:00
exports: interfaces.exports,
core_type_to_exports,
2020-06-04 12:46:12 +03:00
adapter_type_to_core,
core_type_to_adapter,
}
}
2020-06-04 19:54:23 +03:00
pub fn types(&self) -> impl Iterator<Item = &Type> {
self.types.iter()
}
pub fn record_types(&self) -> impl Iterator<Item = (u64, &Arc<IRecordType>)> {
2020-10-01 12:19:38 +03:00
self.types.iter().enumerate().filter_map(|(id, t)| match t {
2021-05-10 12:51:22 +03:00
ITAstType::Record(r) => Some((id as u64, r)),
2020-10-01 12:19:38 +03:00
_ => None,
})
}
2020-06-04 19:06:23 +03:00
pub fn type_by_idx(&self, idx: u32) -> Option<&Type> {
self.types.get(idx as usize)
2020-06-04 12:46:12 +03:00
}
2021-05-10 12:51:22 +03:00
pub fn type_by_idx_r(&self, idx: u32) -> Result<&Type, MITInterfacesError> {
2020-06-04 19:54:23 +03:00
self.types
.get(idx as usize)
2021-05-10 12:51:22 +03:00
.ok_or(MITInterfacesError::NoSuchType(idx))
2020-06-04 19:54:23 +03:00
}
2020-06-07 12:33:26 +03:00
pub fn imports(&self) -> impl Iterator<Item = &Import<'_>> {
2020-06-04 19:54:23 +03:00
self.imports.iter()
2020-06-04 12:46:12 +03:00
}
2020-06-06 21:34:13 +03:00
pub fn imports_by_type(
2020-06-04 12:46:12 +03:00
&self,
import_type: CoreFunctionType,
2020-06-06 21:34:13 +03:00
) -> Option<&Vec<(ImportName<'a>, ImportNamespace<'a>)>> {
2020-06-07 12:33:26 +03:00
self.core_type_to_imports.get_vec(&import_type)
2020-06-04 12:46:12 +03:00
}
2020-06-06 21:34:13 +03:00
pub fn imports_by_type_r(
2020-06-04 12:46:12 +03:00
&self,
2020-06-04 19:54:23 +03:00
import_type: CoreFunctionType,
2021-05-10 12:51:22 +03:00
) -> Result<&Vec<(ImportName<'a>, ImportNamespace<'a>)>, MITInterfacesError> {
2020-06-07 12:33:26 +03:00
self.core_type_to_imports
2020-06-06 21:34:13 +03:00
.get_vec(&import_type)
2021-05-10 12:51:22 +03:00
.ok_or(MITInterfacesError::NoSuchImport(import_type))
2020-06-04 19:54:23 +03:00
}
pub fn adapters(&self) -> impl Iterator<Item = (&AdapterFunctionType, &Vec<Instruction>)> {
self.adapters.iter()
2020-06-04 12:46:12 +03:00
}
pub fn adapter_by_type(&self, adapter_type: AdapterFunctionType) -> Option<&Vec<Instruction>> {
self.adapters.get(&adapter_type)
}
2020-06-04 19:54:23 +03:00
pub fn adapter_by_type_r(
&self,
adapter_type: AdapterFunctionType,
2021-05-10 12:51:22 +03:00
) -> Result<&Vec<Instruction>, MITInterfacesError> {
2020-06-04 19:54:23 +03:00
self.adapters
.get(&adapter_type)
2021-05-10 12:51:22 +03:00
.ok_or(MITInterfacesError::NoSuchAdapter(adapter_type))
2020-06-04 19:54:23 +03:00
}
2020-06-07 12:33:26 +03:00
pub fn exports(&self) -> impl Iterator<Item = &Export<'_>> {
2020-06-04 19:06:23 +03:00
self.exports.iter()
2020-06-04 12:46:12 +03:00
}
2020-06-06 21:34:13 +03:00
pub fn exports_by_type(&self, export_type: u32) -> Option<&Vec<ExportName<'a>>> {
2020-06-07 12:33:26 +03:00
self.core_type_to_exports.get_vec(&export_type)
2020-06-06 21:34:13 +03:00
}
pub fn exports_by_type_r(
&self,
export_type: CoreFunctionType,
2021-05-10 12:51:22 +03:00
) -> Result<&Vec<ExportName<'a>>, MITInterfacesError> {
2020-06-07 12:33:26 +03:00
self.core_type_to_exports
2020-06-06 21:34:13 +03:00
.get_vec(&export_type)
2021-05-10 12:51:22 +03:00
.ok_or(MITInterfacesError::NoSuchImport(export_type))
2020-06-06 21:34:13 +03:00
}
2020-06-04 19:06:23 +03:00
pub fn implementations(
2020-06-04 12:46:12 +03:00
&self,
2020-06-04 19:06:23 +03:00
) -> impl Iterator<Item = (&AdapterFunctionType, &CoreFunctionType)> {
self.adapter_type_to_core.iter()
2020-06-04 12:46:12 +03:00
}
pub fn adapter_types_by_core_type(
&self,
core_function_type: CoreFunctionType,
) -> Option<&Vec<AdapterFunctionType>> {
2020-07-11 23:04:55 +03:00
self.core_type_to_adapter.get_vec(&core_function_type)
2020-06-04 12:46:12 +03:00
}
pub fn core_types_by_adapter_type(
&self,
adapter_function_type: AdapterFunctionType,
) -> Option<&Vec<CoreFunctionType>> {
2020-07-11 23:04:55 +03:00
self.adapter_type_to_core.get_vec(&adapter_function_type)
2020-06-04 12:46:12 +03:00
}
}