lua 5.1

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
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

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

v = createType()
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
Party member
Posts: 2605
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.

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

There are good reasons why LÖVE uses LuaJIT.
User avatar
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands

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
         local h = metatable(op).__len
         if h then
           -- call the handler with the operand
           return (h(op))
         else  -- no handler available: default behavior
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: No registered users and 24 guests