Skip to content

Instantly share code, notes, and snippets.

@creationix
Created August 24, 2023 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/b56de2dde4021673f24242258efdcc80 to your computer and use it in GitHub Desktop.
Save creationix/b56de2dde4021673f24242258efdcc80 to your computer and use it in GitHub Desktop.
do
-- Polyfill for luajit without __pairs and __ipairs extensions
local triggered = false
pairs(setmetatable({}, { __pairs = function()
triggered = true
return function() end
end }))
if not triggered then
print "Polyfilling pairs"
function _G.pairs(t)
local mt = getmetatable(t)
if mt and mt.__pairs then
return mt.__pairs(t)
else
return next, t, nil
end
end
end
triggered = false
ipairs(setmetatable({}, { __ipairs = function()
triggered = true
return function() end
end }))
if not triggered then
print "Polyfilling ipairs"
function _G.ipairs(t)
local mt = getmetatable(t)
if mt and mt.__ipairs then
return mt.__ipairs(t)
else
return next, t, nil
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment