Skip to content

Commit

Permalink
Add wasmi_wast crate (#1279)
Browse files Browse the repository at this point in the history
* add wasmi_wast crate

* fix bugs with feature solution

* re-order dev-dependencies

* rename argument -> result

* remove unnecessary wat dependency from wasmi_wast

* fix intra doc links in wasmi_wast
  • Loading branch information
Robbepop authored Nov 2, 2024
1 parent c36a741 commit 585a129
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
23 changes: 16 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"crates/wasi",
"crates/ir",
"crates/fuzz",
"crates/wast",
"fuzz",
]
exclude = []
Expand All @@ -35,6 +36,7 @@ wasmi_collections = { version = "0.38.0", path = "crates/collections", default-f
wasmi_c_api_impl = { version = "0.38.0", path = "crates/c_api" }
wasmi_c_api_macros = { version = "0.38.0", path = "crates/c_api/macro" }
wasmi_fuzz = { version = "0.38.0", path = "crates/fuzz" }
wasmi_wast = { version = "0.38.0", path = "crates/wast" }

[profile.bench]
lto = "fat"
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ arrayvec = { version = "0.7.4", default-features = false }
[dev-dependencies]
wat = "1"
assert_matches = "1.5"
wast = { version = "219.0.1", default-features = false, features = ["wasm-module"] }
anyhow = "1.0"
anyhow = "1"
wasmi_wast = { workspace = true }
criterion = { version = "0.5", default-features = false }

[features]
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! [Wasmtime's API example](https://docs.rs/wasmtime/0.39.1/wasmtime/).
//!
//! ```
//! use anyhow::{anyhow, Result};
//! use anyhow::Result;
//! use wasmi::*;
//!
//! fn main() -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions crates/wasmi/tests/spec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod runner;

use self::runner::{ParsingMode, RunnerConfig, WastRunner};
use wasmi::Config;
use wasmi_wast::{ParsingMode, RunnerConfig, WastRunner};

/// Runs the Wasm test spec identified by the given name.
fn process_wast(path: &'static str, wast: &'static str, config: RunnerConfig) {
Expand Down
20 changes: 20 additions & 0 deletions crates/wast/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "wasmi_wast"
version.workspace = true
rust-version.workspace = true
documentation = "https://docs.rs/wasmi_wast"
description = "Utilities to execute Wast files and directives with Wasmi"
authors.workspace = true
repository.workspace = true
edition.workspace = true
readme.workspace = true
license.workspace = true
keywords.workspace = true
categories.workspace = true
exclude.workspace = true
publish = false

[dependencies]
wasmi = { workspace = true, features = ["std"] }
wast = { version = "219.0.1", default-features = false, features = ["wasm-module"] }
anyhow = "1.0"
17 changes: 10 additions & 7 deletions crates/wasmi/tests/spec/runner.rs → crates/wast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{bail, Context as _, Result};
use std::collections::HashMap;
use wasmi::{
core::{ValType, F32, F64},
Config,
Engine,
Extern,
Expand All @@ -17,7 +18,6 @@ use wasmi::{
TableType,
Val,
};
use wasmi_core::{ValType, F32, F64};
use wast::{
core::{AbstractHeapType, HeapType, NanPattern, WastArgCore, WastRetCore},
lexer::Lexer,
Expand Down Expand Up @@ -65,7 +65,7 @@ pub struct WastRunner {
}

impl WastRunner {
/// Creates a new [`TestContext`] with the given [`WastSource`].
/// Creates a new [`WastRunner`] with the given [`RunnerConfig`].
pub fn new(config: RunnerConfig) -> Self {
let engine = Engine::new(&config.config);
let linker = Linker::new(&engine);
Expand Down Expand Up @@ -132,7 +132,7 @@ impl WastRunner {
Ok(())
}

/// Compiles the Wasm module and stores it into the [`TestContext`].
/// Compiles, validates and instantiates the Wasm module.
///
/// # Errors
///
Expand Down Expand Up @@ -204,7 +204,7 @@ impl WastRunner {
Ok(())
}

/// Converts the [`WastArgCore`][`wast::core::WastArgCore`] into a [`wasmi::Value`] if possible.
/// Converts the [`WastArgCore`][`wast::core::WastArgCore`] into a [`wasmi::Val`] if possible.
fn value(&mut self, value: &WastArgCore) -> Option<Val> {
use wasmi::{ExternRef, FuncRef};
use wast::core::{AbstractHeapType, HeapType};
Expand Down Expand Up @@ -419,8 +419,11 @@ impl<'runner> DirectivesProcessor<'runner> {

/// Asserts that `result` match the `expected` value.
fn assert_result(&self, result: &Val, expected: &WastRet) -> Result<()> {
let WastRet::Core(expected) = expected else {
bail!("unexpected component-model return value: {expected:?}")
let expected = match expected {
WastRet::Core(arg) => arg,
WastRet::Component(arg) => {
bail!("encountered unsupported component-model result: {arg:?}")
}
};
let is_equal = match (result, expected) {
(Val::I32(result), WastRetCore::I32(expected)) => result == expected,
Expand Down Expand Up @@ -577,7 +580,7 @@ impl<'runner> DirectivesProcessor<'runner> {
let arg = match arg {
WastArg::Core(arg) => arg,
WastArg::Component(arg) => {
bail!("Wasmi does not support the Wasm `component-model` but found {arg:?}")
bail!("encountered unsupported component-model argument: {arg:?}")
}
};
let Some(val) = self.runner.value(arg) else {
Expand Down

0 comments on commit 585a129

Please sign in to comment.