From b80a4fc4c467e5c2b54f8915b9468344782b468d Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sun, 17 Dec 2023 14:07:50 +0100 Subject: [PATCH 1/2] Wasmi CLI: enabled Wasm tail-calls and extended-const Those Wasm proposals are not stabilized, yet but we can assume them to be stabilized very soon so enabling them by default is probably a good idea. --- crates/cli/src/context.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/cli/src/context.rs b/crates/cli/src/context.rs index ca23c1611c..4fe6e64bdd 100644 --- a/crates/cli/src/context.rs +++ b/crates/cli/src/context.rs @@ -25,6 +25,8 @@ impl Context { /// - If adding WASI defintions to the linker failed. pub fn new(wasm_file: &Path, wasi_ctx: WasiCtx, fuel: Option) -> Result { let mut config = Config::default(); + config.wasm_tail_call(true); + config.wasm_extended_const(true); if fuel.is_some() { config.consume_fuel(true); } From d22457379a40964667bf6a75e12a7526987c2afb Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sun, 17 Dec 2023 14:08:31 +0100 Subject: [PATCH 2/2] Wasmi CLI: add --lazy to enable lazy compilation --- crates/cli/src/args.rs | 9 +++++++++ crates/cli/src/context.rs | 14 ++++++++++++-- crates/cli/src/main.rs | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/cli/src/args.rs b/crates/cli/src/args.rs index fa7c86e3fe..d334f06e07 100644 --- a/crates/cli/src/args.rs +++ b/crates/cli/src/args.rs @@ -82,6 +82,10 @@ pub struct Args { #[clap(long = "invoke", value_name = "FUNCTION")] invoke: Option, + /// Enable lazy Wasm compilation. + #[clap(long = "lazy")] + lazy: bool, + /// Enable execution fiel metering with N units of fuel. /// /// The execution will trap after running out of the N units of fuel. @@ -114,6 +118,11 @@ impl Args { self.fuel } + /// Returns `true` if lazy Wasm compilation is enabled. + pub fn lazy(&self) -> bool { + self.lazy + } + /// Pre-opens all directories given in `--dir` and returns them for use by the [`WasiCtx`]. /// /// # Errors diff --git a/crates/cli/src/context.rs b/crates/cli/src/context.rs index 4fe6e64bdd..f4022c6842 100644 --- a/crates/cli/src/context.rs +++ b/crates/cli/src/context.rs @@ -1,7 +1,7 @@ use crate::utils; use anyhow::{anyhow, Error}; use std::path::Path; -use wasmi::{Config, ExternType, Func, FuncType, Instance, Module, Store}; +use wasmi::{CompilationMode, Config, ExternType, Func, FuncType, Instance, Module, Store}; use wasmi_wasi::WasiCtx; /// The [`Context`] for the `wasmi` CLI application. @@ -23,13 +23,23 @@ impl Context { /// /// - If parsing, validating, compiling or instantiating the Wasm module failed. /// - If adding WASI defintions to the linker failed. - pub fn new(wasm_file: &Path, wasi_ctx: WasiCtx, fuel: Option) -> Result { + pub fn new( + wasm_file: &Path, + wasi_ctx: WasiCtx, + fuel: Option, + lazy: bool, + ) -> Result { let mut config = Config::default(); config.wasm_tail_call(true); config.wasm_extended_const(true); if fuel.is_some() { config.consume_fuel(true); } + let mode = match lazy { + true => CompilationMode::Lazy, + false => CompilationMode::Eager, + }; + config.compilation_mode(mode); let engine = wasmi::Engine::new(&config); let wasm_bytes = utils::read_wasm_or_wat(wasm_file)?; let module = wasmi::Module::new(&engine, &mut &wasm_bytes[..]).map_err(|error| { diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index c994a80cbc..084348a734 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -20,7 +20,7 @@ fn main() -> Result<()> { let args = Args::parse(); let wasm_file = args.wasm_file(); let wasi_ctx = args.wasi_context()?; - let mut ctx = Context::new(wasm_file, wasi_ctx, args.fuel())?; + let mut ctx = Context::new(wasm_file, wasi_ctx, args.fuel(), args.lazy())?; let (func_name, func) = get_invoked_func(&args, &ctx)?; let ty = func.ty(ctx.store()); let func_args = utils::decode_func_args(&ty, args.func_args())?;