Files
marine/engine/src/module/wit_instance.rs

145 lines
4.7 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;
2020-07-17 23:07:02 +03:00
use crate::Result;
2020-06-02 17:20:00 +03:00
2020-06-04 19:06:23 +03:00
use fce_wit_interfaces::FCEWITInterfaces;
use fce_wit_interfaces::WITAstType;
2020-06-04 20:36:07 +03:00
use wasmer_wit::interpreter::wasm;
2020-06-03 23:08:18 +03:00
use wasmer_wit::interpreter::wasm::structures::{LocalImportIndex, TypedIndex};
2020-06-02 17:20:00 +03:00
use wasmer_core::Instance as WasmerInstance;
2020-05-30 01:55:39 +03:00
use std::collections::HashMap;
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,
2020-06-04 19:06:23 +03:00
wit: &FCEWITInterfaces<'_>,
2020-06-07 22:57:30 +03:00
modules: &HashMap<String, FCEModule>,
2020-07-17 23:07:02 +03:00
) -> Result<Self> {
2020-06-04 19:06:23 +03:00
let mut exports = Self::extract_raw_exports(&wasmer_instance, wit)?;
let imports = Self::extract_imports(modules, wit, 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,
2020-06-04 19:06:23 +03:00
wit: &FCEWITInterfaces<'_>,
2020-07-17 23:07:02 +03:00
) -> Result<HashMap<usize, WITFunction>> {
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;
2020-06-04 19:06:23 +03:00
wit.exports()
2020-05-30 01:55:39 +03:00
.enumerate()
2020-06-07 12:33:26 +03:00
.map(|(export_id, export)| {
let export_func = module_exports.get(export.name)?;
2020-05-30 01:55:39 +03:00
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-07 22:57:30 +03:00
modules: &HashMap<String, FCEModule>,
2020-06-04 19:06:23 +03:00
wit: &FCEWITInterfaces<'_>,
2020-06-01 02:45:04 +03:00
start_index: usize,
2020-07-17 23:07:02 +03:00
) -> Result<HashMap<usize, WITFunction>> {
2020-06-04 19:06:23 +03:00
wit.imports()
2020-07-11 23:04:55 +03:00
.filter(|import|
2020-06-04 19:06:23 +03:00
// filter out imports that have implementations
2020-07-11 23:04:55 +03:00
matches!(wit.adapter_types_by_core_type(import.function_type), Some(_)))
2020-06-04 19:06:23 +03:00
.enumerate()
2020-06-07 12:33:26 +03:00
.map(|(idx, import)| match modules.get(import.namespace) {
Some(module) => {
2020-06-10 14:56:15 +03:00
let func = WITFunction::from_import(module, import.name)?;
2020-06-07 12:33:26 +03:00
Ok((start_index + idx as usize, func))
2020-06-01 02:45:04 +03:00
}
2020-07-11 23:04:55 +03:00
None => Err(FCEError::NoSuchModule(import.namespace.to_string())),
2020-06-04 19:06:23 +03:00
})
2020-07-17 23:07:02 +03:00
.collect::<Result<HashMap<_, _>>>()
2020-06-01 02:45:04 +03:00
}
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-04 19:06:23 +03:00
fn wit_type(&self, _index: u32) -> Option<&WITAstType> {
2020-05-30 01:55:39 +03:00
None
}
}