Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@un-def
Last active March 5, 2023 11:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save un-def/914b1a93181e43e8a2adc35ad5c9b03a to your computer and use it in GitHub Desktop.
Save un-def/914b1a93181e43e8a2adc35ad5c9b03a to your computer and use it in GitHub Desktop.
A simple function to detect Lua version
local luaversion = function()
if ({false, [1] = true})[1] then -- luacheck: ignore 314
return 'LuaJIT'
elseif 1 / 0 == 1 / '-0' then
return 0 + '0' .. '' == '0' and 'Lua 5.4' or 'Lua 5.3'
end
local f = function() return function() end end
return f() == f() and 'Lua 5.2' or 'Lua 5.1'
end
return {
luaversion = luaversion,
}
#!/bin/sh
if ! command -V hererocks > /dev/null; then
echo 'hererocks not found'
exit 1
fi
echo "using $(hererocks --version)"
echo
tmp=$(mktemp -d)
trap 'rm -r "${tmp}"; trap - EXIT' EXIT INT QUIT TERM HUP
for lua in 'lua 5.1' 'lua 5.2' 'lua 5.3' 'lua 5.4' 'luajit 2.0' 'luajit 2.1'; do
echo "installing ${lua}"
lua_dir="${tmp}/$(echo "${lua}" | tr ' .' '_')"
if ! install_log=$(eval hererocks "--${lua}" "${lua_dir}" 2>&1); then
echo "failed to install ${lua}:"
echo "${install_log}"
exit 1
fi
lua_bin="${lua_dir}/bin/lua"
echo "installed $(${lua_bin} -v 2>&1)"
echo "_VERSION = $("${lua_bin}" -e 'print(_VERSION)')"
echo "luaversion() = $("${lua_bin}" -e 'print(require("luaversion").luaversion())')"
echo
done
@un-def
Copy link
Author

un-def commented Jul 19, 2020

luaversion.lua is based on Egor Skriptunoff's script: http://lua-users.org/lists/lua-l/2016-05/msg00297.html


test.sh output:

using Hererocks 0.22.0

installing lua 5.1
installed Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
_VERSION = Lua 5.1
luaversion() = Lua 5.1

installing lua 5.2
installed Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
_VERSION = Lua 5.2
luaversion() = Lua 5.2

installing lua 5.3
installed Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
_VERSION = Lua 5.3
luaversion() = Lua 5.3

installing lua 5.4
installed Lua 5.4.0  Copyright (C) 1994-2020 Lua.org, PUC-Rio
_VERSION = Lua 5.4
luaversion() = Lua 5.4

installing luajit 2.0
installed LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
_VERSION = Lua 5.1
luaversion() = LuaJIT

installing luajit 2.1
installed LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
_VERSION = Lua 5.1
luaversion() = LuaJIT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment