swc_plugin_runner/
wasix_runtime.rs#![allow(unused)]
use std::{path::PathBuf, sync::Arc};
use parking_lot::Mutex;
use swc_common::sync::Lazy;
use wasmer::Store;
use wasmer_wasix::Runtime;
static ENGINE: Lazy<Mutex<wasmer::Engine>> = Lazy::new(|| {
use enumset::EnumSet;
use wasmer::{
sys::{BaseTunables, EngineBuilder},
CompilerConfig, Target, Triple,
};
let mut set = EnumSet::new();
#[cfg(target_arch = "x86_64")]
set.insert(wasmer::CpuFeature::SSE2);
let target = Target::new(Triple::host(), set);
let config = wasmer_compiler_cranelift::Cranelift::default();
let mut engine = EngineBuilder::new(Box::new(config) as Box<dyn CompilerConfig>)
.set_target(Some(target))
.engine();
let tunables = BaseTunables::for_target(engine.target());
engine.set_tunables(tunables);
parking_lot::Mutex::new(wasmer::Engine::from(engine))
});
#[derive(Debug)]
struct StubHttpClient;
impl wasmer_wasix::http::HttpClient for StubHttpClient {
fn request(
&self,
_request: wasmer_wasix::http::HttpRequest,
) -> futures::future::BoxFuture<'_, Result<wasmer_wasix::http::HttpResponse, anyhow::Error>>
{
unimplemented!()
}
}
pub fn build_wasi_runtime(
_fs_cache_path: Option<PathBuf>,
) -> Option<Arc<dyn Runtime + Send + Sync>> {
use wasmer_wasix::{
runtime::{
module_cache::{ModuleCache, SharedCache},
package_loader::UnsupportedPackageLoader,
resolver::MultiSource,
task_manager::tokio::TokioTaskManager,
},
virtual_net, PluggableRuntime,
};
let cache =
SharedCache::default().with_fallback(wasmer_wasix::runtime::module_cache::in_memory());
let dummy_loader = UnsupportedPackageLoader;
let rt = PluggableRuntime {
rt: Arc::new(TokioTaskManager::default()),
networking: Arc::new(virtual_net::UnsupportedVirtualNetworking::default()),
engine: Some(ENGINE.lock().clone()),
tty: None,
source: Arc::new(MultiSource::new()),
module_cache: Arc::new(cache),
http_client: None,
package_loader: Arc::new(dummy_loader),
};
Some(Arc::new(rt))
}
#[cfg(not(target_arch = "wasm32"))]
#[allow(unused_mut)]
pub(crate) fn new_store() -> Store {
let engine = ENGINE.lock().clone();
Store::new(engine)
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn new_store() -> Store {
Store::default()
}