Skip to content
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

Fix bigint div/mod to throw native RangeError #7154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 2 additions & 6 deletions compiler/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1657,13 +1657,9 @@ let bigint_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
not (bool (p1 = p2 && String.equal (normalize v1) (normalize v2)))
| _ -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1

let bigint_div ~checked ?comment (e0 : t) (e1 : t) =
if checked then runtime_call Primitive_modules.bigint "div" [e0; e1]
else bigint_op ?comment Div e0 e1
let bigint_div ?comment (e0 : t) (e1 : t) = bigint_op ?comment Div e0 e1

let bigint_mod ~checked ?comment (e0 : t) (e1 : t) =
if checked then runtime_call Primitive_modules.bigint "mod_" [e0; e1]
else bigint_op ?comment Mod e0 e1
let bigint_mod ?comment (e0 : t) (e1 : t) = bigint_op ?comment Mod e0 e1

(* TODO -- alpha conversion
remember to add parens..
Expand Down
4 changes: 2 additions & 2 deletions compiler/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ val bigint_op : ?comment:string -> Js_op.binop -> t -> t -> t

val bigint_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t

val bigint_div : checked:bool -> ?comment:string -> t -> t -> t
val bigint_div : ?comment:string -> t -> t -> t

val bigint_mod : checked:bool -> ?comment:string -> t -> t -> t
val bigint_mod : ?comment:string -> t -> t -> t

val js_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t

Expand Down
4 changes: 2 additions & 2 deletions compiler/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
| _ -> assert false)
| Pdivbigint -> (
match args with
| [e1; e2] -> E.bigint_div ~checked:!Js_config.check_div_by_zero e1 e2
| [e1; e2] -> E.bigint_div e1 e2
| _ -> assert false)
| Pmodint -> (
match args with
| [e1; e2] -> E.int32_mod ~checked:!Js_config.check_div_by_zero e1 e2
| _ -> assert false)
| Pmodbigint -> (
match args with
| [e1; e2] -> E.bigint_mod ~checked:!Js_config.check_div_by_zero e1 e2
| [e1; e2] -> E.bigint_mod e1 e2
| _ -> assert false)
| Ppowbigint -> (
match args with
Expand Down
22 changes: 0 additions & 22 deletions lib/es6/Primitive_bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,9 @@ function max(x, y) {
}
}

function div(x, y) {
if (y === 0n) {
throw {
RE_EXN_ID: "Division_by_zero",
Error: new Error()
};
}
return x / y;
}

function mod_(x, y) {
if (y === 0n) {
throw {
RE_EXN_ID: "Division_by_zero",
Error: new Error()
};
}
return x % y;
}

export {
compare,
min,
max,
div,
mod_,
}
/* No side effect */
22 changes: 0 additions & 22 deletions lib/js/Primitive_bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,7 @@ function max(x, y) {
}
}

function div(x, y) {
if (y === 0n) {
throw {
RE_EXN_ID: "Division_by_zero",
Error: new Error()
};
}
return x / y;
}

function mod_(x, y) {
if (y === 0n) {
throw {
RE_EXN_ID: "Division_by_zero",
Error: new Error()
};
}
return x % y;
}

exports.compare = compare;
exports.min = min;
exports.max = max;
exports.div = div;
exports.mod_ = mod_;
/* No side effect */
18 changes: 0 additions & 18 deletions runtime/Primitive_bigint.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,3 @@ let max = (x: bigint, y: bigint): bigint =>
} else {
y
}

external div: (bigint, bigint) => bigint = "%divbigint"

let div = (x: bigint, y: bigint) =>
if y == 0n {
raise(Division_by_zero)
} else {
div(x, y)
}

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

let mod_ = (x: bigint, y: bigint) =>
if y == 0n {
raise(Division_by_zero)
} else {
mod_(x, y)
}
3 changes: 1 addition & 2 deletions tests/tests/src/core/Core_TempTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as Float from "rescript/lib/es6/Float.js";
import * as $$BigInt from "rescript/lib/es6/BigInt.js";
import * as Option from "rescript/lib/es6/Option.js";
import * as Core_IntlTests from "./intl/Core_IntlTests.mjs";
import * as Primitive_bigint from "rescript/lib/es6/Primitive_bigint.js";
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";

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

console.info("---");

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

console.info("");

Expand Down