Skip to content

Commit c972a13

Browse files
committed
add bigint_test and binding bigint methods, NaN, etc
1 parent 6d8439a commit c972a13

File tree

4 files changed

+329
-1
lines changed

4 files changed

+329
-1
lines changed

jscomp/others/js_bigint.res

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,103 @@
11
/*** JavaScript BigInt API */
22

3+
@val
4+
/**
5+
The special value "Not a Number". See [`NaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN) on MDN.
6+
*/
7+
external _NaN: bigint = "NaN"
8+
9+
@val
10+
@scope("Number")
11+
/**
12+
Tests if the given value is `_NaN`
13+
14+
Note that both `_NaN = _NaN` and `_NaN == _NaN` will return `false`. `isNaN` is
15+
therefore necessary to test for `_NaN`. Return `true` if the given value is
16+
`_NaN`, `false` otherwise. See [`isNaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN) on MDN.
17+
*/
18+
external isNaN: bigint => bool = "isNaN"
19+
20+
/**
21+
Tests if the given value is finite. Return `true` if the given value is a finite
22+
number, `false` otherwise. See [`isFinite`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite) on MDN.
23+
24+
## Examples
25+
26+
```rescript
27+
/* returns [false] */
28+
Js.Bigint.isFinite(infinity)
29+
30+
/* returns [false] */
31+
Js.Bigint.isFinite(neg_infinity)
32+
33+
/* returns [false] */
34+
Js.Bigint.isFinite(Js.Bigint._NaN)
35+
36+
/* returns [true] */
37+
Js.Bigint.isFinite(1234.)
38+
```
39+
*/
40+
external isFinite: bigint => bool = "isFinite"
41+
42+
@val
43+
/**
44+
Parses the given `string` into a `bigint` using JavaScript semantics. Return the
45+
number as a `bigint` if successfully parsed, `null`, `undefined`, `_NaN` otherwise.
46+
47+
## Examples
48+
49+
```rescript
50+
/* returns 123n */
51+
Js.Bigint.fromString("123")
52+
53+
/* returns 0n */
54+
Js.Bigint.fromString("")
55+
56+
/* returns 17n */
57+
Js.Bigint.fromString("0x11")
58+
59+
/* returns 3n */
60+
Js.Bigint.fromString("0b11")
61+
62+
/* returns 9n */
63+
Js.Bigint.fromString("0o11")
64+
```
65+
*/
66+
external fromStringExn: string => bigint = "BigInt"
67+
68+
// Operations
69+
370
external \"~-": bigint => bigint = "%negbigint"
471
external \"~+": bigint => bigint = "%identity"
572
external \"+": (bigint, bigint) => bigint = "%addbigint"
673
external \"-": (bigint, bigint) => bigint = "%subbigint"
774
external \"*": (bigint, bigint) => bigint = "%mulbigint"
875
external \"/": (bigint, bigint) => bigint = "%divbigint"
976
external mod: (bigint, bigint) => bigint = "%modbigint"
77+
78+
@send
79+
/**
80+
Formats a `bigint` as a string. Return a `string` representing the given value.
81+
See [`toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) on MDN.
82+
83+
## Examples
84+
85+
```rescript
86+
/* prints "123" */
87+
Js.Bigint.toString(123n)->Js.log
88+
```
89+
*/
90+
external toString: bigint => string = "toString"
91+
92+
@send
93+
/**
94+
Returns a string with a language-sensitive representation of this Bigint value.
95+
96+
## Examples
97+
98+
```rescript
99+
/* prints "123" */
100+
Js.Bigint.toString(123n)->Js.log
101+
```
102+
*/
103+
external toLocaleString: bigint => string = "toLocaleString"

jscomp/test/bigint_test.js

Lines changed: 170 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/bigint_test.res

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
let (test_id, suites) = (ref(0), ref(list{}))
2+
let eq = loc => Mt_global.collect_eq(test_id, suites, loc)
3+
let approx = loc => Mt_global.collect_approx(test_id, suites, loc)
4+
5+
let bigint_compare = (x: bigint, y) => Pervasives.compare(x, y)
6+
let generic_compare = Pervasives.compare
7+
let bigint_equal = (x: bigint, y) => x == y
8+
let generic_equal = \"="
9+
let bigint_notequal = (x: bigint, y) => x != y
10+
let generic_notequal = \"<>"
11+
let bigint_lessthan = (x: bigint, y) => x < y
12+
let generic_lessthan = \"<"
13+
let bigint_greaterthan = (x: bigint, y) => x > y
14+
let generic_greaterthan = \">"
15+
let bigint_lessequal = (x: bigint, y) => x <= y
16+
let generic_lessequal = \"<="
17+
let bigint_greaterequal = (x: bigint, y) => x >= y
18+
let generic_greaterequal = \">="
19+
20+
let () = {
21+
eq(__LOC__, bigint_compare(Js.Bigint._NaN, Js.Bigint._NaN), 0)
22+
eq(__LOC__, generic_compare(Js.Bigint._NaN, Js.Bigint._NaN), 0)
23+
eq(__LOC__, bigint_compare(Js.Bigint._NaN, -1n), -1)
24+
eq(__LOC__, generic_compare(Js.Bigint._NaN, -1n), -1)
25+
eq(__LOC__, bigint_compare(-1n, Js.Bigint._NaN), 1)
26+
eq(__LOC__, generic_compare(-1n, Js.Bigint._NaN), 1)
27+
eq(__LOC__, bigint_equal(Js.Bigint._NaN, Js.Bigint._NaN), false)
28+
eq(__LOC__, generic_equal(Js.Bigint._NaN, Js.Bigint._NaN), false)
29+
eq(__LOC__, bigint_equal(1n, Js.Bigint._NaN), false)
30+
eq(__LOC__, generic_equal(1n, Js.Bigint._NaN), false)
31+
eq(__LOC__, bigint_equal(Js.Bigint._NaN, 1n), false)
32+
eq(__LOC__, generic_equal(Js.Bigint._NaN, 1n), false)
33+
eq(__LOC__, bigint_notequal(Js.Bigint._NaN, Js.Bigint._NaN), true)
34+
eq(__LOC__, generic_notequal(Js.Bigint._NaN, Js.Bigint._NaN), true)
35+
eq(__LOC__, bigint_notequal(1n, Js.Bigint._NaN), true)
36+
eq(__LOC__, generic_notequal(1n, Js.Bigint._NaN), true)
37+
eq(__LOC__, bigint_notequal(Js.Bigint._NaN, 1n), true)
38+
eq(__LOC__, generic_notequal(Js.Bigint._NaN, 1n), true)
39+
eq(__LOC__, bigint_lessthan(Js.Bigint._NaN, Js.Bigint._NaN), false)
40+
eq(__LOC__, generic_lessthan(Js.Bigint._NaN, Js.Bigint._NaN), false)
41+
eq(__LOC__, bigint_lessthan(1n, Js.Bigint._NaN), false)
42+
eq(__LOC__, generic_lessthan(1n, Js.Bigint._NaN), false)
43+
eq(__LOC__, bigint_lessthan(Js.Bigint._NaN, 1n), false)
44+
eq(__LOC__, generic_lessthan(Js.Bigint._NaN, 1n), false)
45+
eq(__LOC__, bigint_greaterthan(Js.Bigint._NaN, Js.Bigint._NaN), false)
46+
eq(__LOC__, generic_greaterthan(Js.Bigint._NaN, Js.Bigint._NaN), false)
47+
eq(__LOC__, bigint_greaterthan(1n, Js.Bigint._NaN), false)
48+
eq(__LOC__, generic_greaterthan(1n, Js.Bigint._NaN), false)
49+
eq(__LOC__, bigint_greaterthan(Js.Bigint._NaN, 1n), false)
50+
eq(__LOC__, generic_greaterthan(Js.Bigint._NaN, 1n), false)
51+
eq(__LOC__, bigint_lessequal(Js.Bigint._NaN, Js.Bigint._NaN), false)
52+
eq(__LOC__, generic_lessequal(Js.Bigint._NaN, Js.Bigint._NaN), false)
53+
eq(__LOC__, bigint_lessequal(1n, Js.Bigint._NaN), false)
54+
eq(__LOC__, generic_lessequal(1n, Js.Bigint._NaN), false)
55+
eq(__LOC__, bigint_lessequal(Js.Bigint._NaN, 1n), false)
56+
eq(__LOC__, generic_lessequal(Js.Bigint._NaN, 1n), false)
57+
eq(__LOC__, bigint_greaterequal(Js.Bigint._NaN, Js.Bigint._NaN), false)
58+
eq(__LOC__, generic_greaterequal(Js.Bigint._NaN, Js.Bigint._NaN), false)
59+
eq(__LOC__, bigint_greaterequal(1n, Js.Bigint._NaN), false)
60+
eq(__LOC__, generic_greaterequal(1n, Js.Bigint._NaN), false)
61+
eq(__LOC__, bigint_greaterequal(Js.Bigint._NaN, 1n), false)
62+
eq(__LOC__, generic_greaterequal(Js.Bigint._NaN, 1n), false)
63+
}

jscomp/test/build.ninja

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)