Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Consolidate store configuration on the CLI (#14165)

Alex Crichton88f8aa256361
diff --git a/src/commands/run.rs b/src/commands/run.rs
index 4159165be2..699241dffc 100644
--- a/src/commands/run.rs
+++ b/src/commands/run.rs
@@ -389,15 +389,7 @@ impl RunCommand {
let mut store = Store::new(&engine, host);
self.populate_with_wasi(&mut linker, &mut store)?;
-
- store.data_mut().limits = self.run.store_limits();
- store.limiter(|t| &mut t.limits);
-
- // If fuel has been configured, we want to add the configured
- // fuel amount to this store.
- if let Some(fuel) = self.run.common.wasm.fuel {
- store.set_fuel(fuel)?;
- }
+ self.run.configure_store(&mut store, |t| &mut t.limits)?;
Ok((store, linker))
}
@@ -1349,18 +1341,7 @@ impl RunCommand {
builder.inherit_stderr();
}
self.run.configure_wasip2(&mut builder)?;
- let mut ctx = builder.build_p1();
- if let Some(max) = self.run.common.wasi.max_resources {
- ctx.ctx().table.set_max_capacity(max);
- #[cfg(feature = "component-model-async")]
- if let Some(table) = store.concurrent_resource_table() {
- table.set_max_capacity(max);
- }
- }
- if let Some(fuel) = self.run.common.wasi.hostcall_fuel {
- store.set_hostcall_fuel(fuel);
- }
- store.data_mut().wasip1_ctx = Some(ctx);
+ store.data_mut().wasip1_ctx = Some(builder.build_p1());
Ok(())
}
diff --git a/src/commands/serve.rs b/src/commands/serve.rs
index 48c2a23fb8..57e4f3368a 100644
--- a/src/commands/serve.rs
+++ b/src/commands/serve.rs
@@ -445,20 +445,7 @@ impl ServeCommand {
}
let mut store = Store::new(engine, host);
-
- if let Some(fuel) = self.run.common.wasi.hostcall_fuel {
- store.set_hostcall_fuel(fuel);
- }
-
- store.data_mut().limits = self.run.store_limits();
- store.limiter(|t| &mut t.limits);
-
- // If fuel has been configured, we want to add the configured
- // fuel amount to this store.
- if let Some(fuel) = self.run.common.wasm.fuel {
- store.set_fuel(fuel)?;
- }
-
+ self.run.configure_store(&mut store, |t| &mut t.limits)?;
Ok(store)
}
diff --git a/src/common.rs b/src/common.rs
index 1500ea8a68..769854337b 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -4,7 +4,7 @@ use clap::Parser;
use std::net::TcpListener;
use std::{fs::File, path::Path, time::Duration};
use wasmtime::{
- Engine, Module, Precompiled, Result, StoreLimits, StoreLimitsBuilder, bail,
+ Engine, Module, Precompiled, Result, Store, StoreLimits, StoreLimitsBuilder, bail,
error::Context as _, format_err,
};
use wasmtime_cli_flags::{CommonOptions, opt::WasmtimeOptionValue};
@@ -480,6 +480,38 @@ impl RunCommon {
Ok(())
}
+
+ pub fn configure_store<T>(
+ &self,
+ store: &mut Store<T>,
+ limits: fn(&mut T) -> &mut StoreLimits,
+ ) -> Result<()>
+ where
+ T: wasmtime_wasi::WasiView,
+ {
+ if let Some(max) = self.common.wasi.max_resources {
+ if self.common.wasi.cli != Some(false) {
+ store.data_mut().ctx().table.set_max_capacity(max);
+ }
+ #[cfg(feature = "component-model-async")]
+ if let Some(table) = store.concurrent_resource_table() {
+ table.set_max_capacity(max);
+ }
+ }
+ if let Some(fuel) = self.common.wasi.hostcall_fuel {
+ store.set_hostcall_fuel(fuel);
+ }
+
+ *limits(store.data_mut()) = self.store_limits();
+ store.limiter(move |t| limits(t));
+
+ // If fuel has been configured, we want to add the configured
+ // fuel amount to this store.
+ if let Some(fuel) = self.common.wasm.fuel {
+ store.set_fuel(fuel)?;
+ }
+ Ok(())
+ }
}
#[derive(Clone, PartialEq)]