struct

Luajit::LuaState

Inherits Struct < Value < Object

Constructors

create

Creates a new LuaState and attaches a default #at_panic handler

Source
new(ptr : Pointer(LibLuaJIT::State))
Source

Class methods

destroy(state : LuaState) : Nil

Destroys state and gives tracked values back to Crystal

Source

Instance methods

abs_index(index : Int32) : Int32

Converts index into an equivalent absolute index

Source
assert_any!(index : Int32) : Nil

Raises LuaError unless there is a value at index

Source
assert_bool!(index : Int32) : Nil

Raises LuaError unless the value at index is a boolean

Source
assert_function!(index : Int32) : Nil

Raises LuaError unless the value at index is a function

Source
assert_integer!(index : Int32) : Nil

Raises LuaError unless the value at index is a number

Source
assert_light_userdata!(index : Int32) : Nil

Raises LuaError unless the value at index is a light userdata

Source
assert_nil!(index : Int32) : Nil

Raises LuaError unless the value at index is nil

Source
assert_number!(index : Int32) : Nil

Raises LuaError unless the value at index is a number

Source
assert_string!(index : Int32, size : UInt64) : Nil

Raises LuaError unless the value at index is a string with size

Source
assert_string!(index : Int32) : Nil

Raises LuaError unless the value at index is a string

Source
assert_table!(index : Int32) : Nil

Raises LuaError unless the value at index is a table

Source
assert_thread!(index : Int32) : Nil

Raises LuaError unless the value at index is a thread

Source
assert_type!(index : Int32, type : LuaType) : Nil

Raises LuaError unless the value at index is type

Source
assert_userdata!(index : Int32) : Nil

Raises LuaError unless the value at index is userdata

Source
at_panic(cb : LuaCFunction) : LuaCFunction

https://www.lua.org/manual/5.1/manual.html#lua_atpanic

Source
at_panic

https://www.lua.org/manual/5.1/manual.html#lua_atpanic

Source
attach_metatable(index : Int32, tname : String) : Bool

Attaches a metatable to value at index with name tname if a metatable exists, otherwise returns false

Raises LuaError if operation fails

Source
c_pcall

Calls the C function block in protected mode

All values returned by block are discarded.

NOTE: Adopted from Lua 5.3, because LuaJIT's version has some inconsistencies when dealing with error handling that made it hard to work with.

Source
call_metamethod(obj : Int32, event : String) : Bool

Calls a metamethod

If object at index obj has a metatable with field event, this method calls this field and passes the object as its only argument. In this case, returns true and pushes onto the stack the value returned by the call. Otherwise, returns false and pushes nothing onto the stack.

Source
check_userdata!(index : Int32, tname : String) : Pointer(Void)

Checks whether the value at index is a userdata of type tname and returns it

Raises LuaError unless tname matches

Source
close

Destroys all objects and frees all dynamic memory

Source
co_resume(narg : Int32) : Int32

Starts and resumes a coroutine in a given thread

https://www.lua.org/manual/5.1/manual.html#lua_resume

Source
co_yield(nresults : Int32) : Int32

Yields a coroutine

https://www.lua.org/manual/5.1/manual.html#lua_yield

Source
concat(n : Int32) : Nil

Concatenates the n values at the top of the stack, pops them, and leaves the result at the top

If n == 1, does nothing

If n == 0, pushes empty string

Raises LuaError if operation fails

Source
create_ref

Creates a LuaRef for object at top of stack

Raises LuaError if value at top of stack is nil

Raises LuaError if ref cannot be created

Source
create_table(narr : Int32, nrec : Int32) : Nil

Creates a new empty table and pushes it onto the stack

narr represents pre-allocated array elements

nrec represents pre-allocated non-array elements

Pre-allocation is useful when you know exactly how many elements the table will have.

Source
create_userdata(ptr : Pointer(Void), tname : String) : Pointer(UInt64)

Creates a full userdata with a type tname, and returns a pointer with the address of ptr

See #get_userdata

Source
eq(index1 : Int32, index2 : Int32) : Bool

Returns true if the two values in indices index1 and index2 are equal

Follows the semantics of the Lua == operator (i.e. may call metamethods)

Raises LuaError if operation fails

Source
execute(str : String) : LuaStatus

Loads and runs the given str

Source
execute(path : Path) : LuaStatus

Loads and runs the given path

Source
execute!(str : String) : Nil

Loads and runs the given str

Raises LuaError if operation fails

Source
execute!(path : Path) : Nil

Loads and runs the given path

Raises LuaError if operation fails

Source
gc

Returns LuaGC wrapper

Source
get_environment(name : String) : Nil

Pushes onto the stack the value of the environment name

Raises LuaError if operation fails

Source
get_fenv(index : Int32) : Nil

Pushes onto the stack the environment table of the value at index

Source
get_field(index : Int32, name : String) : Nil

Pushes onto the stack the value t[k], where t is the value at index, and k is the name of the field

As in Lua, this function may trigger a metamethod for the "index" event.

Raises LuaError if operation fails

Source
get_global(name : String) : Nil

Pushes onto the stack the value of the global name

Raises LuaError if operation fails

Source
get_hook

Returns the current hook function

Source
get_hook_count

Returns the current hook count

Source
get_hook_mask

Returns the current hook mask

Source
get_info(what : String, ar : LuaDebug) : LuaDebug | Nil

Returns information about a specific function or function invocation

https://www.lua.org/manual/5.1/manual.html#lua_getinfo

Source
get_local(ar : LuaDebug, n : Int32 = 1) : String | Nil

Gets information about a local variable of ar

https://www.lua.org/manual/5.1/manual.html#lua_getlocal

Source
get_metafield(obj : Int32, e : String) : Bool

Pushes onto the stack the field e from the metatable of the object at index obj

If the object does not have a metatable, or if the metatable does not have this field, returns false and pushes nothing.

Source
get_metatable(tname : String) : Nil

Pushes onto the stack the metatable associated with tname in the registry

Raises LuaError if operation fails

See #new_metatable

Source
get_metatable(index : Int32) : Bool

Pushes onto the stack the metatable of the value at index

If index is not valid, or value does not have a metatable, returns false.

Source
get_ref_value(r : LuaRef) : Nil

Retrieves ref value for r

Source
get_registry(name : String) : Nil

Pushes onto the stack the value of the registry name

Raises LuaError if operation fails

Source
get_stack(level : Int32) : LuaDebug | Nil

Get information about the interpreter runtime stack

https://www.lua.org/manual/5.1/manual.html#lua_getstack

Source
get_table(index : Int32) : Nil

Pushes onto the stack the value t[k], where t is the value at index, and k is the value at the top of the stack

Pops the key from the stack (putting the resulting value in its place).

As in Lua, this function may trigger a metamethod for the "index" event

Raises LuaError if operation fails

Source
get_type(index : Int32) : LuaType

Returns the type of the value at the given index

Source
get_upvalue(fn_index : Int32, n : Int32) : String | Nil

Gets information about a closure's upvalue

https://www.lua.org/manual/5.1/manual.html#lua_getupvalue

Source
get_userdata(index : Int32, tname : String) : Pointer(Void)

Retrieves a full userdata at index with name tname and returns it

NOTE: Can only be used if the userdata was originally created from #create_userdata.

Raises LuaError if operation fails

Source
insert(index : Int32) : Nil

Moves the top element into index, shifting elements above to open space

WARNING: Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.

Source
is_bool?(index : Int32) : Bool

Returns true if value at the given index has type boolean

Source
is_c_function?(index : Int32) : Bool

Returns true if value at the given index is a C function

Source
is_function?(index : Int32) : Bool

Returns true if value at the given index is a function (either C or Lua)

Source
is_light_userdata?(index : Int32) : Bool

Returns true if value at the given index is a light userdata

Source
is_nil?(index : Int32) : Bool

Returns true if value at the given index is nil

Source
is_none?(index : Int32) : Bool

Returns true if value at the given index is not valid (refers to an element outside the current stack)

Source
is_none_or_nil?(index : Int32) : Bool

Returns true if value at the given index is not valid (refers to an element outside the current stack) or is nil

Source
is_number?(index : Int32) : Bool

Returns true if value at the given index is a number or a string convertible to a number

Source
is_pseudo?(index : Int32) : Bool

Returns true if index is a pseudo-index

Source
is_string?(index : Int32) : Bool

Returns true if value at the given index is a string or a number (which is always convertible to a string)

Source
is_table?(index : Int32) : Bool

Returns true if value at the given index is a table

Source
is_thread?(index : Int32) : Bool

Returns true if value at the given index is a thread

Source
is_userdata?(index : Int32) : Bool

Returns true if value at the given index is a userdata (either full or light)

Source
less_than(index1 : Int32, index2 : Int32) : Bool

Returns true if the value at index1 is smaller than the value at index2

Follows the semantics of the Lua < operator (i.e. may call metamethods)

Raises LuaError if operation fails

Source
new_metatable(tname : String) : Bool

Creates a new table to be used as a metatable for userdata, adds it to the registry with key tname, and returns true

If the registry already has the key tname, returns false.

In both cases pushes onto the stack the final value associated with tname in the registry.

Source
new_table

Creates a new empty table and pushes it onto the stack

Source
new_thread

Creates a new thread, represented as a new LuaState, and pushes it onto the stack

Shares all global objects (such as tables), but has independent stack.

Source
new_userdata(size : UInt64) : Pointer(Void)

Allocates a new block of memory with the given size, pushes onto the stack a new full userdata with the block address, and returns this address.

Userdata represent C values in Lua. A full userdata represents a block of memory. It is an object (like a table): you must create it, it can have its own metatable, and you can detect when it is being collected. A full userdata is only equal to itself (under raw equality).

When Lua collects a full userdata with a gc metamethod, Lua calls the metamethod and marks the userdata as finalized. When this userdata is collected again then Lua frees its corresponding memory.

Source
new_userdata(size : Int32) : Pointer(Void)

Allocates a new block of memory with the given size, pushes onto the stack a new full userdata with the block address, and returns this address.

Userdata represent C values in Lua. A full userdata represents a block of memory. It is an object (like a table): you must create it, it can have its own metatable, and you can detect when it is being collected. A full userdata is only equal to itself (under raw equality).

When Lua collects a full userdata with a gc metamethod, Lua calls the metamethod and marks the userdata as finalized. When this userdata is collected again then Lua frees its corresponding memory.

Source
next(index : Int32) : Bool

Pops a key from the stack, and pushes a key-value pair from the table at index (the "next" pair after the given key).

If there are no more elements in the table, then returns false (and pushes nothing).

While traversing a table, do not call #to_string directly on a key, unless you know that the key is actually a string. Recall that #to_string changes the value at the given index; this confuses the next call to #next.

Raises LuaError if operation fails

A typical traversal looks like this:

# table is in the stack at index 't'
state.push(nil)
while state.next(t)
  # uses 'key' (at index -2) and 'value' (at index -1)
  puts "#{state.type_name_at(-2)} - #{state.type_name_at(-1)}"
  # removes 'value'; keeps 'key' for next iteration
  state.pop(1)
end
Source
open_library(type : LuaLibrary) : Nil

Opens standard Lua library type

Source
pcall(nargs : Int32, nresults : Int32, errfunc : Int32 = 0) : LuaStatus

Calls a function in protected mode

https://www.lua.org/manual/5.1/manual.html#lua_pcall

Source
pcall(nargs : Int32, nresults : Int32, errfunc : Int32 = 0, & : LuaStatus -> ) : LuaStatus

Same as #pcall, but yields LuaStatus on failure

Source
pop(n : Int32) : Nil

Pops n elements from the stack

Source
push(_x : Nil) : Nil

Pushes a nil value onto the stack

Source
push(x : Float64) : Nil

Pushes a Float64 onto the stack

Source
push(x : Float32) : Nil

Pushes a Float32 converted to a Float64 onto the stack

Source
push(x : Int64) : Nil

Pushes an Int64 onto the stack

Source
push(x : Int32) : Nil

Pushes an Int32 converted to an Int64 onto the stack

Source
push(x : String) : Nil

Pushes a String onto the stack

Source
push(x : Char) : Nil

Pushes a Char converted to a String onto the stack

Source
push(x : Symbol) : Nil

Pushes a Symbol converted to a String onto the stack

Source
push(x : LuaCFunction) : Nil

Pushes a C function onto the stack

Source
push(x : Function) : Nil

Pushes a C closure onto the stack

Source
push(x : Bool) : Nil

Pushes a boolean onto the stack

Source
push(x : Pointer(Void)) : Nil

Pushes a light userdata onto the stack

A light userdata is equivalent to a Pointer

Source
push(x : Array) : Nil
Source
push(x : Hash) : Nil
Source
push_fn

Pushes a C function onto the stack

Source
push_fn_closure

Pushes a C closure onto the stack

Source
push_thread(x : LuaState) : ThreadStatus

Pushes thread represented by x onto the stack, returns ThreadStatus

Source
push_value(index : Int32) : Nil

Pushes a copy of the element at index onto the stack

Source
raise_arg_error!(pos : Int32, reason : String) : NoReturn

Raises LuaError at pos with reason

Source
raise_type_error!(pos : Int32, type : String) : NoReturn

Raises LuaError at pos with expected type

Source
raw_eq(index1 : Int32, index2 : Int32) : Bool

Returns true if the two values in indices index1 and index2 are primitively equal

Does not call metamethods

Source
raw_get(index : Int32) : Nil

Similar to #get_table, but does a raw access (i.e., without metamethods)

Source
raw_get_index(index : Int32, n : Int32) : Nil

Pushes onto the stack the value t[n], where t is the value at index, and n is an array-like index

The access is raw; that is, it does not invoke metamethods.

Source
raw_set(index : Int32) : Nil

Similar to #set_table, but does a raw assignment (i.e., without metamethods)

Source
raw_set_index(index : Int32, n : Int32) : Nil

Does the equivalent of t[n] = v, where t is the value at index, v is the value at the top of the stack, and n is an array-like index

Pops the value from the stack.

The assignment is raw; that is, it does not invoke metamethods.

Source
ref(index : Int32) : Int32

https://www.lua.org/manual/5.1/manual.html#luaL_ref

Source
ref_to_h(r : LuaRef) : Hash(String | Float64, LuaAny)

Convert r value to Hash

Source
register(name : String, regs = [] of LuaReg) : Nil

Opens a library with name and registers all functions in regs

Source
register(regs : Array(LuaReg)) : Nil

Registers all functions in regs to table at the top of the stack

Source
register_fn(name : String, &block : Function) : Nil

Registers a named function to table at the top of the stack

Raises LuaError if operation fails

Source
register_fn_global(name : String, &block : Function) : Nil

Registers a global function

Raises LuaError if operation fails

Source
remove(index : Int32) : Nil

Removes the element at index, shifting down elements above to fill gap

WARNING: Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.

Source
remove_ref(r : LuaRef) : Nil

Releases r so that it can be collected

Source
remove_ref(any : LuaAny) : Nil

Same as #remove_ref, but only if any is a LuaRef

Source
remove_refs(hash : Hash(String | Float64, LuaAny)) : Nil

Releases any LuaRef inside hash

Source
replace(index : Int32) : Nil

Moves the top element into the given position (and pops it), without shifting any element (therefore replacing the value at the given position)

Source
set_environment(name : String) : Nil

Pops a value from the stack and sets it as the new value of environment name

Raises LuaError if operation fails

Source
set_fenv(index : Int32) : Int32

Pops a table from the stack and sets it as the new environment for the value at index

If the value at index is neither a function, nor a thread, nor a userdata, returns 0. Otherwise returns 1.

Source
set_field(index : Int32, k : String) : Nil

Does the equivalent to t[k] = v, where t is the value at index, k is the key, and v is the value at top of stack.

Pops the value from the stack.

Raises LuaError if operation fails

Source
set_global(name : String) : Nil

Pops a value from the stack and sets it as the new value of global name

Raises LuaError if operation fails

Source
set_hook(f : LuaHook, mask : Int32, count : Int32) : Nil

Sets the debugging hook function

https://www.lua.org/manual/5.1/manual.html#lua_sethook

Source
set_local(ar : LuaDebug, n : Int32 = 1) : String | Nil

Sets the value of a local variable of ar

https://www.lua.org/manual/5.1/manual.html#lua_setlocal

Source
set_metatable(index : Int32) : Int32

Pops a table from the stack and sets it as the new metatable for the value at index

Source
set_registry(name : String) : Nil

Pops a value from the stack and sets it as the new value of registry name

Raises LuaError if operation fails

Source
set_table(index : Int32) : Nil

Does the equivalent to t[k] = v, where t is the value at index, v is the value at top of stack, and k the value just behind v

Pops both key and value from the stack.

Raises LuaError if operation fails

Source
set_top(index : Int32) : Nil

Accepts any acceptable index, or 0, and sets the stack top to this index

If the new top is larger than the old one, then the new elements are filled with nil.

If index is 0, then all stack elements are removed.

Source
set_upvalue(fn_index : Int32, n : Int32) : String | Nil

Sets the value of a closure's upvalue

https://www.lua.org/manual/5.1/manual.html#lua_setupvalue

Source
size

Returns the index of the top element in the stack

Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack).

Source
size_at(index : Int32) : UInt64

Returns the "length" of the value at index

For strings, this is the string length. For tables, this is the result of the length operator ('#'). For userdata, this is the size of the block of memory allocated for the userdata. For other values, it is 0.

Source
status

Returns the status of self

Source
to_a(index : Int32) : Array(LuaAny)

Converts value at index to Array

Source
to_any?(index : Int32) : LuaAny | Nil

Converts value at index to LuaAny, otherwise returns nil

Source
to_boolean(index : Int32) : Bool

Converts the Lua value at index to Bool

Returns true for any Lua value different than false or nil

Returns false when called with a non-valid index

Source
to_c_function!(index : Int32) : LuaCFunction

Same as #to_c_function?, but assumes value is not nil

Source
to_c_function?(index : Int32) : LuaCFunction | Nil

Converts a value at index to a C function, otherwise returns nil

Source
to_f(index : Int32) : Float64

Converts the Lua value at index to Float64

The value must be a number or a string convertible to a number, otherwise returns 0

Source
to_f32(index : Int32) : Float32

Same as #to_f64 but returns a Float32

Source
to_f64(index : Int32) : Float64

Converts the Lua value at index to Float64

The value must be a number or a string convertible to a number, otherwise returns 0

Source
to_h(index : Int32) : Hash(String | Float64, LuaAny)

Converts value at index to Hash

Source
to_i(index : Int32) : Int32

Same as #to_i64 but returns a Int32

Source
to_i32(index : Int32) : Int32

Same as #to_i64 but returns a Int32

Source
to_i64(index : Int32) : Int64

Converts the Lua value at index to Int64

The value must be a number or a string convertible to a number, otherwise returns 0

If the number is not an integer, it is truncated in some non-specific way

Source
to_pointer!(index : Int32) : Pointer(Void)

Same as #to_pointer?, but assumes value is not nil

Source
to_pointer?(index : Int32) : Pointer(Void) | Nil

Converts the value at index to a Pointer

The value can be a userdata, a table, a thread, or a function; otherwise, returns nil.

Different objects will give different pointers. There is no way to convert the pointer back to its original value.

Source
to_string(index : Int32, size : UInt64) : String

Converts the Lua value at index to a C string

The Lua value must be a string or a number, otherwise returns "".

If the value is a number, it also changes the actual value in the stack to a string.

Source
to_string(index : Int32) : String

Converts the Lua value at index to a C string

The Lua value must be a string or a number, otherwise returns "".

If the value is a number, it also changes the actual value in the stack to a string.

Source
to_thread!(index : Int32) : LuaState

Same as #to_thread?, but assumes value is not nil

Source
to_thread?(index : Int32) : LuaState | Nil

Converts the value at index to a Lua thread, otherwise returns nil

Source
to_unsafe
Source
to_userdata!(index : Int32) : Pointer(Void)

Same as #to_userdata?, but assumes value is not nil

Source
to_userdata?(index : Int32) : Pointer(Void) | Nil

If the value at index is a full userdata, returns its block address

If the value is a light userdata, returns its pointer

Otherwise returns nil

Source
track(ptr : Pointer(Void)) : Nil

Add pointer to be tracked by Crystal

Source
type_name(lua_type : LuaType) : String

Returns the name of the lua_type value

Source
type_name_at(index : Int32) : String

Returns the name of the type of the value at the given index

Source
unref(index : Int32, ref : Int32) : Nil

https://www.lua.org/manual/5.1/manual.html#luaL_unref

Source
untrack(ptr : Pointer(Void)) : Nil

Remove pointer tracked by Crystal

Source
upvalue(index : Int32) : Int32

Produces upvalue indices

Source
version

Returns the version number of this core

Source
xmove(from : LuaState, to : LuaState, n : Int32) : Nil

Exchange values between different threads of the same global state

https://www.lua.org/manual/5.1/manual.html#lua_xmove

Source

Nested types