Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lua/callable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ module LuaCallable
end

def self.__call(state : LibLua::State) : Int32
data = LibLua.topointer(state, Lua::REGISTRYINDEX - 1) # lua_upvalueindex(1)
ptr = LibLua.topointer(state, Lua::REGISTRYINDEX - 2) # lua_upvalueindex(2)
data = LibLua.topointer(state, Lua.registry_index - 1) # lua_upvalueindex(1)
ptr = LibLua.topointer(state, Lua.registry_index - 2) # lua_upvalueindex(2)
proc = Proc(LibLua::State, Int32).new(ptr, data)
proc.call(state)
end
Expand Down
18 changes: 15 additions & 3 deletions src/lua/constants.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@ module Lua
ERRFILE = 6
end

REFNIL = -1
NOREF = -2
REGISTRYINDEX = -(Int32::MAX // 2 + 1000)
REFNIL = -1
NOREF = -2

# Detect REGISTRYINDEX from the linked Lua version.
# Lua 5.4: -(1_000_000 + 1000), Lua 5.5+: -(INT_MAX/2 + 1000)
@@registry_index : Int32 = begin
state = LibLua.l_newstate
ver = LibLua.version(state)
LibLua.close(state)
ver >= 505 ? -(Int32::MAX // 2 + 1000) : -1_001_000
end

def self.registry_index : Int32
@@registry_index
end

MULTRET = -1

Expand Down
6 changes: 3 additions & 3 deletions src/lua/stack/registry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ module Lua
# and removes it from the stack. Returns a reference.
def reference(pos)
LibLua.pushvalue(@state, pos)
LibLua.l_ref(@state, Lua::REGISTRYINDEX)
LibLua.l_ref(@state, Lua.registry_index)
end

# Retrieves an object referred by reference.
def rawgeti(ref)
TYPE.new LibLua.rawgeti(@state, Lua::REGISTRYINDEX, ref)
TYPE.new LibLua.rawgeti(@state, Lua.registry_index, ref)
end

# Frees a reference and its associated object.
def unref(ref)
LibLua.l_unref(@state, Lua::REGISTRYINDEX, ref)
LibLua.l_unref(@state, Lua.registry_index, ref)
end
end
end
Loading