Skip to content

Commit

Permalink
Rename "float32" to "f32". (#1935)
Browse files Browse the repository at this point in the history
The component-model type "float32" was [renamed to "f32"], so rename
Wave's type to match.

[renamed to "f32"]: WebAssembly/component-model#324
  • Loading branch information
sunfishcode authored Dec 3, 2024
1 parent 8b8809d commit 48cc636
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 107 deletions.
4 changes: 2 additions & 2 deletions crates/wasm-wave/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl Node {
WasmTypeKind::U16 => V::make_u16(self.as_number(src)?),
WasmTypeKind::U32 => V::make_u32(self.as_number(src)?),
WasmTypeKind::U64 => V::make_u64(self.as_number(src)?),
WasmTypeKind::Float32 => V::make_float32(self.as_number(src)?),
WasmTypeKind::Float64 => V::make_float64(self.as_number(src)?),
WasmTypeKind::F32 => V::make_f32(self.as_number(src)?),
WasmTypeKind::F64 => V::make_f64(self.as_number(src)?),
WasmTypeKind::Char => V::make_char(self.as_char(src)?),
WasmTypeKind::String => V::make_string(self.as_str(src)?),
WasmTypeKind::List => self.to_wasm_list(ty, src)?,
Expand Down
6 changes: 3 additions & 3 deletions crates/wasm-wave/src/value/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ impl_primitives!(
(U16, u16),
(U32, u32),
(U64, u64),
(Float32, f32),
(Float64, f64),
(F32, f32),
(F64, f64),
(Char, char)
);

Expand Down Expand Up @@ -217,7 +217,7 @@ mod tests {
for ty in [
Type::BOOL,
Type::U8,
Type::FLOAT32,
Type::F32,
Type::STRING,
Type::list(Type::BOOL),
Type::record([("a", Type::BOOL)]).unwrap(),
Expand Down
28 changes: 14 additions & 14 deletions crates/wasm-wave/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub(super) enum ValueEnum {
U32(u32),
S64(i64),
U64(u64),
Float32(f32),
Float64(f64),
F32(f32),
F64(f64),
Char(char),
String(Box<str>),
List(List),
Expand Down Expand Up @@ -144,8 +144,8 @@ impl WasmValue for Value {
ValueEnum::U16(_) => WasmTypeKind::U16,
ValueEnum::U32(_) => WasmTypeKind::U32,
ValueEnum::U64(_) => WasmTypeKind::U64,
ValueEnum::Float32(_) => WasmTypeKind::Float32,
ValueEnum::Float64(_) => WasmTypeKind::Float64,
ValueEnum::F32(_) => WasmTypeKind::F32,
ValueEnum::F64(_) => WasmTypeKind::F64,
ValueEnum::Char(_) => WasmTypeKind::Char,
ValueEnum::String(_) => WasmTypeKind::String,
ValueEnum::List(_) => WasmTypeKind::List,
Expand Down Expand Up @@ -173,14 +173,14 @@ impl WasmValue for Value {
(Char, char, make_char, unwrap_char)
);

fn make_float32(val: f32) -> Self {
fn make_f32(val: f32) -> Self {
let val = canonicalize_nan32(val);
Self(ValueEnum::Float32(val))
Self(ValueEnum::F32(val))
}

fn make_float64(val: f64) -> Self {
fn make_f64(val: f64) -> Self {
let val = canonicalize_nan64(val);
Self(ValueEnum::Float64(val))
Self(ValueEnum::F64(val))
}

fn make_string(val: std::borrow::Cow<str>) -> Self {
Expand Down Expand Up @@ -315,13 +315,13 @@ impl WasmValue for Value {
Ok(Self(ValueEnum::Flags(Flags { ty, flags })))
}

fn unwrap_float32(&self) -> f32 {
let val = *unwrap_val!(&self.0, ValueEnum::Float32, "float32");
fn unwrap_f32(&self) -> f32 {
let val = *unwrap_val!(&self.0, ValueEnum::F32, "f32");
canonicalize_nan32(val)
}

fn unwrap_float64(&self) -> f64 {
let val = *unwrap_val!(&self.0, ValueEnum::Float64, "float64");
fn unwrap_f64(&self) -> f64 {
let val = *unwrap_val!(&self.0, ValueEnum::F64, "f64");
canonicalize_nan64(val)
}

Expand Down Expand Up @@ -404,8 +404,8 @@ fn check_type2(expected: &Type, val: &Value) -> Result<(), WasmValueError> {
(ValueEnum::U16(_), &Type::U16) => {}
(ValueEnum::U32(_), &Type::U32) => {}
(ValueEnum::U64(_), &Type::U64) => {}
(ValueEnum::Float32(_), &Type::FLOAT32) => {}
(ValueEnum::Float64(_), &Type::FLOAT64) => {}
(ValueEnum::F32(_), &Type::F32) => {}
(ValueEnum::F64(_), &Type::F64) => {}
(ValueEnum::Char(_), &Type::CHAR) => {}
(ValueEnum::String(_), &Type::STRING) => {}
(ValueEnum::List(list), _) => {
Expand Down
8 changes: 4 additions & 4 deletions crates/wasm-wave/src/value/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ fn float_round_trips() {
(f32::INFINITY, f64::INFINITY),
(f32::NEG_INFINITY, f64::NEG_INFINITY),
] {
test_value_round_trip(Value::make_float32(float32));
test_value_round_trip(Value::make_float64(float64));
test_value_round_trip(Value::make_f32(float32));
test_value_round_trip(Value::make_f64(float64));
}
}

Expand Down Expand Up @@ -130,8 +130,8 @@ fn local_ty(val: &Value) -> Type {
ValueEnum::U16(_) => Type::U16,
ValueEnum::U32(_) => Type::U32,
ValueEnum::U64(_) => Type::U64,
ValueEnum::Float32(_) => Type::FLOAT32,
ValueEnum::Float64(_) => Type::FLOAT64,
ValueEnum::F32(_) => Type::F32,
ValueEnum::F64(_) => Type::F64,
ValueEnum::Char(_) => Type::CHAR,
ValueEnum::String(_) => Type::STRING,
ValueEnum::List(inner) => Type(TypeEnum::List(inner.ty.clone())),
Expand Down
6 changes: 3 additions & 3 deletions crates/wasm-wave/src/value/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl Type {
pub const U16: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::U16)));
pub const U32: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::U32)));
pub const U64: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::U64)));
pub const FLOAT32: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::Float32)));
pub const FLOAT64: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::Float64)));
pub const F32: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::F32)));
pub const F64: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::F64)));
pub const CHAR: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::Char)));
pub const STRING: Self = Self(TypeEnum::Simple(SimpleType(WasmTypeKind::String)));

Expand Down Expand Up @@ -139,7 +139,7 @@ const fn is_simple(kind: WasmTypeKind) -> bool {
use WasmTypeKind::*;
matches!(
kind,
Bool | S8 | S16 | S32 | S64 | U8 | U16 | U32 | U64 | Float32 | Float64 | Char | String
Bool | S8 | S16 | S32 | S64 | U8 | U16 | U32 | U64 | F32 | F64 | Char | String
)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-wave/src/value/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl<'a> TypeResolver<'a> {
TypeDefKind::Type(Type::S16) => Ok(value::Type::S16),
TypeDefKind::Type(Type::S32) => Ok(value::Type::S32),
TypeDefKind::Type(Type::S64) => Ok(value::Type::S64),
TypeDefKind::Type(Type::F32) => Ok(value::Type::FLOAT32),
TypeDefKind::Type(Type::F64) => Ok(value::Type::FLOAT64),
TypeDefKind::Type(Type::F32) => Ok(value::Type::F32),
TypeDefKind::Type(Type::F64) => Ok(value::Type::F64),
TypeDefKind::Type(Type::Char) => Ok(value::Type::CHAR),
TypeDefKind::Type(Type::String) => Ok(value::Type::STRING),
TypeDefKind::Type(Type::Id(_)) => unreachable!(),
Expand Down
8 changes: 4 additions & 4 deletions crates/wasm-wave/src/wasm/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub enum WasmTypeKind {
U16,
U32,
U64,
Float32,
Float64,
F32,
F64,
Char,
String,
List,
Expand All @@ -43,8 +43,8 @@ impl std::fmt::Display for WasmTypeKind {
WasmTypeKind::U16 => "u16",
WasmTypeKind::U32 => "u32",
WasmTypeKind::U64 => "u64",
WasmTypeKind::Float32 => "float32",
WasmTypeKind::Float64 => "float64",
WasmTypeKind::F32 => "f32",
WasmTypeKind::F64 => "f64",
WasmTypeKind::Char => "char",
WasmTypeKind::String => "string",
WasmTypeKind::List => "list",
Expand Down
16 changes: 8 additions & 8 deletions crates/wasm-wave/src/wasm/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ pub trait WasmValue: Clone + Sized {
/// Returns a new WasmValue of the given type.
///
/// The Rust `f32` type has many distinct NaN bitpatterns, however the
/// component-model `float32` type only has a single NaN value, so this
/// component-model `f32` type only has a single NaN value, so this
/// function does not preserve NaN bitpatterns.
///
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_float32(val: f32) -> Self {
fn make_f32(val: f32) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
///
/// The Rust `f64` type has many distinct NaN bitpatterns, however the
/// component-model `float64` type only has a single NaN value, so this
/// component-model `f64` type only has a single NaN value, so this
/// function does not preserve NaN bitpatterns.
///
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_float64(val: f64) -> Self {
fn make_f64(val: f64) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
Expand Down Expand Up @@ -237,23 +237,23 @@ pub trait WasmValue: Clone + Sized {
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
///
/// The Rust `f32` type has many distinct NaN bitpatterns, however the
/// component-model `float64` type only has a single NaN value, so this
/// component-model `f64` type only has a single NaN value, so this
/// function does not preserve NaN bitpatterns.
///
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_float32(&self) -> f32 {
fn unwrap_f32(&self) -> f32 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
///
/// The Rust `f64` type has many distinct NaN bitpatterns, however the
/// component-model `float64` type only has a single NaN value, so this
/// component-model `f64` type only has a single NaN value, so this
/// function does not preserve NaN bitpatterns.
///
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_float64(&self) -> f64 {
fn unwrap_f64(&self) -> f64 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
Expand Down
8 changes: 4 additions & 4 deletions crates/wasm-wave/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ impl<W: Write> Writer<W> {
WasmTypeKind::U16 => self.write_display(val.unwrap_u16()),
WasmTypeKind::U32 => self.write_display(val.unwrap_u32()),
WasmTypeKind::U64 => self.write_display(val.unwrap_u64()),
WasmTypeKind::Float32 => {
let f = val.unwrap_float32();
WasmTypeKind::F32 => {
let f = val.unwrap_f32();
if f.is_nan() {
self.write_str("nan") // Display is "NaN"
} else {
self.write_display(f)
}
}
WasmTypeKind::Float64 => {
let f = val.unwrap_float64();
WasmTypeKind::F64 => {
let f = val.unwrap_f64();
if f.is_nan() {
self.write_str("nan") // Display is "NaN"
} else {
Expand Down
10 changes: 2 additions & 8 deletions crates/wasm-wave/tests/nan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ fn nan() {

{
use wasm_wave::value::Value;
assert_eq!(
Value::make_float32(val).unwrap_float32().to_bits(),
expected
);
assert_eq!(Value::make_f32(val).unwrap_f32().to_bits(), expected);
}
}

Expand All @@ -52,10 +49,7 @@ fn nan() {

{
use wasm_wave::value::Value;
assert_eq!(
Value::make_float64(val).unwrap_float64().to_bits(),
expected
);
assert_eq!(Value::make_f64(val).unwrap_f64().to_bits(), expected);
}
}
}
4 changes: 2 additions & 2 deletions crates/wasm-wave/tests/types.wit
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ world tests {
export bools: func() -> tuple<bool, bool>
export sints: func() -> tuple<s8, s16, s32, s64>
export uints: func() -> tuple<u8, u16, u32, u64>
export floats: func() -> tuple<float32, float64>
export floats: func() -> tuple<f32, f64>
export options: func() -> tuple<option<u8>, option<option<s8>>>
export list-chars: func() -> list<char>
export list-strings: func() -> list<string>
Expand Down Expand Up @@ -41,4 +41,4 @@ world tests {
export %flags: func() -> flags-type

export func-type: func(a: bool, b: enum-type) -> result<u8>
}
}
16 changes: 8 additions & 8 deletions crates/wasm-wave/tests/ui/accept-floats.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
list-float32(vals: [0, 0, 0, 0, 0, 0, 0])
list-float32(vals: [-3.1415, -100000, -0])
list-float32(vals: [nan, inf, -inf])
list-f32(vals: [0, 0, 0, 0, 0, 0, 0])
list-f32(vals: [-3.1415, -100000, -0])
list-f32(vals: [nan, inf, -inf])
// Largest normal f32
float32(val: 340282350000000000000000000000000000000)
f32(val: 340282350000000000000000000000000000000)
// Truncated precision
float32(val: 340282350000000000000000000000000000000)
f32(val: 340282350000000000000000000000000000000)
// Too large; infinity
float32(val: inf)
f32(val: inf)
// Smallest positive non-zero f32
float32(val: 0.000000000000000000000000000000000000000000001)
f32(val: 0.000000000000000000000000000000000000000000001)
// Too small; round to zero
float32(val: 0)
f32(val: 0)
16 changes: 8 additions & 8 deletions crates/wasm-wave/tests/ui/accept-floats.waves
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
list-float32([0, 0.0, 0e0, 0.0e0, 0e-1, 0e+1, 0.000e100]);
list-float32([-3.1415, -100000, -0.0e-0]);
list-float32([nan, inf, -inf]);
list-f32([0, 0.0, 0e0, 0.0e0, 0e-1, 0e+1, 0.000e100]);
list-f32([-3.1415, -100000, -0.0e-0]);
list-f32([nan, inf, -inf]);
// Largest normal f32
float32(3.4028234664e38);
f32(3.4028234664e38);
// Truncated precision
float32(3.4028234664123e38);
f32(3.4028234664123e38);
// Too large; infinity
float32(3.4028234664e39);
f32(3.4028234664e39);
// Smallest positive non-zero f32
float32(1.4012984643e-45);
f32(1.4012984643e-45);
// Too small; round to zero
float32(1.4012984643e-46);
f32(1.4012984643e-46);
32 changes: 16 additions & 16 deletions crates/wasm-wave/tests/ui/reject-floats.out
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Reject "-nan".
invalid token at 8..9
invalid token at 8..9
unexpected token: LabelOrKeyword at 9..10
unexpected token: LabelOrKeyword at 9..10
invalid token at 4..5
invalid token at 4..5
unexpected token: LabelOrKeyword at 5..6
unexpected token: LabelOrKeyword at 5..6
// Reject "infinity", "-infinity", and uppercase variations.
invalid value type at 8..16
unexpected token: LabelOrKeyword at 12..17
invalid value type at 8..16
invalid token at 8..9
unexpected token: LabelOrKeyword at 9..16
invalid token at 8..9
unexpected token: LabelOrKeyword at 9..16
invalid token at 8..9
invalid value type at 4..12
unexpected token: LabelOrKeyword at 8..13
invalid value type at 4..12
invalid token at 4..5
unexpected token: LabelOrKeyword at 5..12
invalid token at 4..5
unexpected token: LabelOrKeyword at 5..12
invalid token at 4..5
// Reject mixed case variations of "inf" and "nan".
unexpected token: LabelOrKeyword at 9..11
invalid token at 8..9
invalid value type at 8..11
invalid token at 8..9
unexpected token: LabelOrKeyword at 5..7
invalid token at 4..5
invalid value type at 4..7
invalid token at 4..5
Loading

0 comments on commit 48cc636

Please sign in to comment.