Add default dunder methods and deny operations for unsupported operations

This commit is contained in:
Chipperfluff 2026-01-01 18:03:25 +01:00
parent d8cfdd2900
commit 5381de391b

View File

@ -34,6 +34,38 @@ end
local function_def_cache = setmetatable({}, { __mode = "k" }) local function_def_cache = setmetatable({}, { __mode = "k" })
oop._call_stack = {} 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) local function normalize_visibility(visibility)
if visibility == nil then if visibility == nil then
return oop.Visibility.PUBLIC return oop.Visibility.PUBLIC
@ -263,6 +295,12 @@ function oop.class(def, base)
end 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 if type(def) == "string" then
cls.__name = def cls.__name = def
elseif type(def) == "function" then elseif type(def) == "function" then