Skip to content

Commit 5d675f7

Browse files
Use native bigint type (#207)
* Use native bigint type * Changelog * Update to native built-in methods * Update test output * Fix a doctest * Fix dict error in docs test by upgrading even more * Fix changelog
1 parent 4394682 commit 5d675f7

24 files changed

+133
-83
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Next version
44

5+
- BREAKING: Use new native `bigint` type. This requires ReScript compiler version "11.1.0-rc.6" or higher. https://github.com/rescript-association/rescript-core/pull/207
6+
57
## 1.2.0
68

79
- Add optional arguments to `JSON.stringify` and `JSON.parseExn` and deprecate `JSON.stringifyWithIndent`, `JSON.stringifyWithReplacer`, `JSON.parseExnWithReviver` etc. https://github.com/rescript-association/rescript-core/pull/201

package-lock.json

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"src/**/*.mjs"
2525
],
2626
"peerDependencies": {
27-
"rescript": ">=11.0.0 || ^11.1.0-rc.2"
27+
"rescript": "^11.1.0-rc.7"
2828
},
2929
"devDependencies": {
3030
"@babel/code-frame": "7.18.6",
3131
"@rescript/tools": "^0.5.0",
32-
"rescript": "11.1.0-rc.2"
32+
"rescript": "^11.1.0-rc.7"
3333
}
34-
}
34+
}

scripts/DocTests.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var bscBin = Path.join(compilerDir, "node_modules", ".bin", "bsc");
4848
var rescriptCoreCompiled = Path.join(compilerDir, "node_modules", "@rescript", "core", "lib", "ocaml");
4949

5050
function makePackageJson(coreVersion) {
51-
return "{\n \"name\": \"test-compiler-examples\",\n \"version\": \"1.0.0\",\n \"dependencies\": {\n \"@rescript/core\": \"file:rescript-core-" + coreVersion + ".tgz\",\n \"rescript\": \"^11.0.1\"\n }\n}\n";
51+
return "{\n \"name\": \"test-compiler-examples\",\n \"version\": \"1.0.0\",\n \"dependencies\": {\n \"@rescript/core\": \"file:rescript-core-" + coreVersion + ".tgz\",\n \"rescript\": \"11.1.0-rc.7\"\n }\n}\n";
5252
}
5353

5454
var rescriptJson = "{\n \"name\": \"dummy\",\n \"sources\": {\n \"dir\": \"dummy\",\n \"subdirs\": true\n },\n \"bs-dependencies\": [\n \"@rescript/core\"\n ],\n \"bsc-flags\": [\n \"-open RescriptCore\"\n ]\n}";
@@ -65,7 +65,7 @@ function prepareCompiler() {
6565
stdio: "ignore",
6666
cwd: compilerDir
6767
});
68-
var dict = JSON.parse(Fs.readFileSync(Path.join(corePath, "package.json")), undefined);
68+
var dict = JSON.parse(Fs.readFileSync(Path.join(corePath, "package.json")));
6969
var currentCoreVersion;
7070
if (!Array.isArray(dict) && (dict === null || typeof dict !== "object") && typeof dict !== "number" && typeof dict !== "string" && typeof dict !== "boolean") {
7171
throw {
@@ -179,7 +179,7 @@ function extractDocFromFile(file) {
179179
"doc",
180180
file
181181
]);
182-
return Tools_Docgen.decodeFromJson(JSON.parse(spawn.stdout.toString(), undefined));
182+
return Tools_Docgen.decodeFromJson(JSON.parse(spawn.stdout.toString()));
183183
}
184184

185185
function getExamples(param) {

scripts/DocTests.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ let makePackageJson = coreVersion =>
8989
"version": "1.0.0",
9090
"dependencies": {
9191
"@rescript/core": "file:rescript-core-${coreVersion}.tgz",
92-
"rescript": "^11.0.1"
92+
"rescript": "11.1.0-rc.7"
9393
}
9494
}
9595
`

src/Core__BigInt.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ function toInt(t) {
55
return Number(t) | 0;
66
}
77

8-
function exp(x, y) {
9-
return (x ** y);
8+
function lnot(x) {
9+
return x ^ -1n;
1010
}
1111

1212
export {
1313
toInt ,
14-
exp ,
14+
lnot ,
1515
}
1616
/* No side effect */

src/Core__BigInt.res

+83-29
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,95 @@
1-
type t = Js.Types.bigint_val
1+
@val external asIntN: (~width: int, bigint) => bigint = "BigInt.asIntN"
2+
@val external asUintN: (~width: int, bigint) => bigint = "BigInt.asUintN"
23

3-
@val external asIntN: (~width: int, t) => t = "BigInt.asIntN"
4-
@val external asUintN: (~width: int, t) => t = "BigInt.asUintN"
4+
@val external fromString: string => bigint = "BigInt"
55

6-
@val external fromString: string => t = "BigInt"
7-
@val external fromInt: int => t = "BigInt"
8-
@val external fromFloat: float => t = "BigInt"
6+
@val
7+
/**
8+
Parses the given `string` into a `bigint` using JavaScript semantics. Return the
9+
number as a `bigint` if successfully parsed. Uncaught syntax exception otherwise.
910
10-
@send external toString: t => string = "toString"
11-
@send external toStringWithRadix: (t, ~radix: int) => string = "toString"
12-
@send external toLocaleString: t => string = "toLocaleString"
11+
## Examples
1312
14-
@val external toFloat: t => float = "Number"
13+
```rescript
14+
/* returns 123n */
15+
BigInt.fromStringExn("123")
16+
17+
/* returns 0n */
18+
BigInt.fromStringExn("")
19+
20+
/* returns 17n */
21+
BigInt.fromStringExn("0x11")
22+
23+
/* returns 3n */
24+
BigInt.fromStringExn("0b11")
25+
26+
/* returns 9n */
27+
BigInt.fromStringExn("0o11")
28+
29+
/* catch exception */
30+
try {
31+
BigInt.fromStringExn("a")
32+
} catch {
33+
| Exn.Error(_error) => 0n
34+
}
35+
```
36+
*/
37+
external fromStringExn: string => bigint = "BigInt"
38+
@val external fromInt: int => bigint = "BigInt"
39+
@val external fromFloat: float => bigint = "BigInt"
40+
41+
@send
42+
/**
43+
Formats a `bigint` as a string. Return a `string` representing the given value.
44+
See [`toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) on MDN.
45+
46+
## Examples
47+
48+
```rescript
49+
/* prints "123" */
50+
Js.BigInt.toString(123n)->Js.log
51+
```
52+
*/
53+
external toString: bigint => string = "toString"
54+
@send external toStringWithRadix: (bigint, ~radix: int) => string = "toString"
55+
56+
@send
57+
/**
58+
Returns a string with a language-sensitive representation of this BigInt value.
59+
60+
## Examples
61+
62+
```rescript
63+
/* prints "123" */
64+
Js.BigInt.toString(123n)->Js.log
65+
```
66+
*/
67+
external toLocaleString: bigint => string = "toLocaleString"
68+
69+
@val external toFloat: bigint => float = "Number"
1570

1671
let toInt = t => t->toFloat->Core__Int.fromFloat
1772

18-
external \"+": (t, t) => t = "%addfloat"
19-
external \"-": (t, t) => t = "%subfloat"
20-
external \"*": (t, t) => t = "%mulfloat"
21-
external \"/": (t, t) => t = "%divfloat"
73+
external \"+": (bigint, bigint) => bigint = "%addbigint"
74+
external \"-": (bigint, bigint) => bigint = "%subbigint"
75+
external \"*": (bigint, bigint) => bigint = "%mulbigint"
76+
external \"/": (bigint, bigint) => bigint = "%divbigint"
77+
external \"~-": bigint => bigint = "%negbigint"
78+
external \"~+": bigint => bigint = "%identity"
79+
external \"**": (bigint, bigint) => bigint = "%powbigint"
2280

23-
external add: (t, t) => t = "%addfloat"
24-
external sub: (t, t) => t = "%subfloat"
25-
external mul: (t, t) => t = "%mulfloat"
26-
external div: (t, t) => t = "%divfloat"
81+
external add: (bigint, bigint) => bigint = "%addfloat"
82+
external sub: (bigint, bigint) => bigint = "%subfloat"
83+
external mul: (bigint, bigint) => bigint = "%mulfloat"
84+
external div: (bigint, bigint) => bigint = "%divfloat"
2785

28-
@noalloc external mod: (t, t) => t = "?fmod_float"
86+
external mod: (bigint, bigint) => bigint = "%modbigint"
2987

30-
external land: (t, t) => t = "%andint"
31-
external lor: (t, t) => t = "%orint"
32-
external lxor: (t, t) => t = "%xorint"
88+
external land: (bigint, bigint) => bigint = "%andbigint"
89+
external lor: (bigint, bigint) => bigint = "%orbigint"
90+
external lxor: (bigint, bigint) => bigint = "%xorbigint"
3391

34-
external lsl: (t, t) => t = "%lslint"
35-
external asr: (t, t) => t = "%asrint"
92+
external lsl: (bigint, bigint) => bigint = "%lslbigint"
93+
external asr: (bigint, bigint) => bigint = "%asrbigint"
3694

37-
let exp = (x: t, y: t) => {
38-
let _ = x
39-
let _ = y
40-
%raw(`x ** y`)
41-
}
95+
let lnot = x => lxor(x, -1n)

src/Core__DataView.res

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ external fromBufferWithRange: (Core__ArrayBuffer.t, ~byteOffset: int, ~length: i
2020
@send external getFloat32: t => float = "getFloat32"
2121
@send external getFloat64: t => float = "getFloat64"
2222

23-
@send external getBigInt64: t => Core__BigInt.t = "getBigInt64"
24-
@send external getBigUint64: t => Core__BigInt.t = "getBigUint64"
23+
@send external getBigInt64: t => bigint = "getBigInt64"
24+
@send external getBigUint64: t => bigint = "getBigUint64"
2525

2626
@send external setInt8: (t, int) => unit = "setInt8"
2727
@send external setUint8: (t, int) => unit = "setUint8"
@@ -33,5 +33,5 @@ external fromBufferWithRange: (Core__ArrayBuffer.t, ~byteOffset: int, ~length: i
3333
@send external setFloat32: (t, float) => unit = "setFloat32"
3434
@send external setFloat64: (t, float) => unit = "setFloat64"
3535

36-
@send external setBigInt64: (t, Core__BigInt.t) => unit = "setBigInt64"
37-
@send external setBigUint64: (t, Core__BigInt.t) => unit = "setBigUint64"
36+
@send external setBigInt64: (t, bigint) => unit = "setBigInt64"
37+
@send external setBigUint64: (t, bigint) => unit = "setBigUint64"

src/Core__Type.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Classify = {
1515
| Object(object)
1616
| Function(function)
1717
| Symbol(Core__Symbol.t)
18-
| BigInt(Core__BigInt.t)
18+
| BigInt(bigint)
1919

2020
@val external _internalClass: 'a => string = "Object.prototype.toString.call"
2121
external _asBool: 'a => bool = "%identity"
@@ -24,7 +24,7 @@ module Classify = {
2424
external _asObject: 'a => object = "%identity"
2525
external _asFunction: 'a => function = "%identity"
2626
external _asSymbol: 'a => Core__Symbol.t = "%identity"
27-
external _asBigInt: 'a => Core__BigInt.t = "%identity"
27+
external _asBigInt: 'a => bigint = "%identity"
2828

2929
let classify = value => {
3030
switch _internalClass(value) {

src/Core__Type.resi

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module Classify: {
5959
| Object(object)
6060
| Function(function)
6161
| Symbol(Core__Symbol.t)
62-
| BigInt(Core__BigInt.t)
62+
| BigInt(bigint)
6363

6464
/**
6565
`classify(anyValue)`

src/intl/Core__Intl__NumberFormat.res

+5-9
Original file line numberDiff line numberDiff line change
@@ -194,20 +194,16 @@ external formatIntToParts: (t, int) => array<numberFormatPart> = "formatToParts"
194194
external formatIntRangeToParts: (t, ~start: int, ~end: int) => array<numberFormatRangePart> =
195195
"formatRange"
196196

197-
@send external formatBigInt: (t, Core__BigInt.t) => string = "format"
197+
@send external formatBigInt: (t, bigint) => string = "format"
198198

199199
@send
200-
external formatBigIntRange: (t, ~start: Core__BigInt.t, ~end: Core__BigInt.t) => array<string> =
201-
"formatRange"
200+
external formatBigIntRange: (t, ~start: bigint, ~end: bigint) => array<string> = "formatRange"
202201
@send
203-
external formatBigIntToParts: (t, Core__BigInt.t) => array<numberFormatPart> = "formatToParts"
202+
external formatBigIntToParts: (t, bigint) => array<numberFormatPart> = "formatToParts"
204203

205204
@send
206-
external formatBigIntRangeToParts: (
207-
t,
208-
~start: Core__BigInt.t,
209-
~end: Core__BigInt.t,
210-
) => array<numberFormatPart> = "formatRange"
205+
external formatBigIntRangeToParts: (t, ~start: bigint, ~end: bigint) => array<numberFormatPart> =
206+
"formatRange"
211207

212208
@send external formatString: (t, string) => string = "format"
213209

src/intl/Core__Intl__PluralRules.res

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type rule = [#zero | #one | #two | #few | #many | #other]
5050

5151
@send external select: (t, float) => rule = "select"
5252
@send external selectInt: (t, int) => rule = "select"
53-
@send external selectBigInt: (t, Core__BigInt.t) => rule = "select"
53+
@send external selectBigInt: (t, bigint) => rule = "select"
5454

5555
@send
5656
external selectRange: (t, ~start: float, ~end: float) => rule = "selectRange"
@@ -59,5 +59,4 @@ external selectRange: (t, ~start: float, ~end: float) => rule = "selectRange"
5959
external selectRangeInt: (t, ~start: int, ~end: int) => rule = "selectRange"
6060

6161
@send
62-
external selectRangeBigInt: (t, ~start: Core__BigInt.t, ~end: Core__BigInt.t) => rule =
63-
"selectRange"
62+
external selectRangeBigInt: (t, ~start: bigint, ~end: bigint) => rule = "selectRange"

src/typed-arrays/Core__BigInt64Array.res

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** The `BigInt64Array` typed array represents an array of 64-bit signed integers in platform byte order. See [BigInt64Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array)
22
*/
3-
type t = Core__TypedArray.t<Core__BigInt.t>
3+
type t = Core__TypedArray.t<bigint>
44

55
module Constants = {
66
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)
@@ -12,7 +12,7 @@ module Constants = {
1212
/** `fromArray` creates a `BigInt64Array` from an array of values. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
1313
*/
1414
@new
15-
external fromArray: array<Core__BigInt.t> => t = "BigInt64Array"
15+
external fromArray: array<bigint> => t = "BigInt64Array"
1616

1717
/** `fromBuffer` creates a `BigInt64Array` from an `ArrayBuffer.t`. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
1818
@@ -51,5 +51,4 @@ external fromArrayLikeOrIterable: 'a => t = "BigInt64Array.from"
5151
/** `fromArrayLikeOrIterableWithMap` creates a `BigInt64Array` from an array-like or iterable object and applies the mapping function to each item. The mapping function expects (value, index). See [TypedArray.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from)
5252
*/
5353
@val
54-
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => Core__BigInt.t) => t =
55-
"BigInt64Array.from"
54+
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => bigint) => t = "BigInt64Array.from"

src/typed-arrays/Core__BigUint64Array.res

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** The `BigUint64Array` typed array represents an array of 64-bit unsigned integers in platform byte order. See [BigUint64Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array)
22
*/
3-
type t = Core__TypedArray.t<Core__BigInt.t>
3+
type t = Core__TypedArray.t<bigint>
44

55
module Constants = {
66
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)
@@ -12,7 +12,7 @@ module Constants = {
1212
/** `fromArray` creates a `BigUint64Array` from an array of values. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array/BigUint64Array)
1313
*/
1414
@new
15-
external fromArray: array<Core__BigInt.t> => t = "BigUint64Array"
15+
external fromArray: array<bigint> => t = "BigUint64Array"
1616

1717
/** `fromBuffer` creates a `BigUint64Array` from an `ArrayBuffer.t`. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array/BigUint64Array)
1818
@@ -51,5 +51,4 @@ external fromArrayLikeOrIterable: 'a => t = "BigUint64Array.from"
5151
/** `fromArrayLikeOrIterableWithMap` creates a `BigUint64Array` from an array-like or iterable object and applies the mapping function to each item. The mapping function expects (value, index). See [TypedArray.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from)
5252
*/
5353
@val
54-
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => Core__BigInt.t) => t =
55-
"BigUint64Array.from"
54+
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => bigint) => t = "BigUint64Array.from"

test/ObjectTests.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ Test.run([
386386
32
387387
],
388388
"is: bigint"
389-
], Caml_obj.equal(BigInt("123"), BigInt("123")), eq, true);
389+
], BigInt("123") === BigInt("123"), eq, true);
390390

391391
Test.run([
392392
[

test/TempTests.mjs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Core__Int from "../src/Core__Int.mjs";
44
import * as IntlTests from "./intl/IntlTests.mjs";
55
import * as Core__Dict from "../src/Core__Dict.mjs";
66
import * as Core__JSON from "../src/Core__JSON.mjs";
7+
import * as Caml_bigint from "rescript/lib/es6/caml_bigint.js";
78
import * as Caml_option from "rescript/lib/es6/caml_option.js";
89
import * as Core__Array from "../src/Core__Array.mjs";
910
import * as Core__Float from "../src/Core__Float.mjs";
@@ -98,7 +99,7 @@ console.info("JSON");
9899

99100
console.info("---");
100101

101-
var json = JSON.parse("{\"foo\": \"bar\"}", undefined);
102+
var json = JSON.parse("{\"foo\": \"bar\"}");
102103

103104
var json$1 = Core__JSON.Classify.classify(json);
104105

@@ -143,7 +144,7 @@ console.info("BigInt");
143144

144145
console.info("---");
145146

146-
console.log(BigInt(1) / BigInt(12.0));
147+
console.log(Caml_bigint.div(BigInt(1), BigInt(12.0)));
147148

148149
console.info("");
149150

0 commit comments

Comments
 (0)