Skip to content

Numeric comparison in JavaScript returns an incorrect result #2407

Description

@LesserGiraffe

Description

Strict equality (===) with numbers returns an incorrect result when calling another function.

Steps to Reproduce

Run the following code:

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class Main {
  public static void main(String[] args) {
    String script = """
      function funcA(a) { }

      function funcB(a) {
        if (a === 0) {
          java.lang.System.out.println('true  ' + a);
        } else {
          java.lang.System.out.println('false  ' + a);
        }
        funcA(a);
      }

      (function funcC() {
        funcB(0);
      })();
    """;

    try (Context cx = Context.enter()) {
      Scriptable scope = cx.initStandardObjects();
      cx.evaluateString(scope, script, "example", 1, null);
    }
  }
}

Expected Output

true 0

Actual Output

false 0

Environment

JDK: openJDK 21
Rhino: 1.9.1
OS: Windows 11

Additional Notes

  • If funcA(a) is not called, the comparison works correctly.
  • If cx.setOptimizationLevel(-1) is called to run in interpreter mode, the comparison works correctly.
  • If the comparison in the if statement is written as 0 === a instead, the comparison works correctly.

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