Docs · Standard library

Platform packages

@libc, @wasm, and @wasi-preview1 declare the operating-system and runtime functions the standard library is built on. @io and @mem import them and choose the right one for the target, so most programs never import them. Import one directly only to call a platform function that @io and @mem do not wrap.

PackageTargetDeclaresUsed by
@libcx86-64-linux, arm64-linuxC standard library functions: I/O, memory, byte operations@io streams, @mem allocators and byte operations
@wasmwasmWebAssembly memory operations and module addresses@mem allocators, @io arguments
@wasi-preview1wasmWASI functions: I/O, arguments, randomness@io streams and arguments, @crypto

Using them

None of the three exports a name. Each declares c_fn functions (and @wasi-preview1 two c_struct types) in the c namespace from @c. Load the package with a bare import and call through c:

Vyi
import { println } from "@fmt"
import { c } from "@c"
import "@wasm"

fn main() !u8 {
    con pages = c.memory_size()
    println(pages > 0)   // prints true
    return Ok(0)
}

The argument and result types are C types (@c primitive types); convert Vyi values at the call, as in c.size_t(buf.len).

On a target a package is not for:

  • @libc and @wasi-preview1 declare nothing. A call such as c.write(...) is reported as undefined name "c.write".
  • @wasm declares its functions, and a program that calls one fails to link.

Choose by target with a file-level if over targetOS from @target (guide 14), as @io and @mem do.

@libc

The functions keep their C names and meanings. *c_ptr c.unsigned_char stands for C's void * and char *.

FunctionSignature
writec_fn write(fd: c.int, buf: *c_ptr c.unsigned_char, len: c.size_t) c.size_t
readc_fn read(fd: c.int, buf: *c_ptr c.unsigned_char, len: c.size_t) c.size_t
mallocc_fn malloc(size: c.size_t) *c_ptr c.unsigned_char
freec_fn free(p: *c_ptr c.unsigned_char)
mmapc_fn mmap(addr: *c_ptr c.unsigned_char, len: c.size_t, prot: c.int, flags: c.int, fd: c.int, off: c.long) *c_ptr c.unsigned_char
munmapc_fn munmap(addr: *c_ptr c.unsigned_char, len: c.size_t) c.int
memmovec_fn memmove(dst: *c_ptr c.unsigned_char, src: *c_ptr c.unsigned_char, n: c.size_t) *c_ptr c.unsigned_char
memcmpc_fn memcmp(a: *c_ptr c.unsigned_char, b: *c_ptr c.unsigned_char, n: c.size_t) c.int
memsetc_fn memset(p: *c_ptr c.unsigned_char, v: c.int, n: c.size_t) *c_ptr c.unsigned_char
  • write and read return c.size_t, which is unsigned. C's -1 for a failure arrives as the largest c.size_t; i32(n) of it is -1.
  • mmap returns C's MAP_FAILED, the address -1, on failure, not a null pointer.
  • Any other function of the C standard library can be declared the same way, as a bodyless c_fn (@c).

@wasm

These are WebAssembly operations, not functions the runtime provides. All use c.int, which is i32.

FunctionSignatureResult
memory_growc_fn memory_grow(pages: c.int) c.intadds pages pages of 64 KiB to linear memory; returns the previous size in pages, or -1
memory_sizec_fn memory_size() c.intthe size of linear memory in 64 KiB pages
heap_basec_fn heap_base() c.intthe address where the heap starts, after the program's data
argv_basec_fn argv_base() c.intthe address of the 16 KiB region that args() from @io reads the arguments into
argv_capacityc_fn argv_capacity() c.intthe size of that region in bytes: 16384

Memory added with memory_grow is not reserved from systemAllocator: a later allocation from systemAllocator can be placed over it.

@wasi-preview1

These are functions of the WASI preview 1 interface (wasi_snapshot_preview1), provided by the runtime that runs the module. Each returns 0 on success or a WASI error number.

DeclarationSignature
ciovec_tc_struct ciovec_t { buf: *c_ptr c.unsigned_char, buf_len: c.size_t }: one buffer to write
iovec_tc_struct iovec_t { buf: *c_ptr c.unsigned_char, buf_len: c.size_t }: one buffer to read into
fd_writec_fn fd_write(fd: c.unsigned_int, iovs: *c_ptr c.ciovec_t, iovs_len: c.size_t, nwritten: *c_ptr c.size_t) c.unsigned_short
fd_readc_fn fd_read(fd: c.unsigned_int, iovs: *c_ptr c.iovec_t, iovs_len: c.size_t, nread: *c_ptr c.size_t) c.unsigned_short
random_getc_fn random_get(buf: *c_ptr c.unsigned_char, buf_len: c.size_t) c.unsigned_short
args_sizes_getc_fn args_sizes_get(argc: *c_ptr c.size_t, argv_buf_size: *c_ptr c.size_t) c.unsigned_short
args_getc_fn args_get(argv: *c_ptr c.unsigned_char, argv_buf: *c_ptr c.unsigned_char) c.unsigned_short
  • fd_read stores the number of bytes read through nread; 0 at the end of the input is a success.
  • args_get writes the arguments, each ended by a zero byte, into argv_buf, and an array of pointers to them into argv, declared here as bytes.

See also: @c, @io, @mem, and the targets reference.