Skip to content

Commit 621c5c2

Browse files
committed
Adding Js.Int.toFloat (continue slimming bs_stdlib_mini)
1 parent 5dde6d4 commit 621c5c2

17 files changed

+55
-49
lines changed

jscomp/others/belt_internalAVLset.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ let rec copy n =
5757
| None -> n
5858
| Some n ->
5959
let l,r = n |. (leftGet , rightGet) in
60-
return @@ node
60+
node
6161
~left:(copy l) ~right:(copy r)
6262
~value:(valueGet n) ~height:(heightGet n)
63+
|. return
6364
(* Creates a new node with leftGet son l, value v and right son r.
6465
We must have all elements of l < v < all elements of r.
6566
l and r must be balanced and | treeHeight l - treeHeight r | <= 2.
@@ -68,9 +69,10 @@ let rec copy n =
6869
let create (l : _ t) v (r : _ t) =
6970
let hl = match toOpt l with None -> 0 | Some n -> heightGet n in
7071
let hr = match toOpt r with None -> 0 | Some n -> heightGet n in
71-
return @@ node ~left:l ~value:v ~right:r ~height:(if hl >= hr then hl + 1 else hr + 1)
72+
node ~left:l ~value:v ~right:r ~height:(if hl >= hr then hl + 1 else hr + 1)
73+
|. return
7274

73-
let singleton x = return @@ node ~left:empty ~value:x ~right:empty ~height:1
75+
let singleton x = node ~left:empty ~value:x ~right:empty ~height:1 |. return
7476

7577
let heightGe l r =
7678
match toOpt l, toOpt r with
@@ -106,7 +108,7 @@ let bal l v r =
106108
create (create l v rll) rlv (create rlr rv rr)
107109
end
108110
end else
109-
return @@ node ~left:l ~value:v ~right:r ~height:(if hl >= hr then hl + 1 else hr + 1)
111+
return (node ~left:l ~value:v ~right:r ~height:(if hl >= hr then hl + 1 else hr + 1))
110112

111113

112114

jscomp/others/js_int.ml

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ let _ = Js.log \@\@ Js.Int.toStringWithRadix 123456 ~radix:36
156156
*)
157157
external toStringWithRadix : int -> radix:int -> string = "toString" [@@bs.send]
158158

159+
external toFloat : int -> float = "%floatofint"
160+
159161
let equal (x: int) y = x = y
160162

161163
let max : int = 2147483647

jscomp/others/js_list.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ let rec mapRevAux f acc ls =
7171

7272
let mapRev f ls = mapRevAux f [] ls
7373

74-
let rec map f ls = rev @@ mapRevAux f [] ls
74+
let rec map f ls = rev (mapRevAux f [] ls )
7575

7676
let rec iter f = function
7777
[] -> ()
@@ -125,7 +125,7 @@ let rec filterRevAux f acc xs =
125125
| true -> filterRevAux f (y::acc) ys
126126
end
127127

128-
let filter f xs = rev @@ filterRevAux f [] xs
128+
let filter f xs = rev (filterRevAux f [] xs )
129129

130130
let rec filterMapRevAux (f: 'a -> 'b option [@bs]) acc xs =
131131
match xs with
@@ -137,7 +137,7 @@ let rec filterMapRevAux (f: 'a -> 'b option [@bs]) acc xs =
137137
end
138138

139139
let rec filterMap f xs =
140-
rev @@ filterMapRevAux f [] xs
140+
rev (filterMapRevAux f [] xs)
141141

142142

143143
let rec countByAux f acc xs =
@@ -149,7 +149,7 @@ let rec countByAux f acc xs =
149149
let rec countBy f xs = countByAux f 0 xs
150150

151151
let init n f =
152-
Js_vector.toList @@ Js_vector.init n f
152+
Js_vector.toList (Js_vector.init n f )
153153

154154
external createUnsafe : int -> 'a Js_vector.t =
155155
"Array" [@@bs.new]

jscomp/others/js_math.ml

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ external unsafe_ceil_int : float -> int = "ceil" [@@bs.val] [@@bs.scope "Math"]
8282
let unsafe_ceil = unsafe_ceil_int
8383
[@@ocaml.deprecated "Please use `unsafe_ceil_int` instead"]
8484
(** smallest int greater than or equal to the argument *)
85-
let ceil_int f =
86-
if f > float Js_int.max then Js_int.max
87-
else if f < float Js_int.min then Js_int.min
85+
let ceil_int (f : float) : int =
86+
if f > Js_int.toFloat Js_int.max then Js_int.max
87+
else if f < Js_int.toFloat Js_int.min then Js_int.min
8888
else unsafe_ceil_int f
8989
let ceil = ceil_int
9090
[@@ocaml.deprecated "Please use `ceil_int` instead"]
@@ -113,8 +113,8 @@ let unsafe_floor = unsafe_floor_int
113113
[@@ocaml.deprecated "Please use `unsafe_floor_int` instead"]
114114
(** largest int greater than or equal to the arugment *)
115115
let floor_int f =
116-
if f > float Js_int.max then Js_int.max
117-
else if f < float Js_int.min then Js_int.min
116+
if f > Js_int.toFloat Js_int.max then Js_int.max
117+
else if f < Js_int.toFloat Js_int.min then Js_int.min
118118
else unsafe_floor f
119119
let floor = floor_int
120120
[@@ocaml.deprecated "Please use `floor_int` instead"]
@@ -171,7 +171,7 @@ external pow_float : base:float -> exp:float -> float = "pow" [@@bs.val] [@@bs.s
171171
external random : unit -> float = "random" [@@bs.val] [@@bs.scope "Math"]
172172
(** random number in \[min,max) *)
173173
let random_int min max =
174-
floor ((random ()) *. (float (max - min))) + min
174+
floor ((random ()) *. (Js_int.toFloat (max - min))) + min
175175

176176
(** rounds to nearest integer, returns a value not representable as [int] if NaN *)
177177
external unsafe_round : float -> int = "round" [@@bs.val] [@@bs.scope "Math"]

jscomp/others/js_vector.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ let filterInPlace p a =
4646
end;
4747
incr i
4848
done;
49-
ignore @@ Js_array.removeFromInPlace ~pos:!j a
49+
Js_array.removeFromInPlace ~pos:!j a |. ignore
5050

5151
let empty a =
52-
ignore @@ Js.Array.removeFromInPlace ~pos:0 a
52+
Js.Array.removeFromInPlace ~pos:0 a |. ignore
5353

5454
let pushBack x xs =
55-
ignore @@ Js.Array.push x xs
55+
Js.Array.push x xs |. ignore
5656

5757
(** Find by JS (===) equality *)
5858
let memByRef x xs =

jscomp/runtime/.depend

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ caml_io.cmj : js.cmj bs_string.cmj
1313
caml_float.cmj : caml_float.cmi
1414
caml_lexer.cmj : caml_lexer.cmi
1515
caml_parser.cmj : caml_parser.cmi
16-
caml_format.cmj : js_nativeint.cmj js_int64.cmj caml_utils.cmj bs_string.cmj \
17-
caml_format.cmi
16+
caml_format.cmj : js_nativeint.cmj js_int64.cmj caml_utils.cmj \
17+
caml_float.cmj bs_string.cmj caml_format.cmi
1818
caml_md5.cmj : bs_string.cmj caml_md5.cmi
1919
caml_queue.cmj : caml_queue.cmi
2020
caml_hash_primitive.cmj : bs_string.cmj caml_hash_primitive.cmi

jscomp/runtime/bs_stdlib_mini.mli

+2-5
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ external ( -. ) : float -> float -> float = "%subfloat"
6565
external ( *. ) : float -> float -> float = "%mulfloat"
6666
external ( /. ) : float -> float -> float = "%divfloat"
6767

68-
external float : int -> float = "%floatofint"
6968

70-
external int_of_float : float -> int = "%intoffloat"
69+
70+
7171

7272
module Bytes : sig
7373
external unsafe_get : bytes -> int -> char = "%bytes_unsafe_get"
@@ -156,9 +156,6 @@ module Int64 : sig
156156
external to_int : int64 -> int = "%int64_to_int"
157157
end
158158

159-
module Oo : sig
160-
external id : < .. > -> int = "%field1"
161-
end
162159

163160

164161

jscomp/runtime/caml_float.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ external sqrt : float -> float = "sqrt" [@@bs.val] [@@bs.scope "Math"]
3636
external max_float : float -> float -> float = "Math.max" [@@bs.val]
3737
external min_float : float -> float -> float = "Math.min" [@@bs.val]
3838
external pow_float : base:float -> exp:float -> float = "Math.pow" [@@bs.val]
39-
40-
39+
external int_of_float : float -> int = "%intoffloat"
40+
external float_of_int : int -> float = "%floatofint"
4141

4242
let caml_int32_float_of_bits : int32 -> float = fun%raw x -> {|
4343
return new Float32Array(new Int32Array([x]).buffer)[0]
@@ -66,7 +66,7 @@ let caml_modf_float (x : float) : float * float =
6666
else (1. /. x , x)
6767

6868
let caml_ldexp_float (x: float) (exp: int) : float =
69-
let x', exp' = ref x, ref (float exp) in
69+
let x', exp' = ref x, ref (float_of_int exp) in
7070
if !exp' > 1023. then begin
7171
exp' := !exp' -. 1023.;
7272
x' := !x' *. pow_float ~base:2. ~exp:1023.;

jscomp/runtime/caml_float.mli

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
(** *)
3030
external floor : float -> float = "Math.floor" [@@bs.val]
31+
external int_of_float : float -> int = "%intoffloat"
32+
external float_of_int : int -> float = "%floatofint"
3133
val caml_int32_float_of_bits : int32 -> float
3234
val caml_int32_bits_of_float : float -> int32
3335

jscomp/runtime/caml_format.ml

+11-11
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ let parse_sign_and_base (s : string) =
9090

9191
let caml_int_of_string s =
9292
let i, sign, hbase = parse_sign_and_base s in
93-
let base = Nativeint.of_int @@ int_of_string_base hbase in
93+
let base = Nativeint.of_int (int_of_string_base hbase) in
9494
let threshold = (-1n >>> 0) in
9595
let len =Bs_string.length s in
9696
let c = if i < len then s.[i] else '\000' in
97-
let d = to_nat @@ parse_digit c in
97+
let d = to_nat (parse_digit c) in
9898
let () =
9999
if d < 0n || d >= base then
100100
caml_failwith "int_of_string" in
@@ -105,7 +105,7 @@ let caml_int_of_string s =
105105
let a = s.[k] in
106106
if a = '_' then aux acc ( k + 1)
107107
else
108-
let v = to_nat @@ parse_digit a in
108+
let v = to_nat (parse_digit a) in
109109
if v < 0n || v >= base then
110110
caml_failwith "int_of_string"
111111
else
@@ -123,7 +123,7 @@ let caml_int_of_string s =
123123

124124
let caml_int64_of_string s =
125125
let i, sign, hbase = parse_sign_and_base s in
126-
let base = Int64.of_int @@ int_of_string_base hbase in
126+
let base = Int64.of_int (int_of_string_base hbase) in
127127
let sign = Int64.of_nativeint sign in
128128
let threshold =
129129
match hbase with
@@ -138,7 +138,7 @@ let caml_int64_of_string s =
138138
in
139139
let len =Bs_string.length s in
140140
let c = if i < len then s.[i] else '\000' in
141-
let d = Int64.of_int @@ parse_digit c in
141+
let d = Int64.of_int (parse_digit c) in
142142
let () =
143143
if d < 0L || d >= base then
144144
caml_failwith "int64_of_string" in
@@ -151,7 +151,7 @@ let caml_int64_of_string s =
151151
let a = s.[k] in
152152
if a = '_' then aux acc ( k + 1)
153153
else
154-
let v = Int64.of_int @@ parse_digit a in
154+
let v = Int64.of_int (parse_digit a) in
155155
if v < 0L || v >= base || acc > threshold then
156156
caml_failwith "int64_of_string"
157157
else
@@ -195,7 +195,7 @@ let lowercase c =
195195
let parse_format fmt =
196196
let len =Bs_string.length fmt in
197197
if len > 31 then
198-
raise @@ Invalid_argument "format_int: format too long" ;
198+
raise (Invalid_argument "format_int: format too long") ;
199199
let rec aux (f : fmt) i : fmt =
200200
if i >= len then f
201201
else
@@ -357,7 +357,7 @@ let aux f (i : nativeint) =
357357
else
358358
Nativeint.shift_right_logical i 0
359359
else i in
360-
let s = ref @@ Bs_string.of_nativeint i ~base:(int_of_base f.base) in
360+
let s = ref (Bs_string.of_nativeint i ~base:(int_of_base f.base)) in
361361
if f.prec >= 0 then
362362
begin
363363
f.filter <- " ";
@@ -501,7 +501,7 @@ let caml_format_float fmt x =
501501
s := "nan";
502502
f.filter <- " "
503503
end
504-
else if not @@ FloatRT.isFinite x then
504+
else if not (FloatRT.isFinite x) then
505505
begin
506506
s := "inf";
507507
f.filter <- " "
@@ -533,7 +533,7 @@ let caml_format_float fmt x =
533533
let prec = if prec <> 0 then prec else 1 in
534534
s := FloatRT.toExponentialWithPrecision x ~digits:(prec - 1);
535535
let j = Bs_string.index_of !s "e" in
536-
let exp = int_of_float @@ FloatRT.fromString @@ Bs_string.slice_rest !s (j + 1) in
536+
let exp = Caml_float.int_of_float (FloatRT.fromString (Bs_string.slice_rest !s (j + 1))) in
537537
if exp < -4 || x >= 1e21 ||Bs_string.length (FloatRT.toFixed x) > prec then
538538
let i = ref (j - 1) in
539539
while !s.[!i] = '0' do
@@ -558,7 +558,7 @@ let caml_format_float fmt x =
558558
decr p
559559
done ;
560560
if !p <> 0 then
561-
let k = ref @@Bs_string.length !s - 1 in
561+
let k = ref (Bs_string.length !s - 1) in
562562
while !s.[!k] = '0' do
563563
decr k
564564
done ;

jscomp/runtime/caml_hash.ml

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
external ( +~ ) : nativeint -> nativeint -> nativeint =
2828
"caml_int32_add"
2929

30+
(*ATTENTION: refer {!Oo.id} *)
31+
external oo_id : Obj.t -> int = "%field1"
32+
3033
open Caml_hash_primitive
3134

3235
let caml_hash count _limit seed obj =
@@ -51,7 +54,7 @@ let caml_hash count _limit seed obj =
5154
Caml_queue.push obj queue;
5255
decr num
5356
in
54-
while not @@ Caml_queue.is_empty queue && !num > 0 do
57+
while not (Caml_queue.is_empty queue) && !num > 0 do
5558
let obj = Caml_queue.unsafe_pop queue in
5659
if Js.typeof obj = "number" then
5760
begin
@@ -80,7 +83,7 @@ let caml_hash count _limit seed obj =
8083
let obj_tag = Obj.tag obj in
8184
let tag = (size lsl 10) lor obj_tag in
8285
if tag = 248 (* Obj.object_tag*) then
83-
hash := caml_hash_mix_int !hash (Nativeint.of_int (Oo.id (Obj.magic obj)))
86+
hash := caml_hash_mix_int !hash (Nativeint.of_int (oo_id obj))
8487
else
8588
begin
8689
hash := caml_hash_mix_int !hash (Nativeint.of_int tag) ;

jscomp/runtime/caml_int64.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ let to_int32 x = Nativeint.logor x.lo 0n (* signed integer *)
408408

409409
let to_hex x =
410410
let aux v =
411-
Bs_string.of_int (Nativeint.to_int @@ Nativeint.shift_right_logical v 0) ~base:16
411+
Bs_string.of_int (Nativeint.to_int (Nativeint.shift_right_logical v 0)) ~base:16
412412
in
413413
match x.hi, x.lo with
414414
| 0n, 0n -> "0"

jscomp/runtime/caml_io.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ let caml_ml_output_char (oc : out_channel) (char : char) : unit =
106106
caml_ml_output oc (Bs_string.of_char char) 0 1
107107

108108
let caml_ml_input (ic : in_channel) (bytes : bytes) offset len : int =
109-
raise @@ Failure "caml_ml_input ic not implemented"
109+
raise (Failure "caml_ml_input ic not implemented")
110110

111111
let caml_ml_input_char (ic : in_channel) : char =
112-
raise @@ Failure "caml_ml_input_char not implemnted"
112+
raise (Failure "caml_ml_input_char not implemnted")
113113

114114
let caml_ml_out_channels_list () : out_channel list =
115115
[stdout; stderr]

jscomp/runtime/caml_obj.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ let rec caml_compare (a : Obj.t) (b : Obj.t) : int =
219219
(* Some None < Some ..*)
220220
-1
221221
else if tag_a = 248 (* object/exception *) then
222-
Pervasives.compare (Obj.magic @@ Obj.field a 1 : int) (Obj.magic @@ Obj.field b 1 )
222+
Pervasives.compare (Obj.magic (Obj.field a 1) : int) (Obj.magic (Obj.field b 1 ))
223223
else if tag_a = 251 (* abstract_tag *) then
224224
raise (Invalid_argument "equal: abstract value")
225225
else if tag_a <> tag_b then
@@ -312,7 +312,7 @@ let rec caml_equal (a : Obj.t) (b : Obj.t) : bool =
312312
else if tag_b = 250 then
313313
caml_equal a (Obj.field b 0)
314314
else if tag_a = 248 (* object/exception *) then
315-
(Obj.magic @@ Obj.field a 1) == (Obj.magic @@ Obj.field b 1 )
315+
(Obj.magic (Obj.field a 1)) == (Obj.magic (Obj.field b 1 ))
316316
else if tag_a = 251 (* abstract_tag *) then
317317
raise (Invalid_argument "equal: abstract value")
318318
else if tag_a <> tag_b then

jscomp/runtime/caml_sys.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ let caml_sys_exit :int -> 'a = fun exit_code ->
9999

100100

101101
let caml_sys_is_directory _s =
102-
raise @@ Failure "caml_sys_is_directory not implemented"
102+
raise (Failure "caml_sys_is_directory not implemented")
103103

104104
(** Need polyfill to make cmdliner work
105105
{!Sys.is_directory} or {!Sys.file_exists} {!Sys.command}
106106
*)
107107
let caml_sys_file_exists _s =
108-
raise @@ Failure "caml_sys_file_exists not implemented"
108+
raise ( Failure "caml_sys_file_exists not implemented")

lib/js/belt_internalAVLset.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ function checkInvariantInternal(_v) {
390390
var r = v.right;
391391
var diff = treeHeight(l) - treeHeight(r) | 0;
392392
if (!(diff <= 2 && diff >= -2)) {
393-
throw new Error("File \"belt_internalAVLset.ml\", line 302, characters 6-12");
393+
throw new Error("File \"belt_internalAVLset.ml\", line 304, characters 6-12");
394394
}
395395
checkInvariantInternal(l);
396396
_v = r;

lib/js/caml_hash.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function caml_hash(count, _limit, seed, obj) {
3737
Caml_builtin_exceptions.assert_failure,
3838
/* tuple */[
3939
"caml_hash.ml",
40-
72,
40+
75,
4141
8
4242
]
4343
];

0 commit comments

Comments
 (0)