lua 5.1

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
wazoowazoo
Prole
Posts: 16
Joined: Sun Jan 22, 2017 2:47 pm

lua 5.1

Post by wazoowazoo »

I have noticed an error in lua 5.1, it happens when setting the __len method in a metatable.
Lets imagine I have written this code :

Code: Select all

meta = {}
function meta.__len(v)
	return 782
end

function createType()
	return setmetatable({}, meta)
end

v = createType()
print(#v)	
this code will return 0 and not 782 as expected.

So I am asking if there are any plans of updating the lua version in future.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: lua 5.1

Post by pgimeno »

It's not enabled by default, but I believe you can enable it if you compile LÖVE from sources. http://luajit.org/extensions.html

Perhaps it would be nice if some future version enables 5.2 extensions by default.

There are good reasons why LÖVE uses LuaJIT. https://github.com/trizen/language-benchmarks https://attractivechaos.github.io/plb/
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: lua 5.1

Post by bartbes »

Note that the lua 5.1 docs specify the following behaviour for the length operator:

Code: Select all

     function len_event (op)
       if type(op) == "string" then
         return strlen(op)         -- primitive string length
       elseif type(op) == "table" then
         return #op                -- primitive table length
       else
         local h = metatable(op).__len
         if h then
           -- call the handler with the operand
           return (h(op))
         else  -- no handler available: default behavior
           error(···)
         end
       end
     end
That is, for tables (and strings) the # operator is a primitive operation, and the method does not get called. Lua 5.2 enabled using __len on tables.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 56 guests