The io module

The io module provides a set of functions for manipulating input and output streams.

Free functions

stderr(): native

Returns a reference to the standard error stream.

stdin(): native

Returns a reference to the standard input stream.

stdout(): native

Returns a reference to the standard output stream.

Submodule inStream

This submodule provides a set of functions for manipulating input streams. It is also the metatable for input streams.

Member functions

close(self): null

Closes the stream.

read(self, buffer: bytes | null): number

Reads a sequence of bytes from the stream into the buffer. Returns the number of bytes read. If buffer is not provided or is null, the function reads and returns a single byte. Returns null if the end of the stream is reached.

readAll(self): bytes

Reads all bytes from the stream and returns them.

readLine(self): string

Reads a line from the stream and returns it.

readText(self): string

Reads all text from the stream and returns it.

use(self, f: callable[native → any]): any

Calls the function f with the stream as its argument and returns the result. The stream is closed after the function returns, even if it throws an error.

Submodule outStream

This submodule provides a set of functions for manipulating output streams. It is also the metatable for output streams.

Member functions

close(self): null

Closes the stream.

flush(self): null

Flushes the stream.

write(self, buffer: bytes, off: number | null, len: number | null): number

Writes a sequence of bytes from the buffer to the stream. Returns the number of bytes written. If off is provided, the function starts writing from the byte at that offset. If len is provided, the function writes at most that many bytes. If off and len are both provided, the function writes at most len bytes starting from the byte at offset off. If off is not provided, the function starts writing from the first byte in the buffer. If len is not provided, the function writes to the end of the buffer.

writeLine(self, line: string): null

Writes a line to the stream.

writeText(self, text: string): null

Writes text to the stream.

use(self, f: callable[native → any]): any

Calls the function f with the stream as its argument and returns the result. The stream is closed after the function returns, even if it throws an error.