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

Lines changed: 2 additions & 0 deletions
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

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

package.json

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 83 additions & 29 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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)`

0 commit comments

Comments
 (0)