Skip to content

Commit

Permalink
Update Wasmi CLI arguments and default config (#849)
Browse files Browse the repository at this point in the history
* 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.

* Wasmi CLI: add --lazy to enable lazy compilation
  • Loading branch information
Robbepop authored Dec 17, 2023
1 parent 20bfc93 commit 89b413c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 9 additions & 0 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ pub struct Args {
#[clap(long = "invoke", value_name = "FUNCTION")]
invoke: Option<String>,

/// 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.
Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions crates/cli/src/context.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,11 +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<u64>) -> Result<Self, Error> {
pub fn new(
wasm_file: &Path,
wasi_ctx: WasiCtx,
fuel: Option<u64>,
lazy: bool,
) -> Result<Self, Error> {
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| {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down

0 comments on commit 89b413c

Please sign in to comment.