(* Signal handlers can run on any domain, including between spawning a child and registering its cleanup owner. Recording the request and raising from the command's poll point keeps unwinding on the domain that owns scheduler, lock, and temporary-output cleanup. *) let with_termination_handlers action = let requested_exit = Atomic.make 0 in let interrupt signal = ignore (Atomic.compare_and_set requested_exit 0 (Termination_signals.exit_code signal)) in let poll () = let exit_code = Atomic.get requested_exit in if exit_code <> 0 then raise (Process.Interrupted exit_code) in let result = Termination_signals.with_handlers interrupt (fun () -> action ~poll) in poll (); result let run_command ~poll = function | Cli.Build { verbosity; folder; prod; features; warn_error; after_build; filter; clear_screen; no_timing; } -> ignore clear_screen; Build.run ~poll ~verbosity ~folder ~prod ~features ~warn_error ~after_build ~filter ~no_timing | Cli.Watch { verbosity; folder; prod; features; warn_error; after_build; filter; clear_screen; no_timing; } -> ignore no_timing; Build.watch ~verbosity ~folder ~prod ~features ~warn_error ~after_build ~filter ~clear_screen | Cli.Format (Cli.Format_stdin extension) -> Format.format_stdin ~poll extension | Cli.Format (Cli.Format_files {verbosity; check; paths}) -> Format.run_files ~poll ~verbosity ~check paths | Cli.Compiler_args {verbosity; path} -> print_endline (Compiler_args_command.run ~verbosity path) | Cli.Clean {verbosity; folder; prod} -> Clean.run ~poll ~verbosity ~folder ~prod let run = function | Cli.Watch _ as command -> run_command ~poll:(fun () -> ()) command | command -> with_termination_handlers (fun ~poll -> run_command ~poll command) (* For the compiler benchmarks (scripts/compilerBenchmarks.js): the runtime's report at exit only counts the main domain's allocations, and the compiler runs on worker domains. *) let report_worker_allocations () = if Option.is_some (Sys.getenv_opt "RESCRIPT_REPORT_WORKER_ALLOCATIONS") then at_exit (fun () -> Printf.eprintf "worker_allocated_words: %d\n%!" (Process.worker_allocated_words ())) let main () = Platform.configure_standard_streams (); report_worker_allocations (); try match Cli.eval Sys.argv with | Cli.Run command -> run command | Cli.Exit code -> exit code with | Project_context.Package_error message | Config.Error message | Source.Error message | Project_context.Error message | Process.Error message | Format.Error message -> prerr_endline message; exit 1 | Watcher.Stop -> exit 0 | Process.Interrupted exit_code -> exit exit_code | (Sys_error _ as exn) | (Unix.Unix_error _ as exn) -> prerr_endline (Printexc.to_string exn); exit 1 let () = main ()