Page 1 of 1

lua 5.1

Posted: Sat Jun 30, 2018 4:21 pm
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.

Re: lua 5.1

Posted: Sat Jun 30, 2018 5:13 pm
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/

Re: lua 5.1

Posted: Sat Jun 30, 2018 7:39 pm
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.