use holochain_wasmer_host::prelude::{
wasmparser, CompileError, CompilerConfig, CpuFeature, Cranelift, Dylib, Module, Store, Target,
Triple,
};
use std::str::FromStr;
use std::sync::Arc;
use tracing::info;
use wasmer_middlewares::*;
#[cfg(not(test))]
pub const WASM_METERING_LIMIT: u64 = 100_000_000_000;
#[cfg(test)]
pub const WASM_METERING_LIMIT: u64 = 10_000_000;
pub fn cranelift() -> Cranelift {
let cost_function = |_operator: &wasmparser::Operator| -> u64 { 1 };
let metering = Arc::new(Metering::new(WASM_METERING_LIMIT, cost_function));
let mut cranelift = Cranelift::default();
cranelift.canonicalize_nans(true).push_middleware(metering);
cranelift
}
pub fn wasmer_ios_target() -> Target {
let triple = Triple::from_str("aarch64-apple-ios").unwrap();
let cpu_feature = CpuFeature::set();
Target::new(triple, cpu_feature)
}
pub fn build_ios_module(wasm: &[u8]) -> Result<Module, CompileError> {
info!(
"Found wasm and was instructed to serialize it for ios in wasmer format, doing so now..."
);
let compiler_config = cranelift();
let ios_target = wasmer_ios_target();
let engine = Dylib::new(compiler_config).target(ios_target).engine();
let store = Store::new(&engine);
Module::from_binary(&store, wasm)
}
pub fn ios_dylib_headless_store() -> Store {
let ios_target = wasmer_ios_target();
let engine = Dylib::headless().target(ios_target).engine();
Store::new(&engine)
}