Skip to content

Commit 955b9ae

Browse files
authored
make BigInt.fromFloat error-safe (#7419)
* make fromFloat error-safe fixes #7089 ⚠️ breaking change * shadow rather than prefix with underscore
1 parent 484fa0d commit 955b9ae

File tree

8 files changed

+31
-4
lines changed

8 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :boom: Breaking Change
1616

1717
- Rename `JsError` to `JsExn` and error modules cleanup. https://github.com/rescript-lang/rescript/pull/7408
18+
- Make `BigInt.fromFloat` return an option rather than throwing an error in case it's passed a value with a decimal value. https://github.com/rescript-lang/rescript/pull/7419
1819

1920
#### :rocket: New Feature
2021

lib/es6/Stdlib_BigInt.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11

22

33

4+
function fromFloat(value) {
5+
try {
6+
return BigInt(value);
7+
} catch (exn) {
8+
return;
9+
}
10+
}
11+
412
function toInt(t) {
513
return Number(t) | 0;
614
}
@@ -10,6 +18,7 @@ function bitwiseNot(x) {
1018
}
1119

1220
export {
21+
fromFloat,
1322
toInt,
1423
bitwiseNot,
1524
}

lib/js/Stdlib_BigInt.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
'use strict';
22

33

4+
function fromFloat(value) {
5+
try {
6+
return BigInt(value);
7+
} catch (exn) {
8+
return;
9+
}
10+
}
11+
412
function toInt(t) {
513
return Number(t) | 0;
614
}
@@ -9,6 +17,7 @@ function bitwiseNot(x) {
917
return x ^ -1n;
1018
}
1119

20+
exports.fromFloat = fromFloat;
1221
exports.toInt = toInt;
1322
exports.bitwiseNot = bitwiseNot;
1423
/* No side effect */

runtime/Stdlib_BigInt.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ external fromStringExn: string => bigint = "BigInt"
3737
@val external fromInt: int => bigint = "BigInt"
3838
@val external fromFloat: float => bigint = "BigInt"
3939

40+
let fromFloat = (value: float) => {
41+
try Some(fromFloat(value)) catch {
42+
| _ => None
43+
}
44+
}
45+
4046
@send
4147
/**
4248
Formats a `bigint` as a string. Return a `string` representing the given value.

tests/tests/src/core/Core_TempTests.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ console.info("BigInt");
140140

141141
console.info("---");
142142

143-
console.log(Primitive_bigint.div(BigInt(1), BigInt(12.0)));
143+
console.log(Primitive_bigint.div(BigInt(1), Stdlib_Option.getOr(Stdlib_BigInt.fromFloat(12.0), 0n)));
144144

145145
console.info("");
146146

tests/tests/src/core/Core_TempTests.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Console.info("---")
7777
@warning("-44")
7878
Console.log({
7979
open BigInt
80-
fromInt(1) / fromFloat(12.0)
80+
fromInt(1) / fromFloat(12.0)->Option.getOr(0n)
8181
})
8282

8383
Console.info("")

tests/tests/src/core/Core_TestTests.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import * as Test from "./Test.mjs";
44
import * as Pervasives from "rescript/lib/es6/Pervasives.js";
5+
import * as Stdlib_BigInt from "rescript/lib/es6/Stdlib_BigInt.js";
6+
import * as Stdlib_Option from "rescript/lib/es6/Stdlib_Option.js";
57
import * as Primitive_object from "rescript/lib/es6/Primitive_object.js";
68

79
let eq = Primitive_object.equal;
810

9-
let bign = BigInt(Number.MAX_VALUE);
11+
let bign = Stdlib_Option.getOr(Stdlib_BigInt.fromFloat(Number.MAX_VALUE), 0n);
1012

1113
let bign$1 = bign + bign;
1214

tests/tests/src/core/Core_TestTests.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let eq = (a, b) => a == b
22

3-
let bign = BigInt.fromFloat(Float.Constants.maxValue)
3+
let bign = BigInt.fromFloat(Float.Constants.maxValue)->Option.getOr(0n)
44
let bign = BigInt.add(bign, bign)
55

66
Test.run(__POS_OF__("print null"), Test.print(null), eq, "null")

0 commit comments

Comments
 (0)