The runtime
What every program carries: its exit, its teardown, its heap.
One runtime
Every program carries the runtime, System.Runtime, loaded as an implicit import. It is the same on every target and reaches the OS through the target's platform unit (System.Runtime.Win64, System.Runtime.Linux64).
Exit codes
ExitCode is the exit code the process ends with: 0 at the start, read and written by any module with no import, and the value it holds once every teardown has run. Windows keeps all 32 bits, Linux the low 8, so 0..255 is portable. return <code>; in an exe's main block ends the program with that exit code. AtExit(handler) runs a handler as the process ends, by whatever exit (the main block's end, Halt, a C exit), the last one registered first: a program's handlers run before the modules' finalize sections, which were registered as it started, and every one sees the ExitCode the program ended with.
module exe runtime_exit;
// Runs as the process ends, whatever ended it
func Goodbye();
begin
println("goodbye with {}", ExitCode);
end;
finalize
begin
println("finalize sees {}", ExitCode);
end;
begin
AtExit(Goodbye);
println("working");
return 3;
end.
working
goodbye with 3
finalize sees 3
Halt
Halt(N) ends the process at once with exit code N through the exit handlers: each module's finalize still runs.
module exe runtime_halt;
// Stops the program when AValue is out of range
func Check(const AValue: int64);
begin
if AValue < 0 then
Halt(2);
end;
println("in range");
end;
finalize
begin
println("finalize runs on Halt too");
end;
begin
println("checking the input");
Check(-1);
println("never printed");
end.
checking the input
finalize runs on Halt too
The heap report
A debug build counts every heap block it hands out and takes back and prints the heap report as the process ends; a release build counts nothing and prints no report. FlushOutput() writes out what stdout still holds, so a line shows as it is printed even when stdout is a file or a pipe.