Skip to content

Commit 1da6a78

Browse files
committed
Use f{32,64} as wrapped types of wasmtime::component::Val::Float{32,64}
The definitions of `wasmtime::component::Val::Float{32,64}` mirrored `wasmtime::Val::F{32,64}` in using `u{32,64}` as their wrapped types. This is necessary for the core wasm `f32`/`f64` types because Rust's floating-point types don't guarantee the preservation of NaN bit representations. The component model `float32`/`float64` on the other hand require NaN canonicalization, so we can use the more natural Rust `f{32,64}` instead. Closes bytecodealliance#5480
1 parent c9c7d49 commit 1da6a78

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
lines changed

crates/fuzzing/src/generators/component_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub fn arbitrary_val(ty: &component::Type, input: &mut Unstructured) -> arbitrar
3434
Type::U32 => Val::U32(input.arbitrary()?),
3535
Type::S64 => Val::S64(input.arbitrary()?),
3636
Type::U64 => Val::U64(input.arbitrary()?),
37-
Type::Float32 => Val::Float32(input.arbitrary::<f32>()?.to_bits()),
38-
Type::Float64 => Val::Float64(input.arbitrary::<f64>()?.to_bits()),
37+
Type::Float32 => Val::Float32(input.arbitrary()?),
38+
Type::Float64 => Val::Float64(input.arbitrary()?),
3939
Type::Char => Val::Char(input.arbitrary()?),
4040
Type::String => Val::String(input.arbitrary()?),
4141
Type::List(list) => {

crates/wasmtime/src/component/values.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::ops::Deref;
1111
use wasmtime_component_util::{DiscriminantSize, FlagsSize};
1212
use wasmtime_environ::component::VariantInfo;
1313

14-
#[derive(PartialEq, Eq, Clone)]
14+
#[derive(PartialEq, Clone)]
1515
pub struct List {
1616
ty: types::List,
1717
values: Box<[Val]>,
@@ -57,7 +57,7 @@ impl fmt::Debug for List {
5757
}
5858
}
5959

60-
#[derive(PartialEq, Eq, Clone)]
60+
#[derive(PartialEq, Clone)]
6161
pub struct Record {
6262
ty: types::Record,
6363
values: Box<[Val]>,
@@ -127,7 +127,7 @@ impl fmt::Debug for Record {
127127
}
128128
}
129129

130-
#[derive(PartialEq, Eq, Clone)]
130+
#[derive(PartialEq, Clone)]
131131
pub struct Tuple {
132132
ty: types::Tuple,
133133
values: Box<[Val]>,
@@ -176,7 +176,7 @@ impl fmt::Debug for Tuple {
176176
}
177177
}
178178

179-
#[derive(PartialEq, Eq, Clone)]
179+
#[derive(PartialEq, Clone)]
180180
pub struct Variant {
181181
ty: types::Variant,
182182
discriminant: u32,
@@ -284,7 +284,7 @@ impl fmt::Debug for Enum {
284284
}
285285
}
286286

287-
#[derive(PartialEq, Eq, Clone)]
287+
#[derive(PartialEq, Clone)]
288288
pub struct Union {
289289
ty: types::Union,
290290
discriminant: u32,
@@ -336,7 +336,7 @@ impl fmt::Debug for Union {
336336
}
337337
}
338338

339-
#[derive(PartialEq, Eq, Clone)]
339+
#[derive(PartialEq, Clone)]
340340
pub struct OptionVal {
341341
ty: types::OptionType,
342342
discriminant: u32,
@@ -378,7 +378,7 @@ impl fmt::Debug for OptionVal {
378378
}
379379
}
380380

381-
#[derive(PartialEq, Eq, Clone)]
381+
#[derive(PartialEq, Clone)]
382382
pub struct ResultVal {
383383
ty: types::ResultType,
384384
discriminant: u32,
@@ -487,7 +487,7 @@ impl fmt::Debug for Flags {
487487
}
488488

489489
/// Represents possible runtime values which a component function can either consume or produce
490-
#[derive(Debug, PartialEq, Eq, Clone)]
490+
#[derive(Debug, Clone, PartialEq)]
491491
#[allow(missing_docs)]
492492
pub enum Val {
493493
Bool(bool),
@@ -499,8 +499,8 @@ pub enum Val {
499499
U32(u32),
500500
S64(i64),
501501
U64(u64),
502-
Float32(u32),
503-
Float64(u64),
502+
Float32(f32),
503+
Float64(f64),
504504
Char(char),
505505
String(Box<str>),
506506
List(List),
@@ -560,8 +560,8 @@ impl Val {
560560
Type::U32 => Val::U32(u32::lift(store, options, next(src))?),
561561
Type::S64 => Val::S64(i64::lift(store, options, next(src))?),
562562
Type::U64 => Val::U64(u64::lift(store, options, next(src))?),
563-
Type::Float32 => Val::Float32(u32::lift(store, options, next(src))?),
564-
Type::Float64 => Val::Float64(u64::lift(store, options, next(src))?),
563+
Type::Float32 => Val::Float32(f32::lift(store, options, next(src))?),
564+
Type::Float64 => Val::Float64(f64::lift(store, options, next(src))?),
565565
Type::Char => Val::Char(char::lift(store, options, next(src))?),
566566
Type::String => {
567567
Val::String(Box::<str>::lift(store, options, &[*next(src), *next(src)])?)
@@ -688,8 +688,8 @@ impl Val {
688688
Type::U32 => Val::U32(u32::load(mem, bytes)?),
689689
Type::S64 => Val::S64(i64::load(mem, bytes)?),
690690
Type::U64 => Val::U64(u64::load(mem, bytes)?),
691-
Type::Float32 => Val::Float32(u32::load(mem, bytes)?),
692-
Type::Float64 => Val::Float64(u64::load(mem, bytes)?),
691+
Type::Float32 => Val::Float32(f32::load(mem, bytes)?),
692+
Type::Float64 => Val::Float64(f64::load(mem, bytes)?),
693693
Type::Char => Val::Char(char::load(mem, bytes)?),
694694
Type::String => Val::String(Box::<str>::load(mem, bytes)?),
695695
Type::List(handle) => {

crates/wast/src/component.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub fn val(v: &WastVal<'_>, ty: &Type) -> Result<Val> {
1818
WastVal::S32(b) => Val::S32(*b),
1919
WastVal::U64(b) => Val::U64(*b),
2020
WastVal::S64(b) => Val::S64(*b),
21-
WastVal::Float32(b) => Val::Float32(b.bits),
22-
WastVal::Float64(b) => Val::Float64(b.bits),
21+
WastVal::Float32(b) => Val::Float32(f32::from_bits(b.bits)),
22+
WastVal::Float64(b) => Val::Float64(f64::from_bits(b.bits)),
2323
WastVal::Char(b) => Val::Char(*b),
2424
WastVal::String(s) => Val::String(s.to_string().into()),
2525
WastVal::List(vals) => match ty {
@@ -173,11 +173,11 @@ pub fn match_val(expected: &WastVal<'_>, actual: &Val) -> Result<()> {
173173
_ => mismatch(expected, actual),
174174
},
175175
WastVal::Float32(e) => match actual {
176-
Val::Float32(a) => core::match_f32(*a, &NanPattern::Value(*e)),
176+
Val::Float32(a) => core::match_f32(a.to_bits(), &NanPattern::Value(*e)),
177177
_ => mismatch(expected, actual),
178178
},
179179
WastVal::Float64(e) => match actual {
180-
Val::Float64(a) => core::match_f64(*a, &NanPattern::Value(*e)),
180+
Val::Float64(a) => core::match_f64(a.to_bits(), &NanPattern::Value(*e)),
181181
_ => mismatch(expected, actual),
182182
},
183183
WastVal::Char(e) => match actual {

tests/all/component_model/dynamic.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ fn primitives() -> Result<()> {
2121
(Val::S64(-31415926535897), "s64", Param(Type::I64, Some(0))),
2222
(Val::U64(31415926535897), "u64", Param(Type::I64, Some(0))),
2323
(
24-
Val::Float32(3.14159265_f32.to_bits()),
24+
Val::Float32(3.14159265),
2525
"float32",
2626
Param(Type::F32, Some(0)),
2727
),
2828
(
29-
Val::Float64(3.14159265_f64.to_bits()),
29+
Val::Float64(3.14159265),
3030
"float64",
3131
Param(Type::F64, Some(0)),
3232
),
@@ -59,10 +59,7 @@ fn primitives() -> Result<()> {
5959
let err = func
6060
.call_and_post_return(
6161
&mut store,
62-
&[
63-
Val::Float64(3.14159265_f64.to_bits()),
64-
Val::Float64(3.14159265_f64.to_bits()),
65-
],
62+
&[Val::Float64(3.14159265), Val::Float64(3.14159265)],
6663
&mut output,
6764
)
6865
.unwrap_err();
@@ -135,7 +132,7 @@ fn lists() -> Result<()> {
135132
.new_val(Box::new([
136133
Val::U32(32343),
137134
Val::U32(79023439),
138-
Val::Float32(3.14159265_f32.to_bits()),
135+
Val::Float32(3.14159265),
139136
]))
140137
.unwrap_err();
141138

@@ -167,7 +164,7 @@ fn records() -> Result<()> {
167164
let inner_type = &ty.unwrap_record().fields().nth(2).unwrap().ty;
168165
let input = ty.unwrap_record().new_val([
169166
("A", Val::U32(32343)),
170-
("B", Val::Float64(3.14159265_f64.to_bits())),
167+
("B", Val::Float64(3.14159265)),
171168
(
172169
"C",
173170
inner_type
@@ -186,7 +183,7 @@ fn records() -> Result<()> {
186183
.unwrap_record()
187184
.new_val([
188185
("A", Val::S32(32343)),
189-
("B", Val::Float64(3.14159265_f64.to_bits())),
186+
("B", Val::Float64(3.14159265)),
190187
(
191188
"C",
192189
inner_type
@@ -204,7 +201,7 @@ fn records() -> Result<()> {
204201
.unwrap_record()
205202
.new_val([
206203
("A", Val::U32(32343)),
207-
("B", Val::Float64(3.14159265_f64.to_bits())),
204+
("B", Val::Float64(3.14159265)),
208205
(
209206
"C",
210207
inner_type
@@ -224,10 +221,7 @@ fn records() -> Result<()> {
224221

225222
let err = ty
226223
.unwrap_record()
227-
.new_val([
228-
("A", Val::U32(32343)),
229-
("B", Val::Float64(3.14159265_f64.to_bits())),
230-
])
224+
.new_val([("A", Val::U32(32343)), ("B", Val::Float64(3.14159265))])
231225
.unwrap_err();
232226

233227
assert!(
@@ -259,7 +253,7 @@ fn variants() -> Result<()> {
259253
let ty = &func.params(&store)[0];
260254
let input = ty
261255
.unwrap_variant()
262-
.new_val("B", Some(Val::Float64(3.14159265_f64.to_bits())))?;
256+
.new_val("B", Some(Val::Float64(3.14159265)))?;
263257
let mut output = [Val::Bool(false)];
264258
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
265259

@@ -481,14 +475,14 @@ fn everything() -> Result<()> {
481475
"J",
482476
j_type
483477
.unwrap_variant()
484-
.new_val("L", Some(Val::Float64(3.14159265_f64.to_bits())))?,
478+
.new_val("L", Some(Val::Float64(3.14159265)))?,
485479
),
486480
("P", Val::S8(42)),
487481
("Q", Val::S16(4242)),
488482
("R", Val::S32(42424242)),
489483
("S", Val::S64(424242424242424242)),
490-
("T", Val::Float32(3.14159265_f32.to_bits())),
491-
("U", Val::Float64(3.14159265_f64.to_bits())),
484+
("T", Val::Float32(3.14159265)),
485+
("U", Val::Float64(3.14159265)),
492486
("V", Val::String(Box::from("wow, nice types"))),
493487
("W", Val::Char('🦀')),
494488
(
@@ -499,9 +493,7 @@ fn everything() -> Result<()> {
499493
),
500494
(
501495
"Z",
502-
z_type
503-
.unwrap_union()
504-
.new_val(1, Val::Float64(3.14159265_f64.to_bits()))?,
496+
z_type.unwrap_union().new_val(1, Val::Float64(3.14159265))?,
505497
),
506498
(
507499
"AA",

0 commit comments

Comments
 (0)