wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

`f64` to `externref` conversion isn't lossless in Firefox v143+

Open tyilo opened this issue 2 months ago • 3 comments

neg_zero should return -0.0 to JS, but it returns +0.0 in Firefox v143+ instead:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn neg_zero() -> JsValue {
    JsValue::from_f64(-0.0)
}
<!doctype html>
<html>
  <head>
    <title>-0.0 wasm test</title>
  </head>
  <body>
    <h1>-0.0 from wasm: <span id="result">...</span></h1>
    <script type="module">
      import init, { neg_zero } from "./pkg/wasm_bindgen_neg_zero_test.js";
      await init();
      const result = neg_zero();
      document.getElementById("result").textContent = Object.is(result, -0.0)? "-0.0": result;
    </script>
  </body>
</html>

Upstream report to Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1997423


The example can be reduced to (without wasm-bindgen):

(module
  (import "fns" "identity" (func $identity (param f64) (result externref)))
  (func (export "neg_zero") (result externref)
    f64.const -0x0p+0
    call $identity
  )
)
<!doctype html>
<html>
  <head>
    <title>-0.0 wasm test</title>
  </head>
  <body>
    <h1>-0.0 from wasm: <span id="result">...</span></h1>
    <script>
      async function init() {
        const obj = await WebAssembly.instantiateStreaming(fetch("test.wasm"), {
          fns: {
            identity: x => x,
          },
        });
        const result = obj.instance.exports.neg_zero();
        document.getElementById("result").textContent = Object.is(result, -0.0)? "-0.0": result;
      }
      
      init();
    </script>
  </body>
</html>

I don't know if the project should do anything about this (as this will probably be fixed in Firefox), but I think it would be nice to have some tests for JS <-> Browser roundtrip for Firefox and Chromium. I couldn't immediately see any such tests in the repo.

tyilo avatar Nov 03 '25 21:11 tyilo

Seems to have been confirmed to be a Firefox bug.

I'm not sure if tests here make sense because at the end of the day we can't do anything about it. While it might be useful to still have the tests so we can report bugs to browsers, it makes more sense to me to instead contribute these tests to those browsers.

But thank you for finding and reporting the bug to Firefox!

daxpedda avatar Nov 04 '25 07:11 daxpedda

I think it would make sense to keep this open until it is fixed in Firefox. If for some reason this doesn't happen, wasm-bindgen would need to change how it generates bindings or loosen it's guarantees.

tyilo avatar Nov 04 '25 07:11 tyilo

I don't think we would change the bindings and we don't have any strict guarantees like that written down anywhere.

In any case, if it really doesn't get fixed we should leave a cautionary note somewhere.

daxpedda avatar Nov 04 '25 07:11 daxpedda