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-09-16 01:14:15 +03:00
|
|
|
use super::IRecordType;
|
2021-02-24 12:25:55 +03:00
|
|
|
use crate::FCEResult;
|
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-11-05 15:18:48 +03:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
pub type RecordTypes = HashMap<u64, Rc<IRecordType>>;
|
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-09-16 01:14:15 +03:00
|
|
|
/// WIT functions indexed by id.
|
2020-06-01 02:45:04 +03:00
|
|
|
funcs: HashMap<usize, WITFunction>,
|
2020-09-16 01:14:15 +03:00
|
|
|
|
|
|
|
/// WIT memories.
|
2020-05-30 01:55:39 +03:00
|
|
|
memories: Vec<WITMemory>,
|
2020-09-16 01:14:15 +03:00
|
|
|
|
2020-11-05 15:18:48 +03:00
|
|
|
/// All record types that instance contains.
|
|
|
|
record_types_by_id: RecordTypes,
|
2020-05-30 01:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-04-12 00:21:47 +03:00
|
|
|
module_name: &str,
|
2020-06-04 19:06:23 +03:00
|
|
|
wit: &FCEWITInterfaces<'_>,
|
2020-06-07 22:57:30 +03:00
|
|
|
modules: &HashMap<String, FCEModule>,
|
2021-02-24 12:25:55 +03:00
|
|
|
) -> FCEResult<Self> {
|
2020-06-04 19:06:23 +03:00
|
|
|
let mut exports = Self::extract_raw_exports(&wasmer_instance, wit)?;
|
2021-04-12 00:21:47 +03:00
|
|
|
let imports = Self::extract_imports(module_name, 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-09-16 01:14:15 +03:00
|
|
|
let record_types_by_id = Self::extract_record_types(wit);
|
2020-07-28 01:19:15 +03:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
funcs,
|
|
|
|
memories,
|
2020-09-16 01:14:15 +03:00
|
|
|
record_types_by_id,
|
2020-07-28 01:19:15 +03:00
|
|
|
})
|
2020-05-30 01:55:39 +03:00
|
|
|
}
|
|
|
|
|
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<'_>,
|
2021-02-24 12:25:55 +03:00
|
|
|
) -> FCEResult<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-11-07 00:08:09 +03:00
|
|
|
Ok((
|
|
|
|
export_id,
|
|
|
|
WITFunction::from_export(export_func, export.name.to_string())?,
|
|
|
|
))
|
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(
|
2021-04-12 00:21:47 +03:00
|
|
|
module_name: &str,
|
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,
|
2021-02-24 12:25:55 +03:00
|
|
|
) -> FCEResult<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-10-21 22:21:16 +03:00
|
|
|
use wasmer_wit::ast::Type;
|
|
|
|
let (arguments, output_types) =
|
|
|
|
match wit.type_by_idx_r(import.function_type - 2)? {
|
|
|
|
Type::Function {
|
|
|
|
arguments,
|
|
|
|
output_types,
|
|
|
|
} => (arguments.clone(), output_types.clone()),
|
|
|
|
ty => {
|
|
|
|
return Err(FCEError::IncorrectWIT(format!(
|
|
|
|
"WIT should has Type::Function, but {:?} met",
|
|
|
|
ty
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-12 00:21:47 +03:00
|
|
|
let func = WITFunction::from_import(
|
|
|
|
module,
|
|
|
|
module_name,
|
|
|
|
import.name,
|
|
|
|
arguments,
|
|
|
|
output_types,
|
|
|
|
)?;
|
2020-10-21 22:21:16 +03:00
|
|
|
|
2020-06-07 12:33:26 +03:00
|
|
|
Ok((start_index + idx as usize, func))
|
2020-06-01 02:45:04 +03:00
|
|
|
}
|
2020-10-01 12:19:38 +03:00
|
|
|
None => Err(FCEError::NoSuchModule(format!(
|
|
|
|
"trying to get imports from module with name {} that is not loaded",
|
|
|
|
import.namespace
|
|
|
|
))),
|
2020-06-04 19:06:23 +03:00
|
|
|
})
|
2021-02-24 12:25:55 +03:00
|
|
|
.collect::<FCEResult<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-07-28 01:19:15 +03:00
|
|
|
|
2020-11-05 15:18:48 +03:00
|
|
|
fn extract_record_types(wit: &FCEWITInterfaces<'_>) -> RecordTypes {
|
|
|
|
let (record_types_by_id, _) = wit.types().fold(
|
2020-09-16 01:14:15 +03:00
|
|
|
(HashMap::new(), 0u64),
|
|
|
|
|(mut record_types_by_id, id), ty| {
|
|
|
|
match ty {
|
|
|
|
WITAstType::Record(record_type) => {
|
|
|
|
record_types_by_id.insert(id, record_type.clone());
|
|
|
|
}
|
|
|
|
WITAstType::Function { .. } => {}
|
|
|
|
};
|
|
|
|
(record_types_by_id, id + 1)
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-11-05 15:18:48 +03:00
|
|
|
record_types_by_id
|
2020-07-28 01:19:15 +03:00
|
|
|
}
|
2020-05-30 01:55:39 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-16 01:14:15 +03:00
|
|
|
fn local_or_import<I: TypedIndex + LocalImportIndex>(&self, index: I) -> 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-11-05 15:18:48 +03:00
|
|
|
fn wit_record_by_id(&self, index: u64) -> Option<&Rc<IRecordType>> {
|
2020-09-16 01:14:15 +03:00
|
|
|
self.record_types_by_id.get(&index)
|
2020-05-30 01:55:39 +03:00
|
|
|
}
|
|
|
|
}
|