lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hello everyone,

I was wandering around Stack Overflow when came across the message by Egor Skriptunoff titled "A tricky way to determine Lua version":

It's about a "simple program that determines Lua version".

> The curious fact about this program is that it doesn't depend on anything
> that can be changed.
> It does not use any standard library function or any global variable
> (as they may be withdrawn from sandboxed environment).
> It does not rely on the name of "_ENV" upvalue
> (as it can be renamed when Lua is being built).

It was written before the Lua 5.4 release, so I decided to update it.

I ended up with the following function:

    local luaversion = function()
        if ({false, [1] = true})[1] then
            return 'LuaJIT'
        elseif 1 / 0 == 1 / '-0' then
            return 0 + '0' .. '' == '0' and 'Lua 5.4' or 'Lua 5.3'
        local f = function() return function() end end
        return f() == f() and 'Lua 5.2' or 'Lua 5.1'

I rely there on the following incompatibility between Lua 5.3 and 5.4 (quoted from the reference manual):

> [...] unlike in previous versions, the new implementation preserves
> the implicit type of the numeral in the string. For instance, the
> result of "1" + "2" now is an integer, not a float.

I also wrote a shell script to demonstrate how the function works with different Lua versions:

It uses hererocks[1] to install Lua interpreters.