Skip to content

Current Process ​

The ambient process namespace describes the running Tyrus program. These values are available without importing tyrus:process; that module is used to start and manage child processes.

API Reference ​

MemberType or signatureDescription
process.argvstring[]Command-line arguments. Index 0 is the executable path; user arguments begin at index 1.
process.pidu32Process identifier of the running program.
process.ppidu32Process identifier of its parent.
process.cwd()() => stringReturns the current working directory.
process.uptime()() => u64Returns elapsed runtime in milliseconds.
process.memoryUsage()() => MemoryInfoReturns resident memory usage in bytes.
process.cpuUsage()() => CpuUsageReturns cumulative user and system CPU time in microseconds.
process.exit(code)(code: i32) => neverEnds the program with the given exit code.

Command-line Arguments ​

Arguments after -- in tyrus run are passed to the program instead of the compiler. Array.at() returns string?, so handle an argument that may be absent explicitly.

tyrus
const main = (): void => {
    const args: string[] = process.argv.slice(1);
    const target: string = args.at(0) ?? "world";
    print(`hello ${target}`);
};

Because a compiled Tyrus program is the executable, the user's first argument is at index 1. Code ported from a runtime where index 0 is an interpreter and index 1 is the script should use slice(1), not slice(2).

Process Information ​

process.memoryUsage() returns this value:

tyrus
interface MemoryInfo {
    readonly rss: u64;
}

rss is the process's resident set size in bytes.

process.cpuUsage() returns this value:

tyrus
interface CpuUsage {
    readonly user: u64;
    readonly system: u64;
}

user and system are cumulative CPU time in microseconds since the process started, not percentages. Unix targets use getrusage; Windows converts the GetProcessTimes 100-nanosecond counters to microseconds. Other targets return 0 for both fields.

tyrus
const main = (): void => {
    print(`pid ${process.pid}, ppid ${process.ppid}`);
    print(`running from ${process.cwd()}`);
    print(`up for ${process.uptime()}ms`);

    const memory = process.memoryUsage();
    print(`rss ${memory.rss} bytes`);

    const cpu = process.cpuUsage();
    print(`cpu ${cpu.user}us user, ${cpu.system}us system`);
};

process.exit(code) ends the program immediately, so statements after it are unreachable. A program that returns normally exits with code 0.