Skip to content

Commit 6d81a5e

Browse files
committed
Fix bigint div/mod to throw native RangeError
1 parent 7a58f3b commit 6d81a5e

File tree

7 files changed

+7
-74
lines changed

7 files changed

+7
-74
lines changed

compiler/core/js_exp_make.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,13 +1657,9 @@ let bigint_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
16571657
not (bool (p1 = p2 && String.equal (normalize v1) (normalize v2)))
16581658
| _ -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
16591659

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

1664-
let bigint_mod ~checked ?comment (e0 : t) (e1 : t) =
1665-
if checked then runtime_call Primitive_modules.bigint "mod_" [e0; e1]
1666-
else bigint_op ?comment Mod e0 e1
1662+
let bigint_mod ?comment (e0 : t) (e1 : t) = bigint_op ?comment Mod e0 e1
16671663

16681664
(* TODO -- alpha conversion
16691665
remember to add parens..

compiler/core/js_exp_make.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ val bigint_op : ?comment:string -> Js_op.binop -> t -> t -> t
287287

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

290-
val bigint_div : checked:bool -> ?comment:string -> t -> t -> t
290+
val bigint_div : ?comment:string -> t -> t -> t
291291

292-
val bigint_mod : checked:bool -> ?comment:string -> t -> t -> t
292+
val bigint_mod : ?comment:string -> t -> t -> t
293293

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

compiler/core/lam_compile_primitive.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,15 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
240240
| _ -> assert false)
241241
| Pdivbigint -> (
242242
match args with
243-
| [e1; e2] -> E.bigint_div ~checked:!Js_config.check_div_by_zero e1 e2
243+
| [e1; e2] -> E.bigint_div e1 e2
244244
| _ -> assert false)
245245
| Pmodint -> (
246246
match args with
247247
| [e1; e2] -> E.int32_mod ~checked:!Js_config.check_div_by_zero e1 e2
248248
| _ -> assert false)
249249
| Pmodbigint -> (
250250
match args with
251-
| [e1; e2] -> E.bigint_mod ~checked:!Js_config.check_div_by_zero e1 e2
251+
| [e1; e2] -> E.bigint_mod e1 e2
252252
| _ -> assert false)
253253
| Ppowbigint -> (
254254
match args with

lib/es6/Primitive_bigint.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,9 @@ function max(x, y) {
2727
}
2828
}
2929

30-
function div(x, y) {
31-
if (y === 0n) {
32-
throw {
33-
RE_EXN_ID: "Division_by_zero",
34-
Error: new Error()
35-
};
36-
}
37-
return x / y;
38-
}
39-
40-
function mod_(x, y) {
41-
if (y === 0n) {
42-
throw {
43-
RE_EXN_ID: "Division_by_zero",
44-
Error: new Error()
45-
};
46-
}
47-
return x % y;
48-
}
49-
5030
export {
5131
compare,
5232
min,
5333
max,
54-
div,
55-
mod_,
5634
}
5735
/* No side effect */

lib/js/Primitive_bigint.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,7 @@ function max(x, y) {
2727
}
2828
}
2929

30-
function div(x, y) {
31-
if (y === 0n) {
32-
throw {
33-
RE_EXN_ID: "Division_by_zero",
34-
Error: new Error()
35-
};
36-
}
37-
return x / y;
38-
}
39-
40-
function mod_(x, y) {
41-
if (y === 0n) {
42-
throw {
43-
RE_EXN_ID: "Division_by_zero",
44-
Error: new Error()
45-
};
46-
}
47-
return x % y;
48-
}
49-
5030
exports.compare = compare;
5131
exports.min = min;
5232
exports.max = max;
53-
exports.div = div;
54-
exports.mod_ = mod_;
5533
/* No side effect */

runtime/Primitive_bigint.res

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,3 @@ let max = (x: bigint, y: bigint): bigint =>
2020
} else {
2121
y
2222
}
23-
24-
external div: (bigint, bigint) => bigint = "%divbigint"
25-
26-
let div = (x: bigint, y: bigint) =>
27-
if y == 0n {
28-
raise(Division_by_zero)
29-
} else {
30-
div(x, y)
31-
}
32-
33-
external mod_: (bigint, bigint) => bigint = "%modbigint"
34-
35-
let mod_ = (x: bigint, y: bigint) =>
36-
if y == 0n {
37-
raise(Division_by_zero)
38-
} else {
39-
mod_(x, y)
40-
}

tests/tests/src/core/Core_TempTests.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import * as Float from "rescript/lib/es6/Float.js";
88
import * as $$BigInt from "rescript/lib/es6/BigInt.js";
99
import * as Option from "rescript/lib/es6/Option.js";
1010
import * as Core_IntlTests from "./intl/Core_IntlTests.mjs";
11-
import * as Primitive_bigint from "rescript/lib/es6/Primitive_bigint.js";
1211
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
1312

1413
console.info("");
@@ -140,7 +139,7 @@ console.info("BigInt");
140139

141140
console.info("---");
142141

143-
console.log(Primitive_bigint.div(BigInt(1), BigInt(12.0)));
142+
console.log(BigInt(1) / BigInt(12.0));
144143

145144
console.info("");
146145

0 commit comments

Comments
 (0)