Skip to content

Commit 5a37b34

Browse files
authored
Merge pull request #2600 from BuckleScript/less_ad_hoc_pervasives_processing
less ad-hoc pervasives processing
2 parents 289cf1d + ffe0981 commit 5a37b34

14 files changed

+51
-96
lines changed

jscomp/core/lam_compile_main.ml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.t)
6666
to make those parts pure (not a function call), then it can be removed
6767
if unused
6868
*)
69-
| Single(_, ({name="infinity";_} as id),_ ), "pervasives.ml"
70-
-> (* TODO: check relative path to compiler*)
71-
Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "Infinity")
72-
| Single(_, ({name="neg_infinity";_} as id),_ ), "pervasives.ml" ->
73-
Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "-Infinity")
74-
| Single(_, ({name="nan";_} as id),_ ), "pervasives.ml" ->
75-
Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "NaN")
7669

7770
(* QUICK hack to make hello world example nicer,
7871
Note the arity of [print_endline] is already analyzed before,
@@ -85,23 +78,6 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.t)
8578
E.ocaml_fun [arg] [S.return_stmt (E.int_to_string (E.var arg))]
8679
)
8780

88-
| Single(_, ({name="max_float";_} as id),_ ), "pervasives.ml" ->
89-
90-
Js_output.of_stmt @@ S.alias_variable id
91-
~exp:(E.js_global_dot "Number" "MAX_VALUE")
92-
| Single(_, ({name="min_float";_} as id) ,_ ), "pervasives.ml" ->
93-
Js_output.of_stmt @@ S.alias_variable id
94-
~exp:(E.js_global_dot "Number" "MIN_VALUE")
95-
| Single(_, ({name="epsilon_float";_} as id) ,_ ), "pervasives.ml" ->
96-
Js_output.of_stmt @@ S.alias_variable id
97-
~exp:(E.float "2.220446049250313e-16")
98-
| Single(_, ({name="cat";_} as id) ,_ ), "bytes.ml" ->
99-
Js_output.of_stmt @@ S.alias_variable id
100-
~exp:(let a = Ext_ident.create "a" in
101-
let b = Ext_ident.create "b" in
102-
E.ocaml_fun [a;b] [S.return_stmt (E.array_append (E.var a) (E.var b))]
103-
)
104-
10581
(** Special handling for values in [Sys] *)
10682
| Single(_, ({name="max_array_length" | "max_string_length";_} as id) ,_ ), "sys.ml" ->
10783
(* See [js_knowledge] Array size section, can not be expressed by OCaml int,

jscomp/stdlib/pervasives.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ external float_of_int : int -> float = "%floatofint"
146146
external truncate : float -> int = "%intoffloat"
147147
external int_of_float : float -> int = "%intoffloat"
148148
external float_of_bits : int64 -> float = "caml_int64_float_of_bits"
149+
150+
#if BS then
151+
external infinity : float = "POSITIVE_INFINITY"
152+
[@@bs.val] [@@bs.scope "Number"]
153+
external neg_infinity : float = "NEGATIVE_INFINITY"
154+
[@@bs.val] [@@bs.scope "Number"]
155+
external nan : float = "NaN"
156+
[@@bs.val] [@@bs.scope "Number"]
157+
external max_float : float = "MAX_VALUE"
158+
[@@bs.val] [@@bs.scope "Number"]
159+
external min_float : float = "MIN_VALUE"
160+
[@@bs.val] [@@bs.scope "Number"]
161+
(* external epsilon_float : float = "EPSILON" (* ES 2015 *)
162+
[@@bs.val] [@@bs.scope "Number"] *)
163+
let epsilon_float = 2.220446049250313e-16
164+
#else
149165
let infinity =
150166
float_of_bits 0x7F_F0_00_00_00_00_00_00L
151167
let neg_infinity =
@@ -157,7 +173,8 @@ let max_float =
157173
let min_float =
158174
float_of_bits 0x00_10_00_00_00_00_00_00L
159175
let epsilon_float =
160-
float_of_bits 0x3C_B0_00_00_00_00_00_00L
176+
float_of_bits 0x3C_B0_00_00_00_00_00_00L
177+
#end
161178

162179
type fpclass =
163180
FP_normal

jscomp/stdlib/pervasives.mli

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,21 @@ external int_of_float : float -> int = "%intoffloat"
463463
(** Truncate the given floating-point number to an integer.
464464
The result is unspecified if the argument is [nan] or falls outside the
465465
range of representable integers. *)
466-
466+
#if BS then
467+
external infinity : float = "POSITIVE_INFINITY"
468+
[@@bs.val] [@@bs.scope "Number"]
469+
external neg_infinity : float = "NEGATIVE_INFINITY"
470+
[@@bs.val] [@@bs.scope "Number"]
471+
external nan : float = "NaN"
472+
[@@bs.val] [@@bs.scope "Number"]
473+
external max_float : float = "MAX_VALUE"
474+
[@@bs.val] [@@bs.scope "Number"]
475+
external min_float : float = "MIN_VALUE"
476+
[@@bs.val] [@@bs.scope "Number"]
477+
(* external epsilon_float : float = "EPSILON" (* ES 2015 *)
478+
[@@bs.val] [@@bs.scope "Number"] *)
479+
val epsilon_float : float
480+
#else
467481
val infinity : float
468482
(** Positive infinity. *)
469483

@@ -488,6 +502,8 @@ val epsilon_float : float
488502
(** The difference between [1.0] and the smallest exactly representable
489503
floating-point number greater than [1.0]. *)
490504

505+
#end
506+
491507
type fpclass =
492508
FP_normal (** Normal number, none of the below *)
493509
| FP_subnormal (** Number very close to 0.0, has reduced precision *)

jscomp/test/caml_format_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,17 +1305,17 @@ var float_data = /* array */[
13051305
],
13061306
/* tuple */[
13071307
"%f",
1308-
Pervasives.nan,
1308+
Number.NaN,
13091309
"nan"
13101310
],
13111311
/* tuple */[
13121312
"%f",
1313-
Pervasives.infinity,
1313+
Number.POSITIVE_INFINITY,
13141314
"inf"
13151315
],
13161316
/* tuple */[
13171317
"%f",
1318-
Pervasives.neg_infinity,
1318+
Number.NEGATIVE_INFINITY,
13191319
"-inf"
13201320
],
13211321
/* tuple */[

jscomp/test/float_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 48, characte
152152
-3
153153
]);
154154

155-
var match$3 = Caml_float.caml_modf_float(Pervasives.nan);
155+
var match$3 = Caml_float.caml_modf_float(Number.NaN);
156156

157157
var param_000 = +isNaN(match$3[0]);
158158

jscomp/test/js_float_test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
5-
var Pervasives = require("../../lib/js/pervasives.js");
65

76
var suites_000 = /* tuple */[
87
"_NaN <> _NaN",
@@ -40,7 +39,7 @@ var suites_001 = /* :: */[
4039
(function () {
4140
return /* Eq */Block.__(0, [
4241
/* false */0,
43-
+isFinite(Pervasives.infinity)
42+
+isFinite(Number.POSITIVE_INFINITY)
4443
]);
4544
})
4645
],
@@ -50,7 +49,7 @@ var suites_001 = /* :: */[
5049
(function () {
5150
return /* Eq */Block.__(0, [
5251
/* false */0,
53-
+isFinite(Pervasives.neg_infinity)
52+
+isFinite(Number.NEGATIVE_INFINITY)
5453
]);
5554
})
5655
],

jscomp/test/ocaml_proto_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ function lexer(lexbuf) {
16281628
case 15 :
16291629
return /* FLOAT */Block.__(4, [Caml_format.caml_float_of_string(Lexing.lexeme(lexbuf$1))]);
16301630
case 16 :
1631-
return /* FLOAT */Block.__(4, [Pervasives.nan]);
1631+
return /* FLOAT */Block.__(4, [Number.NaN]);
16321632
case 17 :
16331633
update_loc(lexbuf$1);
16341634
___ocaml_lex_state = 0;

jscomp/test/test_pervasive.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ var Pervasives$1 = /* module */[
5555
/* max_int */Pervasives.max_int,
5656
/* min_int */Pervasives.min_int,
5757
/* lnot */Pervasives.lnot,
58-
/* infinity */Pervasives.infinity,
59-
/* neg_infinity */Pervasives.neg_infinity,
60-
/* nan */Pervasives.nan,
61-
/* max_float */Pervasives.max_float,
62-
/* min_float */Pervasives.min_float,
6358
/* epsilon_float */Pervasives.epsilon_float,
6459
/* char_of_int */Pervasives.char_of_int,
6560
/* string_of_bool */Pervasives.string_of_bool,

jscomp/test/test_pervasives2.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ var List$1 = /* module */[
5656
/* max_int */Pervasives.max_int,
5757
/* min_int */Pervasives.min_int,
5858
/* lnot */Pervasives.lnot,
59-
/* infinity */Pervasives.infinity,
60-
/* neg_infinity */Pervasives.neg_infinity,
61-
/* nan */Pervasives.nan,
62-
/* max_float */Pervasives.max_float,
63-
/* min_float */Pervasives.min_float,
6459
/* epsilon_float */Pervasives.epsilon_float,
6560
/* char_of_int */Pervasives.char_of_int,
6661
/* string_of_bool */Pervasives.string_of_bool,
@@ -150,11 +145,6 @@ var U = /* module */[
150145
/* max_int */Pervasives.max_int,
151146
/* min_int */Pervasives.min_int,
152147
/* lnot */Pervasives.lnot,
153-
/* infinity */Pervasives.infinity,
154-
/* neg_infinity */Pervasives.neg_infinity,
155-
/* nan */Pervasives.nan,
156-
/* max_float */Pervasives.max_float,
157-
/* min_float */Pervasives.min_float,
158148
/* epsilon_float */Pervasives.epsilon_float,
159149
/* char_of_int */Pervasives.char_of_int,
160150
/* string_of_bool */Pervasives.string_of_bool,

jscomp/test/test_pervasives3.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ var Pervasives$1 = /* module */[
1111
/* max_int */Pervasives.max_int,
1212
/* min_int */Pervasives.min_int,
1313
/* lnot */Pervasives.lnot,
14-
/* infinity */Pervasives.infinity,
15-
/* neg_infinity */Pervasives.neg_infinity,
16-
/* nan */Pervasives.nan,
17-
/* max_float */Pervasives.max_float,
18-
/* min_float */Pervasives.min_float,
1914
/* epsilon_float */Pervasives.epsilon_float,
2015
/* char_of_int */Pervasives.char_of_int,
2116
/* string_of_bool */Pervasives.string_of_bool,

0 commit comments

Comments
 (0)