Skip to content

Commit 3407763

Browse files
committed
throw Division_by_zero in bigint div, mod
1 parent cdf3d94 commit 3407763

17 files changed

+200
-31
lines changed

jscomp/core/js_exp_make.ml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,6 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
787787
float_equal ?comment a e1
788788
| Number (Float { f = f0; _ }), Number (Float { f = f1 }) when f0 = f1 ->
789789
true_
790-
| Number (Bigint { i = i0 }), Number (Bigint { i = i1 }) -> bool (i0 = i1)
791790
| _ -> { expression_desc = Bin (EqEqEq, e0, e1); comment }
792791

793792
let int_equal = float_equal
@@ -1263,10 +1262,19 @@ let rec int32_band ?comment (e1 : J.expression) (e2 : J.expression) :
12631262
let bigint_op ?comment op (e1: t) (e2: t) = bin ?comment op e1 e2
12641263

12651264
let bigint_comp (cmp : Lam_compat.comparison) ?comment (e0: t) (e1: t) =
1266-
match (cmp, e0.expression_desc, e1.expression_desc) with
1267-
| Ceq, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 = i1)
1268-
| Cneq, Number (Bigint {i = i0; _}), Number (Bigint {i = i1; _}) -> bool (i0 <> i1)
1269-
| _ -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
1265+
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
1266+
1267+
let bigint_div ~checked ?comment (e0: t) (e1: t) =
1268+
if checked then
1269+
runtime_call Js_runtime_modules.bigint "div" [e0; e1]
1270+
else
1271+
bigint_op ?comment Div e0 e1
1272+
1273+
let bigint_mod ~checked ?comment (e0: t) (e1: t) =
1274+
if checked then
1275+
runtime_call Js_runtime_modules.bigint "mod_" [e0; e1]
1276+
else
1277+
bigint_op ?comment Mod e0 e1
12701278

12711279
(* TODO -- alpha conversion
12721280
remember to add parens..

jscomp/core/js_exp_make.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ val bigint_op : ?comment: string -> Js_op.binop -> t -> t -> t
280280

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

283+
val bigint_div : checked:bool -> ?comment:string -> t -> t -> t
284+
285+
val bigint_mod : checked:bool -> ?comment:string -> t -> t -> t
286+
283287
val js_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t
284288

285289
val not : t -> t

jscomp/core/lam.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,7 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
506506
(* FIXME: could raise? *)
507507
Lift.bool
508508
(Lam_compat.cmp_float cmp (float_of_string a) (float_of_string b))
509-
| Pbigintcomp cmp, Const_bigint a, Const_bigint b ->
510-
(match cmp with
511-
| Ceq ->
512-
Lift.bool (a = b)
513-
| Cneq ->
514-
Lift.bool (a <> b)
515-
|_ -> default ())
509+
| Pbigintcomp cmp, Const_bigint a, Const_bigint b -> default ()
516510
| Pintcomp ((Ceq | Cneq) as op), Const_pointer a, Const_pointer b ->
517511
Lift.bool
518512
(match op with

jscomp/core/lam_compile_primitive.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,19 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
214214
| [ e1; e2 ] -> E.int32_div ~checked:!Js_config.check_div_by_zero e1 e2
215215
| _ -> assert false)
216216
| Pdivint64 -> Js_long.div args
217-
| Pdivbigint -> (match args with [ e1; e2 ] -> E.bigint_op Div e1 e2 | _ -> assert false)
217+
| Pdivbigint -> (
218+
match args with
219+
| [ e1; e2 ] -> E.bigint_div ~checked:!Js_config.check_div_by_zero e1 e2
220+
| _ -> assert false)
218221
| Pmodint -> (
219222
match args with
220223
| [ e1; e2 ] -> E.int32_mod ~checked:!Js_config.check_div_by_zero e1 e2
221224
| _ -> assert false)
222225
| Pmodint64 -> Js_long.mod_ args
223-
| Pmodbigint -> (match args with [ e1; e2 ] -> E.bigint_op Mod e1 e2 | _ -> assert false)
226+
| Pmodbigint -> (
227+
match args with
228+
| [ e1; e2 ] -> E.bigint_mod ~checked:!Js_config.check_div_by_zero e1 e2
229+
| _ -> assert false)
224230
| Ppowbigint -> (match args with [ e1; e2 ] -> E.bigint_op Pow e1 e2 | _ -> assert false)
225231
| Plslint -> (
226232
match args with [ e1; e2 ] -> E.int32_lsl e1 e2 | _ -> assert false)

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
252252
match args with
253253
| [ e1; e2 ] -> E.unchecked_int32_mul e1 e2
254254
| _ -> assert false)
255+
| "?bigint_div" -> (
256+
match args with
257+
| [ e1; e2 ] -> E.bigint_div e1 e2 ~checked:false
258+
| _ -> assert false)
259+
| "?bigint_mod" -> (
260+
match args with
261+
| [ e1; e2 ] -> E.bigint_mod e1 e2 ~checked:false
262+
| _ -> assert false)
255263
| "?await" -> (
256264
match args with
257265
| [e] -> {e with expression_desc = Await e}

jscomp/ext/js_runtime_modules.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ let md5 = "Caml_md5"
5959

6060
let int32 = "Caml_int32"
6161

62+
let bigint = "Caml_bigint"
63+
6264
let option = "Caml_option"
6365

6466
let module_ = "Caml_module"

jscomp/ml/printlambda.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ let primitive ppf = function
187187
| Pxorbigint -> fprintf ppf "xor"
188188
| Plslbigint -> fprintf ppf "lsl"
189189
| Pasrbigint -> fprintf ppf "asr"
190-
| Pdivbigint Safe -> fprintf ppf "/n"
191-
| Pdivbigint Unsafe -> fprintf ppf "/nu"
190+
| Pdivbigint Safe -> fprintf ppf "/"
191+
| Pdivbigint Unsafe -> fprintf ppf "/u"
192192
| Pmodbigint Safe -> fprintf ppf "mod"
193193
| Pmodbigint Unsafe -> fprintf ppf "mod_unsafe"
194194
| Pbigintcomp(Ceq) -> fprintf ppf "==,"

jscomp/runtime/caml_bigint.res

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24+
25+
let div = (x: bigint, y: bigint) =>
26+
if y == 0n {
27+
raise(Division_by_zero)
28+
} else {
29+
Caml_bigint_extern.div(x, y)
30+
}
31+
32+
let mod_ = (x: bigint, y: bigint) =>
33+
if y == 0n {
34+
raise(Division_by_zero)
35+
} else {
36+
Caml_bigint_extern.rem(x, y)
37+
}

jscomp/runtime/caml_bigint.resi

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24+
25+
let div: (bigint, bigint) => bigint
26+
27+
let mod_: (bigint, bigint) => bigint

jscomp/runtime/caml_bigint_extern.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
external div: (bigint, bigint) => bigint = "?bigint_div"
2+
external rem: (bigint, bigint) => bigint = "?bigint_mod"

jscomp/runtime/release.ninja

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ o runtime/caml.cmj : cc_cmi runtime/caml.res | runtime/caml.cmi runtime/caml_int
1717
o runtime/caml.cmi : cc runtime/caml.resi | runtime/bs_stdlib_mini.cmi runtime/caml_int64_extern.cmj runtime/js.cmi runtime/js.cmj
1818
o runtime/caml_array.cmj : cc_cmi runtime/caml_array.res | runtime/caml_array.cmi runtime/caml_array_extern.cmj
1919
o runtime/caml_array.cmi : cc runtime/caml_array.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
20+
o runtime/caml_bigint.cmj : cc_cmi runtime/caml_bigint.res | runtime/caml_bigint.cmi runtime/caml_bigint_extern.cmj
21+
o runtime/caml_bigint.cmi : cc runtime/caml_bigint.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2022
o runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.res | runtime/caml_bytes.cmi
2123
o runtime/caml_bytes.cmi : cc runtime/caml_bytes.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2224
o runtime/caml_exceptions.cmj : cc_cmi runtime/caml_exceptions.res | runtime/caml_exceptions.cmi runtime/js.cmj
@@ -52,6 +54,7 @@ o runtime/caml_string.cmi : cc runtime/caml_string.resi | runtime/bs_stdlib_mini
5254
o runtime/caml_sys.cmj : cc_cmi runtime/caml_sys.res | runtime/caml_array_extern.cmj runtime/caml_sys.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
5355
o runtime/caml_sys.cmi : cc runtime/caml_sys.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5456
o runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj : cc runtime/caml_array_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
57+
o runtime/caml_bigint_extern.cmi runtime/caml_bigint_extern.cmj : cc runtime/caml_bigint_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5558
o runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj : cc runtime/caml_external_polyfill.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5659
o runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj : cc runtime/caml_float_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5760
o runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj : cc runtime/caml_int64_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
@@ -60,4 +63,4 @@ o runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj : cc runti
6063
o runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6164
o runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6265
o runtime/curry.cmi runtime/curry.cmj : cc runtime/curry.res | runtime/bs_stdlib_mini.cmi runtime/caml_array.cmj runtime/caml_array_extern.cmj runtime/js.cmi runtime/js.cmj
63-
o runtime : phony runtime/bs_stdlib_mini.cmi runtime/js.cmj runtime/js.cmi runtime/caml.cmi runtime/caml.cmj runtime/caml_array.cmi runtime/caml_array.cmj runtime/caml_bytes.cmi runtime/caml_bytes.cmj runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj runtime/caml_float.cmi runtime/caml_float.cmj runtime/caml_format.cmi runtime/caml_format.cmj runtime/caml_hash.cmi runtime/caml_hash.cmj runtime/caml_hash_primitive.cmi runtime/caml_hash_primitive.cmj runtime/caml_int32.cmi runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_int64.cmj runtime/caml_lexer.cmi runtime/caml_lexer.cmj runtime/caml_md5.cmi runtime/caml_md5.cmj runtime/caml_module.cmi runtime/caml_module.cmj runtime/caml_obj.cmi runtime/caml_obj.cmj runtime/caml_option.cmi runtime/caml_option.cmj runtime/caml_parser.cmi runtime/caml_parser.cmj runtime/caml_splice_call.cmi runtime/caml_splice_call.cmj runtime/caml_string.cmi runtime/caml_string.cmj runtime/caml_sys.cmi runtime/caml_sys.cmj runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj runtime/curry.cmi runtime/curry.cmj
66+
o runtime : phony runtime/bs_stdlib_mini.cmi runtime/js.cmj runtime/js.cmi runtime/caml.cmi runtime/caml.cmj runtime/caml_array.cmi runtime/caml_array.cmj runtime/caml_bigint.cmi runtime/caml_bigint.cmj runtime/caml_bytes.cmi runtime/caml_bytes.cmj runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj runtime/caml_float.cmi runtime/caml_float.cmj runtime/caml_format.cmi runtime/caml_format.cmj runtime/caml_hash.cmi runtime/caml_hash.cmj runtime/caml_hash_primitive.cmi runtime/caml_hash_primitive.cmj runtime/caml_int32.cmi runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_int64.cmj runtime/caml_lexer.cmi runtime/caml_lexer.cmj runtime/caml_md5.cmi runtime/caml_md5.cmj runtime/caml_module.cmi runtime/caml_module.cmj runtime/caml_obj.cmi runtime/caml_obj.cmj runtime/caml_option.cmi runtime/caml_option.cmj runtime/caml_parser.cmi runtime/caml_parser.cmj runtime/caml_splice_call.cmi runtime/caml_splice_call.cmj runtime/caml_string.cmi runtime/caml_string.cmj runtime/caml_sys.cmi runtime/caml_sys.cmj runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj runtime/caml_bigint_extern.cmi runtime/caml_bigint_extern.cmj runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj runtime/curry.cmi runtime/curry.cmj

jscomp/test/bigint_test.js

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

lib/es6/caml_bigint.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
4+
function div(x, y) {
5+
if (y === 0n) {
6+
throw {
7+
RE_EXN_ID: "Division_by_zero",
8+
Error: new Error()
9+
};
10+
}
11+
return x / y;
12+
}
13+
14+
function mod_(x, y) {
15+
if (y === 0n) {
16+
throw {
17+
RE_EXN_ID: "Division_by_zero",
18+
Error: new Error()
19+
};
20+
}
21+
return x % y;
22+
}
23+
24+
export {
25+
div ,
26+
mod_ ,
27+
}
28+
/* No side effect */

lib/es6/caml_bigint_extern.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */

lib/js/caml_bigint.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
4+
function div(x, y) {
5+
if (y === 0n) {
6+
throw {
7+
RE_EXN_ID: "Division_by_zero",
8+
Error: new Error()
9+
};
10+
}
11+
return x / y;
12+
}
13+
14+
function mod_(x, y) {
15+
if (y === 0n) {
16+
throw {
17+
RE_EXN_ID: "Division_by_zero",
18+
Error: new Error()
19+
};
20+
}
21+
return x % y;
22+
}
23+
24+
exports.div = div;
25+
exports.mod_ = mod_;
26+
/* No side effect */

lib/js/caml_bigint_extern.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */

packages/artifacts.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ lib/es6/callback.js
6868
lib/es6/caml.js
6969
lib/es6/caml_array.js
7070
lib/es6/caml_array_extern.js
71+
lib/es6/caml_bigint.js
72+
lib/es6/caml_bigint_extern.js
7173
lib/es6/caml_bytes.js
7274
lib/es6/caml_exceptions.js
7375
lib/es6/caml_external_polyfill.js
@@ -231,6 +233,8 @@ lib/js/callback.js
231233
lib/js/caml.js
232234
lib/js/caml_array.js
233235
lib/js/caml_array_extern.js
236+
lib/js/caml_bigint.js
237+
lib/js/caml_bigint_extern.js
234238
lib/js/caml_bytes.js
235239
lib/js/caml_exceptions.js
236240
lib/js/caml_external_polyfill.js

0 commit comments

Comments
 (0)