diff --git a/src/compiler/args.ml b/src/compiler/args.ml index 81721e90f68..1f7d09b0d00 100644 --- a/src/compiler/args.ml +++ b/src/compiler/args.ml @@ -111,9 +111,9 @@ let parse_args com = ),"","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 ""; @@ -138,9 +138,9 @@ let parse_args com = actx.hxb_libs <- lib :: actx.hxb_libs ),"","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 ),"","select startup class"); ("Compilation",["-L";"--library"],["-lib"],Arg.String (fun _ -> ()),"","use a haxelib library"); diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index 078b63fa1a7..3654a6e0a5d 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -336,7 +336,7 @@ 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() @@ -344,7 +344,7 @@ let finalize_typing ctx tctx = 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 = diff --git a/src/compiler/displayProcessing.ml b/src/compiler/displayProcessing.ml index 29e1fccbf46..d2dde9cef58 100644 --- a/src/compiler/displayProcessing.ml +++ b/src/compiler/displayProcessing.ml @@ -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 <- []; @@ -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 diff --git a/src/context/common.ml b/src/context/common.ml index 6512b412c52..59359f5d01a 100644 --- a/src/context/common.ml +++ b/src/context/common.ml @@ -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; @@ -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 *) @@ -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 *) @@ -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 = []; @@ -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 = []; @@ -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 = { @@ -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 diff --git a/src/generators/gencpp.ml b/src/generators/gencpp.ml index a5c7a2d965b..05fcb19cf10 100644 --- a/src/generators/gencpp.ml +++ b/src/generators/gencpp.ml @@ -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 @@ -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 @@ -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 diff --git a/src/generators/genhl.ml b/src/generators/genhl.ml index ccfbb8d028d..e3ba1481b68 100644 --- a/src/generators/genhl.ml +++ b/src/generators/genhl.ml @@ -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); diff --git a/src/generators/genjs.ml b/src/generators/genjs.ml index 72ab5dad9c6..05eae361308 100644 --- a/src/generators/genjs.ml +++ b/src/generators/genjs.ml @@ -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 diff --git a/src/generators/genlua.ml b/src/generators/genlua.ml index 9811be22a5d..176185edbcf 100644 --- a/src/generators/genlua.ml +++ b/src/generators/genlua.ml @@ -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"; diff --git a/src/generators/genneko.ml b/src/generators/genneko.ml index 27c81c1f116..ce081ea0f7b 100644 --- a/src/generators/genneko.ml +++ b/src/generators/genneko.ml @@ -780,7 +780,7 @@ let generate com = { psource = "
"; 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 diff --git a/src/generators/genphp7.ml b/src/generators/genphp7.ml index 0b242cf982a..c36622206ff 100644 --- a/src/generators/genphp7.ml +++ b/src/generators/genphp7.ml @@ -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 diff --git a/src/generators/genpy.ml b/src/generators/genpy.ml index ab9168472e7..1a59a94fb3e 100644 --- a/src/generators/genpy.ml +++ b/src/generators/genpy.ml @@ -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 -> diff --git a/src/generators/genswf9.ml b/src/generators/genswf9.ml index 061f47f148b..6495c7a7456 100644 --- a/src/generators/genswf9.ml +++ b/src/generators/genswf9.ml @@ -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; @@ -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 diff --git a/src/macro/eval/evalStdLib.ml b/src/macro/eval/evalStdLib.ml index 7cdb911bb58..9212e689728 100644 --- a/src/macro/eval/evalStdLib.ml +++ b/src/macro/eval/evalStdLib.ml @@ -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 diff --git a/src/macro/macroApi.ml b/src/macro/macroApi.ml index 818ac473346..bea386127d7 100644 --- a/src/macro/macroApi.ml +++ b/src/macro/macroApi.ml @@ -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 @@ -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; ] ); @@ -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) diff --git a/src/typing/finalization.ml b/src/typing/finalization.ml index 7ce75e2a14c..61953a43f75 100644 --- a/src/typing/finalization.ml +++ b/src/typing/finalization.ml @@ -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 diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index a9b0252c67d..5a826098df5 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -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; @@ -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)