Add default dunder methods and deny operations for unsupported operations
This commit is contained in:
parent
d8cfdd2900
commit
5381de391b
38
oop/oop.lua
38
oop/oop.lua
@ -34,6 +34,38 @@ end
|
||||
local function_def_cache = setmetatable({}, { __mode = "k" })
|
||||
oop._call_stack = {}
|
||||
|
||||
local default_dunders = {
|
||||
"__add",
|
||||
"__sub",
|
||||
"__mul",
|
||||
"__div",
|
||||
"__idiv",
|
||||
"__mod",
|
||||
"__pow",
|
||||
"__unm",
|
||||
"__band",
|
||||
"__bor",
|
||||
"__bxor",
|
||||
"__bnot",
|
||||
"__shl",
|
||||
"__shr",
|
||||
"__concat",
|
||||
"__len",
|
||||
"__eq",
|
||||
"__lt",
|
||||
"__le",
|
||||
"__pairs",
|
||||
"__ipairs",
|
||||
"__call",
|
||||
"__tostring"
|
||||
}
|
||||
|
||||
local function deny_op(name)
|
||||
return function()
|
||||
error("operation not allowed: " .. name, 2)
|
||||
end
|
||||
end
|
||||
|
||||
local function normalize_visibility(visibility)
|
||||
if visibility == nil then
|
||||
return oop.Visibility.PUBLIC
|
||||
@ -263,6 +295,12 @@ function oop.class(def, base)
|
||||
end
|
||||
})
|
||||
|
||||
for _, name in ipairs(default_dunders) do
|
||||
if rawget(cls, name) == nil then
|
||||
cls[name] = deny_op(name)
|
||||
end
|
||||
end
|
||||
|
||||
if type(def) == "string" then
|
||||
cls.__name = def
|
||||
elseif type(def) == "function" then
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user