diff --git a/crates/plugin_runtime/build.rs b/crates/plugin_runtime/build.rs index 3371b0ab391b22f6c03a62546e8eed78d7e8cc8b..0bae17d7cda4c37b1ca7dd952d920399af408dbf 100644 --- a/crates/plugin_runtime/build.rs +++ b/crates/plugin_runtime/build.rs @@ -60,7 +60,7 @@ fn main() { fn create_default_engine() -> Engine { let mut config = Config::default(); config.async_support(true); - config.epoch_interruption(true); + config.consume_fuel(true); Engine::new(&config).expect("Could not create engine") } diff --git a/crates/plugin_runtime/src/lib.rs b/crates/plugin_runtime/src/lib.rs index c4c145d801c9519dcf6773d7d8f3fd695b08f3b7..555d92587459e7ad76fedbaa76c09e59c8d8c612 100644 --- a/crates/plugin_runtime/src/lib.rs +++ b/crates/plugin_runtime/src/lib.rs @@ -23,7 +23,7 @@ mod tests { } async { - let (mut runtime, incrementer) = PluginBuilder::new_with_default_ctx() + let mut runtime = PluginBuilder::new_with_default_ctx() .unwrap() .host_function("mystery_number", |input: u32| input + 7) .unwrap() @@ -53,8 +53,6 @@ mod tests { .await .unwrap(); - std::thread::spawn(move || incrementer.block_on()); - let plugin = TestPlugin { noop: runtime.function("noop").unwrap(), constant: runtime.function("constant").unwrap(), diff --git a/crates/plugin_runtime/src/plugin.rs b/crates/plugin_runtime/src/plugin.rs index dbc6a539df41292072b1bb4d5e6f779cd9ae5107..1db81375bf401e0e30addfa304d202aa878a0a37 100644 --- a/crates/plugin_runtime/src/plugin.rs +++ b/crates/plugin_runtime/src/plugin.rs @@ -62,8 +62,7 @@ pub struct PluginBuilder { wasi_ctx: WasiCtx, engine: Engine, linker: Linker, - delta: u64, - epoch: std::time::Duration, + fuel_refill: u64, } /// Creates a default engine for compiling Wasm. @@ -73,7 +72,7 @@ pub struct PluginBuilder { pub fn create_default_engine() -> Result { let mut config = Config::default(); config.async_support(true); - config.epoch_interruption(true); + config.consume_fuel(true); Engine::new(&config) } @@ -88,8 +87,7 @@ impl PluginBuilder { wasi_ctx, engine, linker, - delta: 1, - epoch: std::time::Duration::from_millis(100), + fuel_refill: 1000, }) } @@ -245,17 +243,7 @@ impl PluginBuilder { /// Initializes a [`Plugin`] from a given compiled Wasm module. /// Both binary (`.wasm`) and text (`.wat`) module formats are supported. - pub async fn init>( - self, - precompiled: bool, - module: T, - ) -> Result< - ( - Plugin, - std::pin::Pin + Send + 'static>>, - ), - Error, - > { + pub async fn init>(self, precompiled: bool, module: T) -> Result { Plugin::init(precompiled, module.as_ref().to_vec(), self).await } } @@ -320,13 +308,7 @@ impl Plugin { precompiled: bool, module: Vec, plugin: PluginBuilder, - ) -> Result< - ( - Self, - std::pin::Pin + Send + 'static>>, - ), - Error, - > { + ) -> Result { // initialize the WebAssembly System Interface context let engine = plugin.engine; let mut linker = plugin.linker; @@ -348,15 +330,9 @@ impl Plugin { Module::new(&engine, module)? }; - // set up automatic yielding after given duration - store.epoch_deadline_async_yield_and_update(plugin.delta); - let epoch = plugin.epoch; - let incrementer = Box::pin(async move { - loop { - smol::Timer::after(epoch).await; - engine.increment_epoch(); - } - }); + // set up automatic yielding after fuel expires + store.out_of_fuel_async_yield(u64::MAX, plugin.fuel_refill); + store.add_fuel(plugin.fuel_refill).unwrap(); // load the provided module into the asynchronous runtime linker.module_async(&mut store, "", &module).await?; @@ -371,9 +347,7 @@ impl Plugin { free_buffer, }); - let plugin = Plugin { store, instance }; - - Ok((plugin, incrementer)) + Ok(Plugin { store, instance }) } /// Attaches a file or directory the the given system path to the runtime. diff --git a/crates/zed/src/languages/language_plugin.rs b/crates/zed/src/languages/language_plugin.rs index cf812fd2ad0dc164b15062f5d61e0479485e0f22..f5bfa661b3b9e3c12bc3d326d41bc3215cc6ec7b 100644 --- a/crates/zed/src/languages/language_plugin.rs +++ b/crates/zed/src/languages/language_plugin.rs @@ -9,7 +9,7 @@ use std::{any::Any, path::PathBuf, sync::Arc}; use util::ResultExt; pub async fn new_json(executor: Arc) -> Result { - let (plugin, incrementer) = PluginBuilder::new_with_default_ctx()? + let plugin = PluginBuilder::new_with_default_ctx()? .host_function_async("command", |command: String| async move { let mut args = command.split(' '); let command = args.next().unwrap(); @@ -26,7 +26,6 @@ pub async fn new_json(executor: Arc) -> Result { ) .await?; - executor.spawn(incrementer).detach(); PluginLspAdapter::new(plugin, executor).await }