class Lua::Stack

Included Modules

Defined in:

lua/stack.cr

Constructors

Instance Method Summary

Instance methods inherited from module Lua::StackMixin::ClassSupport

pushclosure(proc : Proc) pushclosure, pushmetatable(type : LuaCallable.class) pushmetatable, pushobject(a : LuaCallable) pushobject

Instance methods inherited from module Lua::StackMixin::StandardLibraries

open_libs(libraries) open_libs

Macros inherited from module Lua::StackMixin::StandardLibraries

open_library(library) open_library

Instance methods inherited from module Lua::StackMixin::CoroutineSupport

newthread(f : Function) newthread, status status

Instance methods inherited from module Lua::StackMixin::TableSupport

pushtable(a : Array)
pushtable(a : Hash)
pushtable

Instance methods inherited from module Lua::StackMixin::Registry

rawgeti(ref) rawgeti, reference(pos) reference, unref(ref) unref

Instance methods inherited from module Lua::StackMixin::Chunk

run(buff : String, name : String | Nil = nil)
run(lua_file : File)
run

Instance methods inherited from module Lua::StackMixin::Util

get_global(name : String) get_global, getinfo(what) getinfo, set_global(name : String, value) set_global, version version

Instance methods inherited from module Lua::StackMixin::Type

crystal_base_type_at(pos : Int) crystal_base_type_at, crystal_type_at(pos : Int) crystal_type_at, crystal_type_info_at(pos : Int) crystal_type_info_at, type_at(pos : Int) type_at, typename(pos : Int)
typename(type : TYPE)
typename

Constructor Detail

def self.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.


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

[View source]

Instance Method Detail

def <<(o) #

Adds Crystal object to Lua stack.

Lua::Stack.new.tap do |stack|
  stack << 10
  stack << "str"
  stack << false
end

[View source]
def [](pos : Int) #

Fetches value from the stack.

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

[View source]
def close #

Destroys all objects in the given Lua state.

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

[View source]
def closed? : Bool #

[View source]
def libs : Set(Symbol) #

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

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

[View source]
def size : Int #

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

[View source]
def state : LibLua::State #

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

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

[View source]