class

Lua::Stack

Inherits Lua::StackMixin::FunctionSupport / Lua::StackMixin::ClassSupport / Lua::StackMixin::StandardLibraries / Lua::StackMixin::CoroutineSupport / Lua::StackMixin::ErrorHandling / Lua::StackMixin::TableSupport / Lua::StackMixin::Registry / Lua::StackMixin::Chunk / Lua::StackMixin::Util / Lua::StackMixin::Type / Reference / Object

Constructors

new(state : LibLua::State, libs)

Initializes new Lua stack running in an existed state. Has to be closed to call the corresponding garbage-collection metamethods on Lua side.

Source
new(libs = :all)

Initializes new Lua stack running in a new, independent state. Has to be closed to call the corresponding garbage-collection metamethods on Lua side.

By default it loads all standard libraries. But that's possible to load only a subset of them using libs named parameter. If you pass nil as libs parameter, none of standard libraries will be loaded.

stack = Lua::Stack.new
# ...
stack.close
Source

Instance methods

<<(o)

Adds Crystal object to Lua stack.

Lua::Stack.new.tap do |stack|
  stack << 10
  stack << "str"
  stack << false
end
Source
[](pos : Int)

Fetches value from the stack.

stack = Lua::Stack.new
stack << 10.01
stack << "lua"
stack[1] # => 10.01
stack[2] # => "lua"
Source
close

Destroys all objects in the given Lua state.

stack = Lua::Stack.new
# ...
stack.close
Source
closed?
Source
libs
Source
pop

Removes element from the top of the stack and returns it.

stack = Lua::Stack.new
stack << 10.01
stack.size # => 1
stack.pop  # => 10.01
stack.size # => 0
Source
remove(n : Int = 1)

Removes n elements from the stack.

stack = Lua::Stak.new
stack << "a"
stack << "b"
stack << "c"
stack.remove(2) # => nil
stack.size      # => 1
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; in particular, 0 means an empty stack.

stack = Lua::Stack.new
stack.size # => 0
stack << 10
stack.size # => 1
Source
state
Source
to_s(io : IO)

Represents the stack as a string.

stack = Lua::Stack.new.tap do |s|
  s << 42.24
  s << false
  s << "hello!"
end
stack.to_s # =>

# 3 : TSTRING(string) hello!
# 2 : TBOOLEAN(boolean) false
# 1 : TNUMBER(number) 42.24
Source
top

Returns the top element and does not remove it from the stack.

stack = Lua::Stack.new
stack << "hey"
stack.top  # => "hey"
stack.size # => 0
Source