lua-users home
lua-l archive

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


On Sat, May 21, 2016 at 3:42 AM, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Ulrich Schmidt once stated:
>>
>> Am 21.05.2016 um 09:13 schrieb Sean Conner:
>> >It was thus said that the Great Vadim A. Misbakh-Soloviov once stated:
>> >>>  Mine relies upon strings still having a metatable.
>> >>But lacks luajit ;)
>> >
>> >  Nah.  If version is nil, it's LuaJIT.
>> >
>> >  -spc (nil can't be a table key, see ... )
>>
>> Thats all true. I still like Egors version because it is simple to use.
>> (without nil tests.) and the results can be changed eg.
>>
>> function needs_setfenv()
>>   local f, t = function() return function() end end, {nil,
>>      [false]  = true,  --'5.1',
>>      [true]   = false, --'5.2',
>>      [1/'-0'] = false, --'5.3',
>>      [1]      = true,  --'JIT'
>>      }
>>   return t[1] or t[1/0] or t[f()==f()];
>> end;
>
>   Hmm ... okay, how about:
>
> local version = ({Q="Lua 5.1",R="Lua 5.2",S="Lua 5.3"})[
>         ("").dump(function() end):sub(5,5)] or "LuaJIT"
>
> And you can change it just as easily:
>
> local version = ({Q=true,R=false,S=false})[
>         ("").dump(function() end):sub(5,5)] or true
>
>   -spc (Okay, so a few comments might be nice ... 8-)

After looking at the suggestions in this thread and experimenting with
a few of them, I like the version by Egor in the OP the best. Why?
Because unlike the other suggestions, it works without any environment
at all.

That means that a rock requiring different dependencies depending on
version (such as one requiring a third-party bitwise operations
library on 5.1 but using the native bitwise stuff on 5.2 and later)
should be able to use that test in the rockspec (which luarocks
executes in a completely empty environment) to adjust dependencies on
the fly during installation based on the Lua version it's running
through. That would not be possible with any of the other methods
proposed, because they rely on the standard libraries.