Skip to content

Commit

Permalink
Use native bigint type (#207)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fhammerschmidt authored Apr 3, 2024
1 parent 4394682 commit 5d675f7
Show file tree
Hide file tree
Showing 24 changed files with 133 additions and 83 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Next version

- 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

## 1.2.0

- 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
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"src/**/*.mjs"
],
"peerDependencies": {
"rescript": ">=11.0.0 || ^11.1.0-rc.2"
"rescript": "^11.1.0-rc.7"
},
"devDependencies": {
"@babel/code-frame": "7.18.6",
"@rescript/tools": "^0.5.0",
"rescript": "11.1.0-rc.2"
"rescript": "^11.1.0-rc.7"
}
}
}
6 changes: 3 additions & 3 deletions scripts/DocTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var bscBin = Path.join(compilerDir, "node_modules", ".bin", "bsc");
var rescriptCoreCompiled = Path.join(compilerDir, "node_modules", "@rescript", "core", "lib", "ocaml");

function makePackageJson(coreVersion) {
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";
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";
}

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}";
Expand All @@ -65,7 +65,7 @@ function prepareCompiler() {
stdio: "ignore",
cwd: compilerDir
});
var dict = JSON.parse(Fs.readFileSync(Path.join(corePath, "package.json")), undefined);
var dict = JSON.parse(Fs.readFileSync(Path.join(corePath, "package.json")));
var currentCoreVersion;
if (!Array.isArray(dict) && (dict === null || typeof dict !== "object") && typeof dict !== "number" && typeof dict !== "string" && typeof dict !== "boolean") {
throw {
Expand Down Expand Up @@ -179,7 +179,7 @@ function extractDocFromFile(file) {
"doc",
file
]);
return Tools_Docgen.decodeFromJson(JSON.parse(spawn.stdout.toString(), undefined));
return Tools_Docgen.decodeFromJson(JSON.parse(spawn.stdout.toString()));
}

function getExamples(param) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/DocTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ let makePackageJson = coreVersion =>
"version": "1.0.0",
"dependencies": {
"@rescript/core": "file:rescript-core-${coreVersion}.tgz",
"rescript": "^11.0.1"
"rescript": "11.1.0-rc.7"
}
}
`
Expand Down
6 changes: 3 additions & 3 deletions src/Core__BigInt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ function toInt(t) {
return Number(t) | 0;
}

function exp(x, y) {
return (x ** y);
function lnot(x) {
return x ^ -1n;
}

export {
toInt ,
exp ,
lnot ,
}
/* No side effect */
112 changes: 83 additions & 29 deletions src/Core__BigInt.res
Original file line number Diff line number Diff line change
@@ -1,41 +1,95 @@
type t = Js.Types.bigint_val
@val external asIntN: (~width: int, bigint) => bigint = "BigInt.asIntN"
@val external asUintN: (~width: int, bigint) => bigint = "BigInt.asUintN"

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

@val external fromString: string => t = "BigInt"
@val external fromInt: int => t = "BigInt"
@val external fromFloat: float => t = "BigInt"
@val
/**
Parses the given `string` into a `bigint` using JavaScript semantics. Return the
number as a `bigint` if successfully parsed. Uncaught syntax exception otherwise.
@send external toString: t => string = "toString"
@send external toStringWithRadix: (t, ~radix: int) => string = "toString"
@send external toLocaleString: t => string = "toLocaleString"
## Examples
@val external toFloat: t => float = "Number"
```rescript
/* returns 123n */
BigInt.fromStringExn("123")
/* returns 0n */
BigInt.fromStringExn("")
/* returns 17n */
BigInt.fromStringExn("0x11")
/* returns 3n */
BigInt.fromStringExn("0b11")
/* returns 9n */
BigInt.fromStringExn("0o11")
/* catch exception */
try {
BigInt.fromStringExn("a")
} catch {
| Exn.Error(_error) => 0n
}
```
*/
external fromStringExn: string => bigint = "BigInt"
@val external fromInt: int => bigint = "BigInt"
@val external fromFloat: float => bigint = "BigInt"

@send
/**
Formats a `bigint` as a string. Return a `string` representing the given value.
See [`toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) on MDN.
## Examples
```rescript
/* prints "123" */
Js.BigInt.toString(123n)->Js.log
```
*/
external toString: bigint => string = "toString"
@send external toStringWithRadix: (bigint, ~radix: int) => string = "toString"

@send
/**
Returns a string with a language-sensitive representation of this BigInt value.
## Examples
```rescript
/* prints "123" */
Js.BigInt.toString(123n)->Js.log
```
*/
external toLocaleString: bigint => string = "toLocaleString"

@val external toFloat: bigint => float = "Number"

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

external \"+": (t, t) => t = "%addfloat"
external \"-": (t, t) => t = "%subfloat"
external \"*": (t, t) => t = "%mulfloat"
external \"/": (t, t) => t = "%divfloat"
external \"+": (bigint, bigint) => bigint = "%addbigint"
external \"-": (bigint, bigint) => bigint = "%subbigint"
external \"*": (bigint, bigint) => bigint = "%mulbigint"
external \"/": (bigint, bigint) => bigint = "%divbigint"
external \"~-": bigint => bigint = "%negbigint"
external \"~+": bigint => bigint = "%identity"
external \"**": (bigint, bigint) => bigint = "%powbigint"

external add: (t, t) => t = "%addfloat"
external sub: (t, t) => t = "%subfloat"
external mul: (t, t) => t = "%mulfloat"
external div: (t, t) => t = "%divfloat"
external add: (bigint, bigint) => bigint = "%addfloat"
external sub: (bigint, bigint) => bigint = "%subfloat"
external mul: (bigint, bigint) => bigint = "%mulfloat"
external div: (bigint, bigint) => bigint = "%divfloat"

@noalloc external mod: (t, t) => t = "?fmod_float"
external mod: (bigint, bigint) => bigint = "%modbigint"

external land: (t, t) => t = "%andint"
external lor: (t, t) => t = "%orint"
external lxor: (t, t) => t = "%xorint"
external land: (bigint, bigint) => bigint = "%andbigint"
external lor: (bigint, bigint) => bigint = "%orbigint"
external lxor: (bigint, bigint) => bigint = "%xorbigint"

external lsl: (t, t) => t = "%lslint"
external asr: (t, t) => t = "%asrint"
external lsl: (bigint, bigint) => bigint = "%lslbigint"
external asr: (bigint, bigint) => bigint = "%asrbigint"

let exp = (x: t, y: t) => {
let _ = x
let _ = y
%raw(`x ** y`)
}
let lnot = x => lxor(x, -1n)
8 changes: 4 additions & 4 deletions src/Core__DataView.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ external fromBufferWithRange: (Core__ArrayBuffer.t, ~byteOffset: int, ~length: i
@send external getFloat32: t => float = "getFloat32"
@send external getFloat64: t => float = "getFloat64"

@send external getBigInt64: t => Core__BigInt.t = "getBigInt64"
@send external getBigUint64: t => Core__BigInt.t = "getBigUint64"
@send external getBigInt64: t => bigint = "getBigInt64"
@send external getBigUint64: t => bigint = "getBigUint64"

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

@send external setBigInt64: (t, Core__BigInt.t) => unit = "setBigInt64"
@send external setBigUint64: (t, Core__BigInt.t) => unit = "setBigUint64"
@send external setBigInt64: (t, bigint) => unit = "setBigInt64"
@send external setBigUint64: (t, bigint) => unit = "setBigUint64"
4 changes: 2 additions & 2 deletions src/Core__Type.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Classify = {
| Object(object)
| Function(function)
| Symbol(Core__Symbol.t)
| BigInt(Core__BigInt.t)
| BigInt(bigint)

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

let classify = value => {
switch _internalClass(value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Core__Type.resi
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module Classify: {
| Object(object)
| Function(function)
| Symbol(Core__Symbol.t)
| BigInt(Core__BigInt.t)
| BigInt(bigint)

/**
`classify(anyValue)`
Expand Down
14 changes: 5 additions & 9 deletions src/intl/Core__Intl__NumberFormat.res
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,16 @@ external formatIntToParts: (t, int) => array<numberFormatPart> = "formatToParts"
external formatIntRangeToParts: (t, ~start: int, ~end: int) => array<numberFormatRangePart> =
"formatRange"

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

@send
external formatBigIntRange: (t, ~start: Core__BigInt.t, ~end: Core__BigInt.t) => array<string> =
"formatRange"
external formatBigIntRange: (t, ~start: bigint, ~end: bigint) => array<string> = "formatRange"
@send
external formatBigIntToParts: (t, Core__BigInt.t) => array<numberFormatPart> = "formatToParts"
external formatBigIntToParts: (t, bigint) => array<numberFormatPart> = "formatToParts"

@send
external formatBigIntRangeToParts: (
t,
~start: Core__BigInt.t,
~end: Core__BigInt.t,
) => array<numberFormatPart> = "formatRange"
external formatBigIntRangeToParts: (t, ~start: bigint, ~end: bigint) => array<numberFormatPart> =
"formatRange"

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

Expand Down
5 changes: 2 additions & 3 deletions src/intl/Core__Intl__PluralRules.res
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type rule = [#zero | #one | #two | #few | #many | #other]

@send external select: (t, float) => rule = "select"
@send external selectInt: (t, int) => rule = "select"
@send external selectBigInt: (t, Core__BigInt.t) => rule = "select"
@send external selectBigInt: (t, bigint) => rule = "select"

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

@send
external selectRangeBigInt: (t, ~start: Core__BigInt.t, ~end: Core__BigInt.t) => rule =
"selectRange"
external selectRangeBigInt: (t, ~start: bigint, ~end: bigint) => rule = "selectRange"
7 changes: 3 additions & 4 deletions src/typed-arrays/Core__BigInt64Array.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** 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)
*/
type t = Core__TypedArray.t<Core__BigInt.t>
type t = Core__TypedArray.t<bigint>

module Constants = {
/**`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)
Expand All @@ -12,7 +12,7 @@ module Constants = {
/** `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)
*/
@new
external fromArray: array<Core__BigInt.t> => t = "BigInt64Array"
external fromArray: array<bigint> => t = "BigInt64Array"

/** `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)
Expand Down Expand Up @@ -51,5 +51,4 @@ external fromArrayLikeOrIterable: 'a => t = "BigInt64Array.from"
/** `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)
*/
@val
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => Core__BigInt.t) => t =
"BigInt64Array.from"
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => bigint) => t = "BigInt64Array.from"
7 changes: 3 additions & 4 deletions src/typed-arrays/Core__BigUint64Array.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** 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)
*/
type t = Core__TypedArray.t<Core__BigInt.t>
type t = Core__TypedArray.t<bigint>

module Constants = {
/**`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)
Expand All @@ -12,7 +12,7 @@ module Constants = {
/** `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)
*/
@new
external fromArray: array<Core__BigInt.t> => t = "BigUint64Array"
external fromArray: array<bigint> => t = "BigUint64Array"

/** `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)
Expand Down Expand Up @@ -51,5 +51,4 @@ external fromArrayLikeOrIterable: 'a => t = "BigUint64Array.from"
/** `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)
*/
@val
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => Core__BigInt.t) => t =
"BigUint64Array.from"
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => bigint) => t = "BigUint64Array.from"
2 changes: 1 addition & 1 deletion test/ObjectTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ Test.run([
32
],
"is: bigint"
], Caml_obj.equal(BigInt("123"), BigInt("123")), eq, true);
], BigInt("123") === BigInt("123"), eq, true);

Test.run([
[
Expand Down
5 changes: 3 additions & 2 deletions test/TempTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Core__Int from "../src/Core__Int.mjs";
import * as IntlTests from "./intl/IntlTests.mjs";
import * as Core__Dict from "../src/Core__Dict.mjs";
import * as Core__JSON from "../src/Core__JSON.mjs";
import * as Caml_bigint from "rescript/lib/es6/caml_bigint.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";
import * as Core__Array from "../src/Core__Array.mjs";
import * as Core__Float from "../src/Core__Float.mjs";
Expand Down Expand Up @@ -98,7 +99,7 @@ console.info("JSON");

console.info("---");

var json = JSON.parse("{\"foo\": \"bar\"}", undefined);
var json = JSON.parse("{\"foo\": \"bar\"}");

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

Expand Down Expand Up @@ -143,7 +144,7 @@ console.info("BigInt");

console.info("---");

console.log(BigInt(1) / BigInt(12.0));
console.log(Caml_bigint.div(BigInt(1), BigInt(12.0)));

console.info("");

Expand Down
Loading

0 comments on commit 5d675f7

Please sign in to comment.