Jump to content




Please insert __pairs and __ipairs in metatables


  • You cannot reply to this topic
2 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 23 May 2016 - 04:10 PM

Here the Code (untested):

-- Lists

local metatables = {}  -- Save all metatables (it isn't RAM costly)

-- Save natives

local nativeSetMetatable = setmetatable
local nativePairs = pairs
local nativeiPairs = ipairs

-- Overwrite origins

setmetatable = function(tab, mt)
  local ok, err = pcall(nativeSetMetatable, tab, mt) -- Error providing
  if ok then
	metatables[tab] = mt -- If no error, then save the table and metatable
  else
	return error(err) -- If an error occur, then report it
  end
end

pairs = function(...)
  local tArgs = {...}
  for _, var in ipairs(tArgs) do
	<ERROR PROVIDING>
  end

  if metatables[tArgs[1]] and type(metatables[tArgs[1]].__pairs) == 'function' then -- Check, if the table have an metatable and if __pairs is a function
	return metatables[tArgs[1]].__pairs(...) -- If true, then call pairs
  end
  return nativePairs(...) -- Call native pairs
end

ipairs = function(...)
  local tArgs = {...}
  for _, var in ipairs(tArgs) do
	<ERROR PROVIDING>
  end

  if metatables[tArgs[1]] and type(metatables[tArgs[1]].__ipairs) == 'function' then -- Check, if the table have an metatable and if __ipairs is a function
	return metatables[tArgs[1]].__ipairs(...) -- If true, then call pairs
  end
  return nativeiPairs(...) -- Call native pairs
end

-- End

Edited by Sewbacca, 23 May 2016 - 04:12 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 23 May 2016 - 04:27 PM

Link

#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 25 May 2016 - 06:24 PM

???





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users