Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 86 additions & 10 deletions crates/wast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::{bail, Context as _, Result};
use core::array;
use std::collections::HashMap;
use wasmi::{
core::{ValType, F32, F64},
core::{ValType, F32, F64, V128},
Config,
Engine,
Extern,
Expand All @@ -18,7 +19,7 @@
Val,
};
use wast::{
core::{AbstractHeapType, HeapType, NanPattern, WastArgCore, WastRetCore},
core::{AbstractHeapType, HeapType, NanPattern, V128Pattern, WastArgCore, WastRetCore},
lexer::Lexer,
parser::ParseBuffer,
token::Id,
Expand Down Expand Up @@ -160,6 +161,10 @@
WastArgCore::I64(arg) => Val::I64(*arg),
WastArgCore::F32(arg) => Val::F32(F32::from_bits(arg.bits)),
WastArgCore::F64(arg) => Val::F64(F64::from_bits(arg.bits)),
WastArgCore::V128(arg) => {
let v128: V128 = u128::from_le_bytes(arg.to_le_bytes()).into();
Val::V128(v128)

Check warning on line 166 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L164-L166

Added lines #L164 - L166 were not covered by tests
}
WastArgCore::RefNull(HeapType::Abstract {
ty: AbstractHeapType::Func,
..
Expand Down Expand Up @@ -387,14 +392,9 @@
let is_equal = match (result, expected) {
(Val::I32(result), WastRetCore::I32(expected)) => result == expected,
(Val::I64(result), WastRetCore::I64(expected)) => result == expected,
(Val::F32(result), WastRetCore::F32(expected)) => match expected {
NanPattern::CanonicalNan | NanPattern::ArithmeticNan => result.to_float().is_nan(),
NanPattern::Value(expected) => result.to_bits() == expected.bits,
},
(Val::F64(result), WastRetCore::F64(expected)) => match expected {
NanPattern::CanonicalNan | NanPattern::ArithmeticNan => result.to_float().is_nan(),
NanPattern::Value(expected) => result.to_bits() == expected.bits,
},
(Val::F32(result), WastRetCore::F32(expected)) => f32_matches(result, expected),
(Val::F64(result), WastRetCore::F64(expected)) => f64_matches(result, expected),
(Val::V128(result), WastRetCore::V128(expected)) => v128_matches(result, expected),

Check warning on line 397 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L397

Added line #L397 was not covered by tests
(
Val::FuncRef(funcref),
WastRetCore::RefNull(Some(HeapType::Abstract {
Expand Down Expand Up @@ -559,3 +559,79 @@
Ok(())
}
}

/// Returns `true` if `actual` matches `expected`.
fn f32_matches(actual: &F32, expected: &NanPattern<wast::token::F32>) -> bool {
match expected {
NanPattern::CanonicalNan | NanPattern::ArithmeticNan => actual.to_float().is_nan(),
NanPattern::Value(expected) => actual.to_bits() == expected.bits,
}
}

/// Returns `true` if `actual` matches `expected`.
fn f64_matches(actual: &F64, expected: &NanPattern<wast::token::F64>) -> bool {
match expected {
NanPattern::CanonicalNan | NanPattern::ArithmeticNan => actual.to_float().is_nan(),
NanPattern::Value(expected) => actual.to_bits() == expected.bits,
}
}

/// Returns `true` if `actual` matches `expected`.
fn v128_matches(actual: &V128, expected: &V128Pattern) -> bool {
match expected {
V128Pattern::I8x16(expected) => {
let actual: [i8; 16] = array::from_fn(|i| extract_lane_as_i8(actual, i));
actual == *expected

Check warning on line 584 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L580-L584

Added lines #L580 - L584 were not covered by tests
}
V128Pattern::I16x8(expected) => {
let actual: [i16; 8] = array::from_fn(|i| extract_lane_as_i16(actual, i));
actual == *expected

Check warning on line 588 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L586-L588

Added lines #L586 - L588 were not covered by tests
}
V128Pattern::I32x4(expected) => {
let actual: [i32; 4] = array::from_fn(|i| extract_lane_as_i32(actual, i));
actual == *expected

Check warning on line 592 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L590-L592

Added lines #L590 - L592 were not covered by tests
}
V128Pattern::I64x2(expected) => {
let actual: [i64; 2] = array::from_fn(|i| extract_lane_as_i64(actual, i));
actual == *expected

Check warning on line 596 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L594-L596

Added lines #L594 - L596 were not covered by tests
}
V128Pattern::F32x4(expected) => {
for (i, expected) in expected.iter().enumerate() {
let bits = extract_lane_as_i32(actual, i) as u32;
if !f32_matches(&F32::from_bits(bits), expected) {
return false;

Check warning on line 602 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L598-L602

Added lines #L598 - L602 were not covered by tests
}
}
true

Check warning on line 605 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L605

Added line #L605 was not covered by tests
}
V128Pattern::F64x2(expected) => {
for (i, expected) in expected.iter().enumerate() {
let bits = extract_lane_as_i64(actual, i) as u64;
if !f64_matches(&F64::from_bits(bits), expected) {
return false;

Check warning on line 611 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L607-L611

Added lines #L607 - L611 were not covered by tests
}
}
true

Check warning on line 614 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L614

Added line #L614 was not covered by tests
}
}
}

/// Returns the `i8` at `lane` from `v128`.
fn extract_lane_as_i8(v128: &V128, lane: usize) -> i8 {
(v128.as_u128() >> (lane * 8)) as i8

Check warning on line 621 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L620-L621

Added lines #L620 - L621 were not covered by tests
}

/// Returns the `i16` at `lane` from `v128`.
fn extract_lane_as_i16(v128: &V128, lane: usize) -> i16 {
(v128.as_u128() >> (lane * 16)) as i16

Check warning on line 626 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L625-L626

Added lines #L625 - L626 were not covered by tests
}

/// Returns the `i32` at `lane` from `v128`.
fn extract_lane_as_i32(v128: &V128, lane: usize) -> i32 {
(v128.as_u128() >> (lane * 32)) as i32

Check warning on line 631 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L630-L631

Added lines #L630 - L631 were not covered by tests
}

/// Returns the `i64` at `lane` from `v128`.
fn extract_lane_as_i64(v128: &V128, lane: usize) -> i64 {
(v128.as_u128() >> (lane * 64)) as i64

Check warning on line 636 in crates/wast/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/wast/src/lib.rs#L635-L636

Added lines #L635 - L636 were not covered by tests
}