API reference

green Lus stable functions
red Lus unstable functions
blue Lua functions
gray Lua functions removed in Lus

Environment

Generic

_G _VERSION assert collectgarbage dofile error fromjson getmetatable ipairs load loadfile next pairs pcall pledge print rawequal rawget rawlen rawset require select setmetatable tojson tonumber tostring type warn xpcall

Coroutine

coroutine.close coroutine.create coroutine.isyieldable coroutine.resume coroutine.running coroutine.status coroutine.wrap coroutine.yield

Debug

debug.debug debug.gethook debug.getinfo debug.getlocal debug.getmetatable debug.getregistry debug.getupvalue debug.getuservalue debug.parse debug.sethook debug.setlocal debug.setmetatable debug.setupvalue debug.setuservalue debug.traceback debug.upvalueid debug.upvaluejoin

Filesystem

fs.copy fs.createdirectory fs.createlink fs.follow fs.move fs.list fs.remove fs.type

fs.path.delimiter fs.path.join fs.path.name fs.path.parent fs.path.separator fs.path.split

IO

io.close io.flush io.input io.lines io.open io.output io.popen io.read io.stderr io.stdin io.stdout io.tmpfile io.type io.write file:close file:flush file:lines file:read file:seek file:setvbuf file:write


Math

math.abs math.acos math.asin math.atan math.ceil math.cos math.deg math.exp math.floor math.fmod math.frexp math.huge math.ldexp math.log math.max math.maxinteger math.min math.mininteger math.modf math.pi math.rad math.random math.randomseed math.sin math.sqrt math.tan math.tointeger math.type math.ult

Network

network.fetch network.tcp.bind network.tcp.connect network.udp.open server:accept server:close server:settimeout socket:close socket:receive socket:send socket:settimeout udp:close udp:receive udp:sendto

Worker

worker.create worker.status worker.receive worker.send worker.message worker.peek

OS

os.clock os.date os.difftime os.execute os.exit os.getenv os.remove os.rename os.platform os.setlocale os.time os.tmpname

Package

package.config package.cpath package.loaded package.loadlib package.path package.preload package.searchers package.searchpath

String

string.byte string.char string.dump string.find string.format string.gmatch string.gsub string.len string.lower string.match string.pack string.packsize string.rep string.reverse string.sub string.transcode string.unpack string.upper

Table

table.clone table.concat table.create table.insert table.move table.pack table.remove table.sort table.unpack

Vector

vector.create vector.pack vector.unpack vector.clone vector.size vector.resize vector.unpackmany

UTF-8

utf8.char utf8.charpattern utf8.codepoint utf8.codes utf8.len utf8.offset

Metamethods

__add __band __bnot __bor __bxor __call __close __concat __div __eq __gc __idiv __index __json __le __len __lt __metatable __mod __mode __mul __name __newindex __pairs __pow __shl __shr __slice __sub __tostring __unm

Environment variables

LUA_CPATH LUA_CPATH_5_5 LUA_INIT LUA_INIT_5_5 LUA_PATH LUA_PATH_5_5

C API

Main library

lua_Alloc lua_CFunction lua_Debug lua_Hook lua_Integer lua_KContext lua_KFunction lua_Number lua_Reader lua_State lua_Unsigned lua_WarnFunction lua_Writer

lua_absindex lua_arith lua_atpanic lua_call lua_callk lua_checkstack lua_close lua_closeslot lua_closethread lua_compare lua_concat lua_copy lua_createtable lua_dump lua_error lua_gc lua_getallocf lua_getextraspace lua_getfield lua_getglobal lua_gethook lua_gethookcount lua_gethookmask lua_geti lua_getinfo lua_getiuservalue lua_getlocal lua_getmetatable lua_getstack lua_gettable lua_gettop lua_getupvalue lua_insert lua_isboolean lua_iscfunction lua_isenum lua_isfunction lua_isinteger lua_islightuserdata lua_isnil lua_isnone lua_isnoneornil lua_isnumber lua_isstring lua_istable lua_isthread lua_isuserdata lua_isvector lua_isyieldable lua_len lua_load lua_newstate lua_newtable lua_newthread lua_newuserdatauv lua_next lua_numbertocstring lua_numbertointeger lua_pcall lua_pcallk lua_pop lua_pushboolean lua_pushcclosure lua_pushcfunction lua_pushenum lua_pushexternalstring lua_pushfstring lua_pushglobaltable lua_pushinteger lua_pushlightuserdata lua_pushliteral lua_pushlstring lua_pushnil lua_pushnumber lua_pushstring lua_pushthread lua_pushvalue lua_pushvfstring lua_rawequal lua_rawget lua_rawgeti lua_rawgetp lua_rawlen lua_rawset lua_rawseti lua_rawsetp lua_register lua_remove lua_replace lua_resume lua_rotate lua_setallocf lua_setfield lua_setglobal lua_sethook lua_seti lua_setiuservalue lua_setlocal lua_setmetatable lua_settable lua_settop lua_setupvalue lua_setwarnf lua_status lua_stringtonumber lua_toboolean lua_tocfunction lua_toclose lua_toenumidx lua_tointeger lua_tointegerx lua_tolstring lua_tonumber lua_tonumberx lua_topointer lua_tostring lua_tothread lua_touserdata lua_type lua_typename lua_upvalueid lua_upvalueindex lua_upvaluejoin lua_version lua_warning lua_xmove lua_yield lua_yieldk


Auxiliary library

luaL_Buffer luaL_Reg luaL_Stream

luaL_addchar luaL_addgsub luaL_addlstring luaL_addsize luaL_addstring luaL_addvalue luaL_alloc luaL_argcheck luaL_argerror luaL_argexpected luaL_buffaddr luaL_buffinit luaL_buffinitsize luaL_bufflen luaL_buffsub luaL_callmeta luaL_checkany luaL_checkinteger luaL_checklstring luaL_checknumber luaL_checkoption luaL_checkstack luaL_checkstring luaL_checktype luaL_checkudata luaL_checkversion luaL_dofile luaL_dostring luaL_error luaL_execresult luaL_fileresult luaL_getmetafield luaL_getmetatable luaL_getsubtable luaL_gsub luaL_len luaL_loadbuffer luaL_loadbufferx luaL_loadfile luaL_loadfilex luaL_loadstring luaL_makeseed luaL_newlib luaL_newlibtable luaL_newmetatable luaL_newstate luaL_openlibs luaL_openselectedlibs luaL_opt luaL_optinteger luaL_optlstring luaL_optnumber luaL_optstring luaL_prepbuffer luaL_prepbuffsize luaL_pushfail luaL_pushresult luaL_pushresultsize luaL_ref luaL_requiref luaL_setfuncs luaL_setmetatable luaL_testudata luaL_tolstring luaL_traceback luaL_typeerror luaL_typename luaL_unref luaL_where

Library openers

luaopen_base luaopen_coroutine luaopen_debug luaopen_io luaopen_math luaopen_os luaopen_package luaopen_string luaopen_table luaopen_utf8 luaopen_fs luaopen_json luaopen_network luaopen_vector luaopen_worker

Worker System

lus_WorkerSetup lus_onworker lus_worker_pool_init lus_worker_pool_shutdown

Permission System

lus_PledgeRequest lus_PledgeGranter LUS_PLEDGE_GRANT lus_initpledge lus_nextpledge lus_setpledge lus_pledge lus_haspledge lus_registerpledge lus_rejectpledge lus_rejectrequest lus_pledgeerror lus_revokepledge lus_issealed lus_checkfsperm

Constants

LUA_ERRERR LUA_ERRFILE LUA_ERRMEM LUA_ERRRUN LUA_ERRSYNTAX LUA_HOOKCALL LUA_HOOKCOUNT LUA_HOOKLINE LUA_HOOKRET LUA_HOOKTAILCALL LUA_LOADED_TABLE LUA_MASKCALL LUA_MASKCOUNT LUA_MASKLINE LUA_MASKRET LUA_MAXINTEGER LUA_MININTEGER LUA_MINSTACK LUA_MULTRET LUA_NOREF LUA_OK LUA_OPADD LUA_OPBAND LUA_OPBNOT LUA_OPBOR LUA_OPBXOR LUA_OPDIV LUA_OPEQ LUA_OPIDIV LUA_OPLE LUA_OPLT LUA_OPMOD LUA_OPMUL LUA_OPPOW LUA_OPSHL LUA_OPSHR LUA_OPSUB LUA_OPUNM LUA_PRELOAD_TABLE LUA_REFNIL LUA_REGISTRYINDEX LUA_RIDX_GLOBALS LUA_RIDX_MAINTHREAD LUA_TBOOLEAN LUA_TFUNCTION LUA_TLIGHTUSERDATA LUA_TNIL LUA_TNONE LUA_TNUMBER LUA_TENUM LUA_TSTRING LUA_TTABLE LUA_TVECTOR LUA_TTHREAD LUA_TUSERDATA LUA_USE_APICHECK LUA_YIELD LUAL_BUFFERSIZE

pledge (name...)

Grants or checks a permission. Returns true if the permission was granted, false if it was denied or the state is sealed.

The name arguments specify the permissions to grant or check.

Special permissions: "all" grants all permissions (CLI only), "seal" prevents future permission changes. The ~ prefix rejects a permission permanently.

pledge("fs")           -- grant filesystem access
pledge("fs:read=/tmp") -- grant read access to /tmp only
pledge("~network")     -- reject network permission
pledge("seal")         -- lock permissions

lus_PledgeRequest

typedef struct lus_PledgeRequest {
  const char *base;     /* "fs", "network", etc. */
  const char *sub;      /* "read", "tcp", or NULL */
  const char *value;    /* requested value, or NULL */
  const char *current;  /* current stored value during iteration */
  int status;           /* LUS_PLEDGE_GRANT, UPDATE, or CHECK */
  int count;            /* number of stored values */
  int has_base;         /* 1 if base permission already granted */
} lus_PledgeRequest;

Request structure passed to granter callbacks. Contains all information about the permission being granted or checked.

status indicates the operation: LUS_PLEDGE_GRANT for new grants, LUS_PLEDGE_UPDATE for updates, LUS_PLEDGE_CHECK for access checks.

lus_PledgeGranter

typedef void (*lus_PledgeGranter)(lua_State *L, lus_PledgeRequest *p);

Callback type for permission granters. Libraries register granters to handle their own permission validation logic.

Granters should call lus_setpledge to confirm valid permissions. Unprocessed requests are automatically denied. For invalid subpermissions, use luaL_error to report an error.

Status Constants

#define LUS_PLEDGE_GRANT  0  /* New permission request */
#define LUS_PLEDGE_UPDATE 1  /* Updating existing permission */
#define LUS_PLEDGE_CHECK  2  /* Read-only permission check */

Values for lus_PledgeRequest.status. Granters should handle GRANT/UPDATE for permission grants and CHECK for access checks.

lus_initpledge

void lus_initpledge(lua_State *L, lus_PledgeRequest *p, const char *base);

Initializes a pledge request for C-side grants. This bypasses granters and is used for direct permission grants from C code.

lus_PledgeRequest p;
lus_initpledge(L, &p, "fs");
lus_setpledge(L, &p, "read", "/tmp/*");

lus_nextpledge

int lus_nextpledge(lua_State *L, lus_PledgeRequest *p);

Iterates through stored values for a permission. Sets p->current to the next stored value.

Returns 1 if there are more values, 0 when done.

while (lus_nextpledge(L, p)) {
  if (match(p->current, p->value))
    lus_setpledge(L, p, p->sub, NULL);
}

lus_setpledge

void lus_setpledge(lua_State *L, lus_PledgeRequest *p,
                   const char *sub, const char *value);

Confirms or sets a pledge value. Marks the request as processed, preventing automatic denial. Must be called by granters to approve a permission.

sub is the subpermission (e.g., "read") or NULL for base. value is the value to store (e.g., path) or NULL for unrestricted.

lus_rejectrequest

void lus_rejectrequest(lua_State *L, lus_PledgeRequest *p);

Permanently rejects a permission using the request struct. Future attempts to grant this permission will fail.

lus_pledgeerror

void lus_pledgeerror(lua_State *L, lus_PledgeRequest *p, const char *msg);

Sets a denial error message for user-facing feedback. Called by granters to explain why a permission was denied.

lus_pledge

int lus_pledge(lua_State *L, const char *name, const char *value);

Grants a permission to the Lua state. Triggers the granter callback for validation. Returns 1 on success, 0 if denied or sealed.

Throws an error if the permission name is not recognized (no granter registered).

lus_haspledge

int lus_haspledge(lua_State *L, const char *name, const char *value);

Checks if a permission has been granted. The value argument specifies the resource being accessed (e.g., a file path).

Returns 1 if access is allowed, 0 if denied.

lus_registerpledge

void lus_registerpledge(lua_State *L, const char *base, lus_PledgeGranter fn);

Registers a granter callback for a permission namespace. Call this in your library's luaopen_* function.

static void my_granter(lua_State *L, lus_PledgeRequest *p) {
  if (p->status == LUS_PLEDGE_CHECK) {
    lus_setpledge(L, p, p->sub, NULL);  /* allow */
  } else {
    lus_setpledge(L, p, p->sub, p->value);  /* grant */
  }
}

LUAMOD_API int luaopen_mylib(lua_State *L) {
  lus_registerpledge(L, "mylib", my_granter);
  /* ... */
}

lus_rejectpledge

int lus_rejectpledge(lua_State *L, const char *name);

Permanently rejects a permission by name (string-based version). Returns 1 on success, 0 if sealed.

lus_revokepledge

int lus_revokepledge(lua_State *L, const char *name);

Revokes a previously granted permission. Clears all stored values for the permission. Returns 1 on success, 0 if sealed or not found.

lus_issealed

int lus_issealed(lua_State *L);

Returns 1 if the permission state is sealed, 0 otherwise. When sealed, no new permissions can be granted or rejected.

lus_checkfsperm

int lus_checkfsperm(lua_State *L, const char *perm, const char *path);

Convenience function for filesystem permission checks. Checks if perm (e.g., "fs:read") is granted for path. Raises an error if denied.

lus_checkfsperm(L, "fs:read", "/etc/passwd");  /* error if denied */

worker.create (path, ...)

Spawns a new worker running the script at path. Optional varargs are serialized and can be received by the worker via worker.peek(). Returns a worker handle. Requires load and fs:read pledges.

local w = worker.create("worker.lus", "hello", 42)
-- worker can receive "hello" and 42 via worker.peek()

worker.status (w)

Returns the status of worker w: "running" if the worker is still executing, or "dead" if it has finished or errored.

worker.receive (w1, ...)

Blocking select-style receive from one or more workers. Blocks until at least one worker has a message. Returns one value per worker: the message if available, or nil if that worker has no message. Propagates worker errors.

local msg = worker.receive(w)
-- or multi-worker select:
local m1, m2 = worker.receive(w1, w2)

worker.send (w, value)

Sends value to worker w's inbox. The worker can receive it via worker.peek(). Values are deep-copied.

worker.message (value)

(Worker-side only) Sends value to the worker's outbox for the parent to receive via worker.receive().

worker.peek ()

(Worker-side only) Blocking receive from the worker's inbox. Blocks until a message from the parent (via worker.send()) is available.

lus_WorkerSetup

typedef void (*lus_WorkerSetup)(lua_State *parent, lua_State *worker);

Callback type for worker state initialization. Called when a new worker is created, allowing embedders to configure the worker's Lua state (e.g., open libraries).

lus_onworker

void lus_onworker(lua_State *L, lus_WorkerSetup fn);

Registers a callback to be invoked when new worker states are created. Typically used to open libraries in worker states.

static void worker_setup(lua_State *parent, lua_State *worker) {
  luaL_openlibs(worker);
}
lus_onworker(L, worker_setup);

lus_worker_pool_init

void lus_worker_pool_init(lua_State *L);

Initializes the global worker thread pool. Called automatically on first worker.create(). M = number of CPU cores (capped at 32).

lus_worker_pool_shutdown

void lus_worker_pool_shutdown(void);

Shuts down the worker thread pool. Waits for all threads to complete.

luaopen_worker

int luaopen_worker(lua_State *L);

Opens the worker library. Called automatically by luaL_openlibs.

debug.parse (code, [chunkname])

Parses a Lus source string and returns its AST (Abstract Syntax Tree) as a nested table structure. Returns nil if parsing fails.

The optional chunkname argument specifies the name used in error messages (defaults to "=(parse)").

local ast = debug.parse("local x = 1 + 2")
-- Returns: {type = "chunk", line = 1, children = {...}}

-- With custom chunk name
local ast = debug.parse(code, "myfile.lus")

Each AST node is a table with at minimum type (node type string) and line (source line number). Additional fields depend on node type. See Acquis 12 for details.

vector.create (capacity [, fast])

Creates a new vector with the given capacity in bytes. If fast is true, the buffer is not zero-initialized (faster but contents are undefined).

local v = vector.create(1024)        -- zero-initialized
local v = vector.create(1024, true)  -- fast, uninitialized

vector.pack (v, offset, fmt, ...)

Packs values into the vector v starting at offset. Uses the same format string as string.pack.

local v = vector.create(16)
vector.pack(v, 0, "I4I4I4", 1, 2, 3)

vector.unpack (v, offset, fmt)

Unpacks values from the vector v starting at offset. Uses the same format string as string.unpack. Returns unpacked values followed by the next offset.

local a, b, c, nextpos = vector.unpack(v, 0, "I4I4I4")

vector.clone (v)

Creates a copy of the vector v.

vector.size (v)

Returns the size of the vector in bytes. Equivalent to #v.

vector.resize (v, newsize)

Resizes the vector to newsize bytes. New bytes are zero-initialized. Existing data within the new size is preserved.

vector.unpackmany (v, offset, fmt [, count])

Returns an iterator that repeatedly unpacks values from v using the format fmt. Optional count limits the number of iterations.

for a, b in vector.unpackmany(v, 0, "I4I4") do
  print(a, b)
end

table.clone (t [, deep])

Creates a copy of the table t. If deep is true, nested tables are recursively cloned. Deep copies preserve circular references.

local x = table.clone(t)        -- shallow copy
local y = table.clone(t, true)  -- deep copy

-- Circular references preserved
local t = {}
t.a = t
local c = table.clone(t, true)
assert(c.a == c)

__slice

The __slice metamethod is called when the slice operator is used on a table. It receives the table, start index, and end index (which may be nil).

local mt = {
  __slice = function(self, start, finish)
    return {start = start, finish = finish}
  end
}
local x = setmetatable({}, mt)
local result = x[5, 10]  -- calls __slice(x, 5, 10)