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 3 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
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 *)
31 changes: 31 additions & 0 deletions jscomp/others/js_bigint.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(** 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 = "BigInt" [@@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" "fmod" [@@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 = [%raw x ** y] [@@inline]