From 4a99b50af84bb92a7a56b5c7a35ce5acf250e974 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Mon, 20 Mar 2023 21:27:11 +0100 Subject: [PATCH] Remove PluggableRuntime::default() We don't want this impl anymore, because the user should be forced to supply the virtual task manager manually so they are cognizent of the fact they WASIX is running on an async runtime. --- lib/cli/src/commands/run/wasi.rs | 3 ++- lib/wasi/src/bin_factory/module_cache.rs | 6 +++--- lib/wasi/src/runtime/mod.rs | 16 ---------------- lib/wasi/src/state/builder.rs | 14 +++++++++++--- lib/wasi/tests/stdio.rs | 16 ++-------------- tests/lib/wast/src/wasi_wast.rs | 3 ++- 6 files changed, 20 insertions(+), 38 deletions(-) diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index 62580bfd0a2..6af4b3c39dc 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -9,6 +9,7 @@ use virtual_fs::{DeviceFile, PassthruFileSystem, RootFileSystemBuilder}; use wasmer::{AsStoreMut, Instance, Module, RuntimeError, Value}; use wasmer_wasix::os::tty_sys::SysTty; use wasmer_wasix::os::TtyBridge; +use wasmer_wasix::runtime::task_manager::tokio::TokioTaskManager; use wasmer_wasix::types::__WASI_STDIN_FILENO; use wasmer_wasix::{ default_fs_backing, get_wasi_versions, PluggableRuntime, WasiEnv, WasiError, WasiFunctionEnv, @@ -129,7 +130,7 @@ impl Wasi { .map(|(a, b)| (a.to_string(), b.to_string())) .collect::>(); - let mut rt = PluggableRuntime::default(); + let mut rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared())); if self.networking { rt.set_networking_implementation(virtual_net::host::LocalNetworking::default()); diff --git a/lib/wasi/src/bin_factory/module_cache.rs b/lib/wasi/src/bin_factory/module_cache.rs index bdeb8f95231..0330bd0beef 100644 --- a/lib/wasi/src/bin_factory/module_cache.rs +++ b/lib/wasi/src/bin_factory/module_cache.rs @@ -283,13 +283,13 @@ impl ModuleCache { #[cfg(test)] #[cfg(feature = "sys")] mod tests { - use std::time::Duration; + use std::{sync::Arc, time::Duration}; use tracing_subscriber::{ filter, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer, }; - use crate::PluggableRuntime; + use crate::{runtime::task_manager::tokio::TokioTaskManager, PluggableRuntime}; use super::*; @@ -306,7 +306,7 @@ mod tests { let mut cache = ModuleCache::new(None, None, true); cache.cache_time = std::time::Duration::from_millis(500); - let rt = PluggableRuntime::default(); + let rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared())); let tasks = rt.task_manager(); let mut store = Vec::new(); diff --git a/lib/wasi/src/runtime/mod.rs b/lib/wasi/src/runtime/mod.rs index bcf8236ff8b..c139ae21e0c 100644 --- a/lib/wasi/src/runtime/mod.rs +++ b/lib/wasi/src/runtime/mod.rs @@ -140,22 +140,6 @@ impl PluggableRuntime { } } -impl Default for PluggableRuntime { - #[cfg(feature = "sys-thread")] - fn default() -> Self { - let rt = task_manager::tokio::TokioTaskManager::shared(); - let mut s = Self::new(Arc::new(rt)); - let engine = wasmer::Store::default().engine().clone(); - s.engine = Some(engine); - s - } - - #[cfg(not(feature = "sys-thread"))] - fn default() -> Self { - unimplemented!("Default WasiRuntime is not implemented on this target") - } -} - impl WasiRuntime for PluggableRuntime { fn networking(&self) -> &DynVirtualNetworking { &self.networking diff --git a/lib/wasi/src/state/builder.rs b/lib/wasi/src/state/builder.rs index 3ea3af812b5..68bc42ab058 100644 --- a/lib/wasi/src/state/builder.rs +++ b/lib/wasi/src/state/builder.rs @@ -722,9 +722,17 @@ impl WasiEnvBuilder { } } - let runtime = self - .runtime - .unwrap_or_else(|| Arc::new(PluggableRuntime::default())); + let runtime = self.runtime.unwrap_or_else(|| { + #[cfg(feature = "sys-thread")] + { + Arc::new(PluggableRuntime::new(Arc::new(crate::runtime::task_manager::tokio::TokioTaskManager::shared()))) + } + + #[cfg(not(feature = "sys-thread"))] + { + panic!("this build does not support a default runtime - specify one with WasiEnvBuilder::runtime()"); + } + }); let uses = self.uses; let map_commands = self.map_commands; diff --git a/lib/wasi/tests/stdio.rs b/lib/wasi/tests/stdio.rs index 1d5324a2b17..7c719a0f6b4 100644 --- a/lib/wasi/tests/stdio.rs +++ b/lib/wasi/tests/stdio.rs @@ -1,8 +1,6 @@ -use std::sync::Arc; - use virtual_fs::{AsyncReadExt, AsyncWriteExt}; use wasmer::{Module, Store}; -use wasmer_wasix::{Pipe, PluggableRuntime, WasiEnv}; +use wasmer_wasix::{Pipe, WasiEnv}; mod sys { #[tokio::test] @@ -76,10 +74,7 @@ async fn test_stdout() { // Create the `WasiEnv`. let (stdout_tx, mut stdout_rx) = Pipe::channel(); - let rt = PluggableRuntime::default(); - let builder = WasiEnv::builder("command-name") - .runtime(Arc::new(rt)) .args(&["Gordon"]) .stdout(Box::new(stdout_tx)); @@ -112,13 +107,10 @@ async fn test_env() { builder.build() }); - let rt = PluggableRuntime::default(); - // Create the `WasiEnv`. let (pipe_tx, mut pipe_rx) = Pipe::channel(); let builder = WasiEnv::builder("command-name") - .runtime(Arc::new(rt)) .args(&["Gordon"]) .env("DOG", "X") .env("TEST", "VALUE") @@ -157,11 +149,7 @@ async fn test_stdin() { let buf = "Hello, stdin!\n".as_bytes().to_owned(); pipe_tx.write_all(&buf[..]).await.unwrap(); - let rt = PluggableRuntime::default(); - - let builder = WasiEnv::builder("command-name") - .runtime(Arc::new(rt)) - .stdin(Box::new(pipe_rx)); + let builder = WasiEnv::builder("command-name").stdin(Box::new(pipe_rx)); #[cfg(feature = "js")] { diff --git a/tests/lib/wast/src/wasi_wast.rs b/tests/lib/wast/src/wasi_wast.rs index dd96c0528bc..302da111a95 100644 --- a/tests/lib/wast/src/wasi_wast.rs +++ b/tests/lib/wast/src/wasi_wast.rs @@ -10,6 +10,7 @@ use virtual_fs::{ AsyncWriteExt, FileSystem, Pipe, ReadBuf, RootFileSystemBuilder, }; use wasmer::{FunctionEnv, Imports, Module, Store}; +use wasmer_wasix::runtime::task_manager::tokio::TokioTaskManager; use wasmer_wasix::types::wasi::{Filesize, Timestamp}; use wasmer_wasix::{ generate_import_object_from_env, get_wasi_version, FsError, PluggableRuntime, VirtualFile, @@ -100,7 +101,7 @@ impl<'a> WasiTest<'a> { out }; - let mut rt = PluggableRuntime::default(); + let mut rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared())); rt.set_engine(Some(store.engine().clone())); let tasks = rt.task_manager().runtime().clone();