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.
| Package | Target | Declares | Used by |
|---|---|---|---|
@libc | x86-64-linux, arm64-linux | C standard library functions: I/O, memory, byte operations | @io streams, @mem allocators and byte operations |
@wasm | wasm | WebAssembly memory operations and module addresses | @mem allocators, @io arguments |
@wasi-preview1 | wasm | WASI 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:
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:
@libcand@wasi-preview1declare nothing. A call such asc.write(...)is reported asundefined name "c.write".@wasmdeclares 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 *.
| Function | Signature |
|---|---|
write | c_fn write(fd: c.int, buf: *c_ptr c.unsigned_char, len: c.size_t) c.size_t |
read | c_fn read(fd: c.int, buf: *c_ptr c.unsigned_char, len: c.size_t) c.size_t |
malloc | c_fn malloc(size: c.size_t) *c_ptr c.unsigned_char |
free | c_fn free(p: *c_ptr c.unsigned_char) |
mmap | c_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 |
munmap | c_fn munmap(addr: *c_ptr c.unsigned_char, len: c.size_t) c.int |
memmove | c_fn memmove(dst: *c_ptr c.unsigned_char, src: *c_ptr c.unsigned_char, n: c.size_t) *c_ptr c.unsigned_char |
memcmp | c_fn memcmp(a: *c_ptr c.unsigned_char, b: *c_ptr c.unsigned_char, n: c.size_t) c.int |
memset | c_fn memset(p: *c_ptr c.unsigned_char, v: c.int, n: c.size_t) *c_ptr c.unsigned_char |
writeandreadreturnc.size_t, which is unsigned. C's-1for a failure arrives as the largestc.size_t;i32(n)of it is-1.mmapreturns C'sMAP_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.
| Function | Signature | Result |
|---|---|---|
memory_grow | c_fn memory_grow(pages: c.int) c.int | adds pages pages of 64 KiB to linear memory; returns the previous size in pages, or -1 |
memory_size | c_fn memory_size() c.int | the size of linear memory in 64 KiB pages |
heap_base | c_fn heap_base() c.int | the address where the heap starts, after the program's data |
argv_base | c_fn argv_base() c.int | the address of the 16 KiB region that args() from @io reads the arguments into |
argv_capacity | c_fn argv_capacity() c.int | the 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.
| Declaration | Signature |
|---|---|
ciovec_t | c_struct ciovec_t { buf: *c_ptr c.unsigned_char, buf_len: c.size_t }: one buffer to write |
iovec_t | c_struct iovec_t { buf: *c_ptr c.unsigned_char, buf_len: c.size_t }: one buffer to read into |
fd_write | c_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_read | c_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_get | c_fn random_get(buf: *c_ptr c.unsigned_char, buf_len: c.size_t) c.unsigned_short |
args_sizes_get | c_fn args_sizes_get(argc: *c_ptr c.size_t, argv_buf_size: *c_ptr c.size_t) c.unsigned_short |
args_get | c_fn args_get(argv: *c_ptr c.unsigned_char, argv_buf: *c_ptr c.unsigned_char) c.unsigned_short |
fd_readstores the number of bytes read throughnread;0at the end of the input is a success.args_getwrites the arguments, each ended by a zero byte, intoargv_buf, and an array of pointers to them intoargv, declared here as bytes.
See also: @c, @io, @mem, and the
targets reference.