Standard Library Specification
This document provides a low-level, authoritative reference for experienced systems developers. It details the runtime signatures, memory contracts, error semantics, and underlying system call implementations of Pith’s standard library namespaces.
Namespace: fs
The fs namespace exposes file descriptor operations and direct filesystem access.
Functions
fn readFile(path: string) string
Reads the complete contents of the file specified by path into a newly allocated, null-terminated string.
- Parameters:
path: Relative or absolute filesystem path.
- Return value:
- A reference-counted string containing the file data.
- Returns an empty string
""on failure (e.g.ENOENT,EACCES, or if allocation fails).
- Underlying Syscalls:
- POSIX:
stat(2)/open(2)/read(2)/close(2). - Windows:
CreateFileA/GetFileSizeEx/ReadFile/CloseHandle.
- POSIX:
- Memory Safety:
- Allocates a contiguous buffer sized to file length plus one byte. The returned
PithValue*has an initial strong reference count of 1 and is reclaimed deterministically at scope exit.
- Allocates a contiguous buffer sized to file length plus one byte. The returned
fn writeFile(path: string, content: string) int
Creates or truncates the file at path and writes the provided content string.
- Parameters:
path: Target file path.content: Data buffer to write.
- Return value:
1if all bytes were successfully written to disk.0on system error or partial write.
- Underlying Syscalls:
- POSIX:
open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644), followed by complete loop write andclose(2). - Windows:
CreateFileA(..., GENERIC_WRITE, ..., CREATE_ALWAYS, ...).
- POSIX:
fn exists(path: string) int
Checks whether a filesystem entry exists at path.
- Parameters:
path: Target file or directory path.
- Return value:
1if the file or directory exists.0if it does not exist or access is denied.
- Underlying Syscalls:
- POSIX:
access(path, F_OK). - Windows:
GetFileAttributesA(path).
- POSIX:
fn remove(path: string) int
Deletes the file entry at path.
- Parameters:
path: Target file path.
- Return value:
1if the file was deleted successfully.0on system error (ENOENT,EACCES, etc.).
- Underlying Syscalls:
- POSIX:
unlink(path). - Windows:
DeleteFileA(path).
- POSIX:
Namespace: os
The os namespace provides process control, command-line arguments, environment variable access, and host kernel identification.
Host Detection Properties
All platform inspection properties are pure query values evaluated at runtime:
val identifyKernel: string
Returns a static string literal identifying the host kernel: "linux", "darwin", "nt", "freebsd", or "unknown".
val identifyKernelVersion: string
Returns the operating system kernel release string. On POSIX systems, this reads from utsname.release via uname(2).
val isLinux: int # 1 on Linux kernels, 0 otherwise
val isDarwin: int # 1 on any Darwin kernel (uname = Darwin), 0 otherwise
val isMacOS: int # 1 specifically on Apple macOS (__APPLE__ && __MACH__), 0 otherwise
val isNT: int # 1 on Windows NT, 0 otherwise
val isFreeBSD: int # 1 on FreeBSD kernels, 0 otherwise
Namespace: proc
The proc namespace provides process control, command-line arguments, environment variable inspection, and process lifecycle primitives.
Properties
val pid: int
Returns the unique process identifier of the current calling process.
- Underlying Syscalls:
- POSIX:
getpid(2). - Windows:
GetCurrentProcessId().
- POSIX:
val argCount: int
Returns the number of command-line arguments passed to the script (equivalent to argc - 1 from the interpreter, or full argc for standalone compiled binaries). Can also be invoked as a function proc.argCount().
Functions
fn getEnv(name: string) string
Queries the environment list for the key name.
- Return value:
- Value associated with
nameas a string. - Empty string
""if the key is not found in the environment block.
- Value associated with
- Underlying Syscalls:
- POSIX:
getenv(3). - Windows:
GetEnvironmentVariableA.
- POSIX:
fn getArg(index: int) string
Returns the argument at 0-based index.
- Return value:
- String argument value.
- Returns an empty string
""ifindex < 0orindex >= argCount().
fn sleep(ms: int) void
Suspends execution of the calling process for at least ms milliseconds.
- Parameters:
ms: Duration in milliseconds (non-negative).
- Underlying Syscalls:
- POSIX:
nanosleep(2). - Windows:
Sleep(DWORD).
- POSIX:
fn exit(code: int) void
Immediately terminates the calling process with status code.
- Underlying Syscalls:
- Invokes C
exit(code)/_exit(2). Buffered file streams are flushed.
- Invokes C
Namespace: net
The net namespace provides minimalist, non-blocking-capable TCP socket networking primitives.
Functions
fn socket(domain: int, type: int, protocol: int) int
Allocates a raw communication socket descriptor.
- Parameters:
domain: Address family (2forAF_INET,10forAF_INET6).type: Socket semantics (1forSOCK_STREAM,2forSOCK_DGRAM).protocol: Protocol number (0for IP default).
- Return value:
- Socket integer descriptor (
>= 0) on success. -1on socket creation error.
- Socket integer descriptor (
- Underlying Syscalls:
- POSIX:
socket(2). - Windows:
WSASocket/socket.
- POSIX:
fn connect(host: string, port: int) int
Resolves the destination host and establishes a TCP stream connection on port.
- Parameters:
host: IPv4/IPv6 address or domain hostname (e.g."127.0.0.1","example.com").port: Destination port number (1to65535).
- Return value:
- Connected socket descriptor (
>= 0). -1if resolution or connection establishment fails.
- Connected socket descriptor (
- Underlying Syscalls:
- POSIX:
gethostbyname(3)/getaddrinfo(3), socket creation, andconnect(2).
- POSIX:
fn send(fd: int, message: string) int
Transmits bytes over an open socket descriptor.
- Parameters:
fd: Connected socket descriptor.message: Payload string to transmit.
- Return value:
- Number of bytes sent (
>= 0). -1on write error or closed connection (EPIPE,ECONNRESET).
- Number of bytes sent (
- Underlying Syscalls:
- POSIX:
send(fd, buf, len, 0)/write(2).
- POSIX:
fn recv(fd: int, maxBytes: int) string
Receives incoming stream bytes from fd.
- Parameters:
fd: Open socket descriptor.maxBytes: Maximum buffer capacity to read in this call.
- Return value:
- A newly allocated string containing received bytes.
- Returns
""on EOF (graceful remote shutdown) or socket read error.
- Underlying Syscalls:
- POSIX:
recv(fd, buf, maxBytes, 0)/read(2).
- POSIX:
fn close(fd: int) int
Closes the active socket descriptor and releases system resources.
- Return value:
0on success.-1on error (EBADF).
- Underlying Syscalls:
- POSIX:
close(2). - Windows:
closesocket.
- POSIX:
Namespace: str
The str namespace provides byte-oriented string queries, substring matching, and ASCII case-folding routines. All returned strings are newly allocated ARC-managed values (+1 reference).
Functions
fn length(s: string) int
Returns the active byte length of s.
- Parameters:
s: Input string.
- Return value:
- Integer byte count (from the
PithValue.lengthheader field).
- Integer byte count (from the
fn contains(s: string, sub: string) int
Tests whether sub is contained as a contiguous substring within s.
- Parameters:
s: Haystack string.sub: Needle substring.
- Return value:
1ifsubis found,0otherwise.
fn startsWith(s: string, prefix: string) int
Tests whether s begins with the prefix bytes prefix.
- Parameters:
s: Target string.prefix: Prefix substring.
- Return value:
1ifsbegins withprefix,0otherwise.
fn endsWith(s: string, suffix: string) int
Tests whether s terminates with the suffix bytes suffix.
- Parameters:
s: Target string.suffix: Suffix substring.
- Return value:
1ifsends withsuffix,0otherwise.
fn upper(s: string) string
Allocates and returns a new string where ASCII lowercase letters a-z are mapped to uppercase A-Z. Non-ASCII bytes and other characters remain unchanged.
- Parameters:
s: Input string.
- Return value:
- Newly allocated ARC string (+1 reference).
fn lower(s: string) string
Allocates and returns a new string where ASCII uppercase letters A-Z are mapped to lowercase a-z. Non-ASCII bytes and other characters remain unchanged.
- Parameters:
s: Input string.
- Return value:
- Newly allocated ARC string (+1 reference).
Memory & ABI Specification
PithValue Representation
All dynamic data (strings, objects) share an identical binary layout defined in include/pith.h:
typedef struct PithValue PithValue;
struct PithValue {
volatile uint32_t strongRefs; /* Atomic reference counter */
uint16_t typeTag; /* Type identifier */
uint16_t flags; /* Bit flags */
uint32_t capacity; /* Allocated byte capacity */
uint32_t length; /* Active byte length */
char data[]; /* Contiguous byte payload */
};
Type Tags
| Constant | Value | Description |
|---|---|---|
PITH_TAG_INT | 1 | 64-bit signed integer |
PITH_TAG_FLOAT | 2 | 64-bit IEEE-754 floating-point |
PITH_TAG_STRING | 3 | Refcounted, length-prefixed, NUL-terminated byte array |
PITH_TAG_BOOL | 4 | 32-bit boolean value (0 or 1) |
PITH_TAG_OBJECT | 5 | Refcounted record / dictionary pointer |
Bit Flags
PITH_FLAG_STATIC(0x01): Marks immortal static data (e.g. string literals emitted directly into the.dataELF section). Calls topithRetainandpithReleaseare no-ops on static-flagged values.PITH_FLAG_SHARED(0x02): Marks values managed across shared boundaries.
Calling Convention & Lowering
- Intermediate Representation:
- Pith statements lower into QBE SSA intermediate representation.
- Expressions evaluate in 64-bit registers: integer values use
l(long, 64-bit), float values used(double, 64-bit).
- Machine ABI:
- System V AMD64 ABI (Linux, FreeBSD, macOS).
- Parameters passed in registers:
%rdi,%rsi,%rdx,%rcx,%r8,%r9. - Floating-point parameters passed in
%xmm0through%xmm7. - Callee-preserved registers:
%rbx,%rsp,%rbp,%r12,%r13,%r14,%r15.