Skip to content

Commit

Permalink
Improve Wasmi and Wasmtime API mirroring with renamings (#1011)
Browse files Browse the repository at this point in the history
* rename Config::engine_limits to enforced_limits

* rename Value[Type] to Val[Type]

this improves mirroring between Wasmi and Wasmtime.

* rename UntypedValue to UntypedVal

Following steps with Value to Val renaming.

* rename TypedValue to TypedVal

Following Value to Val renaming.

* fix some forgotten doc links

* rename WasmType[List] to WasmTy[List]

Improves Wasmi and Wasmtime mirroring.

* rename AsContext::UserState to Data

Improves Wasmi and Wasmtime mirroring.

* update changelog for v0.32

* add forgotten changelog item

* fix changelog item

* remove details from changelog

Details can be found in the PR description.

* add forgotten changelog item from another PR

* add forgotten item
  • Loading branch information
Robbepop committed May 2, 2024
1 parent 232b95c commit 67d33c1
Show file tree
Hide file tree
Showing 96 changed files with 1,809 additions and 1,922 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Dates in this file are formattes as `YYYY-MM-DD`.
- They now show `nan:0x{bytes}` where `{bytes}` is their respective raw bytes.
- Implement `Sync` for `ResumableInvocation` and `TypedResumableInvocation`. (https://github.com/wasmi-labs/wasmi/pull/870)
- Properly mirror Wasmtime's fuel API. (https://github.com/wasmi-labs/wasmi/pull/1002)
- Renamed some Wasmi items to improve its Wasmtime mirroring. (https://github.com/wasmi-labs/wasmi/pull/1011)
- Improve Wasmtime API mirror for Store fuel. (https://github.com/wasmi-labs/wasmi/pull/1002)

### Removed

Expand Down
42 changes: 21 additions & 21 deletions crates/cli/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
use crate::context::Context;
use std::fmt::{self, Display};
use wasmi::{core::ValueType, FuncType, Value};
use wasmi::{core::ValType, FuncType, Val};

/// [`Display`]-wrapper type for [`ValueType`].
pub struct DisplayValueType<'a>(&'a ValueType);
/// [`Display`]-wrapper type for [`ValType`].
pub struct DisplayValueType<'a>(&'a ValType);

impl<'a> From<&'a ValueType> for DisplayValueType<'a> {
fn from(value_type: &'a ValueType) -> Self {
impl<'a> From<&'a ValType> for DisplayValueType<'a> {
fn from(value_type: &'a ValType) -> Self {
Self(value_type)
}
}

impl Display for DisplayValueType<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
ValueType::I32 => write!(f, "i32"),
ValueType::I64 => write!(f, "i64"),
ValueType::F32 => write!(f, "f32"),
ValueType::F64 => write!(f, "f64"),
ValueType::FuncRef => write!(f, "funcref"),
ValueType::ExternRef => write!(f, "externref"),
ValType::I32 => write!(f, "i32"),
ValType::I64 => write!(f, "i64"),
ValType::F32 => write!(f, "f32"),
ValType::F64 => write!(f, "f64"),
ValType::FuncRef => write!(f, "funcref"),
ValType::ExternRef => write!(f, "externref"),
}
}
}

/// [`Display`]-wrapper type for [`Value`].
pub struct DisplayValue<'a>(&'a Value);
/// [`Display`]-wrapper type for [`Val`].
pub struct DisplayValue<'a>(&'a Val);

impl<'a> From<&'a Value> for DisplayValue<'a> {
fn from(value: &'a Value) -> Self {
impl<'a> From<&'a Val> for DisplayValue<'a> {
fn from(value: &'a Val) -> Self {
Self(value)
}
}

impl<'a> fmt::Display for DisplayValue<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Value::I32(value) => write!(f, "{value}"),
Value::I64(value) => write!(f, "{value}"),
Value::F32(value) => write!(f, "{value}"),
Value::F64(value) => write!(f, "{value}"),
Value::FuncRef(value) => panic!("cannot display funcref values but found {value:?}"),
Value::ExternRef(value) => {
Val::I32(value) => write!(f, "{value}"),
Val::I64(value) => write!(f, "{value}"),
Val::F32(value) => write!(f, "{value}"),
Val::F64(value) => write!(f, "{value}"),
Val::FuncRef(value) => panic!("cannot display funcref values but found {value:?}"),
Val::ExternRef(value) => {
panic!("cannot display externref values but found {value:?}")
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::{anyhow, bail, Error, Result};
use clap::Parser;
use context::Context;
use std::{path::Path, process};
use wasmi::{Func, FuncType, Value};
use wasmi::{Func, FuncType, Val};

mod args;
mod context;
Expand Down Expand Up @@ -80,7 +80,7 @@ fn print_remaining_fuel(args: &Args, ctx: &Context) {
/// # Errors
///
/// If too many or too few function arguments were given to the invoked function.
fn typecheck_args(func_name: &str, func_ty: &FuncType, args: &[Value]) -> Result<(), Error> {
fn typecheck_args(func_name: &str, func_ty: &FuncType, args: &[Val]) -> Result<(), Error> {
if func_ty.params().len() != args.len() {
bail!(
"invalid amount of arguments given to function {}. expected {} but received {}",
Expand Down Expand Up @@ -125,15 +125,15 @@ fn get_invoked_func(args: &Args, ctx: &Context) -> Result<(String, Func), Error>
}

/// Prints a signalling text that Wasm execution has started.
fn print_execution_start(wasm_file: &Path, func_name: &str, func_args: &[Value]) {
fn print_execution_start(wasm_file: &Path, func_name: &str, func_args: &[Val]) {
println!(
"executing File({wasm_file:?})::{func_name}({}) ...",
DisplaySequence::new(", ", func_args.iter().map(DisplayValue::from))
);
}

/// Prints the results of the Wasm computation in a human readable form.
fn print_pretty_results(results: &[Value]) {
fn print_pretty_results(results: &[Val]) {
match results.len() {
0 => {}
1 => {
Expand Down
42 changes: 8 additions & 34 deletions crates/cli/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use core::borrow::Borrow;
use wasmi::core::ValueType;
use wasmi::core::ValType;

fn assert_display(func_type: impl Borrow<FuncType>, expected: &str) {
assert_eq!(
Expand All @@ -16,50 +16,34 @@ fn display_0in_0out() {

#[test]
fn display_1in_0out() {
assert_display(FuncType::new([ValueType::I32], []), "fn(i32)");
assert_display(FuncType::new([ValType::I32], []), "fn(i32)");
}

#[test]
fn display_0in_1out() {
assert_display(FuncType::new([], [ValueType::I32]), "fn() -> i32");
assert_display(FuncType::new([], [ValType::I32]), "fn() -> i32");
}

#[test]
fn display_1in_1out() {
assert_display(
FuncType::new([ValueType::I32], [ValueType::I32]),
FuncType::new([ValType::I32], [ValType::I32]),
"fn(i32) -> i32",
);
}

#[test]
fn display_4in_0out() {
assert_display(
FuncType::new(
[
ValueType::I32,
ValueType::I64,
ValueType::F32,
ValueType::F64,
],
[],
),
FuncType::new([ValType::I32, ValType::I64, ValType::F32, ValType::F64], []),
"fn(i32, i64, f32, f64)",
);
}

#[test]
fn display_0in_4out() {
assert_display(
FuncType::new(
[],
[
ValueType::I32,
ValueType::I64,
ValueType::F32,
ValueType::F64,
],
),
FuncType::new([], [ValType::I32, ValType::I64, ValType::F32, ValType::F64]),
"fn() -> (i32, i64, f32, f64)",
);
}
Expand All @@ -68,18 +52,8 @@ fn display_0in_4out() {
fn display_4in_4out() {
assert_display(
FuncType::new(
[
ValueType::I32,
ValueType::I64,
ValueType::F32,
ValueType::F64,
],
[
ValueType::I32,
ValueType::I64,
ValueType::F32,
ValueType::F64,
],
[ValType::I32, ValType::I64, ValType::F32, ValType::F64],
[ValType::I32, ValType::I64, ValType::F32, ValType::F64],
),
"fn(i32, i64, f32, f64) -> (i32, i64, f32, f64)",
);
Expand Down
32 changes: 16 additions & 16 deletions crates/cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::display::DisplayValueType;
use anyhow::{anyhow, bail, Error};
use std::{ffi::OsStr, fs, path::Path};
use wasmi::{
core::{ValueType, F32, F64},
core::{ValType, F32, F64},
FuncType,
Value,
Val,
};

/// Converts the given `.wat` into `.wasm`.
Expand All @@ -30,27 +30,27 @@ pub fn read_wasm_or_wat(wasm_file: &Path) -> Result<Vec<u8>, Error> {
Ok(wasm_bytes)
}

/// Returns a [`Value`] buffer capable of holding the return values.
/// Returns a [`Val`] buffer capable of holding the return values.
///
/// The returned buffer can be used as function results for [`Func::call`](`wasmi::Func::call`).
pub fn prepare_func_results(ty: &FuncType) -> Box<[Value]> {
ty.results().iter().copied().map(Value::default).collect()
pub fn prepare_func_results(ty: &FuncType) -> Box<[Val]> {
ty.results().iter().copied().map(Val::default).collect()
}

/// Decode the given `args` for the [`FuncType`] `ty`.
///
/// Returns the decoded `args` as a slice of [`Value`] which can be used
/// Returns the decoded `args` as a slice of [`Val`] which can be used
/// as function arguments for [`Func::call`][`wasmi::Func::call`].
///
/// # Errors
///
/// - If there is a type mismatch between `args` and the expected [`ValueType`] by `ty`.
/// - If there is a type mismatch between `args` and the expected [`ValType`] by `ty`.
/// - If too many or too few `args` are given for [`FuncType`] `ty`.
/// - If unsupported [`ExternRef`] or [`FuncRef`] types are encountered.
///
/// [`FuncRef`]: wasmi::FuncRef
/// [`ExternRef`]: wasmi::ExternRef
pub fn decode_func_args(ty: &FuncType, args: &[String]) -> Result<Box<[Value]>, Error> {
pub fn decode_func_args(ty: &FuncType, args: &[String]) -> Result<Box<[Val]>, Error> {
ty.params()
.iter()
.zip(args)
Expand All @@ -68,22 +68,22 @@ pub fn decode_func_args(ty: &FuncType, args: &[String]) -> Result<Box<[Value]>,
};
}
match param_type {
ValueType::I32 => arg.parse::<i32>().map(Value::from).map_err(make_err!()),
ValueType::I64 => arg.parse::<i64>().map(Value::from).map_err(make_err!()),
ValueType::F32 => arg
ValType::I32 => arg.parse::<i32>().map(Val::from).map_err(make_err!()),
ValType::I64 => arg.parse::<i64>().map(Val::from).map_err(make_err!()),
ValType::F32 => arg
.parse::<f32>()
.map(F32::from)
.map(Value::from)
.map(Val::from)
.map_err(make_err!()),
ValueType::F64 => arg
ValType::F64 => arg
.parse::<f64>()
.map(F64::from)
.map(Value::from)
.map(Val::from)
.map_err(make_err!()),
ValueType::FuncRef => {
ValType::FuncRef => {
bail!("the wasmi CLI cannot take arguments of type funcref")
}
ValueType::ExternRef => {
ValType::ExternRef => {
bail!("the wasmi CLI cannot take arguments of type externref")
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ pub use self::{
nan_preserving_float::{F32, F64},
trap::{Trap, TrapCode},
units::Pages,
untyped::{DecodeUntypedSlice, EncodeUntypedSlice, UntypedError, UntypedValue},
value::ValueType,
untyped::{DecodeUntypedSlice, EncodeUntypedSlice, UntypedError, UntypedVal},
value::ValType,
};

0 comments on commit 67d33c1

Please sign in to comment.