io

package
v1.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package io provides I/O primitives for reading and writing.

Textual Read (R7RS 6.13.2)

  • read, read-char, peek-char, char-ready?, read-line, read-string
  • read-syntax, read-token

Textual Write (R7RS 6.13.3)

  • write, display, newline, write-char, write-string
  • write-simple, write-shared
  • flush-output-port

Binary I/O

  • read-u8, peek-u8, u8-ready?, write-u8
  • read-bytevector, read-bytevector!, write-bytevector

Port Predicates

  • port?, input-port?, output-port?
  • textual-port?, binary-port?
  • input-port-open?, output-port-open?
  • eof-object, eof-object?

String and Bytevector Ports

  • open-input-string, open-output-string, get-output-string
  • open-input-bytevector, open-output-bytevector, get-output-bytevector

Port State

  • current-input-port, current-output-port, current-error-port
  • close-port, close-input-port, close-output-port
  • call-with-port

Use Extension or AddToRegistry to register all primitives.

Package io provides I/O primitives for reading and writing.

Index

Constants

View Source
const (
	// MaxReadBytevectorBytes is the maximum size of bytevector that
	// read-bytevector can allocate (100 MB).
	MaxReadBytevectorBytes = 100 * 1024 * 1024 // 100 MB
)
View Source
const (
	// MaxReadStringBytes is the maximum memory that read-string can allocate
	// for the character buffer (100 MB). Assumes 4 bytes per rune (worst case).
	MaxReadStringBytes = 100 * 1024 * 1024 // 100 MB
)

Variables

View Source
var AddToRegistry = Builder.AddToRegistry

AddToRegistry registers all I/O primitives.

View Source
var Builder = registry.NewRegistryBuilder(
	addReadWrite,
	addPorts,
	addPortState,
	addPortProcs,
)

Builder aggregates all I/O registration functions.

View Source
var Extension = registry.NewDescribedExtension("io",
	"I/O ports: reading, writing, string/bytevector ports, display, write.",
	AddToRegistry)

Extension is the I/O extension.

View Source
var PrimInputPortQ = helpers.MakeTypePredicate(func(o values.Value) bool {
	p, ok := o.(*values.PortObject)
	if !ok {
		return false
	}
	_, hasReader := p.AsReader()
	return hasReader
})

PrimInputPortQ R7RS §6.13.1 input-port? predicate.

View Source
var PrimOutputPortQ = helpers.MakeTypePredicate(func(o values.Value) bool {
	p, ok := o.(*values.PortObject)
	if !ok {
		return false
	}
	_, hasWriter := p.AsWriter()
	return hasWriter
})

PrimOutputPortQ R7RS §6.13.1 output-port? predicate.

View Source
var PrimPortQ = helpers.MakeTypePredicate(func(o values.Value) bool {
	_, ok := o.(values.Port)
	return ok
})

PrimPortQ R7RS §6.13.1 port? predicate.

Functions

func PrimBinaryPortQ

func PrimBinaryPortQ(mc machine.CallContext) error

PrimBinaryPortQ implements the binary-port? primitive. R7RS §6.13.1: Returns #t if the port is a binary port, #f otherwise.

func PrimCharReadyQ

func PrimCharReadyQ(mc machine.CallContext) error

PrimCharReadyQ implements the char-ready? primitive. R7RS §6.13.2: (char-ready? [port]) Returns #t if a character is ready on the input port, #f otherwise.

func PrimCloseInputPort

func PrimCloseInputPort(mc machine.CallContext) error

PrimCloseInputPort implements the (close-input-port) primitive. Requires an input port; errors if given an output-only port.

R7RS §6.13.1: close-input-port takes an input port.

func PrimCloseOutputPort

func PrimCloseOutputPort(mc machine.CallContext) error

PrimCloseOutputPort implements the (close-output-port) primitive. Requires an output port; errors if given an input-only port. Flushes buffered data before closing.

R7RS §6.13.1: close-output-port takes an output port.

func PrimClosePort

func PrimClosePort(mc machine.CallContext) error

PrimClosePort implements the (close-port) primitive. Closes an input or output port.

R7RS §6.13.1: Closes the resource associated with port.

func PrimEofObject

func PrimEofObject(mc machine.CallContext) error

PrimEofObject implements the (eof-object) primitive. Returns the EOF object.

func PrimEofObjectQ

func PrimEofObjectQ(mc machine.CallContext) error

PrimEofObjectQ implements the (eof-object?) primitive. Returns #t if the argument is the EOF object.

func PrimFlushOutputPort

func PrimFlushOutputPort(mc machine.CallContext) error

PrimFlushOutputPort implements the flush-output-port primitive. R7RS §6.13.3: (flush-output-port [port]) Flushes any buffered output to the underlying output device.

Ports without a real flusher slot (string output, bytevector buffered output) have nothing to flush — but flush-output-port must still reject closed ports per the general R7RS port-closed guard. Explicit IsClosed check covers that path; ports with a flusher slot get the same guard via the wrapper in values/port_helpers.go.

func PrimGetOutputBytevector

func PrimGetOutputBytevector(mc machine.CallContext) error

PrimGetOutputBytevector implements the Scheme get-output-bytevector primitive.

Two-step extraction symmetric to PrimGetOutputString: first verify the value is a *PortObject (any port), then ask for the bytevector-extractor capability via AsByteVectorExtractor. This produces distinct errors for "not a port at all" vs "wrong port flavor."

func PrimGetOutputString

func PrimGetOutputString(mc machine.CallContext) error

PrimGetOutputString implements the Scheme get-output-string primitive.

Two-step extraction: first verify the value is a *PortObject (any port), then ask for string-extractor capability via StringContent. This produces distinct errors for "not a port at all" (ErrNotAPort) vs "wrong port flavor" (ErrNotAStringOutputPort).

func PrimInputPortOpenQ

func PrimInputPortOpenQ(mc machine.CallContext) error

PrimInputPortOpenQ implements the (input-port-open?) primitive. Returns #t if input port is open.

R7RS §6.13.1: Returns #t if port is still open and capable of performing input.

func PrimNewline

func PrimNewline(mc machine.CallContext) error

PrimNewline implements the newline primitive. Writes a newline character to the output port.

func PrimOpenInputBytevector

func PrimOpenInputBytevector(mc machine.CallContext) error

PrimOpenInputBytevector implements the Scheme open-input-bytevector primitive.

func PrimOpenInputString

func PrimOpenInputString(mc machine.CallContext) error

PrimOpenInputString implements the Scheme open-input-string primitive.

func PrimOpenOutputBytevector

func PrimOpenOutputBytevector(mc machine.CallContext) error

PrimOpenOutputBytevector implements the Scheme open-output-bytevector primitive. Per R7RS §6.13.2 it takes no arguments and returns a fresh binary output port that accumulates bytes for retrieval by get-output-bytevector.

func PrimOpenOutputString

func PrimOpenOutputString(mc machine.CallContext) error

PrimOpenOutputString implements the Scheme open-output-string primitive.

func PrimOutputPortOpenQ

func PrimOutputPortOpenQ(mc machine.CallContext) error

PrimOutputPortOpenQ implements the output-port-open? primitive. Returns #t if the output port is open, #f otherwise.

R7RS §6.13.1: Returns #t if port is still open and capable of performing output.

func PrimPeekChar

func PrimPeekChar(mc machine.CallContext) error

PrimPeekChar implements the peek-char primitive. R7RS §6.13.2: (peek-char [port]) Reads and returns a single character from the input port without consuming it.

func PrimPeekU8

func PrimPeekU8(mc machine.CallContext) error

PrimPeekU8 implements the peek-u8 primitive. R7RS §6.13.3: (peek-u8 [port]) Like read-u8, but does not consume the byte from the port.

func PrimRead

func PrimRead(mc machine.CallContext) error

PrimRead implements the (read) primitive. Reads a Scheme datum from port. Reads from the current input port if no port is specified. R7RS §6.13.2: read uses datum labels to handle circular and shared structures.

func PrimReadBytevector

func PrimReadBytevector(mc machine.CallContext) error

PrimReadBytevector implements the read-bytevector primitive. R7RS §6.13.3: (read-bytevector k [port]) Reads the next k bytes from port into a newly allocated bytevector. Returns eof-object if no bytes are available before end of file.

func PrimReadBytevectorBang

func PrimReadBytevectorBang(mc machine.CallContext) error

PrimReadBytevectorBang implements the read-bytevector! primitive. R7RS §6.13.3: (read-bytevector! bytevector [port [start [end]]]) Reads bytes from port into an existing bytevector. Returns the number of bytes read, or eof-object if no bytes available.

func PrimReadChar

func PrimReadChar(mc machine.CallContext) error

PrimReadChar implements the read-char primitive. R7RS §6.13.2: (read-char [port]) Reads and returns a single character from the input port.

func PrimReadLine

func PrimReadLine(mc machine.CallContext) error

PrimReadLine implements the read-line primitive. R7RS §6.13.2: (read-line [port]) Reads a line of text from the input port, not including the line ending.

func PrimReadString

func PrimReadString(mc machine.CallContext) error

PrimReadString implements the read-string primitive. R7RS §6.13.2: (read-string k [port]) Reads up to k characters from the input port and returns them as a string.

func PrimReadSyntax

func PrimReadSyntax(mc machine.CallContext) error

PrimReadSyntax implements the (read-syntax) primitive. Reads datum with source information. Reads from the current input port if no port is specified.

func PrimReadToken

func PrimReadToken(mc machine.CallContext) error

PrimReadToken implements the (read-token) primitive. Reads a single token from port. Reads from the current input port if no port is specified.

func PrimReadU8

func PrimReadU8(mc machine.CallContext) error

PrimReadU8 implements the read-u8 primitive. R7RS §6.13.3: (read-u8 [port]) Reads the next byte from the given binary input port and returns it as an exact integer. Returns eof-object at end of file.

func PrimTextualPortQ

func PrimTextualPortQ(mc machine.CallContext) error

PrimTextualPortQ implements the textual-port? primitive. R7RS §6.13.1: Returns #t if the port is a textual port, #f otherwise.

func PrimU8ReadyQ

func PrimU8ReadyQ(mc machine.CallContext) error

PrimU8ReadyQ implements the u8-ready? primitive. R7RS §6.13.3: (u8-ready? [port]) Returns #t if a byte is available for reading from the binary input port.

func PrimWriteBytevector

func PrimWriteBytevector(mc machine.CallContext) error

PrimWriteBytevector implements the write-bytevector primitive. R7RS §6.13.3: (write-bytevector bytevector [port [start [end]]]) Writes the bytes of bytevector to port.

func PrimWriteChar

func PrimWriteChar(mc machine.CallContext) error

PrimWriteChar implements the write-char primitive. Writes a character to the current output port or to the specified output port.

func PrimWriteString

func PrimWriteString(mc machine.CallContext) error

PrimWriteString implements the write-string primitive. R7RS §6.13.3: (write-string string [port [start [end]]]) Writes the characters of string (optionally between start and end) to port.

func PrimWriteU8

func PrimWriteU8(mc machine.CallContext) error

PrimWriteU8 implements the write-u8 primitive. R7RS §6.13.3: (write-u8 byte [port]) Writes byte to the given binary output port and returns an unspecified value.

Types

type State

type State struct {
	// contains filtered or unexported fields
}

State is the per-engine I/O port + cache state. One per Namespace, reached by primitives via stateFrom(cc). Replaces the former package globals so two Engines in one process do not share port parameters or caches (staff-sweep #8).

The tokenizers/parsers caches are keyed by port value; keeping them in the same per-engine struct as the port parameters is what makes their isolation hold — two engines reading their default current-input-port now key on distinct default port objects, so their read positions no longer collide.

func NewState

func NewState(auth security.Authorizer) *State

NewState builds a fresh per-engine State: three port parameters over the process std streams, and empty caches. Fully constructed here — there is no lazy initialization, unlike the former package-global InitState.

auth gates each host stream. This is the only gate the three pre-opened ports have: they are capability objects the engine hands to Scheme at construction, so the question "may this engine touch the host's stdin?" is answered once, here, rather than on every read. A nil authorizer allows, per the open-by- default convention in security.CheckWithAuthorizer. Before this gate existed a WithProfile(Console) engine under WithAuthorizer(DenyAll()) still read the host's stdin and wrote its stdout, with the authorizer recording no requests at all (reviews/2026-08-07/REVIEW.md 2.1.1).

func (*State) GetInputPort

func (p *State) GetInputPort() (*values.PortObject, error)

GetInputPort returns this engine's base input port, or a wrapped sentinel if the parameter does not hold a textual input port. It serves non-VM callers (tests, embedders) that read the base value directly, without going through the resolve-time error contract of resolveCurrentInputPort.

func (*State) GetOutputPort

func (p *State) GetOutputPort() (*values.PortObject, error)

GetOutputPort returns this engine's base output port, or a wrapped sentinel if the parameter does not hold a textual output port.

func (*State) SetInputPort

func (p *State) SetInputPort(port *values.PortObject)

SetInputPort sets this engine's base input port. Used by tests and by embedders configuring an engine's ports before running. Per R7RS parameter semantics the value is not validated here; a non-textual port surfaces as a Scheme error when a default-port read resolves it (resolveCurrentInputPort).

func (*State) SetOutputPort

func (p *State) SetOutputPort(port *values.PortObject)

SetOutputPort sets this engine's base output port. Not validated here (see SetInputPort); a non-textual port errors at resolve time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL