use crate::{
wasmtime::{context::WrappedContext, types::map_val_type, wasmtime_syscall_handler},
ImportLinker,
};
use std::sync::Arc;
pub fn wasmtime_import_linker<T: 'static>(
engine: &wasmtime::Engine,
import_linker: &Arc<ImportLinker>,
) -> wasmtime::Linker<WrappedContext<T>> {
let mut linker = wasmtime::Linker::<WrappedContext<T>>::new(engine);
for (import_name, import_entity) in import_linker.iter() {
let params = import_entity
.params
.iter()
.copied()
.map(map_val_type)
.collect::<Vec<_>>();
let result = import_entity
.result
.iter()
.copied()
.map(map_val_type)
.collect::<Vec<_>>();
let func_type = wasmtime::FuncType::new(engine, params, result);
linker
.func_new(
import_name.module(),
import_name.name(),
func_type,
move |caller, params, result| {
wasmtime_syscall_handler(import_entity.sys_func_idx, caller, params, result)
},
)
.unwrap_or_else(|_| panic!("function import collision: {}", import_name));
}
linker
}