Skip to content

Commit 35374ea

Browse files
authored
Merge pull request #2601 from BuckleScript/less_ad_hoc_pervasives_processing
specialize string_of_int and less ad-hoc module post-processing
2 parents fe21e20 + b44bd95 commit 35374ea

File tree

137 files changed

+743
-794
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+743
-794
lines changed

jscomp/all.depend

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ core/lam_compile_main.cmx : ext/literals.cmx core/lam_util.cmx \
675675
core/js_exp_make.cmx core/js_dump_program.cmx common/js_config.cmx \
676676
core/js_cmj_format.cmx core/j.cmx ext/ident_set.cmx ext/ext_string.cmx \
677677
ext/ext_pervasives.cmx ext/ext_path.cmx ext/ext_namespace.cmx \
678-
common/ext_log.cmx ext/ext_list.cmx ext/ext_ident.cmx \
679-
ext/ext_filename.cmx ext/ext_char.cmx core/lam_compile_main.cmi
678+
common/ext_log.cmx ext/ext_list.cmx ext/ext_filename.cmx ext/ext_char.cmx \
679+
core/lam_compile_main.cmi
680680
core/js_implementation.cmx : core/ocaml_parse.cmx ext/literals.cmx \
681681
core/lam_compile_main.cmx core/lam_compile_env.cmx common/js_config.cmx \
682682
ext/ext_string.cmx ext/ext_pervasives.cmx ext/ext_namespace.cmx \

jscomp/core/lam_compile_main.ml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,36 +71,6 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.t)
7171
Note the arity of [print_endline] is already analyzed before,
7272
so it should be safe
7373
*)
74-
| Single(_, ({name="string_of_int";_} as id),_ ), "pervasives.ml" ->
75-
Js_output.of_stmt @@ S.alias_variable id
76-
~exp:(
77-
let arg = Ext_ident.create "param" in
78-
E.ocaml_fun [arg] [S.return_stmt (E.int_to_string (E.var arg))]
79-
)
80-
81-
(** Special handling for values in [Sys] *)
82-
| Single(_, ({name="max_array_length" | "max_string_length";_} as id) ,_ ), "sys.ml" ->
83-
(* See [js_knowledge] Array size section, can not be expressed by OCaml int,
84-
note that casual handling of {!Sys.max_string_length} could result into
85-
negative value which could cause wrong behavior of {!Buffer.create}
86-
*)
87-
Js_output.of_stmt @@ S.alias_variable id ~exp:(E.float "2147483647") (*2 ^ 31 - 1*)
88-
89-
| Single(_, ({name="max_int";_} as id) ,_ ), ("sys.ml" | "nativeint.ml") ->
90-
(* See [js_knowledge] Max int section, (2. ** 53. -. 1.;;)
91-
can not be expressed by OCaml int
92-
FIXME: we need handle {!Nativeint} and {!Sys} differently
93-
*)
94-
Js_output.of_stmt @@ S.alias_variable id
95-
~exp:(E.float "9007199254740991.")
96-
97-
| Single(_, ({name="min_int";_} as id) ,_ ), ("sys.ml" | "nativeint.ml") ->
98-
(* See [js_knowledge] Max int section, -. (2. ** 53. -. 1.);;
99-
can not be expressed by OCaml int
100-
FIXME: we need handle {!Nativeint} and {!Sys} differently
101-
*)
102-
Js_output.of_stmt @@ S.alias_variable id
103-
~exp:(E.float ("-9007199254740991."))
10474

10575
| Single (kind, id, lam), _ ->
10676
(* let lam = Optimizer.simplify_lets [] lam in *)

jscomp/stdlib/nativeint.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ let pred n = sub n 1n
4040
let abs n = if n >= 0n then n else neg n
4141
#if BS then
4242
let size = 54 (* 54 is not a multiple of 8 *)
43+
let min_int = -9007199254740991n
44+
let max_int = 9007199254740991n
4345
#else
4446
let size = Sys.word_size
45-
#end
4647
let min_int = shift_left 1n (size - 1)
4748
let max_int = sub min_int 1n
49+
#end
4850
let lognot n = logxor n (-1n)
4951

5052
external format : string -> nativeint -> string = "caml_nativeint_format"

jscomp/stdlib/pervasives.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,12 @@ let bool_of_string = function
244244
| "false" -> false
245245
| _ -> invalid_arg "bool_of_string"
246246

247+
#if BS then
248+
external string_of_int : int -> string = "String" [@@bs.val]
249+
#else
247250
let string_of_int n =
248251
format_int "%d" n
249-
252+
#end
250253
external int_of_string : string -> int = "caml_int_of_string"
251254
external string_get : string -> int -> char = "%string_safe_get"
252255

jscomp/stdlib/pervasives.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,12 @@ val bool_of_string : string -> bool
568568
Raise [Invalid_argument "bool_of_string"] if the string is not
569569
["true"] or ["false"]. *)
570570

571+
#if BS then
572+
external string_of_int : int -> string = "String" [@@bs.val]
573+
#else
571574
val string_of_int : int -> string
572575
(** Return the string representation of an integer, in decimal. *)
573-
576+
#end
574577
external int_of_string : string -> int = "caml_int_of_string"
575578
(** Convert the given string to an integer.
576579
The string is read in decimal (by default) or in hexadecimal (if it

jscomp/stdlib/sys.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ let word_size = word_size ()
3333
let unix = unix ()
3434
let win32 = win32 ()
3535
let cygwin = cygwin ()
36+
#if BS then
37+
let max_array_length = 2147483647 (* 2^ 31 - 1 *)
38+
let max_string_length = 2147483647
39+
#else
3640
let max_array_length = (1 lsl (word_size - 10)) - 1;;
3741
let max_string_length = word_size / 8 * max_array_length - 1;;
38-
42+
#end
3943
external file_exists: string -> bool = "caml_sys_file_exists"
4044
external is_directory : string -> bool = "caml_sys_is_directory"
4145
external remove: string -> unit = "caml_sys_remove"

jscomp/test/a_filename_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function eq(loc, x, y) {
1313
test_id[0] = test_id[0] + 1 | 0;
1414
suites[0] = /* :: */[
1515
/* tuple */[
16-
loc + (" id " + test_id[0]),
16+
loc + (" id " + String(test_id[0])),
1717
(function () {
1818
return /* Eq */Block.__(0, [
1919
x,

jscomp/test/a_scope_bug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function odd(_z) {
77
var z = _z;
88
var even = Caml_int32.imul(z, z);
99
var a = (even + 4 | 0) + even | 0;
10-
console.log("" + a);
10+
console.log(String(a));
1111
_z = 32;
1212
continue ;
1313

jscomp/test/arity_deopt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function eq(loc, x, y) {
1212
test_id[0] = test_id[0] + 1 | 0;
1313
suites[0] = /* :: */[
1414
/* tuple */[
15-
loc + (" id " + test_id[0]),
15+
loc + (" id " + String(test_id[0])),
1616
(function () {
1717
return /* Eq */Block.__(0, [
1818
x,

jscomp/test/array_subtle_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function eq(loc, param) {
1515
test_id[0] = test_id[0] + 1 | 0;
1616
suites[0] = /* :: */[
1717
/* tuple */[
18-
loc + (" id " + test_id[0]),
18+
loc + (" id " + String(test_id[0])),
1919
(function () {
2020
return /* Eq */Block.__(0, [
2121
x,

0 commit comments

Comments
 (0)