Skip to content

Commit cae9f85

Browse files
committed
1 parent b33d6fc commit cae9f85

16 files changed

+235
-77
lines changed

jscomp/bsb/bsb_namespace_map_gen.ml

+38-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,44 @@ let (//) = Ext_path.combine
2828

2929

3030

31-
32-
let output ~dir namespace
31+
let write_file fname digest contents =
32+
let oc = open_out_bin fname in
33+
Digest.output oc digest;
34+
output_char oc '\n';
35+
output_string oc contents;
36+
close_out oc
37+
(**
38+
TODO:
39+
sort filegroupts to ensure deterministic behavior
40+
41+
if [.bsbuild] is not changed
42+
[.mlmap] does not need to be changed too
43+
44+
*)
45+
let output
46+
~dir
47+
(namespace : string)
3348
(file_groups : Bsb_file_groups.file_groups )
3449
=
3550
let fname = namespace ^ Literals.suffix_mlmap in
36-
let oc = open_out_bin (dir// fname ) in
37-
List.iter
38-
(fun (x : Bsb_file_groups.file_group) ->
39-
String_map.iter x.sources (fun k _ ->
40-
output_string oc k ;
41-
output_string oc "\n"
42-
)
43-
) file_groups ;
44-
close_out oc
51+
let buf = Buffer.create 10000 in
52+
Ext_list.iter file_groups
53+
(fun x ->
54+
String_map.iter x.sources (fun k _ ->
55+
Buffer.add_string buf k ;
56+
Buffer.add_char buf '\n'
57+
)
58+
);
59+
let contents = Buffer.contents buf in
60+
let digest = Digest.string contents in
61+
let fname = (dir// fname ) in
62+
if Sys.file_exists fname then
63+
let ic = open_in_bin fname in
64+
let old_digest = really_input_string ic Ext_pervasives.digest_length in
65+
close_in ic ;
66+
(if old_digest <> digest then
67+
write_file fname digest contents)
68+
else
69+
write_file fname digest contents
70+
71+

jscomp/core/js_implementation.ml

+10-6
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,20 @@ let make_structure_item ~ns cunit : Parsetree.structure_item =
190190
; loc}))
191191

192192

193-
193+
(** decoding [.mlmap]
194+
keep in sync {!Bsb_namespace_map_gen.output}
195+
*)
194196
let implementation_map ppf sourcefile outputprefix =
195-
let list_of_modules = Ext_io.rev_lines_of_file sourcefile
196-
in
197+
let ichan = open_in_bin sourcefile in
198+
seek_in ichan (Ext_pervasives.digest_length +1);
199+
let list_of_modules = Ext_io.rev_lines_of_chann ichan in
200+
close_in ichan;
197201
let ns =
198202
Ext_string.capitalize_ascii
199203
(Filename.chop_extension (Filename.basename sourcefile)) in
200-
let ml_ast = Ext_list.fold_left list_of_modules [] (fun acc module_name ->
201-
if Ext_string.is_empty module_name then acc
202-
else make_structure_item ~ns module_name :: acc
204+
let ml_ast = Ext_list.fold_left list_of_modules [] (fun acc line ->
205+
if Ext_string.is_empty line then acc
206+
else make_structure_item ~ns line :: acc
203207
) in
204208
Compmisc.init_path false;
205209
ml_ast

jscomp/ext/ext_io.ml

+9-6
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ let load_file f =
3333
end
3434

3535

36-
let rev_lines_of_file file =
37-
Ext_pervasives.finally (open_in_bin file) close_in begin fun chan ->
38-
let rec loop acc =
36+
let rev_lines_of_chann chan =
37+
let rec loop acc chan =
3938
match input_line chan with
40-
| line -> loop (line :: acc)
39+
| line -> loop (line :: acc) chan
4140
| exception End_of_file -> close_in chan ; acc in
42-
loop []
43-
end
41+
loop [] chan
42+
43+
44+
let rev_lines_of_file file =
45+
Ext_pervasives.finally (open_in_bin file) close_in rev_lines_of_chann
46+
4447

4548
let write_file f content =
4649
Ext_pervasives.finally (open_out_bin f) close_out begin fun oc ->

jscomp/ext/ext_io.mli

+2
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ val load_file : string -> string
2626

2727
val rev_lines_of_file : string -> string list
2828

29+
val rev_lines_of_chann : in_channel -> string list
30+
2931
val write_file : string -> string -> unit

jscomp/ext/ext_pervasives.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ let hash_variant s =
8383
if !accu > 0x3FFFFFFF then !accu - (1 lsl 31) else !accu
8484

8585
let todo loc =
86-
failwith (loc ^ " Not supported yet")
86+
failwith (loc ^ " Not supported yet")
87+
88+
let digest_length = 16

jscomp/ext/ext_pervasives.mli

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ external id : 'a -> 'a = "%identity"
6060
*)
6161
val hash_variant : string -> int
6262

63-
val todo : string -> 'a
63+
val todo : string -> 'a
64+
65+
val digest_length : int

lib/4.02.3/bsb.ml

+53-17
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ external id : 'a -> 'a = "%identity"
458458
val hash_variant : string -> int
459459

460460
val todo : string -> 'a
461+
462+
val digest_length : int
461463
end = struct
462464
#1 "ext_pervasives.ml"
463465
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -546,6 +548,8 @@ let hash_variant s =
546548

547549
let todo loc =
548550
failwith (loc ^ " Not supported yet")
551+
552+
let digest_length = 16
549553
end
550554
module Ext_string : sig
551555
#1 "ext_string.mli"
@@ -12853,20 +12857,47 @@ let (//) = Ext_path.combine
1285312857

1285412858

1285512859

12856-
12857-
let output ~dir namespace
12860+
let write_file fname digest contents =
12861+
let oc = open_out_bin fname in
12862+
Digest.output oc digest;
12863+
output_char oc '\n';
12864+
output_string oc contents;
12865+
close_out oc
12866+
(**
12867+
TODO:
12868+
sort filegroupts to ensure deterministic behavior
12869+
12870+
if [.bsbuild] is not changed
12871+
[.mlmap] does not need to be changed too
12872+
12873+
*)
12874+
let output
12875+
~dir
12876+
(namespace : string)
1285812877
(file_groups : Bsb_file_groups.file_groups )
1285912878
=
1286012879
let fname = namespace ^ Literals.suffix_mlmap in
12861-
let oc = open_out_bin (dir// fname ) in
12862-
List.iter
12863-
(fun (x : Bsb_file_groups.file_group) ->
12864-
String_map.iter x.sources (fun k _ ->
12865-
output_string oc k ;
12866-
output_string oc "\n"
12867-
)
12868-
) file_groups ;
12869-
close_out oc
12880+
let buf = Buffer.create 10000 in
12881+
Ext_list.iter file_groups
12882+
(fun x ->
12883+
String_map.iter x.sources (fun k _ ->
12884+
Buffer.add_string buf k ;
12885+
Buffer.add_char buf '\n'
12886+
)
12887+
);
12888+
let contents = Buffer.contents buf in
12889+
let digest = Digest.string contents in
12890+
let fname = (dir// fname ) in
12891+
if Sys.file_exists fname then
12892+
let ic = open_in_bin fname in
12893+
let old_digest = really_input_string ic Ext_pervasives.digest_length in
12894+
close_in ic ;
12895+
(if old_digest <> digest then
12896+
write_file fname digest contents)
12897+
else
12898+
write_file fname digest contents
12899+
12900+
1287012901
end
1287112902
module Bsb_ninja_global_vars
1287212903
= struct
@@ -16875,6 +16906,8 @@ val load_file : string -> string
1687516906

1687616907
val rev_lines_of_file : string -> string list
1687716908

16909+
val rev_lines_of_chann : in_channel -> string list
16910+
1687816911
val write_file : string -> string -> unit
1687916912

1688016913
end = struct
@@ -16914,14 +16947,17 @@ let load_file f =
1691416947
end
1691516948

1691616949

16917-
let rev_lines_of_file file =
16918-
Ext_pervasives.finally (open_in_bin file) close_in begin fun chan ->
16919-
let rec loop acc =
16950+
let rev_lines_of_chann chan =
16951+
let rec loop acc chan =
1692016952
match input_line chan with
16921-
| line -> loop (line :: acc)
16953+
| line -> loop (line :: acc) chan
1692216954
| exception End_of_file -> close_in chan ; acc in
16923-
loop []
16924-
end
16955+
loop [] chan
16956+
16957+
16958+
let rev_lines_of_file file =
16959+
Ext_pervasives.finally (open_in_bin file) close_in rev_lines_of_chann
16960+
1692516961

1692616962
let write_file f content =
1692716963
Ext_pervasives.finally (open_out_bin f) close_out begin fun oc ->

lib/4.02.3/bsb_helper.ml

+4
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,8 @@ external id : 'a -> 'a = "%identity"
797797
val hash_variant : string -> int
798798

799799
val todo : string -> 'a
800+
801+
val digest_length : int
800802
end = struct
801803
#1 "ext_pervasives.ml"
802804
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -885,6 +887,8 @@ let hash_variant s =
885887

886888
let todo loc =
887889
failwith (loc ^ " Not supported yet")
890+
891+
let digest_length = 16
888892
end
889893
module Ext_string : sig
890894
#1 "ext_string.mli"

lib/4.02.3/bsdep.ml

+4
Original file line numberDiff line numberDiff line change
@@ -23247,6 +23247,8 @@ external id : 'a -> 'a = "%identity"
2324723247
val hash_variant : string -> int
2324823248

2324923249
val todo : string -> 'a
23250+
23251+
val digest_length : int
2325023252
end = struct
2325123253
#1 "ext_pervasives.ml"
2325223254
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -23335,6 +23337,8 @@ let hash_variant s =
2333523337

2333623338
let todo loc =
2333723339
failwith (loc ^ " Not supported yet")
23340+
23341+
let digest_length = 16
2333823342
end
2333923343
module Ext_string : sig
2334023344
#1 "ext_string.mli"

lib/4.02.3/bsppx.ml

+4
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ external id : 'a -> 'a = "%identity"
579579
val hash_variant : string -> int
580580

581581
val todo : string -> 'a
582+
583+
val digest_length : int
582584
end = struct
583585
#1 "ext_pervasives.ml"
584586
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -667,6 +669,8 @@ let hash_variant s =
667669

668670
let todo loc =
669671
failwith (loc ^ " Not supported yet")
672+
673+
let digest_length = 16
670674
end
671675
module Ext_string : sig
672676
#1 "ext_string.mli"

lib/4.02.3/unstable/all_ounit_tests.ml

+4
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,8 @@ external id : 'a -> 'a = "%identity"
19241924
val hash_variant : string -> int
19251925

19261926
val todo : string -> 'a
1927+
1928+
val digest_length : int
19271929
end = struct
19281930
#1 "ext_pervasives.ml"
19291931
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -2012,6 +2014,8 @@ let hash_variant s =
20122014

20132015
let todo loc =
20142016
failwith (loc ^ " Not supported yet")
2017+
2018+
let digest_length = 16
20152019
end
20162020
module Ext_string : sig
20172021
#1 "ext_string.mli"

0 commit comments

Comments
 (0)