Last active
July 1, 2021 02:04
-
-
Save rangercyh/588a11610b4d41281bdb5baf5a3d1122 to your computer and use it in GitHub Desktop.
access control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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