Enhance oop.install function: add support for dynamic class creation via __newindex
This commit is contained in:
parent
83fde73df8
commit
7c7671a15a
19
oop/oop.lua
19
oop/oop.lua
@ -307,6 +307,25 @@ function oop.install(env)
|
|||||||
target.isinstance = oop.isinstance
|
target.isinstance = oop.isinstance
|
||||||
target.issubclass = oop.issubclass
|
target.issubclass = oop.issubclass
|
||||||
target.Visibility = oop.Visibility
|
target.Visibility = oop.Visibility
|
||||||
|
|
||||||
|
local mt = getmetatable(target)
|
||||||
|
if not mt then
|
||||||
|
mt = {}
|
||||||
|
setmetatable(target, mt)
|
||||||
|
end
|
||||||
|
local prev_newindex = mt.__newindex
|
||||||
|
mt.__newindex = function(t, k, v)
|
||||||
|
local val = v
|
||||||
|
if type(k) == "string" and type(v) == "function" and k:match("^[A-Z]") then
|
||||||
|
val = oop.class(v)
|
||||||
|
val.__name = k
|
||||||
|
function_def_cache[v] = val
|
||||||
|
end
|
||||||
|
if prev_newindex then
|
||||||
|
return prev_newindex(t, k, val)
|
||||||
|
end
|
||||||
|
rawset(t, k, val)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
oop.install()
|
oop.install()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user