From c663501a1f262d2bc2821217fa7cf6192d39acfe 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 | 10 ++++++---- tests/lib/wast/src/wasi_wast.rs | 3 ++- 6 files changed, 24 insertions(+), 28 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..6fd1ecf7582 100644 --- a/lib/wasi/tests/stdio.rs +++ b/lib/wasi/tests/stdio.rs @@ -2,7 +2,9 @@ use std::sync::Arc; use virtual_fs::{AsyncReadExt, AsyncWriteExt}; use wasmer::{Module, Store}; -use wasmer_wasix::{Pipe, PluggableRuntime, WasiEnv}; +use wasmer_wasix::{ + runtime::task_manager::tokio::TokioTaskManager, Pipe, PluggableRuntime, WasiEnv, +}; mod sys { #[tokio::test] @@ -76,7 +78,7 @@ async fn test_stdout() { // Create the `WasiEnv`. let (stdout_tx, mut stdout_rx) = Pipe::channel(); - let rt = PluggableRuntime::default(); + let rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared())); let builder = WasiEnv::builder("command-name") .runtime(Arc::new(rt)) @@ -112,7 +114,7 @@ async fn test_env() { builder.build() }); - let rt = PluggableRuntime::default(); + let rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared())); // Create the `WasiEnv`. let (pipe_tx, mut pipe_rx) = Pipe::channel(); @@ -157,7 +159,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 rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared())); let builder = WasiEnv::builder("command-name") .runtime(Arc::new(rt)) 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();