Skip to content

Std.Process

effect Args

The arguments of the process, in order, without the name of the program.

  • fn get(index: Int) -> Option[String]

    The argument at index (from 0), or None when index is negative or not less than the number of arguments.

  • fn list() -> List[String]

    The arguments in the order given, without the name of the program.

error InvalidInput

Standard input is not valid UTF-8.

error StdinFailure(message: String)

The host cannot read standard input. message is the host’s text.

  • message: String
effect Stdin

Reads standard input as text (UTF-8).

  • suspend fn read_line() -> Option[String] fails InvalidInput | StdinFailure

    The next line without its line terminator (\n, or \r\n), or None at the end of the input.

  • suspend fn read_text() -> String fails InvalidInput | StdinFailure

    The rest of the input, line terminators included, or "" at the end of the input.

handler SystemArgs: Args
uses { ProcessHost }

The arguments given by the host: argv[1] and after on native, what the launcher of emela run passes on JS, and get-arguments without its first element on WASM. Has no state.

handler SystemStdin: Stdin
uses { Async, ProcessHost }

The standard input of the process (JS process.stdin, file descriptor 0 on native). The host starts reading only when an operation first waits, and does not keep the program running while no operation waits. Requires Async. Has no state.

type StdinLines(lines: List[String])

The state of TestStdin: the lines not read yet.

  • lines: List[String]
handler TestStdin: Stdin
uses { State[StdinLines] }

The test handler. It keeps the lines in State[StdinLines] and resumes at once, so use it inside State.run:

(line, rest) = State.run(StdinLines(lines: ["a", "b"]), || {
with TestStdin {
stdin <- Stdin
stdin.read_line()
}
})

read_line removes the first line and returns it, or None when no line is left. read_text returns the lines left joined with \n and leaves none. It never fails.

effect Stdout

Writes to the standard output of the process. Does not fail and does not suspend.

  • fn print(text: String) -> ()

    Writes text as is.

  • fn print_line(text: String) -> ()

    Writes text, then \n.

effect Stderr

Writes to the standard error of the process. Does not fail and does not suspend.

  • fn print(text: String) -> ()

    Writes text as is.

  • fn print_line(text: String) -> ()

    Writes text, then \n.

handler SystemStdout: Stdout
uses { ProcessHost }

The standard output of the process: file descriptor 1, written synchronously, on JS, stdout on native, and wasi:cli/stdout on WASM. Has no state.

handler SystemStderr: Stderr
uses { ProcessHost }

The standard error of the process: file descriptor 2, written synchronously, on JS, stderr on native, and wasi:cli/stderr on WASM. Has no state.

type StdoutText(text: String)

The state of TestStdout: everything written so far.

  • text: String
type StderrText(text: String)

The state of TestStderr: everything written so far.

  • text: String
handler TestStdout: Stdout
uses { State[StdoutText] }

The test handler of Stdout. It appends what is written to text of State[StdoutText], so use it inside State.run:

(_, out) = State.run(StdoutText(text: ""), || with TestStdout {
stdout <- Stdout
stdout.print("a")
})
assert out.text == "a"

print_line appends \n after the text.

handler TestStderr: Stderr
uses { State[StderrText] }

The test handler of Stderr, the same as TestStdout with State[StderrText].

layer Process { SystemConsole, SystemClock, SystemRandom, SystemHttp, SystemArgs, SystemStdin, SystemStdout, SystemStderr, SystemEnv, SystemFileSystem, SystemTcp }
uses { Async, ProcessHost }

Platform and the System handlers that need a process: the arguments, the standard streams, the environment variables, the file system and TCP. Requires Async and ProcessHost ([effect.host]), which main, tests and Http.serve are given. A program that uses none of these writes with Platform { ... } instead.

opaque enum ExitCode

The exit code of a program, an Int from 0 to 255. A main that returns one exits with it ([dyn.error]). Making one uses no effect.

fn exit_code(code: Int) -> ExitCode

The exit code code. A defect if code is outside 0 to 255: the exit status of a POSIX process and the argument of exit-with-code of WASI are 8 bits wide.

fn exit_code_to_int(code: ExitCode) -> Int

The number of the exit code.

const EXIT_SUCCESS: ExitCode

The exit code 0.

const EXIT_FAILURE: ExitCode

The exit code 1.