Skip to content

Commit

Permalink
group com.main/main_class
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Jan 28, 2024
1 parent fe395ef commit bf329ea
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/compiler/args.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ let parse_args com =
),"<name[=path]>","generate code for a custom target");
("Target",[],["-x"], Arg.String (fun cl ->
let cpath = Path.parse_type_path cl in
(match com.main_class with
(match com.main.main_class with
| Some c -> if cpath <> c then raise (Arg.Bad "Multiple --main classes specified")
| None -> com.main_class <- Some cpath);
| None -> com.main.main_class <- Some cpath);
actx.classes <- cpath :: actx.classes;
Common.define com Define.Interp;
set_platform com Eval "";
Expand All @@ -138,9 +138,9 @@ let parse_args com =
actx.hxb_libs <- lib :: actx.hxb_libs
),"<path>","add a hxb library");
("Compilation",["-m";"--main"],["-main"],Arg.String (fun cl ->
if com.main_class <> None then raise (Arg.Bad "Multiple --main classes specified");
if com.main.main_class <> None then raise (Arg.Bad "Multiple --main classes specified");
let cpath = Path.parse_type_path cl in
com.main_class <- Some cpath;
com.main.main_class <- Some cpath;
actx.classes <- cpath :: actx.classes
),"<class>","select startup class");
("Compilation",["-L";"--library"],["-lib"],Arg.String (fun _ -> ()),"<name[:ver]>","use a haxelib library");
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,15 @@ let finalize_typing ctx tctx =
enter_stage com CFilteringStart;
ServerMessage.compiler_stage com;
let main, types, modules = run_or_diagnose ctx (fun () -> Finalization.generate tctx) in
com.main <- main;
com.main.main_expr <- main;
com.types <- types;
com.modules <- modules;
t()

let filter ctx tctx before_destruction =
let t = Timer.timer ["filters"] in
DeprecationCheck.run ctx.com;
run_or_diagnose ctx (fun () -> Filters.run tctx ctx.com.main before_destruction);
run_or_diagnose ctx (fun () -> Filters.run tctx ctx.com.main.main_expr before_destruction);
t()

let compile ctx actx callbacks =
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/displayProcessing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ let process_display_file com actx =
DPKNone
| DFPOnly when (DisplayPosition.display_position#get).pfile = file_input_marker ->
actx.classes <- [];
com.main_class <- None;
com.main.main_class <- None;
begin match com.file_contents with
| [_, Some input] ->
com.file_contents <- [];
Expand All @@ -154,7 +154,7 @@ let process_display_file com actx =
| dfp ->
if dfp = DFPOnly then begin
actx.classes <- [];
com.main_class <- None;
com.main.main_class <- None;
end;
let real = Path.get_real_path (DisplayPosition.display_position#get).pfile in
let path = match get_module_path_from_file_path com real with
Expand Down
23 changes: 16 additions & 7 deletions src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ class virtual abstract_hxb_lib = object(self)
method virtual get_file_path : string
end

type context_main = {
mutable main_class : path option;
mutable main_expr : texpr option;
}

type context = {
compilation_step : int;
mutable stage : compiler_stage;
Expand All @@ -359,7 +364,7 @@ type context = {
mutable config : platform_config;
empty_class_path : ClassPath.class_path;
class_paths : ClassPaths.class_paths;
mutable main_class : path option;
main : context_main;
mutable package_rules : (string,package_rule) PMap.t;
mutable report_mode : report_mode;
(* communication *)
Expand Down Expand Up @@ -400,7 +405,6 @@ type context = {
mutable file : string;
mutable features : (string,bool) Hashtbl.t;
mutable modules : Type.module_def list;
mutable main : Type.texpr option;
mutable types : Type.module_type list;
mutable resources : (string,string) Hashtbl.t;
(* target-specific *)
Expand Down Expand Up @@ -819,7 +823,10 @@ let create compilation_step cs version args display_mode =
run_command_args = (fun s args -> com.run_command (Printf.sprintf "%s %s" s (String.concat " " args)));
empty_class_path = new ClassPath.directory_class_path "" User;
class_paths = new ClassPaths.class_paths;
main_class = None;
main = {
main_class = None;
main_expr = None;
};
package_rules = PMap.empty;
file = "";
types = [];
Expand All @@ -828,7 +835,6 @@ let create compilation_step cs version args display_mode =
modules = [];
module_lut = new module_lut;
module_nonexistent_lut = new hashtbl_lookup;
main = None;
flash_version = 10.;
resources = Hashtbl.create 0;
net_std = [];
Expand Down Expand Up @@ -907,7 +913,10 @@ let clone com is_macro_context =
tbool = mk_mono();
tstring = mk_mono();
};
main_class = None;
main = {
main_class = None;
main_expr = None;
};
features = Hashtbl.create 0;
callbacks = new compiler_callbacks;
display_information = {
Expand Down Expand Up @@ -1231,6 +1240,6 @@ let get_entry_point com =
| Some c when (PMap.mem "main" c.cl_statics) -> c
| _ -> Option.get (ExtList.List.find_map (fun t -> match t with TClassDecl c when c.cl_path = path -> Some c | _ -> None) m.m_types)
in
let e = Option.get com.main in (* must be present at this point *)
let e = Option.get com.main.main_expr in (* must be present at this point *)
(snd path, c, e)
) com.main_class
) com.main.main_class
6 changes: 3 additions & 3 deletions src/generators/gencpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8478,7 +8478,7 @@ let generate_cppia ctx =
);
) common_ctx.types;

(match common_ctx.main with
(match common_ctx.main.main_expr with
| None -> script#writeOpLine IaNoMain;
| Some e -> script#writeOpLine IaMain;
script#gen_expression e
Expand Down Expand Up @@ -8590,7 +8590,7 @@ let generate_source ctx =
List.iter (fun job -> job () ) !jobs;


(match common_ctx.main with
(match common_ctx.main.main_expr with
| None -> generate_dummy_main common_ctx
| Some e ->
let main_field = { (mk_field "__main__" t_dynamic e.epos null_pos) with
Expand Down Expand Up @@ -8653,7 +8653,7 @@ let generate_source ctx =
end;
end;

let output_name = match common_ctx.main_class with
let output_name = match common_ctx.main.main_class with
| Some path -> (snd path)
| _ -> "output" in

Expand Down
2 changes: 1 addition & 1 deletion src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4187,7 +4187,7 @@ let generate com =

let ctx = create_context com false dump in
add_types ctx com.types;
let code = build_code ctx com.types com.main in
let code = build_code ctx com.types com.main.main_expr in
Array.sort (fun (lib1,_,_,_) (lib2,_,_,_) -> lib1 - lib2) code.natives;
if dump then begin
(match ctx.dump_out with None -> () | Some ch -> IO.close_out ch);
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ let generate com =
end;
List.iter (gen_block_element ~newline_after:true ~keep_blocks:(ctx.es_version >= 6) ctx) (List.rev ctx.inits);
List.iter (generate_static ctx) (List.rev ctx.statics);
(match com.main with
(match com.main.main_expr with
| None -> ()
| Some e -> gen_expr ctx e; newline ctx);
if ctx.js_modern then begin
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genlua.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,7 @@ let generate com =
gen_value ctx { e with eexpr = TFunction fn; etype = TFun ([],com.basic.tvoid) };
println ctx ", _hx_handle_error)";
println ctx "if not success then _G.error(err) end";
) com.main;
) com.main.main_expr;

if anyExposed then
println ctx "return _hx_exports";
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genneko.ml
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ let generate com =
{ psource = "<header>"; pline = 1; }
) in
let el = build ctx com.types in
let emain = (match com.main with None -> [] | Some e -> [gen_expr ctx e]) in
let emain = (match com.main.main_expr with None -> [] | Some e -> [gen_expr ctx e]) in
let e = (EBlock ((header()) @ libs :: el @ emain), null_pos) in
let source = Common.defined com Define.NekoSource in
let use_nekoc = Common.defined com Define.UseNekoc in
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genphp7.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4038,7 +4038,7 @@ class generator (ctx:php_generator_context) =
Returns PHP code for entry point
*)
method private get_entry_point : (string * string) option =
match ctx.pgc_common.main with
match ctx.pgc_common.main.main_expr with
| None -> None
| Some expr ->
let writer = new code_writer ctx ([], "") "" in
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genpy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ module Generator = struct
List.iter (fun f -> f()) (List.rev ctx.class_inits)

let gen_main ctx =
match ctx.com.main with
match ctx.com.main.main_expr with
| None ->
()
| Some e ->
Expand Down
4 changes: 2 additions & 2 deletions src/generators/genswf9.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ let generate_inits ctx =
j()
| _ -> ()
) ctx.com.types;
(match ctx.com.main with
(match ctx.com.main.main_expr with
| None -> ()
| Some e -> gen_expr ctx false e);
write ctx HRetVoid;
Expand Down Expand Up @@ -2888,7 +2888,7 @@ let generate com boot_name =
try_scope_reg = None;
for_call = false;
} in
let types = if ctx.swc && com.main_class = None then
let types = if ctx.swc && com.main.main_class = None then
(*
make sure that both Boot and RealBoot are the first two classes in the SWC
this way initializing RealBoot will also run externs __init__ blocks before
Expand Down
2 changes: 1 addition & 1 deletion src/macro/eval/evalStdLib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2632,7 +2632,7 @@ module StdSys = struct
let programPath = vfun0 (fun () ->
let ctx = get_ctx() in
let com = ctx.curapi.get_com() in
match com.main_class with
match com.main.main_class with
| None -> vnull
| Some p ->
match ctx.curapi.get_type (s_type_path p) with
Expand Down
6 changes: 3 additions & 3 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@ let macro_api ccom get_api =
let api = encode_obj [
"outputFile", encode_string com.file;
"types", encode_array (List.map (fun t -> encode_type (type_of_module_type t)) com.types);
"main", (match com.main with None -> vnull | Some e -> encode_texpr e);
"main", (match com.main.main_expr with None -> vnull | Some e -> encode_texpr e);
"generateValue", vfun1 (fun v ->
let e = decode_texpr v in
let str = Genjs.gen_single_expr js_ctx e false in
Expand Down Expand Up @@ -2253,7 +2253,7 @@ let macro_api ccom get_api =
"platform", encode_platform com.platform;
"platformConfig", encode_platform_config com.config;
"stdPath", encode_array (List.map (fun path -> encode_string path#path) com.class_paths#get_std_paths);
"mainClass", (match com.main_class with None -> vnull | Some path -> encode_path path);
"mainClass", (match com.main.main_class with None -> vnull | Some path -> encode_path path);
"packageRules", encode_string_map encode_package_rule com.package_rules;
]
);
Expand All @@ -2262,7 +2262,7 @@ let macro_api ccom get_api =
vnull
);
"get_main_expr", vfun0 (fun() ->
match (ccom()).main with None -> vnull | Some e -> encode_texpr e
match (ccom()).main.main_expr with None -> vnull | Some e -> encode_texpr e
);
"get_module_types", vfun0 (fun() ->
encode_array (List.map encode_module_type (ccom()).types)
Expand Down
2 changes: 1 addition & 1 deletion src/typing/finalization.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open Typecore
(* FINALIZATION *)

let get_main ctx types =
match ctx.com.main_class with
match ctx.com.main.main_class with
| None -> None
| Some path ->
let p = null_pos in
Expand Down
3 changes: 1 addition & 2 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,6 @@ let create_macro_context com =
let com2 = Common.clone com true in
com.get_macros <- (fun() -> Some com2);
com2.package_rules <- PMap.empty;
com2.main_class <- None;
(* Inherit most display settings, but require normal typing. *)
com2.display <- {com.display with dms_kind = DMNone; dms_full_typing = true; dms_force_macro_typing = true; dms_inline = true; };
com2.class_paths#lock_context "macro" false;
Expand Down Expand Up @@ -1070,7 +1069,7 @@ let interpret ctx =
let mctx = get_macro_context ctx in
let mctx = Interp.create ctx.com (make_macro_api ctx mctx null_pos) false in
Interp.add_types mctx ctx.com.types (fun t -> ());
match ctx.com.main with
match ctx.com.main.main_expr with
| None -> ()
| Some e -> ignore(Interp.eval_expr mctx e)

Expand Down

0 comments on commit bf329ea

Please sign in to comment.