Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math.imul sometimes returns incorrectly wrapped negative numbers #448

Closed
sjrd opened this issue Jun 12, 2018 · 1 comment
Closed

Math.imul sometimes returns incorrectly wrapped negative numbers #448

sjrd opened this issue Jun 12, 2018 · 1 comment

Comments

@sjrd
Copy link
Contributor

sjrd commented Jun 12, 2018

Expected, with Node.js v10.0.0:

$ /localhome/doeraene/opt/node-v10.0.0-linux-x64/bin/node -v
v10.0.0
$ /localhome/doeraene/opt/node-v10.0.0-linux-x64/bin/node 
> Math.imul(2147483611, -14)
518

Actual, with Rhino 1.7.9 (also 1.7.7+, since the introduction of Math.imul in 79e11ac):

$ java -jar rhino-1.7.9.jar 
Rhino 1.7.9 2018 03 15
js> Math.imul(2147483611, -14)
-4294966778
js> Math.imul(2147483611, -14) | 0
518

Negative numbers smaller than -2**31 should be wrapped around to the positive side, basically using ToInt32.

sjrd added a commit to sjrd/scala-js that referenced this issue Jun 12, 2018
This is the last version that does not introduce `Math.imul`. The
implementation added in Rhino 1.7.7, and still used in 1.7.9, is
faulty. See mozilla/rhino#448.
@sjrd
Copy link
Contributor Author

sjrd commented Jun 12, 2018

In addition, calling Math.imul with less than two arguments should not return NaN, but 0. That's simply because ToInt32(undefined) === 0. Moreover, if there is only one argument but ToInt32(args[0]) throws, the exception should be thrown, and not swallowed by the early exit with args.length < 2. Here is a transcript from a Node.js session:

$ node
> Math.imul(4)
0
> Math.imul()
0
> var o = { valueOf() { throw new RangeError("meh"); } };
undefined
> Math.imul(o, 5)
RangeError: meh
    at Object.valueOf (repl:1:29)
    at Math.imul (<anonymous>)
> Math.imul(o)
RangeError: meh
    at Object.valueOf (repl:1:29)
    at Math.imul (<anonymous>)

FWIW, I believe the following implementation of js_imul would be correct (untested):

    private Object js_imul(Object[] args)
    {
        if ((args == null) || (args.length == 0)) {
            return ScriptRuntime.toNumber(0);
        }

        int x = Conversions.toInt32(args[0]);
        int y = args.length < 2 ? 0 : Conversions.toInt32(args[1]);
        return ScriptRuntime.toNumber(x * y);
    }

sjrd added a commit to sjrd/rhino that referenced this issue Jun 29, 2018
As an added bonus, return an `int` from `js_imul` to avoid boxing.
At call site, it is assigned to a `double`, not an `Object`, so
eagerly boxing the result inside `js_imul` is counter-productive.
@gbrail gbrail closed this as completed in f192525 Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant