Skip to content

Commit

Permalink
Fix parsing f32/f64 CLI arguments as floats (#7440)
Browse files Browse the repository at this point in the history
Type inference wasn't enough for this situation since floats are stored
as `u32` and `u64` while they're at rest to avoid modification.

Closes #7401
  • Loading branch information
alexcrichton authored Nov 1, 2023
1 parent dcc8c2b commit efeeaf5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ impl RunCommand {
// parses base-10 representations.
ValType::I32 => Val::I32(val.parse()?),
ValType::I64 => Val::I64(val.parse()?),
ValType::F32 => Val::F32(val.parse()?),
ValType::F64 => Val::F64(val.parse()?),
ValType::F32 => Val::F32(val.parse::<f32>()?.to_bits()),
ValType::F64 => Val::F64(val.parse::<f64>()?.to_bits()),
t => bail!("unsupported argument type {:?}", t),
});
}
Expand Down
19 changes: 19 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,25 @@ warning: this CLI invocation of Wasmtime is going to break in the future -- for
Ok(())
}

#[test]
fn float_args() -> Result<()> {
let result = run_wasmtime(&[
"--invoke",
"echo_f32",
"tests/all/cli_tests/simple.wat",
"1.0",
])?;
assert_eq!(result, "1\n");
let result = run_wasmtime(&[
"--invoke",
"echo_f64",
"tests/all/cli_tests/simple.wat",
"1.1",
])?;
assert_eq!(result, "1.1\n");
Ok(())
}

mod test_programs {
use super::{get_wasmtime_command, run_wasmtime};
use anyhow::Result;
Expand Down
2 changes: 2 additions & 0 deletions tests/all/cli_tests/simple.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
)
(func (export "get_f32") (result f32) f32.const 100)
(func (export "get_f64") (result f64) f64.const 100)
(func (export "echo_f32") (param f32) (result f32) local.get 0)
(func (export "echo_f64") (param f64) (result f64) local.get 0)
)

0 comments on commit efeeaf5

Please sign in to comment.