diff --git a/src/lua/callable.cr b/src/lua/callable.cr index 92f7bcc..17f2df4 100644 --- a/src/lua/callable.cr +++ b/src/lua/callable.cr @@ -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 diff --git a/src/lua/constants.cr b/src/lua/constants.cr index 9bcde32..be3ec9f 100644 --- a/src/lua/constants.cr +++ b/src/lua/constants.cr @@ -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 diff --git a/src/lua/stack/registry.cr b/src/lua/stack/registry.cr index d1ead0e..f31d48b 100644 --- a/src/lua/stack/registry.cr +++ b/src/lua/stack/registry.cr @@ -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