Skip to content

Add BigInt bindings #5350

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

Closed
wants to merge 6 commits into from
Closed
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
188 changes: 95 additions & 93 deletions jscomp/main/builtin_cmi_datasets.ml

Large diffs are not rendered by default.

78 changes: 40 additions & 38 deletions jscomp/main/builtin_cmj_datasets.ml

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions jscomp/others/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
type 'a t = < .. > as 'a
(**/**)

(* internal types for FFI, these types are not used by normal users
(* internal types for FFI, these types are not used by normal users
Absent cmi file when looking up module alias.
*)
module Fn = struct
type 'a arity0 = {
i0 : unit -> 'a [@internal]
i0 : unit -> 'a [@internal]
}
type 'a arity1 = {
i1 : 'a [@internal]
Expand Down Expand Up @@ -123,15 +123,15 @@ end

(**/**)
module MapperRt = Js_mapperRt
module Internal = struct
open Fn
module Internal = struct
open Fn
external opaqueFullApply : 'a -> 'a = "%uncurried_apply"

(* Use opaque instead of [._n] to prevent some optimizations happening *)
external run : 'a arity0 -> 'a = "#run"
external run : 'a arity0 -> 'a = "#run"
external opaque : 'a -> 'a = "%opaque"

end
end
(**/**)


Expand Down Expand Up @@ -303,3 +303,6 @@ module List = Js_list
module Vector = Js_vector

module Console = Js_console

module BigInt = Js_bigint
(** Provide bindings for BigInt *)
34 changes: 34 additions & 0 deletions jscomp/others/js_bigint.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(** JavaScript BigInt API *)

type t

external ofString : string -> t = "BigInt" [@@bs.val]
external ofInt : int -> t = "BigInt" [@@bs.val]
external ofFloat : float -> t = "BigInt" [@@bs.val]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is dangerous enough that I think we may not want to include it.

The ReScript expression ofFloat(99999999999999999999999999999.) for example will produce the value 99999999999999991433150857216n as the float literal will be truncated by the JS environment.

Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What OCaml version is being used to compile ReScript nowadays? Newer OCaml versions have an 'alert' feature that trigger a warning if you use some piece of code. E.g. I'm doing that in https://github.com/yawaramin/ocaml-decimal/blob/08c57183c8673b5058bd6010570e69f0201c03c7/lib/decimal.mli#L321

If that's not available, perhaps we can mark this function as deprecated with the @deprecated tag.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


external toString : t -> string = "toString" [@@bs.send]
external toStringWithRadix : t -> radix:int -> string = "toString" [@@bs.send]

external (+) : t -> t -> t = "%addfloat"
external (-) : t -> t -> t = "%subfloat"
external ( * ) : t -> t -> t = "%mulfloat"
external (/) : t -> t -> t = "%divfloat"

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 (mod) : t -> t -> t = "caml_fmod_float" [@@noalloc]

external (land) : t -> t -> t = "%andint"
external (lor) : t -> t -> t = "%orint"
external (lxor) : t -> t -> t = "%xorint"

external (lsl) : t -> t -> t = "%lslint"
external (asr) : t -> t -> t = "%asrint"

let exp x y =
let _ = x in
let _ = y in
[%raw "x ** y"] [@@inline]
3 changes: 2 additions & 1 deletion jscomp/others/release.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ o others/node.cmj others/node.cmi : cc others/node.ml | $bsc others/js.cmi other
o others/js_OO.cmi others/js_OO.cmj : cc others/js_OO.ml | others/belt_internals.cmi others/js.cmi $bsc
o others/js_array.cmi others/js_array.cmj : cc others/js_array.ml | others/belt_internals.cmi others/js.cmi others/js_array2.cmj $bsc
o others/js_array2.cmi others/js_array2.cmj : cc others/js_array2.ml | others/belt_internals.cmi others/js.cmi $bsc
o others/js_bigint.cmi others/js_bigint.cmj : cc others/js_bigint.ml | others/belt_internals.cmi others/js.cmi $bsc
o others/js_cast.cmj : cc_cmi others/js_cast.ml | others/belt_internals.cmi others/js.cmi others/js_cast.cmi $bsc
o others/js_cast.cmi : cc others/js_cast.mli | others/belt_internals.cmi others/js.cmi $bsc
o others/js_console.cmi others/js_console.cmj : cc others/js_console.ml | others/belt_internals.cmi others/js.cmi $bsc
Expand Down Expand Up @@ -58,7 +59,7 @@ o others/js_undefined.cmj : cc_cmi others/js_undefined.ml | others/belt_internal
o others/js_undefined.cmi : cc others/js_undefined.mli | others/belt_internals.cmi others/js.cmi others/js.cmj $bsc
o others/js_vector.cmj : cc_cmi others/js_vector.ml | others/belt_internals.cmi others/js.cmi others/js_array2.cmj others/js_vector.cmi $bsc
o others/js_vector.cmi : cc others/js_vector.mli | others/belt_internals.cmi others/js.cmi $bsc
o js_pkg : phony others/js_OO.cmi others/js_OO.cmj others/js_array.cmi others/js_array.cmj others/js_array2.cmi others/js_array2.cmj others/js_cast.cmi others/js_cast.cmj others/js_console.cmi others/js_console.cmj others/js_date.cmi others/js_date.cmj others/js_dict.cmi others/js_dict.cmj others/js_exn.cmi others/js_exn.cmj others/js_float.cmi others/js_float.cmj others/js_global.cmi others/js_global.cmj others/js_int.cmi others/js_int.cmj others/js_json.cmi others/js_json.cmj others/js_list.cmi others/js_list.cmj others/js_mapperRt.cmi others/js_mapperRt.cmj others/js_math.cmi others/js_math.cmj others/js_null.cmi others/js_null.cmj others/js_null_undefined.cmi others/js_null_undefined.cmj others/js_obj.cmi others/js_obj.cmj others/js_option.cmi others/js_option.cmj others/js_promise.cmi others/js_promise.cmj others/js_re.cmi others/js_re.cmj others/js_result.cmi others/js_result.cmj others/js_string.cmi others/js_string.cmj others/js_string2.cmi others/js_string2.cmj others/js_typed_array.cmi others/js_typed_array.cmj others/js_typed_array2.cmi others/js_typed_array2.cmj others/js_types.cmi others/js_types.cmj others/js_undefined.cmi others/js_undefined.cmj others/js_vector.cmi others/js_vector.cmj
o js_pkg : phony others/js_OO.cmi others/js_OO.cmj others/js_array.cmi others/js_array.cmj others/js_array2.cmi others/js_array2.cmj others/js_bigint.cmi others/js_bigint.cmj others/js_cast.cmi others/js_cast.cmj others/js_console.cmi others/js_console.cmj others/js_date.cmi others/js_date.cmj others/js_dict.cmi others/js_dict.cmj others/js_exn.cmi others/js_exn.cmj others/js_float.cmi others/js_float.cmj others/js_global.cmi others/js_global.cmj others/js_int.cmi others/js_int.cmj others/js_json.cmi others/js_json.cmj others/js_list.cmi others/js_list.cmj others/js_mapperRt.cmi others/js_mapperRt.cmj others/js_math.cmi others/js_math.cmj others/js_null.cmi others/js_null.cmj others/js_null_undefined.cmi others/js_null_undefined.cmj others/js_obj.cmi others/js_obj.cmj others/js_option.cmi others/js_option.cmj others/js_promise.cmi others/js_promise.cmj others/js_re.cmi others/js_re.cmj others/js_result.cmi others/js_result.cmj others/js_string.cmi others/js_string.cmj others/js_string2.cmi others/js_string2.cmj others/js_typed_array.cmi others/js_typed_array.cmj others/js_typed_array2.cmi others/js_typed_array2.cmj others/js_types.cmi others/js_types.cmj others/js_undefined.cmi others/js_undefined.cmj others/js_vector.cmi others/js_vector.cmj
o others/belt_Array.cmj : cc_cmi others/belt_Array.ml | others/belt.cmi others/belt_Array.cmi others/belt_internals.cmi others/js.cmi others/js.cmj others/js_math.cmj $bsc js_pkg
o others/belt_Array.cmi : cc others/belt_Array.mli | others/belt.cmi others/belt_internals.cmi others/js.cmi others/js.cmj $bsc js_pkg
o others/belt_Float.cmj : cc_cmi others/belt_Float.ml | others/belt.cmi others/belt_Float.cmi others/belt_internals.cmi others/js.cmi $bsc js_pkg
Expand Down
Loading