Skip to content

Instantly share code, notes, and snippets.

@m0tive
Created November 18, 2015 18:27
Show Gist options
  • Save m0tive/c09c3818428e03c194f8 to your computer and use it in GitHub Desktop.
Save m0tive/c09c3818428e03c194f8 to your computer and use it in GitHub Desktop.
Wrap the lua core methods so you don't overwrite them
do
local lua = {}
local core = setmetatable({}, { __index = lua, })
_G = setmetatable(_G, { __index = core })
for k,v in pairs(_G) do
if k ~= "_G" then
lua[k] = v
if type(v) == "table" then
core[k] = setmetatable({}, { __index = lua[k] })
end
_G[k] = nil
end
end
_G.core = core
_G.lua = lua
setmetatable(lua, {
__newindex = function(self, k,v)
return error("Cannot set new items on table 'lua'")
end,
})
getmetatable(_G).__newindex = function(self, k,v)
print("Warning: adding new member to _G: " .. tostring(k))
rawset(_G,k,v)
end
end
table.ext = true
function print(...)
return lua.print("~ ", ...)
end
foo = 2
print(table.ext, core.table.ext, lua.table.ext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment