struct Luajit::LuaState

Defined in:

luajit/lua_state.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.create : LuaState #

Creates a new LuaState and attaches a default #at_panic handler


[View source]
def self.new(ptr : Pointer(LibLuaJIT::State)) #

[View source]

Class Method Detail

def self.destroy(state : LuaState) : Nil #

Destroys state and gives tracked values back to Crystal


[View source]

Instance Method Detail

def abs_index(index : Int32) : Int32 #

Converts index into an equivalent absolute index


[View source]
def assert_any!(index : Int32) : Nil #

Raises LuaError unless there is a value at index


[View source]
def assert_bool!(index : Int32) : Nil #

Raises LuaError unless the value at index is a boolean


[View source]
def assert_function!(index : Int32) : Nil #

Raises LuaError unless the value at index is a function


[View source]
def assert_integer!(index : Int32) : Nil #

Raises LuaError unless the value at index is a number


[View source]
def assert_light_userdata!(index : Int32) : Nil #

Raises LuaError unless the value at index is a light userdata


[View source]
def assert_nil!(index : Int32) : Nil #

Raises LuaError unless the value at index is nil


[View source]
def assert_number!(index : Int32) : Nil #

Raises LuaError unless the value at index is a number


[View source]
def assert_string!(index : Int32, size : UInt64) : Nil #

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


[View source]
def assert_string!(index : Int32) : Nil #

Raises LuaError unless the value at index is a string


[View source]
def assert_table!(index : Int32) : Nil #

Raises LuaError unless the value at index is a table


[View source]
def assert_thread!(index : Int32) : Nil #

Raises LuaError unless the value at index is a thread


[View source]
def assert_type!(index : Int32, type : LuaType) : Nil #

Raises LuaError unless the value at index is type


[View source]
def assert_userdata!(index : Int32) : Nil #

Raises LuaError unless the value at index is userdata


[View source]
def at_panic(cb : LuaCFunction) : LuaCFunction #

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


[View source]
def at_panic(&block : LuaCFunction) : LuaCFunction #

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


[View source]
def 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


[View source]
def c_pcall(&block : Function) : LuaStatus #

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.


[View source]
def 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.


[View source]
def 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


[View source]
def close : Nil #

Destroys all objects and frees all dynamic memory


[View source]
def 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


[View source]
def co_yield(nresults : Int32) : Int32 #

Yields a coroutine

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


[View source]
def 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


[View source]
def create_ref : LuaRef #

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


[View source]
def 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.


[View source]
def 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


[View source]
def 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


[View source]
def execute(str : String) : LuaStatus #

Loads and runs the given str


[View source]
def execute(path : Path) : LuaStatus #

Loads and runs the given path


[View source]
def execute!(str : String) : Nil #

Loads and runs the given str

Raises LuaError if operation fails


[View source]
def execute!(path : Path) : Nil #

Loads and runs the given path

Raises LuaError if operation fails


[View source]
def gc : LuaGC #

Returns LuaGC wrapper


[View source]
def get_environment(name : String) : Nil #

Pushes onto the stack the value of the environment name

Raises LuaError if operation fails


[View source]
def get_fenv(index : Int32) : Nil #

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


[View source]
def 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


[View source]
def get_global(name : String) : Nil #

Pushes onto the stack the value of the global name

Raises LuaError if operation fails


[View source]
def get_hook : LuaHook #

Returns the current hook function


[View source]
def get_hook_count : Int32 #

Returns the current hook count


[View source]
def get_hook_mask : Int32 #

Returns the current hook mask


[View source]
def 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


[View source]
def 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


[View source]
def 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.


[View source]
def 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


[View source]
def 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.


[View source]
def get_ref_value(r : LuaRef) : Nil #

Retrieves ref value for r


[View source]
def get_registry(name : String) : Nil #

Pushes onto the stack the value of the registry name

Raises LuaError if operation fails


[View source]
def get_stack(level : Int32) : LuaDebug | Nil #

Get information about the interpreter runtime stack

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


[View source]
def 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


[View source]
def get_type(index : Int32) : LuaType #

Returns the type of the value at the given index


[View source]
def 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


[View source]
def 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


[View source]
def 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.


[View source]
def is_bool?(index : Int32) : Bool #

Returns true if value at the given index has type boolean


[View source]
def is_c_function?(index : Int32) : Bool #

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


[View source]
def is_function?(index : Int32) : Bool #

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


[View source]
def is_light_userdata?(index : Int32) : Bool #

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


[View source]
def is_nil?(index : Int32) : Bool #

Returns true if value at the given index is nil


[View source]
def is_none?(index : Int32) : Bool #

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


[View source]
def 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


[View source]
def is_number?(index : Int32) : Bool #

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


[View source]
def is_pseudo?(index : Int32) : Bool #

Returns true if index is a pseudo-index


[View source]
def 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)


[View source]
def is_table?(index : Int32) : Bool #

Returns true if value at the given index is a table


[View source]
def is_thread?(index : Int32) : Bool #

Returns true if value at the given index is a thread


[View source]
def is_userdata?(index : Int32) : Bool #

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


[View source]
def 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


[View source]
def 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.


[View source]
def new_table : Nil #

Creates a new empty table and pushes it onto the stack


[View source]
def new_thread : LuaState #

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.


[View source]
def 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.


[View source]
def 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.


[View source]
def 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

[View source]
def open_library(type : LuaLibrary) : Nil #

Opens standard Lua library type


[View source]
def 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


[View source]
def pcall(nargs : Int32, nresults : Int32, errfunc : Int32 = 0, & : LuaStatus -> ) : LuaStatus #

Same as #pcall, but yields LuaStatus on failure


[View source]
def pop(n : Int32) : Nil #

Pops n elements from the stack


[View source]
def push(_x : Nil) : Nil #

Pushes a nil value onto the stack


[View source]
def push(x : Float64) : Nil #

Pushes a Float64 onto the stack


[View source]
def push(x : Float32) : Nil #

Pushes a Float32 converted to a Float64 onto the stack


[View source]
def push(x : Int64) : Nil #

Pushes an Int64 onto the stack


[View source]
def push(x : Int32) : Nil #

Pushes an Int32 converted to an Int64 onto the stack


[View source]
def push(x : String) : Nil #

Pushes a String onto the stack


[View source]
def push(x : Char) : Nil #

Pushes a Char converted to a String onto the stack


[View source]
def push(x : Symbol) : Nil #

Pushes a Symbol converted to a String onto the stack


[View source]
def push(x : LuaCFunction) : Nil #

Pushes a C function onto the stack


[View source]
def push(x : Function) : Nil #

Pushes a C closure onto the stack


[View source]
def push(x : Bool) : Nil #

Pushes a boolean onto the stack


[View source]
def push(x : Pointer(Void)) : Nil #

Pushes a light userdata onto the stack

A light userdata is equivalent to a Pointer


[View source]
def push(x : Array) : Nil #

[View source]
def push(x : Hash) : Nil #

[View source]
def push_fn(&block : LuaCFunction) : Nil #

Pushes a C function onto the stack


[View source]
def push_fn_closure(&block : Function) : Nil #

Pushes a C closure onto the stack


[View source]
def push_thread(x : LuaState) : ThreadStatus #

Pushes thread represented by x onto the stack, returns ThreadStatus


[View source]
def push_value(index : Int32) : Nil #

Pushes a copy of the element at index onto the stack


[View source]
def raise_arg_error!(pos : Int32, reason : String) : NoReturn #

Raises LuaError at pos with reason


[View source]
def raise_type_error!(pos : Int32, type : String) : NoReturn #

Raises LuaError at pos with expected type


[View source]
def 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


[View source]
def raw_get(index : Int32) : Nil #

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


[View source]
def 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.


[View source]
def raw_set(index : Int32) : Nil #

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


[View source]
def 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.


[View source]
def ref(index : Int32) : Int32 #

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


[View source]
def ref_to_h(r : LuaRef) : Hash(String | Float64, LuaAny) #

Convert r value to Hash


[View source]
def register(name : String, regs = [] of LuaReg) : Nil #

Opens a library with name and registers all functions in regs


[View source]
def register(regs : Array(LuaReg)) : Nil #

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


[View source]
def register_fn(name : String, &block : Function) : Nil #

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

Raises LuaError if operation fails


[View source]
def register_fn_global(name : String, &block : Function) : Nil #

Registers a global function

Raises LuaError if operation fails


[View source]
def 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.


[View source]
def remove_ref(r : LuaRef) : Nil #

Releases r so that it can be collected


[View source]
def remove_ref(any : LuaAny) : Nil #

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


[View source]
def remove_refs(hash : Hash(String | Float64, LuaAny)) : Nil #

Releases any LuaRef inside hash


[View source]
def 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)


[View source]
def 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


[View source]
def 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.


[View source]
def 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


[View source]
def 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


[View source]
def 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


[View source]
def 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


[View source]
def set_metatable(index : Int32) : Int32 #

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


[View source]
def 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


[View source]
def 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


[View source]
def 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.


[View source]
def 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


[View source]
def size : Int32 #

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).


[View source]
def 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.


[View source]
def status : LuaStatus #

Returns the status of self


[View source]
def to_a(index : Int32) : Array(LuaAny) #

Converts value at index to Array


[View source]
def to_any?(index : Int32) : LuaAny | Nil #

Converts value at index to LuaAny, otherwise returns nil


[View source]
def 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


[View source]
def to_c_function!(index : Int32) : LuaCFunction #

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


[View source]
def to_c_function?(index : Int32) : LuaCFunction | Nil #

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


[View source]
def 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


[View source]
def to_f32(index : Int32) : Float32 #

Same as #to_f64 but returns a Float32


[View source]
def 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


[View source]
def to_h(index : Int32) : Hash(String | Float64, LuaAny) #

Converts value at index to Hash


[View source]
def to_i(index : Int32) : Int32 #

Same as #to_i64 but returns a Int32


[View source]
def to_i32(index : Int32) : Int32 #

Same as #to_i64 but returns a Int32


[View source]
def 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


[View source]
def to_pointer!(index : Int32) : Pointer(Void) #

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


[View source]
def 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.


[View source]
def 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.


[View source]
def 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.


[View source]
def to_thread!(index : Int32) : LuaState #

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


[View source]
def to_thread?(index : Int32) : LuaState | Nil #

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


[View source]
def to_unsafe : Pointer(Void) #

[View source]
def to_userdata!(index : Int32) : Pointer(Void) #

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


[View source]
def 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


[View source]
def track(ptr : Pointer(Void)) : Nil #

Add pointer to be tracked by Crystal


[View source]
def type_name(lua_type : LuaType) : String #

Returns the name of the lua_type value


[View source]
def type_name_at(index : Int32) : String #

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


[View source]
def unref(index : Int32, ref : Int32) : Nil #

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


[View source]
def untrack(ptr : Pointer(Void)) : Nil #

Remove pointer tracked by Crystal


[View source]
def upvalue(index : Int32) : Int32 #

Produces upvalue indices


[View source]
def version : Float64 #

Returns the version number of this core


[View source]
def 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


[View source]