Expand description
RemedyLua: a Lua 5.4-compatible scripting language, entirely in safe Rust.
- Lua 5.4 semantics: values, tables with array and hash parts, metatables and
metamethods (
__index,__newindex,__call,__eq,__lt,__le,__concat,__len, arithmetic and bitwise ones,__close,__tostring,__name,__pairs,__metatable,__mode), closures with upvalues, varargs, multiple returns, integer and float subtypes,goto,<const>and<close>, coroutines,error/pcall/xpcallwith any value..luafiles load as plain Lua 5.4. - RemedyLua additions (
.rluafiles,Dialect::RemedyLua): optional type annotations (local v: number = 1, function signatures, table shapes,typealiases), checked by a gradual type checker (check), compound assignment (+= -= *= /= //= %= ..=),continue, and string interpolation (`hi {name}`). They are pure syntax at run time: they compile to the same bytecode plain Lua would. - A strict superset of Lua 5.4: C Lua’s fixed implementation limits (200 locals, 255 upvalues, syntax nesting, C stack depth) are gone; only the host’s budgets bound a program.
- Limits in every step (
Limits): fuel (each instruction, and library functions charge for their work:string.rep,gsub,sort…), a memory cap counted when memory is taken, call depth, nesting of library callbacks, string and output sizes. A call over its fuel or memory stops withError::Limit; scripts cannot catch it (pcall,xpcall,coroutine.resumeand__closeall pass it on). - An incremental collector: marking and sweeping run in small steps paid for by allocation, so scripts run between frames without long pauses.
- A host API: Rust functions (
Lua::create_function,Lua::register), userdata (Lua::create_userdata), owned data in and out (Data), and capabilities (Libs): a host chooses which libraries each script gets. There is no file, process or network access in the language itself, andloadtakes text only, when allowed.
§Embedding: handles and their lifetimes
A Value is Copy and does not keep what it names alive. A heap value the host holds
is valid while one of these holds:
- it is reachable from the script (a global, a table the script keeps);
- a
Rootholds it (Lua::root); - the host made or got it outside a call (
Lua::create_string,Lua::create_table,Lua::create_function,Lua::create_userdata,Lua::from_data,Lua::get, and the results ofLua::call/Lua::exec): then it is valid until the host’s nextLua::call(orexec,call_global) returns, or until it callsLua::collect_garbage. And so that a host that never calls cannot fill memory with them, they are let go when memory is full outside a call even after a collection (then refused as collected); - inside a host function: it is one of the function’s arguments, or the function made it
(valid until the function returns). The results of a
Lua::callmade inside one are valid until its nextcall.
A stale handle is refused (Error::Host) or reads as nil; it never reaches another
object, and a handle from another state is refused too. A panic that unwinds through a
call (a host function’s, a clock’s, a print sink’s) poisons the state: every later call
returns Error::Host. The reference has the full embedding guide.
use remedy_lua::{Lua, Limits, Libs, Value};
let mut lua = Lua::new(Limits::default(), Libs::STANDARD);
let out = lua.exec("local t = {} for i = 1, 3 do t[i] = i * i end return #t, t[3]", "=demo").unwrap();
assert!(matches!(out[..], [Value::Int(3), Value::Int(9)]));Structs§
- Collector
Status - What
Lua::collector_statusreports. - Data
Limits - Bounds for copying a value out as
Data. - Diagnostic
- One message about source text.
- FuncRef
- A function: a Lua closure or a native (Rust) function.
- Interrupt
- A handle that stops the state’s running call from any thread:
Interrupt::interruptmakes the call stop withError::Limit(LimitKind::Interrupted)within a fraction of a millisecond of work, wherever it is (a loop, a longgsuborsort, a coroutine), and scripts cannot catch it. It stays set untilInterrupt::reset, and while it is set every entry into the state (a call, or making a value) stops at once. - IoError
- An error from a
Streamor the host’s opener, as C’serrno:strerror’s message and the number. Scripts seenil, message, code. - Libs
- The libraries a script gets: its capabilities. Combine with
|. - Limits
- How much a state may use. Fuel is per call from the host; the rest hold always.
- Lua
- What runs scripts: a RemedyLua state with its globals, heap and limits. One thread
uses it at a time; it may move between threads (
Send). - Module
Source - A module’s source for
require(Libs::REQUIRE), from the host: Lua source text, compiled asloadcompiles it (text only, charged the same) as chunk@origin.requiregivesoriginas its second result, as C Lua gives a module’s file name. - Pos
- A place in source text: line and column, both from 1.
- Root
- A value kept alive while this handle lives (values themselves do not keep objects
alive; see
Value). - StrRef
- A string.
- Table
Ref - A table.
- Thread
Ref - A thread (coroutine).
- UdRef
- A full userdata: a host value with an optional metatable.
Enums§
- Collector
Phase - The collector’s phase (
Lua::collector_status). - Data
- Owned data that crosses between Rust and scripts by copy: no functions, userdata or
threads; tables become key-value lists in traversal order. More variants may come:
match with a
_arm. - Dialect
- Which language a source is in.
- Error
- What went wrong.
- Limit
Kind - Which limit stopped a call. More kinds may come: match with a
_arm. - Severity
- How serious a diagnostic is. More levels may come: match with a
_arm. - Value
- A RemedyLua value. Strings, tables, functions, userdata and threads are references
into the collector’s heap (
StrRefand friends); the rest are plain data. Values areCopy: holding one does not keep what it names alive (seeRoot).
Constants§
- MAX_
DATA_ DEPTH - The deepest nesting
Lua::to_datafollows, whateverDataLimits::depthasks.
Traits§
- Stream
- A stream a script’s
ioreads and writes. Each method defaults to C’s failure on a stream that cannot do it. Calls run inside the script’s call: a stream that blocks holds the script (fuel and the deadline are not read meanwhile).
Functions§
- check
- Parses
source(a.rluaor.luafile, byname’s extension) without running anything, and type-checks a.rluafile’s annotations: its diagnostics. A syntax error is the only diagnostic (the first one). Otherwise, for.rlua, the type errors (Severity::Error, at most 500), and aSeverity::Warningif the check stopped at its work or memory bound. A plain.luafile has no annotations, so it gets none. Checking never changes how the program runs (see The type checker). - format
- Re-indents
source(four spaces a level, normalized spacing at line starts), keeping comments and everything else as written. Fails when the source does not parse (within the memorycheckmay use).
Type Aliases§
- Clock
- The host’s clock for time limits: the time now, from any fixed start (a wall clock, or a thread’s CPU clock).
- Module
Loader - The host’s module loader (
Lua::set_module_loader): the source of the module named, orNonewhen the host has no such module. It runs inside the script’s call, on the host’s own time; it gets no access to the state.