libant is the C API that lets you embed the Ant JavaScript engine directly into your own application. You create a runtime instance, optionally expose C functions as JavaScript globals, evaluate scripts, and drive the async event loop — all from C. This is how you add scriptable behavior to a game engine, a CLI tool, a config system, or any other C program without shipping a separate runtime binary.
Build the embedded library and the example with the scripts in
libant/: run ./libant/build.sh to compile libant, then ./libant/example.sh to build and run examples/embed/embed.c.Getting started
Include<ant.h> and link against libant. Every program that embeds Ant follows the same three-step lifecycle: create a runtime, do work, destroy it.
Create and initialize a runtime
Call Pass
js_create_dynamic() to allocate a new runtime. Then set the stack base so the garbage collector can scan the native stack, and call ant_runtime_init to wire up the standard built-ins.NULL as the last argument to ant_runtime_init when you do not need persistent localStorage. Pass a file path string to enable persistence.Evaluate JavaScript
Use
js_eval_bytecode_eval(js, code, len) to compile and run a string of JavaScript. It returns an ant_value_t — check its type with vtype() before reading the value.Core API reference
Runtime lifecycle
Allocate and return a new Ant runtime instance. Returns
NULL on failure. Each instance is fully independent — you can create multiple runtimes in the same process.Set the GC stack-base pointer. Pass the address of a local variable declared near the top of the frame that owns the runtime. The GC uses this to find the bottom of the native stack when scanning for roots.
Override the JavaScript stack size in bytes. Call before any evaluation if you need a non-default stack limit.
Initialize the runtime with command-line arguments and optional
localStorage persistence. Pass NULL for localstorage_file to disable persistence.Compile and evaluate the JavaScript string
code of length len. State (variables, function definitions) accumulates across multiple calls on the same runtime — see Stateful sessions.Drive the async event loop to completion. Call this after
js_eval_bytecode_eval if your script schedules timers, resolves promises, or enqueues microtasks.Destroy the runtime and free all associated memory.
Value types
Every JavaScript value is represented asant_value_t. Use vtype(val) to inspect its type before reading it.
| Type constant | JavaScript type |
|---|---|
T_NUM | number |
T_STR | string |
T_OBJ | object |
T_ARR | array |
T_BOOL | boolean |
T_ERR | Error |
T_UNDEF | undefined |
T_NULL | null |
Creating values
Create a number value.
Create a string value from a C string and length. Ant copies the bytes — you do not need to keep
ptr alive after the call.Create an empty object (
{}).Create an empty array (
[]).Wrap a C function pointer as a JavaScript function value. The function must have the signature
ant_value_t fn(ant_t *js, ant_value_t *args, int nargs).Boolean constants. Use directly — no function call needed.
Create an
undefined value.Create a
null value.Create an Error value with the given message string.
Reading values
Extract a
double from a number value.Extract a C string from a string value, writing its length into
len.Convert any value to a display string, equivalent to calling
String(val) in JavaScript. Useful for printing and logging.Exposing C functions to JavaScript
Declare your C functions with the required signature, register them as globals usingjs_set and js_mkfun, then call them from any evaluated script.
js_chkargs(args, nargs, fmt) validates that the argument list matches a format string. Use "d" for number, "s" for string. It returns false if validation fails, at which point you should return an error value.
Object and array operations
Return the global object. Use
js_set and js_get on this to expose values to all evaluated scripts.Read a property from an object by key string.
Set a property on an object.
Append a value to the end of an array.
Read an element from an array by zero-based index.
Return the length of an array.
Calling JavaScript functions from C
Evaluate the function definitions first, then retrieve the function values from the global object and call them withsv_vm_call.
sv_vm_call(vm, js, fn, this, args, nargs, NULL, false) — pass js->vm for the first argument, the function value, a this value (js_mkundef() for a plain call), and the argument array with its length.
Iterating object properties
Usejs_prop_iter_begin, js_prop_iter_next, and js_prop_iter_end to walk the enumerable properties of an object:
js_prop_iter_end even if you exit the loop early, to release resources held by the iterator.
Stateful sessions
State accumulates across multiplejs_eval_bytecode_eval calls on the same runtime. You can define variables and functions in one call and use them in later calls:
Async code and the event loop
Scripts that usesetTimeout, Promise, or queueMicrotask schedule work on the event loop. Call js_run_event_loop(js) after evaluating the script to drain all pending work:
Error handling
js_eval_bytecode_eval never throws — it returns an ant_value_t with type T_ERR on failure. Always check before using the result:
js_mkerr(js, msg) on failure rather than calling abort or exit. Ant turns the returned error value into a JavaScript exception that the script can catch.