Skip to content

Instantly share code, notes, and snippets.

@rangercyh
Last active July 1, 2021 02:04
Show Gist options
  • Save rangercyh/588a11610b4d41281bdb5baf5a3d1122 to your computer and use it in GitHub Desktop.
Save rangercyh/588a11610b4d41281bdb5baf5a3d1122 to your computer and use it in GitHub Desktop.
access control
local raw_setmetatable = setmetatable
local setmetatable = function(t, mt)
local raw_t = raw_setmetatable(t, mt)
return raw_setmetatable({}, {
__index = function(raw, k)
if not(mt[k]) then
error("can not visit this member")
end
return function(_, ...)
return raw_t[k](raw_t, ...)
end
end,
__newindex = function(raw, k, v)
error("can not add new member")
end,
})
end
local M = {}
local mt = {}
mt.__index = mt
function mt:f1(m)
return self.a + m
end
function mt:f2()
self.aa = 3
end
function mt:f3()
return self.aa
end
M.new = function()
return setmetatable({
a = 1,
b = 2,
}, mt)
end
return M
local m = require("1")
local t = m.new()
print(t:f1(1))
t:f2()
print(t:f3())
print(t.a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment