Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/compile-request.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Read bsc's flags into a typed request, and reject invalid values such as an unknown experimental feature, color setting or genType module.
69 changes: 5 additions & 64 deletions compiler/backend/bsc_args.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,12 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

type string_action =
| String_call of (string -> unit)
| String_optional_set of string option ref
| String_list_add of string list ref

type unit_action =
| Unit_call of (unit -> unit)
| Unit_set of bool ref
| Unit_clear of bool ref

type spec = Unit_dummy | Unit of unit_action | String of string_action

exception Bad = Arg.Bad
exception Help of string

let bad_arg s = raise_notrace (Bad s)

type error = Unknown of string | Missing of string

type t = spec Ext_spec.t
type t = (string * string) array

let ( +> ) = Buffer.add_string

Expand All @@ -50,10 +36,10 @@ let usage_b (buf : Buffer.t) ~usage (speclist : t) =
buf +> "\nOptions:\n";
let max_col = ref 0 in
speclist
|> Array.iter (fun (key, _, _) ->
|> Array.iter (fun (key, _) ->
if String.length key > !max_col then max_col := String.length key);
speclist
|> Array.iter (fun (key, _, doc) ->
|> Array.iter (fun (key, doc) ->
if not (Ext_string.starts_with doc "*internal*") then (
buf +> " ";
buf +> key;
Expand All @@ -77,52 +63,7 @@ let usage_b (buf : Buffer.t) ~usage (speclist : t) =
done;
buf +> "\n"))

let stop_raise ~usage ~(error : error) (speclist : t) =
let usage_string ~usage (speclist : t) =
let b = Buffer.create 200 in
(match error with
| Unknown ("-help" | "--help" | "-h") ->
usage_b b ~usage speclist;
raise_notrace (Help (Buffer.contents b))
| Unknown s ->
b +> "Unknown option \"";
b +> s;
b +> "\".\n"
| Missing s ->
b +> "Option \"";
b +> s;
b +> "\" needs an argument.\n");
usage_b b ~usage speclist;
bad_arg (Buffer.contents b)

let parse_exn ~usage ~argv ?(start = 1) (speclist : t)
(anonfun : rev_args:string list -> unit) =
let finish = Array.length argv in
let current = ref start in
let rev_list = ref [] in
while !current < finish do
let s = argv.(!current) in
incr current;
if s <> "" && s.[0] = '-' then
match Ext_spec.assoc3 speclist s with
| Some action -> (
match action with
| Unit_dummy -> ()
| Unit r -> (
match r with
| Unit_set r -> r := true
| Unit_clear r -> r := false
| Unit_call f -> f ())
| String f -> (
if !current >= finish then
stop_raise ~usage ~error:(Missing s) speclist
else
let arg = argv.(!current) in
incr current;
match f with
| String_call f -> f arg
| String_optional_set s -> s := Some arg
| String_list_add s -> s := arg :: !s))
| None -> stop_raise ~usage ~error:(Unknown s) speclist
else rev_list := s :: !rev_list
done;
anonfun ~rev_args:!rev_list
Buffer.contents b
24 changes: 4 additions & 20 deletions compiler/backend/bsc_args.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,13 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

type string_action =
| String_call of (string -> unit)
| String_optional_set of string option ref
| String_list_add of string list ref

type unit_action =
| Unit_call of (unit -> unit)
| Unit_set of bool ref
| Unit_clear of bool ref

type spec = Unit_dummy | Unit of unit_action | String of string_action

type t = (string * spec * string) array
type t = (string * string) array
(** Options and their documentation. *)

exception Bad of string
exception Help of string

val bad_arg : string -> 'a

val parse_exn :
usage:string ->
argv:string array ->
?start:int ->
t ->
(rev_args:string list -> unit) ->
unit
val usage_string : usage:string -> t -> string
(** The usage line followed by the documented options of [t]. *)
36 changes: 0 additions & 36 deletions compiler/backend/ext_spec.ml

This file was deleted.

3 changes: 0 additions & 3 deletions compiler/backend/ext_spec.mli

This file was deleted.

52 changes: 32 additions & 20 deletions compiler/backend/js_packages_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,38 @@ let query_package_infos ({name; module_systems} : t)
Package_found {rel_path; pkg_rel_path; suffix = k.suffix}
| None -> Package_not_found)

let package_info_of_string (s : string) : package_info =
let handle_module_system module_system =
match module_system_of_string module_system with
| Some x -> x
| None -> Bsc_args.bad_arg ("invalid module system " ^ module_system)
in
match Ext_string.split ~keep_empty:true s ':' with
| [path] -> {module_system = Esmodule; path; suffix = Literals.suffix_js}
| [module_system; path] ->
{
module_system = handle_module_system module_system;
path;
suffix = Literals.suffix_js;
}
| [module_system; path; suffix] ->
{module_system = handle_module_system module_system; path; suffix}
| _ -> Bsc_args.bad_arg ("invalid npm package path: " ^ s)

let string_of_package_info {module_system; path; suffix} =
let module_system =
match module_system with
| Commonjs -> "commonjs"
| Esmodule -> "esmodule"
in
String.concat ":" [module_system; path; suffix]

let add_package_info (packages_info : t) (m : package_info) : t =
if is_empty packages_info then
Bsc_args.bad_arg "Set package name first using -bs-package-name"
else {packages_info with module_systems = m :: packages_info.module_systems}

let add_npm_package_path (packages_info : t) (s : string) : t =
if is_empty packages_info then
Bsc_args.bad_arg "Set package name first using -bs-package-name"
else
let handle_module_system module_system =
match module_system_of_string module_system with
| Some x -> x
| None -> Bsc_args.bad_arg ("invalid module system " ^ module_system)
in
let m =
match Ext_string.split ~keep_empty:true s ':' with
| [path] -> {module_system = Esmodule; path; suffix = Literals.suffix_js}
| [module_system; path] ->
{
module_system = handle_module_system module_system;
path;
suffix = Literals.suffix_js;
}
| [module_system; path; suffix] ->
{module_system = handle_module_system module_system; path; suffix}
| _ -> Bsc_args.bad_arg ("invalid npm package path: " ^ s)
in
{packages_info with module_systems = m :: packages_info.module_systems}
else add_package_info packages_info (package_info_of_string s)
7 changes: 7 additions & 0 deletions compiler/backend/js_packages_info.mli
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ val from_name : string -> t
val is_empty : t -> bool

val add_npm_package_path : t -> string -> t

val package_info_of_string : string -> package_info
(** Reads [module_system:path:suffix], as given to [-bs-package-output]. *)

val string_of_package_info : package_info -> string

val add_package_info : t -> package_info -> t
(** used by command line option
e.g [-bs-package-output commonjs:xx/path]
*)
Expand Down
5 changes: 5 additions & 0 deletions compiler/backend/js_packages_state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ let set_package_map module_name =
(Clflags.current ()).open_modules :=
module_name :: !((Clflags.current ()).open_modules)

let add_package_output info =
let state = current () in
state.packages_info <-
Js_packages_info.add_package_info state.packages_info info

let update_npm_package_path s =
let state = current () in
state.packages_info <-
Expand Down
2 changes: 2 additions & 0 deletions compiler/backend/js_packages_state.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ val set_package_map : string -> unit
val get_packages_info : unit -> Js_packages_info.t

val update_npm_package_path : string -> unit

val add_package_output : Js_packages_info.package_info -> unit
Loading
Loading