Bindings for the Lua programming language
Dependencies
moon update
moon add tonyfettes/lualet lua = @aux.new_state()
match lua {
Some(l) => {
// Load standard libraries
@lib.open_libs(l)
// Push and execute Lua code
@lua.push_integer(l, 42)
let value = @lua.to_integer(l, -1)
@lua.pop(l, 1)
// Clean up
@lua.close(l)
}
None => println("Failed to create Lua state")
}test "arith" {
let lua = @aux.new_state()
guard lua is Some(lua) else { fail("cannot create state") }
@lua.push_integer(lua, 10)
@lua.push_integer(lua, 32)
@lua.arith(lua, Add)
let val = @lua.to_integer(lua, -1)
@lua.pop(lua, 1)
@lua.close(lua)
inspect(val, content="42")
}test "load" {
let src = b"print(\"Hello, world!\")"
let mut off = 0UL
let lua = @aux.new_state()
guard lua is Some(lua) else { fail("cannot create state") }
defer @lua.close(lua)
@lib.open_libs(lua)
let status = @lua.load(lua, reader_func, "<test>")
guard status is @lua.Ok else { fail("error loading chunk") }
let status = @lua.pcall(lua, 0, 0, 0)
guard status is @lua.Ok else { fail("error calling function") }
}moon build # Build the project
moon test # Run tests
moon check # Lint the codefn l_alloc(
ptr : @c.Pointer[Unit],
osize : UInt64,
nsize : UInt64,
) -> @c.Pointer[Unit] {
ignore(osize)
if nsize == 0 {
@memory.free(ptr)
return @c.Pointer::null()
} else {
return @memory.realloc(ptr, nsize)
}
}type CFunctionfn foo(l : @lua.State) -> Int {
let n = @lua.get_top(l) // number of arguments
let mut sum = 0.0
for i = 1; i <= n; i = i + 1 {
if !@lua.is_number(l, i) {
@lua.push_string(l, b"incorrect argument")
@lua.error(l)
}
sum @lua.to_number(l, i)
}
@lua.push_number(l, sum / n) // first result
@lua.push_number(l, sum) // second result
return 2 // number of results
}pub(all) enum Type {
Nil
Boolean
LightUserdata
Number
String
Table
Function
Userdata
Thread
}pub(all) type WarnFunction (Bytes, Bool) -> Unit#deprecated("Use `struct T(A)` to declare a newtype and use `.0` access the underlying type instead.")
fn WarnFunction::inner(self : WarnFunction) -> ((Bytes, Bool) -> Unit)let ErrMem : Int a = f("how", t.x, 14)@lua.get_global(lua, "f"); // function to be called
@lua.push_literal(lua, "how"); // 1st argument
@lua.get_global(lua, "t"); // table to be indexed
@lua.get_field(lua, -1, "x"); // push result of t.x (2nd arg)
@lua.remove(lua, -2); // remove 't' from the stack
@lua.push_integer(lua, 14); // 3rd argument
@lua.call(lua, 3, 1); // call 'f' with 3 arguments and 1 result
@lua.set_global(lua, "a"); // set global 'a'let ar = Debug(@memory.malloc(Debug::sizeof()));
defer @memory.free(ar.0);
@lua.get_global(lua, "f"); // get global 'f'
@lua.get_info(lua, ">S", ar);
println("\{ar.line_defined()}");let lua : State = ...
// table is in the stack at index 't'
lua.push_nil()
while lua.next(t) {
// uses 'key' (at index -2) and 'value' (at index -1)
let key_tn = lua.type_name(lua.type_(-2))
let val_tn = lua.type_name(lua.type_(-1))
println("\{key_tn} - \{val_tn}")
// removes 'value'; keeps 'key' for next iteration
lua.pop(1)
}fn upvalue_index(i : Int) -> IntBindings for the Lua programming language
Dependencies