Skip to content

Stdlib: rename binary operations to match JavaScript terms #7353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/es6/Stdlib_BigInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ function toInt(t) {
return Number(t) | 0;
}

function lnot(x) {
function bitwiseNot(x) {
return x ^ -1n;
}

export {
toInt,
lnot,
bitwiseNot,
}
/* No side effect */
8 changes: 2 additions & 6 deletions lib/es6/Stdlib_Int.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ function clamp(min, max, value) {
}
}

function lnot(x) {
function bitwiseNot(x) {
return x ^ -1;
}

let Bitwise = {
lnot: lnot
};

let Constants = {
minValue: -2147483648,
maxValue: 2147483647
Expand All @@ -81,6 +77,6 @@ export {
range,
rangeWithOptions,
clamp,
Bitwise,
bitwiseNot,
}
/* No side effect */
4 changes: 2 additions & 2 deletions lib/js/Stdlib_BigInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ function toInt(t) {
return Number(t) | 0;
}

function lnot(x) {
function bitwiseNot(x) {
return x ^ -1n;
}

exports.toInt = toInt;
exports.lnot = lnot;
exports.bitwiseNot = bitwiseNot;
/* No side effect */
8 changes: 2 additions & 6 deletions lib/js/Stdlib_Int.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ function clamp(min, max, value) {
}
}

function lnot(x) {
function bitwiseNot(x) {
return x ^ -1;
}

let Bitwise = {
lnot: lnot
};

let Constants = {
minValue: -2147483648,
maxValue: 2147483647
Expand All @@ -80,5 +76,5 @@ exports.fromString = fromString;
exports.range = range;
exports.rangeWithOptions = rangeWithOptions;
exports.clamp = clamp;
exports.Bitwise = Bitwise;
exports.bitwiseNot = bitwiseNot;
/* No side effect */
13 changes: 7 additions & 6 deletions runtime/Stdlib_BigInt.res
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ external div: (bigint, bigint) => bigint = "%divbigint"

external mod: (bigint, bigint) => bigint = "%modbigint"

external land: (bigint, bigint) => bigint = "%andbigint"
external lor: (bigint, bigint) => bigint = "%orbigint"
external lxor: (bigint, bigint) => bigint = "%xorbigint"
external bitwiseAnd: (bigint, bigint) => bigint = "%andbigint"
external bitwiseOr: (bigint, bigint) => bigint = "%orbigint"
external bitwiseXor: (bigint, bigint) => bigint = "%xorbigint"

external lsl: (bigint, bigint) => bigint = "%lslbigint"
external asr: (bigint, bigint) => bigint = "%asrbigint"
// TODO: make it a primitive
let bitwiseNot = x => bitwiseXor(x, -1n)

let lnot = x => lxor(x, -1n)
external shiftLeft: (bigint, bigint) => bigint = "%lslbigint"
external shiftRight: (bigint, bigint) => bigint = "%asrbigint"

/**
`ignore(bigint)` ignores the provided bigint and returns unit.
Expand Down
17 changes: 8 additions & 9 deletions runtime/Stdlib_Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,15 @@ let clamp = (~min=?, ~max=?, value): int => {
}
}

module Bitwise = {
external land: (int, int) => int = "%andint"
external lor: (int, int) => int = "%orint"
external lxor: (int, int) => int = "%xorint"
external bitwiseAnd: (int, int) => int = "%andint"
external bitwiseOr: (int, int) => int = "%orint"
external bitwiseXor: (int, int) => int = "%xorint"

external lsl: (int, int) => int = "%lslint"
external lsr: (int, int) => int = "%lsrint"
external asr: (int, int) => int = "%asrint"
// TODO: make it a primitive
let bitwiseNot = x => bitwiseXor(x, -1)

let lnot = x => lxor(x, -1)
}
external shiftLeft: (int, int) => int = "%lslint"
external shiftRight: (int, int) => int = "%asrint"
external shiftRightUnsigned: (int, int) => int = "%lsrint"

external ignore: int => unit = "%ignore"
119 changes: 61 additions & 58 deletions runtime/Stdlib_Int.resi
Original file line number Diff line number Diff line change
Expand Up @@ -393,84 +393,87 @@ Int.clamp(42, ~min=50, ~max=40) == 50
*/
let clamp: (~min: int=?, ~max: int=?, int) => int

module Bitwise: {
/**
`land(n1, n2)` calculates the bitwise logical AND of two integers.
/**
`bitwiseAnd(n1, n2)` calculates the bitwise AND of two integers.

## Examples
## Examples

```rescript
Int.Bitwise.land(7, 4) == 4
```
*/
external land: (int, int) => int = "%andint"
```rescript
Int.bitwiseAnd(7, 4) == 4
```
*/
external bitwiseAnd: (int, int) => int = "%andint"

/**
`lor(n1, n2)` calculates the bitwise logical OR of two integers.
/**
`bitwiseOr(n1, n2)` calculates the bitwise OR of two integers.

## Examples
## Examples

```rescript
Int.Bitwise.lor(7, 4) == 7
```
*/
external lor: (int, int) => int = "%orint"
```rescript
Int.bitwiseOr(7, 4) == 7
```
*/
external bitwiseOr: (int, int) => int = "%orint"

/**
`lxor(n1, n2)` calculates the bitwise logical XOR of two integers.
/**
`bigwiseXor(n1, n2)` calculates the bitwise XOR of two integers.

## Examples
## Examples

```rescript
Int.Bitwise.lxor(7, 4) == 3
```
*/
external lxor: (int, int) => int = "%xorint"
```rescript
Int.bitwiseXor(7, 4) == 3
```
*/
external bitwiseXor: (int, int) => int = "%xorint"

/**
`lnot(n)` calculates the bitwise logical NOT of an integer.
/**
`bitwiseNot(n)` calculates the bitwise NOT of an integer.

## Examples
## Examples

```rescript
Int.Bitwise.lnot(2) == -3
```
*/
let lnot: int => int
```rescript
Int.bitwiseNot(2) == -3
```
*/
let bitwiseNot: int => int

/**
`lsl(n, length)` calculates the bitwise logical left shift of an integer `n` by `length`.
/**
`shiftLeft(n, length)` calculates the shifted value of an integer `n` by `length` bits to the left.

## Examples
## Examples

```rescript
Int.Bitwise.lsl(4, 1) == 8
```
*/
external lsl: (int, int) => int = "%lslint"
```rescript
Int.shiftLeft(4, 1) == 8
```
*/
external shiftLeft: (int, int) => int = "%lslint"

/**
`lsr(n, length)` calculates the bitwise logical right shift of an integer `n` by `length`.
/**
`shiftRight(n, length)` calculates the shifted value of an integer `n` by `length` bits to the right.

## Examples
Also known as "arithmetic right shift" operation.

```rescript
Int.Bitwise.lsr(8, 1) == 4
```
*/
external lsr: (int, int) => int = "%lsrint"
## Examples

/**
`asr(n, length)` calculates the bitwise arithmetic right shift of an integer `n` by `length`.
```rescript
Int.shiftRight(8, 1) == 4
```
*/
external shiftRight: (int, int) => int = "%asrint"

## Examples
/**
`shiftRightUnsigned(n, length)` calculates the shifted value of an integer `n` by `length` bits to the right.
Excess bits shifted off to the right are discarded, and zero bits are shifted in from the left.

```rescript
Int.Bitwise.asr(4, 1) == 2
```
*/
external asr: (int, int) => int = "%asrint"
}
Also known as "zero-filling right shift" operation.

## Examples

```rescript
Int.shiftRightUnsigned(4, 1) == 2
```
*/
external shiftRightUnsigned: (int, int) => int = "%lsrint"

/**
`ignore(int)` ignores the provided int and returns unit.
Expand Down
Loading