two-way c<->revo interop

docs | codeberg | github (mirror) | license

c is the only language that has everything a computer can do implemented in it

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⢀⣠⢤⡤⡤⠤⣤⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⢾⣥⢻⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣶⣿⡱⣆⠤⡁⢎⡡⢵⠀⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⣾⠸⣿⡎⢿⣿⣶⡀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⡿⢘⣠⠞⡧⣙⠤⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⢸⢿⡇⣿⣿⡘⣿⣿⣿⡀⠀⠀⠀⠀⢠⣼⣿⣿⣿⣿⡿⣃⣿⠣⢤⣤⢛⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⢥⣃⣏⣷⡜⣿⣧⠹⣿⣿⣿⡄⠀⠀⣴⣿⣿⣿⣿⣿⣯⢾⣿⣿⣾⡿⠶⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠵⣣⣟⣼⣷⣻⠸⣿⣦⠹⣿⣿⣿⡄⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠭⣘⠻⣿⣧⣿⣾⣧⠘⣿⡿⣹⣽⣿⡟⢿⣿⣿⣿⣿⣿⣿⣿⡧⣼⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠠⠤⡤⢤⠶⡤⣆⣤⡀⡄⠀⠀⠀⠈⠐⠃⢈⠻⢿⣿⣿⡇⠘⡍⡷⢘⣿⣌⡄⠀⣼⣿⣿⣿⣿⣿⡗⠺⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠰⠀⣀⠦⣑⢦⡙⡲⢍⡶⣋⠶⣡⠞⡝⢯⢷⣞⣶⢠⠀⠀⠀⠠⠀⠙⢿⡝⡄⡇⢱⢸⡏⢸⠆⢸⡟⣿⣿⣿⣿⣿⣟⠥⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠆⠀⠌⡄⡙⡘⠎⢷⡩⢏⠶⣩⠳⣡⠛⢬⢣⡍⠺⢞⢇⣾⠿⠆⠀⢎⡀⡄⠙⢡⣎⠞⡘⢿⡐⢀⡟⣲⢻⣿⣿⣿⣿⣏⡅⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠈⠀⢂⠌⡑⢹⣛⢦⠹⢜⡲⡡⠜⡤⢃⠀⠁⠺⢑⢪⣌⢋⣄⠀⠡⠈⠳⣦⠅⠀⠻⡆⠇⣾⡄⢸⠘⢰⣼⣿⣿⣿⡟⠒⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠠⠂⠍⢢⠘⢎⠵⢢⠔⣉⡘⠔⠫⠜⢦⠠⡄⢌⠂⠳⣎⠷⣄⡀⠀⠀⠙⣆⠐⣿⡆⣿⠀⡧⠰⣟⣿⣻⣿⡟⠑⠀⢀⡀⣄⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠒⡄⠉⠎⡔⢃⠎⡱⠌⣙⠒⡆⣆⠀⠈⠃⠙⠀⠙⢼⣉⠷⢦⡐⠢⡘⢆⠙⢿⣿⠐⣱⠿⣀⣡⡶⠇⠀⢀⡐⣤⣻⣽⣯⣿⣶⣥⡄⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠃⡴⢠⢈⡐⠡⠒⠤⡈⢄⡘⢀⠀⠆⡡⠤⣀⣄⠰⠬⠡⢙⡳⢾⣮⣡⢌⠻⠀⡿⠙⡳⢃⠉⠀⠀⠈⣁⢋⠟⡻⠿⣿⣿⣿⣿⣷⣄⡀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠲⢤⠓⣌⠲⠤⣄⣈⢂⠒⡌⢰⡐⠤⡈⢍⡛⡫⢭⣍⣓⢾⣿⣝⢷⠤⠀⠀⠀⠤⢒⡈⣋⢛⡒⠾⠶⣴⣦⣀⡈⠍⠛⢿⣿⣽⢦⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠚⠥⡳⡄⣙⢪⣝⠺⣧⢟⣷⡾⣧⣱⣦⠚⢀⣉⣓⣊⡙⣻⣆⠒⠄⡀⠈⢆⢳⡸⢯⣿⣿⣷⣶⣬⣙⣛⠻⢦⣤⣌⠛⣯⠓⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⣀⠃⠮⢣⡹⢦⣛⠶⠛⠋⠀⢠⣳⡿⠟⠉⡼⢳⢌⡃⠌⠐⠀⠀⠎⠳⡛⠿⠛⡟⠿⠻⠿⢶⣻⠰⢦⡻⣝⢠⠏⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠄⢢⠐⠈⠄⠀⢄⠠⠀⢀⠀⡔⢁⠣⢃⠁⡐⢨⠑⡱⢊⠔⠈⠀⠀⢠⡈⡆⢁⣤⠡⠄⠄⠆⢁⡂⠰⠨⣥⢛⠬⡣⠍⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⢂⠔⠈⣠⢮⡑⠊⠁⠈⠀⡠⠂⣸⠁⠨⠑⡂⢌⠀⢄⢣⣿⢨⠀⠰⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠋⠺⠥⠃⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡴⢡⡞⣨⣾⣿⡗⠈⠁⠄⡡⢐⡁⢀⡏⣘⠀⢰⢁⡂⠀⣺⣼⣵⡎⠡⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡸⢡⡗⣰⣿⣿⣿⣼⣷⡏⣄⠣⢠⠂⢸⣷⣿⡀⣿⡘⢇⢲⣿⣿⣿⣏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡏⡟⣰⣿⣿⣿⣿⣿⡿⣟⡽⡂⠧⠀⣾⣿⣿⡟⣿⡇⣾⣿⣿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡧⢁⣿⡟⣽⣿⣿⣿⡿⣉⡴⠳⠀⠀⣿⣿⣿⣧⢻⣇⢿⢾⣿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⢻⡼⣽⣿⣿⣿⡿⠓⠈⠀⠀⠀⠀⢸⡟⢿⣿⣟⣿⣷⡌⢹⣿⣿⢿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠻⣜⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠘⣿⣼⣿⣿⡿⣽⣷⠀⡟⢿⣽⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⢣⡝⣿⣷⣿⣿⣧⠙⣿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠭⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⢾⣿⣿⣷⣿⣿⡄⣻⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠌⠛⠿⡏⠙⠁⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

despite being written in zig, revo gives you a real c api for embedding
and extensions. the header is auto-generated from zig callconv(.c)
functions, always in sync with what the library actually exports.
ref:src/capi/exports.zig
ref:src/capi/header_gen.zig

build

zig build lib

you get a static library and an auto-generated header:

~ zig-out/lib/liberevo.a
~ zig-out/include/revo/revo.h (#include "revo.h" with -Izig-out/include/revo,
or #include <revo/revo.h> with -Izig-out/include)

all generated decls are prefixed REVO_API (dllexport/dllimport on
windows, default visibility on gcc/clang, empty for REVO_STATIC builds)
and the header carries #define REVO_VERSION "x.y.z". every revo_* /
erevo_* decl in it comes straight from an exported callconv(.c) zig fn,
so the header is always in sync with what the library actually exports.

ref:src/capi/embed.zig

the opaque handles in src/capi/embed.zig are the ones you get in your C code

vm lifecycle

#include "revo.h"

ErevoVM *vm = erevo_vm_create();
if (!vm) return 1;

// ... use vm ...

erevo_vm_destroy(vm);

ErevoVM is an opaque handle

compile and run

ErevoProgram *prog = erevo_compile(vm, "main.rv", "1 + 2");
if (!prog) {
    puts(erevo_vm_last_error(vm));
    return 1;
}

RevoValue result;
if (!erevo_run(vm, prog, &result)) {
    puts(erevo_vm_last_error(vm));
}

// eval does compile + run + free in one step
if (!erevo_eval(vm, "main.rv", "1 + 2", &result)) {
    puts(erevo_vm_last_error(vm));
}

erevo_program_destroy(prog);

ref:pub fn erevo_compile(
ref:pub fn erevo_run(
ref:pub fn erevo_eval(
ref:pub fn erevo_program_destroy(

erevo_run writes the result value through the optional result pointer

errors

const char *msg = erevo_vm_last_error(vm);

returns a null-terminated string, valid until the next api call on the
same vm

value type

all values are a single nanboxed uint64_t:

typedef uint64_t RevoValue;

numbers are the raw f64 bits; boxed values pack the type nibble (bits
51-48) and a payload (low 48 bits) behind a box tag. the payload is an
intern id, except opaque, whose payload is the low 48 bits of the
wrapped pointer:

typedef enum {
    revo_number = 0,
    revo_string = 8,
    revo_atom = 9,
    revo_function = 10,
    revo_table = 11,
    revo_resource = 12,
    revo_opaque = 13,
} RevoType;

constructors

RevoValue v = revo_nil();                  // :nil
RevoValue v = revo_bool(1);                // :true / :false
RevoValue v = revo_string_val(string_id);  // from interned id
RevoValue v = revo_num(3.14);              // number
RevoValue v = revo_atom_val(atom_id);      // atom by raw id
RevoValue v = revo_table_val(table_id);    // from table id
RevoValue v = revo_function_val(func_id);  // from function id

(revo_string(id) / revo_table(id) / revo_function(id) still work as
compat aliases for the _val forms.)

extractors

double   revo_num_value(RevoValue);
uint64_t revo_string_id(RevoValue);
uint64_t revo_atom_id(RevoValue);
uint64_t revo_table_id(RevoValue);
uint64_t revo_function_id(RevoValue);
void    *revo_opaque_ptr(RevoValue);      // null if not opaque (see below)
void    *revo_resource_ptr(void*, RevoValue);  // null unless resource (see below)
int      revo_bool_val(RevoValue);   // 0 or 1, 0 if not bool
int      revo_type(RevoValue);       // the RevoType of the value

type checks

int revo_is_nil(RevoValue);
int revo_is_number(RevoValue);
int revo_is_string(RevoValue);
int revo_is_atom(RevoValue);
int revo_is_function(RevoValue);
int revo_is_table(RevoValue);
int revo_is_resource(RevoValue);
int revo_is_opaque(RevoValue);
int revo_is_bool(RevoValue);

built-in atoms have guaranteed and consistent values in the RevoAtom enum, such as

ra_nil, ra_true, ra_false, ra_ok, ra_err, ra_some,
ra_none, ra_undef, ra_missing, ra_no_result, ra_range

this means you don’t have to intern them manually

ref:pub const RevoAtom

opaque

opaque handles. an opaque wraps a raw void* revo never touches.
caller owns the memory

RevoValue v = revo_opaque_new(ptr);    // wrap
void *p = revo_opaque_ptr(v);         // unwrap
int is_f = revo_is_opaque(v);         // check

ref:pub fn revo_opaque_new(
ref:pub fn revo_opaque_ptr(

null is ambiguous. revo_opaque_ptr is null for non-opaque
values and null ptrs:

if (!revo_is_opaque(v)) {
    // not opaque at all
} else {
    void *p = revo_opaque_ptr(v);  // null here means a genuine null ptr
}

low 48 bits only (REVO_PAYLOAD_MASK). fits canonical user ptrs on
x86_64/arm64; no tagged ptrs or high-bit integers

lifetime. the box holds the address, not the pointee. malloc +
explicit free; revo is never the owner:

typedef struct { double total; } Total;

int total_new(void *vm, size_t argc, RevoValue *argv, RevoValue *out) {
    (void)argc; (void)argv;
    Total *t = malloc(sizeof(Total));
    if (!t) return revo_c_err_other(vm, "out of memory");
    t->total = 0;
    *out = revo_opaque_new(t);
    return REVO_OK;
}

int total_add(void *vm, size_t argc, RevoValue *argv, RevoValue *out) {
    if (argc < 2) return revo_c_err_arity(vm, argc, 2);
    if (!revo_is_opaque(argv[0])) return revo_c_err_type(vm, 0, "opaque", argv[0]);
    if (!revo_is_number(argv[1])) return revo_c_err_type(vm, 1, "number", argv[1]);
    Total *t = revo_opaque_ptr(argv[0]);
    if (!t) return revo_c_err_other(vm, "null handle");
    t->total += revo_num_value(argv[1]);
    *out = revo_num(t->total);
    return REVO_OK;
}

int total_free(void *vm, size_t argc, RevoValue *argv, RevoValue *out) {
    (void)vm;
    if (argc >= 1 && revo_is_opaque(argv[0])) free(revo_opaque_ptr(argv[0]));
    *out = revo_nil();
    return REVO_OK;
}

transport. ordinary values: globals, table fields, call args, *out

ptr identity, opaque render (<opaque *>)

resource

owned handles!

while light opaque is a bare immediate; resource is what you want
for handles with methods & cleanup

a caller-owned ptr in a gc cell + a per-handle metatable:
revo code can neither see nor overwrite the address.
the cell frees at sweep, the pointee never

RevoValue h = revo_resource_new(vm, ptr);  // nil on failure
void *p = revo_resource_ptr(vm, h);        // null unless resource
int is_e = revo_is_resource(h);            // check

ref:pub fn revo_resource_new(
ref:pub fn revo_resource_ptr(

null-ptr cells unwrap null just like opaque, so revo_is_resource
first when it matters

metatables. one table per resource kind & every handle points at it:

revo_resource_setmetatable(vm, h, mt);  // false unless resource + table
revo_resource_getmetatable(vm, h, &out);

methods come through __index like tables. same table back from
getmetatable means same kind: that is your type check. nil clears

__gc. metatable __gc runs once with the handle when swept
(leftovers run at destroy). one-shot: the field clears when queued,
set a fresh metatable inside __gc to re-arm. no timing promises.
and light opaque never finalizes: no cell, no metatable, no __gc

ref:pub fn revo_resource_setmetatable(
ref:pub fn revo_resource_getmetatable(

rooting

a RevoValue in a c local roots nothing. values reachable only from c
can be swept (ids reused) by the next collection. pin what you hold
across calls:

uint64_t r = revo_ref(vm, val);  // 0 on failure
// eval / call freely; revo_getref(vm, r) stays valid
RevoValue same = revo_getref(vm, r);
revo_unref(vm, r);               // release exactly once

ids monotonic, never reused: stale reads :nil. revo_ref on :nil
is 0; revo_getref on 0/unknown/released is :nil; revo_unref
there is a noop. globals root too

ref:pub fn revo_ref(
ref:pub fn revo_getref(
ref:pub fn revo_unref(

finalizers

explicit free stays primary: gc promises no timing. a table can
carry a finalizer running once with the table as sole arg when swept
(leftovers run at destroy):

bool ok = revo_table_set_finalizer(vm, handle_table, fin_fn);
bool dropped = revo_table_remove_finalizer(vm, handle_table);

same mechanism as regex/sockets: _ptr field, freed in finalizer
and explicit free, both tolerating a missing _ptr. keep fin_fn
reachable til it fires

ref:pub fn revo_table_set_finalizer(
ref:pub fn revo_table_remove_finalizer(

ffi (posix only)

dynamic c calls!

declare by api then call the handle

types are atoms: i32|u32|i64|u64|f32|f64|bool|ptr|void|string.
returns surface as numbers (u64 past f64 range errors)
, strings copy, pointers come back the way mother nature made them

ffi.varfunc adds a total count for ... functions

decl problems fail at declare time;
bad args fail at call time; both carry messages

handles are resource values with __call + __gc already attached

strings copy in, never borrow. nil is null, on both sides

if you misdeclare a signature and you crash like c does

strings

strings are interned! every unique string has a stable uint64_t id

uint64_t sid = revo_intern(vm, "hello", 5);
RevoValue val = revo_string_val(sid);

the pointer must stay valid for the duration of the call

to read string data back:

const unsigned char *data = revo_string_data(vm, sid);
uint64_t len = revo_string_length(vm, sid);

revo_string_data returns a pointer to the internal string buffer
(valid until the string is gc’d) and is not nul-terminated!!! use
revo_string_length for the byte count, copy when you need a c string

atoms intern the same way (revo_intern_atom); every _cstr wrapper
(revo_intern_cstr, revo_intern_atom_cstr, revo_table_set_name_cstr,
revo_table_get_name_cstr, plus the globals pair below) calls strlen
internally
ref:pub fn revo_intern(
ref:pub fn revo_intern_atom(
ref:pub fn revo_string_data(
ref:pub fn revo_string_length(

calling revo functions from c

RevoValue fn_val;             // get from eval, global, etc.
RevoValue args[2] = { revo_num(10.0), revo_num(20.0) };
RevoValue result;

int ok = revo_call(vm, fn_val, 2, args, &result);

returns 0 if the value wasn’t callable or the call threw. max 16 args.
why is in revo_call_last_error (empty when the last call worked,
valid til the next call on the same vm)
ref:pub fn revo_call(
ref:pub fn revo_call_last_error(

to expose a c function without a .so, wrap the pointer (name borrowed,
keep it static):

RevoValue fn_val = revo_cfunc_new(vm, my_fn, "my_fn", 5);

nil on null fn or allocation failure
ref:pub fn revo_cfunc_new(

globals

revo_setglobal(vm, "name", 4, revo_num(42.0));
RevoValue v = revo_getglobal(vm, "name", 4);

// or via c-string wrappers (call strlen internally)
revo_setglobal_cstr(vm, "name", revo_num(42.0));
RevoValue v = revo_getglobal_cstr(vm, "name");

missing keys return :nil
ref:pub fn revo_getglobal(
ref:pub fn revo_setglobal(

tables

functions take the table value itself, lookups report presence
through the return value so missing keys are distinct from nil values:

RevoValue t = revo_table_create(vm);

// named fields
revo_table_set_name(vm, t, "x", 1, revo_num(42.0));
RevoValue v;
bool found = revo_table_get_name(vm, t, "x", 1, &v);  // true

// generic keys (metatable-aware, like t[k])
RevoValue key = revo_atom_val(revo_intern_atom(vm, ...));
revo_table_set(vm, t, key, revo_num(42.0));

// array part
RevoValue arr = revo_table_from_items(vm, 2, (RevoValue[]){ revo_num(1.0), revo_num(2.0) });
revo_table_push(vm, arr, revo_num(3.0));
revo_table_get_idx(vm, arr, 1, &v);   // 2.0, false when out of range

uint64_t n = revo_table_len(vm, t);    // total entries
uint64_t a = revo_table_alen(vm, arr); // array part only

bool gone = revo_table_remove(vm, t, key);  // true when something was there

ref:pub fn revo_table_create(
ref:pub fn revo_table_set(
ref:pub fn revo_table_get(
ref:pub fn revo_table_remove(
ref:pub fn revo_table_len(
ref:pub fn revo_table_from_items(

results

host functions answer with {:ok, v} / {:err, e} tables:

if (bad) {
    *out_result = revo_err(vm, revo_atom_val(
        revo_intern_atom(vm, "BadInput", 8)));
    return;
}
// ... later, on the receiving side:
if (revo_is_ok(vm, val)) {
    RevoValue payload;
    revo_ok_value(vm, val, &payload);
}

ref:pub fn revo_ok(
ref:pub fn revo_err(
ref:pub fn revo_is_ok(
ref:pub fn revo_is_err(
ref:pub fn revo_ok_value(

writing c extensions

extensions are shared libraries that export a revo_bindings array.
every c function follows this signature:

typedef int (*RevoFn)(void *vm, size_t argc, RevoValue *argv, RevoValue *out);

return REVO_OK (0), anything else raises (*out ignored on err):

#define REVO_OK 0
#define REVO_ERR_ARITY 1
#define REVO_ERR_TYPE 2
#define REVO_ERR_OTHER 3

a minimal extension:

#include "revo.h"

int greet(void *vm, size_t argc, RevoValue *argv, RevoValue *out) {
    (void)vm; (void)argc; (void)argv;
    *out = revo_num(42.0);
    return REVO_OK;
}

__attribute__((visibility("default")))
const RevoBinding revo_bindings[] = {
    {"greet", greet},
    {NULL, NULL},
};

errors. the big HostResult three: arity, type, other
return the helper’s result directly:

if (argc < 2) return revo_c_err_arity(vm, argc, 2);
if (!revo_is_number(argv[0])) return revo_c_err_type(vm, 0, "number", argv[0]);
if (!ok) return revo_c_err_other(vm, "boom");
int revo_c_err_arity(void *vm, uint64_t got, uint64_t expected);
int revo_c_err_type(void *vm, uint64_t arg, const char *expected, RevoValue got);
int revo_c_err_other(void *vm, const char *msg);

got renders through typeof; messages copy before return, literals fine
bare REVO_ERR_* without a helper raises with a generic message

ref:pub fn revo_c_err_arity(
ref:pub fn revo_c_err_type(
ref:pub fn revo_c_err_other(

each binding is just name and fn_ptr, and every binding lands flat in
the module table under the import’s name

the last row is an all-null terminator

a worked-through example is at ref:examples/foreign/c/extension.c , rebuild the .so after editing

data conversion

RevoValue is nanboxed: numbers are the raw f64 bits, everything else
is the type nibble plus a payload packed into the low 48 bits (an intern
id, except opaque, which stores the low 48 bits of the pointer).
the type helpers read a c value out of the same word:

`revo type`    - `c check`          - `c extractor`

42 (number)    - revo_is_number   - revo_num_value
"hi" (string)  - revo_is_string   - revo_string_id
:atom          - revo_is_atom     - revo_atom_id
fn()           - revo_is_function - revo_function_id
{} (table)     - revo_is_table    - revo_table_id
{1, 2} (table) - revo_is_table    - revo_table_id
opaque ptr    - revo_is_opaque      - revo_opaque_ptr
resource ptr   - revo_is_resource     - revo_resource_ptr

a value can be moved through any of the constructors in its row
(the word is self-describing, no separate tag field)

strings must be interned before returning:

const char *msg = "hello";
uint64_t id = revo_intern(vm, msg, 5);
*out = revo_string_val(id);

loading from revo

imports are flat and untyped, so the .so is opaque to the compiler and
nothing checks calls made straight into it:

put an .rv module next to the library that returns one ascribed table, and callers typecheck against it:

ref:fn cload(

building

# linux
cc -shared -fPIC -o extension.so extension.c -I/path/to/zig-out/include

# macos
cc -shared -fPIC -o extension.dylib extension.c -I/path/to/zig-out/include

best practices