Skip to main content

Crate remedy_lua

Crate remedy_lua 

Source
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/ xpcall with any value. .lua files load as plain Lua 5.4.
  • RemedyLua additions (.rlua files, Dialect::RemedyLua): optional type annotations (local v: number = 1, function signatures, table shapes, type aliases), 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 with Error::Limit; scripts cannot catch it (pcall, xpcall, coroutine.resume and __close all 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, and load takes 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:

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§

CollectorStatus
What Lua::collector_status reports.
DataLimits
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::interrupt makes the call stop with Error::Limit(LimitKind::Interrupted) within a fraction of a millisecond of work, wherever it is (a loop, a long gsub or sort, a coroutine), and scripts cannot catch it. It stays set until Interrupt::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 Stream or the host’s opener, as C’s errno: strerror’s message and the number. Scripts see nil, 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).
ModuleSource
A module’s source for require (Libs::REQUIRE), from the host: Lua source text, compiled as load compiles it (text only, charged the same) as chunk @origin. require gives origin as 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.
TableRef
A table.
ThreadRef
A thread (coroutine).
UdRef
A full userdata: a host value with an optional metatable.

Enums§

CollectorPhase
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.
LimitKind
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 (StrRef and friends); the rest are plain data. Values are Copy: holding one does not keep what it names alive (see Root).

Constants§

MAX_DATA_DEPTH
The deepest nesting Lua::to_data follows, whatever DataLimits::depth asks.

Traits§

Stream
A stream a script’s io reads 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 .rlua or .lua file, by name’s extension) without running anything, and type-checks a .rlua file’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 a Severity::Warning if the check stopped at its work or memory bound. A plain .lua file 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 memory check may 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).
ModuleLoader
The host’s module loader (Lua::set_module_loader): the source of the module named, or None when 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.