Skip to content

bigInt comparision #6097

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

Merged
merged 17 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ These are only breaking changes for unformatted code.
- Fix issue where `foo(x,_)` in uncurried mode would generate a curried function https://github.com/rescript-lang/rescript-compiler/pull/6082
- Fix printing of uncurried application when the lhs is a function definition https://github.com/rescript-lang/rescript-compiler/pull/6084
- Fix parsing uncurried type starting with path https://github.com/rescript-lang/rescript-compiler/pull/6089
- Fix bigInt comparison https://github.com/rescript-lang/rescript-compiler/pull/6097

#### :nail_care: Polish

Expand Down
32 changes: 17 additions & 15 deletions jscomp/runtime/caml_obj.ml
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,15 @@ let rec compare (a : Obj.t) (b : Obj.t) : int =
raise (Invalid_argument "compare: functional value")
| "function", _ -> 1
| _, "function" -> -1
| "bigint", "bigint" ->
Pervasives.compare (Obj.magic a : float) (Obj.magic b : float)
| "number", "number" ->
Pervasives.compare (Obj.magic a : float) (Obj.magic b : float)
| "number", _ ->
| "bigint", _ | "number", _ ->
if b == Obj.repr Js.null || Caml_option.isNested b then 1
(* Some (Some ..) < x *)
else -1 (* Integer < Block in OCaml runtime GPR #1195, except Some.. *)
| _, "number" ->
| _, "bigint" | _, "number" ->
if a == Obj.repr Js.null || Caml_option.isNested a then -1 else 1
| _ ->
if a == Obj.repr Js.null then
Expand Down Expand Up @@ -282,8 +284,8 @@ let rec equal (a : Obj.t) (b : Obj.t) : bool =
else
let a_type = Js.typeof a in
if
a_type = "string" || a_type = "number" || a_type = "boolean"
|| a_type = "undefined"
a_type = "string" || a_type = "number" || a_type = "bigint"
|| a_type = "boolean" || a_type = "undefined"
|| a == [%raw {|null|}]
then false
else
Expand All @@ -293,7 +295,8 @@ let rec equal (a : Obj.t) (b : Obj.t) : bool =
(* first, check using reference equality *)
else if
(* a_type = "object" || "symbol" *)
b_type = "number" || b_type = "undefined" || b == [%raw {|null|}]
b_type = "number" || b_type = "bigint" || b_type = "undefined"
|| b == [%raw {|null|}]
then false
else
(* [a] [b] could not be null, so it can not raise *)
Expand Down Expand Up @@ -354,29 +357,28 @@ let equal_nullable (x : Obj.t) (y : Obj.t Js.nullable) =
| None -> x == Obj.magic y
| Some y -> equal x y

let isNumberOrBigInt a = Js.typeof a = "number" || Js.typeof a = "bigint"

let canNumericCompare a b = isNumberOrBigInt a && isNumberOrBigInt b

let notequal a b =
if Js.typeof a = "number" && Js.typeof b = "number" then
(Obj.magic a : float) <> (Obj.magic b : float)
if canNumericCompare a b then (Obj.magic a : float) <> (Obj.magic b : float)
else not (equal a b)

let greaterequal a b =
if Js.typeof a = "number" && Js.typeof b = "number" then
(Obj.magic a : float) >= (Obj.magic b : float)
if canNumericCompare a b then (Obj.magic a : float) >= (Obj.magic b : float)
else compare a b >= 0

let greaterthan a b =
if Js.typeof a = "number" && Js.typeof b = "number" then
(Obj.magic a : float) > (Obj.magic b : float)
if canNumericCompare a b then (Obj.magic a : float) > (Obj.magic b : float)
else compare a b > 0

let lessequal a b =
if Js.typeof a = "number" && Js.typeof b = "number" then
(Obj.magic a : float) <= (Obj.magic b : float)
if canNumericCompare a b then (Obj.magic a : float) <= (Obj.magic b : float)
else compare a b <= 0

let lessthan a b =
if Js.typeof a = "number" && Js.typeof b = "number" then
(Obj.magic a : float) < (Obj.magic b : float)
if canNumericCompare a b then (Obj.magic a : float) < (Obj.magic b : float)
else compare a b < 0

let min (x : Obj.t) y = if compare x y <= 0 then x else y
Expand Down
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

Loading