@@ -37,57 +37,52 @@ module Line_reader : sig
37
37
38
38
val drop : t -> unit
39
39
40
- val close : t -> unit
40
+ val reset : t -> unit
41
41
42
42
val lnum : t -> int
43
43
44
44
val fname : t -> string
45
45
end = struct
46
46
type t =
47
- { ic : in_channel
48
- ; fname : string
49
- ; mutable next : string option
47
+ { fname : string
48
+ ; lines : string list
49
+ ; mutable current : string list
50
50
; mutable lnum : int
51
51
}
52
52
53
- let close t = close_in t.ic
53
+ let reset t =
54
+ t.current < - t.lines;
55
+ t.lnum < - 0
54
56
55
57
let open_ fname =
56
- let ic = open_in_bin fname in
57
- { ic; lnum = 0 ; fname; next = None }
58
+ let lines =
59
+ In_channel. input_all (open_in_bin fname) |> String. split_on_char ~sep: '\n'
60
+ in
61
+ { lines; lnum = 0 ; fname; current = lines }
58
62
59
63
let next t =
60
64
let lnum = t.lnum in
61
65
let s =
62
- match t.next with
63
- | None -> input_line t.ic
64
- | Some s ->
65
- t.next < - None ;
66
- s
66
+ match t.current with
67
+ | [] -> raise End_of_file
68
+ | x :: rem ->
69
+ t.current < - rem ;
70
+ x
67
71
in
68
72
t.lnum < - lnum + 1 ;
69
73
s
70
74
71
75
let peek t =
72
- match t.next with
73
- | Some x -> Some x
74
- | None -> (
75
- try
76
- let s = input_line t.ic in
77
- t.next < - Some s;
78
- Some s
79
- with End_of_file -> None )
76
+ match t.current with
77
+ | x :: _ -> Some x
78
+ | [] -> None
80
79
81
80
let drop t =
82
- match t.next with
83
- | Some _ ->
84
- t.next < - None ;
81
+ match t.current with
82
+ | [] -> ()
83
+ | _ :: rem ->
84
+ t.current < - rem;
85
85
t.lnum < - t.lnum + 1
86
- | None -> (
87
- try
88
- let (_ : string ) = input_line t.ic in
89
- t.lnum < - t.lnum + 1
90
- with End_of_file -> () )
91
86
92
87
let lnum t = t.lnum
93
88
@@ -182,7 +177,7 @@ let action ~resolve_sourcemap_url ~drop_source_map file line =
182
177
module Units : sig
183
178
val read : Line_reader .t -> Unit_info .t -> Unit_info .t
184
179
185
- val scan_file : string -> Build_info .t option * Unit_info .t list
180
+ val scan_file : string -> Build_info .t option * Unit_info .t list * Line_reader .t
186
181
end = struct
187
182
let rec read ic uinfo =
188
183
match Line_reader. peek ic with
@@ -230,8 +225,8 @@ end = struct
230
225
in
231
226
let build_info = find_build_info ic in
232
227
let units = scan_all ic [] in
233
- Line_reader. close ic;
234
- build_info, units
228
+ Line_reader. reset ic;
229
+ build_info, units, ic
235
230
end
236
231
237
232
let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source_map =
@@ -246,7 +241,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
246
241
List. fold_right
247
242
files
248
243
~init: (StringSet. empty, StringSet. empty, StringSet. empty)
249
- ~f: (fun (_file , (build_info , units )) acc ->
244
+ ~f: (fun (_file , (build_info , units , _reader )) acc ->
250
245
let cmo_file =
251
246
match build_info with
252
247
| Some bi -> (
@@ -286,7 +281,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
286
281
let t = Timer. make () in
287
282
let sym = ref Ocaml_compiler.Symtable.GlobalMap. empty in
288
283
let sym_js = ref [] in
289
- List. iter files ~f: (fun (_ , (_ , units )) ->
284
+ List. iter files ~f: (fun (_ , (_ , units , _ )) ->
290
285
List. iter units ~f: (fun (u : Unit_info.t ) ->
291
286
StringSet. iter
292
287
(fun s ->
@@ -299,7 +294,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
299
294
u.Unit_info. provides));
300
295
301
296
let build_info_emitted = ref false in
302
- List. iter files ~f: (fun (file , (build_info_for_file , units )) ->
297
+ List. iter files ~f: (fun (file , (build_info_for_file , units , ic )) ->
303
298
let is_runtime =
304
299
match build_info_for_file with
305
300
| Some bi -> (
@@ -309,7 +304,6 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
309
304
| None -> None
310
305
in
311
306
let sm_for_file = ref None in
312
- let ic = Line_reader. open_ file in
313
307
let skip ic = Line_reader. drop ic in
314
308
let line_offset = Line_writer. lnum oc in
315
309
let reloc = ref [] in
@@ -412,7 +406,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
412
406
read ()
413
407
in
414
408
read () ;
415
- Line_reader. close ic;
409
+ Line_reader. reset ic;
416
410
(match is_runtime with
417
411
| None -> ()
418
412
| Some bi ->
0 commit comments