Files
marine/wit_fce/src/vm/instance/wit_instance.rs

161 lines
5.1 KiB
Rust
Raw Normal View History

2020-05-30 01:55:39 +03:00
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-06-02 17:20:00 +03:00
use super::wit_prelude::*;
use super::fce_module::FCEModule;
use wasmer_wit::interpreter::wasm;
use super::IAstType;
use wasmer_wit::ast::Interfaces;
use wasmer_wit::interpreter::wasm::structures::{
LocalImportIndex, TypedIndex,
};
use wasmer_core::Instance as WasmerInstance;
2020-05-30 01:55:39 +03:00
use std::collections::HashMap;
2020-06-01 02:45:04 +03:00
use std::sync::Arc;
2020-05-30 01:55:39 +03:00
2020-06-02 17:20:00 +03:00
/// Contains all import and export functions that could be called from WIT context by call-core.
2020-06-01 02:45:04 +03:00
#[derive(Clone)]
2020-06-02 17:20:00 +03:00
pub(super) struct WITInstance {
2020-06-01 02:45:04 +03:00
funcs: HashMap<usize, WITFunction>,
2020-05-30 01:55:39 +03:00
memories: Vec<WITMemory>,
}
impl WITInstance {
2020-06-02 17:20:00 +03:00
pub(super) fn new(
2020-05-30 01:55:39 +03:00
wasmer_instance: &WasmerInstance,
interfaces: &Interfaces,
2020-06-02 17:20:00 +03:00
modules: &HashMap<String, Arc<FCEModule>>,
2020-05-30 01:55:39 +03:00
) -> Result<Self, WITFCEError> {
2020-06-02 17:20:00 +03:00
let mut exports = Self::extract_raw_exports(&wasmer_instance, interfaces)?;
2020-06-01 02:45:04 +03:00
let imports = Self::extract_imports(modules, interfaces, exports.len())?;
2020-05-30 01:55:39 +03:00
let memories = Self::extract_memories(&wasmer_instance);
2020-06-01 02:45:04 +03:00
exports.extend(imports);
let funcs = exports;
2020-05-30 01:55:39 +03:00
Ok(Self { funcs, memories })
}
2020-06-02 17:20:00 +03:00
fn extract_raw_exports(
2020-05-30 01:55:39 +03:00
wasmer_instance: &WasmerInstance,
interfaces: &Interfaces,
2020-06-01 02:45:04 +03:00
) -> Result<HashMap<usize, WITFunction>, WITFCEError> {
2020-06-02 17:20:00 +03:00
use wasmer_core::DynFunc;
2020-05-30 01:55:39 +03:00
let module_exports = &wasmer_instance.exports;
interfaces
.exports
.iter()
.enumerate()
.map(|(export_id, export)| {
let export_func = module_exports.get(export.name)?;
unsafe {
// TODO: refactor this with new Wasmer API when it is ready
// here it is safe because dyn func is never lives WITInstance
let export_func =
std::mem::transmute::<DynFunc<'_>, DynFunc<'static>>(export_func);
2020-06-02 00:12:23 +03:00
Ok((export_id, WITFunction::from_export(export_func)?))
2020-05-30 01:55:39 +03:00
}
})
.collect()
}
2020-06-01 02:45:04 +03:00
/// Extracts only those imports that don't have implementations.
fn extract_imports(
2020-06-02 17:20:00 +03:00
modules: &HashMap<String, Arc<FCEModule>>,
2020-06-01 02:45:04 +03:00
interfaces: &Interfaces,
start_index: usize,
) -> Result<HashMap<usize, WITFunction>, WITFCEError> {
// uses to filter import functions that have an adapter implementation
let core_to_adapter = interfaces
.implementations
.iter()
.map(|i| (i.core_function_type, i.adapter_function_type))
2020-06-02 17:20:00 +03:00
.collect::<HashMap<u32, u32>>();
2020-06-01 02:45:04 +03:00
let mut non_wit_callable_imports = HashMap::new();
for import in interfaces.imports.iter() {
2020-06-02 00:12:23 +03:00
if core_to_adapter.get(&import.function_type).is_some() {
2020-06-01 02:45:04 +03:00
continue;
}
match modules.get(import.namespace) {
Some(module) => {
let func = WITFunction::from_import(module.clone(), import.name.to_string())?;
non_wit_callable_imports
.insert(start_index + non_wit_callable_imports.len() as usize, func);
}
None => return Err(WITFCEError::NoSuchModule),
}
}
Ok(non_wit_callable_imports)
}
2020-05-30 01:55:39 +03:00
fn extract_memories(wasmer_instance: &WasmerInstance) -> Vec<WITMemory> {
2020-06-02 17:20:00 +03:00
use wasmer_core::export::Export::Memory;
2020-05-30 01:55:39 +03:00
let mut memories = wasmer_instance
.exports()
.filter_map(|(_, export)| match export {
Memory(memory) => Some(WITMemory(memory)),
_ => None,
})
.collect::<Vec<_>>();
if let Some(Memory(memory)) = wasmer_instance
.import_object
.maybe_with_namespace("env", |env| env.get_export("memory"))
{
memories.push(WITMemory(memory));
}
memories
}
}
2020-06-02 17:20:00 +03:00
impl wasm::structures::Instance<WITExport, WITFunction, WITMemory, WITMemoryView<'_>>
2020-05-30 01:55:39 +03:00
for WITInstance
{
fn export(&self, _export_name: &str) -> Option<&WITExport> {
2020-06-02 17:20:00 +03:00
// exports aren't used in this version of WIT
2020-05-30 01:55:39 +03:00
None
}
fn local_or_import<I: TypedIndex + LocalImportIndex>(
&mut self,
index: I,
2020-06-01 02:45:04 +03:00
) -> Option<&WITFunction> {
2020-05-30 01:55:39 +03:00
self.funcs.get(&index.index())
}
fn memory(&self, index: usize) -> Option<&WITMemory> {
if index >= self.memories.len() {
None
} else {
Some(&self.memories[index])
}
}
2020-06-02 17:20:00 +03:00
fn wit_type(&self, _index: u32) -> Option<&IAstType> {
2020-05-30 01:55:39 +03:00
None
}
}