Skip to content

[js-api] Unclear spec for -0 in ToWebAssemblyValue? #1984

Description

@Liedtke

The algorithm ToWebAssemblyValue has the following specification for i31:

7.4. Else if v is a Number and v is equal to ? ToInt32(v) and ℝ(v) < 2^30 and ℝ(v) ⩾ -2^30,

What does "v is equal to ToInt32(v)" exactly mean? ToInt32(-0) is +0. Is +0 equal to -0? JS would say yes (-0 === +0), however, it is an important property that if a parameter is typed externref or anyref, ToWebAssemblyValue() should not have any observable side effects on JS values (especially when passing it back to JS).

The ECMAScript spec seems to explicitly list -0 in many cases (for clarity?), maybe this algorithm should do the same?

Activity

  1. bvisness commented on Sep 23, 2025

    @bvisness
    Contributor

    I imagine that it would be equality as defined for Number, which is defined as typical floating point equality, meaning -0 would equal +0.

  2. rossberg commented on Sep 23, 2025

    @rossberg
    Member

    No, this should not equate -0 and +0, since it's only supposed to include values that are in the image of the ToJSValue for an i31. The correct check should be using SameValue.

  3. bvisness commented on Sep 23, 2025

    @bvisness
    Contributor

    Well, we explicitly implemented the opposite based on the above reading of the spec, but I'm sure we'd be able to change it.

  4. rossberg commented on Sep 23, 2025

    @rossberg
    Member

    Yeah, that would also be my literal interpretation of what's written. I think the text should be fixed, but maybe other folks have a different opinion?

  5. eqrion commented on Sep 24, 2025

    @eqrion
    Collaborator

    I'm fine with modifying the spec text so that -0 is disallowed for i31ref. I think having Object.is(ToWebAssemblyValue(ToJSValue(x)), x) always true is a nice property.

  6. Liedtke commented on Sep 24, 2025

    @Liedtke
    ContributorAuthor

    I think having Object.is(ToWebAssemblyValue(ToJSValue(x)), x) always true is a nice property.

    I guess, we could add a spec test like:

    let global = new WebAssembly.Global({value: "externref", mutable: true});
    global.value = -0;
    assert(Object.is(global.value, -0));
    

    If that is agreeable, I can extend #1983 to test for that.

  7. eqrion commented on Oct 2, 2025

    @eqrion
    Collaborator

    That's fine with me. I think the JS-API spec text needs to be updated to clarify this too, as the plain reading of it now would permit -0. SpiderMonkey will need to update it's implementation again, but that's fine.

  8. tlively commented on Oct 15, 2025

    @tlively
    Member

    @kmiller68, any concerns about disallowing -0 or i31ref?

    @Liedtke, extending the test would be good.

  9. Ms2ger commented on Oct 16, 2025

    @Ms2ger
    Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions