From 8ecf002bb6ab29963ae253650fd873a835de2a59 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 1 Apr 2021 10:23:30 -0700 Subject: [PATCH] Fix printing float results from the CLI Previously their bit patterns were printed interpreted as decimals, now they're printed as floats. --- src/commands/run.rs | 6 +++--- tests/all/cli_tests.rs | 20 ++++++++++++++++++++ tests/wasm/simple.wat | 4 +++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/commands/run.rs b/src/commands/run.rs index f8b701014689..abc54bf9b1d7 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -333,10 +333,10 @@ impl RunCommand { match result { Val::I32(i) => println!("{}", i), Val::I64(i) => println!("{}", i), - Val::F32(f) => println!("{}", f), - Val::F64(f) => println!("{}", f), + Val::F32(f) => println!("{}", f32::from_bits(f)), + Val::F64(f) => println!("{}", f64::from_bits(f)), Val::ExternRef(_) => println!(""), - Val::FuncRef(_) => println!(""), + Val::FuncRef(_) => println!(""), Val::V128(i) => println!("{}", i), } } diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs index 5c825cd59040..ae09dd97cd5c 100644 --- a/tests/all/cli_tests.rs +++ b/tests/all/cli_tests.rs @@ -97,6 +97,26 @@ fn run_wasmtime_simple_wat() -> Result<()> { "--disable-cache", "4", ])?; + assert_eq!( + run_wasmtime(&[ + "run", + wasm.path().to_str().unwrap(), + "--invoke", + "get_f32", + "--disable-cache", + ])?, + "100\n" + ); + assert_eq!( + run_wasmtime(&[ + "run", + wasm.path().to_str().unwrap(), + "--invoke", + "get_f64", + "--disable-cache", + ])?, + "100\n" + ); Ok(()) } diff --git a/tests/wasm/simple.wat b/tests/wasm/simple.wat index 7b618ee4291c..a851dfa00e2b 100644 --- a/tests/wasm/simple.wat +++ b/tests/wasm/simple.wat @@ -2,4 +2,6 @@ (func (export "simple") (param i32) (result i32) local.get 0 ) -) \ No newline at end of file + (func (export "get_f32") (result f32) f32.const 100) + (func (export "get_f64") (result f64) f64.const 100) +)