Skip to content

Commit 1b594f9

Browse files
authored
Merge pull request #3749 from BuckleScript/minimal_bsb_helper
minimize the size of bsb_helper
2 parents 09f7c37 + 05a168a commit 1b594f9

Some content is hidden

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

57 files changed

+49972
-50818
lines changed

jscomp/bsb/bsb_ninja_file_groups.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let handle_generators oc
3737
Ext_list.iter group.generators (fun {output; input; command} ->
3838
(*TODO: add a loc for better error message *)
3939
match String_map.find_opt custom_rules command with
40-
| None -> Ext_pervasives.failwithf ~loc:__LOC__ "custom rule %s used but not defined" command
40+
| None -> Ext_fmt.failwithf ~loc:__LOC__ "custom rule %s used but not defined" command
4141
| Some rule ->
4242
Bsb_ninja_targets.output_build oc
4343
~outputs:(Ext_list.map output map_to_source_dir)

jscomp/bsb_helper/bsb_arg.ml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
2+
type key = string
3+
type doc = string
4+
type usage_msg = string
5+
type anon_fun = (string -> unit)
6+
7+
type spec =
8+
| Unit of (unit -> unit)
9+
| Set of bool ref
10+
| String of (string -> unit)
11+
| Set_string of string ref
12+
| Int of (int -> unit)
13+
| Set_int of int ref
14+
15+
exception Bad of string
16+
exception Help of string
17+
18+
type error =
19+
| Unknown of string
20+
| Wrong of string * string * string (* option, actual, expected *)
21+
| Missing of string
22+
| Message of string
23+
24+
exception Stop of error
25+
26+
27+
type t = (string * spec * string) list
28+
29+
let rec assoc3 (x : string) (l : t) =
30+
match l with
31+
| [] -> None
32+
| (y1, y2, y3) :: t when y1 = x -> Some y2
33+
| _ :: t -> assoc3 x t
34+
;;
35+
36+
37+
38+
let usage_b (buf : Ext_buffer.t) speclist errmsg =
39+
let print_spec buf (key, spec, doc) =
40+
if doc <> "" then begin
41+
Ext_buffer.add_string buf " ";
42+
Ext_buffer.add_string_char buf key ' ';
43+
Ext_buffer.add_string_char buf doc '\n'
44+
end
45+
in
46+
47+
Ext_buffer.add_string_char buf errmsg '\n';
48+
Ext_list.iter speclist (print_spec buf)
49+
;;
50+
51+
52+
53+
let stop_raise progname (error : error) speclist errmsg =
54+
let b = Ext_buffer.create 200 in
55+
begin match error with
56+
| Unknown ("-help" | "--help" | "-h") ->
57+
usage_b b speclist errmsg;
58+
output_string stdout (Ext_buffer.contents b);
59+
exit 0
60+
61+
| Unknown s ->
62+
Ext_buffer.add_string_char b progname ':';
63+
Ext_buffer.add_string b " unknown option '";
64+
Ext_buffer.add_string b s ;
65+
Ext_buffer.add_string b "'.\n"
66+
| Missing s ->
67+
Ext_buffer.add_string_char b progname ':';
68+
Ext_buffer.add_string b " option '";
69+
Ext_buffer.add_string b s;
70+
Ext_buffer.add_string b "' needs an argument.\n"
71+
| Wrong (opt, arg, expected) ->
72+
Ext_buffer.add_string_char b progname ':';
73+
Ext_buffer.add_string b " wrong argument '";
74+
Ext_buffer.add_string b arg;
75+
Ext_buffer.add_string b "'; option '";
76+
Ext_buffer.add_string b opt;
77+
Ext_buffer.add_string b "' expects ";
78+
Ext_buffer.add_string b expected;
79+
Ext_buffer.add_string b ".\n"
80+
| Message s ->
81+
Ext_buffer.add_string_char b progname ':';
82+
Ext_buffer.add_char_string b ' ' s;
83+
Ext_buffer.add_string b ".\n"
84+
end;
85+
usage_b b speclist errmsg;
86+
raise (Bad (Ext_buffer.contents b))
87+
88+
89+
let parse_exn (speclist : t) anonfun errmsg =
90+
let argv = Sys.argv in
91+
let stop_raise error = stop_raise argv.(0) error speclist errmsg in
92+
let l = Array.length argv in
93+
let current = ref 1 in (* 0 is progname*)
94+
while !current < l do
95+
let s = argv.(!current) in
96+
if s <> "" && s.[0] = '-' then begin
97+
let action =
98+
match assoc3 s speclist with
99+
| Some action -> action
100+
| None -> stop_raise (Unknown s)
101+
in
102+
begin try
103+
let rec treat_action = function
104+
| Unit f -> f ();
105+
| Set r -> r := true;
106+
| String f when !current + 1 < l ->
107+
f argv.(!current + 1);
108+
incr current;
109+
| Set_string r when !current + 1 < l ->
110+
r := argv.(!current + 1);
111+
incr current;
112+
| Int f when !current + 1 < l ->
113+
let arg = argv.(!current + 1) in
114+
begin try f (int_of_string arg)
115+
with Failure "int_of_string" ->
116+
raise (Stop (Wrong (s, arg, "an integer")))
117+
end;
118+
incr current;
119+
| Set_int r when !current + 1 < l ->
120+
let arg = argv.(!current + 1) in
121+
begin try r := (int_of_string arg)
122+
with Failure "int_of_string" ->
123+
raise (Stop (Wrong (s, arg, "an integer")))
124+
end;
125+
incr current;
126+
| _ -> raise (Stop (Missing s))
127+
in
128+
treat_action action
129+
with Bad m -> stop_raise (Message m);
130+
| Stop e -> stop_raise e;
131+
end;
132+
incr current;
133+
end else begin
134+
(try anonfun s with Bad m -> stop_raise (Message m));
135+
incr current;
136+
end;
137+
done;
138+
;;
139+
140+
141+
142+
(* let parse l f msg =
143+
try
144+
parse_exn l f msg;
145+
with
146+
| Bad msg ->
147+
output_string stderr msg ; exit 2;
148+
| Help msg ->
149+
output_string stdout msg; exit 0;
150+
;;
151+
*)

jscomp/bsb_helper/bsb_arg.mli

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
4+
type spec =
5+
| Unit of (unit -> unit)
6+
| Set of bool ref
7+
| String of (string -> unit)
8+
| Set_string of string ref
9+
| Int of (int -> unit)
10+
| Set_int of int ref
11+
12+
type key = string
13+
type doc = string
14+
type usage_msg = string
15+
type anon_fun = (string -> unit)
16+
17+
val parse_exn :
18+
(key * spec * doc) list -> anon_fun -> usage_msg -> unit
19+
20+
21+

jscomp/bsb_helper/bsb_db_decode.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ let read_build_cache ~dir : t =
9393
Ext_io.load_file (Filename.concat dir bsbuild_cache) in
9494
decode_internal all_content (ref (Ext_digest.length + 1)), all_content
9595

96-
let cmp (a : string) b = String_map.compare_key a b
96+
(* Invariant: the same as encoding String_map.compare_key *)
97+
let cmp = Ext_string.compare
98+
9799

98100
let rec binarySearchAux (arr : string array) (lo : int) (hi : int) (key : string) : _ option =
99101
let mid = (lo + hi)/2 in
@@ -129,7 +131,7 @@ let find_opt_aux sorted key : _ option =
129131

130132

131133
type module_info = {
132-
case : Bsb_db.case;
134+
case : bool ; (* which is Bsb_db.case*)
133135
dir_name : string
134136
}
135137

jscomp/bsb_helper/bsb_db_decode.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ val read_build_cache :
4848

4949

5050
type module_info = {
51-
case : Bsb_db.case;
51+
case : bool (* Bsb_db.case*);
5252
dir_name : string
5353
}
5454

jscomp/common/js_config.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@
3030
(* let add_npm_package_path s =
3131
match !packages_info with
3232
| Empty ->
33-
Ext_pervasives.bad_argf "please set package name first using -bs-package-name ";
33+
Ext_arg.bad_argf "please set package name first using -bs-package-name ";
3434
| NonBrowser(name, envs) ->
3535
let env, path =
3636
match Ext_string.split ~keep_empty:false s ':' with
3737
| [ package_name; path] ->
3838
(match Js_packages_info.module_system_of_string package_name with
3939
| Some x -> x
4040
| None ->
41-
Ext_pervasives.bad_argf "invalid module system %s" package_name), path
41+
Ext_arg.bad_argf "invalid module system %s" package_name), path
4242
| [path] ->
4343
NodeJS, path
4444
| _ ->
45-
Ext_pervasives.bad_argf "invalid npm package path: %s" s
45+
Ext_arg.bad_argf "invalid npm package path: %s" s
4646
in
4747
packages_info := NonBrowser (name, ((env,path) :: envs)) *)
4848
(** Browser is not set via command line only for internal use *)

jscomp/core/js_cmj_format.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ let digest_length = 16 (*16 chars *)
8888
let verify_magic_in_beg ic =
8989
let buffer = really_input_string ic cmj_magic_number_length in
9090
if buffer <> cmj_magic_number then
91-
Ext_pervasives.failwithf ~loc:__LOC__
91+
Ext_fmt.failwithf ~loc:__LOC__
9292
"cmj files have incompatible versions, please rebuilt using the new compiler : %s"
9393
__LOC__
9494

@@ -116,7 +116,7 @@ let from_string s : t =
116116
if magic_number = cmj_magic_number then
117117
Marshal.from_string s (digest_length + cmj_magic_number_length)
118118
else
119-
Ext_pervasives.failwithf ~loc:__LOC__
119+
Ext_fmt.failwithf ~loc:__LOC__
120120
"cmj files have incompatible versions, please rebuilt using the new compiler : %s"
121121
__LOC__
122122

jscomp/core/js_exp_make.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,15 @@ let int32_lsl ?comment (e1 : J.expression) (e2 : J.expression) : J.expression =
11281128
expression_desc = Bin (Lsl, e1,e2)
11291129
}
11301130

1131+
let is_pos_pow n =
1132+
let module M = struct exception E end in
1133+
let rec aux c (n : Int32.t) =
1134+
if n <= 0l then -2
1135+
else if n = 1l then c
1136+
else if Int32.logand n 1l = 0l then
1137+
aux (c + 1) (Int32.shift_right n 1 )
1138+
else raise_notrace M.E in
1139+
try aux 0 n with M.E -> -1
11311140

11321141
let int32_mul ?comment
11331142
(e1 : J.expression)
@@ -1144,7 +1153,7 @@ let int32_mul ?comment
11441153
| e , {expression_desc = Number (Int {i = i0} | Uint i0 ); _}
11451154
| {expression_desc = Number (Int {i = i0} | Uint i0 ); _}, e
11461155
->
1147-
let i = Ext_pervasives.is_pos_pow i0 in
1156+
let i = is_pos_pow i0 in
11481157
if i >= 0 then
11491158
int32_lsl e (small_int i)
11501159
else

jscomp/core/js_implementation.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ let after_parsing_sig ppf outputprefix ast =
7878
initial_env sg ;
7979
end
8080
end
81-
let interface ppf sourcefile outputprefix =
81+
82+
83+
84+
let interface ppf fname outputprefix =
8285
Compmisc.init_path false;
83-
Ocaml_parse.parse_interface ppf sourcefile
86+
Pparse.parse_interface ~tool_name:Js_config.tool_name ppf fname
87+
|> Ppx_entry.rewrite_signature
8488
|> print_if ppf Clflags.dump_parsetree Printast.interface
8589
|> print_if ppf Clflags.dump_source Pprintast.signature
8690
|> after_parsing_sig ppf outputprefix
@@ -142,7 +146,8 @@ let after_parsing_impl ppf outputprefix ast =
142146
end
143147
let implementation ppf fname outputprefix =
144148
Compmisc.init_path false;
145-
Ocaml_parse.parse_implementation ppf fname
149+
Pparse.parse_implementation ~tool_name:Js_config.tool_name ppf fname
150+
|> Ppx_entry.rewrite_implementation
146151
|> print_if ppf Clflags.dump_parsetree Printast.implementation
147152
|> print_if ppf Clflags.dump_source Pprintast.structure
148153
|> after_parsing_impl ppf outputprefix

jscomp/core/js_packages_info.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,23 +228,23 @@ let get_output_dir
228228

229229
let add_npm_package_path (packages_info : t) (s : string) : t =
230230
if is_empty packages_info then
231-
Ext_pervasives.bad_argf "please set package name first using -bs-package-name "
231+
Ext_arg.bad_argf "please set package name first using -bs-package-name "
232232
else
233233
let module_system, path =
234234
match Ext_string.split ~keep_empty:false s ':' with
235235
| [ module_system; path] ->
236236
(match module_system_of_string module_system with
237237
| Some x -> x
238238
| None ->
239-
Ext_pervasives.bad_argf "invalid module system %s" module_system), path
239+
Ext_arg.bad_argf "invalid module system %s" module_system), path
240240
| [path] ->
241241
NodeJS, path
242242
| module_system :: path ->
243243
(match module_system_of_string module_system with
244244
| Some x -> x
245-
| None -> Ext_pervasives.bad_argf "invalid module system %s" module_system), (String.concat ":" path)
245+
| None -> Ext_arg.bad_argf "invalid module system %s" module_system), (String.concat ":" path)
246246
| _ ->
247-
Ext_pervasives.bad_argf "invalid npm package path: %s" s
247+
Ext_arg.bad_argf "invalid npm package path: %s" s
248248
in
249249
{ packages_info with module_systems = {module_system; path}::packages_info.module_systems}
250250

0 commit comments

Comments
 (0)